blob: 88a2c31b06deabc55943317c8b1cfdc46c96bdd7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01002/*
3 * Copyright (C) 2005-2006 Atmel Corporation
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01004 */
5#include <common.h>
Wenyou Yang577aa3b2016-11-02 10:06:56 +08006#include <clk.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>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090042#include <linux/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
Ramon Friedc6d07bf2019-07-14 18:25:14 +030048/*
49 * These buffer sizes must be power of 2 and divisible
50 * by RX_BUFFER_MULTIPLE
51 */
52#define MACB_RX_BUFFER_SIZE 128
53#define GEM_RX_BUFFER_SIZE 2048
Ramon Fried9c295802019-07-16 22:04:36 +030054#define RX_BUFFER_MULTIPLE 64
Ramon Friedc6d07bf2019-07-14 18:25:14 +030055
56#define MACB_RX_RING_SIZE 32
Andreas Bießmannceef9832014-05-26 22:55:18 +020057#define MACB_TX_RING_SIZE 16
Ramon Friedc6d07bf2019-07-14 18:25:14 +030058
Andreas Bießmannceef9832014-05-26 22:55:18 +020059#define MACB_TX_TIMEOUT 1000
60#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010061
Wilson Lee4bf56912017-08-22 20:25:07 -070062#ifdef CONFIG_MACB_ZYNQ
63/* INCR4 AHB bursts */
64#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
65/* Use full configured addressable space (8 Kb) */
66#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
67/* Use full configured addressable space (4 Kb) */
68#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
69/* Set RXBUF with use of 128 byte */
70#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
71#define MACB_ZYNQ_GEM_DMACR_INIT \
72 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
73 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
74 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_RXBUF)
76#endif
77
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010078struct macb_dma_desc {
79 u32 addr;
80 u32 ctrl;
81};
82
Wu, Josh5ae0e382014-05-27 16:31:05 +080083#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
84#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
85#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080086#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080087
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010088#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010089#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010090
91struct macb_device {
92 void *regs;
Ramon Frieded3c64f2019-07-16 22:04:35 +030093 unsigned int dma_burst_length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010094
95 unsigned int rx_tail;
96 unsigned int tx_head;
97 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -060098 unsigned int next_rx_tail;
99 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100100
101 void *rx_buffer;
102 void *tx_buffer;
103 struct macb_dma_desc *rx_ring;
104 struct macb_dma_desc *tx_ring;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300105 size_t rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100106
107 unsigned long rx_buffer_dma;
108 unsigned long rx_ring_dma;
109 unsigned long tx_ring_dma;
110
Wu, Joshade4ea42015-06-03 16:45:44 +0800111 struct macb_dma_desc *dummy_desc;
112 unsigned long dummy_desc_dma;
113
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100114 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600115#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100116 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600117#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100118 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800119 struct mii_dev *bus;
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800120#ifdef CONFIG_PHYLIB
121 struct phy_device *phydev;
122#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800123
124#ifdef CONFIG_DM_ETH
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800125#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800126 unsigned long pclk_rate;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800127#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800128 phy_interface_t phy_interface;
129#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100130};
Ramon Frieded3c64f2019-07-16 22:04:35 +0300131
132struct macb_config {
133 unsigned int dma_burst_length;
134};
135
Simon Glassf1dcc192016-05-05 07:28:11 -0600136#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100137#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600138#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100139
Bo Shend256be22013-04-24 15:59:28 +0800140static int macb_is_gem(struct macb_device *macb)
141{
Atish Patrafbcaa262019-02-25 08:14:42 +0000142 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shend256be22013-04-24 15:59:28 +0800143}
144
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100145#ifndef cpu_is_sama5d2
146#define cpu_is_sama5d2() 0
147#endif
148
149#ifndef cpu_is_sama5d4
150#define cpu_is_sama5d4() 0
151#endif
152
153static int gem_is_gigabit_capable(struct macb_device *macb)
154{
155 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400156 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100157 * configured to support only 10/100.
158 */
159 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
160}
161
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100162static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
163{
164 unsigned long netctl;
165 unsigned long netstat;
166 unsigned long frame;
167
168 netctl = macb_readl(macb, NCR);
169 netctl |= MACB_BIT(MPE);
170 macb_writel(macb, NCR, netctl);
171
172 frame = (MACB_BF(SOF, 1)
173 | MACB_BF(RW, 1)
174 | MACB_BF(PHYA, macb->phy_addr)
175 | MACB_BF(REGA, reg)
176 | MACB_BF(CODE, 2)
177 | MACB_BF(DATA, value));
178 macb_writel(macb, MAN, frame);
179
180 do {
181 netstat = macb_readl(macb, NSR);
182 } while (!(netstat & MACB_BIT(IDLE)));
183
184 netctl = macb_readl(macb, NCR);
185 netctl &= ~MACB_BIT(MPE);
186 macb_writel(macb, NCR, netctl);
187}
188
189static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
190{
191 unsigned long netctl;
192 unsigned long netstat;
193 unsigned long frame;
194
195 netctl = macb_readl(macb, NCR);
196 netctl |= MACB_BIT(MPE);
197 macb_writel(macb, NCR, netctl);
198
199 frame = (MACB_BF(SOF, 1)
200 | MACB_BF(RW, 2)
201 | MACB_BF(PHYA, macb->phy_addr)
202 | MACB_BF(REGA, reg)
203 | MACB_BF(CODE, 2));
204 macb_writel(macb, MAN, frame);
205
206 do {
207 netstat = macb_readl(macb, NSR);
208 } while (!(netstat & MACB_BIT(IDLE)));
209
210 frame = macb_readl(macb, MAN);
211
212 netctl = macb_readl(macb, NCR);
213 netctl &= ~MACB_BIT(MPE);
214 macb_writel(macb, NCR, netctl);
215
216 return MACB_BFEXT(DATA, frame);
217}
218
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500219void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530220{
221 return;
222}
223
Bo Shenb1a00062013-04-24 15:59:27 +0800224#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200225
Joe Hershberger5a49f172016-08-08 11:28:38 -0500226int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200227{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500228 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600229#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500230 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600231 struct macb_device *macb = dev_get_priv(dev);
232#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500233 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200234 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600235#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200236
Andreas Bießmannceef9832014-05-26 22:55:18 +0200237 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200238 return -1;
239
Joe Hershberger5a49f172016-08-08 11:28:38 -0500240 arch_get_mdio_control(bus->name);
241 value = macb_mdio_read(macb, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200242
Joe Hershberger5a49f172016-08-08 11:28:38 -0500243 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200244}
245
Joe Hershberger5a49f172016-08-08 11:28:38 -0500246int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
247 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200248{
Simon Glassf1dcc192016-05-05 07:28:11 -0600249#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500250 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600251 struct macb_device *macb = dev_get_priv(dev);
252#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500253 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200254 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600255#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200256
Andreas Bießmannceef9832014-05-26 22:55:18 +0200257 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200258 return -1;
259
Joe Hershberger5a49f172016-08-08 11:28:38 -0500260 arch_get_mdio_control(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200261 macb_mdio_write(macb, reg, value);
262
263 return 0;
264}
265#endif
266
Wu, Josh5ae0e382014-05-27 16:31:05 +0800267#define RX 1
268#define TX 0
269static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
270{
271 if (rx)
Heiko Schocher592a7492016-08-29 07:46:11 +0200272 invalidate_dcache_range(macb->rx_ring_dma,
273 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
274 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800275 else
Heiko Schocher592a7492016-08-29 07:46:11 +0200276 invalidate_dcache_range(macb->tx_ring_dma,
277 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
278 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800279}
280
281static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
282{
283 if (rx)
284 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200285 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800286 else
287 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200288 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800289}
290
291static inline void macb_flush_rx_buffer(struct macb_device *macb)
292{
293 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200294 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800295}
296
297static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
298{
299 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200300 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800301}
Semih Hazar0f751d62009-12-17 15:07:15 +0200302
Jon Loeliger07d38a12007-07-09 17:30:01 -0500303#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100304
Simon Glassd5555b72016-05-05 07:28:09 -0600305static int _macb_send(struct macb_device *macb, const char *name, void *packet,
306 int length)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100307{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100308 unsigned long paddr, ctrl;
309 unsigned int tx_head = macb->tx_head;
310 int i;
311
312 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
313
314 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300315 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmannceef9832014-05-26 22:55:18 +0200316 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300317 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100318 macb->tx_head = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200319 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100320 macb->tx_head++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200321 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100322
323 macb->tx_ring[tx_head].ctrl = ctrl;
324 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200325 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800326 macb_flush_ring_desc(macb, TX);
327 /* Do we need check paddr and length is dcache line aligned? */
Simon Glassf589f8c2016-05-05 07:28:10 -0600328 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100329 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
330
331 /*
332 * I guess this is necessary because the networking core may
333 * re-use the transmit buffer as soon as we return...
334 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200335 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200336 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800337 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200338 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300339 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100340 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100341 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100342 }
343
344 dma_unmap_single(packet, length, paddr);
345
Andreas Bießmannceef9832014-05-26 22:55:18 +0200346 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300347 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glassd5555b72016-05-05 07:28:09 -0600348 printf("%s: TX underrun\n", name);
Ramon Fried0a2827e2019-07-16 22:04:33 +0300349 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glassd5555b72016-05-05 07:28:09 -0600350 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200351 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600352 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100353 }
354
355 /* No one cares anyway */
356 return 0;
357}
358
359static void reclaim_rx_buffers(struct macb_device *macb,
360 unsigned int new_tail)
361{
362 unsigned int i;
363
364 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800365
366 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100367 while (i > new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300368 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100369 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200370 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100371 i = 0;
372 }
373
374 while (i < new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300375 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100376 i++;
377 }
378
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200379 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800380 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100381 macb->rx_tail = new_tail;
382}
383
Simon Glassd5555b72016-05-05 07:28:09 -0600384static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100385{
Simon Glassd5555b72016-05-05 07:28:09 -0600386 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100387 void *buffer;
388 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100389 u32 status;
390
Simon Glassd5555b72016-05-05 07:28:09 -0600391 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100392 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800393 macb_invalidate_ring_desc(macb, RX);
394
Ramon Fried0a2827e2019-07-16 22:04:33 +0300395 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glassd5555b72016-05-05 07:28:09 -0600396 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100397
Simon Glassd5555b72016-05-05 07:28:09 -0600398 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300399 if (status & MACB_BIT(RX_SOF)) {
Simon Glassd5555b72016-05-05 07:28:09 -0600400 if (next_rx_tail != macb->rx_tail)
401 reclaim_rx_buffers(macb, next_rx_tail);
402 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100403 }
404
Ramon Fried0a2827e2019-07-16 22:04:33 +0300405 if (status & MACB_BIT(RX_EOF)) {
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300406 buffer = macb->rx_buffer +
407 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100408 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800409
410 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600411 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100412 unsigned int headlen, taillen;
413
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300414 headlen = macb->rx_buffer_size *
415 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100416 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500417 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100418 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500419 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100420 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600421 *packetp = (void *)net_rx_packets[0];
422 } else {
423 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100424 }
425
Simon Glassd5555b72016-05-05 07:28:09 -0600426 if (++next_rx_tail >= MACB_RX_RING_SIZE)
427 next_rx_tail = 0;
428 macb->next_rx_tail = next_rx_tail;
429 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100430 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600431 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
432 macb->wrapped = true;
433 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100434 }
435 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200436 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100437 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100438}
439
Simon Glassd5555b72016-05-05 07:28:09 -0600440static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200441{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200442 int i;
443 u16 status, adv;
444
445 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
446 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600447 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200448 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
449 | BMCR_ANRESTART));
450
Andreas Bießmannceef9832014-05-26 22:55:18 +0200451 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200452 status = macb_mdio_read(macb, MII_BMSR);
453 if (status & BMSR_ANEGCOMPLETE)
454 break;
455 udelay(100);
456 }
457
458 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600459 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200460 else
461 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600462 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200463}
464
Wenyou Yanga212b662016-05-17 13:11:35 +0800465static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100466{
467 int i;
468 u16 phy_id;
469
470 /* Search for PHY... */
471 for (i = 0; i < 32; i++) {
472 macb->phy_addr = i;
473 phy_id = macb_mdio_read(macb, MII_PHYSID1);
474 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800475 printf("%s: PHY present at %d\n", name, i);
Wilson Lee4bf56912017-08-22 20:25:07 -0700476 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100477 }
478 }
479
480 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800481 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100482
Wilson Lee4bf56912017-08-22 20:25:07 -0700483 return -ENODEV;
484}
485
486/**
487 * macb_linkspd_cb - Linkspeed change callback function
Bin Menga5e3d232019-05-22 00:09:45 -0700488 * @dev/@regs: MACB udevice (DM version) or
489 * Base Register of MACB devices (non-DM version)
Wilson Lee4bf56912017-08-22 20:25:07 -0700490 * @speed: Linkspeed
491 * Returns 0 when operation success and negative errno number
492 * when operation failed.
493 */
Bin Menga5e3d232019-05-22 00:09:45 -0700494#ifdef CONFIG_DM_ETH
495int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
496{
Bin Meng3ef64442019-05-22 00:09:46 -0700497#ifdef CONFIG_CLK
498 struct clk tx_clk;
499 ulong rate;
500 int ret;
501
502 /*
503 * "tx_clk" is an optional clock source for MACB.
504 * Ignore if it does not exist in DT.
505 */
506 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
507 if (ret)
508 return 0;
509
510 switch (speed) {
511 case _10BASET:
512 rate = 2500000; /* 2.5 MHz */
513 break;
514 case _100BASET:
515 rate = 25000000; /* 25 MHz */
516 break;
517 case _1000BASET:
518 rate = 125000000; /* 125 MHz */
519 break;
520 default:
521 /* does not change anything */
522 return 0;
523 }
524
525 if (tx_clk.dev) {
526 ret = clk_set_rate(&tx_clk, rate);
527 if (ret)
528 return ret;
529 }
530#endif
531
Bin Menga5e3d232019-05-22 00:09:45 -0700532 return 0;
533}
534#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700535int __weak macb_linkspd_cb(void *regs, unsigned int speed)
536{
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100537 return 0;
538}
Bin Menga5e3d232019-05-22 00:09:45 -0700539#endif
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100540
Wenyou Yanga212b662016-05-17 13:11:35 +0800541#ifdef CONFIG_DM_ETH
542static int macb_phy_init(struct udevice *dev, const char *name)
543#else
Simon Glassd5555b72016-05-05 07:28:09 -0600544static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800545#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100546{
Wenyou Yanga212b662016-05-17 13:11:35 +0800547#ifdef CONFIG_DM_ETH
548 struct macb_device *macb = dev_get_priv(dev);
549#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100550 u32 ncfgr;
551 u16 phy_id, status, adv, lpa;
552 int media, speed, duplex;
Wilson Lee4bf56912017-08-22 20:25:07 -0700553 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100554 int i;
555
Simon Glassd5555b72016-05-05 07:28:09 -0600556 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100557 /* Auto-detect phy_addr */
Wilson Lee4bf56912017-08-22 20:25:07 -0700558 ret = macb_phy_find(macb, name);
559 if (ret)
560 return ret;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100561
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100562 /* Check if the PHY is up to snuff... */
563 phy_id = macb_mdio_read(macb, MII_PHYSID1);
564 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600565 printf("%s: No PHY present\n", name);
Wilson Lee4bf56912017-08-22 20:25:07 -0700566 return -ENODEV;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100567 }
568
Bo Shenb1a00062013-04-24 15:59:27 +0800569#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800570#ifdef CONFIG_DM_ETH
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800571 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yanga212b662016-05-17 13:11:35 +0800572 macb->phy_interface);
573#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800574 /* need to consider other phy interface mode */
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800575 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800576 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800577#endif
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800578 if (!macb->phydev) {
Bo Shen8314ccd2013-08-19 10:35:47 +0800579 printf("phy_connect failed\n");
580 return -ENODEV;
581 }
582
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800583 phy_config(macb->phydev);
Bo Shenb1a00062013-04-24 15:59:27 +0800584#endif
585
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200586 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100587 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200588 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600589 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200590
Andreas Bießmannceef9832014-05-26 22:55:18 +0200591 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100592 status = macb_mdio_read(macb, MII_BMSR);
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100593 if (status & BMSR_LSTATUS) {
594 /*
595 * Delay a bit after the link is established,
596 * so that the next xfer does not fail
597 */
598 mdelay(10);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100599 break;
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100600 }
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200601 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100602 }
603 }
604
605 if (!(status & BMSR_LSTATUS)) {
606 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600607 name, status);
Wilson Lee4bf56912017-08-22 20:25:07 -0700608 return -ENETDOWN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100609 }
Bo Shend256be22013-04-24 15:59:28 +0800610
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100611 /* First check for GMAC and that it is GiB capable */
612 if (gem_is_gigabit_capable(macb)) {
Radu Pirea1b0c9912019-06-07 14:18:35 +0300613 lpa = macb_mdio_read(macb, MII_LPA);
Bo Shend256be22013-04-24 15:59:28 +0800614
Radu Pirea0dc97fc2019-06-07 14:18:36 +0300615 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
616 LPA_1000XHALF)) {
617 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
618 1 : 0);
Andreas Bießmann47609572014-09-18 23:46:48 +0200619
620 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600621 name,
Bo Shend256be22013-04-24 15:59:28 +0800622 duplex ? "full" : "half",
623 lpa);
624
625 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200626 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
627 ncfgr |= GEM_BIT(GBE);
628
Bo Shend256be22013-04-24 15:59:28 +0800629 if (duplex)
630 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200631
Bo Shend256be22013-04-24 15:59:28 +0800632 macb_writel(macb, NCFGR, ncfgr);
633
Bin Menga5e3d232019-05-22 00:09:45 -0700634#ifdef CONFIG_DM_ETH
635 ret = macb_linkspd_cb(dev, _1000BASET);
636#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700637 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700638#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700639 if (ret)
640 return ret;
641
642 return 0;
Bo Shend256be22013-04-24 15:59:28 +0800643 }
644 }
645
646 /* fall back for EMAC checking */
647 adv = macb_mdio_read(macb, MII_ADVERTISE);
648 lpa = macb_mdio_read(macb, MII_LPA);
649 media = mii_nway_result(lpa & adv);
650 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
651 ? 1 : 0);
652 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
653 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600654 name,
Bo Shend256be22013-04-24 15:59:28 +0800655 speed ? "100" : "10",
656 duplex ? "full" : "half",
657 lpa);
658
659 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800660 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee4bf56912017-08-22 20:25:07 -0700661 if (speed) {
Bo Shend256be22013-04-24 15:59:28 +0800662 ncfgr |= MACB_BIT(SPD);
Bin Menga5e3d232019-05-22 00:09:45 -0700663#ifdef CONFIG_DM_ETH
664 ret = macb_linkspd_cb(dev, _100BASET);
665#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700666 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700667#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700668 } else {
Bin Menga5e3d232019-05-22 00:09:45 -0700669#ifdef CONFIG_DM_ETH
670 ret = macb_linkspd_cb(dev, _10BASET);
671#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700672 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700673#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700674 }
675
676 if (ret)
677 return ret;
678
Bo Shend256be22013-04-24 15:59:28 +0800679 if (duplex)
680 ncfgr |= MACB_BIT(FD);
681 macb_writel(macb, NCFGR, ncfgr);
682
Wilson Lee4bf56912017-08-22 20:25:07 -0700683 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100684}
685
Wu, Joshade4ea42015-06-03 16:45:44 +0800686static int gmac_init_multi_queues(struct macb_device *macb)
687{
688 int i, num_queues = 1;
689 u32 queue_mask;
690
691 /* bit 0 is never set but queue 0 always exists */
692 queue_mask = gem_readl(macb, DCFG6) & 0xff;
693 queue_mask |= 0x1;
694
695 for (i = 1; i < MACB_MAX_QUEUES; i++)
696 if (queue_mask & (1 << i))
697 num_queues++;
698
Ramon Fried0a2827e2019-07-16 22:04:33 +0300699 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Joshade4ea42015-06-03 16:45:44 +0800700 macb->dummy_desc->addr = 0;
701 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200702 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Joshade4ea42015-06-03 16:45:44 +0800703
704 for (i = 1; i < num_queues; i++)
705 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
706
707 return 0;
708}
709
Ramon Fried9c295802019-07-16 22:04:36 +0300710static void gmac_configure_dma(struct macb_device *macb)
711{
712 u32 buffer_size;
713 u32 dmacfg;
714
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300715 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Fried9c295802019-07-16 22:04:36 +0300716 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
717 dmacfg |= GEM_BF(RXBS, buffer_size);
718
719 if (macb->dma_burst_length)
720 dmacfg = GEM_BFINS(FBLDO, macb->dma_burst_length, dmacfg);
721
722 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
723 dmacfg &= ~GEM_BIT(ENDIA_PKT);
724
725#ifdef CONFIG_SYS_LITTLE_ENDIAN
726 dmacfg &= ~GEM_BIT(ENDIA_DESC);
727#else
728 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
729#endif
730
731 dmacfg &= ~GEM_BIT(ADDR64);
732 gem_writel(macb, DMACFG, dmacfg);
733}
734
Wenyou Yanga212b662016-05-17 13:11:35 +0800735#ifdef CONFIG_DM_ETH
736static int _macb_init(struct udevice *dev, const char *name)
737#else
Simon Glassd5555b72016-05-05 07:28:09 -0600738static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800739#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100740{
Wenyou Yanga212b662016-05-17 13:11:35 +0800741#ifdef CONFIG_DM_ETH
742 struct macb_device *macb = dev_get_priv(dev);
743#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100744 unsigned long paddr;
Wilson Lee4bf56912017-08-22 20:25:07 -0700745 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100746 int i;
747
748 /*
749 * macb_halt should have been called at some point before now,
750 * so we'll assume the controller is idle.
751 */
752
753 /* initialize DMA descriptors */
754 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200755 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
756 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300757 paddr |= MACB_BIT(RX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100758 macb->rx_ring[i].addr = paddr;
759 macb->rx_ring[i].ctrl = 0;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300760 paddr += macb->rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100761 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800762 macb_flush_ring_desc(macb, RX);
763 macb_flush_rx_buffer(macb);
764
Andreas Bießmannceef9832014-05-26 22:55:18 +0200765 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100766 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200767 if (i == (MACB_TX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300768 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
769 MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100770 else
Ramon Fried0a2827e2019-07-16 22:04:33 +0300771 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100772 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800773 macb_flush_ring_desc(macb, TX);
774
Andreas Bießmannceef9832014-05-26 22:55:18 +0200775 macb->rx_tail = 0;
776 macb->tx_head = 0;
777 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600778 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100779
Wilson Lee4bf56912017-08-22 20:25:07 -0700780#ifdef CONFIG_MACB_ZYNQ
781 macb_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
782#endif
783
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100784 macb_writel(macb, RBQP, macb->rx_ring_dma);
785 macb_writel(macb, TBQP, macb->tx_ring_dma);
786
Bo Shend256be22013-04-24 15:59:28 +0800787 if (macb_is_gem(macb)) {
Ramon Fried9c295802019-07-16 22:04:36 +0300788 /* Initialize DMA properties */
789 gmac_configure_dma(macb);
Wu, Joshade4ea42015-06-03 16:45:44 +0800790 /* Check the multi queue and initialize the queue for tx */
791 gmac_init_multi_queues(macb);
792
Bo Shencabf61c2014-11-10 15:24:01 +0800793 /*
794 * When the GMAC IP with GE feature, this bit is used to
795 * select interface between RGMII and GMII.
796 * When the GMAC IP without GE feature, this bit is used
797 * to select interface between RMII and MII.
798 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800799#ifdef CONFIG_DM_ETH
Wenyou Yang6de046e2017-04-20 11:13:13 +0800800 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
801 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
Ramon Fried6c636512019-07-16 22:03:00 +0300802 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Wenyou Yanga212b662016-05-17 13:11:35 +0800803 else
Ramon Fried6c636512019-07-16 22:03:00 +0300804 gem_writel(macb, USRIO, 0);
Ramon Fried5a1899f2019-07-16 22:04:34 +0300805
806 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
807 unsigned int ncfgr = macb_readl(macb, NCFGR);
808
809 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
810 macb_writel(macb, NCFGR, ncfgr);
811 }
Wenyou Yanga212b662016-05-17 13:11:35 +0800812#else
Bo Shencabf61c2014-11-10 15:24:01 +0800813#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Ramon Fried6c636512019-07-16 22:03:00 +0300814 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Bo Shend256be22013-04-24 15:59:28 +0800815#else
Ramon Fried6c636512019-07-16 22:03:00 +0300816 gem_writel(macb, USRIO, 0);
Bo Shend256be22013-04-24 15:59:28 +0800817#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800818#endif
Bo Shend256be22013-04-24 15:59:28 +0800819 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100820 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800821#ifdef CONFIG_DM_ETH
822#ifdef CONFIG_AT91FAMILY
823 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
824 macb_writel(macb, USRIO,
825 MACB_BIT(RMII) | MACB_BIT(CLKEN));
826 } else {
827 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
828 }
829#else
830 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
831 macb_writel(macb, USRIO, 0);
832 else
833 macb_writel(macb, USRIO, MACB_BIT(MII));
834#endif
835#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100836#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800837#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000838 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
839#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100840 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000841#endif
842#else
Bo Shend8f64b42013-04-24 15:59:26 +0800843#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000844 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100845#else
846 macb_writel(macb, USRIO, MACB_BIT(MII));
847#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000848#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800849#endif
Bo Shend256be22013-04-24 15:59:28 +0800850 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100851
Wenyou Yanga212b662016-05-17 13:11:35 +0800852#ifdef CONFIG_DM_ETH
Wilson Lee4bf56912017-08-22 20:25:07 -0700853 ret = macb_phy_init(dev, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800854#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700855 ret = macb_phy_init(macb, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800856#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700857 if (ret)
858 return ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100859
860 /* Enable TX and RX */
861 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
862
Ben Warren422b1a02008-01-09 18:15:53 -0500863 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100864}
865
Simon Glassd5555b72016-05-05 07:28:09 -0600866static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100867{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100868 u32 ncr, tsr;
869
870 /* Halt the controller and wait for any ongoing transmission to end. */
871 ncr = macb_readl(macb, NCR);
872 ncr |= MACB_BIT(THALT);
873 macb_writel(macb, NCR, ncr);
874
875 do {
876 tsr = macb_readl(macb, TSR);
877 } while (tsr & MACB_BIT(TGO));
878
879 /* Disable TX and RX, and clear statistics */
880 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
881}
882
Simon Glassd5555b72016-05-05 07:28:09 -0600883static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700884{
Ben Warren6bb46792010-06-01 11:55:42 -0700885 u32 hwaddr_bottom;
886 u16 hwaddr_top;
887
888 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600889 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
890 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700891 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600892 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700893 macb_writel(macb, SA1T, hwaddr_top);
894 return 0;
895}
896
Bo Shend256be22013-04-24 15:59:28 +0800897static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
898{
899 u32 config;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800900#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800901 unsigned long macb_hz = macb->pclk_rate;
902#else
Bo Shend256be22013-04-24 15:59:28 +0800903 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800904#endif
Bo Shend256be22013-04-24 15:59:28 +0800905
906 if (macb_hz < 20000000)
907 config = MACB_BF(CLK, MACB_CLK_DIV8);
908 else if (macb_hz < 40000000)
909 config = MACB_BF(CLK, MACB_CLK_DIV16);
910 else if (macb_hz < 80000000)
911 config = MACB_BF(CLK, MACB_CLK_DIV32);
912 else
913 config = MACB_BF(CLK, MACB_CLK_DIV64);
914
915 return config;
916}
917
918static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
919{
920 u32 config;
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800921
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800922#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800923 unsigned long macb_hz = macb->pclk_rate;
924#else
Bo Shend256be22013-04-24 15:59:28 +0800925 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800926#endif
Bo Shend256be22013-04-24 15:59:28 +0800927
928 if (macb_hz < 20000000)
929 config = GEM_BF(CLK, GEM_CLK_DIV8);
930 else if (macb_hz < 40000000)
931 config = GEM_BF(CLK, GEM_CLK_DIV16);
932 else if (macb_hz < 80000000)
933 config = GEM_BF(CLK, GEM_CLK_DIV32);
934 else if (macb_hz < 120000000)
935 config = GEM_BF(CLK, GEM_CLK_DIV48);
936 else if (macb_hz < 160000000)
937 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Fried9e65f802019-07-16 22:04:32 +0300938 else if (macb_hz < 240000000)
Bo Shend256be22013-04-24 15:59:28 +0800939 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Fried9e65f802019-07-16 22:04:32 +0300940 else if (macb_hz < 320000000)
941 config = GEM_BF(CLK, GEM_CLK_DIV128);
942 else
943 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shend256be22013-04-24 15:59:28 +0800944
945 return config;
946}
947
Bo Shen32e4f6b2013-09-18 15:07:44 +0800948/*
949 * Get the DMA bus width field of the network configuration register that we
950 * should program. We find the width from decoding the design configuration
951 * register to find the maximum supported data bus width.
952 */
953static u32 macb_dbw(struct macb_device *macb)
954{
955 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
956 case 4:
957 return GEM_BF(DBW, GEM_DBW128);
958 case 2:
959 return GEM_BF(DBW, GEM_DBW64);
960 case 1:
961 default:
962 return GEM_BF(DBW, GEM_DBW32);
963 }
964}
965
Simon Glassd5555b72016-05-05 07:28:09 -0600966static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100967{
Simon Glassd5555b72016-05-05 07:28:09 -0600968 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100969 u32 ncfgr;
970
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300971 if (macb_is_gem(macb))
972 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
973 else
974 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
975
Simon Glassd5555b72016-05-05 07:28:09 -0600976 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300977 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
978 MACB_RX_RING_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100979 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800980 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100981 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800982 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100983 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +0800984 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
985 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100986
Simon Glassd5555b72016-05-05 07:28:09 -0600987 /*
988 * Do some basic initialization so that we at least can talk
989 * to the PHY
990 */
991 if (macb_is_gem(macb)) {
992 ncfgr = gem_mdc_clk_div(id, macb);
993 ncfgr |= macb_dbw(macb);
994 } else {
995 ncfgr = macb_mdc_clk_div(id, macb);
996 }
997
998 macb_writel(macb, NCFGR, ncfgr);
999}
1000
Simon Glassf1dcc192016-05-05 07:28:11 -06001001#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -06001002static int macb_send(struct eth_device *netdev, void *packet, int length)
1003{
1004 struct macb_device *macb = to_macb(netdev);
1005
1006 return _macb_send(macb, netdev->name, packet, length);
1007}
1008
1009static int macb_recv(struct eth_device *netdev)
1010{
1011 struct macb_device *macb = to_macb(netdev);
1012 uchar *packet;
1013 int length;
1014
1015 macb->wrapped = false;
1016 for (;;) {
1017 macb->next_rx_tail = macb->rx_tail;
1018 length = _macb_recv(macb, &packet);
1019 if (length >= 0) {
1020 net_process_received_packet(packet, length);
1021 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6cdf0722018-03-18 11:32:53 +01001022 } else {
Simon Glassd5555b72016-05-05 07:28:09 -06001023 return length;
1024 }
1025 }
1026}
1027
1028static int macb_init(struct eth_device *netdev, bd_t *bd)
1029{
1030 struct macb_device *macb = to_macb(netdev);
1031
1032 return _macb_init(macb, netdev->name);
1033}
1034
1035static void macb_halt(struct eth_device *netdev)
1036{
1037 struct macb_device *macb = to_macb(netdev);
1038
1039 return _macb_halt(macb);
1040}
1041
1042static int macb_write_hwaddr(struct eth_device *netdev)
1043{
1044 struct macb_device *macb = to_macb(netdev);
1045
1046 return _macb_write_hwaddr(macb, netdev->enetaddr);
1047}
1048
1049int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1050{
1051 struct macb_device *macb;
1052 struct eth_device *netdev;
1053
1054 macb = malloc(sizeof(struct macb_device));
1055 if (!macb) {
1056 printf("Error: Failed to allocate memory for MACB%d\n", id);
1057 return -1;
1058 }
1059 memset(macb, 0, sizeof(struct macb_device));
1060
1061 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +08001062
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001063 macb->regs = regs;
1064 macb->phy_addr = phy_addr;
1065
Bo Shend256be22013-04-24 15:59:28 +08001066 if (macb_is_gem(macb))
1067 sprintf(netdev->name, "gmac%d", id);
1068 else
1069 sprintf(netdev->name, "macb%d", id);
1070
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001071 netdev->init = macb_init;
1072 netdev->halt = macb_halt;
1073 netdev->send = macb_send;
1074 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -07001075 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001076
Simon Glassd5555b72016-05-05 07:28:09 -06001077 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001078
1079 eth_register(netdev);
1080
Bo Shenb1a00062013-04-24 15:59:27 +08001081#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001082 int retval;
1083 struct mii_dev *mdiodev = mdio_alloc();
1084 if (!mdiodev)
1085 return -ENOMEM;
1086 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1087 mdiodev->read = macb_miiphy_read;
1088 mdiodev->write = macb_miiphy_write;
1089
1090 retval = mdio_register(mdiodev);
1091 if (retval < 0)
1092 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +08001093 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +02001094#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001095 return 0;
1096}
Simon Glassf1dcc192016-05-05 07:28:11 -06001097#endif /* !CONFIG_DM_ETH */
1098
1099#ifdef CONFIG_DM_ETH
1100
1101static int macb_start(struct udevice *dev)
1102{
Wenyou Yanga212b662016-05-17 13:11:35 +08001103 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -06001104}
1105
1106static int macb_send(struct udevice *dev, void *packet, int length)
1107{
1108 struct macb_device *macb = dev_get_priv(dev);
1109
1110 return _macb_send(macb, dev->name, packet, length);
1111}
1112
1113static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1114{
1115 struct macb_device *macb = dev_get_priv(dev);
1116
1117 macb->next_rx_tail = macb->rx_tail;
1118 macb->wrapped = false;
1119
1120 return _macb_recv(macb, packetp);
1121}
1122
1123static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1124{
1125 struct macb_device *macb = dev_get_priv(dev);
1126
1127 reclaim_rx_buffers(macb, macb->next_rx_tail);
1128
1129 return 0;
1130}
1131
1132static void macb_stop(struct udevice *dev)
1133{
1134 struct macb_device *macb = dev_get_priv(dev);
1135
1136 _macb_halt(macb);
1137}
1138
1139static int macb_write_hwaddr(struct udevice *dev)
1140{
1141 struct eth_pdata *plat = dev_get_platdata(dev);
1142 struct macb_device *macb = dev_get_priv(dev);
1143
1144 return _macb_write_hwaddr(macb, plat->enetaddr);
1145}
1146
1147static const struct eth_ops macb_eth_ops = {
1148 .start = macb_start,
1149 .send = macb_send,
1150 .recv = macb_recv,
1151 .stop = macb_stop,
1152 .free_pkt = macb_free_pkt,
1153 .write_hwaddr = macb_write_hwaddr,
1154};
1155
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001156#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001157static int macb_enable_clk(struct udevice *dev)
1158{
1159 struct macb_device *macb = dev_get_priv(dev);
1160 struct clk clk;
1161 ulong clk_rate;
1162 int ret;
1163
1164 ret = clk_get_by_index(dev, 0, &clk);
1165 if (ret)
1166 return -EINVAL;
1167
Wilson Lee4bf56912017-08-22 20:25:07 -07001168 /*
Anup Patel2e242f52019-02-25 08:14:36 +00001169 * If clock driver didn't support enable or disable then
1170 * we get -ENOSYS from clk_enable(). To handle this, we
1171 * don't fail for ret == -ENOSYS.
Wilson Lee4bf56912017-08-22 20:25:07 -07001172 */
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001173 ret = clk_enable(&clk);
Anup Patel2e242f52019-02-25 08:14:36 +00001174 if (ret && ret != -ENOSYS)
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001175 return ret;
1176
1177 clk_rate = clk_get_rate(&clk);
1178 if (!clk_rate)
1179 return -EINVAL;
1180
1181 macb->pclk_rate = clk_rate;
1182
1183 return 0;
1184}
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001185#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001186
Ramon Frieded3c64f2019-07-16 22:04:35 +03001187static const struct macb_config default_gem_config = {
1188 .dma_burst_length = 16,
1189};
1190
Simon Glassf1dcc192016-05-05 07:28:11 -06001191static int macb_eth_probe(struct udevice *dev)
1192{
Ramon Frieded3c64f2019-07-16 22:04:35 +03001193 const struct macb_config *macb_config;
Simon Glassf1dcc192016-05-05 07:28:11 -06001194 struct eth_pdata *pdata = dev_get_platdata(dev);
1195 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yanga212b662016-05-17 13:11:35 +08001196 const char *phy_mode;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001197 __maybe_unused int ret;
Wenyou Yanga212b662016-05-17 13:11:35 +08001198
Simon Glasse160f7d2017-01-17 16:52:55 -07001199 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1200 NULL);
Wenyou Yanga212b662016-05-17 13:11:35 +08001201 if (phy_mode)
1202 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1203 if (macb->phy_interface == -1) {
1204 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1205 return -EINVAL;
1206 }
Wenyou Yanga212b662016-05-17 13:11:35 +08001207
Simon Glassf1dcc192016-05-05 07:28:11 -06001208 macb->regs = (void *)pdata->iobase;
1209
Ramon Frieded3c64f2019-07-16 22:04:35 +03001210 macb_config = (struct macb_config *)dev_get_driver_data(dev);
1211 if (!macb_config)
1212 macb_config = &default_gem_config;
1213
1214 macb->dma_burst_length = macb_config->dma_burst_length;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001215#ifdef CONFIG_CLK
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001216 ret = macb_enable_clk(dev);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001217 if (ret)
1218 return ret;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001219#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001220
Simon Glassf1dcc192016-05-05 07:28:11 -06001221 _macb_eth_initialize(macb);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001222
Simon Glassf1dcc192016-05-05 07:28:11 -06001223#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001224 macb->bus = mdio_alloc();
1225 if (!macb->bus)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001226 return -ENOMEM;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001227 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1228 macb->bus->read = macb_miiphy_read;
1229 macb->bus->write = macb_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -05001230
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001231 ret = mdio_register(macb->bus);
1232 if (ret < 0)
1233 return ret;
Simon Glassf1dcc192016-05-05 07:28:11 -06001234 macb->bus = miiphy_get_dev_by_name(dev->name);
1235#endif
1236
1237 return 0;
1238}
1239
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001240static int macb_eth_remove(struct udevice *dev)
1241{
1242 struct macb_device *macb = dev_get_priv(dev);
1243
1244#ifdef CONFIG_PHYLIB
1245 free(macb->phydev);
1246#endif
1247 mdio_unregister(macb->bus);
1248 mdio_free(macb->bus);
1249
1250 return 0;
1251}
1252
Wilson Lee4bf56912017-08-22 20:25:07 -07001253/**
1254 * macb_late_eth_ofdata_to_platdata
1255 * @dev: udevice struct
1256 * Returns 0 when operation success and negative errno number
1257 * when operation failed.
1258 */
1259int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1260{
1261 return 0;
1262}
1263
Simon Glassf1dcc192016-05-05 07:28:11 -06001264static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1265{
1266 struct eth_pdata *pdata = dev_get_platdata(dev);
1267
Ramon Fried9043c4e2018-12-27 19:58:42 +02001268 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1269 if (!pdata->iobase)
1270 return -EINVAL;
Wilson Lee4bf56912017-08-22 20:25:07 -07001271
1272 return macb_late_eth_ofdata_to_platdata(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -06001273}
1274
Ramon Frieded3c64f2019-07-16 22:04:35 +03001275static const struct macb_config sama5d4_config = {
1276 .dma_burst_length = 4,
1277};
1278
Simon Glassf1dcc192016-05-05 07:28:11 -06001279static const struct udevice_id macb_eth_ids[] = {
1280 { .compatible = "cdns,macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001281 { .compatible = "cdns,at91sam9260-macb" },
1282 { .compatible = "atmel,sama5d2-gem" },
1283 { .compatible = "atmel,sama5d3-gem" },
Ramon Frieded3c64f2019-07-16 22:04:35 +03001284 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee4bf56912017-08-22 20:25:07 -07001285 { .compatible = "cdns,zynq-gem" },
Simon Glassf1dcc192016-05-05 07:28:11 -06001286 { }
1287};
1288
1289U_BOOT_DRIVER(eth_macb) = {
1290 .name = "eth_macb",
1291 .id = UCLASS_ETH,
1292 .of_match = macb_eth_ids,
1293 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1294 .probe = macb_eth_probe,
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001295 .remove = macb_eth_remove,
Simon Glassf1dcc192016-05-05 07:28:11 -06001296 .ops = &macb_eth_ops,
1297 .priv_auto_alloc_size = sizeof(struct macb_device),
1298 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1299};
1300#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001301
Jon Loeliger07d38a12007-07-09 17:30:01 -05001302#endif