xfs: modify buffer item reference counting
[linux-2.6/kvm.git] / drivers / net / macb.c
blobc8a18a6203c8638c58963b03de7706b9821b0f74
1 /*
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.
9 */
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/dma-mapping.h>
21 #include <linux/platform_device.h>
22 #include <linux/phy.h>
24 #include <mach/board.h>
25 #include <mach/cpu.h>
27 #include "macb.h"
29 #define RX_BUFFER_SIZE 128
30 #define RX_RING_SIZE 512
31 #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
33 /* Make the IP header word-aligned (the ethernet header is 14 bytes) */
34 #define RX_OFFSET 2
36 #define TX_RING_SIZE 128
37 #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
38 #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
40 #define TX_RING_GAP(bp) \
41 (TX_RING_SIZE - (bp)->tx_pending)
42 #define TX_BUFFS_AVAIL(bp) \
43 (((bp)->tx_tail <= (bp)->tx_head) ? \
44 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
45 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
46 #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
48 #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
50 /* minimum number of free TX descriptors before waking up TX process */
51 #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
53 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
54 | MACB_BIT(ISR_ROVR))
56 static void __macb_set_hwaddr(struct macb *bp)
58 u32 bottom;
59 u16 top;
61 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
62 macb_writel(bp, SA1B, bottom);
63 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
64 macb_writel(bp, SA1T, top);
67 static void __init macb_get_hwaddr(struct macb *bp)
69 u32 bottom;
70 u16 top;
71 u8 addr[6];
73 bottom = macb_readl(bp, SA1B);
74 top = macb_readl(bp, SA1T);
76 addr[0] = bottom & 0xff;
77 addr[1] = (bottom >> 8) & 0xff;
78 addr[2] = (bottom >> 16) & 0xff;
79 addr[3] = (bottom >> 24) & 0xff;
80 addr[4] = top & 0xff;
81 addr[5] = (top >> 8) & 0xff;
83 if (is_valid_ether_addr(addr)) {
84 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
85 } else {
86 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
87 random_ether_addr(bp->dev->dev_addr);
91 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
93 struct macb *bp = bus->priv;
94 int value;
96 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
97 | MACB_BF(RW, MACB_MAN_READ)
98 | MACB_BF(PHYA, mii_id)
99 | MACB_BF(REGA, regnum)
100 | MACB_BF(CODE, MACB_MAN_CODE)));
102 /* wait for end of transfer */
103 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
104 cpu_relax();
106 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
108 return value;
111 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
112 u16 value)
114 struct macb *bp = bus->priv;
116 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
117 | MACB_BF(RW, MACB_MAN_WRITE)
118 | MACB_BF(PHYA, mii_id)
119 | MACB_BF(REGA, regnum)
120 | MACB_BF(CODE, MACB_MAN_CODE)
121 | MACB_BF(DATA, value)));
123 /* wait for end of transfer */
124 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
125 cpu_relax();
127 return 0;
130 static int macb_mdio_reset(struct mii_bus *bus)
132 return 0;
135 static void macb_handle_link_change(struct net_device *dev)
137 struct macb *bp = netdev_priv(dev);
138 struct phy_device *phydev = bp->phy_dev;
139 unsigned long flags;
141 int status_change = 0;
143 spin_lock_irqsave(&bp->lock, flags);
145 if (phydev->link) {
146 if ((bp->speed != phydev->speed) ||
147 (bp->duplex != phydev->duplex)) {
148 u32 reg;
150 reg = macb_readl(bp, NCFGR);
151 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
153 if (phydev->duplex)
154 reg |= MACB_BIT(FD);
155 if (phydev->speed == SPEED_100)
156 reg |= MACB_BIT(SPD);
158 macb_writel(bp, NCFGR, reg);
160 bp->speed = phydev->speed;
161 bp->duplex = phydev->duplex;
162 status_change = 1;
166 if (phydev->link != bp->link) {
167 if (!phydev->link) {
168 bp->speed = 0;
169 bp->duplex = -1;
171 bp->link = phydev->link;
173 status_change = 1;
176 spin_unlock_irqrestore(&bp->lock, flags);
178 if (status_change) {
179 if (phydev->link)
180 printk(KERN_INFO "%s: link up (%d/%s)\n",
181 dev->name, phydev->speed,
182 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
183 else
184 printk(KERN_INFO "%s: link down\n", dev->name);
188 /* based on au1000_eth. c*/
189 static int macb_mii_probe(struct net_device *dev)
191 struct macb *bp = netdev_priv(dev);
192 struct phy_device *phydev;
193 struct eth_platform_data *pdata;
194 int ret;
196 phydev = phy_find_first(bp->mii_bus);
197 if (!phydev) {
198 printk (KERN_ERR "%s: no PHY found\n", dev->name);
199 return -1;
202 pdata = bp->pdev->dev.platform_data;
203 /* TODO : add pin_irq */
205 /* attach the mac to the phy */
206 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
207 pdata && pdata->is_rmii ?
208 PHY_INTERFACE_MODE_RMII :
209 PHY_INTERFACE_MODE_MII);
210 if (ret) {
211 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
212 return ret;
215 /* mask with MAC supported features */
216 phydev->supported &= PHY_BASIC_FEATURES;
218 phydev->advertising = phydev->supported;
220 bp->link = 0;
221 bp->speed = 0;
222 bp->duplex = -1;
223 bp->phy_dev = phydev;
225 return 0;
228 static int macb_mii_init(struct macb *bp)
230 struct eth_platform_data *pdata;
231 int err = -ENXIO, i;
233 /* Enable management port */
234 macb_writel(bp, NCR, MACB_BIT(MPE));
236 bp->mii_bus = mdiobus_alloc();
237 if (bp->mii_bus == NULL) {
238 err = -ENOMEM;
239 goto err_out;
242 bp->mii_bus->name = "MACB_mii_bus";
243 bp->mii_bus->read = &macb_mdio_read;
244 bp->mii_bus->write = &macb_mdio_write;
245 bp->mii_bus->reset = &macb_mdio_reset;
246 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
247 bp->mii_bus->priv = bp;
248 bp->mii_bus->parent = &bp->dev->dev;
249 pdata = bp->pdev->dev.platform_data;
251 if (pdata)
252 bp->mii_bus->phy_mask = pdata->phy_mask;
254 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
255 if (!bp->mii_bus->irq) {
256 err = -ENOMEM;
257 goto err_out_free_mdiobus;
260 for (i = 0; i < PHY_MAX_ADDR; i++)
261 bp->mii_bus->irq[i] = PHY_POLL;
263 platform_set_drvdata(bp->dev, bp->mii_bus);
265 if (mdiobus_register(bp->mii_bus))
266 goto err_out_free_mdio_irq;
268 if (macb_mii_probe(bp->dev) != 0) {
269 goto err_out_unregister_bus;
272 return 0;
274 err_out_unregister_bus:
275 mdiobus_unregister(bp->mii_bus);
276 err_out_free_mdio_irq:
277 kfree(bp->mii_bus->irq);
278 err_out_free_mdiobus:
279 mdiobus_free(bp->mii_bus);
280 err_out:
281 return err;
284 static void macb_update_stats(struct macb *bp)
286 u32 __iomem *reg = bp->regs + MACB_PFR;
287 u32 *p = &bp->hw_stats.rx_pause_frames;
288 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
290 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
292 for(; p < end; p++, reg++)
293 *p += __raw_readl(reg);
296 static void macb_tx(struct macb *bp)
298 unsigned int tail;
299 unsigned int head;
300 u32 status;
302 status = macb_readl(bp, TSR);
303 macb_writel(bp, TSR, status);
305 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
306 (unsigned long)status);
308 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
309 int i;
310 printk(KERN_ERR "%s: TX %s, resetting buffers\n",
311 bp->dev->name, status & MACB_BIT(UND) ?
312 "underrun" : "retry limit exceeded");
314 /* Transfer ongoing, disable transmitter, to avoid confusion */
315 if (status & MACB_BIT(TGO))
316 macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
318 head = bp->tx_head;
320 /*Mark all the buffer as used to avoid sending a lost buffer*/
321 for (i = 0; i < TX_RING_SIZE; i++)
322 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
324 /* free transmit buffer in upper layer*/
325 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
326 struct ring_info *rp = &bp->tx_skb[tail];
327 struct sk_buff *skb = rp->skb;
329 BUG_ON(skb == NULL);
331 rmb();
333 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
334 DMA_TO_DEVICE);
335 rp->skb = NULL;
336 dev_kfree_skb_irq(skb);
339 bp->tx_head = bp->tx_tail = 0;
341 /* Enable the transmitter again */
342 if (status & MACB_BIT(TGO))
343 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
346 if (!(status & MACB_BIT(COMP)))
348 * This may happen when a buffer becomes complete
349 * between reading the ISR and scanning the
350 * descriptors. Nothing to worry about.
352 return;
354 head = bp->tx_head;
355 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
356 struct ring_info *rp = &bp->tx_skb[tail];
357 struct sk_buff *skb = rp->skb;
358 u32 bufstat;
360 BUG_ON(skb == NULL);
362 rmb();
363 bufstat = bp->tx_ring[tail].ctrl;
365 if (!(bufstat & MACB_BIT(TX_USED)))
366 break;
368 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
369 tail, skb->data);
370 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
371 DMA_TO_DEVICE);
372 bp->stats.tx_packets++;
373 bp->stats.tx_bytes += skb->len;
374 rp->skb = NULL;
375 dev_kfree_skb_irq(skb);
378 bp->tx_tail = tail;
379 if (netif_queue_stopped(bp->dev) &&
380 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
381 netif_wake_queue(bp->dev);
384 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
385 unsigned int last_frag)
387 unsigned int len;
388 unsigned int frag;
389 unsigned int offset = 0;
390 struct sk_buff *skb;
392 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
394 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
395 first_frag, last_frag, len);
397 skb = dev_alloc_skb(len + RX_OFFSET);
398 if (!skb) {
399 bp->stats.rx_dropped++;
400 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
401 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
402 if (frag == last_frag)
403 break;
405 wmb();
406 return 1;
409 skb_reserve(skb, RX_OFFSET);
410 skb->ip_summed = CHECKSUM_NONE;
411 skb_put(skb, len);
413 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
414 unsigned int frag_len = RX_BUFFER_SIZE;
416 if (offset + frag_len > len) {
417 BUG_ON(frag != last_frag);
418 frag_len = len - offset;
420 skb_copy_to_linear_data_offset(skb, offset,
421 (bp->rx_buffers +
422 (RX_BUFFER_SIZE * frag)),
423 frag_len);
424 offset += RX_BUFFER_SIZE;
425 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
426 wmb();
428 if (frag == last_frag)
429 break;
432 skb->protocol = eth_type_trans(skb, bp->dev);
434 bp->stats.rx_packets++;
435 bp->stats.rx_bytes += len;
436 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
437 skb->len, skb->csum);
438 netif_receive_skb(skb);
440 return 0;
443 /* Mark DMA descriptors from begin up to and not including end as unused */
444 static void discard_partial_frame(struct macb *bp, unsigned int begin,
445 unsigned int end)
447 unsigned int frag;
449 for (frag = begin; frag != end; frag = NEXT_RX(frag))
450 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
451 wmb();
454 * When this happens, the hardware stats registers for
455 * whatever caused this is updated, so we don't have to record
456 * anything.
460 static int macb_rx(struct macb *bp, int budget)
462 int received = 0;
463 unsigned int tail = bp->rx_tail;
464 int first_frag = -1;
466 for (; budget > 0; tail = NEXT_RX(tail)) {
467 u32 addr, ctrl;
469 rmb();
470 addr = bp->rx_ring[tail].addr;
471 ctrl = bp->rx_ring[tail].ctrl;
473 if (!(addr & MACB_BIT(RX_USED)))
474 break;
476 if (ctrl & MACB_BIT(RX_SOF)) {
477 if (first_frag != -1)
478 discard_partial_frame(bp, first_frag, tail);
479 first_frag = tail;
482 if (ctrl & MACB_BIT(RX_EOF)) {
483 int dropped;
484 BUG_ON(first_frag == -1);
486 dropped = macb_rx_frame(bp, first_frag, tail);
487 first_frag = -1;
488 if (!dropped) {
489 received++;
490 budget--;
495 if (first_frag != -1)
496 bp->rx_tail = first_frag;
497 else
498 bp->rx_tail = tail;
500 return received;
503 static int macb_poll(struct napi_struct *napi, int budget)
505 struct macb *bp = container_of(napi, struct macb, napi);
506 int work_done;
507 u32 status;
509 status = macb_readl(bp, RSR);
510 macb_writel(bp, RSR, status);
512 work_done = 0;
514 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
515 (unsigned long)status, budget);
517 work_done = macb_rx(bp, budget);
518 if (work_done < budget)
519 napi_complete(napi);
522 * We've done what we can to clean the buffers. Make sure we
523 * get notified when new packets arrive.
525 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
527 /* TODO: Handle errors */
529 return work_done;
532 static irqreturn_t macb_interrupt(int irq, void *dev_id)
534 struct net_device *dev = dev_id;
535 struct macb *bp = netdev_priv(dev);
536 u32 status;
538 status = macb_readl(bp, ISR);
540 if (unlikely(!status))
541 return IRQ_NONE;
543 spin_lock(&bp->lock);
545 while (status) {
546 /* close possible race with dev_close */
547 if (unlikely(!netif_running(dev))) {
548 macb_writel(bp, IDR, ~0UL);
549 break;
552 if (status & MACB_RX_INT_FLAGS) {
553 if (napi_schedule_prep(&bp->napi)) {
555 * There's no point taking any more interrupts
556 * until we have processed the buffers
558 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
559 dev_dbg(&bp->pdev->dev,
560 "scheduling RX softirq\n");
561 __napi_schedule(&bp->napi);
565 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
566 MACB_BIT(ISR_RLE)))
567 macb_tx(bp);
570 * Link change detection isn't possible with RMII, so we'll
571 * add that if/when we get our hands on a full-blown MII PHY.
574 if (status & MACB_BIT(HRESP)) {
576 * TODO: Reset the hardware, and maybe move the printk
577 * to a lower-priority context as well (work queue?)
579 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
580 dev->name);
583 status = macb_readl(bp, ISR);
586 spin_unlock(&bp->lock);
588 return IRQ_HANDLED;
591 #ifdef CONFIG_NET_POLL_CONTROLLER
593 * Polling receive - used by netconsole and other diagnostic tools
594 * to allow network i/o with interrupts disabled.
596 static void macb_poll_controller(struct net_device *dev)
598 unsigned long flags;
600 local_irq_save(flags);
601 macb_interrupt(dev->irq, dev);
602 local_irq_restore(flags);
604 #endif
606 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
608 struct macb *bp = netdev_priv(dev);
609 dma_addr_t mapping;
610 unsigned int len, entry;
611 u32 ctrl;
612 unsigned long flags;
614 #ifdef DEBUG
615 int i;
616 dev_dbg(&bp->pdev->dev,
617 "start_xmit: len %u head %p data %p tail %p end %p\n",
618 skb->len, skb->head, skb->data,
619 skb_tail_pointer(skb), skb_end_pointer(skb));
620 dev_dbg(&bp->pdev->dev,
621 "data:");
622 for (i = 0; i < 16; i++)
623 printk(" %02x", (unsigned int)skb->data[i]);
624 printk("\n");
625 #endif
627 len = skb->len;
628 spin_lock_irqsave(&bp->lock, flags);
630 /* This is a hard error, log it. */
631 if (TX_BUFFS_AVAIL(bp) < 1) {
632 netif_stop_queue(dev);
633 spin_unlock_irqrestore(&bp->lock, flags);
634 dev_err(&bp->pdev->dev,
635 "BUG! Tx Ring full when queue awake!\n");
636 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
637 bp->tx_head, bp->tx_tail);
638 return NETDEV_TX_BUSY;
641 entry = bp->tx_head;
642 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
643 mapping = dma_map_single(&bp->pdev->dev, skb->data,
644 len, DMA_TO_DEVICE);
645 bp->tx_skb[entry].skb = skb;
646 bp->tx_skb[entry].mapping = mapping;
647 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
648 skb->data, (unsigned long)mapping);
650 ctrl = MACB_BF(TX_FRMLEN, len);
651 ctrl |= MACB_BIT(TX_LAST);
652 if (entry == (TX_RING_SIZE - 1))
653 ctrl |= MACB_BIT(TX_WRAP);
655 bp->tx_ring[entry].addr = mapping;
656 bp->tx_ring[entry].ctrl = ctrl;
657 wmb();
659 entry = NEXT_TX(entry);
660 bp->tx_head = entry;
662 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
664 if (TX_BUFFS_AVAIL(bp) < 1)
665 netif_stop_queue(dev);
667 spin_unlock_irqrestore(&bp->lock, flags);
669 dev->trans_start = jiffies;
671 return NETDEV_TX_OK;
674 static void macb_free_consistent(struct macb *bp)
676 if (bp->tx_skb) {
677 kfree(bp->tx_skb);
678 bp->tx_skb = NULL;
680 if (bp->rx_ring) {
681 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
682 bp->rx_ring, bp->rx_ring_dma);
683 bp->rx_ring = NULL;
685 if (bp->tx_ring) {
686 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
687 bp->tx_ring, bp->tx_ring_dma);
688 bp->tx_ring = NULL;
690 if (bp->rx_buffers) {
691 dma_free_coherent(&bp->pdev->dev,
692 RX_RING_SIZE * RX_BUFFER_SIZE,
693 bp->rx_buffers, bp->rx_buffers_dma);
694 bp->rx_buffers = NULL;
698 static int macb_alloc_consistent(struct macb *bp)
700 int size;
702 size = TX_RING_SIZE * sizeof(struct ring_info);
703 bp->tx_skb = kmalloc(size, GFP_KERNEL);
704 if (!bp->tx_skb)
705 goto out_err;
707 size = RX_RING_BYTES;
708 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
709 &bp->rx_ring_dma, GFP_KERNEL);
710 if (!bp->rx_ring)
711 goto out_err;
712 dev_dbg(&bp->pdev->dev,
713 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
714 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
716 size = TX_RING_BYTES;
717 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
718 &bp->tx_ring_dma, GFP_KERNEL);
719 if (!bp->tx_ring)
720 goto out_err;
721 dev_dbg(&bp->pdev->dev,
722 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
723 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
725 size = RX_RING_SIZE * RX_BUFFER_SIZE;
726 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
727 &bp->rx_buffers_dma, GFP_KERNEL);
728 if (!bp->rx_buffers)
729 goto out_err;
730 dev_dbg(&bp->pdev->dev,
731 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
732 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
734 return 0;
736 out_err:
737 macb_free_consistent(bp);
738 return -ENOMEM;
741 static void macb_init_rings(struct macb *bp)
743 int i;
744 dma_addr_t addr;
746 addr = bp->rx_buffers_dma;
747 for (i = 0; i < RX_RING_SIZE; i++) {
748 bp->rx_ring[i].addr = addr;
749 bp->rx_ring[i].ctrl = 0;
750 addr += RX_BUFFER_SIZE;
752 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
754 for (i = 0; i < TX_RING_SIZE; i++) {
755 bp->tx_ring[i].addr = 0;
756 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
758 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
760 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
763 static void macb_reset_hw(struct macb *bp)
765 /* Make sure we have the write buffer for ourselves */
766 wmb();
769 * Disable RX and TX (XXX: Should we halt the transmission
770 * more gracefully?)
772 macb_writel(bp, NCR, 0);
774 /* Clear the stats registers (XXX: Update stats first?) */
775 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
777 /* Clear all status flags */
778 macb_writel(bp, TSR, ~0UL);
779 macb_writel(bp, RSR, ~0UL);
781 /* Disable all interrupts */
782 macb_writel(bp, IDR, ~0UL);
783 macb_readl(bp, ISR);
786 static void macb_init_hw(struct macb *bp)
788 u32 config;
790 macb_reset_hw(bp);
791 __macb_set_hwaddr(bp);
793 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
794 config |= MACB_BIT(PAE); /* PAuse Enable */
795 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
796 if (bp->dev->flags & IFF_PROMISC)
797 config |= MACB_BIT(CAF); /* Copy All Frames */
798 if (!(bp->dev->flags & IFF_BROADCAST))
799 config |= MACB_BIT(NBC); /* No BroadCast */
800 macb_writel(bp, NCFGR, config);
802 /* Initialize TX and RX buffers */
803 macb_writel(bp, RBQP, bp->rx_ring_dma);
804 macb_writel(bp, TBQP, bp->tx_ring_dma);
806 /* Enable TX and RX */
807 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
809 /* Enable interrupts */
810 macb_writel(bp, IER, (MACB_BIT(RCOMP)
811 | MACB_BIT(RXUBR)
812 | MACB_BIT(ISR_TUND)
813 | MACB_BIT(ISR_RLE)
814 | MACB_BIT(TXERR)
815 | MACB_BIT(TCOMP)
816 | MACB_BIT(ISR_ROVR)
817 | MACB_BIT(HRESP)));
822 * The hash address register is 64 bits long and takes up two
823 * locations in the memory map. The least significant bits are stored
824 * in EMAC_HSL and the most significant bits in EMAC_HSH.
826 * The unicast hash enable and the multicast hash enable bits in the
827 * network configuration register enable the reception of hash matched
828 * frames. The destination address is reduced to a 6 bit index into
829 * the 64 bit hash register using the following hash function. The
830 * hash function is an exclusive or of every sixth bit of the
831 * destination address.
833 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
834 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
835 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
836 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
837 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
838 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
840 * da[0] represents the least significant bit of the first byte
841 * received, that is, the multicast/unicast indicator, and da[47]
842 * represents the most significant bit of the last byte received. If
843 * the hash index, hi[n], points to a bit that is set in the hash
844 * register then the frame will be matched according to whether the
845 * frame is multicast or unicast. A multicast match will be signalled
846 * if the multicast hash enable bit is set, da[0] is 1 and the hash
847 * index points to a bit set in the hash register. A unicast match
848 * will be signalled if the unicast hash enable bit is set, da[0] is 0
849 * and the hash index points to a bit set in the hash register. To
850 * receive all multicast frames, the hash register should be set with
851 * all ones and the multicast hash enable bit should be set in the
852 * network configuration register.
855 static inline int hash_bit_value(int bitnr, __u8 *addr)
857 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
858 return 1;
859 return 0;
863 * Return the hash index value for the specified address.
865 static int hash_get_index(__u8 *addr)
867 int i, j, bitval;
868 int hash_index = 0;
870 for (j = 0; j < 6; j++) {
871 for (i = 0, bitval = 0; i < 8; i++)
872 bitval ^= hash_bit_value(i*6 + j, addr);
874 hash_index |= (bitval << j);
877 return hash_index;
881 * Add multicast addresses to the internal multicast-hash table.
883 static void macb_sethashtable(struct net_device *dev)
885 struct dev_mc_list *curr;
886 unsigned long mc_filter[2];
887 unsigned int bitnr;
888 struct macb *bp = netdev_priv(dev);
890 mc_filter[0] = mc_filter[1] = 0;
892 netdev_for_each_mc_addr(curr, dev) {
893 bitnr = hash_get_index(curr->dmi_addr);
894 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
897 macb_writel(bp, HRB, mc_filter[0]);
898 macb_writel(bp, HRT, mc_filter[1]);
902 * Enable/Disable promiscuous and multicast modes.
904 static void macb_set_rx_mode(struct net_device *dev)
906 unsigned long cfg;
907 struct macb *bp = netdev_priv(dev);
909 cfg = macb_readl(bp, NCFGR);
911 if (dev->flags & IFF_PROMISC)
912 /* Enable promiscuous mode */
913 cfg |= MACB_BIT(CAF);
914 else if (dev->flags & (~IFF_PROMISC))
915 /* Disable promiscuous mode */
916 cfg &= ~MACB_BIT(CAF);
918 if (dev->flags & IFF_ALLMULTI) {
919 /* Enable all multicast mode */
920 macb_writel(bp, HRB, -1);
921 macb_writel(bp, HRT, -1);
922 cfg |= MACB_BIT(NCFGR_MTI);
923 } else if (!netdev_mc_empty(dev)) {
924 /* Enable specific multicasts */
925 macb_sethashtable(dev);
926 cfg |= MACB_BIT(NCFGR_MTI);
927 } else if (dev->flags & (~IFF_ALLMULTI)) {
928 /* Disable all multicast mode */
929 macb_writel(bp, HRB, 0);
930 macb_writel(bp, HRT, 0);
931 cfg &= ~MACB_BIT(NCFGR_MTI);
934 macb_writel(bp, NCFGR, cfg);
937 static int macb_open(struct net_device *dev)
939 struct macb *bp = netdev_priv(dev);
940 int err;
942 dev_dbg(&bp->pdev->dev, "open\n");
944 /* if the phy is not yet register, retry later*/
945 if (!bp->phy_dev)
946 return -EAGAIN;
948 if (!is_valid_ether_addr(dev->dev_addr))
949 return -EADDRNOTAVAIL;
951 err = macb_alloc_consistent(bp);
952 if (err) {
953 printk(KERN_ERR
954 "%s: Unable to allocate DMA memory (error %d)\n",
955 dev->name, err);
956 return err;
959 napi_enable(&bp->napi);
961 macb_init_rings(bp);
962 macb_init_hw(bp);
964 /* schedule a link state check */
965 phy_start(bp->phy_dev);
967 netif_start_queue(dev);
969 return 0;
972 static int macb_close(struct net_device *dev)
974 struct macb *bp = netdev_priv(dev);
975 unsigned long flags;
977 netif_stop_queue(dev);
978 napi_disable(&bp->napi);
980 if (bp->phy_dev)
981 phy_stop(bp->phy_dev);
983 spin_lock_irqsave(&bp->lock, flags);
984 macb_reset_hw(bp);
985 netif_carrier_off(dev);
986 spin_unlock_irqrestore(&bp->lock, flags);
988 macb_free_consistent(bp);
990 return 0;
993 static struct net_device_stats *macb_get_stats(struct net_device *dev)
995 struct macb *bp = netdev_priv(dev);
996 struct net_device_stats *nstat = &bp->stats;
997 struct macb_stats *hwstat = &bp->hw_stats;
999 /* read stats from hardware */
1000 macb_update_stats(bp);
1002 /* Convert HW stats into netdevice stats */
1003 nstat->rx_errors = (hwstat->rx_fcs_errors +
1004 hwstat->rx_align_errors +
1005 hwstat->rx_resource_errors +
1006 hwstat->rx_overruns +
1007 hwstat->rx_oversize_pkts +
1008 hwstat->rx_jabbers +
1009 hwstat->rx_undersize_pkts +
1010 hwstat->sqe_test_errors +
1011 hwstat->rx_length_mismatch);
1012 nstat->tx_errors = (hwstat->tx_late_cols +
1013 hwstat->tx_excessive_cols +
1014 hwstat->tx_underruns +
1015 hwstat->tx_carrier_errors);
1016 nstat->collisions = (hwstat->tx_single_cols +
1017 hwstat->tx_multiple_cols +
1018 hwstat->tx_excessive_cols);
1019 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1020 hwstat->rx_jabbers +
1021 hwstat->rx_undersize_pkts +
1022 hwstat->rx_length_mismatch);
1023 nstat->rx_over_errors = hwstat->rx_resource_errors;
1024 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1025 nstat->rx_frame_errors = hwstat->rx_align_errors;
1026 nstat->rx_fifo_errors = hwstat->rx_overruns;
1027 /* XXX: What does "missed" mean? */
1028 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1029 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1030 nstat->tx_fifo_errors = hwstat->tx_underruns;
1031 /* Don't know about heartbeat or window errors... */
1033 return nstat;
1036 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1038 struct macb *bp = netdev_priv(dev);
1039 struct phy_device *phydev = bp->phy_dev;
1041 if (!phydev)
1042 return -ENODEV;
1044 return phy_ethtool_gset(phydev, cmd);
1047 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1049 struct macb *bp = netdev_priv(dev);
1050 struct phy_device *phydev = bp->phy_dev;
1052 if (!phydev)
1053 return -ENODEV;
1055 return phy_ethtool_sset(phydev, cmd);
1058 static void macb_get_drvinfo(struct net_device *dev,
1059 struct ethtool_drvinfo *info)
1061 struct macb *bp = netdev_priv(dev);
1063 strcpy(info->driver, bp->pdev->dev.driver->name);
1064 strcpy(info->version, "$Revision: 1.14 $");
1065 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
1068 static const struct ethtool_ops macb_ethtool_ops = {
1069 .get_settings = macb_get_settings,
1070 .set_settings = macb_set_settings,
1071 .get_drvinfo = macb_get_drvinfo,
1072 .get_link = ethtool_op_get_link,
1075 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1077 struct macb *bp = netdev_priv(dev);
1078 struct phy_device *phydev = bp->phy_dev;
1080 if (!netif_running(dev))
1081 return -EINVAL;
1083 if (!phydev)
1084 return -ENODEV;
1086 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
1089 static const struct net_device_ops macb_netdev_ops = {
1090 .ndo_open = macb_open,
1091 .ndo_stop = macb_close,
1092 .ndo_start_xmit = macb_start_xmit,
1093 .ndo_set_multicast_list = macb_set_rx_mode,
1094 .ndo_get_stats = macb_get_stats,
1095 .ndo_do_ioctl = macb_ioctl,
1096 .ndo_validate_addr = eth_validate_addr,
1097 .ndo_change_mtu = eth_change_mtu,
1098 .ndo_set_mac_address = eth_mac_addr,
1099 #ifdef CONFIG_NET_POLL_CONTROLLER
1100 .ndo_poll_controller = macb_poll_controller,
1101 #endif
1104 static int __init macb_probe(struct platform_device *pdev)
1106 struct eth_platform_data *pdata;
1107 struct resource *regs;
1108 struct net_device *dev;
1109 struct macb *bp;
1110 struct phy_device *phydev;
1111 unsigned long pclk_hz;
1112 u32 config;
1113 int err = -ENXIO;
1115 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1116 if (!regs) {
1117 dev_err(&pdev->dev, "no mmio resource defined\n");
1118 goto err_out;
1121 err = -ENOMEM;
1122 dev = alloc_etherdev(sizeof(*bp));
1123 if (!dev) {
1124 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1125 goto err_out;
1128 SET_NETDEV_DEV(dev, &pdev->dev);
1130 /* TODO: Actually, we have some interesting features... */
1131 dev->features |= 0;
1133 bp = netdev_priv(dev);
1134 bp->pdev = pdev;
1135 bp->dev = dev;
1137 spin_lock_init(&bp->lock);
1139 #if defined(CONFIG_ARCH_AT91)
1140 bp->pclk = clk_get(&pdev->dev, "macb_clk");
1141 if (IS_ERR(bp->pclk)) {
1142 dev_err(&pdev->dev, "failed to get macb_clk\n");
1143 goto err_out_free_dev;
1145 clk_enable(bp->pclk);
1146 #else
1147 bp->pclk = clk_get(&pdev->dev, "pclk");
1148 if (IS_ERR(bp->pclk)) {
1149 dev_err(&pdev->dev, "failed to get pclk\n");
1150 goto err_out_free_dev;
1152 bp->hclk = clk_get(&pdev->dev, "hclk");
1153 if (IS_ERR(bp->hclk)) {
1154 dev_err(&pdev->dev, "failed to get hclk\n");
1155 goto err_out_put_pclk;
1158 clk_enable(bp->pclk);
1159 clk_enable(bp->hclk);
1160 #endif
1162 bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
1163 if (!bp->regs) {
1164 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1165 err = -ENOMEM;
1166 goto err_out_disable_clocks;
1169 dev->irq = platform_get_irq(pdev, 0);
1170 err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
1171 dev->name, dev);
1172 if (err) {
1173 printk(KERN_ERR
1174 "%s: Unable to request IRQ %d (error %d)\n",
1175 dev->name, dev->irq, err);
1176 goto err_out_iounmap;
1179 dev->netdev_ops = &macb_netdev_ops;
1180 netif_napi_add(dev, &bp->napi, macb_poll, 64);
1181 dev->ethtool_ops = &macb_ethtool_ops;
1183 dev->base_addr = regs->start;
1185 /* Set MII management clock divider */
1186 pclk_hz = clk_get_rate(bp->pclk);
1187 if (pclk_hz <= 20000000)
1188 config = MACB_BF(CLK, MACB_CLK_DIV8);
1189 else if (pclk_hz <= 40000000)
1190 config = MACB_BF(CLK, MACB_CLK_DIV16);
1191 else if (pclk_hz <= 80000000)
1192 config = MACB_BF(CLK, MACB_CLK_DIV32);
1193 else
1194 config = MACB_BF(CLK, MACB_CLK_DIV64);
1195 macb_writel(bp, NCFGR, config);
1197 macb_get_hwaddr(bp);
1198 pdata = pdev->dev.platform_data;
1200 if (pdata && pdata->is_rmii)
1201 #if defined(CONFIG_ARCH_AT91)
1202 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1203 #else
1204 macb_writel(bp, USRIO, 0);
1205 #endif
1206 else
1207 #if defined(CONFIG_ARCH_AT91)
1208 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1209 #else
1210 macb_writel(bp, USRIO, MACB_BIT(MII));
1211 #endif
1213 bp->tx_pending = DEF_TX_RING_PENDING;
1215 err = register_netdev(dev);
1216 if (err) {
1217 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1218 goto err_out_free_irq;
1221 if (macb_mii_init(bp) != 0) {
1222 goto err_out_unregister_netdev;
1225 platform_set_drvdata(pdev, dev);
1227 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
1228 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
1230 phydev = bp->phy_dev;
1231 printk(KERN_INFO "%s: attached PHY driver [%s] "
1232 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
1233 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
1235 return 0;
1237 err_out_unregister_netdev:
1238 unregister_netdev(dev);
1239 err_out_free_irq:
1240 free_irq(dev->irq, dev);
1241 err_out_iounmap:
1242 iounmap(bp->regs);
1243 err_out_disable_clocks:
1244 #ifndef CONFIG_ARCH_AT91
1245 clk_disable(bp->hclk);
1246 clk_put(bp->hclk);
1247 #endif
1248 clk_disable(bp->pclk);
1249 #ifndef CONFIG_ARCH_AT91
1250 err_out_put_pclk:
1251 #endif
1252 clk_put(bp->pclk);
1253 err_out_free_dev:
1254 free_netdev(dev);
1255 err_out:
1256 platform_set_drvdata(pdev, NULL);
1257 return err;
1260 static int __exit macb_remove(struct platform_device *pdev)
1262 struct net_device *dev;
1263 struct macb *bp;
1265 dev = platform_get_drvdata(pdev);
1267 if (dev) {
1268 bp = netdev_priv(dev);
1269 if (bp->phy_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);
1276 iounmap(bp->regs);
1277 #ifndef CONFIG_ARCH_AT91
1278 clk_disable(bp->hclk);
1279 clk_put(bp->hclk);
1280 #endif
1281 clk_disable(bp->pclk);
1282 clk_put(bp->pclk);
1283 free_netdev(dev);
1284 platform_set_drvdata(pdev, NULL);
1287 return 0;
1290 #ifdef CONFIG_PM
1291 static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1293 struct net_device *netdev = platform_get_drvdata(pdev);
1294 struct macb *bp = netdev_priv(netdev);
1296 netif_device_detach(netdev);
1298 #ifndef CONFIG_ARCH_AT91
1299 clk_disable(bp->hclk);
1300 #endif
1301 clk_disable(bp->pclk);
1303 return 0;
1306 static int macb_resume(struct platform_device *pdev)
1308 struct net_device *netdev = platform_get_drvdata(pdev);
1309 struct macb *bp = netdev_priv(netdev);
1311 clk_enable(bp->pclk);
1312 #ifndef CONFIG_ARCH_AT91
1313 clk_enable(bp->hclk);
1314 #endif
1316 netif_device_attach(netdev);
1318 return 0;
1320 #else
1321 #define macb_suspend NULL
1322 #define macb_resume NULL
1323 #endif
1325 static struct platform_driver macb_driver = {
1326 .remove = __exit_p(macb_remove),
1327 .suspend = macb_suspend,
1328 .resume = macb_resume,
1329 .driver = {
1330 .name = "macb",
1331 .owner = THIS_MODULE,
1335 static int __init macb_init(void)
1337 return platform_driver_probe(&macb_driver, macb_probe);
1340 static void __exit macb_exit(void)
1342 platform_driver_unregister(&macb_driver);
1345 module_init(macb_init);
1346 module_exit(macb_exit);
1348 MODULE_LICENSE("GPL");
1349 MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
1350 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
1351 MODULE_ALIAS("platform:macb");