]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/net/macb.c
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))
58 #define RXADDR_USED 0x00000001
59 #define RXADDR_WRAP 0x00000002
61 #define RXBUF_FRMLEN_MASK 0x00000fff
62 #define RXBUF_FRAME_START 0x00004000
63 #define RXBUF_FRAME_END 0x00008000
64 #define RXBUF_TYPEID_MATCH 0x00400000
65 #define RXBUF_ADDR4_MATCH 0x00800000
66 #define RXBUF_ADDR3_MATCH 0x01000000
67 #define RXBUF_ADDR2_MATCH 0x02000000
68 #define RXBUF_ADDR1_MATCH 0x04000000
69 #define RXBUF_BROADCAST 0x80000000
71 #define TXBUF_FRMLEN_MASK 0x000007ff
72 #define TXBUF_FRAME_END 0x00008000
73 #define TXBUF_NOCRC 0x00010000
74 #define TXBUF_EXHAUSTED 0x08000000
75 #define TXBUF_UNDERRUN 0x10000000
76 #define TXBUF_MAXRETRY 0x20000000
77 #define TXBUF_WRAP 0x40000000
78 #define TXBUF_USED 0x80000000
89 struct macb_dma_desc
*rx_ring
;
90 struct macb_dma_desc
*tx_ring
;
92 unsigned long rx_buffer_dma
;
93 unsigned long rx_ring_dma
;
94 unsigned long tx_ring_dma
;
96 const struct device
*dev
;
97 struct eth_device netdev
;
98 unsigned short phy_addr
;
101 #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
103 static int macb_is_gem(struct macb_device
*macb
)
105 return MACB_BFEXT(IDNUM
, macb_readl(macb
, MID
)) == 0x2;
108 static void macb_mdio_write(struct macb_device
*macb
, u8 reg
, u16 value
)
110 unsigned long netctl
;
111 unsigned long netstat
;
114 netctl
= macb_readl(macb
, NCR
);
115 netctl
|= MACB_BIT(MPE
);
116 macb_writel(macb
, NCR
, netctl
);
118 frame
= (MACB_BF(SOF
, 1)
120 | MACB_BF(PHYA
, macb
->phy_addr
)
123 | MACB_BF(DATA
, value
));
124 macb_writel(macb
, MAN
, frame
);
127 netstat
= macb_readl(macb
, NSR
);
128 } while (!(netstat
& MACB_BIT(IDLE
)));
130 netctl
= macb_readl(macb
, NCR
);
131 netctl
&= ~MACB_BIT(MPE
);
132 macb_writel(macb
, NCR
, netctl
);
135 static u16
macb_mdio_read(struct macb_device
*macb
, u8 reg
)
137 unsigned long netctl
;
138 unsigned long netstat
;
141 netctl
= macb_readl(macb
, NCR
);
142 netctl
|= MACB_BIT(MPE
);
143 macb_writel(macb
, NCR
, netctl
);
145 frame
= (MACB_BF(SOF
, 1)
147 | MACB_BF(PHYA
, macb
->phy_addr
)
150 macb_writel(macb
, MAN
, frame
);
153 netstat
= macb_readl(macb
, NSR
);
154 } while (!(netstat
& MACB_BIT(IDLE
)));
156 frame
= macb_readl(macb
, MAN
);
158 netctl
= macb_readl(macb
, NCR
);
159 netctl
&= ~MACB_BIT(MPE
);
160 macb_writel(macb
, NCR
, netctl
);
162 return MACB_BFEXT(DATA
, frame
);
165 void __weak
arch_get_mdio_control(const char *name
)
170 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
172 int macb_miiphy_read(const char *devname
, u8 phy_adr
, u8 reg
, u16
*value
)
174 struct eth_device
*dev
= eth_get_dev_by_name(devname
);
175 struct macb_device
*macb
= to_macb(dev
);
177 if (macb
->phy_addr
!= phy_adr
)
180 arch_get_mdio_control(devname
);
181 *value
= macb_mdio_read(macb
, reg
);
186 int macb_miiphy_write(const char *devname
, u8 phy_adr
, u8 reg
, u16 value
)
188 struct eth_device
*dev
= eth_get_dev_by_name(devname
);
189 struct macb_device
*macb
= to_macb(dev
);
191 if (macb
->phy_addr
!= phy_adr
)
194 arch_get_mdio_control(devname
);
195 macb_mdio_write(macb
, reg
, value
);
203 static inline void macb_invalidate_ring_desc(struct macb_device
*macb
, bool rx
)
206 invalidate_dcache_range(macb
->rx_ring_dma
, macb
->rx_ring_dma
+
207 MACB_RX_DMA_DESC_SIZE
);
209 invalidate_dcache_range(macb
->tx_ring_dma
, macb
->tx_ring_dma
+
210 MACB_TX_DMA_DESC_SIZE
);
213 static inline void macb_flush_ring_desc(struct macb_device
*macb
, bool rx
)
216 flush_dcache_range(macb
->rx_ring_dma
, macb
->rx_ring_dma
+
217 MACB_RX_DMA_DESC_SIZE
);
219 flush_dcache_range(macb
->tx_ring_dma
, macb
->tx_ring_dma
+
220 MACB_TX_DMA_DESC_SIZE
);
223 static inline void macb_flush_rx_buffer(struct macb_device
*macb
)
225 flush_dcache_range(macb
->rx_buffer_dma
, macb
->rx_buffer_dma
+
226 MACB_RX_BUFFER_SIZE
);
229 static inline void macb_invalidate_rx_buffer(struct macb_device
*macb
)
231 invalidate_dcache_range(macb
->rx_buffer_dma
, macb
->rx_buffer_dma
+
232 MACB_RX_BUFFER_SIZE
);
235 #if defined(CONFIG_CMD_NET)
237 static int macb_send(struct eth_device
*netdev
, void *packet
, int length
)
239 struct macb_device
*macb
= to_macb(netdev
);
240 unsigned long paddr
, ctrl
;
241 unsigned int tx_head
= macb
->tx_head
;
244 paddr
= dma_map_single(packet
, length
, DMA_TO_DEVICE
);
246 ctrl
= length
& TXBUF_FRMLEN_MASK
;
247 ctrl
|= TXBUF_FRAME_END
;
248 if (tx_head
== (MACB_TX_RING_SIZE
- 1)) {
255 macb
->tx_ring
[tx_head
].ctrl
= ctrl
;
256 macb
->tx_ring
[tx_head
].addr
= paddr
;
258 macb_flush_ring_desc(macb
, TX
);
259 /* Do we need check paddr and length is dcache line aligned? */
260 flush_dcache_range(paddr
, paddr
+ length
);
261 macb_writel(macb
, NCR
, MACB_BIT(TE
) | MACB_BIT(RE
) | MACB_BIT(TSTART
));
264 * I guess this is necessary because the networking core may
265 * re-use the transmit buffer as soon as we return...
267 for (i
= 0; i
<= MACB_TX_TIMEOUT
; i
++) {
269 macb_invalidate_ring_desc(macb
, TX
);
270 ctrl
= macb
->tx_ring
[tx_head
].ctrl
;
271 if (ctrl
& TXBUF_USED
)
276 dma_unmap_single(packet
, length
, paddr
);
278 if (i
<= MACB_TX_TIMEOUT
) {
279 if (ctrl
& TXBUF_UNDERRUN
)
280 printf("%s: TX underrun\n", netdev
->name
);
281 if (ctrl
& TXBUF_EXHAUSTED
)
282 printf("%s: TX buffers exhausted in mid frame\n",
285 printf("%s: TX timeout\n", netdev
->name
);
288 /* No one cares anyway */
292 static void reclaim_rx_buffers(struct macb_device
*macb
,
293 unsigned int new_tail
)
299 macb_invalidate_ring_desc(macb
, RX
);
300 while (i
> new_tail
) {
301 macb
->rx_ring
[i
].addr
&= ~RXADDR_USED
;
303 if (i
> MACB_RX_RING_SIZE
)
307 while (i
< new_tail
) {
308 macb
->rx_ring
[i
].addr
&= ~RXADDR_USED
;
313 macb_flush_ring_desc(macb
, RX
);
314 macb
->rx_tail
= new_tail
;
317 static int macb_recv(struct eth_device
*netdev
)
319 struct macb_device
*macb
= to_macb(netdev
);
320 unsigned int rx_tail
= macb
->rx_tail
;
327 macb_invalidate_ring_desc(macb
, RX
);
329 if (!(macb
->rx_ring
[rx_tail
].addr
& RXADDR_USED
))
332 status
= macb
->rx_ring
[rx_tail
].ctrl
;
333 if (status
& RXBUF_FRAME_START
) {
334 if (rx_tail
!= macb
->rx_tail
)
335 reclaim_rx_buffers(macb
, rx_tail
);
339 if (status
& RXBUF_FRAME_END
) {
340 buffer
= macb
->rx_buffer
+ 128 * macb
->rx_tail
;
341 length
= status
& RXBUF_FRMLEN_MASK
;
343 macb_invalidate_rx_buffer(macb
);
345 unsigned int headlen
, taillen
;
347 headlen
= 128 * (MACB_RX_RING_SIZE
349 taillen
= length
- headlen
;
350 memcpy((void *)net_rx_packets
[0],
352 memcpy((void *)net_rx_packets
[0] + headlen
,
353 macb
->rx_buffer
, taillen
);
354 buffer
= (void *)net_rx_packets
[0];
357 net_process_received_packet(buffer
, length
);
358 if (++rx_tail
>= MACB_RX_RING_SIZE
)
360 reclaim_rx_buffers(macb
, rx_tail
);
362 if (++rx_tail
>= MACB_RX_RING_SIZE
) {
373 static void macb_phy_reset(struct macb_device
*macb
)
375 struct eth_device
*netdev
= &macb
->netdev
;
379 adv
= ADVERTISE_CSMA
| ADVERTISE_ALL
;
380 macb_mdio_write(macb
, MII_ADVERTISE
, adv
);
381 printf("%s: Starting autonegotiation...\n", netdev
->name
);
382 macb_mdio_write(macb
, MII_BMCR
, (BMCR_ANENABLE
385 for (i
= 0; i
< MACB_AUTONEG_TIMEOUT
/ 100; i
++) {
386 status
= macb_mdio_read(macb
, MII_BMSR
);
387 if (status
& BMSR_ANEGCOMPLETE
)
392 if (status
& BMSR_ANEGCOMPLETE
)
393 printf("%s: Autonegotiation complete\n", netdev
->name
);
395 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
396 netdev
->name
, status
);
399 #ifdef CONFIG_MACB_SEARCH_PHY
400 static int macb_phy_find(struct macb_device
*macb
)
405 /* Search for PHY... */
406 for (i
= 0; i
< 32; i
++) {
408 phy_id
= macb_mdio_read(macb
, MII_PHYSID1
);
409 if (phy_id
!= 0xffff) {
410 printf("%s: PHY present at %d\n", macb
->netdev
.name
, i
);
415 /* PHY isn't up to snuff */
416 printf("%s: PHY not found\n", macb
->netdev
.name
);
420 #endif /* CONFIG_MACB_SEARCH_PHY */
423 static int macb_phy_init(struct macb_device
*macb
)
425 struct eth_device
*netdev
= &macb
->netdev
;
427 struct phy_device
*phydev
;
430 u16 phy_id
, status
, adv
, lpa
;
431 int media
, speed
, duplex
;
434 arch_get_mdio_control(netdev
->name
);
435 #ifdef CONFIG_MACB_SEARCH_PHY
436 /* Auto-detect phy_addr */
437 if (!macb_phy_find(macb
))
439 #endif /* CONFIG_MACB_SEARCH_PHY */
441 /* Check if the PHY is up to snuff... */
442 phy_id
= macb_mdio_read(macb
, MII_PHYSID1
);
443 if (phy_id
== 0xffff) {
444 printf("%s: No PHY present\n", netdev
->name
);
449 /* need to consider other phy interface mode */
450 phydev
= phy_connect(macb
->bus
, macb
->phy_addr
, netdev
,
451 PHY_INTERFACE_MODE_RGMII
);
453 printf("phy_connect failed\n");
460 status
= macb_mdio_read(macb
, MII_BMSR
);
461 if (!(status
& BMSR_LSTATUS
)) {
462 /* Try to re-negotiate if we don't have link already. */
463 macb_phy_reset(macb
);
465 for (i
= 0; i
< MACB_AUTONEG_TIMEOUT
/ 100; i
++) {
466 status
= macb_mdio_read(macb
, MII_BMSR
);
467 if (status
& BMSR_LSTATUS
)
473 if (!(status
& BMSR_LSTATUS
)) {
474 printf("%s: link down (status: 0x%04x)\n",
475 netdev
->name
, status
);
479 /* First check for GMAC */
480 if (macb_is_gem(macb
)) {
481 lpa
= macb_mdio_read(macb
, MII_STAT1000
);
483 if (lpa
& (LPA_1000FULL
| LPA_1000HALF
)) {
484 duplex
= ((lpa
& LPA_1000FULL
) ? 1 : 0);
486 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
488 duplex
? "full" : "half",
491 ncfgr
= macb_readl(macb
, NCFGR
);
492 ncfgr
&= ~(MACB_BIT(SPD
) | MACB_BIT(FD
));
493 ncfgr
|= GEM_BIT(GBE
);
496 ncfgr
|= MACB_BIT(FD
);
498 macb_writel(macb
, NCFGR
, ncfgr
);
504 /* fall back for EMAC checking */
505 adv
= macb_mdio_read(macb
, MII_ADVERTISE
);
506 lpa
= macb_mdio_read(macb
, MII_LPA
);
507 media
= mii_nway_result(lpa
& adv
);
508 speed
= (media
& (ADVERTISE_100FULL
| ADVERTISE_100HALF
)
510 duplex
= (media
& ADVERTISE_FULL
) ? 1 : 0;
511 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
513 speed
? "100" : "10",
514 duplex
? "full" : "half",
517 ncfgr
= macb_readl(macb
, NCFGR
);
518 ncfgr
&= ~(MACB_BIT(SPD
) | MACB_BIT(FD
) | GEM_BIT(GBE
));
520 ncfgr
|= MACB_BIT(SPD
);
522 ncfgr
|= MACB_BIT(FD
);
523 macb_writel(macb
, NCFGR
, ncfgr
);
528 static int macb_init(struct eth_device
*netdev
, bd_t
*bd
)
530 struct macb_device
*macb
= to_macb(netdev
);
535 * macb_halt should have been called at some point before now,
536 * so we'll assume the controller is idle.
539 /* initialize DMA descriptors */
540 paddr
= macb
->rx_buffer_dma
;
541 for (i
= 0; i
< MACB_RX_RING_SIZE
; i
++) {
542 if (i
== (MACB_RX_RING_SIZE
- 1))
543 paddr
|= RXADDR_WRAP
;
544 macb
->rx_ring
[i
].addr
= paddr
;
545 macb
->rx_ring
[i
].ctrl
= 0;
548 macb_flush_ring_desc(macb
, RX
);
549 macb_flush_rx_buffer(macb
);
551 for (i
= 0; i
< MACB_TX_RING_SIZE
; i
++) {
552 macb
->tx_ring
[i
].addr
= 0;
553 if (i
== (MACB_TX_RING_SIZE
- 1))
554 macb
->tx_ring
[i
].ctrl
= TXBUF_USED
| TXBUF_WRAP
;
556 macb
->tx_ring
[i
].ctrl
= TXBUF_USED
;
558 macb_flush_ring_desc(macb
, TX
);
564 macb_writel(macb
, RBQP
, macb
->rx_ring_dma
);
565 macb_writel(macb
, TBQP
, macb
->tx_ring_dma
);
567 if (macb_is_gem(macb
)) {
569 * When the GMAC IP with GE feature, this bit is used to
570 * select interface between RGMII and GMII.
571 * When the GMAC IP without GE feature, this bit is used
572 * to select interface between RMII and MII.
574 #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
575 gem_writel(macb
, UR
, GEM_BIT(RGMII
));
577 gem_writel(macb
, UR
, 0);
580 /* choose RMII or MII mode. This depends on the board */
582 #ifdef CONFIG_AT91FAMILY
583 macb_writel(macb
, USRIO
, MACB_BIT(RMII
) | MACB_BIT(CLKEN
));
585 macb_writel(macb
, USRIO
, 0);
588 #ifdef CONFIG_AT91FAMILY
589 macb_writel(macb
, USRIO
, MACB_BIT(CLKEN
));
591 macb_writel(macb
, USRIO
, MACB_BIT(MII
));
593 #endif /* CONFIG_RMII */
596 if (!macb_phy_init(macb
))
599 /* Enable TX and RX */
600 macb_writel(macb
, NCR
, MACB_BIT(TE
) | MACB_BIT(RE
));
605 static void macb_halt(struct eth_device
*netdev
)
607 struct macb_device
*macb
= to_macb(netdev
);
610 /* Halt the controller and wait for any ongoing transmission to end. */
611 ncr
= macb_readl(macb
, NCR
);
612 ncr
|= MACB_BIT(THALT
);
613 macb_writel(macb
, NCR
, ncr
);
616 tsr
= macb_readl(macb
, TSR
);
617 } while (tsr
& MACB_BIT(TGO
));
619 /* Disable TX and RX, and clear statistics */
620 macb_writel(macb
, NCR
, MACB_BIT(CLRSTAT
));
623 static int macb_write_hwaddr(struct eth_device
*dev
)
625 struct macb_device
*macb
= to_macb(dev
);
629 /* set hardware address */
630 hwaddr_bottom
= dev
->enetaddr
[0] | dev
->enetaddr
[1] << 8 |
631 dev
->enetaddr
[2] << 16 | dev
->enetaddr
[3] << 24;
632 macb_writel(macb
, SA1B
, hwaddr_bottom
);
633 hwaddr_top
= dev
->enetaddr
[4] | dev
->enetaddr
[5] << 8;
634 macb_writel(macb
, SA1T
, hwaddr_top
);
638 static u32
macb_mdc_clk_div(int id
, struct macb_device
*macb
)
641 unsigned long macb_hz
= get_macb_pclk_rate(id
);
643 if (macb_hz
< 20000000)
644 config
= MACB_BF(CLK
, MACB_CLK_DIV8
);
645 else if (macb_hz
< 40000000)
646 config
= MACB_BF(CLK
, MACB_CLK_DIV16
);
647 else if (macb_hz
< 80000000)
648 config
= MACB_BF(CLK
, MACB_CLK_DIV32
);
650 config
= MACB_BF(CLK
, MACB_CLK_DIV64
);
655 static u32
gem_mdc_clk_div(int id
, struct macb_device
*macb
)
658 unsigned long macb_hz
= get_macb_pclk_rate(id
);
660 if (macb_hz
< 20000000)
661 config
= GEM_BF(CLK
, GEM_CLK_DIV8
);
662 else if (macb_hz
< 40000000)
663 config
= GEM_BF(CLK
, GEM_CLK_DIV16
);
664 else if (macb_hz
< 80000000)
665 config
= GEM_BF(CLK
, GEM_CLK_DIV32
);
666 else if (macb_hz
< 120000000)
667 config
= GEM_BF(CLK
, GEM_CLK_DIV48
);
668 else if (macb_hz
< 160000000)
669 config
= GEM_BF(CLK
, GEM_CLK_DIV64
);
671 config
= GEM_BF(CLK
, GEM_CLK_DIV96
);
677 * Get the DMA bus width field of the network configuration register that we
678 * should program. We find the width from decoding the design configuration
679 * register to find the maximum supported data bus width.
681 static u32
macb_dbw(struct macb_device
*macb
)
683 switch (GEM_BFEXT(DBWDEF
, gem_readl(macb
, DCFG1
))) {
685 return GEM_BF(DBW
, GEM_DBW128
);
687 return GEM_BF(DBW
, GEM_DBW64
);
690 return GEM_BF(DBW
, GEM_DBW32
);
694 int macb_eth_initialize(int id
, void *regs
, unsigned int phy_addr
)
696 struct macb_device
*macb
;
697 struct eth_device
*netdev
;
700 macb
= malloc(sizeof(struct macb_device
));
702 printf("Error: Failed to allocate memory for MACB%d\n", id
);
705 memset(macb
, 0, sizeof(struct macb_device
));
707 netdev
= &macb
->netdev
;
709 macb
->rx_buffer
= dma_alloc_coherent(MACB_RX_BUFFER_SIZE
,
710 &macb
->rx_buffer_dma
);
711 macb
->rx_ring
= dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE
,
713 macb
->tx_ring
= dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE
,
716 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
719 macb
->phy_addr
= phy_addr
;
721 if (macb_is_gem(macb
))
722 sprintf(netdev
->name
, "gmac%d", id
);
724 sprintf(netdev
->name
, "macb%d", id
);
726 netdev
->init
= macb_init
;
727 netdev
->halt
= macb_halt
;
728 netdev
->send
= macb_send
;
729 netdev
->recv
= macb_recv
;
730 netdev
->write_hwaddr
= macb_write_hwaddr
;
733 * Do some basic initialization so that we at least can talk
736 if (macb_is_gem(macb
)) {
737 ncfgr
= gem_mdc_clk_div(id
, macb
);
738 ncfgr
|= macb_dbw(macb
);
740 ncfgr
= macb_mdc_clk_div(id
, macb
);
743 macb_writel(macb
, NCFGR
, ncfgr
);
745 eth_register(netdev
);
747 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
748 miiphy_register(netdev
->name
, macb_miiphy_read
, macb_miiphy_write
);
749 macb
->bus
= miiphy_get_dev_by_name(netdev
->name
);