ieee1394: sbp2: fix sbp2_remove_device for error cases
[usb.git] / drivers / net / macb.c
bloba4bb0264180a4563d5bd59bd1ae7a2ad357bd7dc
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 <asm/arch/board.h>
25 #include <asm/arch/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));
87 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
89 struct macb *bp = bus->priv;
90 int value;
92 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
93 | MACB_BF(RW, MACB_MAN_READ)
94 | MACB_BF(PHYA, mii_id)
95 | MACB_BF(REGA, regnum)
96 | MACB_BF(CODE, MACB_MAN_CODE)));
98 /* wait for end of transfer */
99 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
100 cpu_relax();
102 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
104 return value;
107 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
108 u16 value)
110 struct macb *bp = bus->priv;
112 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
113 | MACB_BF(RW, MACB_MAN_WRITE)
114 | MACB_BF(PHYA, mii_id)
115 | MACB_BF(REGA, regnum)
116 | MACB_BF(CODE, MACB_MAN_CODE)
117 | MACB_BF(DATA, value)));
119 /* wait for end of transfer */
120 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
121 cpu_relax();
123 return 0;
126 static int macb_mdio_reset(struct mii_bus *bus)
128 return 0;
131 static void macb_handle_link_change(struct net_device *dev)
133 struct macb *bp = netdev_priv(dev);
134 struct phy_device *phydev = bp->phy_dev;
135 unsigned long flags;
137 int status_change = 0;
139 spin_lock_irqsave(&bp->lock, flags);
141 if (phydev->link) {
142 if ((bp->speed != phydev->speed) ||
143 (bp->duplex != phydev->duplex)) {
144 u32 reg;
146 reg = macb_readl(bp, NCFGR);
147 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
149 if (phydev->duplex)
150 reg |= MACB_BIT(FD);
151 if (phydev->speed)
152 reg |= MACB_BIT(SPD);
154 macb_writel(bp, NCFGR, reg);
156 bp->speed = phydev->speed;
157 bp->duplex = phydev->duplex;
158 status_change = 1;
162 if (phydev->link != bp->link) {
163 if (phydev->link)
164 netif_schedule(dev);
165 else {
166 bp->speed = 0;
167 bp->duplex = -1;
169 bp->link = phydev->link;
171 status_change = 1;
174 spin_unlock_irqrestore(&bp->lock, flags);
176 if (status_change) {
177 if (phydev->link)
178 printk(KERN_INFO "%s: link up (%d/%s)\n",
179 dev->name, phydev->speed,
180 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
181 else
182 printk(KERN_INFO "%s: link down\n", dev->name);
186 /* based on au1000_eth. c*/
187 static int macb_mii_probe(struct net_device *dev)
189 struct macb *bp = netdev_priv(dev);
190 struct phy_device *phydev = NULL;
191 struct eth_platform_data *pdata;
192 int phy_addr;
194 /* find the first phy */
195 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
196 if (bp->mii_bus.phy_map[phy_addr]) {
197 phydev = bp->mii_bus.phy_map[phy_addr];
198 break;
202 if (!phydev) {
203 printk (KERN_ERR "%s: no PHY found\n", dev->name);
204 return -1;
207 pdata = bp->pdev->dev.platform_data;
208 /* TODO : add pin_irq */
210 /* attach the mac to the phy */
211 if (pdata && pdata->is_rmii) {
212 phydev = phy_connect(dev, phydev->dev.bus_id,
213 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
214 } else {
215 phydev = phy_connect(dev, phydev->dev.bus_id,
216 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
219 if (IS_ERR(phydev)) {
220 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
221 return PTR_ERR(phydev);
224 /* mask with MAC supported features */
225 phydev->supported &= PHY_BASIC_FEATURES;
227 phydev->advertising = phydev->supported;
229 bp->link = 0;
230 bp->speed = 0;
231 bp->duplex = -1;
232 bp->phy_dev = phydev;
234 return 0;
237 static int macb_mii_init(struct macb *bp)
239 struct eth_platform_data *pdata;
240 int err = -ENXIO, i;
242 /* Enable managment port */
243 macb_writel(bp, NCR, MACB_BIT(MPE));
245 bp->mii_bus.name = "MACB_mii_bus",
246 bp->mii_bus.read = &macb_mdio_read,
247 bp->mii_bus.write = &macb_mdio_write,
248 bp->mii_bus.reset = &macb_mdio_reset,
249 bp->mii_bus.id = bp->pdev->id,
250 bp->mii_bus.priv = bp,
251 bp->mii_bus.dev = &bp->dev->dev;
252 pdata = bp->pdev->dev.platform_data;
254 if (pdata)
255 bp->mii_bus.phy_mask = pdata->phy_mask;
257 bp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
258 if (!bp->mii_bus.irq) {
259 err = -ENOMEM;
260 goto err_out;
263 for (i = 0; i < PHY_MAX_ADDR; i++)
264 bp->mii_bus.irq[i] = PHY_POLL;
266 platform_set_drvdata(bp->dev, &bp->mii_bus);
268 if (mdiobus_register(&bp->mii_bus))
269 goto err_out_free_mdio_irq;
271 if (macb_mii_probe(bp->dev) != 0) {
272 goto err_out_unregister_bus;
275 return 0;
277 err_out_unregister_bus:
278 mdiobus_unregister(&bp->mii_bus);
279 err_out_free_mdio_irq:
280 kfree(bp->mii_bus.irq);
281 err_out:
282 return err;
285 static void macb_update_stats(struct macb *bp)
287 u32 __iomem *reg = bp->regs + MACB_PFR;
288 u32 *p = &bp->hw_stats.rx_pause_frames;
289 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
291 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
293 for(; p < end; p++, reg++)
294 *p += __raw_readl(reg);
297 static void macb_tx(struct macb *bp)
299 unsigned int tail;
300 unsigned int head;
301 u32 status;
303 status = macb_readl(bp, TSR);
304 macb_writel(bp, TSR, status);
306 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
307 (unsigned long)status);
309 if (status & MACB_BIT(UND)) {
310 printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
311 bp->dev->name);
312 bp->tx_head = bp->tx_tail = 0;
315 if (!(status & MACB_BIT(COMP)))
317 * This may happen when a buffer becomes complete
318 * between reading the ISR and scanning the
319 * descriptors. Nothing to worry about.
321 return;
323 head = bp->tx_head;
324 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
325 struct ring_info *rp = &bp->tx_skb[tail];
326 struct sk_buff *skb = rp->skb;
327 u32 bufstat;
329 BUG_ON(skb == NULL);
331 rmb();
332 bufstat = bp->tx_ring[tail].ctrl;
334 if (!(bufstat & MACB_BIT(TX_USED)))
335 break;
337 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
338 tail, skb->data);
339 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
340 DMA_TO_DEVICE);
341 bp->stats.tx_packets++;
342 bp->stats.tx_bytes += skb->len;
343 rp->skb = NULL;
344 dev_kfree_skb_irq(skb);
347 bp->tx_tail = tail;
348 if (netif_queue_stopped(bp->dev) &&
349 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
350 netif_wake_queue(bp->dev);
353 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
354 unsigned int last_frag)
356 unsigned int len;
357 unsigned int frag;
358 unsigned int offset = 0;
359 struct sk_buff *skb;
361 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
363 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
364 first_frag, last_frag, len);
366 skb = dev_alloc_skb(len + RX_OFFSET);
367 if (!skb) {
368 bp->stats.rx_dropped++;
369 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
370 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
371 if (frag == last_frag)
372 break;
374 wmb();
375 return 1;
378 skb_reserve(skb, RX_OFFSET);
379 skb->ip_summed = CHECKSUM_NONE;
380 skb_put(skb, len);
382 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
383 unsigned int frag_len = RX_BUFFER_SIZE;
385 if (offset + frag_len > len) {
386 BUG_ON(frag != last_frag);
387 frag_len = len - offset;
389 skb_copy_to_linear_data_offset(skb, offset,
390 (bp->rx_buffers +
391 (RX_BUFFER_SIZE * frag)),
392 frag_len);
393 offset += RX_BUFFER_SIZE;
394 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
395 wmb();
397 if (frag == last_frag)
398 break;
401 skb->protocol = eth_type_trans(skb, bp->dev);
403 bp->stats.rx_packets++;
404 bp->stats.rx_bytes += len;
405 bp->dev->last_rx = jiffies;
406 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
407 skb->len, skb->csum);
408 netif_receive_skb(skb);
410 return 0;
413 /* Mark DMA descriptors from begin up to and not including end as unused */
414 static void discard_partial_frame(struct macb *bp, unsigned int begin,
415 unsigned int end)
417 unsigned int frag;
419 for (frag = begin; frag != end; frag = NEXT_RX(frag))
420 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
421 wmb();
424 * When this happens, the hardware stats registers for
425 * whatever caused this is updated, so we don't have to record
426 * anything.
430 static int macb_rx(struct macb *bp, int budget)
432 int received = 0;
433 unsigned int tail = bp->rx_tail;
434 int first_frag = -1;
436 for (; budget > 0; tail = NEXT_RX(tail)) {
437 u32 addr, ctrl;
439 rmb();
440 addr = bp->rx_ring[tail].addr;
441 ctrl = bp->rx_ring[tail].ctrl;
443 if (!(addr & MACB_BIT(RX_USED)))
444 break;
446 if (ctrl & MACB_BIT(RX_SOF)) {
447 if (first_frag != -1)
448 discard_partial_frame(bp, first_frag, tail);
449 first_frag = tail;
452 if (ctrl & MACB_BIT(RX_EOF)) {
453 int dropped;
454 BUG_ON(first_frag == -1);
456 dropped = macb_rx_frame(bp, first_frag, tail);
457 first_frag = -1;
458 if (!dropped) {
459 received++;
460 budget--;
465 if (first_frag != -1)
466 bp->rx_tail = first_frag;
467 else
468 bp->rx_tail = tail;
470 return received;
473 static int macb_poll(struct net_device *dev, int *budget)
475 struct macb *bp = netdev_priv(dev);
476 int orig_budget, work_done, retval = 0;
477 u32 status;
479 status = macb_readl(bp, RSR);
480 macb_writel(bp, RSR, status);
482 if (!status) {
484 * This may happen if an interrupt was pending before
485 * this function was called last time, and no packets
486 * have been received since.
488 netif_rx_complete(dev);
489 goto out;
492 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
493 (unsigned long)status, *budget);
495 if (!(status & MACB_BIT(REC))) {
496 dev_warn(&bp->pdev->dev,
497 "No RX buffers complete, status = %02lx\n",
498 (unsigned long)status);
499 netif_rx_complete(dev);
500 goto out;
503 orig_budget = *budget;
504 if (orig_budget > dev->quota)
505 orig_budget = dev->quota;
507 work_done = macb_rx(bp, orig_budget);
508 if (work_done < orig_budget) {
509 netif_rx_complete(dev);
510 retval = 0;
511 } else {
512 retval = 1;
516 * We've done what we can to clean the buffers. Make sure we
517 * get notified when new packets arrive.
519 out:
520 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
522 /* TODO: Handle errors */
524 return retval;
527 static irqreturn_t macb_interrupt(int irq, void *dev_id)
529 struct net_device *dev = dev_id;
530 struct macb *bp = netdev_priv(dev);
531 u32 status;
533 status = macb_readl(bp, ISR);
535 if (unlikely(!status))
536 return IRQ_NONE;
538 spin_lock(&bp->lock);
540 while (status) {
541 /* close possible race with dev_close */
542 if (unlikely(!netif_running(dev))) {
543 macb_writel(bp, IDR, ~0UL);
544 break;
547 if (status & MACB_RX_INT_FLAGS) {
548 if (netif_rx_schedule_prep(dev)) {
550 * There's no point taking any more interrupts
551 * until we have processed the buffers
553 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
554 dev_dbg(&bp->pdev->dev,
555 "scheduling RX softirq\n");
556 __netif_rx_schedule(dev);
560 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
561 macb_tx(bp);
564 * Link change detection isn't possible with RMII, so we'll
565 * add that if/when we get our hands on a full-blown MII PHY.
568 if (status & MACB_BIT(HRESP)) {
570 * TODO: Reset the hardware, and maybe move the printk
571 * to a lower-priority context as well (work queue?)
573 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
574 dev->name);
577 status = macb_readl(bp, ISR);
580 spin_unlock(&bp->lock);
582 return IRQ_HANDLED;
585 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
587 struct macb *bp = netdev_priv(dev);
588 dma_addr_t mapping;
589 unsigned int len, entry;
590 u32 ctrl;
592 #ifdef DEBUG
593 int i;
594 dev_dbg(&bp->pdev->dev,
595 "start_xmit: len %u head %p data %p tail %p end %p\n",
596 skb->len, skb->head, skb->data,
597 skb_tail_pointer(skb), skb_end_pointer(skb));
598 dev_dbg(&bp->pdev->dev,
599 "data:");
600 for (i = 0; i < 16; i++)
601 printk(" %02x", (unsigned int)skb->data[i]);
602 printk("\n");
603 #endif
605 len = skb->len;
606 spin_lock_irq(&bp->lock);
608 /* This is a hard error, log it. */
609 if (TX_BUFFS_AVAIL(bp) < 1) {
610 netif_stop_queue(dev);
611 spin_unlock_irq(&bp->lock);
612 dev_err(&bp->pdev->dev,
613 "BUG! Tx Ring full when queue awake!\n");
614 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
615 bp->tx_head, bp->tx_tail);
616 return 1;
619 entry = bp->tx_head;
620 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
621 mapping = dma_map_single(&bp->pdev->dev, skb->data,
622 len, DMA_TO_DEVICE);
623 bp->tx_skb[entry].skb = skb;
624 bp->tx_skb[entry].mapping = mapping;
625 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
626 skb->data, (unsigned long)mapping);
628 ctrl = MACB_BF(TX_FRMLEN, len);
629 ctrl |= MACB_BIT(TX_LAST);
630 if (entry == (TX_RING_SIZE - 1))
631 ctrl |= MACB_BIT(TX_WRAP);
633 bp->tx_ring[entry].addr = mapping;
634 bp->tx_ring[entry].ctrl = ctrl;
635 wmb();
637 entry = NEXT_TX(entry);
638 bp->tx_head = entry;
640 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
642 if (TX_BUFFS_AVAIL(bp) < 1)
643 netif_stop_queue(dev);
645 spin_unlock_irq(&bp->lock);
647 dev->trans_start = jiffies;
649 return 0;
652 static void macb_free_consistent(struct macb *bp)
654 if (bp->tx_skb) {
655 kfree(bp->tx_skb);
656 bp->tx_skb = NULL;
658 if (bp->rx_ring) {
659 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
660 bp->rx_ring, bp->rx_ring_dma);
661 bp->rx_ring = NULL;
663 if (bp->tx_ring) {
664 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
665 bp->tx_ring, bp->tx_ring_dma);
666 bp->tx_ring = NULL;
668 if (bp->rx_buffers) {
669 dma_free_coherent(&bp->pdev->dev,
670 RX_RING_SIZE * RX_BUFFER_SIZE,
671 bp->rx_buffers, bp->rx_buffers_dma);
672 bp->rx_buffers = NULL;
676 static int macb_alloc_consistent(struct macb *bp)
678 int size;
680 size = TX_RING_SIZE * sizeof(struct ring_info);
681 bp->tx_skb = kmalloc(size, GFP_KERNEL);
682 if (!bp->tx_skb)
683 goto out_err;
685 size = RX_RING_BYTES;
686 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
687 &bp->rx_ring_dma, GFP_KERNEL);
688 if (!bp->rx_ring)
689 goto out_err;
690 dev_dbg(&bp->pdev->dev,
691 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
692 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
694 size = TX_RING_BYTES;
695 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
696 &bp->tx_ring_dma, GFP_KERNEL);
697 if (!bp->tx_ring)
698 goto out_err;
699 dev_dbg(&bp->pdev->dev,
700 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
701 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
703 size = RX_RING_SIZE * RX_BUFFER_SIZE;
704 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
705 &bp->rx_buffers_dma, GFP_KERNEL);
706 if (!bp->rx_buffers)
707 goto out_err;
708 dev_dbg(&bp->pdev->dev,
709 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
710 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
712 return 0;
714 out_err:
715 macb_free_consistent(bp);
716 return -ENOMEM;
719 static void macb_init_rings(struct macb *bp)
721 int i;
722 dma_addr_t addr;
724 addr = bp->rx_buffers_dma;
725 for (i = 0; i < RX_RING_SIZE; i++) {
726 bp->rx_ring[i].addr = addr;
727 bp->rx_ring[i].ctrl = 0;
728 addr += RX_BUFFER_SIZE;
730 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
732 for (i = 0; i < TX_RING_SIZE; i++) {
733 bp->tx_ring[i].addr = 0;
734 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
736 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
738 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
741 static void macb_reset_hw(struct macb *bp)
743 /* Make sure we have the write buffer for ourselves */
744 wmb();
747 * Disable RX and TX (XXX: Should we halt the transmission
748 * more gracefully?)
750 macb_writel(bp, NCR, 0);
752 /* Clear the stats registers (XXX: Update stats first?) */
753 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
755 /* Clear all status flags */
756 macb_writel(bp, TSR, ~0UL);
757 macb_writel(bp, RSR, ~0UL);
759 /* Disable all interrupts */
760 macb_writel(bp, IDR, ~0UL);
761 macb_readl(bp, ISR);
764 static void macb_init_hw(struct macb *bp)
766 u32 config;
768 macb_reset_hw(bp);
769 __macb_set_hwaddr(bp);
771 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
772 config |= MACB_BIT(PAE); /* PAuse Enable */
773 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
774 if (bp->dev->flags & IFF_PROMISC)
775 config |= MACB_BIT(CAF); /* Copy All Frames */
776 if (!(bp->dev->flags & IFF_BROADCAST))
777 config |= MACB_BIT(NBC); /* No BroadCast */
778 macb_writel(bp, NCFGR, config);
780 /* Initialize TX and RX buffers */
781 macb_writel(bp, RBQP, bp->rx_ring_dma);
782 macb_writel(bp, TBQP, bp->tx_ring_dma);
784 /* Enable TX and RX */
785 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
787 /* Enable interrupts */
788 macb_writel(bp, IER, (MACB_BIT(RCOMP)
789 | MACB_BIT(RXUBR)
790 | MACB_BIT(ISR_TUND)
791 | MACB_BIT(ISR_RLE)
792 | MACB_BIT(TXERR)
793 | MACB_BIT(TCOMP)
794 | MACB_BIT(ISR_ROVR)
795 | MACB_BIT(HRESP)));
800 * The hash address register is 64 bits long and takes up two
801 * locations in the memory map. The least significant bits are stored
802 * in EMAC_HSL and the most significant bits in EMAC_HSH.
804 * The unicast hash enable and the multicast hash enable bits in the
805 * network configuration register enable the reception of hash matched
806 * frames. The destination address is reduced to a 6 bit index into
807 * the 64 bit hash register using the following hash function. The
808 * hash function is an exclusive or of every sixth bit of the
809 * destination address.
811 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
812 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
813 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
814 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
815 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
816 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
818 * da[0] represents the least significant bit of the first byte
819 * received, that is, the multicast/unicast indicator, and da[47]
820 * represents the most significant bit of the last byte received. If
821 * the hash index, hi[n], points to a bit that is set in the hash
822 * register then the frame will be matched according to whether the
823 * frame is multicast or unicast. A multicast match will be signalled
824 * if the multicast hash enable bit is set, da[0] is 1 and the hash
825 * index points to a bit set in the hash register. A unicast match
826 * will be signalled if the unicast hash enable bit is set, da[0] is 0
827 * and the hash index points to a bit set in the hash register. To
828 * receive all multicast frames, the hash register should be set with
829 * all ones and the multicast hash enable bit should be set in the
830 * network configuration register.
833 static inline int hash_bit_value(int bitnr, __u8 *addr)
835 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
836 return 1;
837 return 0;
841 * Return the hash index value for the specified address.
843 static int hash_get_index(__u8 *addr)
845 int i, j, bitval;
846 int hash_index = 0;
848 for (j = 0; j < 6; j++) {
849 for (i = 0, bitval = 0; i < 8; i++)
850 bitval ^= hash_bit_value(i*6 + j, addr);
852 hash_index |= (bitval << j);
855 return hash_index;
859 * Add multicast addresses to the internal multicast-hash table.
861 static void macb_sethashtable(struct net_device *dev)
863 struct dev_mc_list *curr;
864 unsigned long mc_filter[2];
865 unsigned int i, bitnr;
866 struct macb *bp = netdev_priv(dev);
868 mc_filter[0] = mc_filter[1] = 0;
870 curr = dev->mc_list;
871 for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
872 if (!curr) break; /* unexpected end of list */
874 bitnr = hash_get_index(curr->dmi_addr);
875 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
878 macb_writel(bp, HRB, mc_filter[0]);
879 macb_writel(bp, HRT, mc_filter[1]);
883 * Enable/Disable promiscuous and multicast modes.
885 static void macb_set_rx_mode(struct net_device *dev)
887 unsigned long cfg;
888 struct macb *bp = netdev_priv(dev);
890 cfg = macb_readl(bp, NCFGR);
892 if (dev->flags & IFF_PROMISC)
893 /* Enable promiscuous mode */
894 cfg |= MACB_BIT(CAF);
895 else if (dev->flags & (~IFF_PROMISC))
896 /* Disable promiscuous mode */
897 cfg &= ~MACB_BIT(CAF);
899 if (dev->flags & IFF_ALLMULTI) {
900 /* Enable all multicast mode */
901 macb_writel(bp, HRB, -1);
902 macb_writel(bp, HRT, -1);
903 cfg |= MACB_BIT(NCFGR_MTI);
904 } else if (dev->mc_count > 0) {
905 /* Enable specific multicasts */
906 macb_sethashtable(dev);
907 cfg |= MACB_BIT(NCFGR_MTI);
908 } else if (dev->flags & (~IFF_ALLMULTI)) {
909 /* Disable all multicast mode */
910 macb_writel(bp, HRB, 0);
911 macb_writel(bp, HRT, 0);
912 cfg &= ~MACB_BIT(NCFGR_MTI);
915 macb_writel(bp, NCFGR, cfg);
918 static int macb_open(struct net_device *dev)
920 struct macb *bp = netdev_priv(dev);
921 int err;
923 dev_dbg(&bp->pdev->dev, "open\n");
925 /* if the phy is not yet register, retry later*/
926 if (!bp->phy_dev)
927 return -EAGAIN;
929 if (!is_valid_ether_addr(dev->dev_addr))
930 return -EADDRNOTAVAIL;
932 err = macb_alloc_consistent(bp);
933 if (err) {
934 printk(KERN_ERR
935 "%s: Unable to allocate DMA memory (error %d)\n",
936 dev->name, err);
937 return err;
940 macb_init_rings(bp);
941 macb_init_hw(bp);
943 /* schedule a link state check */
944 phy_start(bp->phy_dev);
946 netif_start_queue(dev);
948 return 0;
951 static int macb_close(struct net_device *dev)
953 struct macb *bp = netdev_priv(dev);
954 unsigned long flags;
956 netif_stop_queue(dev);
958 if (bp->phy_dev)
959 phy_stop(bp->phy_dev);
961 spin_lock_irqsave(&bp->lock, flags);
962 macb_reset_hw(bp);
963 netif_carrier_off(dev);
964 spin_unlock_irqrestore(&bp->lock, flags);
966 macb_free_consistent(bp);
968 return 0;
971 static struct net_device_stats *macb_get_stats(struct net_device *dev)
973 struct macb *bp = netdev_priv(dev);
974 struct net_device_stats *nstat = &bp->stats;
975 struct macb_stats *hwstat = &bp->hw_stats;
977 /* read stats from hardware */
978 macb_update_stats(bp);
980 /* Convert HW stats into netdevice stats */
981 nstat->rx_errors = (hwstat->rx_fcs_errors +
982 hwstat->rx_align_errors +
983 hwstat->rx_resource_errors +
984 hwstat->rx_overruns +
985 hwstat->rx_oversize_pkts +
986 hwstat->rx_jabbers +
987 hwstat->rx_undersize_pkts +
988 hwstat->sqe_test_errors +
989 hwstat->rx_length_mismatch);
990 nstat->tx_errors = (hwstat->tx_late_cols +
991 hwstat->tx_excessive_cols +
992 hwstat->tx_underruns +
993 hwstat->tx_carrier_errors);
994 nstat->collisions = (hwstat->tx_single_cols +
995 hwstat->tx_multiple_cols +
996 hwstat->tx_excessive_cols);
997 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
998 hwstat->rx_jabbers +
999 hwstat->rx_undersize_pkts +
1000 hwstat->rx_length_mismatch);
1001 nstat->rx_over_errors = hwstat->rx_resource_errors;
1002 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1003 nstat->rx_frame_errors = hwstat->rx_align_errors;
1004 nstat->rx_fifo_errors = hwstat->rx_overruns;
1005 /* XXX: What does "missed" mean? */
1006 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1007 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1008 nstat->tx_fifo_errors = hwstat->tx_underruns;
1009 /* Don't know about heartbeat or window errors... */
1011 return nstat;
1014 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1016 struct macb *bp = netdev_priv(dev);
1017 struct phy_device *phydev = bp->phy_dev;
1019 if (!phydev)
1020 return -ENODEV;
1022 return phy_ethtool_gset(phydev, cmd);
1025 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1027 struct macb *bp = netdev_priv(dev);
1028 struct phy_device *phydev = bp->phy_dev;
1030 if (!phydev)
1031 return -ENODEV;
1033 return phy_ethtool_sset(phydev, cmd);
1036 static void macb_get_drvinfo(struct net_device *dev,
1037 struct ethtool_drvinfo *info)
1039 struct macb *bp = netdev_priv(dev);
1041 strcpy(info->driver, bp->pdev->dev.driver->name);
1042 strcpy(info->version, "$Revision: 1.14 $");
1043 strcpy(info->bus_info, bp->pdev->dev.bus_id);
1046 static struct ethtool_ops macb_ethtool_ops = {
1047 .get_settings = macb_get_settings,
1048 .set_settings = macb_set_settings,
1049 .get_drvinfo = macb_get_drvinfo,
1050 .get_link = ethtool_op_get_link,
1053 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1055 struct macb *bp = netdev_priv(dev);
1056 struct phy_device *phydev = bp->phy_dev;
1058 if (!netif_running(dev))
1059 return -EINVAL;
1061 if (!phydev)
1062 return -ENODEV;
1064 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
1067 static int __devinit macb_probe(struct platform_device *pdev)
1069 struct eth_platform_data *pdata;
1070 struct resource *regs;
1071 struct net_device *dev;
1072 struct macb *bp;
1073 struct phy_device *phydev;
1074 unsigned long pclk_hz;
1075 u32 config;
1076 int err = -ENXIO;
1078 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1079 if (!regs) {
1080 dev_err(&pdev->dev, "no mmio resource defined\n");
1081 goto err_out;
1084 err = -ENOMEM;
1085 dev = alloc_etherdev(sizeof(*bp));
1086 if (!dev) {
1087 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1088 goto err_out;
1091 SET_MODULE_OWNER(dev);
1092 SET_NETDEV_DEV(dev, &pdev->dev);
1094 /* TODO: Actually, we have some interesting features... */
1095 dev->features |= 0;
1097 bp = netdev_priv(dev);
1098 bp->pdev = pdev;
1099 bp->dev = dev;
1101 spin_lock_init(&bp->lock);
1103 #if defined(CONFIG_ARCH_AT91)
1104 bp->pclk = clk_get(&pdev->dev, "macb_clk");
1105 if (IS_ERR(bp->pclk)) {
1106 dev_err(&pdev->dev, "failed to get macb_clk\n");
1107 goto err_out_free_dev;
1109 clk_enable(bp->pclk);
1110 #else
1111 bp->pclk = clk_get(&pdev->dev, "pclk");
1112 if (IS_ERR(bp->pclk)) {
1113 dev_err(&pdev->dev, "failed to get pclk\n");
1114 goto err_out_free_dev;
1116 bp->hclk = clk_get(&pdev->dev, "hclk");
1117 if (IS_ERR(bp->hclk)) {
1118 dev_err(&pdev->dev, "failed to get hclk\n");
1119 goto err_out_put_pclk;
1122 clk_enable(bp->pclk);
1123 clk_enable(bp->hclk);
1124 #endif
1126 bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
1127 if (!bp->regs) {
1128 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1129 err = -ENOMEM;
1130 goto err_out_disable_clocks;
1133 dev->irq = platform_get_irq(pdev, 0);
1134 err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
1135 dev->name, dev);
1136 if (err) {
1137 printk(KERN_ERR
1138 "%s: Unable to request IRQ %d (error %d)\n",
1139 dev->name, dev->irq, err);
1140 goto err_out_iounmap;
1143 dev->open = macb_open;
1144 dev->stop = macb_close;
1145 dev->hard_start_xmit = macb_start_xmit;
1146 dev->get_stats = macb_get_stats;
1147 dev->set_multicast_list = macb_set_rx_mode;
1148 dev->do_ioctl = macb_ioctl;
1149 dev->poll = macb_poll;
1150 dev->weight = 64;
1151 dev->ethtool_ops = &macb_ethtool_ops;
1153 dev->base_addr = regs->start;
1155 /* Set MII management clock divider */
1156 pclk_hz = clk_get_rate(bp->pclk);
1157 if (pclk_hz <= 20000000)
1158 config = MACB_BF(CLK, MACB_CLK_DIV8);
1159 else if (pclk_hz <= 40000000)
1160 config = MACB_BF(CLK, MACB_CLK_DIV16);
1161 else if (pclk_hz <= 80000000)
1162 config = MACB_BF(CLK, MACB_CLK_DIV32);
1163 else
1164 config = MACB_BF(CLK, MACB_CLK_DIV64);
1165 macb_writel(bp, NCFGR, config);
1167 macb_get_hwaddr(bp);
1168 pdata = pdev->dev.platform_data;
1170 if (pdata && pdata->is_rmii)
1171 #if defined(CONFIG_ARCH_AT91)
1172 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1173 #else
1174 macb_writel(bp, USRIO, 0);
1175 #endif
1176 else
1177 #if defined(CONFIG_ARCH_AT91)
1178 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1179 #else
1180 macb_writel(bp, USRIO, MACB_BIT(MII));
1181 #endif
1183 bp->tx_pending = DEF_TX_RING_PENDING;
1185 err = register_netdev(dev);
1186 if (err) {
1187 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1188 goto err_out_free_irq;
1191 if (macb_mii_init(bp) != 0) {
1192 goto err_out_unregister_netdev;
1195 platform_set_drvdata(pdev, dev);
1197 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d "
1198 "(%02x:%02x:%02x:%02x:%02x:%02x)\n",
1199 dev->name, dev->base_addr, dev->irq,
1200 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1201 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1203 phydev = bp->phy_dev;
1204 printk(KERN_INFO "%s: attached PHY driver [%s] "
1205 "(mii_bus:phy_addr=%s, irq=%d)\n",
1206 dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
1208 return 0;
1210 err_out_unregister_netdev:
1211 unregister_netdev(dev);
1212 err_out_free_irq:
1213 free_irq(dev->irq, dev);
1214 err_out_iounmap:
1215 iounmap(bp->regs);
1216 err_out_disable_clocks:
1217 #ifndef CONFIG_ARCH_AT91
1218 clk_disable(bp->hclk);
1219 clk_put(bp->hclk);
1220 #endif
1221 clk_disable(bp->pclk);
1222 #ifndef CONFIG_ARCH_AT91
1223 err_out_put_pclk:
1224 #endif
1225 clk_put(bp->pclk);
1226 err_out_free_dev:
1227 free_netdev(dev);
1228 err_out:
1229 platform_set_drvdata(pdev, NULL);
1230 return err;
1233 static int __devexit macb_remove(struct platform_device *pdev)
1235 struct net_device *dev;
1236 struct macb *bp;
1238 dev = platform_get_drvdata(pdev);
1240 if (dev) {
1241 bp = netdev_priv(dev);
1242 mdiobus_unregister(&bp->mii_bus);
1243 kfree(bp->mii_bus.irq);
1244 unregister_netdev(dev);
1245 free_irq(dev->irq, dev);
1246 iounmap(bp->regs);
1247 #ifndef CONFIG_ARCH_AT91
1248 clk_disable(bp->hclk);
1249 clk_put(bp->hclk);
1250 #endif
1251 clk_disable(bp->pclk);
1252 clk_put(bp->pclk);
1253 free_netdev(dev);
1254 platform_set_drvdata(pdev, NULL);
1257 return 0;
1260 static struct platform_driver macb_driver = {
1261 .probe = macb_probe,
1262 .remove = __devexit_p(macb_remove),
1263 .driver = {
1264 .name = "macb",
1268 static int __init macb_init(void)
1270 return platform_driver_register(&macb_driver);
1273 static void __exit macb_exit(void)
1275 platform_driver_unregister(&macb_driver);
1278 module_init(macb_init);
1279 module_exit(macb_exit);
1281 MODULE_LICENSE("GPL");
1282 MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
1283 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");