1 // SPDX-License-Identifier: GPL-2.0-only
3 * Cadence MACB/GEM Ethernet Controller driver
5 * Copyright (C) 2004-2006 Atmel Corporation
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/clk-provider.h>
11 #include <linux/crc32.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/circ_buf.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_device.h>
27 #include <linux/phylink.h>
29 #include <linux/of_device.h>
30 #include <linux/of_gpio.h>
31 #include <linux/of_mdio.h>
32 #include <linux/of_net.h>
34 #include <linux/udp.h>
35 #include <linux/tcp.h>
36 #include <linux/iopoll.h>
37 #include <linux/phy/phy.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/reset.h>
42 /* This structure is only used for MACB on SiFive FU540 devices */
43 struct sifive_fu540_macb_mgmt {
49 #define MACB_RX_BUFFER_SIZE 128
50 #define RX_BUFFER_MULTIPLE 64 /* bytes */
52 #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
53 #define MIN_RX_RING_SIZE 64
54 #define MAX_RX_RING_SIZE 8192
55 #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
58 #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
59 #define MIN_TX_RING_SIZE 64
60 #define MAX_TX_RING_SIZE 4096
61 #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
64 /* level of occupied TX descriptors under which we wake up TX process */
65 #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
67 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
68 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
71 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
74 /* Max length of transmit frame must be a multiple of 8 bytes */
75 #define MACB_TX_LEN_ALIGN 8
76 #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
77 /* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
78 * false amba_error in TX path from the DMA assuming there is not enough
79 * space in the SRAM (16KB) even when there is.
81 #define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
83 #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
84 #define MACB_NETIF_LSO NETIF_F_TSO
86 #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
87 #define MACB_WOL_ENABLED (0x1 << 1)
89 #define HS_SPEED_10000M 4
90 #define MACB_SERDES_RATE_10G 1
92 /* Graceful stop timeouts in us. We should allow up to
93 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
95 #define MACB_HALT_TIMEOUT 1230
97 #define MACB_PM_TIMEOUT 100 /* ms */
99 #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
101 /* DMA buffer descriptor might be different size
102 * depends on hardware configuration:
104 * 1. dma address width 32 bits:
105 * word 1: 32 bit address of Data Buffer
108 * 2. dma address width 64 bits:
109 * word 1: 32 bit address of Data Buffer
111 * word 3: upper 32 bit address of Data Buffer
114 * 3. dma address width 32 bits with hardware timestamping:
115 * word 1: 32 bit address of Data Buffer
117 * word 3: timestamp word 1
118 * word 4: timestamp word 2
120 * 4. dma address width 64 bits with hardware timestamping:
121 * word 1: 32 bit address of Data Buffer
123 * word 3: upper 32 bit address of Data Buffer
125 * word 5: timestamp word 1
126 * word 6: timestamp word 2
128 static unsigned int macb_dma_desc_get_size(struct macb *bp)
131 unsigned int desc_size;
133 switch (bp->hw_dma_cap) {
135 desc_size = sizeof(struct macb_dma_desc)
136 + sizeof(struct macb_dma_desc_64);
139 desc_size = sizeof(struct macb_dma_desc)
140 + sizeof(struct macb_dma_desc_ptp);
142 case HW_DMA_CAP_64B_PTP:
143 desc_size = sizeof(struct macb_dma_desc)
144 + sizeof(struct macb_dma_desc_64)
145 + sizeof(struct macb_dma_desc_ptp);
148 desc_size = sizeof(struct macb_dma_desc);
152 return sizeof(struct macb_dma_desc);
155 static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
158 switch (bp->hw_dma_cap) {
163 case HW_DMA_CAP_64B_PTP:
173 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
174 static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
176 return (struct macb_dma_desc_64 *)((void *)desc
177 + sizeof(struct macb_dma_desc));
181 /* Ring buffer accessors */
182 static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
184 return index & (bp->tx_ring_size - 1);
187 static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
190 index = macb_tx_ring_wrap(queue->bp, index);
191 index = macb_adj_dma_desc_idx(queue->bp, index);
192 return &queue->tx_ring[index];
195 static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
198 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
201 static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
205 offset = macb_tx_ring_wrap(queue->bp, index) *
206 macb_dma_desc_get_size(queue->bp);
208 return queue->tx_ring_dma + offset;
211 static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
213 return index & (bp->rx_ring_size - 1);
216 static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
218 index = macb_rx_ring_wrap(queue->bp, index);
219 index = macb_adj_dma_desc_idx(queue->bp, index);
220 return &queue->rx_ring[index];
223 static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
225 return queue->rx_buffers + queue->bp->rx_buffer_size *
226 macb_rx_ring_wrap(queue->bp, index);
230 static u32 hw_readl_native(struct macb *bp, int offset)
232 return __raw_readl(bp->regs + offset);
235 static void hw_writel_native(struct macb *bp, int offset, u32 value)
237 __raw_writel(value, bp->regs + offset);
240 static u32 hw_readl(struct macb *bp, int offset)
242 return readl_relaxed(bp->regs + offset);
245 static void hw_writel(struct macb *bp, int offset, u32 value)
247 writel_relaxed(value, bp->regs + offset);
250 /* Find the CPU endianness by using the loopback bit of NCR register. When the
251 * CPU is in big endian we need to program swapped mode for management
254 static bool hw_is_native_io(void __iomem *addr)
256 u32 value = MACB_BIT(LLB);
258 __raw_writel(value, addr + MACB_NCR);
259 value = __raw_readl(addr + MACB_NCR);
261 /* Write 0 back to disable everything */
262 __raw_writel(0, addr + MACB_NCR);
264 return value == MACB_BIT(LLB);
267 static bool hw_is_gem(void __iomem *addr, bool native_io)
272 id = __raw_readl(addr + MACB_MID);
274 id = readl_relaxed(addr + MACB_MID);
276 return MACB_BFEXT(IDNUM, id) >= 0x2;
279 static void macb_set_hwaddr(struct macb *bp)
284 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
285 macb_or_gem_writel(bp, SA1B, bottom);
286 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
287 macb_or_gem_writel(bp, SA1T, top);
289 /* Clear unused address register sets */
290 macb_or_gem_writel(bp, SA2B, 0);
291 macb_or_gem_writel(bp, SA2T, 0);
292 macb_or_gem_writel(bp, SA3B, 0);
293 macb_or_gem_writel(bp, SA3T, 0);
294 macb_or_gem_writel(bp, SA4B, 0);
295 macb_or_gem_writel(bp, SA4T, 0);
298 static void macb_get_hwaddr(struct macb *bp)
305 /* Check all 4 address register for valid address */
306 for (i = 0; i < 4; i++) {
307 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
308 top = macb_or_gem_readl(bp, SA1T + i * 8);
310 addr[0] = bottom & 0xff;
311 addr[1] = (bottom >> 8) & 0xff;
312 addr[2] = (bottom >> 16) & 0xff;
313 addr[3] = (bottom >> 24) & 0xff;
314 addr[4] = top & 0xff;
315 addr[5] = (top >> 8) & 0xff;
317 if (is_valid_ether_addr(addr)) {
318 eth_hw_addr_set(bp->dev, addr);
323 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
324 eth_hw_addr_random(bp->dev);
327 static int macb_mdio_wait_for_idle(struct macb *bp)
331 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
332 1, MACB_MDIO_TIMEOUT);
335 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
337 struct macb *bp = bus->priv;
340 status = pm_runtime_get_sync(&bp->pdev->dev);
342 pm_runtime_put_noidle(&bp->pdev->dev);
346 status = macb_mdio_wait_for_idle(bp);
350 if (regnum & MII_ADDR_C45) {
351 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
352 | MACB_BF(RW, MACB_MAN_C45_ADDR)
353 | MACB_BF(PHYA, mii_id)
354 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
355 | MACB_BF(DATA, regnum & 0xFFFF)
356 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
358 status = macb_mdio_wait_for_idle(bp);
362 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
363 | MACB_BF(RW, MACB_MAN_C45_READ)
364 | MACB_BF(PHYA, mii_id)
365 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
366 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
368 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
369 | MACB_BF(RW, MACB_MAN_C22_READ)
370 | MACB_BF(PHYA, mii_id)
371 | MACB_BF(REGA, regnum)
372 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
375 status = macb_mdio_wait_for_idle(bp);
379 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
382 pm_runtime_mark_last_busy(&bp->pdev->dev);
383 pm_runtime_put_autosuspend(&bp->pdev->dev);
388 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
391 struct macb *bp = bus->priv;
394 status = pm_runtime_get_sync(&bp->pdev->dev);
396 pm_runtime_put_noidle(&bp->pdev->dev);
400 status = macb_mdio_wait_for_idle(bp);
402 goto mdio_write_exit;
404 if (regnum & MII_ADDR_C45) {
405 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
406 | MACB_BF(RW, MACB_MAN_C45_ADDR)
407 | MACB_BF(PHYA, mii_id)
408 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
409 | MACB_BF(DATA, regnum & 0xFFFF)
410 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
412 status = macb_mdio_wait_for_idle(bp);
414 goto mdio_write_exit;
416 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
417 | MACB_BF(RW, MACB_MAN_C45_WRITE)
418 | MACB_BF(PHYA, mii_id)
419 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
420 | MACB_BF(CODE, MACB_MAN_C45_CODE)
421 | MACB_BF(DATA, value)));
423 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
424 | MACB_BF(RW, MACB_MAN_C22_WRITE)
425 | MACB_BF(PHYA, mii_id)
426 | MACB_BF(REGA, regnum)
427 | MACB_BF(CODE, MACB_MAN_C22_CODE)
428 | MACB_BF(DATA, value)));
431 status = macb_mdio_wait_for_idle(bp);
433 goto mdio_write_exit;
436 pm_runtime_mark_last_busy(&bp->pdev->dev);
437 pm_runtime_put_autosuspend(&bp->pdev->dev);
442 static void macb_init_buffers(struct macb *bp)
444 struct macb_queue *queue;
447 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
448 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
449 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
450 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
451 queue_writel(queue, RBQPH,
452 upper_32_bits(queue->rx_ring_dma));
454 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
455 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
456 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
457 queue_writel(queue, TBQPH,
458 upper_32_bits(queue->tx_ring_dma));
464 * macb_set_tx_clk() - Set a clock to a new frequency
465 * @bp: pointer to struct macb
466 * @speed: New frequency in Hz
468 static void macb_set_tx_clk(struct macb *bp, int speed)
470 long ferr, rate, rate_rounded;
472 if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
475 /* In case of MII the PHY is the clock master */
476 if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
493 rate_rounded = clk_round_rate(bp->tx_clk, rate);
494 if (rate_rounded < 0)
497 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
500 ferr = abs(rate_rounded - rate);
501 ferr = DIV_ROUND_UP(ferr, rate / 100000);
504 "unable to generate target frequency: %ld Hz\n",
507 if (clk_set_rate(bp->tx_clk, rate_rounded))
508 netdev_err(bp->dev, "adjusting tx_clk failed.\n");
511 static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
512 phy_interface_t interface, int speed,
515 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
518 config = gem_readl(bp, USX_CONTROL);
519 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
520 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
521 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
522 config |= GEM_BIT(TX_EN);
523 gem_writel(bp, USX_CONTROL, config);
526 static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
527 struct phylink_link_state *state)
529 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
532 state->speed = SPEED_10000;
534 state->an_complete = 1;
536 val = gem_readl(bp, USX_STATUS);
537 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
538 val = gem_readl(bp, NCFGR);
539 if (val & GEM_BIT(PAE))
540 state->pause = MLO_PAUSE_RX;
543 static int macb_usx_pcs_config(struct phylink_pcs *pcs,
545 phy_interface_t interface,
546 const unsigned long *advertising,
547 bool permit_pause_to_mac)
549 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
551 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
557 static void macb_pcs_get_state(struct phylink_pcs *pcs,
558 struct phylink_link_state *state)
563 static void macb_pcs_an_restart(struct phylink_pcs *pcs)
568 static int macb_pcs_config(struct phylink_pcs *pcs,
570 phy_interface_t interface,
571 const unsigned long *advertising,
572 bool permit_pause_to_mac)
577 static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
578 .pcs_get_state = macb_usx_pcs_get_state,
579 .pcs_config = macb_usx_pcs_config,
580 .pcs_link_up = macb_usx_pcs_link_up,
583 static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
584 .pcs_get_state = macb_pcs_get_state,
585 .pcs_an_restart = macb_pcs_an_restart,
586 .pcs_config = macb_pcs_config,
589 static void macb_mac_config(struct phylink_config *config, unsigned int mode,
590 const struct phylink_link_state *state)
592 struct net_device *ndev = to_net_dev(config->dev);
593 struct macb *bp = netdev_priv(ndev);
598 spin_lock_irqsave(&bp->lock, flags);
600 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
601 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
603 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
604 if (state->interface == PHY_INTERFACE_MODE_RMII)
605 ctrl |= MACB_BIT(RM9200_RMII);
606 } else if (macb_is_gem(bp)) {
607 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
608 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
610 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
611 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
612 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
613 ctrl |= GEM_BIT(PCSSEL);
614 ncr |= GEM_BIT(ENABLE_HS_MAC);
615 } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
616 bp->phy_interface == PHY_INTERFACE_MODE_MII) {
617 ncr |= MACB_BIT(MIIONRGMII);
621 /* Apply the new configuration, if any */
623 macb_or_gem_writel(bp, NCFGR, ctrl);
626 macb_or_gem_writel(bp, NCR, ncr);
628 /* Disable AN for SGMII fixed link configuration, enable otherwise.
629 * Must be written after PCSSEL is set in NCFGR,
630 * otherwise writes will not take effect.
632 if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
633 u32 pcsctrl, old_pcsctrl;
635 old_pcsctrl = gem_readl(bp, PCSCNTRL);
636 if (mode == MLO_AN_FIXED)
637 pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
639 pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
640 if (old_pcsctrl != pcsctrl)
641 gem_writel(bp, PCSCNTRL, pcsctrl);
644 spin_unlock_irqrestore(&bp->lock, flags);
647 static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
648 phy_interface_t interface)
650 struct net_device *ndev = to_net_dev(config->dev);
651 struct macb *bp = netdev_priv(ndev);
652 struct macb_queue *queue;
656 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
657 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
658 queue_writel(queue, IDR,
659 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
661 /* Disable Rx and Tx */
662 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
663 macb_writel(bp, NCR, ctrl);
665 netif_tx_stop_all_queues(ndev);
668 static void macb_mac_link_up(struct phylink_config *config,
669 struct phy_device *phy,
670 unsigned int mode, phy_interface_t interface,
671 int speed, int duplex,
672 bool tx_pause, bool rx_pause)
674 struct net_device *ndev = to_net_dev(config->dev);
675 struct macb *bp = netdev_priv(ndev);
676 struct macb_queue *queue;
681 spin_lock_irqsave(&bp->lock, flags);
683 ctrl = macb_or_gem_readl(bp, NCFGR);
685 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
687 if (speed == SPEED_100)
688 ctrl |= MACB_BIT(SPD);
691 ctrl |= MACB_BIT(FD);
693 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
694 ctrl &= ~MACB_BIT(PAE);
695 if (macb_is_gem(bp)) {
696 ctrl &= ~GEM_BIT(GBE);
698 if (speed == SPEED_1000)
699 ctrl |= GEM_BIT(GBE);
703 ctrl |= MACB_BIT(PAE);
705 macb_set_tx_clk(bp, speed);
707 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
708 * cleared the pipeline and control registers.
710 bp->macbgem_ops.mog_init_rings(bp);
711 macb_init_buffers(bp);
713 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
714 queue_writel(queue, IER,
715 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
718 macb_or_gem_writel(bp, NCFGR, ctrl);
720 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
721 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
722 gem_readl(bp, HS_MAC_CONFIG)));
724 spin_unlock_irqrestore(&bp->lock, flags);
726 /* Enable Rx and Tx */
727 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
729 netif_tx_wake_all_queues(ndev);
732 static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
733 phy_interface_t interface)
735 struct net_device *ndev = to_net_dev(config->dev);
736 struct macb *bp = netdev_priv(ndev);
738 if (interface == PHY_INTERFACE_MODE_10GBASER)
739 return &bp->phylink_usx_pcs;
740 else if (interface == PHY_INTERFACE_MODE_SGMII)
741 return &bp->phylink_sgmii_pcs;
746 static const struct phylink_mac_ops macb_phylink_ops = {
747 .validate = phylink_generic_validate,
748 .mac_select_pcs = macb_mac_select_pcs,
749 .mac_config = macb_mac_config,
750 .mac_link_down = macb_mac_link_down,
751 .mac_link_up = macb_mac_link_up,
754 static bool macb_phy_handle_exists(struct device_node *dn)
756 dn = of_parse_phandle(dn, "phy-handle", 0);
761 static int macb_phylink_connect(struct macb *bp)
763 struct device_node *dn = bp->pdev->dev.of_node;
764 struct net_device *dev = bp->dev;
765 struct phy_device *phydev;
769 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
771 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
772 phydev = phy_find_first(bp->mii_bus);
774 netdev_err(dev, "no PHY found\n");
778 /* attach the mac to the phy */
779 ret = phylink_connect_phy(bp->phylink, phydev);
783 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
787 phylink_start(bp->phylink);
792 static void macb_get_pcs_fixed_state(struct phylink_config *config,
793 struct phylink_link_state *state)
795 struct net_device *ndev = to_net_dev(config->dev);
796 struct macb *bp = netdev_priv(ndev);
798 state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
801 /* based on au1000_eth. c*/
802 static int macb_mii_probe(struct net_device *dev)
804 struct macb *bp = netdev_priv(dev);
806 bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops;
807 bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops;
809 bp->phylink_config.dev = &dev->dev;
810 bp->phylink_config.type = PHYLINK_NETDEV;
812 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
813 bp->phylink_config.poll_fixed_state = true;
814 bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
817 bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
820 __set_bit(PHY_INTERFACE_MODE_MII,
821 bp->phylink_config.supported_interfaces);
822 __set_bit(PHY_INTERFACE_MODE_RMII,
823 bp->phylink_config.supported_interfaces);
825 /* Determine what modes are supported */
826 if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) {
827 bp->phylink_config.mac_capabilities |= MAC_1000FD;
828 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
829 bp->phylink_config.mac_capabilities |= MAC_1000HD;
831 __set_bit(PHY_INTERFACE_MODE_GMII,
832 bp->phylink_config.supported_interfaces);
833 phy_interface_set_rgmii(bp->phylink_config.supported_interfaces);
835 if (bp->caps & MACB_CAPS_PCS)
836 __set_bit(PHY_INTERFACE_MODE_SGMII,
837 bp->phylink_config.supported_interfaces);
839 if (bp->caps & MACB_CAPS_HIGH_SPEED) {
840 __set_bit(PHY_INTERFACE_MODE_10GBASER,
841 bp->phylink_config.supported_interfaces);
842 bp->phylink_config.mac_capabilities |= MAC_10000FD;
846 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
847 bp->phy_interface, &macb_phylink_ops);
848 if (IS_ERR(bp->phylink)) {
849 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
850 PTR_ERR(bp->phylink));
851 return PTR_ERR(bp->phylink);
857 static int macb_mdiobus_register(struct macb *bp)
859 struct device_node *child, *np = bp->pdev->dev.of_node;
861 /* If we have a child named mdio, probe it instead of looking for PHYs
862 * directly under the MAC node
864 child = of_get_child_by_name(np, "mdio");
866 int ret = of_mdiobus_register(bp->mii_bus, child);
872 if (of_phy_is_fixed_link(np))
873 return mdiobus_register(bp->mii_bus);
875 /* Only create the PHY from the device tree if at least one PHY is
876 * described. Otherwise scan the entire MDIO bus. We do this to support
877 * old device tree that did not follow the best practices and did not
878 * describe their network PHYs.
880 for_each_available_child_of_node(np, child)
881 if (of_mdiobus_child_is_phy(child)) {
882 /* The loop increments the child refcount,
883 * decrement it before returning.
887 return of_mdiobus_register(bp->mii_bus, np);
890 return mdiobus_register(bp->mii_bus);
893 static int macb_mii_init(struct macb *bp)
897 /* Enable management port */
898 macb_writel(bp, NCR, MACB_BIT(MPE));
900 bp->mii_bus = mdiobus_alloc();
906 bp->mii_bus->name = "MACB_mii_bus";
907 bp->mii_bus->read = &macb_mdio_read;
908 bp->mii_bus->write = &macb_mdio_write;
909 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
910 bp->pdev->name, bp->pdev->id);
911 bp->mii_bus->priv = bp;
912 bp->mii_bus->parent = &bp->pdev->dev;
914 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
916 err = macb_mdiobus_register(bp);
918 goto err_out_free_mdiobus;
920 err = macb_mii_probe(bp->dev);
922 goto err_out_unregister_bus;
926 err_out_unregister_bus:
927 mdiobus_unregister(bp->mii_bus);
928 err_out_free_mdiobus:
929 mdiobus_free(bp->mii_bus);
934 static void macb_update_stats(struct macb *bp)
936 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
937 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
938 int offset = MACB_PFR;
940 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
942 for (; p < end; p++, offset += 4)
943 *p += bp->macb_reg_readl(bp, offset);
946 static int macb_halt_tx(struct macb *bp)
948 unsigned long halt_time, timeout;
951 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
953 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
956 status = macb_readl(bp, TSR);
957 if (!(status & MACB_BIT(TGO)))
961 } while (time_before(halt_time, timeout));
966 static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
968 if (tx_skb->mapping) {
969 if (tx_skb->mapped_as_page)
970 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
971 tx_skb->size, DMA_TO_DEVICE);
973 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
974 tx_skb->size, DMA_TO_DEVICE);
979 dev_kfree_skb_any(tx_skb->skb);
984 static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
986 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
987 struct macb_dma_desc_64 *desc_64;
989 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
990 desc_64 = macb_64b_desc(bp, desc);
991 desc_64->addrh = upper_32_bits(addr);
992 /* The low bits of RX address contain the RX_USED bit, clearing
993 * of which allows packet RX. Make sure the high bits are also
994 * visible to HW at that point.
999 desc->addr = lower_32_bits(addr);
1002 static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1004 dma_addr_t addr = 0;
1005 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1006 struct macb_dma_desc_64 *desc_64;
1008 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
1009 desc_64 = macb_64b_desc(bp, desc);
1010 addr = ((u64)(desc_64->addrh) << 32);
1013 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1017 static void macb_tx_error_task(struct work_struct *work)
1019 struct macb_queue *queue = container_of(work, struct macb_queue,
1021 struct macb *bp = queue->bp;
1022 struct macb_tx_skb *tx_skb;
1023 struct macb_dma_desc *desc;
1024 struct sk_buff *skb;
1026 unsigned long flags;
1028 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1029 (unsigned int)(queue - bp->queues),
1030 queue->tx_tail, queue->tx_head);
1032 /* Prevent the queue IRQ handlers from running: each of them may call
1033 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1034 * As explained below, we have to halt the transmission before updating
1035 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1036 * network engine about the macb/gem being halted.
1038 spin_lock_irqsave(&bp->lock, flags);
1040 /* Make sure nobody is trying to queue up new packets */
1041 netif_tx_stop_all_queues(bp->dev);
1043 /* Stop transmission now
1044 * (in case we have just queued new packets)
1045 * macb/gem must be halted to write TBQP register
1047 if (macb_halt_tx(bp))
1048 /* Just complain for now, reinitializing TX path can be good */
1049 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1051 /* Treat frames in TX queue including the ones that caused the error.
1052 * Free transmit buffers in upper layer.
1054 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1057 desc = macb_tx_desc(queue, tail);
1059 tx_skb = macb_tx_skb(queue, tail);
1062 if (ctrl & MACB_BIT(TX_USED)) {
1063 /* skb is set for the last buffer of the frame */
1065 macb_tx_unmap(bp, tx_skb);
1067 tx_skb = macb_tx_skb(queue, tail);
1071 /* ctrl still refers to the first buffer descriptor
1072 * since it's the only one written back by the hardware
1074 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1075 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
1076 macb_tx_ring_wrap(bp, tail),
1078 bp->dev->stats.tx_packets++;
1079 queue->stats.tx_packets++;
1080 bp->dev->stats.tx_bytes += skb->len;
1081 queue->stats.tx_bytes += skb->len;
1084 /* "Buffers exhausted mid-frame" errors may only happen
1085 * if the driver is buggy, so complain loudly about
1086 * those. Statistics are updated by hardware.
1088 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1090 "BUG: TX buffers exhausted mid-frame\n");
1092 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1095 macb_tx_unmap(bp, tx_skb);
1098 /* Set end of TX queue */
1099 desc = macb_tx_desc(queue, 0);
1100 macb_set_addr(bp, desc, 0);
1101 desc->ctrl = MACB_BIT(TX_USED);
1103 /* Make descriptor updates visible to hardware */
1106 /* Reinitialize the TX desc queue */
1107 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1108 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1109 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1110 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
1112 /* Make TX ring reflect state of hardware */
1116 /* Housework before enabling TX IRQ */
1117 macb_writel(bp, TSR, macb_readl(bp, TSR));
1118 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1120 /* Now we are ready to start transmission again */
1121 netif_tx_start_all_queues(bp->dev);
1122 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1124 spin_unlock_irqrestore(&bp->lock, flags);
1127 static void macb_tx_interrupt(struct macb_queue *queue)
1132 struct macb *bp = queue->bp;
1133 u16 queue_index = queue - bp->queues;
1135 status = macb_readl(bp, TSR);
1136 macb_writel(bp, TSR, status);
1138 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1139 queue_writel(queue, ISR, MACB_BIT(TCOMP));
1141 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
1142 (unsigned long)status);
1144 head = queue->tx_head;
1145 for (tail = queue->tx_tail; tail != head; tail++) {
1146 struct macb_tx_skb *tx_skb;
1147 struct sk_buff *skb;
1148 struct macb_dma_desc *desc;
1151 desc = macb_tx_desc(queue, tail);
1153 /* Make hw descriptor updates visible to CPU */
1158 /* TX_USED bit is only set by hardware on the very first buffer
1159 * descriptor of the transmitted frame.
1161 if (!(ctrl & MACB_BIT(TX_USED)))
1164 /* Process all buffers of the current transmitted frame */
1166 tx_skb = macb_tx_skb(queue, tail);
1169 /* First, update TX stats if needed */
1171 if (unlikely(skb_shinfo(skb)->tx_flags &
1173 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
1174 /* skb now belongs to timestamp buffer
1175 * and will be removed later
1179 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
1180 macb_tx_ring_wrap(bp, tail),
1182 bp->dev->stats.tx_packets++;
1183 queue->stats.tx_packets++;
1184 bp->dev->stats.tx_bytes += skb->len;
1185 queue->stats.tx_bytes += skb->len;
1188 /* Now we can safely release resources */
1189 macb_tx_unmap(bp, tx_skb);
1191 /* skb is set only for the last buffer of the frame.
1192 * WARNING: at this point skb has been freed by
1200 queue->tx_tail = tail;
1201 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1202 CIRC_CNT(queue->tx_head, queue->tx_tail,
1203 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
1204 netif_wake_subqueue(bp->dev, queue_index);
1207 static void gem_rx_refill(struct macb_queue *queue)
1210 struct sk_buff *skb;
1212 struct macb *bp = queue->bp;
1213 struct macb_dma_desc *desc;
1215 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1216 bp->rx_ring_size) > 0) {
1217 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
1219 /* Make hw descriptor updates visible to CPU */
1222 queue->rx_prepared_head++;
1223 desc = macb_rx_desc(queue, entry);
1225 if (!queue->rx_skbuff[entry]) {
1226 /* allocate sk_buff for this free entry in ring */
1227 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
1228 if (unlikely(!skb)) {
1230 "Unable to allocate sk_buff\n");
1234 /* now fill corresponding descriptor entry */
1235 paddr = dma_map_single(&bp->pdev->dev, skb->data,
1238 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1243 queue->rx_skbuff[entry] = skb;
1245 if (entry == bp->rx_ring_size - 1)
1246 paddr |= MACB_BIT(RX_WRAP);
1248 /* Setting addr clears RX_USED and allows reception,
1249 * make sure ctrl is cleared first to avoid a race.
1252 macb_set_addr(bp, desc, paddr);
1254 /* properly align Ethernet header */
1255 skb_reserve(skb, NET_IP_ALIGN);
1259 desc->addr &= ~MACB_BIT(RX_USED);
1263 /* Make descriptor updates visible to hardware */
1266 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1267 queue, queue->rx_prepared_head, queue->rx_tail);
1270 /* Mark DMA descriptors from begin up to and not including end as unused */
1271 static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
1276 for (frag = begin; frag != end; frag++) {
1277 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
1279 desc->addr &= ~MACB_BIT(RX_USED);
1282 /* Make descriptor updates visible to hardware */
1285 /* When this happens, the hardware stats registers for
1286 * whatever caused this is updated, so we don't have to record
1291 static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1294 struct macb *bp = queue->bp;
1297 struct sk_buff *skb;
1298 struct macb_dma_desc *desc;
1301 while (count < budget) {
1306 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1307 desc = macb_rx_desc(queue, entry);
1309 /* Make hw descriptor updates visible to CPU */
1312 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
1313 addr = macb_get_addr(bp, desc);
1318 /* Ensure ctrl is at least as up-to-date as rxused */
1326 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1328 "not whole frame pointed by descriptor\n");
1329 bp->dev->stats.rx_dropped++;
1330 queue->stats.rx_dropped++;
1333 skb = queue->rx_skbuff[entry];
1334 if (unlikely(!skb)) {
1336 "inconsistent Rx descriptor chain\n");
1337 bp->dev->stats.rx_dropped++;
1338 queue->stats.rx_dropped++;
1341 /* now everything is ready for receiving packet */
1342 queue->rx_skbuff[entry] = NULL;
1343 len = ctrl & bp->rx_frm_len_mask;
1345 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1348 dma_unmap_single(&bp->pdev->dev, addr,
1349 bp->rx_buffer_size, DMA_FROM_DEVICE);
1351 skb->protocol = eth_type_trans(skb, bp->dev);
1352 skb_checksum_none_assert(skb);
1353 if (bp->dev->features & NETIF_F_RXCSUM &&
1354 !(bp->dev->flags & IFF_PROMISC) &&
1355 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1356 skb->ip_summed = CHECKSUM_UNNECESSARY;
1358 bp->dev->stats.rx_packets++;
1359 queue->stats.rx_packets++;
1360 bp->dev->stats.rx_bytes += skb->len;
1361 queue->stats.rx_bytes += skb->len;
1363 gem_ptp_do_rxstamp(bp, skb, desc);
1365 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1366 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1367 skb->len, skb->csum);
1368 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
1369 skb_mac_header(skb), 16, true);
1370 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1371 skb->data, 32, true);
1374 napi_gro_receive(napi, skb);
1377 gem_rx_refill(queue);
1382 static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1383 unsigned int first_frag, unsigned int last_frag)
1387 unsigned int offset;
1388 struct sk_buff *skb;
1389 struct macb_dma_desc *desc;
1390 struct macb *bp = queue->bp;
1392 desc = macb_rx_desc(queue, last_frag);
1393 len = desc->ctrl & bp->rx_frm_len_mask;
1395 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
1396 macb_rx_ring_wrap(bp, first_frag),
1397 macb_rx_ring_wrap(bp, last_frag), len);
1399 /* The ethernet header starts NET_IP_ALIGN bytes into the
1400 * first buffer. Since the header is 14 bytes, this makes the
1401 * payload word-aligned.
1403 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1404 * the two padding bytes into the skb so that we avoid hitting
1405 * the slowpath in memcpy(), and pull them off afterwards.
1407 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
1409 bp->dev->stats.rx_dropped++;
1410 for (frag = first_frag; ; frag++) {
1411 desc = macb_rx_desc(queue, frag);
1412 desc->addr &= ~MACB_BIT(RX_USED);
1413 if (frag == last_frag)
1417 /* Make descriptor updates visible to hardware */
1424 len += NET_IP_ALIGN;
1425 skb_checksum_none_assert(skb);
1428 for (frag = first_frag; ; frag++) {
1429 unsigned int frag_len = bp->rx_buffer_size;
1431 if (offset + frag_len > len) {
1432 if (unlikely(frag != last_frag)) {
1433 dev_kfree_skb_any(skb);
1436 frag_len = len - offset;
1438 skb_copy_to_linear_data_offset(skb, offset,
1439 macb_rx_buffer(queue, frag),
1441 offset += bp->rx_buffer_size;
1442 desc = macb_rx_desc(queue, frag);
1443 desc->addr &= ~MACB_BIT(RX_USED);
1445 if (frag == last_frag)
1449 /* Make descriptor updates visible to hardware */
1452 __skb_pull(skb, NET_IP_ALIGN);
1453 skb->protocol = eth_type_trans(skb, bp->dev);
1455 bp->dev->stats.rx_packets++;
1456 bp->dev->stats.rx_bytes += skb->len;
1457 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1458 skb->len, skb->csum);
1459 napi_gro_receive(napi, skb);
1464 static inline void macb_init_rx_ring(struct macb_queue *queue)
1466 struct macb *bp = queue->bp;
1468 struct macb_dma_desc *desc = NULL;
1471 addr = queue->rx_buffers_dma;
1472 for (i = 0; i < bp->rx_ring_size; i++) {
1473 desc = macb_rx_desc(queue, i);
1474 macb_set_addr(bp, desc, addr);
1476 addr += bp->rx_buffer_size;
1478 desc->addr |= MACB_BIT(RX_WRAP);
1482 static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1485 struct macb *bp = queue->bp;
1486 bool reset_rx_queue = false;
1489 int first_frag = -1;
1491 for (tail = queue->rx_tail; budget > 0; tail++) {
1492 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
1495 /* Make hw descriptor updates visible to CPU */
1498 if (!(desc->addr & MACB_BIT(RX_USED)))
1501 /* Ensure ctrl is at least as up-to-date as addr */
1506 if (ctrl & MACB_BIT(RX_SOF)) {
1507 if (first_frag != -1)
1508 discard_partial_frame(queue, first_frag, tail);
1512 if (ctrl & MACB_BIT(RX_EOF)) {
1515 if (unlikely(first_frag == -1)) {
1516 reset_rx_queue = true;
1520 dropped = macb_rx_frame(queue, napi, first_frag, tail);
1522 if (unlikely(dropped < 0)) {
1523 reset_rx_queue = true;
1533 if (unlikely(reset_rx_queue)) {
1534 unsigned long flags;
1537 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1539 spin_lock_irqsave(&bp->lock, flags);
1541 ctrl = macb_readl(bp, NCR);
1542 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1544 macb_init_rx_ring(queue);
1545 queue_writel(queue, RBQP, queue->rx_ring_dma);
1547 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1549 spin_unlock_irqrestore(&bp->lock, flags);
1553 if (first_frag != -1)
1554 queue->rx_tail = first_frag;
1556 queue->rx_tail = tail;
1561 static int macb_poll(struct napi_struct *napi, int budget)
1563 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1564 struct macb *bp = queue->bp;
1568 status = macb_readl(bp, RSR);
1569 macb_writel(bp, RSR, status);
1571 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
1572 (unsigned long)status, budget);
1574 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
1575 if (work_done < budget) {
1576 napi_complete_done(napi, work_done);
1578 /* RSR bits only seem to propagate to raise interrupts when
1579 * interrupts are enabled at the time, so if bits are already
1580 * set due to packets received while interrupts were disabled,
1581 * they will not cause another interrupt to be generated when
1582 * interrupts are re-enabled.
1583 * Check for this case here. This has been seen to happen
1584 * around 30% of the time under heavy network load.
1586 status = macb_readl(bp, RSR);
1588 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1589 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1590 napi_reschedule(napi);
1592 queue_writel(queue, IER, bp->rx_intr_mask);
1594 /* In rare cases, packets could have been received in
1595 * the window between the check above and re-enabling
1596 * interrupts. Therefore, a double-check is required
1597 * to avoid losing a wakeup. This can potentially race
1598 * with the interrupt handler doing the same actions
1599 * if an interrupt is raised just after enabling them,
1600 * but this should be harmless.
1602 status = macb_readl(bp, RSR);
1603 if (unlikely(status)) {
1604 queue_writel(queue, IDR, bp->rx_intr_mask);
1605 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1606 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1607 napi_schedule(napi);
1612 /* TODO: Handle errors */
1617 static void macb_hresp_error_task(struct tasklet_struct *t)
1619 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
1620 struct net_device *dev = bp->dev;
1621 struct macb_queue *queue;
1625 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1626 queue_writel(queue, IDR, bp->rx_intr_mask |
1630 ctrl = macb_readl(bp, NCR);
1631 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1632 macb_writel(bp, NCR, ctrl);
1634 netif_tx_stop_all_queues(dev);
1635 netif_carrier_off(dev);
1637 bp->macbgem_ops.mog_init_rings(bp);
1639 /* Initialize TX and RX buffers */
1640 macb_init_buffers(bp);
1642 /* Enable interrupts */
1643 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1644 queue_writel(queue, IER,
1649 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1650 macb_writel(bp, NCR, ctrl);
1652 netif_carrier_on(dev);
1653 netif_tx_start_all_queues(dev);
1656 static void macb_tx_restart(struct macb_queue *queue)
1658 unsigned int head = queue->tx_head;
1659 unsigned int tail = queue->tx_tail;
1660 struct macb *bp = queue->bp;
1662 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1663 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1668 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1671 static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1673 struct macb_queue *queue = dev_id;
1674 struct macb *bp = queue->bp;
1677 status = queue_readl(queue, ISR);
1679 if (unlikely(!status))
1682 spin_lock(&bp->lock);
1684 if (status & MACB_BIT(WOL)) {
1685 queue_writel(queue, IDR, MACB_BIT(WOL));
1686 macb_writel(bp, WOL, 0);
1687 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1688 (unsigned int)(queue - bp->queues),
1689 (unsigned long)status);
1690 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1691 queue_writel(queue, ISR, MACB_BIT(WOL));
1692 pm_wakeup_event(&bp->pdev->dev, 0);
1695 spin_unlock(&bp->lock);
1700 static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1702 struct macb_queue *queue = dev_id;
1703 struct macb *bp = queue->bp;
1706 status = queue_readl(queue, ISR);
1708 if (unlikely(!status))
1711 spin_lock(&bp->lock);
1713 if (status & GEM_BIT(WOL)) {
1714 queue_writel(queue, IDR, GEM_BIT(WOL));
1715 gem_writel(bp, WOL, 0);
1716 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1717 (unsigned int)(queue - bp->queues),
1718 (unsigned long)status);
1719 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1720 queue_writel(queue, ISR, GEM_BIT(WOL));
1721 pm_wakeup_event(&bp->pdev->dev, 0);
1724 spin_unlock(&bp->lock);
1729 static irqreturn_t macb_interrupt(int irq, void *dev_id)
1731 struct macb_queue *queue = dev_id;
1732 struct macb *bp = queue->bp;
1733 struct net_device *dev = bp->dev;
1736 status = queue_readl(queue, ISR);
1738 if (unlikely(!status))
1741 spin_lock(&bp->lock);
1744 /* close possible race with dev_close */
1745 if (unlikely(!netif_running(dev))) {
1746 queue_writel(queue, IDR, -1);
1747 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1748 queue_writel(queue, ISR, -1);
1752 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1753 (unsigned int)(queue - bp->queues),
1754 (unsigned long)status);
1756 if (status & bp->rx_intr_mask) {
1757 /* There's no point taking any more interrupts
1758 * until we have processed the buffers. The
1759 * scheduling call may fail if the poll routine
1760 * is already scheduled, so disable interrupts
1763 queue_writel(queue, IDR, bp->rx_intr_mask);
1764 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1765 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1767 if (napi_schedule_prep(&queue->napi)) {
1768 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1769 __napi_schedule(&queue->napi);
1773 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1774 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1775 schedule_work(&queue->tx_error_task);
1777 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1778 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1783 if (status & MACB_BIT(TCOMP))
1784 macb_tx_interrupt(queue);
1786 if (status & MACB_BIT(TXUBR))
1787 macb_tx_restart(queue);
1789 /* Link change detection isn't possible with RMII, so we'll
1790 * add that if/when we get our hands on a full-blown MII PHY.
1793 /* There is a hardware issue under heavy load where DMA can
1794 * stop, this causes endless "used buffer descriptor read"
1795 * interrupts but it can be cleared by re-enabling RX. See
1796 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1797 * section 16.7.4 for details. RXUBR is only enabled for
1798 * these two versions.
1800 if (status & MACB_BIT(RXUBR)) {
1801 ctrl = macb_readl(bp, NCR);
1802 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1804 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1806 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1807 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1810 if (status & MACB_BIT(ISR_ROVR)) {
1811 /* We missed at least one packet */
1812 if (macb_is_gem(bp))
1813 bp->hw_stats.gem.rx_overruns++;
1815 bp->hw_stats.macb.rx_overruns++;
1817 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1818 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1821 if (status & MACB_BIT(HRESP)) {
1822 tasklet_schedule(&bp->hresp_err_tasklet);
1823 netdev_err(dev, "DMA bus error: HRESP not OK\n");
1825 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1826 queue_writel(queue, ISR, MACB_BIT(HRESP));
1828 status = queue_readl(queue, ISR);
1831 spin_unlock(&bp->lock);
1836 #ifdef CONFIG_NET_POLL_CONTROLLER
1837 /* Polling receive - used by netconsole and other diagnostic tools
1838 * to allow network i/o with interrupts disabled.
1840 static void macb_poll_controller(struct net_device *dev)
1842 struct macb *bp = netdev_priv(dev);
1843 struct macb_queue *queue;
1844 unsigned long flags;
1847 local_irq_save(flags);
1848 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1849 macb_interrupt(dev->irq, queue);
1850 local_irq_restore(flags);
1854 static unsigned int macb_tx_map(struct macb *bp,
1855 struct macb_queue *queue,
1856 struct sk_buff *skb,
1857 unsigned int hdrlen)
1860 unsigned int len, entry, i, tx_head = queue->tx_head;
1861 struct macb_tx_skb *tx_skb = NULL;
1862 struct macb_dma_desc *desc;
1863 unsigned int offset, size, count = 0;
1864 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1865 unsigned int eof = 1, mss_mfs = 0;
1866 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1869 if (skb_shinfo(skb)->gso_size != 0) {
1870 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1872 lso_ctrl = MACB_LSO_UFO_ENABLE;
1875 lso_ctrl = MACB_LSO_TSO_ENABLE;
1878 /* First, map non-paged data */
1879 len = skb_headlen(skb);
1881 /* first buffer length */
1886 entry = macb_tx_ring_wrap(bp, tx_head);
1887 tx_skb = &queue->tx_skb[entry];
1889 mapping = dma_map_single(&bp->pdev->dev,
1891 size, DMA_TO_DEVICE);
1892 if (dma_mapping_error(&bp->pdev->dev, mapping))
1895 /* Save info to properly release resources */
1897 tx_skb->mapping = mapping;
1898 tx_skb->size = size;
1899 tx_skb->mapped_as_page = false;
1906 size = min(len, bp->max_tx_length);
1909 /* Then, map paged data from fragments */
1910 for (f = 0; f < nr_frags; f++) {
1911 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1913 len = skb_frag_size(frag);
1916 size = min(len, bp->max_tx_length);
1917 entry = macb_tx_ring_wrap(bp, tx_head);
1918 tx_skb = &queue->tx_skb[entry];
1920 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1921 offset, size, DMA_TO_DEVICE);
1922 if (dma_mapping_error(&bp->pdev->dev, mapping))
1925 /* Save info to properly release resources */
1927 tx_skb->mapping = mapping;
1928 tx_skb->size = size;
1929 tx_skb->mapped_as_page = true;
1938 /* Should never happen */
1939 if (unlikely(!tx_skb)) {
1940 netdev_err(bp->dev, "BUG! empty skb!\n");
1944 /* This is the last buffer of the frame: save socket buffer */
1947 /* Update TX ring: update buffer descriptors in reverse order
1948 * to avoid race condition
1951 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1952 * to set the end of TX queue
1955 entry = macb_tx_ring_wrap(bp, i);
1956 ctrl = MACB_BIT(TX_USED);
1957 desc = macb_tx_desc(queue, entry);
1961 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1962 /* include header and FCS in value given to h/w */
1963 mss_mfs = skb_shinfo(skb)->gso_size +
1964 skb_transport_offset(skb) +
1967 mss_mfs = skb_shinfo(skb)->gso_size;
1968 /* TCP Sequence Number Source Select
1969 * can be set only for TSO
1977 entry = macb_tx_ring_wrap(bp, i);
1978 tx_skb = &queue->tx_skb[entry];
1979 desc = macb_tx_desc(queue, entry);
1981 ctrl = (u32)tx_skb->size;
1983 ctrl |= MACB_BIT(TX_LAST);
1986 if (unlikely(entry == (bp->tx_ring_size - 1)))
1987 ctrl |= MACB_BIT(TX_WRAP);
1989 /* First descriptor is header descriptor */
1990 if (i == queue->tx_head) {
1991 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1992 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1993 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1994 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1995 ctrl |= MACB_BIT(TX_NOCRC);
1997 /* Only set MSS/MFS on payload descriptors
1998 * (second or later descriptor)
2000 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
2002 /* Set TX buffer descriptor */
2003 macb_set_addr(bp, desc, tx_skb->mapping);
2004 /* desc->addr must be visible to hardware before clearing
2005 * 'TX_USED' bit in desc->ctrl.
2009 } while (i != queue->tx_head);
2011 queue->tx_head = tx_head;
2016 netdev_err(bp->dev, "TX DMA map failed\n");
2018 for (i = queue->tx_head; i != tx_head; i++) {
2019 tx_skb = macb_tx_skb(queue, i);
2021 macb_tx_unmap(bp, tx_skb);
2027 static netdev_features_t macb_features_check(struct sk_buff *skb,
2028 struct net_device *dev,
2029 netdev_features_t features)
2031 unsigned int nr_frags, f;
2032 unsigned int hdrlen;
2034 /* Validate LSO compatibility */
2036 /* there is only one buffer or protocol is not UDP */
2037 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
2040 /* length of header */
2041 hdrlen = skb_transport_offset(skb);
2044 * When software supplies two or more payload buffers all payload buffers
2045 * apart from the last must be a multiple of 8 bytes in size.
2047 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2048 return features & ~MACB_NETIF_LSO;
2050 nr_frags = skb_shinfo(skb)->nr_frags;
2051 /* No need to check last fragment */
2053 for (f = 0; f < nr_frags; f++) {
2054 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2056 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2057 return features & ~MACB_NETIF_LSO;
2062 static inline int macb_clear_csum(struct sk_buff *skb)
2064 /* no change for packets without checksum offloading */
2065 if (skb->ip_summed != CHECKSUM_PARTIAL)
2068 /* make sure we can modify the header */
2069 if (unlikely(skb_cow_head(skb, 0)))
2072 /* initialize checksum field
2073 * This is required - at least for Zynq, which otherwise calculates
2074 * wrong UDP header checksums for UDP packets with UDP data len <=2
2076 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2080 static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2082 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2083 skb_is_nonlinear(*skb);
2084 int padlen = ETH_ZLEN - (*skb)->len;
2085 int headroom = skb_headroom(*skb);
2086 int tailroom = skb_tailroom(*skb);
2087 struct sk_buff *nskb;
2090 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2091 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2092 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2096 /* FCS could be appeded to tailroom. */
2097 if (tailroom >= ETH_FCS_LEN)
2099 /* FCS could be appeded by moving data to headroom. */
2100 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2102 /* No room for FCS, need to reallocate skb. */
2104 padlen = ETH_FCS_LEN;
2106 /* Add room for FCS. */
2107 padlen += ETH_FCS_LEN;
2110 if (!cloned && headroom + tailroom >= padlen) {
2111 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2112 skb_set_tail_pointer(*skb, (*skb)->len);
2114 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2118 dev_consume_skb_any(*skb);
2122 if (padlen > ETH_FCS_LEN)
2123 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
2126 /* set FCS to packet */
2127 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2130 skb_put_u8(*skb, fcs & 0xff);
2131 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2132 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2133 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2138 static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
2140 u16 queue_index = skb_get_queue_mapping(skb);
2141 struct macb *bp = netdev_priv(dev);
2142 struct macb_queue *queue = &bp->queues[queue_index];
2143 unsigned long flags;
2144 unsigned int desc_cnt, nr_frags, frag_size, f;
2145 unsigned int hdrlen;
2147 netdev_tx_t ret = NETDEV_TX_OK;
2149 if (macb_clear_csum(skb)) {
2150 dev_kfree_skb_any(skb);
2154 if (macb_pad_and_fcs(&skb, dev)) {
2155 dev_kfree_skb_any(skb);
2159 is_lso = (skb_shinfo(skb)->gso_size != 0);
2162 /* length of headers */
2163 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
2164 /* only queue eth + ip headers separately for UDP */
2165 hdrlen = skb_transport_offset(skb);
2167 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2168 if (skb_headlen(skb) < hdrlen) {
2169 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2170 /* if this is required, would need to copy to single buffer */
2171 return NETDEV_TX_BUSY;
2174 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
2176 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
2177 netdev_vdbg(bp->dev,
2178 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2179 queue_index, skb->len, skb->head, skb->data,
2180 skb_tail_pointer(skb), skb_end_pointer(skb));
2181 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2182 skb->data, 16, true);
2185 /* Count how many TX buffer descriptors are needed to send this
2186 * socket buffer: skb fragments of jumbo frames may need to be
2187 * split into many buffer descriptors.
2189 if (is_lso && (skb_headlen(skb) > hdrlen))
2190 /* extra header descriptor if also payload in first buffer */
2191 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2193 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
2194 nr_frags = skb_shinfo(skb)->nr_frags;
2195 for (f = 0; f < nr_frags; f++) {
2196 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
2197 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
2200 spin_lock_irqsave(&bp->lock, flags);
2202 /* This is a hard error, log it. */
2203 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
2204 bp->tx_ring_size) < desc_cnt) {
2205 netif_stop_subqueue(dev, queue_index);
2206 spin_unlock_irqrestore(&bp->lock, flags);
2207 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
2208 queue->tx_head, queue->tx_tail);
2209 return NETDEV_TX_BUSY;
2212 /* Map socket buffer for DMA transfer */
2213 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
2214 dev_kfree_skb_any(skb);
2218 /* Make newly initialized descriptor visible to hardware */
2220 skb_tx_timestamp(skb);
2222 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2224 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
2225 netif_stop_subqueue(dev, queue_index);
2228 spin_unlock_irqrestore(&bp->lock, flags);
2233 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
2235 if (!macb_is_gem(bp)) {
2236 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2238 bp->rx_buffer_size = size;
2240 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
2242 "RX buffer must be multiple of %d bytes, expanding\n",
2243 RX_BUFFER_MULTIPLE);
2244 bp->rx_buffer_size =
2245 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
2249 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
2250 bp->dev->mtu, bp->rx_buffer_size);
2253 static void gem_free_rx_buffers(struct macb *bp)
2255 struct sk_buff *skb;
2256 struct macb_dma_desc *desc;
2257 struct macb_queue *queue;
2262 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2263 if (!queue->rx_skbuff)
2266 for (i = 0; i < bp->rx_ring_size; i++) {
2267 skb = queue->rx_skbuff[i];
2272 desc = macb_rx_desc(queue, i);
2273 addr = macb_get_addr(bp, desc);
2275 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2277 dev_kfree_skb_any(skb);
2281 kfree(queue->rx_skbuff);
2282 queue->rx_skbuff = NULL;
2286 static void macb_free_rx_buffers(struct macb *bp)
2288 struct macb_queue *queue = &bp->queues[0];
2290 if (queue->rx_buffers) {
2291 dma_free_coherent(&bp->pdev->dev,
2292 bp->rx_ring_size * bp->rx_buffer_size,
2293 queue->rx_buffers, queue->rx_buffers_dma);
2294 queue->rx_buffers = NULL;
2298 static void macb_free_consistent(struct macb *bp)
2300 struct macb_queue *queue;
2304 bp->macbgem_ops.mog_free_rx_buffers(bp);
2306 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2307 kfree(queue->tx_skb);
2308 queue->tx_skb = NULL;
2309 if (queue->tx_ring) {
2310 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2311 dma_free_coherent(&bp->pdev->dev, size,
2312 queue->tx_ring, queue->tx_ring_dma);
2313 queue->tx_ring = NULL;
2315 if (queue->rx_ring) {
2316 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2317 dma_free_coherent(&bp->pdev->dev, size,
2318 queue->rx_ring, queue->rx_ring_dma);
2319 queue->rx_ring = NULL;
2324 static int gem_alloc_rx_buffers(struct macb *bp)
2326 struct macb_queue *queue;
2330 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2331 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2332 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2333 if (!queue->rx_skbuff)
2337 "Allocated %d RX struct sk_buff entries at %p\n",
2338 bp->rx_ring_size, queue->rx_skbuff);
2343 static int macb_alloc_rx_buffers(struct macb *bp)
2345 struct macb_queue *queue = &bp->queues[0];
2348 size = bp->rx_ring_size * bp->rx_buffer_size;
2349 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2350 &queue->rx_buffers_dma, GFP_KERNEL);
2351 if (!queue->rx_buffers)
2355 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
2356 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
2360 static int macb_alloc_consistent(struct macb *bp)
2362 struct macb_queue *queue;
2366 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2367 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2368 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2369 &queue->tx_ring_dma,
2371 if (!queue->tx_ring)
2374 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2375 q, size, (unsigned long)queue->tx_ring_dma,
2378 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
2379 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2383 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2384 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2385 &queue->rx_ring_dma, GFP_KERNEL);
2386 if (!queue->rx_ring)
2389 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2390 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
2392 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2398 macb_free_consistent(bp);
2402 static void gem_init_rings(struct macb *bp)
2404 struct macb_queue *queue;
2405 struct macb_dma_desc *desc = NULL;
2409 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2410 for (i = 0; i < bp->tx_ring_size; i++) {
2411 desc = macb_tx_desc(queue, i);
2412 macb_set_addr(bp, desc, 0);
2413 desc->ctrl = MACB_BIT(TX_USED);
2415 desc->ctrl |= MACB_BIT(TX_WRAP);
2420 queue->rx_prepared_head = 0;
2422 gem_rx_refill(queue);
2427 static void macb_init_rings(struct macb *bp)
2430 struct macb_dma_desc *desc = NULL;
2432 macb_init_rx_ring(&bp->queues[0]);
2434 for (i = 0; i < bp->tx_ring_size; i++) {
2435 desc = macb_tx_desc(&bp->queues[0], i);
2436 macb_set_addr(bp, desc, 0);
2437 desc->ctrl = MACB_BIT(TX_USED);
2439 bp->queues[0].tx_head = 0;
2440 bp->queues[0].tx_tail = 0;
2441 desc->ctrl |= MACB_BIT(TX_WRAP);
2444 static void macb_reset_hw(struct macb *bp)
2446 struct macb_queue *queue;
2448 u32 ctrl = macb_readl(bp, NCR);
2450 /* Disable RX and TX (XXX: Should we halt the transmission
2453 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2455 /* Clear the stats registers (XXX: Update stats first?) */
2456 ctrl |= MACB_BIT(CLRSTAT);
2458 macb_writel(bp, NCR, ctrl);
2460 /* Clear all status flags */
2461 macb_writel(bp, TSR, -1);
2462 macb_writel(bp, RSR, -1);
2464 /* Disable all interrupts */
2465 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2466 queue_writel(queue, IDR, -1);
2467 queue_readl(queue, ISR);
2468 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2469 queue_writel(queue, ISR, -1);
2473 static u32 gem_mdc_clk_div(struct macb *bp)
2476 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2478 if (pclk_hz <= 20000000)
2479 config = GEM_BF(CLK, GEM_CLK_DIV8);
2480 else if (pclk_hz <= 40000000)
2481 config = GEM_BF(CLK, GEM_CLK_DIV16);
2482 else if (pclk_hz <= 80000000)
2483 config = GEM_BF(CLK, GEM_CLK_DIV32);
2484 else if (pclk_hz <= 120000000)
2485 config = GEM_BF(CLK, GEM_CLK_DIV48);
2486 else if (pclk_hz <= 160000000)
2487 config = GEM_BF(CLK, GEM_CLK_DIV64);
2489 config = GEM_BF(CLK, GEM_CLK_DIV96);
2494 static u32 macb_mdc_clk_div(struct macb *bp)
2497 unsigned long pclk_hz;
2499 if (macb_is_gem(bp))
2500 return gem_mdc_clk_div(bp);
2502 pclk_hz = clk_get_rate(bp->pclk);
2503 if (pclk_hz <= 20000000)
2504 config = MACB_BF(CLK, MACB_CLK_DIV8);
2505 else if (pclk_hz <= 40000000)
2506 config = MACB_BF(CLK, MACB_CLK_DIV16);
2507 else if (pclk_hz <= 80000000)
2508 config = MACB_BF(CLK, MACB_CLK_DIV32);
2510 config = MACB_BF(CLK, MACB_CLK_DIV64);
2515 /* Get the DMA bus width field of the network configuration register that we
2516 * should program. We find the width from decoding the design configuration
2517 * register to find the maximum supported data bus width.
2519 static u32 macb_dbw(struct macb *bp)
2521 if (!macb_is_gem(bp))
2524 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2526 return GEM_BF(DBW, GEM_DBW128);
2528 return GEM_BF(DBW, GEM_DBW64);
2531 return GEM_BF(DBW, GEM_DBW32);
2535 /* Configure the receive DMA engine
2536 * - use the correct receive buffer size
2537 * - set best burst length for DMA operations
2538 * (if not supported by FIFO, it will fallback to default)
2539 * - set both rx/tx packet buffers to full memory size
2540 * These are configurable parameters for GEM.
2542 static void macb_configure_dma(struct macb *bp)
2544 struct macb_queue *queue;
2549 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
2550 if (macb_is_gem(bp)) {
2551 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2552 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2554 queue_writel(queue, RBQS, buffer_size);
2556 dmacfg |= GEM_BF(RXBS, buffer_size);
2558 if (bp->dma_burst_length)
2559 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2560 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2561 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2564 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2566 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2568 if (bp->dev->features & NETIF_F_HW_CSUM)
2569 dmacfg |= GEM_BIT(TXCOEN);
2571 dmacfg &= ~GEM_BIT(TXCOEN);
2573 dmacfg &= ~GEM_BIT(ADDR64);
2574 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2575 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2576 dmacfg |= GEM_BIT(ADDR64);
2578 #ifdef CONFIG_MACB_USE_HWSTAMP
2579 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2580 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2582 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2584 gem_writel(bp, DMACFG, dmacfg);
2588 static void macb_init_hw(struct macb *bp)
2593 macb_set_hwaddr(bp);
2595 config = macb_mdc_clk_div(bp);
2596 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
2597 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
2598 if (bp->caps & MACB_CAPS_JUMBO)
2599 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2601 config |= MACB_BIT(BIG); /* Receive oversized frames */
2602 if (bp->dev->flags & IFF_PROMISC)
2603 config |= MACB_BIT(CAF); /* Copy All Frames */
2604 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2605 config |= GEM_BIT(RXCOEN);
2606 if (!(bp->dev->flags & IFF_BROADCAST))
2607 config |= MACB_BIT(NBC); /* No BroadCast */
2608 config |= macb_dbw(bp);
2609 macb_writel(bp, NCFGR, config);
2610 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2611 gem_writel(bp, JML, bp->jumbo_max_len);
2612 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
2613 if (bp->caps & MACB_CAPS_JUMBO)
2614 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
2616 macb_configure_dma(bp);
2619 /* The hash address register is 64 bits long and takes up two
2620 * locations in the memory map. The least significant bits are stored
2621 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2623 * The unicast hash enable and the multicast hash enable bits in the
2624 * network configuration register enable the reception of hash matched
2625 * frames. The destination address is reduced to a 6 bit index into
2626 * the 64 bit hash register using the following hash function. The
2627 * hash function is an exclusive or of every sixth bit of the
2628 * destination address.
2630 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2631 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2632 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2633 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2634 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2635 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2637 * da[0] represents the least significant bit of the first byte
2638 * received, that is, the multicast/unicast indicator, and da[47]
2639 * represents the most significant bit of the last byte received. If
2640 * the hash index, hi[n], points to a bit that is set in the hash
2641 * register then the frame will be matched according to whether the
2642 * frame is multicast or unicast. A multicast match will be signalled
2643 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2644 * index points to a bit set in the hash register. A unicast match
2645 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2646 * and the hash index points to a bit set in the hash register. To
2647 * receive all multicast frames, the hash register should be set with
2648 * all ones and the multicast hash enable bit should be set in the
2649 * network configuration register.
2652 static inline int hash_bit_value(int bitnr, __u8 *addr)
2654 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2659 /* Return the hash index value for the specified address. */
2660 static int hash_get_index(__u8 *addr)
2665 for (j = 0; j < 6; j++) {
2666 for (i = 0, bitval = 0; i < 8; i++)
2667 bitval ^= hash_bit_value(i * 6 + j, addr);
2669 hash_index |= (bitval << j);
2675 /* Add multicast addresses to the internal multicast-hash table. */
2676 static void macb_sethashtable(struct net_device *dev)
2678 struct netdev_hw_addr *ha;
2679 unsigned long mc_filter[2];
2681 struct macb *bp = netdev_priv(dev);
2686 netdev_for_each_mc_addr(ha, dev) {
2687 bitnr = hash_get_index(ha->addr);
2688 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2691 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2692 macb_or_gem_writel(bp, HRT, mc_filter[1]);
2695 /* Enable/Disable promiscuous and multicast modes. */
2696 static void macb_set_rx_mode(struct net_device *dev)
2699 struct macb *bp = netdev_priv(dev);
2701 cfg = macb_readl(bp, NCFGR);
2703 if (dev->flags & IFF_PROMISC) {
2704 /* Enable promiscuous mode */
2705 cfg |= MACB_BIT(CAF);
2707 /* Disable RX checksum offload */
2708 if (macb_is_gem(bp))
2709 cfg &= ~GEM_BIT(RXCOEN);
2711 /* Disable promiscuous mode */
2712 cfg &= ~MACB_BIT(CAF);
2714 /* Enable RX checksum offload only if requested */
2715 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2716 cfg |= GEM_BIT(RXCOEN);
2719 if (dev->flags & IFF_ALLMULTI) {
2720 /* Enable all multicast mode */
2721 macb_or_gem_writel(bp, HRB, -1);
2722 macb_or_gem_writel(bp, HRT, -1);
2723 cfg |= MACB_BIT(NCFGR_MTI);
2724 } else if (!netdev_mc_empty(dev)) {
2725 /* Enable specific multicasts */
2726 macb_sethashtable(dev);
2727 cfg |= MACB_BIT(NCFGR_MTI);
2728 } else if (dev->flags & (~IFF_ALLMULTI)) {
2729 /* Disable all multicast mode */
2730 macb_or_gem_writel(bp, HRB, 0);
2731 macb_or_gem_writel(bp, HRT, 0);
2732 cfg &= ~MACB_BIT(NCFGR_MTI);
2735 macb_writel(bp, NCFGR, cfg);
2738 static int macb_open(struct net_device *dev)
2740 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
2741 struct macb *bp = netdev_priv(dev);
2742 struct macb_queue *queue;
2746 netdev_dbg(bp->dev, "open\n");
2748 err = pm_runtime_get_sync(&bp->pdev->dev);
2752 /* RX buffers initialization */
2753 macb_init_rx_buffer_size(bp, bufsz);
2755 err = macb_alloc_consistent(bp);
2757 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2762 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2763 napi_enable(&queue->napi);
2767 err = phy_power_on(bp->sgmii_phy);
2771 err = macb_phylink_connect(bp);
2775 netif_tx_start_all_queues(dev);
2778 bp->ptp_info->ptp_init(dev);
2783 phy_power_off(bp->sgmii_phy);
2787 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2788 napi_disable(&queue->napi);
2789 macb_free_consistent(bp);
2791 pm_runtime_put_sync(&bp->pdev->dev);
2795 static int macb_close(struct net_device *dev)
2797 struct macb *bp = netdev_priv(dev);
2798 struct macb_queue *queue;
2799 unsigned long flags;
2802 netif_tx_stop_all_queues(dev);
2804 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2805 napi_disable(&queue->napi);
2807 phylink_stop(bp->phylink);
2808 phylink_disconnect_phy(bp->phylink);
2810 phy_power_off(bp->sgmii_phy);
2812 spin_lock_irqsave(&bp->lock, flags);
2814 netif_carrier_off(dev);
2815 spin_unlock_irqrestore(&bp->lock, flags);
2817 macb_free_consistent(bp);
2820 bp->ptp_info->ptp_remove(dev);
2822 pm_runtime_put(&bp->pdev->dev);
2827 static int macb_change_mtu(struct net_device *dev, int new_mtu)
2829 if (netif_running(dev))
2837 static void gem_update_stats(struct macb *bp)
2839 struct macb_queue *queue;
2840 unsigned int i, q, idx;
2841 unsigned long *stat;
2843 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2845 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2846 u32 offset = gem_statistics[i].offset;
2847 u64 val = bp->macb_reg_readl(bp, offset);
2849 bp->ethtool_stats[i] += val;
2852 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2853 /* Add GEM_OCTTXH, GEM_OCTRXH */
2854 val = bp->macb_reg_readl(bp, offset + 4);
2855 bp->ethtool_stats[i] += ((u64)val) << 32;
2860 idx = GEM_STATS_LEN;
2861 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2862 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2863 bp->ethtool_stats[idx++] = *stat;
2866 static struct net_device_stats *gem_get_stats(struct macb *bp)
2868 struct gem_stats *hwstat = &bp->hw_stats.gem;
2869 struct net_device_stats *nstat = &bp->dev->stats;
2871 if (!netif_running(bp->dev))
2874 gem_update_stats(bp);
2876 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2877 hwstat->rx_alignment_errors +
2878 hwstat->rx_resource_errors +
2879 hwstat->rx_overruns +
2880 hwstat->rx_oversize_frames +
2881 hwstat->rx_jabbers +
2882 hwstat->rx_undersized_frames +
2883 hwstat->rx_length_field_frame_errors);
2884 nstat->tx_errors = (hwstat->tx_late_collisions +
2885 hwstat->tx_excessive_collisions +
2886 hwstat->tx_underrun +
2887 hwstat->tx_carrier_sense_errors);
2888 nstat->multicast = hwstat->rx_multicast_frames;
2889 nstat->collisions = (hwstat->tx_single_collision_frames +
2890 hwstat->tx_multiple_collision_frames +
2891 hwstat->tx_excessive_collisions);
2892 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2893 hwstat->rx_jabbers +
2894 hwstat->rx_undersized_frames +
2895 hwstat->rx_length_field_frame_errors);
2896 nstat->rx_over_errors = hwstat->rx_resource_errors;
2897 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2898 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2899 nstat->rx_fifo_errors = hwstat->rx_overruns;
2900 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2901 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2902 nstat->tx_fifo_errors = hwstat->tx_underrun;
2907 static void gem_get_ethtool_stats(struct net_device *dev,
2908 struct ethtool_stats *stats, u64 *data)
2912 bp = netdev_priv(dev);
2913 gem_update_stats(bp);
2914 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2915 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
2918 static int gem_get_sset_count(struct net_device *dev, int sset)
2920 struct macb *bp = netdev_priv(dev);
2924 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
2930 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2932 char stat_string[ETH_GSTRING_LEN];
2933 struct macb *bp = netdev_priv(dev);
2934 struct macb_queue *queue;
2940 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2941 memcpy(p, gem_statistics[i].stat_string,
2944 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2945 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2946 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2947 q, queue_statistics[i].stat_string);
2948 memcpy(p, stat_string, ETH_GSTRING_LEN);
2955 static struct net_device_stats *macb_get_stats(struct net_device *dev)
2957 struct macb *bp = netdev_priv(dev);
2958 struct net_device_stats *nstat = &bp->dev->stats;
2959 struct macb_stats *hwstat = &bp->hw_stats.macb;
2961 if (macb_is_gem(bp))
2962 return gem_get_stats(bp);
2964 /* read stats from hardware */
2965 macb_update_stats(bp);
2967 /* Convert HW stats into netdevice stats */
2968 nstat->rx_errors = (hwstat->rx_fcs_errors +
2969 hwstat->rx_align_errors +
2970 hwstat->rx_resource_errors +
2971 hwstat->rx_overruns +
2972 hwstat->rx_oversize_pkts +
2973 hwstat->rx_jabbers +
2974 hwstat->rx_undersize_pkts +
2975 hwstat->rx_length_mismatch);
2976 nstat->tx_errors = (hwstat->tx_late_cols +
2977 hwstat->tx_excessive_cols +
2978 hwstat->tx_underruns +
2979 hwstat->tx_carrier_errors +
2980 hwstat->sqe_test_errors);
2981 nstat->collisions = (hwstat->tx_single_cols +
2982 hwstat->tx_multiple_cols +
2983 hwstat->tx_excessive_cols);
2984 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2985 hwstat->rx_jabbers +
2986 hwstat->rx_undersize_pkts +
2987 hwstat->rx_length_mismatch);
2988 nstat->rx_over_errors = hwstat->rx_resource_errors +
2989 hwstat->rx_overruns;
2990 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2991 nstat->rx_frame_errors = hwstat->rx_align_errors;
2992 nstat->rx_fifo_errors = hwstat->rx_overruns;
2993 /* XXX: What does "missed" mean? */
2994 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2995 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2996 nstat->tx_fifo_errors = hwstat->tx_underruns;
2997 /* Don't know about heartbeat or window errors... */
3002 static int macb_get_regs_len(struct net_device *netdev)
3004 return MACB_GREGS_NBR * sizeof(u32);
3007 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
3010 struct macb *bp = netdev_priv(dev);
3011 unsigned int tail, head;
3014 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
3015 | MACB_GREGS_VERSION;
3017 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
3018 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
3020 regs_buff[0] = macb_readl(bp, NCR);
3021 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
3022 regs_buff[2] = macb_readl(bp, NSR);
3023 regs_buff[3] = macb_readl(bp, TSR);
3024 regs_buff[4] = macb_readl(bp, RBQP);
3025 regs_buff[5] = macb_readl(bp, TBQP);
3026 regs_buff[6] = macb_readl(bp, RSR);
3027 regs_buff[7] = macb_readl(bp, IMR);
3029 regs_buff[8] = tail;
3030 regs_buff[9] = head;
3031 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
3032 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
3034 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3035 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
3036 if (macb_is_gem(bp))
3037 regs_buff[13] = gem_readl(bp, DMACFG);
3040 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3042 struct macb *bp = netdev_priv(netdev);
3044 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
3045 phylink_ethtool_get_wol(bp->phylink, wol);
3046 wol->supported |= WAKE_MAGIC;
3048 if (bp->wol & MACB_WOL_ENABLED)
3049 wol->wolopts |= WAKE_MAGIC;
3053 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3055 struct macb *bp = netdev_priv(netdev);
3058 /* Pass the order to phylink layer */
3059 ret = phylink_ethtool_set_wol(bp->phylink, wol);
3060 /* Don't manage WoL on MAC if handled by the PHY
3061 * or if there's a failure in talking to the PHY
3063 if (!ret || ret != -EOPNOTSUPP)
3066 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3067 (wol->wolopts & ~WAKE_MAGIC))
3070 if (wol->wolopts & WAKE_MAGIC)
3071 bp->wol |= MACB_WOL_ENABLED;
3073 bp->wol &= ~MACB_WOL_ENABLED;
3075 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3080 static int macb_get_link_ksettings(struct net_device *netdev,
3081 struct ethtool_link_ksettings *kset)
3083 struct macb *bp = netdev_priv(netdev);
3085 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3088 static int macb_set_link_ksettings(struct net_device *netdev,
3089 const struct ethtool_link_ksettings *kset)
3091 struct macb *bp = netdev_priv(netdev);
3093 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3096 static void macb_get_ringparam(struct net_device *netdev,
3097 struct ethtool_ringparam *ring,
3098 struct kernel_ethtool_ringparam *kernel_ring,
3099 struct netlink_ext_ack *extack)
3101 struct macb *bp = netdev_priv(netdev);
3103 ring->rx_max_pending = MAX_RX_RING_SIZE;
3104 ring->tx_max_pending = MAX_TX_RING_SIZE;
3106 ring->rx_pending = bp->rx_ring_size;
3107 ring->tx_pending = bp->tx_ring_size;
3110 static int macb_set_ringparam(struct net_device *netdev,
3111 struct ethtool_ringparam *ring,
3112 struct kernel_ethtool_ringparam *kernel_ring,
3113 struct netlink_ext_ack *extack)
3115 struct macb *bp = netdev_priv(netdev);
3116 u32 new_rx_size, new_tx_size;
3117 unsigned int reset = 0;
3119 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3122 new_rx_size = clamp_t(u32, ring->rx_pending,
3123 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3124 new_rx_size = roundup_pow_of_two(new_rx_size);
3126 new_tx_size = clamp_t(u32, ring->tx_pending,
3127 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3128 new_tx_size = roundup_pow_of_two(new_tx_size);
3130 if ((new_tx_size == bp->tx_ring_size) &&
3131 (new_rx_size == bp->rx_ring_size)) {
3136 if (netif_running(bp->dev)) {
3138 macb_close(bp->dev);
3141 bp->rx_ring_size = new_rx_size;
3142 bp->tx_ring_size = new_tx_size;
3150 #ifdef CONFIG_MACB_USE_HWSTAMP
3151 static unsigned int gem_get_tsu_rate(struct macb *bp)
3153 struct clk *tsu_clk;
3154 unsigned int tsu_rate;
3156 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3157 if (!IS_ERR(tsu_clk))
3158 tsu_rate = clk_get_rate(tsu_clk);
3159 /* try pclk instead */
3160 else if (!IS_ERR(bp->pclk)) {
3162 tsu_rate = clk_get_rate(tsu_clk);
3168 static s32 gem_get_ptp_max_adj(void)
3173 static int gem_get_ts_info(struct net_device *dev,
3174 struct ethtool_ts_info *info)
3176 struct macb *bp = netdev_priv(dev);
3178 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3179 ethtool_op_get_ts_info(dev, info);
3183 info->so_timestamping =
3184 SOF_TIMESTAMPING_TX_SOFTWARE |
3185 SOF_TIMESTAMPING_RX_SOFTWARE |
3186 SOF_TIMESTAMPING_SOFTWARE |
3187 SOF_TIMESTAMPING_TX_HARDWARE |
3188 SOF_TIMESTAMPING_RX_HARDWARE |
3189 SOF_TIMESTAMPING_RAW_HARDWARE;
3191 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3192 (1 << HWTSTAMP_TX_OFF) |
3193 (1 << HWTSTAMP_TX_ON);
3195 (1 << HWTSTAMP_FILTER_NONE) |
3196 (1 << HWTSTAMP_FILTER_ALL);
3198 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3203 static struct macb_ptp_info gem_ptp_info = {
3204 .ptp_init = gem_ptp_init,
3205 .ptp_remove = gem_ptp_remove,
3206 .get_ptp_max_adj = gem_get_ptp_max_adj,
3207 .get_tsu_rate = gem_get_tsu_rate,
3208 .get_ts_info = gem_get_ts_info,
3209 .get_hwtst = gem_get_hwtst,
3210 .set_hwtst = gem_set_hwtst,
3214 static int macb_get_ts_info(struct net_device *netdev,
3215 struct ethtool_ts_info *info)
3217 struct macb *bp = netdev_priv(netdev);
3220 return bp->ptp_info->get_ts_info(netdev, info);
3222 return ethtool_op_get_ts_info(netdev, info);
3225 static void gem_enable_flow_filters(struct macb *bp, bool enable)
3227 struct net_device *netdev = bp->dev;
3228 struct ethtool_rx_fs_item *item;
3232 if (!(netdev->features & NETIF_F_NTUPLE))
3235 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3237 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3238 struct ethtool_rx_flow_spec *fs = &item->fs;
3239 struct ethtool_tcpip4_spec *tp4sp_m;
3241 if (fs->location >= num_t2_scr)
3244 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3246 /* enable/disable screener regs for the flow entry */
3247 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3249 /* only enable fields with no masking */
3250 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3252 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3253 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3255 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3257 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3258 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3260 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3262 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3263 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3265 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3267 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3271 static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3273 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3274 uint16_t index = fs->location;
3280 if (!macb_is_gem(bp))
3283 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3284 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3286 /* ignore field if any masking set */
3287 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3288 /* 1st compare reg - IP source address */
3291 w0 = tp4sp_v->ip4src;
3292 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3293 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3294 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3295 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3296 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3300 /* ignore field if any masking set */
3301 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3302 /* 2nd compare reg - IP destination address */
3305 w0 = tp4sp_v->ip4dst;
3306 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3307 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3308 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3309 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3310 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3314 /* ignore both port fields if masking set in both */
3315 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3316 /* 3rd compare reg - source port, destination port */
3319 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3320 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3321 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3322 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3323 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3324 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3326 /* only one port definition */
3327 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3328 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3329 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3330 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3331 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3332 } else { /* dst port */
3333 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3334 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3337 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3338 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3343 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3344 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3346 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3348 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3350 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3351 gem_writel_n(bp, SCRT2, index, t2_scr);
3354 static int gem_add_flow_filter(struct net_device *netdev,
3355 struct ethtool_rxnfc *cmd)
3357 struct macb *bp = netdev_priv(netdev);
3358 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3359 struct ethtool_rx_fs_item *item, *newfs;
3360 unsigned long flags;
3364 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
3367 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3370 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3371 fs->flow_type, (int)fs->ring_cookie, fs->location,
3372 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3373 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3374 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3376 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3378 /* find correct place to add in list */
3379 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3380 if (item->fs.location > newfs->fs.location) {
3381 list_add_tail(&newfs->list, &item->list);
3384 } else if (item->fs.location == fs->location) {
3385 netdev_err(netdev, "Rule not added: location %d not free!\n",
3392 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
3394 gem_prog_cmp_regs(bp, fs);
3395 bp->rx_fs_list.count++;
3396 /* enable filtering if NTUPLE on */
3397 gem_enable_flow_filters(bp, 1);
3399 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3403 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3408 static int gem_del_flow_filter(struct net_device *netdev,
3409 struct ethtool_rxnfc *cmd)
3411 struct macb *bp = netdev_priv(netdev);
3412 struct ethtool_rx_fs_item *item;
3413 struct ethtool_rx_flow_spec *fs;
3414 unsigned long flags;
3416 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3418 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3419 if (item->fs.location == cmd->fs.location) {
3420 /* disable screener regs for the flow entry */
3423 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3424 fs->flow_type, (int)fs->ring_cookie, fs->location,
3425 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3426 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3427 htons(fs->h_u.tcp_ip4_spec.psrc),
3428 htons(fs->h_u.tcp_ip4_spec.pdst));
3430 gem_writel_n(bp, SCRT2, fs->location, 0);
3432 list_del(&item->list);
3433 bp->rx_fs_list.count--;
3434 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3440 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3444 static int gem_get_flow_entry(struct net_device *netdev,
3445 struct ethtool_rxnfc *cmd)
3447 struct macb *bp = netdev_priv(netdev);
3448 struct ethtool_rx_fs_item *item;
3450 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3451 if (item->fs.location == cmd->fs.location) {
3452 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3459 static int gem_get_all_flow_entries(struct net_device *netdev,
3460 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3462 struct macb *bp = netdev_priv(netdev);
3463 struct ethtool_rx_fs_item *item;
3466 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3467 if (cnt == cmd->rule_cnt)
3469 rule_locs[cnt] = item->fs.location;
3472 cmd->data = bp->max_tuples;
3473 cmd->rule_cnt = cnt;
3478 static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3481 struct macb *bp = netdev_priv(netdev);
3485 case ETHTOOL_GRXRINGS:
3486 cmd->data = bp->num_queues;
3488 case ETHTOOL_GRXCLSRLCNT:
3489 cmd->rule_cnt = bp->rx_fs_list.count;
3491 case ETHTOOL_GRXCLSRULE:
3492 ret = gem_get_flow_entry(netdev, cmd);
3494 case ETHTOOL_GRXCLSRLALL:
3495 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3499 "Command parameter %d is not supported\n", cmd->cmd);
3506 static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3508 struct macb *bp = netdev_priv(netdev);
3512 case ETHTOOL_SRXCLSRLINS:
3513 if ((cmd->fs.location >= bp->max_tuples)
3514 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3518 ret = gem_add_flow_filter(netdev, cmd);
3520 case ETHTOOL_SRXCLSRLDEL:
3521 ret = gem_del_flow_filter(netdev, cmd);
3525 "Command parameter %d is not supported\n", cmd->cmd);
3532 static const struct ethtool_ops macb_ethtool_ops = {
3533 .get_regs_len = macb_get_regs_len,
3534 .get_regs = macb_get_regs,
3535 .get_link = ethtool_op_get_link,
3536 .get_ts_info = ethtool_op_get_ts_info,
3537 .get_wol = macb_get_wol,
3538 .set_wol = macb_set_wol,
3539 .get_link_ksettings = macb_get_link_ksettings,
3540 .set_link_ksettings = macb_set_link_ksettings,
3541 .get_ringparam = macb_get_ringparam,
3542 .set_ringparam = macb_set_ringparam,
3545 static const struct ethtool_ops gem_ethtool_ops = {
3546 .get_regs_len = macb_get_regs_len,
3547 .get_regs = macb_get_regs,
3548 .get_wol = macb_get_wol,
3549 .set_wol = macb_set_wol,
3550 .get_link = ethtool_op_get_link,
3551 .get_ts_info = macb_get_ts_info,
3552 .get_ethtool_stats = gem_get_ethtool_stats,
3553 .get_strings = gem_get_ethtool_strings,
3554 .get_sset_count = gem_get_sset_count,
3555 .get_link_ksettings = macb_get_link_ksettings,
3556 .set_link_ksettings = macb_set_link_ksettings,
3557 .get_ringparam = macb_get_ringparam,
3558 .set_ringparam = macb_set_ringparam,
3559 .get_rxnfc = gem_get_rxnfc,
3560 .set_rxnfc = gem_set_rxnfc,
3563 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3565 struct macb *bp = netdev_priv(dev);
3567 if (!netif_running(dev))
3573 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3575 return bp->ptp_info->get_hwtst(dev, rq);
3579 return phylink_mii_ioctl(bp->phylink, rq, cmd);
3582 static inline void macb_set_txcsum_feature(struct macb *bp,
3583 netdev_features_t features)
3587 if (!macb_is_gem(bp))
3590 val = gem_readl(bp, DMACFG);
3591 if (features & NETIF_F_HW_CSUM)
3592 val |= GEM_BIT(TXCOEN);
3594 val &= ~GEM_BIT(TXCOEN);
3596 gem_writel(bp, DMACFG, val);
3599 static inline void macb_set_rxcsum_feature(struct macb *bp,
3600 netdev_features_t features)
3602 struct net_device *netdev = bp->dev;
3605 if (!macb_is_gem(bp))
3608 val = gem_readl(bp, NCFGR);
3609 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3610 val |= GEM_BIT(RXCOEN);
3612 val &= ~GEM_BIT(RXCOEN);
3614 gem_writel(bp, NCFGR, val);
3617 static inline void macb_set_rxflow_feature(struct macb *bp,
3618 netdev_features_t features)
3620 if (!macb_is_gem(bp))
3623 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3626 static int macb_set_features(struct net_device *netdev,
3627 netdev_features_t features)
3629 struct macb *bp = netdev_priv(netdev);
3630 netdev_features_t changed = features ^ netdev->features;
3632 /* TX checksum offload */
3633 if (changed & NETIF_F_HW_CSUM)
3634 macb_set_txcsum_feature(bp, features);
3636 /* RX checksum offload */
3637 if (changed & NETIF_F_RXCSUM)
3638 macb_set_rxcsum_feature(bp, features);
3640 /* RX Flow Filters */
3641 if (changed & NETIF_F_NTUPLE)
3642 macb_set_rxflow_feature(bp, features);
3647 static void macb_restore_features(struct macb *bp)
3649 struct net_device *netdev = bp->dev;
3650 netdev_features_t features = netdev->features;
3651 struct ethtool_rx_fs_item *item;
3653 /* TX checksum offload */
3654 macb_set_txcsum_feature(bp, features);
3656 /* RX checksum offload */
3657 macb_set_rxcsum_feature(bp, features);
3659 /* RX Flow Filters */
3660 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3661 gem_prog_cmp_regs(bp, &item->fs);
3663 macb_set_rxflow_feature(bp, features);
3666 static const struct net_device_ops macb_netdev_ops = {
3667 .ndo_open = macb_open,
3668 .ndo_stop = macb_close,
3669 .ndo_start_xmit = macb_start_xmit,
3670 .ndo_set_rx_mode = macb_set_rx_mode,
3671 .ndo_get_stats = macb_get_stats,
3672 .ndo_eth_ioctl = macb_ioctl,
3673 .ndo_validate_addr = eth_validate_addr,
3674 .ndo_change_mtu = macb_change_mtu,
3675 .ndo_set_mac_address = eth_mac_addr,
3676 #ifdef CONFIG_NET_POLL_CONTROLLER
3677 .ndo_poll_controller = macb_poll_controller,
3679 .ndo_set_features = macb_set_features,
3680 .ndo_features_check = macb_features_check,
3683 /* Configure peripheral capabilities according to device tree
3684 * and integration options used
3686 static void macb_configure_caps(struct macb *bp,
3687 const struct macb_config *dt_conf)
3692 bp->caps = dt_conf->caps;
3694 if (hw_is_gem(bp->regs, bp->native_io)) {
3695 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3697 dcfg = gem_readl(bp, DCFG1);
3698 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3699 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3700 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3701 bp->caps |= MACB_CAPS_PCS;
3702 dcfg = gem_readl(bp, DCFG12);
3703 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3704 bp->caps |= MACB_CAPS_HIGH_SPEED;
3705 dcfg = gem_readl(bp, DCFG2);
3706 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3707 bp->caps |= MACB_CAPS_FIFO_MODE;
3708 #ifdef CONFIG_MACB_USE_HWSTAMP
3709 if (gem_has_ptp(bp)) {
3710 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3711 dev_err(&bp->pdev->dev,
3712 "GEM doesn't support hardware ptp.\n");
3714 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
3715 bp->ptp_info = &gem_ptp_info;
3721 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
3724 static void macb_probe_queues(void __iomem *mem,
3726 unsigned int *queue_mask,
3727 unsigned int *num_queues)
3732 /* is it macb or gem ?
3734 * We need to read directly from the hardware here because
3735 * we are early in the probe process and don't have the
3736 * MACB_CAPS_MACB_IS_GEM flag positioned
3738 if (!hw_is_gem(mem, native_io))
3741 /* bit 0 is never set but queue 0 always exists */
3742 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
3743 *num_queues = hweight32(*queue_mask);
3746 static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3747 struct clk *rx_clk, struct clk *tsu_clk)
3749 struct clk_bulk_data clks[] = {
3750 { .clk = tsu_clk, },
3757 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3760 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
3761 struct clk **hclk, struct clk **tx_clk,
3762 struct clk **rx_clk, struct clk **tsu_clk)
3764 struct macb_platform_data *pdata;
3767 pdata = dev_get_platdata(&pdev->dev);
3769 *pclk = pdata->pclk;
3770 *hclk = pdata->hclk;
3772 *pclk = devm_clk_get(&pdev->dev, "pclk");
3773 *hclk = devm_clk_get(&pdev->dev, "hclk");
3776 if (IS_ERR_OR_NULL(*pclk))
3777 return dev_err_probe(&pdev->dev,
3778 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
3779 "failed to get pclk\n");
3781 if (IS_ERR_OR_NULL(*hclk))
3782 return dev_err_probe(&pdev->dev,
3783 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
3784 "failed to get hclk\n");
3786 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
3787 if (IS_ERR(*tx_clk))
3788 return PTR_ERR(*tx_clk);
3790 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
3791 if (IS_ERR(*rx_clk))
3792 return PTR_ERR(*rx_clk);
3794 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
3795 if (IS_ERR(*tsu_clk))
3796 return PTR_ERR(*tsu_clk);
3798 err = clk_prepare_enable(*pclk);
3800 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
3804 err = clk_prepare_enable(*hclk);
3806 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
3807 goto err_disable_pclk;
3810 err = clk_prepare_enable(*tx_clk);
3812 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
3813 goto err_disable_hclk;
3816 err = clk_prepare_enable(*rx_clk);
3818 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
3819 goto err_disable_txclk;
3822 err = clk_prepare_enable(*tsu_clk);
3824 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
3825 goto err_disable_rxclk;
3831 clk_disable_unprepare(*rx_clk);
3834 clk_disable_unprepare(*tx_clk);
3837 clk_disable_unprepare(*hclk);
3840 clk_disable_unprepare(*pclk);
3845 static int macb_init(struct platform_device *pdev)
3847 struct net_device *dev = platform_get_drvdata(pdev);
3848 unsigned int hw_q, q;
3849 struct macb *bp = netdev_priv(dev);
3850 struct macb_queue *queue;
3854 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3855 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3857 /* set the queue register mapping once for all: queue0 has a special
3858 * register mapping but we don't want to test the queue index then
3859 * compute the corresponding register offset at run time.
3861 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
3862 if (!(bp->queue_mask & (1 << hw_q)))
3865 queue = &bp->queues[q];
3867 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
3869 queue->ISR = GEM_ISR(hw_q - 1);
3870 queue->IER = GEM_IER(hw_q - 1);
3871 queue->IDR = GEM_IDR(hw_q - 1);
3872 queue->IMR = GEM_IMR(hw_q - 1);
3873 queue->TBQP = GEM_TBQP(hw_q - 1);
3874 queue->RBQP = GEM_RBQP(hw_q - 1);
3875 queue->RBQS = GEM_RBQS(hw_q - 1);
3876 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3877 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3878 queue->TBQPH = GEM_TBQPH(hw_q - 1);
3879 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3883 /* queue0 uses legacy registers */
3884 queue->ISR = MACB_ISR;
3885 queue->IER = MACB_IER;
3886 queue->IDR = MACB_IDR;
3887 queue->IMR = MACB_IMR;
3888 queue->TBQP = MACB_TBQP;
3889 queue->RBQP = MACB_RBQP;
3890 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3891 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3892 queue->TBQPH = MACB_TBQPH;
3893 queue->RBQPH = MACB_RBQPH;
3898 /* get irq: here we use the linux queue index, not the hardware
3899 * queue index. the queue irq definitions in the device tree
3900 * must remove the optional gaps that could exist in the
3901 * hardware queue mask.
3903 queue->irq = platform_get_irq(pdev, q);
3904 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
3905 IRQF_SHARED, dev->name, queue);
3908 "Unable to request IRQ %d (error %d)\n",
3913 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
3917 dev->netdev_ops = &macb_netdev_ops;
3919 /* setup appropriated routines according to adapter type */
3920 if (macb_is_gem(bp)) {
3921 bp->max_tx_length = GEM_MAX_TX_LEN;
3922 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3923 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3924 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3925 bp->macbgem_ops.mog_rx = gem_rx;
3926 dev->ethtool_ops = &gem_ethtool_ops;
3928 bp->max_tx_length = MACB_MAX_TX_LEN;
3929 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3930 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3931 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3932 bp->macbgem_ops.mog_rx = macb_rx;
3933 dev->ethtool_ops = &macb_ethtool_ops;
3937 dev->hw_features = NETIF_F_SG;
3939 /* Check LSO capability */
3940 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3941 dev->hw_features |= MACB_NETIF_LSO;
3943 /* Checksum offload is only available on gem with packet buffer */
3944 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
3945 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3946 if (bp->caps & MACB_CAPS_SG_DISABLED)
3947 dev->hw_features &= ~NETIF_F_SG;
3948 dev->features = dev->hw_features;
3950 /* Check RX Flow Filters support.
3951 * Max Rx flows set by availability of screeners & compare regs:
3952 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3954 reg = gem_readl(bp, DCFG8);
3955 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3956 GEM_BFEXT(T2SCR, reg));
3957 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3958 if (bp->max_tuples > 0) {
3959 /* also needs one ethtype match to check IPv4 */
3960 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3961 /* program this reg now */
3963 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3964 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3965 /* Filtering is supported in hw but don't enable it in kernel now */
3966 dev->hw_features |= NETIF_F_NTUPLE;
3967 /* init Rx flow definitions */
3968 bp->rx_fs_list.count = 0;
3969 spin_lock_init(&bp->rx_fs_lock);
3974 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3976 if (phy_interface_mode_is_rgmii(bp->phy_interface))
3977 val = bp->usrio->rgmii;
3978 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
3979 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3980 val = bp->usrio->rmii;
3981 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3982 val = bp->usrio->mii;
3984 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3985 val |= bp->usrio->refclk;
3987 macb_or_gem_writel(bp, USRIO, val);
3990 /* Set MII management clock divider */
3991 val = macb_mdc_clk_div(bp);
3992 val |= macb_dbw(bp);
3993 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3994 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
3995 macb_writel(bp, NCFGR, val);
4000 static const struct macb_usrio_config macb_default_usrio = {
4001 .mii = MACB_BIT(MII),
4002 .rmii = MACB_BIT(RMII),
4003 .rgmii = GEM_BIT(RGMII),
4004 .refclk = MACB_BIT(CLKEN),
4007 #if defined(CONFIG_OF)
4008 /* 1518 rounded up */
4009 #define AT91ETHER_MAX_RBUFF_SZ 0x600
4010 /* max number of receive buffers */
4011 #define AT91ETHER_MAX_RX_DESCR 9
4013 static struct sifive_fu540_macb_mgmt *mgmt;
4015 static int at91ether_alloc_coherent(struct macb *lp)
4017 struct macb_queue *q = &lp->queues[0];
4019 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
4020 (AT91ETHER_MAX_RX_DESCR *
4021 macb_dma_desc_get_size(lp)),
4022 &q->rx_ring_dma, GFP_KERNEL);
4026 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
4027 AT91ETHER_MAX_RX_DESCR *
4028 AT91ETHER_MAX_RBUFF_SZ,
4029 &q->rx_buffers_dma, GFP_KERNEL);
4030 if (!q->rx_buffers) {
4031 dma_free_coherent(&lp->pdev->dev,
4032 AT91ETHER_MAX_RX_DESCR *
4033 macb_dma_desc_get_size(lp),
4034 q->rx_ring, q->rx_ring_dma);
4042 static void at91ether_free_coherent(struct macb *lp)
4044 struct macb_queue *q = &lp->queues[0];
4047 dma_free_coherent(&lp->pdev->dev,
4048 AT91ETHER_MAX_RX_DESCR *
4049 macb_dma_desc_get_size(lp),
4050 q->rx_ring, q->rx_ring_dma);
4054 if (q->rx_buffers) {
4055 dma_free_coherent(&lp->pdev->dev,
4056 AT91ETHER_MAX_RX_DESCR *
4057 AT91ETHER_MAX_RBUFF_SZ,
4058 q->rx_buffers, q->rx_buffers_dma);
4059 q->rx_buffers = NULL;
4063 /* Initialize and start the Receiver and Transmit subsystems */
4064 static int at91ether_start(struct macb *lp)
4066 struct macb_queue *q = &lp->queues[0];
4067 struct macb_dma_desc *desc;
4072 ret = at91ether_alloc_coherent(lp);
4076 addr = q->rx_buffers_dma;
4077 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
4078 desc = macb_rx_desc(q, i);
4079 macb_set_addr(lp, desc, addr);
4081 addr += AT91ETHER_MAX_RBUFF_SZ;
4084 /* Set the Wrap bit on the last descriptor */
4085 desc->addr |= MACB_BIT(RX_WRAP);
4087 /* Reset buffer index */
4090 /* Program address of descriptor list in Rx Buffer Queue register */
4091 macb_writel(lp, RBQP, q->rx_ring_dma);
4093 /* Enable Receive and Transmit */
4094 ctl = macb_readl(lp, NCR);
4095 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4097 /* Enable MAC interrupts */
4098 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4100 MACB_BIT(ISR_TUND) |
4103 MACB_BIT(ISR_ROVR) |
4109 static void at91ether_stop(struct macb *lp)
4113 /* Disable MAC interrupts */
4114 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4116 MACB_BIT(ISR_TUND) |
4119 MACB_BIT(ISR_ROVR) |
4122 /* Disable Receiver and Transmitter */
4123 ctl = macb_readl(lp, NCR);
4124 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4126 /* Free resources. */
4127 at91ether_free_coherent(lp);
4130 /* Open the ethernet interface */
4131 static int at91ether_open(struct net_device *dev)
4133 struct macb *lp = netdev_priv(dev);
4137 ret = pm_runtime_get_sync(&lp->pdev->dev);
4139 pm_runtime_put_noidle(&lp->pdev->dev);
4143 /* Clear internal statistics */
4144 ctl = macb_readl(lp, NCR);
4145 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4147 macb_set_hwaddr(lp);
4149 ret = at91ether_start(lp);
4153 ret = macb_phylink_connect(lp);
4157 netif_start_queue(dev);
4164 pm_runtime_put_sync(&lp->pdev->dev);
4168 /* Close the interface */
4169 static int at91ether_close(struct net_device *dev)
4171 struct macb *lp = netdev_priv(dev);
4173 netif_stop_queue(dev);
4175 phylink_stop(lp->phylink);
4176 phylink_disconnect_phy(lp->phylink);
4180 return pm_runtime_put(&lp->pdev->dev);
4183 /* Transmit packet */
4184 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4185 struct net_device *dev)
4187 struct macb *lp = netdev_priv(dev);
4189 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
4192 netif_stop_queue(dev);
4194 /* Store packet information (to free when Tx completed) */
4195 lp->rm9200_txq[desc].skb = skb;
4196 lp->rm9200_txq[desc].size = skb->len;
4197 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4198 skb->len, DMA_TO_DEVICE);
4199 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
4200 dev_kfree_skb_any(skb);
4201 dev->stats.tx_dropped++;
4202 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4203 return NETDEV_TX_OK;
4206 /* Set address of the data in the Transmit Address register */
4207 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
4208 /* Set length of the packet in the Transmit Control register */
4209 macb_writel(lp, TCR, skb->len);
4212 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4213 return NETDEV_TX_BUSY;
4216 return NETDEV_TX_OK;
4219 /* Extract received frame from buffer descriptors and sent to upper layers.
4220 * (Called from interrupt context)
4222 static void at91ether_rx(struct net_device *dev)
4224 struct macb *lp = netdev_priv(dev);
4225 struct macb_queue *q = &lp->queues[0];
4226 struct macb_dma_desc *desc;
4227 unsigned char *p_recv;
4228 struct sk_buff *skb;
4229 unsigned int pktlen;
4231 desc = macb_rx_desc(q, q->rx_tail);
4232 while (desc->addr & MACB_BIT(RX_USED)) {
4233 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
4234 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
4235 skb = netdev_alloc_skb(dev, pktlen + 2);
4237 skb_reserve(skb, 2);
4238 skb_put_data(skb, p_recv, pktlen);
4240 skb->protocol = eth_type_trans(skb, dev);
4241 dev->stats.rx_packets++;
4242 dev->stats.rx_bytes += pktlen;
4245 dev->stats.rx_dropped++;
4248 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
4249 dev->stats.multicast++;
4251 /* reset ownership bit */
4252 desc->addr &= ~MACB_BIT(RX_USED);
4254 /* wrap after last buffer */
4255 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4260 desc = macb_rx_desc(q, q->rx_tail);
4264 /* MAC interrupt handler */
4265 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4267 struct net_device *dev = dev_id;
4268 struct macb *lp = netdev_priv(dev);
4272 /* MAC Interrupt Status register indicates what interrupts are pending.
4273 * It is automatically cleared once read.
4275 intstatus = macb_readl(lp, ISR);
4277 /* Receive complete */
4278 if (intstatus & MACB_BIT(RCOMP))
4281 /* Transmit complete */
4282 if (intstatus & MACB_BIT(TCOMP)) {
4283 /* The TCOM bit is set even if the transmission failed */
4284 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
4285 dev->stats.tx_errors++;
4288 if (lp->rm9200_txq[desc].skb) {
4289 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4290 lp->rm9200_txq[desc].skb = NULL;
4291 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4292 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
4293 dev->stats.tx_packets++;
4294 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
4296 netif_wake_queue(dev);
4299 /* Work-around for EMAC Errata section 41.3.1 */
4300 if (intstatus & MACB_BIT(RXUBR)) {
4301 ctl = macb_readl(lp, NCR);
4302 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
4304 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4307 if (intstatus & MACB_BIT(ISR_ROVR))
4308 netdev_err(dev, "ROVR error\n");
4313 #ifdef CONFIG_NET_POLL_CONTROLLER
4314 static void at91ether_poll_controller(struct net_device *dev)
4316 unsigned long flags;
4318 local_irq_save(flags);
4319 at91ether_interrupt(dev->irq, dev);
4320 local_irq_restore(flags);
4324 static const struct net_device_ops at91ether_netdev_ops = {
4325 .ndo_open = at91ether_open,
4326 .ndo_stop = at91ether_close,
4327 .ndo_start_xmit = at91ether_start_xmit,
4328 .ndo_get_stats = macb_get_stats,
4329 .ndo_set_rx_mode = macb_set_rx_mode,
4330 .ndo_set_mac_address = eth_mac_addr,
4331 .ndo_eth_ioctl = macb_ioctl,
4332 .ndo_validate_addr = eth_validate_addr,
4333 #ifdef CONFIG_NET_POLL_CONTROLLER
4334 .ndo_poll_controller = at91ether_poll_controller,
4338 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
4339 struct clk **hclk, struct clk **tx_clk,
4340 struct clk **rx_clk, struct clk **tsu_clk)
4349 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4351 return PTR_ERR(*pclk);
4353 err = clk_prepare_enable(*pclk);
4355 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
4362 static int at91ether_init(struct platform_device *pdev)
4364 struct net_device *dev = platform_get_drvdata(pdev);
4365 struct macb *bp = netdev_priv(dev);
4368 bp->queues[0].bp = bp;
4370 dev->netdev_ops = &at91ether_netdev_ops;
4371 dev->ethtool_ops = &macb_ethtool_ops;
4373 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4378 macb_writel(bp, NCR, 0);
4380 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
4385 static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4386 unsigned long parent_rate)
4391 static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4392 unsigned long *parent_rate)
4394 if (WARN_ON(rate < 2500000))
4396 else if (rate == 2500000)
4398 else if (WARN_ON(rate < 13750000))
4400 else if (WARN_ON(rate < 25000000))
4402 else if (rate == 25000000)
4404 else if (WARN_ON(rate < 75000000))
4406 else if (WARN_ON(rate < 125000000))
4408 else if (rate == 125000000)
4411 WARN_ON(rate > 125000000);
4416 static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4417 unsigned long parent_rate)
4419 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4420 if (rate != 125000000)
4421 iowrite32(1, mgmt->reg);
4423 iowrite32(0, mgmt->reg);
4429 static const struct clk_ops fu540_c000_ops = {
4430 .recalc_rate = fu540_macb_tx_recalc_rate,
4431 .round_rate = fu540_macb_tx_round_rate,
4432 .set_rate = fu540_macb_tx_set_rate,
4435 static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4436 struct clk **hclk, struct clk **tx_clk,
4437 struct clk **rx_clk, struct clk **tsu_clk)
4439 struct clk_init_data init;
4442 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4446 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4449 goto err_disable_clks;
4452 init.name = "sifive-gemgxl-mgmt";
4453 init.ops = &fu540_c000_ops;
4455 init.num_parents = 0;
4458 mgmt->hw.init = &init;
4460 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
4461 if (IS_ERR(*tx_clk)) {
4462 err = PTR_ERR(*tx_clk);
4463 goto err_disable_clks;
4466 err = clk_prepare_enable(*tx_clk);
4468 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4470 goto err_disable_clks;
4472 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4478 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
4483 static int fu540_c000_init(struct platform_device *pdev)
4485 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4486 if (IS_ERR(mgmt->reg))
4487 return PTR_ERR(mgmt->reg);
4489 return macb_init(pdev);
4492 static const struct macb_usrio_config sama7g5_usrio = {
4500 static const struct macb_config fu540_c000_config = {
4501 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4502 MACB_CAPS_GEM_HAS_PTP,
4503 .dma_burst_length = 16,
4504 .clk_init = fu540_c000_clk_init,
4505 .init = fu540_c000_init,
4506 .jumbo_max_len = 10240,
4507 .usrio = &macb_default_usrio,
4510 static const struct macb_config at91sam9260_config = {
4511 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4512 .clk_init = macb_clk_init,
4514 .usrio = &macb_default_usrio,
4517 static const struct macb_config sama5d3macb_config = {
4518 .caps = MACB_CAPS_SG_DISABLED
4519 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4520 .clk_init = macb_clk_init,
4522 .usrio = &macb_default_usrio,
4525 static const struct macb_config pc302gem_config = {
4526 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4527 .dma_burst_length = 16,
4528 .clk_init = macb_clk_init,
4530 .usrio = &macb_default_usrio,
4533 static const struct macb_config sama5d2_config = {
4534 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4535 .dma_burst_length = 16,
4536 .clk_init = macb_clk_init,
4538 .usrio = &macb_default_usrio,
4541 static const struct macb_config sama5d29_config = {
4542 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP,
4543 .dma_burst_length = 16,
4544 .clk_init = macb_clk_init,
4546 .usrio = &macb_default_usrio,
4549 static const struct macb_config sama5d3_config = {
4550 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
4551 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
4552 .dma_burst_length = 16,
4553 .clk_init = macb_clk_init,
4555 .jumbo_max_len = 10240,
4556 .usrio = &macb_default_usrio,
4559 static const struct macb_config sama5d4_config = {
4560 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4561 .dma_burst_length = 4,
4562 .clk_init = macb_clk_init,
4564 .usrio = &macb_default_usrio,
4567 static const struct macb_config emac_config = {
4568 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
4569 .clk_init = at91ether_clk_init,
4570 .init = at91ether_init,
4571 .usrio = &macb_default_usrio,
4574 static const struct macb_config np4_config = {
4575 .caps = MACB_CAPS_USRIO_DISABLED,
4576 .clk_init = macb_clk_init,
4578 .usrio = &macb_default_usrio,
4581 static int zynqmp_init(struct platform_device *pdev)
4583 struct net_device *dev = platform_get_drvdata(pdev);
4584 struct macb *bp = netdev_priv(dev);
4587 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4588 /* Ensure PS-GTR PHY device used in SGMII mode is ready */
4589 bp->sgmii_phy = devm_phy_get(&pdev->dev, "sgmii-phy");
4591 if (IS_ERR(bp->sgmii_phy)) {
4592 ret = PTR_ERR(bp->sgmii_phy);
4593 dev_err_probe(&pdev->dev, ret,
4594 "failed to get PS-GTR PHY\n");
4598 ret = phy_init(bp->sgmii_phy);
4600 dev_err(&pdev->dev, "failed to init PS-GTR PHY: %d\n",
4606 /* Fully reset GEM controller at hardware level using zynqmp-reset driver,
4607 * if mapped in device tree.
4609 ret = device_reset_optional(&pdev->dev);
4611 dev_err_probe(&pdev->dev, ret, "failed to reset controller");
4612 phy_exit(bp->sgmii_phy);
4616 ret = macb_init(pdev);
4618 phy_exit(bp->sgmii_phy);
4623 static const struct macb_config zynqmp_config = {
4624 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4626 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
4627 .dma_burst_length = 16,
4628 .clk_init = macb_clk_init,
4629 .init = zynqmp_init,
4630 .jumbo_max_len = 10240,
4631 .usrio = &macb_default_usrio,
4634 static const struct macb_config zynq_config = {
4635 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4636 MACB_CAPS_NEEDS_RSTONUBR,
4637 .dma_burst_length = 16,
4638 .clk_init = macb_clk_init,
4640 .usrio = &macb_default_usrio,
4643 static const struct macb_config sama7g5_gem_config = {
4644 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
4645 MACB_CAPS_MIIONRGMII,
4646 .dma_burst_length = 16,
4647 .clk_init = macb_clk_init,
4649 .usrio = &sama7g5_usrio,
4652 static const struct macb_config sama7g5_emac_config = {
4653 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
4654 MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII,
4655 .dma_burst_length = 16,
4656 .clk_init = macb_clk_init,
4658 .usrio = &sama7g5_usrio,
4661 static const struct of_device_id macb_dt_ids[] = {
4662 { .compatible = "cdns,at32ap7000-macb" },
4663 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4664 { .compatible = "cdns,macb" },
4665 { .compatible = "cdns,np4-macb", .data = &np4_config },
4666 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4667 { .compatible = "cdns,gem", .data = &pc302gem_config },
4668 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
4669 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
4670 { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
4671 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
4672 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
4673 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4674 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4675 { .compatible = "cdns,emac", .data = &emac_config },
4676 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
4677 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
4678 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
4679 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
4680 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
4683 MODULE_DEVICE_TABLE(of, macb_dt_ids);
4684 #endif /* CONFIG_OF */
4686 static const struct macb_config default_gem_config = {
4687 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4689 MACB_CAPS_GEM_HAS_PTP,
4690 .dma_burst_length = 16,
4691 .clk_init = macb_clk_init,
4693 .usrio = &macb_default_usrio,
4694 .jumbo_max_len = 10240,
4697 static int macb_probe(struct platform_device *pdev)
4699 const struct macb_config *macb_config = &default_gem_config;
4700 int (*clk_init)(struct platform_device *, struct clk **,
4701 struct clk **, struct clk **, struct clk **,
4702 struct clk **) = macb_config->clk_init;
4703 int (*init)(struct platform_device *) = macb_config->init;
4704 struct device_node *np = pdev->dev.of_node;
4705 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
4706 struct clk *tsu_clk = NULL;
4707 unsigned int queue_mask, num_queues;
4709 phy_interface_t interface;
4710 struct net_device *dev;
4711 struct resource *regs;
4716 mem = devm_platform_get_and_ioremap_resource(pdev, 0, ®s);
4718 return PTR_ERR(mem);
4721 const struct of_device_id *match;
4723 match = of_match_node(macb_dt_ids, np);
4724 if (match && match->data) {
4725 macb_config = match->data;
4726 clk_init = macb_config->clk_init;
4727 init = macb_config->init;
4731 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
4735 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4736 pm_runtime_use_autosuspend(&pdev->dev);
4737 pm_runtime_get_noresume(&pdev->dev);
4738 pm_runtime_set_active(&pdev->dev);
4739 pm_runtime_enable(&pdev->dev);
4740 native_io = hw_is_native_io(mem);
4742 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
4743 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
4746 goto err_disable_clocks;
4749 dev->base_addr = regs->start;
4751 SET_NETDEV_DEV(dev, &pdev->dev);
4753 bp = netdev_priv(dev);
4757 bp->native_io = native_io;
4759 bp->macb_reg_readl = hw_readl_native;
4760 bp->macb_reg_writel = hw_writel_native;
4762 bp->macb_reg_readl = hw_readl;
4763 bp->macb_reg_writel = hw_writel;
4765 bp->num_queues = num_queues;
4766 bp->queue_mask = queue_mask;
4768 bp->dma_burst_length = macb_config->dma_burst_length;
4771 bp->tx_clk = tx_clk;
4772 bp->rx_clk = rx_clk;
4773 bp->tsu_clk = tsu_clk;
4775 bp->jumbo_max_len = macb_config->jumbo_max_len;
4778 if (of_get_property(np, "magic-packet", NULL))
4779 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4780 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4782 bp->usrio = macb_config->usrio;
4784 spin_lock_init(&bp->lock);
4786 /* setup capabilities */
4787 macb_configure_caps(bp, macb_config);
4789 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4790 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4791 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
4792 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4795 platform_set_drvdata(pdev, dev);
4797 dev->irq = platform_get_irq(pdev, 0);
4800 goto err_out_free_netdev;
4803 /* MTU range: 68 - 1500 or 10240 */
4804 dev->min_mtu = GEM_MTU_MIN_SIZE;
4805 if (bp->caps & MACB_CAPS_JUMBO)
4806 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4808 dev->max_mtu = ETH_DATA_LEN;
4810 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4811 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4813 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4814 macb_dma_desc_get_size(bp);
4816 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4818 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4819 macb_dma_desc_get_size(bp);
4822 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4823 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4824 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4826 err = of_get_ethdev_address(np, bp->dev);
4827 if (err == -EPROBE_DEFER)
4828 goto err_out_free_netdev;
4830 macb_get_hwaddr(bp);
4832 err = of_get_phy_mode(np, &interface);
4834 /* not found in DT, MII by default */
4835 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4837 bp->phy_interface = interface;
4839 /* IP specific init */
4842 goto err_out_free_netdev;
4844 err = macb_mii_init(bp);
4846 goto err_out_phy_exit;
4848 netif_carrier_off(dev);
4850 err = register_netdev(dev);
4852 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
4853 goto err_out_unregister_mdio;
4856 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
4858 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4859 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4860 dev->base_addr, dev->irq, dev->dev_addr);
4862 pm_runtime_mark_last_busy(&bp->pdev->dev);
4863 pm_runtime_put_autosuspend(&bp->pdev->dev);
4867 err_out_unregister_mdio:
4868 mdiobus_unregister(bp->mii_bus);
4869 mdiobus_free(bp->mii_bus);
4872 phy_exit(bp->sgmii_phy);
4874 err_out_free_netdev:
4878 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
4879 pm_runtime_disable(&pdev->dev);
4880 pm_runtime_set_suspended(&pdev->dev);
4881 pm_runtime_dont_use_autosuspend(&pdev->dev);
4886 static int macb_remove(struct platform_device *pdev)
4888 struct net_device *dev;
4891 dev = platform_get_drvdata(pdev);
4894 bp = netdev_priv(dev);
4895 phy_exit(bp->sgmii_phy);
4896 mdiobus_unregister(bp->mii_bus);
4897 mdiobus_free(bp->mii_bus);
4899 unregister_netdev(dev);
4900 tasklet_kill(&bp->hresp_err_tasklet);
4901 pm_runtime_disable(&pdev->dev);
4902 pm_runtime_dont_use_autosuspend(&pdev->dev);
4903 if (!pm_runtime_suspended(&pdev->dev)) {
4904 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4905 bp->rx_clk, bp->tsu_clk);
4906 pm_runtime_set_suspended(&pdev->dev);
4908 phylink_destroy(bp->phylink);
4915 static int __maybe_unused macb_suspend(struct device *dev)
4917 struct net_device *netdev = dev_get_drvdata(dev);
4918 struct macb *bp = netdev_priv(netdev);
4919 struct macb_queue *queue;
4920 unsigned long flags;
4924 if (!netif_running(netdev))
4927 if (bp->wol & MACB_WOL_ENABLED) {
4928 spin_lock_irqsave(&bp->lock, flags);
4929 /* Flush all status bits */
4930 macb_writel(bp, TSR, -1);
4931 macb_writel(bp, RSR, -1);
4932 for (q = 0, queue = bp->queues; q < bp->num_queues;
4934 /* Disable all interrupts */
4935 queue_writel(queue, IDR, -1);
4936 queue_readl(queue, ISR);
4937 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4938 queue_writel(queue, ISR, -1);
4940 /* Change interrupt handler and
4941 * Enable WoL IRQ on queue 0
4943 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4944 if (macb_is_gem(bp)) {
4945 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4946 IRQF_SHARED, netdev->name, bp->queues);
4949 "Unable to request IRQ %d (error %d)\n",
4950 bp->queues[0].irq, err);
4951 spin_unlock_irqrestore(&bp->lock, flags);
4954 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4955 gem_writel(bp, WOL, MACB_BIT(MAG));
4957 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4958 IRQF_SHARED, netdev->name, bp->queues);
4961 "Unable to request IRQ %d (error %d)\n",
4962 bp->queues[0].irq, err);
4963 spin_unlock_irqrestore(&bp->lock, flags);
4966 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4967 macb_writel(bp, WOL, MACB_BIT(MAG));
4969 spin_unlock_irqrestore(&bp->lock, flags);
4971 enable_irq_wake(bp->queues[0].irq);
4974 netif_device_detach(netdev);
4975 for (q = 0, queue = bp->queues; q < bp->num_queues;
4977 napi_disable(&queue->napi);
4979 if (!(bp->wol & MACB_WOL_ENABLED)) {
4981 phylink_stop(bp->phylink);
4983 spin_lock_irqsave(&bp->lock, flags);
4985 spin_unlock_irqrestore(&bp->lock, flags);
4988 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4989 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4991 if (netdev->hw_features & NETIF_F_NTUPLE)
4992 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4995 bp->ptp_info->ptp_remove(netdev);
4996 if (!device_may_wakeup(dev))
4997 pm_runtime_force_suspend(dev);
5002 static int __maybe_unused macb_resume(struct device *dev)
5004 struct net_device *netdev = dev_get_drvdata(dev);
5005 struct macb *bp = netdev_priv(netdev);
5006 struct macb_queue *queue;
5007 unsigned long flags;
5011 if (!netif_running(netdev))
5014 if (!device_may_wakeup(dev))
5015 pm_runtime_force_resume(dev);
5017 if (bp->wol & MACB_WOL_ENABLED) {
5018 spin_lock_irqsave(&bp->lock, flags);
5020 if (macb_is_gem(bp)) {
5021 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
5022 gem_writel(bp, WOL, 0);
5024 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
5025 macb_writel(bp, WOL, 0);
5027 /* Clear ISR on queue 0 */
5028 queue_readl(bp->queues, ISR);
5029 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
5030 queue_writel(bp->queues, ISR, -1);
5031 /* Replace interrupt handler on queue 0 */
5032 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
5033 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
5034 IRQF_SHARED, netdev->name, bp->queues);
5037 "Unable to request IRQ %d (error %d)\n",
5038 bp->queues[0].irq, err);
5039 spin_unlock_irqrestore(&bp->lock, flags);
5042 spin_unlock_irqrestore(&bp->lock, flags);
5044 disable_irq_wake(bp->queues[0].irq);
5046 /* Now make sure we disable phy before moving
5047 * to common restore path
5050 phylink_stop(bp->phylink);
5054 for (q = 0, queue = bp->queues; q < bp->num_queues;
5056 napi_enable(&queue->napi);
5058 if (netdev->hw_features & NETIF_F_NTUPLE)
5059 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
5061 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
5062 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
5064 macb_writel(bp, NCR, MACB_BIT(MPE));
5066 macb_set_rx_mode(netdev);
5067 macb_restore_features(bp);
5069 phylink_start(bp->phylink);
5072 netif_device_attach(netdev);
5074 bp->ptp_info->ptp_init(netdev);
5079 static int __maybe_unused macb_runtime_suspend(struct device *dev)
5081 struct net_device *netdev = dev_get_drvdata(dev);
5082 struct macb *bp = netdev_priv(netdev);
5084 if (!(device_may_wakeup(dev)))
5085 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
5087 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
5092 static int __maybe_unused macb_runtime_resume(struct device *dev)
5094 struct net_device *netdev = dev_get_drvdata(dev);
5095 struct macb *bp = netdev_priv(netdev);
5097 if (!(device_may_wakeup(dev))) {
5098 clk_prepare_enable(bp->pclk);
5099 clk_prepare_enable(bp->hclk);
5100 clk_prepare_enable(bp->tx_clk);
5101 clk_prepare_enable(bp->rx_clk);
5103 clk_prepare_enable(bp->tsu_clk);
5108 static const struct dev_pm_ops macb_pm_ops = {
5109 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5110 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5113 static struct platform_driver macb_driver = {
5114 .probe = macb_probe,
5115 .remove = macb_remove,
5118 .of_match_table = of_match_ptr(macb_dt_ids),
5123 module_platform_driver(macb_driver);
5125 MODULE_LICENSE("GPL");
5126 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
5127 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
5128 MODULE_ALIAS("platform:macb");