blob: c1f2391d6354064a6c8c9aaf30ef1c66b4f15b69 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060037#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070038#include <malloc.h>
Stephen Warren50709602016-10-21 14:46:47 -060039#include <memalign.h>
40#include <miiphy.h>
41#include <net.h>
42#include <netdev.h>
43#include <phy.h>
44#include <reset.h>
45#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060046#include <asm/cache.h>
Stephen Warren50709602016-10-21 14:46:47 -060047#include <asm/gpio.h>
48#include <asm/io.h>
Ye Liad122b72020-05-03 22:41:15 +080049#include <eth_phy.h>
Fugang Duandd455e62020-05-03 22:41:18 +080050#ifdef CONFIG_ARCH_IMX8M
51#include <asm/arch/clock.h>
52#include <asm/mach-imx/sys_proto.h>
53#endif
Simon Glassdbd79542020-05-10 11:40:11 -060054#include <linux/delay.h>
Stephen Warren50709602016-10-21 14:46:47 -060055
Peng Fanc0a59952022-07-26 16:41:14 +080056#include "dwc_eth_qos.h"
Stephen Warren50709602016-10-21 14:46:47 -060057
58/*
59 * TX and RX descriptors are 16 bytes. This causes problems with the cache
60 * maintenance on CPUs where the cache-line size exceeds the size of these
61 * descriptors. What will happen is that when the driver receives a packet
62 * it will be immediately requeued for the hardware to reuse. The CPU will
63 * therefore need to flush the cache-line containing the descriptor, which
64 * will cause all other descriptors in the same cache-line to be flushed
65 * along with it. If one of those descriptors had been written to by the
66 * device those changes (and the associated packet) will be lost.
67 *
68 * To work around this, we make use of non-cached memory if available. If
69 * descriptors are mapped uncached there's no need to manually flush them
70 * or invalidate them.
71 *
72 * Note that this only applies to descriptors. The packet data buffers do
73 * not have the same constraints since they are 1536 bytes large, so they
74 * are unlikely to share cache-lines.
75 */
Marek Vasut89077732021-01-07 11:12:16 +010076static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -060077{
Marek Vasut89077732021-01-07 11:12:16 +010078 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
79 (unsigned int)ARCH_DMA_MINALIGN);
80
81 return memalign(eqos->desc_size, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060082}
83
84static void eqos_free_descs(void *descs)
85{
Stephen Warren50709602016-10-21 14:46:47 -060086 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -060087}
88
Marek Vasut89077732021-01-07 11:12:16 +010089static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
90 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -060091{
Marek Vasut89077732021-01-07 11:12:16 +010092 return eqos->descs +
93 ((rx ? EQOS_DESCRIPTORS_TX : 0) + num) * eqos->desc_size;
Stephen Warren50709602016-10-21 14:46:47 -060094}
95
Peng Fanc0a59952022-07-26 16:41:14 +080096void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -060097{
Marek Vasut89077732021-01-07 11:12:16 +010098 unsigned long start = (unsigned long)desc;
99 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
100 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200101
102 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600103}
104
Peng Fanc0a59952022-07-26 16:41:14 +0800105void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200106{
Marek Vasut89077732021-01-07 11:12:16 +0100107 unsigned long start = (unsigned long)desc;
108 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
109 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200110
111 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200112}
113
Peng Fanc0a59952022-07-26 16:41:14 +0800114void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600115{
116 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
117 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
118
119 invalidate_dcache_range(start, end);
120}
121
Peng Fanc0a59952022-07-26 16:41:14 +0800122void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200123{
124 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
125 unsigned long end = roundup((unsigned long)buf + size,
126 ARCH_DMA_MINALIGN);
127
128 invalidate_dcache_range(start, end);
129}
130
131static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600132{
133 flush_cache((unsigned long)buf, size);
134}
135
Peng Fanc0a59952022-07-26 16:41:14 +0800136void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200137{
138 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
139 unsigned long end = roundup((unsigned long)buf + size,
140 ARCH_DMA_MINALIGN);
141
142 flush_dcache_range(start, end);
143}
144
Stephen Warren50709602016-10-21 14:46:47 -0600145static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
146{
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100147 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
148 EQOS_MAC_MDIO_ADDRESS_GB, false,
149 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600150}
151
152static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
153 int mdio_reg)
154{
155 struct eqos_priv *eqos = bus->priv;
156 u32 val;
157 int ret;
158
159 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
160 mdio_reg);
161
162 ret = eqos_mdio_wait_idle(eqos);
163 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900164 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600165 return ret;
166 }
167
168 val = readl(&eqos->mac_regs->mdio_address);
169 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
170 EQOS_MAC_MDIO_ADDRESS_C45E;
171 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
172 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200173 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600174 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
175 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
176 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
177 EQOS_MAC_MDIO_ADDRESS_GB;
178 writel(val, &eqos->mac_regs->mdio_address);
179
Christophe Roullier6beb7802019-05-17 15:08:44 +0200180 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600181
182 ret = eqos_mdio_wait_idle(eqos);
183 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900184 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600185 return ret;
186 }
187
188 val = readl(&eqos->mac_regs->mdio_data);
189 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
190
191 debug("%s: val=%x\n", __func__, val);
192
193 return val;
194}
195
196static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
197 int mdio_reg, u16 mdio_val)
198{
199 struct eqos_priv *eqos = bus->priv;
200 u32 val;
201 int ret;
202
203 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
204 mdio_addr, mdio_reg, mdio_val);
205
206 ret = eqos_mdio_wait_idle(eqos);
207 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900208 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600209 return ret;
210 }
211
212 writel(mdio_val, &eqos->mac_regs->mdio_data);
213
214 val = readl(&eqos->mac_regs->mdio_address);
215 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
216 EQOS_MAC_MDIO_ADDRESS_C45E;
217 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
218 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200219 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600220 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
221 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
222 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
223 EQOS_MAC_MDIO_ADDRESS_GB;
224 writel(val, &eqos->mac_regs->mdio_address);
225
Christophe Roullier6beb7802019-05-17 15:08:44 +0200226 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600227
228 ret = eqos_mdio_wait_idle(eqos);
229 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900230 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600231 return ret;
232 }
233
234 return 0;
235}
236
237static int eqos_start_clks_tegra186(struct udevice *dev)
238{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800239#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600240 struct eqos_priv *eqos = dev_get_priv(dev);
241 int ret;
242
243 debug("%s(dev=%p):\n", __func__, dev);
244
245 ret = clk_enable(&eqos->clk_slave_bus);
246 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900247 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600248 goto err;
249 }
250
251 ret = clk_enable(&eqos->clk_master_bus);
252 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900253 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600254 goto err_disable_clk_slave_bus;
255 }
256
257 ret = clk_enable(&eqos->clk_rx);
258 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900259 pr_err("clk_enable(clk_rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600260 goto err_disable_clk_master_bus;
261 }
262
263 ret = clk_enable(&eqos->clk_ptp_ref);
264 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900265 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600266 goto err_disable_clk_rx;
267 }
268
269 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
270 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900271 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600272 goto err_disable_clk_ptp_ref;
273 }
274
275 ret = clk_enable(&eqos->clk_tx);
276 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900277 pr_err("clk_enable(clk_tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600278 goto err_disable_clk_ptp_ref;
279 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800280#endif
Stephen Warren50709602016-10-21 14:46:47 -0600281
282 debug("%s: OK\n", __func__);
283 return 0;
284
Fugang Duan37aae5f2020-05-03 22:41:17 +0800285#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600286err_disable_clk_ptp_ref:
287 clk_disable(&eqos->clk_ptp_ref);
288err_disable_clk_rx:
289 clk_disable(&eqos->clk_rx);
290err_disable_clk_master_bus:
291 clk_disable(&eqos->clk_master_bus);
292err_disable_clk_slave_bus:
293 clk_disable(&eqos->clk_slave_bus);
294err:
295 debug("%s: FAILED: %d\n", __func__, ret);
296 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800297#endif
Stephen Warren50709602016-10-21 14:46:47 -0600298}
299
Christophe Roullier6beb7802019-05-17 15:08:44 +0200300static int eqos_start_clks_stm32(struct udevice *dev)
301{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800302#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200303 struct eqos_priv *eqos = dev_get_priv(dev);
304 int ret;
305
306 debug("%s(dev=%p):\n", __func__, dev);
307
308 ret = clk_enable(&eqos->clk_master_bus);
309 if (ret < 0) {
310 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
311 goto err;
312 }
313
314 ret = clk_enable(&eqos->clk_rx);
315 if (ret < 0) {
316 pr_err("clk_enable(clk_rx) failed: %d", ret);
317 goto err_disable_clk_master_bus;
318 }
319
320 ret = clk_enable(&eqos->clk_tx);
321 if (ret < 0) {
322 pr_err("clk_enable(clk_tx) failed: %d", ret);
323 goto err_disable_clk_rx;
324 }
325
Daniil Stas81597922021-05-23 22:24:48 +0000326 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200327 ret = clk_enable(&eqos->clk_ck);
328 if (ret < 0) {
329 pr_err("clk_enable(clk_ck) failed: %d", ret);
330 goto err_disable_clk_tx;
331 }
Daniil Stas81597922021-05-23 22:24:48 +0000332 eqos->clk_ck_enabled = true;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200333 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800334#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200335
336 debug("%s: OK\n", __func__);
337 return 0;
338
Fugang Duan37aae5f2020-05-03 22:41:17 +0800339#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200340err_disable_clk_tx:
341 clk_disable(&eqos->clk_tx);
342err_disable_clk_rx:
343 clk_disable(&eqos->clk_rx);
344err_disable_clk_master_bus:
345 clk_disable(&eqos->clk_master_bus);
346err:
347 debug("%s: FAILED: %d\n", __func__, ret);
348 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800349#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200350}
351
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200352static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600353{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800354#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600355 struct eqos_priv *eqos = dev_get_priv(dev);
356
357 debug("%s(dev=%p):\n", __func__, dev);
358
359 clk_disable(&eqos->clk_tx);
360 clk_disable(&eqos->clk_ptp_ref);
361 clk_disable(&eqos->clk_rx);
362 clk_disable(&eqos->clk_master_bus);
363 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800364#endif
Stephen Warren50709602016-10-21 14:46:47 -0600365
366 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200367 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600368}
369
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200370static int eqos_stop_clks_stm32(struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200371{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800372#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200373 struct eqos_priv *eqos = dev_get_priv(dev);
374
375 debug("%s(dev=%p):\n", __func__, dev);
376
377 clk_disable(&eqos->clk_tx);
378 clk_disable(&eqos->clk_rx);
379 clk_disable(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800380#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200381
382 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200383 return 0;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800384}
385
Stephen Warren50709602016-10-21 14:46:47 -0600386static int eqos_start_resets_tegra186(struct udevice *dev)
387{
388 struct eqos_priv *eqos = dev_get_priv(dev);
389 int ret;
390
391 debug("%s(dev=%p):\n", __func__, dev);
392
393 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
394 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900395 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600396 return ret;
397 }
398
399 udelay(2);
400
401 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
402 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900403 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600404 return ret;
405 }
406
407 ret = reset_assert(&eqos->reset_ctl);
408 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900409 pr_err("reset_assert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600410 return ret;
411 }
412
413 udelay(2);
414
415 ret = reset_deassert(&eqos->reset_ctl);
416 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900417 pr_err("reset_deassert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600418 return ret;
419 }
420
421 debug("%s: OK\n", __func__);
422 return 0;
423}
424
425static int eqos_stop_resets_tegra186(struct udevice *dev)
426{
427 struct eqos_priv *eqos = dev_get_priv(dev);
428
429 reset_assert(&eqos->reset_ctl);
430 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
431
Christophe Roullier6beb7802019-05-17 15:08:44 +0200432 return 0;
433}
434
Stephen Warren50709602016-10-21 14:46:47 -0600435static int eqos_calibrate_pads_tegra186(struct udevice *dev)
436{
437 struct eqos_priv *eqos = dev_get_priv(dev);
438 int ret;
439
440 debug("%s(dev=%p):\n", __func__, dev);
441
442 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
443 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
444
445 udelay(1);
446
447 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
448 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
449
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100450 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
451 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600452 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900453 pr_err("calibrate didn't start");
Stephen Warren50709602016-10-21 14:46:47 -0600454 goto failed;
455 }
456
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100457 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
458 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600459 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900460 pr_err("calibrate didn't finish");
Stephen Warren50709602016-10-21 14:46:47 -0600461 goto failed;
462 }
463
464 ret = 0;
465
466failed:
467 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
468 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
469
470 debug("%s: returns %d\n", __func__, ret);
471
472 return ret;
473}
474
475static int eqos_disable_calibration_tegra186(struct udevice *dev)
476{
477 struct eqos_priv *eqos = dev_get_priv(dev);
478
479 debug("%s(dev=%p):\n", __func__, dev);
480
481 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
482 EQOS_AUTO_CAL_CONFIG_ENABLE);
483
484 return 0;
485}
486
487static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
488{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800489#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600490 struct eqos_priv *eqos = dev_get_priv(dev);
491
492 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800493#else
494 return 0;
495#endif
Stephen Warren50709602016-10-21 14:46:47 -0600496}
497
Christophe Roullier6beb7802019-05-17 15:08:44 +0200498static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
499{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800500#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200501 struct eqos_priv *eqos = dev_get_priv(dev);
502
503 return clk_get_rate(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800504#else
505 return 0;
506#endif
507}
508
Stephen Warren50709602016-10-21 14:46:47 -0600509static int eqos_set_full_duplex(struct udevice *dev)
510{
511 struct eqos_priv *eqos = dev_get_priv(dev);
512
513 debug("%s(dev=%p):\n", __func__, dev);
514
515 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
516
517 return 0;
518}
519
520static int eqos_set_half_duplex(struct udevice *dev)
521{
522 struct eqos_priv *eqos = dev_get_priv(dev);
523
524 debug("%s(dev=%p):\n", __func__, dev);
525
526 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
527
528 /* WAR: Flush TX queue when switching to half-duplex */
529 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
530 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
531
532 return 0;
533}
534
535static int eqos_set_gmii_speed(struct udevice *dev)
536{
537 struct eqos_priv *eqos = dev_get_priv(dev);
538
539 debug("%s(dev=%p):\n", __func__, dev);
540
541 clrbits_le32(&eqos->mac_regs->configuration,
542 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
543
544 return 0;
545}
546
547static int eqos_set_mii_speed_100(struct udevice *dev)
548{
549 struct eqos_priv *eqos = dev_get_priv(dev);
550
551 debug("%s(dev=%p):\n", __func__, dev);
552
553 setbits_le32(&eqos->mac_regs->configuration,
554 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
555
556 return 0;
557}
558
559static int eqos_set_mii_speed_10(struct udevice *dev)
560{
561 struct eqos_priv *eqos = dev_get_priv(dev);
562
563 debug("%s(dev=%p):\n", __func__, dev);
564
565 clrsetbits_le32(&eqos->mac_regs->configuration,
566 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
567
568 return 0;
569}
570
571static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
572{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800573#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600574 struct eqos_priv *eqos = dev_get_priv(dev);
575 ulong rate;
576 int ret;
577
578 debug("%s(dev=%p):\n", __func__, dev);
579
580 switch (eqos->phy->speed) {
581 case SPEED_1000:
582 rate = 125 * 1000 * 1000;
583 break;
584 case SPEED_100:
585 rate = 25 * 1000 * 1000;
586 break;
587 case SPEED_10:
588 rate = 2.5 * 1000 * 1000;
589 break;
590 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900591 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600592 return -EINVAL;
593 }
594
595 ret = clk_set_rate(&eqos->clk_tx, rate);
596 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900597 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600598 return ret;
599 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800600#endif
Stephen Warren50709602016-10-21 14:46:47 -0600601
602 return 0;
603}
604
605static int eqos_adjust_link(struct udevice *dev)
606{
607 struct eqos_priv *eqos = dev_get_priv(dev);
608 int ret;
609 bool en_calibration;
610
611 debug("%s(dev=%p):\n", __func__, dev);
612
613 if (eqos->phy->duplex)
614 ret = eqos_set_full_duplex(dev);
615 else
616 ret = eqos_set_half_duplex(dev);
617 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900618 pr_err("eqos_set_*_duplex() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600619 return ret;
620 }
621
622 switch (eqos->phy->speed) {
623 case SPEED_1000:
624 en_calibration = true;
625 ret = eqos_set_gmii_speed(dev);
626 break;
627 case SPEED_100:
628 en_calibration = true;
629 ret = eqos_set_mii_speed_100(dev);
630 break;
631 case SPEED_10:
632 en_calibration = false;
633 ret = eqos_set_mii_speed_10(dev);
634 break;
635 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900636 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600637 return -EINVAL;
638 }
639 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900640 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600641 return ret;
642 }
643
644 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200645 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600646 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200647 pr_err("eqos_calibrate_pads() failed: %d",
648 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600649 return ret;
650 }
651 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200652 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600653 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200654 pr_err("eqos_disable_calibration() failed: %d",
655 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600656 return ret;
657 }
658 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200659 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600660 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200661 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600662 return ret;
663 }
664
665 return 0;
666}
667
668static int eqos_write_hwaddr(struct udevice *dev)
669{
Simon Glassfa20e932020-12-03 16:55:20 -0700670 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600671 struct eqos_priv *eqos = dev_get_priv(dev);
672 uint32_t val;
673
674 /*
675 * This function may be called before start() or after stop(). At that
676 * time, on at least some configurations of the EQoS HW, all clocks to
677 * the EQoS HW block will be stopped, and a reset signal applied. If
678 * any register access is attempted in this state, bus timeouts or CPU
679 * hangs may occur. This check prevents that.
680 *
681 * A simple solution to this problem would be to not implement
682 * write_hwaddr(), since start() always writes the MAC address into HW
683 * anyway. However, it is desirable to implement write_hwaddr() to
684 * support the case of SW that runs subsequent to U-Boot which expects
685 * the MAC address to already be programmed into the EQoS registers,
686 * which must happen irrespective of whether the U-Boot user (or
687 * scripts) actually made use of the EQoS device, and hence
688 * irrespective of whether start() was ever called.
689 *
690 * Note that this requirement by subsequent SW is not valid for
691 * Tegra186, and is likely not valid for any non-PCI instantiation of
692 * the EQoS HW block. This function is implemented solely as
693 * future-proofing with the expectation the driver will eventually be
694 * ported to some system where the expectation above is true.
695 */
696 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
697 return 0;
698
699 /* Update the MAC address */
700 val = (plat->enetaddr[5] << 8) |
701 (plat->enetaddr[4]);
702 writel(val, &eqos->mac_regs->address0_high);
703 val = (plat->enetaddr[3] << 24) |
704 (plat->enetaddr[2] << 16) |
705 (plat->enetaddr[1] << 8) |
706 (plat->enetaddr[0]);
707 writel(val, &eqos->mac_regs->address0_low);
708
709 return 0;
710}
711
Ye Li3fb1a0e2020-05-03 22:41:20 +0800712static int eqos_read_rom_hwaddr(struct udevice *dev)
713{
Simon Glassfa20e932020-12-03 16:55:20 -0700714 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800715 struct eqos_priv *eqos = dev_get_priv(dev);
716 int ret;
717
718 ret = eqos->config->ops->eqos_get_enetaddr(dev);
719 if (ret < 0)
720 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800721
Ye Li3fb1a0e2020-05-03 22:41:20 +0800722 return !is_valid_ethaddr(pdata->enetaddr);
723}
724
Ye Li2f2aa482022-07-26 16:41:16 +0800725static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
726{
727 struct ofnode_phandle_args phandle_args;
728 int reg;
729
730 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
731 &phandle_args)) {
732 debug("Failed to find phy-handle");
733 return -ENODEV;
734 }
735
736 priv->phy_of_node = phandle_args.node;
737
738 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
739
740 return reg;
741}
742
Stephen Warren50709602016-10-21 14:46:47 -0600743static int eqos_start(struct udevice *dev)
744{
745 struct eqos_priv *eqos = dev_get_priv(dev);
746 int ret, i;
747 ulong rate;
748 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
749 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100750 ulong desc_pad;
Stephen Warren50709602016-10-21 14:46:47 -0600751
752 debug("%s(dev=%p):\n", __func__, dev);
753
754 eqos->tx_desc_idx = 0;
755 eqos->rx_desc_idx = 0;
756
Christophe Roullier6beb7802019-05-17 15:08:44 +0200757 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600758 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200759 pr_err("eqos_start_resets() failed: %d", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100760 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600761 }
762
763 udelay(10);
764
765 eqos->reg_access_ok = true;
766
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100767 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200768 EQOS_DMA_MODE_SWR, false,
769 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600770 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900771 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -0600772 goto err_stop_resets;
773 }
774
Christophe Roullier6beb7802019-05-17 15:08:44 +0200775 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600776 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200777 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600778 goto err_stop_resets;
779 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200780 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600781
Stephen Warren50709602016-10-21 14:46:47 -0600782 val = (rate / 1000000) - 1;
783 writel(val, &eqos->mac_regs->us_tic_counter);
784
Christophe Roullier6beb7802019-05-17 15:08:44 +0200785 /*
786 * if PHY was already connected and configured,
787 * don't need to reconnect/reconfigure again
788 */
Stephen Warren50709602016-10-21 14:46:47 -0600789 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800790 int addr = -1;
Ye Li2f2aa482022-07-26 16:41:16 +0800791 addr = eqos_get_phy_addr(eqos, dev);
Ye Liad122b72020-05-03 22:41:15 +0800792#ifdef DWC_NET_PHYADDR
793 addr = DWC_NET_PHYADDR;
794#endif
795 eqos->phy = phy_connect(eqos->mii, addr, dev,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200796 eqos->config->interface(dev));
797 if (!eqos->phy) {
798 pr_err("phy_connect() failed");
799 goto err_stop_resets;
800 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100801
802 if (eqos->max_speed) {
803 ret = phy_set_supported(eqos->phy, eqos->max_speed);
804 if (ret) {
805 pr_err("phy_set_supported() failed: %d", ret);
806 goto err_shutdown_phy;
807 }
808 }
809
Ye Li2f2aa482022-07-26 16:41:16 +0800810 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200811 ret = phy_config(eqos->phy);
812 if (ret < 0) {
813 pr_err("phy_config() failed: %d", ret);
814 goto err_shutdown_phy;
815 }
Stephen Warren50709602016-10-21 14:46:47 -0600816 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200817
Stephen Warren50709602016-10-21 14:46:47 -0600818 ret = phy_startup(eqos->phy);
819 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900820 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600821 goto err_shutdown_phy;
822 }
823
824 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900825 pr_err("No link");
Stephen Warren50709602016-10-21 14:46:47 -0600826 goto err_shutdown_phy;
827 }
828
829 ret = eqos_adjust_link(dev);
830 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900831 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600832 goto err_shutdown_phy;
833 }
834
835 /* Configure MTL */
836
837 /* Enable Store and Forward mode for TX */
838 /* Program Tx operating mode */
839 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
840 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
841 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
842 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
843
844 /* Transmit Queue weight */
845 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
846
847 /* Enable Store and Forward mode for RX, since no jumbo frame */
848 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000849 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600850
851 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
852 val = readl(&eqos->mac_regs->hw_feature1);
853 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
854 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
855 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
856 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
857
858 /*
859 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
860 * r/tqs is encoded as (n / 256) - 1.
861 */
862 tqs = (128 << tx_fifo_sz) / 256 - 1;
863 rqs = (128 << rx_fifo_sz) / 256 - 1;
864
865 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
866 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
867 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
868 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
869 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
870 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
871 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
872 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
873
874 /* Flow control used only if each channel gets 4KB or more FIFO */
875 if (rqs >= ((4096 / 256) - 1)) {
876 u32 rfd, rfa;
877
878 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
879 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
880
881 /*
882 * Set Threshold for Activating Flow Contol space for min 2
883 * frames ie, (1500 * 1) = 1500 bytes.
884 *
885 * Set Threshold for Deactivating Flow Contol for space of
886 * min 1 frame (frame size 1500bytes) in receive fifo
887 */
888 if (rqs == ((4096 / 256) - 1)) {
889 /*
890 * This violates the above formula because of FIFO size
891 * limit therefore overflow may occur inspite of this.
892 */
893 rfd = 0x3; /* Full-3K */
894 rfa = 0x1; /* Full-1.5K */
895 } else if (rqs == ((8192 / 256) - 1)) {
896 rfd = 0x6; /* Full-4K */
897 rfa = 0xa; /* Full-6K */
898 } else if (rqs == ((16384 / 256) - 1)) {
899 rfd = 0x6; /* Full-4K */
900 rfa = 0x12; /* Full-10K */
901 } else {
902 rfd = 0x6; /* Full-4K */
903 rfa = 0x1E; /* Full-16K */
904 }
905
906 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
907 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
908 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
909 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
910 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
911 (rfd <<
912 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
913 (rfa <<
914 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
915 }
916
917 /* Configure MAC */
918
919 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
920 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
921 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200922 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600923 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
924
Fugang Duan37aae5f2020-05-03 22:41:17 +0800925 /* Multicast and Broadcast Queue Enable */
926 setbits_le32(&eqos->mac_regs->unused_0a4,
927 0x00100000);
928 /* enable promise mode */
929 setbits_le32(&eqos->mac_regs->unused_004[1],
930 0x1);
931
Stephen Warren50709602016-10-21 14:46:47 -0600932 /* Set TX flow control parameters */
933 /* Set Pause Time */
934 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
935 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
936 /* Assign priority for TX flow control */
937 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
938 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
939 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
940 /* Assign priority for RX flow control */
941 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
942 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
943 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
944 /* Enable flow control */
945 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
946 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
947 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
948 EQOS_MAC_RX_FLOW_CTRL_RFE);
949
950 clrsetbits_le32(&eqos->mac_regs->configuration,
951 EQOS_MAC_CONFIGURATION_GPSLCE |
952 EQOS_MAC_CONFIGURATION_WD |
953 EQOS_MAC_CONFIGURATION_JD |
954 EQOS_MAC_CONFIGURATION_JE,
955 EQOS_MAC_CONFIGURATION_CST |
956 EQOS_MAC_CONFIGURATION_ACS);
957
958 eqos_write_hwaddr(dev);
959
960 /* Configure DMA */
961
962 /* Enable OSP mode */
963 setbits_le32(&eqos->dma_regs->ch0_tx_control,
964 EQOS_DMA_CH0_TX_CONTROL_OSP);
965
966 /* RX buffer size. Must be a multiple of bus width */
967 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
968 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
969 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
970 EQOS_MAX_PACKET_SIZE <<
971 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
972
Marek Vasut89077732021-01-07 11:12:16 +0100973 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
974 eqos->config->axi_bus_width;
975
Stephen Warren50709602016-10-21 14:46:47 -0600976 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +0100977 EQOS_DMA_CH0_CONTROL_PBLX8 |
978 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -0600979
980 /*
981 * Burst length must be < 1/2 FIFO size.
982 * FIFO size in tqs is encoded as (n / 256) - 1.
983 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
984 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
985 */
986 pbl = tqs + 1;
987 if (pbl > 32)
988 pbl = 32;
989 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
990 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
991 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
992 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
993
994 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
995 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
996 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
997 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
998
999 /* DMA performance configuration */
1000 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1001 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1002 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1003 writel(val, &eqos->dma_regs->sysbus_mode);
1004
1005 /* Set up descriptors */
1006
Marek Vasut89077732021-01-07 11:12:16 +01001007 memset(eqos->descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_NUM);
1008
1009 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
1010 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
1011 eqos->config->ops->eqos_flush_desc(tx_desc);
1012 }
1013
Stephen Warren50709602016-10-21 14:46:47 -06001014 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +01001015 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Stephen Warren50709602016-10-21 14:46:47 -06001016 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1017 (i * EQOS_MAX_PACKET_SIZE));
Marek Vasutd54c98e2020-03-23 02:02:57 +01001018 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001019 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +01001020 eqos->config->ops->eqos_flush_desc(rx_desc);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001021 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf +
1022 (i * EQOS_MAX_PACKET_SIZE),
1023 EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -06001024 }
Stephen Warren50709602016-10-21 14:46:47 -06001025
1026 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001027 writel((ulong)eqos_get_desc(eqos, 0, false),
1028 &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001029 writel(EQOS_DESCRIPTORS_TX - 1,
1030 &eqos->dma_regs->ch0_txdesc_ring_length);
1031
1032 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001033 writel((ulong)eqos_get_desc(eqos, 0, true),
1034 &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001035 writel(EQOS_DESCRIPTORS_RX - 1,
1036 &eqos->dma_regs->ch0_rxdesc_ring_length);
1037
1038 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001039 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1040 EQOS_DMA_CH0_TX_CONTROL_ST);
1041 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1042 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001043 setbits_le32(&eqos->mac_regs->configuration,
1044 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001045
1046 /* TX tail pointer not written until we need to TX a packet */
1047 /*
1048 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1049 * first descriptor, implying all descriptors were available. However,
1050 * that's not distinguishable from none of the descriptors being
1051 * available.
1052 */
Marek Vasut89077732021-01-07 11:12:16 +01001053 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001054 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1055
1056 eqos->started = true;
1057
1058 debug("%s: OK\n", __func__);
1059 return 0;
1060
1061err_shutdown_phy:
1062 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001063err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001064 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001065err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001066 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001067 return ret;
1068}
1069
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001070static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001071{
1072 struct eqos_priv *eqos = dev_get_priv(dev);
1073 int i;
1074
1075 debug("%s(dev=%p):\n", __func__, dev);
1076
1077 if (!eqos->started)
1078 return;
1079 eqos->started = false;
1080 eqos->reg_access_ok = false;
1081
1082 /* Disable TX DMA */
1083 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1084 EQOS_DMA_CH0_TX_CONTROL_ST);
1085
1086 /* Wait for TX all packets to drain out of MTL */
1087 for (i = 0; i < 1000000; i++) {
1088 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1089 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1090 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1091 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1092 if ((trcsts != 1) && (!txqsts))
1093 break;
1094 }
1095
1096 /* Turn off MAC TX and RX */
1097 clrbits_le32(&eqos->mac_regs->configuration,
1098 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1099
1100 /* Wait for all RX packets to drain out of MTL */
1101 for (i = 0; i < 1000000; i++) {
1102 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1103 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1104 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1105 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1106 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1107 if ((!prxq) && (!rxqsts))
1108 break;
1109 }
1110
1111 /* Turn off RX DMA */
1112 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1113 EQOS_DMA_CH0_RX_CONTROL_SR);
1114
1115 if (eqos->phy) {
1116 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001117 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001118 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001119
1120 debug("%s: OK\n", __func__);
1121}
1122
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001123static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001124{
1125 struct eqos_priv *eqos = dev_get_priv(dev);
1126 struct eqos_desc *tx_desc;
1127 int i;
1128
1129 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1130 length);
1131
1132 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001133 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001134
Marek Vasut89077732021-01-07 11:12:16 +01001135 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001136 eqos->tx_desc_idx++;
1137 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1138
1139 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1140 tx_desc->des1 = 0;
1141 tx_desc->des2 = length;
1142 /*
1143 * Make sure that if HW sees the _OWN write below, it will see all the
1144 * writes to the rest of the descriptor too.
1145 */
1146 mb();
1147 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001148 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001149
Marek Vasut89077732021-01-07 11:12:16 +01001150 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001151 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001152
1153 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001154 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001155 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1156 return 0;
1157 udelay(1);
1158 }
1159
1160 debug("%s: TX timeout\n", __func__);
1161
1162 return -ETIMEDOUT;
1163}
1164
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001165static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001166{
1167 struct eqos_priv *eqos = dev_get_priv(dev);
1168 struct eqos_desc *rx_desc;
1169 int length;
1170
1171 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1172
Marek Vasut89077732021-01-07 11:12:16 +01001173 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001174 eqos->config->ops->eqos_inval_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001175 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1176 debug("%s: RX packet not available\n", __func__);
1177 return -EAGAIN;
1178 }
1179
1180 *packetp = eqos->rx_dma_buf +
1181 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1182 length = rx_desc->des3 & 0x7fff;
1183 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1184
Christophe Roullier6beb7802019-05-17 15:08:44 +02001185 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001186
1187 return length;
1188}
1189
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001190static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001191{
1192 struct eqos_priv *eqos = dev_get_priv(dev);
1193 uchar *packet_expected;
1194 struct eqos_desc *rx_desc;
1195
1196 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1197
1198 packet_expected = eqos->rx_dma_buf +
1199 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1200 if (packet != packet_expected) {
1201 debug("%s: Unexpected packet (expected %p)\n", __func__,
1202 packet_expected);
1203 return -EINVAL;
1204 }
1205
Fugang Duan37aae5f2020-05-03 22:41:17 +08001206 eqos->config->ops->eqos_inval_buffer(packet, length);
1207
Marek Vasut89077732021-01-07 11:12:16 +01001208 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001209
Marek Vasut091b6db2020-03-23 02:11:46 +01001210 rx_desc->des0 = 0;
1211 mb();
1212 eqos->config->ops->eqos_flush_desc(rx_desc);
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001213 eqos->config->ops->eqos_inval_buffer(packet, length);
Stephen Warren50709602016-10-21 14:46:47 -06001214 rx_desc->des0 = (u32)(ulong)packet;
1215 rx_desc->des1 = 0;
1216 rx_desc->des2 = 0;
1217 /*
1218 * Make sure that if HW sees the _OWN write below, it will see all the
1219 * writes to the rest of the descriptor too.
1220 */
1221 mb();
Marek Vasutd54c98e2020-03-23 02:02:57 +01001222 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001223 eqos->config->ops->eqos_flush_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001224
1225 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1226
1227 eqos->rx_desc_idx++;
1228 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1229
1230 return 0;
1231}
1232
1233static int eqos_probe_resources_core(struct udevice *dev)
1234{
1235 struct eqos_priv *eqos = dev_get_priv(dev);
1236 int ret;
1237
1238 debug("%s(dev=%p):\n", __func__, dev);
1239
Marek Vasut89077732021-01-07 11:12:16 +01001240 eqos->descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_NUM);
Stephen Warren50709602016-10-21 14:46:47 -06001241 if (!eqos->descs) {
1242 debug("%s: eqos_alloc_descs() failed\n", __func__);
1243 ret = -ENOMEM;
1244 goto err;
1245 }
Stephen Warren50709602016-10-21 14:46:47 -06001246
1247 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1248 if (!eqos->tx_dma_buf) {
1249 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1250 ret = -ENOMEM;
1251 goto err_free_descs;
1252 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001253 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001254
1255 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1256 if (!eqos->rx_dma_buf) {
1257 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1258 ret = -ENOMEM;
1259 goto err_free_tx_dma_buf;
1260 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001261 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001262
1263 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1264 if (!eqos->rx_pkt) {
1265 debug("%s: malloc(rx_pkt) failed\n", __func__);
1266 ret = -ENOMEM;
1267 goto err_free_rx_dma_buf;
1268 }
1269 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1270
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001271 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1272 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1273
Stephen Warren50709602016-10-21 14:46:47 -06001274 debug("%s: OK\n", __func__);
1275 return 0;
1276
1277err_free_rx_dma_buf:
1278 free(eqos->rx_dma_buf);
1279err_free_tx_dma_buf:
1280 free(eqos->tx_dma_buf);
1281err_free_descs:
1282 eqos_free_descs(eqos->descs);
1283err:
1284
1285 debug("%s: returns %d\n", __func__, ret);
1286 return ret;
1287}
1288
1289static int eqos_remove_resources_core(struct udevice *dev)
1290{
1291 struct eqos_priv *eqos = dev_get_priv(dev);
1292
1293 debug("%s(dev=%p):\n", __func__, dev);
1294
1295 free(eqos->rx_pkt);
1296 free(eqos->rx_dma_buf);
1297 free(eqos->tx_dma_buf);
1298 eqos_free_descs(eqos->descs);
1299
1300 debug("%s: OK\n", __func__);
1301 return 0;
1302}
1303
1304static int eqos_probe_resources_tegra186(struct udevice *dev)
1305{
1306 struct eqos_priv *eqos = dev_get_priv(dev);
1307 int ret;
1308
1309 debug("%s(dev=%p):\n", __func__, dev);
1310
1311 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1312 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001313 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001314 return ret;
1315 }
1316
1317 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1318 &eqos->phy_reset_gpio,
1319 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1320 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001321 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001322 goto err_free_reset_eqos;
1323 }
1324
1325 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1326 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001327 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001328 goto err_free_gpio_phy_reset;
1329 }
1330
1331 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1332 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001333 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001334 goto err_free_clk_slave_bus;
1335 }
1336
1337 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1338 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001339 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001340 goto err_free_clk_master_bus;
1341 }
1342
1343 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1344 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001345 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001346 goto err_free_clk_rx;
1347 return ret;
1348 }
1349
1350 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1351 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001352 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001353 goto err_free_clk_ptp_ref;
1354 }
1355
1356 debug("%s: OK\n", __func__);
1357 return 0;
1358
1359err_free_clk_ptp_ref:
1360 clk_free(&eqos->clk_ptp_ref);
1361err_free_clk_rx:
1362 clk_free(&eqos->clk_rx);
1363err_free_clk_master_bus:
1364 clk_free(&eqos->clk_master_bus);
1365err_free_clk_slave_bus:
1366 clk_free(&eqos->clk_slave_bus);
1367err_free_gpio_phy_reset:
1368 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1369err_free_reset_eqos:
1370 reset_free(&eqos->reset_ctl);
1371
1372 debug("%s: returns %d\n", __func__, ret);
1373 return ret;
1374}
1375
Christophe Roullier6beb7802019-05-17 15:08:44 +02001376/* board-specific Ethernet Interface initializations. */
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001377__weak int board_interface_eth_init(struct udevice *dev,
1378 phy_interface_t interface_type)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001379{
1380 return 0;
1381}
1382
1383static int eqos_probe_resources_stm32(struct udevice *dev)
1384{
1385 struct eqos_priv *eqos = dev_get_priv(dev);
1386 int ret;
1387 phy_interface_t interface;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001388
1389 debug("%s(dev=%p):\n", __func__, dev);
1390
1391 interface = eqos->config->interface(dev);
1392
Marek BehĂșn48631e42022-04-07 00:33:03 +02001393 if (interface == PHY_INTERFACE_MODE_NA) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001394 pr_err("Invalid PHY interface\n");
1395 return -EINVAL;
1396 }
1397
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001398 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001399 if (ret)
1400 return -EINVAL;
1401
Patrick Delaunay5c8db372020-03-18 10:50:16 +01001402 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1403
Christophe Roullier6beb7802019-05-17 15:08:44 +02001404 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1405 if (ret) {
1406 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1407 goto err_probe;
1408 }
1409
1410 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1411 if (ret) {
1412 pr_err("clk_get_by_name(rx) failed: %d", ret);
1413 goto err_free_clk_master_bus;
1414 }
1415
1416 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1417 if (ret) {
1418 pr_err("clk_get_by_name(tx) failed: %d", ret);
1419 goto err_free_clk_rx;
1420 }
1421
1422 /* Get ETH_CLK clocks (optional) */
1423 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1424 if (ret)
1425 pr_warn("No phy clock provided %d", ret);
1426
1427 debug("%s: OK\n", __func__);
1428 return 0;
1429
1430err_free_clk_rx:
1431 clk_free(&eqos->clk_rx);
1432err_free_clk_master_bus:
1433 clk_free(&eqos->clk_master_bus);
1434err_probe:
1435
1436 debug("%s: returns %d\n", __func__, ret);
1437 return ret;
1438}
1439
Marek BehĂșnbc194772022-04-07 00:33:01 +02001440static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001441{
1442 return PHY_INTERFACE_MODE_MII;
1443}
1444
Stephen Warren50709602016-10-21 14:46:47 -06001445static int eqos_remove_resources_tegra186(struct udevice *dev)
1446{
1447 struct eqos_priv *eqos = dev_get_priv(dev);
1448
1449 debug("%s(dev=%p):\n", __func__, dev);
1450
Fugang Duan37aae5f2020-05-03 22:41:17 +08001451#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -06001452 clk_free(&eqos->clk_tx);
1453 clk_free(&eqos->clk_ptp_ref);
1454 clk_free(&eqos->clk_rx);
1455 clk_free(&eqos->clk_slave_bus);
1456 clk_free(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001457#endif
Stephen Warren50709602016-10-21 14:46:47 -06001458 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1459 reset_free(&eqos->reset_ctl);
1460
1461 debug("%s: OK\n", __func__);
1462 return 0;
1463}
1464
Christophe Roullier6beb7802019-05-17 15:08:44 +02001465static int eqos_remove_resources_stm32(struct udevice *dev)
1466{
1467 struct eqos_priv *eqos = dev_get_priv(dev);
1468
1469 debug("%s(dev=%p):\n", __func__, dev);
1470
Peng Fan809993f2022-07-26 16:41:13 +08001471#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +02001472 clk_free(&eqos->clk_tx);
1473 clk_free(&eqos->clk_rx);
1474 clk_free(&eqos->clk_master_bus);
1475 if (clk_valid(&eqos->clk_ck))
1476 clk_free(&eqos->clk_ck);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001477#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001478
Christophe Roullier104dab52020-03-18 10:50:15 +01001479 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
1480 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1481
Christophe Roullier6beb7802019-05-17 15:08:44 +02001482 debug("%s: OK\n", __func__);
1483 return 0;
1484}
1485
Stephen Warren50709602016-10-21 14:46:47 -06001486static int eqos_probe(struct udevice *dev)
1487{
1488 struct eqos_priv *eqos = dev_get_priv(dev);
1489 int ret;
1490
1491 debug("%s(dev=%p):\n", __func__, dev);
1492
1493 eqos->dev = dev;
1494 eqos->config = (void *)dev_get_driver_data(dev);
1495
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001496 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001497 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001498 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001499 return -ENODEV;
1500 }
1501 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1502 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1503 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1504 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1505
1506 ret = eqos_probe_resources_core(dev);
1507 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001508 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001509 return ret;
1510 }
1511
Christophe Roullier6beb7802019-05-17 15:08:44 +02001512 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001513 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001514 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001515 goto err_remove_resources_core;
1516 }
1517
Marek Vasut30b28c42021-11-13 03:23:52 +01001518 ret = eqos->config->ops->eqos_start_clks(dev);
1519 if (ret < 0) {
1520 pr_err("eqos_start_clks() failed: %d", ret);
1521 goto err_remove_resources_tegra;
1522 }
1523
Ye Liad122b72020-05-03 22:41:15 +08001524#ifdef CONFIG_DM_ETH_PHY
1525 eqos->mii = eth_phy_get_mdio_bus(dev);
1526#endif
Stephen Warren50709602016-10-21 14:46:47 -06001527 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001528 eqos->mii = mdio_alloc();
1529 if (!eqos->mii) {
1530 pr_err("mdio_alloc() failed");
1531 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001532 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001533 }
1534 eqos->mii->read = eqos_mdio_read;
1535 eqos->mii->write = eqos_mdio_write;
1536 eqos->mii->priv = eqos;
1537 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001538
Ye Liad122b72020-05-03 22:41:15 +08001539 ret = mdio_register(eqos->mii);
1540 if (ret < 0) {
1541 pr_err("mdio_register() failed: %d", ret);
1542 goto err_free_mdio;
1543 }
Stephen Warren50709602016-10-21 14:46:47 -06001544 }
1545
Ye Liad122b72020-05-03 22:41:15 +08001546#ifdef CONFIG_DM_ETH_PHY
1547 eth_phy_set_mdio_bus(dev, eqos->mii);
1548#endif
1549
Stephen Warren50709602016-10-21 14:46:47 -06001550 debug("%s: OK\n", __func__);
1551 return 0;
1552
1553err_free_mdio:
1554 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001555err_stop_clks:
1556 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001557err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001558 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001559err_remove_resources_core:
1560 eqos_remove_resources_core(dev);
1561
1562 debug("%s: returns %d\n", __func__, ret);
1563 return ret;
1564}
1565
1566static int eqos_remove(struct udevice *dev)
1567{
1568 struct eqos_priv *eqos = dev_get_priv(dev);
1569
1570 debug("%s(dev=%p):\n", __func__, dev);
1571
1572 mdio_unregister(eqos->mii);
1573 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001574 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001575 eqos->config->ops->eqos_remove_resources(dev);
1576
Stephen Warren50709602016-10-21 14:46:47 -06001577 eqos_probe_resources_core(dev);
1578
1579 debug("%s: OK\n", __func__);
1580 return 0;
1581}
1582
Peng Fanc0a59952022-07-26 16:41:14 +08001583int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001584{
1585 return 0;
1586}
1587
Stephen Warren50709602016-10-21 14:46:47 -06001588static const struct eth_ops eqos_ops = {
1589 .start = eqos_start,
1590 .stop = eqos_stop,
1591 .send = eqos_send,
1592 .recv = eqos_recv,
1593 .free_pkt = eqos_free_pkt,
1594 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001595 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001596};
1597
Christophe Roullier6beb7802019-05-17 15:08:44 +02001598static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001599 .eqos_inval_desc = eqos_inval_desc_generic,
1600 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001601 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1602 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1603 .eqos_probe_resources = eqos_probe_resources_tegra186,
1604 .eqos_remove_resources = eqos_remove_resources_tegra186,
1605 .eqos_stop_resets = eqos_stop_resets_tegra186,
1606 .eqos_start_resets = eqos_start_resets_tegra186,
1607 .eqos_stop_clks = eqos_stop_clks_tegra186,
1608 .eqos_start_clks = eqos_start_clks_tegra186,
1609 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1610 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1611 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
1612 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1613};
1614
Patrick Delaunay68083902020-06-08 11:27:19 +02001615static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001616 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001617 .mdio_wait = 10,
1618 .swr_wait = 10,
1619 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1620 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001621 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001622 .interface = eqos_get_interface_tegra186,
1623 .ops = &eqos_tegra186_ops
1624};
1625
1626static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08001627 .eqos_inval_desc = eqos_inval_desc_generic,
1628 .eqos_flush_desc = eqos_flush_desc_generic,
1629 .eqos_inval_buffer = eqos_inval_buffer_generic,
1630 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001631 .eqos_probe_resources = eqos_probe_resources_stm32,
1632 .eqos_remove_resources = eqos_remove_resources_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001633 .eqos_stop_resets = eqos_null_ops,
1634 .eqos_start_resets = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001635 .eqos_stop_clks = eqos_stop_clks_stm32,
1636 .eqos_start_clks = eqos_start_clks_stm32,
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001637 .eqos_calibrate_pads = eqos_null_ops,
1638 .eqos_disable_calibration = eqos_null_ops,
1639 .eqos_set_tx_clk_speed = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001640 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1641};
1642
Patrick Delaunay68083902020-06-08 11:27:19 +02001643static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001644 .reg_access_always_ok = false,
1645 .mdio_wait = 10000,
1646 .swr_wait = 50,
1647 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1648 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01001649 .axi_bus_width = EQOS_AXI_WIDTH_64,
Marek BehĂșnbc194772022-04-07 00:33:01 +02001650 .interface = dev_read_phy_mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001651 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001652};
1653
1654static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001655#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001656 {
1657 .compatible = "nvidia,tegra186-eqos",
1658 .data = (ulong)&eqos_tegra186_config
1659 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001660#endif
1661#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001662 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001663 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02001664 .data = (ulong)&eqos_stm32_config
1665 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001666#endif
1667#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001668 {
Marek Vasut7af11382022-02-26 04:36:37 +01001669 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001670 .data = (ulong)&eqos_imx_config
1671 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001672#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001673
Stephen Warren50709602016-10-21 14:46:47 -06001674 { }
1675};
1676
1677U_BOOT_DRIVER(eth_eqos) = {
1678 .name = "eth_eqos",
1679 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001680 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001681 .probe = eqos_probe,
1682 .remove = eqos_remove,
1683 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001684 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001685 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001686};