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;
131 unsigned short phy_addr;
134 struct phy_device *phydev;
138 unsigned long pclk_rate;
140 phy_interface_t phy_interface;
143 struct macb_usrio_cfg {
151 unsigned int dma_burst_length;
152 unsigned int hw_dma_cap;
155 int (*clk_init)(struct udevice *dev, ulong rate);
156 const struct macb_usrio_cfg *usrio;
159 static int macb_is_gem(struct macb_device *macb)
161 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
164 #ifndef cpu_is_sama5d2
165 #define cpu_is_sama5d2() 0
168 #ifndef cpu_is_sama5d4
169 #define cpu_is_sama5d4() 0
172 static int gem_is_gigabit_capable(struct macb_device *macb)
175 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
176 * configured to support only 10/100.
178 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
181 static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
184 unsigned long netctl;
185 unsigned long netstat;
188 netctl = macb_readl(macb, NCR);
189 netctl |= MACB_BIT(MPE);
190 macb_writel(macb, NCR, netctl);
192 frame = (MACB_BF(SOF, 1)
194 | MACB_BF(PHYA, phy_adr)
197 | MACB_BF(DATA, value));
198 macb_writel(macb, MAN, frame);
201 netstat = macb_readl(macb, NSR);
202 } while (!(netstat & MACB_BIT(IDLE)));
204 netctl = macb_readl(macb, NCR);
205 netctl &= ~MACB_BIT(MPE);
206 macb_writel(macb, NCR, netctl);
209 static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
211 unsigned long netctl;
212 unsigned long netstat;
215 netctl = macb_readl(macb, NCR);
216 netctl |= MACB_BIT(MPE);
217 macb_writel(macb, NCR, netctl);
219 frame = (MACB_BF(SOF, 1)
221 | MACB_BF(PHYA, phy_adr)
224 macb_writel(macb, MAN, frame);
227 netstat = macb_readl(macb, NSR);
228 } while (!(netstat & MACB_BIT(IDLE)));
230 frame = macb_readl(macb, MAN);
232 netctl = macb_readl(macb, NCR);
233 netctl &= ~MACB_BIT(MPE);
234 macb_writel(macb, NCR, netctl);
236 return MACB_BFEXT(DATA, frame);
239 void __weak arch_get_mdio_control(const char *name)
244 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
246 int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
249 struct udevice *dev = eth_get_dev_by_name(bus->name);
250 struct macb_device *macb = dev_get_priv(dev);
252 arch_get_mdio_control(bus->name);
253 value = macb_mdio_read(macb, phy_adr, reg);
258 int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
261 struct udevice *dev = eth_get_dev_by_name(bus->name);
262 struct macb_device *macb = dev_get_priv(dev);
264 arch_get_mdio_control(bus->name);
265 macb_mdio_write(macb, phy_adr, reg, value);
273 static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
276 invalidate_dcache_range(macb->rx_ring_dma,
277 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
280 invalidate_dcache_range(macb->tx_ring_dma,
281 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
285 static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
288 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
289 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
291 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
292 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
295 static inline void macb_flush_rx_buffer(struct macb_device *macb)
297 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
298 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
302 static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
304 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
305 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
309 #if defined(CONFIG_CMD_NET)
311 static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
313 return (struct macb_dma_desc_64 *)((void *)desc
314 + sizeof(struct macb_dma_desc));
317 static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
320 struct macb_dma_desc_64 *desc_64;
322 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
323 desc_64 = macb_64b_desc(desc);
324 desc_64->addrh = upper_32_bits(addr);
326 desc->addr = lower_32_bits(addr);
329 static int _macb_send(struct macb_device *macb, const char *name, void *packet,
332 unsigned long paddr, ctrl;
333 unsigned int tx_head = macb->tx_head;
336 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
338 ctrl = length & TXBUF_FRMLEN_MASK;
339 ctrl |= MACB_BIT(TX_LAST);
340 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
341 ctrl |= MACB_BIT(TX_WRAP);
347 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
348 tx_head = tx_head * 2;
350 macb->tx_ring[tx_head].ctrl = ctrl;
351 macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
354 macb_flush_ring_desc(macb, TX);
355 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
358 * I guess this is necessary because the networking core may
359 * re-use the transmit buffer as soon as we return...
361 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
363 macb_invalidate_ring_desc(macb, TX);
364 ctrl = macb->tx_ring[tx_head].ctrl;
365 if (ctrl & MACB_BIT(TX_USED))
370 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
372 if (i <= MACB_TX_TIMEOUT) {
373 if (ctrl & MACB_BIT(TX_UNDERRUN))
374 printf("%s: TX underrun\n", name);
375 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
376 printf("%s: TX buffers exhausted in mid frame\n", name);
378 printf("%s: TX timeout\n", name);
381 /* No one cares anyway */
385 static void reclaim_rx_buffer(struct macb_device *macb,
393 * There may be multiple descriptors per CPU cacheline,
394 * so a cache flush would flush the whole line, meaning the content of other descriptors
395 * in the cacheline would also flush. If one of the other descriptors had been
396 * written to by the controller, the flush would cause those changes to be lost.
398 * To circumvent this issue, we do the actual freeing only when we need to free
399 * the last descriptor in the current cacheline. When the current descriptor is the
400 * last in the cacheline, we free all the descriptors that belong to that cacheline.
402 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
403 mask = DESC_PER_CACHELINE_64 - 1;
406 mask = DESC_PER_CACHELINE_32 - 1;
410 /* we exit without freeing if idx is not the last descriptor in the cacheline */
411 if ((idx & mask) != mask)
414 for (i = idx & (~mask); i <= idx; i++)
415 macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
418 static void reclaim_rx_buffers(struct macb_device *macb,
419 unsigned int new_tail)
425 macb_invalidate_ring_desc(macb, RX);
426 while (i > new_tail) {
427 reclaim_rx_buffer(macb, i);
429 if (i >= MACB_RX_RING_SIZE)
433 while (i < new_tail) {
434 reclaim_rx_buffer(macb, i);
439 macb_flush_ring_desc(macb, RX);
440 macb->rx_tail = new_tail;
443 static int _macb_recv(struct macb_device *macb, uchar **packetp)
445 unsigned int next_rx_tail = macb->next_rx_tail;
451 macb->wrapped = false;
453 macb_invalidate_ring_desc(macb, RX);
455 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
456 next_rx_tail = next_rx_tail * 2;
458 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
461 status = macb->rx_ring[next_rx_tail].ctrl;
462 if (status & MACB_BIT(RX_SOF)) {
463 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
464 next_rx_tail = next_rx_tail / 2;
468 if (next_rx_tail != macb->rx_tail)
469 reclaim_rx_buffers(macb, next_rx_tail);
470 macb->wrapped = false;
473 if (status & MACB_BIT(RX_EOF)) {
474 buffer = macb->rx_buffer +
475 macb->rx_buffer_size * macb->rx_tail;
476 length = status & RXBUF_FRMLEN_MASK;
478 macb_invalidate_rx_buffer(macb);
480 unsigned int headlen, taillen;
482 headlen = macb->rx_buffer_size *
483 (MACB_RX_RING_SIZE - macb->rx_tail);
484 taillen = length - headlen;
485 memcpy((void *)net_rx_packets[0],
487 memcpy((void *)net_rx_packets[0] + headlen,
488 macb->rx_buffer, taillen);
489 *packetp = (void *)net_rx_packets[0];
494 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
496 next_rx_tail = next_rx_tail / 2;
499 if (++next_rx_tail >= MACB_RX_RING_SIZE)
501 macb->next_rx_tail = next_rx_tail;
504 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
506 next_rx_tail = next_rx_tail / 2;
510 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
511 macb->wrapped = true;
519 static void macb_phy_reset(struct macb_device *macb, const char *name)
524 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
525 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
526 printf("%s: Starting autonegotiation...\n", name);
527 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
530 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
531 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
532 if (status & BMSR_ANEGCOMPLETE)
537 if (status & BMSR_ANEGCOMPLETE)
538 printf("%s: Autonegotiation complete\n", name);
540 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
544 static int macb_phy_find(struct macb_device *macb, const char *name)
549 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
550 if (phy_id != 0xffff) {
551 printf("%s: PHY present at %d\n", name, macb->phy_addr);
555 /* Search for PHY... */
556 for (i = 0; i < 32; i++) {
558 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
559 if (phy_id != 0xffff) {
560 printf("%s: PHY present at %d\n", name, i);
565 /* PHY isn't up to snuff */
566 printf("%s: PHY not found\n", name);
572 * macb_linkspd_cb - Linkspeed change callback function
573 * @dev/@regs: MACB udevice (DM version) or
574 * Base Register of MACB devices (non-DM version)
576 * Returns 0 when operation success and negative errno number
577 * when operation failed.
579 static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
583 gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
588 * SiFive GEMGXL TX clock operation mode:
590 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
591 * and output clock on GMII output signal GTX_CLK
592 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
594 writel(rate != 125000000, gemgxl_regs);
598 static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
603 ret = clk_get_by_name(dev, "tx_clk", &clk);
608 * This is for using GCK. Clock rate is addressed via assigned-clock
609 * property, so only clock enable is needed here. The switching to
610 * proper clock rate depending on link speed is managed by IP logic.
612 return clk_enable(&clk);
615 int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
618 struct macb_device *macb = dev_get_priv(dev);
625 rate = 2500000; /* 2.5 MHz */
628 rate = 25000000; /* 25 MHz */
631 rate = 125000000; /* 125 MHz */
634 /* does not change anything */
638 if (macb->config->clk_init)
639 return macb->config->clk_init(dev, rate);
642 * "tx_clk" is an optional clock source for MACB.
643 * Ignore if it does not exist in DT.
645 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
650 ret = clk_set_rate(&tx_clk, rate);
659 static int macb_phy_init(struct udevice *dev, const char *name)
661 struct macb_device *macb = dev_get_priv(dev);
663 u16 phy_id, status, adv, lpa;
664 int media, speed, duplex;
668 arch_get_mdio_control(name);
669 /* Auto-detect phy_addr */
670 ret = macb_phy_find(macb, name);
674 /* Check if the PHY is up to snuff... */
675 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
676 if (phy_id == 0xffff) {
677 printf("%s: No PHY present\n", name);
682 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
683 macb->phy_interface);
685 printf("phy_connect failed\n");
689 phy_config(macb->phydev);
692 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
693 if (!(status & BMSR_LSTATUS)) {
694 /* Try to re-negotiate if we don't have link already. */
695 macb_phy_reset(macb, name);
697 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
698 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
699 if (status & BMSR_LSTATUS) {
701 * Delay a bit after the link is established,
702 * so that the next xfer does not fail
711 if (!(status & BMSR_LSTATUS)) {
712 printf("%s: link down (status: 0x%04x)\n",
717 /* First check for GMAC and that it is GiB capable */
718 if (gem_is_gigabit_capable(macb)) {
719 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
721 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
723 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
726 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
728 duplex ? "full" : "half",
731 ncfgr = macb_readl(macb, NCFGR);
732 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
733 ncfgr |= GEM_BIT(GBE);
736 ncfgr |= MACB_BIT(FD);
738 macb_writel(macb, NCFGR, ncfgr);
740 ret = macb_linkspd_cb(dev, _1000BASET);
748 /* fall back for EMAC checking */
749 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
750 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
751 media = mii_nway_result(lpa & adv);
752 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
754 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
755 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
757 speed ? "100" : "10",
758 duplex ? "full" : "half",
761 ncfgr = macb_readl(macb, NCFGR);
762 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
764 ncfgr |= MACB_BIT(SPD);
765 ret = macb_linkspd_cb(dev, _100BASET);
767 ret = macb_linkspd_cb(dev, _10BASET);
774 ncfgr |= MACB_BIT(FD);
775 macb_writel(macb, NCFGR, ncfgr);
780 static int gmac_init_multi_queues(struct macb_device *macb)
782 int i, num_queues = 1;
786 /* bit 0 is never set but queue 0 always exists */
787 queue_mask = gem_readl(macb, DCFG6) & 0xff;
790 for (i = 1; i < MACB_MAX_QUEUES; i++)
791 if (queue_mask & (1 << i))
794 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
795 macb->dummy_desc->addr = 0;
796 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
797 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
798 paddr = macb->dummy_desc_dma;
800 for (i = 1; i < num_queues; i++) {
801 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
802 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
803 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
804 gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
806 gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
813 static void gmac_configure_dma(struct macb_device *macb)
818 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
819 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
820 dmacfg |= GEM_BF(RXBS, buffer_size);
822 if (macb->config->dma_burst_length)
823 dmacfg = GEM_BFINS(FBLDO,
824 macb->config->dma_burst_length, dmacfg);
826 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
827 dmacfg &= ~GEM_BIT(ENDIA_PKT);
829 if (macb->is_big_endian)
830 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
832 dmacfg &= ~GEM_BIT(ENDIA_DESC);
834 dmacfg &= ~GEM_BIT(ADDR64);
835 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
836 dmacfg |= GEM_BIT(ADDR64);
838 gem_writel(macb, DMACFG, dmacfg);
841 static int _macb_init(struct udevice *dev, const char *name)
843 struct macb_device *macb = dev_get_priv(dev);
844 unsigned int val = 0;
851 * macb_halt should have been called at some point before now,
852 * so we'll assume the controller is idle.
855 /* initialize DMA descriptors */
856 paddr = macb->rx_buffer_dma;
857 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
858 if (i == (MACB_RX_RING_SIZE - 1))
859 paddr |= MACB_BIT(RX_WRAP);
860 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
864 macb->rx_ring[count].ctrl = 0;
865 macb_set_addr(macb, &macb->rx_ring[count], paddr);
866 paddr += macb->rx_buffer_size;
868 macb_flush_ring_desc(macb, RX);
869 macb_flush_rx_buffer(macb);
871 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
872 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
876 macb_set_addr(macb, &macb->tx_ring[count], 0);
877 if (i == (MACB_TX_RING_SIZE - 1))
878 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
881 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
883 macb_flush_ring_desc(macb, TX);
888 macb->next_rx_tail = 0;
890 #ifdef CONFIG_MACB_ZYNQ
891 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
894 macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
895 macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
896 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
897 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
898 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
901 if (macb_is_gem(macb)) {
902 /* Initialize DMA properties */
903 gmac_configure_dma(macb);
904 /* Check the multi queue and initialize the queue for tx */
905 gmac_init_multi_queues(macb);
908 * When the GMAC IP with GE feature, this bit is used to
909 * select interface between RGMII and GMII.
910 * When the GMAC IP without GE feature, this bit is used
911 * to select interface between RMII and MII.
913 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
914 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
915 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
916 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
917 val = macb->config->usrio->rgmii;
918 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
919 val = macb->config->usrio->rmii;
920 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
921 val = macb->config->usrio->mii;
923 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
924 val |= macb->config->usrio->clken;
926 gem_writel(macb, USRIO, val);
928 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
929 unsigned int ncfgr = macb_readl(macb, NCFGR);
931 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
932 macb_writel(macb, NCFGR, ncfgr);
935 /* choose RMII or MII mode. This depends on the board */
936 #ifdef CONFIG_AT91FAMILY
937 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
938 macb_writel(macb, USRIO,
939 macb->config->usrio->rmii |
940 macb->config->usrio->clken);
942 macb_writel(macb, USRIO, macb->config->usrio->clken);
945 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
946 macb_writel(macb, USRIO, 0);
948 macb_writel(macb, USRIO, macb->config->usrio->mii);
952 ret = macb_phy_init(dev, name);
956 /* Enable TX and RX */
957 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
962 static void _macb_halt(struct macb_device *macb)
966 /* Halt the controller and wait for any ongoing transmission to end. */
967 ncr = macb_readl(macb, NCR);
968 ncr |= MACB_BIT(THALT);
969 macb_writel(macb, NCR, ncr);
972 tsr = macb_readl(macb, TSR);
973 } while (tsr & MACB_BIT(TGO));
975 /* Disable TX and RX, and clear statistics */
976 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
979 static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
984 /* set hardware address */
985 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
986 enetaddr[2] << 16 | enetaddr[3] << 24;
987 macb_writel(macb, SA1B, hwaddr_bottom);
988 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
989 macb_writel(macb, SA1T, hwaddr_top);
993 static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
996 #if defined(CONFIG_CLK)
997 unsigned long macb_hz = macb->pclk_rate;
999 unsigned long macb_hz = get_macb_pclk_rate(id);
1002 if (macb_hz < 20000000)
1003 config = MACB_BF(CLK, MACB_CLK_DIV8);
1004 else if (macb_hz < 40000000)
1005 config = MACB_BF(CLK, MACB_CLK_DIV16);
1006 else if (macb_hz < 80000000)
1007 config = MACB_BF(CLK, MACB_CLK_DIV32);
1009 config = MACB_BF(CLK, MACB_CLK_DIV64);
1014 static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1018 #if defined(CONFIG_CLK)
1019 unsigned long macb_hz = macb->pclk_rate;
1021 unsigned long macb_hz = get_macb_pclk_rate(id);
1024 if (macb_hz < 20000000)
1025 config = GEM_BF(CLK, GEM_CLK_DIV8);
1026 else if (macb_hz < 40000000)
1027 config = GEM_BF(CLK, GEM_CLK_DIV16);
1028 else if (macb_hz < 80000000)
1029 config = GEM_BF(CLK, GEM_CLK_DIV32);
1030 else if (macb_hz < 120000000)
1031 config = GEM_BF(CLK, GEM_CLK_DIV48);
1032 else if (macb_hz < 160000000)
1033 config = GEM_BF(CLK, GEM_CLK_DIV64);
1034 else if (macb_hz < 240000000)
1035 config = GEM_BF(CLK, GEM_CLK_DIV96);
1036 else if (macb_hz < 320000000)
1037 config = GEM_BF(CLK, GEM_CLK_DIV128);
1039 config = GEM_BF(CLK, GEM_CLK_DIV224);
1045 * Get the DMA bus width field of the network configuration register that we
1046 * should program. We find the width from decoding the design configuration
1047 * register to find the maximum supported data bus width.
1049 static u32 macb_dbw(struct macb_device *macb)
1051 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1053 return GEM_BF(DBW, GEM_DBW128);
1055 return GEM_BF(DBW, GEM_DBW64);
1058 return GEM_BF(DBW, GEM_DBW32);
1062 static void _macb_eth_initialize(struct macb_device *macb)
1064 int id = 0; /* This is not used by functions we call */
1067 if (macb_is_gem(macb))
1068 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1070 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1072 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
1073 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1075 &macb->rx_buffer_dma);
1076 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1077 &macb->rx_ring_dma);
1078 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1079 &macb->tx_ring_dma);
1080 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1081 &macb->dummy_desc_dma);
1084 * Do some basic initialization so that we at least can talk
1087 if (macb_is_gem(macb)) {
1088 ncfgr = gem_mdc_clk_div(id, macb);
1089 ncfgr |= macb_dbw(macb);
1091 ncfgr = macb_mdc_clk_div(id, macb);
1094 macb_writel(macb, NCFGR, ncfgr);
1097 static int macb_start(struct udevice *dev)
1099 return _macb_init(dev, dev->name);
1102 static int macb_send(struct udevice *dev, void *packet, int length)
1104 struct macb_device *macb = dev_get_priv(dev);
1106 return _macb_send(macb, dev->name, packet, length);
1109 static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1111 struct macb_device *macb = dev_get_priv(dev);
1113 macb->next_rx_tail = macb->rx_tail;
1114 macb->wrapped = false;
1116 return _macb_recv(macb, packetp);
1119 static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1121 struct macb_device *macb = dev_get_priv(dev);
1123 reclaim_rx_buffers(macb, macb->next_rx_tail);
1128 static void macb_stop(struct udevice *dev)
1130 struct macb_device *macb = dev_get_priv(dev);
1135 static int macb_write_hwaddr(struct udevice *dev)
1137 struct eth_pdata *plat = dev_get_plat(dev);
1138 struct macb_device *macb = dev_get_priv(dev);
1140 return _macb_write_hwaddr(macb, plat->enetaddr);
1143 static const struct eth_ops macb_eth_ops = {
1144 .start = macb_start,
1148 .free_pkt = macb_free_pkt,
1149 .write_hwaddr = macb_write_hwaddr,
1153 static int macb_enable_clk(struct udevice *dev)
1155 struct macb_device *macb = dev_get_priv(dev);
1160 ret = clk_get_by_index(dev, 0, &clk);
1165 * If clock driver didn't support enable or disable then
1166 * we get -ENOSYS from clk_enable(). To handle this, we
1167 * don't fail for ret == -ENOSYS.
1169 ret = clk_enable(&clk);
1170 if (ret && ret != -ENOSYS)
1173 clk_rate = clk_get_rate(&clk);
1177 macb->pclk_rate = clk_rate;
1183 static const struct macb_usrio_cfg macb_default_usrio = {
1184 .mii = MACB_BIT(MII),
1185 .rmii = MACB_BIT(RMII),
1186 .rgmii = GEM_BIT(RGMII),
1187 .clken = MACB_BIT(CLKEN),
1190 static struct macb_config default_gem_config = {
1191 .dma_burst_length = 16,
1192 .hw_dma_cap = HW_DMA_CAP_32B,
1194 .usrio = &macb_default_usrio,
1197 static int macb_eth_probe(struct udevice *dev)
1199 struct eth_pdata *pdata = dev_get_plat(dev);
1200 struct macb_device *macb = dev_get_priv(dev);
1201 struct ofnode_phandle_args phandle_args;
1204 macb->phy_interface = dev_read_phy_mode(dev);
1205 if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
1208 /* Read phyaddr from DT */
1209 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1211 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1214 macb->regs = (void *)(uintptr_t)pdata->iobase;
1216 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1218 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1219 if (!macb->config) {
1220 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
1221 if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
1222 default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
1224 macb->config = &default_gem_config;
1228 ret = macb_enable_clk(dev);
1233 _macb_eth_initialize(macb);
1235 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1236 macb->bus = mdio_alloc();
1239 strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1240 macb->bus->read = macb_miiphy_read;
1241 macb->bus->write = macb_miiphy_write;
1243 ret = mdio_register(macb->bus);
1246 macb->bus = miiphy_get_dev_by_name(dev->name);
1252 static int macb_eth_remove(struct udevice *dev)
1254 struct macb_device *macb = dev_get_priv(dev);
1256 #ifdef CONFIG_PHYLIB
1259 mdio_unregister(macb->bus);
1260 mdio_free(macb->bus);
1266 * macb_late_eth_of_to_plat
1267 * @dev: udevice struct
1268 * Returns 0 when operation success and negative errno number
1269 * when operation failed.
1271 int __weak macb_late_eth_of_to_plat(struct udevice *dev)
1276 static int macb_eth_of_to_plat(struct udevice *dev)
1278 struct eth_pdata *pdata = dev_get_plat(dev);
1280 pdata->iobase = (uintptr_t)dev_remap_addr(dev);
1284 return macb_late_eth_of_to_plat(dev);
1287 static const struct macb_usrio_cfg sama7g5_usrio = {
1294 static const struct macb_config sama5d4_config = {
1295 .dma_burst_length = 4,
1296 .hw_dma_cap = HW_DMA_CAP_32B,
1298 .usrio = &macb_default_usrio,
1301 static const struct macb_config sifive_config = {
1302 .dma_burst_length = 16,
1303 .hw_dma_cap = HW_DMA_CAP_32B,
1304 .clk_init = macb_sifive_clk_init,
1305 .usrio = &macb_default_usrio,
1308 static const struct macb_config sama7g5_gmac_config = {
1309 .dma_burst_length = 16,
1310 .hw_dma_cap = HW_DMA_CAP_32B,
1311 .clk_init = macb_sama7g5_clk_init,
1312 .usrio = &sama7g5_usrio,
1315 static const struct macb_config sama7g5_emac_config = {
1316 .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1317 .dma_burst_length = 16,
1318 .hw_dma_cap = HW_DMA_CAP_32B,
1319 .usrio = &sama7g5_usrio,
1322 static const struct udevice_id macb_eth_ids[] = {
1323 { .compatible = "cdns,macb" },
1324 { .compatible = "cdns,at91sam9260-macb" },
1325 { .compatible = "cdns,sam9x60-macb" },
1326 { .compatible = "cdns,sama7g5-gem",
1327 .data = (ulong)&sama7g5_gmac_config },
1328 { .compatible = "cdns,sama7g5-emac",
1329 .data = (ulong)&sama7g5_emac_config },
1330 { .compatible = "atmel,sama5d2-gem" },
1331 { .compatible = "atmel,sama5d3-gem" },
1332 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
1333 { .compatible = "cdns,zynq-gem" },
1334 { .compatible = "sifive,fu540-c000-gem",
1335 .data = (ulong)&sifive_config },
1339 U_BOOT_DRIVER(eth_macb) = {
1342 .of_match = macb_eth_ids,
1343 .of_to_plat = macb_eth_of_to_plat,
1344 .probe = macb_eth_probe,
1345 .remove = macb_eth_remove,
1346 .ops = &macb_eth_ops,
1347 .priv_auto = sizeof(struct macb_device),
1348 .plat_auto = sizeof(struct eth_pdata),