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/circ_buf.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/platform_data/macb.h>
28 #include <linux/platform_device.h>
29 #include <linux/phy.h>
31 #include <linux/of_device.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_mdio.h>
34 #include <linux/of_net.h>
36 #include <linux/udp.h>
37 #include <linux/tcp.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/crc32.h>
40 #include <linux/inetdevice.h>
43 #define MACB_RX_BUFFER_SIZE 128
44 #define RX_BUFFER_MULTIPLE 64 /* bytes */
46 #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
47 #define MIN_RX_RING_SIZE 64
48 #define MAX_RX_RING_SIZE 8192
49 #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
52 #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
53 #define MIN_TX_RING_SIZE 64
54 #define MAX_TX_RING_SIZE 4096
55 #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
58 /* level of occupied TX descriptors under which we wake up TX process */
59 #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
61 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
63 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
66 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
68 /* Max length of transmit frame must be a multiple of 8 bytes */
69 #define MACB_TX_LEN_ALIGN 8
70 #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
71 #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
73 #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
74 #define MACB_NETIF_LSO NETIF_F_TSO
76 /* Graceful stop timeouts in us. We should allow up to
77 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
79 #define MACB_HALT_TIMEOUT 1230
80 #define MACB_PM_TIMEOUT 100 /* ms */
82 /* DMA buffer descriptor might be different size
83 * depends on hardware configuration:
85 * 1. dma address width 32 bits:
86 * word 1: 32 bit address of Data Buffer
89 * 2. dma address width 64 bits:
90 * word 1: 32 bit address of Data Buffer
92 * word 3: upper 32 bit address of Data Buffer
95 * 3. dma address width 32 bits with hardware timestamping:
96 * word 1: 32 bit address of Data Buffer
98 * word 3: timestamp word 1
99 * word 4: timestamp word 2
101 * 4. dma address width 64 bits with hardware timestamping:
102 * word 1: 32 bit address of Data Buffer
104 * word 3: upper 32 bit address of Data Buffer
106 * word 5: timestamp word 1
107 * word 6: timestamp word 2
109 static unsigned int macb_dma_desc_get_size(struct macb *bp)
112 unsigned int desc_size;
114 switch (bp->hw_dma_cap) {
116 desc_size = sizeof(struct macb_dma_desc)
117 + sizeof(struct macb_dma_desc_64);
120 desc_size = sizeof(struct macb_dma_desc)
121 + sizeof(struct macb_dma_desc_ptp);
123 case HW_DMA_CAP_64B_PTP:
124 desc_size = sizeof(struct macb_dma_desc)
125 + sizeof(struct macb_dma_desc_64)
126 + sizeof(struct macb_dma_desc_ptp);
129 desc_size = sizeof(struct macb_dma_desc);
133 return sizeof(struct macb_dma_desc);
136 static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
139 switch (bp->hw_dma_cap) {
144 case HW_DMA_CAP_64B_PTP:
154 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
155 static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
157 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
158 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
163 /* Ring buffer accessors */
164 static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
166 return index & (bp->tx_ring_size - 1);
169 static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
172 index = macb_tx_ring_wrap(queue->bp, index);
173 index = macb_adj_dma_desc_idx(queue->bp, index);
174 return &queue->tx_ring[index];
177 static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
180 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
183 static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
187 offset = macb_tx_ring_wrap(queue->bp, index) *
188 macb_dma_desc_get_size(queue->bp);
190 return queue->tx_ring_dma + offset;
193 static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
195 return index & (bp->rx_ring_size - 1);
198 static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
200 index = macb_rx_ring_wrap(bp, index);
201 index = macb_adj_dma_desc_idx(bp, index);
202 return &bp->rx_ring[index];
205 static void *macb_rx_buffer(struct macb *bp, unsigned int index)
207 return bp->rx_buffers + bp->rx_buffer_size *
208 macb_rx_ring_wrap(bp, index);
212 static u32 hw_readl_native(struct macb *bp, int offset)
214 return __raw_readl(bp->regs + offset);
217 static void hw_writel_native(struct macb *bp, int offset, u32 value)
219 __raw_writel(value, bp->regs + offset);
222 static u32 hw_readl(struct macb *bp, int offset)
224 return readl_relaxed(bp->regs + offset);
227 static void hw_writel(struct macb *bp, int offset, u32 value)
229 writel_relaxed(value, bp->regs + offset);
232 /* Find the CPU endianness by using the loopback bit of NCR register. When the
233 * CPU is in big endian we need to program swapped mode for management
236 static bool hw_is_native_io(void __iomem *addr)
238 u32 value = MACB_BIT(LLB);
240 __raw_writel(value, addr + MACB_NCR);
241 value = __raw_readl(addr + MACB_NCR);
243 /* Write 0 back to disable everything */
244 __raw_writel(0, addr + MACB_NCR);
246 return value == MACB_BIT(LLB);
249 static bool hw_is_gem(void __iomem *addr, bool native_io)
254 id = __raw_readl(addr + MACB_MID);
256 id = readl_relaxed(addr + MACB_MID);
258 return MACB_BFEXT(IDNUM, id) >= 0x2;
261 static void macb_set_hwaddr(struct macb *bp)
266 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
267 macb_or_gem_writel(bp, SA1B, bottom);
268 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
269 macb_or_gem_writel(bp, SA1T, top);
271 gem_writel(bp, RXPTPUNI, bottom);
272 gem_writel(bp, TXPTPUNI, bottom);
274 /* Clear unused address register sets */
275 macb_or_gem_writel(bp, SA2B, 0);
276 macb_or_gem_writel(bp, SA2T, 0);
277 macb_or_gem_writel(bp, SA3B, 0);
278 macb_or_gem_writel(bp, SA3T, 0);
279 macb_or_gem_writel(bp, SA4B, 0);
280 macb_or_gem_writel(bp, SA4T, 0);
283 static void macb_get_hwaddr(struct macb *bp)
285 struct macb_platform_data *pdata;
291 pdata = dev_get_platdata(&bp->pdev->dev);
293 /* Check all 4 address register for valid address */
294 for (i = 0; i < 4; i++) {
295 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
296 top = macb_or_gem_readl(bp, SA1T + i * 8);
298 if (pdata && pdata->rev_eth_addr) {
299 addr[5] = bottom & 0xff;
300 addr[4] = (bottom >> 8) & 0xff;
301 addr[3] = (bottom >> 16) & 0xff;
302 addr[2] = (bottom >> 24) & 0xff;
303 addr[1] = top & 0xff;
304 addr[0] = (top & 0xff00) >> 8;
306 addr[0] = bottom & 0xff;
307 addr[1] = (bottom >> 8) & 0xff;
308 addr[2] = (bottom >> 16) & 0xff;
309 addr[3] = (bottom >> 24) & 0xff;
310 addr[4] = top & 0xff;
311 addr[5] = (top >> 8) & 0xff;
314 if (is_valid_ether_addr(addr)) {
315 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
320 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
321 eth_hw_addr_random(bp->dev);
324 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
326 struct macb *bp = bus->priv;
331 err = pm_runtime_get_sync(&bp->pdev->dev);
335 timeout = jiffies + msecs_to_jiffies(1000);
336 /* wait for end of transfer */
338 if (MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
342 } while (!time_after_eq(jiffies, timeout));
344 if (time_after_eq(jiffies, timeout)) {
345 netdev_err(bp->dev, "wait for end of transfer timed out\n");
346 pm_runtime_mark_last_busy(&bp->pdev->dev);
347 pm_runtime_put_autosuspend(&bp->pdev->dev);
351 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
352 | MACB_BF(RW, MACB_MAN_READ)
353 | MACB_BF(PHYA, mii_id)
354 | MACB_BF(REGA, regnum)
355 | MACB_BF(CODE, MACB_MAN_CODE)));
357 timeout = jiffies + msecs_to_jiffies(1000);
358 /* wait for end of transfer */
360 if (MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
364 } while (!time_after_eq(jiffies, timeout));
366 if (time_after_eq(jiffies, timeout)) {
367 netdev_err(bp->dev, "wait for end of transfer timed out\n");
368 pm_runtime_mark_last_busy(&bp->pdev->dev);
369 pm_runtime_put_autosuspend(&bp->pdev->dev);
373 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
375 pm_runtime_mark_last_busy(&bp->pdev->dev);
376 pm_runtime_put_autosuspend(&bp->pdev->dev);
380 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
383 struct macb *bp = bus->priv;
387 err = pm_runtime_get_sync(&bp->pdev->dev);
391 timeout = jiffies + msecs_to_jiffies(1000);
392 /* wait for end of transfer */
394 if (MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
398 } while (!time_after_eq(jiffies, timeout));
400 if (time_after_eq(jiffies, timeout)) {
401 netdev_err(bp->dev, "wait for end of transfer timed out\n");
402 pm_runtime_mark_last_busy(&bp->pdev->dev);
403 pm_runtime_put_autosuspend(&bp->pdev->dev);
407 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
408 | MACB_BF(RW, MACB_MAN_WRITE)
409 | MACB_BF(PHYA, mii_id)
410 | MACB_BF(REGA, regnum)
411 | MACB_BF(CODE, MACB_MAN_CODE)
412 | MACB_BF(DATA, value)));
414 timeout = jiffies + msecs_to_jiffies(1000);
415 /* wait for end of transfer */
417 if (MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
421 } while (!time_after_eq(jiffies, timeout));
423 if (time_after_eq(jiffies, timeout)) {
424 netdev_err(bp->dev, "wait for end of transfer timed out\n");
425 pm_runtime_mark_last_busy(&bp->pdev->dev);
426 pm_runtime_put_autosuspend(&bp->pdev->dev);
430 pm_runtime_mark_last_busy(&bp->pdev->dev);
431 pm_runtime_put_autosuspend(&bp->pdev->dev);
436 * macb_set_tx_clk - Set a clock to a new frequency
437 * @clk: Pointer to the clock to change
438 * @speed: New frequency in Hz
439 * @dev: Pointer to the struct net_device
441 static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
443 long ferr, rate, rate_rounded;
462 rate_rounded = clk_round_rate(clk, rate);
463 if (rate_rounded < 0)
466 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
469 ferr = abs(rate_rounded - rate);
470 ferr = DIV_ROUND_UP(ferr, rate / 100000);
472 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
475 if (clk_set_rate(clk, rate_rounded))
476 netdev_err(dev, "adjusting tx_clk failed.\n");
479 static void macb_handle_link_change(struct net_device *dev)
481 struct macb *bp = netdev_priv(dev);
482 struct phy_device *phydev = bp->phy_dev;
484 int status_change = 0;
486 spin_lock_irqsave(&bp->lock, flags);
489 if ((bp->speed != phydev->speed) ||
490 (bp->duplex != phydev->duplex)) {
493 reg = macb_readl(bp, NCFGR);
494 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
496 reg &= ~GEM_BIT(GBE);
500 if (phydev->speed == SPEED_100)
501 reg |= MACB_BIT(SPD);
502 if (phydev->speed == SPEED_1000 &&
503 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
506 macb_or_gem_writel(bp, NCFGR, reg);
508 bp->speed = phydev->speed;
509 bp->duplex = phydev->duplex;
514 if (phydev->link != bp->link) {
519 bp->link = phydev->link;
524 spin_unlock_irqrestore(&bp->lock, flags);
528 /* Update the TX clock rate if and only if the link is
529 * up and there has been a link change.
531 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
533 netif_carrier_on(dev);
534 netdev_info(dev, "link up (%d/%s)\n",
536 phydev->duplex == DUPLEX_FULL ?
539 netif_carrier_off(dev);
540 netdev_info(dev, "link down\n");
545 /* based on au1000_eth. c*/
546 static int macb_mii_probe(struct net_device *dev)
548 struct macb *bp = netdev_priv(dev);
549 struct macb_platform_data *pdata;
550 struct phy_device *phydev;
555 phydev = of_phy_connect(dev, bp->phy_node,
556 &macb_handle_link_change, 0,
561 phydev = phy_find_first(bp->mii_bus);
563 netdev_err(dev, "no PHY found\n");
567 pdata = dev_get_platdata(&bp->pdev->dev);
568 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
569 ret = devm_gpio_request(&bp->pdev->dev,
570 pdata->phy_irq_pin, "phy int");
572 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
573 phydev->irq = (phy_irq < 0) ?
578 /* attach the mac to the phy */
579 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
582 netdev_err(dev, "Could not attach to PHY\n");
587 /* mask with MAC supported features */
588 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
589 phydev->supported &= PHY_GBIT_FEATURES;
591 phydev->supported &= PHY_BASIC_FEATURES;
593 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
594 phydev->supported &= ~SUPPORTED_1000baseT_Half;
596 phydev->advertising = phydev->supported;
601 bp->phy_dev = phydev;
606 static int macb_mii_init(struct macb *bp)
608 struct macb_platform_data *pdata;
609 struct device_node *np, *mdio_np;
612 /* Enable management port */
613 macb_writel(bp, NCR, MACB_BIT(MPE));
615 bp->mii_bus = mdiobus_alloc();
621 bp->mii_bus->name = "MACB_mii_bus";
622 bp->mii_bus->read = &macb_mdio_read;
623 bp->mii_bus->write = &macb_mdio_write;
624 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
625 bp->pdev->name, bp->pdev->id);
626 bp->mii_bus->priv = bp;
627 bp->mii_bus->parent = &bp->dev->dev;
628 pdata = dev_get_platdata(&bp->pdev->dev);
630 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
632 np = bp->pdev->dev.of_node;
633 mdio_np = of_get_child_by_name(np, "mdio");
635 of_node_put(mdio_np);
636 err = of_mdiobus_register(bp->mii_bus, mdio_np);
638 goto err_out_unregister_bus;
640 /* try dt phy registration */
641 err = of_mdiobus_register(bp->mii_bus, np);
643 /* fallback to standard phy registration if no phy were
644 * found during dt phy registration
646 if (!err && !phy_find_first(bp->mii_bus)) {
647 for (i = 0; i < PHY_MAX_ADDR; i++) {
648 struct phy_device *phydev;
650 phydev = mdiobus_scan(bp->mii_bus, i);
651 if (IS_ERR(phydev) &&
652 PTR_ERR(phydev) != -ENODEV) {
653 err = PTR_ERR(phydev);
659 goto err_out_unregister_bus;
663 bp->mii_bus->phy_mask = pdata->phy_mask;
665 err = mdiobus_register(bp->mii_bus);
669 goto err_out_free_mdiobus;
671 err = macb_mii_probe(bp->dev);
673 goto err_out_unregister_bus;
677 err_out_unregister_bus:
678 mdiobus_unregister(bp->mii_bus);
679 err_out_free_mdiobus:
680 mdiobus_free(bp->mii_bus);
685 static void macb_update_stats(struct macb *bp)
687 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
688 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
689 int offset = MACB_PFR;
691 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
693 for (; p < end; p++, offset += 4)
694 *p += bp->macb_reg_readl(bp, offset);
697 static int macb_halt_tx(struct macb *bp)
699 unsigned long halt_time, timeout;
702 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
704 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
707 status = macb_readl(bp, TSR);
708 if (!(status & MACB_BIT(TGO)))
711 usleep_range(10, 250);
712 } while (time_before(halt_time, timeout));
717 static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
719 if (tx_skb->mapping) {
720 if (tx_skb->mapped_as_page)
721 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
722 tx_skb->size, DMA_TO_DEVICE);
724 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
725 tx_skb->size, DMA_TO_DEVICE);
730 dev_kfree_skb_any(tx_skb->skb);
735 static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
737 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
738 struct macb_dma_desc_64 *desc_64;
740 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
741 desc_64 = macb_64b_desc(bp, desc);
742 desc_64->addrh = upper_32_bits(addr);
745 desc->addr = lower_32_bits(addr);
748 static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
751 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
752 struct macb_dma_desc_64 *desc_64;
754 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
755 desc_64 = macb_64b_desc(bp, desc);
756 addr = ((u64)(desc_64->addrh) << 32);
759 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
763 static void macb_tx_error_task(struct work_struct *work)
765 struct macb_queue *queue = container_of(work, struct macb_queue,
767 struct macb *bp = queue->bp;
768 struct macb_tx_skb *tx_skb;
769 struct macb_dma_desc *desc;
774 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
775 (unsigned int)(queue - bp->queues),
776 queue->tx_tail, queue->tx_head);
778 /* Prevent the queue IRQ handlers from running: each of them may call
779 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
780 * As explained below, we have to halt the transmission before updating
781 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
782 * network engine about the macb/gem being halted.
784 spin_lock_irqsave(&bp->lock, flags);
786 /* Make sure nobody is trying to queue up new packets */
787 netif_tx_stop_all_queues(bp->dev);
789 /* Stop transmission now
790 * (in case we have just queued new packets)
791 * macb/gem must be halted to write TBQP register
793 if (macb_halt_tx(bp))
794 /* Just complain for now, reinitializing TX path can be good */
795 netdev_err(bp->dev, "BUG: halt tx timed out\n");
797 /* Treat frames in TX queue including the ones that caused the error.
798 * Free transmit buffers in upper layer.
800 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
803 desc = macb_tx_desc(queue, tail);
805 tx_skb = macb_tx_skb(queue, tail);
808 if (ctrl & MACB_BIT(TX_USED)) {
809 /* skb is set for the last buffer of the frame */
811 macb_tx_unmap(bp, tx_skb);
813 tx_skb = macb_tx_skb(queue, tail);
817 /* ctrl still refers to the first buffer descriptor
818 * since it's the only one written back by the hardware
820 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
821 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
822 macb_tx_ring_wrap(bp, tail),
824 bp->dev->stats.tx_packets++;
825 bp->dev->stats.tx_bytes += skb->len;
828 /* "Buffers exhausted mid-frame" errors may only happen
829 * if the driver is buggy, so complain loudly about
830 * those. Statistics are updated by hardware.
832 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
834 "BUG: TX buffers exhausted mid-frame\n");
836 desc->ctrl = ctrl | MACB_BIT(TX_USED);
839 macb_tx_unmap(bp, tx_skb);
842 /* Set end of TX queue */
843 desc = macb_tx_desc(queue, 0);
844 macb_set_addr(bp, desc, 0);
845 desc->ctrl = MACB_BIT(TX_USED);
847 /* Make descriptor updates visible to hardware */
850 /* Reinitialize the TX desc queue */
851 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
852 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
853 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
854 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
856 /* Make TX ring reflect state of hardware */
860 /* Housework before enabling TX IRQ */
861 macb_writel(bp, TSR, macb_readl(bp, TSR));
862 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
864 /* Now we are ready to start transmission again */
865 netif_tx_start_all_queues(bp->dev);
866 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
868 spin_unlock_irqrestore(&bp->lock, flags);
871 static void macb_tx_interrupt(struct macb_queue *queue)
876 struct macb *bp = queue->bp;
877 u16 queue_index = queue - bp->queues;
879 status = macb_readl(bp, TSR);
880 macb_writel(bp, TSR, status);
882 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
883 queue_writel(queue, ISR, MACB_BIT(TCOMP));
885 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
886 (unsigned long)status);
888 head = queue->tx_head;
889 for (tail = queue->tx_tail; tail != head; tail++) {
890 struct macb_tx_skb *tx_skb;
892 struct macb_dma_desc *desc;
895 desc = macb_tx_desc(queue, tail);
897 /* Make hw descriptor updates visible to CPU */
902 /* TX_USED bit is only set by hardware on the very first buffer
903 * descriptor of the transmitted frame.
905 if (!(ctrl & MACB_BIT(TX_USED)))
908 /* Process all buffers of the current transmitted frame */
910 tx_skb = macb_tx_skb(queue, tail);
913 /* First, update TX stats if needed */
915 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
916 /* skb now belongs to timestamp buffer
917 * and will be removed later
921 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
922 macb_tx_ring_wrap(bp, tail),
924 bp->dev->stats.tx_packets++;
925 bp->dev->stats.tx_bytes += skb->len;
928 /* Now we can safely release resources */
929 macb_tx_unmap(bp, tx_skb);
931 /* skb is set only for the last buffer of the frame.
932 * WARNING: at this point skb has been freed by
940 queue->tx_tail = tail;
941 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
942 CIRC_CNT(queue->tx_head, queue->tx_tail,
943 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
944 netif_wake_subqueue(bp->dev, queue_index);
947 static void gem_rx_refill(struct macb *bp)
952 struct macb_dma_desc *desc;
954 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
955 bp->rx_ring_size) > 0) {
956 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
958 /* Make hw descriptor updates visible to CPU */
961 bp->rx_prepared_head++;
962 desc = macb_rx_desc(bp, entry);
964 if (!bp->rx_skbuff[entry]) {
965 /* allocate sk_buff for this free entry in ring */
966 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
967 if (unlikely(!skb)) {
969 "Unable to allocate sk_buff\n");
973 /* now fill corresponding descriptor entry */
974 paddr = dma_map_single(&bp->pdev->dev, skb->data,
977 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
982 bp->rx_skbuff[entry] = skb;
984 if (entry == bp->rx_ring_size - 1)
985 paddr |= MACB_BIT(RX_WRAP);
986 macb_set_addr(bp, desc, paddr);
989 /* properly align Ethernet header */
990 skb_reserve(skb, NET_IP_ALIGN);
992 desc->addr &= ~MACB_BIT(RX_USED);
997 /* Make descriptor updates visible to hardware */
1000 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
1001 bp->rx_prepared_head, bp->rx_tail);
1004 /* Mark DMA descriptors from begin up to and not including end as unused */
1005 static void discard_partial_frame(struct macb *bp, unsigned int begin,
1010 for (frag = begin; frag != end; frag++) {
1011 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
1013 desc->addr &= ~MACB_BIT(RX_USED);
1016 /* Make descriptor updates visible to hardware */
1019 /* When this happens, the hardware stats registers for
1020 * whatever caused this is updated, so we don't have to record
1025 static int macb_validate_hw_csum(struct sk_buff *skb)
1027 u32 pkt_csum = *((u32 *)&skb->data[skb->len - ETH_FCS_LEN]);
1028 u32 csum = ~crc32_le(~0, skb_mac_header(skb),
1029 skb->len + ETH_HLEN - ETH_FCS_LEN);
1031 return (pkt_csum != csum);
1034 static int gem_rx(struct macb *bp, int budget)
1038 struct sk_buff *skb;
1039 struct macb_dma_desc *desc;
1042 while (count < budget) {
1047 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
1049 desc = macb_rx_desc(bp, entry);
1051 /* Make hw descriptor updates visible to CPU */
1054 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
1055 addr = macb_get_addr(bp, desc);
1064 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1066 "not whole frame pointed by descriptor\n");
1067 bp->dev->stats.rx_dropped++;
1070 skb = bp->rx_skbuff[entry];
1071 if (unlikely(!skb)) {
1073 "inconsistent Rx descriptor chain\n");
1074 bp->dev->stats.rx_dropped++;
1077 /* now everything is ready for receiving packet */
1078 bp->rx_skbuff[entry] = NULL;
1079 len = ctrl & bp->rx_frm_len_mask;
1081 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1084 dma_unmap_single(&bp->pdev->dev, addr,
1085 bp->rx_buffer_size, DMA_FROM_DEVICE);
1087 skb->protocol = eth_type_trans(skb, bp->dev);
1089 /* Validate MAC fcs if RX checsum offload disabled */
1090 if (!(bp->dev->features & NETIF_F_RXCSUM)) {
1091 if (macb_validate_hw_csum(skb)) {
1092 netdev_err(bp->dev, "incorrect FCS\n");
1093 bp->dev->stats.rx_dropped++;
1098 skb_checksum_none_assert(skb);
1099 if (bp->dev->features & NETIF_F_RXCSUM &&
1100 !(bp->dev->flags & IFF_PROMISC) &&
1101 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1102 skb->ip_summed = CHECKSUM_UNNECESSARY;
1104 bp->dev->stats.rx_packets++;
1105 bp->dev->stats.rx_bytes += skb->len;
1107 gem_ptp_do_rxstamp(bp, skb, desc);
1109 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1110 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1111 skb->len, skb->csum);
1112 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
1113 skb_mac_header(skb), 16, true);
1114 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1115 skb->data, 32, true);
1118 netif_receive_skb(skb);
1126 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
1127 unsigned int last_frag)
1131 unsigned int offset;
1132 struct sk_buff *skb;
1133 struct macb_dma_desc *desc;
1135 desc = macb_rx_desc(bp, last_frag);
1136 len = desc->ctrl & bp->rx_frm_len_mask;
1138 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
1139 macb_rx_ring_wrap(bp, first_frag),
1140 macb_rx_ring_wrap(bp, last_frag), len);
1142 /* The ethernet header starts NET_IP_ALIGN bytes into the
1143 * first buffer. Since the header is 14 bytes, this makes the
1144 * payload word-aligned.
1146 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1147 * the two padding bytes into the skb so that we avoid hitting
1148 * the slowpath in memcpy(), and pull them off afterwards.
1150 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
1152 bp->dev->stats.rx_dropped++;
1153 for (frag = first_frag; ; frag++) {
1154 desc = macb_rx_desc(bp, frag);
1155 desc->addr &= ~MACB_BIT(RX_USED);
1156 if (frag == last_frag)
1160 /* Make descriptor updates visible to hardware */
1167 len += NET_IP_ALIGN;
1168 skb_checksum_none_assert(skb);
1171 for (frag = first_frag; ; frag++) {
1172 unsigned int frag_len = bp->rx_buffer_size;
1174 if (offset + frag_len > len) {
1175 if (unlikely(frag != last_frag)) {
1176 dev_kfree_skb_any(skb);
1179 frag_len = len - offset;
1181 skb_copy_to_linear_data_offset(skb, offset,
1182 macb_rx_buffer(bp, frag),
1184 offset += bp->rx_buffer_size;
1185 desc = macb_rx_desc(bp, frag);
1186 desc->addr &= ~MACB_BIT(RX_USED);
1188 if (frag == last_frag)
1192 /* Validate MAC fcs if RX checsum offload disabled */
1193 if (!(bp->dev->features & NETIF_F_RXCSUM)) {
1194 if (macb_validate_hw_csum(skb)) {
1195 netdev_err(bp->dev, "incorrect FCS\n");
1196 bp->dev->stats.rx_dropped++;
1198 /* Make descriptor updates visible to hardware */
1205 /* Make descriptor updates visible to hardware */
1208 __skb_pull(skb, NET_IP_ALIGN);
1209 skb->protocol = eth_type_trans(skb, bp->dev);
1211 bp->dev->stats.rx_packets++;
1212 bp->dev->stats.rx_bytes += skb->len;
1213 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1214 skb->len, skb->csum);
1215 netif_receive_skb(skb);
1220 static inline void macb_init_rx_ring(struct macb *bp)
1223 struct macb_dma_desc *desc = NULL;
1226 addr = bp->rx_buffers_dma;
1227 for (i = 0; i < bp->rx_ring_size; i++) {
1228 desc = macb_rx_desc(bp, i);
1229 macb_set_addr(bp, desc, addr);
1231 addr += bp->rx_buffer_size;
1233 desc->addr |= MACB_BIT(RX_WRAP);
1237 static int macb_rx(struct macb *bp, int budget)
1239 bool reset_rx_queue = false;
1242 int first_frag = -1;
1244 for (tail = bp->rx_tail; budget > 0; tail++) {
1245 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
1248 /* Make hw descriptor updates visible to CPU */
1253 if (!(desc->addr & MACB_BIT(RX_USED)))
1256 if (ctrl & MACB_BIT(RX_SOF)) {
1257 if (first_frag != -1)
1258 discard_partial_frame(bp, first_frag, tail);
1262 if (ctrl & MACB_BIT(RX_EOF)) {
1265 if (unlikely(first_frag == -1)) {
1266 reset_rx_queue = true;
1270 dropped = macb_rx_frame(bp, first_frag, tail);
1272 if (unlikely(dropped < 0)) {
1273 reset_rx_queue = true;
1283 if (unlikely(reset_rx_queue)) {
1284 unsigned long flags;
1287 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1289 spin_lock_irqsave(&bp->lock, flags);
1291 ctrl = macb_readl(bp, NCR);
1292 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1294 macb_init_rx_ring(bp);
1295 macb_writel(bp, RBQP, bp->rx_ring_dma);
1297 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1299 spin_unlock_irqrestore(&bp->lock, flags);
1303 if (first_frag != -1)
1304 bp->rx_tail = first_frag;
1311 static int macb_poll(struct napi_struct *napi, int budget)
1313 struct macb *bp = container_of(napi, struct macb, napi);
1317 status = macb_readl(bp, RSR);
1318 macb_writel(bp, RSR, status);
1322 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
1323 (unsigned long)status, budget);
1325 work_done = bp->macbgem_ops.mog_rx(bp, budget);
1326 if (work_done < budget) {
1327 napi_complete_done(napi, work_done);
1329 /* Packets received while interrupts were disabled */
1330 status = macb_readl(bp, RSR);
1332 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1333 macb_writel(bp, ISR, MACB_BIT(RCOMP));
1334 napi_reschedule(napi);
1336 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1340 /* TODO: Handle errors */
1345 static void macb_hresp_error_task(unsigned long data)
1347 struct macb *bp = (struct macb *)data;
1348 struct net_device *dev = bp->dev;
1349 struct macb_queue *queue = bp->queues;
1353 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1354 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1358 ctrl = macb_readl(bp, NCR);
1359 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1360 macb_writel(bp, NCR, ctrl);
1362 netif_tx_stop_all_queues(dev);
1363 netif_carrier_off(dev);
1365 bp->macbgem_ops.mog_init_rings(bp);
1367 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
1368 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1369 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1370 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
1372 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1373 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1374 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1375 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1376 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
1378 /* We only use the first queue at the moment. Remaining
1379 * queues must be tied-off before we enable the receiver.
1381 * See the documentation for receive_q1_ptr for more info.
1384 queue_writel(queue, RBQP,
1385 lower_32_bits(bp->rx_ring_tieoff_dma));
1387 /* Enable interrupts */
1388 queue_writel(queue, IER,
1394 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1395 macb_writel(bp, NCR, ctrl);
1397 netif_carrier_on(dev);
1398 netif_tx_start_all_queues(dev);
1401 static irqreturn_t macb_interrupt(int irq, void *dev_id)
1403 struct macb_queue *queue = dev_id;
1404 struct macb *bp = queue->bp;
1405 struct net_device *dev = bp->dev;
1408 status = queue_readl(queue, ISR);
1410 if (unlikely(!status))
1413 spin_lock(&bp->lock);
1416 if (status & MACB_BIT(WOL)) {
1417 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1418 queue_writel(queue, ISR, MACB_BIT(WOL));
1422 /* close possible race with dev_close */
1423 if (unlikely(!netif_running(dev))) {
1424 queue_writel(queue, IDR, -1);
1425 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1426 queue_writel(queue, ISR, -1);
1430 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1431 (unsigned int)(queue - bp->queues),
1432 (unsigned long)status);
1434 if (status & MACB_RX_INT_FLAGS) {
1435 /* There's no point taking any more interrupts
1436 * until we have processed the buffers. The
1437 * scheduling call may fail if the poll routine
1438 * is already scheduled, so disable interrupts
1441 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
1442 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1443 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1445 if (napi_schedule_prep(&bp->napi)) {
1446 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1447 __napi_schedule(&bp->napi);
1451 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1452 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1453 schedule_work(&queue->tx_error_task);
1455 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1456 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1461 if (status & MACB_BIT(TCOMP))
1462 macb_tx_interrupt(queue);
1464 /* Link change detection isn't possible with RMII, so we'll
1465 * add that if/when we get our hands on a full-blown MII PHY.
1468 /* There is a hardware issue under heavy load where DMA can
1469 * stop, this causes endless "used buffer descriptor read"
1470 * interrupts but it can be cleared by re-enabling RX. See
1471 * the at91 manual, section 41.3.1 or the Zynq manual
1472 * section 16.7.4 for details.
1474 if (status & MACB_BIT(RXUBR)) {
1475 ctrl = macb_readl(bp, NCR);
1476 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1478 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1480 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1481 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1484 if (status & MACB_BIT(ISR_ROVR)) {
1485 /* We missed at least one packet */
1486 if (macb_is_gem(bp))
1487 bp->hw_stats.gem.rx_overruns++;
1489 bp->hw_stats.macb.rx_overruns++;
1491 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1492 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1495 if (status & MACB_BIT(HRESP)) {
1496 tasklet_schedule(&bp->hresp_err_tasklet);
1497 netdev_err(dev, "DMA bus error: HRESP not OK\n");
1499 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1500 queue_writel(queue, ISR, MACB_BIT(HRESP));
1502 status = queue_readl(queue, ISR);
1505 spin_unlock(&bp->lock);
1510 #ifdef CONFIG_NET_POLL_CONTROLLER
1511 /* Polling receive - used by netconsole and other diagnostic tools
1512 * to allow network i/o with interrupts disabled.
1514 static void macb_poll_controller(struct net_device *dev)
1516 struct macb *bp = netdev_priv(dev);
1517 struct macb_queue *queue;
1518 unsigned long flags;
1521 local_irq_save(flags);
1522 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1523 macb_interrupt(dev->irq, queue);
1524 local_irq_restore(flags);
1528 static unsigned int macb_tx_map(struct macb *bp,
1529 struct macb_queue *queue,
1530 struct sk_buff *skb,
1531 unsigned int hdrlen)
1534 unsigned int len, entry, i, tx_head = queue->tx_head;
1535 struct macb_tx_skb *tx_skb = NULL;
1536 struct macb_dma_desc *desc;
1537 unsigned int offset, size, count = 0;
1538 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1539 unsigned int eof = 1, mss_mfs = 0;
1540 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1543 if (skb_shinfo(skb)->gso_size != 0) {
1544 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1546 lso_ctrl = MACB_LSO_UFO_ENABLE;
1549 lso_ctrl = MACB_LSO_TSO_ENABLE;
1552 /* First, map non-paged data */
1553 len = skb_headlen(skb);
1555 /* first buffer length */
1560 entry = macb_tx_ring_wrap(bp, tx_head);
1561 tx_skb = &queue->tx_skb[entry];
1563 mapping = dma_map_single(&bp->pdev->dev,
1565 size, DMA_TO_DEVICE);
1566 if (dma_mapping_error(&bp->pdev->dev, mapping))
1569 /* Save info to properly release resources */
1571 tx_skb->mapping = mapping;
1572 tx_skb->size = size;
1573 tx_skb->mapped_as_page = false;
1580 size = min(len, bp->max_tx_length);
1583 /* Then, map paged data from fragments */
1584 for (f = 0; f < nr_frags; f++) {
1585 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1587 len = skb_frag_size(frag);
1590 size = min(len, bp->max_tx_length);
1591 entry = macb_tx_ring_wrap(bp, tx_head);
1592 tx_skb = &queue->tx_skb[entry];
1594 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1595 offset, size, DMA_TO_DEVICE);
1596 if (dma_mapping_error(&bp->pdev->dev, mapping))
1599 /* Save info to properly release resources */
1601 tx_skb->mapping = mapping;
1602 tx_skb->size = size;
1603 tx_skb->mapped_as_page = true;
1612 /* Should never happen */
1613 if (unlikely(!tx_skb)) {
1614 netdev_err(bp->dev, "BUG! empty skb!\n");
1618 /* This is the last buffer of the frame: save socket buffer */
1621 /* Update TX ring: update buffer descriptors in reverse order
1622 * to avoid race condition
1625 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1626 * to set the end of TX queue
1629 entry = macb_tx_ring_wrap(bp, i);
1630 ctrl = MACB_BIT(TX_USED);
1631 desc = macb_tx_desc(queue, entry);
1635 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1636 /* include header and FCS in value given to h/w */
1637 mss_mfs = skb_shinfo(skb)->gso_size +
1638 skb_transport_offset(skb) +
1641 mss_mfs = skb_shinfo(skb)->gso_size;
1642 /* TCP Sequence Number Source Select
1643 * can be set only for TSO
1651 entry = macb_tx_ring_wrap(bp, i);
1652 tx_skb = &queue->tx_skb[entry];
1653 desc = macb_tx_desc(queue, entry);
1655 ctrl = (u32)tx_skb->size;
1657 ctrl |= MACB_BIT(TX_LAST);
1660 if (unlikely(entry == (bp->tx_ring_size - 1)))
1661 ctrl |= MACB_BIT(TX_WRAP);
1663 /* First descriptor is header descriptor */
1664 if (i == queue->tx_head) {
1665 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1666 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1668 /* Only set MSS/MFS on payload descriptors
1669 * (second or later descriptor)
1671 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1673 /* Set TX buffer descriptor */
1674 macb_set_addr(bp, desc, tx_skb->mapping);
1675 /* desc->addr must be visible to hardware before clearing
1676 * 'TX_USED' bit in desc->ctrl.
1680 } while (i != queue->tx_head);
1682 queue->tx_head = tx_head;
1687 netdev_err(bp->dev, "TX DMA map failed\n");
1689 for (i = queue->tx_head; i != tx_head; i++) {
1690 tx_skb = macb_tx_skb(queue, i);
1692 macb_tx_unmap(bp, tx_skb);
1698 static netdev_features_t macb_features_check(struct sk_buff *skb,
1699 struct net_device *dev,
1700 netdev_features_t features)
1702 unsigned int nr_frags, f;
1703 unsigned int hdrlen;
1705 /* Validate LSO compatibility */
1707 /* there is only one buffer */
1708 if (!skb_is_nonlinear(skb))
1711 /* length of header */
1712 hdrlen = skb_transport_offset(skb);
1713 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1714 hdrlen += tcp_hdrlen(skb);
1717 * When software supplies two or more payload buffers all payload buffers
1718 * apart from the last must be a multiple of 8 bytes in size.
1720 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1721 return features & ~MACB_NETIF_LSO;
1723 nr_frags = skb_shinfo(skb)->nr_frags;
1724 /* No need to check last fragment */
1726 for (f = 0; f < nr_frags; f++) {
1727 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1729 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1730 return features & ~MACB_NETIF_LSO;
1735 static inline int macb_clear_csum(struct sk_buff *skb)
1737 /* no change for packets without checksum offloading */
1738 if (skb->ip_summed != CHECKSUM_PARTIAL)
1741 /* make sure we can modify the header */
1742 if (unlikely(skb_cow_head(skb, 0)))
1745 /* initialize checksum field
1746 * This is required - at least for Zynq, which otherwise calculates
1747 * wrong UDP header checksums for UDP packets with UDP data len <=2
1749 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1753 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1755 u16 queue_index = skb_get_queue_mapping(skb);
1756 struct macb *bp = netdev_priv(dev);
1757 struct macb_queue *queue = &bp->queues[queue_index];
1758 unsigned long flags;
1759 unsigned int desc_cnt, nr_frags, frag_size, f;
1760 unsigned int hdrlen;
1761 bool is_lso, is_udp = 0;
1763 is_lso = (skb_shinfo(skb)->gso_size != 0);
1766 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1768 /* length of headers */
1770 /* only queue eth + ip headers separately for UDP */
1771 hdrlen = skb_transport_offset(skb);
1773 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1774 if (skb_headlen(skb) < hdrlen) {
1775 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1776 /* if this is required, would need to copy to single buffer */
1777 return NETDEV_TX_BUSY;
1780 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
1782 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1783 netdev_vdbg(bp->dev,
1784 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1785 queue_index, skb->len, skb->head, skb->data,
1786 skb_tail_pointer(skb), skb_end_pointer(skb));
1787 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1788 skb->data, 16, true);
1791 /* Count how many TX buffer descriptors are needed to send this
1792 * socket buffer: skb fragments of jumbo frames may need to be
1793 * split into many buffer descriptors.
1795 if (is_lso && (skb_headlen(skb) > hdrlen))
1796 /* extra header descriptor if also payload in first buffer */
1797 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1799 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
1800 nr_frags = skb_shinfo(skb)->nr_frags;
1801 for (f = 0; f < nr_frags; f++) {
1802 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1803 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
1806 spin_lock_irqsave(&bp->lock, flags);
1808 /* This is a hard error, log it. */
1809 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1810 bp->tx_ring_size) < desc_cnt) {
1811 netif_stop_subqueue(dev, queue_index);
1812 spin_unlock_irqrestore(&bp->lock, flags);
1813 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1814 queue->tx_head, queue->tx_tail);
1815 return NETDEV_TX_BUSY;
1818 if (macb_clear_csum(skb)) {
1819 dev_kfree_skb_any(skb);
1823 /* Map socket buffer for DMA transfer */
1824 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
1825 dev_kfree_skb_any(skb);
1829 /* Make newly initialized descriptor visible to hardware */
1831 skb_tx_timestamp(skb);
1833 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1835 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
1836 netif_stop_subqueue(dev, queue_index);
1839 spin_unlock_irqrestore(&bp->lock, flags);
1841 return NETDEV_TX_OK;
1844 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1846 if (!macb_is_gem(bp)) {
1847 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1849 bp->rx_buffer_size = size;
1851 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1853 "RX buffer must be multiple of %d bytes, expanding\n",
1854 RX_BUFFER_MULTIPLE);
1855 bp->rx_buffer_size =
1856 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1860 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
1861 bp->dev->mtu, bp->rx_buffer_size);
1864 static void gem_free_rx_buffers(struct macb *bp)
1866 struct sk_buff *skb;
1867 struct macb_dma_desc *desc;
1874 for (i = 0; i < bp->rx_ring_size; i++) {
1875 skb = bp->rx_skbuff[i];
1880 desc = macb_rx_desc(bp, i);
1881 addr = macb_get_addr(bp, desc);
1883 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1885 dev_kfree_skb_any(skb);
1889 kfree(bp->rx_skbuff);
1890 bp->rx_skbuff = NULL;
1893 static void macb_free_rx_buffers(struct macb *bp)
1895 if (bp->rx_buffers) {
1896 dma_free_coherent(&bp->pdev->dev,
1897 bp->rx_ring_size * bp->rx_buffer_size,
1898 bp->rx_buffers, bp->rx_buffers_dma);
1899 bp->rx_buffers = NULL;
1903 static void macb_free_consistent(struct macb *bp)
1905 struct macb_queue *queue;
1908 bp->macbgem_ops.mog_free_rx_buffers(bp);
1910 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
1911 bp->rx_ring, bp->rx_ring_dma);
1915 if (bp->rx_ring_tieoff) {
1916 dma_free_coherent(&bp->pdev->dev, macb_dma_desc_get_size(bp),
1917 bp->rx_ring_tieoff, bp->rx_ring_tieoff_dma);
1918 bp->rx_ring_tieoff = NULL;
1921 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1922 kfree(queue->tx_skb);
1923 queue->tx_skb = NULL;
1924 if (queue->tx_ring) {
1925 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
1926 queue->tx_ring, queue->tx_ring_dma);
1927 queue->tx_ring = NULL;
1932 static int gem_alloc_rx_buffers(struct macb *bp)
1936 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1937 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1942 "Allocated %d RX struct sk_buff entries at %p\n",
1943 bp->rx_ring_size, bp->rx_skbuff);
1947 static int macb_alloc_rx_buffers(struct macb *bp)
1951 size = bp->rx_ring_size * bp->rx_buffer_size;
1952 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1953 &bp->rx_buffers_dma, GFP_KERNEL);
1954 if (!bp->rx_buffers)
1958 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1959 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1963 static int macb_alloc_consistent(struct macb *bp)
1965 struct macb_queue *queue;
1969 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1970 size = TX_RING_BYTES(bp);
1971 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1972 &queue->tx_ring_dma,
1974 if (!queue->tx_ring)
1977 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1978 q, size, (unsigned long)queue->tx_ring_dma,
1981 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
1982 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1987 size = RX_RING_BYTES(bp);
1988 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1989 &bp->rx_ring_dma, GFP_KERNEL);
1993 /* If we have more than one queue, allocate a tie off descriptor
1994 * that will be used to disable unused RX queues.
1996 if (bp->num_queues > 1) {
1997 bp->rx_ring_tieoff = dma_alloc_coherent(&bp->pdev->dev,
1998 macb_dma_desc_get_size(bp),
1999 &bp->rx_ring_tieoff_dma,
2001 if (!bp->rx_ring_tieoff)
2006 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2007 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
2009 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2015 macb_free_consistent(bp);
2019 static void macb_init_tieoff(struct macb *bp)
2021 struct macb_dma_desc *d = bp->rx_ring_tieoff;
2023 if (bp->num_queues > 1) {
2024 /* Setup a wrapping descriptor with no free slots
2025 * (WRAP and USED) to tie off/disable unused RX queues.
2027 macb_set_addr(bp, d, MACB_BIT(RX_WRAP) | MACB_BIT(RX_USED));
2032 static void gem_init_rings(struct macb *bp)
2034 struct macb_queue *queue;
2035 struct macb_dma_desc *desc = NULL;
2039 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2040 for (i = 0; i < bp->tx_ring_size; i++) {
2041 desc = macb_tx_desc(queue, i);
2042 macb_set_addr(bp, desc, 0);
2043 desc->ctrl = MACB_BIT(TX_USED);
2045 desc->ctrl |= MACB_BIT(TX_WRAP);
2051 bp->rx_prepared_head = 0;
2054 macb_init_tieoff(bp);
2057 static void macb_init_rings(struct macb *bp)
2060 struct macb_dma_desc *desc = NULL;
2062 macb_init_rx_ring(bp);
2064 for (i = 0; i < bp->tx_ring_size; i++) {
2065 desc = macb_tx_desc(&bp->queues[0], i);
2066 macb_set_addr(bp, desc, 0);
2067 desc->ctrl = MACB_BIT(TX_USED);
2069 bp->queues[0].tx_head = 0;
2070 bp->queues[0].tx_tail = 0;
2071 desc->ctrl |= MACB_BIT(TX_WRAP);
2073 macb_init_tieoff(bp);
2076 static void macb_reset_hw(struct macb *bp)
2078 struct macb_queue *queue;
2081 /* Disable RX and TX (XXX: Should we halt the transmission
2084 macb_writel(bp, NCR, 0);
2086 /* Clear the stats registers (XXX: Update stats first?) */
2087 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
2089 /* Clear all status flags */
2090 macb_writel(bp, TSR, -1);
2091 macb_writel(bp, RSR, -1);
2093 /* Disable RX partial store and forward and reset watermark value */
2094 if (bp->caps & MACB_CAPS_PARTIAL_STORE_FORWARD)
2095 gem_writel(bp, PBUFRXCUT, 0xFFF);
2097 /* Disable all interrupts */
2098 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2099 queue_writel(queue, IDR, -1);
2100 queue_readl(queue, ISR);
2101 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2102 queue_writel(queue, ISR, -1);
2106 static u32 gem_mdc_clk_div(struct macb *bp)
2109 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2111 if (pclk_hz <= 20000000)
2112 config = GEM_BF(CLK, GEM_CLK_DIV8);
2113 else if (pclk_hz <= 40000000)
2114 config = GEM_BF(CLK, GEM_CLK_DIV16);
2115 else if (pclk_hz <= 80000000)
2116 config = GEM_BF(CLK, GEM_CLK_DIV32);
2117 else if (pclk_hz <= 120000000)
2118 config = GEM_BF(CLK, GEM_CLK_DIV48);
2119 else if (pclk_hz <= 160000000)
2120 config = GEM_BF(CLK, GEM_CLK_DIV64);
2122 config = GEM_BF(CLK, GEM_CLK_DIV96);
2127 static u32 macb_mdc_clk_div(struct macb *bp)
2130 unsigned long pclk_hz;
2132 if (macb_is_gem(bp))
2133 return gem_mdc_clk_div(bp);
2135 pclk_hz = clk_get_rate(bp->pclk);
2136 if (pclk_hz <= 20000000)
2137 config = MACB_BF(CLK, MACB_CLK_DIV8);
2138 else if (pclk_hz <= 40000000)
2139 config = MACB_BF(CLK, MACB_CLK_DIV16);
2140 else if (pclk_hz <= 80000000)
2141 config = MACB_BF(CLK, MACB_CLK_DIV32);
2143 config = MACB_BF(CLK, MACB_CLK_DIV64);
2148 /* Get the DMA bus width field of the network configuration register that we
2149 * should program. We find the width from decoding the design configuration
2150 * register to find the maximum supported data bus width.
2152 static u32 macb_dbw(struct macb *bp)
2154 if (!macb_is_gem(bp))
2157 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2159 return GEM_BF(DBW, GEM_DBW128);
2161 return GEM_BF(DBW, GEM_DBW64);
2164 return GEM_BF(DBW, GEM_DBW32);
2168 /* Configure the receive DMA engine
2169 * - use the correct receive buffer size
2170 * - set best burst length for DMA operations
2171 * (if not supported by FIFO, it will fallback to default)
2172 * - set both rx/tx packet buffers to full memory size
2173 * These are configurable parameters for GEM.
2175 static void macb_configure_dma(struct macb *bp)
2179 if (macb_is_gem(bp)) {
2180 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2181 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
2182 if (bp->dma_burst_length)
2183 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2184 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2185 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2188 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2190 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2192 if (bp->dev->features & NETIF_F_HW_CSUM)
2193 dmacfg |= GEM_BIT(TXCOEN);
2195 dmacfg &= ~GEM_BIT(TXCOEN);
2197 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2198 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2199 dmacfg |= GEM_BIT(ADDR64);
2201 #ifdef CONFIG_MACB_USE_HWSTAMP
2202 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2203 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2205 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2207 gem_writel(bp, DMACFG, dmacfg);
2211 static void macb_init_hw(struct macb *bp)
2213 struct macb_queue *queue;
2219 macb_set_hwaddr(bp);
2221 config = macb_mdc_clk_div(bp);
2222 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2223 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
2224 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
2225 config |= MACB_BIT(PAE); /* PAuse Enable */
2227 /* Do not discard Rx FCS if RX checsum offload disabled */
2228 if (bp->dev->features & NETIF_F_RXCSUM)
2229 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
2231 if (bp->caps & MACB_CAPS_JUMBO)
2232 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2234 config |= MACB_BIT(BIG); /* Receive oversized frames */
2235 if (bp->dev->flags & IFF_PROMISC)
2236 config |= MACB_BIT(CAF); /* Copy All Frames */
2237 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2238 config |= GEM_BIT(RXCOEN);
2239 if (!(bp->dev->flags & IFF_BROADCAST))
2240 config |= MACB_BIT(NBC); /* No BroadCast */
2241 config |= macb_dbw(bp);
2242 macb_writel(bp, NCFGR, config);
2243 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2244 gem_writel(bp, JML, bp->jumbo_max_len);
2245 bp->speed = SPEED_10;
2246 if (bp->caps & MACB_CAPS_PARTIAL_STORE_FORWARD)
2247 bp->duplex = DUPLEX_FULL;
2249 bp->duplex = DUPLEX_HALF;
2250 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
2251 if (bp->caps & MACB_CAPS_JUMBO)
2252 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
2254 macb_configure_dma(bp);
2256 /* Enable RX partial store and forward and set watermark */
2257 if (bp->caps & MACB_CAPS_PARTIAL_STORE_FORWARD) {
2258 gem_writel(bp, PBUFRXCUT,
2259 (gem_readl(bp, PBUFRXCUT) &
2260 GEM_BF(WTRMRK, bp->rx_watermark)) |
2261 GEM_BIT(ENCUTTHRU));
2264 /* Initialize TX and RX buffers */
2265 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
2266 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2267 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2268 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
2270 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2271 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
2272 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2273 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2274 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
2276 /* We only use the first queue at the moment. Remaining
2277 * queues must be tied-off before we enable the receiver.
2279 * See the documentation for receive_q1_ptr for more info.
2282 queue_writel(queue, RBQP,
2283 lower_32_bits(bp->rx_ring_tieoff_dma));
2285 /* Enable interrupts */
2286 queue_writel(queue, IER,
2292 if ((bp->phy_interface == PHY_INTERFACE_MODE_SGMII) &&
2293 (bp->caps & MACB_CAPS_PCS))
2294 gem_writel(bp, PCSCNTRL,
2295 gem_readl(bp, PCSCNTRL) | GEM_BIT(PCSAUTONEG));
2297 /* Enable TX and RX */
2298 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE) |
2302 /* The hash address register is 64 bits long and takes up two
2303 * locations in the memory map. The least significant bits are stored
2304 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2306 * The unicast hash enable and the multicast hash enable bits in the
2307 * network configuration register enable the reception of hash matched
2308 * frames. The destination address is reduced to a 6 bit index into
2309 * the 64 bit hash register using the following hash function. The
2310 * hash function is an exclusive or of every sixth bit of the
2311 * destination address.
2313 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2314 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2315 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2316 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2317 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2318 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2320 * da[0] represents the least significant bit of the first byte
2321 * received, that is, the multicast/unicast indicator, and da[47]
2322 * represents the most significant bit of the last byte received. If
2323 * the hash index, hi[n], points to a bit that is set in the hash
2324 * register then the frame will be matched according to whether the
2325 * frame is multicast or unicast. A multicast match will be signalled
2326 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2327 * index points to a bit set in the hash register. A unicast match
2328 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2329 * and the hash index points to a bit set in the hash register. To
2330 * receive all multicast frames, the hash register should be set with
2331 * all ones and the multicast hash enable bit should be set in the
2332 * network configuration register.
2335 static inline int hash_bit_value(int bitnr, __u8 *addr)
2337 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2342 /* Return the hash index value for the specified address. */
2343 static int hash_get_index(__u8 *addr)
2348 for (j = 0; j < 6; j++) {
2349 for (i = 0, bitval = 0; i < 8; i++)
2350 bitval ^= hash_bit_value(i * 6 + j, addr);
2352 hash_index |= (bitval << j);
2358 /* Add multicast addresses to the internal multicast-hash table. */
2359 static void macb_sethashtable(struct net_device *dev)
2361 struct netdev_hw_addr *ha;
2362 unsigned long mc_filter[2];
2364 struct macb *bp = netdev_priv(dev);
2369 netdev_for_each_mc_addr(ha, dev) {
2370 bitnr = hash_get_index(ha->addr);
2371 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2374 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2375 macb_or_gem_writel(bp, HRT, mc_filter[1]);
2378 /* Enable/Disable promiscuous and multicast modes. */
2379 static void macb_set_rx_mode(struct net_device *dev)
2382 struct macb *bp = netdev_priv(dev);
2384 cfg = macb_readl(bp, NCFGR);
2386 if (dev->flags & IFF_PROMISC) {
2387 /* Enable promiscuous mode */
2388 cfg |= MACB_BIT(CAF);
2390 /* Disable RX checksum offload */
2391 if (macb_is_gem(bp))
2392 cfg &= ~GEM_BIT(RXCOEN);
2394 /* Disable promiscuous mode */
2395 cfg &= ~MACB_BIT(CAF);
2397 /* Enable RX checksum offload only if requested */
2398 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2399 cfg |= GEM_BIT(RXCOEN);
2402 if (dev->flags & IFF_ALLMULTI) {
2403 /* Enable all multicast mode */
2404 macb_or_gem_writel(bp, HRB, -1);
2405 macb_or_gem_writel(bp, HRT, -1);
2406 cfg |= MACB_BIT(NCFGR_MTI);
2407 } else if (!netdev_mc_empty(dev)) {
2408 /* Enable specific multicasts */
2409 macb_sethashtable(dev);
2410 cfg |= MACB_BIT(NCFGR_MTI);
2411 } else if (dev->flags & (~IFF_ALLMULTI)) {
2412 /* Disable all multicast mode */
2413 macb_or_gem_writel(bp, HRB, 0);
2414 macb_or_gem_writel(bp, HRT, 0);
2415 cfg &= ~MACB_BIT(NCFGR_MTI);
2418 macb_writel(bp, NCFGR, cfg);
2421 static int macb_open(struct net_device *dev)
2423 struct macb *bp = netdev_priv(dev);
2424 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
2427 netdev_dbg(bp->dev, "open\n");
2429 err = pm_runtime_get_sync(&bp->pdev->dev);
2433 /* carrier starts down */
2434 netif_carrier_off(dev);
2436 /* if the phy is not yet register, retry later*/
2440 /* RX buffers initialization */
2441 macb_init_rx_buffer_size(bp, bufsz);
2443 err = macb_alloc_consistent(bp);
2445 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2450 napi_enable(&bp->napi);
2452 bp->macbgem_ops.mog_init_rings(bp);
2455 /* schedule a link state check */
2456 phy_start(bp->phy_dev);
2458 netif_tx_start_all_queues(dev);
2461 bp->ptp_info->ptp_init(dev);
2466 static int macb_close(struct net_device *dev)
2468 struct macb *bp = netdev_priv(dev);
2469 unsigned long flags;
2471 netif_tx_stop_all_queues(dev);
2472 napi_disable(&bp->napi);
2475 phy_stop(bp->phy_dev);
2477 spin_lock_irqsave(&bp->lock, flags);
2479 netif_carrier_off(dev);
2480 spin_unlock_irqrestore(&bp->lock, flags);
2482 macb_free_consistent(bp);
2485 bp->ptp_info->ptp_remove(dev);
2487 pm_runtime_put(&bp->pdev->dev);
2492 static int macb_change_mtu(struct net_device *dev, int new_mtu)
2494 if (netif_running(dev))
2502 static void gem_update_stats(struct macb *bp)
2505 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2507 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2508 u32 offset = gem_statistics[i].offset;
2509 u64 val = bp->macb_reg_readl(bp, offset);
2511 bp->ethtool_stats[i] += val;
2514 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2515 /* Add GEM_OCTTXH, GEM_OCTRXH */
2516 val = bp->macb_reg_readl(bp, offset + 4);
2517 bp->ethtool_stats[i] += ((u64)val) << 32;
2523 static struct net_device_stats *gem_get_stats(struct macb *bp)
2525 struct gem_stats *hwstat = &bp->hw_stats.gem;
2526 struct net_device_stats *nstat = &bp->dev->stats;
2528 gem_update_stats(bp);
2530 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2531 hwstat->rx_alignment_errors +
2532 hwstat->rx_resource_errors +
2533 hwstat->rx_overruns +
2534 hwstat->rx_oversize_frames +
2535 hwstat->rx_jabbers +
2536 hwstat->rx_undersized_frames +
2537 hwstat->rx_length_field_frame_errors);
2538 nstat->tx_errors = (hwstat->tx_late_collisions +
2539 hwstat->tx_excessive_collisions +
2540 hwstat->tx_underrun +
2541 hwstat->tx_carrier_sense_errors);
2542 nstat->multicast = hwstat->rx_multicast_frames;
2543 nstat->collisions = (hwstat->tx_single_collision_frames +
2544 hwstat->tx_multiple_collision_frames +
2545 hwstat->tx_excessive_collisions);
2546 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2547 hwstat->rx_jabbers +
2548 hwstat->rx_undersized_frames +
2549 hwstat->rx_length_field_frame_errors);
2550 nstat->rx_over_errors = hwstat->rx_resource_errors;
2551 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2552 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2553 nstat->rx_fifo_errors = hwstat->rx_overruns;
2554 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2555 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2556 nstat->tx_fifo_errors = hwstat->tx_underrun;
2561 static void gem_get_ethtool_stats(struct net_device *dev,
2562 struct ethtool_stats *stats, u64 *data)
2566 bp = netdev_priv(dev);
2567 gem_update_stats(bp);
2568 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
2571 static int gem_get_sset_count(struct net_device *dev, int sset)
2575 return GEM_STATS_LEN;
2581 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2587 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2588 memcpy(p, gem_statistics[i].stat_string,
2594 static struct net_device_stats *macb_get_stats(struct net_device *dev)
2596 struct macb *bp = netdev_priv(dev);
2597 struct net_device_stats *nstat = &bp->dev->stats;
2598 struct macb_stats *hwstat = &bp->hw_stats.macb;
2600 if (macb_is_gem(bp))
2601 return gem_get_stats(bp);
2603 /* read stats from hardware */
2604 macb_update_stats(bp);
2606 /* Convert HW stats into netdevice stats */
2607 nstat->rx_errors = (hwstat->rx_fcs_errors +
2608 hwstat->rx_align_errors +
2609 hwstat->rx_resource_errors +
2610 hwstat->rx_overruns +
2611 hwstat->rx_oversize_pkts +
2612 hwstat->rx_jabbers +
2613 hwstat->rx_undersize_pkts +
2614 hwstat->rx_length_mismatch);
2615 nstat->tx_errors = (hwstat->tx_late_cols +
2616 hwstat->tx_excessive_cols +
2617 hwstat->tx_underruns +
2618 hwstat->tx_carrier_errors +
2619 hwstat->sqe_test_errors);
2620 nstat->collisions = (hwstat->tx_single_cols +
2621 hwstat->tx_multiple_cols +
2622 hwstat->tx_excessive_cols);
2623 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2624 hwstat->rx_jabbers +
2625 hwstat->rx_undersize_pkts +
2626 hwstat->rx_length_mismatch);
2627 nstat->rx_over_errors = hwstat->rx_resource_errors +
2628 hwstat->rx_overruns;
2629 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2630 nstat->rx_frame_errors = hwstat->rx_align_errors;
2631 nstat->rx_fifo_errors = hwstat->rx_overruns;
2632 /* XXX: What does "missed" mean? */
2633 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2634 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2635 nstat->tx_fifo_errors = hwstat->tx_underruns;
2636 /* Don't know about heartbeat or window errors... */
2641 static int macb_get_regs_len(struct net_device *netdev)
2643 return MACB_GREGS_NBR * sizeof(u32);
2646 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2649 struct macb *bp = netdev_priv(dev);
2650 unsigned int tail, head;
2653 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2654 | MACB_GREGS_VERSION;
2656 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2657 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
2659 regs_buff[0] = macb_readl(bp, NCR);
2660 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2661 regs_buff[2] = macb_readl(bp, NSR);
2662 regs_buff[3] = macb_readl(bp, TSR);
2663 regs_buff[4] = macb_readl(bp, RBQP);
2664 regs_buff[5] = macb_readl(bp, TBQP);
2665 regs_buff[6] = macb_readl(bp, RSR);
2666 regs_buff[7] = macb_readl(bp, IMR);
2668 regs_buff[8] = tail;
2669 regs_buff[9] = head;
2670 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2671 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
2673 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2674 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
2675 if (macb_is_gem(bp))
2676 regs_buff[13] = gem_readl(bp, DMACFG);
2680 static void macb_get_ringparam(struct net_device *netdev,
2681 struct ethtool_ringparam *ring)
2683 struct macb *bp = netdev_priv(netdev);
2685 ring->rx_max_pending = MAX_RX_RING_SIZE;
2686 ring->tx_max_pending = MAX_TX_RING_SIZE;
2688 ring->rx_pending = bp->rx_ring_size;
2689 ring->tx_pending = bp->tx_ring_size;
2692 static int macb_set_ringparam(struct net_device *netdev,
2693 struct ethtool_ringparam *ring)
2695 struct macb *bp = netdev_priv(netdev);
2696 u32 new_rx_size, new_tx_size;
2697 unsigned int reset = 0;
2699 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2702 new_rx_size = clamp_t(u32, ring->rx_pending,
2703 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2704 new_rx_size = roundup_pow_of_two(new_rx_size);
2706 new_tx_size = clamp_t(u32, ring->tx_pending,
2707 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2708 new_tx_size = roundup_pow_of_two(new_tx_size);
2710 if ((new_tx_size == bp->tx_ring_size) &&
2711 (new_rx_size == bp->rx_ring_size)) {
2716 if (netif_running(bp->dev)) {
2718 macb_close(bp->dev);
2721 bp->rx_ring_size = new_rx_size;
2722 bp->tx_ring_size = new_tx_size;
2730 #ifdef CONFIG_MACB_USE_HWSTAMP
2731 static unsigned int gem_get_tsu_rate(struct macb *bp)
2733 struct clk *tsu_clk;
2734 unsigned int tsu_rate;
2736 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2737 if (!IS_ERR(tsu_clk))
2738 tsu_rate = clk_get_rate(tsu_clk);
2739 /* try pclk instead */
2740 else if (!IS_ERR(bp->pclk)) {
2742 tsu_rate = clk_get_rate(tsu_clk);
2748 static s32 gem_get_ptp_max_adj(void)
2753 static int gem_get_ts_info(struct net_device *dev,
2754 struct ethtool_ts_info *info)
2756 struct macb *bp = netdev_priv(dev);
2758 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2759 ethtool_op_get_ts_info(dev, info);
2763 info->so_timestamping =
2764 SOF_TIMESTAMPING_TX_SOFTWARE |
2765 SOF_TIMESTAMPING_RX_SOFTWARE |
2766 SOF_TIMESTAMPING_SOFTWARE |
2767 SOF_TIMESTAMPING_TX_HARDWARE |
2768 SOF_TIMESTAMPING_RX_HARDWARE |
2769 SOF_TIMESTAMPING_RAW_HARDWARE;
2771 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2772 (1 << HWTSTAMP_TX_OFF) |
2773 (1 << HWTSTAMP_TX_ON);
2775 (1 << HWTSTAMP_FILTER_NONE) |
2776 (1 << HWTSTAMP_FILTER_ALL);
2778 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2783 static struct macb_ptp_info gem_ptp_info = {
2784 .ptp_init = gem_ptp_init,
2785 .ptp_remove = gem_ptp_remove,
2786 .get_ptp_max_adj = gem_get_ptp_max_adj,
2787 .get_tsu_rate = gem_get_tsu_rate,
2788 .get_ts_info = gem_get_ts_info,
2789 .get_hwtst = gem_get_hwtst,
2790 .set_hwtst = gem_set_hwtst,
2794 static int macb_get_ts_info(struct net_device *netdev,
2795 struct ethtool_ts_info *info)
2797 struct macb *bp = netdev_priv(netdev);
2800 return bp->ptp_info->get_ts_info(netdev, info);
2802 return ethtool_op_get_ts_info(netdev, info);
2805 static const struct ethtool_ops macb_ethtool_ops = {
2806 .get_regs_len = macb_get_regs_len,
2807 .get_regs = macb_get_regs,
2808 .get_link = ethtool_op_get_link,
2809 .get_ts_info = ethtool_op_get_ts_info,
2810 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2811 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2812 .get_ringparam = macb_get_ringparam,
2813 .set_ringparam = macb_set_ringparam,
2816 static const struct ethtool_ops gem_ethtool_ops = {
2817 .get_regs_len = macb_get_regs_len,
2818 .get_regs = macb_get_regs,
2819 .get_link = ethtool_op_get_link,
2820 .get_ts_info = macb_get_ts_info,
2821 .get_ethtool_stats = gem_get_ethtool_stats,
2822 .get_strings = gem_get_ethtool_strings,
2823 .get_sset_count = gem_get_sset_count,
2824 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2825 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2826 .get_ringparam = macb_get_ringparam,
2827 .set_ringparam = macb_set_ringparam,
2830 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2832 struct phy_device *phydev = dev->phydev;
2833 struct macb *bp = netdev_priv(dev);
2835 if (!netif_running(dev))
2842 return phy_mii_ioctl(phydev, rq, cmd);
2846 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2848 return bp->ptp_info->get_hwtst(dev, rq);
2850 return phy_mii_ioctl(phydev, rq, cmd);
2854 static int macb_set_features(struct net_device *netdev,
2855 netdev_features_t features)
2857 struct macb *bp = netdev_priv(netdev);
2858 netdev_features_t changed = features ^ netdev->features;
2860 /* TX checksum offload */
2861 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2864 dmacfg = gem_readl(bp, DMACFG);
2865 if (features & NETIF_F_HW_CSUM)
2866 dmacfg |= GEM_BIT(TXCOEN);
2868 dmacfg &= ~GEM_BIT(TXCOEN);
2869 gem_writel(bp, DMACFG, dmacfg);
2872 /* RX checksum offload */
2873 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2876 netcfg = gem_readl(bp, NCFGR);
2877 if (features & NETIF_F_RXCSUM &&
2878 !(netdev->flags & IFF_PROMISC))
2879 netcfg |= GEM_BIT(RXCOEN);
2881 netcfg &= ~GEM_BIT(RXCOEN);
2882 gem_writel(bp, NCFGR, netcfg);
2888 static const struct net_device_ops macb_netdev_ops = {
2889 .ndo_open = macb_open,
2890 .ndo_stop = macb_close,
2891 .ndo_start_xmit = macb_start_xmit,
2892 .ndo_set_rx_mode = macb_set_rx_mode,
2893 .ndo_get_stats = macb_get_stats,
2894 .ndo_do_ioctl = macb_ioctl,
2895 .ndo_validate_addr = eth_validate_addr,
2896 .ndo_change_mtu = macb_change_mtu,
2897 .ndo_set_mac_address = eth_mac_addr,
2898 #ifdef CONFIG_NET_POLL_CONTROLLER
2899 .ndo_poll_controller = macb_poll_controller,
2901 .ndo_set_features = macb_set_features,
2902 .ndo_features_check = macb_features_check,
2905 /* Configure peripheral capabilities according to device tree
2906 * and integration options used
2908 static void macb_configure_caps(struct macb *bp,
2909 const struct macb_config *dt_conf)
2915 bp->caps = dt_conf->caps;
2917 /* By default we set to partial store and forward mode for zynqmp.
2918 * Disable if not set in devicetree.
2920 if (bp->caps & MACB_CAPS_PARTIAL_STORE_FORWARD) {
2921 retval = of_property_read_u16(bp->pdev->dev.of_node,
2925 /* Disable partial store and forward in case of error or
2926 * invalid watermark value
2928 if (retval || bp->rx_watermark > 0xFFF) {
2929 dev_info(&bp->pdev->dev,
2930 "Not enabling partial store and forward\n");
2931 bp->caps &= ~MACB_CAPS_PARTIAL_STORE_FORWARD;
2935 if (hw_is_gem(bp->regs, bp->native_io)) {
2936 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2938 dcfg = gem_readl(bp, DCFG1);
2939 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2940 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2941 dcfg = gem_readl(bp, DCFG2);
2942 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2943 bp->caps |= MACB_CAPS_FIFO_MODE;
2944 #ifdef CONFIG_MACB_USE_HWSTAMP
2945 if (gem_has_ptp(bp)) {
2946 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
2947 pr_err("GEM doesn't support hardware ptp.\n");
2949 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
2950 bp->ptp_info = &gem_ptp_info;
2956 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
2959 #if defined(CONFIG_OF)
2960 static void macb_reset_phy(struct platform_device *pdev)
2962 int err, phy_reset, msec = 1;
2964 struct device_node *np = pdev->dev.of_node;
2969 of_property_read_u32(np, "phy-reset-duration", &msec);
2970 active_low = of_property_read_bool(np, "phy-reset-active-low");
2972 phy_reset = of_get_named_gpio(np, "phy-reset-gpio", 0);
2973 if (!gpio_is_valid(phy_reset))
2976 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2977 active_low ? GPIOF_OUT_INIT_LOW :
2978 GPIOF_OUT_INIT_HIGH, "phy-reset");
2980 dev_err(&pdev->dev, "failed to get phy-reset-gpio: %d\n", err);
2984 gpio_set_value(phy_reset, active_low);
2986 #else /* CONFIG_OF */
2987 static void macb_reset_phy(struct platform_device *pdev)
2990 #endif /* CONFIG_OF */
2992 static void macb_probe_queues(void __iomem *mem,
2994 unsigned int *queue_mask,
2995 unsigned int *num_queues)
3002 /* is it macb or gem ?
3004 * We need to read directly from the hardware here because
3005 * we are early in the probe process and don't have the
3006 * MACB_CAPS_MACB_IS_GEM flag positioned
3008 if (!hw_is_gem(mem, native_io))
3011 /* bit 0 is never set but queue 0 always exists */
3012 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3016 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3017 if (*queue_mask & (1 << hw_q))
3021 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
3022 struct clk **hclk, struct clk **tx_clk,
3023 struct clk **rx_clk, struct clk **tsu_clk)
3027 *pclk = devm_clk_get(&pdev->dev, "pclk");
3028 if (IS_ERR(*pclk)) {
3029 err = PTR_ERR(*pclk);
3030 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3034 *hclk = devm_clk_get(&pdev->dev, "hclk");
3035 if (IS_ERR(*hclk)) {
3036 err = PTR_ERR(*hclk);
3037 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3041 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3042 if (IS_ERR(*tx_clk))
3045 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3046 if (IS_ERR(*rx_clk))
3049 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3050 if (IS_ERR(*tsu_clk))
3053 err = clk_prepare_enable(*pclk);
3055 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3059 err = clk_prepare_enable(*hclk);
3061 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3062 goto err_disable_pclk;
3065 err = clk_prepare_enable(*tx_clk);
3067 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3068 goto err_disable_hclk;
3071 err = clk_prepare_enable(*rx_clk);
3073 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3074 goto err_disable_txclk;
3077 err = clk_prepare_enable(*tsu_clk);
3079 dev_err(&pdev->dev, "failed to enable tsu_clk (%u)\n", err);
3080 goto err_disable_rxclk;
3086 clk_disable_unprepare(*rx_clk);
3089 clk_disable_unprepare(*tx_clk);
3092 clk_disable_unprepare(*hclk);
3095 clk_disable_unprepare(*pclk);
3100 static int macb_init(struct platform_device *pdev)
3102 struct net_device *dev = platform_get_drvdata(pdev);
3103 unsigned int hw_q, q;
3104 struct macb *bp = netdev_priv(dev);
3105 struct macb_queue *queue;
3109 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3110 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3112 /* set the queue register mapping once for all: queue0 has a special
3113 * register mapping but we don't want to test the queue index then
3114 * compute the corresponding register offset at run time.
3116 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
3117 if (!(bp->queue_mask & (1 << hw_q)))
3120 queue = &bp->queues[q];
3123 queue->ISR = GEM_ISR(hw_q - 1);
3124 queue->IER = GEM_IER(hw_q - 1);
3125 queue->IDR = GEM_IDR(hw_q - 1);
3126 queue->IMR = GEM_IMR(hw_q - 1);
3127 queue->TBQP = GEM_TBQP(hw_q - 1);
3128 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3129 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
3130 queue->TBQPH = GEM_TBQPH(hw_q - 1);
3132 queue->RBQP = GEM_RBQP(hw_q - 1);
3134 /* queue0 uses legacy registers */
3135 queue->ISR = MACB_ISR;
3136 queue->IER = MACB_IER;
3137 queue->IDR = MACB_IDR;
3138 queue->IMR = MACB_IMR;
3139 queue->TBQP = MACB_TBQP;
3140 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3141 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
3142 queue->TBQPH = MACB_TBQPH;
3144 queue->RBQP = MACB_RBQP;
3147 /* get irq: here we use the linux queue index, not the hardware
3148 * queue index. the queue irq definitions in the device tree
3149 * must remove the optional gaps that could exist in the
3150 * hardware queue mask.
3152 queue->irq = platform_get_irq(pdev, q);
3153 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
3154 IRQF_SHARED, dev->name, queue);
3157 "Unable to request IRQ %d (error %d)\n",
3162 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
3166 dev->netdev_ops = &macb_netdev_ops;
3167 netif_napi_add(dev, &bp->napi, macb_poll, 64);
3169 /* setup appropriated routines according to adapter type */
3170 if (macb_is_gem(bp)) {
3171 bp->max_tx_length = GEM_MAX_TX_LEN;
3172 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3173 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3174 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3175 bp->macbgem_ops.mog_rx = gem_rx;
3176 dev->ethtool_ops = &gem_ethtool_ops;
3178 bp->max_tx_length = MACB_MAX_TX_LEN;
3179 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3180 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3181 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3182 bp->macbgem_ops.mog_rx = macb_rx;
3183 dev->ethtool_ops = &macb_ethtool_ops;
3187 dev->hw_features = NETIF_F_SG;
3189 /* Check LSO capability */
3190 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3191 dev->hw_features |= MACB_NETIF_LSO;
3193 /* Checksum offload is only available on gem with packet buffer */
3194 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
3195 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3196 if (bp->caps & MACB_CAPS_PARTIAL_STORE_FORWARD)
3197 dev->hw_features &= ~NETIF_F_RXCSUM;
3198 if (bp->caps & MACB_CAPS_SG_DISABLED)
3199 dev->hw_features &= ~NETIF_F_SG;
3200 dev->features = dev->hw_features;
3202 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3204 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3205 val = GEM_BIT(RGMII);
3206 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
3207 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3208 val = MACB_BIT(RMII);
3209 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3210 val = MACB_BIT(MII);
3212 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3213 val |= MACB_BIT(CLKEN);
3215 macb_or_gem_writel(bp, USRIO, val);
3218 /* Set MII management clock divider */
3219 val = macb_mdc_clk_div(bp);
3220 val |= macb_dbw(bp);
3221 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3222 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
3223 macb_writel(bp, NCFGR, val);
3225 if ((bp->phy_interface == PHY_INTERFACE_MODE_SGMII) &&
3226 (bp->caps & MACB_CAPS_PCS))
3227 gem_writel(bp, PCSCNTRL,
3228 gem_readl(bp, PCSCNTRL) | GEM_BIT(PCSAUTONEG));
3233 #if defined(CONFIG_OF)
3234 /* 1518 rounded up */
3235 #define AT91ETHER_MAX_RBUFF_SZ 0x600
3236 /* max number of receive buffers */
3237 #define AT91ETHER_MAX_RX_DESCR 9
3239 /* Initialize and start the Receiver and Transmit subsystems */
3240 static int at91ether_start(struct net_device *dev)
3242 struct macb *lp = netdev_priv(dev);
3243 struct macb_dma_desc *desc;
3248 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
3249 (AT91ETHER_MAX_RX_DESCR *
3250 macb_dma_desc_get_size(lp)),
3251 &lp->rx_ring_dma, GFP_KERNEL);
3255 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
3256 AT91ETHER_MAX_RX_DESCR *
3257 AT91ETHER_MAX_RBUFF_SZ,
3258 &lp->rx_buffers_dma, GFP_KERNEL);
3259 if (!lp->rx_buffers) {
3260 dma_free_coherent(&lp->pdev->dev,
3261 AT91ETHER_MAX_RX_DESCR *
3262 macb_dma_desc_get_size(lp),
3263 lp->rx_ring, lp->rx_ring_dma);
3268 addr = lp->rx_buffers_dma;
3269 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
3270 desc = macb_rx_desc(lp, i);
3271 macb_set_addr(lp, desc, addr);
3273 addr += AT91ETHER_MAX_RBUFF_SZ;
3276 /* Set the Wrap bit on the last descriptor */
3277 desc->addr |= MACB_BIT(RX_WRAP);
3279 /* Reset buffer index */
3282 /* Program address of descriptor list in Rx Buffer Queue register */
3283 macb_writel(lp, RBQP, lp->rx_ring_dma);
3285 /* Enable Receive and Transmit */
3286 ctl = macb_readl(lp, NCR);
3287 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3292 /* Open the ethernet interface */
3293 static int at91ether_open(struct net_device *dev)
3295 struct macb *lp = netdev_priv(dev);
3299 /* Clear internal statistics */
3300 ctl = macb_readl(lp, NCR);
3301 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3303 macb_set_hwaddr(lp);
3305 ret = at91ether_start(dev);
3309 /* Enable MAC interrupts */
3310 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3312 MACB_BIT(ISR_TUND) |
3315 MACB_BIT(ISR_ROVR) |
3318 /* schedule a link state check */
3319 phy_start(lp->phy_dev);
3321 netif_start_queue(dev);
3326 /* Close the interface */
3327 static int at91ether_close(struct net_device *dev)
3329 struct macb *lp = netdev_priv(dev);
3332 /* Disable Receiver and Transmitter */
3333 ctl = macb_readl(lp, NCR);
3334 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3336 /* Disable MAC interrupts */
3337 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3339 MACB_BIT(ISR_TUND) |
3342 MACB_BIT(ISR_ROVR) |
3345 netif_stop_queue(dev);
3347 dma_free_coherent(&lp->pdev->dev,
3348 AT91ETHER_MAX_RX_DESCR *
3349 macb_dma_desc_get_size(lp),
3350 lp->rx_ring, lp->rx_ring_dma);
3353 dma_free_coherent(&lp->pdev->dev,
3354 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3355 lp->rx_buffers, lp->rx_buffers_dma);
3356 lp->rx_buffers = NULL;
3361 /* Transmit packet */
3362 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3364 struct macb *lp = netdev_priv(dev);
3366 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3367 netif_stop_queue(dev);
3369 /* Store packet information (to free when Tx completed) */
3371 lp->skb_length = skb->len;
3372 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3374 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3375 dev_kfree_skb_any(skb);
3376 dev->stats.tx_dropped++;
3377 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3378 return NETDEV_TX_OK;
3381 /* Set address of the data in the Transmit Address register */
3382 macb_writel(lp, TAR, lp->skb_physaddr);
3383 /* Set length of the packet in the Transmit Control register */
3384 macb_writel(lp, TCR, skb->len);
3387 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3388 return NETDEV_TX_BUSY;
3391 return NETDEV_TX_OK;
3394 /* Extract received frame from buffer descriptors and sent to upper layers.
3395 * (Called from interrupt context)
3397 static void at91ether_rx(struct net_device *dev)
3399 struct macb *lp = netdev_priv(dev);
3400 struct macb_dma_desc *desc;
3401 unsigned char *p_recv;
3402 struct sk_buff *skb;
3403 unsigned int pktlen;
3405 desc = macb_rx_desc(lp, lp->rx_tail);
3406 while (desc->addr & MACB_BIT(RX_USED)) {
3407 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
3408 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
3409 skb = netdev_alloc_skb(dev, pktlen + 2);
3411 skb_reserve(skb, 2);
3412 skb_put_data(skb, p_recv, pktlen);
3414 skb->protocol = eth_type_trans(skb, dev);
3415 dev->stats.rx_packets++;
3416 dev->stats.rx_bytes += pktlen;
3419 dev->stats.rx_dropped++;
3422 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
3423 dev->stats.multicast++;
3425 /* reset ownership bit */
3426 desc->addr &= ~MACB_BIT(RX_USED);
3428 /* wrap after last buffer */
3429 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3434 desc = macb_rx_desc(lp, lp->rx_tail);
3438 /* MAC interrupt handler */
3439 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3441 struct net_device *dev = dev_id;
3442 struct macb *lp = netdev_priv(dev);
3445 /* MAC Interrupt Status register indicates what interrupts are pending.
3446 * It is automatically cleared once read.
3448 intstatus = macb_readl(lp, ISR);
3450 /* Receive complete */
3451 if (intstatus & MACB_BIT(RCOMP))
3454 /* Transmit complete */
3455 if (intstatus & MACB_BIT(TCOMP)) {
3456 /* The TCOM bit is set even if the transmission failed */
3457 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
3458 dev->stats.tx_errors++;
3461 dev_kfree_skb_irq(lp->skb);
3463 dma_unmap_single(NULL, lp->skb_physaddr,
3464 lp->skb_length, DMA_TO_DEVICE);
3465 dev->stats.tx_packets++;
3466 dev->stats.tx_bytes += lp->skb_length;
3468 netif_wake_queue(dev);
3471 /* Work-around for EMAC Errata section 41.3.1 */
3472 if (intstatus & MACB_BIT(RXUBR)) {
3473 ctl = macb_readl(lp, NCR);
3474 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
3476 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3479 if (intstatus & MACB_BIT(ISR_ROVR))
3480 netdev_err(dev, "ROVR error\n");
3485 #ifdef CONFIG_NET_POLL_CONTROLLER
3486 static void at91ether_poll_controller(struct net_device *dev)
3488 unsigned long flags;
3490 local_irq_save(flags);
3491 at91ether_interrupt(dev->irq, dev);
3492 local_irq_restore(flags);
3496 static const struct net_device_ops at91ether_netdev_ops = {
3497 .ndo_open = at91ether_open,
3498 .ndo_stop = at91ether_close,
3499 .ndo_start_xmit = at91ether_start_xmit,
3500 .ndo_get_stats = macb_get_stats,
3501 .ndo_set_rx_mode = macb_set_rx_mode,
3502 .ndo_set_mac_address = eth_mac_addr,
3503 .ndo_do_ioctl = macb_ioctl,
3504 .ndo_validate_addr = eth_validate_addr,
3505 .ndo_change_mtu = eth_change_mtu,
3506 #ifdef CONFIG_NET_POLL_CONTROLLER
3507 .ndo_poll_controller = at91ether_poll_controller,
3511 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
3512 struct clk **hclk, struct clk **tx_clk,
3513 struct clk **rx_clk, struct clk **tsu_clk)
3522 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3524 return PTR_ERR(*pclk);
3526 err = clk_prepare_enable(*pclk);
3528 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3535 static int at91ether_init(struct platform_device *pdev)
3537 struct net_device *dev = platform_get_drvdata(pdev);
3538 struct macb *bp = netdev_priv(dev);
3542 dev->netdev_ops = &at91ether_netdev_ops;
3543 dev->ethtool_ops = &macb_ethtool_ops;
3545 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3550 macb_writel(bp, NCR, 0);
3552 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3553 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3554 reg |= MACB_BIT(RM9200_RMII);
3556 macb_writel(bp, NCFGR, reg);
3561 static const struct macb_config at91sam9260_config = {
3562 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3563 .clk_init = macb_clk_init,
3567 static const struct macb_config pc302gem_config = {
3568 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3569 .dma_burst_length = 16,
3570 .clk_init = macb_clk_init,
3574 static const struct macb_config sama5d2_config = {
3575 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3576 .dma_burst_length = 16,
3577 .clk_init = macb_clk_init,
3581 static const struct macb_config sama5d3_config = {
3582 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3583 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
3584 .dma_burst_length = 16,
3585 .clk_init = macb_clk_init,
3587 .jumbo_max_len = 10240,
3590 static const struct macb_config sama5d4_config = {
3591 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3592 .dma_burst_length = 4,
3593 .clk_init = macb_clk_init,
3597 static const struct macb_config emac_config = {
3598 .clk_init = at91ether_clk_init,
3599 .init = at91ether_init,
3602 static const struct macb_config np4_config = {
3603 .caps = MACB_CAPS_USRIO_DISABLED,
3604 .clk_init = macb_clk_init,
3608 static const struct macb_config zynqmp_config = {
3609 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
3610 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_PCS |
3611 MACB_CAPS_PARTIAL_STORE_FORWARD | MACB_CAPS_WOL,
3612 .dma_burst_length = 16,
3613 .clk_init = macb_clk_init,
3615 .jumbo_max_len = 10240,
3618 static const struct macb_config zynq_config = {
3619 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
3620 .dma_burst_length = 16,
3621 .clk_init = macb_clk_init,
3625 static const struct of_device_id macb_dt_ids[] = {
3626 { .compatible = "cdns,at32ap7000-macb" },
3627 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3628 { .compatible = "cdns,macb" },
3629 { .compatible = "cdns,np4-macb", .data = &np4_config },
3630 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3631 { .compatible = "cdns,gem", .data = &pc302gem_config },
3632 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
3633 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3634 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3635 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3636 { .compatible = "cdns,emac", .data = &emac_config },
3637 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
3638 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
3641 MODULE_DEVICE_TABLE(of, macb_dt_ids);
3642 #endif /* CONFIG_OF */
3644 static int macb_probe(struct platform_device *pdev)
3646 int (*clk_init)(struct platform_device *, struct clk **,
3647 struct clk **, struct clk **, struct clk **,
3648 struct clk **) = macb_clk_init;
3649 int (*init)(struct platform_device *) = macb_init;
3650 struct device_node *np = pdev->dev.of_node;
3651 struct device_node *phy_node;
3652 const struct macb_config *macb_config = NULL;
3653 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
3654 struct clk *tsu_clk = NULL;
3655 unsigned int queue_mask, num_queues;
3656 struct macb_platform_data *pdata;
3658 struct phy_device *phydev;
3659 struct net_device *dev;
3660 struct resource *regs;
3666 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3667 mem = devm_ioremap_resource(&pdev->dev, regs);
3669 return PTR_ERR(mem);
3672 const struct of_device_id *match;
3674 match = of_match_node(macb_dt_ids, np);
3675 if (match && match->data) {
3676 macb_config = match->data;
3677 clk_init = macb_config->clk_init;
3678 init = macb_config->init;
3682 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
3686 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
3687 pm_runtime_use_autosuspend(&pdev->dev);
3688 pm_runtime_get_noresume(&pdev->dev);
3689 pm_runtime_set_active(&pdev->dev);
3690 pm_runtime_enable(&pdev->dev);
3691 native_io = hw_is_native_io(mem);
3693 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
3694 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
3697 goto err_disable_clocks;
3700 dev->base_addr = regs->start;
3702 SET_NETDEV_DEV(dev, &pdev->dev);
3704 bp = netdev_priv(dev);
3708 bp->native_io = native_io;
3710 bp->macb_reg_readl = hw_readl_native;
3711 bp->macb_reg_writel = hw_writel_native;
3713 bp->macb_reg_readl = hw_readl;
3714 bp->macb_reg_writel = hw_writel;
3716 bp->num_queues = num_queues;
3717 bp->queue_mask = queue_mask;
3719 bp->dma_burst_length = macb_config->dma_burst_length;
3722 bp->tx_clk = tx_clk;
3723 bp->rx_clk = rx_clk;
3724 bp->tsu_clk = tsu_clk;
3726 bp->tsu_rate = clk_get_rate(tsu_clk);
3729 bp->jumbo_max_len = macb_config->jumbo_max_len;
3731 spin_lock_init(&bp->lock);
3733 /* setup capabilities */
3734 macb_configure_caps(bp, macb_config);
3736 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3737 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3738 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3739 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3742 platform_set_drvdata(pdev, dev);
3744 dev->irq = platform_get_irq(pdev, 0);
3747 goto err_out_free_netdev;
3750 /* MTU range: 68 - 1500 or 10240 */
3751 dev->min_mtu = GEM_MTU_MIN_SIZE;
3752 if (bp->caps & MACB_CAPS_JUMBO)
3753 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3755 dev->max_mtu = ETH_DATA_LEN;
3757 mac = of_get_mac_address(np);
3759 ether_addr_copy(bp->dev->dev_addr, mac);
3761 macb_get_hwaddr(bp);
3763 /* Power up the PHY if there is a GPIO reset */
3764 phy_node = of_parse_phandle(np, "phy-handle", 0);
3765 if (!phy_node && of_phy_is_fixed_link(np)) {
3766 err = of_phy_register_fixed_link(np);
3768 dev_err(&pdev->dev, "broken fixed-link specification");
3771 phy_node = of_node_get(np);
3772 bp->phy_node = phy_node;
3774 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
3775 if (gpio_is_valid(gpio)) {
3776 bp->reset_gpio = gpio_to_desc(gpio);
3777 gpiod_direction_output(bp->reset_gpio, 1);
3781 err = of_get_phy_mode(np);
3783 pdata = dev_get_platdata(&pdev->dev);
3784 if (pdata && pdata->is_rmii)
3785 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3787 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3789 bp->phy_interface = err;
3792 macb_reset_phy(pdev);
3794 /* IP specific init */
3797 goto err_out_free_netdev;
3799 err = register_netdev(dev);
3801 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
3802 goto err_out_unregister_netdev;
3805 err = macb_mii_init(bp);
3807 goto err_out_unregister_netdev;
3809 netif_carrier_off(dev);
3811 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
3814 if (bp->caps & MACB_CAPS_WOL)
3815 device_set_wakeup_capable(&bp->dev->dev, 1);
3817 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3818 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3819 dev->base_addr, dev->irq, dev->dev_addr);
3821 phydev = bp->phy_dev;
3822 phy_attached_info(phydev);
3823 pm_runtime_mark_last_busy(&bp->pdev->dev);
3824 pm_runtime_put_autosuspend(&bp->pdev->dev);
3828 err_out_unregister_netdev:
3829 unregister_netdev(dev);
3831 err_out_free_netdev:
3835 of_node_put(phy_node);
3838 clk_disable_unprepare(tx_clk);
3839 clk_disable_unprepare(hclk);
3840 clk_disable_unprepare(pclk);
3841 clk_disable_unprepare(rx_clk);
3842 clk_disable_unprepare(tsu_clk);
3843 pm_runtime_disable(&pdev->dev);
3844 pm_runtime_set_suspended(&pdev->dev);
3845 pm_runtime_dont_use_autosuspend(&pdev->dev);
3850 static int macb_remove(struct platform_device *pdev)
3852 struct net_device *dev;
3855 dev = platform_get_drvdata(pdev);
3858 bp = netdev_priv(dev);
3860 phy_disconnect(bp->phy_dev);
3861 mdiobus_unregister(bp->mii_bus);
3863 mdiobus_free(bp->mii_bus);
3865 /* Shutdown the PHY if there is a GPIO reset */
3867 gpiod_set_value(bp->reset_gpio, 0);
3869 unregister_netdev(dev);
3870 pm_runtime_disable(&pdev->dev);
3871 pm_runtime_dont_use_autosuspend(&pdev->dev);
3872 if (!pm_runtime_suspended(&pdev->dev)) {
3873 clk_disable_unprepare(bp->tx_clk);
3874 clk_disable_unprepare(bp->hclk);
3875 clk_disable_unprepare(bp->pclk);
3876 clk_disable_unprepare(bp->rx_clk);
3877 clk_disable_unprepare(bp->tsu_clk);
3878 pm_runtime_set_suspended(&pdev->dev);
3880 of_node_put(bp->phy_node);
3887 static int __maybe_unused macb_suspend(struct device *dev)
3889 struct platform_device *pdev = to_platform_device(dev);
3890 struct net_device *netdev = platform_get_drvdata(pdev);
3891 struct macb *bp = netdev_priv(netdev);
3892 struct macb_queue *queue = bp->queues;
3893 unsigned long flags;
3895 u32 ctrl, arpipmask;
3897 if (!netif_running(netdev))
3900 if (device_may_wakeup(&bp->dev->dev)) {
3901 spin_lock_irqsave(&bp->lock, flags);
3902 ctrl = macb_readl(bp, NCR);
3903 ctrl &= ~(MACB_BIT(TE) | MACB_BIT(RE));
3904 macb_writel(bp, NCR, ctrl);
3905 /* Tie off RXQ0 as well */
3906 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_tieoff_dma));
3907 ctrl = macb_readl(bp, NCR);
3908 ctrl |= MACB_BIT(RE);
3909 macb_writel(bp, NCR, ctrl);
3910 gem_writel(bp, NCFGR, gem_readl(bp, NCFGR) & ~MACB_BIT(NBC));
3911 macb_writel(bp, TSR, -1);
3912 macb_writel(bp, RSR, -1);
3913 macb_readl(bp, ISR);
3914 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
3915 macb_writel(bp, ISR, -1);
3917 /* Enable WOL (Q0 only) and disable all other interrupts */
3918 macb_writel(bp, IER, MACB_BIT(WOL));
3919 for (q = 1, queue = bp->queues; q < bp->num_queues;
3921 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
3926 arpipmask = cpu_to_be32p(&bp->dev->ip_ptr->ifa_list->ifa_local)
3928 gem_writel(bp, WOL, MACB_BIT(ARP) | arpipmask);
3929 spin_unlock_irqrestore(&bp->lock, flags);
3930 enable_irq_wake(bp->queues[0].irq);
3931 netif_device_detach(netdev);
3932 napi_disable(&bp->napi);
3934 netif_device_detach(netdev);
3935 napi_disable(&bp->napi);
3936 phy_stop(bp->phy_dev);
3937 phy_suspend(bp->phy_dev);
3938 spin_lock_irqsave(&bp->lock, flags);
3940 spin_unlock_irqrestore(&bp->lock, flags);
3943 netif_carrier_off(netdev);
3945 bp->ptp_info->ptp_remove(netdev);
3946 pm_runtime_force_suspend(dev);
3951 static int __maybe_unused macb_resume(struct device *dev)
3953 struct platform_device *pdev = to_platform_device(dev);
3954 struct net_device *netdev = platform_get_drvdata(pdev);
3955 struct macb *bp = netdev_priv(netdev);
3956 unsigned long flags;
3958 if (!netif_running(netdev))
3961 pm_runtime_force_resume(dev);
3963 if (device_may_wakeup(&bp->dev->dev)) {
3964 spin_lock_irqsave(&bp->lock, flags);
3965 macb_writel(bp, IDR, MACB_BIT(WOL));
3966 gem_writel(bp, WOL, 0);
3967 /* Clear Q0 ISR as WOL was enabled on Q0 */
3968 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
3969 macb_writel(bp, ISR, -1);
3970 disable_irq_wake(bp->queues[0].irq);
3971 spin_unlock_irqrestore(&bp->lock, flags);
3972 macb_writel(bp, NCR, MACB_BIT(MPE));
3973 napi_enable(&bp->napi);
3974 netif_carrier_on(netdev);
3976 macb_writel(bp, NCR, MACB_BIT(MPE));
3977 napi_enable(&bp->napi);
3978 netif_carrier_on(netdev);
3979 phy_resume(bp->phy_dev);
3980 phy_start(bp->phy_dev);
3983 bp->macbgem_ops.mog_init_rings(bp);
3985 macb_set_rx_mode(netdev);
3986 netif_device_attach(netdev);
3988 bp->ptp_info->ptp_init(netdev);
3993 static int __maybe_unused macb_runtime_suspend(struct device *dev)
3995 struct platform_device *pdev = to_platform_device(dev);
3996 struct net_device *netdev = platform_get_drvdata(pdev);
3997 struct macb *bp = netdev_priv(netdev);
3999 if (!(device_may_wakeup(&bp->dev->dev))) {
4000 clk_disable_unprepare(bp->tx_clk);
4001 clk_disable_unprepare(bp->hclk);
4002 clk_disable_unprepare(bp->pclk);
4003 clk_disable_unprepare(bp->rx_clk);
4005 clk_disable_unprepare(bp->tsu_clk);
4010 static int __maybe_unused macb_runtime_resume(struct device *dev)
4012 struct platform_device *pdev = to_platform_device(dev);
4013 struct net_device *netdev = platform_get_drvdata(pdev);
4014 struct macb *bp = netdev_priv(netdev);
4016 if (!(device_may_wakeup(&bp->dev->dev))) {
4017 clk_prepare_enable(bp->pclk);
4018 clk_prepare_enable(bp->hclk);
4019 clk_prepare_enable(bp->tx_clk);
4020 clk_prepare_enable(bp->rx_clk);
4022 clk_prepare_enable(bp->tsu_clk);
4027 static const struct dev_pm_ops macb_pm_ops = {
4028 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4029 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4032 static struct platform_driver macb_driver = {
4033 .probe = macb_probe,
4034 .remove = macb_remove,
4037 .of_match_table = of_match_ptr(macb_dt_ids),
4042 module_platform_driver(macb_driver);
4044 MODULE_LICENSE("GPL");
4045 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
4046 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4047 MODULE_ALIAS("platform:macb");