blob: bfc48dac079b89aa057588a6cec7228c6548f32d [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 Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Simon Glass75c5d182016-05-05 07:28:11 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010012
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010013/*
14 * The u-boot networking stack is a little weird. It seems like the
15 * networking core allocates receive buffers up front without any
16 * regard to the hardware that's supposed to actually receive those
17 * packets.
18 *
19 * The MACB receives packets into 128-byte receive buffers, so the
20 * buffers allocated by the core isn't very practical to use. We'll
21 * allocate our own, but we need one such buffer in case a packet
22 * wraps around the DMA ring so that we have to copy it.
23 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020024 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010025 * configuration header. This way, the core allocates one RX buffer
26 * and one TX buffer, each of which can hold a ethernet packet of
27 * maximum size.
28 *
29 * For some reason, the networking core unconditionally specifies a
30 * 32-byte packet "alignment" (which really should be called
31 * "padding"). MACB shouldn't need that, but we'll refrain from any
32 * core modifications here...
33 */
34
35#include <net.h>
36#include <malloc.h>
Semih Hazar790088e2009-12-17 15:07:15 +020037#include <miiphy.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010038
39#include <linux/mii.h>
40#include <asm/io.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090041#include <linux/dma-mapping.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010042#include <asm/arch/clk.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090043#include <linux/errno.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010044
45#include "macb.h"
46
Wenyou Yang7b811852016-05-17 13:11:35 +080047DECLARE_GLOBAL_DATA_PTR;
48
Ramon Fried377d19d2019-07-14 18:25:14 +030049/*
50 * These buffer sizes must be power of 2 and divisible
51 * by RX_BUFFER_MULTIPLE
52 */
53#define MACB_RX_BUFFER_SIZE 128
54#define GEM_RX_BUFFER_SIZE 2048
Ramon Friedb40501f2019-07-16 22:04:36 +030055#define RX_BUFFER_MULTIPLE 64
Ramon Fried377d19d2019-07-14 18:25:14 +030056
57#define MACB_RX_RING_SIZE 32
Andreas Bießmann1e868122014-05-26 22:55:18 +020058#define MACB_TX_RING_SIZE 16
Ramon Fried377d19d2019-07-14 18:25:14 +030059
Andreas Bießmann1e868122014-05-26 22:55:18 +020060#define MACB_TX_TIMEOUT 1000
61#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010062
Wilson Lee41d6d1e2017-08-22 20:25:07 -070063#ifdef CONFIG_MACB_ZYNQ
64/* INCR4 AHB bursts */
65#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
66/* Use full configured addressable space (8 Kb) */
67#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
68/* Use full configured addressable space (4 Kb) */
69#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
70/* Set RXBUF with use of 128 byte */
71#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
72#define MACB_ZYNQ_GEM_DMACR_INIT \
73 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
74 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
76 MACB_ZYNQ_GEM_DMACR_RXBUF)
77#endif
78
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010079struct macb_dma_desc {
80 u32 addr;
81 u32 ctrl;
82};
83
Padmarao Begari7a2c4962021-01-15 08:20:36 +053084struct macb_dma_desc_64 {
85 u32 addrh;
86 u32 unused;
87};
88
89#define HW_DMA_CAP_32B 0
90#define HW_DMA_CAP_64B 1
91
92#define DMA_DESC_SIZE 16
93#define DMA_DESC_BYTES(n) ((n) * DMA_DESC_SIZE)
Wu, Josh18052402014-05-27 16:31:05 +080094#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
95#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Josh012d68d2015-06-03 16:45:44 +080096#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh18052402014-05-27 16:31:05 +080097
Yaron Micher1ae088d2022-11-10 19:31:34 +020098#define DESC_PER_CACHELINE_32 (ARCH_DMA_MINALIGN/sizeof(struct macb_dma_desc))
99#define DESC_PER_CACHELINE_64 (ARCH_DMA_MINALIGN/DMA_DESC_SIZE)
100
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100101#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100102#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100103
104struct macb_device {
105 void *regs;
Anup Patel88799a62019-07-24 04:09:32 +0000106
Anup Patela1818b12019-07-24 04:09:37 +0000107 bool is_big_endian;
108
Anup Patel88799a62019-07-24 04:09:32 +0000109 const struct macb_config *config;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100110
111 unsigned int rx_tail;
112 unsigned int tx_head;
113 unsigned int tx_tail;
Simon Glass5ad27512016-05-05 07:28:09 -0600114 unsigned int next_rx_tail;
115 bool wrapped;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100116
117 void *rx_buffer;
118 void *tx_buffer;
119 struct macb_dma_desc *rx_ring;
120 struct macb_dma_desc *tx_ring;
Ramon Fried377d19d2019-07-14 18:25:14 +0300121 size_t rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100122
123 unsigned long rx_buffer_dma;
124 unsigned long rx_ring_dma;
125 unsigned long tx_ring_dma;
126
Wu, Josh012d68d2015-06-03 16:45:44 +0800127 struct macb_dma_desc *dummy_desc;
128 unsigned long dummy_desc_dma;
129
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100130 const struct device *dev;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100131 unsigned short phy_addr;
Bo Shen7d91deb2013-04-24 15:59:27 +0800132 struct mii_dev *bus;
Wenyou Yang44835ea2017-04-14 14:36:04 +0800133#ifdef CONFIG_PHYLIB
134 struct phy_device *phydev;
135#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800136
Wenyou Yang19449362017-02-14 16:24:40 +0800137#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800138 unsigned long pclk_rate;
Wenyou Yang19449362017-02-14 16:24:40 +0800139#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800140 phy_interface_t phy_interface;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100141};
Ramon Fried834040c2019-07-16 22:04:35 +0300142
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200143struct macb_usrio_cfg {
144 unsigned int mii;
145 unsigned int rmii;
146 unsigned int rgmii;
147 unsigned int clken;
148};
149
Ramon Fried834040c2019-07-16 22:04:35 +0300150struct macb_config {
151 unsigned int dma_burst_length;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530152 unsigned int hw_dma_cap;
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200153 unsigned int caps;
Anup Patel88799a62019-07-24 04:09:32 +0000154
155 int (*clk_init)(struct udevice *dev, ulong rate);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200156 const struct macb_usrio_cfg *usrio;
Ramon Fried834040c2019-07-16 22:04:35 +0300157};
158
Bo Shen6f7d7d92013-04-24 15:59:28 +0800159static int macb_is_gem(struct macb_device *macb)
160{
Atish Patrae1a85182019-02-25 08:14:42 +0000161 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800162}
163
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100164#ifndef cpu_is_sama5d2
165#define cpu_is_sama5d2() 0
166#endif
167
168#ifndef cpu_is_sama5d4
169#define cpu_is_sama5d4() 0
170#endif
171
172static int gem_is_gigabit_capable(struct macb_device *macb)
173{
174 /*
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400175 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100176 * configured to support only 10/100.
177 */
178 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
179}
180
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200181static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
182 u16 value)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100183{
184 unsigned long netctl;
185 unsigned long netstat;
186 unsigned long frame;
187
188 netctl = macb_readl(macb, NCR);
189 netctl |= MACB_BIT(MPE);
190 macb_writel(macb, NCR, netctl);
191
192 frame = (MACB_BF(SOF, 1)
193 | MACB_BF(RW, 1)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200194 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100195 | MACB_BF(REGA, reg)
196 | MACB_BF(CODE, 2)
197 | MACB_BF(DATA, value));
198 macb_writel(macb, MAN, frame);
199
200 do {
201 netstat = macb_readl(macb, NSR);
202 } while (!(netstat & MACB_BIT(IDLE)));
203
204 netctl = macb_readl(macb, NCR);
205 netctl &= ~MACB_BIT(MPE);
206 macb_writel(macb, NCR, netctl);
207}
208
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200209static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100210{
211 unsigned long netctl;
212 unsigned long netstat;
213 unsigned long frame;
214
215 netctl = macb_readl(macb, NCR);
216 netctl |= MACB_BIT(MPE);
217 macb_writel(macb, NCR, netctl);
218
219 frame = (MACB_BF(SOF, 1)
220 | MACB_BF(RW, 2)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200221 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100222 | MACB_BF(REGA, reg)
223 | MACB_BF(CODE, 2));
224 macb_writel(macb, MAN, frame);
225
226 do {
227 netstat = macb_readl(macb, NSR);
228 } while (!(netstat & MACB_BIT(IDLE)));
229
230 frame = macb_readl(macb, MAN);
231
232 netctl = macb_readl(macb, NCR);
233 netctl &= ~MACB_BIT(MPE);
234 macb_writel(macb, NCR, netctl);
235
236 return MACB_BFEXT(DATA, frame);
237}
238
Joe Hershberger9e5742b2013-06-24 19:06:38 -0500239void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim77cdf0f2012-12-13 17:22:52 +0530240{
241 return;
242}
243
Bo Shen7d91deb2013-04-24 15:59:27 +0800244#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar790088e2009-12-17 15:07:15 +0200245
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500246int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar790088e2009-12-17 15:07:15 +0200247{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500248 u16 value = 0;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500249 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600250 struct macb_device *macb = dev_get_priv(dev);
Semih Hazar790088e2009-12-17 15:07:15 +0200251
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500252 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200253 value = macb_mdio_read(macb, phy_adr, reg);
Semih Hazar790088e2009-12-17 15:07:15 +0200254
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500255 return value;
Semih Hazar790088e2009-12-17 15:07:15 +0200256}
257
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500258int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
259 u16 value)
Semih Hazar790088e2009-12-17 15:07:15 +0200260{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500261 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600262 struct macb_device *macb = dev_get_priv(dev);
Semih Hazar790088e2009-12-17 15:07:15 +0200263
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500264 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200265 macb_mdio_write(macb, phy_adr, reg, value);
Semih Hazar790088e2009-12-17 15:07:15 +0200266
267 return 0;
268}
269#endif
270
Wu, Josh18052402014-05-27 16:31:05 +0800271#define RX 1
272#define TX 0
273static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
274{
275 if (rx)
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200276 invalidate_dcache_range(macb->rx_ring_dma,
277 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
278 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800279 else
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200280 invalidate_dcache_range(macb->tx_ring_dma,
281 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
282 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800283}
284
285static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
286{
287 if (rx)
288 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200289 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800290 else
291 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200292 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800293}
294
295static inline void macb_flush_rx_buffer(struct macb_device *macb)
296{
297 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese7df65a52019-08-26 09:18:11 +0200298 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
299 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800300}
301
302static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
303{
304 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese7df65a52019-08-26 09:18:11 +0200305 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
306 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800307}
Semih Hazar790088e2009-12-17 15:07:15 +0200308
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500309#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100310
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530311static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
312{
313 return (struct macb_dma_desc_64 *)((void *)desc
314 + sizeof(struct macb_dma_desc));
315}
316
317static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
318 ulong addr)
319{
320 struct macb_dma_desc_64 *desc_64;
321
322 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
323 desc_64 = macb_64b_desc(desc);
324 desc_64->addrh = upper_32_bits(addr);
325 }
326 desc->addr = lower_32_bits(addr);
327}
328
Simon Glass5ad27512016-05-05 07:28:09 -0600329static int _macb_send(struct macb_device *macb, const char *name, void *packet,
330 int length)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100331{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100332 unsigned long paddr, ctrl;
333 unsigned int tx_head = macb->tx_head;
334 int i;
335
336 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
337
338 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried6402fb192019-07-16 22:04:33 +0300339 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200340 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300341 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100342 macb->tx_head = 0;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200343 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100344 macb->tx_head++;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200345 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100346
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530347 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
348 tx_head = tx_head * 2;
349
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100350 macb->tx_ring[tx_head].ctrl = ctrl;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530351 macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
352
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200353 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800354 macb_flush_ring_desc(macb, TX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100355 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
356
357 /*
358 * I guess this is necessary because the networking core may
359 * re-use the transmit buffer as soon as we return...
360 */
Andreas Bießmann1e868122014-05-26 22:55:18 +0200361 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200362 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800363 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200364 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300365 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100366 break;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100367 udelay(1);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100368 }
369
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900370 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100371
Andreas Bießmann1e868122014-05-26 22:55:18 +0200372 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300373 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glass5ad27512016-05-05 07:28:09 -0600374 printf("%s: TX underrun\n", name);
Ramon Fried6402fb192019-07-16 22:04:33 +0300375 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glass5ad27512016-05-05 07:28:09 -0600376 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200377 } else {
Simon Glass5ad27512016-05-05 07:28:09 -0600378 printf("%s: TX timeout\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100379 }
380
381 /* No one cares anyway */
382 return 0;
383}
384
Yaron Micher1ae088d2022-11-10 19:31:34 +0200385static void reclaim_rx_buffer(struct macb_device *macb,
386 unsigned int idx)
387{
388 unsigned int mask;
389 unsigned int shift;
390 unsigned int i;
391
392 /*
393 * There may be multiple descriptors per CPU cacheline,
394 * so a cache flush would flush the whole line, meaning the content of other descriptors
395 * in the cacheline would also flush. If one of the other descriptors had been
396 * written to by the controller, the flush would cause those changes to be lost.
397 *
398 * To circumvent this issue, we do the actual freeing only when we need to free
399 * the last descriptor in the current cacheline. When the current descriptor is the
400 * last in the cacheline, we free all the descriptors that belong to that cacheline.
401 */
402 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
403 mask = DESC_PER_CACHELINE_64 - 1;
404 shift = 1;
405 } else {
406 mask = DESC_PER_CACHELINE_32 - 1;
407 shift = 0;
408 }
409
410 /* we exit without freeing if idx is not the last descriptor in the cacheline */
411 if ((idx & mask) != mask)
412 return;
413
414 for (i = idx & (~mask); i <= idx; i++)
415 macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
416}
417
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100418static void reclaim_rx_buffers(struct macb_device *macb,
419 unsigned int new_tail)
420{
421 unsigned int i;
422
423 i = macb->rx_tail;
Wu, Josh18052402014-05-27 16:31:05 +0800424
425 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100426 while (i > new_tail) {
Yaron Micher1ae088d2022-11-10 19:31:34 +0200427 reclaim_rx_buffer(macb, i);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100428 i++;
Yaron Micher1ae088d2022-11-10 19:31:34 +0200429 if (i >= MACB_RX_RING_SIZE)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100430 i = 0;
431 }
432
433 while (i < new_tail) {
Yaron Micher1ae088d2022-11-10 19:31:34 +0200434 reclaim_rx_buffer(macb, i);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100435 i++;
436 }
437
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200438 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800439 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100440 macb->rx_tail = new_tail;
441}
442
Simon Glass5ad27512016-05-05 07:28:09 -0600443static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100444{
Simon Glass5ad27512016-05-05 07:28:09 -0600445 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100446 void *buffer;
447 int length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100448 u32 status;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530449 u8 flag = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100450
Simon Glass5ad27512016-05-05 07:28:09 -0600451 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100452 for (;;) {
Wu, Josh18052402014-05-27 16:31:05 +0800453 macb_invalidate_ring_desc(macb, RX);
454
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530455 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
456 next_rx_tail = next_rx_tail * 2;
457
Ramon Fried6402fb192019-07-16 22:04:33 +0300458 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glass5ad27512016-05-05 07:28:09 -0600459 return -EAGAIN;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100460
Simon Glass5ad27512016-05-05 07:28:09 -0600461 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300462 if (status & MACB_BIT(RX_SOF)) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530463 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
464 next_rx_tail = next_rx_tail / 2;
465 flag = true;
466 }
467
Simon Glass5ad27512016-05-05 07:28:09 -0600468 if (next_rx_tail != macb->rx_tail)
469 reclaim_rx_buffers(macb, next_rx_tail);
470 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100471 }
472
Ramon Fried6402fb192019-07-16 22:04:33 +0300473 if (status & MACB_BIT(RX_EOF)) {
Ramon Fried377d19d2019-07-14 18:25:14 +0300474 buffer = macb->rx_buffer +
475 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100476 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh18052402014-05-27 16:31:05 +0800477
478 macb_invalidate_rx_buffer(macb);
Simon Glass5ad27512016-05-05 07:28:09 -0600479 if (macb->wrapped) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100480 unsigned int headlen, taillen;
481
Ramon Fried377d19d2019-07-14 18:25:14 +0300482 headlen = macb->rx_buffer_size *
483 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100484 taillen = length - headlen;
Joe Hershberger9f09a362015-04-08 01:41:06 -0500485 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100486 buffer, headlen);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500487 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100488 macb->rx_buffer, taillen);
Simon Glass5ad27512016-05-05 07:28:09 -0600489 *packetp = (void *)net_rx_packets[0];
490 } else {
491 *packetp = buffer;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100492 }
493
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530494 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
495 if (!flag)
496 next_rx_tail = next_rx_tail / 2;
497 }
498
Simon Glass5ad27512016-05-05 07:28:09 -0600499 if (++next_rx_tail >= MACB_RX_RING_SIZE)
500 next_rx_tail = 0;
501 macb->next_rx_tail = next_rx_tail;
502 return length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100503 } else {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530504 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
505 if (!flag)
506 next_rx_tail = next_rx_tail / 2;
507 flag = false;
508 }
509
Simon Glass5ad27512016-05-05 07:28:09 -0600510 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
511 macb->wrapped = true;
512 next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100513 }
514 }
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200515 barrier();
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100516 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100517}
518
Simon Glass5ad27512016-05-05 07:28:09 -0600519static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100520{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100521 int i;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200522 u16 status, adv;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100523
524 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200525 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
Simon Glass5ad27512016-05-05 07:28:09 -0600526 printf("%s: Starting autonegotiation...\n", name);
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200527 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100528 | BMCR_ANRESTART));
529
Andreas Bießmann1e868122014-05-26 22:55:18 +0200530 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200531 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100532 if (status & BMSR_ANEGCOMPLETE)
533 break;
534 udelay(100);
535 }
536
537 if (status & BMSR_ANEGCOMPLETE)
Simon Glass5ad27512016-05-05 07:28:09 -0600538 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100539 else
540 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600541 name, status);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200542}
543
Wenyou Yang7b811852016-05-17 13:11:35 +0800544static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100545{
546 int i;
547 u16 phy_id;
548
Padmarao Begari34394ba2021-01-15 08:20:37 +0530549 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
550 if (phy_id != 0xffff) {
551 printf("%s: PHY present at %d\n", name, macb->phy_addr);
552 return 0;
553 }
554
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100555 /* Search for PHY... */
556 for (i = 0; i < 32; i++) {
557 macb->phy_addr = i;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200558 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100559 if (phy_id != 0xffff) {
Wenyou Yang7b811852016-05-17 13:11:35 +0800560 printf("%s: PHY present at %d\n", name, i);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700561 return 0;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100562 }
563 }
564
565 /* PHY isn't up to snuff */
Wenyou Yang7b811852016-05-17 13:11:35 +0800566 printf("%s: PHY not found\n", name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100567
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700568 return -ENODEV;
569}
570
571/**
572 * macb_linkspd_cb - Linkspeed change callback function
Bin Mengcf821322019-05-22 00:09:45 -0700573 * @dev/@regs: MACB udevice (DM version) or
574 * Base Register of MACB devices (non-DM version)
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700575 * @speed: Linkspeed
576 * Returns 0 when operation success and negative errno number
577 * when operation failed.
578 */
Anup Patel88799a62019-07-24 04:09:32 +0000579static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
580{
Anup Patel88799a62019-07-24 04:09:32 +0000581 void *gemgxl_regs;
582
Bin Meng81ce6372021-09-12 11:15:14 +0800583 gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
Anup Patel88799a62019-07-24 04:09:32 +0000584 if (!gemgxl_regs)
585 return -ENODEV;
586
587 /*
588 * SiFive GEMGXL TX clock operation mode:
589 *
590 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
591 * and output clock on GMII output signal GTX_CLK
592 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
593 */
594 writel(rate != 125000000, gemgxl_regs);
595 return 0;
596}
597
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +0200598static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
599{
600 struct clk clk;
601 int ret;
602
603 ret = clk_get_by_name(dev, "tx_clk", &clk);
604 if (ret)
605 return ret;
606
607 /*
608 * This is for using GCK. Clock rate is addressed via assigned-clock
609 * property, so only clock enable is needed here. The switching to
610 * proper clock rate depending on link speed is managed by IP logic.
611 */
612 return clk_enable(&clk);
613}
614
Bin Mengcf821322019-05-22 00:09:45 -0700615int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
616{
Bin Meng12766ca2019-05-22 00:09:46 -0700617#ifdef CONFIG_CLK
Anup Patel88799a62019-07-24 04:09:32 +0000618 struct macb_device *macb = dev_get_priv(dev);
Bin Meng12766ca2019-05-22 00:09:46 -0700619 struct clk tx_clk;
620 ulong rate;
621 int ret;
622
Bin Meng12766ca2019-05-22 00:09:46 -0700623 switch (speed) {
624 case _10BASET:
625 rate = 2500000; /* 2.5 MHz */
626 break;
627 case _100BASET:
628 rate = 25000000; /* 25 MHz */
629 break;
630 case _1000BASET:
631 rate = 125000000; /* 125 MHz */
632 break;
633 default:
634 /* does not change anything */
635 return 0;
636 }
637
Anup Patel88799a62019-07-24 04:09:32 +0000638 if (macb->config->clk_init)
639 return macb->config->clk_init(dev, rate);
640
641 /*
642 * "tx_clk" is an optional clock source for MACB.
643 * Ignore if it does not exist in DT.
644 */
645 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
646 if (ret)
647 return 0;
648
Bin Meng12766ca2019-05-22 00:09:46 -0700649 if (tx_clk.dev) {
650 ret = clk_set_rate(&tx_clk, rate);
Claudiu Bezneae2888dc2021-01-19 13:26:45 +0200651 if (ret < 0)
Bin Meng12766ca2019-05-22 00:09:46 -0700652 return ret;
653 }
654#endif
655
Bin Mengcf821322019-05-22 00:09:45 -0700656 return 0;
657}
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100658
Wenyou Yang7b811852016-05-17 13:11:35 +0800659static int macb_phy_init(struct udevice *dev, const char *name)
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200660{
Wenyou Yang7b811852016-05-17 13:11:35 +0800661 struct macb_device *macb = dev_get_priv(dev);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200662 u32 ncfgr;
663 u16 phy_id, status, adv, lpa;
664 int media, speed, duplex;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700665 int ret;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200666 int i;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100667
Simon Glass5ad27512016-05-05 07:28:09 -0600668 arch_get_mdio_control(name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100669 /* Auto-detect phy_addr */
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700670 ret = macb_phy_find(macb, name);
671 if (ret)
672 return ret;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100673
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200674 /* Check if the PHY is up to snuff... */
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200675 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200676 if (phy_id == 0xffff) {
Simon Glass5ad27512016-05-05 07:28:09 -0600677 printf("%s: No PHY present\n", name);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700678 return -ENODEV;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200679 }
680
Bo Shen7d91deb2013-04-24 15:59:27 +0800681#ifdef CONFIG_PHYLIB
Wenyou Yang44835ea2017-04-14 14:36:04 +0800682 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yang7b811852016-05-17 13:11:35 +0800683 macb->phy_interface);
Wenyou Yang44835ea2017-04-14 14:36:04 +0800684 if (!macb->phydev) {
Bo Shene04fe552013-08-19 10:35:47 +0800685 printf("phy_connect failed\n");
686 return -ENODEV;
687 }
688
Wenyou Yang44835ea2017-04-14 14:36:04 +0800689 phy_config(macb->phydev);
Bo Shen7d91deb2013-04-24 15:59:27 +0800690#endif
691
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200692 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100693 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200694 /* Try to re-negotiate if we don't have link already. */
Simon Glass5ad27512016-05-05 07:28:09 -0600695 macb_phy_reset(macb, name);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200696
Andreas Bießmann1e868122014-05-26 22:55:18 +0200697 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200698 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Stefan Roese40291802019-03-27 11:20:19 +0100699 if (status & BMSR_LSTATUS) {
700 /*
701 * Delay a bit after the link is established,
702 * so that the next xfer does not fail
703 */
704 mdelay(10);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100705 break;
Stefan Roese40291802019-03-27 11:20:19 +0100706 }
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200707 udelay(100);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100708 }
709 }
710
711 if (!(status & BMSR_LSTATUS)) {
712 printf("%s: link down (status: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600713 name, status);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700714 return -ENETDOWN;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800715 }
716
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100717 /* First check for GMAC and that it is GiB capable */
718 if (gem_is_gigabit_capable(macb)) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200719 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100720
Radu Pirea1676dfb2019-06-07 14:18:36 +0300721 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
722 LPA_1000XHALF)) {
723 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
724 1 : 0);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200725
726 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600727 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800728 duplex ? "full" : "half",
729 lpa);
730
731 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200732 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
733 ncfgr |= GEM_BIT(GBE);
734
Bo Shen6f7d7d92013-04-24 15:59:28 +0800735 if (duplex)
736 ncfgr |= MACB_BIT(FD);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200737
Bo Shen6f7d7d92013-04-24 15:59:28 +0800738 macb_writel(macb, NCFGR, ncfgr);
739
Bin Mengcf821322019-05-22 00:09:45 -0700740 ret = macb_linkspd_cb(dev, _1000BASET);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700741 if (ret)
742 return ret;
743
744 return 0;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800745 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100746 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800747
748 /* fall back for EMAC checking */
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200749 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
750 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800751 media = mii_nway_result(lpa & adv);
752 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
753 ? 1 : 0);
754 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
755 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600756 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800757 speed ? "100" : "10",
758 duplex ? "full" : "half",
759 lpa);
760
761 ncfgr = macb_readl(macb, NCFGR);
Bo Shenfe19ef32015-03-04 13:35:16 +0800762 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700763 if (speed) {
Bo Shen6f7d7d92013-04-24 15:59:28 +0800764 ncfgr |= MACB_BIT(SPD);
Bin Mengcf821322019-05-22 00:09:45 -0700765 ret = macb_linkspd_cb(dev, _100BASET);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700766 } else {
Bin Mengcf821322019-05-22 00:09:45 -0700767 ret = macb_linkspd_cb(dev, _10BASET);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700768 }
769
770 if (ret)
771 return ret;
772
Bo Shen6f7d7d92013-04-24 15:59:28 +0800773 if (duplex)
774 ncfgr |= MACB_BIT(FD);
775 macb_writel(macb, NCFGR, ncfgr);
776
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700777 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100778}
779
Wu, Josh012d68d2015-06-03 16:45:44 +0800780static int gmac_init_multi_queues(struct macb_device *macb)
781{
782 int i, num_queues = 1;
783 u32 queue_mask;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530784 unsigned long paddr;
Wu, Josh012d68d2015-06-03 16:45:44 +0800785
786 /* bit 0 is never set but queue 0 always exists */
787 queue_mask = gem_readl(macb, DCFG6) & 0xff;
788 queue_mask |= 0x1;
789
790 for (i = 1; i < MACB_MAX_QUEUES; i++)
791 if (queue_mask & (1 << i))
792 num_queues++;
793
Ramon Fried6402fb192019-07-16 22:04:33 +0300794 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Josh012d68d2015-06-03 16:45:44 +0800795 macb->dummy_desc->addr = 0;
796 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200797 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530798 paddr = macb->dummy_desc_dma;
Wu, Josh012d68d2015-06-03 16:45:44 +0800799
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530800 for (i = 1; i < num_queues; i++) {
801 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
802 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
803 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
804 gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
805 i - 1);
806 gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
807 i - 1);
808 }
809 }
Wu, Josh012d68d2015-06-03 16:45:44 +0800810 return 0;
811}
812
Ramon Friedb40501f2019-07-16 22:04:36 +0300813static void gmac_configure_dma(struct macb_device *macb)
814{
815 u32 buffer_size;
816 u32 dmacfg;
817
Ramon Fried377d19d2019-07-14 18:25:14 +0300818 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Friedb40501f2019-07-16 22:04:36 +0300819 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
820 dmacfg |= GEM_BF(RXBS, buffer_size);
821
Anup Patel88799a62019-07-24 04:09:32 +0000822 if (macb->config->dma_burst_length)
823 dmacfg = GEM_BFINS(FBLDO,
824 macb->config->dma_burst_length, dmacfg);
Ramon Friedb40501f2019-07-16 22:04:36 +0300825
826 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
827 dmacfg &= ~GEM_BIT(ENDIA_PKT);
828
Anup Patela1818b12019-07-24 04:09:37 +0000829 if (macb->is_big_endian)
Ramon Friedb40501f2019-07-16 22:04:36 +0300830 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
Anup Patela1818b12019-07-24 04:09:37 +0000831 else
832 dmacfg &= ~GEM_BIT(ENDIA_DESC);
Ramon Friedb40501f2019-07-16 22:04:36 +0300833
834 dmacfg &= ~GEM_BIT(ADDR64);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530835 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
836 dmacfg |= GEM_BIT(ADDR64);
837
Ramon Friedb40501f2019-07-16 22:04:36 +0300838 gem_writel(macb, DMACFG, dmacfg);
839}
840
Wenyou Yang7b811852016-05-17 13:11:35 +0800841static int _macb_init(struct udevice *dev, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100842{
Wenyou Yang7b811852016-05-17 13:11:35 +0800843 struct macb_device *macb = dev_get_priv(dev);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200844 unsigned int val = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100845 unsigned long paddr;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700846 int ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100847 int i;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530848 int count;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100849
850 /*
851 * macb_halt should have been called at some point before now,
852 * so we'll assume the controller is idle.
853 */
854
855 /* initialize DMA descriptors */
856 paddr = macb->rx_buffer_dma;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200857 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
858 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried6402fb192019-07-16 22:04:33 +0300859 paddr |= MACB_BIT(RX_WRAP);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530860 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
861 count = i * 2;
862 else
863 count = i;
864 macb->rx_ring[count].ctrl = 0;
865 macb_set_addr(macb, &macb->rx_ring[count], paddr);
Ramon Fried377d19d2019-07-14 18:25:14 +0300866 paddr += macb->rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100867 }
Wu, Josh18052402014-05-27 16:31:05 +0800868 macb_flush_ring_desc(macb, RX);
869 macb_flush_rx_buffer(macb);
870
Andreas Bießmann1e868122014-05-26 22:55:18 +0200871 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530872 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
873 count = i * 2;
874 else
875 count = i;
876 macb_set_addr(macb, &macb->tx_ring[count], 0);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200877 if (i == (MACB_TX_RING_SIZE - 1))
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530878 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
Ramon Fried6402fb192019-07-16 22:04:33 +0300879 MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100880 else
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530881 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100882 }
Wu, Josh18052402014-05-27 16:31:05 +0800883 macb_flush_ring_desc(macb, TX);
884
Andreas Bießmann1e868122014-05-26 22:55:18 +0200885 macb->rx_tail = 0;
886 macb->tx_head = 0;
887 macb->tx_tail = 0;
Simon Glass5ad27512016-05-05 07:28:09 -0600888 macb->next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100889
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700890#ifdef CONFIG_MACB_ZYNQ
Michal Simekfc91bdb2020-03-26 15:01:29 +0100891 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700892#endif
893
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530894 macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
895 macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
896 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
897 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
898 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
899 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100900
Bo Shen6f7d7d92013-04-24 15:59:28 +0800901 if (macb_is_gem(macb)) {
Ramon Friedb40501f2019-07-16 22:04:36 +0300902 /* Initialize DMA properties */
903 gmac_configure_dma(macb);
Wu, Josh012d68d2015-06-03 16:45:44 +0800904 /* Check the multi queue and initialize the queue for tx */
905 gmac_init_multi_queues(macb);
906
Bo Shen4660b332014-11-10 15:24:01 +0800907 /*
908 * When the GMAC IP with GE feature, this bit is used to
909 * select interface between RGMII and GMII.
910 * When the GMAC IP without GE feature, this bit is used
911 * to select interface between RMII and MII.
912 */
Claudiu Bezneaa81146b2021-01-19 13:26:48 +0200913 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
914 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
915 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
916 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200917 val = macb->config->usrio->rgmii;
918 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
919 val = macb->config->usrio->rmii;
920 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
921 val = macb->config->usrio->mii;
922
923 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
924 val |= macb->config->usrio->clken;
925
926 gem_writel(macb, USRIO, val);
Ramon Fried588a5b72019-07-16 22:04:34 +0300927
928 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
929 unsigned int ncfgr = macb_readl(macb, NCFGR);
930
931 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
932 macb_writel(macb, NCFGR, ncfgr);
933 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800934 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100935 /* choose RMII or MII mode. This depends on the board */
Wenyou Yang7b811852016-05-17 13:11:35 +0800936#ifdef CONFIG_AT91FAMILY
937 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
938 macb_writel(macb, USRIO,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200939 macb->config->usrio->rmii |
940 macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +0800941 } else {
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200942 macb_writel(macb, USRIO, macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +0800943 }
944#else
945 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
946 macb_writel(macb, USRIO, 0);
947 else
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200948 macb_writel(macb, USRIO, macb->config->usrio->mii);
Wenyou Yang7b811852016-05-17 13:11:35 +0800949#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800950 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100951
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700952 ret = macb_phy_init(dev, name);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700953 if (ret)
954 return ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100955
956 /* Enable TX and RX */
957 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
958
Ben Warrende9fcb52008-01-09 18:15:53 -0500959 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100960}
961
Simon Glass5ad27512016-05-05 07:28:09 -0600962static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100963{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100964 u32 ncr, tsr;
965
966 /* Halt the controller and wait for any ongoing transmission to end. */
967 ncr = macb_readl(macb, NCR);
968 ncr |= MACB_BIT(THALT);
969 macb_writel(macb, NCR, ncr);
970
971 do {
972 tsr = macb_readl(macb, TSR);
973 } while (tsr & MACB_BIT(TGO));
974
975 /* Disable TX and RX, and clear statistics */
976 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
977}
978
Simon Glass5ad27512016-05-05 07:28:09 -0600979static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren33f84312010-06-01 11:55:42 -0700980{
Ben Warren33f84312010-06-01 11:55:42 -0700981 u32 hwaddr_bottom;
982 u16 hwaddr_top;
983
984 /* set hardware address */
Simon Glass5ad27512016-05-05 07:28:09 -0600985 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
986 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren33f84312010-06-01 11:55:42 -0700987 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glass5ad27512016-05-05 07:28:09 -0600988 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren33f84312010-06-01 11:55:42 -0700989 macb_writel(macb, SA1T, hwaddr_top);
990 return 0;
991}
992
Bo Shen6f7d7d92013-04-24 15:59:28 +0800993static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
994{
995 u32 config;
Tom Rini0b20e4f2022-11-27 10:25:15 -0500996#if defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800997 unsigned long macb_hz = macb->pclk_rate;
998#else
Bo Shen6f7d7d92013-04-24 15:59:28 +0800999 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001000#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001001
1002 if (macb_hz < 20000000)
1003 config = MACB_BF(CLK, MACB_CLK_DIV8);
1004 else if (macb_hz < 40000000)
1005 config = MACB_BF(CLK, MACB_CLK_DIV16);
1006 else if (macb_hz < 80000000)
1007 config = MACB_BF(CLK, MACB_CLK_DIV32);
1008 else
1009 config = MACB_BF(CLK, MACB_CLK_DIV64);
1010
1011 return config;
1012}
1013
1014static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1015{
1016 u32 config;
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001017
Tom Rini0b20e4f2022-11-27 10:25:15 -05001018#if defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001019 unsigned long macb_hz = macb->pclk_rate;
1020#else
Bo Shen6f7d7d92013-04-24 15:59:28 +08001021 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001022#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001023
1024 if (macb_hz < 20000000)
1025 config = GEM_BF(CLK, GEM_CLK_DIV8);
1026 else if (macb_hz < 40000000)
1027 config = GEM_BF(CLK, GEM_CLK_DIV16);
1028 else if (macb_hz < 80000000)
1029 config = GEM_BF(CLK, GEM_CLK_DIV32);
1030 else if (macb_hz < 120000000)
1031 config = GEM_BF(CLK, GEM_CLK_DIV48);
1032 else if (macb_hz < 160000000)
1033 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001034 else if (macb_hz < 240000000)
Bo Shen6f7d7d92013-04-24 15:59:28 +08001035 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001036 else if (macb_hz < 320000000)
1037 config = GEM_BF(CLK, GEM_CLK_DIV128);
1038 else
1039 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shen6f7d7d92013-04-24 15:59:28 +08001040
1041 return config;
1042}
1043
Bo Shen0e6624a2013-09-18 15:07:44 +08001044/*
1045 * Get the DMA bus width field of the network configuration register that we
1046 * should program. We find the width from decoding the design configuration
1047 * register to find the maximum supported data bus width.
1048 */
1049static u32 macb_dbw(struct macb_device *macb)
1050{
1051 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1052 case 4:
1053 return GEM_BF(DBW, GEM_DBW128);
1054 case 2:
1055 return GEM_BF(DBW, GEM_DBW64);
1056 case 1:
1057 default:
1058 return GEM_BF(DBW, GEM_DBW32);
1059 }
Simon Glass5ad27512016-05-05 07:28:09 -06001060}
1061
1062static void _macb_eth_initialize(struct macb_device *macb)
1063{
1064 int id = 0; /* This is not used by functions we call */
1065 u32 ncfgr;
1066
Ramon Fried377d19d2019-07-14 18:25:14 +03001067 if (macb_is_gem(macb))
1068 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1069 else
1070 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1071
Simon Glass5ad27512016-05-05 07:28:09 -06001072 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Fried377d19d2019-07-14 18:25:14 +03001073 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1074 MACB_RX_RING_SIZE,
Simon Glass5ad27512016-05-05 07:28:09 -06001075 &macb->rx_buffer_dma);
1076 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1077 &macb->rx_ring_dma);
1078 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1079 &macb->tx_ring_dma);
1080 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1081 &macb->dummy_desc_dma);
1082
1083 /*
1084 * Do some basic initialization so that we at least can talk
1085 * to the PHY
1086 */
1087 if (macb_is_gem(macb)) {
1088 ncfgr = gem_mdc_clk_div(id, macb);
1089 ncfgr |= macb_dbw(macb);
1090 } else {
1091 ncfgr = macb_mdc_clk_div(id, macb);
1092 }
1093
1094 macb_writel(macb, NCFGR, ncfgr);
1095}
1096
Simon Glass75c5d182016-05-05 07:28:11 -06001097static int macb_start(struct udevice *dev)
1098{
Wenyou Yang7b811852016-05-17 13:11:35 +08001099 return _macb_init(dev, dev->name);
Simon Glass75c5d182016-05-05 07:28:11 -06001100}
1101
1102static int macb_send(struct udevice *dev, void *packet, int length)
1103{
1104 struct macb_device *macb = dev_get_priv(dev);
1105
1106 return _macb_send(macb, dev->name, packet, length);
1107}
1108
1109static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1110{
1111 struct macb_device *macb = dev_get_priv(dev);
1112
1113 macb->next_rx_tail = macb->rx_tail;
1114 macb->wrapped = false;
1115
1116 return _macb_recv(macb, packetp);
1117}
1118
1119static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1120{
1121 struct macb_device *macb = dev_get_priv(dev);
1122
1123 reclaim_rx_buffers(macb, macb->next_rx_tail);
1124
1125 return 0;
1126}
1127
1128static void macb_stop(struct udevice *dev)
1129{
1130 struct macb_device *macb = dev_get_priv(dev);
1131
1132 _macb_halt(macb);
1133}
1134
1135static int macb_write_hwaddr(struct udevice *dev)
1136{
Simon Glassfa20e932020-12-03 16:55:20 -07001137 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001138 struct macb_device *macb = dev_get_priv(dev);
1139
1140 return _macb_write_hwaddr(macb, plat->enetaddr);
1141}
1142
1143static const struct eth_ops macb_eth_ops = {
1144 .start = macb_start,
1145 .send = macb_send,
1146 .recv = macb_recv,
1147 .stop = macb_stop,
1148 .free_pkt = macb_free_pkt,
1149 .write_hwaddr = macb_write_hwaddr,
1150};
1151
Wenyou Yang19449362017-02-14 16:24:40 +08001152#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001153static int macb_enable_clk(struct udevice *dev)
1154{
1155 struct macb_device *macb = dev_get_priv(dev);
1156 struct clk clk;
1157 ulong clk_rate;
1158 int ret;
1159
1160 ret = clk_get_by_index(dev, 0, &clk);
1161 if (ret)
1162 return -EINVAL;
1163
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001164 /*
Anup Patel51b51f82019-02-25 08:14:36 +00001165 * If clock driver didn't support enable or disable then
1166 * we get -ENOSYS from clk_enable(). To handle this, we
1167 * don't fail for ret == -ENOSYS.
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001168 */
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001169 ret = clk_enable(&clk);
Anup Patel51b51f82019-02-25 08:14:36 +00001170 if (ret && ret != -ENOSYS)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001171 return ret;
1172
1173 clk_rate = clk_get_rate(&clk);
1174 if (!clk_rate)
1175 return -EINVAL;
1176
1177 macb->pclk_rate = clk_rate;
1178
1179 return 0;
1180}
Wenyou Yang19449362017-02-14 16:24:40 +08001181#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001182
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001183static const struct macb_usrio_cfg macb_default_usrio = {
1184 .mii = MACB_BIT(MII),
1185 .rmii = MACB_BIT(RMII),
1186 .rgmii = GEM_BIT(RGMII),
1187 .clken = MACB_BIT(CLKEN),
1188};
1189
Padmarao Begari63459cd2021-11-17 18:21:15 +05301190static struct macb_config default_gem_config = {
Ramon Fried834040c2019-07-16 22:04:35 +03001191 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301192 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001193 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001194 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001195};
1196
Simon Glass75c5d182016-05-05 07:28:11 -06001197static int macb_eth_probe(struct udevice *dev)
1198{
Simon Glassfa20e932020-12-03 16:55:20 -07001199 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001200 struct macb_device *macb = dev_get_priv(dev);
Padmarao Begari34394ba2021-01-15 08:20:37 +05301201 struct ofnode_phandle_args phandle_args;
Anup Patel88799a62019-07-24 04:09:32 +00001202 int ret;
Wenyou Yang7b811852016-05-17 13:11:35 +08001203
Marek Behúnbc194772022-04-07 00:33:01 +02001204 macb->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +02001205 if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
Wenyou Yang7b811852016-05-17 13:11:35 +08001206 return -EINVAL;
Wenyou Yang7b811852016-05-17 13:11:35 +08001207
Padmarao Begari34394ba2021-01-15 08:20:37 +05301208 /* Read phyaddr from DT */
1209 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1210 &phandle_args))
1211 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1212 "reg", -1);
1213
Bin Meng81ce6372021-09-12 11:15:14 +08001214 macb->regs = (void *)(uintptr_t)pdata->iobase;
Simon Glass75c5d182016-05-05 07:28:11 -06001215
Anup Patela1818b12019-07-24 04:09:37 +00001216 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1217
Anup Patel88799a62019-07-24 04:09:32 +00001218 macb->config = (struct macb_config *)dev_get_driver_data(dev);
Padmarao Begari63459cd2021-11-17 18:21:15 +05301219 if (!macb->config) {
1220 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
1221 if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
1222 default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
1223 }
Anup Patel88799a62019-07-24 04:09:32 +00001224 macb->config = &default_gem_config;
Padmarao Begari63459cd2021-11-17 18:21:15 +05301225 }
Ramon Fried834040c2019-07-16 22:04:35 +03001226
Wenyou Yang19449362017-02-14 16:24:40 +08001227#ifdef CONFIG_CLK
Wenyou Yang44835ea2017-04-14 14:36:04 +08001228 ret = macb_enable_clk(dev);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001229 if (ret)
1230 return ret;
Wenyou Yang19449362017-02-14 16:24:40 +08001231#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001232
Simon Glass75c5d182016-05-05 07:28:11 -06001233 _macb_eth_initialize(macb);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001234
Simon Glass75c5d182016-05-05 07:28:11 -06001235#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang44835ea2017-04-14 14:36:04 +08001236 macb->bus = mdio_alloc();
1237 if (!macb->bus)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001238 return -ENOMEM;
Vladimir Oltean7947ab42021-09-27 14:21:52 +03001239 strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
Wenyou Yang44835ea2017-04-14 14:36:04 +08001240 macb->bus->read = macb_miiphy_read;
1241 macb->bus->write = macb_miiphy_write;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001242
Wenyou Yang44835ea2017-04-14 14:36:04 +08001243 ret = mdio_register(macb->bus);
1244 if (ret < 0)
1245 return ret;
Simon Glass75c5d182016-05-05 07:28:11 -06001246 macb->bus = miiphy_get_dev_by_name(dev->name);
1247#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +08001248
1249 return 0;
1250}
1251
1252static int macb_eth_remove(struct udevice *dev)
1253{
1254 struct macb_device *macb = dev_get_priv(dev);
1255
1256#ifdef CONFIG_PHYLIB
1257 free(macb->phydev);
1258#endif
1259 mdio_unregister(macb->bus);
1260 mdio_free(macb->bus);
Simon Glass75c5d182016-05-05 07:28:11 -06001261
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001262 return 0;
1263}
1264
1265/**
Simon Glassaad29ae2020-12-03 16:55:21 -07001266 * macb_late_eth_of_to_plat
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001267 * @dev: udevice struct
1268 * Returns 0 when operation success and negative errno number
1269 * when operation failed.
1270 */
Simon Glassaad29ae2020-12-03 16:55:21 -07001271int __weak macb_late_eth_of_to_plat(struct udevice *dev)
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001272{
Simon Glass75c5d182016-05-05 07:28:11 -06001273 return 0;
1274}
1275
Simon Glassaad29ae2020-12-03 16:55:21 -07001276static int macb_eth_of_to_plat(struct udevice *dev)
Simon Glass75c5d182016-05-05 07:28:11 -06001277{
Simon Glassfa20e932020-12-03 16:55:20 -07001278 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001279
Bin Meng81ce6372021-09-12 11:15:14 +08001280 pdata->iobase = (uintptr_t)dev_remap_addr(dev);
Ramon Friedbf15d2f2018-12-27 19:58:42 +02001281 if (!pdata->iobase)
1282 return -EINVAL;
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001283
Simon Glassaad29ae2020-12-03 16:55:21 -07001284 return macb_late_eth_of_to_plat(dev);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001285}
1286
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001287static const struct macb_usrio_cfg sama7g5_usrio = {
1288 .mii = 0,
1289 .rmii = 1,
1290 .rgmii = 2,
1291 .clken = BIT(2),
1292};
1293
Ramon Fried834040c2019-07-16 22:04:35 +03001294static const struct macb_config sama5d4_config = {
1295 .dma_burst_length = 4,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301296 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001297 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001298 .usrio = &macb_default_usrio,
Anup Patel88799a62019-07-24 04:09:32 +00001299};
1300
1301static const struct macb_config sifive_config = {
1302 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301303 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001304 .clk_init = macb_sifive_clk_init,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001305 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001306};
1307
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001308static const struct macb_config sama7g5_gmac_config = {
1309 .dma_burst_length = 16,
1310 .hw_dma_cap = HW_DMA_CAP_32B,
1311 .clk_init = macb_sama7g5_clk_init,
1312 .usrio = &sama7g5_usrio,
1313};
1314
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001315static const struct macb_config sama7g5_emac_config = {
1316 .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1317 .dma_burst_length = 16,
1318 .hw_dma_cap = HW_DMA_CAP_32B,
1319 .usrio = &sama7g5_usrio,
1320};
1321
Simon Glass75c5d182016-05-05 07:28:11 -06001322static const struct udevice_id macb_eth_ids[] = {
1323 { .compatible = "cdns,macb" },
Wenyou Yang8f155402017-04-14 14:36:05 +08001324 { .compatible = "cdns,at91sam9260-macb" },
Nicolas Ferre9115f572019-09-27 13:08:32 +00001325 { .compatible = "cdns,sam9x60-macb" },
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001326 { .compatible = "cdns,sama7g5-gem",
1327 .data = (ulong)&sama7g5_gmac_config },
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001328 { .compatible = "cdns,sama7g5-emac",
1329 .data = (ulong)&sama7g5_emac_config },
Wenyou Yang8f155402017-04-14 14:36:05 +08001330 { .compatible = "atmel,sama5d2-gem" },
1331 { .compatible = "atmel,sama5d3-gem" },
Ramon Fried834040c2019-07-16 22:04:35 +03001332 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001333 { .compatible = "cdns,zynq-gem" },
Anup Patel88799a62019-07-24 04:09:32 +00001334 { .compatible = "sifive,fu540-c000-gem",
1335 .data = (ulong)&sifive_config },
Simon Glass75c5d182016-05-05 07:28:11 -06001336 { }
1337};
1338
1339U_BOOT_DRIVER(eth_macb) = {
1340 .name = "eth_macb",
1341 .id = UCLASS_ETH,
1342 .of_match = macb_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001343 .of_to_plat = macb_eth_of_to_plat,
Simon Glass75c5d182016-05-05 07:28:11 -06001344 .probe = macb_eth_probe,
Wenyou Yang44835ea2017-04-14 14:36:04 +08001345 .remove = macb_eth_remove,
Simon Glass75c5d182016-05-05 07:28:11 -06001346 .ops = &macb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001347 .priv_auto = sizeof(struct macb_device),
Simon Glass71fa5b42020-12-03 16:55:18 -07001348 .plat_auto = sizeof(struct eth_pdata),
Simon Glass75c5d182016-05-05 07:28:11 -06001349};
1350#endif