blob: 112deb546dec7f22c2e26fd9ef8bec5a556f199c [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>
Stephen Warren50709602016-10-21 14:46:47 -060055
Peng Fanc0a59952022-07-26 16:41:14 +080056#include "dwc_eth_qos.h"
Stephen Warren50709602016-10-21 14:46:47 -060057
58/*
59 * TX and RX descriptors are 16 bytes. This causes problems with the cache
60 * maintenance on CPUs where the cache-line size exceeds the size of these
61 * descriptors. What will happen is that when the driver receives a packet
62 * it will be immediately requeued for the hardware to reuse. The CPU will
63 * therefore need to flush the cache-line containing the descriptor, which
64 * will cause all other descriptors in the same cache-line to be flushed
65 * along with it. If one of those descriptors had been written to by the
66 * device those changes (and the associated packet) will be lost.
67 *
68 * To work around this, we make use of non-cached memory if available. If
69 * descriptors are mapped uncached there's no need to manually flush them
70 * or invalidate them.
71 *
72 * Note that this only applies to descriptors. The packet data buffers do
73 * not have the same constraints since they are 1536 bytes large, so they
74 * are unlikely to share cache-lines.
75 */
Marek Vasut89077732021-01-07 11:12:16 +010076static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -060077{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020078 return memalign(ARCH_DMA_MINALIGN, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060079}
80
81static void eqos_free_descs(void *descs)
82{
Stephen Warren50709602016-10-21 14:46:47 -060083 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -060084}
85
Marek Vasut89077732021-01-07 11:12:16 +010086static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
87 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -060088{
Marek Vasut90cc13a2022-10-09 17:51:45 +020089 return (rx ? eqos->rx_descs : eqos->tx_descs) +
90 (num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060091}
92
Peng Fanc0a59952022-07-26 16:41:14 +080093void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -060094{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020095 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +010096 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
97 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +020098
99 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600100}
101
Peng Fanc0a59952022-07-26 16:41:14 +0800102void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200103{
Marek Vasut3e8a1be2022-10-09 17:51:46 +0200104 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +0100105 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
106 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200107
108 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200109}
110
Peng Fanc0a59952022-07-26 16:41:14 +0800111void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600112{
113 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
114 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
115
116 invalidate_dcache_range(start, end);
117}
118
Peng Fanc0a59952022-07-26 16:41:14 +0800119void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200120{
121 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
122 unsigned long end = roundup((unsigned long)buf + size,
123 ARCH_DMA_MINALIGN);
124
125 invalidate_dcache_range(start, end);
126}
127
128static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600129{
130 flush_cache((unsigned long)buf, size);
131}
132
Peng Fanc0a59952022-07-26 16:41:14 +0800133void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200134{
135 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
136 unsigned long end = roundup((unsigned long)buf + size,
137 ARCH_DMA_MINALIGN);
138
139 flush_dcache_range(start, end);
140}
141
Stephen Warren50709602016-10-21 14:46:47 -0600142static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
143{
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100144 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
145 EQOS_MAC_MDIO_ADDRESS_GB, false,
146 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600147}
148
149static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
150 int mdio_reg)
151{
152 struct eqos_priv *eqos = bus->priv;
153 u32 val;
154 int ret;
155
156 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
157 mdio_reg);
158
159 ret = eqos_mdio_wait_idle(eqos);
160 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900161 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600162 return ret;
163 }
164
165 val = readl(&eqos->mac_regs->mdio_address);
166 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
167 EQOS_MAC_MDIO_ADDRESS_C45E;
168 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
169 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200170 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600171 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
172 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
173 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
174 EQOS_MAC_MDIO_ADDRESS_GB;
175 writel(val, &eqos->mac_regs->mdio_address);
176
Christophe Roullier6beb7802019-05-17 15:08:44 +0200177 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600178
179 ret = eqos_mdio_wait_idle(eqos);
180 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900181 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600182 return ret;
183 }
184
185 val = readl(&eqos->mac_regs->mdio_data);
186 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
187
188 debug("%s: val=%x\n", __func__, val);
189
190 return val;
191}
192
193static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
194 int mdio_reg, u16 mdio_val)
195{
196 struct eqos_priv *eqos = bus->priv;
197 u32 val;
198 int ret;
199
200 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
201 mdio_addr, mdio_reg, mdio_val);
202
203 ret = eqos_mdio_wait_idle(eqos);
204 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900205 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600206 return ret;
207 }
208
209 writel(mdio_val, &eqos->mac_regs->mdio_data);
210
211 val = readl(&eqos->mac_regs->mdio_address);
212 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
213 EQOS_MAC_MDIO_ADDRESS_C45E;
214 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
215 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200216 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600217 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
218 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
219 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
220 EQOS_MAC_MDIO_ADDRESS_GB;
221 writel(val, &eqos->mac_regs->mdio_address);
222
Christophe Roullier6beb7802019-05-17 15:08:44 +0200223 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600224
225 ret = eqos_mdio_wait_idle(eqos);
226 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900227 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600228 return ret;
229 }
230
231 return 0;
232}
233
234static int eqos_start_clks_tegra186(struct udevice *dev)
235{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800236#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600237 struct eqos_priv *eqos = dev_get_priv(dev);
238 int ret;
239
240 debug("%s(dev=%p):\n", __func__, dev);
241
242 ret = clk_enable(&eqos->clk_slave_bus);
243 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900244 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600245 goto err;
246 }
247
248 ret = clk_enable(&eqos->clk_master_bus);
249 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900250 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600251 goto err_disable_clk_slave_bus;
252 }
253
254 ret = clk_enable(&eqos->clk_rx);
255 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900256 pr_err("clk_enable(clk_rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600257 goto err_disable_clk_master_bus;
258 }
259
260 ret = clk_enable(&eqos->clk_ptp_ref);
261 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900262 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600263 goto err_disable_clk_rx;
264 }
265
266 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
267 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900268 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600269 goto err_disable_clk_ptp_ref;
270 }
271
272 ret = clk_enable(&eqos->clk_tx);
273 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900274 pr_err("clk_enable(clk_tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600275 goto err_disable_clk_ptp_ref;
276 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800277#endif
Stephen Warren50709602016-10-21 14:46:47 -0600278
279 debug("%s: OK\n", __func__);
280 return 0;
281
Fugang Duan37aae5f2020-05-03 22:41:17 +0800282#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600283err_disable_clk_ptp_ref:
284 clk_disable(&eqos->clk_ptp_ref);
285err_disable_clk_rx:
286 clk_disable(&eqos->clk_rx);
287err_disable_clk_master_bus:
288 clk_disable(&eqos->clk_master_bus);
289err_disable_clk_slave_bus:
290 clk_disable(&eqos->clk_slave_bus);
291err:
292 debug("%s: FAILED: %d\n", __func__, ret);
293 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800294#endif
Stephen Warren50709602016-10-21 14:46:47 -0600295}
296
Christophe Roullier6beb7802019-05-17 15:08:44 +0200297static int eqos_start_clks_stm32(struct udevice *dev)
298{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800299#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200300 struct eqos_priv *eqos = dev_get_priv(dev);
301 int ret;
302
303 debug("%s(dev=%p):\n", __func__, dev);
304
305 ret = clk_enable(&eqos->clk_master_bus);
306 if (ret < 0) {
307 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
308 goto err;
309 }
310
311 ret = clk_enable(&eqos->clk_rx);
312 if (ret < 0) {
313 pr_err("clk_enable(clk_rx) failed: %d", ret);
314 goto err_disable_clk_master_bus;
315 }
316
317 ret = clk_enable(&eqos->clk_tx);
318 if (ret < 0) {
319 pr_err("clk_enable(clk_tx) failed: %d", ret);
320 goto err_disable_clk_rx;
321 }
322
Daniil Stas81597922021-05-23 22:24:48 +0000323 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200324 ret = clk_enable(&eqos->clk_ck);
325 if (ret < 0) {
326 pr_err("clk_enable(clk_ck) failed: %d", ret);
327 goto err_disable_clk_tx;
328 }
Daniil Stas81597922021-05-23 22:24:48 +0000329 eqos->clk_ck_enabled = true;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200330 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800331#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200332
333 debug("%s: OK\n", __func__);
334 return 0;
335
Fugang Duan37aae5f2020-05-03 22:41:17 +0800336#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200337err_disable_clk_tx:
338 clk_disable(&eqos->clk_tx);
339err_disable_clk_rx:
340 clk_disable(&eqos->clk_rx);
341err_disable_clk_master_bus:
342 clk_disable(&eqos->clk_master_bus);
343err:
344 debug("%s: FAILED: %d\n", __func__, ret);
345 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800346#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200347}
348
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200349static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600350{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800351#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600352 struct eqos_priv *eqos = dev_get_priv(dev);
353
354 debug("%s(dev=%p):\n", __func__, dev);
355
356 clk_disable(&eqos->clk_tx);
357 clk_disable(&eqos->clk_ptp_ref);
358 clk_disable(&eqos->clk_rx);
359 clk_disable(&eqos->clk_master_bus);
360 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800361#endif
Stephen Warren50709602016-10-21 14:46:47 -0600362
363 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200364 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600365}
366
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200367static int eqos_stop_clks_stm32(struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200368{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800369#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200370 struct eqos_priv *eqos = dev_get_priv(dev);
371
372 debug("%s(dev=%p):\n", __func__, dev);
373
374 clk_disable(&eqos->clk_tx);
375 clk_disable(&eqos->clk_rx);
376 clk_disable(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800377#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200378
379 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200380 return 0;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800381}
382
Stephen Warren50709602016-10-21 14:46:47 -0600383static int eqos_start_resets_tegra186(struct udevice *dev)
384{
385 struct eqos_priv *eqos = dev_get_priv(dev);
386 int ret;
387
388 debug("%s(dev=%p):\n", __func__, dev);
389
390 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
391 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900392 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600393 return ret;
394 }
395
396 udelay(2);
397
398 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
399 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900400 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600401 return ret;
402 }
403
404 ret = reset_assert(&eqos->reset_ctl);
405 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900406 pr_err("reset_assert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600407 return ret;
408 }
409
410 udelay(2);
411
412 ret = reset_deassert(&eqos->reset_ctl);
413 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900414 pr_err("reset_deassert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600415 return ret;
416 }
417
418 debug("%s: OK\n", __func__);
419 return 0;
420}
421
422static int eqos_stop_resets_tegra186(struct udevice *dev)
423{
424 struct eqos_priv *eqos = dev_get_priv(dev);
425
426 reset_assert(&eqos->reset_ctl);
427 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
428
Christophe Roullier6beb7802019-05-17 15:08:44 +0200429 return 0;
430}
431
Stephen Warren50709602016-10-21 14:46:47 -0600432static int eqos_calibrate_pads_tegra186(struct udevice *dev)
433{
434 struct eqos_priv *eqos = dev_get_priv(dev);
435 int ret;
436
437 debug("%s(dev=%p):\n", __func__, dev);
438
439 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
440 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
441
442 udelay(1);
443
444 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
445 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
446
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100447 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
448 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600449 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900450 pr_err("calibrate didn't start");
Stephen Warren50709602016-10-21 14:46:47 -0600451 goto failed;
452 }
453
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100454 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
455 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600456 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900457 pr_err("calibrate didn't finish");
Stephen Warren50709602016-10-21 14:46:47 -0600458 goto failed;
459 }
460
461 ret = 0;
462
463failed:
464 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
465 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
466
467 debug("%s: returns %d\n", __func__, ret);
468
469 return ret;
470}
471
472static int eqos_disable_calibration_tegra186(struct udevice *dev)
473{
474 struct eqos_priv *eqos = dev_get_priv(dev);
475
476 debug("%s(dev=%p):\n", __func__, dev);
477
478 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
479 EQOS_AUTO_CAL_CONFIG_ENABLE);
480
481 return 0;
482}
483
484static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
485{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800486#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600487 struct eqos_priv *eqos = dev_get_priv(dev);
488
489 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800490#else
491 return 0;
492#endif
Stephen Warren50709602016-10-21 14:46:47 -0600493}
494
Christophe Roullier6beb7802019-05-17 15:08:44 +0200495static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
496{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800497#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200498 struct eqos_priv *eqos = dev_get_priv(dev);
499
500 return clk_get_rate(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800501#else
502 return 0;
503#endif
504}
505
Stephen Warren50709602016-10-21 14:46:47 -0600506static int eqos_set_full_duplex(struct udevice *dev)
507{
508 struct eqos_priv *eqos = dev_get_priv(dev);
509
510 debug("%s(dev=%p):\n", __func__, dev);
511
512 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
513
514 return 0;
515}
516
517static int eqos_set_half_duplex(struct udevice *dev)
518{
519 struct eqos_priv *eqos = dev_get_priv(dev);
520
521 debug("%s(dev=%p):\n", __func__, dev);
522
523 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
524
525 /* WAR: Flush TX queue when switching to half-duplex */
526 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
527 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
528
529 return 0;
530}
531
532static int eqos_set_gmii_speed(struct udevice *dev)
533{
534 struct eqos_priv *eqos = dev_get_priv(dev);
535
536 debug("%s(dev=%p):\n", __func__, dev);
537
538 clrbits_le32(&eqos->mac_regs->configuration,
539 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
540
541 return 0;
542}
543
544static int eqos_set_mii_speed_100(struct udevice *dev)
545{
546 struct eqos_priv *eqos = dev_get_priv(dev);
547
548 debug("%s(dev=%p):\n", __func__, dev);
549
550 setbits_le32(&eqos->mac_regs->configuration,
551 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
552
553 return 0;
554}
555
556static int eqos_set_mii_speed_10(struct udevice *dev)
557{
558 struct eqos_priv *eqos = dev_get_priv(dev);
559
560 debug("%s(dev=%p):\n", __func__, dev);
561
562 clrsetbits_le32(&eqos->mac_regs->configuration,
563 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
564
565 return 0;
566}
567
568static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
569{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800570#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600571 struct eqos_priv *eqos = dev_get_priv(dev);
572 ulong rate;
573 int ret;
574
575 debug("%s(dev=%p):\n", __func__, dev);
576
577 switch (eqos->phy->speed) {
578 case SPEED_1000:
579 rate = 125 * 1000 * 1000;
580 break;
581 case SPEED_100:
582 rate = 25 * 1000 * 1000;
583 break;
584 case SPEED_10:
585 rate = 2.5 * 1000 * 1000;
586 break;
587 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900588 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600589 return -EINVAL;
590 }
591
592 ret = clk_set_rate(&eqos->clk_tx, rate);
593 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900594 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600595 return ret;
596 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800597#endif
Stephen Warren50709602016-10-21 14:46:47 -0600598
599 return 0;
600}
601
602static int eqos_adjust_link(struct udevice *dev)
603{
604 struct eqos_priv *eqos = dev_get_priv(dev);
605 int ret;
606 bool en_calibration;
607
608 debug("%s(dev=%p):\n", __func__, dev);
609
610 if (eqos->phy->duplex)
611 ret = eqos_set_full_duplex(dev);
612 else
613 ret = eqos_set_half_duplex(dev);
614 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900615 pr_err("eqos_set_*_duplex() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600616 return ret;
617 }
618
619 switch (eqos->phy->speed) {
620 case SPEED_1000:
621 en_calibration = true;
622 ret = eqos_set_gmii_speed(dev);
623 break;
624 case SPEED_100:
625 en_calibration = true;
626 ret = eqos_set_mii_speed_100(dev);
627 break;
628 case SPEED_10:
629 en_calibration = false;
630 ret = eqos_set_mii_speed_10(dev);
631 break;
632 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900633 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600634 return -EINVAL;
635 }
636 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900637 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600638 return ret;
639 }
640
641 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200642 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600643 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200644 pr_err("eqos_calibrate_pads() failed: %d",
645 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600646 return ret;
647 }
648 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200649 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600650 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200651 pr_err("eqos_disable_calibration() failed: %d",
652 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600653 return ret;
654 }
655 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200656 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600657 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200658 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600659 return ret;
660 }
661
662 return 0;
663}
664
665static int eqos_write_hwaddr(struct udevice *dev)
666{
Simon Glassfa20e932020-12-03 16:55:20 -0700667 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600668 struct eqos_priv *eqos = dev_get_priv(dev);
669 uint32_t val;
670
671 /*
672 * This function may be called before start() or after stop(). At that
673 * time, on at least some configurations of the EQoS HW, all clocks to
674 * the EQoS HW block will be stopped, and a reset signal applied. If
675 * any register access is attempted in this state, bus timeouts or CPU
676 * hangs may occur. This check prevents that.
677 *
678 * A simple solution to this problem would be to not implement
679 * write_hwaddr(), since start() always writes the MAC address into HW
680 * anyway. However, it is desirable to implement write_hwaddr() to
681 * support the case of SW that runs subsequent to U-Boot which expects
682 * the MAC address to already be programmed into the EQoS registers,
683 * which must happen irrespective of whether the U-Boot user (or
684 * scripts) actually made use of the EQoS device, and hence
685 * irrespective of whether start() was ever called.
686 *
687 * Note that this requirement by subsequent SW is not valid for
688 * Tegra186, and is likely not valid for any non-PCI instantiation of
689 * the EQoS HW block. This function is implemented solely as
690 * future-proofing with the expectation the driver will eventually be
691 * ported to some system where the expectation above is true.
692 */
693 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
694 return 0;
695
696 /* Update the MAC address */
697 val = (plat->enetaddr[5] << 8) |
698 (plat->enetaddr[4]);
699 writel(val, &eqos->mac_regs->address0_high);
700 val = (plat->enetaddr[3] << 24) |
701 (plat->enetaddr[2] << 16) |
702 (plat->enetaddr[1] << 8) |
703 (plat->enetaddr[0]);
704 writel(val, &eqos->mac_regs->address0_low);
705
706 return 0;
707}
708
Ye Li3fb1a0e2020-05-03 22:41:20 +0800709static int eqos_read_rom_hwaddr(struct udevice *dev)
710{
Simon Glassfa20e932020-12-03 16:55:20 -0700711 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800712 struct eqos_priv *eqos = dev_get_priv(dev);
713 int ret;
714
715 ret = eqos->config->ops->eqos_get_enetaddr(dev);
716 if (ret < 0)
717 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800718
Ye Li3fb1a0e2020-05-03 22:41:20 +0800719 return !is_valid_ethaddr(pdata->enetaddr);
720}
721
Ye Li2f2aa482022-07-26 16:41:16 +0800722static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
723{
724 struct ofnode_phandle_args phandle_args;
725 int reg;
726
727 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
728 &phandle_args)) {
729 debug("Failed to find phy-handle");
730 return -ENODEV;
731 }
732
733 priv->phy_of_node = phandle_args.node;
734
735 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
736
737 return reg;
738}
739
Stephen Warren50709602016-10-21 14:46:47 -0600740static int eqos_start(struct udevice *dev)
741{
742 struct eqos_priv *eqos = dev_get_priv(dev);
743 int ret, i;
744 ulong rate;
745 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
746 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100747 ulong desc_pad;
Stephen Warren50709602016-10-21 14:46:47 -0600748
749 debug("%s(dev=%p):\n", __func__, dev);
750
751 eqos->tx_desc_idx = 0;
752 eqos->rx_desc_idx = 0;
753
Christophe Roullier6beb7802019-05-17 15:08:44 +0200754 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600755 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200756 pr_err("eqos_start_resets() failed: %d", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100757 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600758 }
759
760 udelay(10);
761
762 eqos->reg_access_ok = true;
763
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100764 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200765 EQOS_DMA_MODE_SWR, false,
766 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600767 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900768 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -0600769 goto err_stop_resets;
770 }
771
Christophe Roullier6beb7802019-05-17 15:08:44 +0200772 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600773 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200774 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600775 goto err_stop_resets;
776 }
Sumit Gargab973c92023-02-01 19:28:53 +0530777
778 if (eqos->config->ops->eqos_get_tick_clk_rate) {
779 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600780
Sumit Gargab973c92023-02-01 19:28:53 +0530781 val = (rate / 1000000) - 1;
782 writel(val, &eqos->mac_regs->us_tic_counter);
783 }
Stephen Warren50709602016-10-21 14:46:47 -0600784
Christophe Roullier6beb7802019-05-17 15:08:44 +0200785 /*
786 * if PHY was already connected and configured,
787 * don't need to reconnect/reconfigure again
788 */
Stephen Warren50709602016-10-21 14:46:47 -0600789 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800790 int addr = -1;
Ye Li2f2aa482022-07-26 16:41:16 +0800791 addr = eqos_get_phy_addr(eqos, dev);
Ye Liad122b72020-05-03 22:41:15 +0800792 eqos->phy = phy_connect(eqos->mii, addr, dev,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200793 eqos->config->interface(dev));
794 if (!eqos->phy) {
795 pr_err("phy_connect() failed");
796 goto err_stop_resets;
797 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100798
799 if (eqos->max_speed) {
800 ret = phy_set_supported(eqos->phy, eqos->max_speed);
801 if (ret) {
802 pr_err("phy_set_supported() failed: %d", ret);
803 goto err_shutdown_phy;
804 }
805 }
806
Ye Li2f2aa482022-07-26 16:41:16 +0800807 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200808 ret = phy_config(eqos->phy);
809 if (ret < 0) {
810 pr_err("phy_config() failed: %d", ret);
811 goto err_shutdown_phy;
812 }
Stephen Warren50709602016-10-21 14:46:47 -0600813 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200814
Stephen Warren50709602016-10-21 14:46:47 -0600815 ret = phy_startup(eqos->phy);
816 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900817 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600818 goto err_shutdown_phy;
819 }
820
821 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900822 pr_err("No link");
Stephen Warren50709602016-10-21 14:46:47 -0600823 goto err_shutdown_phy;
824 }
825
826 ret = eqos_adjust_link(dev);
827 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900828 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600829 goto err_shutdown_phy;
830 }
831
832 /* Configure MTL */
833
834 /* Enable Store and Forward mode for TX */
835 /* Program Tx operating mode */
836 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
837 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
838 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
839 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
840
841 /* Transmit Queue weight */
842 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
843
844 /* Enable Store and Forward mode for RX, since no jumbo frame */
845 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000846 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600847
848 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
849 val = readl(&eqos->mac_regs->hw_feature1);
850 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
851 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
852 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
853 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
854
Sumit Garg4d5c9652023-02-01 19:28:54 +0530855 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
856 tx_fifo_sz = 128 << tx_fifo_sz;
857 rx_fifo_sz = 128 << rx_fifo_sz;
858
859 /* Allow platform to override TX/RX fifo size */
860 if (eqos->tx_fifo_sz)
861 tx_fifo_sz = eqos->tx_fifo_sz;
862 if (eqos->rx_fifo_sz)
863 rx_fifo_sz = eqos->rx_fifo_sz;
864
865 /* r/tqs is encoded as (n / 256) - 1 */
866 tqs = tx_fifo_sz / 256 - 1;
867 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600868
869 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
870 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
871 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
872 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
873 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
874 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
875 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
876 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
877
878 /* Flow control used only if each channel gets 4KB or more FIFO */
879 if (rqs >= ((4096 / 256) - 1)) {
880 u32 rfd, rfa;
881
882 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
883 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
884
885 /*
886 * Set Threshold for Activating Flow Contol space for min 2
887 * frames ie, (1500 * 1) = 1500 bytes.
888 *
889 * Set Threshold for Deactivating Flow Contol for space of
890 * min 1 frame (frame size 1500bytes) in receive fifo
891 */
892 if (rqs == ((4096 / 256) - 1)) {
893 /*
894 * This violates the above formula because of FIFO size
895 * limit therefore overflow may occur inspite of this.
896 */
897 rfd = 0x3; /* Full-3K */
898 rfa = 0x1; /* Full-1.5K */
899 } else if (rqs == ((8192 / 256) - 1)) {
900 rfd = 0x6; /* Full-4K */
901 rfa = 0xa; /* Full-6K */
902 } else if (rqs == ((16384 / 256) - 1)) {
903 rfd = 0x6; /* Full-4K */
904 rfa = 0x12; /* Full-10K */
905 } else {
906 rfd = 0x6; /* Full-4K */
907 rfa = 0x1E; /* Full-16K */
908 }
909
910 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
911 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
912 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
913 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
914 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
915 (rfd <<
916 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
917 (rfa <<
918 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
919 }
920
921 /* Configure MAC */
922
923 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
924 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
925 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200926 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600927 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
928
Fugang Duan37aae5f2020-05-03 22:41:17 +0800929 /* Multicast and Broadcast Queue Enable */
930 setbits_le32(&eqos->mac_regs->unused_0a4,
931 0x00100000);
932 /* enable promise mode */
933 setbits_le32(&eqos->mac_regs->unused_004[1],
934 0x1);
935
Stephen Warren50709602016-10-21 14:46:47 -0600936 /* Set TX flow control parameters */
937 /* Set Pause Time */
938 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
939 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
940 /* Assign priority for TX flow control */
941 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
942 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
943 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
944 /* Assign priority for RX flow control */
945 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
946 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
947 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
948 /* Enable flow control */
949 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
950 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
951 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
952 EQOS_MAC_RX_FLOW_CTRL_RFE);
953
954 clrsetbits_le32(&eqos->mac_regs->configuration,
955 EQOS_MAC_CONFIGURATION_GPSLCE |
956 EQOS_MAC_CONFIGURATION_WD |
957 EQOS_MAC_CONFIGURATION_JD |
958 EQOS_MAC_CONFIGURATION_JE,
959 EQOS_MAC_CONFIGURATION_CST |
960 EQOS_MAC_CONFIGURATION_ACS);
961
962 eqos_write_hwaddr(dev);
963
964 /* Configure DMA */
965
966 /* Enable OSP mode */
967 setbits_le32(&eqos->dma_regs->ch0_tx_control,
968 EQOS_DMA_CH0_TX_CONTROL_OSP);
969
970 /* RX buffer size. Must be a multiple of bus width */
971 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
972 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
973 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
974 EQOS_MAX_PACKET_SIZE <<
975 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
976
Marek Vasut89077732021-01-07 11:12:16 +0100977 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
978 eqos->config->axi_bus_width;
979
Stephen Warren50709602016-10-21 14:46:47 -0600980 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +0100981 EQOS_DMA_CH0_CONTROL_PBLX8 |
982 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -0600983
984 /*
985 * Burst length must be < 1/2 FIFO size.
986 * FIFO size in tqs is encoded as (n / 256) - 1.
987 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
988 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
989 */
990 pbl = tqs + 1;
991 if (pbl > 32)
992 pbl = 32;
993 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
994 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
995 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
996 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
997
998 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
999 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1000 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1001 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1002
1003 /* DMA performance configuration */
1004 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1005 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1006 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1007 writel(val, &eqos->dma_regs->sysbus_mode);
1008
1009 /* Set up descriptors */
1010
Marek Vasut90cc13a2022-10-09 17:51:45 +02001011 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
1012 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +01001013
1014 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
1015 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
1016 eqos->config->ops->eqos_flush_desc(tx_desc);
1017 }
1018
Stephen Warren50709602016-10-21 14:46:47 -06001019 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +01001020 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Stephen Warren50709602016-10-21 14:46:47 -06001021 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1022 (i * EQOS_MAX_PACKET_SIZE));
Marek Vasutd54c98e2020-03-23 02:02:57 +01001023 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001024 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +01001025 eqos->config->ops->eqos_flush_desc(rx_desc);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001026 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf +
1027 (i * EQOS_MAX_PACKET_SIZE),
1028 EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -06001029 }
Stephen Warren50709602016-10-21 14:46:47 -06001030
1031 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001032 writel((ulong)eqos_get_desc(eqos, 0, false),
1033 &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001034 writel(EQOS_DESCRIPTORS_TX - 1,
1035 &eqos->dma_regs->ch0_txdesc_ring_length);
1036
1037 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001038 writel((ulong)eqos_get_desc(eqos, 0, true),
1039 &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001040 writel(EQOS_DESCRIPTORS_RX - 1,
1041 &eqos->dma_regs->ch0_rxdesc_ring_length);
1042
1043 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001044 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1045 EQOS_DMA_CH0_TX_CONTROL_ST);
1046 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1047 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001048 setbits_le32(&eqos->mac_regs->configuration,
1049 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001050
1051 /* TX tail pointer not written until we need to TX a packet */
1052 /*
1053 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1054 * first descriptor, implying all descriptors were available. However,
1055 * that's not distinguishable from none of the descriptors being
1056 * available.
1057 */
Marek Vasut89077732021-01-07 11:12:16 +01001058 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001059 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1060
1061 eqos->started = true;
1062
1063 debug("%s: OK\n", __func__);
1064 return 0;
1065
1066err_shutdown_phy:
1067 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001068err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001069 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001070err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001071 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001072 return ret;
1073}
1074
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001075static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001076{
1077 struct eqos_priv *eqos = dev_get_priv(dev);
1078 int i;
1079
1080 debug("%s(dev=%p):\n", __func__, dev);
1081
1082 if (!eqos->started)
1083 return;
1084 eqos->started = false;
1085 eqos->reg_access_ok = false;
1086
1087 /* Disable TX DMA */
1088 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1089 EQOS_DMA_CH0_TX_CONTROL_ST);
1090
1091 /* Wait for TX all packets to drain out of MTL */
1092 for (i = 0; i < 1000000; i++) {
1093 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1094 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1095 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1096 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1097 if ((trcsts != 1) && (!txqsts))
1098 break;
1099 }
1100
1101 /* Turn off MAC TX and RX */
1102 clrbits_le32(&eqos->mac_regs->configuration,
1103 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1104
1105 /* Wait for all RX packets to drain out of MTL */
1106 for (i = 0; i < 1000000; i++) {
1107 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1108 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1109 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1110 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1111 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1112 if ((!prxq) && (!rxqsts))
1113 break;
1114 }
1115
1116 /* Turn off RX DMA */
1117 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1118 EQOS_DMA_CH0_RX_CONTROL_SR);
1119
1120 if (eqos->phy) {
1121 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001122 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001123 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001124
1125 debug("%s: OK\n", __func__);
1126}
1127
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001128static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001129{
1130 struct eqos_priv *eqos = dev_get_priv(dev);
1131 struct eqos_desc *tx_desc;
1132 int i;
1133
1134 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1135 length);
1136
1137 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001138 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001139
Marek Vasut89077732021-01-07 11:12:16 +01001140 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001141 eqos->tx_desc_idx++;
1142 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1143
1144 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1145 tx_desc->des1 = 0;
1146 tx_desc->des2 = length;
1147 /*
1148 * Make sure that if HW sees the _OWN write below, it will see all the
1149 * writes to the rest of the descriptor too.
1150 */
1151 mb();
1152 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001153 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001154
Marek Vasut89077732021-01-07 11:12:16 +01001155 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001156 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001157
1158 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001159 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001160 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1161 return 0;
1162 udelay(1);
1163 }
1164
1165 debug("%s: TX timeout\n", __func__);
1166
1167 return -ETIMEDOUT;
1168}
1169
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001170static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001171{
1172 struct eqos_priv *eqos = dev_get_priv(dev);
1173 struct eqos_desc *rx_desc;
1174 int length;
1175
1176 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1177
Marek Vasut89077732021-01-07 11:12:16 +01001178 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001179 eqos->config->ops->eqos_inval_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001180 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1181 debug("%s: RX packet not available\n", __func__);
1182 return -EAGAIN;
1183 }
1184
1185 *packetp = eqos->rx_dma_buf +
1186 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1187 length = rx_desc->des3 & 0x7fff;
1188 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1189
Christophe Roullier6beb7802019-05-17 15:08:44 +02001190 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001191
1192 return length;
1193}
1194
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001195static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001196{
1197 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001198 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001199 uchar *packet_expected;
1200 struct eqos_desc *rx_desc;
1201
1202 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1203
1204 packet_expected = eqos->rx_dma_buf +
1205 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1206 if (packet != packet_expected) {
1207 debug("%s: Unexpected packet (expected %p)\n", __func__,
1208 packet_expected);
1209 return -EINVAL;
1210 }
1211
Fugang Duan37aae5f2020-05-03 22:41:17 +08001212 eqos->config->ops->eqos_inval_buffer(packet, length);
1213
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001214 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1215 for (idx = eqos->rx_desc_idx - idx_mask;
1216 idx <= eqos->rx_desc_idx;
1217 idx++) {
1218 rx_desc = eqos_get_desc(eqos, idx, true);
1219 rx_desc->des0 = 0;
1220 mb();
1221 eqos->config->ops->eqos_flush_desc(rx_desc);
1222 eqos->config->ops->eqos_inval_buffer(packet, length);
1223 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1224 (idx * EQOS_MAX_PACKET_SIZE));
1225 rx_desc->des1 = 0;
1226 rx_desc->des2 = 0;
1227 /*
1228 * Make sure that if HW sees the _OWN write below,
1229 * it will see all the writes to the rest of the
1230 * descriptor too.
1231 */
1232 mb();
1233 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1234 eqos->config->ops->eqos_flush_desc(rx_desc);
1235 }
1236 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1237 }
Stephen Warren50709602016-10-21 14:46:47 -06001238
1239 eqos->rx_desc_idx++;
1240 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1241
1242 return 0;
1243}
1244
1245static int eqos_probe_resources_core(struct udevice *dev)
1246{
1247 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001248 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001249 int ret;
1250
1251 debug("%s(dev=%p):\n", __func__, dev);
1252
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001253 /* Maximum distance between neighboring descriptors, in Bytes. */
1254 desc_step = sizeof(struct eqos_desc) +
1255 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1256 if (desc_step < ARCH_DMA_MINALIGN) {
1257 /*
1258 * The EQoS hardware implementation cannot place one descriptor
1259 * per cacheline, it is necessary to place multiple descriptors
1260 * per cacheline in memory and do cache management carefully.
1261 */
1262 eqos->desc_size = BIT(fls(desc_step) - 1);
1263 } else {
1264 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1265 (unsigned int)ARCH_DMA_MINALIGN);
1266 }
1267 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001268
1269 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1270 if (!eqos->tx_descs) {
1271 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001272 ret = -ENOMEM;
1273 goto err;
1274 }
Stephen Warren50709602016-10-21 14:46:47 -06001275
Marek Vasut90cc13a2022-10-09 17:51:45 +02001276 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1277 if (!eqos->rx_descs) {
1278 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1279 ret = -ENOMEM;
1280 goto err_free_tx_descs;
1281 }
1282
Stephen Warren50709602016-10-21 14:46:47 -06001283 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1284 if (!eqos->tx_dma_buf) {
1285 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1286 ret = -ENOMEM;
1287 goto err_free_descs;
1288 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001289 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001290
1291 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1292 if (!eqos->rx_dma_buf) {
1293 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1294 ret = -ENOMEM;
1295 goto err_free_tx_dma_buf;
1296 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001297 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001298
1299 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1300 if (!eqos->rx_pkt) {
1301 debug("%s: malloc(rx_pkt) failed\n", __func__);
1302 ret = -ENOMEM;
1303 goto err_free_rx_dma_buf;
1304 }
1305 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1306
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001307 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1308 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1309
Stephen Warren50709602016-10-21 14:46:47 -06001310 debug("%s: OK\n", __func__);
1311 return 0;
1312
1313err_free_rx_dma_buf:
1314 free(eqos->rx_dma_buf);
1315err_free_tx_dma_buf:
1316 free(eqos->tx_dma_buf);
1317err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001318 eqos_free_descs(eqos->rx_descs);
1319err_free_tx_descs:
1320 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001321err:
1322
1323 debug("%s: returns %d\n", __func__, ret);
1324 return ret;
1325}
1326
1327static int eqos_remove_resources_core(struct udevice *dev)
1328{
1329 struct eqos_priv *eqos = dev_get_priv(dev);
1330
1331 debug("%s(dev=%p):\n", __func__, dev);
1332
1333 free(eqos->rx_pkt);
1334 free(eqos->rx_dma_buf);
1335 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001336 eqos_free_descs(eqos->rx_descs);
1337 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001338
1339 debug("%s: OK\n", __func__);
1340 return 0;
1341}
1342
1343static int eqos_probe_resources_tegra186(struct udevice *dev)
1344{
1345 struct eqos_priv *eqos = dev_get_priv(dev);
1346 int ret;
1347
1348 debug("%s(dev=%p):\n", __func__, dev);
1349
1350 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1351 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001352 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001353 return ret;
1354 }
1355
1356 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1357 &eqos->phy_reset_gpio,
1358 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1359 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001360 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001361 goto err_free_reset_eqos;
1362 }
1363
1364 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1365 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001366 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001367 goto err_free_gpio_phy_reset;
1368 }
1369
1370 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1371 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001372 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001373 goto err_free_clk_slave_bus;
1374 }
1375
1376 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1377 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001378 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001379 goto err_free_clk_master_bus;
1380 }
1381
1382 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1383 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001384 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001385 goto err_free_clk_rx;
1386 return ret;
1387 }
1388
1389 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1390 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001391 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001392 goto err_free_clk_ptp_ref;
1393 }
1394
1395 debug("%s: OK\n", __func__);
1396 return 0;
1397
1398err_free_clk_ptp_ref:
1399 clk_free(&eqos->clk_ptp_ref);
1400err_free_clk_rx:
1401 clk_free(&eqos->clk_rx);
1402err_free_clk_master_bus:
1403 clk_free(&eqos->clk_master_bus);
1404err_free_clk_slave_bus:
1405 clk_free(&eqos->clk_slave_bus);
1406err_free_gpio_phy_reset:
1407 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1408err_free_reset_eqos:
1409 reset_free(&eqos->reset_ctl);
1410
1411 debug("%s: returns %d\n", __func__, ret);
1412 return ret;
1413}
1414
Christophe Roullier6beb7802019-05-17 15:08:44 +02001415/* board-specific Ethernet Interface initializations. */
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001416__weak int board_interface_eth_init(struct udevice *dev,
1417 phy_interface_t interface_type)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001418{
1419 return 0;
1420}
1421
1422static int eqos_probe_resources_stm32(struct udevice *dev)
1423{
1424 struct eqos_priv *eqos = dev_get_priv(dev);
1425 int ret;
1426 phy_interface_t interface;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001427
1428 debug("%s(dev=%p):\n", __func__, dev);
1429
1430 interface = eqos->config->interface(dev);
1431
Marek BehĂșn48631e42022-04-07 00:33:03 +02001432 if (interface == PHY_INTERFACE_MODE_NA) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001433 pr_err("Invalid PHY interface\n");
1434 return -EINVAL;
1435 }
1436
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001437 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001438 if (ret)
1439 return -EINVAL;
1440
1441 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1442 if (ret) {
1443 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1444 goto err_probe;
1445 }
1446
1447 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1448 if (ret) {
1449 pr_err("clk_get_by_name(rx) failed: %d", ret);
1450 goto err_free_clk_master_bus;
1451 }
1452
1453 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1454 if (ret) {
1455 pr_err("clk_get_by_name(tx) failed: %d", ret);
1456 goto err_free_clk_rx;
1457 }
1458
1459 /* Get ETH_CLK clocks (optional) */
1460 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1461 if (ret)
1462 pr_warn("No phy clock provided %d", ret);
1463
1464 debug("%s: OK\n", __func__);
1465 return 0;
1466
1467err_free_clk_rx:
1468 clk_free(&eqos->clk_rx);
1469err_free_clk_master_bus:
1470 clk_free(&eqos->clk_master_bus);
1471err_probe:
1472
1473 debug("%s: returns %d\n", __func__, ret);
1474 return ret;
1475}
1476
Marek BehĂșnbc194772022-04-07 00:33:01 +02001477static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001478{
1479 return PHY_INTERFACE_MODE_MII;
1480}
1481
Stephen Warren50709602016-10-21 14:46:47 -06001482static int eqos_remove_resources_tegra186(struct udevice *dev)
1483{
1484 struct eqos_priv *eqos = dev_get_priv(dev);
1485
1486 debug("%s(dev=%p):\n", __func__, dev);
1487
Fugang Duan37aae5f2020-05-03 22:41:17 +08001488#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -06001489 clk_free(&eqos->clk_tx);
1490 clk_free(&eqos->clk_ptp_ref);
1491 clk_free(&eqos->clk_rx);
1492 clk_free(&eqos->clk_slave_bus);
1493 clk_free(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001494#endif
Stephen Warren50709602016-10-21 14:46:47 -06001495 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1496 reset_free(&eqos->reset_ctl);
1497
1498 debug("%s: OK\n", __func__);
1499 return 0;
1500}
1501
Christophe Roullier6beb7802019-05-17 15:08:44 +02001502static int eqos_remove_resources_stm32(struct udevice *dev)
1503{
1504 struct eqos_priv *eqos = dev_get_priv(dev);
1505
1506 debug("%s(dev=%p):\n", __func__, dev);
1507
Peng Fan809993f2022-07-26 16:41:13 +08001508#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +02001509 clk_free(&eqos->clk_tx);
1510 clk_free(&eqos->clk_rx);
1511 clk_free(&eqos->clk_master_bus);
1512 if (clk_valid(&eqos->clk_ck))
1513 clk_free(&eqos->clk_ck);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001514#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001515
Christophe Roullier104dab52020-03-18 10:50:15 +01001516 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
1517 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1518
Christophe Roullier6beb7802019-05-17 15:08:44 +02001519 debug("%s: OK\n", __func__);
1520 return 0;
1521}
1522
Stephen Warren50709602016-10-21 14:46:47 -06001523static int eqos_probe(struct udevice *dev)
1524{
1525 struct eqos_priv *eqos = dev_get_priv(dev);
1526 int ret;
1527
1528 debug("%s(dev=%p):\n", __func__, dev);
1529
1530 eqos->dev = dev;
1531 eqos->config = (void *)dev_get_driver_data(dev);
1532
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001533 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001534 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001535 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001536 return -ENODEV;
1537 }
1538 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1539 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1540 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1541 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1542
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001543 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1544
Stephen Warren50709602016-10-21 14:46:47 -06001545 ret = eqos_probe_resources_core(dev);
1546 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001547 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001548 return ret;
1549 }
1550
Christophe Roullier6beb7802019-05-17 15:08:44 +02001551 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001552 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001553 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001554 goto err_remove_resources_core;
1555 }
1556
Marek Vasut30b28c42021-11-13 03:23:52 +01001557 ret = eqos->config->ops->eqos_start_clks(dev);
1558 if (ret < 0) {
1559 pr_err("eqos_start_clks() failed: %d", ret);
1560 goto err_remove_resources_tegra;
1561 }
1562
Ye Liad122b72020-05-03 22:41:15 +08001563#ifdef CONFIG_DM_ETH_PHY
1564 eqos->mii = eth_phy_get_mdio_bus(dev);
1565#endif
Stephen Warren50709602016-10-21 14:46:47 -06001566 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001567 eqos->mii = mdio_alloc();
1568 if (!eqos->mii) {
1569 pr_err("mdio_alloc() failed");
1570 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001571 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001572 }
1573 eqos->mii->read = eqos_mdio_read;
1574 eqos->mii->write = eqos_mdio_write;
1575 eqos->mii->priv = eqos;
1576 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001577
Ye Liad122b72020-05-03 22:41:15 +08001578 ret = mdio_register(eqos->mii);
1579 if (ret < 0) {
1580 pr_err("mdio_register() failed: %d", ret);
1581 goto err_free_mdio;
1582 }
Stephen Warren50709602016-10-21 14:46:47 -06001583 }
1584
Ye Liad122b72020-05-03 22:41:15 +08001585#ifdef CONFIG_DM_ETH_PHY
1586 eth_phy_set_mdio_bus(dev, eqos->mii);
1587#endif
1588
Stephen Warren50709602016-10-21 14:46:47 -06001589 debug("%s: OK\n", __func__);
1590 return 0;
1591
1592err_free_mdio:
1593 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001594err_stop_clks:
1595 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001596err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001597 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001598err_remove_resources_core:
1599 eqos_remove_resources_core(dev);
1600
1601 debug("%s: returns %d\n", __func__, ret);
1602 return ret;
1603}
1604
1605static int eqos_remove(struct udevice *dev)
1606{
1607 struct eqos_priv *eqos = dev_get_priv(dev);
1608
1609 debug("%s(dev=%p):\n", __func__, dev);
1610
1611 mdio_unregister(eqos->mii);
1612 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001613 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001614 eqos->config->ops->eqos_remove_resources(dev);
1615
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001616 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001617
1618 debug("%s: OK\n", __func__);
1619 return 0;
1620}
1621
Peng Fanc0a59952022-07-26 16:41:14 +08001622int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001623{
1624 return 0;
1625}
1626
Stephen Warren50709602016-10-21 14:46:47 -06001627static const struct eth_ops eqos_ops = {
1628 .start = eqos_start,
1629 .stop = eqos_stop,
1630 .send = eqos_send,
1631 .recv = eqos_recv,
1632 .free_pkt = eqos_free_pkt,
1633 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001634 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001635};
1636
Christophe Roullier6beb7802019-05-17 15:08:44 +02001637static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001638 .eqos_inval_desc = eqos_inval_desc_generic,
1639 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001640 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1641 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1642 .eqos_probe_resources = eqos_probe_resources_tegra186,
1643 .eqos_remove_resources = eqos_remove_resources_tegra186,
1644 .eqos_stop_resets = eqos_stop_resets_tegra186,
1645 .eqos_start_resets = eqos_start_resets_tegra186,
1646 .eqos_stop_clks = eqos_stop_clks_tegra186,
1647 .eqos_start_clks = eqos_start_clks_tegra186,
1648 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1649 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1650 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001651 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001652 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1653};
1654
Patrick Delaunay68083902020-06-08 11:27:19 +02001655static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001656 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001657 .mdio_wait = 10,
1658 .swr_wait = 10,
1659 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1660 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001661 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001662 .interface = eqos_get_interface_tegra186,
1663 .ops = &eqos_tegra186_ops
1664};
1665
1666static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08001667 .eqos_inval_desc = eqos_inval_desc_generic,
1668 .eqos_flush_desc = eqos_flush_desc_generic,
1669 .eqos_inval_buffer = eqos_inval_buffer_generic,
1670 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001671 .eqos_probe_resources = eqos_probe_resources_stm32,
1672 .eqos_remove_resources = eqos_remove_resources_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001673 .eqos_stop_resets = eqos_null_ops,
1674 .eqos_start_resets = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001675 .eqos_stop_clks = eqos_stop_clks_stm32,
1676 .eqos_start_clks = eqos_start_clks_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001677 .eqos_calibrate_pads = eqos_null_ops,
1678 .eqos_disable_calibration = eqos_null_ops,
1679 .eqos_set_tx_clk_speed = eqos_null_ops,
Patrice Chotardd9824432022-08-02 10:55:26 +02001680 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001681 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1682};
1683
Patrick Delaunay68083902020-06-08 11:27:19 +02001684static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001685 .reg_access_always_ok = false,
1686 .mdio_wait = 10000,
1687 .swr_wait = 50,
1688 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1689 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01001690 .axi_bus_width = EQOS_AXI_WIDTH_64,
Marek BehĂșnbc194772022-04-07 00:33:01 +02001691 .interface = dev_read_phy_mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001692 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001693};
1694
1695static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001696#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001697 {
1698 .compatible = "nvidia,tegra186-eqos",
1699 .data = (ulong)&eqos_tegra186_config
1700 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001701#endif
1702#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001703 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001704 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02001705 .data = (ulong)&eqos_stm32_config
1706 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001707#endif
1708#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001709 {
Marek Vasut7af11382022-02-26 04:36:37 +01001710 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001711 .data = (ulong)&eqos_imx_config
1712 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001713#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001714
Sumit Garg7c3be942023-02-01 19:28:55 +05301715#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1716 {
1717 .compatible = "qcom,qcs404-ethqos",
1718 .data = (ulong)&eqos_qcom_config
1719 },
1720#endif
1721
Stephen Warren50709602016-10-21 14:46:47 -06001722 { }
1723};
1724
1725U_BOOT_DRIVER(eth_eqos) = {
1726 .name = "eth_eqos",
1727 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001728 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001729 .probe = eqos_probe,
1730 .remove = eqos_remove,
1731 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001732 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001733 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001734};