1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2005-2006 Atmel Corporation
11 * The u-boot networking stack is a little weird. It seems like the
12 * networking core allocates receive buffers up front without any
13 * regard to the hardware that's supposed to actually receive those
16 * The MACB receives packets into 128-byte receive buffers, so the
17 * buffers allocated by the core isn't very practical to use. We'll
18 * allocate our own, but we need one such buffer in case a packet
19 * wraps around the DMA ring so that we have to copy it.
21 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
22 * configuration header. This way, the core allocates one RX buffer
23 * and one TX buffer, each of which can hold a ethernet packet of
26 * For some reason, the networking core unconditionally specifies a
27 * 32-byte packet "alignment" (which really should be called
28 * "padding"). MACB shouldn't need that, but we'll refrain from any
29 * 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 #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
85 #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
86 #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
87 #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
89 #define RXBUF_FRMLEN_MASK 0x00000fff
90 #define TXBUF_FRMLEN_MASK 0x000007ff
97 const struct macb_config *config;
100 unsigned int tx_head;
101 unsigned int tx_tail;
102 unsigned int next_rx_tail;
107 struct macb_dma_desc *rx_ring;
108 struct macb_dma_desc *tx_ring;
109 size_t rx_buffer_size;
111 unsigned long rx_buffer_dma;
112 unsigned long rx_ring_dma;
113 unsigned long tx_ring_dma;
115 struct macb_dma_desc *dummy_desc;
116 unsigned long dummy_desc_dma;
118 const struct device *dev;
119 #ifndef CONFIG_DM_ETH
120 struct eth_device netdev;
122 unsigned short phy_addr;
125 struct phy_device *phydev;
130 unsigned long pclk_rate;
132 phy_interface_t phy_interface;
137 unsigned int dma_burst_length;
139 int (*clk_init)(struct udevice *dev, ulong rate);
142 #ifndef CONFIG_DM_ETH
143 #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
146 static int macb_is_gem(struct macb_device *macb)
148 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
151 #ifndef cpu_is_sama5d2
152 #define cpu_is_sama5d2() 0
155 #ifndef cpu_is_sama5d4
156 #define cpu_is_sama5d4() 0
159 static int gem_is_gigabit_capable(struct macb_device *macb)
162 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
163 * configured to support only 10/100.
165 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
168 static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
171 unsigned long netctl;
172 unsigned long netstat;
175 netctl = macb_readl(macb, NCR);
176 netctl |= MACB_BIT(MPE);
177 macb_writel(macb, NCR, netctl);
179 frame = (MACB_BF(SOF, 1)
181 | MACB_BF(PHYA, phy_adr)
184 | MACB_BF(DATA, value));
185 macb_writel(macb, MAN, frame);
188 netstat = macb_readl(macb, NSR);
189 } while (!(netstat & MACB_BIT(IDLE)));
191 netctl = macb_readl(macb, NCR);
192 netctl &= ~MACB_BIT(MPE);
193 macb_writel(macb, NCR, netctl);
196 static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
198 unsigned long netctl;
199 unsigned long netstat;
202 netctl = macb_readl(macb, NCR);
203 netctl |= MACB_BIT(MPE);
204 macb_writel(macb, NCR, netctl);
206 frame = (MACB_BF(SOF, 1)
208 | MACB_BF(PHYA, phy_adr)
211 macb_writel(macb, MAN, frame);
214 netstat = macb_readl(macb, NSR);
215 } while (!(netstat & MACB_BIT(IDLE)));
217 frame = macb_readl(macb, MAN);
219 netctl = macb_readl(macb, NCR);
220 netctl &= ~MACB_BIT(MPE);
221 macb_writel(macb, NCR, netctl);
223 return MACB_BFEXT(DATA, frame);
226 void __weak arch_get_mdio_control(const char *name)
231 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
233 int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
237 struct udevice *dev = eth_get_dev_by_name(bus->name);
238 struct macb_device *macb = dev_get_priv(dev);
240 struct eth_device *dev = eth_get_dev_by_name(bus->name);
241 struct macb_device *macb = to_macb(dev);
244 arch_get_mdio_control(bus->name);
245 value = macb_mdio_read(macb, phy_adr, reg);
250 int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
254 struct udevice *dev = eth_get_dev_by_name(bus->name);
255 struct macb_device *macb = dev_get_priv(dev);
257 struct eth_device *dev = eth_get_dev_by_name(bus->name);
258 struct macb_device *macb = to_macb(dev);
261 arch_get_mdio_control(bus->name);
262 macb_mdio_write(macb, phy_adr, reg, value);
270 static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
273 invalidate_dcache_range(macb->rx_ring_dma,
274 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
277 invalidate_dcache_range(macb->tx_ring_dma,
278 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
282 static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
285 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
286 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
288 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
289 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
292 static inline void macb_flush_rx_buffer(struct macb_device *macb)
294 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
295 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
299 static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
301 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
302 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
306 #if defined(CONFIG_CMD_NET)
308 static int _macb_send(struct macb_device *macb, const char *name, void *packet,
311 unsigned long paddr, ctrl;
312 unsigned int tx_head = macb->tx_head;
315 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
317 ctrl = length & TXBUF_FRMLEN_MASK;
318 ctrl |= MACB_BIT(TX_LAST);
319 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
320 ctrl |= MACB_BIT(TX_WRAP);
326 macb->tx_ring[tx_head].ctrl = ctrl;
327 macb->tx_ring[tx_head].addr = paddr;
329 macb_flush_ring_desc(macb, TX);
330 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
333 * I guess this is necessary because the networking core may
334 * re-use the transmit buffer as soon as we return...
336 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
338 macb_invalidate_ring_desc(macb, TX);
339 ctrl = macb->tx_ring[tx_head].ctrl;
340 if (ctrl & MACB_BIT(TX_USED))
345 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
347 if (i <= MACB_TX_TIMEOUT) {
348 if (ctrl & MACB_BIT(TX_UNDERRUN))
349 printf("%s: TX underrun\n", name);
350 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
351 printf("%s: TX buffers exhausted in mid frame\n", name);
353 printf("%s: TX timeout\n", name);
356 /* No one cares anyway */
360 static void reclaim_rx_buffers(struct macb_device *macb,
361 unsigned int new_tail)
367 macb_invalidate_ring_desc(macb, RX);
368 while (i > new_tail) {
369 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
371 if (i > MACB_RX_RING_SIZE)
375 while (i < new_tail) {
376 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
381 macb_flush_ring_desc(macb, RX);
382 macb->rx_tail = new_tail;
385 static int _macb_recv(struct macb_device *macb, uchar **packetp)
387 unsigned int next_rx_tail = macb->next_rx_tail;
392 macb->wrapped = false;
394 macb_invalidate_ring_desc(macb, RX);
396 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
399 status = macb->rx_ring[next_rx_tail].ctrl;
400 if (status & MACB_BIT(RX_SOF)) {
401 if (next_rx_tail != macb->rx_tail)
402 reclaim_rx_buffers(macb, next_rx_tail);
403 macb->wrapped = false;
406 if (status & MACB_BIT(RX_EOF)) {
407 buffer = macb->rx_buffer +
408 macb->rx_buffer_size * macb->rx_tail;
409 length = status & RXBUF_FRMLEN_MASK;
411 macb_invalidate_rx_buffer(macb);
413 unsigned int headlen, taillen;
415 headlen = macb->rx_buffer_size *
416 (MACB_RX_RING_SIZE - macb->rx_tail);
417 taillen = length - headlen;
418 memcpy((void *)net_rx_packets[0],
420 memcpy((void *)net_rx_packets[0] + headlen,
421 macb->rx_buffer, taillen);
422 *packetp = (void *)net_rx_packets[0];
427 if (++next_rx_tail >= MACB_RX_RING_SIZE)
429 macb->next_rx_tail = next_rx_tail;
432 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
433 macb->wrapped = true;
441 static void macb_phy_reset(struct macb_device *macb, const char *name)
446 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
447 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
448 printf("%s: Starting autonegotiation...\n", name);
449 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
452 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
453 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
454 if (status & BMSR_ANEGCOMPLETE)
459 if (status & BMSR_ANEGCOMPLETE)
460 printf("%s: Autonegotiation complete\n", name);
462 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
466 static int macb_phy_find(struct macb_device *macb, const char *name)
471 /* Search for PHY... */
472 for (i = 0; i < 32; i++) {
474 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
475 if (phy_id != 0xffff) {
476 printf("%s: PHY present at %d\n", name, i);
481 /* PHY isn't up to snuff */
482 printf("%s: PHY not found\n", name);
488 * macb_linkspd_cb - Linkspeed change callback function
489 * @dev/@regs: MACB udevice (DM version) or
490 * Base Register of MACB devices (non-DM version)
492 * Returns 0 when operation success and negative errno number
493 * when operation failed.
496 static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
501 addr = dev_read_addr_index(dev, 1);
502 if (addr == FDT_ADDR_T_NONE)
505 gemgxl_regs = (void __iomem *)addr;
510 * SiFive GEMGXL TX clock operation mode:
512 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
513 * and output clock on GMII output signal GTX_CLK
514 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
516 writel(rate != 125000000, gemgxl_regs);
520 int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
523 struct macb_device *macb = dev_get_priv(dev);
530 rate = 2500000; /* 2.5 MHz */
533 rate = 25000000; /* 25 MHz */
536 rate = 125000000; /* 125 MHz */
539 /* does not change anything */
543 if (macb->config->clk_init)
544 return macb->config->clk_init(dev, rate);
547 * "tx_clk" is an optional clock source for MACB.
548 * Ignore if it does not exist in DT.
550 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
555 ret = clk_set_rate(&tx_clk, rate);
564 int __weak macb_linkspd_cb(void *regs, unsigned int speed)
571 static int macb_phy_init(struct udevice *dev, const char *name)
573 static int macb_phy_init(struct macb_device *macb, const char *name)
577 struct macb_device *macb = dev_get_priv(dev);
580 u16 phy_id, status, adv, lpa;
581 int media, speed, duplex;
585 arch_get_mdio_control(name);
586 /* Auto-detect phy_addr */
587 ret = macb_phy_find(macb, name);
591 /* Check if the PHY is up to snuff... */
592 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
593 if (phy_id == 0xffff) {
594 printf("%s: No PHY present\n", name);
600 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
601 macb->phy_interface);
603 /* need to consider other phy interface mode */
604 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
605 PHY_INTERFACE_MODE_RGMII);
608 printf("phy_connect failed\n");
612 phy_config(macb->phydev);
615 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
616 if (!(status & BMSR_LSTATUS)) {
617 /* Try to re-negotiate if we don't have link already. */
618 macb_phy_reset(macb, name);
620 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
621 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
622 if (status & BMSR_LSTATUS) {
624 * Delay a bit after the link is established,
625 * so that the next xfer does not fail
634 if (!(status & BMSR_LSTATUS)) {
635 printf("%s: link down (status: 0x%04x)\n",
640 /* First check for GMAC and that it is GiB capable */
641 if (gem_is_gigabit_capable(macb)) {
642 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
644 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
646 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
649 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
651 duplex ? "full" : "half",
654 ncfgr = macb_readl(macb, NCFGR);
655 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
656 ncfgr |= GEM_BIT(GBE);
659 ncfgr |= MACB_BIT(FD);
661 macb_writel(macb, NCFGR, ncfgr);
664 ret = macb_linkspd_cb(dev, _1000BASET);
666 ret = macb_linkspd_cb(macb->regs, _1000BASET);
675 /* fall back for EMAC checking */
676 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
677 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
678 media = mii_nway_result(lpa & adv);
679 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
681 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
682 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
684 speed ? "100" : "10",
685 duplex ? "full" : "half",
688 ncfgr = macb_readl(macb, NCFGR);
689 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
691 ncfgr |= MACB_BIT(SPD);
693 ret = macb_linkspd_cb(dev, _100BASET);
695 ret = macb_linkspd_cb(macb->regs, _100BASET);
699 ret = macb_linkspd_cb(dev, _10BASET);
701 ret = macb_linkspd_cb(macb->regs, _10BASET);
709 ncfgr |= MACB_BIT(FD);
710 macb_writel(macb, NCFGR, ncfgr);
715 static int gmac_init_multi_queues(struct macb_device *macb)
717 int i, num_queues = 1;
720 /* bit 0 is never set but queue 0 always exists */
721 queue_mask = gem_readl(macb, DCFG6) & 0xff;
724 for (i = 1; i < MACB_MAX_QUEUES; i++)
725 if (queue_mask & (1 << i))
728 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
729 macb->dummy_desc->addr = 0;
730 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
731 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
733 for (i = 1; i < num_queues; i++)
734 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
739 static void gmac_configure_dma(struct macb_device *macb)
744 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
745 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
746 dmacfg |= GEM_BF(RXBS, buffer_size);
748 if (macb->config->dma_burst_length)
749 dmacfg = GEM_BFINS(FBLDO,
750 macb->config->dma_burst_length, dmacfg);
752 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
753 dmacfg &= ~GEM_BIT(ENDIA_PKT);
755 if (macb->is_big_endian)
756 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
758 dmacfg &= ~GEM_BIT(ENDIA_DESC);
760 dmacfg &= ~GEM_BIT(ADDR64);
761 gem_writel(macb, DMACFG, dmacfg);
765 static int _macb_init(struct udevice *dev, const char *name)
767 static int _macb_init(struct macb_device *macb, const char *name)
771 struct macb_device *macb = dev_get_priv(dev);
778 * macb_halt should have been called at some point before now,
779 * so we'll assume the controller is idle.
782 /* initialize DMA descriptors */
783 paddr = macb->rx_buffer_dma;
784 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
785 if (i == (MACB_RX_RING_SIZE - 1))
786 paddr |= MACB_BIT(RX_WRAP);
787 macb->rx_ring[i].addr = paddr;
788 macb->rx_ring[i].ctrl = 0;
789 paddr += macb->rx_buffer_size;
791 macb_flush_ring_desc(macb, RX);
792 macb_flush_rx_buffer(macb);
794 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
795 macb->tx_ring[i].addr = 0;
796 if (i == (MACB_TX_RING_SIZE - 1))
797 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
800 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
802 macb_flush_ring_desc(macb, TX);
807 macb->next_rx_tail = 0;
809 #ifdef CONFIG_MACB_ZYNQ
810 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
813 macb_writel(macb, RBQP, macb->rx_ring_dma);
814 macb_writel(macb, TBQP, macb->tx_ring_dma);
816 if (macb_is_gem(macb)) {
817 /* Initialize DMA properties */
818 gmac_configure_dma(macb);
819 /* Check the multi queue and initialize the queue for tx */
820 gmac_init_multi_queues(macb);
823 * When the GMAC IP with GE feature, this bit is used to
824 * select interface between RGMII and GMII.
825 * When the GMAC IP without GE feature, this bit is used
826 * to select interface between RMII and MII.
829 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
830 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
831 gem_writel(macb, USRIO, GEM_BIT(RGMII));
833 gem_writel(macb, USRIO, 0);
835 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
836 unsigned int ncfgr = macb_readl(macb, NCFGR);
838 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
839 macb_writel(macb, NCFGR, ncfgr);
842 #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
843 gem_writel(macb, USRIO, GEM_BIT(RGMII));
845 gem_writel(macb, USRIO, 0);
849 /* choose RMII or MII mode. This depends on the board */
851 #ifdef CONFIG_AT91FAMILY
852 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
853 macb_writel(macb, USRIO,
854 MACB_BIT(RMII) | MACB_BIT(CLKEN));
856 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
859 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
860 macb_writel(macb, USRIO, 0);
862 macb_writel(macb, USRIO, MACB_BIT(MII));
866 #ifdef CONFIG_AT91FAMILY
867 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
869 macb_writel(macb, USRIO, 0);
872 #ifdef CONFIG_AT91FAMILY
873 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
875 macb_writel(macb, USRIO, MACB_BIT(MII));
877 #endif /* CONFIG_RMII */
882 ret = macb_phy_init(dev, name);
884 ret = macb_phy_init(macb, name);
889 /* Enable TX and RX */
890 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
895 static void _macb_halt(struct macb_device *macb)
899 /* Halt the controller and wait for any ongoing transmission to end. */
900 ncr = macb_readl(macb, NCR);
901 ncr |= MACB_BIT(THALT);
902 macb_writel(macb, NCR, ncr);
905 tsr = macb_readl(macb, TSR);
906 } while (tsr & MACB_BIT(TGO));
908 /* Disable TX and RX, and clear statistics */
909 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
912 static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
917 /* set hardware address */
918 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
919 enetaddr[2] << 16 | enetaddr[3] << 24;
920 macb_writel(macb, SA1B, hwaddr_bottom);
921 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
922 macb_writel(macb, SA1T, hwaddr_top);
926 static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
929 #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
930 unsigned long macb_hz = macb->pclk_rate;
932 unsigned long macb_hz = get_macb_pclk_rate(id);
935 if (macb_hz < 20000000)
936 config = MACB_BF(CLK, MACB_CLK_DIV8);
937 else if (macb_hz < 40000000)
938 config = MACB_BF(CLK, MACB_CLK_DIV16);
939 else if (macb_hz < 80000000)
940 config = MACB_BF(CLK, MACB_CLK_DIV32);
942 config = MACB_BF(CLK, MACB_CLK_DIV64);
947 static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
951 #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
952 unsigned long macb_hz = macb->pclk_rate;
954 unsigned long macb_hz = get_macb_pclk_rate(id);
957 if (macb_hz < 20000000)
958 config = GEM_BF(CLK, GEM_CLK_DIV8);
959 else if (macb_hz < 40000000)
960 config = GEM_BF(CLK, GEM_CLK_DIV16);
961 else if (macb_hz < 80000000)
962 config = GEM_BF(CLK, GEM_CLK_DIV32);
963 else if (macb_hz < 120000000)
964 config = GEM_BF(CLK, GEM_CLK_DIV48);
965 else if (macb_hz < 160000000)
966 config = GEM_BF(CLK, GEM_CLK_DIV64);
967 else if (macb_hz < 240000000)
968 config = GEM_BF(CLK, GEM_CLK_DIV96);
969 else if (macb_hz < 320000000)
970 config = GEM_BF(CLK, GEM_CLK_DIV128);
972 config = GEM_BF(CLK, GEM_CLK_DIV224);
978 * Get the DMA bus width field of the network configuration register that we
979 * should program. We find the width from decoding the design configuration
980 * register to find the maximum supported data bus width.
982 static u32 macb_dbw(struct macb_device *macb)
984 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
986 return GEM_BF(DBW, GEM_DBW128);
988 return GEM_BF(DBW, GEM_DBW64);
991 return GEM_BF(DBW, GEM_DBW32);
995 static void _macb_eth_initialize(struct macb_device *macb)
997 int id = 0; /* This is not used by functions we call */
1000 if (macb_is_gem(macb))
1001 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1003 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1005 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
1006 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1008 &macb->rx_buffer_dma);
1009 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1010 &macb->rx_ring_dma);
1011 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1012 &macb->tx_ring_dma);
1013 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1014 &macb->dummy_desc_dma);
1017 * Do some basic initialization so that we at least can talk
1020 if (macb_is_gem(macb)) {
1021 ncfgr = gem_mdc_clk_div(id, macb);
1022 ncfgr |= macb_dbw(macb);
1024 ncfgr = macb_mdc_clk_div(id, macb);
1027 macb_writel(macb, NCFGR, ncfgr);
1030 #ifndef CONFIG_DM_ETH
1031 static int macb_send(struct eth_device *netdev, void *packet, int length)
1033 struct macb_device *macb = to_macb(netdev);
1035 return _macb_send(macb, netdev->name, packet, length);
1038 static int macb_recv(struct eth_device *netdev)
1040 struct macb_device *macb = to_macb(netdev);
1044 macb->wrapped = false;
1046 macb->next_rx_tail = macb->rx_tail;
1047 length = _macb_recv(macb, &packet);
1049 net_process_received_packet(packet, length);
1050 reclaim_rx_buffers(macb, macb->next_rx_tail);
1057 static int macb_init(struct eth_device *netdev, bd_t *bd)
1059 struct macb_device *macb = to_macb(netdev);
1061 return _macb_init(macb, netdev->name);
1064 static void macb_halt(struct eth_device *netdev)
1066 struct macb_device *macb = to_macb(netdev);
1068 return _macb_halt(macb);
1071 static int macb_write_hwaddr(struct eth_device *netdev)
1073 struct macb_device *macb = to_macb(netdev);
1075 return _macb_write_hwaddr(macb, netdev->enetaddr);
1078 int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1080 struct macb_device *macb;
1081 struct eth_device *netdev;
1083 macb = malloc(sizeof(struct macb_device));
1085 printf("Error: Failed to allocate memory for MACB%d\n", id);
1088 memset(macb, 0, sizeof(struct macb_device));
1090 netdev = &macb->netdev;
1093 macb->phy_addr = phy_addr;
1095 if (macb_is_gem(macb))
1096 sprintf(netdev->name, "gmac%d", id);
1098 sprintf(netdev->name, "macb%d", id);
1100 netdev->init = macb_init;
1101 netdev->halt = macb_halt;
1102 netdev->send = macb_send;
1103 netdev->recv = macb_recv;
1104 netdev->write_hwaddr = macb_write_hwaddr;
1106 _macb_eth_initialize(macb);
1108 eth_register(netdev);
1110 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1112 struct mii_dev *mdiodev = mdio_alloc();
1115 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1116 mdiodev->read = macb_miiphy_read;
1117 mdiodev->write = macb_miiphy_write;
1119 retval = mdio_register(mdiodev);
1122 macb->bus = miiphy_get_dev_by_name(netdev->name);
1126 #endif /* !CONFIG_DM_ETH */
1128 #ifdef CONFIG_DM_ETH
1130 static int macb_start(struct udevice *dev)
1132 return _macb_init(dev, dev->name);
1135 static int macb_send(struct udevice *dev, void *packet, int length)
1137 struct macb_device *macb = dev_get_priv(dev);
1139 return _macb_send(macb, dev->name, packet, length);
1142 static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1144 struct macb_device *macb = dev_get_priv(dev);
1146 macb->next_rx_tail = macb->rx_tail;
1147 macb->wrapped = false;
1149 return _macb_recv(macb, packetp);
1152 static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1154 struct macb_device *macb = dev_get_priv(dev);
1156 reclaim_rx_buffers(macb, macb->next_rx_tail);
1161 static void macb_stop(struct udevice *dev)
1163 struct macb_device *macb = dev_get_priv(dev);
1168 static int macb_write_hwaddr(struct udevice *dev)
1170 struct eth_pdata *plat = dev_get_platdata(dev);
1171 struct macb_device *macb = dev_get_priv(dev);
1173 return _macb_write_hwaddr(macb, plat->enetaddr);
1176 static const struct eth_ops macb_eth_ops = {
1177 .start = macb_start,
1181 .free_pkt = macb_free_pkt,
1182 .write_hwaddr = macb_write_hwaddr,
1186 static int macb_enable_clk(struct udevice *dev)
1188 struct macb_device *macb = dev_get_priv(dev);
1193 ret = clk_get_by_index(dev, 0, &clk);
1198 * If clock driver didn't support enable or disable then
1199 * we get -ENOSYS from clk_enable(). To handle this, we
1200 * don't fail for ret == -ENOSYS.
1202 ret = clk_enable(&clk);
1203 if (ret && ret != -ENOSYS)
1206 clk_rate = clk_get_rate(&clk);
1210 macb->pclk_rate = clk_rate;
1216 static const struct macb_config default_gem_config = {
1217 .dma_burst_length = 16,
1221 static int macb_eth_probe(struct udevice *dev)
1223 struct eth_pdata *pdata = dev_get_platdata(dev);
1224 struct macb_device *macb = dev_get_priv(dev);
1225 const char *phy_mode;
1228 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1231 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1232 if (macb->phy_interface == -1) {
1233 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1237 macb->regs = (void *)pdata->iobase;
1239 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1241 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1243 macb->config = &default_gem_config;
1246 ret = macb_enable_clk(dev);
1251 _macb_eth_initialize(macb);
1253 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1254 macb->bus = mdio_alloc();
1257 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1258 macb->bus->read = macb_miiphy_read;
1259 macb->bus->write = macb_miiphy_write;
1261 ret = mdio_register(macb->bus);
1264 macb->bus = miiphy_get_dev_by_name(dev->name);
1270 static int macb_eth_remove(struct udevice *dev)
1272 struct macb_device *macb = dev_get_priv(dev);
1274 #ifdef CONFIG_PHYLIB
1277 mdio_unregister(macb->bus);
1278 mdio_free(macb->bus);
1284 * macb_late_eth_ofdata_to_platdata
1285 * @dev: udevice struct
1286 * Returns 0 when operation success and negative errno number
1287 * when operation failed.
1289 int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1294 static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1296 struct eth_pdata *pdata = dev_get_platdata(dev);
1298 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1302 return macb_late_eth_ofdata_to_platdata(dev);
1305 static const struct macb_config sama5d4_config = {
1306 .dma_burst_length = 4,
1310 static const struct macb_config sifive_config = {
1311 .dma_burst_length = 16,
1312 .clk_init = macb_sifive_clk_init,
1315 static const struct udevice_id macb_eth_ids[] = {
1316 { .compatible = "cdns,macb" },
1317 { .compatible = "cdns,at91sam9260-macb" },
1318 { .compatible = "cdns,sam9x60-macb" },
1319 { .compatible = "atmel,sama5d2-gem" },
1320 { .compatible = "atmel,sama5d3-gem" },
1321 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
1322 { .compatible = "cdns,zynq-gem" },
1323 { .compatible = "sifive,fu540-c000-gem",
1324 .data = (ulong)&sifive_config },
1328 U_BOOT_DRIVER(eth_macb) = {
1331 .of_match = macb_eth_ids,
1332 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1333 .probe = macb_eth_probe,
1334 .remove = macb_eth_remove,
1335 .ops = &macb_eth_ops,
1336 .priv_auto_alloc_size = sizeof(struct macb_device),
1337 .platdata_auto_alloc_size = sizeof(struct eth_pdata),