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/interrupt.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/platform_data/macb.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy.h>
28 #define RX_BUFFER_SIZE 128
29 #define RX_RING_SIZE 512
30 #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
32 /* Make the IP header word-aligned (the ethernet header is 14 bytes) */
35 #define TX_RING_SIZE 128
36 #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
37 #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
39 #define TX_RING_GAP(bp) \
40 (TX_RING_SIZE - (bp)->tx_pending)
41 #define TX_BUFFS_AVAIL(bp) \
42 (((bp)->tx_tail <= (bp)->tx_head) ? \
43 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
44 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
45 #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
47 #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
49 /* minimum number of free TX descriptors before waking up TX process */
50 #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
52 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
55 static void __macb_set_hwaddr(struct macb *bp)
60 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
61 macb_writel(bp, SA1B, bottom);
62 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
63 macb_writel(bp, SA1T, top);
66 static void __init macb_get_hwaddr(struct macb *bp)
72 bottom = macb_readl(bp, SA1B);
73 top = macb_readl(bp, SA1T);
75 addr[0] = bottom & 0xff;
76 addr[1] = (bottom >> 8) & 0xff;
77 addr[2] = (bottom >> 16) & 0xff;
78 addr[3] = (bottom >> 24) & 0xff;
80 addr[5] = (top >> 8) & 0xff;
82 if (is_valid_ether_addr(addr)) {
83 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
85 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
86 random_ether_addr(bp->dev->dev_addr);
90 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
92 struct macb *bp = bus->priv;
95 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
96 | MACB_BF(RW, MACB_MAN_READ)
97 | MACB_BF(PHYA, mii_id)
98 | MACB_BF(REGA, regnum)
99 | MACB_BF(CODE, MACB_MAN_CODE)));
101 /* wait for end of transfer */
102 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
105 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
110 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
113 struct macb *bp = bus->priv;
115 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
116 | MACB_BF(RW, MACB_MAN_WRITE)
117 | MACB_BF(PHYA, mii_id)
118 | MACB_BF(REGA, regnum)
119 | MACB_BF(CODE, MACB_MAN_CODE)
120 | MACB_BF(DATA, value)));
122 /* wait for end of transfer */
123 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
129 static int macb_mdio_reset(struct mii_bus *bus)
134 static void macb_handle_link_change(struct net_device *dev)
136 struct macb *bp = netdev_priv(dev);
137 struct phy_device *phydev = bp->phy_dev;
140 int status_change = 0;
142 spin_lock_irqsave(&bp->lock, flags);
145 if ((bp->speed != phydev->speed) ||
146 (bp->duplex != phydev->duplex)) {
149 reg = macb_readl(bp, NCFGR);
150 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
154 if (phydev->speed == SPEED_100)
155 reg |= MACB_BIT(SPD);
157 macb_writel(bp, NCFGR, reg);
159 bp->speed = phydev->speed;
160 bp->duplex = phydev->duplex;
165 if (phydev->link != bp->link) {
170 bp->link = phydev->link;
175 spin_unlock_irqrestore(&bp->lock, flags);
179 printk(KERN_INFO "%s: link up (%d/%s)\n",
180 dev->name, phydev->speed,
181 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
183 printk(KERN_INFO "%s: link down\n", dev->name);
187 /* based on au1000_eth. c*/
188 static int macb_mii_probe(struct net_device *dev)
190 struct macb *bp = netdev_priv(dev);
191 struct phy_device *phydev;
192 struct macb_platform_data *pdata;
195 phydev = phy_find_first(bp->mii_bus);
197 printk (KERN_ERR "%s: no PHY found\n", dev->name);
201 pdata = bp->pdev->dev.platform_data;
202 /* TODO : add pin_irq */
204 /* attach the mac to the phy */
205 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
206 pdata && pdata->is_rmii ?
207 PHY_INTERFACE_MODE_RMII :
208 PHY_INTERFACE_MODE_MII);
210 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
214 /* mask with MAC supported features */
215 phydev->supported &= PHY_BASIC_FEATURES;
217 phydev->advertising = phydev->supported;
222 bp->phy_dev = phydev;
227 static int macb_mii_init(struct macb *bp)
229 struct macb_platform_data *pdata;
232 /* Enable management port */
233 macb_writel(bp, NCR, MACB_BIT(MPE));
235 bp->mii_bus = mdiobus_alloc();
236 if (bp->mii_bus == NULL) {
241 bp->mii_bus->name = "MACB_mii_bus";
242 bp->mii_bus->read = &macb_mdio_read;
243 bp->mii_bus->write = &macb_mdio_write;
244 bp->mii_bus->reset = &macb_mdio_reset;
245 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
246 bp->mii_bus->priv = bp;
247 bp->mii_bus->parent = &bp->dev->dev;
248 pdata = bp->pdev->dev.platform_data;
251 bp->mii_bus->phy_mask = pdata->phy_mask;
253 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
254 if (!bp->mii_bus->irq) {
256 goto err_out_free_mdiobus;
259 for (i = 0; i < PHY_MAX_ADDR; i++)
260 bp->mii_bus->irq[i] = PHY_POLL;
262 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
264 if (mdiobus_register(bp->mii_bus))
265 goto err_out_free_mdio_irq;
267 if (macb_mii_probe(bp->dev) != 0) {
268 goto err_out_unregister_bus;
273 err_out_unregister_bus:
274 mdiobus_unregister(bp->mii_bus);
275 err_out_free_mdio_irq:
276 kfree(bp->mii_bus->irq);
277 err_out_free_mdiobus:
278 mdiobus_free(bp->mii_bus);
283 static void macb_update_stats(struct macb *bp)
285 u32 __iomem *reg = bp->regs + MACB_PFR;
286 u32 *p = &bp->hw_stats.rx_pause_frames;
287 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
289 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
291 for(; p < end; p++, reg++)
292 *p += __raw_readl(reg);
295 static void macb_tx(struct macb *bp)
301 status = macb_readl(bp, TSR);
302 macb_writel(bp, TSR, status);
304 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
305 (unsigned long)status);
307 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
309 printk(KERN_ERR "%s: TX %s, resetting buffers\n",
310 bp->dev->name, status & MACB_BIT(UND) ?
311 "underrun" : "retry limit exceeded");
313 /* Transfer ongoing, disable transmitter, to avoid confusion */
314 if (status & MACB_BIT(TGO))
315 macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
319 /*Mark all the buffer as used to avoid sending a lost buffer*/
320 for (i = 0; i < TX_RING_SIZE; i++)
321 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
324 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
326 /* free transmit buffer in upper layer*/
327 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
328 struct ring_info *rp = &bp->tx_skb[tail];
329 struct sk_buff *skb = rp->skb;
335 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
338 dev_kfree_skb_irq(skb);
341 bp->tx_head = bp->tx_tail = 0;
343 /* Enable the transmitter again */
344 if (status & MACB_BIT(TGO))
345 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
348 if (!(status & MACB_BIT(COMP)))
350 * This may happen when a buffer becomes complete
351 * between reading the ISR and scanning the
352 * descriptors. Nothing to worry about.
357 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
358 struct ring_info *rp = &bp->tx_skb[tail];
359 struct sk_buff *skb = rp->skb;
365 bufstat = bp->tx_ring[tail].ctrl;
367 if (!(bufstat & MACB_BIT(TX_USED)))
370 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
372 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
374 bp->stats.tx_packets++;
375 bp->stats.tx_bytes += skb->len;
377 dev_kfree_skb_irq(skb);
381 if (netif_queue_stopped(bp->dev) &&
382 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
383 netif_wake_queue(bp->dev);
386 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
387 unsigned int last_frag)
391 unsigned int offset = 0;
394 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
396 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
397 first_frag, last_frag, len);
399 skb = dev_alloc_skb(len + RX_OFFSET);
401 bp->stats.rx_dropped++;
402 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
403 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
404 if (frag == last_frag)
411 skb_reserve(skb, RX_OFFSET);
412 skb_checksum_none_assert(skb);
415 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
416 unsigned int frag_len = RX_BUFFER_SIZE;
418 if (offset + frag_len > len) {
419 BUG_ON(frag != last_frag);
420 frag_len = len - offset;
422 skb_copy_to_linear_data_offset(skb, offset,
424 (RX_BUFFER_SIZE * frag)),
426 offset += RX_BUFFER_SIZE;
427 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
430 if (frag == last_frag)
434 skb->protocol = eth_type_trans(skb, bp->dev);
436 bp->stats.rx_packets++;
437 bp->stats.rx_bytes += len;
438 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
439 skb->len, skb->csum);
440 netif_receive_skb(skb);
445 /* Mark DMA descriptors from begin up to and not including end as unused */
446 static void discard_partial_frame(struct macb *bp, unsigned int begin,
451 for (frag = begin; frag != end; frag = NEXT_RX(frag))
452 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
456 * When this happens, the hardware stats registers for
457 * whatever caused this is updated, so we don't have to record
462 static int macb_rx(struct macb *bp, int budget)
465 unsigned int tail = bp->rx_tail;
468 for (; budget > 0; tail = NEXT_RX(tail)) {
472 addr = bp->rx_ring[tail].addr;
473 ctrl = bp->rx_ring[tail].ctrl;
475 if (!(addr & MACB_BIT(RX_USED)))
478 if (ctrl & MACB_BIT(RX_SOF)) {
479 if (first_frag != -1)
480 discard_partial_frame(bp, first_frag, tail);
484 if (ctrl & MACB_BIT(RX_EOF)) {
486 BUG_ON(first_frag == -1);
488 dropped = macb_rx_frame(bp, first_frag, tail);
497 if (first_frag != -1)
498 bp->rx_tail = first_frag;
505 static int macb_poll(struct napi_struct *napi, int budget)
507 struct macb *bp = container_of(napi, struct macb, napi);
511 status = macb_readl(bp, RSR);
512 macb_writel(bp, RSR, status);
516 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
517 (unsigned long)status, budget);
519 work_done = macb_rx(bp, budget);
520 if (work_done < budget) {
524 * We've done what we can to clean the buffers. Make sure we
525 * get notified when new packets arrive.
527 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
530 /* TODO: Handle errors */
535 static irqreturn_t macb_interrupt(int irq, void *dev_id)
537 struct net_device *dev = dev_id;
538 struct macb *bp = netdev_priv(dev);
541 status = macb_readl(bp, ISR);
543 if (unlikely(!status))
546 spin_lock(&bp->lock);
549 /* close possible race with dev_close */
550 if (unlikely(!netif_running(dev))) {
551 macb_writel(bp, IDR, ~0UL);
555 if (status & MACB_RX_INT_FLAGS) {
557 * There's no point taking any more interrupts
558 * until we have processed the buffers. The
559 * scheduling call may fail if the poll routine
560 * is already scheduled, so disable interrupts
563 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
565 if (napi_schedule_prep(&bp->napi)) {
566 dev_dbg(&bp->pdev->dev,
567 "scheduling RX softirq\n");
568 __napi_schedule(&bp->napi);
572 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
577 * Link change detection isn't possible with RMII, so we'll
578 * add that if/when we get our hands on a full-blown MII PHY.
581 if (status & MACB_BIT(ISR_ROVR)) {
582 /* We missed at least one packet */
583 bp->hw_stats.rx_overruns++;
586 if (status & MACB_BIT(HRESP)) {
588 * TODO: Reset the hardware, and maybe move the printk
589 * to a lower-priority context as well (work queue?)
591 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
595 status = macb_readl(bp, ISR);
598 spin_unlock(&bp->lock);
603 #ifdef CONFIG_NET_POLL_CONTROLLER
605 * Polling receive - used by netconsole and other diagnostic tools
606 * to allow network i/o with interrupts disabled.
608 static void macb_poll_controller(struct net_device *dev)
612 local_irq_save(flags);
613 macb_interrupt(dev->irq, dev);
614 local_irq_restore(flags);
618 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
620 struct macb *bp = netdev_priv(dev);
622 unsigned int len, entry;
628 dev_dbg(&bp->pdev->dev,
629 "start_xmit: len %u head %p data %p tail %p end %p\n",
630 skb->len, skb->head, skb->data,
631 skb_tail_pointer(skb), skb_end_pointer(skb));
632 dev_dbg(&bp->pdev->dev,
634 for (i = 0; i < 16; i++)
635 printk(" %02x", (unsigned int)skb->data[i]);
640 spin_lock_irqsave(&bp->lock, flags);
642 /* This is a hard error, log it. */
643 if (TX_BUFFS_AVAIL(bp) < 1) {
644 netif_stop_queue(dev);
645 spin_unlock_irqrestore(&bp->lock, flags);
646 dev_err(&bp->pdev->dev,
647 "BUG! Tx Ring full when queue awake!\n");
648 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
649 bp->tx_head, bp->tx_tail);
650 return NETDEV_TX_BUSY;
654 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
655 mapping = dma_map_single(&bp->pdev->dev, skb->data,
657 bp->tx_skb[entry].skb = skb;
658 bp->tx_skb[entry].mapping = mapping;
659 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
660 skb->data, (unsigned long)mapping);
662 ctrl = MACB_BF(TX_FRMLEN, len);
663 ctrl |= MACB_BIT(TX_LAST);
664 if (entry == (TX_RING_SIZE - 1))
665 ctrl |= MACB_BIT(TX_WRAP);
667 bp->tx_ring[entry].addr = mapping;
668 bp->tx_ring[entry].ctrl = ctrl;
671 entry = NEXT_TX(entry);
674 skb_tx_timestamp(skb);
676 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
678 if (TX_BUFFS_AVAIL(bp) < 1)
679 netif_stop_queue(dev);
681 spin_unlock_irqrestore(&bp->lock, flags);
686 static void macb_free_consistent(struct macb *bp)
693 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
694 bp->rx_ring, bp->rx_ring_dma);
698 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
699 bp->tx_ring, bp->tx_ring_dma);
702 if (bp->rx_buffers) {
703 dma_free_coherent(&bp->pdev->dev,
704 RX_RING_SIZE * RX_BUFFER_SIZE,
705 bp->rx_buffers, bp->rx_buffers_dma);
706 bp->rx_buffers = NULL;
710 static int macb_alloc_consistent(struct macb *bp)
714 size = TX_RING_SIZE * sizeof(struct ring_info);
715 bp->tx_skb = kmalloc(size, GFP_KERNEL);
719 size = RX_RING_BYTES;
720 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
721 &bp->rx_ring_dma, GFP_KERNEL);
724 dev_dbg(&bp->pdev->dev,
725 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
726 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
728 size = TX_RING_BYTES;
729 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
730 &bp->tx_ring_dma, GFP_KERNEL);
733 dev_dbg(&bp->pdev->dev,
734 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
735 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
737 size = RX_RING_SIZE * RX_BUFFER_SIZE;
738 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
739 &bp->rx_buffers_dma, GFP_KERNEL);
742 dev_dbg(&bp->pdev->dev,
743 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
744 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
749 macb_free_consistent(bp);
753 static void macb_init_rings(struct macb *bp)
758 addr = bp->rx_buffers_dma;
759 for (i = 0; i < RX_RING_SIZE; i++) {
760 bp->rx_ring[i].addr = addr;
761 bp->rx_ring[i].ctrl = 0;
762 addr += RX_BUFFER_SIZE;
764 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
766 for (i = 0; i < TX_RING_SIZE; i++) {
767 bp->tx_ring[i].addr = 0;
768 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
770 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
772 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
775 static void macb_reset_hw(struct macb *bp)
777 /* Make sure we have the write buffer for ourselves */
781 * Disable RX and TX (XXX: Should we halt the transmission
784 macb_writel(bp, NCR, 0);
786 /* Clear the stats registers (XXX: Update stats first?) */
787 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
789 /* Clear all status flags */
790 macb_writel(bp, TSR, ~0UL);
791 macb_writel(bp, RSR, ~0UL);
793 /* Disable all interrupts */
794 macb_writel(bp, IDR, ~0UL);
798 static void macb_init_hw(struct macb *bp)
803 __macb_set_hwaddr(bp);
805 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
806 config |= MACB_BIT(PAE); /* PAuse Enable */
807 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
808 config |= MACB_BIT(BIG); /* Receive oversized frames */
809 if (bp->dev->flags & IFF_PROMISC)
810 config |= MACB_BIT(CAF); /* Copy All Frames */
811 if (!(bp->dev->flags & IFF_BROADCAST))
812 config |= MACB_BIT(NBC); /* No BroadCast */
813 macb_writel(bp, NCFGR, config);
815 /* Initialize TX and RX buffers */
816 macb_writel(bp, RBQP, bp->rx_ring_dma);
817 macb_writel(bp, TBQP, bp->tx_ring_dma);
819 /* Enable TX and RX */
820 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
822 /* Enable interrupts */
823 macb_writel(bp, IER, (MACB_BIT(RCOMP)
835 * The hash address register is 64 bits long and takes up two
836 * locations in the memory map. The least significant bits are stored
837 * in EMAC_HSL and the most significant bits in EMAC_HSH.
839 * The unicast hash enable and the multicast hash enable bits in the
840 * network configuration register enable the reception of hash matched
841 * frames. The destination address is reduced to a 6 bit index into
842 * the 64 bit hash register using the following hash function. The
843 * hash function is an exclusive or of every sixth bit of the
844 * destination address.
846 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
847 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
848 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
849 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
850 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
851 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
853 * da[0] represents the least significant bit of the first byte
854 * received, that is, the multicast/unicast indicator, and da[47]
855 * represents the most significant bit of the last byte received. If
856 * the hash index, hi[n], points to a bit that is set in the hash
857 * register then the frame will be matched according to whether the
858 * frame is multicast or unicast. A multicast match will be signalled
859 * if the multicast hash enable bit is set, da[0] is 1 and the hash
860 * index points to a bit set in the hash register. A unicast match
861 * will be signalled if the unicast hash enable bit is set, da[0] is 0
862 * and the hash index points to a bit set in the hash register. To
863 * receive all multicast frames, the hash register should be set with
864 * all ones and the multicast hash enable bit should be set in the
865 * network configuration register.
868 static inline int hash_bit_value(int bitnr, __u8 *addr)
870 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
876 * Return the hash index value for the specified address.
878 static int hash_get_index(__u8 *addr)
883 for (j = 0; j < 6; j++) {
884 for (i = 0, bitval = 0; i < 8; i++)
885 bitval ^= hash_bit_value(i*6 + j, addr);
887 hash_index |= (bitval << j);
894 * Add multicast addresses to the internal multicast-hash table.
896 static void macb_sethashtable(struct net_device *dev)
898 struct netdev_hw_addr *ha;
899 unsigned long mc_filter[2];
901 struct macb *bp = netdev_priv(dev);
903 mc_filter[0] = mc_filter[1] = 0;
905 netdev_for_each_mc_addr(ha, dev) {
906 bitnr = hash_get_index(ha->addr);
907 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
910 macb_writel(bp, HRB, mc_filter[0]);
911 macb_writel(bp, HRT, mc_filter[1]);
915 * Enable/Disable promiscuous and multicast modes.
917 static void macb_set_rx_mode(struct net_device *dev)
920 struct macb *bp = netdev_priv(dev);
922 cfg = macb_readl(bp, NCFGR);
924 if (dev->flags & IFF_PROMISC)
925 /* Enable promiscuous mode */
926 cfg |= MACB_BIT(CAF);
927 else if (dev->flags & (~IFF_PROMISC))
928 /* Disable promiscuous mode */
929 cfg &= ~MACB_BIT(CAF);
931 if (dev->flags & IFF_ALLMULTI) {
932 /* Enable all multicast mode */
933 macb_writel(bp, HRB, -1);
934 macb_writel(bp, HRT, -1);
935 cfg |= MACB_BIT(NCFGR_MTI);
936 } else if (!netdev_mc_empty(dev)) {
937 /* Enable specific multicasts */
938 macb_sethashtable(dev);
939 cfg |= MACB_BIT(NCFGR_MTI);
940 } else if (dev->flags & (~IFF_ALLMULTI)) {
941 /* Disable all multicast mode */
942 macb_writel(bp, HRB, 0);
943 macb_writel(bp, HRT, 0);
944 cfg &= ~MACB_BIT(NCFGR_MTI);
947 macb_writel(bp, NCFGR, cfg);
950 static int macb_open(struct net_device *dev)
952 struct macb *bp = netdev_priv(dev);
955 dev_dbg(&bp->pdev->dev, "open\n");
957 /* if the phy is not yet register, retry later*/
961 if (!is_valid_ether_addr(dev->dev_addr))
962 return -EADDRNOTAVAIL;
964 err = macb_alloc_consistent(bp);
967 "%s: Unable to allocate DMA memory (error %d)\n",
972 napi_enable(&bp->napi);
977 /* schedule a link state check */
978 phy_start(bp->phy_dev);
980 netif_start_queue(dev);
985 static int macb_close(struct net_device *dev)
987 struct macb *bp = netdev_priv(dev);
990 netif_stop_queue(dev);
991 napi_disable(&bp->napi);
994 phy_stop(bp->phy_dev);
996 spin_lock_irqsave(&bp->lock, flags);
998 netif_carrier_off(dev);
999 spin_unlock_irqrestore(&bp->lock, flags);
1001 macb_free_consistent(bp);
1006 static struct net_device_stats *macb_get_stats(struct net_device *dev)
1008 struct macb *bp = netdev_priv(dev);
1009 struct net_device_stats *nstat = &bp->stats;
1010 struct macb_stats *hwstat = &bp->hw_stats;
1012 /* read stats from hardware */
1013 macb_update_stats(bp);
1015 /* Convert HW stats into netdevice stats */
1016 nstat->rx_errors = (hwstat->rx_fcs_errors +
1017 hwstat->rx_align_errors +
1018 hwstat->rx_resource_errors +
1019 hwstat->rx_overruns +
1020 hwstat->rx_oversize_pkts +
1021 hwstat->rx_jabbers +
1022 hwstat->rx_undersize_pkts +
1023 hwstat->sqe_test_errors +
1024 hwstat->rx_length_mismatch);
1025 nstat->tx_errors = (hwstat->tx_late_cols +
1026 hwstat->tx_excessive_cols +
1027 hwstat->tx_underruns +
1028 hwstat->tx_carrier_errors);
1029 nstat->collisions = (hwstat->tx_single_cols +
1030 hwstat->tx_multiple_cols +
1031 hwstat->tx_excessive_cols);
1032 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1033 hwstat->rx_jabbers +
1034 hwstat->rx_undersize_pkts +
1035 hwstat->rx_length_mismatch);
1036 nstat->rx_over_errors = hwstat->rx_resource_errors +
1037 hwstat->rx_overruns;
1038 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1039 nstat->rx_frame_errors = hwstat->rx_align_errors;
1040 nstat->rx_fifo_errors = hwstat->rx_overruns;
1041 /* XXX: What does "missed" mean? */
1042 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1043 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1044 nstat->tx_fifo_errors = hwstat->tx_underruns;
1045 /* Don't know about heartbeat or window errors... */
1050 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1052 struct macb *bp = netdev_priv(dev);
1053 struct phy_device *phydev = bp->phy_dev;
1058 return phy_ethtool_gset(phydev, cmd);
1061 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1063 struct macb *bp = netdev_priv(dev);
1064 struct phy_device *phydev = bp->phy_dev;
1069 return phy_ethtool_sset(phydev, cmd);
1072 static void macb_get_drvinfo(struct net_device *dev,
1073 struct ethtool_drvinfo *info)
1075 struct macb *bp = netdev_priv(dev);
1077 strcpy(info->driver, bp->pdev->dev.driver->name);
1078 strcpy(info->version, "$Revision: 1.14 $");
1079 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
1082 static const struct ethtool_ops macb_ethtool_ops = {
1083 .get_settings = macb_get_settings,
1084 .set_settings = macb_set_settings,
1085 .get_drvinfo = macb_get_drvinfo,
1086 .get_link = ethtool_op_get_link,
1089 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1091 struct macb *bp = netdev_priv(dev);
1092 struct phy_device *phydev = bp->phy_dev;
1094 if (!netif_running(dev))
1100 return phy_mii_ioctl(phydev, rq, cmd);
1103 static const struct net_device_ops macb_netdev_ops = {
1104 .ndo_open = macb_open,
1105 .ndo_stop = macb_close,
1106 .ndo_start_xmit = macb_start_xmit,
1107 .ndo_set_rx_mode = macb_set_rx_mode,
1108 .ndo_get_stats = macb_get_stats,
1109 .ndo_do_ioctl = macb_ioctl,
1110 .ndo_validate_addr = eth_validate_addr,
1111 .ndo_change_mtu = eth_change_mtu,
1112 .ndo_set_mac_address = eth_mac_addr,
1113 #ifdef CONFIG_NET_POLL_CONTROLLER
1114 .ndo_poll_controller = macb_poll_controller,
1118 static int __init macb_probe(struct platform_device *pdev)
1120 struct macb_platform_data *pdata;
1121 struct resource *regs;
1122 struct net_device *dev;
1124 struct phy_device *phydev;
1125 unsigned long pclk_hz;
1129 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1131 dev_err(&pdev->dev, "no mmio resource defined\n");
1136 dev = alloc_etherdev(sizeof(*bp));
1138 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1142 SET_NETDEV_DEV(dev, &pdev->dev);
1144 /* TODO: Actually, we have some interesting features... */
1147 bp = netdev_priv(dev);
1151 spin_lock_init(&bp->lock);
1153 bp->pclk = clk_get(&pdev->dev, "pclk");
1154 if (IS_ERR(bp->pclk)) {
1155 dev_err(&pdev->dev, "failed to get macb_clk\n");
1156 goto err_out_free_dev;
1158 clk_enable(bp->pclk);
1160 bp->hclk = clk_get(&pdev->dev, "hclk");
1161 if (IS_ERR(bp->hclk)) {
1162 dev_err(&pdev->dev, "failed to get hclk\n");
1163 goto err_out_put_pclk;
1165 clk_enable(bp->hclk);
1167 bp->regs = ioremap(regs->start, resource_size(regs));
1169 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1171 goto err_out_disable_clocks;
1174 dev->irq = platform_get_irq(pdev, 0);
1175 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
1178 "%s: Unable to request IRQ %d (error %d)\n",
1179 dev->name, dev->irq, err);
1180 goto err_out_iounmap;
1183 dev->netdev_ops = &macb_netdev_ops;
1184 netif_napi_add(dev, &bp->napi, macb_poll, 64);
1185 dev->ethtool_ops = &macb_ethtool_ops;
1187 dev->base_addr = regs->start;
1189 /* Set MII management clock divider */
1190 pclk_hz = clk_get_rate(bp->pclk);
1191 if (pclk_hz <= 20000000)
1192 config = MACB_BF(CLK, MACB_CLK_DIV8);
1193 else if (pclk_hz <= 40000000)
1194 config = MACB_BF(CLK, MACB_CLK_DIV16);
1195 else if (pclk_hz <= 80000000)
1196 config = MACB_BF(CLK, MACB_CLK_DIV32);
1198 config = MACB_BF(CLK, MACB_CLK_DIV64);
1199 macb_writel(bp, NCFGR, config);
1201 macb_get_hwaddr(bp);
1202 pdata = pdev->dev.platform_data;
1204 if (pdata && pdata->is_rmii)
1205 #if defined(CONFIG_ARCH_AT91)
1206 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1208 macb_writel(bp, USRIO, 0);
1211 #if defined(CONFIG_ARCH_AT91)
1212 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1214 macb_writel(bp, USRIO, MACB_BIT(MII));
1217 bp->tx_pending = DEF_TX_RING_PENDING;
1219 err = register_netdev(dev);
1221 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1222 goto err_out_free_irq;
1225 if (macb_mii_init(bp) != 0) {
1226 goto err_out_unregister_netdev;
1229 platform_set_drvdata(pdev, dev);
1231 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
1232 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
1234 phydev = bp->phy_dev;
1235 printk(KERN_INFO "%s: attached PHY driver [%s] "
1236 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
1237 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
1241 err_out_unregister_netdev:
1242 unregister_netdev(dev);
1244 free_irq(dev->irq, dev);
1247 err_out_disable_clocks:
1248 clk_disable(bp->hclk);
1250 clk_disable(bp->pclk);
1256 platform_set_drvdata(pdev, NULL);
1260 static int __exit macb_remove(struct platform_device *pdev)
1262 struct net_device *dev;
1265 dev = platform_get_drvdata(pdev);
1268 bp = netdev_priv(dev);
1270 phy_disconnect(bp->phy_dev);
1271 mdiobus_unregister(bp->mii_bus);
1272 kfree(bp->mii_bus->irq);
1273 mdiobus_free(bp->mii_bus);
1274 unregister_netdev(dev);
1275 free_irq(dev->irq, dev);
1277 clk_disable(bp->hclk);
1279 clk_disable(bp->pclk);
1282 platform_set_drvdata(pdev, NULL);
1289 static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1291 struct net_device *netdev = platform_get_drvdata(pdev);
1292 struct macb *bp = netdev_priv(netdev);
1294 netif_device_detach(netdev);
1296 clk_disable(bp->hclk);
1297 clk_disable(bp->pclk);
1302 static int macb_resume(struct platform_device *pdev)
1304 struct net_device *netdev = platform_get_drvdata(pdev);
1305 struct macb *bp = netdev_priv(netdev);
1307 clk_enable(bp->pclk);
1308 clk_enable(bp->hclk);
1310 netif_device_attach(netdev);
1315 #define macb_suspend NULL
1316 #define macb_resume NULL
1319 static struct platform_driver macb_driver = {
1320 .remove = __exit_p(macb_remove),
1321 .suspend = macb_suspend,
1322 .resume = macb_resume,
1325 .owner = THIS_MODULE,
1329 static int __init macb_init(void)
1331 return platform_driver_probe(&macb_driver, macb_probe);
1334 static void __exit macb_exit(void)
1336 platform_driver_unregister(&macb_driver);
1339 module_init(macb_init);
1340 module_exit(macb_exit);
1342 MODULE_LICENSE("GPL");
1343 MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
1344 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1345 MODULE_ALIAS("platform:macb");