2 * Copyright (C) 2005-2006 Atmel Corporation
4 * SPDX-License-Identifier: GPL-2.0+
9 * The u-boot networking stack is a little weird. It seems like the
10 * networking core allocates receive buffers up front without any
11 * regard to the hardware that's supposed to actually receive those
14 * The MACB receives packets into 128-byte receive buffers, so the
15 * buffers allocated by the core isn't very practical to use. We'll
16 * allocate our own, but we need one such buffer in case a packet
17 * wraps around the DMA ring so that we have to copy it.
19 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
20 * configuration header. This way, the core allocates one RX buffer
21 * and one TX buffer, each of which can hold a ethernet packet of
24 * For some reason, the networking core unconditionally specifies a
25 * 32-byte packet "alignment" (which really should be called
26 * "padding"). MACB shouldn't need that, but we'll refrain from any
27 * core modifications here...
35 #include <linux/mii.h>
37 #include <asm/dma-mapping.h>
38 #include <asm/arch/clk.h>
39 #include <asm-generic/errno.h>
43 #define MACB_RX_BUFFER_SIZE 4096
44 #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
45 #define MACB_TX_RING_SIZE 16
46 #define MACB_TX_TIMEOUT 1000
47 #define MACB_AUTONEG_TIMEOUT 5000000
49 struct macb_dma_desc {
54 #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
55 #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
56 #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
57 #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
59 #define RXADDR_USED 0x00000001
60 #define RXADDR_WRAP 0x00000002
62 #define RXBUF_FRMLEN_MASK 0x00000fff
63 #define RXBUF_FRAME_START 0x00004000
64 #define RXBUF_FRAME_END 0x00008000
65 #define RXBUF_TYPEID_MATCH 0x00400000
66 #define RXBUF_ADDR4_MATCH 0x00800000
67 #define RXBUF_ADDR3_MATCH 0x01000000
68 #define RXBUF_ADDR2_MATCH 0x02000000
69 #define RXBUF_ADDR1_MATCH 0x04000000
70 #define RXBUF_BROADCAST 0x80000000
72 #define TXBUF_FRMLEN_MASK 0x000007ff
73 #define TXBUF_FRAME_END 0x00008000
74 #define TXBUF_NOCRC 0x00010000
75 #define TXBUF_EXHAUSTED 0x08000000
76 #define TXBUF_UNDERRUN 0x10000000
77 #define TXBUF_MAXRETRY 0x20000000
78 #define TXBUF_WRAP 0x40000000
79 #define TXBUF_USED 0x80000000
90 struct macb_dma_desc *rx_ring;
91 struct macb_dma_desc *tx_ring;
93 unsigned long rx_buffer_dma;
94 unsigned long rx_ring_dma;
95 unsigned long tx_ring_dma;
97 struct macb_dma_desc *dummy_desc;
98 unsigned long dummy_desc_dma;
100 const struct device *dev;
101 struct eth_device netdev;
102 unsigned short phy_addr;
105 #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
107 static int macb_is_gem(struct macb_device *macb)
109 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
112 static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
114 unsigned long netctl;
115 unsigned long netstat;
118 netctl = macb_readl(macb, NCR);
119 netctl |= MACB_BIT(MPE);
120 macb_writel(macb, NCR, netctl);
122 frame = (MACB_BF(SOF, 1)
124 | MACB_BF(PHYA, macb->phy_addr)
127 | MACB_BF(DATA, value));
128 macb_writel(macb, MAN, frame);
131 netstat = macb_readl(macb, NSR);
132 } while (!(netstat & MACB_BIT(IDLE)));
134 netctl = macb_readl(macb, NCR);
135 netctl &= ~MACB_BIT(MPE);
136 macb_writel(macb, NCR, netctl);
139 static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
141 unsigned long netctl;
142 unsigned long netstat;
145 netctl = macb_readl(macb, NCR);
146 netctl |= MACB_BIT(MPE);
147 macb_writel(macb, NCR, netctl);
149 frame = (MACB_BF(SOF, 1)
151 | MACB_BF(PHYA, macb->phy_addr)
154 macb_writel(macb, MAN, frame);
157 netstat = macb_readl(macb, NSR);
158 } while (!(netstat & MACB_BIT(IDLE)));
160 frame = macb_readl(macb, MAN);
162 netctl = macb_readl(macb, NCR);
163 netctl &= ~MACB_BIT(MPE);
164 macb_writel(macb, NCR, netctl);
166 return MACB_BFEXT(DATA, frame);
169 void __weak arch_get_mdio_control(const char *name)
174 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
176 int macb_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
178 struct eth_device *dev = eth_get_dev_by_name(devname);
179 struct macb_device *macb = to_macb(dev);
181 if (macb->phy_addr != phy_adr)
184 arch_get_mdio_control(devname);
185 *value = macb_mdio_read(macb, reg);
190 int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
192 struct eth_device *dev = eth_get_dev_by_name(devname);
193 struct macb_device *macb = to_macb(dev);
195 if (macb->phy_addr != phy_adr)
198 arch_get_mdio_control(devname);
199 macb_mdio_write(macb, reg, value);
207 static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
210 invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
211 MACB_RX_DMA_DESC_SIZE);
213 invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
214 MACB_TX_DMA_DESC_SIZE);
217 static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
220 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
221 MACB_RX_DMA_DESC_SIZE);
223 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
224 MACB_TX_DMA_DESC_SIZE);
227 static inline void macb_flush_rx_buffer(struct macb_device *macb)
229 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
230 MACB_RX_BUFFER_SIZE);
233 static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
235 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
236 MACB_RX_BUFFER_SIZE);
239 #if defined(CONFIG_CMD_NET)
241 static int macb_send(struct eth_device *netdev, void *packet, int length)
243 struct macb_device *macb = to_macb(netdev);
244 unsigned long paddr, ctrl;
245 unsigned int tx_head = macb->tx_head;
248 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
250 ctrl = length & TXBUF_FRMLEN_MASK;
251 ctrl |= TXBUF_FRAME_END;
252 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
259 macb->tx_ring[tx_head].ctrl = ctrl;
260 macb->tx_ring[tx_head].addr = paddr;
262 macb_flush_ring_desc(macb, TX);
263 /* Do we need check paddr and length is dcache line aligned? */
264 flush_dcache_range(paddr, paddr + length);
265 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
268 * I guess this is necessary because the networking core may
269 * re-use the transmit buffer as soon as we return...
271 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
273 macb_invalidate_ring_desc(macb, TX);
274 ctrl = macb->tx_ring[tx_head].ctrl;
275 if (ctrl & TXBUF_USED)
280 dma_unmap_single(packet, length, paddr);
282 if (i <= MACB_TX_TIMEOUT) {
283 if (ctrl & TXBUF_UNDERRUN)
284 printf("%s: TX underrun\n", netdev->name);
285 if (ctrl & TXBUF_EXHAUSTED)
286 printf("%s: TX buffers exhausted in mid frame\n",
289 printf("%s: TX timeout\n", netdev->name);
292 /* No one cares anyway */
296 static void reclaim_rx_buffers(struct macb_device *macb,
297 unsigned int new_tail)
303 macb_invalidate_ring_desc(macb, RX);
304 while (i > new_tail) {
305 macb->rx_ring[i].addr &= ~RXADDR_USED;
307 if (i > MACB_RX_RING_SIZE)
311 while (i < new_tail) {
312 macb->rx_ring[i].addr &= ~RXADDR_USED;
317 macb_flush_ring_desc(macb, RX);
318 macb->rx_tail = new_tail;
321 static int macb_recv(struct eth_device *netdev)
323 struct macb_device *macb = to_macb(netdev);
324 unsigned int rx_tail = macb->rx_tail;
331 macb_invalidate_ring_desc(macb, RX);
333 if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
336 status = macb->rx_ring[rx_tail].ctrl;
337 if (status & RXBUF_FRAME_START) {
338 if (rx_tail != macb->rx_tail)
339 reclaim_rx_buffers(macb, rx_tail);
343 if (status & RXBUF_FRAME_END) {
344 buffer = macb->rx_buffer + 128 * macb->rx_tail;
345 length = status & RXBUF_FRMLEN_MASK;
347 macb_invalidate_rx_buffer(macb);
349 unsigned int headlen, taillen;
351 headlen = 128 * (MACB_RX_RING_SIZE
353 taillen = length - headlen;
354 memcpy((void *)net_rx_packets[0],
356 memcpy((void *)net_rx_packets[0] + headlen,
357 macb->rx_buffer, taillen);
358 buffer = (void *)net_rx_packets[0];
361 net_process_received_packet(buffer, length);
362 if (++rx_tail >= MACB_RX_RING_SIZE)
364 reclaim_rx_buffers(macb, rx_tail);
366 if (++rx_tail >= MACB_RX_RING_SIZE) {
377 static void macb_phy_reset(struct macb_device *macb)
379 struct eth_device *netdev = &macb->netdev;
383 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
384 macb_mdio_write(macb, MII_ADVERTISE, adv);
385 printf("%s: Starting autonegotiation...\n", netdev->name);
386 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
389 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
390 status = macb_mdio_read(macb, MII_BMSR);
391 if (status & BMSR_ANEGCOMPLETE)
396 if (status & BMSR_ANEGCOMPLETE)
397 printf("%s: Autonegotiation complete\n", netdev->name);
399 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
400 netdev->name, status);
403 #ifdef CONFIG_MACB_SEARCH_PHY
404 static int macb_phy_find(struct macb_device *macb)
409 /* Search for PHY... */
410 for (i = 0; i < 32; i++) {
412 phy_id = macb_mdio_read(macb, MII_PHYSID1);
413 if (phy_id != 0xffff) {
414 printf("%s: PHY present at %d\n", macb->netdev.name, i);
419 /* PHY isn't up to snuff */
420 printf("%s: PHY not found\n", macb->netdev.name);
424 #endif /* CONFIG_MACB_SEARCH_PHY */
427 #define gbit_phy_support(p) ((p)->supported & \
428 (SUPPORTED_1000baseT_Full | \
429 SUPPORTED_1000baseT_Half))
430 #elif defined(CONFIG_RGMII) || defined(CONFIG_GMII)
431 #define gbit_phy_support(p) 1
433 #define gbit_phy_support(p) 0
436 static int macb_phy_init(struct macb_device *macb)
438 struct eth_device *netdev = &macb->netdev;
440 struct phy_device *phydev;
443 u16 phy_id, status, adv, lpa;
444 int media, speed, duplex;
447 arch_get_mdio_control(netdev->name);
448 #ifdef CONFIG_MACB_SEARCH_PHY
449 /* Auto-detect phy_addr */
450 if (!macb_phy_find(macb))
452 #endif /* CONFIG_MACB_SEARCH_PHY */
454 /* Check if the PHY is up to snuff... */
455 phy_id = macb_mdio_read(macb, MII_PHYSID1);
456 if (phy_id == 0xffff) {
457 printf("%s: No PHY present\n", netdev->name);
462 /* need to consider other phy interface mode */
463 phydev = phy_connect(macb->bus, macb->phy_addr, netdev,
464 PHY_INTERFACE_MODE_RGMII);
466 printf("phy_connect failed\n");
473 status = macb_mdio_read(macb, MII_BMSR);
474 if (!(status & BMSR_LSTATUS)) {
475 /* Try to re-negotiate if we don't have link already. */
476 macb_phy_reset(macb);
478 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
479 status = macb_mdio_read(macb, MII_BMSR);
480 if (status & BMSR_LSTATUS)
486 if (!(status & BMSR_LSTATUS)) {
487 printf("%s: link down (status: 0x%04x)\n",
488 netdev->name, status);
492 /* First check for GMAC */
493 if (macb_is_gem(macb)) {
494 if (gbit_phy_support(phydev)) {
495 lpa = macb_mdio_read(macb, MII_STAT1000);
497 if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
498 duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
500 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
502 duplex ? "full" : "half",
505 ncfgr = macb_readl(macb, NCFGR);
506 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
507 ncfgr |= GEM_BIT(GBE);
510 ncfgr |= MACB_BIT(FD);
512 macb_writel(macb, NCFGR, ncfgr);
519 /* fall back for EMAC checking */
520 adv = macb_mdio_read(macb, MII_ADVERTISE);
521 lpa = macb_mdio_read(macb, MII_LPA);
522 media = mii_nway_result(lpa & adv);
523 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
525 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
526 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
528 speed ? "100" : "10",
529 duplex ? "full" : "half",
532 ncfgr = macb_readl(macb, NCFGR);
533 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
535 ncfgr |= MACB_BIT(SPD);
537 ncfgr |= MACB_BIT(FD);
538 macb_writel(macb, NCFGR, ncfgr);
543 static int gmac_init_multi_queues(struct macb_device *macb)
545 int i, num_queues = 1;
548 /* bit 0 is never set but queue 0 always exists */
549 queue_mask = gem_readl(macb, DCFG6) & 0xff;
552 for (i = 1; i < MACB_MAX_QUEUES; i++)
553 if (queue_mask & (1 << i))
556 macb->dummy_desc->ctrl = TXBUF_USED;
557 macb->dummy_desc->addr = 0;
558 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
559 MACB_TX_DUMMY_DMA_DESC_SIZE);
561 for (i = 1; i < num_queues; i++)
562 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
567 static int macb_init(struct eth_device *netdev, bd_t *bd)
569 struct macb_device *macb = to_macb(netdev);
574 * macb_halt should have been called at some point before now,
575 * so we'll assume the controller is idle.
578 /* initialize DMA descriptors */
579 paddr = macb->rx_buffer_dma;
580 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
581 if (i == (MACB_RX_RING_SIZE - 1))
582 paddr |= RXADDR_WRAP;
583 macb->rx_ring[i].addr = paddr;
584 macb->rx_ring[i].ctrl = 0;
587 macb_flush_ring_desc(macb, RX);
588 macb_flush_rx_buffer(macb);
590 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
591 macb->tx_ring[i].addr = 0;
592 if (i == (MACB_TX_RING_SIZE - 1))
593 macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
595 macb->tx_ring[i].ctrl = TXBUF_USED;
597 macb_flush_ring_desc(macb, TX);
603 macb_writel(macb, RBQP, macb->rx_ring_dma);
604 macb_writel(macb, TBQP, macb->tx_ring_dma);
606 if (macb_is_gem(macb)) {
607 /* Check the multi queue and initialize the queue for tx */
608 gmac_init_multi_queues(macb);
611 * When the GMAC IP with GE feature, this bit is used to
612 * select interface between RGMII and GMII.
613 * When the GMAC IP without GE feature, this bit is used
614 * to select interface between RMII and MII.
616 #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
617 gem_writel(macb, UR, GEM_BIT(RGMII));
619 gem_writel(macb, UR, 0);
622 /* choose RMII or MII mode. This depends on the board */
624 #ifdef CONFIG_AT91FAMILY
625 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
627 macb_writel(macb, USRIO, 0);
630 #ifdef CONFIG_AT91FAMILY
631 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
633 macb_writel(macb, USRIO, MACB_BIT(MII));
635 #endif /* CONFIG_RMII */
638 if (!macb_phy_init(macb))
641 /* Enable TX and RX */
642 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
647 static void macb_halt(struct eth_device *netdev)
649 struct macb_device *macb = to_macb(netdev);
652 /* Halt the controller and wait for any ongoing transmission to end. */
653 ncr = macb_readl(macb, NCR);
654 ncr |= MACB_BIT(THALT);
655 macb_writel(macb, NCR, ncr);
658 tsr = macb_readl(macb, TSR);
659 } while (tsr & MACB_BIT(TGO));
661 /* Disable TX and RX, and clear statistics */
662 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
665 static int macb_write_hwaddr(struct eth_device *dev)
667 struct macb_device *macb = to_macb(dev);
671 /* set hardware address */
672 hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 |
673 dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24;
674 macb_writel(macb, SA1B, hwaddr_bottom);
675 hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8;
676 macb_writel(macb, SA1T, hwaddr_top);
680 static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
683 unsigned long macb_hz = get_macb_pclk_rate(id);
685 if (macb_hz < 20000000)
686 config = MACB_BF(CLK, MACB_CLK_DIV8);
687 else if (macb_hz < 40000000)
688 config = MACB_BF(CLK, MACB_CLK_DIV16);
689 else if (macb_hz < 80000000)
690 config = MACB_BF(CLK, MACB_CLK_DIV32);
692 config = MACB_BF(CLK, MACB_CLK_DIV64);
697 static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
700 unsigned long macb_hz = get_macb_pclk_rate(id);
702 if (macb_hz < 20000000)
703 config = GEM_BF(CLK, GEM_CLK_DIV8);
704 else if (macb_hz < 40000000)
705 config = GEM_BF(CLK, GEM_CLK_DIV16);
706 else if (macb_hz < 80000000)
707 config = GEM_BF(CLK, GEM_CLK_DIV32);
708 else if (macb_hz < 120000000)
709 config = GEM_BF(CLK, GEM_CLK_DIV48);
710 else if (macb_hz < 160000000)
711 config = GEM_BF(CLK, GEM_CLK_DIV64);
713 config = GEM_BF(CLK, GEM_CLK_DIV96);
719 * Get the DMA bus width field of the network configuration register that we
720 * should program. We find the width from decoding the design configuration
721 * register to find the maximum supported data bus width.
723 static u32 macb_dbw(struct macb_device *macb)
725 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
727 return GEM_BF(DBW, GEM_DBW128);
729 return GEM_BF(DBW, GEM_DBW64);
732 return GEM_BF(DBW, GEM_DBW32);
736 int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
738 struct macb_device *macb;
739 struct eth_device *netdev;
742 macb = malloc(sizeof(struct macb_device));
744 printf("Error: Failed to allocate memory for MACB%d\n", id);
747 memset(macb, 0, sizeof(struct macb_device));
749 netdev = &macb->netdev;
751 macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
752 &macb->rx_buffer_dma);
753 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
755 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
757 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
758 &macb->dummy_desc_dma);
760 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
763 macb->phy_addr = phy_addr;
765 if (macb_is_gem(macb))
766 sprintf(netdev->name, "gmac%d", id);
768 sprintf(netdev->name, "macb%d", id);
770 netdev->init = macb_init;
771 netdev->halt = macb_halt;
772 netdev->send = macb_send;
773 netdev->recv = macb_recv;
774 netdev->write_hwaddr = macb_write_hwaddr;
777 * Do some basic initialization so that we at least can talk
780 if (macb_is_gem(macb)) {
781 ncfgr = gem_mdc_clk_div(id, macb);
782 ncfgr |= macb_dbw(macb);
784 ncfgr = macb_mdc_clk_div(id, macb);
787 macb_writel(macb, NCFGR, ncfgr);
789 eth_register(netdev);
791 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
792 miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
793 macb->bus = miiphy_get_dev_by_name(netdev->name);