blob: cbf5f6055189088ad09be3e0e2767bcb2c96b574 [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 */
Wenyou Yang3d8d3482016-11-02 10:06:56 +08005#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07006#include <cpu_func.h>
Simon Glass75c5d182016-05-05 07:28:11 -06007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010011
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010012/*
13 * The u-boot networking stack is a little weird. It seems like the
14 * networking core allocates receive buffers up front without any
15 * regard to the hardware that's supposed to actually receive those
16 * packets.
17 *
18 * The MACB receives packets into 128-byte receive buffers, so the
19 * buffers allocated by the core isn't very practical to use. We'll
20 * allocate our own, but we need one such buffer in case a packet
21 * wraps around the DMA ring so that we have to copy it.
22 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010024 * configuration header. This way, the core allocates one RX buffer
25 * and one TX buffer, each of which can hold a ethernet packet of
26 * maximum size.
27 *
28 * For some reason, the networking core unconditionally specifies a
29 * 32-byte packet "alignment" (which really should be called
30 * "padding"). MACB shouldn't need that, but we'll refrain from any
31 * core modifications here...
32 */
33
34#include <net.h>
35#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>
Masahiro Yamada6373a172020-02-14 16:40:19 +090040#include <linux/dma-mapping.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010041#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
Ramon Fried377d19d2019-07-14 18:25:14 +030048/*
49 * These buffer sizes must be power of 2 and divisible
50 * by RX_BUFFER_MULTIPLE
51 */
52#define MACB_RX_BUFFER_SIZE 128
53#define GEM_RX_BUFFER_SIZE 2048
Ramon Friedb40501f2019-07-16 22:04:36 +030054#define RX_BUFFER_MULTIPLE 64
Ramon Fried377d19d2019-07-14 18:25:14 +030055
56#define MACB_RX_RING_SIZE 32
Andreas Bießmann1e868122014-05-26 22:55:18 +020057#define MACB_TX_RING_SIZE 16
Ramon Fried377d19d2019-07-14 18:25:14 +030058
Andreas Bießmann1e868122014-05-26 22:55:18 +020059#define MACB_TX_TIMEOUT 1000
60#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010061
Wilson Lee41d6d1e2017-08-22 20:25:07 -070062#ifdef CONFIG_MACB_ZYNQ
63/* INCR4 AHB bursts */
64#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
65/* Use full configured addressable space (8 Kb) */
66#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
67/* Use full configured addressable space (4 Kb) */
68#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
69/* Set RXBUF with use of 128 byte */
70#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
71#define MACB_ZYNQ_GEM_DMACR_INIT \
72 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
73 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
74 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_RXBUF)
76#endif
77
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010078struct macb_dma_desc {
79 u32 addr;
80 u32 ctrl;
81};
82
Padmarao Begari7a2c4962021-01-15 08:20:36 +053083struct macb_dma_desc_64 {
84 u32 addrh;
85 u32 unused;
86};
87
88#define HW_DMA_CAP_32B 0
89#define HW_DMA_CAP_64B 1
90
91#define DMA_DESC_SIZE 16
92#define DMA_DESC_BYTES(n) ((n) * DMA_DESC_SIZE)
Wu, Josh18052402014-05-27 16:31:05 +080093#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
94#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Josh012d68d2015-06-03 16:45:44 +080095#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh18052402014-05-27 16:31:05 +080096
Yaron Micher1ae088d2022-11-10 19:31:34 +020097#define DESC_PER_CACHELINE_32 (ARCH_DMA_MINALIGN/sizeof(struct macb_dma_desc))
98#define DESC_PER_CACHELINE_64 (ARCH_DMA_MINALIGN/DMA_DESC_SIZE)
99
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100100#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100101#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100102
103struct macb_device {
104 void *regs;
Anup Patel88799a62019-07-24 04:09:32 +0000105
Anup Patela1818b12019-07-24 04:09:37 +0000106 bool is_big_endian;
107
Anup Patel88799a62019-07-24 04:09:32 +0000108 const struct macb_config *config;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100109
110 unsigned int rx_tail;
111 unsigned int tx_head;
112 unsigned int tx_tail;
Simon Glass5ad27512016-05-05 07:28:09 -0600113 unsigned int next_rx_tail;
114 bool wrapped;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100115
116 void *rx_buffer;
117 void *tx_buffer;
118 struct macb_dma_desc *rx_ring;
119 struct macb_dma_desc *tx_ring;
Ramon Fried377d19d2019-07-14 18:25:14 +0300120 size_t rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100121
122 unsigned long rx_buffer_dma;
123 unsigned long rx_ring_dma;
124 unsigned long tx_ring_dma;
125
Wu, Josh012d68d2015-06-03 16:45:44 +0800126 struct macb_dma_desc *dummy_desc;
127 unsigned long dummy_desc_dma;
128
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100129 const struct device *dev;
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100130 unsigned int duplex;
131 unsigned int speed;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100132 unsigned short phy_addr;
Bo Shen7d91deb2013-04-24 15:59:27 +0800133 struct mii_dev *bus;
Wenyou Yang44835ea2017-04-14 14:36:04 +0800134#ifdef CONFIG_PHYLIB
135 struct phy_device *phydev;
136#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800137
Wenyou Yang19449362017-02-14 16:24:40 +0800138#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800139 unsigned long pclk_rate;
Wenyou Yang19449362017-02-14 16:24:40 +0800140#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800141 phy_interface_t phy_interface;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100142};
Ramon Fried834040c2019-07-16 22:04:35 +0300143
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200144struct macb_usrio_cfg {
145 unsigned int mii;
146 unsigned int rmii;
147 unsigned int rgmii;
148 unsigned int clken;
149};
150
Ramon Fried834040c2019-07-16 22:04:35 +0300151struct macb_config {
152 unsigned int dma_burst_length;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530153 unsigned int hw_dma_cap;
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200154 unsigned int caps;
Anup Patel88799a62019-07-24 04:09:32 +0000155
156 int (*clk_init)(struct udevice *dev, ulong rate);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200157 const struct macb_usrio_cfg *usrio;
Ramon Fried834040c2019-07-16 22:04:35 +0300158};
159
Bo Shen6f7d7d92013-04-24 15:59:28 +0800160static int macb_is_gem(struct macb_device *macb)
161{
Atish Patrae1a85182019-02-25 08:14:42 +0000162 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800163}
164
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100165#ifndef cpu_is_sama5d2
166#define cpu_is_sama5d2() 0
167#endif
168
169#ifndef cpu_is_sama5d4
170#define cpu_is_sama5d4() 0
171#endif
172
173static int gem_is_gigabit_capable(struct macb_device *macb)
174{
175 /*
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400176 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100177 * configured to support only 10/100.
178 */
179 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
180}
181
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100182/* Is the port a fixed link */
183static int macb_port_is_fixed_link(struct macb_device *macb)
184{
185 return macb->phy_addr > PHY_MAX_ADDR;
186}
187
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200188static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
189 u16 value)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100190{
191 unsigned long netctl;
192 unsigned long netstat;
193 unsigned long frame;
194
195 netctl = macb_readl(macb, NCR);
196 netctl |= MACB_BIT(MPE);
197 macb_writel(macb, NCR, netctl);
198
199 frame = (MACB_BF(SOF, 1)
200 | MACB_BF(RW, 1)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200201 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100202 | MACB_BF(REGA, reg)
203 | MACB_BF(CODE, 2)
204 | MACB_BF(DATA, value));
205 macb_writel(macb, MAN, frame);
206
207 do {
208 netstat = macb_readl(macb, NSR);
209 } while (!(netstat & MACB_BIT(IDLE)));
210
211 netctl = macb_readl(macb, NCR);
212 netctl &= ~MACB_BIT(MPE);
213 macb_writel(macb, NCR, netctl);
214}
215
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200216static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100217{
218 unsigned long netctl;
219 unsigned long netstat;
220 unsigned long frame;
221
222 netctl = macb_readl(macb, NCR);
223 netctl |= MACB_BIT(MPE);
224 macb_writel(macb, NCR, netctl);
225
226 frame = (MACB_BF(SOF, 1)
227 | MACB_BF(RW, 2)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200228 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100229 | MACB_BF(REGA, reg)
230 | MACB_BF(CODE, 2));
231 macb_writel(macb, MAN, frame);
232
233 do {
234 netstat = macb_readl(macb, NSR);
235 } while (!(netstat & MACB_BIT(IDLE)));
236
237 frame = macb_readl(macb, MAN);
238
239 netctl = macb_readl(macb, NCR);
240 netctl &= ~MACB_BIT(MPE);
241 macb_writel(macb, NCR, netctl);
242
243 return MACB_BFEXT(DATA, frame);
244}
245
Joe Hershberger9e5742b2013-06-24 19:06:38 -0500246void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim77cdf0f2012-12-13 17:22:52 +0530247{
248 return;
249}
250
Bo Shen7d91deb2013-04-24 15:59:27 +0800251#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar790088e2009-12-17 15:07:15 +0200252
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500253int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar790088e2009-12-17 15:07:15 +0200254{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500255 u16 value = 0;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500256 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600257 struct macb_device *macb = dev_get_priv(dev);
Semih Hazar790088e2009-12-17 15:07:15 +0200258
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500259 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200260 value = macb_mdio_read(macb, phy_adr, reg);
Semih Hazar790088e2009-12-17 15:07:15 +0200261
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500262 return value;
Semih Hazar790088e2009-12-17 15:07:15 +0200263}
264
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500265int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
266 u16 value)
Semih Hazar790088e2009-12-17 15:07:15 +0200267{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500268 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600269 struct macb_device *macb = dev_get_priv(dev);
Semih Hazar790088e2009-12-17 15:07:15 +0200270
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500271 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200272 macb_mdio_write(macb, phy_adr, reg, value);
Semih Hazar790088e2009-12-17 15:07:15 +0200273
274 return 0;
275}
276#endif
277
Wu, Josh18052402014-05-27 16:31:05 +0800278#define RX 1
279#define TX 0
280static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
281{
282 if (rx)
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200283 invalidate_dcache_range(macb->rx_ring_dma,
284 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
285 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800286 else
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200287 invalidate_dcache_range(macb->tx_ring_dma,
288 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
289 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800290}
291
292static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
293{
294 if (rx)
295 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200296 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800297 else
298 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200299 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800300}
301
302static inline void macb_flush_rx_buffer(struct macb_device *macb)
303{
304 flush_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}
308
309static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
310{
311 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese7df65a52019-08-26 09:18:11 +0200312 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
313 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800314}
Semih Hazar790088e2009-12-17 15:07:15 +0200315
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500316#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100317
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530318static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
319{
320 return (struct macb_dma_desc_64 *)((void *)desc
321 + sizeof(struct macb_dma_desc));
322}
323
324static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
325 ulong addr)
326{
327 struct macb_dma_desc_64 *desc_64;
328
329 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
330 desc_64 = macb_64b_desc(desc);
331 desc_64->addrh = upper_32_bits(addr);
332 }
333 desc->addr = lower_32_bits(addr);
334}
335
Simon Glass5ad27512016-05-05 07:28:09 -0600336static int _macb_send(struct macb_device *macb, const char *name, void *packet,
337 int length)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100338{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100339 unsigned long paddr, ctrl;
340 unsigned int tx_head = macb->tx_head;
341 int i;
342
343 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
344
345 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried6402fb192019-07-16 22:04:33 +0300346 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200347 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300348 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100349 macb->tx_head = 0;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200350 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100351 macb->tx_head++;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200352 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100353
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530354 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
355 tx_head = tx_head * 2;
356
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100357 macb->tx_ring[tx_head].ctrl = ctrl;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530358 macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
359
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200360 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800361 macb_flush_ring_desc(macb, TX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100362 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
363
364 /*
365 * I guess this is necessary because the networking core may
366 * re-use the transmit buffer as soon as we return...
367 */
Andreas Bießmann1e868122014-05-26 22:55:18 +0200368 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200369 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800370 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200371 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300372 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100373 break;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100374 udelay(1);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100375 }
376
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900377 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100378
Andreas Bießmann1e868122014-05-26 22:55:18 +0200379 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300380 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glass5ad27512016-05-05 07:28:09 -0600381 printf("%s: TX underrun\n", name);
Ramon Fried6402fb192019-07-16 22:04:33 +0300382 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glass5ad27512016-05-05 07:28:09 -0600383 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200384 } else {
Simon Glass5ad27512016-05-05 07:28:09 -0600385 printf("%s: TX timeout\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100386 }
387
388 /* No one cares anyway */
389 return 0;
390}
391
Yaron Micher1ae088d2022-11-10 19:31:34 +0200392static void reclaim_rx_buffer(struct macb_device *macb,
393 unsigned int idx)
394{
395 unsigned int mask;
396 unsigned int shift;
397 unsigned int i;
398
399 /*
400 * There may be multiple descriptors per CPU cacheline,
401 * so a cache flush would flush the whole line, meaning the content of other descriptors
402 * in the cacheline would also flush. If one of the other descriptors had been
403 * written to by the controller, the flush would cause those changes to be lost.
404 *
405 * To circumvent this issue, we do the actual freeing only when we need to free
406 * the last descriptor in the current cacheline. When the current descriptor is the
407 * last in the cacheline, we free all the descriptors that belong to that cacheline.
408 */
409 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
410 mask = DESC_PER_CACHELINE_64 - 1;
411 shift = 1;
412 } else {
413 mask = DESC_PER_CACHELINE_32 - 1;
414 shift = 0;
415 }
416
417 /* we exit without freeing if idx is not the last descriptor in the cacheline */
418 if ((idx & mask) != mask)
419 return;
420
421 for (i = idx & (~mask); i <= idx; i++)
422 macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
423}
424
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100425static void reclaim_rx_buffers(struct macb_device *macb,
426 unsigned int new_tail)
427{
428 unsigned int i;
429
430 i = macb->rx_tail;
Wu, Josh18052402014-05-27 16:31:05 +0800431
432 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100433 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++;
Yaron Micher1ae088d2022-11-10 19:31:34 +0200436 if (i >= MACB_RX_RING_SIZE)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100437 i = 0;
438 }
439
440 while (i < new_tail) {
Yaron Micher1ae088d2022-11-10 19:31:34 +0200441 reclaim_rx_buffer(macb, i);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100442 i++;
443 }
444
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200445 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800446 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100447 macb->rx_tail = new_tail;
448}
449
Simon Glass5ad27512016-05-05 07:28:09 -0600450static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100451{
Simon Glass5ad27512016-05-05 07:28:09 -0600452 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100453 void *buffer;
454 int length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100455 u32 status;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530456 u8 flag = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100457
Simon Glass5ad27512016-05-05 07:28:09 -0600458 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100459 for (;;) {
Wu, Josh18052402014-05-27 16:31:05 +0800460 macb_invalidate_ring_desc(macb, RX);
461
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530462 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
463 next_rx_tail = next_rx_tail * 2;
464
Ramon Fried6402fb192019-07-16 22:04:33 +0300465 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glass5ad27512016-05-05 07:28:09 -0600466 return -EAGAIN;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100467
Simon Glass5ad27512016-05-05 07:28:09 -0600468 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300469 if (status & MACB_BIT(RX_SOF)) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530470 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
471 next_rx_tail = next_rx_tail / 2;
472 flag = true;
473 }
474
Simon Glass5ad27512016-05-05 07:28:09 -0600475 if (next_rx_tail != macb->rx_tail)
476 reclaim_rx_buffers(macb, next_rx_tail);
477 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100478 }
479
Ramon Fried6402fb192019-07-16 22:04:33 +0300480 if (status & MACB_BIT(RX_EOF)) {
Ramon Fried377d19d2019-07-14 18:25:14 +0300481 buffer = macb->rx_buffer +
482 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100483 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh18052402014-05-27 16:31:05 +0800484
485 macb_invalidate_rx_buffer(macb);
Simon Glass5ad27512016-05-05 07:28:09 -0600486 if (macb->wrapped) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100487 unsigned int headlen, taillen;
488
Ramon Fried377d19d2019-07-14 18:25:14 +0300489 headlen = macb->rx_buffer_size *
490 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100491 taillen = length - headlen;
Joe Hershberger9f09a362015-04-08 01:41:06 -0500492 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100493 buffer, headlen);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500494 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100495 macb->rx_buffer, taillen);
Simon Glass5ad27512016-05-05 07:28:09 -0600496 *packetp = (void *)net_rx_packets[0];
497 } else {
498 *packetp = buffer;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100499 }
500
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530501 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
502 if (!flag)
503 next_rx_tail = next_rx_tail / 2;
504 }
505
Simon Glass5ad27512016-05-05 07:28:09 -0600506 if (++next_rx_tail >= MACB_RX_RING_SIZE)
507 next_rx_tail = 0;
508 macb->next_rx_tail = next_rx_tail;
509 return length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100510 } else {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530511 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
512 if (!flag)
513 next_rx_tail = next_rx_tail / 2;
514 flag = false;
515 }
516
Simon Glass5ad27512016-05-05 07:28:09 -0600517 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
518 macb->wrapped = true;
519 next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100520 }
521 }
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200522 barrier();
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100523 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100524}
525
Simon Glass5ad27512016-05-05 07:28:09 -0600526static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100527{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100528 int i;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200529 u16 status, adv;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100530
531 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200532 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
Simon Glass5ad27512016-05-05 07:28:09 -0600533 printf("%s: Starting autonegotiation...\n", name);
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200534 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100535 | BMCR_ANRESTART));
536
Andreas Bießmann1e868122014-05-26 22:55:18 +0200537 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200538 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100539 if (status & BMSR_ANEGCOMPLETE)
540 break;
541 udelay(100);
542 }
543
544 if (status & BMSR_ANEGCOMPLETE)
Simon Glass5ad27512016-05-05 07:28:09 -0600545 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100546 else
547 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600548 name, status);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200549}
550
Wenyou Yang7b811852016-05-17 13:11:35 +0800551static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100552{
553 int i;
554 u16 phy_id;
555
Padmarao Begari34394ba2021-01-15 08:20:37 +0530556 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
557 if (phy_id != 0xffff) {
558 printf("%s: PHY present at %d\n", name, macb->phy_addr);
559 return 0;
560 }
561
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100562 /* Search for PHY... */
563 for (i = 0; i < 32; i++) {
564 macb->phy_addr = i;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200565 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100566 if (phy_id != 0xffff) {
Wenyou Yang7b811852016-05-17 13:11:35 +0800567 printf("%s: PHY present at %d\n", name, i);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700568 return 0;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100569 }
570 }
571
572 /* PHY isn't up to snuff */
Wenyou Yang7b811852016-05-17 13:11:35 +0800573 printf("%s: PHY not found\n", name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100574
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700575 return -ENODEV;
576}
577
578/**
579 * macb_linkspd_cb - Linkspeed change callback function
Bin Mengcf821322019-05-22 00:09:45 -0700580 * @dev/@regs: MACB udevice (DM version) or
581 * Base Register of MACB devices (non-DM version)
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700582 * @speed: Linkspeed
583 * Returns 0 when operation success and negative errno number
584 * when operation failed.
585 */
Anup Patel88799a62019-07-24 04:09:32 +0000586static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
587{
Anup Patel88799a62019-07-24 04:09:32 +0000588 void *gemgxl_regs;
589
Bin Meng81ce6372021-09-12 11:15:14 +0800590 gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
Anup Patel88799a62019-07-24 04:09:32 +0000591 if (!gemgxl_regs)
592 return -ENODEV;
593
594 /*
595 * SiFive GEMGXL TX clock operation mode:
596 *
597 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
598 * and output clock on GMII output signal GTX_CLK
599 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
600 */
601 writel(rate != 125000000, gemgxl_regs);
602 return 0;
603}
604
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +0200605static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
606{
607 struct clk clk;
608 int ret;
609
610 ret = clk_get_by_name(dev, "tx_clk", &clk);
611 if (ret)
612 return ret;
613
614 /*
615 * This is for using GCK. Clock rate is addressed via assigned-clock
616 * property, so only clock enable is needed here. The switching to
617 * proper clock rate depending on link speed is managed by IP logic.
618 */
619 return clk_enable(&clk);
620}
621
Bin Mengcf821322019-05-22 00:09:45 -0700622int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
623{
Bin Meng12766ca2019-05-22 00:09:46 -0700624#ifdef CONFIG_CLK
Anup Patel88799a62019-07-24 04:09:32 +0000625 struct macb_device *macb = dev_get_priv(dev);
Bin Meng12766ca2019-05-22 00:09:46 -0700626 struct clk tx_clk;
627 ulong rate;
628 int ret;
629
Bin Meng12766ca2019-05-22 00:09:46 -0700630 switch (speed) {
631 case _10BASET:
632 rate = 2500000; /* 2.5 MHz */
633 break;
634 case _100BASET:
635 rate = 25000000; /* 25 MHz */
636 break;
637 case _1000BASET:
638 rate = 125000000; /* 125 MHz */
639 break;
640 default:
641 /* does not change anything */
642 return 0;
643 }
644
Anup Patel88799a62019-07-24 04:09:32 +0000645 if (macb->config->clk_init)
646 return macb->config->clk_init(dev, rate);
647
648 /*
649 * "tx_clk" is an optional clock source for MACB.
650 * Ignore if it does not exist in DT.
651 */
652 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
653 if (ret)
654 return 0;
655
Bin Meng12766ca2019-05-22 00:09:46 -0700656 if (tx_clk.dev) {
657 ret = clk_set_rate(&tx_clk, rate);
Claudiu Bezneae2888dc2021-01-19 13:26:45 +0200658 if (ret < 0)
Bin Meng12766ca2019-05-22 00:09:46 -0700659 return ret;
660 }
661#endif
662
Bin Mengcf821322019-05-22 00:09:45 -0700663 return 0;
664}
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100665
Wenyou Yang7b811852016-05-17 13:11:35 +0800666static int macb_phy_init(struct udevice *dev, const char *name)
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200667{
Wenyou Yang7b811852016-05-17 13:11:35 +0800668 struct macb_device *macb = dev_get_priv(dev);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200669 u32 ncfgr;
670 u16 phy_id, status, adv, lpa;
671 int media, speed, duplex;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700672 int ret;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200673 int i;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100674
Simon Glass5ad27512016-05-05 07:28:09 -0600675 arch_get_mdio_control(name);
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100676 /* If port is not fixed -> setup PHY */
677 if (!macb_port_is_fixed_link(macb)) {
678 /* Auto-detect phy_addr */
679 ret = macb_phy_find(macb, name);
680 if (ret)
681 return ret;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100682
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100683 /* Check if the PHY is up to snuff... */
684 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
685 if (phy_id == 0xffff) {
686 printf("%s: No PHY present\n", name);
687 return -ENODEV;
688 }
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200689
Bo Shen7d91deb2013-04-24 15:59:27 +0800690#ifdef CONFIG_PHYLIB
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100691 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
692 macb->phy_interface);
693 if (!macb->phydev) {
694 printf("phy_connect failed\n");
695 return -ENODEV;
696 }
Bo Shene04fe552013-08-19 10:35:47 +0800697
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100698 phy_config(macb->phydev);
Bo Shen7d91deb2013-04-24 15:59:27 +0800699#endif
700
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100701 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
702 if (!(status & BMSR_LSTATUS)) {
703 /* Try to re-negotiate if we don't have link already. */
704 macb_phy_reset(macb, name);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200705
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100706 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
707 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
708 if (status & BMSR_LSTATUS) {
709 /*
710 * Delay a bit after the link is established,
711 * so that the next xfer does not fail
712 */
713 mdelay(10);
714 break;
715 }
716 udelay(100);
Stefan Roese40291802019-03-27 11:20:19 +0100717 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100718 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100719
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100720 if (!(status & BMSR_LSTATUS)) {
721 printf("%s: link down (status: 0x%04x)\n",
722 name, status);
723 return -ENETDOWN;
724 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800725
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100726 /* First check for GMAC and that it is GiB capable */
727 if (gem_is_gigabit_capable(macb)) {
728 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100729
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100730 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
731 LPA_1000XHALF)) {
732 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
733 1 : 0);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200734
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100735 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
736 name,
737 duplex ? "full" : "half",
738 lpa);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800739
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100740 ncfgr = macb_readl(macb, NCFGR);
741 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
742 ncfgr |= GEM_BIT(GBE);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200743
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100744 if (duplex)
745 ncfgr |= MACB_BIT(FD);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200746
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100747 macb_writel(macb, NCFGR, ncfgr);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800748
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100749 ret = macb_linkspd_cb(dev, _1000BASET);
750 if (ret)
751 return ret;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700752
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100753 return 0;
754 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800755 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800756
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +0100757 /* fall back for EMAC checking */
758 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
759 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
760 media = mii_nway_result(lpa & adv);
761 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
762 ? 1 : 0);
763 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
764 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
765 name,
766 speed ? "100" : "10",
767 duplex ? "full" : "half",
768 lpa);
769 } else {
770 /* if macb port is a fixed link */
771 /* TODO : manage gigabit capable processors */
772 speed = macb->speed;
773 duplex = macb->duplex;
774 printf("%s: link up, %sMbps %s-duplex\n",
775 name,
776 speed ? "100" : "10",
777 duplex ? "full" : "half");
778 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800779
780 ncfgr = macb_readl(macb, NCFGR);
Bo Shenfe19ef32015-03-04 13:35:16 +0800781 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700782 if (speed) {
Bo Shen6f7d7d92013-04-24 15:59:28 +0800783 ncfgr |= MACB_BIT(SPD);
Bin Mengcf821322019-05-22 00:09:45 -0700784 ret = macb_linkspd_cb(dev, _100BASET);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700785 } else {
Bin Mengcf821322019-05-22 00:09:45 -0700786 ret = macb_linkspd_cb(dev, _10BASET);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700787 }
788
789 if (ret)
790 return ret;
791
Bo Shen6f7d7d92013-04-24 15:59:28 +0800792 if (duplex)
793 ncfgr |= MACB_BIT(FD);
794 macb_writel(macb, NCFGR, ncfgr);
795
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700796 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100797}
798
Wu, Josh012d68d2015-06-03 16:45:44 +0800799static int gmac_init_multi_queues(struct macb_device *macb)
800{
801 int i, num_queues = 1;
802 u32 queue_mask;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530803 unsigned long paddr;
Wu, Josh012d68d2015-06-03 16:45:44 +0800804
805 /* bit 0 is never set but queue 0 always exists */
806 queue_mask = gem_readl(macb, DCFG6) & 0xff;
807 queue_mask |= 0x1;
808
809 for (i = 1; i < MACB_MAX_QUEUES; i++)
810 if (queue_mask & (1 << i))
811 num_queues++;
812
Ramon Fried6402fb192019-07-16 22:04:33 +0300813 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Josh012d68d2015-06-03 16:45:44 +0800814 macb->dummy_desc->addr = 0;
815 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200816 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530817 paddr = macb->dummy_desc_dma;
Wu, Josh012d68d2015-06-03 16:45:44 +0800818
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530819 for (i = 1; i < num_queues; i++) {
820 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
821 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
822 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
823 gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
824 i - 1);
825 gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
826 i - 1);
827 }
828 }
Wu, Josh012d68d2015-06-03 16:45:44 +0800829 return 0;
830}
831
Ramon Friedb40501f2019-07-16 22:04:36 +0300832static void gmac_configure_dma(struct macb_device *macb)
833{
834 u32 buffer_size;
835 u32 dmacfg;
836
Ramon Fried377d19d2019-07-14 18:25:14 +0300837 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Friedb40501f2019-07-16 22:04:36 +0300838 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
839 dmacfg |= GEM_BF(RXBS, buffer_size);
840
Anup Patel88799a62019-07-24 04:09:32 +0000841 if (macb->config->dma_burst_length)
842 dmacfg = GEM_BFINS(FBLDO,
843 macb->config->dma_burst_length, dmacfg);
Ramon Friedb40501f2019-07-16 22:04:36 +0300844
845 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
846 dmacfg &= ~GEM_BIT(ENDIA_PKT);
847
Anup Patela1818b12019-07-24 04:09:37 +0000848 if (macb->is_big_endian)
Ramon Friedb40501f2019-07-16 22:04:36 +0300849 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
Anup Patela1818b12019-07-24 04:09:37 +0000850 else
851 dmacfg &= ~GEM_BIT(ENDIA_DESC);
Ramon Friedb40501f2019-07-16 22:04:36 +0300852
853 dmacfg &= ~GEM_BIT(ADDR64);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530854 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
855 dmacfg |= GEM_BIT(ADDR64);
856
Ramon Friedb40501f2019-07-16 22:04:36 +0300857 gem_writel(macb, DMACFG, dmacfg);
858}
859
Wenyou Yang7b811852016-05-17 13:11:35 +0800860static int _macb_init(struct udevice *dev, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100861{
Wenyou Yang7b811852016-05-17 13:11:35 +0800862 struct macb_device *macb = dev_get_priv(dev);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200863 unsigned int val = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100864 unsigned long paddr;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700865 int ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100866 int i;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530867 int count;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100868
869 /*
870 * macb_halt should have been called at some point before now,
871 * so we'll assume the controller is idle.
872 */
873
874 /* initialize DMA descriptors */
875 paddr = macb->rx_buffer_dma;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200876 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
877 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried6402fb192019-07-16 22:04:33 +0300878 paddr |= MACB_BIT(RX_WRAP);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530879 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
880 count = i * 2;
881 else
882 count = i;
883 macb->rx_ring[count].ctrl = 0;
884 macb_set_addr(macb, &macb->rx_ring[count], paddr);
Ramon Fried377d19d2019-07-14 18:25:14 +0300885 paddr += macb->rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100886 }
Wu, Josh18052402014-05-27 16:31:05 +0800887 macb_flush_ring_desc(macb, RX);
888 macb_flush_rx_buffer(macb);
889
Andreas Bießmann1e868122014-05-26 22:55:18 +0200890 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530891 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
892 count = i * 2;
893 else
894 count = i;
895 macb_set_addr(macb, &macb->tx_ring[count], 0);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200896 if (i == (MACB_TX_RING_SIZE - 1))
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530897 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
Ramon Fried6402fb192019-07-16 22:04:33 +0300898 MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100899 else
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530900 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100901 }
Wu, Josh18052402014-05-27 16:31:05 +0800902 macb_flush_ring_desc(macb, TX);
903
Andreas Bießmann1e868122014-05-26 22:55:18 +0200904 macb->rx_tail = 0;
905 macb->tx_head = 0;
906 macb->tx_tail = 0;
Simon Glass5ad27512016-05-05 07:28:09 -0600907 macb->next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100908
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700909#ifdef CONFIG_MACB_ZYNQ
Michal Simekfc91bdb2020-03-26 15:01:29 +0100910 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700911#endif
912
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530913 macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
914 macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
915 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
916 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
917 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
918 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100919
Bo Shen6f7d7d92013-04-24 15:59:28 +0800920 if (macb_is_gem(macb)) {
Ramon Friedb40501f2019-07-16 22:04:36 +0300921 /* Initialize DMA properties */
922 gmac_configure_dma(macb);
Wu, Josh012d68d2015-06-03 16:45:44 +0800923 /* Check the multi queue and initialize the queue for tx */
924 gmac_init_multi_queues(macb);
925
Bo Shen4660b332014-11-10 15:24:01 +0800926 /*
927 * When the GMAC IP with GE feature, this bit is used to
928 * select interface between RGMII and GMII.
929 * When the GMAC IP without GE feature, this bit is used
930 * to select interface between RMII and MII.
931 */
Claudiu Bezneaa81146b2021-01-19 13:26:48 +0200932 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
933 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
934 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
935 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200936 val = macb->config->usrio->rgmii;
937 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
938 val = macb->config->usrio->rmii;
939 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
940 val = macb->config->usrio->mii;
941
942 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
943 val |= macb->config->usrio->clken;
944
945 gem_writel(macb, USRIO, val);
Ramon Fried588a5b72019-07-16 22:04:34 +0300946
947 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
948 unsigned int ncfgr = macb_readl(macb, NCFGR);
949
950 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
951 macb_writel(macb, NCFGR, ncfgr);
952 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800953 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100954 /* choose RMII or MII mode. This depends on the board */
Wenyou Yang7b811852016-05-17 13:11:35 +0800955#ifdef CONFIG_AT91FAMILY
956 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
957 macb_writel(macb, USRIO,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200958 macb->config->usrio->rmii |
959 macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +0800960 } else {
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200961 macb_writel(macb, USRIO, macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +0800962 }
963#else
964 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
965 macb_writel(macb, USRIO, 0);
966 else
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200967 macb_writel(macb, USRIO, macb->config->usrio->mii);
Wenyou Yang7b811852016-05-17 13:11:35 +0800968#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +0800969 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100970
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700971 ret = macb_phy_init(dev, name);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700972 if (ret)
973 return ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100974
975 /* Enable TX and RX */
976 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
977
Ben Warrende9fcb52008-01-09 18:15:53 -0500978 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100979}
980
Simon Glass5ad27512016-05-05 07:28:09 -0600981static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100982{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100983 u32 ncr, tsr;
984
985 /* Halt the controller and wait for any ongoing transmission to end. */
986 ncr = macb_readl(macb, NCR);
987 ncr |= MACB_BIT(THALT);
988 macb_writel(macb, NCR, ncr);
989
990 do {
991 tsr = macb_readl(macb, TSR);
992 } while (tsr & MACB_BIT(TGO));
993
994 /* Disable TX and RX, and clear statistics */
995 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
996}
997
Simon Glass5ad27512016-05-05 07:28:09 -0600998static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren33f84312010-06-01 11:55:42 -0700999{
Ben Warren33f84312010-06-01 11:55:42 -07001000 u32 hwaddr_bottom;
1001 u16 hwaddr_top;
1002
1003 /* set hardware address */
Simon Glass5ad27512016-05-05 07:28:09 -06001004 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
1005 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren33f84312010-06-01 11:55:42 -07001006 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glass5ad27512016-05-05 07:28:09 -06001007 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren33f84312010-06-01 11:55:42 -07001008 macb_writel(macb, SA1T, hwaddr_top);
1009 return 0;
1010}
1011
Bo Shen6f7d7d92013-04-24 15:59:28 +08001012static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
1013{
1014 u32 config;
Tom Rini0b20e4f2022-11-27 10:25:15 -05001015#if defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001016 unsigned long macb_hz = macb->pclk_rate;
1017#else
Bo Shen6f7d7d92013-04-24 15:59:28 +08001018 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001019#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001020
1021 if (macb_hz < 20000000)
1022 config = MACB_BF(CLK, MACB_CLK_DIV8);
1023 else if (macb_hz < 40000000)
1024 config = MACB_BF(CLK, MACB_CLK_DIV16);
1025 else if (macb_hz < 80000000)
1026 config = MACB_BF(CLK, MACB_CLK_DIV32);
1027 else
1028 config = MACB_BF(CLK, MACB_CLK_DIV64);
1029
1030 return config;
1031}
1032
1033static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1034{
1035 u32 config;
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001036
Tom Rini0b20e4f2022-11-27 10:25:15 -05001037#if defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001038 unsigned long macb_hz = macb->pclk_rate;
1039#else
Bo Shen6f7d7d92013-04-24 15:59:28 +08001040 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001041#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001042
1043 if (macb_hz < 20000000)
1044 config = GEM_BF(CLK, GEM_CLK_DIV8);
1045 else if (macb_hz < 40000000)
1046 config = GEM_BF(CLK, GEM_CLK_DIV16);
1047 else if (macb_hz < 80000000)
1048 config = GEM_BF(CLK, GEM_CLK_DIV32);
1049 else if (macb_hz < 120000000)
1050 config = GEM_BF(CLK, GEM_CLK_DIV48);
1051 else if (macb_hz < 160000000)
1052 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001053 else if (macb_hz < 240000000)
Bo Shen6f7d7d92013-04-24 15:59:28 +08001054 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001055 else if (macb_hz < 320000000)
1056 config = GEM_BF(CLK, GEM_CLK_DIV128);
1057 else
1058 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shen6f7d7d92013-04-24 15:59:28 +08001059
1060 return config;
1061}
1062
Bo Shen0e6624a2013-09-18 15:07:44 +08001063/*
1064 * Get the DMA bus width field of the network configuration register that we
1065 * should program. We find the width from decoding the design configuration
1066 * register to find the maximum supported data bus width.
1067 */
1068static u32 macb_dbw(struct macb_device *macb)
1069{
1070 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1071 case 4:
1072 return GEM_BF(DBW, GEM_DBW128);
1073 case 2:
1074 return GEM_BF(DBW, GEM_DBW64);
1075 case 1:
1076 default:
1077 return GEM_BF(DBW, GEM_DBW32);
1078 }
Simon Glass5ad27512016-05-05 07:28:09 -06001079}
1080
1081static void _macb_eth_initialize(struct macb_device *macb)
1082{
1083 int id = 0; /* This is not used by functions we call */
1084 u32 ncfgr;
1085
Ramon Fried377d19d2019-07-14 18:25:14 +03001086 if (macb_is_gem(macb))
1087 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1088 else
1089 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1090
Simon Glass5ad27512016-05-05 07:28:09 -06001091 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Fried377d19d2019-07-14 18:25:14 +03001092 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1093 MACB_RX_RING_SIZE,
Simon Glass5ad27512016-05-05 07:28:09 -06001094 &macb->rx_buffer_dma);
1095 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1096 &macb->rx_ring_dma);
1097 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1098 &macb->tx_ring_dma);
1099 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1100 &macb->dummy_desc_dma);
1101
1102 /*
1103 * Do some basic initialization so that we at least can talk
1104 * to the PHY
1105 */
1106 if (macb_is_gem(macb)) {
1107 ncfgr = gem_mdc_clk_div(id, macb);
1108 ncfgr |= macb_dbw(macb);
1109 } else {
1110 ncfgr = macb_mdc_clk_div(id, macb);
1111 }
1112
1113 macb_writel(macb, NCFGR, ncfgr);
1114}
1115
Simon Glass75c5d182016-05-05 07:28:11 -06001116static int macb_start(struct udevice *dev)
1117{
Wenyou Yang7b811852016-05-17 13:11:35 +08001118 return _macb_init(dev, dev->name);
Simon Glass75c5d182016-05-05 07:28:11 -06001119}
1120
1121static int macb_send(struct udevice *dev, void *packet, int length)
1122{
1123 struct macb_device *macb = dev_get_priv(dev);
1124
1125 return _macb_send(macb, dev->name, packet, length);
1126}
1127
1128static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1129{
1130 struct macb_device *macb = dev_get_priv(dev);
1131
1132 macb->next_rx_tail = macb->rx_tail;
1133 macb->wrapped = false;
1134
1135 return _macb_recv(macb, packetp);
1136}
1137
1138static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1139{
1140 struct macb_device *macb = dev_get_priv(dev);
1141
1142 reclaim_rx_buffers(macb, macb->next_rx_tail);
1143
1144 return 0;
1145}
1146
1147static void macb_stop(struct udevice *dev)
1148{
1149 struct macb_device *macb = dev_get_priv(dev);
1150
1151 _macb_halt(macb);
1152}
1153
1154static int macb_write_hwaddr(struct udevice *dev)
1155{
Simon Glassfa20e932020-12-03 16:55:20 -07001156 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001157 struct macb_device *macb = dev_get_priv(dev);
1158
1159 return _macb_write_hwaddr(macb, plat->enetaddr);
1160}
1161
1162static const struct eth_ops macb_eth_ops = {
1163 .start = macb_start,
1164 .send = macb_send,
1165 .recv = macb_recv,
1166 .stop = macb_stop,
1167 .free_pkt = macb_free_pkt,
1168 .write_hwaddr = macb_write_hwaddr,
1169};
1170
Wenyou Yang19449362017-02-14 16:24:40 +08001171#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001172static int macb_enable_clk(struct udevice *dev)
1173{
1174 struct macb_device *macb = dev_get_priv(dev);
1175 struct clk clk;
1176 ulong clk_rate;
1177 int ret;
1178
1179 ret = clk_get_by_index(dev, 0, &clk);
1180 if (ret)
1181 return -EINVAL;
1182
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001183 /*
Anup Patel51b51f82019-02-25 08:14:36 +00001184 * If clock driver didn't support enable or disable then
1185 * we get -ENOSYS from clk_enable(). To handle this, we
1186 * don't fail for ret == -ENOSYS.
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001187 */
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001188 ret = clk_enable(&clk);
Anup Patel51b51f82019-02-25 08:14:36 +00001189 if (ret && ret != -ENOSYS)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001190 return ret;
1191
1192 clk_rate = clk_get_rate(&clk);
1193 if (!clk_rate)
1194 return -EINVAL;
1195
1196 macb->pclk_rate = clk_rate;
1197
1198 return 0;
1199}
Wenyou Yang19449362017-02-14 16:24:40 +08001200#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001201
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001202static const struct macb_usrio_cfg macb_default_usrio = {
1203 .mii = MACB_BIT(MII),
1204 .rmii = MACB_BIT(RMII),
1205 .rgmii = GEM_BIT(RGMII),
1206 .clken = MACB_BIT(CLKEN),
1207};
1208
Padmarao Begari63459cd2021-11-17 18:21:15 +05301209static struct macb_config default_gem_config = {
Ramon Fried834040c2019-07-16 22:04:35 +03001210 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301211 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001212 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001213 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001214};
1215
Simon Glass75c5d182016-05-05 07:28:11 -06001216static int macb_eth_probe(struct udevice *dev)
1217{
Simon Glassfa20e932020-12-03 16:55:20 -07001218 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001219 struct macb_device *macb = dev_get_priv(dev);
Padmarao Begari34394ba2021-01-15 08:20:37 +05301220 struct ofnode_phandle_args phandle_args;
Anup Patel88799a62019-07-24 04:09:32 +00001221 int ret;
Wenyou Yang7b811852016-05-17 13:11:35 +08001222
Marek Behúnbc194772022-04-07 00:33:01 +02001223 macb->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +02001224 if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
Wenyou Yang7b811852016-05-17 13:11:35 +08001225 return -EINVAL;
Wenyou Yang7b811852016-05-17 13:11:35 +08001226
Padmarao Begari34394ba2021-01-15 08:20:37 +05301227 /* Read phyaddr from DT */
1228 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1229 &phandle_args))
1230 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1231 "reg", -1);
1232
Bin Meng81ce6372021-09-12 11:15:14 +08001233 macb->regs = (void *)(uintptr_t)pdata->iobase;
Simon Glass75c5d182016-05-05 07:28:11 -06001234
Anup Patela1818b12019-07-24 04:09:37 +00001235 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1236
Anup Patel88799a62019-07-24 04:09:32 +00001237 macb->config = (struct macb_config *)dev_get_driver_data(dev);
Padmarao Begari63459cd2021-11-17 18:21:15 +05301238 if (!macb->config) {
1239 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
1240 if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
1241 default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
1242 }
Anup Patel88799a62019-07-24 04:09:32 +00001243 macb->config = &default_gem_config;
Padmarao Begari63459cd2021-11-17 18:21:15 +05301244 }
Ramon Fried834040c2019-07-16 22:04:35 +03001245
Wenyou Yang19449362017-02-14 16:24:40 +08001246#ifdef CONFIG_CLK
Wenyou Yang44835ea2017-04-14 14:36:04 +08001247 ret = macb_enable_clk(dev);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001248 if (ret)
1249 return ret;
Wenyou Yang19449362017-02-14 16:24:40 +08001250#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001251
Simon Glass75c5d182016-05-05 07:28:11 -06001252 _macb_eth_initialize(macb);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001253
Simon Glass75c5d182016-05-05 07:28:11 -06001254#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang44835ea2017-04-14 14:36:04 +08001255 macb->bus = mdio_alloc();
1256 if (!macb->bus)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001257 return -ENOMEM;
Vladimir Oltean7947ab42021-09-27 14:21:52 +03001258 strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
Wenyou Yang44835ea2017-04-14 14:36:04 +08001259 macb->bus->read = macb_miiphy_read;
1260 macb->bus->write = macb_miiphy_write;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001261
Wenyou Yang44835ea2017-04-14 14:36:04 +08001262 ret = mdio_register(macb->bus);
1263 if (ret < 0)
1264 return ret;
Simon Glass75c5d182016-05-05 07:28:11 -06001265 macb->bus = miiphy_get_dev_by_name(dev->name);
1266#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +08001267
1268 return 0;
1269}
1270
1271static int macb_eth_remove(struct udevice *dev)
1272{
1273 struct macb_device *macb = dev_get_priv(dev);
1274
1275#ifdef CONFIG_PHYLIB
1276 free(macb->phydev);
1277#endif
1278 mdio_unregister(macb->bus);
1279 mdio_free(macb->bus);
Simon Glass75c5d182016-05-05 07:28:11 -06001280
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001281 return 0;
1282}
1283
1284/**
Simon Glassaad29ae2020-12-03 16:55:21 -07001285 * macb_late_eth_of_to_plat
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001286 * @dev: udevice struct
1287 * Returns 0 when operation success and negative errno number
1288 * when operation failed.
1289 */
Simon Glassaad29ae2020-12-03 16:55:21 -07001290int __weak macb_late_eth_of_to_plat(struct udevice *dev)
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001291{
Simon Glass75c5d182016-05-05 07:28:11 -06001292 return 0;
1293}
1294
Simon Glassaad29ae2020-12-03 16:55:21 -07001295static int macb_eth_of_to_plat(struct udevice *dev)
Simon Glass75c5d182016-05-05 07:28:11 -06001296{
Simon Glassfa20e932020-12-03 16:55:20 -07001297 struct eth_pdata *pdata = dev_get_plat(dev);
BELOUARGA Mohamedc0ff8a42024-02-06 20:04:02 +01001298 struct macb_device *macb = dev_get_priv(dev);
1299 void *blob = (void *)gd->fdt_blob;
1300 int node = dev_of_offset(dev);
1301 int fl_node, speed_fdt;
1302
1303 /* fetch 'fixed-link' property */
1304 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1305 if (fl_node >= 0) {
1306 /* set phy_addr to invalid value for fixed link */
1307 macb->phy_addr = PHY_MAX_ADDR + 1;
1308 macb->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1309 speed_fdt = fdtdec_get_int(blob, fl_node, "speed", 0);
1310 if (speed_fdt == 100) {
1311 macb->speed = 1;
1312 } else if (speed_fdt == 10) {
1313 macb->speed = 0;
1314 } else {
1315 printf("%s: The given speed %d of ethernet in the DT is not supported\n",
1316 __func__, speed_fdt);
1317 return -EINVAL;
1318 }
1319 }
Simon Glass75c5d182016-05-05 07:28:11 -06001320
Bin Meng81ce6372021-09-12 11:15:14 +08001321 pdata->iobase = (uintptr_t)dev_remap_addr(dev);
Ramon Friedbf15d2f2018-12-27 19:58:42 +02001322 if (!pdata->iobase)
1323 return -EINVAL;
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001324
Simon Glassaad29ae2020-12-03 16:55:21 -07001325 return macb_late_eth_of_to_plat(dev);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001326}
1327
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001328static const struct macb_usrio_cfg sama7g5_usrio = {
1329 .mii = 0,
1330 .rmii = 1,
1331 .rgmii = 2,
1332 .clken = BIT(2),
1333};
1334
Ramon Fried834040c2019-07-16 22:04:35 +03001335static const struct macb_config sama5d4_config = {
1336 .dma_burst_length = 4,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301337 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001338 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001339 .usrio = &macb_default_usrio,
Anup Patel88799a62019-07-24 04:09:32 +00001340};
1341
1342static const struct macb_config sifive_config = {
1343 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301344 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001345 .clk_init = macb_sifive_clk_init,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001346 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001347};
1348
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001349static const struct macb_config sama7g5_gmac_config = {
1350 .dma_burst_length = 16,
1351 .hw_dma_cap = HW_DMA_CAP_32B,
1352 .clk_init = macb_sama7g5_clk_init,
1353 .usrio = &sama7g5_usrio,
1354};
1355
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001356static const struct macb_config sama7g5_emac_config = {
1357 .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1358 .dma_burst_length = 16,
1359 .hw_dma_cap = HW_DMA_CAP_32B,
1360 .usrio = &sama7g5_usrio,
1361};
1362
Simon Glass75c5d182016-05-05 07:28:11 -06001363static const struct udevice_id macb_eth_ids[] = {
1364 { .compatible = "cdns,macb" },
Wenyou Yang8f155402017-04-14 14:36:05 +08001365 { .compatible = "cdns,at91sam9260-macb" },
Nicolas Ferre9115f572019-09-27 13:08:32 +00001366 { .compatible = "cdns,sam9x60-macb" },
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001367 { .compatible = "cdns,sama7g5-gem",
1368 .data = (ulong)&sama7g5_gmac_config },
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001369 { .compatible = "cdns,sama7g5-emac",
1370 .data = (ulong)&sama7g5_emac_config },
Wenyou Yang8f155402017-04-14 14:36:05 +08001371 { .compatible = "atmel,sama5d2-gem" },
1372 { .compatible = "atmel,sama5d3-gem" },
Ramon Fried834040c2019-07-16 22:04:35 +03001373 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001374 { .compatible = "cdns,zynq-gem" },
Anup Patel88799a62019-07-24 04:09:32 +00001375 { .compatible = "sifive,fu540-c000-gem",
1376 .data = (ulong)&sifive_config },
Simon Glass75c5d182016-05-05 07:28:11 -06001377 { }
1378};
1379
1380U_BOOT_DRIVER(eth_macb) = {
1381 .name = "eth_macb",
1382 .id = UCLASS_ETH,
1383 .of_match = macb_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001384 .of_to_plat = macb_eth_of_to_plat,
Simon Glass75c5d182016-05-05 07:28:11 -06001385 .probe = macb_eth_probe,
Wenyou Yang44835ea2017-04-14 14:36:04 +08001386 .remove = macb_eth_remove,
Simon Glass75c5d182016-05-05 07:28:11 -06001387 .ops = &macb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001388 .priv_auto = sizeof(struct macb_device),
Simon Glass71fa5b42020-12-03 16:55:18 -07001389 .plat_auto = sizeof(struct eth_pdata),
Simon Glass75c5d182016-05-05 07:28:11 -06001390};
1391#endif