2 * Cadence MACB/GEM 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/platform_data/macb.h>
24 #include <linux/platform_device.h>
25 #include <linux/phy.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) */
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) \
56 static void __macb_set_hwaddr(struct macb *bp)
61 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
62 macb_or_gem_writel(bp, SA1B, bottom);
63 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
64 macb_or_gem_writel(bp, SA1T, top);
67 static void __init macb_get_hwaddr(struct macb *bp)
73 bottom = macb_or_gem_readl(bp, SA1B);
74 top = macb_or_gem_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;
81 addr[5] = (top >> 8) & 0xff;
83 if (is_valid_ether_addr(addr)) {
84 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
86 netdev_info(bp->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;
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)))
106 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
111 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
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)))
130 static int macb_mdio_reset(struct mii_bus *bus)
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;
141 int status_change = 0;
143 spin_lock_irqsave(&bp->lock, flags);
146 if ((bp->speed != phydev->speed) ||
147 (bp->duplex != phydev->duplex)) {
150 reg = macb_readl(bp, NCFGR);
151 reg &= ~(MACB_BIT(SPD) | 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;
166 if (phydev->link != bp->link) {
171 bp->link = phydev->link;
176 spin_unlock_irqrestore(&bp->lock, flags);
180 netdev_info(dev, "link up (%d/%s)\n",
182 phydev->duplex == DUPLEX_FULL ?
185 netdev_info(dev, "link down\n");
189 /* based on au1000_eth. c*/
190 static int macb_mii_probe(struct net_device *dev)
192 struct macb *bp = netdev_priv(dev);
193 struct phy_device *phydev;
194 struct macb_platform_data *pdata;
197 phydev = phy_find_first(bp->mii_bus);
199 netdev_err(dev, "no PHY found\n");
203 pdata = bp->pdev->dev.platform_data;
204 /* TODO : add pin_irq */
206 /* attach the mac to the phy */
207 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
208 pdata && pdata->is_rmii ?
209 PHY_INTERFACE_MODE_RMII :
210 PHY_INTERFACE_MODE_MII);
212 netdev_err(dev, "Could not attach to PHY\n");
216 /* mask with MAC supported features */
217 phydev->supported &= PHY_BASIC_FEATURES;
219 phydev->advertising = phydev->supported;
224 bp->phy_dev = phydev;
229 static int macb_mii_init(struct macb *bp)
231 struct macb_platform_data *pdata;
234 /* Enable management port */
235 macb_writel(bp, NCR, MACB_BIT(MPE));
237 bp->mii_bus = mdiobus_alloc();
238 if (bp->mii_bus == NULL) {
243 bp->mii_bus->name = "MACB_mii_bus";
244 bp->mii_bus->read = &macb_mdio_read;
245 bp->mii_bus->write = &macb_mdio_write;
246 bp->mii_bus->reset = &macb_mdio_reset;
247 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
248 bp->mii_bus->priv = bp;
249 bp->mii_bus->parent = &bp->dev->dev;
250 pdata = bp->pdev->dev.platform_data;
253 bp->mii_bus->phy_mask = pdata->phy_mask;
255 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
256 if (!bp->mii_bus->irq) {
258 goto err_out_free_mdiobus;
261 for (i = 0; i < PHY_MAX_ADDR; i++)
262 bp->mii_bus->irq[i] = PHY_POLL;
264 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
266 if (mdiobus_register(bp->mii_bus))
267 goto err_out_free_mdio_irq;
269 if (macb_mii_probe(bp->dev) != 0) {
270 goto err_out_unregister_bus;
275 err_out_unregister_bus:
276 mdiobus_unregister(bp->mii_bus);
277 err_out_free_mdio_irq:
278 kfree(bp->mii_bus->irq);
279 err_out_free_mdiobus:
280 mdiobus_free(bp->mii_bus);
285 static void macb_update_stats(struct macb *bp)
287 u32 __iomem *reg = bp->regs + MACB_PFR;
288 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
289 u32 *end = &bp->hw_stats.macb.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)
303 status = macb_readl(bp, TSR);
304 macb_writel(bp, TSR, status);
306 netdev_dbg(bp->dev, "macb_tx status = %02lx\n", (unsigned long)status);
308 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
310 netdev_err(bp->dev, "TX %s, resetting buffers\n",
311 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));
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);
325 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
327 /* free transmit buffer in upper layer*/
328 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
329 struct ring_info *rp = &bp->tx_skb[tail];
330 struct sk_buff *skb = rp->skb;
336 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
339 dev_kfree_skb_irq(skb);
342 bp->tx_head = bp->tx_tail = 0;
344 /* Enable the transmitter again */
345 if (status & MACB_BIT(TGO))
346 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
349 if (!(status & MACB_BIT(COMP)))
351 * This may happen when a buffer becomes complete
352 * between reading the ISR and scanning the
353 * descriptors. Nothing to worry about.
358 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
359 struct ring_info *rp = &bp->tx_skb[tail];
360 struct sk_buff *skb = rp->skb;
366 bufstat = bp->tx_ring[tail].ctrl;
368 if (!(bufstat & MACB_BIT(TX_USED)))
371 netdev_dbg(bp->dev, "skb %u (data %p) TX complete\n",
373 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
375 bp->stats.tx_packets++;
376 bp->stats.tx_bytes += skb->len;
378 dev_kfree_skb_irq(skb);
382 if (netif_queue_stopped(bp->dev) &&
383 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
384 netif_wake_queue(bp->dev);
387 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
388 unsigned int last_frag)
392 unsigned int offset = 0;
395 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
397 netdev_dbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
398 first_frag, last_frag, len);
400 skb = dev_alloc_skb(len + RX_OFFSET);
402 bp->stats.rx_dropped++;
403 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
404 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
405 if (frag == last_frag)
412 skb_reserve(skb, RX_OFFSET);
413 skb_checksum_none_assert(skb);
416 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
417 unsigned int frag_len = RX_BUFFER_SIZE;
419 if (offset + frag_len > len) {
420 BUG_ON(frag != last_frag);
421 frag_len = len - offset;
423 skb_copy_to_linear_data_offset(skb, offset,
425 (RX_BUFFER_SIZE * frag)),
427 offset += RX_BUFFER_SIZE;
428 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
431 if (frag == last_frag)
435 skb->protocol = eth_type_trans(skb, bp->dev);
437 bp->stats.rx_packets++;
438 bp->stats.rx_bytes += len;
439 netdev_dbg(bp->dev, "received skb of length %u, csum: %08x\n",
440 skb->len, skb->csum);
441 netif_receive_skb(skb);
446 /* Mark DMA descriptors from begin up to and not including end as unused */
447 static void discard_partial_frame(struct macb *bp, unsigned int begin,
452 for (frag = begin; frag != end; frag = NEXT_RX(frag))
453 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
457 * When this happens, the hardware stats registers for
458 * whatever caused this is updated, so we don't have to record
463 static int macb_rx(struct macb *bp, int budget)
466 unsigned int tail = bp->rx_tail;
469 for (; budget > 0; tail = NEXT_RX(tail)) {
473 addr = bp->rx_ring[tail].addr;
474 ctrl = bp->rx_ring[tail].ctrl;
476 if (!(addr & MACB_BIT(RX_USED)))
479 if (ctrl & MACB_BIT(RX_SOF)) {
480 if (first_frag != -1)
481 discard_partial_frame(bp, first_frag, tail);
485 if (ctrl & MACB_BIT(RX_EOF)) {
487 BUG_ON(first_frag == -1);
489 dropped = macb_rx_frame(bp, first_frag, tail);
498 if (first_frag != -1)
499 bp->rx_tail = first_frag;
506 static int macb_poll(struct napi_struct *napi, int budget)
508 struct macb *bp = container_of(napi, struct macb, napi);
512 status = macb_readl(bp, RSR);
513 macb_writel(bp, RSR, status);
517 netdev_dbg(bp->dev, "poll: status = %08lx, budget = %d\n",
518 (unsigned long)status, budget);
520 work_done = macb_rx(bp, budget);
521 if (work_done < budget) {
525 * We've done what we can to clean the buffers. Make sure we
526 * get notified when new packets arrive.
528 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
531 /* TODO: Handle errors */
536 static irqreturn_t macb_interrupt(int irq, void *dev_id)
538 struct net_device *dev = dev_id;
539 struct macb *bp = netdev_priv(dev);
542 status = macb_readl(bp, ISR);
544 if (unlikely(!status))
547 spin_lock(&bp->lock);
550 /* close possible race with dev_close */
551 if (unlikely(!netif_running(dev))) {
552 macb_writel(bp, IDR, ~0UL);
556 if (status & MACB_RX_INT_FLAGS) {
558 * There's no point taking any more interrupts
559 * until we have processed the buffers. The
560 * scheduling call may fail if the poll routine
561 * is already scheduled, so disable interrupts
564 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
566 if (napi_schedule_prep(&bp->napi)) {
567 netdev_dbg(bp->dev, "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 */
584 bp->hw_stats.gem.rx_overruns++;
586 bp->hw_stats.macb.rx_overruns++;
589 if (status & MACB_BIT(HRESP)) {
591 * TODO: Reset the hardware, and maybe move the
592 * netdev_err to a lower-priority context as well
595 netdev_err(dev, "DMA bus error: HRESP not OK\n");
598 status = macb_readl(bp, ISR);
601 spin_unlock(&bp->lock);
606 #ifdef CONFIG_NET_POLL_CONTROLLER
608 * Polling receive - used by netconsole and other diagnostic tools
609 * to allow network i/o with interrupts disabled.
611 static void macb_poll_controller(struct net_device *dev)
615 local_irq_save(flags);
616 macb_interrupt(dev->irq, dev);
617 local_irq_restore(flags);
621 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
623 struct macb *bp = netdev_priv(dev);
625 unsigned int len, entry;
631 "start_xmit: len %u head %p data %p tail %p end %p\n",
632 skb->len, skb->head, skb->data,
633 skb_tail_pointer(skb), skb_end_pointer(skb));
634 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
635 skb->data, 16, true);
639 spin_lock_irqsave(&bp->lock, flags);
641 /* This is a hard error, log it. */
642 if (TX_BUFFS_AVAIL(bp) < 1) {
643 netif_stop_queue(dev);
644 spin_unlock_irqrestore(&bp->lock, flags);
645 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
646 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
647 bp->tx_head, bp->tx_tail);
648 return NETDEV_TX_BUSY;
652 netdev_dbg(bp->dev, "Allocated ring entry %u\n", entry);
653 mapping = dma_map_single(&bp->pdev->dev, skb->data,
655 bp->tx_skb[entry].skb = skb;
656 bp->tx_skb[entry].mapping = mapping;
657 netdev_dbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
658 skb->data, (unsigned long)mapping);
660 ctrl = MACB_BF(TX_FRMLEN, len);
661 ctrl |= MACB_BIT(TX_LAST);
662 if (entry == (TX_RING_SIZE - 1))
663 ctrl |= MACB_BIT(TX_WRAP);
665 bp->tx_ring[entry].addr = mapping;
666 bp->tx_ring[entry].ctrl = ctrl;
669 entry = NEXT_TX(entry);
672 skb_tx_timestamp(skb);
674 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
676 if (TX_BUFFS_AVAIL(bp) < 1)
677 netif_stop_queue(dev);
679 spin_unlock_irqrestore(&bp->lock, flags);
684 static void macb_free_consistent(struct macb *bp)
691 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
692 bp->rx_ring, bp->rx_ring_dma);
696 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
697 bp->tx_ring, bp->tx_ring_dma);
700 if (bp->rx_buffers) {
701 dma_free_coherent(&bp->pdev->dev,
702 RX_RING_SIZE * RX_BUFFER_SIZE,
703 bp->rx_buffers, bp->rx_buffers_dma);
704 bp->rx_buffers = NULL;
708 static int macb_alloc_consistent(struct macb *bp)
712 size = TX_RING_SIZE * sizeof(struct ring_info);
713 bp->tx_skb = kmalloc(size, GFP_KERNEL);
717 size = RX_RING_BYTES;
718 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
719 &bp->rx_ring_dma, GFP_KERNEL);
723 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
724 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
726 size = TX_RING_BYTES;
727 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
728 &bp->tx_ring_dma, GFP_KERNEL);
732 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
733 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
735 size = RX_RING_SIZE * RX_BUFFER_SIZE;
736 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
737 &bp->rx_buffers_dma, GFP_KERNEL);
741 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
742 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
747 macb_free_consistent(bp);
751 static void macb_init_rings(struct macb *bp)
756 addr = bp->rx_buffers_dma;
757 for (i = 0; i < RX_RING_SIZE; i++) {
758 bp->rx_ring[i].addr = addr;
759 bp->rx_ring[i].ctrl = 0;
760 addr += RX_BUFFER_SIZE;
762 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
764 for (i = 0; i < TX_RING_SIZE; i++) {
765 bp->tx_ring[i].addr = 0;
766 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
768 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
770 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
773 static void macb_reset_hw(struct macb *bp)
775 /* Make sure we have the write buffer for ourselves */
779 * Disable RX and TX (XXX: Should we halt the transmission
782 macb_writel(bp, NCR, 0);
784 /* Clear the stats registers (XXX: Update stats first?) */
785 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
787 /* Clear all status flags */
788 macb_writel(bp, TSR, ~0UL);
789 macb_writel(bp, RSR, ~0UL);
791 /* Disable all interrupts */
792 macb_writel(bp, IDR, ~0UL);
796 static u32 gem_mdc_clk_div(struct macb *bp)
799 unsigned long pclk_hz = clk_get_rate(bp->pclk);
801 if (pclk_hz <= 20000000)
802 config = GEM_BF(CLK, GEM_CLK_DIV8);
803 else if (pclk_hz <= 40000000)
804 config = GEM_BF(CLK, GEM_CLK_DIV16);
805 else if (pclk_hz <= 80000000)
806 config = GEM_BF(CLK, GEM_CLK_DIV32);
807 else if (pclk_hz <= 120000000)
808 config = GEM_BF(CLK, GEM_CLK_DIV48);
809 else if (pclk_hz <= 160000000)
810 config = GEM_BF(CLK, GEM_CLK_DIV64);
812 config = GEM_BF(CLK, GEM_CLK_DIV96);
817 static u32 macb_mdc_clk_div(struct macb *bp)
820 unsigned long pclk_hz;
823 return gem_mdc_clk_div(bp);
825 pclk_hz = clk_get_rate(bp->pclk);
826 if (pclk_hz <= 20000000)
827 config = MACB_BF(CLK, MACB_CLK_DIV8);
828 else if (pclk_hz <= 40000000)
829 config = MACB_BF(CLK, MACB_CLK_DIV16);
830 else if (pclk_hz <= 80000000)
831 config = MACB_BF(CLK, MACB_CLK_DIV32);
833 config = MACB_BF(CLK, MACB_CLK_DIV64);
839 * Get the DMA bus width field of the network configuration register that we
840 * should program. We find the width from decoding the design configuration
841 * register to find the maximum supported data bus width.
843 static u32 macb_dbw(struct macb *bp)
845 if (!macb_is_gem(bp))
848 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
850 return GEM_BF(DBW, GEM_DBW128);
852 return GEM_BF(DBW, GEM_DBW64);
855 return GEM_BF(DBW, GEM_DBW32);
859 static void macb_init_hw(struct macb *bp)
864 __macb_set_hwaddr(bp);
866 config = macb_mdc_clk_div(bp);
867 config |= MACB_BIT(PAE); /* PAuse Enable */
868 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
869 config |= MACB_BIT(BIG); /* Receive oversized frames */
870 if (bp->dev->flags & IFF_PROMISC)
871 config |= MACB_BIT(CAF); /* Copy All Frames */
872 if (!(bp->dev->flags & IFF_BROADCAST))
873 config |= MACB_BIT(NBC); /* No BroadCast */
874 config |= macb_dbw(bp);
875 macb_writel(bp, NCFGR, config);
877 /* Initialize TX and RX buffers */
878 macb_writel(bp, RBQP, bp->rx_ring_dma);
879 macb_writel(bp, TBQP, bp->tx_ring_dma);
881 /* Enable TX and RX */
882 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
884 /* Enable interrupts */
885 macb_writel(bp, IER, (MACB_BIT(RCOMP)
897 * The hash address register is 64 bits long and takes up two
898 * locations in the memory map. The least significant bits are stored
899 * in EMAC_HSL and the most significant bits in EMAC_HSH.
901 * The unicast hash enable and the multicast hash enable bits in the
902 * network configuration register enable the reception of hash matched
903 * frames. The destination address is reduced to a 6 bit index into
904 * the 64 bit hash register using the following hash function. The
905 * hash function is an exclusive or of every sixth bit of the
906 * destination address.
908 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
909 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
910 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
911 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
912 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
913 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
915 * da[0] represents the least significant bit of the first byte
916 * received, that is, the multicast/unicast indicator, and da[47]
917 * represents the most significant bit of the last byte received. If
918 * the hash index, hi[n], points to a bit that is set in the hash
919 * register then the frame will be matched according to whether the
920 * frame is multicast or unicast. A multicast match will be signalled
921 * if the multicast hash enable bit is set, da[0] is 1 and the hash
922 * index points to a bit set in the hash register. A unicast match
923 * will be signalled if the unicast hash enable bit is set, da[0] is 0
924 * and the hash index points to a bit set in the hash register. To
925 * receive all multicast frames, the hash register should be set with
926 * all ones and the multicast hash enable bit should be set in the
927 * network configuration register.
930 static inline int hash_bit_value(int bitnr, __u8 *addr)
932 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
938 * Return the hash index value for the specified address.
940 static int hash_get_index(__u8 *addr)
945 for (j = 0; j < 6; j++) {
946 for (i = 0, bitval = 0; i < 8; i++)
947 bitval ^= hash_bit_value(i*6 + j, addr);
949 hash_index |= (bitval << j);
956 * Add multicast addresses to the internal multicast-hash table.
958 static void macb_sethashtable(struct net_device *dev)
960 struct netdev_hw_addr *ha;
961 unsigned long mc_filter[2];
963 struct macb *bp = netdev_priv(dev);
965 mc_filter[0] = mc_filter[1] = 0;
967 netdev_for_each_mc_addr(ha, dev) {
968 bitnr = hash_get_index(ha->addr);
969 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
972 macb_or_gem_writel(bp, HRB, mc_filter[0]);
973 macb_or_gem_writel(bp, HRT, mc_filter[1]);
977 * Enable/Disable promiscuous and multicast modes.
979 static void macb_set_rx_mode(struct net_device *dev)
982 struct macb *bp = netdev_priv(dev);
984 cfg = macb_readl(bp, NCFGR);
986 if (dev->flags & IFF_PROMISC)
987 /* Enable promiscuous mode */
988 cfg |= MACB_BIT(CAF);
989 else if (dev->flags & (~IFF_PROMISC))
990 /* Disable promiscuous mode */
991 cfg &= ~MACB_BIT(CAF);
993 if (dev->flags & IFF_ALLMULTI) {
994 /* Enable all multicast mode */
995 macb_or_gem_writel(bp, HRB, -1);
996 macb_or_gem_writel(bp, HRT, -1);
997 cfg |= MACB_BIT(NCFGR_MTI);
998 } else if (!netdev_mc_empty(dev)) {
999 /* Enable specific multicasts */
1000 macb_sethashtable(dev);
1001 cfg |= MACB_BIT(NCFGR_MTI);
1002 } else if (dev->flags & (~IFF_ALLMULTI)) {
1003 /* Disable all multicast mode */
1004 macb_or_gem_writel(bp, HRB, 0);
1005 macb_or_gem_writel(bp, HRT, 0);
1006 cfg &= ~MACB_BIT(NCFGR_MTI);
1009 macb_writel(bp, NCFGR, cfg);
1012 static int macb_open(struct net_device *dev)
1014 struct macb *bp = netdev_priv(dev);
1017 netdev_dbg(bp->dev, "open\n");
1019 /* if the phy is not yet register, retry later*/
1023 if (!is_valid_ether_addr(dev->dev_addr))
1024 return -EADDRNOTAVAIL;
1026 err = macb_alloc_consistent(bp);
1028 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1033 napi_enable(&bp->napi);
1035 macb_init_rings(bp);
1038 /* schedule a link state check */
1039 phy_start(bp->phy_dev);
1041 netif_start_queue(dev);
1046 static int macb_close(struct net_device *dev)
1048 struct macb *bp = netdev_priv(dev);
1049 unsigned long flags;
1051 netif_stop_queue(dev);
1052 napi_disable(&bp->napi);
1055 phy_stop(bp->phy_dev);
1057 spin_lock_irqsave(&bp->lock, flags);
1059 netif_carrier_off(dev);
1060 spin_unlock_irqrestore(&bp->lock, flags);
1062 macb_free_consistent(bp);
1067 static void gem_update_stats(struct macb *bp)
1069 u32 __iomem *reg = bp->regs + GEM_OTX;
1070 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1071 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1073 for (; p < end; p++, reg++)
1074 *p += __raw_readl(reg);
1077 static struct net_device_stats *gem_get_stats(struct macb *bp)
1079 struct gem_stats *hwstat = &bp->hw_stats.gem;
1080 struct net_device_stats *nstat = &bp->stats;
1082 gem_update_stats(bp);
1084 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1085 hwstat->rx_alignment_errors +
1086 hwstat->rx_resource_errors +
1087 hwstat->rx_overruns +
1088 hwstat->rx_oversize_frames +
1089 hwstat->rx_jabbers +
1090 hwstat->rx_undersized_frames +
1091 hwstat->rx_length_field_frame_errors);
1092 nstat->tx_errors = (hwstat->tx_late_collisions +
1093 hwstat->tx_excessive_collisions +
1094 hwstat->tx_underrun +
1095 hwstat->tx_carrier_sense_errors);
1096 nstat->multicast = hwstat->rx_multicast_frames;
1097 nstat->collisions = (hwstat->tx_single_collision_frames +
1098 hwstat->tx_multiple_collision_frames +
1099 hwstat->tx_excessive_collisions);
1100 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1101 hwstat->rx_jabbers +
1102 hwstat->rx_undersized_frames +
1103 hwstat->rx_length_field_frame_errors);
1104 nstat->rx_over_errors = hwstat->rx_resource_errors;
1105 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1106 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1107 nstat->rx_fifo_errors = hwstat->rx_overruns;
1108 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1109 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1110 nstat->tx_fifo_errors = hwstat->tx_underrun;
1115 static struct net_device_stats *macb_get_stats(struct net_device *dev)
1117 struct macb *bp = netdev_priv(dev);
1118 struct net_device_stats *nstat = &bp->stats;
1119 struct macb_stats *hwstat = &bp->hw_stats.macb;
1121 if (macb_is_gem(bp))
1122 return gem_get_stats(bp);
1124 /* read stats from hardware */
1125 macb_update_stats(bp);
1127 /* Convert HW stats into netdevice stats */
1128 nstat->rx_errors = (hwstat->rx_fcs_errors +
1129 hwstat->rx_align_errors +
1130 hwstat->rx_resource_errors +
1131 hwstat->rx_overruns +
1132 hwstat->rx_oversize_pkts +
1133 hwstat->rx_jabbers +
1134 hwstat->rx_undersize_pkts +
1135 hwstat->sqe_test_errors +
1136 hwstat->rx_length_mismatch);
1137 nstat->tx_errors = (hwstat->tx_late_cols +
1138 hwstat->tx_excessive_cols +
1139 hwstat->tx_underruns +
1140 hwstat->tx_carrier_errors);
1141 nstat->collisions = (hwstat->tx_single_cols +
1142 hwstat->tx_multiple_cols +
1143 hwstat->tx_excessive_cols);
1144 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1145 hwstat->rx_jabbers +
1146 hwstat->rx_undersize_pkts +
1147 hwstat->rx_length_mismatch);
1148 nstat->rx_over_errors = hwstat->rx_resource_errors +
1149 hwstat->rx_overruns;
1150 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1151 nstat->rx_frame_errors = hwstat->rx_align_errors;
1152 nstat->rx_fifo_errors = hwstat->rx_overruns;
1153 /* XXX: What does "missed" mean? */
1154 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1155 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1156 nstat->tx_fifo_errors = hwstat->tx_underruns;
1157 /* Don't know about heartbeat or window errors... */
1162 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1164 struct macb *bp = netdev_priv(dev);
1165 struct phy_device *phydev = bp->phy_dev;
1170 return phy_ethtool_gset(phydev, cmd);
1173 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1175 struct macb *bp = netdev_priv(dev);
1176 struct phy_device *phydev = bp->phy_dev;
1181 return phy_ethtool_sset(phydev, cmd);
1184 static void macb_get_drvinfo(struct net_device *dev,
1185 struct ethtool_drvinfo *info)
1187 struct macb *bp = netdev_priv(dev);
1189 strcpy(info->driver, bp->pdev->dev.driver->name);
1190 strcpy(info->version, "$Revision: 1.14 $");
1191 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
1194 static const struct ethtool_ops macb_ethtool_ops = {
1195 .get_settings = macb_get_settings,
1196 .set_settings = macb_set_settings,
1197 .get_drvinfo = macb_get_drvinfo,
1198 .get_link = ethtool_op_get_link,
1201 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1203 struct macb *bp = netdev_priv(dev);
1204 struct phy_device *phydev = bp->phy_dev;
1206 if (!netif_running(dev))
1212 return phy_mii_ioctl(phydev, rq, cmd);
1215 static const struct net_device_ops macb_netdev_ops = {
1216 .ndo_open = macb_open,
1217 .ndo_stop = macb_close,
1218 .ndo_start_xmit = macb_start_xmit,
1219 .ndo_set_rx_mode = macb_set_rx_mode,
1220 .ndo_get_stats = macb_get_stats,
1221 .ndo_do_ioctl = macb_ioctl,
1222 .ndo_validate_addr = eth_validate_addr,
1223 .ndo_change_mtu = eth_change_mtu,
1224 .ndo_set_mac_address = eth_mac_addr,
1225 #ifdef CONFIG_NET_POLL_CONTROLLER
1226 .ndo_poll_controller = macb_poll_controller,
1230 static int __init macb_probe(struct platform_device *pdev)
1232 struct macb_platform_data *pdata;
1233 struct resource *regs;
1234 struct net_device *dev;
1236 struct phy_device *phydev;
1240 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1242 dev_err(&pdev->dev, "no mmio resource defined\n");
1247 dev = alloc_etherdev(sizeof(*bp));
1249 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1253 SET_NETDEV_DEV(dev, &pdev->dev);
1255 /* TODO: Actually, we have some interesting features... */
1258 bp = netdev_priv(dev);
1262 spin_lock_init(&bp->lock);
1264 bp->pclk = clk_get(&pdev->dev, "pclk");
1265 if (IS_ERR(bp->pclk)) {
1266 dev_err(&pdev->dev, "failed to get macb_clk\n");
1267 goto err_out_free_dev;
1269 clk_enable(bp->pclk);
1271 bp->hclk = clk_get(&pdev->dev, "hclk");
1272 if (IS_ERR(bp->hclk)) {
1273 dev_err(&pdev->dev, "failed to get hclk\n");
1274 goto err_out_put_pclk;
1276 clk_enable(bp->hclk);
1278 bp->regs = ioremap(regs->start, resource_size(regs));
1280 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1282 goto err_out_disable_clocks;
1285 dev->irq = platform_get_irq(pdev, 0);
1286 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
1288 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1290 goto err_out_iounmap;
1293 dev->netdev_ops = &macb_netdev_ops;
1294 netif_napi_add(dev, &bp->napi, macb_poll, 64);
1295 dev->ethtool_ops = &macb_ethtool_ops;
1297 dev->base_addr = regs->start;
1299 /* Set MII management clock divider */
1300 config = macb_mdc_clk_div(bp);
1301 config |= macb_dbw(bp);
1302 macb_writel(bp, NCFGR, config);
1304 macb_get_hwaddr(bp);
1305 pdata = pdev->dev.platform_data;
1307 if (pdata && pdata->is_rmii)
1308 #if defined(CONFIG_ARCH_AT91)
1309 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1312 macb_or_gem_writel(bp, USRIO, 0);
1315 #if defined(CONFIG_ARCH_AT91)
1316 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
1318 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
1321 bp->tx_pending = DEF_TX_RING_PENDING;
1323 err = register_netdev(dev);
1325 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1326 goto err_out_free_irq;
1329 if (macb_mii_init(bp) != 0) {
1330 goto err_out_unregister_netdev;
1333 platform_set_drvdata(pdev, dev);
1335 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1336 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1337 dev->irq, dev->dev_addr);
1339 phydev = bp->phy_dev;
1340 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1341 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
1345 err_out_unregister_netdev:
1346 unregister_netdev(dev);
1348 free_irq(dev->irq, dev);
1351 err_out_disable_clocks:
1352 clk_disable(bp->hclk);
1354 clk_disable(bp->pclk);
1360 platform_set_drvdata(pdev, NULL);
1364 static int __exit macb_remove(struct platform_device *pdev)
1366 struct net_device *dev;
1369 dev = platform_get_drvdata(pdev);
1372 bp = netdev_priv(dev);
1374 phy_disconnect(bp->phy_dev);
1375 mdiobus_unregister(bp->mii_bus);
1376 kfree(bp->mii_bus->irq);
1377 mdiobus_free(bp->mii_bus);
1378 unregister_netdev(dev);
1379 free_irq(dev->irq, dev);
1381 clk_disable(bp->hclk);
1383 clk_disable(bp->pclk);
1386 platform_set_drvdata(pdev, NULL);
1393 static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1395 struct net_device *netdev = platform_get_drvdata(pdev);
1396 struct macb *bp = netdev_priv(netdev);
1398 netif_device_detach(netdev);
1400 clk_disable(bp->hclk);
1401 clk_disable(bp->pclk);
1406 static int macb_resume(struct platform_device *pdev)
1408 struct net_device *netdev = platform_get_drvdata(pdev);
1409 struct macb *bp = netdev_priv(netdev);
1411 clk_enable(bp->pclk);
1412 clk_enable(bp->hclk);
1414 netif_device_attach(netdev);
1419 #define macb_suspend NULL
1420 #define macb_resume NULL
1423 static struct platform_driver macb_driver = {
1424 .remove = __exit_p(macb_remove),
1425 .suspend = macb_suspend,
1426 .resume = macb_resume,
1429 .owner = THIS_MODULE,
1433 static int __init macb_init(void)
1435 return platform_driver_probe(&macb_driver, macb_probe);
1438 static void __exit macb_exit(void)
1440 platform_driver_unregister(&macb_driver);
1443 module_init(macb_init);
1444 module_exit(macb_exit);
1446 MODULE_LICENSE("GPL");
1447 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
1448 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1449 MODULE_ALIAS("platform:macb");