blob: 67ac86f82bcd1dd1aae2d966e6ce98f25d1f03e5 [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 <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070033#include <cpu_func.h>
Stephen Warren50709602016-10-21 14:46:47 -060034#include <dm.h>
35#include <errno.h>
Patrick Delaunay41729272022-06-30 11:09:41 +020036#include <eth_phy.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>
Fugang Duandd455e62020-05-03 22:41:18 +080049#ifdef CONFIG_ARCH_IMX8M
50#include <asm/arch/clock.h>
51#include <asm/mach-imx/sys_proto.h>
52#endif
Simon Glassdbd79542020-05-10 11:40:11 -060053#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060054#include <linux/printk.h>
Stephen Warren50709602016-10-21 14:46:47 -060055
Peng Fanc0a59952022-07-26 16:41:14 +080056#include "dwc_eth_qos.h"
Stephen Warren50709602016-10-21 14:46:47 -060057
58/*
59 * TX and RX descriptors are 16 bytes. This causes problems with the cache
60 * maintenance on CPUs where the cache-line size exceeds the size of these
61 * descriptors. What will happen is that when the driver receives a packet
62 * it will be immediately requeued for the hardware to reuse. The CPU will
63 * therefore need to flush the cache-line containing the descriptor, which
64 * will cause all other descriptors in the same cache-line to be flushed
65 * along with it. If one of those descriptors had been written to by the
66 * device those changes (and the associated packet) will be lost.
67 *
68 * To work around this, we make use of non-cached memory if available. If
69 * descriptors are mapped uncached there's no need to manually flush them
70 * or invalidate them.
71 *
72 * Note that this only applies to descriptors. The packet data buffers do
73 * not have the same constraints since they are 1536 bytes large, so they
74 * are unlikely to share cache-lines.
75 */
Marek Vasut89077732021-01-07 11:12:16 +010076static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -060077{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020078 return memalign(ARCH_DMA_MINALIGN, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060079}
80
81static void eqos_free_descs(void *descs)
82{
Stephen Warren50709602016-10-21 14:46:47 -060083 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -060084}
85
Marek Vasut89077732021-01-07 11:12:16 +010086static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
87 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -060088{
Marek Vasut90cc13a2022-10-09 17:51:45 +020089 return (rx ? eqos->rx_descs : eqos->tx_descs) +
90 (num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060091}
92
Peng Fanc0a59952022-07-26 16:41:14 +080093void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -060094{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020095 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +010096 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
97 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +020098
99 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600100}
101
Peng Fanc0a59952022-07-26 16:41:14 +0800102void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200103{
Marek Vasut3e8a1be2022-10-09 17:51:46 +0200104 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +0100105 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
106 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200107
108 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200109}
110
Marek Vasut7b6fec22023-03-06 15:53:45 +0100111static void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600112{
113 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
114 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
115
116 invalidate_dcache_range(start, end);
117}
118
Peng Fanc0a59952022-07-26 16:41:14 +0800119void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200120{
121 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
122 unsigned long end = roundup((unsigned long)buf + size,
123 ARCH_DMA_MINALIGN);
124
125 invalidate_dcache_range(start, end);
126}
127
128static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600129{
130 flush_cache((unsigned long)buf, size);
131}
132
Peng Fanc0a59952022-07-26 16:41:14 +0800133void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200134{
135 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
136 unsigned long end = roundup((unsigned long)buf + size,
137 ARCH_DMA_MINALIGN);
138
139 flush_dcache_range(start, end);
140}
141
Stephen Warren50709602016-10-21 14:46:47 -0600142static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
143{
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100144 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
145 EQOS_MAC_MDIO_ADDRESS_GB, false,
146 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600147}
148
149static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
150 int mdio_reg)
151{
152 struct eqos_priv *eqos = bus->priv;
153 u32 val;
154 int ret;
155
156 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
157 mdio_reg);
158
159 ret = eqos_mdio_wait_idle(eqos);
160 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200161 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600162 return ret;
163 }
164
165 val = readl(&eqos->mac_regs->mdio_address);
166 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
167 EQOS_MAC_MDIO_ADDRESS_C45E;
168 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
169 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200170 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600171 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
172 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
173 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
174 EQOS_MAC_MDIO_ADDRESS_GB;
175 writel(val, &eqos->mac_regs->mdio_address);
176
Christophe Roullier6beb7802019-05-17 15:08:44 +0200177 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600178
179 ret = eqos_mdio_wait_idle(eqos);
180 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200181 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600182 return ret;
183 }
184
185 val = readl(&eqos->mac_regs->mdio_data);
186 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
187
188 debug("%s: val=%x\n", __func__, val);
189
190 return val;
191}
192
193static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
194 int mdio_reg, u16 mdio_val)
195{
196 struct eqos_priv *eqos = bus->priv;
197 u32 val;
198 int ret;
199
200 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
201 mdio_addr, mdio_reg, mdio_val);
202
203 ret = eqos_mdio_wait_idle(eqos);
204 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200205 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600206 return ret;
207 }
208
209 writel(mdio_val, &eqos->mac_regs->mdio_data);
210
211 val = readl(&eqos->mac_regs->mdio_address);
212 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
213 EQOS_MAC_MDIO_ADDRESS_C45E;
214 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
215 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200216 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600217 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
218 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
219 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
220 EQOS_MAC_MDIO_ADDRESS_GB;
221 writel(val, &eqos->mac_regs->mdio_address);
222
Christophe Roullier6beb7802019-05-17 15:08:44 +0200223 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600224
225 ret = eqos_mdio_wait_idle(eqos);
226 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200227 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600228 return ret;
229 }
230
231 return 0;
232}
233
234static int eqos_start_clks_tegra186(struct udevice *dev)
235{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800236#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600237 struct eqos_priv *eqos = dev_get_priv(dev);
238 int ret;
239
240 debug("%s(dev=%p):\n", __func__, dev);
241
242 ret = clk_enable(&eqos->clk_slave_bus);
243 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200244 pr_err("clk_enable(clk_slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600245 goto err;
246 }
247
248 ret = clk_enable(&eqos->clk_master_bus);
249 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200250 pr_err("clk_enable(clk_master_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600251 goto err_disable_clk_slave_bus;
252 }
253
254 ret = clk_enable(&eqos->clk_rx);
255 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200256 pr_err("clk_enable(clk_rx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600257 goto err_disable_clk_master_bus;
258 }
259
260 ret = clk_enable(&eqos->clk_ptp_ref);
261 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200262 pr_err("clk_enable(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600263 goto err_disable_clk_rx;
264 }
265
266 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
267 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200268 pr_err("clk_set_rate(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600269 goto err_disable_clk_ptp_ref;
270 }
271
272 ret = clk_enable(&eqos->clk_tx);
273 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200274 pr_err("clk_enable(clk_tx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600275 goto err_disable_clk_ptp_ref;
276 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800277#endif
Stephen Warren50709602016-10-21 14:46:47 -0600278
279 debug("%s: OK\n", __func__);
280 return 0;
281
Fugang Duan37aae5f2020-05-03 22:41:17 +0800282#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600283err_disable_clk_ptp_ref:
284 clk_disable(&eqos->clk_ptp_ref);
285err_disable_clk_rx:
286 clk_disable(&eqos->clk_rx);
287err_disable_clk_master_bus:
288 clk_disable(&eqos->clk_master_bus);
289err_disable_clk_slave_bus:
290 clk_disable(&eqos->clk_slave_bus);
291err:
292 debug("%s: FAILED: %d\n", __func__, ret);
293 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800294#endif
Stephen Warren50709602016-10-21 14:46:47 -0600295}
296
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200297static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600298{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800299#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600300 struct eqos_priv *eqos = dev_get_priv(dev);
301
302 debug("%s(dev=%p):\n", __func__, dev);
303
304 clk_disable(&eqos->clk_tx);
305 clk_disable(&eqos->clk_ptp_ref);
306 clk_disable(&eqos->clk_rx);
307 clk_disable(&eqos->clk_master_bus);
308 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800309#endif
Stephen Warren50709602016-10-21 14:46:47 -0600310
311 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200312 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600313}
314
315static int eqos_start_resets_tegra186(struct udevice *dev)
316{
317 struct eqos_priv *eqos = dev_get_priv(dev);
318 int ret;
319
320 debug("%s(dev=%p):\n", __func__, dev);
321
322 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
323 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200324 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600325 return ret;
326 }
327
328 udelay(2);
329
330 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
331 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200332 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600333 return ret;
334 }
335
336 ret = reset_assert(&eqos->reset_ctl);
337 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200338 pr_err("reset_assert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600339 return ret;
340 }
341
342 udelay(2);
343
344 ret = reset_deassert(&eqos->reset_ctl);
345 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200346 pr_err("reset_deassert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600347 return ret;
348 }
349
350 debug("%s: OK\n", __func__);
351 return 0;
352}
353
354static int eqos_stop_resets_tegra186(struct udevice *dev)
355{
356 struct eqos_priv *eqos = dev_get_priv(dev);
357
358 reset_assert(&eqos->reset_ctl);
359 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
360
Christophe Roullier6beb7802019-05-17 15:08:44 +0200361 return 0;
362}
363
Stephen Warren50709602016-10-21 14:46:47 -0600364static int eqos_calibrate_pads_tegra186(struct udevice *dev)
365{
366 struct eqos_priv *eqos = dev_get_priv(dev);
367 int ret;
368
369 debug("%s(dev=%p):\n", __func__, dev);
370
371 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
372 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
373
374 udelay(1);
375
376 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
377 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
378
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100379 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
380 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600381 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200382 pr_err("calibrate didn't start\n");
Stephen Warren50709602016-10-21 14:46:47 -0600383 goto failed;
384 }
385
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100386 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
387 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600388 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200389 pr_err("calibrate didn't finish\n");
Stephen Warren50709602016-10-21 14:46:47 -0600390 goto failed;
391 }
392
393 ret = 0;
394
395failed:
396 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
397 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
398
399 debug("%s: returns %d\n", __func__, ret);
400
401 return ret;
402}
403
404static int eqos_disable_calibration_tegra186(struct udevice *dev)
405{
406 struct eqos_priv *eqos = dev_get_priv(dev);
407
408 debug("%s(dev=%p):\n", __func__, dev);
409
410 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
411 EQOS_AUTO_CAL_CONFIG_ENABLE);
412
413 return 0;
414}
415
416static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
417{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800418#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600419 struct eqos_priv *eqos = dev_get_priv(dev);
420
421 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800422#else
423 return 0;
424#endif
Stephen Warren50709602016-10-21 14:46:47 -0600425}
426
427static int eqos_set_full_duplex(struct udevice *dev)
428{
429 struct eqos_priv *eqos = dev_get_priv(dev);
430
431 debug("%s(dev=%p):\n", __func__, dev);
432
433 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
434
435 return 0;
436}
437
438static int eqos_set_half_duplex(struct udevice *dev)
439{
440 struct eqos_priv *eqos = dev_get_priv(dev);
441
442 debug("%s(dev=%p):\n", __func__, dev);
443
444 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
445
446 /* WAR: Flush TX queue when switching to half-duplex */
447 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
448 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
449
450 return 0;
451}
452
453static int eqos_set_gmii_speed(struct udevice *dev)
454{
455 struct eqos_priv *eqos = dev_get_priv(dev);
456
457 debug("%s(dev=%p):\n", __func__, dev);
458
459 clrbits_le32(&eqos->mac_regs->configuration,
460 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
461
462 return 0;
463}
464
465static int eqos_set_mii_speed_100(struct udevice *dev)
466{
467 struct eqos_priv *eqos = dev_get_priv(dev);
468
469 debug("%s(dev=%p):\n", __func__, dev);
470
471 setbits_le32(&eqos->mac_regs->configuration,
472 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
473
474 return 0;
475}
476
477static int eqos_set_mii_speed_10(struct udevice *dev)
478{
479 struct eqos_priv *eqos = dev_get_priv(dev);
480
481 debug("%s(dev=%p):\n", __func__, dev);
482
483 clrsetbits_le32(&eqos->mac_regs->configuration,
484 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
485
486 return 0;
487}
488
489static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
490{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800491#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600492 struct eqos_priv *eqos = dev_get_priv(dev);
493 ulong rate;
494 int ret;
495
496 debug("%s(dev=%p):\n", __func__, dev);
497
498 switch (eqos->phy->speed) {
499 case SPEED_1000:
500 rate = 125 * 1000 * 1000;
501 break;
502 case SPEED_100:
503 rate = 25 * 1000 * 1000;
504 break;
505 case SPEED_10:
506 rate = 2.5 * 1000 * 1000;
507 break;
508 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200509 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600510 return -EINVAL;
511 }
512
513 ret = clk_set_rate(&eqos->clk_tx, rate);
514 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200515 pr_err("clk_set_rate(tx_clk, %lu) failed: %d\n", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600516 return ret;
517 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800518#endif
Stephen Warren50709602016-10-21 14:46:47 -0600519
520 return 0;
521}
522
523static int eqos_adjust_link(struct udevice *dev)
524{
525 struct eqos_priv *eqos = dev_get_priv(dev);
526 int ret;
527 bool en_calibration;
528
529 debug("%s(dev=%p):\n", __func__, dev);
530
531 if (eqos->phy->duplex)
532 ret = eqos_set_full_duplex(dev);
533 else
534 ret = eqos_set_half_duplex(dev);
535 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200536 pr_err("eqos_set_*_duplex() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600537 return ret;
538 }
539
540 switch (eqos->phy->speed) {
541 case SPEED_1000:
542 en_calibration = true;
543 ret = eqos_set_gmii_speed(dev);
544 break;
545 case SPEED_100:
546 en_calibration = true;
547 ret = eqos_set_mii_speed_100(dev);
548 break;
549 case SPEED_10:
550 en_calibration = false;
551 ret = eqos_set_mii_speed_10(dev);
552 break;
553 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200554 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600555 return -EINVAL;
556 }
557 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200558 pr_err("eqos_set_*mii_speed*() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600559 return ret;
560 }
561
562 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200563 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600564 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200565 pr_err("eqos_calibrate_pads() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200566 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600567 return ret;
568 }
569 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200570 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600571 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200572 pr_err("eqos_disable_calibration() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200573 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600574 return ret;
575 }
576 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200577 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600578 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200579 pr_err("eqos_set_tx_clk_speed() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600580 return ret;
581 }
582
583 return 0;
584}
585
586static int eqos_write_hwaddr(struct udevice *dev)
587{
Simon Glassfa20e932020-12-03 16:55:20 -0700588 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600589 struct eqos_priv *eqos = dev_get_priv(dev);
590 uint32_t val;
591
592 /*
593 * This function may be called before start() or after stop(). At that
594 * time, on at least some configurations of the EQoS HW, all clocks to
595 * the EQoS HW block will be stopped, and a reset signal applied. If
596 * any register access is attempted in this state, bus timeouts or CPU
597 * hangs may occur. This check prevents that.
598 *
599 * A simple solution to this problem would be to not implement
600 * write_hwaddr(), since start() always writes the MAC address into HW
601 * anyway. However, it is desirable to implement write_hwaddr() to
602 * support the case of SW that runs subsequent to U-Boot which expects
603 * the MAC address to already be programmed into the EQoS registers,
604 * which must happen irrespective of whether the U-Boot user (or
605 * scripts) actually made use of the EQoS device, and hence
606 * irrespective of whether start() was ever called.
607 *
608 * Note that this requirement by subsequent SW is not valid for
609 * Tegra186, and is likely not valid for any non-PCI instantiation of
610 * the EQoS HW block. This function is implemented solely as
611 * future-proofing with the expectation the driver will eventually be
612 * ported to some system where the expectation above is true.
613 */
614 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
615 return 0;
616
617 /* Update the MAC address */
618 val = (plat->enetaddr[5] << 8) |
619 (plat->enetaddr[4]);
620 writel(val, &eqos->mac_regs->address0_high);
621 val = (plat->enetaddr[3] << 24) |
622 (plat->enetaddr[2] << 16) |
623 (plat->enetaddr[1] << 8) |
624 (plat->enetaddr[0]);
625 writel(val, &eqos->mac_regs->address0_low);
626
627 return 0;
628}
629
Ye Li3fb1a0e2020-05-03 22:41:20 +0800630static int eqos_read_rom_hwaddr(struct udevice *dev)
631{
Simon Glassfa20e932020-12-03 16:55:20 -0700632 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800633 struct eqos_priv *eqos = dev_get_priv(dev);
634 int ret;
635
636 ret = eqos->config->ops->eqos_get_enetaddr(dev);
637 if (ret < 0)
638 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800639
Ye Li3fb1a0e2020-05-03 22:41:20 +0800640 return !is_valid_ethaddr(pdata->enetaddr);
641}
642
Ye Li2f2aa482022-07-26 16:41:16 +0800643static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
644{
645 struct ofnode_phandle_args phandle_args;
646 int reg;
647
648 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
649 &phandle_args)) {
650 debug("Failed to find phy-handle");
651 return -ENODEV;
652 }
653
654 priv->phy_of_node = phandle_args.node;
655
656 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
657
658 return reg;
659}
660
Stephen Warren50709602016-10-21 14:46:47 -0600661static int eqos_start(struct udevice *dev)
662{
663 struct eqos_priv *eqos = dev_get_priv(dev);
664 int ret, i;
665 ulong rate;
666 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
667 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100668 ulong desc_pad;
Ley Foon Tan963db382022-12-09 14:33:14 +0800669 ulong addr64;
Stephen Warren50709602016-10-21 14:46:47 -0600670
671 debug("%s(dev=%p):\n", __func__, dev);
672
673 eqos->tx_desc_idx = 0;
674 eqos->rx_desc_idx = 0;
675
Christophe Roullier6beb7802019-05-17 15:08:44 +0200676 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600677 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200678 pr_err("eqos_start_resets() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100679 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600680 }
681
682 udelay(10);
683
684 eqos->reg_access_ok = true;
685
Marek Vasute66825a2023-03-06 15:53:46 +0100686 /*
687 * Assert the SWR first, the actually reset the MAC and to latch in
688 * e.g. i.MX8M Plus GPR[1] content, which selects interface mode.
689 */
690 setbits_le32(&eqos->dma_regs->mode, EQOS_DMA_MODE_SWR);
691
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100692 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200693 EQOS_DMA_MODE_SWR, false,
694 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600695 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200696 pr_err("EQOS_DMA_MODE_SWR stuck\n");
Stephen Warren50709602016-10-21 14:46:47 -0600697 goto err_stop_resets;
698 }
699
Christophe Roullier6beb7802019-05-17 15:08:44 +0200700 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600701 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200702 pr_err("eqos_calibrate_pads() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600703 goto err_stop_resets;
704 }
Sumit Gargab973c92023-02-01 19:28:53 +0530705
706 if (eqos->config->ops->eqos_get_tick_clk_rate) {
707 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600708
Sumit Gargab973c92023-02-01 19:28:53 +0530709 val = (rate / 1000000) - 1;
710 writel(val, &eqos->mac_regs->us_tic_counter);
711 }
Stephen Warren50709602016-10-21 14:46:47 -0600712
Christophe Roullier6beb7802019-05-17 15:08:44 +0200713 /*
714 * if PHY was already connected and configured,
715 * don't need to reconnect/reconfigure again
716 */
Stephen Warren50709602016-10-21 14:46:47 -0600717 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800718 int addr = -1;
Elmar Psilogdd65ba22023-02-20 16:03:15 +0100719 ofnode fixed_node;
720
721 if (IS_ENABLED(CONFIG_PHY_FIXED)) {
722 fixed_node = ofnode_find_subnode(dev_ofnode(dev),
723 "fixed-link");
724 if (ofnode_valid(fixed_node))
725 eqos->phy = fixed_phy_create(dev_ofnode(dev));
726 }
727
728 if (!eqos->phy) {
729 addr = eqos_get_phy_addr(eqos, dev);
730 eqos->phy = phy_connect(eqos->mii, addr, dev,
731 eqos->config->interface(dev));
732 }
733
Christophe Roullier6beb7802019-05-17 15:08:44 +0200734 if (!eqos->phy) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200735 pr_err("phy_connect() failed\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000736 ret = -ENODEV;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200737 goto err_stop_resets;
738 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100739
740 if (eqos->max_speed) {
741 ret = phy_set_supported(eqos->phy, eqos->max_speed);
742 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200743 pr_err("phy_set_supported() failed: %d\n", ret);
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100744 goto err_shutdown_phy;
745 }
746 }
747
Ye Li2f2aa482022-07-26 16:41:16 +0800748 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200749 ret = phy_config(eqos->phy);
750 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200751 pr_err("phy_config() failed: %d\n", ret);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200752 goto err_shutdown_phy;
753 }
Stephen Warren50709602016-10-21 14:46:47 -0600754 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200755
Stephen Warren50709602016-10-21 14:46:47 -0600756 ret = phy_startup(eqos->phy);
757 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200758 pr_err("phy_startup() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600759 goto err_shutdown_phy;
760 }
761
762 if (!eqos->phy->link) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200763 pr_err("No link\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000764 ret = -EAGAIN;
Stephen Warren50709602016-10-21 14:46:47 -0600765 goto err_shutdown_phy;
766 }
767
768 ret = eqos_adjust_link(dev);
769 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200770 pr_err("eqos_adjust_link() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600771 goto err_shutdown_phy;
772 }
773
774 /* Configure MTL */
775
776 /* Enable Store and Forward mode for TX */
777 /* Program Tx operating mode */
778 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
779 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
780 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
781 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
782
783 /* Transmit Queue weight */
784 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
785
786 /* Enable Store and Forward mode for RX, since no jumbo frame */
787 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000788 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600789
790 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
791 val = readl(&eqos->mac_regs->hw_feature1);
792 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
793 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
794 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
795 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
796
Sumit Garg4d5c9652023-02-01 19:28:54 +0530797 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
798 tx_fifo_sz = 128 << tx_fifo_sz;
799 rx_fifo_sz = 128 << rx_fifo_sz;
800
801 /* Allow platform to override TX/RX fifo size */
802 if (eqos->tx_fifo_sz)
803 tx_fifo_sz = eqos->tx_fifo_sz;
804 if (eqos->rx_fifo_sz)
805 rx_fifo_sz = eqos->rx_fifo_sz;
806
807 /* r/tqs is encoded as (n / 256) - 1 */
808 tqs = tx_fifo_sz / 256 - 1;
809 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600810
811 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
812 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
813 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
814 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
815 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
816 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
817 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
818 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
819
820 /* Flow control used only if each channel gets 4KB or more FIFO */
821 if (rqs >= ((4096 / 256) - 1)) {
822 u32 rfd, rfa;
823
824 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
825 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
826
827 /*
828 * Set Threshold for Activating Flow Contol space for min 2
829 * frames ie, (1500 * 1) = 1500 bytes.
830 *
831 * Set Threshold for Deactivating Flow Contol for space of
832 * min 1 frame (frame size 1500bytes) in receive fifo
833 */
834 if (rqs == ((4096 / 256) - 1)) {
835 /*
836 * This violates the above formula because of FIFO size
837 * limit therefore overflow may occur inspite of this.
838 */
839 rfd = 0x3; /* Full-3K */
840 rfa = 0x1; /* Full-1.5K */
841 } else if (rqs == ((8192 / 256) - 1)) {
842 rfd = 0x6; /* Full-4K */
843 rfa = 0xa; /* Full-6K */
844 } else if (rqs == ((16384 / 256) - 1)) {
845 rfd = 0x6; /* Full-4K */
846 rfa = 0x12; /* Full-10K */
847 } else {
848 rfd = 0x6; /* Full-4K */
849 rfa = 0x1E; /* Full-16K */
850 }
851
852 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
853 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
854 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
855 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
856 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
857 (rfd <<
858 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
859 (rfa <<
860 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
861 }
862
863 /* Configure MAC */
864
865 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
866 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
867 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200868 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600869 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
870
Fugang Duan37aae5f2020-05-03 22:41:17 +0800871 /* Multicast and Broadcast Queue Enable */
872 setbits_le32(&eqos->mac_regs->unused_0a4,
873 0x00100000);
874 /* enable promise mode */
875 setbits_le32(&eqos->mac_regs->unused_004[1],
876 0x1);
877
Stephen Warren50709602016-10-21 14:46:47 -0600878 /* Set TX flow control parameters */
879 /* Set Pause Time */
880 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
881 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
882 /* Assign priority for TX flow control */
883 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
884 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
885 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
886 /* Assign priority for RX flow control */
887 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
888 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
889 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
890 /* Enable flow control */
891 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
892 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
893 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
894 EQOS_MAC_RX_FLOW_CTRL_RFE);
895
896 clrsetbits_le32(&eqos->mac_regs->configuration,
897 EQOS_MAC_CONFIGURATION_GPSLCE |
898 EQOS_MAC_CONFIGURATION_WD |
899 EQOS_MAC_CONFIGURATION_JD |
900 EQOS_MAC_CONFIGURATION_JE,
901 EQOS_MAC_CONFIGURATION_CST |
902 EQOS_MAC_CONFIGURATION_ACS);
903
904 eqos_write_hwaddr(dev);
905
906 /* Configure DMA */
907
908 /* Enable OSP mode */
909 setbits_le32(&eqos->dma_regs->ch0_tx_control,
910 EQOS_DMA_CH0_TX_CONTROL_OSP);
911
912 /* RX buffer size. Must be a multiple of bus width */
913 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
914 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
915 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
916 EQOS_MAX_PACKET_SIZE <<
917 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
918
Marek Vasut89077732021-01-07 11:12:16 +0100919 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
920 eqos->config->axi_bus_width;
921
Stephen Warren50709602016-10-21 14:46:47 -0600922 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +0100923 EQOS_DMA_CH0_CONTROL_PBLX8 |
924 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -0600925
926 /*
927 * Burst length must be < 1/2 FIFO size.
928 * FIFO size in tqs is encoded as (n / 256) - 1.
929 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
930 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
931 */
932 pbl = tqs + 1;
933 if (pbl > 32)
934 pbl = 32;
935 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
936 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
937 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
938 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
939
940 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
941 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
942 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
943 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
944
945 /* DMA performance configuration */
946 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
947 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
948 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
949 writel(val, &eqos->dma_regs->sysbus_mode);
950
951 /* Set up descriptors */
952
Marek Vasut90cc13a2022-10-09 17:51:45 +0200953 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
954 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +0100955
956 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
957 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
958 eqos->config->ops->eqos_flush_desc(tx_desc);
959 }
960
Stephen Warren50709602016-10-21 14:46:47 -0600961 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +0100962 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Ley Foon Tan963db382022-12-09 14:33:14 +0800963
964 addr64 = (ulong)(eqos->rx_dma_buf + (i * EQOS_MAX_PACKET_SIZE));
965 rx_desc->des0 = lower_32_bits(addr64);
966 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasutd54c98e2020-03-23 02:02:57 +0100967 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800968 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +0100969 eqos->config->ops->eqos_flush_desc(rx_desc);
Ley Foon Tan963db382022-12-09 14:33:14 +0800970 eqos->config->ops->eqos_inval_buffer((void *)addr64, EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -0600971 }
Stephen Warren50709602016-10-21 14:46:47 -0600972
Ley Foon Tan963db382022-12-09 14:33:14 +0800973 addr64 = (ulong)eqos_get_desc(eqos, 0, false);
974 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_haddress);
975 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -0600976 writel(EQOS_DESCRIPTORS_TX - 1,
977 &eqos->dma_regs->ch0_txdesc_ring_length);
978
Ley Foon Tan963db382022-12-09 14:33:14 +0800979 addr64 = (ulong)eqos_get_desc(eqos, 0, true);
980 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_haddress);
981 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -0600982 writel(EQOS_DESCRIPTORS_RX - 1,
983 &eqos->dma_regs->ch0_rxdesc_ring_length);
984
985 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -0600986 setbits_le32(&eqos->dma_regs->ch0_tx_control,
987 EQOS_DMA_CH0_TX_CONTROL_ST);
988 setbits_le32(&eqos->dma_regs->ch0_rx_control,
989 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800990 setbits_le32(&eqos->mac_regs->configuration,
991 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -0600992
993 /* TX tail pointer not written until we need to TX a packet */
994 /*
995 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
996 * first descriptor, implying all descriptors were available. However,
997 * that's not distinguishable from none of the descriptors being
998 * available.
999 */
Marek Vasut89077732021-01-07 11:12:16 +01001000 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001001 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1002
1003 eqos->started = true;
1004
1005 debug("%s: OK\n", __func__);
1006 return 0;
1007
1008err_shutdown_phy:
1009 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001010err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001011 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001012err:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001013 pr_err("FAILED: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001014 return ret;
1015}
1016
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001017static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001018{
1019 struct eqos_priv *eqos = dev_get_priv(dev);
1020 int i;
1021
1022 debug("%s(dev=%p):\n", __func__, dev);
1023
1024 if (!eqos->started)
1025 return;
1026 eqos->started = false;
1027 eqos->reg_access_ok = false;
1028
1029 /* Disable TX DMA */
1030 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1031 EQOS_DMA_CH0_TX_CONTROL_ST);
1032
1033 /* Wait for TX all packets to drain out of MTL */
1034 for (i = 0; i < 1000000; i++) {
1035 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1036 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1037 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1038 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1039 if ((trcsts != 1) && (!txqsts))
1040 break;
1041 }
1042
1043 /* Turn off MAC TX and RX */
1044 clrbits_le32(&eqos->mac_regs->configuration,
1045 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1046
1047 /* Wait for all RX packets to drain out of MTL */
1048 for (i = 0; i < 1000000; i++) {
1049 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1050 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1051 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1052 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1053 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1054 if ((!prxq) && (!rxqsts))
1055 break;
1056 }
1057
1058 /* Turn off RX DMA */
1059 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1060 EQOS_DMA_CH0_RX_CONTROL_SR);
1061
1062 if (eqos->phy) {
1063 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001064 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001065 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001066
1067 debug("%s: OK\n", __func__);
1068}
1069
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001070static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001071{
1072 struct eqos_priv *eqos = dev_get_priv(dev);
1073 struct eqos_desc *tx_desc;
1074 int i;
1075
1076 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1077 length);
1078
1079 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001080 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001081
Marek Vasut89077732021-01-07 11:12:16 +01001082 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001083 eqos->tx_desc_idx++;
1084 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1085
Ley Foon Tan963db382022-12-09 14:33:14 +08001086 tx_desc->des0 = lower_32_bits((ulong)eqos->tx_dma_buf);
1087 tx_desc->des1 = upper_32_bits((ulong)eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001088 tx_desc->des2 = length;
1089 /*
1090 * Make sure that if HW sees the _OWN write below, it will see all the
1091 * writes to the rest of the descriptor too.
1092 */
1093 mb();
1094 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001095 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001096
Marek Vasut89077732021-01-07 11:12:16 +01001097 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001098 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001099
1100 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001101 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001102 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1103 return 0;
1104 udelay(1);
1105 }
1106
1107 debug("%s: TX timeout\n", __func__);
1108
1109 return -ETIMEDOUT;
1110}
1111
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001112static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001113{
1114 struct eqos_priv *eqos = dev_get_priv(dev);
1115 struct eqos_desc *rx_desc;
1116 int length;
1117
Marek Vasut89077732021-01-07 11:12:16 +01001118 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001119 eqos->config->ops->eqos_inval_desc(rx_desc);
Jonas Karlmane2c45462023-10-01 19:17:18 +00001120 if (rx_desc->des3 & EQOS_DESC3_OWN)
Stephen Warren50709602016-10-21 14:46:47 -06001121 return -EAGAIN;
Jonas Karlmane2c45462023-10-01 19:17:18 +00001122
1123 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
Stephen Warren50709602016-10-21 14:46:47 -06001124
1125 *packetp = eqos->rx_dma_buf +
1126 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1127 length = rx_desc->des3 & 0x7fff;
1128 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1129
Christophe Roullier6beb7802019-05-17 15:08:44 +02001130 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001131
1132 return length;
1133}
1134
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001135static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001136{
1137 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001138 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001139 uchar *packet_expected;
Patrice Chotard7b1baf32024-04-05 18:15:29 +02001140 struct eqos_desc *rx_desc = NULL;
Stephen Warren50709602016-10-21 14:46:47 -06001141
1142 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1143
1144 packet_expected = eqos->rx_dma_buf +
1145 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1146 if (packet != packet_expected) {
1147 debug("%s: Unexpected packet (expected %p)\n", __func__,
1148 packet_expected);
1149 return -EINVAL;
1150 }
1151
Fugang Duan37aae5f2020-05-03 22:41:17 +08001152 eqos->config->ops->eqos_inval_buffer(packet, length);
1153
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001154 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1155 for (idx = eqos->rx_desc_idx - idx_mask;
1156 idx <= eqos->rx_desc_idx;
1157 idx++) {
Ley Foon Tan963db382022-12-09 14:33:14 +08001158 ulong addr64;
1159
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001160 rx_desc = eqos_get_desc(eqos, idx, true);
1161 rx_desc->des0 = 0;
Ley Foon Tan963db382022-12-09 14:33:14 +08001162 rx_desc->des1 = 0;
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001163 mb();
1164 eqos->config->ops->eqos_flush_desc(rx_desc);
1165 eqos->config->ops->eqos_inval_buffer(packet, length);
Ley Foon Tan963db382022-12-09 14:33:14 +08001166 addr64 = (ulong)(eqos->rx_dma_buf + (idx * EQOS_MAX_PACKET_SIZE));
1167 rx_desc->des0 = lower_32_bits(addr64);
1168 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001169 rx_desc->des2 = 0;
1170 /*
1171 * Make sure that if HW sees the _OWN write below,
1172 * it will see all the writes to the rest of the
1173 * descriptor too.
1174 */
1175 mb();
1176 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1177 eqos->config->ops->eqos_flush_desc(rx_desc);
1178 }
1179 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1180 }
Stephen Warren50709602016-10-21 14:46:47 -06001181
1182 eqos->rx_desc_idx++;
1183 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1184
1185 return 0;
1186}
1187
1188static int eqos_probe_resources_core(struct udevice *dev)
1189{
1190 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001191 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001192 int ret;
1193
1194 debug("%s(dev=%p):\n", __func__, dev);
1195
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001196 /* Maximum distance between neighboring descriptors, in Bytes. */
1197 desc_step = sizeof(struct eqos_desc) +
1198 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1199 if (desc_step < ARCH_DMA_MINALIGN) {
1200 /*
1201 * The EQoS hardware implementation cannot place one descriptor
1202 * per cacheline, it is necessary to place multiple descriptors
1203 * per cacheline in memory and do cache management carefully.
1204 */
1205 eqos->desc_size = BIT(fls(desc_step) - 1);
1206 } else {
1207 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1208 (unsigned int)ARCH_DMA_MINALIGN);
1209 }
1210 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001211
1212 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1213 if (!eqos->tx_descs) {
1214 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001215 ret = -ENOMEM;
1216 goto err;
1217 }
Stephen Warren50709602016-10-21 14:46:47 -06001218
Marek Vasut90cc13a2022-10-09 17:51:45 +02001219 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1220 if (!eqos->rx_descs) {
1221 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1222 ret = -ENOMEM;
1223 goto err_free_tx_descs;
1224 }
1225
Stephen Warren50709602016-10-21 14:46:47 -06001226 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1227 if (!eqos->tx_dma_buf) {
1228 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1229 ret = -ENOMEM;
1230 goto err_free_descs;
1231 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001232 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001233
1234 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1235 if (!eqos->rx_dma_buf) {
1236 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1237 ret = -ENOMEM;
1238 goto err_free_tx_dma_buf;
1239 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001240 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001241
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001242 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1243 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1244
Stephen Warren50709602016-10-21 14:46:47 -06001245 debug("%s: OK\n", __func__);
1246 return 0;
1247
Stephen Warren50709602016-10-21 14:46:47 -06001248err_free_tx_dma_buf:
1249 free(eqos->tx_dma_buf);
1250err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001251 eqos_free_descs(eqos->rx_descs);
1252err_free_tx_descs:
1253 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001254err:
1255
1256 debug("%s: returns %d\n", __func__, ret);
1257 return ret;
1258}
1259
1260static int eqos_remove_resources_core(struct udevice *dev)
1261{
1262 struct eqos_priv *eqos = dev_get_priv(dev);
1263
1264 debug("%s(dev=%p):\n", __func__, dev);
1265
Stephen Warren50709602016-10-21 14:46:47 -06001266 free(eqos->rx_dma_buf);
1267 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001268 eqos_free_descs(eqos->rx_descs);
1269 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001270
1271 debug("%s: OK\n", __func__);
1272 return 0;
1273}
1274
1275static int eqos_probe_resources_tegra186(struct udevice *dev)
1276{
1277 struct eqos_priv *eqos = dev_get_priv(dev);
1278 int ret;
1279
1280 debug("%s(dev=%p):\n", __func__, dev);
1281
1282 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1283 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001284 pr_err("reset_get_by_name(rst) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001285 return ret;
1286 }
1287
1288 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1289 &eqos->phy_reset_gpio,
1290 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1291 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001292 pr_err("gpio_request_by_name(phy reset) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001293 goto err_free_reset_eqos;
1294 }
1295
1296 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1297 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001298 pr_err("clk_get_by_name(slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001299 goto err_free_gpio_phy_reset;
1300 }
1301
1302 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1303 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001304 pr_err("clk_get_by_name(master_bus) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001305 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001306 }
1307
1308 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1309 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001310 pr_err("clk_get_by_name(rx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001311 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001312 }
1313
1314 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1315 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001316 pr_err("clk_get_by_name(ptp_ref) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001317 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001318 }
1319
1320 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1321 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001322 pr_err("clk_get_by_name(tx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001323 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001324 }
1325
1326 debug("%s: OK\n", __func__);
1327 return 0;
1328
Stephen Warren50709602016-10-21 14:46:47 -06001329err_free_gpio_phy_reset:
1330 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1331err_free_reset_eqos:
1332 reset_free(&eqos->reset_ctl);
1333
1334 debug("%s: returns %d\n", __func__, ret);
1335 return ret;
1336}
1337
Marek Behúnbc194772022-04-07 00:33:01 +02001338static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001339{
1340 return PHY_INTERFACE_MODE_MII;
1341}
1342
Stephen Warren50709602016-10-21 14:46:47 -06001343static int eqos_remove_resources_tegra186(struct udevice *dev)
1344{
1345 struct eqos_priv *eqos = dev_get_priv(dev);
1346
1347 debug("%s(dev=%p):\n", __func__, dev);
1348
Stephen Warren50709602016-10-21 14:46:47 -06001349 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1350 reset_free(&eqos->reset_ctl);
1351
1352 debug("%s: OK\n", __func__);
1353 return 0;
1354}
1355
1356static int eqos_probe(struct udevice *dev)
1357{
1358 struct eqos_priv *eqos = dev_get_priv(dev);
1359 int ret;
1360
1361 debug("%s(dev=%p):\n", __func__, dev);
1362
1363 eqos->dev = dev;
1364 eqos->config = (void *)dev_get_driver_data(dev);
1365
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001366 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001367 if (eqos->regs == FDT_ADDR_T_NONE) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001368 pr_err("dev_read_addr() failed\n");
Stephen Warren50709602016-10-21 14:46:47 -06001369 return -ENODEV;
1370 }
1371 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1372 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1373 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1374 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1375
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001376 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1377
Stephen Warren50709602016-10-21 14:46:47 -06001378 ret = eqos_probe_resources_core(dev);
1379 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001380 pr_err("eqos_probe_resources_core() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001381 return ret;
1382 }
1383
Christophe Roullier6beb7802019-05-17 15:08:44 +02001384 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001385 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001386 pr_err("eqos_probe_resources() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001387 goto err_remove_resources_core;
1388 }
1389
Marek Vasut30b28c42021-11-13 03:23:52 +01001390 ret = eqos->config->ops->eqos_start_clks(dev);
1391 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001392 pr_err("eqos_start_clks() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +01001393 goto err_remove_resources_tegra;
1394 }
1395
Ye Liad122b72020-05-03 22:41:15 +08001396#ifdef CONFIG_DM_ETH_PHY
1397 eqos->mii = eth_phy_get_mdio_bus(dev);
1398#endif
Stephen Warren50709602016-10-21 14:46:47 -06001399 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001400 eqos->mii = mdio_alloc();
1401 if (!eqos->mii) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001402 pr_err("mdio_alloc() failed\n");
Ye Liad122b72020-05-03 22:41:15 +08001403 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001404 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001405 }
1406 eqos->mii->read = eqos_mdio_read;
1407 eqos->mii->write = eqos_mdio_write;
1408 eqos->mii->priv = eqos;
1409 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001410
Ye Liad122b72020-05-03 22:41:15 +08001411 ret = mdio_register(eqos->mii);
1412 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001413 pr_err("mdio_register() failed: %d\n", ret);
Ye Liad122b72020-05-03 22:41:15 +08001414 goto err_free_mdio;
1415 }
Stephen Warren50709602016-10-21 14:46:47 -06001416 }
1417
Ye Liad122b72020-05-03 22:41:15 +08001418#ifdef CONFIG_DM_ETH_PHY
1419 eth_phy_set_mdio_bus(dev, eqos->mii);
1420#endif
1421
Stephen Warren50709602016-10-21 14:46:47 -06001422 debug("%s: OK\n", __func__);
1423 return 0;
1424
1425err_free_mdio:
1426 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001427err_stop_clks:
1428 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001429err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001430 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001431err_remove_resources_core:
1432 eqos_remove_resources_core(dev);
1433
1434 debug("%s: returns %d\n", __func__, ret);
1435 return ret;
1436}
1437
1438static int eqos_remove(struct udevice *dev)
1439{
1440 struct eqos_priv *eqos = dev_get_priv(dev);
1441
1442 debug("%s(dev=%p):\n", __func__, dev);
1443
1444 mdio_unregister(eqos->mii);
1445 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001446 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001447 eqos->config->ops->eqos_remove_resources(dev);
1448
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001449 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001450
1451 debug("%s: OK\n", __func__);
1452 return 0;
1453}
1454
Peng Fanc0a59952022-07-26 16:41:14 +08001455int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001456{
1457 return 0;
1458}
1459
Stephen Warren50709602016-10-21 14:46:47 -06001460static const struct eth_ops eqos_ops = {
1461 .start = eqos_start,
1462 .stop = eqos_stop,
1463 .send = eqos_send,
1464 .recv = eqos_recv,
1465 .free_pkt = eqos_free_pkt,
1466 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001467 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001468};
1469
Christophe Roullier6beb7802019-05-17 15:08:44 +02001470static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001471 .eqos_inval_desc = eqos_inval_desc_generic,
1472 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001473 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1474 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1475 .eqos_probe_resources = eqos_probe_resources_tegra186,
1476 .eqos_remove_resources = eqos_remove_resources_tegra186,
1477 .eqos_stop_resets = eqos_stop_resets_tegra186,
1478 .eqos_start_resets = eqos_start_resets_tegra186,
1479 .eqos_stop_clks = eqos_stop_clks_tegra186,
1480 .eqos_start_clks = eqos_start_clks_tegra186,
1481 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1482 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1483 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001484 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001485 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1486};
1487
Patrick Delaunay68083902020-06-08 11:27:19 +02001488static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001489 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001490 .mdio_wait = 10,
1491 .swr_wait = 10,
1492 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1493 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001494 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001495 .interface = eqos_get_interface_tegra186,
1496 .ops = &eqos_tegra186_ops
1497};
1498
Stephen Warren50709602016-10-21 14:46:47 -06001499static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001500#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001501 {
1502 .compatible = "nvidia,tegra186-eqos",
1503 .data = (ulong)&eqos_tegra186_config
1504 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001505#endif
1506#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001507 {
Christophe Roullier25a16862024-03-26 13:07:31 +01001508 .compatible = "st,stm32mp13-dwmac",
1509 .data = (ulong)&eqos_stm32mp13_config
1510 },
1511 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001512 .compatible = "st,stm32mp1-dwmac",
Marek Vasut944ba372024-03-26 13:07:23 +01001513 .data = (ulong)&eqos_stm32mp15_config
Christophe Roullier6beb7802019-05-17 15:08:44 +02001514 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001515#endif
1516#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001517 {
Marek Vasut7af11382022-02-26 04:36:37 +01001518 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001519 .data = (ulong)&eqos_imx_config
1520 },
Sébastien Szymanski28b7fc42023-10-17 11:44:58 +02001521 {
1522 .compatible = "nxp,imx93-dwmac-eqos",
1523 .data = (ulong)&eqos_imx_config
1524 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001525#endif
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001526#if IS_ENABLED(CONFIG_DWC_ETH_QOS_ROCKCHIP)
1527 {
1528 .compatible = "rockchip,rk3568-gmac",
1529 .data = (ulong)&eqos_rockchip_config
1530 },
Jonas Karlman1b615702023-10-01 19:17:20 +00001531 {
1532 .compatible = "rockchip,rk3588-gmac",
1533 .data = (ulong)&eqos_rockchip_config
1534 },
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001535#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301536#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1537 {
1538 .compatible = "qcom,qcs404-ethqos",
1539 .data = (ulong)&eqos_qcom_config
1540 },
1541#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001542#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1543 {
1544 .compatible = "starfive,jh7110-dwmac",
1545 .data = (ulong)&eqos_jh7110_config
1546 },
1547#endif
Stephen Warren50709602016-10-21 14:46:47 -06001548 { }
1549};
1550
1551U_BOOT_DRIVER(eth_eqos) = {
1552 .name = "eth_eqos",
1553 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001554 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001555 .probe = eqos_probe,
1556 .remove = eqos_remove,
1557 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001558 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001559 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001560};