2 * Atmel MACB Ethernet Controller driver
4 * Copyright (C) 2004-2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/mii.h>
21 #include <linux/mutex.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/ethtool.h>
24 #include <linux/platform_device.h>
26 #include <asm/arch/board.h>
30 #define RX_BUFFER_SIZE 128
31 #define RX_RING_SIZE 512
32 #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
34 /* Make the IP header word-aligned (the ethernet header is 14 bytes) */
37 #define TX_RING_SIZE 128
38 #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
39 #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
41 #define TX_RING_GAP(bp) \
42 (TX_RING_SIZE - (bp)->tx_pending)
43 #define TX_BUFFS_AVAIL(bp) \
44 (((bp)->tx_tail <= (bp)->tx_head) ? \
45 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
46 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
47 #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
49 #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
51 /* minimum number of free TX descriptors before waking up TX process */
52 #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
54 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
57 static void __macb_set_hwaddr(struct macb *bp)
62 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
63 macb_writel(bp, SA1B, bottom);
64 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
65 macb_writel(bp, SA1T, top);
68 static void __init macb_get_hwaddr(struct macb *bp)
74 bottom = macb_readl(bp, SA1B);
75 top = macb_readl(bp, SA1T);
77 addr[0] = bottom & 0xff;
78 addr[1] = (bottom >> 8) & 0xff;
79 addr[2] = (bottom >> 16) & 0xff;
80 addr[3] = (bottom >> 24) & 0xff;
82 addr[5] = (top >> 8) & 0xff;
84 if (is_valid_ether_addr(addr))
85 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
88 static void macb_enable_mdio(struct macb *bp)
93 spin_lock_irqsave(&bp->lock, flags);
94 reg = macb_readl(bp, NCR);
96 macb_writel(bp, NCR, reg);
97 macb_writel(bp, IER, MACB_BIT(MFD));
98 spin_unlock_irqrestore(&bp->lock, flags);
101 static void macb_disable_mdio(struct macb *bp)
106 spin_lock_irqsave(&bp->lock, flags);
107 reg = macb_readl(bp, NCR);
108 reg &= ~MACB_BIT(MPE);
109 macb_writel(bp, NCR, reg);
110 macb_writel(bp, IDR, MACB_BIT(MFD));
111 spin_unlock_irqrestore(&bp->lock, flags);
114 static int macb_mdio_read(struct net_device *dev, int phy_id, int location)
116 struct macb *bp = netdev_priv(dev);
119 mutex_lock(&bp->mdio_mutex);
121 macb_enable_mdio(bp);
122 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
123 | MACB_BF(RW, MACB_MAN_READ)
124 | MACB_BF(PHYA, phy_id)
125 | MACB_BF(REGA, location)
126 | MACB_BF(CODE, MACB_MAN_CODE)));
128 wait_for_completion(&bp->mdio_complete);
130 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
131 macb_disable_mdio(bp);
132 mutex_unlock(&bp->mdio_mutex);
137 static void macb_mdio_write(struct net_device *dev, int phy_id,
138 int location, int val)
140 struct macb *bp = netdev_priv(dev);
142 dev_dbg(&bp->pdev->dev, "mdio_write %02x:%02x <- %04x\n",
143 phy_id, location, val);
145 mutex_lock(&bp->mdio_mutex);
146 macb_enable_mdio(bp);
148 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
149 | MACB_BF(RW, MACB_MAN_WRITE)
150 | MACB_BF(PHYA, phy_id)
151 | MACB_BF(REGA, location)
152 | MACB_BF(CODE, MACB_MAN_CODE)
153 | MACB_BF(DATA, val)));
155 wait_for_completion(&bp->mdio_complete);
157 macb_disable_mdio(bp);
158 mutex_unlock(&bp->mdio_mutex);
161 static int macb_phy_probe(struct macb *bp)
166 for (phy_address = 0; phy_address < 32; phy_address++) {
167 phyid1 = macb_mdio_read(bp->dev, phy_address, MII_PHYSID1);
168 phyid2 = macb_mdio_read(bp->dev, phy_address, MII_PHYSID2);
170 if (phyid1 != 0xffff && phyid1 != 0x0000
171 && phyid2 != 0xffff && phyid2 != 0x0000)
175 if (phy_address == 32)
178 dev_info(&bp->pdev->dev,
179 "detected PHY at address %d (ID %04x:%04x)\n",
180 phy_address, phyid1, phyid2);
182 bp->mii.phy_id = phy_address;
186 static void macb_set_media(struct macb *bp, int media)
190 spin_lock_irq(&bp->lock);
191 reg = macb_readl(bp, NCFGR);
192 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
193 if (media & (ADVERTISE_100HALF | ADVERTISE_100FULL))
194 reg |= MACB_BIT(SPD);
195 if (media & ADVERTISE_FULL)
197 macb_writel(bp, NCFGR, reg);
198 spin_unlock_irq(&bp->lock);
201 static void macb_check_media(struct macb *bp, int ok_to_print, int init_media)
203 struct mii_if_info *mii = &bp->mii;
204 unsigned int old_carrier, new_carrier;
205 int advertise, lpa, media, duplex;
207 /* if forced media, go no further */
208 if (mii->force_media)
211 /* check current and old link status */
212 old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0;
213 new_carrier = (unsigned int) mii_link_ok(mii);
215 /* if carrier state did not change, assume nothing else did */
216 if (!init_media && old_carrier == new_carrier)
219 /* no carrier, nothing much to do */
221 netif_carrier_off(mii->dev);
222 printk(KERN_INFO "%s: link down\n", mii->dev->name);
227 * we have carrier, see who's on the other end
229 netif_carrier_on(mii->dev);
231 /* get MII advertise and LPA values */
232 if (!init_media && mii->advertising) {
233 advertise = mii->advertising;
235 advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
236 mii->advertising = advertise;
238 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
240 /* figure out media and duplex from advertise and LPA values */
241 media = mii_nway_result(lpa & advertise);
242 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
245 printk(KERN_INFO "%s: link up, %sMbps, %s-duplex, lpa 0x%04X\n",
247 media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? "100" : "10",
248 duplex ? "full" : "half", lpa);
250 mii->full_duplex = duplex;
252 /* Let the MAC know about the new link state */
253 macb_set_media(bp, media);
256 static void macb_update_stats(struct macb *bp)
258 u32 __iomem *reg = bp->regs + MACB_PFR;
259 u32 *p = &bp->hw_stats.rx_pause_frames;
260 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
262 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
264 for(; p < end; p++, reg++)
265 *p += __raw_readl(reg);
268 static void macb_periodic_task(struct work_struct *work)
270 struct macb *bp = container_of(work, struct macb, periodic_task.work);
272 macb_update_stats(bp);
273 macb_check_media(bp, 1, 0);
275 schedule_delayed_work(&bp->periodic_task, HZ);
278 static void macb_tx(struct macb *bp)
284 status = macb_readl(bp, TSR);
285 macb_writel(bp, TSR, status);
287 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
288 (unsigned long)status);
290 if (status & MACB_BIT(UND)) {
291 printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
293 bp->tx_head = bp->tx_tail = 0;
296 if (!(status & MACB_BIT(COMP)))
298 * This may happen when a buffer becomes complete
299 * between reading the ISR and scanning the
300 * descriptors. Nothing to worry about.
305 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
306 struct ring_info *rp = &bp->tx_skb[tail];
307 struct sk_buff *skb = rp->skb;
313 bufstat = bp->tx_ring[tail].ctrl;
315 if (!(bufstat & MACB_BIT(TX_USED)))
318 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
320 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
322 bp->stats.tx_packets++;
323 bp->stats.tx_bytes += skb->len;
325 dev_kfree_skb_irq(skb);
329 if (netif_queue_stopped(bp->dev) &&
330 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
331 netif_wake_queue(bp->dev);
334 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
335 unsigned int last_frag)
339 unsigned int offset = 0;
342 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
344 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
345 first_frag, last_frag, len);
347 skb = dev_alloc_skb(len + RX_OFFSET);
349 bp->stats.rx_dropped++;
350 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
351 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
352 if (frag == last_frag)
359 skb_reserve(skb, RX_OFFSET);
361 skb->ip_summed = CHECKSUM_NONE;
364 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
365 unsigned int frag_len = RX_BUFFER_SIZE;
367 if (offset + frag_len > len) {
368 BUG_ON(frag != last_frag);
369 frag_len = len - offset;
371 memcpy(skb->data + offset,
372 bp->rx_buffers + (RX_BUFFER_SIZE * frag),
374 offset += RX_BUFFER_SIZE;
375 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
378 if (frag == last_frag)
382 skb->protocol = eth_type_trans(skb, bp->dev);
384 bp->stats.rx_packets++;
385 bp->stats.rx_bytes += len;
386 bp->dev->last_rx = jiffies;
387 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
388 skb->len, skb->csum);
389 netif_receive_skb(skb);
394 /* Mark DMA descriptors from begin up to and not including end as unused */
395 static void discard_partial_frame(struct macb *bp, unsigned int begin,
400 for (frag = begin; frag != end; frag = NEXT_RX(frag))
401 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
405 * When this happens, the hardware stats registers for
406 * whatever caused this is updated, so we don't have to record
411 static int macb_rx(struct macb *bp, int budget)
414 unsigned int tail = bp->rx_tail;
417 for (; budget > 0; tail = NEXT_RX(tail)) {
421 addr = bp->rx_ring[tail].addr;
422 ctrl = bp->rx_ring[tail].ctrl;
424 if (!(addr & MACB_BIT(RX_USED)))
427 if (ctrl & MACB_BIT(RX_SOF)) {
428 if (first_frag != -1)
429 discard_partial_frame(bp, first_frag, tail);
433 if (ctrl & MACB_BIT(RX_EOF)) {
435 BUG_ON(first_frag == -1);
437 dropped = macb_rx_frame(bp, first_frag, tail);
446 if (first_frag != -1)
447 bp->rx_tail = first_frag;
454 static int macb_poll(struct net_device *dev, int *budget)
456 struct macb *bp = netdev_priv(dev);
457 int orig_budget, work_done, retval = 0;
460 status = macb_readl(bp, RSR);
461 macb_writel(bp, RSR, status);
465 * This may happen if an interrupt was pending before
466 * this function was called last time, and no packets
467 * have been received since.
469 netif_rx_complete(dev);
473 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
474 (unsigned long)status, *budget);
476 if (!(status & MACB_BIT(REC))) {
477 dev_warn(&bp->pdev->dev,
478 "No RX buffers complete, status = %02lx\n",
479 (unsigned long)status);
480 netif_rx_complete(dev);
484 orig_budget = *budget;
485 if (orig_budget > dev->quota)
486 orig_budget = dev->quota;
488 work_done = macb_rx(bp, orig_budget);
489 if (work_done < orig_budget) {
490 netif_rx_complete(dev);
497 * We've done what we can to clean the buffers. Make sure we
498 * get notified when new packets arrive.
501 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
503 /* TODO: Handle errors */
508 static irqreturn_t macb_interrupt(int irq, void *dev_id)
510 struct net_device *dev = dev_id;
511 struct macb *bp = netdev_priv(dev);
514 status = macb_readl(bp, ISR);
516 if (unlikely(!status))
519 spin_lock(&bp->lock);
522 if (status & MACB_BIT(MFD))
523 complete(&bp->mdio_complete);
525 /* close possible race with dev_close */
526 if (unlikely(!netif_running(dev))) {
527 macb_writel(bp, IDR, ~0UL);
531 if (status & MACB_RX_INT_FLAGS) {
532 if (netif_rx_schedule_prep(dev)) {
534 * There's no point taking any more interrupts
535 * until we have processed the buffers
537 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
538 dev_dbg(&bp->pdev->dev, "scheduling RX softirq\n");
539 __netif_rx_schedule(dev);
543 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
547 * Link change detection isn't possible with RMII, so we'll
548 * add that if/when we get our hands on a full-blown MII PHY.
551 if (status & MACB_BIT(HRESP)) {
553 * TODO: Reset the hardware, and maybe move the printk
554 * to a lower-priority context as well (work queue?)
556 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
560 status = macb_readl(bp, ISR);
563 spin_unlock(&bp->lock);
568 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
570 struct macb *bp = netdev_priv(dev);
572 unsigned int len, entry;
577 dev_dbg(&bp->pdev->dev,
578 "start_xmit: len %u head %p data %p tail %p end %p\n",
579 skb->len, skb->head, skb->data, skb->tail, skb->end);
580 dev_dbg(&bp->pdev->dev,
582 for (i = 0; i < 16; i++)
583 printk(" %02x", (unsigned int)skb->data[i]);
588 spin_lock_irq(&bp->lock);
590 /* This is a hard error, log it. */
591 if (TX_BUFFS_AVAIL(bp) < 1) {
592 netif_stop_queue(dev);
593 spin_unlock_irq(&bp->lock);
594 dev_err(&bp->pdev->dev,
595 "BUG! Tx Ring full when queue awake!\n");
596 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
597 bp->tx_head, bp->tx_tail);
602 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
603 mapping = dma_map_single(&bp->pdev->dev, skb->data,
605 bp->tx_skb[entry].skb = skb;
606 bp->tx_skb[entry].mapping = mapping;
607 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
608 skb->data, (unsigned long)mapping);
610 ctrl = MACB_BF(TX_FRMLEN, len);
611 ctrl |= MACB_BIT(TX_LAST);
612 if (entry == (TX_RING_SIZE - 1))
613 ctrl |= MACB_BIT(TX_WRAP);
615 bp->tx_ring[entry].addr = mapping;
616 bp->tx_ring[entry].ctrl = ctrl;
619 entry = NEXT_TX(entry);
622 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
624 if (TX_BUFFS_AVAIL(bp) < 1)
625 netif_stop_queue(dev);
627 spin_unlock_irq(&bp->lock);
629 dev->trans_start = jiffies;
634 static void macb_free_consistent(struct macb *bp)
641 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
642 bp->rx_ring, bp->rx_ring_dma);
646 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
647 bp->tx_ring, bp->tx_ring_dma);
650 if (bp->rx_buffers) {
651 dma_free_coherent(&bp->pdev->dev,
652 RX_RING_SIZE * RX_BUFFER_SIZE,
653 bp->rx_buffers, bp->rx_buffers_dma);
654 bp->rx_buffers = NULL;
658 static int macb_alloc_consistent(struct macb *bp)
662 size = TX_RING_SIZE * sizeof(struct ring_info);
663 bp->tx_skb = kmalloc(size, GFP_KERNEL);
667 size = RX_RING_BYTES;
668 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
669 &bp->rx_ring_dma, GFP_KERNEL);
672 dev_dbg(&bp->pdev->dev,
673 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
674 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
676 size = TX_RING_BYTES;
677 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
678 &bp->tx_ring_dma, GFP_KERNEL);
681 dev_dbg(&bp->pdev->dev,
682 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
683 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
685 size = RX_RING_SIZE * RX_BUFFER_SIZE;
686 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
687 &bp->rx_buffers_dma, GFP_KERNEL);
690 dev_dbg(&bp->pdev->dev,
691 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
692 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
697 macb_free_consistent(bp);
701 static void macb_init_rings(struct macb *bp)
706 addr = bp->rx_buffers_dma;
707 for (i = 0; i < RX_RING_SIZE; i++) {
708 bp->rx_ring[i].addr = addr;
709 bp->rx_ring[i].ctrl = 0;
710 addr += RX_BUFFER_SIZE;
712 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
714 for (i = 0; i < TX_RING_SIZE; i++) {
715 bp->tx_ring[i].addr = 0;
716 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
718 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
720 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
723 static void macb_reset_hw(struct macb *bp)
725 /* Make sure we have the write buffer for ourselves */
729 * Disable RX and TX (XXX: Should we halt the transmission
732 macb_writel(bp, NCR, 0);
734 /* Clear the stats registers (XXX: Update stats first?) */
735 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
737 /* Clear all status flags */
738 macb_writel(bp, TSR, ~0UL);
739 macb_writel(bp, RSR, ~0UL);
741 /* Disable all interrupts */
742 macb_writel(bp, IDR, ~0UL);
746 static void macb_init_hw(struct macb *bp)
751 __macb_set_hwaddr(bp);
753 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
754 config |= MACB_BIT(PAE); /* PAuse Enable */
755 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
756 if (bp->dev->flags & IFF_PROMISC)
757 config |= MACB_BIT(CAF); /* Copy All Frames */
758 if (!(bp->dev->flags & IFF_BROADCAST))
759 config |= MACB_BIT(NBC); /* No BroadCast */
760 macb_writel(bp, NCFGR, config);
762 /* Initialize TX and RX buffers */
763 macb_writel(bp, RBQP, bp->rx_ring_dma);
764 macb_writel(bp, TBQP, bp->tx_ring_dma);
766 /* Enable TX and RX */
767 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE));
769 /* Enable interrupts */
770 macb_writel(bp, IER, (MACB_BIT(RCOMP)
780 static void macb_init_phy(struct net_device *dev)
782 struct macb *bp = netdev_priv(dev);
784 /* Set some reasonable default settings */
785 macb_mdio_write(dev, bp->mii.phy_id, MII_ADVERTISE,
786 ADVERTISE_CSMA | ADVERTISE_ALL);
787 macb_mdio_write(dev, bp->mii.phy_id, MII_BMCR,
788 (BMCR_SPEED100 | BMCR_ANENABLE
789 | BMCR_ANRESTART | BMCR_FULLDPLX));
792 static int macb_open(struct net_device *dev)
794 struct macb *bp = netdev_priv(dev);
797 dev_dbg(&bp->pdev->dev, "open\n");
799 if (!is_valid_ether_addr(dev->dev_addr))
800 return -EADDRNOTAVAIL;
802 err = macb_alloc_consistent(bp);
805 "%s: Unable to allocate DMA memory (error %d)\n",
814 macb_check_media(bp, 1, 1);
815 netif_start_queue(dev);
817 schedule_delayed_work(&bp->periodic_task, HZ);
822 static int macb_close(struct net_device *dev)
824 struct macb *bp = netdev_priv(dev);
827 cancel_rearming_delayed_work(&bp->periodic_task);
829 netif_stop_queue(dev);
831 spin_lock_irqsave(&bp->lock, flags);
833 netif_carrier_off(dev);
834 spin_unlock_irqrestore(&bp->lock, flags);
836 macb_free_consistent(bp);
841 static struct net_device_stats *macb_get_stats(struct net_device *dev)
843 struct macb *bp = netdev_priv(dev);
844 struct net_device_stats *nstat = &bp->stats;
845 struct macb_stats *hwstat = &bp->hw_stats;
847 /* Convert HW stats into netdevice stats */
848 nstat->rx_errors = (hwstat->rx_fcs_errors +
849 hwstat->rx_align_errors +
850 hwstat->rx_resource_errors +
851 hwstat->rx_overruns +
852 hwstat->rx_oversize_pkts +
854 hwstat->rx_undersize_pkts +
855 hwstat->sqe_test_errors +
856 hwstat->rx_length_mismatch);
857 nstat->tx_errors = (hwstat->tx_late_cols +
858 hwstat->tx_excessive_cols +
859 hwstat->tx_underruns +
860 hwstat->tx_carrier_errors);
861 nstat->collisions = (hwstat->tx_single_cols +
862 hwstat->tx_multiple_cols +
863 hwstat->tx_excessive_cols);
864 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
866 hwstat->rx_undersize_pkts +
867 hwstat->rx_length_mismatch);
868 nstat->rx_over_errors = hwstat->rx_resource_errors;
869 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
870 nstat->rx_frame_errors = hwstat->rx_align_errors;
871 nstat->rx_fifo_errors = hwstat->rx_overruns;
872 /* XXX: What does "missed" mean? */
873 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
874 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
875 nstat->tx_fifo_errors = hwstat->tx_underruns;
876 /* Don't know about heartbeat or window errors... */
881 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
883 struct macb *bp = netdev_priv(dev);
885 return mii_ethtool_gset(&bp->mii, cmd);
888 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
890 struct macb *bp = netdev_priv(dev);
892 return mii_ethtool_sset(&bp->mii, cmd);
895 static void macb_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
897 struct macb *bp = netdev_priv(dev);
899 strcpy(info->driver, bp->pdev->dev.driver->name);
900 strcpy(info->version, "$Revision: 1.14 $");
901 strcpy(info->bus_info, bp->pdev->dev.bus_id);
904 static int macb_nway_reset(struct net_device *dev)
906 struct macb *bp = netdev_priv(dev);
907 return mii_nway_restart(&bp->mii);
910 static struct ethtool_ops macb_ethtool_ops = {
911 .get_settings = macb_get_settings,
912 .set_settings = macb_set_settings,
913 .get_drvinfo = macb_get_drvinfo,
914 .nway_reset = macb_nway_reset,
915 .get_link = ethtool_op_get_link,
918 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
920 struct macb *bp = netdev_priv(dev);
922 if (!netif_running(dev))
925 return generic_mii_ioctl(&bp->mii, if_mii(rq), cmd, NULL);
928 static ssize_t macb_mii_show(const struct device *_dev, char *buf,
931 struct net_device *dev = to_net_dev(_dev);
932 struct macb *bp = netdev_priv(dev);
933 ssize_t ret = -EINVAL;
935 if (netif_running(dev)) {
937 value = macb_mdio_read(dev, bp->mii.phy_id, addr);
938 ret = sprintf(buf, "0x%04x\n", (uint16_t)value);
944 #define MII_ENTRY(name, addr) \
945 static ssize_t show_##name(struct device *_dev, \
946 struct device_attribute *attr, \
949 return macb_mii_show(_dev, buf, addr); \
951 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
953 MII_ENTRY(bmcr, MII_BMCR);
954 MII_ENTRY(bmsr, MII_BMSR);
955 MII_ENTRY(physid1, MII_PHYSID1);
956 MII_ENTRY(physid2, MII_PHYSID2);
957 MII_ENTRY(advertise, MII_ADVERTISE);
958 MII_ENTRY(lpa, MII_LPA);
959 MII_ENTRY(expansion, MII_EXPANSION);
961 static struct attribute *macb_mii_attrs[] = {
964 &dev_attr_physid1.attr,
965 &dev_attr_physid2.attr,
966 &dev_attr_advertise.attr,
968 &dev_attr_expansion.attr,
972 static struct attribute_group macb_mii_group = {
974 .attrs = macb_mii_attrs,
977 static void macb_unregister_sysfs(struct net_device *net)
979 struct device *_dev = &net->dev;
981 sysfs_remove_group(&_dev->kobj, &macb_mii_group);
984 static int macb_register_sysfs(struct net_device *net)
986 struct device *_dev = &net->dev;
989 ret = sysfs_create_group(&_dev->kobj, &macb_mii_group);
992 "%s: sysfs mii attribute registration failed: %d\n",
996 static int __devinit macb_probe(struct platform_device *pdev)
998 struct eth_platform_data *pdata;
999 struct resource *regs;
1000 struct net_device *dev;
1002 unsigned long pclk_hz;
1006 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1008 dev_err(&pdev->dev, "no mmio resource defined\n");
1013 dev = alloc_etherdev(sizeof(*bp));
1015 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1019 SET_MODULE_OWNER(dev);
1020 SET_NETDEV_DEV(dev, &pdev->dev);
1022 /* TODO: Actually, we have some interesting features... */
1025 bp = netdev_priv(dev);
1029 spin_lock_init(&bp->lock);
1031 #if defined(CONFIG_ARCH_AT91)
1032 bp->pclk = clk_get(&pdev->dev, "macb_clk");
1033 if (IS_ERR(bp->pclk)) {
1034 dev_err(&pdev->dev, "failed to get macb_clk\n");
1035 goto err_out_free_dev;
1037 clk_enable(bp->pclk);
1039 bp->pclk = clk_get(&pdev->dev, "pclk");
1040 if (IS_ERR(bp->pclk)) {
1041 dev_err(&pdev->dev, "failed to get pclk\n");
1042 goto err_out_free_dev;
1044 bp->hclk = clk_get(&pdev->dev, "hclk");
1045 if (IS_ERR(bp->hclk)) {
1046 dev_err(&pdev->dev, "failed to get hclk\n");
1047 goto err_out_put_pclk;
1050 clk_enable(bp->pclk);
1051 clk_enable(bp->hclk);
1054 bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
1056 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1058 goto err_out_disable_clocks;
1061 dev->irq = platform_get_irq(pdev, 0);
1062 err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
1066 "%s: Unable to request IRQ %d (error %d)\n",
1067 dev->name, dev->irq, err);
1068 goto err_out_iounmap;
1071 dev->open = macb_open;
1072 dev->stop = macb_close;
1073 dev->hard_start_xmit = macb_start_xmit;
1074 dev->get_stats = macb_get_stats;
1075 dev->do_ioctl = macb_ioctl;
1076 dev->poll = macb_poll;
1078 dev->ethtool_ops = &macb_ethtool_ops;
1080 dev->base_addr = regs->start;
1082 INIT_DELAYED_WORK(&bp->periodic_task, macb_periodic_task);
1083 mutex_init(&bp->mdio_mutex);
1084 init_completion(&bp->mdio_complete);
1086 /* Set MII management clock divider */
1087 pclk_hz = clk_get_rate(bp->pclk);
1088 if (pclk_hz <= 20000000)
1089 config = MACB_BF(CLK, MACB_CLK_DIV8);
1090 else if (pclk_hz <= 40000000)
1091 config = MACB_BF(CLK, MACB_CLK_DIV16);
1092 else if (pclk_hz <= 80000000)
1093 config = MACB_BF(CLK, MACB_CLK_DIV32);
1095 config = MACB_BF(CLK, MACB_CLK_DIV64);
1096 macb_writel(bp, NCFGR, config);
1099 bp->mii.mdio_read = macb_mdio_read;
1100 bp->mii.mdio_write = macb_mdio_write;
1101 bp->mii.phy_id_mask = 0x1f;
1102 bp->mii.reg_num_mask = 0x1f;
1104 macb_get_hwaddr(bp);
1105 err = macb_phy_probe(bp);
1107 dev_err(&pdev->dev, "Failed to detect PHY, aborting.\n");
1108 goto err_out_free_irq;
1111 pdata = pdev->dev.platform_data;
1112 if (pdata && pdata->is_rmii)
1113 #if defined(CONFIG_ARCH_AT91)
1114 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1116 macb_writel(bp, USRIO, 0);
1119 #if defined(CONFIG_ARCH_AT91)
1120 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1122 macb_writel(bp, USRIO, MACB_BIT(MII));
1125 bp->tx_pending = DEF_TX_RING_PENDING;
1127 err = register_netdev(dev);
1129 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1130 goto err_out_free_irq;
1133 platform_set_drvdata(pdev, dev);
1135 macb_register_sysfs(dev);
1137 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d "
1138 "(%02x:%02x:%02x:%02x:%02x:%02x)\n",
1139 dev->name, dev->base_addr, dev->irq,
1140 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1141 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1146 free_irq(dev->irq, dev);
1149 err_out_disable_clocks:
1150 #ifndef CONFIG_ARCH_AT91
1151 clk_disable(bp->hclk);
1154 clk_disable(bp->pclk);
1160 platform_set_drvdata(pdev, NULL);
1164 static int __devexit macb_remove(struct platform_device *pdev)
1166 struct net_device *dev;
1169 dev = platform_get_drvdata(pdev);
1172 bp = netdev_priv(dev);
1173 macb_unregister_sysfs(dev);
1174 unregister_netdev(dev);
1175 free_irq(dev->irq, dev);
1177 #ifndef CONFIG_ARCH_AT91
1178 clk_disable(bp->hclk);
1181 clk_disable(bp->pclk);
1184 platform_set_drvdata(pdev, NULL);
1190 static struct platform_driver macb_driver = {
1191 .probe = macb_probe,
1192 .remove = __devexit_p(macb_remove),
1198 static int __init macb_init(void)
1200 return platform_driver_register(&macb_driver);
1203 static void __exit macb_exit(void)
1205 platform_driver_unregister(&macb_driver);
1208 module_init(macb_init);
1209 module_exit(macb_exit);
1211 MODULE_LICENSE("GPL");
1212 MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
1213 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");