blob: 8e29f7b6c8f6d7467aa42cbe31f82c175d626c35 [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
Marek Vasut89077732021-01-07 11:12:16 +01001197 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001198 eqos->config->ops->eqos_inval_desc(rx_desc);
Jonas Karlmane2c45462023-10-01 19:17:18 +00001199 if (rx_desc->des3 & EQOS_DESC3_OWN)
Stephen Warren50709602016-10-21 14:46:47 -06001200 return -EAGAIN;
Jonas Karlmane2c45462023-10-01 19:17:18 +00001201
1202 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
Stephen Warren50709602016-10-21 14:46:47 -06001203
1204 *packetp = eqos->rx_dma_buf +
1205 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1206 length = rx_desc->des3 & 0x7fff;
1207 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1208
Christophe Roullier6beb7802019-05-17 15:08:44 +02001209 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001210
1211 return length;
1212}
1213
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001214static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001215{
1216 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001217 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001218 uchar *packet_expected;
1219 struct eqos_desc *rx_desc;
1220
1221 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1222
1223 packet_expected = eqos->rx_dma_buf +
1224 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1225 if (packet != packet_expected) {
1226 debug("%s: Unexpected packet (expected %p)\n", __func__,
1227 packet_expected);
1228 return -EINVAL;
1229 }
1230
Fugang Duan37aae5f2020-05-03 22:41:17 +08001231 eqos->config->ops->eqos_inval_buffer(packet, length);
1232
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001233 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1234 for (idx = eqos->rx_desc_idx - idx_mask;
1235 idx <= eqos->rx_desc_idx;
1236 idx++) {
1237 rx_desc = eqos_get_desc(eqos, idx, true);
1238 rx_desc->des0 = 0;
1239 mb();
1240 eqos->config->ops->eqos_flush_desc(rx_desc);
1241 eqos->config->ops->eqos_inval_buffer(packet, length);
1242 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1243 (idx * EQOS_MAX_PACKET_SIZE));
1244 rx_desc->des1 = 0;
1245 rx_desc->des2 = 0;
1246 /*
1247 * Make sure that if HW sees the _OWN write below,
1248 * it will see all the writes to the rest of the
1249 * descriptor too.
1250 */
1251 mb();
1252 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1253 eqos->config->ops->eqos_flush_desc(rx_desc);
1254 }
1255 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1256 }
Stephen Warren50709602016-10-21 14:46:47 -06001257
1258 eqos->rx_desc_idx++;
1259 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1260
1261 return 0;
1262}
1263
1264static int eqos_probe_resources_core(struct udevice *dev)
1265{
1266 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001267 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001268 int ret;
1269
1270 debug("%s(dev=%p):\n", __func__, dev);
1271
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001272 /* Maximum distance between neighboring descriptors, in Bytes. */
1273 desc_step = sizeof(struct eqos_desc) +
1274 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1275 if (desc_step < ARCH_DMA_MINALIGN) {
1276 /*
1277 * The EQoS hardware implementation cannot place one descriptor
1278 * per cacheline, it is necessary to place multiple descriptors
1279 * per cacheline in memory and do cache management carefully.
1280 */
1281 eqos->desc_size = BIT(fls(desc_step) - 1);
1282 } else {
1283 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1284 (unsigned int)ARCH_DMA_MINALIGN);
1285 }
1286 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001287
1288 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1289 if (!eqos->tx_descs) {
1290 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001291 ret = -ENOMEM;
1292 goto err;
1293 }
Stephen Warren50709602016-10-21 14:46:47 -06001294
Marek Vasut90cc13a2022-10-09 17:51:45 +02001295 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1296 if (!eqos->rx_descs) {
1297 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1298 ret = -ENOMEM;
1299 goto err_free_tx_descs;
1300 }
1301
Stephen Warren50709602016-10-21 14:46:47 -06001302 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1303 if (!eqos->tx_dma_buf) {
1304 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1305 ret = -ENOMEM;
1306 goto err_free_descs;
1307 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001308 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001309
1310 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1311 if (!eqos->rx_dma_buf) {
1312 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1313 ret = -ENOMEM;
1314 goto err_free_tx_dma_buf;
1315 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001316 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001317
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001318 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1319 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1320
Stephen Warren50709602016-10-21 14:46:47 -06001321 debug("%s: OK\n", __func__);
1322 return 0;
1323
Stephen Warren50709602016-10-21 14:46:47 -06001324err_free_tx_dma_buf:
1325 free(eqos->tx_dma_buf);
1326err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001327 eqos_free_descs(eqos->rx_descs);
1328err_free_tx_descs:
1329 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001330err:
1331
1332 debug("%s: returns %d\n", __func__, ret);
1333 return ret;
1334}
1335
1336static int eqos_remove_resources_core(struct udevice *dev)
1337{
1338 struct eqos_priv *eqos = dev_get_priv(dev);
1339
1340 debug("%s(dev=%p):\n", __func__, dev);
1341
Stephen Warren50709602016-10-21 14:46:47 -06001342 free(eqos->rx_dma_buf);
1343 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001344 eqos_free_descs(eqos->rx_descs);
1345 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001346
1347 debug("%s: OK\n", __func__);
1348 return 0;
1349}
1350
1351static int eqos_probe_resources_tegra186(struct udevice *dev)
1352{
1353 struct eqos_priv *eqos = dev_get_priv(dev);
1354 int ret;
1355
1356 debug("%s(dev=%p):\n", __func__, dev);
1357
1358 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1359 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001360 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001361 return ret;
1362 }
1363
1364 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1365 &eqos->phy_reset_gpio,
1366 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1367 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001368 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001369 goto err_free_reset_eqos;
1370 }
1371
1372 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1373 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001374 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001375 goto err_free_gpio_phy_reset;
1376 }
1377
1378 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1379 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001380 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001381 goto err_free_clk_slave_bus;
1382 }
1383
1384 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1385 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001386 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001387 goto err_free_clk_master_bus;
1388 }
1389
1390 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1391 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001392 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001393 goto err_free_clk_rx;
Stephen Warren50709602016-10-21 14:46:47 -06001394 }
1395
1396 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1397 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001398 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001399 goto err_free_clk_ptp_ref;
1400 }
1401
1402 debug("%s: OK\n", __func__);
1403 return 0;
1404
1405err_free_clk_ptp_ref:
1406 clk_free(&eqos->clk_ptp_ref);
1407err_free_clk_rx:
1408 clk_free(&eqos->clk_rx);
1409err_free_clk_master_bus:
1410 clk_free(&eqos->clk_master_bus);
1411err_free_clk_slave_bus:
1412 clk_free(&eqos->clk_slave_bus);
1413err_free_gpio_phy_reset:
1414 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1415err_free_reset_eqos:
1416 reset_free(&eqos->reset_ctl);
1417
1418 debug("%s: returns %d\n", __func__, ret);
1419 return ret;
1420}
1421
Christophe Roullier6beb7802019-05-17 15:08:44 +02001422static 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{
Marek Vasut006ab6b2023-03-06 15:53:44 +01001504 struct eqos_priv * __maybe_unused eqos = dev_get_priv(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001505
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
1516 debug("%s: OK\n", __func__);
1517 return 0;
1518}
1519
Stephen Warren50709602016-10-21 14:46:47 -06001520static int eqos_probe(struct udevice *dev)
1521{
1522 struct eqos_priv *eqos = dev_get_priv(dev);
1523 int ret;
1524
1525 debug("%s(dev=%p):\n", __func__, dev);
1526
1527 eqos->dev = dev;
1528 eqos->config = (void *)dev_get_driver_data(dev);
1529
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001530 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001531 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001532 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001533 return -ENODEV;
1534 }
1535 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1536 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1537 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1538 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1539
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001540 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1541
Stephen Warren50709602016-10-21 14:46:47 -06001542 ret = eqos_probe_resources_core(dev);
1543 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001544 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001545 return ret;
1546 }
1547
Christophe Roullier6beb7802019-05-17 15:08:44 +02001548 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001549 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001550 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001551 goto err_remove_resources_core;
1552 }
1553
Marek Vasut30b28c42021-11-13 03:23:52 +01001554 ret = eqos->config->ops->eqos_start_clks(dev);
1555 if (ret < 0) {
1556 pr_err("eqos_start_clks() failed: %d", ret);
1557 goto err_remove_resources_tegra;
1558 }
1559
Ye Liad122b72020-05-03 22:41:15 +08001560#ifdef CONFIG_DM_ETH_PHY
1561 eqos->mii = eth_phy_get_mdio_bus(dev);
1562#endif
Stephen Warren50709602016-10-21 14:46:47 -06001563 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001564 eqos->mii = mdio_alloc();
1565 if (!eqos->mii) {
1566 pr_err("mdio_alloc() failed");
1567 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001568 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001569 }
1570 eqos->mii->read = eqos_mdio_read;
1571 eqos->mii->write = eqos_mdio_write;
1572 eqos->mii->priv = eqos;
1573 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001574
Ye Liad122b72020-05-03 22:41:15 +08001575 ret = mdio_register(eqos->mii);
1576 if (ret < 0) {
1577 pr_err("mdio_register() failed: %d", ret);
1578 goto err_free_mdio;
1579 }
Stephen Warren50709602016-10-21 14:46:47 -06001580 }
1581
Ye Liad122b72020-05-03 22:41:15 +08001582#ifdef CONFIG_DM_ETH_PHY
1583 eth_phy_set_mdio_bus(dev, eqos->mii);
1584#endif
1585
Stephen Warren50709602016-10-21 14:46:47 -06001586 debug("%s: OK\n", __func__);
1587 return 0;
1588
1589err_free_mdio:
1590 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001591err_stop_clks:
1592 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001593err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001594 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001595err_remove_resources_core:
1596 eqos_remove_resources_core(dev);
1597
1598 debug("%s: returns %d\n", __func__, ret);
1599 return ret;
1600}
1601
1602static int eqos_remove(struct udevice *dev)
1603{
1604 struct eqos_priv *eqos = dev_get_priv(dev);
1605
1606 debug("%s(dev=%p):\n", __func__, dev);
1607
1608 mdio_unregister(eqos->mii);
1609 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001610 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001611 eqos->config->ops->eqos_remove_resources(dev);
1612
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001613 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001614
1615 debug("%s: OK\n", __func__);
1616 return 0;
1617}
1618
Peng Fanc0a59952022-07-26 16:41:14 +08001619int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001620{
1621 return 0;
1622}
1623
Stephen Warren50709602016-10-21 14:46:47 -06001624static const struct eth_ops eqos_ops = {
1625 .start = eqos_start,
1626 .stop = eqos_stop,
1627 .send = eqos_send,
1628 .recv = eqos_recv,
1629 .free_pkt = eqos_free_pkt,
1630 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001631 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001632};
1633
Christophe Roullier6beb7802019-05-17 15:08:44 +02001634static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001635 .eqos_inval_desc = eqos_inval_desc_generic,
1636 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001637 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1638 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1639 .eqos_probe_resources = eqos_probe_resources_tegra186,
1640 .eqos_remove_resources = eqos_remove_resources_tegra186,
1641 .eqos_stop_resets = eqos_stop_resets_tegra186,
1642 .eqos_start_resets = eqos_start_resets_tegra186,
1643 .eqos_stop_clks = eqos_stop_clks_tegra186,
1644 .eqos_start_clks = eqos_start_clks_tegra186,
1645 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1646 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1647 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001648 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001649 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1650};
1651
Patrick Delaunay68083902020-06-08 11:27:19 +02001652static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001653 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001654 .mdio_wait = 10,
1655 .swr_wait = 10,
1656 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1657 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001658 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001659 .interface = eqos_get_interface_tegra186,
1660 .ops = &eqos_tegra186_ops
1661};
1662
1663static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08001664 .eqos_inval_desc = eqos_inval_desc_generic,
1665 .eqos_flush_desc = eqos_flush_desc_generic,
1666 .eqos_inval_buffer = eqos_inval_buffer_generic,
1667 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001668 .eqos_probe_resources = eqos_probe_resources_stm32,
1669 .eqos_remove_resources = eqos_remove_resources_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001670 .eqos_stop_resets = eqos_null_ops,
1671 .eqos_start_resets = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001672 .eqos_stop_clks = eqos_stop_clks_stm32,
1673 .eqos_start_clks = eqos_start_clks_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001674 .eqos_calibrate_pads = eqos_null_ops,
1675 .eqos_disable_calibration = eqos_null_ops,
1676 .eqos_set_tx_clk_speed = eqos_null_ops,
Patrice Chotardd9824432022-08-02 10:55:26 +02001677 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001678 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1679};
1680
Patrick Delaunay68083902020-06-08 11:27:19 +02001681static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001682 .reg_access_always_ok = false,
1683 .mdio_wait = 10000,
1684 .swr_wait = 50,
1685 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1686 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01001687 .axi_bus_width = EQOS_AXI_WIDTH_64,
Marek BehĂșnbc194772022-04-07 00:33:01 +02001688 .interface = dev_read_phy_mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001689 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001690};
1691
1692static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001693#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001694 {
1695 .compatible = "nvidia,tegra186-eqos",
1696 .data = (ulong)&eqos_tegra186_config
1697 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001698#endif
1699#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001700 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001701 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02001702 .data = (ulong)&eqos_stm32_config
1703 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001704#endif
1705#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001706 {
Marek Vasut7af11382022-02-26 04:36:37 +01001707 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001708 .data = (ulong)&eqos_imx_config
1709 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001710#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001711
Sumit Garg7c3be942023-02-01 19:28:55 +05301712#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1713 {
1714 .compatible = "qcom,qcs404-ethqos",
1715 .data = (ulong)&eqos_qcom_config
1716 },
1717#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001718#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1719 {
1720 .compatible = "starfive,jh7110-dwmac",
1721 .data = (ulong)&eqos_jh7110_config
1722 },
1723#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301724
Stephen Warren50709602016-10-21 14:46:47 -06001725 { }
1726};
1727
1728U_BOOT_DRIVER(eth_eqos) = {
1729 .name = "eth_eqos",
1730 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001731 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001732 .probe = eqos_probe,
1733 .remove = eqos_remove,
1734 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001735 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001736 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001737};