blob: 27be5cf70b4d79d3fa70984624d5f42f0114ed43 [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;
Stephen Warren50709602016-10-21 14:46:47 -0600749
750 debug("%s(dev=%p):\n", __func__, dev);
751
752 eqos->tx_desc_idx = 0;
753 eqos->rx_desc_idx = 0;
754
Christophe Roullier6beb7802019-05-17 15:08:44 +0200755 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600756 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200757 pr_err("eqos_start_resets() failed: %d", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100758 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600759 }
760
761 udelay(10);
762
763 eqos->reg_access_ok = true;
764
Marek Vasute66825a2023-03-06 15:53:46 +0100765 /*
766 * Assert the SWR first, the actually reset the MAC and to latch in
767 * e.g. i.MX8M Plus GPR[1] content, which selects interface mode.
768 */
769 setbits_le32(&eqos->dma_regs->mode, EQOS_DMA_MODE_SWR);
770
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100771 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200772 EQOS_DMA_MODE_SWR, false,
773 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600774 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900775 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -0600776 goto err_stop_resets;
777 }
778
Christophe Roullier6beb7802019-05-17 15:08:44 +0200779 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600780 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200781 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600782 goto err_stop_resets;
783 }
Sumit Gargab973c92023-02-01 19:28:53 +0530784
785 if (eqos->config->ops->eqos_get_tick_clk_rate) {
786 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600787
Sumit Gargab973c92023-02-01 19:28:53 +0530788 val = (rate / 1000000) - 1;
789 writel(val, &eqos->mac_regs->us_tic_counter);
790 }
Stephen Warren50709602016-10-21 14:46:47 -0600791
Christophe Roullier6beb7802019-05-17 15:08:44 +0200792 /*
793 * if PHY was already connected and configured,
794 * don't need to reconnect/reconfigure again
795 */
Stephen Warren50709602016-10-21 14:46:47 -0600796 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800797 int addr = -1;
Elmar Psilogdd65ba22023-02-20 16:03:15 +0100798 ofnode fixed_node;
799
800 if (IS_ENABLED(CONFIG_PHY_FIXED)) {
801 fixed_node = ofnode_find_subnode(dev_ofnode(dev),
802 "fixed-link");
803 if (ofnode_valid(fixed_node))
804 eqos->phy = fixed_phy_create(dev_ofnode(dev));
805 }
806
807 if (!eqos->phy) {
808 addr = eqos_get_phy_addr(eqos, dev);
809 eqos->phy = phy_connect(eqos->mii, addr, dev,
810 eqos->config->interface(dev));
811 }
812
Christophe Roullier6beb7802019-05-17 15:08:44 +0200813 if (!eqos->phy) {
814 pr_err("phy_connect() failed");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000815 ret = -ENODEV;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200816 goto err_stop_resets;
817 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100818
819 if (eqos->max_speed) {
820 ret = phy_set_supported(eqos->phy, eqos->max_speed);
821 if (ret) {
822 pr_err("phy_set_supported() failed: %d", ret);
823 goto err_shutdown_phy;
824 }
825 }
826
Ye Li2f2aa482022-07-26 16:41:16 +0800827 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200828 ret = phy_config(eqos->phy);
829 if (ret < 0) {
830 pr_err("phy_config() failed: %d", ret);
831 goto err_shutdown_phy;
832 }
Stephen Warren50709602016-10-21 14:46:47 -0600833 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200834
Stephen Warren50709602016-10-21 14:46:47 -0600835 ret = phy_startup(eqos->phy);
836 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900837 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600838 goto err_shutdown_phy;
839 }
840
841 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900842 pr_err("No link");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000843 ret = -EAGAIN;
Stephen Warren50709602016-10-21 14:46:47 -0600844 goto err_shutdown_phy;
845 }
846
847 ret = eqos_adjust_link(dev);
848 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900849 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600850 goto err_shutdown_phy;
851 }
852
853 /* Configure MTL */
854
855 /* Enable Store and Forward mode for TX */
856 /* Program Tx operating mode */
857 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
858 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
859 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
860 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
861
862 /* Transmit Queue weight */
863 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
864
865 /* Enable Store and Forward mode for RX, since no jumbo frame */
866 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000867 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600868
869 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
870 val = readl(&eqos->mac_regs->hw_feature1);
871 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
872 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
873 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
874 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
875
Sumit Garg4d5c9652023-02-01 19:28:54 +0530876 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
877 tx_fifo_sz = 128 << tx_fifo_sz;
878 rx_fifo_sz = 128 << rx_fifo_sz;
879
880 /* Allow platform to override TX/RX fifo size */
881 if (eqos->tx_fifo_sz)
882 tx_fifo_sz = eqos->tx_fifo_sz;
883 if (eqos->rx_fifo_sz)
884 rx_fifo_sz = eqos->rx_fifo_sz;
885
886 /* r/tqs is encoded as (n / 256) - 1 */
887 tqs = tx_fifo_sz / 256 - 1;
888 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600889
890 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
891 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
892 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
893 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
894 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
895 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
896 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
897 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
898
899 /* Flow control used only if each channel gets 4KB or more FIFO */
900 if (rqs >= ((4096 / 256) - 1)) {
901 u32 rfd, rfa;
902
903 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
904 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
905
906 /*
907 * Set Threshold for Activating Flow Contol space for min 2
908 * frames ie, (1500 * 1) = 1500 bytes.
909 *
910 * Set Threshold for Deactivating Flow Contol for space of
911 * min 1 frame (frame size 1500bytes) in receive fifo
912 */
913 if (rqs == ((4096 / 256) - 1)) {
914 /*
915 * This violates the above formula because of FIFO size
916 * limit therefore overflow may occur inspite of this.
917 */
918 rfd = 0x3; /* Full-3K */
919 rfa = 0x1; /* Full-1.5K */
920 } else if (rqs == ((8192 / 256) - 1)) {
921 rfd = 0x6; /* Full-4K */
922 rfa = 0xa; /* Full-6K */
923 } else if (rqs == ((16384 / 256) - 1)) {
924 rfd = 0x6; /* Full-4K */
925 rfa = 0x12; /* Full-10K */
926 } else {
927 rfd = 0x6; /* Full-4K */
928 rfa = 0x1E; /* Full-16K */
929 }
930
931 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
932 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
933 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
934 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
935 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
936 (rfd <<
937 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
938 (rfa <<
939 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
940 }
941
942 /* Configure MAC */
943
944 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
945 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
946 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200947 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600948 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
949
Fugang Duan37aae5f2020-05-03 22:41:17 +0800950 /* Multicast and Broadcast Queue Enable */
951 setbits_le32(&eqos->mac_regs->unused_0a4,
952 0x00100000);
953 /* enable promise mode */
954 setbits_le32(&eqos->mac_regs->unused_004[1],
955 0x1);
956
Stephen Warren50709602016-10-21 14:46:47 -0600957 /* Set TX flow control parameters */
958 /* Set Pause Time */
959 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
960 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
961 /* Assign priority for TX flow control */
962 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
963 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
964 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
965 /* Assign priority for RX flow control */
966 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
967 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
968 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
969 /* Enable flow control */
970 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
971 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
972 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
973 EQOS_MAC_RX_FLOW_CTRL_RFE);
974
975 clrsetbits_le32(&eqos->mac_regs->configuration,
976 EQOS_MAC_CONFIGURATION_GPSLCE |
977 EQOS_MAC_CONFIGURATION_WD |
978 EQOS_MAC_CONFIGURATION_JD |
979 EQOS_MAC_CONFIGURATION_JE,
980 EQOS_MAC_CONFIGURATION_CST |
981 EQOS_MAC_CONFIGURATION_ACS);
982
983 eqos_write_hwaddr(dev);
984
985 /* Configure DMA */
986
987 /* Enable OSP mode */
988 setbits_le32(&eqos->dma_regs->ch0_tx_control,
989 EQOS_DMA_CH0_TX_CONTROL_OSP);
990
991 /* RX buffer size. Must be a multiple of bus width */
992 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
993 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
994 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
995 EQOS_MAX_PACKET_SIZE <<
996 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
997
Marek Vasut89077732021-01-07 11:12:16 +0100998 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
999 eqos->config->axi_bus_width;
1000
Stephen Warren50709602016-10-21 14:46:47 -06001001 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +01001002 EQOS_DMA_CH0_CONTROL_PBLX8 |
1003 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -06001004
1005 /*
1006 * Burst length must be < 1/2 FIFO size.
1007 * FIFO size in tqs is encoded as (n / 256) - 1.
1008 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1009 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1010 */
1011 pbl = tqs + 1;
1012 if (pbl > 32)
1013 pbl = 32;
1014 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1015 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1016 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1017 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1018
1019 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1020 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1021 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1022 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1023
1024 /* DMA performance configuration */
1025 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1026 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1027 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1028 writel(val, &eqos->dma_regs->sysbus_mode);
1029
1030 /* Set up descriptors */
1031
Marek Vasut90cc13a2022-10-09 17:51:45 +02001032 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
1033 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +01001034
1035 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
1036 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
1037 eqos->config->ops->eqos_flush_desc(tx_desc);
1038 }
1039
Stephen Warren50709602016-10-21 14:46:47 -06001040 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +01001041 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Stephen Warren50709602016-10-21 14:46:47 -06001042 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1043 (i * EQOS_MAX_PACKET_SIZE));
Marek Vasutd54c98e2020-03-23 02:02:57 +01001044 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001045 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +01001046 eqos->config->ops->eqos_flush_desc(rx_desc);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001047 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf +
1048 (i * EQOS_MAX_PACKET_SIZE),
1049 EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -06001050 }
Stephen Warren50709602016-10-21 14:46:47 -06001051
1052 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001053 writel((ulong)eqos_get_desc(eqos, 0, false),
1054 &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001055 writel(EQOS_DESCRIPTORS_TX - 1,
1056 &eqos->dma_regs->ch0_txdesc_ring_length);
1057
1058 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001059 writel((ulong)eqos_get_desc(eqos, 0, true),
1060 &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001061 writel(EQOS_DESCRIPTORS_RX - 1,
1062 &eqos->dma_regs->ch0_rxdesc_ring_length);
1063
1064 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001065 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1066 EQOS_DMA_CH0_TX_CONTROL_ST);
1067 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1068 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001069 setbits_le32(&eqos->mac_regs->configuration,
1070 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001071
1072 /* TX tail pointer not written until we need to TX a packet */
1073 /*
1074 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1075 * first descriptor, implying all descriptors were available. However,
1076 * that's not distinguishable from none of the descriptors being
1077 * available.
1078 */
Marek Vasut89077732021-01-07 11:12:16 +01001079 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001080 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1081
1082 eqos->started = true;
1083
1084 debug("%s: OK\n", __func__);
1085 return 0;
1086
1087err_shutdown_phy:
1088 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001089err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001090 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001091err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001092 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001093 return ret;
1094}
1095
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001096static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001097{
1098 struct eqos_priv *eqos = dev_get_priv(dev);
1099 int i;
1100
1101 debug("%s(dev=%p):\n", __func__, dev);
1102
1103 if (!eqos->started)
1104 return;
1105 eqos->started = false;
1106 eqos->reg_access_ok = false;
1107
1108 /* Disable TX DMA */
1109 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1110 EQOS_DMA_CH0_TX_CONTROL_ST);
1111
1112 /* Wait for TX all packets to drain out of MTL */
1113 for (i = 0; i < 1000000; i++) {
1114 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1115 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1116 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1117 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1118 if ((trcsts != 1) && (!txqsts))
1119 break;
1120 }
1121
1122 /* Turn off MAC TX and RX */
1123 clrbits_le32(&eqos->mac_regs->configuration,
1124 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1125
1126 /* Wait for all RX packets to drain out of MTL */
1127 for (i = 0; i < 1000000; i++) {
1128 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1129 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1130 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1131 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1132 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1133 if ((!prxq) && (!rxqsts))
1134 break;
1135 }
1136
1137 /* Turn off RX DMA */
1138 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1139 EQOS_DMA_CH0_RX_CONTROL_SR);
1140
1141 if (eqos->phy) {
1142 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001143 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001144 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001145
1146 debug("%s: OK\n", __func__);
1147}
1148
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001149static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001150{
1151 struct eqos_priv *eqos = dev_get_priv(dev);
1152 struct eqos_desc *tx_desc;
1153 int i;
1154
1155 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1156 length);
1157
1158 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001159 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001160
Marek Vasut89077732021-01-07 11:12:16 +01001161 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001162 eqos->tx_desc_idx++;
1163 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1164
1165 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1166 tx_desc->des1 = 0;
1167 tx_desc->des2 = length;
1168 /*
1169 * Make sure that if HW sees the _OWN write below, it will see all the
1170 * writes to the rest of the descriptor too.
1171 */
1172 mb();
1173 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001174 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001175
Marek Vasut89077732021-01-07 11:12:16 +01001176 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001177 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001178
1179 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001180 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001181 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1182 return 0;
1183 udelay(1);
1184 }
1185
1186 debug("%s: TX timeout\n", __func__);
1187
1188 return -ETIMEDOUT;
1189}
1190
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001191static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001192{
1193 struct eqos_priv *eqos = dev_get_priv(dev);
1194 struct eqos_desc *rx_desc;
1195 int length;
1196
1197 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1198
Marek Vasut89077732021-01-07 11:12:16 +01001199 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001200 eqos->config->ops->eqos_inval_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001201 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1202 debug("%s: RX packet not available\n", __func__);
1203 return -EAGAIN;
1204 }
1205
1206 *packetp = eqos->rx_dma_buf +
1207 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1208 length = rx_desc->des3 & 0x7fff;
1209 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1210
Christophe Roullier6beb7802019-05-17 15:08:44 +02001211 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001212
1213 return length;
1214}
1215
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001216static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001217{
1218 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001219 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001220 uchar *packet_expected;
1221 struct eqos_desc *rx_desc;
1222
1223 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1224
1225 packet_expected = eqos->rx_dma_buf +
1226 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1227 if (packet != packet_expected) {
1228 debug("%s: Unexpected packet (expected %p)\n", __func__,
1229 packet_expected);
1230 return -EINVAL;
1231 }
1232
Fugang Duan37aae5f2020-05-03 22:41:17 +08001233 eqos->config->ops->eqos_inval_buffer(packet, length);
1234
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001235 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1236 for (idx = eqos->rx_desc_idx - idx_mask;
1237 idx <= eqos->rx_desc_idx;
1238 idx++) {
1239 rx_desc = eqos_get_desc(eqos, idx, true);
1240 rx_desc->des0 = 0;
1241 mb();
1242 eqos->config->ops->eqos_flush_desc(rx_desc);
1243 eqos->config->ops->eqos_inval_buffer(packet, length);
1244 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1245 (idx * EQOS_MAX_PACKET_SIZE));
1246 rx_desc->des1 = 0;
1247 rx_desc->des2 = 0;
1248 /*
1249 * Make sure that if HW sees the _OWN write below,
1250 * it will see all the writes to the rest of the
1251 * descriptor too.
1252 */
1253 mb();
1254 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1255 eqos->config->ops->eqos_flush_desc(rx_desc);
1256 }
1257 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1258 }
Stephen Warren50709602016-10-21 14:46:47 -06001259
1260 eqos->rx_desc_idx++;
1261 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1262
1263 return 0;
1264}
1265
1266static int eqos_probe_resources_core(struct udevice *dev)
1267{
1268 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001269 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001270 int ret;
1271
1272 debug("%s(dev=%p):\n", __func__, dev);
1273
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001274 /* Maximum distance between neighboring descriptors, in Bytes. */
1275 desc_step = sizeof(struct eqos_desc) +
1276 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1277 if (desc_step < ARCH_DMA_MINALIGN) {
1278 /*
1279 * The EQoS hardware implementation cannot place one descriptor
1280 * per cacheline, it is necessary to place multiple descriptors
1281 * per cacheline in memory and do cache management carefully.
1282 */
1283 eqos->desc_size = BIT(fls(desc_step) - 1);
1284 } else {
1285 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1286 (unsigned int)ARCH_DMA_MINALIGN);
1287 }
1288 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001289
1290 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1291 if (!eqos->tx_descs) {
1292 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001293 ret = -ENOMEM;
1294 goto err;
1295 }
Stephen Warren50709602016-10-21 14:46:47 -06001296
Marek Vasut90cc13a2022-10-09 17:51:45 +02001297 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1298 if (!eqos->rx_descs) {
1299 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1300 ret = -ENOMEM;
1301 goto err_free_tx_descs;
1302 }
1303
Stephen Warren50709602016-10-21 14:46:47 -06001304 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1305 if (!eqos->tx_dma_buf) {
1306 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1307 ret = -ENOMEM;
1308 goto err_free_descs;
1309 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001310 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001311
1312 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1313 if (!eqos->rx_dma_buf) {
1314 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1315 ret = -ENOMEM;
1316 goto err_free_tx_dma_buf;
1317 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001318 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001319
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001320 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1321 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1322
Stephen Warren50709602016-10-21 14:46:47 -06001323 debug("%s: OK\n", __func__);
1324 return 0;
1325
Stephen Warren50709602016-10-21 14:46:47 -06001326err_free_tx_dma_buf:
1327 free(eqos->tx_dma_buf);
1328err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001329 eqos_free_descs(eqos->rx_descs);
1330err_free_tx_descs:
1331 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001332err:
1333
1334 debug("%s: returns %d\n", __func__, ret);
1335 return ret;
1336}
1337
1338static int eqos_remove_resources_core(struct udevice *dev)
1339{
1340 struct eqos_priv *eqos = dev_get_priv(dev);
1341
1342 debug("%s(dev=%p):\n", __func__, dev);
1343
Stephen Warren50709602016-10-21 14:46:47 -06001344 free(eqos->rx_dma_buf);
1345 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001346 eqos_free_descs(eqos->rx_descs);
1347 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001348
1349 debug("%s: OK\n", __func__);
1350 return 0;
1351}
1352
1353static int eqos_probe_resources_tegra186(struct udevice *dev)
1354{
1355 struct eqos_priv *eqos = dev_get_priv(dev);
1356 int ret;
1357
1358 debug("%s(dev=%p):\n", __func__, dev);
1359
1360 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1361 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001362 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001363 return ret;
1364 }
1365
1366 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1367 &eqos->phy_reset_gpio,
1368 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1369 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001370 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001371 goto err_free_reset_eqos;
1372 }
1373
1374 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1375 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001376 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001377 goto err_free_gpio_phy_reset;
1378 }
1379
1380 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1381 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001382 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001383 goto err_free_clk_slave_bus;
1384 }
1385
1386 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1387 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001388 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001389 goto err_free_clk_master_bus;
1390 }
1391
1392 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1393 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001394 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001395 goto err_free_clk_rx;
Stephen Warren50709602016-10-21 14:46:47 -06001396 }
1397
1398 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1399 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001400 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001401 goto err_free_clk_ptp_ref;
1402 }
1403
1404 debug("%s: OK\n", __func__);
1405 return 0;
1406
1407err_free_clk_ptp_ref:
1408 clk_free(&eqos->clk_ptp_ref);
1409err_free_clk_rx:
1410 clk_free(&eqos->clk_rx);
1411err_free_clk_master_bus:
1412 clk_free(&eqos->clk_master_bus);
1413err_free_clk_slave_bus:
1414 clk_free(&eqos->clk_slave_bus);
1415err_free_gpio_phy_reset:
1416 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1417err_free_reset_eqos:
1418 reset_free(&eqos->reset_ctl);
1419
1420 debug("%s: returns %d\n", __func__, ret);
1421 return ret;
1422}
1423
Christophe Roullier6beb7802019-05-17 15:08:44 +02001424static int eqos_probe_resources_stm32(struct udevice *dev)
1425{
1426 struct eqos_priv *eqos = dev_get_priv(dev);
1427 int ret;
1428 phy_interface_t interface;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001429
1430 debug("%s(dev=%p):\n", __func__, dev);
1431
1432 interface = eqos->config->interface(dev);
1433
Marek BehĂșn48631e42022-04-07 00:33:03 +02001434 if (interface == PHY_INTERFACE_MODE_NA) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001435 pr_err("Invalid PHY interface\n");
1436 return -EINVAL;
1437 }
1438
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001439 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001440 if (ret)
1441 return -EINVAL;
1442
1443 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1444 if (ret) {
1445 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1446 goto err_probe;
1447 }
1448
1449 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1450 if (ret) {
1451 pr_err("clk_get_by_name(rx) failed: %d", ret);
1452 goto err_free_clk_master_bus;
1453 }
1454
1455 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1456 if (ret) {
1457 pr_err("clk_get_by_name(tx) failed: %d", ret);
1458 goto err_free_clk_rx;
1459 }
1460
1461 /* Get ETH_CLK clocks (optional) */
1462 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1463 if (ret)
1464 pr_warn("No phy clock provided %d", ret);
1465
1466 debug("%s: OK\n", __func__);
1467 return 0;
1468
1469err_free_clk_rx:
1470 clk_free(&eqos->clk_rx);
1471err_free_clk_master_bus:
1472 clk_free(&eqos->clk_master_bus);
1473err_probe:
1474
1475 debug("%s: returns %d\n", __func__, ret);
1476 return ret;
1477}
1478
Marek BehĂșnbc194772022-04-07 00:33:01 +02001479static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001480{
1481 return PHY_INTERFACE_MODE_MII;
1482}
1483
Stephen Warren50709602016-10-21 14:46:47 -06001484static int eqos_remove_resources_tegra186(struct udevice *dev)
1485{
1486 struct eqos_priv *eqos = dev_get_priv(dev);
1487
1488 debug("%s(dev=%p):\n", __func__, dev);
1489
Fugang Duan37aae5f2020-05-03 22:41:17 +08001490#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -06001491 clk_free(&eqos->clk_tx);
1492 clk_free(&eqos->clk_ptp_ref);
1493 clk_free(&eqos->clk_rx);
1494 clk_free(&eqos->clk_slave_bus);
1495 clk_free(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001496#endif
Stephen Warren50709602016-10-21 14:46:47 -06001497 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1498 reset_free(&eqos->reset_ctl);
1499
1500 debug("%s: OK\n", __func__);
1501 return 0;
1502}
1503
Christophe Roullier6beb7802019-05-17 15:08:44 +02001504static int eqos_remove_resources_stm32(struct udevice *dev)
1505{
Marek Vasut006ab6b2023-03-06 15:53:44 +01001506 struct eqos_priv * __maybe_unused eqos = dev_get_priv(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001507
1508 debug("%s(dev=%p):\n", __func__, dev);
1509
Peng Fan809993f2022-07-26 16:41:13 +08001510#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +02001511 clk_free(&eqos->clk_tx);
1512 clk_free(&eqos->clk_rx);
1513 clk_free(&eqos->clk_master_bus);
1514 if (clk_valid(&eqos->clk_ck))
1515 clk_free(&eqos->clk_ck);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001516#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001517
1518 debug("%s: OK\n", __func__);
1519 return 0;
1520}
1521
Stephen Warren50709602016-10-21 14:46:47 -06001522static int eqos_probe(struct udevice *dev)
1523{
1524 struct eqos_priv *eqos = dev_get_priv(dev);
1525 int ret;
1526
1527 debug("%s(dev=%p):\n", __func__, dev);
1528
1529 eqos->dev = dev;
1530 eqos->config = (void *)dev_get_driver_data(dev);
1531
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001532 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001533 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001534 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001535 return -ENODEV;
1536 }
1537 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1538 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1539 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1540 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1541
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001542 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1543
Stephen Warren50709602016-10-21 14:46:47 -06001544 ret = eqos_probe_resources_core(dev);
1545 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001546 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001547 return ret;
1548 }
1549
Christophe Roullier6beb7802019-05-17 15:08:44 +02001550 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001551 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001552 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001553 goto err_remove_resources_core;
1554 }
1555
Marek Vasut30b28c42021-11-13 03:23:52 +01001556 ret = eqos->config->ops->eqos_start_clks(dev);
1557 if (ret < 0) {
1558 pr_err("eqos_start_clks() failed: %d", ret);
1559 goto err_remove_resources_tegra;
1560 }
1561
Ye Liad122b72020-05-03 22:41:15 +08001562#ifdef CONFIG_DM_ETH_PHY
1563 eqos->mii = eth_phy_get_mdio_bus(dev);
1564#endif
Stephen Warren50709602016-10-21 14:46:47 -06001565 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001566 eqos->mii = mdio_alloc();
1567 if (!eqos->mii) {
1568 pr_err("mdio_alloc() failed");
1569 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001570 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001571 }
1572 eqos->mii->read = eqos_mdio_read;
1573 eqos->mii->write = eqos_mdio_write;
1574 eqos->mii->priv = eqos;
1575 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001576
Ye Liad122b72020-05-03 22:41:15 +08001577 ret = mdio_register(eqos->mii);
1578 if (ret < 0) {
1579 pr_err("mdio_register() failed: %d", ret);
1580 goto err_free_mdio;
1581 }
Stephen Warren50709602016-10-21 14:46:47 -06001582 }
1583
Ye Liad122b72020-05-03 22:41:15 +08001584#ifdef CONFIG_DM_ETH_PHY
1585 eth_phy_set_mdio_bus(dev, eqos->mii);
1586#endif
1587
Stephen Warren50709602016-10-21 14:46:47 -06001588 debug("%s: OK\n", __func__);
1589 return 0;
1590
1591err_free_mdio:
1592 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001593err_stop_clks:
1594 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001595err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001596 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001597err_remove_resources_core:
1598 eqos_remove_resources_core(dev);
1599
1600 debug("%s: returns %d\n", __func__, ret);
1601 return ret;
1602}
1603
1604static int eqos_remove(struct udevice *dev)
1605{
1606 struct eqos_priv *eqos = dev_get_priv(dev);
1607
1608 debug("%s(dev=%p):\n", __func__, dev);
1609
1610 mdio_unregister(eqos->mii);
1611 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001612 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001613 eqos->config->ops->eqos_remove_resources(dev);
1614
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001615 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001616
1617 debug("%s: OK\n", __func__);
1618 return 0;
1619}
1620
Peng Fanc0a59952022-07-26 16:41:14 +08001621int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001622{
1623 return 0;
1624}
1625
Stephen Warren50709602016-10-21 14:46:47 -06001626static const struct eth_ops eqos_ops = {
1627 .start = eqos_start,
1628 .stop = eqos_stop,
1629 .send = eqos_send,
1630 .recv = eqos_recv,
1631 .free_pkt = eqos_free_pkt,
1632 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001633 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001634};
1635
Christophe Roullier6beb7802019-05-17 15:08:44 +02001636static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001637 .eqos_inval_desc = eqos_inval_desc_generic,
1638 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001639 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1640 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1641 .eqos_probe_resources = eqos_probe_resources_tegra186,
1642 .eqos_remove_resources = eqos_remove_resources_tegra186,
1643 .eqos_stop_resets = eqos_stop_resets_tegra186,
1644 .eqos_start_resets = eqos_start_resets_tegra186,
1645 .eqos_stop_clks = eqos_stop_clks_tegra186,
1646 .eqos_start_clks = eqos_start_clks_tegra186,
1647 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1648 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1649 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001650 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001651 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1652};
1653
Patrick Delaunay68083902020-06-08 11:27:19 +02001654static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001655 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001656 .mdio_wait = 10,
1657 .swr_wait = 10,
1658 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1659 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001660 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001661 .interface = eqos_get_interface_tegra186,
1662 .ops = &eqos_tegra186_ops
1663};
1664
1665static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08001666 .eqos_inval_desc = eqos_inval_desc_generic,
1667 .eqos_flush_desc = eqos_flush_desc_generic,
1668 .eqos_inval_buffer = eqos_inval_buffer_generic,
1669 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001670 .eqos_probe_resources = eqos_probe_resources_stm32,
1671 .eqos_remove_resources = eqos_remove_resources_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001672 .eqos_stop_resets = eqos_null_ops,
1673 .eqos_start_resets = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001674 .eqos_stop_clks = eqos_stop_clks_stm32,
1675 .eqos_start_clks = eqos_start_clks_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001676 .eqos_calibrate_pads = eqos_null_ops,
1677 .eqos_disable_calibration = eqos_null_ops,
1678 .eqos_set_tx_clk_speed = eqos_null_ops,
Patrice Chotardd9824432022-08-02 10:55:26 +02001679 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001680 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1681};
1682
Patrick Delaunay68083902020-06-08 11:27:19 +02001683static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001684 .reg_access_always_ok = false,
1685 .mdio_wait = 10000,
1686 .swr_wait = 50,
1687 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1688 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01001689 .axi_bus_width = EQOS_AXI_WIDTH_64,
Marek BehĂșnbc194772022-04-07 00:33:01 +02001690 .interface = dev_read_phy_mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001691 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001692};
1693
1694static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001695#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001696 {
1697 .compatible = "nvidia,tegra186-eqos",
1698 .data = (ulong)&eqos_tegra186_config
1699 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001700#endif
1701#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001702 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001703 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02001704 .data = (ulong)&eqos_stm32_config
1705 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001706#endif
1707#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001708 {
Marek Vasut7af11382022-02-26 04:36:37 +01001709 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001710 .data = (ulong)&eqos_imx_config
1711 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001712#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001713
Sumit Garg7c3be942023-02-01 19:28:55 +05301714#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1715 {
1716 .compatible = "qcom,qcs404-ethqos",
1717 .data = (ulong)&eqos_qcom_config
1718 },
1719#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001720#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1721 {
1722 .compatible = "starfive,jh7110-dwmac",
1723 .data = (ulong)&eqos_jh7110_config
1724 },
1725#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301726
Stephen Warren50709602016-10-21 14:46:47 -06001727 { }
1728};
1729
1730U_BOOT_DRIVER(eth_eqos) = {
1731 .name = "eth_eqos",
1732 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001733 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001734 .probe = eqos_probe,
1735 .remove = eqos_remove,
1736 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001737 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001738 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001739};