1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2005-2006 Atmel Corporation
10 #include <asm/global_data.h>
11 #include <linux/delay.h>
14 * The u-boot networking stack is a little weird. It seems like the
15 * networking core allocates receive buffers up front without any
16 * regard to the hardware that's supposed to actually receive those
19 * The MACB receives packets into 128-byte receive buffers, so the
20 * buffers allocated by the core isn't very practical to use. We'll
21 * allocate our own, but we need one such buffer in case a packet
22 * wraps around the DMA ring so that we have to copy it.
24 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
25 * configuration header. This way, the core allocates one RX buffer
26 * and one TX buffer, each of which can hold a ethernet packet of
29 * For some reason, the networking core unconditionally specifies a
30 * 32-byte packet "alignment" (which really should be called
31 * "padding"). MACB shouldn't need that, but we'll refrain from any
32 * core modifications here...
39 #include <linux/mii.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/arch/clk.h>
43 #include <linux/errno.h>
47 DECLARE_GLOBAL_DATA_PTR;
50 * These buffer sizes must be power of 2 and divisible
51 * by RX_BUFFER_MULTIPLE
53 #define MACB_RX_BUFFER_SIZE 128
54 #define GEM_RX_BUFFER_SIZE 2048
55 #define RX_BUFFER_MULTIPLE 64
57 #define MACB_RX_RING_SIZE 32
58 #define MACB_TX_RING_SIZE 16
60 #define MACB_TX_TIMEOUT 1000
61 #define MACB_AUTONEG_TIMEOUT 5000000
63 #ifdef CONFIG_MACB_ZYNQ
64 /* INCR4 AHB bursts */
65 #define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
66 /* Use full configured addressable space (8 Kb) */
67 #define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
68 /* Use full configured addressable space (4 Kb) */
69 #define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
70 /* Set RXBUF with use of 128 byte */
71 #define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
72 #define MACB_ZYNQ_GEM_DMACR_INIT \
73 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
74 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
76 MACB_ZYNQ_GEM_DMACR_RXBUF)
79 struct macb_dma_desc {
84 struct macb_dma_desc_64 {
89 #define HW_DMA_CAP_32B 0
90 #define HW_DMA_CAP_64B 1
92 #define DMA_DESC_SIZE 16
93 #define DMA_DESC_BYTES(n) ((n) * DMA_DESC_SIZE)
94 #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
95 #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
96 #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
98 #define DESC_PER_CACHELINE_32 (ARCH_DMA_MINALIGN/sizeof(struct macb_dma_desc))
99 #define DESC_PER_CACHELINE_64 (ARCH_DMA_MINALIGN/DMA_DESC_SIZE)
101 #define RXBUF_FRMLEN_MASK 0x00000fff
102 #define TXBUF_FRMLEN_MASK 0x000007ff
109 const struct macb_config *config;
111 unsigned int rx_tail;
112 unsigned int tx_head;
113 unsigned int tx_tail;
114 unsigned int next_rx_tail;
119 struct macb_dma_desc *rx_ring;
120 struct macb_dma_desc *tx_ring;
121 size_t rx_buffer_size;
123 unsigned long rx_buffer_dma;
124 unsigned long rx_ring_dma;
125 unsigned long tx_ring_dma;
127 struct macb_dma_desc *dummy_desc;
128 unsigned long dummy_desc_dma;
130 const struct device *dev;
133 unsigned short phy_addr;
136 struct phy_device *phydev;
140 unsigned long pclk_rate;
142 phy_interface_t phy_interface;
145 struct macb_usrio_cfg {
153 unsigned int dma_burst_length;
154 unsigned int hw_dma_cap;
157 int (*clk_init)(struct udevice *dev, ulong rate);
158 const struct macb_usrio_cfg *usrio;
161 static int macb_is_gem(struct macb_device *macb)
163 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
166 #ifndef cpu_is_sama5d2
167 #define cpu_is_sama5d2() 0
170 #ifndef cpu_is_sama5d4
171 #define cpu_is_sama5d4() 0
174 static int gem_is_gigabit_capable(struct macb_device *macb)
177 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
178 * configured to support only 10/100.
180 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
183 /* Is the port a fixed link */
184 static int macb_port_is_fixed_link(struct macb_device *macb)
186 return macb->phy_addr > PHY_MAX_ADDR;
189 static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
192 unsigned long netctl;
193 unsigned long netstat;
196 netctl = macb_readl(macb, NCR);
197 netctl |= MACB_BIT(MPE);
198 macb_writel(macb, NCR, netctl);
200 frame = (MACB_BF(SOF, 1)
202 | MACB_BF(PHYA, phy_adr)
205 | MACB_BF(DATA, value));
206 macb_writel(macb, MAN, frame);
209 netstat = macb_readl(macb, NSR);
210 } while (!(netstat & MACB_BIT(IDLE)));
212 netctl = macb_readl(macb, NCR);
213 netctl &= ~MACB_BIT(MPE);
214 macb_writel(macb, NCR, netctl);
217 static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
219 unsigned long netctl;
220 unsigned long netstat;
223 netctl = macb_readl(macb, NCR);
224 netctl |= MACB_BIT(MPE);
225 macb_writel(macb, NCR, netctl);
227 frame = (MACB_BF(SOF, 1)
229 | MACB_BF(PHYA, phy_adr)
232 macb_writel(macb, MAN, frame);
235 netstat = macb_readl(macb, NSR);
236 } while (!(netstat & MACB_BIT(IDLE)));
238 frame = macb_readl(macb, MAN);
240 netctl = macb_readl(macb, NCR);
241 netctl &= ~MACB_BIT(MPE);
242 macb_writel(macb, NCR, netctl);
244 return MACB_BFEXT(DATA, frame);
247 void __weak arch_get_mdio_control(const char *name)
252 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
254 int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
257 struct udevice *dev = eth_get_dev_by_name(bus->name);
258 struct macb_device *macb = dev_get_priv(dev);
260 arch_get_mdio_control(bus->name);
261 value = macb_mdio_read(macb, phy_adr, reg);
266 int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
269 struct udevice *dev = eth_get_dev_by_name(bus->name);
270 struct macb_device *macb = dev_get_priv(dev);
272 arch_get_mdio_control(bus->name);
273 macb_mdio_write(macb, phy_adr, reg, value);
281 static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
284 invalidate_dcache_range(macb->rx_ring_dma,
285 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
288 invalidate_dcache_range(macb->tx_ring_dma,
289 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
293 static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
296 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
297 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
299 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
300 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
303 static inline void macb_flush_rx_buffer(struct macb_device *macb)
305 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
306 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
310 static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
312 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
313 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
317 #if defined(CONFIG_CMD_NET)
319 static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
321 return (struct macb_dma_desc_64 *)((void *)desc
322 + sizeof(struct macb_dma_desc));
325 static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
328 struct macb_dma_desc_64 *desc_64;
330 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
331 desc_64 = macb_64b_desc(desc);
332 desc_64->addrh = upper_32_bits(addr);
334 desc->addr = lower_32_bits(addr);
337 static int _macb_send(struct macb_device *macb, const char *name, void *packet,
340 unsigned long paddr, ctrl;
341 unsigned int tx_head = macb->tx_head;
344 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
346 ctrl = length & TXBUF_FRMLEN_MASK;
347 ctrl |= MACB_BIT(TX_LAST);
348 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
349 ctrl |= MACB_BIT(TX_WRAP);
355 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
356 tx_head = tx_head * 2;
358 macb->tx_ring[tx_head].ctrl = ctrl;
359 macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
362 macb_flush_ring_desc(macb, TX);
363 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
366 * I guess this is necessary because the networking core may
367 * re-use the transmit buffer as soon as we return...
369 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
371 macb_invalidate_ring_desc(macb, TX);
372 ctrl = macb->tx_ring[tx_head].ctrl;
373 if (ctrl & MACB_BIT(TX_USED))
378 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
380 if (i <= MACB_TX_TIMEOUT) {
381 if (ctrl & MACB_BIT(TX_UNDERRUN))
382 printf("%s: TX underrun\n", name);
383 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
384 printf("%s: TX buffers exhausted in mid frame\n", name);
386 printf("%s: TX timeout\n", name);
389 /* No one cares anyway */
393 static void reclaim_rx_buffer(struct macb_device *macb,
401 * There may be multiple descriptors per CPU cacheline,
402 * so a cache flush would flush the whole line, meaning the content of other descriptors
403 * in the cacheline would also flush. If one of the other descriptors had been
404 * written to by the controller, the flush would cause those changes to be lost.
406 * To circumvent this issue, we do the actual freeing only when we need to free
407 * the last descriptor in the current cacheline. When the current descriptor is the
408 * last in the cacheline, we free all the descriptors that belong to that cacheline.
410 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
411 mask = DESC_PER_CACHELINE_64 - 1;
414 mask = DESC_PER_CACHELINE_32 - 1;
418 /* we exit without freeing if idx is not the last descriptor in the cacheline */
419 if ((idx & mask) != mask)
422 for (i = idx & (~mask); i <= idx; i++)
423 macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
426 static void reclaim_rx_buffers(struct macb_device *macb,
427 unsigned int new_tail)
433 macb_invalidate_ring_desc(macb, RX);
434 while (i > new_tail) {
435 reclaim_rx_buffer(macb, i);
437 if (i >= MACB_RX_RING_SIZE)
441 while (i < new_tail) {
442 reclaim_rx_buffer(macb, i);
447 macb_flush_ring_desc(macb, RX);
448 macb->rx_tail = new_tail;
451 static int _macb_recv(struct macb_device *macb, uchar **packetp)
453 unsigned int next_rx_tail = macb->next_rx_tail;
459 macb->wrapped = false;
461 macb_invalidate_ring_desc(macb, RX);
463 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
464 next_rx_tail = next_rx_tail * 2;
466 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
469 status = macb->rx_ring[next_rx_tail].ctrl;
470 if (status & MACB_BIT(RX_SOF)) {
471 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
472 next_rx_tail = next_rx_tail / 2;
476 if (next_rx_tail != macb->rx_tail)
477 reclaim_rx_buffers(macb, next_rx_tail);
478 macb->wrapped = false;
481 if (status & MACB_BIT(RX_EOF)) {
482 buffer = macb->rx_buffer +
483 macb->rx_buffer_size * macb->rx_tail;
484 length = status & RXBUF_FRMLEN_MASK;
486 macb_invalidate_rx_buffer(macb);
488 unsigned int headlen, taillen;
490 headlen = macb->rx_buffer_size *
491 (MACB_RX_RING_SIZE - macb->rx_tail);
492 taillen = length - headlen;
493 memcpy((void *)net_rx_packets[0],
495 memcpy((void *)net_rx_packets[0] + headlen,
496 macb->rx_buffer, taillen);
497 *packetp = (void *)net_rx_packets[0];
502 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
504 next_rx_tail = next_rx_tail / 2;
507 if (++next_rx_tail >= MACB_RX_RING_SIZE)
509 macb->next_rx_tail = next_rx_tail;
512 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
514 next_rx_tail = next_rx_tail / 2;
518 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
519 macb->wrapped = true;
527 static void macb_phy_reset(struct macb_device *macb, const char *name)
532 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
533 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
534 printf("%s: Starting autonegotiation...\n", name);
535 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
538 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
539 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
540 if (status & BMSR_ANEGCOMPLETE)
545 if (status & BMSR_ANEGCOMPLETE)
546 printf("%s: Autonegotiation complete\n", name);
548 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
552 static int macb_phy_find(struct macb_device *macb, const char *name)
557 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
558 if (phy_id != 0xffff) {
559 printf("%s: PHY present at %d\n", name, macb->phy_addr);
563 /* Search for PHY... */
564 for (i = 0; i < 32; i++) {
566 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
567 if (phy_id != 0xffff) {
568 printf("%s: PHY present at %d\n", name, i);
573 /* PHY isn't up to snuff */
574 printf("%s: PHY not found\n", name);
580 * macb_linkspd_cb - Linkspeed change callback function
581 * @dev/@regs: MACB udevice (DM version) or
582 * Base Register of MACB devices (non-DM version)
584 * Returns 0 when operation success and negative errno number
585 * when operation failed.
587 static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
591 gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
596 * SiFive GEMGXL TX clock operation mode:
598 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
599 * and output clock on GMII output signal GTX_CLK
600 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
602 writel(rate != 125000000, gemgxl_regs);
606 static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
611 ret = clk_get_by_name(dev, "tx_clk", &clk);
616 * This is for using GCK. Clock rate is addressed via assigned-clock
617 * property, so only clock enable is needed here. The switching to
618 * proper clock rate depending on link speed is managed by IP logic.
620 return clk_enable(&clk);
623 int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
626 struct macb_device *macb = dev_get_priv(dev);
633 rate = 2500000; /* 2.5 MHz */
636 rate = 25000000; /* 25 MHz */
639 rate = 125000000; /* 125 MHz */
642 /* does not change anything */
646 if (macb->config->clk_init)
647 return macb->config->clk_init(dev, rate);
650 * "tx_clk" is an optional clock source for MACB.
651 * Ignore if it does not exist in DT.
653 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
658 ret = clk_set_rate(&tx_clk, rate);
667 static int macb_phy_init(struct udevice *dev, const char *name)
669 struct macb_device *macb = dev_get_priv(dev);
671 u16 phy_id, status, adv, lpa;
672 int media, speed, duplex;
676 arch_get_mdio_control(name);
677 /* If port is not fixed -> setup PHY */
678 if (!macb_port_is_fixed_link(macb)) {
679 /* Auto-detect phy_addr */
680 ret = macb_phy_find(macb, name);
684 /* Check if the PHY is up to snuff... */
685 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
686 if (phy_id == 0xffff) {
687 printf("%s: No PHY present\n", name);
692 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
693 macb->phy_interface);
695 printf("phy_connect failed\n");
699 phy_config(macb->phydev);
702 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
703 if (!(status & BMSR_LSTATUS)) {
704 /* Try to re-negotiate if we don't have link already. */
705 macb_phy_reset(macb, name);
707 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
708 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
709 if (status & BMSR_LSTATUS) {
711 * Delay a bit after the link is established,
712 * so that the next xfer does not fail
721 if (!(status & BMSR_LSTATUS)) {
722 printf("%s: link down (status: 0x%04x)\n",
727 /* First check for GMAC and that it is GiB capable */
728 if (gem_is_gigabit_capable(macb)) {
729 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
731 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
733 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
736 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
738 duplex ? "full" : "half",
741 ncfgr = macb_readl(macb, NCFGR);
742 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
743 ncfgr |= GEM_BIT(GBE);
746 ncfgr |= MACB_BIT(FD);
748 macb_writel(macb, NCFGR, ncfgr);
750 ret = macb_linkspd_cb(dev, _1000BASET);
758 /* fall back for EMAC checking */
759 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
760 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
761 media = mii_nway_result(lpa & adv);
762 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
764 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
765 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
767 speed ? "100" : "10",
768 duplex ? "full" : "half",
771 /* if macb port is a fixed link */
772 /* TODO : manage gigabit capable processors */
774 duplex = macb->duplex;
775 printf("%s: link up, %sMbps %s-duplex\n",
777 speed ? "100" : "10",
778 duplex ? "full" : "half");
781 ncfgr = macb_readl(macb, NCFGR);
782 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
784 ncfgr |= MACB_BIT(SPD);
785 ret = macb_linkspd_cb(dev, _100BASET);
787 ret = macb_linkspd_cb(dev, _10BASET);
794 ncfgr |= MACB_BIT(FD);
795 macb_writel(macb, NCFGR, ncfgr);
800 static int gmac_init_multi_queues(struct macb_device *macb)
802 int i, num_queues = 1;
806 /* bit 0 is never set but queue 0 always exists */
807 queue_mask = gem_readl(macb, DCFG6) & 0xff;
810 for (i = 1; i < MACB_MAX_QUEUES; i++)
811 if (queue_mask & (1 << i))
814 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
815 macb->dummy_desc->addr = 0;
816 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
817 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
818 paddr = macb->dummy_desc_dma;
820 for (i = 1; i < num_queues; i++) {
821 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
822 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
823 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
824 gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
826 gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
833 static void gmac_configure_dma(struct macb_device *macb)
838 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
839 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
840 dmacfg |= GEM_BF(RXBS, buffer_size);
842 if (macb->config->dma_burst_length)
843 dmacfg = GEM_BFINS(FBLDO,
844 macb->config->dma_burst_length, dmacfg);
846 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
847 dmacfg &= ~GEM_BIT(ENDIA_PKT);
849 if (macb->is_big_endian)
850 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
852 dmacfg &= ~GEM_BIT(ENDIA_DESC);
854 dmacfg &= ~GEM_BIT(ADDR64);
855 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
856 dmacfg |= GEM_BIT(ADDR64);
858 gem_writel(macb, DMACFG, dmacfg);
861 static int _macb_init(struct udevice *dev, const char *name)
863 struct macb_device *macb = dev_get_priv(dev);
864 unsigned int val = 0;
871 * macb_halt should have been called at some point before now,
872 * so we'll assume the controller is idle.
875 /* initialize DMA descriptors */
876 paddr = macb->rx_buffer_dma;
877 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
878 if (i == (MACB_RX_RING_SIZE - 1))
879 paddr |= MACB_BIT(RX_WRAP);
880 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
884 macb->rx_ring[count].ctrl = 0;
885 macb_set_addr(macb, &macb->rx_ring[count], paddr);
886 paddr += macb->rx_buffer_size;
888 macb_flush_ring_desc(macb, RX);
889 macb_flush_rx_buffer(macb);
891 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
892 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
896 macb_set_addr(macb, &macb->tx_ring[count], 0);
897 if (i == (MACB_TX_RING_SIZE - 1))
898 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
901 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
903 macb_flush_ring_desc(macb, TX);
908 macb->next_rx_tail = 0;
910 #ifdef CONFIG_MACB_ZYNQ
911 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
914 macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
915 macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
916 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
917 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
918 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
921 if (macb_is_gem(macb)) {
922 /* Initialize DMA properties */
923 gmac_configure_dma(macb);
924 /* Check the multi queue and initialize the queue for tx */
925 gmac_init_multi_queues(macb);
928 * When the GMAC IP with GE feature, this bit is used to
929 * select interface between RGMII and GMII.
930 * When the GMAC IP without GE feature, this bit is used
931 * to select interface between RMII and MII.
933 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
934 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
935 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
936 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
937 val = macb->config->usrio->rgmii;
938 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
939 val = macb->config->usrio->rmii;
940 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
941 val = macb->config->usrio->mii;
943 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
944 val |= macb->config->usrio->clken;
946 gem_writel(macb, USRIO, val);
948 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
949 unsigned int ncfgr = macb_readl(macb, NCFGR);
951 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
952 macb_writel(macb, NCFGR, ncfgr);
955 /* choose RMII or MII mode. This depends on the board */
956 #ifdef CONFIG_AT91FAMILY
957 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
958 macb_writel(macb, USRIO,
959 macb->config->usrio->rmii |
960 macb->config->usrio->clken);
962 macb_writel(macb, USRIO, macb->config->usrio->clken);
965 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
966 macb_writel(macb, USRIO, 0);
968 macb_writel(macb, USRIO, macb->config->usrio->mii);
972 ret = macb_phy_init(dev, name);
976 /* Enable TX and RX */
977 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
982 static void _macb_halt(struct macb_device *macb)
986 /* Halt the controller and wait for any ongoing transmission to end. */
987 ncr = macb_readl(macb, NCR);
988 ncr |= MACB_BIT(THALT);
989 macb_writel(macb, NCR, ncr);
992 tsr = macb_readl(macb, TSR);
993 } while (tsr & MACB_BIT(TGO));
995 /* Disable TX and RX, and clear statistics */
996 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
999 static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
1004 /* set hardware address */
1005 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
1006 enetaddr[2] << 16 | enetaddr[3] << 24;
1007 macb_writel(macb, SA1B, hwaddr_bottom);
1008 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
1009 macb_writel(macb, SA1T, hwaddr_top);
1013 static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
1016 #if defined(CONFIG_CLK)
1017 unsigned long macb_hz = macb->pclk_rate;
1019 unsigned long macb_hz = get_macb_pclk_rate(id);
1022 if (macb_hz < 20000000)
1023 config = MACB_BF(CLK, MACB_CLK_DIV8);
1024 else if (macb_hz < 40000000)
1025 config = MACB_BF(CLK, MACB_CLK_DIV16);
1026 else if (macb_hz < 80000000)
1027 config = MACB_BF(CLK, MACB_CLK_DIV32);
1029 config = MACB_BF(CLK, MACB_CLK_DIV64);
1034 static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1038 #if defined(CONFIG_CLK)
1039 unsigned long macb_hz = macb->pclk_rate;
1041 unsigned long macb_hz = get_macb_pclk_rate(id);
1044 if (macb_hz < 20000000)
1045 config = GEM_BF(CLK, GEM_CLK_DIV8);
1046 else if (macb_hz < 40000000)
1047 config = GEM_BF(CLK, GEM_CLK_DIV16);
1048 else if (macb_hz < 80000000)
1049 config = GEM_BF(CLK, GEM_CLK_DIV32);
1050 else if (macb_hz < 120000000)
1051 config = GEM_BF(CLK, GEM_CLK_DIV48);
1052 else if (macb_hz < 160000000)
1053 config = GEM_BF(CLK, GEM_CLK_DIV64);
1054 else if (macb_hz < 240000000)
1055 config = GEM_BF(CLK, GEM_CLK_DIV96);
1056 else if (macb_hz < 320000000)
1057 config = GEM_BF(CLK, GEM_CLK_DIV128);
1059 config = GEM_BF(CLK, GEM_CLK_DIV224);
1065 * Get the DMA bus width field of the network configuration register that we
1066 * should program. We find the width from decoding the design configuration
1067 * register to find the maximum supported data bus width.
1069 static u32 macb_dbw(struct macb_device *macb)
1071 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1073 return GEM_BF(DBW, GEM_DBW128);
1075 return GEM_BF(DBW, GEM_DBW64);
1078 return GEM_BF(DBW, GEM_DBW32);
1082 static void _macb_eth_initialize(struct macb_device *macb)
1084 int id = 0; /* This is not used by functions we call */
1087 if (macb_is_gem(macb))
1088 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1090 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1092 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
1093 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1095 &macb->rx_buffer_dma);
1096 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1097 &macb->rx_ring_dma);
1098 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1099 &macb->tx_ring_dma);
1100 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1101 &macb->dummy_desc_dma);
1104 * Do some basic initialization so that we at least can talk
1107 if (macb_is_gem(macb)) {
1108 ncfgr = gem_mdc_clk_div(id, macb);
1109 ncfgr |= macb_dbw(macb);
1111 ncfgr = macb_mdc_clk_div(id, macb);
1114 macb_writel(macb, NCFGR, ncfgr);
1117 static int macb_start(struct udevice *dev)
1119 return _macb_init(dev, dev->name);
1122 static int macb_send(struct udevice *dev, void *packet, int length)
1124 struct macb_device *macb = dev_get_priv(dev);
1126 return _macb_send(macb, dev->name, packet, length);
1129 static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1131 struct macb_device *macb = dev_get_priv(dev);
1133 macb->next_rx_tail = macb->rx_tail;
1134 macb->wrapped = false;
1136 return _macb_recv(macb, packetp);
1139 static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1141 struct macb_device *macb = dev_get_priv(dev);
1143 reclaim_rx_buffers(macb, macb->next_rx_tail);
1148 static void macb_stop(struct udevice *dev)
1150 struct macb_device *macb = dev_get_priv(dev);
1155 static int macb_write_hwaddr(struct udevice *dev)
1157 struct eth_pdata *plat = dev_get_plat(dev);
1158 struct macb_device *macb = dev_get_priv(dev);
1160 return _macb_write_hwaddr(macb, plat->enetaddr);
1163 static const struct eth_ops macb_eth_ops = {
1164 .start = macb_start,
1168 .free_pkt = macb_free_pkt,
1169 .write_hwaddr = macb_write_hwaddr,
1173 static int macb_enable_clk(struct udevice *dev)
1175 struct macb_device *macb = dev_get_priv(dev);
1180 ret = clk_get_by_index(dev, 0, &clk);
1185 * If clock driver didn't support enable or disable then
1186 * we get -ENOSYS from clk_enable(). To handle this, we
1187 * don't fail for ret == -ENOSYS.
1189 ret = clk_enable(&clk);
1190 if (ret && ret != -ENOSYS)
1193 clk_rate = clk_get_rate(&clk);
1197 macb->pclk_rate = clk_rate;
1203 static const struct macb_usrio_cfg macb_default_usrio = {
1204 .mii = MACB_BIT(MII),
1205 .rmii = MACB_BIT(RMII),
1206 .rgmii = GEM_BIT(RGMII),
1207 .clken = MACB_BIT(CLKEN),
1210 static struct macb_config default_gem_config = {
1211 .dma_burst_length = 16,
1212 .hw_dma_cap = HW_DMA_CAP_32B,
1214 .usrio = &macb_default_usrio,
1217 static int macb_eth_probe(struct udevice *dev)
1219 struct eth_pdata *pdata = dev_get_plat(dev);
1220 struct macb_device *macb = dev_get_priv(dev);
1221 struct ofnode_phandle_args phandle_args;
1224 macb->phy_interface = dev_read_phy_mode(dev);
1225 if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
1228 /* Read phyaddr from DT */
1229 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1231 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1234 macb->regs = (void *)(uintptr_t)pdata->iobase;
1236 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1238 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1239 if (!macb->config) {
1240 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
1241 if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
1242 default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
1244 macb->config = &default_gem_config;
1248 ret = macb_enable_clk(dev);
1253 _macb_eth_initialize(macb);
1255 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1256 macb->bus = mdio_alloc();
1259 strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1260 macb->bus->read = macb_miiphy_read;
1261 macb->bus->write = macb_miiphy_write;
1263 ret = mdio_register(macb->bus);
1266 macb->bus = miiphy_get_dev_by_name(dev->name);
1272 static int macb_eth_remove(struct udevice *dev)
1274 struct macb_device *macb = dev_get_priv(dev);
1276 #ifdef CONFIG_PHYLIB
1279 mdio_unregister(macb->bus);
1280 mdio_free(macb->bus);
1286 * macb_late_eth_of_to_plat
1287 * @dev: udevice struct
1288 * Returns 0 when operation success and negative errno number
1289 * when operation failed.
1291 int __weak macb_late_eth_of_to_plat(struct udevice *dev)
1296 static int macb_eth_of_to_plat(struct udevice *dev)
1298 struct eth_pdata *pdata = dev_get_plat(dev);
1299 struct macb_device *macb = dev_get_priv(dev);
1300 void *blob = (void *)gd->fdt_blob;
1301 int node = dev_of_offset(dev);
1302 int fl_node, speed_fdt;
1304 /* fetch 'fixed-link' property */
1305 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1307 /* set phy_addr to invalid value for fixed link */
1308 macb->phy_addr = PHY_MAX_ADDR + 1;
1309 macb->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1310 speed_fdt = fdtdec_get_int(blob, fl_node, "speed", 0);
1311 if (speed_fdt == 100) {
1313 } else if (speed_fdt == 10) {
1316 printf("%s: The given speed %d of ethernet in the DT is not supported\n",
1317 __func__, speed_fdt);
1322 pdata->iobase = (uintptr_t)dev_remap_addr(dev);
1326 return macb_late_eth_of_to_plat(dev);
1329 static const struct macb_usrio_cfg sama7g5_usrio = {
1336 static const struct macb_config sama5d4_config = {
1337 .dma_burst_length = 4,
1338 .hw_dma_cap = HW_DMA_CAP_32B,
1340 .usrio = &macb_default_usrio,
1343 static const struct macb_config sifive_config = {
1344 .dma_burst_length = 16,
1345 .hw_dma_cap = HW_DMA_CAP_32B,
1346 .clk_init = macb_sifive_clk_init,
1347 .usrio = &macb_default_usrio,
1350 static const struct macb_config sama7g5_gmac_config = {
1351 .dma_burst_length = 16,
1352 .hw_dma_cap = HW_DMA_CAP_32B,
1353 .clk_init = macb_sama7g5_clk_init,
1354 .usrio = &sama7g5_usrio,
1357 static const struct macb_config sama7g5_emac_config = {
1358 .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1359 .dma_burst_length = 16,
1360 .hw_dma_cap = HW_DMA_CAP_32B,
1361 .usrio = &sama7g5_usrio,
1364 static const struct udevice_id macb_eth_ids[] = {
1365 { .compatible = "cdns,macb" },
1366 { .compatible = "cdns,at91sam9260-macb" },
1367 { .compatible = "cdns,sam9x60-macb" },
1368 { .compatible = "cdns,sama7g5-gem",
1369 .data = (ulong)&sama7g5_gmac_config },
1370 { .compatible = "cdns,sama7g5-emac",
1371 .data = (ulong)&sama7g5_emac_config },
1372 { .compatible = "atmel,sama5d2-gem" },
1373 { .compatible = "atmel,sama5d3-gem" },
1374 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
1375 { .compatible = "cdns,zynq-gem" },
1376 { .compatible = "sifive,fu540-c000-gem",
1377 .data = (ulong)&sifive_config },
1381 U_BOOT_DRIVER(eth_macb) = {
1384 .of_match = macb_eth_ids,
1385 .of_to_plat = macb_eth_of_to_plat,
1386 .probe = macb_eth_probe,
1387 .remove = macb_eth_remove,
1388 .ops = &macb_eth_ops,
1389 .priv_auto = sizeof(struct macb_device),
1390 .plat_auto = sizeof(struct eth_pdata),