blob: 32a5d52165ac7d11c7d97c1e73de6202e2750ecf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren50709602016-10-21 14:46:47 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
4 *
Stephen Warren50709602016-10-21 14:46:47 -06005 * Portions based on U-Boot's rtl8169.c.
6 */
7
8/*
9 * This driver supports the Synopsys Designware Ethernet QOS (Quality Of
10 * Service) IP block. The IP supports multiple options for bus type, clocking/
11 * reset structure, and feature list.
12 *
13 * The driver is written such that generic core logic is kept separate from
14 * configuration-specific logic. Code that interacts with configuration-
15 * specific resources is split out into separate functions to avoid polluting
16 * common code. If/when this driver is enhanced to support multiple
17 * configurations, the core code should be adapted to call all configuration-
18 * specific functions through function pointers, with the definition of those
19 * function pointers being supplied by struct udevice_id eqos_ids[]'s .data
20 * field.
21 *
22 * The following configurations are currently supported:
23 * tegra186:
24 * NVIDIA's Tegra186 chip. This configuration uses an AXI master/DMA bus, an
25 * AHB slave/register bus, contains the DMA, MTL, and MAC sub-blocks, and
26 * supports a single RGMII PHY. This configuration also has SW control over
27 * all clock and reset signals to the HW block.
28 */
Patrick Delaunay9e6ed382020-09-09 18:30:06 +020029
Patrick Delaunay57872842021-07-20 20:15:29 +020030#define LOG_CATEGORY UCLASS_ETH
31
Stephen Warren50709602016-10-21 14:46:47 -060032#include <common.h>
33#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070034#include <cpu_func.h>
Stephen Warren50709602016-10-21 14:46:47 -060035#include <dm.h>
36#include <errno.h>
Patrick Delaunay41729272022-06-30 11:09:41 +020037#include <eth_phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060038#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070039#include <malloc.h>
Stephen Warren50709602016-10-21 14:46:47 -060040#include <memalign.h>
41#include <miiphy.h>
42#include <net.h>
43#include <netdev.h>
44#include <phy.h>
45#include <reset.h>
46#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060047#include <asm/cache.h>
Stephen Warren50709602016-10-21 14:46:47 -060048#include <asm/gpio.h>
49#include <asm/io.h>
Fugang Duandd455e62020-05-03 22:41:18 +080050#ifdef CONFIG_ARCH_IMX8M
51#include <asm/arch/clock.h>
52#include <asm/mach-imx/sys_proto.h>
53#endif
Simon Glassdbd79542020-05-10 11:40:11 -060054#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060055#include <linux/printk.h>
Stephen Warren50709602016-10-21 14:46:47 -060056
Peng Fanc0a59952022-07-26 16:41:14 +080057#include "dwc_eth_qos.h"
Stephen Warren50709602016-10-21 14:46:47 -060058
59/*
60 * TX and RX descriptors are 16 bytes. This causes problems with the cache
61 * maintenance on CPUs where the cache-line size exceeds the size of these
62 * descriptors. What will happen is that when the driver receives a packet
63 * it will be immediately requeued for the hardware to reuse. The CPU will
64 * therefore need to flush the cache-line containing the descriptor, which
65 * will cause all other descriptors in the same cache-line to be flushed
66 * along with it. If one of those descriptors had been written to by the
67 * device those changes (and the associated packet) will be lost.
68 *
69 * To work around this, we make use of non-cached memory if available. If
70 * descriptors are mapped uncached there's no need to manually flush them
71 * or invalidate them.
72 *
73 * Note that this only applies to descriptors. The packet data buffers do
74 * not have the same constraints since they are 1536 bytes large, so they
75 * are unlikely to share cache-lines.
76 */
Marek Vasut89077732021-01-07 11:12:16 +010077static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -060078{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020079 return memalign(ARCH_DMA_MINALIGN, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060080}
81
82static void eqos_free_descs(void *descs)
83{
Stephen Warren50709602016-10-21 14:46:47 -060084 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -060085}
86
Marek Vasut89077732021-01-07 11:12:16 +010087static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
88 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -060089{
Marek Vasut90cc13a2022-10-09 17:51:45 +020090 return (rx ? eqos->rx_descs : eqos->tx_descs) +
91 (num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -060092}
93
Peng Fanc0a59952022-07-26 16:41:14 +080094void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -060095{
Marek Vasut3e8a1be2022-10-09 17:51:46 +020096 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +010097 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
98 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +020099
100 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600101}
102
Peng Fanc0a59952022-07-26 16:41:14 +0800103void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200104{
Marek Vasut3e8a1be2022-10-09 17:51:46 +0200105 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut89077732021-01-07 11:12:16 +0100106 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
107 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200108
109 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200110}
111
Marek Vasut7b6fec22023-03-06 15:53:45 +0100112static void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600113{
114 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
115 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
116
117 invalidate_dcache_range(start, end);
118}
119
Peng Fanc0a59952022-07-26 16:41:14 +0800120void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200121{
122 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
123 unsigned long end = roundup((unsigned long)buf + size,
124 ARCH_DMA_MINALIGN);
125
126 invalidate_dcache_range(start, end);
127}
128
129static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600130{
131 flush_cache((unsigned long)buf, size);
132}
133
Peng Fanc0a59952022-07-26 16:41:14 +0800134void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200135{
136 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
137 unsigned long end = roundup((unsigned long)buf + size,
138 ARCH_DMA_MINALIGN);
139
140 flush_dcache_range(start, end);
141}
142
Stephen Warren50709602016-10-21 14:46:47 -0600143static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
144{
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100145 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
146 EQOS_MAC_MDIO_ADDRESS_GB, false,
147 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600148}
149
150static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
151 int mdio_reg)
152{
153 struct eqos_priv *eqos = bus->priv;
154 u32 val;
155 int ret;
156
157 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
158 mdio_reg);
159
160 ret = eqos_mdio_wait_idle(eqos);
161 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200162 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600163 return ret;
164 }
165
166 val = readl(&eqos->mac_regs->mdio_address);
167 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
168 EQOS_MAC_MDIO_ADDRESS_C45E;
169 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
170 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200171 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600172 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
173 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
174 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
175 EQOS_MAC_MDIO_ADDRESS_GB;
176 writel(val, &eqos->mac_regs->mdio_address);
177
Christophe Roullier6beb7802019-05-17 15:08:44 +0200178 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600179
180 ret = eqos_mdio_wait_idle(eqos);
181 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200182 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600183 return ret;
184 }
185
186 val = readl(&eqos->mac_regs->mdio_data);
187 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
188
189 debug("%s: val=%x\n", __func__, val);
190
191 return val;
192}
193
194static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
195 int mdio_reg, u16 mdio_val)
196{
197 struct eqos_priv *eqos = bus->priv;
198 u32 val;
199 int ret;
200
201 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
202 mdio_addr, mdio_reg, mdio_val);
203
204 ret = eqos_mdio_wait_idle(eqos);
205 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200206 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600207 return ret;
208 }
209
210 writel(mdio_val, &eqos->mac_regs->mdio_data);
211
212 val = readl(&eqos->mac_regs->mdio_address);
213 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
214 EQOS_MAC_MDIO_ADDRESS_C45E;
215 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
216 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200217 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600218 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
219 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
220 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
221 EQOS_MAC_MDIO_ADDRESS_GB;
222 writel(val, &eqos->mac_regs->mdio_address);
223
Christophe Roullier6beb7802019-05-17 15:08:44 +0200224 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600225
226 ret = eqos_mdio_wait_idle(eqos);
227 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200228 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600229 return ret;
230 }
231
232 return 0;
233}
234
235static int eqos_start_clks_tegra186(struct udevice *dev)
236{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800237#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600238 struct eqos_priv *eqos = dev_get_priv(dev);
239 int ret;
240
241 debug("%s(dev=%p):\n", __func__, dev);
242
243 ret = clk_enable(&eqos->clk_slave_bus);
244 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200245 pr_err("clk_enable(clk_slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600246 goto err;
247 }
248
249 ret = clk_enable(&eqos->clk_master_bus);
250 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200251 pr_err("clk_enable(clk_master_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600252 goto err_disable_clk_slave_bus;
253 }
254
255 ret = clk_enable(&eqos->clk_rx);
256 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200257 pr_err("clk_enable(clk_rx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600258 goto err_disable_clk_master_bus;
259 }
260
261 ret = clk_enable(&eqos->clk_ptp_ref);
262 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200263 pr_err("clk_enable(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600264 goto err_disable_clk_rx;
265 }
266
267 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
268 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200269 pr_err("clk_set_rate(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600270 goto err_disable_clk_ptp_ref;
271 }
272
273 ret = clk_enable(&eqos->clk_tx);
274 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200275 pr_err("clk_enable(clk_tx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600276 goto err_disable_clk_ptp_ref;
277 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800278#endif
Stephen Warren50709602016-10-21 14:46:47 -0600279
280 debug("%s: OK\n", __func__);
281 return 0;
282
Fugang Duan37aae5f2020-05-03 22:41:17 +0800283#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600284err_disable_clk_ptp_ref:
285 clk_disable(&eqos->clk_ptp_ref);
286err_disable_clk_rx:
287 clk_disable(&eqos->clk_rx);
288err_disable_clk_master_bus:
289 clk_disable(&eqos->clk_master_bus);
290err_disable_clk_slave_bus:
291 clk_disable(&eqos->clk_slave_bus);
292err:
293 debug("%s: FAILED: %d\n", __func__, ret);
294 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800295#endif
Stephen Warren50709602016-10-21 14:46:47 -0600296}
297
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200298static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600299{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800300#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600301 struct eqos_priv *eqos = dev_get_priv(dev);
302
303 debug("%s(dev=%p):\n", __func__, dev);
304
305 clk_disable(&eqos->clk_tx);
306 clk_disable(&eqos->clk_ptp_ref);
307 clk_disable(&eqos->clk_rx);
308 clk_disable(&eqos->clk_master_bus);
309 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800310#endif
Stephen Warren50709602016-10-21 14:46:47 -0600311
312 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200313 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600314}
315
316static int eqos_start_resets_tegra186(struct udevice *dev)
317{
318 struct eqos_priv *eqos = dev_get_priv(dev);
319 int ret;
320
321 debug("%s(dev=%p):\n", __func__, dev);
322
323 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
324 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200325 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600326 return ret;
327 }
328
329 udelay(2);
330
331 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
332 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200333 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600334 return ret;
335 }
336
337 ret = reset_assert(&eqos->reset_ctl);
338 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200339 pr_err("reset_assert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600340 return ret;
341 }
342
343 udelay(2);
344
345 ret = reset_deassert(&eqos->reset_ctl);
346 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200347 pr_err("reset_deassert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600348 return ret;
349 }
350
351 debug("%s: OK\n", __func__);
352 return 0;
353}
354
355static int eqos_stop_resets_tegra186(struct udevice *dev)
356{
357 struct eqos_priv *eqos = dev_get_priv(dev);
358
359 reset_assert(&eqos->reset_ctl);
360 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
361
Christophe Roullier6beb7802019-05-17 15:08:44 +0200362 return 0;
363}
364
Stephen Warren50709602016-10-21 14:46:47 -0600365static int eqos_calibrate_pads_tegra186(struct udevice *dev)
366{
367 struct eqos_priv *eqos = dev_get_priv(dev);
368 int ret;
369
370 debug("%s(dev=%p):\n", __func__, dev);
371
372 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
373 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
374
375 udelay(1);
376
377 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
378 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
379
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100380 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
381 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600382 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200383 pr_err("calibrate didn't start\n");
Stephen Warren50709602016-10-21 14:46:47 -0600384 goto failed;
385 }
386
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100387 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
388 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600389 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200390 pr_err("calibrate didn't finish\n");
Stephen Warren50709602016-10-21 14:46:47 -0600391 goto failed;
392 }
393
394 ret = 0;
395
396failed:
397 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
398 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
399
400 debug("%s: returns %d\n", __func__, ret);
401
402 return ret;
403}
404
405static int eqos_disable_calibration_tegra186(struct udevice *dev)
406{
407 struct eqos_priv *eqos = dev_get_priv(dev);
408
409 debug("%s(dev=%p):\n", __func__, dev);
410
411 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
412 EQOS_AUTO_CAL_CONFIG_ENABLE);
413
414 return 0;
415}
416
417static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
418{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800419#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600420 struct eqos_priv *eqos = dev_get_priv(dev);
421
422 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800423#else
424 return 0;
425#endif
Stephen Warren50709602016-10-21 14:46:47 -0600426}
427
428static int eqos_set_full_duplex(struct udevice *dev)
429{
430 struct eqos_priv *eqos = dev_get_priv(dev);
431
432 debug("%s(dev=%p):\n", __func__, dev);
433
434 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
435
436 return 0;
437}
438
439static int eqos_set_half_duplex(struct udevice *dev)
440{
441 struct eqos_priv *eqos = dev_get_priv(dev);
442
443 debug("%s(dev=%p):\n", __func__, dev);
444
445 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
446
447 /* WAR: Flush TX queue when switching to half-duplex */
448 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
449 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
450
451 return 0;
452}
453
454static int eqos_set_gmii_speed(struct udevice *dev)
455{
456 struct eqos_priv *eqos = dev_get_priv(dev);
457
458 debug("%s(dev=%p):\n", __func__, dev);
459
460 clrbits_le32(&eqos->mac_regs->configuration,
461 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
462
463 return 0;
464}
465
466static int eqos_set_mii_speed_100(struct udevice *dev)
467{
468 struct eqos_priv *eqos = dev_get_priv(dev);
469
470 debug("%s(dev=%p):\n", __func__, dev);
471
472 setbits_le32(&eqos->mac_regs->configuration,
473 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
474
475 return 0;
476}
477
478static int eqos_set_mii_speed_10(struct udevice *dev)
479{
480 struct eqos_priv *eqos = dev_get_priv(dev);
481
482 debug("%s(dev=%p):\n", __func__, dev);
483
484 clrsetbits_le32(&eqos->mac_regs->configuration,
485 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
486
487 return 0;
488}
489
490static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
491{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800492#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600493 struct eqos_priv *eqos = dev_get_priv(dev);
494 ulong rate;
495 int ret;
496
497 debug("%s(dev=%p):\n", __func__, dev);
498
499 switch (eqos->phy->speed) {
500 case SPEED_1000:
501 rate = 125 * 1000 * 1000;
502 break;
503 case SPEED_100:
504 rate = 25 * 1000 * 1000;
505 break;
506 case SPEED_10:
507 rate = 2.5 * 1000 * 1000;
508 break;
509 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200510 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600511 return -EINVAL;
512 }
513
514 ret = clk_set_rate(&eqos->clk_tx, rate);
515 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200516 pr_err("clk_set_rate(tx_clk, %lu) failed: %d\n", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600517 return ret;
518 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800519#endif
Stephen Warren50709602016-10-21 14:46:47 -0600520
521 return 0;
522}
523
524static int eqos_adjust_link(struct udevice *dev)
525{
526 struct eqos_priv *eqos = dev_get_priv(dev);
527 int ret;
528 bool en_calibration;
529
530 debug("%s(dev=%p):\n", __func__, dev);
531
532 if (eqos->phy->duplex)
533 ret = eqos_set_full_duplex(dev);
534 else
535 ret = eqos_set_half_duplex(dev);
536 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200537 pr_err("eqos_set_*_duplex() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600538 return ret;
539 }
540
541 switch (eqos->phy->speed) {
542 case SPEED_1000:
543 en_calibration = true;
544 ret = eqos_set_gmii_speed(dev);
545 break;
546 case SPEED_100:
547 en_calibration = true;
548 ret = eqos_set_mii_speed_100(dev);
549 break;
550 case SPEED_10:
551 en_calibration = false;
552 ret = eqos_set_mii_speed_10(dev);
553 break;
554 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200555 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600556 return -EINVAL;
557 }
558 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200559 pr_err("eqos_set_*mii_speed*() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600560 return ret;
561 }
562
563 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200564 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600565 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200566 pr_err("eqos_calibrate_pads() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200567 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600568 return ret;
569 }
570 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200571 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600572 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200573 pr_err("eqos_disable_calibration() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200574 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600575 return ret;
576 }
577 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200578 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600579 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200580 pr_err("eqos_set_tx_clk_speed() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600581 return ret;
582 }
583
584 return 0;
585}
586
587static int eqos_write_hwaddr(struct udevice *dev)
588{
Simon Glassfa20e932020-12-03 16:55:20 -0700589 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600590 struct eqos_priv *eqos = dev_get_priv(dev);
591 uint32_t val;
592
593 /*
594 * This function may be called before start() or after stop(). At that
595 * time, on at least some configurations of the EQoS HW, all clocks to
596 * the EQoS HW block will be stopped, and a reset signal applied. If
597 * any register access is attempted in this state, bus timeouts or CPU
598 * hangs may occur. This check prevents that.
599 *
600 * A simple solution to this problem would be to not implement
601 * write_hwaddr(), since start() always writes the MAC address into HW
602 * anyway. However, it is desirable to implement write_hwaddr() to
603 * support the case of SW that runs subsequent to U-Boot which expects
604 * the MAC address to already be programmed into the EQoS registers,
605 * which must happen irrespective of whether the U-Boot user (or
606 * scripts) actually made use of the EQoS device, and hence
607 * irrespective of whether start() was ever called.
608 *
609 * Note that this requirement by subsequent SW is not valid for
610 * Tegra186, and is likely not valid for any non-PCI instantiation of
611 * the EQoS HW block. This function is implemented solely as
612 * future-proofing with the expectation the driver will eventually be
613 * ported to some system where the expectation above is true.
614 */
615 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
616 return 0;
617
618 /* Update the MAC address */
619 val = (plat->enetaddr[5] << 8) |
620 (plat->enetaddr[4]);
621 writel(val, &eqos->mac_regs->address0_high);
622 val = (plat->enetaddr[3] << 24) |
623 (plat->enetaddr[2] << 16) |
624 (plat->enetaddr[1] << 8) |
625 (plat->enetaddr[0]);
626 writel(val, &eqos->mac_regs->address0_low);
627
628 return 0;
629}
630
Ye Li3fb1a0e2020-05-03 22:41:20 +0800631static int eqos_read_rom_hwaddr(struct udevice *dev)
632{
Simon Glassfa20e932020-12-03 16:55:20 -0700633 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800634 struct eqos_priv *eqos = dev_get_priv(dev);
635 int ret;
636
637 ret = eqos->config->ops->eqos_get_enetaddr(dev);
638 if (ret < 0)
639 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800640
Ye Li3fb1a0e2020-05-03 22:41:20 +0800641 return !is_valid_ethaddr(pdata->enetaddr);
642}
643
Ye Li2f2aa482022-07-26 16:41:16 +0800644static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
645{
646 struct ofnode_phandle_args phandle_args;
647 int reg;
648
649 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
650 &phandle_args)) {
651 debug("Failed to find phy-handle");
652 return -ENODEV;
653 }
654
655 priv->phy_of_node = phandle_args.node;
656
657 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
658
659 return reg;
660}
661
Stephen Warren50709602016-10-21 14:46:47 -0600662static int eqos_start(struct udevice *dev)
663{
664 struct eqos_priv *eqos = dev_get_priv(dev);
665 int ret, i;
666 ulong rate;
667 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
668 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100669 ulong desc_pad;
Ley Foon Tan963db382022-12-09 14:33:14 +0800670 ulong addr64;
Stephen Warren50709602016-10-21 14:46:47 -0600671
672 debug("%s(dev=%p):\n", __func__, dev);
673
674 eqos->tx_desc_idx = 0;
675 eqos->rx_desc_idx = 0;
676
Christophe Roullier6beb7802019-05-17 15:08:44 +0200677 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600678 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200679 pr_err("eqos_start_resets() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100680 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600681 }
682
683 udelay(10);
684
685 eqos->reg_access_ok = true;
686
Marek Vasute66825a2023-03-06 15:53:46 +0100687 /*
688 * Assert the SWR first, the actually reset the MAC and to latch in
689 * e.g. i.MX8M Plus GPR[1] content, which selects interface mode.
690 */
691 setbits_le32(&eqos->dma_regs->mode, EQOS_DMA_MODE_SWR);
692
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100693 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200694 EQOS_DMA_MODE_SWR, false,
695 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600696 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200697 pr_err("EQOS_DMA_MODE_SWR stuck\n");
Stephen Warren50709602016-10-21 14:46:47 -0600698 goto err_stop_resets;
699 }
700
Christophe Roullier6beb7802019-05-17 15:08:44 +0200701 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600702 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200703 pr_err("eqos_calibrate_pads() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600704 goto err_stop_resets;
705 }
Sumit Gargab973c92023-02-01 19:28:53 +0530706
707 if (eqos->config->ops->eqos_get_tick_clk_rate) {
708 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600709
Sumit Gargab973c92023-02-01 19:28:53 +0530710 val = (rate / 1000000) - 1;
711 writel(val, &eqos->mac_regs->us_tic_counter);
712 }
Stephen Warren50709602016-10-21 14:46:47 -0600713
Christophe Roullier6beb7802019-05-17 15:08:44 +0200714 /*
715 * if PHY was already connected and configured,
716 * don't need to reconnect/reconfigure again
717 */
Stephen Warren50709602016-10-21 14:46:47 -0600718 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800719 int addr = -1;
Elmar Psilogdd65ba22023-02-20 16:03:15 +0100720 ofnode fixed_node;
721
722 if (IS_ENABLED(CONFIG_PHY_FIXED)) {
723 fixed_node = ofnode_find_subnode(dev_ofnode(dev),
724 "fixed-link");
725 if (ofnode_valid(fixed_node))
726 eqos->phy = fixed_phy_create(dev_ofnode(dev));
727 }
728
729 if (!eqos->phy) {
730 addr = eqos_get_phy_addr(eqos, dev);
731 eqos->phy = phy_connect(eqos->mii, addr, dev,
732 eqos->config->interface(dev));
733 }
734
Christophe Roullier6beb7802019-05-17 15:08:44 +0200735 if (!eqos->phy) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200736 pr_err("phy_connect() failed\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000737 ret = -ENODEV;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200738 goto err_stop_resets;
739 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100740
741 if (eqos->max_speed) {
742 ret = phy_set_supported(eqos->phy, eqos->max_speed);
743 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200744 pr_err("phy_set_supported() failed: %d\n", ret);
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100745 goto err_shutdown_phy;
746 }
747 }
748
Ye Li2f2aa482022-07-26 16:41:16 +0800749 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200750 ret = phy_config(eqos->phy);
751 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200752 pr_err("phy_config() failed: %d\n", ret);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200753 goto err_shutdown_phy;
754 }
Stephen Warren50709602016-10-21 14:46:47 -0600755 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200756
Stephen Warren50709602016-10-21 14:46:47 -0600757 ret = phy_startup(eqos->phy);
758 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200759 pr_err("phy_startup() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600760 goto err_shutdown_phy;
761 }
762
763 if (!eqos->phy->link) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200764 pr_err("No link\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000765 ret = -EAGAIN;
Stephen Warren50709602016-10-21 14:46:47 -0600766 goto err_shutdown_phy;
767 }
768
769 ret = eqos_adjust_link(dev);
770 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200771 pr_err("eqos_adjust_link() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600772 goto err_shutdown_phy;
773 }
774
775 /* Configure MTL */
776
777 /* Enable Store and Forward mode for TX */
778 /* Program Tx operating mode */
779 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
780 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
781 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
782 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
783
784 /* Transmit Queue weight */
785 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
786
787 /* Enable Store and Forward mode for RX, since no jumbo frame */
788 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000789 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600790
791 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
792 val = readl(&eqos->mac_regs->hw_feature1);
793 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
794 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
795 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
796 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
797
Sumit Garg4d5c9652023-02-01 19:28:54 +0530798 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
799 tx_fifo_sz = 128 << tx_fifo_sz;
800 rx_fifo_sz = 128 << rx_fifo_sz;
801
802 /* Allow platform to override TX/RX fifo size */
803 if (eqos->tx_fifo_sz)
804 tx_fifo_sz = eqos->tx_fifo_sz;
805 if (eqos->rx_fifo_sz)
806 rx_fifo_sz = eqos->rx_fifo_sz;
807
808 /* r/tqs is encoded as (n / 256) - 1 */
809 tqs = tx_fifo_sz / 256 - 1;
810 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600811
812 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
813 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
814 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
815 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
816 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
817 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
818 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
819 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
820
821 /* Flow control used only if each channel gets 4KB or more FIFO */
822 if (rqs >= ((4096 / 256) - 1)) {
823 u32 rfd, rfa;
824
825 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
826 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
827
828 /*
829 * Set Threshold for Activating Flow Contol space for min 2
830 * frames ie, (1500 * 1) = 1500 bytes.
831 *
832 * Set Threshold for Deactivating Flow Contol for space of
833 * min 1 frame (frame size 1500bytes) in receive fifo
834 */
835 if (rqs == ((4096 / 256) - 1)) {
836 /*
837 * This violates the above formula because of FIFO size
838 * limit therefore overflow may occur inspite of this.
839 */
840 rfd = 0x3; /* Full-3K */
841 rfa = 0x1; /* Full-1.5K */
842 } else if (rqs == ((8192 / 256) - 1)) {
843 rfd = 0x6; /* Full-4K */
844 rfa = 0xa; /* Full-6K */
845 } else if (rqs == ((16384 / 256) - 1)) {
846 rfd = 0x6; /* Full-4K */
847 rfa = 0x12; /* Full-10K */
848 } else {
849 rfd = 0x6; /* Full-4K */
850 rfa = 0x1E; /* Full-16K */
851 }
852
853 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
854 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
855 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
856 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
857 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
858 (rfd <<
859 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
860 (rfa <<
861 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
862 }
863
864 /* Configure MAC */
865
866 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
867 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
868 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200869 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600870 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
871
Fugang Duan37aae5f2020-05-03 22:41:17 +0800872 /* Multicast and Broadcast Queue Enable */
873 setbits_le32(&eqos->mac_regs->unused_0a4,
874 0x00100000);
875 /* enable promise mode */
876 setbits_le32(&eqos->mac_regs->unused_004[1],
877 0x1);
878
Stephen Warren50709602016-10-21 14:46:47 -0600879 /* Set TX flow control parameters */
880 /* Set Pause Time */
881 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
882 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
883 /* Assign priority for TX flow control */
884 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
885 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
886 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
887 /* Assign priority for RX flow control */
888 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
889 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
890 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
891 /* Enable flow control */
892 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
893 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
894 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
895 EQOS_MAC_RX_FLOW_CTRL_RFE);
896
897 clrsetbits_le32(&eqos->mac_regs->configuration,
898 EQOS_MAC_CONFIGURATION_GPSLCE |
899 EQOS_MAC_CONFIGURATION_WD |
900 EQOS_MAC_CONFIGURATION_JD |
901 EQOS_MAC_CONFIGURATION_JE,
902 EQOS_MAC_CONFIGURATION_CST |
903 EQOS_MAC_CONFIGURATION_ACS);
904
905 eqos_write_hwaddr(dev);
906
907 /* Configure DMA */
908
909 /* Enable OSP mode */
910 setbits_le32(&eqos->dma_regs->ch0_tx_control,
911 EQOS_DMA_CH0_TX_CONTROL_OSP);
912
913 /* RX buffer size. Must be a multiple of bus width */
914 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
915 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
916 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
917 EQOS_MAX_PACKET_SIZE <<
918 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
919
Marek Vasut89077732021-01-07 11:12:16 +0100920 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
921 eqos->config->axi_bus_width;
922
Stephen Warren50709602016-10-21 14:46:47 -0600923 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +0100924 EQOS_DMA_CH0_CONTROL_PBLX8 |
925 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -0600926
927 /*
928 * Burst length must be < 1/2 FIFO size.
929 * FIFO size in tqs is encoded as (n / 256) - 1.
930 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
931 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
932 */
933 pbl = tqs + 1;
934 if (pbl > 32)
935 pbl = 32;
936 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
937 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
938 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
939 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
940
941 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
942 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
943 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
944 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
945
946 /* DMA performance configuration */
947 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
948 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
949 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
950 writel(val, &eqos->dma_regs->sysbus_mode);
951
952 /* Set up descriptors */
953
Marek Vasut90cc13a2022-10-09 17:51:45 +0200954 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
955 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +0100956
957 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
958 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
959 eqos->config->ops->eqos_flush_desc(tx_desc);
960 }
961
Stephen Warren50709602016-10-21 14:46:47 -0600962 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +0100963 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Ley Foon Tan963db382022-12-09 14:33:14 +0800964
965 addr64 = (ulong)(eqos->rx_dma_buf + (i * EQOS_MAX_PACKET_SIZE));
966 rx_desc->des0 = lower_32_bits(addr64);
967 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasutd54c98e2020-03-23 02:02:57 +0100968 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800969 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +0100970 eqos->config->ops->eqos_flush_desc(rx_desc);
Ley Foon Tan963db382022-12-09 14:33:14 +0800971 eqos->config->ops->eqos_inval_buffer((void *)addr64, EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -0600972 }
Stephen Warren50709602016-10-21 14:46:47 -0600973
Ley Foon Tan963db382022-12-09 14:33:14 +0800974 addr64 = (ulong)eqos_get_desc(eqos, 0, false);
975 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_haddress);
976 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -0600977 writel(EQOS_DESCRIPTORS_TX - 1,
978 &eqos->dma_regs->ch0_txdesc_ring_length);
979
Ley Foon Tan963db382022-12-09 14:33:14 +0800980 addr64 = (ulong)eqos_get_desc(eqos, 0, true);
981 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_haddress);
982 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -0600983 writel(EQOS_DESCRIPTORS_RX - 1,
984 &eqos->dma_regs->ch0_rxdesc_ring_length);
985
986 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -0600987 setbits_le32(&eqos->dma_regs->ch0_tx_control,
988 EQOS_DMA_CH0_TX_CONTROL_ST);
989 setbits_le32(&eqos->dma_regs->ch0_rx_control,
990 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800991 setbits_le32(&eqos->mac_regs->configuration,
992 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -0600993
994 /* TX tail pointer not written until we need to TX a packet */
995 /*
996 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
997 * first descriptor, implying all descriptors were available. However,
998 * that's not distinguishable from none of the descriptors being
999 * available.
1000 */
Marek Vasut89077732021-01-07 11:12:16 +01001001 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001002 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1003
1004 eqos->started = true;
1005
1006 debug("%s: OK\n", __func__);
1007 return 0;
1008
1009err_shutdown_phy:
1010 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001011err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001012 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001013err:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001014 pr_err("FAILED: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001015 return ret;
1016}
1017
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001018static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001019{
1020 struct eqos_priv *eqos = dev_get_priv(dev);
1021 int i;
1022
1023 debug("%s(dev=%p):\n", __func__, dev);
1024
1025 if (!eqos->started)
1026 return;
1027 eqos->started = false;
1028 eqos->reg_access_ok = false;
1029
1030 /* Disable TX DMA */
1031 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1032 EQOS_DMA_CH0_TX_CONTROL_ST);
1033
1034 /* Wait for TX all packets to drain out of MTL */
1035 for (i = 0; i < 1000000; i++) {
1036 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1037 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1038 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1039 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1040 if ((trcsts != 1) && (!txqsts))
1041 break;
1042 }
1043
1044 /* Turn off MAC TX and RX */
1045 clrbits_le32(&eqos->mac_regs->configuration,
1046 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1047
1048 /* Wait for all RX packets to drain out of MTL */
1049 for (i = 0; i < 1000000; i++) {
1050 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1051 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1052 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1053 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1054 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1055 if ((!prxq) && (!rxqsts))
1056 break;
1057 }
1058
1059 /* Turn off RX DMA */
1060 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1061 EQOS_DMA_CH0_RX_CONTROL_SR);
1062
1063 if (eqos->phy) {
1064 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001065 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001066 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001067
1068 debug("%s: OK\n", __func__);
1069}
1070
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001071static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001072{
1073 struct eqos_priv *eqos = dev_get_priv(dev);
1074 struct eqos_desc *tx_desc;
1075 int i;
1076
1077 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1078 length);
1079
1080 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001081 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001082
Marek Vasut89077732021-01-07 11:12:16 +01001083 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001084 eqos->tx_desc_idx++;
1085 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1086
Ley Foon Tan963db382022-12-09 14:33:14 +08001087 tx_desc->des0 = lower_32_bits((ulong)eqos->tx_dma_buf);
1088 tx_desc->des1 = upper_32_bits((ulong)eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001089 tx_desc->des2 = length;
1090 /*
1091 * Make sure that if HW sees the _OWN write below, it will see all the
1092 * writes to the rest of the descriptor too.
1093 */
1094 mb();
1095 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001096 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001097
Marek Vasut89077732021-01-07 11:12:16 +01001098 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001099 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001100
1101 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001102 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001103 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1104 return 0;
1105 udelay(1);
1106 }
1107
1108 debug("%s: TX timeout\n", __func__);
1109
1110 return -ETIMEDOUT;
1111}
1112
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001113static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001114{
1115 struct eqos_priv *eqos = dev_get_priv(dev);
1116 struct eqos_desc *rx_desc;
1117 int length;
1118
Marek Vasut89077732021-01-07 11:12:16 +01001119 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001120 eqos->config->ops->eqos_inval_desc(rx_desc);
Jonas Karlmane2c45462023-10-01 19:17:18 +00001121 if (rx_desc->des3 & EQOS_DESC3_OWN)
Stephen Warren50709602016-10-21 14:46:47 -06001122 return -EAGAIN;
Jonas Karlmane2c45462023-10-01 19:17:18 +00001123
1124 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
Stephen Warren50709602016-10-21 14:46:47 -06001125
1126 *packetp = eqos->rx_dma_buf +
1127 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1128 length = rx_desc->des3 & 0x7fff;
1129 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1130
Christophe Roullier6beb7802019-05-17 15:08:44 +02001131 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001132
1133 return length;
1134}
1135
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001136static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001137{
1138 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001139 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001140 uchar *packet_expected;
Patrice Chotard7b1baf32024-04-05 18:15:29 +02001141 struct eqos_desc *rx_desc = NULL;
Stephen Warren50709602016-10-21 14:46:47 -06001142
1143 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1144
1145 packet_expected = eqos->rx_dma_buf +
1146 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1147 if (packet != packet_expected) {
1148 debug("%s: Unexpected packet (expected %p)\n", __func__,
1149 packet_expected);
1150 return -EINVAL;
1151 }
1152
Fugang Duan37aae5f2020-05-03 22:41:17 +08001153 eqos->config->ops->eqos_inval_buffer(packet, length);
1154
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001155 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1156 for (idx = eqos->rx_desc_idx - idx_mask;
1157 idx <= eqos->rx_desc_idx;
1158 idx++) {
Ley Foon Tan963db382022-12-09 14:33:14 +08001159 ulong addr64;
1160
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001161 rx_desc = eqos_get_desc(eqos, idx, true);
1162 rx_desc->des0 = 0;
Ley Foon Tan963db382022-12-09 14:33:14 +08001163 rx_desc->des1 = 0;
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001164 mb();
1165 eqos->config->ops->eqos_flush_desc(rx_desc);
1166 eqos->config->ops->eqos_inval_buffer(packet, length);
Ley Foon Tan963db382022-12-09 14:33:14 +08001167 addr64 = (ulong)(eqos->rx_dma_buf + (idx * EQOS_MAX_PACKET_SIZE));
1168 rx_desc->des0 = lower_32_bits(addr64);
1169 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001170 rx_desc->des2 = 0;
1171 /*
1172 * Make sure that if HW sees the _OWN write below,
1173 * it will see all the writes to the rest of the
1174 * descriptor too.
1175 */
1176 mb();
1177 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1178 eqos->config->ops->eqos_flush_desc(rx_desc);
1179 }
1180 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1181 }
Stephen Warren50709602016-10-21 14:46:47 -06001182
1183 eqos->rx_desc_idx++;
1184 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1185
1186 return 0;
1187}
1188
1189static int eqos_probe_resources_core(struct udevice *dev)
1190{
1191 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001192 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001193 int ret;
1194
1195 debug("%s(dev=%p):\n", __func__, dev);
1196
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001197 /* Maximum distance between neighboring descriptors, in Bytes. */
1198 desc_step = sizeof(struct eqos_desc) +
1199 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1200 if (desc_step < ARCH_DMA_MINALIGN) {
1201 /*
1202 * The EQoS hardware implementation cannot place one descriptor
1203 * per cacheline, it is necessary to place multiple descriptors
1204 * per cacheline in memory and do cache management carefully.
1205 */
1206 eqos->desc_size = BIT(fls(desc_step) - 1);
1207 } else {
1208 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1209 (unsigned int)ARCH_DMA_MINALIGN);
1210 }
1211 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001212
1213 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1214 if (!eqos->tx_descs) {
1215 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001216 ret = -ENOMEM;
1217 goto err;
1218 }
Stephen Warren50709602016-10-21 14:46:47 -06001219
Marek Vasut90cc13a2022-10-09 17:51:45 +02001220 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1221 if (!eqos->rx_descs) {
1222 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1223 ret = -ENOMEM;
1224 goto err_free_tx_descs;
1225 }
1226
Stephen Warren50709602016-10-21 14:46:47 -06001227 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1228 if (!eqos->tx_dma_buf) {
1229 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1230 ret = -ENOMEM;
1231 goto err_free_descs;
1232 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001233 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001234
1235 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1236 if (!eqos->rx_dma_buf) {
1237 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1238 ret = -ENOMEM;
1239 goto err_free_tx_dma_buf;
1240 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001241 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001242
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001243 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1244 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1245
Stephen Warren50709602016-10-21 14:46:47 -06001246 debug("%s: OK\n", __func__);
1247 return 0;
1248
Stephen Warren50709602016-10-21 14:46:47 -06001249err_free_tx_dma_buf:
1250 free(eqos->tx_dma_buf);
1251err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001252 eqos_free_descs(eqos->rx_descs);
1253err_free_tx_descs:
1254 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001255err:
1256
1257 debug("%s: returns %d\n", __func__, ret);
1258 return ret;
1259}
1260
1261static int eqos_remove_resources_core(struct udevice *dev)
1262{
1263 struct eqos_priv *eqos = dev_get_priv(dev);
1264
1265 debug("%s(dev=%p):\n", __func__, dev);
1266
Stephen Warren50709602016-10-21 14:46:47 -06001267 free(eqos->rx_dma_buf);
1268 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001269 eqos_free_descs(eqos->rx_descs);
1270 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001271
1272 debug("%s: OK\n", __func__);
1273 return 0;
1274}
1275
1276static int eqos_probe_resources_tegra186(struct udevice *dev)
1277{
1278 struct eqos_priv *eqos = dev_get_priv(dev);
1279 int ret;
1280
1281 debug("%s(dev=%p):\n", __func__, dev);
1282
1283 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1284 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001285 pr_err("reset_get_by_name(rst) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001286 return ret;
1287 }
1288
1289 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1290 &eqos->phy_reset_gpio,
1291 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1292 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001293 pr_err("gpio_request_by_name(phy reset) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001294 goto err_free_reset_eqos;
1295 }
1296
1297 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1298 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001299 pr_err("clk_get_by_name(slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001300 goto err_free_gpio_phy_reset;
1301 }
1302
1303 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1304 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001305 pr_err("clk_get_by_name(master_bus) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001306 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001307 }
1308
1309 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1310 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001311 pr_err("clk_get_by_name(rx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001312 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001313 }
1314
1315 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1316 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001317 pr_err("clk_get_by_name(ptp_ref) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001318 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001319 }
1320
1321 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1322 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001323 pr_err("clk_get_by_name(tx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001324 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001325 }
1326
1327 debug("%s: OK\n", __func__);
1328 return 0;
1329
Stephen Warren50709602016-10-21 14:46:47 -06001330err_free_gpio_phy_reset:
1331 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1332err_free_reset_eqos:
1333 reset_free(&eqos->reset_ctl);
1334
1335 debug("%s: returns %d\n", __func__, ret);
1336 return ret;
1337}
1338
Marek Behúnbc194772022-04-07 00:33:01 +02001339static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001340{
1341 return PHY_INTERFACE_MODE_MII;
1342}
1343
Stephen Warren50709602016-10-21 14:46:47 -06001344static int eqos_remove_resources_tegra186(struct udevice *dev)
1345{
1346 struct eqos_priv *eqos = dev_get_priv(dev);
1347
1348 debug("%s(dev=%p):\n", __func__, dev);
1349
Stephen Warren50709602016-10-21 14:46:47 -06001350 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1351 reset_free(&eqos->reset_ctl);
1352
1353 debug("%s: OK\n", __func__);
1354 return 0;
1355}
1356
1357static int eqos_probe(struct udevice *dev)
1358{
1359 struct eqos_priv *eqos = dev_get_priv(dev);
1360 int ret;
1361
1362 debug("%s(dev=%p):\n", __func__, dev);
1363
1364 eqos->dev = dev;
1365 eqos->config = (void *)dev_get_driver_data(dev);
1366
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001367 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001368 if (eqos->regs == FDT_ADDR_T_NONE) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001369 pr_err("dev_read_addr() failed\n");
Stephen Warren50709602016-10-21 14:46:47 -06001370 return -ENODEV;
1371 }
1372 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1373 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1374 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1375 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1376
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001377 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1378
Stephen Warren50709602016-10-21 14:46:47 -06001379 ret = eqos_probe_resources_core(dev);
1380 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001381 pr_err("eqos_probe_resources_core() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001382 return ret;
1383 }
1384
Christophe Roullier6beb7802019-05-17 15:08:44 +02001385 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001386 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001387 pr_err("eqos_probe_resources() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001388 goto err_remove_resources_core;
1389 }
1390
Marek Vasut30b28c42021-11-13 03:23:52 +01001391 ret = eqos->config->ops->eqos_start_clks(dev);
1392 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001393 pr_err("eqos_start_clks() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +01001394 goto err_remove_resources_tegra;
1395 }
1396
Ye Liad122b72020-05-03 22:41:15 +08001397#ifdef CONFIG_DM_ETH_PHY
1398 eqos->mii = eth_phy_get_mdio_bus(dev);
1399#endif
Stephen Warren50709602016-10-21 14:46:47 -06001400 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001401 eqos->mii = mdio_alloc();
1402 if (!eqos->mii) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001403 pr_err("mdio_alloc() failed\n");
Ye Liad122b72020-05-03 22:41:15 +08001404 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001405 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001406 }
1407 eqos->mii->read = eqos_mdio_read;
1408 eqos->mii->write = eqos_mdio_write;
1409 eqos->mii->priv = eqos;
1410 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001411
Ye Liad122b72020-05-03 22:41:15 +08001412 ret = mdio_register(eqos->mii);
1413 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001414 pr_err("mdio_register() failed: %d\n", ret);
Ye Liad122b72020-05-03 22:41:15 +08001415 goto err_free_mdio;
1416 }
Stephen Warren50709602016-10-21 14:46:47 -06001417 }
1418
Ye Liad122b72020-05-03 22:41:15 +08001419#ifdef CONFIG_DM_ETH_PHY
1420 eth_phy_set_mdio_bus(dev, eqos->mii);
1421#endif
1422
Stephen Warren50709602016-10-21 14:46:47 -06001423 debug("%s: OK\n", __func__);
1424 return 0;
1425
1426err_free_mdio:
1427 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001428err_stop_clks:
1429 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001430err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001431 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001432err_remove_resources_core:
1433 eqos_remove_resources_core(dev);
1434
1435 debug("%s: returns %d\n", __func__, ret);
1436 return ret;
1437}
1438
1439static int eqos_remove(struct udevice *dev)
1440{
1441 struct eqos_priv *eqos = dev_get_priv(dev);
1442
1443 debug("%s(dev=%p):\n", __func__, dev);
1444
1445 mdio_unregister(eqos->mii);
1446 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001447 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001448 eqos->config->ops->eqos_remove_resources(dev);
1449
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001450 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001451
1452 debug("%s: OK\n", __func__);
1453 return 0;
1454}
1455
Peng Fanc0a59952022-07-26 16:41:14 +08001456int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001457{
1458 return 0;
1459}
1460
Stephen Warren50709602016-10-21 14:46:47 -06001461static const struct eth_ops eqos_ops = {
1462 .start = eqos_start,
1463 .stop = eqos_stop,
1464 .send = eqos_send,
1465 .recv = eqos_recv,
1466 .free_pkt = eqos_free_pkt,
1467 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001468 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001469};
1470
Christophe Roullier6beb7802019-05-17 15:08:44 +02001471static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001472 .eqos_inval_desc = eqos_inval_desc_generic,
1473 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001474 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1475 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1476 .eqos_probe_resources = eqos_probe_resources_tegra186,
1477 .eqos_remove_resources = eqos_remove_resources_tegra186,
1478 .eqos_stop_resets = eqos_stop_resets_tegra186,
1479 .eqos_start_resets = eqos_start_resets_tegra186,
1480 .eqos_stop_clks = eqos_stop_clks_tegra186,
1481 .eqos_start_clks = eqos_start_clks_tegra186,
1482 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1483 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1484 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001485 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001486 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1487};
1488
Patrick Delaunay68083902020-06-08 11:27:19 +02001489static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001490 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001491 .mdio_wait = 10,
1492 .swr_wait = 10,
1493 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1494 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001495 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001496 .interface = eqos_get_interface_tegra186,
1497 .ops = &eqos_tegra186_ops
1498};
1499
Stephen Warren50709602016-10-21 14:46:47 -06001500static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001501#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001502 {
1503 .compatible = "nvidia,tegra186-eqos",
1504 .data = (ulong)&eqos_tegra186_config
1505 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001506#endif
1507#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001508 {
Christophe Roullier25a16862024-03-26 13:07:31 +01001509 .compatible = "st,stm32mp13-dwmac",
1510 .data = (ulong)&eqos_stm32mp13_config
1511 },
1512 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001513 .compatible = "st,stm32mp1-dwmac",
Marek Vasut944ba372024-03-26 13:07:23 +01001514 .data = (ulong)&eqos_stm32mp15_config
Christophe Roullier6beb7802019-05-17 15:08:44 +02001515 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001516#endif
1517#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001518 {
Marek Vasut7af11382022-02-26 04:36:37 +01001519 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001520 .data = (ulong)&eqos_imx_config
1521 },
Sébastien Szymanski28b7fc42023-10-17 11:44:58 +02001522 {
1523 .compatible = "nxp,imx93-dwmac-eqos",
1524 .data = (ulong)&eqos_imx_config
1525 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001526#endif
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001527#if IS_ENABLED(CONFIG_DWC_ETH_QOS_ROCKCHIP)
1528 {
1529 .compatible = "rockchip,rk3568-gmac",
1530 .data = (ulong)&eqos_rockchip_config
1531 },
Jonas Karlman1b615702023-10-01 19:17:20 +00001532 {
1533 .compatible = "rockchip,rk3588-gmac",
1534 .data = (ulong)&eqos_rockchip_config
1535 },
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001536#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301537#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1538 {
1539 .compatible = "qcom,qcs404-ethqos",
1540 .data = (ulong)&eqos_qcom_config
1541 },
1542#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001543#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1544 {
1545 .compatible = "starfive,jh7110-dwmac",
1546 .data = (ulong)&eqos_jh7110_config
1547 },
1548#endif
Stephen Warren50709602016-10-21 14:46:47 -06001549 { }
1550};
1551
1552U_BOOT_DRIVER(eth_eqos) = {
1553 .name = "eth_eqos",
1554 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001555 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001556 .probe = eqos_probe,
1557 .remove = eqos_remove,
1558 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001559 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001560 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001561};