blob: 035109dc4372e5f55ebc644a9539990cf41000b7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01002/*
3 * Copyright (C) 2005-2006 Atmel Corporation
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01004 */
5#include <common.h>
Wenyou Yang3d8d3482016-11-02 10:06:56 +08006#include <clk.h>
Simon Glass75c5d182016-05-05 07:28:11 -06007#include <dm.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01008
Haavard Skinnemoen51c8f242006-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-VILLARD03836942008-10-16 15:01:15 +020020 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen51c8f242006-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 Glass75c5d182016-05-05 07:28:11 -060032#ifndef CONFIG_DM_ETH
Ben Warren2f2b6b62008-08-31 22:22:04 -070033#include <netdev.h>
Simon Glass75c5d182016-05-05 07:28:11 -060034#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010035#include <malloc.h>
Semih Hazar790088e2009-12-17 15:07:15 +020036#include <miiphy.h>
Haavard Skinnemoen51c8f242006-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 Yamada64e4f7f2016-09-21 11:28:57 +090042#include <linux/errno.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010043
44#include "macb.h"
45
Wenyou Yang7b811852016-05-17 13:11:35 +080046DECLARE_GLOBAL_DATA_PTR;
47
Andreas Bießmann1e868122014-05-26 22:55:18 +020048#define MACB_RX_BUFFER_SIZE 4096
49#define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
Ramon Friedb40501f2019-07-16 22:04:36 +030050#define RX_BUFFER_MULTIPLE 64
Andreas Bießmann1e868122014-05-26 22:55:18 +020051#define MACB_TX_RING_SIZE 16
52#define MACB_TX_TIMEOUT 1000
53#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010054
Wilson Lee41d6d1e2017-08-22 20:25:07 -070055#ifdef CONFIG_MACB_ZYNQ
56/* INCR4 AHB bursts */
57#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
58/* Use full configured addressable space (8 Kb) */
59#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
60/* Use full configured addressable space (4 Kb) */
61#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
62/* Set RXBUF with use of 128 byte */
63#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
64#define MACB_ZYNQ_GEM_DMACR_INIT \
65 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
66 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
67 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
68 MACB_ZYNQ_GEM_DMACR_RXBUF)
69#endif
70
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010071struct macb_dma_desc {
72 u32 addr;
73 u32 ctrl;
74};
75
Wu, Josh18052402014-05-27 16:31:05 +080076#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
77#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
78#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Josh012d68d2015-06-03 16:45:44 +080079#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh18052402014-05-27 16:31:05 +080080
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010081#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010082#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010083
84struct macb_device {
85 void *regs;
Ramon Fried834040c2019-07-16 22:04:35 +030086 unsigned int dma_burst_length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010087
88 unsigned int rx_tail;
89 unsigned int tx_head;
90 unsigned int tx_tail;
Simon Glass5ad27512016-05-05 07:28:09 -060091 unsigned int next_rx_tail;
92 bool wrapped;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010093
94 void *rx_buffer;
95 void *tx_buffer;
96 struct macb_dma_desc *rx_ring;
97 struct macb_dma_desc *tx_ring;
98
99 unsigned long rx_buffer_dma;
100 unsigned long rx_ring_dma;
101 unsigned long tx_ring_dma;
102
Wu, Josh012d68d2015-06-03 16:45:44 +0800103 struct macb_dma_desc *dummy_desc;
104 unsigned long dummy_desc_dma;
105
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100106 const struct device *dev;
Simon Glass75c5d182016-05-05 07:28:11 -0600107#ifndef CONFIG_DM_ETH
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100108 struct eth_device netdev;
Simon Glass75c5d182016-05-05 07:28:11 -0600109#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100110 unsigned short phy_addr;
Bo Shen7d91deb2013-04-24 15:59:27 +0800111 struct mii_dev *bus;
Wenyou Yang44835ea2017-04-14 14:36:04 +0800112#ifdef CONFIG_PHYLIB
113 struct phy_device *phydev;
114#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800115
116#ifdef CONFIG_DM_ETH
Wenyou Yang19449362017-02-14 16:24:40 +0800117#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800118 unsigned long pclk_rate;
Wenyou Yang19449362017-02-14 16:24:40 +0800119#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800120 phy_interface_t phy_interface;
121#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100122};
Ramon Fried834040c2019-07-16 22:04:35 +0300123
124struct macb_config {
125 unsigned int dma_burst_length;
126};
127
Simon Glass75c5d182016-05-05 07:28:11 -0600128#ifndef CONFIG_DM_ETH
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100129#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glass75c5d182016-05-05 07:28:11 -0600130#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100131
Bo Shen6f7d7d92013-04-24 15:59:28 +0800132static int macb_is_gem(struct macb_device *macb)
133{
Atish Patrae1a85182019-02-25 08:14:42 +0000134 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800135}
136
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100137#ifndef cpu_is_sama5d2
138#define cpu_is_sama5d2() 0
139#endif
140
141#ifndef cpu_is_sama5d4
142#define cpu_is_sama5d4() 0
143#endif
144
145static int gem_is_gigabit_capable(struct macb_device *macb)
146{
147 /*
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400148 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100149 * configured to support only 10/100.
150 */
151 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
152}
153
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100154static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
155{
156 unsigned long netctl;
157 unsigned long netstat;
158 unsigned long frame;
159
160 netctl = macb_readl(macb, NCR);
161 netctl |= MACB_BIT(MPE);
162 macb_writel(macb, NCR, netctl);
163
164 frame = (MACB_BF(SOF, 1)
165 | MACB_BF(RW, 1)
166 | MACB_BF(PHYA, macb->phy_addr)
167 | MACB_BF(REGA, reg)
168 | MACB_BF(CODE, 2)
169 | MACB_BF(DATA, value));
170 macb_writel(macb, MAN, frame);
171
172 do {
173 netstat = macb_readl(macb, NSR);
174 } while (!(netstat & MACB_BIT(IDLE)));
175
176 netctl = macb_readl(macb, NCR);
177 netctl &= ~MACB_BIT(MPE);
178 macb_writel(macb, NCR, netctl);
179}
180
181static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
182{
183 unsigned long netctl;
184 unsigned long netstat;
185 unsigned long frame;
186
187 netctl = macb_readl(macb, NCR);
188 netctl |= MACB_BIT(MPE);
189 macb_writel(macb, NCR, netctl);
190
191 frame = (MACB_BF(SOF, 1)
192 | MACB_BF(RW, 2)
193 | MACB_BF(PHYA, macb->phy_addr)
194 | MACB_BF(REGA, reg)
195 | MACB_BF(CODE, 2));
196 macb_writel(macb, MAN, frame);
197
198 do {
199 netstat = macb_readl(macb, NSR);
200 } while (!(netstat & MACB_BIT(IDLE)));
201
202 frame = macb_readl(macb, MAN);
203
204 netctl = macb_readl(macb, NCR);
205 netctl &= ~MACB_BIT(MPE);
206 macb_writel(macb, NCR, netctl);
207
208 return MACB_BFEXT(DATA, frame);
209}
210
Joe Hershberger9e5742b2013-06-24 19:06:38 -0500211void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim77cdf0f2012-12-13 17:22:52 +0530212{
213 return;
214}
215
Bo Shen7d91deb2013-04-24 15:59:27 +0800216#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar790088e2009-12-17 15:07:15 +0200217
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500218int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar790088e2009-12-17 15:07:15 +0200219{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500220 u16 value = 0;
Simon Glass75c5d182016-05-05 07:28:11 -0600221#ifdef CONFIG_DM_ETH
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500222 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600223 struct macb_device *macb = dev_get_priv(dev);
224#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500225 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar790088e2009-12-17 15:07:15 +0200226 struct macb_device *macb = to_macb(dev);
Simon Glass75c5d182016-05-05 07:28:11 -0600227#endif
Semih Hazar790088e2009-12-17 15:07:15 +0200228
Andreas Bießmann1e868122014-05-26 22:55:18 +0200229 if (macb->phy_addr != phy_adr)
Semih Hazar790088e2009-12-17 15:07:15 +0200230 return -1;
231
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500232 arch_get_mdio_control(bus->name);
233 value = macb_mdio_read(macb, reg);
Semih Hazar790088e2009-12-17 15:07:15 +0200234
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500235 return value;
Semih Hazar790088e2009-12-17 15:07:15 +0200236}
237
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500238int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
239 u16 value)
Semih Hazar790088e2009-12-17 15:07:15 +0200240{
Simon Glass75c5d182016-05-05 07:28:11 -0600241#ifdef CONFIG_DM_ETH
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500242 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600243 struct macb_device *macb = dev_get_priv(dev);
244#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500245 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar790088e2009-12-17 15:07:15 +0200246 struct macb_device *macb = to_macb(dev);
Simon Glass75c5d182016-05-05 07:28:11 -0600247#endif
Semih Hazar790088e2009-12-17 15:07:15 +0200248
Andreas Bießmann1e868122014-05-26 22:55:18 +0200249 if (macb->phy_addr != phy_adr)
Semih Hazar790088e2009-12-17 15:07:15 +0200250 return -1;
251
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500252 arch_get_mdio_control(bus->name);
Semih Hazar790088e2009-12-17 15:07:15 +0200253 macb_mdio_write(macb, reg, value);
254
255 return 0;
256}
257#endif
258
Wu, Josh18052402014-05-27 16:31:05 +0800259#define RX 1
260#define TX 0
261static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
262{
263 if (rx)
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200264 invalidate_dcache_range(macb->rx_ring_dma,
265 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
266 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800267 else
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200268 invalidate_dcache_range(macb->tx_ring_dma,
269 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
270 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800271}
272
273static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
274{
275 if (rx)
276 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200277 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800278 else
279 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200280 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800281}
282
283static inline void macb_flush_rx_buffer(struct macb_device *macb)
284{
285 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200286 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800287}
288
289static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
290{
291 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200292 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800293}
Semih Hazar790088e2009-12-17 15:07:15 +0200294
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500295#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100296
Simon Glass5ad27512016-05-05 07:28:09 -0600297static int _macb_send(struct macb_device *macb, const char *name, void *packet,
298 int length)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100299{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100300 unsigned long paddr, ctrl;
301 unsigned int tx_head = macb->tx_head;
302 int i;
303
304 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
305
306 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried6402fb192019-07-16 22:04:33 +0300307 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200308 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300309 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100310 macb->tx_head = 0;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200311 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100312 macb->tx_head++;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200313 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100314
315 macb->tx_ring[tx_head].ctrl = ctrl;
316 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200317 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800318 macb_flush_ring_desc(macb, TX);
319 /* Do we need check paddr and length is dcache line aligned? */
Simon Glass3d5dcef2016-05-05 07:28:10 -0600320 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100321 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
322
323 /*
324 * I guess this is necessary because the networking core may
325 * re-use the transmit buffer as soon as we return...
326 */
Andreas Bießmann1e868122014-05-26 22:55:18 +0200327 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200328 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800329 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200330 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300331 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100332 break;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100333 udelay(1);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100334 }
335
336 dma_unmap_single(packet, length, paddr);
337
Andreas Bießmann1e868122014-05-26 22:55:18 +0200338 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300339 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glass5ad27512016-05-05 07:28:09 -0600340 printf("%s: TX underrun\n", name);
Ramon Fried6402fb192019-07-16 22:04:33 +0300341 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glass5ad27512016-05-05 07:28:09 -0600342 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200343 } else {
Simon Glass5ad27512016-05-05 07:28:09 -0600344 printf("%s: TX timeout\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100345 }
346
347 /* No one cares anyway */
348 return 0;
349}
350
351static void reclaim_rx_buffers(struct macb_device *macb,
352 unsigned int new_tail)
353{
354 unsigned int i;
355
356 i = macb->rx_tail;
Wu, Josh18052402014-05-27 16:31:05 +0800357
358 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100359 while (i > new_tail) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300360 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100361 i++;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200362 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100363 i = 0;
364 }
365
366 while (i < new_tail) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300367 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100368 i++;
369 }
370
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200371 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800372 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100373 macb->rx_tail = new_tail;
374}
375
Simon Glass5ad27512016-05-05 07:28:09 -0600376static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100377{
Simon Glass5ad27512016-05-05 07:28:09 -0600378 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100379 void *buffer;
380 int length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100381 u32 status;
382
Simon Glass5ad27512016-05-05 07:28:09 -0600383 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100384 for (;;) {
Wu, Josh18052402014-05-27 16:31:05 +0800385 macb_invalidate_ring_desc(macb, RX);
386
Ramon Fried6402fb192019-07-16 22:04:33 +0300387 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glass5ad27512016-05-05 07:28:09 -0600388 return -EAGAIN;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100389
Simon Glass5ad27512016-05-05 07:28:09 -0600390 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300391 if (status & MACB_BIT(RX_SOF)) {
Simon Glass5ad27512016-05-05 07:28:09 -0600392 if (next_rx_tail != macb->rx_tail)
393 reclaim_rx_buffers(macb, next_rx_tail);
394 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100395 }
396
Ramon Fried6402fb192019-07-16 22:04:33 +0300397 if (status & MACB_BIT(RX_EOF)) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100398 buffer = macb->rx_buffer + 128 * macb->rx_tail;
399 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh18052402014-05-27 16:31:05 +0800400
401 macb_invalidate_rx_buffer(macb);
Simon Glass5ad27512016-05-05 07:28:09 -0600402 if (macb->wrapped) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100403 unsigned int headlen, taillen;
404
Andreas Bießmann1e868122014-05-26 22:55:18 +0200405 headlen = 128 * (MACB_RX_RING_SIZE
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100406 - macb->rx_tail);
407 taillen = length - headlen;
Joe Hershberger9f09a362015-04-08 01:41:06 -0500408 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100409 buffer, headlen);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500410 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100411 macb->rx_buffer, taillen);
Simon Glass5ad27512016-05-05 07:28:09 -0600412 *packetp = (void *)net_rx_packets[0];
413 } else {
414 *packetp = buffer;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100415 }
416
Simon Glass5ad27512016-05-05 07:28:09 -0600417 if (++next_rx_tail >= MACB_RX_RING_SIZE)
418 next_rx_tail = 0;
419 macb->next_rx_tail = next_rx_tail;
420 return length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100421 } else {
Simon Glass5ad27512016-05-05 07:28:09 -0600422 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
423 macb->wrapped = true;
424 next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100425 }
426 }
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200427 barrier();
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100428 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100429}
430
Simon Glass5ad27512016-05-05 07:28:09 -0600431static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100432{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100433 int i;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200434 u16 status, adv;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100435
436 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
437 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glass5ad27512016-05-05 07:28:09 -0600438 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100439 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
440 | BMCR_ANRESTART));
441
Andreas Bießmann1e868122014-05-26 22:55:18 +0200442 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100443 status = macb_mdio_read(macb, MII_BMSR);
444 if (status & BMSR_ANEGCOMPLETE)
445 break;
446 udelay(100);
447 }
448
449 if (status & BMSR_ANEGCOMPLETE)
Simon Glass5ad27512016-05-05 07:28:09 -0600450 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100451 else
452 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600453 name, status);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200454}
455
Wenyou Yang7b811852016-05-17 13:11:35 +0800456static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100457{
458 int i;
459 u16 phy_id;
460
461 /* Search for PHY... */
462 for (i = 0; i < 32; i++) {
463 macb->phy_addr = i;
464 phy_id = macb_mdio_read(macb, MII_PHYSID1);
465 if (phy_id != 0xffff) {
Wenyou Yang7b811852016-05-17 13:11:35 +0800466 printf("%s: PHY present at %d\n", name, i);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700467 return 0;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100468 }
469 }
470
471 /* PHY isn't up to snuff */
Wenyou Yang7b811852016-05-17 13:11:35 +0800472 printf("%s: PHY not found\n", name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100473
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700474 return -ENODEV;
475}
476
477/**
478 * macb_linkspd_cb - Linkspeed change callback function
Bin Mengcf821322019-05-22 00:09:45 -0700479 * @dev/@regs: MACB udevice (DM version) or
480 * Base Register of MACB devices (non-DM version)
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700481 * @speed: Linkspeed
482 * Returns 0 when operation success and negative errno number
483 * when operation failed.
484 */
Bin Mengcf821322019-05-22 00:09:45 -0700485#ifdef CONFIG_DM_ETH
486int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
487{
Bin Meng12766ca2019-05-22 00:09:46 -0700488#ifdef CONFIG_CLK
489 struct clk tx_clk;
490 ulong rate;
491 int ret;
492
493 /*
494 * "tx_clk" is an optional clock source for MACB.
495 * Ignore if it does not exist in DT.
496 */
497 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
498 if (ret)
499 return 0;
500
501 switch (speed) {
502 case _10BASET:
503 rate = 2500000; /* 2.5 MHz */
504 break;
505 case _100BASET:
506 rate = 25000000; /* 25 MHz */
507 break;
508 case _1000BASET:
509 rate = 125000000; /* 125 MHz */
510 break;
511 default:
512 /* does not change anything */
513 return 0;
514 }
515
516 if (tx_clk.dev) {
517 ret = clk_set_rate(&tx_clk, rate);
518 if (ret)
519 return ret;
520 }
521#endif
522
Bin Mengcf821322019-05-22 00:09:45 -0700523 return 0;
524}
525#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700526int __weak macb_linkspd_cb(void *regs, unsigned int speed)
527{
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100528 return 0;
529}
Bin Mengcf821322019-05-22 00:09:45 -0700530#endif
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100531
Wenyou Yang7b811852016-05-17 13:11:35 +0800532#ifdef CONFIG_DM_ETH
533static int macb_phy_init(struct udevice *dev, const char *name)
534#else
Simon Glass5ad27512016-05-05 07:28:09 -0600535static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yang7b811852016-05-17 13:11:35 +0800536#endif
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200537{
Wenyou Yang7b811852016-05-17 13:11:35 +0800538#ifdef CONFIG_DM_ETH
539 struct macb_device *macb = dev_get_priv(dev);
540#endif
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200541 u32 ncfgr;
542 u16 phy_id, status, adv, lpa;
543 int media, speed, duplex;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700544 int ret;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200545 int i;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100546
Simon Glass5ad27512016-05-05 07:28:09 -0600547 arch_get_mdio_control(name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100548 /* Auto-detect phy_addr */
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700549 ret = macb_phy_find(macb, name);
550 if (ret)
551 return ret;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100552
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200553 /* Check if the PHY is up to snuff... */
554 phy_id = macb_mdio_read(macb, MII_PHYSID1);
555 if (phy_id == 0xffff) {
Simon Glass5ad27512016-05-05 07:28:09 -0600556 printf("%s: No PHY present\n", name);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700557 return -ENODEV;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200558 }
559
Bo Shen7d91deb2013-04-24 15:59:27 +0800560#ifdef CONFIG_PHYLIB
Wenyou Yang7b811852016-05-17 13:11:35 +0800561#ifdef CONFIG_DM_ETH
Wenyou Yang44835ea2017-04-14 14:36:04 +0800562 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yang7b811852016-05-17 13:11:35 +0800563 macb->phy_interface);
564#else
Bo Shene04fe552013-08-19 10:35:47 +0800565 /* need to consider other phy interface mode */
Wenyou Yang44835ea2017-04-14 14:36:04 +0800566 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shene04fe552013-08-19 10:35:47 +0800567 PHY_INTERFACE_MODE_RGMII);
Wenyou Yang7b811852016-05-17 13:11:35 +0800568#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +0800569 if (!macb->phydev) {
Bo Shene04fe552013-08-19 10:35:47 +0800570 printf("phy_connect failed\n");
571 return -ENODEV;
572 }
573
Wenyou Yang44835ea2017-04-14 14:36:04 +0800574 phy_config(macb->phydev);
Bo Shen7d91deb2013-04-24 15:59:27 +0800575#endif
576
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200577 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100578 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200579 /* Try to re-negotiate if we don't have link already. */
Simon Glass5ad27512016-05-05 07:28:09 -0600580 macb_phy_reset(macb, name);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200581
Andreas Bießmann1e868122014-05-26 22:55:18 +0200582 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100583 status = macb_mdio_read(macb, MII_BMSR);
Stefan Roese40291802019-03-27 11:20:19 +0100584 if (status & BMSR_LSTATUS) {
585 /*
586 * Delay a bit after the link is established,
587 * so that the next xfer does not fail
588 */
589 mdelay(10);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100590 break;
Stefan Roese40291802019-03-27 11:20:19 +0100591 }
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200592 udelay(100);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100593 }
594 }
595
596 if (!(status & BMSR_LSTATUS)) {
597 printf("%s: link down (status: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600598 name, status);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700599 return -ENETDOWN;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800600 }
601
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100602 /* First check for GMAC and that it is GiB capable */
603 if (gem_is_gigabit_capable(macb)) {
Radu Pirea015800c2019-06-07 14:18:35 +0300604 lpa = macb_mdio_read(macb, MII_LPA);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100605
Radu Pirea1676dfb2019-06-07 14:18:36 +0300606 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
607 LPA_1000XHALF)) {
608 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
609 1 : 0);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200610
611 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600612 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800613 duplex ? "full" : "half",
614 lpa);
615
616 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200617 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
618 ncfgr |= GEM_BIT(GBE);
619
Bo Shen6f7d7d92013-04-24 15:59:28 +0800620 if (duplex)
621 ncfgr |= MACB_BIT(FD);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200622
Bo Shen6f7d7d92013-04-24 15:59:28 +0800623 macb_writel(macb, NCFGR, ncfgr);
624
Bin Mengcf821322019-05-22 00:09:45 -0700625#ifdef CONFIG_DM_ETH
626 ret = macb_linkspd_cb(dev, _1000BASET);
627#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700628 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700629#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700630 if (ret)
631 return ret;
632
633 return 0;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800634 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100635 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800636
637 /* fall back for EMAC checking */
638 adv = macb_mdio_read(macb, MII_ADVERTISE);
639 lpa = macb_mdio_read(macb, MII_LPA);
640 media = mii_nway_result(lpa & adv);
641 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
642 ? 1 : 0);
643 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
644 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600645 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800646 speed ? "100" : "10",
647 duplex ? "full" : "half",
648 lpa);
649
650 ncfgr = macb_readl(macb, NCFGR);
Bo Shenfe19ef32015-03-04 13:35:16 +0800651 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700652 if (speed) {
Bo Shen6f7d7d92013-04-24 15:59:28 +0800653 ncfgr |= MACB_BIT(SPD);
Bin Mengcf821322019-05-22 00:09:45 -0700654#ifdef CONFIG_DM_ETH
655 ret = macb_linkspd_cb(dev, _100BASET);
656#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700657 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700658#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700659 } else {
Bin Mengcf821322019-05-22 00:09:45 -0700660#ifdef CONFIG_DM_ETH
661 ret = macb_linkspd_cb(dev, _10BASET);
662#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700663 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700664#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700665 }
666
667 if (ret)
668 return ret;
669
Bo Shen6f7d7d92013-04-24 15:59:28 +0800670 if (duplex)
671 ncfgr |= MACB_BIT(FD);
672 macb_writel(macb, NCFGR, ncfgr);
673
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700674 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100675}
676
Wu, Josh012d68d2015-06-03 16:45:44 +0800677static int gmac_init_multi_queues(struct macb_device *macb)
678{
679 int i, num_queues = 1;
680 u32 queue_mask;
681
682 /* bit 0 is never set but queue 0 always exists */
683 queue_mask = gem_readl(macb, DCFG6) & 0xff;
684 queue_mask |= 0x1;
685
686 for (i = 1; i < MACB_MAX_QUEUES; i++)
687 if (queue_mask & (1 << i))
688 num_queues++;
689
Ramon Fried6402fb192019-07-16 22:04:33 +0300690 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Josh012d68d2015-06-03 16:45:44 +0800691 macb->dummy_desc->addr = 0;
692 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200693 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh012d68d2015-06-03 16:45:44 +0800694
695 for (i = 1; i < num_queues; i++)
696 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
697
698 return 0;
699}
700
Ramon Friedb40501f2019-07-16 22:04:36 +0300701static void gmac_configure_dma(struct macb_device *macb)
702{
703 u32 buffer_size;
704 u32 dmacfg;
705
706 buffer_size = 128 / RX_BUFFER_MULTIPLE;
707 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
708 dmacfg |= GEM_BF(RXBS, buffer_size);
709
710 if (macb->dma_burst_length)
711 dmacfg = GEM_BFINS(FBLDO, macb->dma_burst_length, dmacfg);
712
713 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
714 dmacfg &= ~GEM_BIT(ENDIA_PKT);
715
716#ifdef CONFIG_SYS_LITTLE_ENDIAN
717 dmacfg &= ~GEM_BIT(ENDIA_DESC);
718#else
719 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
720#endif
721
722 dmacfg &= ~GEM_BIT(ADDR64);
723 gem_writel(macb, DMACFG, dmacfg);
724}
725
Wenyou Yang7b811852016-05-17 13:11:35 +0800726#ifdef CONFIG_DM_ETH
727static int _macb_init(struct udevice *dev, const char *name)
728#else
Simon Glass5ad27512016-05-05 07:28:09 -0600729static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yang7b811852016-05-17 13:11:35 +0800730#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100731{
Wenyou Yang7b811852016-05-17 13:11:35 +0800732#ifdef CONFIG_DM_ETH
733 struct macb_device *macb = dev_get_priv(dev);
734#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100735 unsigned long paddr;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700736 int ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100737 int i;
738
739 /*
740 * macb_halt should have been called at some point before now,
741 * so we'll assume the controller is idle.
742 */
743
744 /* initialize DMA descriptors */
745 paddr = macb->rx_buffer_dma;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200746 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
747 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried6402fb192019-07-16 22:04:33 +0300748 paddr |= MACB_BIT(RX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100749 macb->rx_ring[i].addr = paddr;
750 macb->rx_ring[i].ctrl = 0;
751 paddr += 128;
752 }
Wu, Josh18052402014-05-27 16:31:05 +0800753 macb_flush_ring_desc(macb, RX);
754 macb_flush_rx_buffer(macb);
755
Andreas Bießmann1e868122014-05-26 22:55:18 +0200756 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100757 macb->tx_ring[i].addr = 0;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200758 if (i == (MACB_TX_RING_SIZE - 1))
Ramon Fried6402fb192019-07-16 22:04:33 +0300759 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
760 MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100761 else
Ramon Fried6402fb192019-07-16 22:04:33 +0300762 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100763 }
Wu, Josh18052402014-05-27 16:31:05 +0800764 macb_flush_ring_desc(macb, TX);
765
Andreas Bießmann1e868122014-05-26 22:55:18 +0200766 macb->rx_tail = 0;
767 macb->tx_head = 0;
768 macb->tx_tail = 0;
Simon Glass5ad27512016-05-05 07:28:09 -0600769 macb->next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100770
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700771#ifdef CONFIG_MACB_ZYNQ
772 macb_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
773#endif
774
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100775 macb_writel(macb, RBQP, macb->rx_ring_dma);
776 macb_writel(macb, TBQP, macb->tx_ring_dma);
777
Bo Shen6f7d7d92013-04-24 15:59:28 +0800778 if (macb_is_gem(macb)) {
Ramon Friedb40501f2019-07-16 22:04:36 +0300779 /* Initialize DMA properties */
780 gmac_configure_dma(macb);
Wu, Josh012d68d2015-06-03 16:45:44 +0800781 /* Check the multi queue and initialize the queue for tx */
782 gmac_init_multi_queues(macb);
783
Bo Shen4660b332014-11-10 15:24:01 +0800784 /*
785 * When the GMAC IP with GE feature, this bit is used to
786 * select interface between RGMII and GMII.
787 * When the GMAC IP without GE feature, this bit is used
788 * to select interface between RMII and MII.
789 */
Wenyou Yang7b811852016-05-17 13:11:35 +0800790#ifdef CONFIG_DM_ETH
Wenyou Yang5653dbc2017-04-20 11:13:13 +0800791 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
792 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
Ramon Fried94e6bd82019-07-16 22:03:00 +0300793 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Wenyou Yang7b811852016-05-17 13:11:35 +0800794 else
Ramon Fried94e6bd82019-07-16 22:03:00 +0300795 gem_writel(macb, USRIO, 0);
Ramon Fried588a5b72019-07-16 22:04:34 +0300796
797 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
798 unsigned int ncfgr = macb_readl(macb, NCFGR);
799
800 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
801 macb_writel(macb, NCFGR, ncfgr);
802 }
Wenyou Yang7b811852016-05-17 13:11:35 +0800803#else
Bo Shen4660b332014-11-10 15:24:01 +0800804#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Ramon Fried94e6bd82019-07-16 22:03:00 +0300805 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Bo Shen6f7d7d92013-04-24 15:59:28 +0800806#else
Ramon Fried94e6bd82019-07-16 22:03:00 +0300807 gem_writel(macb, USRIO, 0);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800808#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800809#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800810 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100811 /* choose RMII or MII mode. This depends on the board */
Wenyou Yang7b811852016-05-17 13:11:35 +0800812#ifdef CONFIG_DM_ETH
813#ifdef CONFIG_AT91FAMILY
814 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
815 macb_writel(macb, USRIO,
816 MACB_BIT(RMII) | MACB_BIT(CLKEN));
817 } else {
818 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
819 }
820#else
821 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
822 macb_writel(macb, USRIO, 0);
823 else
824 macb_writel(macb, USRIO, MACB_BIT(MII));
825#endif
826#else
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100827#ifdef CONFIG_RMII
Bo Shencc29ce52013-04-24 15:59:26 +0800828#ifdef CONFIG_AT91FAMILY
Stelian Pop87a82542008-01-03 21:15:56 +0000829 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
830#else
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100831 macb_writel(macb, USRIO, 0);
Stelian Pop87a82542008-01-03 21:15:56 +0000832#endif
833#else
Bo Shencc29ce52013-04-24 15:59:26 +0800834#ifdef CONFIG_AT91FAMILY
Stelian Pop87a82542008-01-03 21:15:56 +0000835 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100836#else
837 macb_writel(macb, USRIO, MACB_BIT(MII));
838#endif
Stelian Pop87a82542008-01-03 21:15:56 +0000839#endif /* CONFIG_RMII */
Wenyou Yang7b811852016-05-17 13:11:35 +0800840#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800841 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100842
Wenyou Yang7b811852016-05-17 13:11:35 +0800843#ifdef CONFIG_DM_ETH
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700844 ret = macb_phy_init(dev, name);
Wenyou Yang7b811852016-05-17 13:11:35 +0800845#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700846 ret = macb_phy_init(macb, name);
Wenyou Yang7b811852016-05-17 13:11:35 +0800847#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700848 if (ret)
849 return ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100850
851 /* Enable TX and RX */
852 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
853
Ben Warrende9fcb52008-01-09 18:15:53 -0500854 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100855}
856
Simon Glass5ad27512016-05-05 07:28:09 -0600857static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100858{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100859 u32 ncr, tsr;
860
861 /* Halt the controller and wait for any ongoing transmission to end. */
862 ncr = macb_readl(macb, NCR);
863 ncr |= MACB_BIT(THALT);
864 macb_writel(macb, NCR, ncr);
865
866 do {
867 tsr = macb_readl(macb, TSR);
868 } while (tsr & MACB_BIT(TGO));
869
870 /* Disable TX and RX, and clear statistics */
871 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
872}
873
Simon Glass5ad27512016-05-05 07:28:09 -0600874static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren33f84312010-06-01 11:55:42 -0700875{
Ben Warren33f84312010-06-01 11:55:42 -0700876 u32 hwaddr_bottom;
877 u16 hwaddr_top;
878
879 /* set hardware address */
Simon Glass5ad27512016-05-05 07:28:09 -0600880 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
881 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren33f84312010-06-01 11:55:42 -0700882 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glass5ad27512016-05-05 07:28:09 -0600883 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren33f84312010-06-01 11:55:42 -0700884 macb_writel(macb, SA1T, hwaddr_top);
885 return 0;
886}
887
Bo Shen6f7d7d92013-04-24 15:59:28 +0800888static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
889{
890 u32 config;
Wenyou Yang19449362017-02-14 16:24:40 +0800891#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800892 unsigned long macb_hz = macb->pclk_rate;
893#else
Bo Shen6f7d7d92013-04-24 15:59:28 +0800894 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800895#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800896
897 if (macb_hz < 20000000)
898 config = MACB_BF(CLK, MACB_CLK_DIV8);
899 else if (macb_hz < 40000000)
900 config = MACB_BF(CLK, MACB_CLK_DIV16);
901 else if (macb_hz < 80000000)
902 config = MACB_BF(CLK, MACB_CLK_DIV32);
903 else
904 config = MACB_BF(CLK, MACB_CLK_DIV64);
905
906 return config;
907}
908
909static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
910{
911 u32 config;
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800912
Wenyou Yang19449362017-02-14 16:24:40 +0800913#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800914 unsigned long macb_hz = macb->pclk_rate;
915#else
Bo Shen6f7d7d92013-04-24 15:59:28 +0800916 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800917#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800918
919 if (macb_hz < 20000000)
920 config = GEM_BF(CLK, GEM_CLK_DIV8);
921 else if (macb_hz < 40000000)
922 config = GEM_BF(CLK, GEM_CLK_DIV16);
923 else if (macb_hz < 80000000)
924 config = GEM_BF(CLK, GEM_CLK_DIV32);
925 else if (macb_hz < 120000000)
926 config = GEM_BF(CLK, GEM_CLK_DIV48);
927 else if (macb_hz < 160000000)
928 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +0300929 else if (macb_hz < 240000000)
Bo Shen6f7d7d92013-04-24 15:59:28 +0800930 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +0300931 else if (macb_hz < 320000000)
932 config = GEM_BF(CLK, GEM_CLK_DIV128);
933 else
934 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800935
936 return config;
937}
938
Bo Shen0e6624a2013-09-18 15:07:44 +0800939/*
940 * Get the DMA bus width field of the network configuration register that we
941 * should program. We find the width from decoding the design configuration
942 * register to find the maximum supported data bus width.
943 */
944static u32 macb_dbw(struct macb_device *macb)
945{
946 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
947 case 4:
948 return GEM_BF(DBW, GEM_DBW128);
949 case 2:
950 return GEM_BF(DBW, GEM_DBW64);
951 case 1:
952 default:
953 return GEM_BF(DBW, GEM_DBW32);
954 }
Simon Glass5ad27512016-05-05 07:28:09 -0600955}
956
957static void _macb_eth_initialize(struct macb_device *macb)
958{
959 int id = 0; /* This is not used by functions we call */
960 u32 ncfgr;
961
962 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
963 macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
964 &macb->rx_buffer_dma);
965 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
966 &macb->rx_ring_dma);
967 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
968 &macb->tx_ring_dma);
969 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
970 &macb->dummy_desc_dma);
971
972 /*
973 * Do some basic initialization so that we at least can talk
974 * to the PHY
975 */
976 if (macb_is_gem(macb)) {
977 ncfgr = gem_mdc_clk_div(id, macb);
978 ncfgr |= macb_dbw(macb);
979 } else {
980 ncfgr = macb_mdc_clk_div(id, macb);
981 }
982
983 macb_writel(macb, NCFGR, ncfgr);
984}
985
Simon Glass75c5d182016-05-05 07:28:11 -0600986#ifndef CONFIG_DM_ETH
Simon Glass5ad27512016-05-05 07:28:09 -0600987static int macb_send(struct eth_device *netdev, void *packet, int length)
988{
989 struct macb_device *macb = to_macb(netdev);
990
991 return _macb_send(macb, netdev->name, packet, length);
Bo Shen0e6624a2013-09-18 15:07:44 +0800992}
993
Simon Glass5ad27512016-05-05 07:28:09 -0600994static int macb_recv(struct eth_device *netdev)
995{
996 struct macb_device *macb = to_macb(netdev);
997 uchar *packet;
998 int length;
999
1000 macb->wrapped = false;
1001 for (;;) {
1002 macb->next_rx_tail = macb->rx_tail;
1003 length = _macb_recv(macb, &packet);
1004 if (length >= 0) {
1005 net_process_received_packet(packet, length);
1006 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6c4aae92018-03-18 11:32:53 +01001007 } else {
Simon Glass5ad27512016-05-05 07:28:09 -06001008 return length;
1009 }
1010 }
1011}
1012
1013static int macb_init(struct eth_device *netdev, bd_t *bd)
1014{
1015 struct macb_device *macb = to_macb(netdev);
1016
1017 return _macb_init(macb, netdev->name);
1018}
1019
1020static void macb_halt(struct eth_device *netdev)
1021{
1022 struct macb_device *macb = to_macb(netdev);
1023
1024 return _macb_halt(macb);
1025}
1026
1027static int macb_write_hwaddr(struct eth_device *netdev)
1028{
1029 struct macb_device *macb = to_macb(netdev);
1030
1031 return _macb_write_hwaddr(macb, netdev->enetaddr);
1032}
1033
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001034int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1035{
1036 struct macb_device *macb;
1037 struct eth_device *netdev;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001038
1039 macb = malloc(sizeof(struct macb_device));
1040 if (!macb) {
1041 printf("Error: Failed to allocate memory for MACB%d\n", id);
1042 return -1;
1043 }
1044 memset(macb, 0, sizeof(struct macb_device));
1045
1046 netdev = &macb->netdev;
1047
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001048 macb->regs = regs;
1049 macb->phy_addr = phy_addr;
1050
Bo Shen6f7d7d92013-04-24 15:59:28 +08001051 if (macb_is_gem(macb))
1052 sprintf(netdev->name, "gmac%d", id);
1053 else
1054 sprintf(netdev->name, "macb%d", id);
1055
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001056 netdev->init = macb_init;
1057 netdev->halt = macb_halt;
1058 netdev->send = macb_send;
1059 netdev->recv = macb_recv;
Ben Warren33f84312010-06-01 11:55:42 -07001060 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001061
Simon Glass5ad27512016-05-05 07:28:09 -06001062 _macb_eth_initialize(macb);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001063
1064 eth_register(netdev);
1065
Bo Shen7d91deb2013-04-24 15:59:27 +08001066#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001067 int retval;
1068 struct mii_dev *mdiodev = mdio_alloc();
1069 if (!mdiodev)
1070 return -ENOMEM;
1071 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1072 mdiodev->read = macb_miiphy_read;
1073 mdiodev->write = macb_miiphy_write;
1074
1075 retval = mdio_register(mdiodev);
1076 if (retval < 0)
1077 return retval;
Bo Shen7d91deb2013-04-24 15:59:27 +08001078 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar790088e2009-12-17 15:07:15 +02001079#endif
Simon Glass75c5d182016-05-05 07:28:11 -06001080 return 0;
1081}
1082#endif /* !CONFIG_DM_ETH */
1083
1084#ifdef CONFIG_DM_ETH
1085
1086static int macb_start(struct udevice *dev)
1087{
Wenyou Yang7b811852016-05-17 13:11:35 +08001088 return _macb_init(dev, dev->name);
Simon Glass75c5d182016-05-05 07:28:11 -06001089}
1090
1091static int macb_send(struct udevice *dev, void *packet, int length)
1092{
1093 struct macb_device *macb = dev_get_priv(dev);
1094
1095 return _macb_send(macb, dev->name, packet, length);
1096}
1097
1098static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1099{
1100 struct macb_device *macb = dev_get_priv(dev);
1101
1102 macb->next_rx_tail = macb->rx_tail;
1103 macb->wrapped = false;
1104
1105 return _macb_recv(macb, packetp);
1106}
1107
1108static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1109{
1110 struct macb_device *macb = dev_get_priv(dev);
1111
1112 reclaim_rx_buffers(macb, macb->next_rx_tail);
1113
1114 return 0;
1115}
1116
1117static void macb_stop(struct udevice *dev)
1118{
1119 struct macb_device *macb = dev_get_priv(dev);
1120
1121 _macb_halt(macb);
1122}
1123
1124static int macb_write_hwaddr(struct udevice *dev)
1125{
1126 struct eth_pdata *plat = dev_get_platdata(dev);
1127 struct macb_device *macb = dev_get_priv(dev);
1128
1129 return _macb_write_hwaddr(macb, plat->enetaddr);
1130}
1131
1132static const struct eth_ops macb_eth_ops = {
1133 .start = macb_start,
1134 .send = macb_send,
1135 .recv = macb_recv,
1136 .stop = macb_stop,
1137 .free_pkt = macb_free_pkt,
1138 .write_hwaddr = macb_write_hwaddr,
1139};
1140
Wenyou Yang19449362017-02-14 16:24:40 +08001141#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001142static int macb_enable_clk(struct udevice *dev)
1143{
1144 struct macb_device *macb = dev_get_priv(dev);
1145 struct clk clk;
1146 ulong clk_rate;
1147 int ret;
1148
1149 ret = clk_get_by_index(dev, 0, &clk);
1150 if (ret)
1151 return -EINVAL;
1152
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001153 /*
Anup Patel51b51f82019-02-25 08:14:36 +00001154 * If clock driver didn't support enable or disable then
1155 * we get -ENOSYS from clk_enable(). To handle this, we
1156 * don't fail for ret == -ENOSYS.
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001157 */
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001158 ret = clk_enable(&clk);
Anup Patel51b51f82019-02-25 08:14:36 +00001159 if (ret && ret != -ENOSYS)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001160 return ret;
1161
1162 clk_rate = clk_get_rate(&clk);
1163 if (!clk_rate)
1164 return -EINVAL;
1165
1166 macb->pclk_rate = clk_rate;
1167
1168 return 0;
1169}
Wenyou Yang19449362017-02-14 16:24:40 +08001170#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001171
Ramon Fried834040c2019-07-16 22:04:35 +03001172static const struct macb_config default_gem_config = {
1173 .dma_burst_length = 16,
1174};
1175
Simon Glass75c5d182016-05-05 07:28:11 -06001176static int macb_eth_probe(struct udevice *dev)
1177{
Ramon Fried834040c2019-07-16 22:04:35 +03001178 const struct macb_config *macb_config;
Simon Glass75c5d182016-05-05 07:28:11 -06001179 struct eth_pdata *pdata = dev_get_platdata(dev);
1180 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yang7b811852016-05-17 13:11:35 +08001181 const char *phy_mode;
Wenyou Yang44835ea2017-04-14 14:36:04 +08001182 __maybe_unused int ret;
Wenyou Yang7b811852016-05-17 13:11:35 +08001183
Simon Glassdd79d6e2017-01-17 16:52:55 -07001184 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1185 NULL);
Wenyou Yang7b811852016-05-17 13:11:35 +08001186 if (phy_mode)
1187 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1188 if (macb->phy_interface == -1) {
1189 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1190 return -EINVAL;
1191 }
Wenyou Yang7b811852016-05-17 13:11:35 +08001192
Simon Glass75c5d182016-05-05 07:28:11 -06001193 macb->regs = (void *)pdata->iobase;
1194
Ramon Fried834040c2019-07-16 22:04:35 +03001195 macb_config = (struct macb_config *)dev_get_driver_data(dev);
1196 if (!macb_config)
1197 macb_config = &default_gem_config;
1198
1199 macb->dma_burst_length = macb_config->dma_burst_length;
Wenyou Yang19449362017-02-14 16:24:40 +08001200#ifdef CONFIG_CLK
Wenyou Yang44835ea2017-04-14 14:36:04 +08001201 ret = macb_enable_clk(dev);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001202 if (ret)
1203 return ret;
Wenyou Yang19449362017-02-14 16:24:40 +08001204#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001205
Simon Glass75c5d182016-05-05 07:28:11 -06001206 _macb_eth_initialize(macb);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001207
Simon Glass75c5d182016-05-05 07:28:11 -06001208#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang44835ea2017-04-14 14:36:04 +08001209 macb->bus = mdio_alloc();
1210 if (!macb->bus)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001211 return -ENOMEM;
Wenyou Yang44835ea2017-04-14 14:36:04 +08001212 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1213 macb->bus->read = macb_miiphy_read;
1214 macb->bus->write = macb_miiphy_write;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001215
Wenyou Yang44835ea2017-04-14 14:36:04 +08001216 ret = mdio_register(macb->bus);
1217 if (ret < 0)
1218 return ret;
Simon Glass75c5d182016-05-05 07:28:11 -06001219 macb->bus = miiphy_get_dev_by_name(dev->name);
1220#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +08001221
1222 return 0;
1223}
1224
1225static int macb_eth_remove(struct udevice *dev)
1226{
1227 struct macb_device *macb = dev_get_priv(dev);
1228
1229#ifdef CONFIG_PHYLIB
1230 free(macb->phydev);
1231#endif
1232 mdio_unregister(macb->bus);
1233 mdio_free(macb->bus);
Simon Glass75c5d182016-05-05 07:28:11 -06001234
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001235 return 0;
1236}
1237
1238/**
1239 * macb_late_eth_ofdata_to_platdata
1240 * @dev: udevice struct
1241 * Returns 0 when operation success and negative errno number
1242 * when operation failed.
1243 */
1244int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1245{
Simon Glass75c5d182016-05-05 07:28:11 -06001246 return 0;
1247}
1248
1249static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1250{
1251 struct eth_pdata *pdata = dev_get_platdata(dev);
1252
Ramon Friedbf15d2f2018-12-27 19:58:42 +02001253 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1254 if (!pdata->iobase)
1255 return -EINVAL;
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001256
1257 return macb_late_eth_ofdata_to_platdata(dev);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001258}
1259
Ramon Fried834040c2019-07-16 22:04:35 +03001260static const struct macb_config sama5d4_config = {
1261 .dma_burst_length = 4,
1262};
1263
Simon Glass75c5d182016-05-05 07:28:11 -06001264static const struct udevice_id macb_eth_ids[] = {
1265 { .compatible = "cdns,macb" },
Wenyou Yang8f155402017-04-14 14:36:05 +08001266 { .compatible = "cdns,at91sam9260-macb" },
1267 { .compatible = "atmel,sama5d2-gem" },
1268 { .compatible = "atmel,sama5d3-gem" },
Ramon Fried834040c2019-07-16 22:04:35 +03001269 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001270 { .compatible = "cdns,zynq-gem" },
Simon Glass75c5d182016-05-05 07:28:11 -06001271 { }
1272};
1273
1274U_BOOT_DRIVER(eth_macb) = {
1275 .name = "eth_macb",
1276 .id = UCLASS_ETH,
1277 .of_match = macb_eth_ids,
1278 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1279 .probe = macb_eth_probe,
Wenyou Yang44835ea2017-04-14 14:36:04 +08001280 .remove = macb_eth_remove,
Simon Glass75c5d182016-05-05 07:28:11 -06001281 .ops = &macb_eth_ops,
1282 .priv_auto_alloc_size = sizeof(struct macb_device),
1283 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1284};
1285#endif
1286
Jon Loeligerb1d408a2007-07-09 17:30:01 -05001287#endif