blob: 921537f8a436ec4c42b255283895da158e520ed1 [file] [log] [blame]
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01005 */
6#include <common.h>
Simon Glassf1dcc192016-05-05 07:28:11 -06007#include <dm.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01008
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01009/*
10 * The u-boot networking stack is a little weird. It seems like the
11 * networking core allocates receive buffers up front without any
12 * regard to the hardware that's supposed to actually receive those
13 * packets.
14 *
15 * The MACB receives packets into 128-byte receive buffers, so the
16 * buffers allocated by the core isn't very practical to use. We'll
17 * allocate our own, but we need one such buffer in case a packet
18 * wraps around the DMA ring so that we have to copy it.
19 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020020 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010021 * configuration header. This way, the core allocates one RX buffer
22 * and one TX buffer, each of which can hold a ethernet packet of
23 * maximum size.
24 *
25 * For some reason, the networking core unconditionally specifies a
26 * 32-byte packet "alignment" (which really should be called
27 * "padding"). MACB shouldn't need that, but we'll refrain from any
28 * core modifications here...
29 */
30
31#include <net.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060032#ifndef CONFIG_DM_ETH
Ben Warren89973f82008-08-31 22:22:04 -070033#include <netdev.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060034#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010035#include <malloc.h>
Semih Hazar0f751d62009-12-17 15:07:15 +020036#include <miiphy.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010037
38#include <linux/mii.h>
39#include <asm/io.h>
40#include <asm/dma-mapping.h>
41#include <asm/arch/clk.h>
Bo Shen8314ccd2013-08-19 10:35:47 +080042#include <asm-generic/errno.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010043
44#include "macb.h"
45
Wenyou Yanga212b662016-05-17 13:11:35 +080046DECLARE_GLOBAL_DATA_PTR;
47
Andreas Bießmannceef9832014-05-26 22:55:18 +020048#define MACB_RX_BUFFER_SIZE 4096
49#define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
50#define MACB_TX_RING_SIZE 16
51#define MACB_TX_TIMEOUT 1000
52#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010053
54struct macb_dma_desc {
55 u32 addr;
56 u32 ctrl;
57};
58
Wu, Josh5ae0e382014-05-27 16:31:05 +080059#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
60#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
61#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080062#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080063
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010064#define RXADDR_USED 0x00000001
65#define RXADDR_WRAP 0x00000002
66
67#define RXBUF_FRMLEN_MASK 0x00000fff
68#define RXBUF_FRAME_START 0x00004000
69#define RXBUF_FRAME_END 0x00008000
70#define RXBUF_TYPEID_MATCH 0x00400000
71#define RXBUF_ADDR4_MATCH 0x00800000
72#define RXBUF_ADDR3_MATCH 0x01000000
73#define RXBUF_ADDR2_MATCH 0x02000000
74#define RXBUF_ADDR1_MATCH 0x04000000
75#define RXBUF_BROADCAST 0x80000000
76
77#define TXBUF_FRMLEN_MASK 0x000007ff
78#define TXBUF_FRAME_END 0x00008000
79#define TXBUF_NOCRC 0x00010000
80#define TXBUF_EXHAUSTED 0x08000000
81#define TXBUF_UNDERRUN 0x10000000
82#define TXBUF_MAXRETRY 0x20000000
83#define TXBUF_WRAP 0x40000000
84#define TXBUF_USED 0x80000000
85
86struct macb_device {
87 void *regs;
88
89 unsigned int rx_tail;
90 unsigned int tx_head;
91 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -060092 unsigned int next_rx_tail;
93 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010094
95 void *rx_buffer;
96 void *tx_buffer;
97 struct macb_dma_desc *rx_ring;
98 struct macb_dma_desc *tx_ring;
99
100 unsigned long rx_buffer_dma;
101 unsigned long rx_ring_dma;
102 unsigned long tx_ring_dma;
103
Wu, Joshade4ea42015-06-03 16:45:44 +0800104 struct macb_dma_desc *dummy_desc;
105 unsigned long dummy_desc_dma;
106
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100107 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600108#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100109 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600110#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100111 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800112 struct mii_dev *bus;
Wenyou Yanga212b662016-05-17 13:11:35 +0800113
114#ifdef CONFIG_DM_ETH
115 phy_interface_t phy_interface;
116#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100117};
Simon Glassf1dcc192016-05-05 07:28:11 -0600118#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100119#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600120#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100121
Bo Shend256be22013-04-24 15:59:28 +0800122static int macb_is_gem(struct macb_device *macb)
123{
124 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
125}
126
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100127#ifndef cpu_is_sama5d2
128#define cpu_is_sama5d2() 0
129#endif
130
131#ifndef cpu_is_sama5d4
132#define cpu_is_sama5d4() 0
133#endif
134
135static int gem_is_gigabit_capable(struct macb_device *macb)
136{
137 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400138 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100139 * configured to support only 10/100.
140 */
141 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
142}
143
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100144static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
145{
146 unsigned long netctl;
147 unsigned long netstat;
148 unsigned long frame;
149
150 netctl = macb_readl(macb, NCR);
151 netctl |= MACB_BIT(MPE);
152 macb_writel(macb, NCR, netctl);
153
154 frame = (MACB_BF(SOF, 1)
155 | MACB_BF(RW, 1)
156 | MACB_BF(PHYA, macb->phy_addr)
157 | MACB_BF(REGA, reg)
158 | MACB_BF(CODE, 2)
159 | MACB_BF(DATA, value));
160 macb_writel(macb, MAN, frame);
161
162 do {
163 netstat = macb_readl(macb, NSR);
164 } while (!(netstat & MACB_BIT(IDLE)));
165
166 netctl = macb_readl(macb, NCR);
167 netctl &= ~MACB_BIT(MPE);
168 macb_writel(macb, NCR, netctl);
169}
170
171static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
172{
173 unsigned long netctl;
174 unsigned long netstat;
175 unsigned long frame;
176
177 netctl = macb_readl(macb, NCR);
178 netctl |= MACB_BIT(MPE);
179 macb_writel(macb, NCR, netctl);
180
181 frame = (MACB_BF(SOF, 1)
182 | MACB_BF(RW, 2)
183 | MACB_BF(PHYA, macb->phy_addr)
184 | MACB_BF(REGA, reg)
185 | MACB_BF(CODE, 2));
186 macb_writel(macb, MAN, frame);
187
188 do {
189 netstat = macb_readl(macb, NSR);
190 } while (!(netstat & MACB_BIT(IDLE)));
191
192 frame = macb_readl(macb, MAN);
193
194 netctl = macb_readl(macb, NCR);
195 netctl &= ~MACB_BIT(MPE);
196 macb_writel(macb, NCR, netctl);
197
198 return MACB_BFEXT(DATA, frame);
199}
200
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500201void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530202{
203 return;
204}
205
Bo Shenb1a00062013-04-24 15:59:27 +0800206#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200207
Joe Hershberger5a49f172016-08-08 11:28:38 -0500208int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200209{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500210 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600211#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500212 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600213 struct macb_device *macb = dev_get_priv(dev);
214#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500215 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200216 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600217#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200218
Andreas Bießmannceef9832014-05-26 22:55:18 +0200219 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200220 return -1;
221
Joe Hershberger5a49f172016-08-08 11:28:38 -0500222 arch_get_mdio_control(bus->name);
223 value = macb_mdio_read(macb, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200224
Joe Hershberger5a49f172016-08-08 11:28:38 -0500225 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200226}
227
Joe Hershberger5a49f172016-08-08 11:28:38 -0500228int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
229 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200230{
Simon Glassf1dcc192016-05-05 07:28:11 -0600231#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500232 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600233 struct macb_device *macb = dev_get_priv(dev);
234#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500235 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200236 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600237#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200238
Andreas Bießmannceef9832014-05-26 22:55:18 +0200239 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200240 return -1;
241
Joe Hershberger5a49f172016-08-08 11:28:38 -0500242 arch_get_mdio_control(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200243 macb_mdio_write(macb, reg, value);
244
245 return 0;
246}
247#endif
248
Wu, Josh5ae0e382014-05-27 16:31:05 +0800249#define RX 1
250#define TX 0
251static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
252{
253 if (rx)
254 invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
255 MACB_RX_DMA_DESC_SIZE);
256 else
257 invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
258 MACB_TX_DMA_DESC_SIZE);
259}
260
261static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
262{
263 if (rx)
264 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
265 MACB_RX_DMA_DESC_SIZE);
266 else
267 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
268 MACB_TX_DMA_DESC_SIZE);
269}
270
271static inline void macb_flush_rx_buffer(struct macb_device *macb)
272{
273 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
274 MACB_RX_BUFFER_SIZE);
275}
276
277static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
278{
279 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
280 MACB_RX_BUFFER_SIZE);
281}
Semih Hazar0f751d62009-12-17 15:07:15 +0200282
Jon Loeliger07d38a12007-07-09 17:30:01 -0500283#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100284
Simon Glassd5555b72016-05-05 07:28:09 -0600285static int _macb_send(struct macb_device *macb, const char *name, void *packet,
286 int length)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100287{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100288 unsigned long paddr, ctrl;
289 unsigned int tx_head = macb->tx_head;
290 int i;
291
292 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
293
294 ctrl = length & TXBUF_FRMLEN_MASK;
295 ctrl |= TXBUF_FRAME_END;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200296 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100297 ctrl |= TXBUF_WRAP;
298 macb->tx_head = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200299 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100300 macb->tx_head++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200301 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100302
303 macb->tx_ring[tx_head].ctrl = ctrl;
304 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200305 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800306 macb_flush_ring_desc(macb, TX);
307 /* Do we need check paddr and length is dcache line aligned? */
Simon Glassf589f8c2016-05-05 07:28:10 -0600308 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100309 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
310
311 /*
312 * I guess this is necessary because the networking core may
313 * re-use the transmit buffer as soon as we return...
314 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200315 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200316 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800317 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200318 ctrl = macb->tx_ring[tx_head].ctrl;
319 if (ctrl & TXBUF_USED)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100320 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100321 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100322 }
323
324 dma_unmap_single(packet, length, paddr);
325
Andreas Bießmannceef9832014-05-26 22:55:18 +0200326 if (i <= MACB_TX_TIMEOUT) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100327 if (ctrl & TXBUF_UNDERRUN)
Simon Glassd5555b72016-05-05 07:28:09 -0600328 printf("%s: TX underrun\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100329 if (ctrl & TXBUF_EXHAUSTED)
Simon Glassd5555b72016-05-05 07:28:09 -0600330 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200331 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600332 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100333 }
334
335 /* No one cares anyway */
336 return 0;
337}
338
339static void reclaim_rx_buffers(struct macb_device *macb,
340 unsigned int new_tail)
341{
342 unsigned int i;
343
344 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800345
346 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100347 while (i > new_tail) {
348 macb->rx_ring[i].addr &= ~RXADDR_USED;
349 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200350 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100351 i = 0;
352 }
353
354 while (i < new_tail) {
355 macb->rx_ring[i].addr &= ~RXADDR_USED;
356 i++;
357 }
358
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200359 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800360 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100361 macb->rx_tail = new_tail;
362}
363
Simon Glassd5555b72016-05-05 07:28:09 -0600364static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100365{
Simon Glassd5555b72016-05-05 07:28:09 -0600366 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100367 void *buffer;
368 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100369 u32 status;
370
Simon Glassd5555b72016-05-05 07:28:09 -0600371 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100372 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800373 macb_invalidate_ring_desc(macb, RX);
374
Simon Glassd5555b72016-05-05 07:28:09 -0600375 if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED))
376 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100377
Simon Glassd5555b72016-05-05 07:28:09 -0600378 status = macb->rx_ring[next_rx_tail].ctrl;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100379 if (status & RXBUF_FRAME_START) {
Simon Glassd5555b72016-05-05 07:28:09 -0600380 if (next_rx_tail != macb->rx_tail)
381 reclaim_rx_buffers(macb, next_rx_tail);
382 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100383 }
384
385 if (status & RXBUF_FRAME_END) {
386 buffer = macb->rx_buffer + 128 * macb->rx_tail;
387 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800388
389 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600390 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100391 unsigned int headlen, taillen;
392
Andreas Bießmannceef9832014-05-26 22:55:18 +0200393 headlen = 128 * (MACB_RX_RING_SIZE
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100394 - macb->rx_tail);
395 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500396 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100397 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500398 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100399 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600400 *packetp = (void *)net_rx_packets[0];
401 } else {
402 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100403 }
404
Simon Glassd5555b72016-05-05 07:28:09 -0600405 if (++next_rx_tail >= MACB_RX_RING_SIZE)
406 next_rx_tail = 0;
407 macb->next_rx_tail = next_rx_tail;
408 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100409 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600410 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
411 macb->wrapped = true;
412 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100413 }
414 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200415 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100416 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100417}
418
Simon Glassd5555b72016-05-05 07:28:09 -0600419static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200420{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200421 int i;
422 u16 status, adv;
423
424 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
425 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600426 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200427 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
428 | BMCR_ANRESTART));
429
Andreas Bießmannceef9832014-05-26 22:55:18 +0200430 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200431 status = macb_mdio_read(macb, MII_BMSR);
432 if (status & BMSR_ANEGCOMPLETE)
433 break;
434 udelay(100);
435 }
436
437 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600438 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200439 else
440 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600441 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200442}
443
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100444#ifdef CONFIG_MACB_SEARCH_PHY
Wenyou Yanga212b662016-05-17 13:11:35 +0800445static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100446{
447 int i;
448 u16 phy_id;
449
450 /* Search for PHY... */
451 for (i = 0; i < 32; i++) {
452 macb->phy_addr = i;
453 phy_id = macb_mdio_read(macb, MII_PHYSID1);
454 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800455 printf("%s: PHY present at %d\n", name, i);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100456 return 1;
457 }
458 }
459
460 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800461 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100462
463 return 0;
464}
465#endif /* CONFIG_MACB_SEARCH_PHY */
466
Wenyou Yanga212b662016-05-17 13:11:35 +0800467#ifdef CONFIG_DM_ETH
468static int macb_phy_init(struct udevice *dev, const char *name)
469#else
Simon Glassd5555b72016-05-05 07:28:09 -0600470static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800471#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100472{
Wenyou Yanga212b662016-05-17 13:11:35 +0800473#ifdef CONFIG_DM_ETH
474 struct macb_device *macb = dev_get_priv(dev);
475#endif
Bo Shenb1a00062013-04-24 15:59:27 +0800476#ifdef CONFIG_PHYLIB
477 struct phy_device *phydev;
478#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100479 u32 ncfgr;
480 u16 phy_id, status, adv, lpa;
481 int media, speed, duplex;
482 int i;
483
Simon Glassd5555b72016-05-05 07:28:09 -0600484 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100485#ifdef CONFIG_MACB_SEARCH_PHY
486 /* Auto-detect phy_addr */
Wenyou Yanga212b662016-05-17 13:11:35 +0800487 if (!macb_phy_find(macb, name))
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100488 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100489#endif /* CONFIG_MACB_SEARCH_PHY */
490
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100491 /* Check if the PHY is up to snuff... */
492 phy_id = macb_mdio_read(macb, MII_PHYSID1);
493 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600494 printf("%s: No PHY present\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100495 return 0;
496 }
497
Bo Shenb1a00062013-04-24 15:59:27 +0800498#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800499#ifdef CONFIG_DM_ETH
500 phydev = phy_connect(macb->bus, macb->phy_addr, dev,
501 macb->phy_interface);
502#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800503 /* need to consider other phy interface mode */
Simon Glassd5555b72016-05-05 07:28:09 -0600504 phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800505 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800506#endif
Bo Shen8314ccd2013-08-19 10:35:47 +0800507 if (!phydev) {
508 printf("phy_connect failed\n");
509 return -ENODEV;
510 }
511
Bo Shenb1a00062013-04-24 15:59:27 +0800512 phy_config(phydev);
513#endif
514
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200515 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100516 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200517 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600518 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200519
Andreas Bießmannceef9832014-05-26 22:55:18 +0200520 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100521 status = macb_mdio_read(macb, MII_BMSR);
522 if (status & BMSR_LSTATUS)
523 break;
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200524 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100525 }
526 }
527
528 if (!(status & BMSR_LSTATUS)) {
529 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600530 name, status);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100531 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100532 }
Bo Shend256be22013-04-24 15:59:28 +0800533
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100534 /* First check for GMAC and that it is GiB capable */
535 if (gem_is_gigabit_capable(macb)) {
Bo Shend256be22013-04-24 15:59:28 +0800536 lpa = macb_mdio_read(macb, MII_STAT1000);
Bo Shend256be22013-04-24 15:59:28 +0800537
Andreas Bießmann47609572014-09-18 23:46:48 +0200538 if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
539 duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
540
541 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600542 name,
Bo Shend256be22013-04-24 15:59:28 +0800543 duplex ? "full" : "half",
544 lpa);
545
546 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200547 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
548 ncfgr |= GEM_BIT(GBE);
549
Bo Shend256be22013-04-24 15:59:28 +0800550 if (duplex)
551 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200552
Bo Shend256be22013-04-24 15:59:28 +0800553 macb_writel(macb, NCFGR, ncfgr);
554
555 return 1;
556 }
557 }
558
559 /* fall back for EMAC checking */
560 adv = macb_mdio_read(macb, MII_ADVERTISE);
561 lpa = macb_mdio_read(macb, MII_LPA);
562 media = mii_nway_result(lpa & adv);
563 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
564 ? 1 : 0);
565 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
566 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600567 name,
Bo Shend256be22013-04-24 15:59:28 +0800568 speed ? "100" : "10",
569 duplex ? "full" : "half",
570 lpa);
571
572 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800573 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Bo Shend256be22013-04-24 15:59:28 +0800574 if (speed)
575 ncfgr |= MACB_BIT(SPD);
576 if (duplex)
577 ncfgr |= MACB_BIT(FD);
578 macb_writel(macb, NCFGR, ncfgr);
579
580 return 1;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100581}
582
Wu, Joshade4ea42015-06-03 16:45:44 +0800583static int gmac_init_multi_queues(struct macb_device *macb)
584{
585 int i, num_queues = 1;
586 u32 queue_mask;
587
588 /* bit 0 is never set but queue 0 always exists */
589 queue_mask = gem_readl(macb, DCFG6) & 0xff;
590 queue_mask |= 0x1;
591
592 for (i = 1; i < MACB_MAX_QUEUES; i++)
593 if (queue_mask & (1 << i))
594 num_queues++;
595
596 macb->dummy_desc->ctrl = TXBUF_USED;
597 macb->dummy_desc->addr = 0;
598 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
599 MACB_TX_DUMMY_DMA_DESC_SIZE);
600
601 for (i = 1; i < num_queues; i++)
602 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
603
604 return 0;
605}
606
Wenyou Yanga212b662016-05-17 13:11:35 +0800607#ifdef CONFIG_DM_ETH
608static int _macb_init(struct udevice *dev, const char *name)
609#else
Simon Glassd5555b72016-05-05 07:28:09 -0600610static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800611#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100612{
Wenyou Yanga212b662016-05-17 13:11:35 +0800613#ifdef CONFIG_DM_ETH
614 struct macb_device *macb = dev_get_priv(dev);
615#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100616 unsigned long paddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100617 int i;
618
619 /*
620 * macb_halt should have been called at some point before now,
621 * so we'll assume the controller is idle.
622 */
623
624 /* initialize DMA descriptors */
625 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200626 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
627 if (i == (MACB_RX_RING_SIZE - 1))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100628 paddr |= RXADDR_WRAP;
629 macb->rx_ring[i].addr = paddr;
630 macb->rx_ring[i].ctrl = 0;
631 paddr += 128;
632 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800633 macb_flush_ring_desc(macb, RX);
634 macb_flush_rx_buffer(macb);
635
Andreas Bießmannceef9832014-05-26 22:55:18 +0200636 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100637 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200638 if (i == (MACB_TX_RING_SIZE - 1))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100639 macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
640 else
641 macb->tx_ring[i].ctrl = TXBUF_USED;
642 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800643 macb_flush_ring_desc(macb, TX);
644
Andreas Bießmannceef9832014-05-26 22:55:18 +0200645 macb->rx_tail = 0;
646 macb->tx_head = 0;
647 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600648 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100649
650 macb_writel(macb, RBQP, macb->rx_ring_dma);
651 macb_writel(macb, TBQP, macb->tx_ring_dma);
652
Bo Shend256be22013-04-24 15:59:28 +0800653 if (macb_is_gem(macb)) {
Wu, Joshade4ea42015-06-03 16:45:44 +0800654 /* Check the multi queue and initialize the queue for tx */
655 gmac_init_multi_queues(macb);
656
Bo Shencabf61c2014-11-10 15:24:01 +0800657 /*
658 * When the GMAC IP with GE feature, this bit is used to
659 * select interface between RGMII and GMII.
660 * When the GMAC IP without GE feature, this bit is used
661 * to select interface between RMII and MII.
662 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800663#ifdef CONFIG_DM_ETH
664 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
665 gem_writel(macb, UR, GEM_BIT(RGMII));
666 else
667 gem_writel(macb, UR, 0);
668#else
Bo Shencabf61c2014-11-10 15:24:01 +0800669#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Bo Shend256be22013-04-24 15:59:28 +0800670 gem_writel(macb, UR, GEM_BIT(RGMII));
671#else
672 gem_writel(macb, UR, 0);
673#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800674#endif
Bo Shend256be22013-04-24 15:59:28 +0800675 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100676 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800677#ifdef CONFIG_DM_ETH
678#ifdef CONFIG_AT91FAMILY
679 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
680 macb_writel(macb, USRIO,
681 MACB_BIT(RMII) | MACB_BIT(CLKEN));
682 } else {
683 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
684 }
685#else
686 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
687 macb_writel(macb, USRIO, 0);
688 else
689 macb_writel(macb, USRIO, MACB_BIT(MII));
690#endif
691#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100692#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800693#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000694 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
695#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100696 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000697#endif
698#else
Bo Shend8f64b42013-04-24 15:59:26 +0800699#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000700 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100701#else
702 macb_writel(macb, USRIO, MACB_BIT(MII));
703#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000704#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800705#endif
Bo Shend256be22013-04-24 15:59:28 +0800706 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100707
Wenyou Yanga212b662016-05-17 13:11:35 +0800708#ifdef CONFIG_DM_ETH
709 if (!macb_phy_init(dev, name))
710#else
Simon Glassd5555b72016-05-05 07:28:09 -0600711 if (!macb_phy_init(macb, name))
Wenyou Yanga212b662016-05-17 13:11:35 +0800712#endif
Ben Warren422b1a02008-01-09 18:15:53 -0500713 return -1;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100714
715 /* Enable TX and RX */
716 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
717
Ben Warren422b1a02008-01-09 18:15:53 -0500718 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100719}
720
Simon Glassd5555b72016-05-05 07:28:09 -0600721static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100722{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100723 u32 ncr, tsr;
724
725 /* Halt the controller and wait for any ongoing transmission to end. */
726 ncr = macb_readl(macb, NCR);
727 ncr |= MACB_BIT(THALT);
728 macb_writel(macb, NCR, ncr);
729
730 do {
731 tsr = macb_readl(macb, TSR);
732 } while (tsr & MACB_BIT(TGO));
733
734 /* Disable TX and RX, and clear statistics */
735 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
736}
737
Simon Glassd5555b72016-05-05 07:28:09 -0600738static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700739{
Ben Warren6bb46792010-06-01 11:55:42 -0700740 u32 hwaddr_bottom;
741 u16 hwaddr_top;
742
743 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600744 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
745 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700746 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600747 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700748 macb_writel(macb, SA1T, hwaddr_top);
749 return 0;
750}
751
Bo Shend256be22013-04-24 15:59:28 +0800752static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
753{
754 u32 config;
755 unsigned long macb_hz = get_macb_pclk_rate(id);
756
757 if (macb_hz < 20000000)
758 config = MACB_BF(CLK, MACB_CLK_DIV8);
759 else if (macb_hz < 40000000)
760 config = MACB_BF(CLK, MACB_CLK_DIV16);
761 else if (macb_hz < 80000000)
762 config = MACB_BF(CLK, MACB_CLK_DIV32);
763 else
764 config = MACB_BF(CLK, MACB_CLK_DIV64);
765
766 return config;
767}
768
769static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
770{
771 u32 config;
772 unsigned long macb_hz = get_macb_pclk_rate(id);
773
774 if (macb_hz < 20000000)
775 config = GEM_BF(CLK, GEM_CLK_DIV8);
776 else if (macb_hz < 40000000)
777 config = GEM_BF(CLK, GEM_CLK_DIV16);
778 else if (macb_hz < 80000000)
779 config = GEM_BF(CLK, GEM_CLK_DIV32);
780 else if (macb_hz < 120000000)
781 config = GEM_BF(CLK, GEM_CLK_DIV48);
782 else if (macb_hz < 160000000)
783 config = GEM_BF(CLK, GEM_CLK_DIV64);
784 else
785 config = GEM_BF(CLK, GEM_CLK_DIV96);
786
787 return config;
788}
789
Bo Shen32e4f6b2013-09-18 15:07:44 +0800790/*
791 * Get the DMA bus width field of the network configuration register that we
792 * should program. We find the width from decoding the design configuration
793 * register to find the maximum supported data bus width.
794 */
795static u32 macb_dbw(struct macb_device *macb)
796{
797 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
798 case 4:
799 return GEM_BF(DBW, GEM_DBW128);
800 case 2:
801 return GEM_BF(DBW, GEM_DBW64);
802 case 1:
803 default:
804 return GEM_BF(DBW, GEM_DBW32);
805 }
806}
807
Simon Glassd5555b72016-05-05 07:28:09 -0600808static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100809{
Simon Glassd5555b72016-05-05 07:28:09 -0600810 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100811 u32 ncfgr;
812
Simon Glassd5555b72016-05-05 07:28:09 -0600813 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200814 macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100815 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800816 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100817 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800818 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100819 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +0800820 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
821 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100822
Simon Glassd5555b72016-05-05 07:28:09 -0600823 /*
824 * Do some basic initialization so that we at least can talk
825 * to the PHY
826 */
827 if (macb_is_gem(macb)) {
828 ncfgr = gem_mdc_clk_div(id, macb);
829 ncfgr |= macb_dbw(macb);
830 } else {
831 ncfgr = macb_mdc_clk_div(id, macb);
832 }
833
834 macb_writel(macb, NCFGR, ncfgr);
835}
836
Simon Glassf1dcc192016-05-05 07:28:11 -0600837#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -0600838static int macb_send(struct eth_device *netdev, void *packet, int length)
839{
840 struct macb_device *macb = to_macb(netdev);
841
842 return _macb_send(macb, netdev->name, packet, length);
843}
844
845static int macb_recv(struct eth_device *netdev)
846{
847 struct macb_device *macb = to_macb(netdev);
848 uchar *packet;
849 int length;
850
851 macb->wrapped = false;
852 for (;;) {
853 macb->next_rx_tail = macb->rx_tail;
854 length = _macb_recv(macb, &packet);
855 if (length >= 0) {
856 net_process_received_packet(packet, length);
857 reclaim_rx_buffers(macb, macb->next_rx_tail);
858 } else if (length < 0) {
859 return length;
860 }
861 }
862}
863
864static int macb_init(struct eth_device *netdev, bd_t *bd)
865{
866 struct macb_device *macb = to_macb(netdev);
867
868 return _macb_init(macb, netdev->name);
869}
870
871static void macb_halt(struct eth_device *netdev)
872{
873 struct macb_device *macb = to_macb(netdev);
874
875 return _macb_halt(macb);
876}
877
878static int macb_write_hwaddr(struct eth_device *netdev)
879{
880 struct macb_device *macb = to_macb(netdev);
881
882 return _macb_write_hwaddr(macb, netdev->enetaddr);
883}
884
885int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
886{
887 struct macb_device *macb;
888 struct eth_device *netdev;
889
890 macb = malloc(sizeof(struct macb_device));
891 if (!macb) {
892 printf("Error: Failed to allocate memory for MACB%d\n", id);
893 return -1;
894 }
895 memset(macb, 0, sizeof(struct macb_device));
896
897 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800898
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100899 macb->regs = regs;
900 macb->phy_addr = phy_addr;
901
Bo Shend256be22013-04-24 15:59:28 +0800902 if (macb_is_gem(macb))
903 sprintf(netdev->name, "gmac%d", id);
904 else
905 sprintf(netdev->name, "macb%d", id);
906
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100907 netdev->init = macb_init;
908 netdev->halt = macb_halt;
909 netdev->send = macb_send;
910 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -0700911 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100912
Simon Glassd5555b72016-05-05 07:28:09 -0600913 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100914
915 eth_register(netdev);
916
Bo Shenb1a00062013-04-24 15:59:27 +0800917#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500918 int retval;
919 struct mii_dev *mdiodev = mdio_alloc();
920 if (!mdiodev)
921 return -ENOMEM;
922 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
923 mdiodev->read = macb_miiphy_read;
924 mdiodev->write = macb_miiphy_write;
925
926 retval = mdio_register(mdiodev);
927 if (retval < 0)
928 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +0800929 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200930#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100931 return 0;
932}
Simon Glassf1dcc192016-05-05 07:28:11 -0600933#endif /* !CONFIG_DM_ETH */
934
935#ifdef CONFIG_DM_ETH
936
937static int macb_start(struct udevice *dev)
938{
Wenyou Yanga212b662016-05-17 13:11:35 +0800939 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600940}
941
942static int macb_send(struct udevice *dev, void *packet, int length)
943{
944 struct macb_device *macb = dev_get_priv(dev);
945
946 return _macb_send(macb, dev->name, packet, length);
947}
948
949static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
950{
951 struct macb_device *macb = dev_get_priv(dev);
952
953 macb->next_rx_tail = macb->rx_tail;
954 macb->wrapped = false;
955
956 return _macb_recv(macb, packetp);
957}
958
959static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
960{
961 struct macb_device *macb = dev_get_priv(dev);
962
963 reclaim_rx_buffers(macb, macb->next_rx_tail);
964
965 return 0;
966}
967
968static void macb_stop(struct udevice *dev)
969{
970 struct macb_device *macb = dev_get_priv(dev);
971
972 _macb_halt(macb);
973}
974
975static int macb_write_hwaddr(struct udevice *dev)
976{
977 struct eth_pdata *plat = dev_get_platdata(dev);
978 struct macb_device *macb = dev_get_priv(dev);
979
980 return _macb_write_hwaddr(macb, plat->enetaddr);
981}
982
983static const struct eth_ops macb_eth_ops = {
984 .start = macb_start,
985 .send = macb_send,
986 .recv = macb_recv,
987 .stop = macb_stop,
988 .free_pkt = macb_free_pkt,
989 .write_hwaddr = macb_write_hwaddr,
990};
991
992static int macb_eth_probe(struct udevice *dev)
993{
994 struct eth_pdata *pdata = dev_get_platdata(dev);
995 struct macb_device *macb = dev_get_priv(dev);
996
Wenyou Yanga212b662016-05-17 13:11:35 +0800997#ifdef CONFIG_DM_ETH
998 const char *phy_mode;
999
1000 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
1001 if (phy_mode)
1002 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1003 if (macb->phy_interface == -1) {
1004 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1005 return -EINVAL;
1006 }
1007#endif
1008
Simon Glassf1dcc192016-05-05 07:28:11 -06001009 macb->regs = (void *)pdata->iobase;
1010
1011 _macb_eth_initialize(macb);
1012#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001013 int retval;
1014 struct mii_dev *mdiodev = mdio_alloc();
1015 if (!mdiodev)
1016 return -ENOMEM;
1017 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
1018 mdiodev->read = macb_miiphy_read;
1019 mdiodev->write = macb_miiphy_write;
1020
1021 retval = mdio_register(mdiodev);
1022 if (retval < 0)
1023 return retval;
Simon Glassf1dcc192016-05-05 07:28:11 -06001024 macb->bus = miiphy_get_dev_by_name(dev->name);
1025#endif
1026
1027 return 0;
1028}
1029
1030static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1031{
1032 struct eth_pdata *pdata = dev_get_platdata(dev);
1033
1034 pdata->iobase = dev_get_addr(dev);
1035 return 0;
1036}
1037
1038static const struct udevice_id macb_eth_ids[] = {
1039 { .compatible = "cdns,macb" },
1040 { }
1041};
1042
1043U_BOOT_DRIVER(eth_macb) = {
1044 .name = "eth_macb",
1045 .id = UCLASS_ETH,
1046 .of_match = macb_eth_ids,
1047 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1048 .probe = macb_eth_probe,
1049 .ops = &macb_eth_ops,
1050 .priv_auto_alloc_size = sizeof(struct macb_device),
1051 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1052};
1053#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001054
Jon Loeliger07d38a12007-07-09 17:30:01 -05001055#endif