blob: a4e3698c600317ebb1c90ffbca49d917f1b8e6aa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren50709602016-10-21 14:46:47 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
4 *
Stephen Warren50709602016-10-21 14:46:47 -06005 * Portions based on U-Boot's rtl8169.c.
6 */
7
8/*
9 * This driver supports the Synopsys Designware Ethernet QOS (Quality Of
10 * Service) IP block. The IP supports multiple options for bus type, clocking/
11 * reset structure, and feature list.
12 *
13 * The driver is written such that generic core logic is kept separate from
14 * configuration-specific logic. Code that interacts with configuration-
15 * specific resources is split out into separate functions to avoid polluting
16 * common code. If/when this driver is enhanced to support multiple
17 * configurations, the core code should be adapted to call all configuration-
18 * specific functions through function pointers, with the definition of those
19 * function pointers being supplied by struct udevice_id eqos_ids[]'s .data
20 * field.
21 *
22 * The following configurations are currently supported:
23 * tegra186:
24 * NVIDIA's Tegra186 chip. This configuration uses an AXI master/DMA bus, an
25 * AHB slave/register bus, contains the DMA, MTL, and MAC sub-blocks, and
26 * supports a single RGMII PHY. This configuration also has SW control over
27 * all clock and reset signals to the HW block.
28 */
Patrick Delaunay9e6ed382020-09-09 18:30:06 +020029
Patrick Delaunay57872842021-07-20 20:15:29 +020030#define LOG_CATEGORY UCLASS_ETH
31
Stephen Warren50709602016-10-21 14:46:47 -060032#include <common.h>
33#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070034#include <cpu_func.h>
Stephen Warren50709602016-10-21 14:46:47 -060035#include <dm.h>
36#include <errno.h>
Patrick Delaunay41729272022-06-30 11:09:41 +020037#include <eth_phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060038#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070039#include <malloc.h>
Stephen Warren50709602016-10-21 14:46:47 -060040#include <memalign.h>
41#include <miiphy.h>
42#include <net.h>
43#include <netdev.h>
44#include <phy.h>
45#include <reset.h>
46#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060047#include <asm/cache.h>
Stephen Warren50709602016-10-21 14:46:47 -060048#include <asm/gpio.h>
49#include <asm/io.h>
Fugang Duandd455e62020-05-03 22:41:18 +080050#ifdef CONFIG_ARCH_IMX8M
51#include <asm/arch/clock.h>
52#include <asm/mach-imx/sys_proto.h>
53#endif
Simon Glassdbd79542020-05-10 11:40:11 -060054#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060055#include <linux/printk.h>
Stephen Warren50709602016-10-21 14:46:47 -060056
Peng Fanc0a59952022-07-26 16:41:14 +080057#include "dwc_eth_qos.h"
Stephen Warren50709602016-10-21 14:46:47 -060058
59/*
60 * TX and RX descriptors are 16 bytes. This causes problems with the cache
61 * maintenance on CPUs where the cache-line size exceeds the size of these
62 * descriptors. What will happen is that when the driver receives a packet
63 * it will be immediately requeued for the hardware to reuse. The CPU will
64 * therefore need to flush the cache-line containing the descriptor, which
65 * will cause all other descriptors in the same cache-line to be flushed
66 * along with it. If one of those descriptors had been written to by the
67 * device those changes (and the associated packet) will be lost.
68 *
69 * To work around this, we make use of non-cached memory if available. If
70 * descriptors are mapped uncached there's no need to manually flush them
71 * or invalidate them.
72 *
73 * Note that this only applies to descriptors. The packet data buffers do
74 * not have the same constraints since they are 1536 bytes large, so they
75 * are unlikely to share cache-lines.
76 */
Marek Vasut89077732021-01-07 11:12:16 +010077static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -060078{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020079 return memalign(ARCH_DMA_MINALIGN, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060080}
81
82static void eqos_free_descs(void *descs)
83{
Stephen Warren50709602016-10-21 14:46:47 -060084 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -060085}
86
Marek Vasut89077732021-01-07 11:12:16 +010087static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
88 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -060089{
Marek Vasut90cc13a2022-10-09 17:51:45 +020090 return (rx ? eqos->rx_descs : eqos->tx_descs) +
91 (num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060092}
93
Peng Fanc0a59952022-07-26 16:41:14 +080094void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -060095{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020096 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +010097 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
98 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +020099
100 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600101}
102
Peng Fanc0a59952022-07-26 16:41:14 +0800103void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200104{
Marek Vasut3e8a1be2022-10-09 17:51:46 +0200105 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +0100106 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
107 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200108
109 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200110}
111
Marek Vasut7b6fec22023-03-06 15:53:45 +0100112static void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600113{
114 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
115 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
116
117 invalidate_dcache_range(start, end);
118}
119
Peng Fanc0a59952022-07-26 16:41:14 +0800120void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200121{
122 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
123 unsigned long end = roundup((unsigned long)buf + size,
124 ARCH_DMA_MINALIGN);
125
126 invalidate_dcache_range(start, end);
127}
128
129static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600130{
131 flush_cache((unsigned long)buf, size);
132}
133
Peng Fanc0a59952022-07-26 16:41:14 +0800134void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200135{
136 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
137 unsigned long end = roundup((unsigned long)buf + size,
138 ARCH_DMA_MINALIGN);
139
140 flush_dcache_range(start, end);
141}
142
Stephen Warren50709602016-10-21 14:46:47 -0600143static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
144{
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100145 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
146 EQOS_MAC_MDIO_ADDRESS_GB, false,
147 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600148}
149
150static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
151 int mdio_reg)
152{
153 struct eqos_priv *eqos = bus->priv;
154 u32 val;
155 int ret;
156
157 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
158 mdio_reg);
159
160 ret = eqos_mdio_wait_idle(eqos);
161 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900162 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600163 return ret;
164 }
165
166 val = readl(&eqos->mac_regs->mdio_address);
167 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
168 EQOS_MAC_MDIO_ADDRESS_C45E;
169 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
170 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200171 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600172 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
173 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
174 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
175 EQOS_MAC_MDIO_ADDRESS_GB;
176 writel(val, &eqos->mac_regs->mdio_address);
177
Christophe Roullier6beb7802019-05-17 15:08:44 +0200178 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600179
180 ret = eqos_mdio_wait_idle(eqos);
181 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900182 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600183 return ret;
184 }
185
186 val = readl(&eqos->mac_regs->mdio_data);
187 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
188
189 debug("%s: val=%x\n", __func__, val);
190
191 return val;
192}
193
194static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
195 int mdio_reg, u16 mdio_val)
196{
197 struct eqos_priv *eqos = bus->priv;
198 u32 val;
199 int ret;
200
201 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
202 mdio_addr, mdio_reg, mdio_val);
203
204 ret = eqos_mdio_wait_idle(eqos);
205 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900206 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600207 return ret;
208 }
209
210 writel(mdio_val, &eqos->mac_regs->mdio_data);
211
212 val = readl(&eqos->mac_regs->mdio_address);
213 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
214 EQOS_MAC_MDIO_ADDRESS_C45E;
215 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
216 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200217 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600218 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
219 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
220 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
221 EQOS_MAC_MDIO_ADDRESS_GB;
222 writel(val, &eqos->mac_regs->mdio_address);
223
Christophe Roullier6beb7802019-05-17 15:08:44 +0200224 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600225
226 ret = eqos_mdio_wait_idle(eqos);
227 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900228 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600229 return ret;
230 }
231
232 return 0;
233}
234
235static int eqos_start_clks_tegra186(struct udevice *dev)
236{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800237#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600238 struct eqos_priv *eqos = dev_get_priv(dev);
239 int ret;
240
241 debug("%s(dev=%p):\n", __func__, dev);
242
243 ret = clk_enable(&eqos->clk_slave_bus);
244 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900245 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600246 goto err;
247 }
248
249 ret = clk_enable(&eqos->clk_master_bus);
250 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900251 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600252 goto err_disable_clk_slave_bus;
253 }
254
255 ret = clk_enable(&eqos->clk_rx);
256 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900257 pr_err("clk_enable(clk_rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600258 goto err_disable_clk_master_bus;
259 }
260
261 ret = clk_enable(&eqos->clk_ptp_ref);
262 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900263 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600264 goto err_disable_clk_rx;
265 }
266
267 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
268 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900269 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600270 goto err_disable_clk_ptp_ref;
271 }
272
273 ret = clk_enable(&eqos->clk_tx);
274 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900275 pr_err("clk_enable(clk_tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600276 goto err_disable_clk_ptp_ref;
277 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800278#endif
Stephen Warren50709602016-10-21 14:46:47 -0600279
280 debug("%s: OK\n", __func__);
281 return 0;
282
Fugang Duan37aae5f2020-05-03 22:41:17 +0800283#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600284err_disable_clk_ptp_ref:
285 clk_disable(&eqos->clk_ptp_ref);
286err_disable_clk_rx:
287 clk_disable(&eqos->clk_rx);
288err_disable_clk_master_bus:
289 clk_disable(&eqos->clk_master_bus);
290err_disable_clk_slave_bus:
291 clk_disable(&eqos->clk_slave_bus);
292err:
293 debug("%s: FAILED: %d\n", __func__, ret);
294 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800295#endif
Stephen Warren50709602016-10-21 14:46:47 -0600296}
297
Christophe Roullier6beb7802019-05-17 15:08:44 +0200298static int eqos_start_clks_stm32(struct udevice *dev)
299{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800300#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200301 struct eqos_priv *eqos = dev_get_priv(dev);
302 int ret;
303
304 debug("%s(dev=%p):\n", __func__, dev);
305
306 ret = clk_enable(&eqos->clk_master_bus);
307 if (ret < 0) {
308 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
309 goto err;
310 }
311
312 ret = clk_enable(&eqos->clk_rx);
313 if (ret < 0) {
314 pr_err("clk_enable(clk_rx) failed: %d", ret);
315 goto err_disable_clk_master_bus;
316 }
317
318 ret = clk_enable(&eqos->clk_tx);
319 if (ret < 0) {
320 pr_err("clk_enable(clk_tx) failed: %d", ret);
321 goto err_disable_clk_rx;
322 }
323
Daniil Stas81597922021-05-23 22:24:48 +0000324 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200325 ret = clk_enable(&eqos->clk_ck);
326 if (ret < 0) {
327 pr_err("clk_enable(clk_ck) failed: %d", ret);
328 goto err_disable_clk_tx;
329 }
Daniil Stas81597922021-05-23 22:24:48 +0000330 eqos->clk_ck_enabled = true;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200331 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800332#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200333
334 debug("%s: OK\n", __func__);
335 return 0;
336
Fugang Duan37aae5f2020-05-03 22:41:17 +0800337#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200338err_disable_clk_tx:
339 clk_disable(&eqos->clk_tx);
340err_disable_clk_rx:
341 clk_disable(&eqos->clk_rx);
342err_disable_clk_master_bus:
343 clk_disable(&eqos->clk_master_bus);
344err:
345 debug("%s: FAILED: %d\n", __func__, ret);
346 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800347#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200348}
349
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200350static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600351{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800352#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600353 struct eqos_priv *eqos = dev_get_priv(dev);
354
355 debug("%s(dev=%p):\n", __func__, dev);
356
357 clk_disable(&eqos->clk_tx);
358 clk_disable(&eqos->clk_ptp_ref);
359 clk_disable(&eqos->clk_rx);
360 clk_disable(&eqos->clk_master_bus);
361 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800362#endif
Stephen Warren50709602016-10-21 14:46:47 -0600363
364 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200365 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600366}
367
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200368static int eqos_stop_clks_stm32(struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200369{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800370#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200371 struct eqos_priv *eqos = dev_get_priv(dev);
372
373 debug("%s(dev=%p):\n", __func__, dev);
374
375 clk_disable(&eqos->clk_tx);
376 clk_disable(&eqos->clk_rx);
377 clk_disable(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800378#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200379
380 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200381 return 0;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800382}
383
Stephen Warren50709602016-10-21 14:46:47 -0600384static int eqos_start_resets_tegra186(struct udevice *dev)
385{
386 struct eqos_priv *eqos = dev_get_priv(dev);
387 int ret;
388
389 debug("%s(dev=%p):\n", __func__, dev);
390
391 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
392 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900393 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600394 return ret;
395 }
396
397 udelay(2);
398
399 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
400 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900401 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600402 return ret;
403 }
404
405 ret = reset_assert(&eqos->reset_ctl);
406 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900407 pr_err("reset_assert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600408 return ret;
409 }
410
411 udelay(2);
412
413 ret = reset_deassert(&eqos->reset_ctl);
414 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900415 pr_err("reset_deassert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600416 return ret;
417 }
418
419 debug("%s: OK\n", __func__);
420 return 0;
421}
422
423static int eqos_stop_resets_tegra186(struct udevice *dev)
424{
425 struct eqos_priv *eqos = dev_get_priv(dev);
426
427 reset_assert(&eqos->reset_ctl);
428 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
429
Christophe Roullier6beb7802019-05-17 15:08:44 +0200430 return 0;
431}
432
Stephen Warren50709602016-10-21 14:46:47 -0600433static int eqos_calibrate_pads_tegra186(struct udevice *dev)
434{
435 struct eqos_priv *eqos = dev_get_priv(dev);
436 int ret;
437
438 debug("%s(dev=%p):\n", __func__, dev);
439
440 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
441 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
442
443 udelay(1);
444
445 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
446 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
447
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100448 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
449 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600450 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900451 pr_err("calibrate didn't start");
Stephen Warren50709602016-10-21 14:46:47 -0600452 goto failed;
453 }
454
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100455 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
456 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600457 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900458 pr_err("calibrate didn't finish");
Stephen Warren50709602016-10-21 14:46:47 -0600459 goto failed;
460 }
461
462 ret = 0;
463
464failed:
465 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
466 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
467
468 debug("%s: returns %d\n", __func__, ret);
469
470 return ret;
471}
472
473static int eqos_disable_calibration_tegra186(struct udevice *dev)
474{
475 struct eqos_priv *eqos = dev_get_priv(dev);
476
477 debug("%s(dev=%p):\n", __func__, dev);
478
479 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
480 EQOS_AUTO_CAL_CONFIG_ENABLE);
481
482 return 0;
483}
484
485static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
486{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800487#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600488 struct eqos_priv *eqos = dev_get_priv(dev);
489
490 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800491#else
492 return 0;
493#endif
Stephen Warren50709602016-10-21 14:46:47 -0600494}
495
Christophe Roullier6beb7802019-05-17 15:08:44 +0200496static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
497{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800498#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200499 struct eqos_priv *eqos = dev_get_priv(dev);
500
501 return clk_get_rate(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800502#else
503 return 0;
504#endif
505}
506
Stephen Warren50709602016-10-21 14:46:47 -0600507static int eqos_set_full_duplex(struct udevice *dev)
508{
509 struct eqos_priv *eqos = dev_get_priv(dev);
510
511 debug("%s(dev=%p):\n", __func__, dev);
512
513 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
514
515 return 0;
516}
517
518static int eqos_set_half_duplex(struct udevice *dev)
519{
520 struct eqos_priv *eqos = dev_get_priv(dev);
521
522 debug("%s(dev=%p):\n", __func__, dev);
523
524 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
525
526 /* WAR: Flush TX queue when switching to half-duplex */
527 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
528 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
529
530 return 0;
531}
532
533static int eqos_set_gmii_speed(struct udevice *dev)
534{
535 struct eqos_priv *eqos = dev_get_priv(dev);
536
537 debug("%s(dev=%p):\n", __func__, dev);
538
539 clrbits_le32(&eqos->mac_regs->configuration,
540 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
541
542 return 0;
543}
544
545static int eqos_set_mii_speed_100(struct udevice *dev)
546{
547 struct eqos_priv *eqos = dev_get_priv(dev);
548
549 debug("%s(dev=%p):\n", __func__, dev);
550
551 setbits_le32(&eqos->mac_regs->configuration,
552 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
553
554 return 0;
555}
556
557static int eqos_set_mii_speed_10(struct udevice *dev)
558{
559 struct eqos_priv *eqos = dev_get_priv(dev);
560
561 debug("%s(dev=%p):\n", __func__, dev);
562
563 clrsetbits_le32(&eqos->mac_regs->configuration,
564 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
565
566 return 0;
567}
568
569static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
570{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800571#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600572 struct eqos_priv *eqos = dev_get_priv(dev);
573 ulong rate;
574 int ret;
575
576 debug("%s(dev=%p):\n", __func__, dev);
577
578 switch (eqos->phy->speed) {
579 case SPEED_1000:
580 rate = 125 * 1000 * 1000;
581 break;
582 case SPEED_100:
583 rate = 25 * 1000 * 1000;
584 break;
585 case SPEED_10:
586 rate = 2.5 * 1000 * 1000;
587 break;
588 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900589 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600590 return -EINVAL;
591 }
592
593 ret = clk_set_rate(&eqos->clk_tx, rate);
594 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900595 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600596 return ret;
597 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800598#endif
Stephen Warren50709602016-10-21 14:46:47 -0600599
600 return 0;
601}
602
603static int eqos_adjust_link(struct udevice *dev)
604{
605 struct eqos_priv *eqos = dev_get_priv(dev);
606 int ret;
607 bool en_calibration;
608
609 debug("%s(dev=%p):\n", __func__, dev);
610
611 if (eqos->phy->duplex)
612 ret = eqos_set_full_duplex(dev);
613 else
614 ret = eqos_set_half_duplex(dev);
615 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900616 pr_err("eqos_set_*_duplex() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600617 return ret;
618 }
619
620 switch (eqos->phy->speed) {
621 case SPEED_1000:
622 en_calibration = true;
623 ret = eqos_set_gmii_speed(dev);
624 break;
625 case SPEED_100:
626 en_calibration = true;
627 ret = eqos_set_mii_speed_100(dev);
628 break;
629 case SPEED_10:
630 en_calibration = false;
631 ret = eqos_set_mii_speed_10(dev);
632 break;
633 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900634 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600635 return -EINVAL;
636 }
637 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900638 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600639 return ret;
640 }
641
642 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200643 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600644 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200645 pr_err("eqos_calibrate_pads() failed: %d",
646 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600647 return ret;
648 }
649 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200650 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600651 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200652 pr_err("eqos_disable_calibration() failed: %d",
653 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600654 return ret;
655 }
656 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200657 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600658 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200659 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600660 return ret;
661 }
662
663 return 0;
664}
665
666static int eqos_write_hwaddr(struct udevice *dev)
667{
Simon Glassfa20e932020-12-03 16:55:20 -0700668 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600669 struct eqos_priv *eqos = dev_get_priv(dev);
670 uint32_t val;
671
672 /*
673 * This function may be called before start() or after stop(). At that
674 * time, on at least some configurations of the EQoS HW, all clocks to
675 * the EQoS HW block will be stopped, and a reset signal applied. If
676 * any register access is attempted in this state, bus timeouts or CPU
677 * hangs may occur. This check prevents that.
678 *
679 * A simple solution to this problem would be to not implement
680 * write_hwaddr(), since start() always writes the MAC address into HW
681 * anyway. However, it is desirable to implement write_hwaddr() to
682 * support the case of SW that runs subsequent to U-Boot which expects
683 * the MAC address to already be programmed into the EQoS registers,
684 * which must happen irrespective of whether the U-Boot user (or
685 * scripts) actually made use of the EQoS device, and hence
686 * irrespective of whether start() was ever called.
687 *
688 * Note that this requirement by subsequent SW is not valid for
689 * Tegra186, and is likely not valid for any non-PCI instantiation of
690 * the EQoS HW block. This function is implemented solely as
691 * future-proofing with the expectation the driver will eventually be
692 * ported to some system where the expectation above is true.
693 */
694 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
695 return 0;
696
697 /* Update the MAC address */
698 val = (plat->enetaddr[5] << 8) |
699 (plat->enetaddr[4]);
700 writel(val, &eqos->mac_regs->address0_high);
701 val = (plat->enetaddr[3] << 24) |
702 (plat->enetaddr[2] << 16) |
703 (plat->enetaddr[1] << 8) |
704 (plat->enetaddr[0]);
705 writel(val, &eqos->mac_regs->address0_low);
706
707 return 0;
708}
709
Ye Li3fb1a0e2020-05-03 22:41:20 +0800710static int eqos_read_rom_hwaddr(struct udevice *dev)
711{
Simon Glassfa20e932020-12-03 16:55:20 -0700712 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800713 struct eqos_priv *eqos = dev_get_priv(dev);
714 int ret;
715
716 ret = eqos->config->ops->eqos_get_enetaddr(dev);
717 if (ret < 0)
718 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800719
Ye Li3fb1a0e2020-05-03 22:41:20 +0800720 return !is_valid_ethaddr(pdata->enetaddr);
721}
722
Ye Li2f2aa482022-07-26 16:41:16 +0800723static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
724{
725 struct ofnode_phandle_args phandle_args;
726 int reg;
727
728 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
729 &phandle_args)) {
730 debug("Failed to find phy-handle");
731 return -ENODEV;
732 }
733
734 priv->phy_of_node = phandle_args.node;
735
736 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
737
738 return reg;
739}
740
Stephen Warren50709602016-10-21 14:46:47 -0600741static int eqos_start(struct udevice *dev)
742{
743 struct eqos_priv *eqos = dev_get_priv(dev);
744 int ret, i;
745 ulong rate;
746 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
747 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100748 ulong desc_pad;
Ley Foon Tan963db382022-12-09 14:33:14 +0800749 ulong addr64;
Stephen Warren50709602016-10-21 14:46:47 -0600750
751 debug("%s(dev=%p):\n", __func__, dev);
752
753 eqos->tx_desc_idx = 0;
754 eqos->rx_desc_idx = 0;
755
Christophe Roullier6beb7802019-05-17 15:08:44 +0200756 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600757 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200758 pr_err("eqos_start_resets() failed: %d", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100759 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600760 }
761
762 udelay(10);
763
764 eqos->reg_access_ok = true;
765
Marek Vasute66825a2023-03-06 15:53:46 +0100766 /*
767 * Assert the SWR first, the actually reset the MAC and to latch in
768 * e.g. i.MX8M Plus GPR[1] content, which selects interface mode.
769 */
770 setbits_le32(&eqos->dma_regs->mode, EQOS_DMA_MODE_SWR);
771
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100772 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200773 EQOS_DMA_MODE_SWR, false,
774 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600775 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900776 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -0600777 goto err_stop_resets;
778 }
779
Christophe Roullier6beb7802019-05-17 15:08:44 +0200780 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600781 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200782 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600783 goto err_stop_resets;
784 }
Sumit Gargab973c92023-02-01 19:28:53 +0530785
786 if (eqos->config->ops->eqos_get_tick_clk_rate) {
787 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600788
Sumit Gargab973c92023-02-01 19:28:53 +0530789 val = (rate / 1000000) - 1;
790 writel(val, &eqos->mac_regs->us_tic_counter);
791 }
Stephen Warren50709602016-10-21 14:46:47 -0600792
Christophe Roullier6beb7802019-05-17 15:08:44 +0200793 /*
794 * if PHY was already connected and configured,
795 * don't need to reconnect/reconfigure again
796 */
Stephen Warren50709602016-10-21 14:46:47 -0600797 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800798 int addr = -1;
Elmar Psilogdd65ba22023-02-20 16:03:15 +0100799 ofnode fixed_node;
800
801 if (IS_ENABLED(CONFIG_PHY_FIXED)) {
802 fixed_node = ofnode_find_subnode(dev_ofnode(dev),
803 "fixed-link");
804 if (ofnode_valid(fixed_node))
805 eqos->phy = fixed_phy_create(dev_ofnode(dev));
806 }
807
808 if (!eqos->phy) {
809 addr = eqos_get_phy_addr(eqos, dev);
810 eqos->phy = phy_connect(eqos->mii, addr, dev,
811 eqos->config->interface(dev));
812 }
813
Christophe Roullier6beb7802019-05-17 15:08:44 +0200814 if (!eqos->phy) {
815 pr_err("phy_connect() failed");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000816 ret = -ENODEV;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200817 goto err_stop_resets;
818 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100819
820 if (eqos->max_speed) {
821 ret = phy_set_supported(eqos->phy, eqos->max_speed);
822 if (ret) {
823 pr_err("phy_set_supported() failed: %d", ret);
824 goto err_shutdown_phy;
825 }
826 }
827
Ye Li2f2aa482022-07-26 16:41:16 +0800828 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200829 ret = phy_config(eqos->phy);
830 if (ret < 0) {
831 pr_err("phy_config() failed: %d", ret);
832 goto err_shutdown_phy;
833 }
Stephen Warren50709602016-10-21 14:46:47 -0600834 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200835
Stephen Warren50709602016-10-21 14:46:47 -0600836 ret = phy_startup(eqos->phy);
837 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900838 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600839 goto err_shutdown_phy;
840 }
841
842 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900843 pr_err("No link");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000844 ret = -EAGAIN;
Stephen Warren50709602016-10-21 14:46:47 -0600845 goto err_shutdown_phy;
846 }
847
848 ret = eqos_adjust_link(dev);
849 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900850 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600851 goto err_shutdown_phy;
852 }
853
854 /* Configure MTL */
855
856 /* Enable Store and Forward mode for TX */
857 /* Program Tx operating mode */
858 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
859 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
860 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
861 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
862
863 /* Transmit Queue weight */
864 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
865
866 /* Enable Store and Forward mode for RX, since no jumbo frame */
867 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000868 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600869
870 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
871 val = readl(&eqos->mac_regs->hw_feature1);
872 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
873 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
874 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
875 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
876
Sumit Garg4d5c9652023-02-01 19:28:54 +0530877 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
878 tx_fifo_sz = 128 << tx_fifo_sz;
879 rx_fifo_sz = 128 << rx_fifo_sz;
880
881 /* Allow platform to override TX/RX fifo size */
882 if (eqos->tx_fifo_sz)
883 tx_fifo_sz = eqos->tx_fifo_sz;
884 if (eqos->rx_fifo_sz)
885 rx_fifo_sz = eqos->rx_fifo_sz;
886
887 /* r/tqs is encoded as (n / 256) - 1 */
888 tqs = tx_fifo_sz / 256 - 1;
889 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600890
891 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
892 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
893 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
894 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
895 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
896 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
897 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
898 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
899
900 /* Flow control used only if each channel gets 4KB or more FIFO */
901 if (rqs >= ((4096 / 256) - 1)) {
902 u32 rfd, rfa;
903
904 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
905 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
906
907 /*
908 * Set Threshold for Activating Flow Contol space for min 2
909 * frames ie, (1500 * 1) = 1500 bytes.
910 *
911 * Set Threshold for Deactivating Flow Contol for space of
912 * min 1 frame (frame size 1500bytes) in receive fifo
913 */
914 if (rqs == ((4096 / 256) - 1)) {
915 /*
916 * This violates the above formula because of FIFO size
917 * limit therefore overflow may occur inspite of this.
918 */
919 rfd = 0x3; /* Full-3K */
920 rfa = 0x1; /* Full-1.5K */
921 } else if (rqs == ((8192 / 256) - 1)) {
922 rfd = 0x6; /* Full-4K */
923 rfa = 0xa; /* Full-6K */
924 } else if (rqs == ((16384 / 256) - 1)) {
925 rfd = 0x6; /* Full-4K */
926 rfa = 0x12; /* Full-10K */
927 } else {
928 rfd = 0x6; /* Full-4K */
929 rfa = 0x1E; /* Full-16K */
930 }
931
932 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
933 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
934 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
935 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
936 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
937 (rfd <<
938 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
939 (rfa <<
940 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
941 }
942
943 /* Configure MAC */
944
945 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
946 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
947 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200948 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600949 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
950
Fugang Duan37aae5f2020-05-03 22:41:17 +0800951 /* Multicast and Broadcast Queue Enable */
952 setbits_le32(&eqos->mac_regs->unused_0a4,
953 0x00100000);
954 /* enable promise mode */
955 setbits_le32(&eqos->mac_regs->unused_004[1],
956 0x1);
957
Stephen Warren50709602016-10-21 14:46:47 -0600958 /* Set TX flow control parameters */
959 /* Set Pause Time */
960 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
961 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
962 /* Assign priority for TX flow control */
963 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
964 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
965 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
966 /* Assign priority for RX flow control */
967 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
968 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
969 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
970 /* Enable flow control */
971 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
972 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
973 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
974 EQOS_MAC_RX_FLOW_CTRL_RFE);
975
976 clrsetbits_le32(&eqos->mac_regs->configuration,
977 EQOS_MAC_CONFIGURATION_GPSLCE |
978 EQOS_MAC_CONFIGURATION_WD |
979 EQOS_MAC_CONFIGURATION_JD |
980 EQOS_MAC_CONFIGURATION_JE,
981 EQOS_MAC_CONFIGURATION_CST |
982 EQOS_MAC_CONFIGURATION_ACS);
983
984 eqos_write_hwaddr(dev);
985
986 /* Configure DMA */
987
988 /* Enable OSP mode */
989 setbits_le32(&eqos->dma_regs->ch0_tx_control,
990 EQOS_DMA_CH0_TX_CONTROL_OSP);
991
992 /* RX buffer size. Must be a multiple of bus width */
993 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
994 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
995 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
996 EQOS_MAX_PACKET_SIZE <<
997 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
998
Marek Vasut89077732021-01-07 11:12:16 +0100999 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
1000 eqos->config->axi_bus_width;
1001
Stephen Warren50709602016-10-21 14:46:47 -06001002 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +01001003 EQOS_DMA_CH0_CONTROL_PBLX8 |
1004 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -06001005
1006 /*
1007 * Burst length must be < 1/2 FIFO size.
1008 * FIFO size in tqs is encoded as (n / 256) - 1.
1009 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1010 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1011 */
1012 pbl = tqs + 1;
1013 if (pbl > 32)
1014 pbl = 32;
1015 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1016 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1017 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1018 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1019
1020 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1021 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1022 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1023 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1024
1025 /* DMA performance configuration */
1026 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1027 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1028 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1029 writel(val, &eqos->dma_regs->sysbus_mode);
1030
1031 /* Set up descriptors */
1032
Marek Vasut90cc13a2022-10-09 17:51:45 +02001033 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
1034 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +01001035
1036 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
1037 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
1038 eqos->config->ops->eqos_flush_desc(tx_desc);
1039 }
1040
Stephen Warren50709602016-10-21 14:46:47 -06001041 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +01001042 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Ley Foon Tan963db382022-12-09 14:33:14 +08001043
1044 addr64 = (ulong)(eqos->rx_dma_buf + (i * EQOS_MAX_PACKET_SIZE));
1045 rx_desc->des0 = lower_32_bits(addr64);
1046 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasutd54c98e2020-03-23 02:02:57 +01001047 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001048 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +01001049 eqos->config->ops->eqos_flush_desc(rx_desc);
Ley Foon Tan963db382022-12-09 14:33:14 +08001050 eqos->config->ops->eqos_inval_buffer((void *)addr64, EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -06001051 }
Stephen Warren50709602016-10-21 14:46:47 -06001052
Ley Foon Tan963db382022-12-09 14:33:14 +08001053 addr64 = (ulong)eqos_get_desc(eqos, 0, false);
1054 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_haddress);
1055 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001056 writel(EQOS_DESCRIPTORS_TX - 1,
1057 &eqos->dma_regs->ch0_txdesc_ring_length);
1058
Ley Foon Tan963db382022-12-09 14:33:14 +08001059 addr64 = (ulong)eqos_get_desc(eqos, 0, true);
1060 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_haddress);
1061 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001062 writel(EQOS_DESCRIPTORS_RX - 1,
1063 &eqos->dma_regs->ch0_rxdesc_ring_length);
1064
1065 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001066 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1067 EQOS_DMA_CH0_TX_CONTROL_ST);
1068 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1069 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001070 setbits_le32(&eqos->mac_regs->configuration,
1071 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001072
1073 /* TX tail pointer not written until we need to TX a packet */
1074 /*
1075 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1076 * first descriptor, implying all descriptors were available. However,
1077 * that's not distinguishable from none of the descriptors being
1078 * available.
1079 */
Marek Vasut89077732021-01-07 11:12:16 +01001080 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001081 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1082
1083 eqos->started = true;
1084
1085 debug("%s: OK\n", __func__);
1086 return 0;
1087
1088err_shutdown_phy:
1089 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001090err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001091 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001092err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001093 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001094 return ret;
1095}
1096
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001097static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001098{
1099 struct eqos_priv *eqos = dev_get_priv(dev);
1100 int i;
1101
1102 debug("%s(dev=%p):\n", __func__, dev);
1103
1104 if (!eqos->started)
1105 return;
1106 eqos->started = false;
1107 eqos->reg_access_ok = false;
1108
1109 /* Disable TX DMA */
1110 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1111 EQOS_DMA_CH0_TX_CONTROL_ST);
1112
1113 /* Wait for TX all packets to drain out of MTL */
1114 for (i = 0; i < 1000000; i++) {
1115 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1116 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1117 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1118 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1119 if ((trcsts != 1) && (!txqsts))
1120 break;
1121 }
1122
1123 /* Turn off MAC TX and RX */
1124 clrbits_le32(&eqos->mac_regs->configuration,
1125 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1126
1127 /* Wait for all RX packets to drain out of MTL */
1128 for (i = 0; i < 1000000; i++) {
1129 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1130 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1131 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1132 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1133 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1134 if ((!prxq) && (!rxqsts))
1135 break;
1136 }
1137
1138 /* Turn off RX DMA */
1139 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1140 EQOS_DMA_CH0_RX_CONTROL_SR);
1141
1142 if (eqos->phy) {
1143 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001144 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001145 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001146
1147 debug("%s: OK\n", __func__);
1148}
1149
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001150static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001151{
1152 struct eqos_priv *eqos = dev_get_priv(dev);
1153 struct eqos_desc *tx_desc;
1154 int i;
1155
1156 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1157 length);
1158
1159 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001160 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001161
Marek Vasut89077732021-01-07 11:12:16 +01001162 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001163 eqos->tx_desc_idx++;
1164 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1165
Ley Foon Tan963db382022-12-09 14:33:14 +08001166 tx_desc->des0 = lower_32_bits((ulong)eqos->tx_dma_buf);
1167 tx_desc->des1 = upper_32_bits((ulong)eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001168 tx_desc->des2 = length;
1169 /*
1170 * Make sure that if HW sees the _OWN write below, it will see all the
1171 * writes to the rest of the descriptor too.
1172 */
1173 mb();
1174 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001175 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001176
Marek Vasut89077732021-01-07 11:12:16 +01001177 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001178 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001179
1180 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001181 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001182 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1183 return 0;
1184 udelay(1);
1185 }
1186
1187 debug("%s: TX timeout\n", __func__);
1188
1189 return -ETIMEDOUT;
1190}
1191
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001192static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001193{
1194 struct eqos_priv *eqos = dev_get_priv(dev);
1195 struct eqos_desc *rx_desc;
1196 int length;
1197
Marek Vasut89077732021-01-07 11:12:16 +01001198 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001199 eqos->config->ops->eqos_inval_desc(rx_desc);
Jonas Karlmane2c45462023-10-01 19:17:18 +00001200 if (rx_desc->des3 & EQOS_DESC3_OWN)
Stephen Warren50709602016-10-21 14:46:47 -06001201 return -EAGAIN;
Jonas Karlmane2c45462023-10-01 19:17:18 +00001202
1203 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
Stephen Warren50709602016-10-21 14:46:47 -06001204
1205 *packetp = eqos->rx_dma_buf +
1206 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1207 length = rx_desc->des3 & 0x7fff;
1208 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1209
Christophe Roullier6beb7802019-05-17 15:08:44 +02001210 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001211
1212 return length;
1213}
1214
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001215static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001216{
1217 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001218 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001219 uchar *packet_expected;
1220 struct eqos_desc *rx_desc;
1221
1222 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1223
1224 packet_expected = eqos->rx_dma_buf +
1225 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1226 if (packet != packet_expected) {
1227 debug("%s: Unexpected packet (expected %p)\n", __func__,
1228 packet_expected);
1229 return -EINVAL;
1230 }
1231
Fugang Duan37aae5f2020-05-03 22:41:17 +08001232 eqos->config->ops->eqos_inval_buffer(packet, length);
1233
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001234 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1235 for (idx = eqos->rx_desc_idx - idx_mask;
1236 idx <= eqos->rx_desc_idx;
1237 idx++) {
Ley Foon Tan963db382022-12-09 14:33:14 +08001238 ulong addr64;
1239
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001240 rx_desc = eqos_get_desc(eqos, idx, true);
1241 rx_desc->des0 = 0;
Ley Foon Tan963db382022-12-09 14:33:14 +08001242 rx_desc->des1 = 0;
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001243 mb();
1244 eqos->config->ops->eqos_flush_desc(rx_desc);
1245 eqos->config->ops->eqos_inval_buffer(packet, length);
Ley Foon Tan963db382022-12-09 14:33:14 +08001246 addr64 = (ulong)(eqos->rx_dma_buf + (idx * EQOS_MAX_PACKET_SIZE));
1247 rx_desc->des0 = lower_32_bits(addr64);
1248 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001249 rx_desc->des2 = 0;
1250 /*
1251 * Make sure that if HW sees the _OWN write below,
1252 * it will see all the writes to the rest of the
1253 * descriptor too.
1254 */
1255 mb();
1256 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1257 eqos->config->ops->eqos_flush_desc(rx_desc);
1258 }
1259 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1260 }
Stephen Warren50709602016-10-21 14:46:47 -06001261
1262 eqos->rx_desc_idx++;
1263 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1264
1265 return 0;
1266}
1267
1268static int eqos_probe_resources_core(struct udevice *dev)
1269{
1270 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001271 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001272 int ret;
1273
1274 debug("%s(dev=%p):\n", __func__, dev);
1275
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001276 /* Maximum distance between neighboring descriptors, in Bytes. */
1277 desc_step = sizeof(struct eqos_desc) +
1278 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1279 if (desc_step < ARCH_DMA_MINALIGN) {
1280 /*
1281 * The EQoS hardware implementation cannot place one descriptor
1282 * per cacheline, it is necessary to place multiple descriptors
1283 * per cacheline in memory and do cache management carefully.
1284 */
1285 eqos->desc_size = BIT(fls(desc_step) - 1);
1286 } else {
1287 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1288 (unsigned int)ARCH_DMA_MINALIGN);
1289 }
1290 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001291
1292 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1293 if (!eqos->tx_descs) {
1294 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001295 ret = -ENOMEM;
1296 goto err;
1297 }
Stephen Warren50709602016-10-21 14:46:47 -06001298
Marek Vasut90cc13a2022-10-09 17:51:45 +02001299 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1300 if (!eqos->rx_descs) {
1301 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1302 ret = -ENOMEM;
1303 goto err_free_tx_descs;
1304 }
1305
Stephen Warren50709602016-10-21 14:46:47 -06001306 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1307 if (!eqos->tx_dma_buf) {
1308 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1309 ret = -ENOMEM;
1310 goto err_free_descs;
1311 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001312 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001313
1314 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1315 if (!eqos->rx_dma_buf) {
1316 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1317 ret = -ENOMEM;
1318 goto err_free_tx_dma_buf;
1319 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001320 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001321
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001322 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1323 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1324
Stephen Warren50709602016-10-21 14:46:47 -06001325 debug("%s: OK\n", __func__);
1326 return 0;
1327
Stephen Warren50709602016-10-21 14:46:47 -06001328err_free_tx_dma_buf:
1329 free(eqos->tx_dma_buf);
1330err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001331 eqos_free_descs(eqos->rx_descs);
1332err_free_tx_descs:
1333 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001334err:
1335
1336 debug("%s: returns %d\n", __func__, ret);
1337 return ret;
1338}
1339
1340static int eqos_remove_resources_core(struct udevice *dev)
1341{
1342 struct eqos_priv *eqos = dev_get_priv(dev);
1343
1344 debug("%s(dev=%p):\n", __func__, dev);
1345
Stephen Warren50709602016-10-21 14:46:47 -06001346 free(eqos->rx_dma_buf);
1347 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001348 eqos_free_descs(eqos->rx_descs);
1349 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001350
1351 debug("%s: OK\n", __func__);
1352 return 0;
1353}
1354
1355static int eqos_probe_resources_tegra186(struct udevice *dev)
1356{
1357 struct eqos_priv *eqos = dev_get_priv(dev);
1358 int ret;
1359
1360 debug("%s(dev=%p):\n", __func__, dev);
1361
1362 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1363 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001364 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001365 return ret;
1366 }
1367
1368 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1369 &eqos->phy_reset_gpio,
1370 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1371 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001372 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001373 goto err_free_reset_eqos;
1374 }
1375
1376 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1377 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001378 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001379 goto err_free_gpio_phy_reset;
1380 }
1381
1382 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1383 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001384 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001385 goto err_free_clk_slave_bus;
1386 }
1387
1388 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1389 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001390 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001391 goto err_free_clk_master_bus;
1392 }
1393
1394 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1395 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001396 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001397 goto err_free_clk_rx;
Stephen Warren50709602016-10-21 14:46:47 -06001398 }
1399
1400 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1401 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001402 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001403 goto err_free_clk_ptp_ref;
1404 }
1405
1406 debug("%s: OK\n", __func__);
1407 return 0;
1408
1409err_free_clk_ptp_ref:
1410 clk_free(&eqos->clk_ptp_ref);
1411err_free_clk_rx:
1412 clk_free(&eqos->clk_rx);
1413err_free_clk_master_bus:
1414 clk_free(&eqos->clk_master_bus);
1415err_free_clk_slave_bus:
1416 clk_free(&eqos->clk_slave_bus);
1417err_free_gpio_phy_reset:
1418 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1419err_free_reset_eqos:
1420 reset_free(&eqos->reset_ctl);
1421
1422 debug("%s: returns %d\n", __func__, ret);
1423 return ret;
1424}
1425
Christophe Roullier6beb7802019-05-17 15:08:44 +02001426static int eqos_probe_resources_stm32(struct udevice *dev)
1427{
1428 struct eqos_priv *eqos = dev_get_priv(dev);
1429 int ret;
1430 phy_interface_t interface;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001431
1432 debug("%s(dev=%p):\n", __func__, dev);
1433
1434 interface = eqos->config->interface(dev);
1435
Marek BehĂșn48631e42022-04-07 00:33:03 +02001436 if (interface == PHY_INTERFACE_MODE_NA) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001437 pr_err("Invalid PHY interface\n");
1438 return -EINVAL;
1439 }
1440
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001441 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001442 if (ret)
1443 return -EINVAL;
1444
1445 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1446 if (ret) {
1447 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1448 goto err_probe;
1449 }
1450
1451 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1452 if (ret) {
1453 pr_err("clk_get_by_name(rx) failed: %d", ret);
1454 goto err_free_clk_master_bus;
1455 }
1456
1457 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1458 if (ret) {
1459 pr_err("clk_get_by_name(tx) failed: %d", ret);
1460 goto err_free_clk_rx;
1461 }
1462
1463 /* Get ETH_CLK clocks (optional) */
1464 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1465 if (ret)
1466 pr_warn("No phy clock provided %d", ret);
1467
1468 debug("%s: OK\n", __func__);
1469 return 0;
1470
1471err_free_clk_rx:
1472 clk_free(&eqos->clk_rx);
1473err_free_clk_master_bus:
1474 clk_free(&eqos->clk_master_bus);
1475err_probe:
1476
1477 debug("%s: returns %d\n", __func__, ret);
1478 return ret;
1479}
1480
Marek BehĂșnbc194772022-04-07 00:33:01 +02001481static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001482{
1483 return PHY_INTERFACE_MODE_MII;
1484}
1485
Stephen Warren50709602016-10-21 14:46:47 -06001486static int eqos_remove_resources_tegra186(struct udevice *dev)
1487{
1488 struct eqos_priv *eqos = dev_get_priv(dev);
1489
1490 debug("%s(dev=%p):\n", __func__, dev);
1491
Fugang Duan37aae5f2020-05-03 22:41:17 +08001492#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -06001493 clk_free(&eqos->clk_tx);
1494 clk_free(&eqos->clk_ptp_ref);
1495 clk_free(&eqos->clk_rx);
1496 clk_free(&eqos->clk_slave_bus);
1497 clk_free(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001498#endif
Stephen Warren50709602016-10-21 14:46:47 -06001499 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1500 reset_free(&eqos->reset_ctl);
1501
1502 debug("%s: OK\n", __func__);
1503 return 0;
1504}
1505
Christophe Roullier6beb7802019-05-17 15:08:44 +02001506static int eqos_remove_resources_stm32(struct udevice *dev)
1507{
Marek Vasut006ab6b2023-03-06 15:53:44 +01001508 struct eqos_priv * __maybe_unused eqos = dev_get_priv(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001509
1510 debug("%s(dev=%p):\n", __func__, dev);
1511
Peng Fan809993f2022-07-26 16:41:13 +08001512#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +02001513 clk_free(&eqos->clk_tx);
1514 clk_free(&eqos->clk_rx);
1515 clk_free(&eqos->clk_master_bus);
1516 if (clk_valid(&eqos->clk_ck))
1517 clk_free(&eqos->clk_ck);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001518#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001519
1520 debug("%s: OK\n", __func__);
1521 return 0;
1522}
1523
Stephen Warren50709602016-10-21 14:46:47 -06001524static int eqos_probe(struct udevice *dev)
1525{
1526 struct eqos_priv *eqos = dev_get_priv(dev);
1527 int ret;
1528
1529 debug("%s(dev=%p):\n", __func__, dev);
1530
1531 eqos->dev = dev;
1532 eqos->config = (void *)dev_get_driver_data(dev);
1533
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001534 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001535 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001536 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001537 return -ENODEV;
1538 }
1539 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1540 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1541 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1542 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1543
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001544 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1545
Stephen Warren50709602016-10-21 14:46:47 -06001546 ret = eqos_probe_resources_core(dev);
1547 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001548 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001549 return ret;
1550 }
1551
Christophe Roullier6beb7802019-05-17 15:08:44 +02001552 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001553 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001554 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001555 goto err_remove_resources_core;
1556 }
1557
Marek Vasut30b28c42021-11-13 03:23:52 +01001558 ret = eqos->config->ops->eqos_start_clks(dev);
1559 if (ret < 0) {
1560 pr_err("eqos_start_clks() failed: %d", ret);
1561 goto err_remove_resources_tegra;
1562 }
1563
Ye Liad122b72020-05-03 22:41:15 +08001564#ifdef CONFIG_DM_ETH_PHY
1565 eqos->mii = eth_phy_get_mdio_bus(dev);
1566#endif
Stephen Warren50709602016-10-21 14:46:47 -06001567 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001568 eqos->mii = mdio_alloc();
1569 if (!eqos->mii) {
1570 pr_err("mdio_alloc() failed");
1571 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001572 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001573 }
1574 eqos->mii->read = eqos_mdio_read;
1575 eqos->mii->write = eqos_mdio_write;
1576 eqos->mii->priv = eqos;
1577 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001578
Ye Liad122b72020-05-03 22:41:15 +08001579 ret = mdio_register(eqos->mii);
1580 if (ret < 0) {
1581 pr_err("mdio_register() failed: %d", ret);
1582 goto err_free_mdio;
1583 }
Stephen Warren50709602016-10-21 14:46:47 -06001584 }
1585
Ye Liad122b72020-05-03 22:41:15 +08001586#ifdef CONFIG_DM_ETH_PHY
1587 eth_phy_set_mdio_bus(dev, eqos->mii);
1588#endif
1589
Stephen Warren50709602016-10-21 14:46:47 -06001590 debug("%s: OK\n", __func__);
1591 return 0;
1592
1593err_free_mdio:
1594 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001595err_stop_clks:
1596 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001597err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001598 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001599err_remove_resources_core:
1600 eqos_remove_resources_core(dev);
1601
1602 debug("%s: returns %d\n", __func__, ret);
1603 return ret;
1604}
1605
1606static int eqos_remove(struct udevice *dev)
1607{
1608 struct eqos_priv *eqos = dev_get_priv(dev);
1609
1610 debug("%s(dev=%p):\n", __func__, dev);
1611
1612 mdio_unregister(eqos->mii);
1613 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001614 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001615 eqos->config->ops->eqos_remove_resources(dev);
1616
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001617 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001618
1619 debug("%s: OK\n", __func__);
1620 return 0;
1621}
1622
Peng Fanc0a59952022-07-26 16:41:14 +08001623int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001624{
1625 return 0;
1626}
1627
Stephen Warren50709602016-10-21 14:46:47 -06001628static const struct eth_ops eqos_ops = {
1629 .start = eqos_start,
1630 .stop = eqos_stop,
1631 .send = eqos_send,
1632 .recv = eqos_recv,
1633 .free_pkt = eqos_free_pkt,
1634 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001635 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001636};
1637
Christophe Roullier6beb7802019-05-17 15:08:44 +02001638static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001639 .eqos_inval_desc = eqos_inval_desc_generic,
1640 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001641 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1642 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1643 .eqos_probe_resources = eqos_probe_resources_tegra186,
1644 .eqos_remove_resources = eqos_remove_resources_tegra186,
1645 .eqos_stop_resets = eqos_stop_resets_tegra186,
1646 .eqos_start_resets = eqos_start_resets_tegra186,
1647 .eqos_stop_clks = eqos_stop_clks_tegra186,
1648 .eqos_start_clks = eqos_start_clks_tegra186,
1649 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1650 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1651 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001652 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001653 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1654};
1655
Patrick Delaunay68083902020-06-08 11:27:19 +02001656static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001657 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001658 .mdio_wait = 10,
1659 .swr_wait = 10,
1660 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1661 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001662 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001663 .interface = eqos_get_interface_tegra186,
1664 .ops = &eqos_tegra186_ops
1665};
1666
1667static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08001668 .eqos_inval_desc = eqos_inval_desc_generic,
1669 .eqos_flush_desc = eqos_flush_desc_generic,
1670 .eqos_inval_buffer = eqos_inval_buffer_generic,
1671 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001672 .eqos_probe_resources = eqos_probe_resources_stm32,
1673 .eqos_remove_resources = eqos_remove_resources_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001674 .eqos_stop_resets = eqos_null_ops,
1675 .eqos_start_resets = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001676 .eqos_stop_clks = eqos_stop_clks_stm32,
1677 .eqos_start_clks = eqos_start_clks_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001678 .eqos_calibrate_pads = eqos_null_ops,
1679 .eqos_disable_calibration = eqos_null_ops,
1680 .eqos_set_tx_clk_speed = eqos_null_ops,
Patrice Chotardd9824432022-08-02 10:55:26 +02001681 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001682 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1683};
1684
Patrick Delaunay68083902020-06-08 11:27:19 +02001685static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001686 .reg_access_always_ok = false,
1687 .mdio_wait = 10000,
1688 .swr_wait = 50,
1689 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1690 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01001691 .axi_bus_width = EQOS_AXI_WIDTH_64,
Marek BehĂșnbc194772022-04-07 00:33:01 +02001692 .interface = dev_read_phy_mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001693 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001694};
1695
1696static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001697#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001698 {
1699 .compatible = "nvidia,tegra186-eqos",
1700 .data = (ulong)&eqos_tegra186_config
1701 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001702#endif
1703#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001704 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001705 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02001706 .data = (ulong)&eqos_stm32_config
1707 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001708#endif
1709#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001710 {
Marek Vasut7af11382022-02-26 04:36:37 +01001711 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001712 .data = (ulong)&eqos_imx_config
1713 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001714#endif
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001715#if IS_ENABLED(CONFIG_DWC_ETH_QOS_ROCKCHIP)
1716 {
1717 .compatible = "rockchip,rk3568-gmac",
1718 .data = (ulong)&eqos_rockchip_config
1719 },
Jonas Karlman1b615702023-10-01 19:17:20 +00001720 {
1721 .compatible = "rockchip,rk3588-gmac",
1722 .data = (ulong)&eqos_rockchip_config
1723 },
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001724#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301725#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1726 {
1727 .compatible = "qcom,qcs404-ethqos",
1728 .data = (ulong)&eqos_qcom_config
1729 },
1730#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001731#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1732 {
1733 .compatible = "starfive,jh7110-dwmac",
1734 .data = (ulong)&eqos_jh7110_config
1735 },
1736#endif
Stephen Warren50709602016-10-21 14:46:47 -06001737 { }
1738};
1739
1740U_BOOT_DRIVER(eth_eqos) = {
1741 .name = "eth_eqos",
1742 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001743 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001744 .probe = eqos_probe,
1745 .remove = eqos_remove,
1746 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001747 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001748 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001749};