blob: 43f0ec7637d803a650a2f98194b60728b75e7ca1 [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
Philip Oberfichtnerabed3722024-05-07 11:42:37 +020053#include <linux/bitfield.h>
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
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200150/* Bitmask common for mdio_read and mdio_write */
151#define EQOS_MDIO_BITFIELD(pa, rda, cr) \
152 FIELD_PREP(EQOS_MAC_MDIO_ADDRESS_PA_MASK, pa) | \
153 FIELD_PREP(EQOS_MAC_MDIO_ADDRESS_RDA_MASK, rda) | \
154 FIELD_PREP(EQOS_MAC_MDIO_ADDRESS_CR_MASK, cr) | \
155 EQOS_MAC_MDIO_ADDRESS_GB
156
157static u32 eqos_mdio_bitfield(struct eqos_priv *eqos, int addr, int devad, int reg)
158{
159 int cr = eqos->config->config_mac_mdio;
160 bool c22 = devad == MDIO_DEVAD_NONE ? true : false;
161
162 if (c22)
163 return EQOS_MDIO_BITFIELD(addr, reg, cr);
164 else
165 return EQOS_MDIO_BITFIELD(addr, devad, cr) |
166 EQOS_MAC_MDIO_ADDRESS_C45E;
167}
168
Stephen Warren50709602016-10-21 14:46:47 -0600169static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
170 int mdio_reg)
171{
172 struct eqos_priv *eqos = bus->priv;
173 u32 val;
174 int ret;
175
176 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
177 mdio_reg);
178
179 ret = eqos_mdio_wait_idle(eqos);
180 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200181 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600182 return ret;
183 }
184
185 val = readl(&eqos->mac_regs->mdio_address);
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200186 val &= EQOS_MAC_MDIO_ADDRESS_SKAP;
187
188 val |= eqos_mdio_bitfield(eqos, mdio_addr, mdio_devad, mdio_reg) |
189 FIELD_PREP(EQOS_MAC_MDIO_ADDRESS_GOC_MASK,
190 EQOS_MAC_MDIO_ADDRESS_GOC_READ);
191
192 if (val & EQOS_MAC_MDIO_ADDRESS_C45E) {
193 writel(FIELD_PREP(EQOS_MAC_MDIO_DATA_RA_MASK, mdio_reg),
194 &eqos->mac_regs->mdio_data);
195 }
196
Stephen Warren50709602016-10-21 14:46:47 -0600197 writel(val, &eqos->mac_regs->mdio_address);
198
Christophe Roullier6beb7802019-05-17 15:08:44 +0200199 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600200
201 ret = eqos_mdio_wait_idle(eqos);
202 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200203 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600204 return ret;
205 }
206
207 val = readl(&eqos->mac_regs->mdio_data);
208 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
209
210 debug("%s: val=%x\n", __func__, val);
211
212 return val;
213}
214
215static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
216 int mdio_reg, u16 mdio_val)
217{
218 struct eqos_priv *eqos = bus->priv;
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200219 u32 v_addr;
220 u32 v_data;
Stephen Warren50709602016-10-21 14:46:47 -0600221 int ret;
222
223 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
224 mdio_addr, mdio_reg, mdio_val);
225
226 ret = eqos_mdio_wait_idle(eqos);
227 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200228 pr_err("MDIO not idle at entry\n");
Stephen Warren50709602016-10-21 14:46:47 -0600229 return ret;
230 }
231
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200232 v_addr = readl(&eqos->mac_regs->mdio_address);
233 v_addr &= EQOS_MAC_MDIO_ADDRESS_SKAP;
Stephen Warren50709602016-10-21 14:46:47 -0600234
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200235 v_addr |= eqos_mdio_bitfield(eqos, mdio_addr, mdio_devad, mdio_reg) |
236 FIELD_PREP(EQOS_MAC_MDIO_ADDRESS_GOC_MASK,
237 EQOS_MAC_MDIO_ADDRESS_GOC_WRITE);
238
239 v_data = mdio_val;
240 if (v_addr & EQOS_MAC_MDIO_ADDRESS_C45E)
241 v_data |= FIELD_PREP(EQOS_MAC_MDIO_DATA_RA_MASK, mdio_reg);
Stephen Warren50709602016-10-21 14:46:47 -0600242
Philip Oberfichtnerabed3722024-05-07 11:42:37 +0200243 writel(v_data, &eqos->mac_regs->mdio_data);
244 writel(v_addr, &eqos->mac_regs->mdio_address);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200245 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600246
247 ret = eqos_mdio_wait_idle(eqos);
248 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200249 pr_err("MDIO read didn't complete\n");
Stephen Warren50709602016-10-21 14:46:47 -0600250 return ret;
251 }
252
253 return 0;
254}
255
256static int eqos_start_clks_tegra186(struct udevice *dev)
257{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800258#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600259 struct eqos_priv *eqos = dev_get_priv(dev);
260 int ret;
261
262 debug("%s(dev=%p):\n", __func__, dev);
263
264 ret = clk_enable(&eqos->clk_slave_bus);
265 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200266 pr_err("clk_enable(clk_slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600267 goto err;
268 }
269
270 ret = clk_enable(&eqos->clk_master_bus);
271 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200272 pr_err("clk_enable(clk_master_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600273 goto err_disable_clk_slave_bus;
274 }
275
276 ret = clk_enable(&eqos->clk_rx);
277 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200278 pr_err("clk_enable(clk_rx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600279 goto err_disable_clk_master_bus;
280 }
281
282 ret = clk_enable(&eqos->clk_ptp_ref);
283 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200284 pr_err("clk_enable(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600285 goto err_disable_clk_rx;
286 }
287
288 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
289 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200290 pr_err("clk_set_rate(clk_ptp_ref) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600291 goto err_disable_clk_ptp_ref;
292 }
293
294 ret = clk_enable(&eqos->clk_tx);
295 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200296 pr_err("clk_enable(clk_tx) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600297 goto err_disable_clk_ptp_ref;
298 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800299#endif
Stephen Warren50709602016-10-21 14:46:47 -0600300
301 debug("%s: OK\n", __func__);
302 return 0;
303
Fugang Duan37aae5f2020-05-03 22:41:17 +0800304#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600305err_disable_clk_ptp_ref:
306 clk_disable(&eqos->clk_ptp_ref);
307err_disable_clk_rx:
308 clk_disable(&eqos->clk_rx);
309err_disable_clk_master_bus:
310 clk_disable(&eqos->clk_master_bus);
311err_disable_clk_slave_bus:
312 clk_disable(&eqos->clk_slave_bus);
313err:
314 debug("%s: FAILED: %d\n", __func__, ret);
315 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800316#endif
Stephen Warren50709602016-10-21 14:46:47 -0600317}
318
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200319static int eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600320{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800321#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600322 struct eqos_priv *eqos = dev_get_priv(dev);
323
324 debug("%s(dev=%p):\n", __func__, dev);
325
326 clk_disable(&eqos->clk_tx);
327 clk_disable(&eqos->clk_ptp_ref);
328 clk_disable(&eqos->clk_rx);
329 clk_disable(&eqos->clk_master_bus);
330 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800331#endif
Stephen Warren50709602016-10-21 14:46:47 -0600332
333 debug("%s: OK\n", __func__);
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +0200334 return 0;
Stephen Warren50709602016-10-21 14:46:47 -0600335}
336
337static int eqos_start_resets_tegra186(struct udevice *dev)
338{
339 struct eqos_priv *eqos = dev_get_priv(dev);
340 int ret;
341
342 debug("%s(dev=%p):\n", __func__, dev);
343
344 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
345 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200346 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600347 return ret;
348 }
349
350 udelay(2);
351
352 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
353 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200354 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600355 return ret;
356 }
357
358 ret = reset_assert(&eqos->reset_ctl);
359 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200360 pr_err("reset_assert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600361 return ret;
362 }
363
364 udelay(2);
365
366 ret = reset_deassert(&eqos->reset_ctl);
367 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200368 pr_err("reset_deassert() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600369 return ret;
370 }
371
372 debug("%s: OK\n", __func__);
373 return 0;
374}
375
376static int eqos_stop_resets_tegra186(struct udevice *dev)
377{
378 struct eqos_priv *eqos = dev_get_priv(dev);
379
380 reset_assert(&eqos->reset_ctl);
381 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
382
Christophe Roullier6beb7802019-05-17 15:08:44 +0200383 return 0;
384}
385
Stephen Warren50709602016-10-21 14:46:47 -0600386static int eqos_calibrate_pads_tegra186(struct udevice *dev)
387{
388 struct eqos_priv *eqos = dev_get_priv(dev);
389 int ret;
390
391 debug("%s(dev=%p):\n", __func__, dev);
392
393 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
394 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
395
396 udelay(1);
397
398 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
399 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
400
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100401 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
402 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600403 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200404 pr_err("calibrate didn't start\n");
Stephen Warren50709602016-10-21 14:46:47 -0600405 goto failed;
406 }
407
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100408 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
409 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600410 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200411 pr_err("calibrate didn't finish\n");
Stephen Warren50709602016-10-21 14:46:47 -0600412 goto failed;
413 }
414
415 ret = 0;
416
417failed:
418 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
419 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
420
421 debug("%s: returns %d\n", __func__, ret);
422
423 return ret;
424}
425
426static int eqos_disable_calibration_tegra186(struct udevice *dev)
427{
428 struct eqos_priv *eqos = dev_get_priv(dev);
429
430 debug("%s(dev=%p):\n", __func__, dev);
431
432 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
433 EQOS_AUTO_CAL_CONFIG_ENABLE);
434
435 return 0;
436}
437
438static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
439{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800440#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600441 struct eqos_priv *eqos = dev_get_priv(dev);
442
443 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800444#else
445 return 0;
446#endif
Stephen Warren50709602016-10-21 14:46:47 -0600447}
448
449static int eqos_set_full_duplex(struct udevice *dev)
450{
451 struct eqos_priv *eqos = dev_get_priv(dev);
452
453 debug("%s(dev=%p):\n", __func__, dev);
454
455 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
456
457 return 0;
458}
459
460static int eqos_set_half_duplex(struct udevice *dev)
461{
462 struct eqos_priv *eqos = dev_get_priv(dev);
463
464 debug("%s(dev=%p):\n", __func__, dev);
465
466 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
467
468 /* WAR: Flush TX queue when switching to half-duplex */
469 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
470 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
471
472 return 0;
473}
474
475static int eqos_set_gmii_speed(struct udevice *dev)
476{
477 struct eqos_priv *eqos = dev_get_priv(dev);
478
479 debug("%s(dev=%p):\n", __func__, dev);
480
481 clrbits_le32(&eqos->mac_regs->configuration,
482 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
483
484 return 0;
485}
486
487static int eqos_set_mii_speed_100(struct udevice *dev)
488{
489 struct eqos_priv *eqos = dev_get_priv(dev);
490
491 debug("%s(dev=%p):\n", __func__, dev);
492
493 setbits_le32(&eqos->mac_regs->configuration,
494 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
495
496 return 0;
497}
498
499static int eqos_set_mii_speed_10(struct udevice *dev)
500{
501 struct eqos_priv *eqos = dev_get_priv(dev);
502
503 debug("%s(dev=%p):\n", __func__, dev);
504
505 clrsetbits_le32(&eqos->mac_regs->configuration,
506 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
507
508 return 0;
509}
510
511static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
512{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800513#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600514 struct eqos_priv *eqos = dev_get_priv(dev);
515 ulong rate;
516 int ret;
517
518 debug("%s(dev=%p):\n", __func__, dev);
519
520 switch (eqos->phy->speed) {
521 case SPEED_1000:
522 rate = 125 * 1000 * 1000;
523 break;
524 case SPEED_100:
525 rate = 25 * 1000 * 1000;
526 break;
527 case SPEED_10:
528 rate = 2.5 * 1000 * 1000;
529 break;
530 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200531 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600532 return -EINVAL;
533 }
534
535 ret = clk_set_rate(&eqos->clk_tx, rate);
536 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200537 pr_err("clk_set_rate(tx_clk, %lu) failed: %d\n", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600538 return ret;
539 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800540#endif
Stephen Warren50709602016-10-21 14:46:47 -0600541
542 return 0;
543}
544
545static int eqos_adjust_link(struct udevice *dev)
546{
547 struct eqos_priv *eqos = dev_get_priv(dev);
548 int ret;
549 bool en_calibration;
550
551 debug("%s(dev=%p):\n", __func__, dev);
552
553 if (eqos->phy->duplex)
554 ret = eqos_set_full_duplex(dev);
555 else
556 ret = eqos_set_half_duplex(dev);
557 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200558 pr_err("eqos_set_*_duplex() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600559 return ret;
560 }
561
562 switch (eqos->phy->speed) {
563 case SPEED_1000:
564 en_calibration = true;
565 ret = eqos_set_gmii_speed(dev);
566 break;
567 case SPEED_100:
568 en_calibration = true;
569 ret = eqos_set_mii_speed_100(dev);
570 break;
571 case SPEED_10:
572 en_calibration = false;
573 ret = eqos_set_mii_speed_10(dev);
574 break;
575 default:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200576 pr_err("invalid speed %d\n", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600577 return -EINVAL;
578 }
579 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200580 pr_err("eqos_set_*mii_speed*() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600581 return ret;
582 }
583
584 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200585 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600586 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200587 pr_err("eqos_calibrate_pads() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200588 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600589 return ret;
590 }
591 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200592 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600593 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200594 pr_err("eqos_disable_calibration() failed: %d\n",
Christophe Roullier6beb7802019-05-17 15:08:44 +0200595 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600596 return ret;
597 }
598 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200599 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600600 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200601 pr_err("eqos_set_tx_clk_speed() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600602 return ret;
603 }
604
605 return 0;
606}
607
608static int eqos_write_hwaddr(struct udevice *dev)
609{
Simon Glassfa20e932020-12-03 16:55:20 -0700610 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600611 struct eqos_priv *eqos = dev_get_priv(dev);
612 uint32_t val;
613
614 /*
615 * This function may be called before start() or after stop(). At that
616 * time, on at least some configurations of the EQoS HW, all clocks to
617 * the EQoS HW block will be stopped, and a reset signal applied. If
618 * any register access is attempted in this state, bus timeouts or CPU
619 * hangs may occur. This check prevents that.
620 *
621 * A simple solution to this problem would be to not implement
622 * write_hwaddr(), since start() always writes the MAC address into HW
623 * anyway. However, it is desirable to implement write_hwaddr() to
624 * support the case of SW that runs subsequent to U-Boot which expects
625 * the MAC address to already be programmed into the EQoS registers,
626 * which must happen irrespective of whether the U-Boot user (or
627 * scripts) actually made use of the EQoS device, and hence
628 * irrespective of whether start() was ever called.
629 *
630 * Note that this requirement by subsequent SW is not valid for
631 * Tegra186, and is likely not valid for any non-PCI instantiation of
632 * the EQoS HW block. This function is implemented solely as
633 * future-proofing with the expectation the driver will eventually be
634 * ported to some system where the expectation above is true.
635 */
636 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
637 return 0;
638
639 /* Update the MAC address */
640 val = (plat->enetaddr[5] << 8) |
641 (plat->enetaddr[4]);
642 writel(val, &eqos->mac_regs->address0_high);
643 val = (plat->enetaddr[3] << 24) |
644 (plat->enetaddr[2] << 16) |
645 (plat->enetaddr[1] << 8) |
646 (plat->enetaddr[0]);
647 writel(val, &eqos->mac_regs->address0_low);
648
649 return 0;
650}
651
Ye Li3fb1a0e2020-05-03 22:41:20 +0800652static int eqos_read_rom_hwaddr(struct udevice *dev)
653{
Simon Glassfa20e932020-12-03 16:55:20 -0700654 struct eth_pdata *pdata = dev_get_plat(dev);
Peng Fanbf69a7b92022-07-26 16:41:17 +0800655 struct eqos_priv *eqos = dev_get_priv(dev);
656 int ret;
657
658 ret = eqos->config->ops->eqos_get_enetaddr(dev);
659 if (ret < 0)
660 return ret;
Ye Li3fb1a0e2020-05-03 22:41:20 +0800661
Ye Li3fb1a0e2020-05-03 22:41:20 +0800662 return !is_valid_ethaddr(pdata->enetaddr);
663}
664
Ye Li2f2aa482022-07-26 16:41:16 +0800665static int eqos_get_phy_addr(struct eqos_priv *priv, struct udevice *dev)
666{
667 struct ofnode_phandle_args phandle_args;
668 int reg;
669
670 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
671 &phandle_args)) {
672 debug("Failed to find phy-handle");
673 return -ENODEV;
674 }
675
676 priv->phy_of_node = phandle_args.node;
677
678 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
679
680 return reg;
681}
682
Stephen Warren50709602016-10-21 14:46:47 -0600683static int eqos_start(struct udevice *dev)
684{
685 struct eqos_priv *eqos = dev_get_priv(dev);
686 int ret, i;
687 ulong rate;
688 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
689 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +0100690 ulong desc_pad;
Ley Foon Tan963db382022-12-09 14:33:14 +0800691 ulong addr64;
Stephen Warren50709602016-10-21 14:46:47 -0600692
693 debug("%s(dev=%p):\n", __func__, dev);
694
695 eqos->tx_desc_idx = 0;
696 eqos->rx_desc_idx = 0;
697
Christophe Roullier6beb7802019-05-17 15:08:44 +0200698 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600699 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200700 pr_err("eqos_start_resets() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +0100701 goto err;
Stephen Warren50709602016-10-21 14:46:47 -0600702 }
703
704 udelay(10);
705
706 eqos->reg_access_ok = true;
707
Marek Vasute66825a2023-03-06 15:53:46 +0100708 /*
709 * Assert the SWR first, the actually reset the MAC and to latch in
710 * e.g. i.MX8M Plus GPR[1] content, which selects interface mode.
711 */
712 setbits_le32(&eqos->dma_regs->mode, EQOS_DMA_MODE_SWR);
713
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100714 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200715 EQOS_DMA_MODE_SWR, false,
716 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -0600717 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200718 pr_err("EQOS_DMA_MODE_SWR stuck\n");
Stephen Warren50709602016-10-21 14:46:47 -0600719 goto err_stop_resets;
720 }
721
Christophe Roullier6beb7802019-05-17 15:08:44 +0200722 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600723 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200724 pr_err("eqos_calibrate_pads() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600725 goto err_stop_resets;
726 }
Sumit Gargab973c92023-02-01 19:28:53 +0530727
728 if (eqos->config->ops->eqos_get_tick_clk_rate) {
729 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600730
Sumit Gargab973c92023-02-01 19:28:53 +0530731 val = (rate / 1000000) - 1;
732 writel(val, &eqos->mac_regs->us_tic_counter);
733 }
Stephen Warren50709602016-10-21 14:46:47 -0600734
Christophe Roullier6beb7802019-05-17 15:08:44 +0200735 /*
736 * if PHY was already connected and configured,
737 * don't need to reconnect/reconfigure again
738 */
Stephen Warren50709602016-10-21 14:46:47 -0600739 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +0800740 int addr = -1;
Elmar Psilogdd65ba22023-02-20 16:03:15 +0100741 ofnode fixed_node;
742
743 if (IS_ENABLED(CONFIG_PHY_FIXED)) {
744 fixed_node = ofnode_find_subnode(dev_ofnode(dev),
745 "fixed-link");
746 if (ofnode_valid(fixed_node))
747 eqos->phy = fixed_phy_create(dev_ofnode(dev));
748 }
749
750 if (!eqos->phy) {
751 addr = eqos_get_phy_addr(eqos, dev);
752 eqos->phy = phy_connect(eqos->mii, addr, dev,
753 eqos->config->interface(dev));
754 }
755
Christophe Roullier6beb7802019-05-17 15:08:44 +0200756 if (!eqos->phy) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200757 pr_err("phy_connect() failed\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000758 ret = -ENODEV;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200759 goto err_stop_resets;
760 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100761
762 if (eqos->max_speed) {
763 ret = phy_set_supported(eqos->phy, eqos->max_speed);
764 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200765 pr_err("phy_set_supported() failed: %d\n", ret);
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100766 goto err_shutdown_phy;
767 }
768 }
769
Ye Li2f2aa482022-07-26 16:41:16 +0800770 eqos->phy->node = eqos->phy_of_node;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200771 ret = phy_config(eqos->phy);
772 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200773 pr_err("phy_config() failed: %d\n", ret);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200774 goto err_shutdown_phy;
775 }
Stephen Warren50709602016-10-21 14:46:47 -0600776 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200777
Stephen Warren50709602016-10-21 14:46:47 -0600778 ret = phy_startup(eqos->phy);
779 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200780 pr_err("phy_startup() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600781 goto err_shutdown_phy;
782 }
783
784 if (!eqos->phy->link) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200785 pr_err("No link\n");
Jonas Karlman3c0a5442023-10-01 19:17:17 +0000786 ret = -EAGAIN;
Stephen Warren50709602016-10-21 14:46:47 -0600787 goto err_shutdown_phy;
788 }
789
790 ret = eqos_adjust_link(dev);
791 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +0200792 pr_err("eqos_adjust_link() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600793 goto err_shutdown_phy;
794 }
795
796 /* Configure MTL */
797
798 /* Enable Store and Forward mode for TX */
799 /* Program Tx operating mode */
800 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
801 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
802 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
803 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
804
805 /* Transmit Queue weight */
806 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
807
808 /* Enable Store and Forward mode for RX, since no jumbo frame */
809 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +0000810 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -0600811
812 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
813 val = readl(&eqos->mac_regs->hw_feature1);
814 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
815 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
816 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
817 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
818
Sumit Garg4d5c9652023-02-01 19:28:54 +0530819 /* r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting */
820 tx_fifo_sz = 128 << tx_fifo_sz;
821 rx_fifo_sz = 128 << rx_fifo_sz;
822
823 /* Allow platform to override TX/RX fifo size */
824 if (eqos->tx_fifo_sz)
825 tx_fifo_sz = eqos->tx_fifo_sz;
826 if (eqos->rx_fifo_sz)
827 rx_fifo_sz = eqos->rx_fifo_sz;
828
829 /* r/tqs is encoded as (n / 256) - 1 */
830 tqs = tx_fifo_sz / 256 - 1;
831 rqs = rx_fifo_sz / 256 - 1;
Stephen Warren50709602016-10-21 14:46:47 -0600832
833 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
834 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
835 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
836 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
837 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
838 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
839 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
840 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
841
842 /* Flow control used only if each channel gets 4KB or more FIFO */
843 if (rqs >= ((4096 / 256) - 1)) {
844 u32 rfd, rfa;
845
846 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
847 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
848
849 /*
850 * Set Threshold for Activating Flow Contol space for min 2
851 * frames ie, (1500 * 1) = 1500 bytes.
852 *
853 * Set Threshold for Deactivating Flow Contol for space of
854 * min 1 frame (frame size 1500bytes) in receive fifo
855 */
856 if (rqs == ((4096 / 256) - 1)) {
857 /*
858 * This violates the above formula because of FIFO size
859 * limit therefore overflow may occur inspite of this.
860 */
861 rfd = 0x3; /* Full-3K */
862 rfa = 0x1; /* Full-1.5K */
863 } else if (rqs == ((8192 / 256) - 1)) {
864 rfd = 0x6; /* Full-4K */
865 rfa = 0xa; /* Full-6K */
866 } else if (rqs == ((16384 / 256) - 1)) {
867 rfd = 0x6; /* Full-4K */
868 rfa = 0x12; /* Full-10K */
869 } else {
870 rfd = 0x6; /* Full-4K */
871 rfa = 0x1E; /* Full-16K */
872 }
873
874 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
875 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
876 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
877 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
878 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
879 (rfd <<
880 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
881 (rfa <<
882 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
883 }
884
885 /* Configure MAC */
886
887 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
888 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
889 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +0200890 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -0600891 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
892
Fugang Duan37aae5f2020-05-03 22:41:17 +0800893 /* Multicast and Broadcast Queue Enable */
894 setbits_le32(&eqos->mac_regs->unused_0a4,
895 0x00100000);
896 /* enable promise mode */
897 setbits_le32(&eqos->mac_regs->unused_004[1],
898 0x1);
899
Stephen Warren50709602016-10-21 14:46:47 -0600900 /* Set TX flow control parameters */
901 /* Set Pause Time */
902 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
903 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
904 /* Assign priority for TX flow control */
905 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
906 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
907 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
908 /* Assign priority for RX flow control */
909 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
910 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
911 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
912 /* Enable flow control */
913 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
914 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
915 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
916 EQOS_MAC_RX_FLOW_CTRL_RFE);
917
918 clrsetbits_le32(&eqos->mac_regs->configuration,
919 EQOS_MAC_CONFIGURATION_GPSLCE |
920 EQOS_MAC_CONFIGURATION_WD |
921 EQOS_MAC_CONFIGURATION_JD |
922 EQOS_MAC_CONFIGURATION_JE,
923 EQOS_MAC_CONFIGURATION_CST |
924 EQOS_MAC_CONFIGURATION_ACS);
925
926 eqos_write_hwaddr(dev);
927
928 /* Configure DMA */
929
930 /* Enable OSP mode */
931 setbits_le32(&eqos->dma_regs->ch0_tx_control,
932 EQOS_DMA_CH0_TX_CONTROL_OSP);
933
934 /* RX buffer size. Must be a multiple of bus width */
935 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
936 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
937 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
938 EQOS_MAX_PACKET_SIZE <<
939 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
940
Marek Vasut89077732021-01-07 11:12:16 +0100941 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
942 eqos->config->axi_bus_width;
943
Stephen Warren50709602016-10-21 14:46:47 -0600944 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +0100945 EQOS_DMA_CH0_CONTROL_PBLX8 |
946 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -0600947
948 /*
949 * Burst length must be < 1/2 FIFO size.
950 * FIFO size in tqs is encoded as (n / 256) - 1.
951 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
952 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
953 */
954 pbl = tqs + 1;
955 if (pbl > 32)
956 pbl = 32;
957 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
958 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
959 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
960 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
961
962 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
963 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
964 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
965 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
966
967 /* DMA performance configuration */
968 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
969 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
970 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
971 writel(val, &eqos->dma_regs->sysbus_mode);
972
973 /* Set up descriptors */
974
Marek Vasut90cc13a2022-10-09 17:51:45 +0200975 memset(eqos->tx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_TX);
976 memset(eqos->rx_descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_RX);
Marek Vasut89077732021-01-07 11:12:16 +0100977
978 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
979 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
980 eqos->config->ops->eqos_flush_desc(tx_desc);
981 }
982
Stephen Warren50709602016-10-21 14:46:47 -0600983 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +0100984 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Ley Foon Tan963db382022-12-09 14:33:14 +0800985
986 addr64 = (ulong)(eqos->rx_dma_buf + (i * EQOS_MAX_PACKET_SIZE));
987 rx_desc->des0 = lower_32_bits(addr64);
988 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasutd54c98e2020-03-23 02:02:57 +0100989 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800990 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +0100991 eqos->config->ops->eqos_flush_desc(rx_desc);
Ley Foon Tan963db382022-12-09 14:33:14 +0800992 eqos->config->ops->eqos_inval_buffer((void *)addr64, EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -0600993 }
Stephen Warren50709602016-10-21 14:46:47 -0600994
Ley Foon Tan963db382022-12-09 14:33:14 +0800995 addr64 = (ulong)eqos_get_desc(eqos, 0, false);
996 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_haddress);
997 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -0600998 writel(EQOS_DESCRIPTORS_TX - 1,
999 &eqos->dma_regs->ch0_txdesc_ring_length);
1000
Ley Foon Tan963db382022-12-09 14:33:14 +08001001 addr64 = (ulong)eqos_get_desc(eqos, 0, true);
1002 writel(upper_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_haddress);
1003 writel(lower_32_bits(addr64), &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001004 writel(EQOS_DESCRIPTORS_RX - 1,
1005 &eqos->dma_regs->ch0_rxdesc_ring_length);
1006
1007 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001008 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1009 EQOS_DMA_CH0_TX_CONTROL_ST);
1010 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1011 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001012 setbits_le32(&eqos->mac_regs->configuration,
1013 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001014
1015 /* TX tail pointer not written until we need to TX a packet */
1016 /*
1017 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1018 * first descriptor, implying all descriptors were available. However,
1019 * that's not distinguishable from none of the descriptors being
1020 * available.
1021 */
Marek Vasut89077732021-01-07 11:12:16 +01001022 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001023 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1024
1025 eqos->started = true;
1026
1027 debug("%s: OK\n", __func__);
1028 return 0;
1029
1030err_shutdown_phy:
1031 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001032err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001033 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001034err:
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001035 pr_err("FAILED: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001036 return ret;
1037}
1038
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001039static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001040{
1041 struct eqos_priv *eqos = dev_get_priv(dev);
1042 int i;
1043
1044 debug("%s(dev=%p):\n", __func__, dev);
1045
1046 if (!eqos->started)
1047 return;
1048 eqos->started = false;
1049 eqos->reg_access_ok = false;
1050
1051 /* Disable TX DMA */
1052 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1053 EQOS_DMA_CH0_TX_CONTROL_ST);
1054
1055 /* Wait for TX all packets to drain out of MTL */
1056 for (i = 0; i < 1000000; i++) {
1057 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1058 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1059 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1060 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1061 if ((trcsts != 1) && (!txqsts))
1062 break;
1063 }
1064
1065 /* Turn off MAC TX and RX */
1066 clrbits_le32(&eqos->mac_regs->configuration,
1067 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1068
1069 /* Wait for all RX packets to drain out of MTL */
1070 for (i = 0; i < 1000000; i++) {
1071 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1072 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1073 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1074 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1075 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1076 if ((!prxq) && (!rxqsts))
1077 break;
1078 }
1079
1080 /* Turn off RX DMA */
1081 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1082 EQOS_DMA_CH0_RX_CONTROL_SR);
1083
1084 if (eqos->phy) {
1085 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001086 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001087 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001088
1089 debug("%s: OK\n", __func__);
1090}
1091
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001092static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001093{
1094 struct eqos_priv *eqos = dev_get_priv(dev);
1095 struct eqos_desc *tx_desc;
1096 int i;
1097
1098 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1099 length);
1100
1101 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001102 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001103
Marek Vasut89077732021-01-07 11:12:16 +01001104 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001105 eqos->tx_desc_idx++;
1106 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1107
Ley Foon Tan963db382022-12-09 14:33:14 +08001108 tx_desc->des0 = lower_32_bits((ulong)eqos->tx_dma_buf);
1109 tx_desc->des1 = upper_32_bits((ulong)eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001110 tx_desc->des2 = length;
1111 /*
1112 * Make sure that if HW sees the _OWN write below, it will see all the
1113 * writes to the rest of the descriptor too.
1114 */
1115 mb();
1116 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001117 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001118
Marek Vasut89077732021-01-07 11:12:16 +01001119 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001120 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001121
1122 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001123 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001124 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1125 return 0;
1126 udelay(1);
1127 }
1128
1129 debug("%s: TX timeout\n", __func__);
1130
1131 return -ETIMEDOUT;
1132}
1133
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001134static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001135{
1136 struct eqos_priv *eqos = dev_get_priv(dev);
1137 struct eqos_desc *rx_desc;
1138 int length;
1139
Marek Vasut89077732021-01-07 11:12:16 +01001140 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001141 eqos->config->ops->eqos_inval_desc(rx_desc);
Jonas Karlmane2c45462023-10-01 19:17:18 +00001142 if (rx_desc->des3 & EQOS_DESC3_OWN)
Stephen Warren50709602016-10-21 14:46:47 -06001143 return -EAGAIN;
Jonas Karlmane2c45462023-10-01 19:17:18 +00001144
1145 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
Stephen Warren50709602016-10-21 14:46:47 -06001146
1147 *packetp = eqos->rx_dma_buf +
1148 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1149 length = rx_desc->des3 & 0x7fff;
1150 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1151
Christophe Roullier6beb7802019-05-17 15:08:44 +02001152 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001153
1154 return length;
1155}
1156
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001157static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001158{
1159 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001160 u32 idx, idx_mask = eqos->desc_per_cacheline - 1;
Stephen Warren50709602016-10-21 14:46:47 -06001161 uchar *packet_expected;
Patrice Chotard7b1baf32024-04-05 18:15:29 +02001162 struct eqos_desc *rx_desc = NULL;
Stephen Warren50709602016-10-21 14:46:47 -06001163
1164 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1165
1166 packet_expected = eqos->rx_dma_buf +
1167 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1168 if (packet != packet_expected) {
1169 debug("%s: Unexpected packet (expected %p)\n", __func__,
1170 packet_expected);
1171 return -EINVAL;
1172 }
1173
Fugang Duan37aae5f2020-05-03 22:41:17 +08001174 eqos->config->ops->eqos_inval_buffer(packet, length);
1175
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001176 if ((eqos->rx_desc_idx & idx_mask) == idx_mask) {
1177 for (idx = eqos->rx_desc_idx - idx_mask;
1178 idx <= eqos->rx_desc_idx;
1179 idx++) {
Ley Foon Tan963db382022-12-09 14:33:14 +08001180 ulong addr64;
1181
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001182 rx_desc = eqos_get_desc(eqos, idx, true);
1183 rx_desc->des0 = 0;
Ley Foon Tan963db382022-12-09 14:33:14 +08001184 rx_desc->des1 = 0;
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001185 mb();
1186 eqos->config->ops->eqos_flush_desc(rx_desc);
1187 eqos->config->ops->eqos_inval_buffer(packet, length);
Ley Foon Tan963db382022-12-09 14:33:14 +08001188 addr64 = (ulong)(eqos->rx_dma_buf + (idx * EQOS_MAX_PACKET_SIZE));
1189 rx_desc->des0 = lower_32_bits(addr64);
1190 rx_desc->des1 = upper_32_bits(addr64);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001191 rx_desc->des2 = 0;
1192 /*
1193 * Make sure that if HW sees the _OWN write below,
1194 * it will see all the writes to the rest of the
1195 * descriptor too.
1196 */
1197 mb();
1198 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1199 eqos->config->ops->eqos_flush_desc(rx_desc);
1200 }
1201 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1202 }
Stephen Warren50709602016-10-21 14:46:47 -06001203
1204 eqos->rx_desc_idx++;
1205 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1206
1207 return 0;
1208}
1209
1210static int eqos_probe_resources_core(struct udevice *dev)
1211{
1212 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001213 unsigned int desc_step;
Stephen Warren50709602016-10-21 14:46:47 -06001214 int ret;
1215
1216 debug("%s(dev=%p):\n", __func__, dev);
1217
Marek Vasut3e8a1be2022-10-09 17:51:46 +02001218 /* Maximum distance between neighboring descriptors, in Bytes. */
1219 desc_step = sizeof(struct eqos_desc) +
1220 EQOS_DMA_CH0_CONTROL_DSL_MASK * eqos->config->axi_bus_width;
1221 if (desc_step < ARCH_DMA_MINALIGN) {
1222 /*
1223 * The EQoS hardware implementation cannot place one descriptor
1224 * per cacheline, it is necessary to place multiple descriptors
1225 * per cacheline in memory and do cache management carefully.
1226 */
1227 eqos->desc_size = BIT(fls(desc_step) - 1);
1228 } else {
1229 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
1230 (unsigned int)ARCH_DMA_MINALIGN);
1231 }
1232 eqos->desc_per_cacheline = ARCH_DMA_MINALIGN / eqos->desc_size;
Marek Vasut90cc13a2022-10-09 17:51:45 +02001233
1234 eqos->tx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_TX);
1235 if (!eqos->tx_descs) {
1236 debug("%s: eqos_alloc_descs(tx) failed\n", __func__);
Stephen Warren50709602016-10-21 14:46:47 -06001237 ret = -ENOMEM;
1238 goto err;
1239 }
Stephen Warren50709602016-10-21 14:46:47 -06001240
Marek Vasut90cc13a2022-10-09 17:51:45 +02001241 eqos->rx_descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_RX);
1242 if (!eqos->rx_descs) {
1243 debug("%s: eqos_alloc_descs(rx) failed\n", __func__);
1244 ret = -ENOMEM;
1245 goto err_free_tx_descs;
1246 }
1247
Stephen Warren50709602016-10-21 14:46:47 -06001248 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1249 if (!eqos->tx_dma_buf) {
1250 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1251 ret = -ENOMEM;
1252 goto err_free_descs;
1253 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001254 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001255
1256 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1257 if (!eqos->rx_dma_buf) {
1258 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1259 ret = -ENOMEM;
1260 goto err_free_tx_dma_buf;
1261 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001262 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001263
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001264 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1265 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1266
Stephen Warren50709602016-10-21 14:46:47 -06001267 debug("%s: OK\n", __func__);
1268 return 0;
1269
Stephen Warren50709602016-10-21 14:46:47 -06001270err_free_tx_dma_buf:
1271 free(eqos->tx_dma_buf);
1272err_free_descs:
Marek Vasut90cc13a2022-10-09 17:51:45 +02001273 eqos_free_descs(eqos->rx_descs);
1274err_free_tx_descs:
1275 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001276err:
1277
1278 debug("%s: returns %d\n", __func__, ret);
1279 return ret;
1280}
1281
1282static int eqos_remove_resources_core(struct udevice *dev)
1283{
1284 struct eqos_priv *eqos = dev_get_priv(dev);
1285
1286 debug("%s(dev=%p):\n", __func__, dev);
1287
Stephen Warren50709602016-10-21 14:46:47 -06001288 free(eqos->rx_dma_buf);
1289 free(eqos->tx_dma_buf);
Marek Vasut90cc13a2022-10-09 17:51:45 +02001290 eqos_free_descs(eqos->rx_descs);
1291 eqos_free_descs(eqos->tx_descs);
Stephen Warren50709602016-10-21 14:46:47 -06001292
1293 debug("%s: OK\n", __func__);
1294 return 0;
1295}
1296
1297static int eqos_probe_resources_tegra186(struct udevice *dev)
1298{
1299 struct eqos_priv *eqos = dev_get_priv(dev);
1300 int ret;
1301
1302 debug("%s(dev=%p):\n", __func__, dev);
1303
1304 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1305 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001306 pr_err("reset_get_by_name(rst) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001307 return ret;
1308 }
1309
1310 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1311 &eqos->phy_reset_gpio,
1312 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1313 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001314 pr_err("gpio_request_by_name(phy reset) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001315 goto err_free_reset_eqos;
1316 }
1317
1318 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1319 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001320 pr_err("clk_get_by_name(slave_bus) failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001321 goto err_free_gpio_phy_reset;
1322 }
1323
1324 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1325 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001326 pr_err("clk_get_by_name(master_bus) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001327 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001328 }
1329
1330 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1331 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001332 pr_err("clk_get_by_name(rx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001333 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001334 }
1335
1336 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1337 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001338 pr_err("clk_get_by_name(ptp_ref) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001339 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001340 }
1341
1342 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1343 if (ret) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001344 pr_err("clk_get_by_name(tx) failed: %d\n", ret);
Sean Andersond318eb32023-12-16 14:38:42 -05001345 goto err_free_gpio_phy_reset;
Stephen Warren50709602016-10-21 14:46:47 -06001346 }
1347
1348 debug("%s: OK\n", __func__);
1349 return 0;
1350
Stephen Warren50709602016-10-21 14:46:47 -06001351err_free_gpio_phy_reset:
1352 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1353err_free_reset_eqos:
1354 reset_free(&eqos->reset_ctl);
1355
1356 debug("%s: returns %d\n", __func__, ret);
1357 return ret;
1358}
1359
Marek Behúnbc194772022-04-07 00:33:01 +02001360static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001361{
1362 return PHY_INTERFACE_MODE_MII;
1363}
1364
Stephen Warren50709602016-10-21 14:46:47 -06001365static int eqos_remove_resources_tegra186(struct udevice *dev)
1366{
1367 struct eqos_priv *eqos = dev_get_priv(dev);
1368
1369 debug("%s(dev=%p):\n", __func__, dev);
1370
Stephen Warren50709602016-10-21 14:46:47 -06001371 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1372 reset_free(&eqos->reset_ctl);
1373
1374 debug("%s: OK\n", __func__);
1375 return 0;
1376}
1377
1378static int eqos_probe(struct udevice *dev)
1379{
1380 struct eqos_priv *eqos = dev_get_priv(dev);
1381 int ret;
1382
1383 debug("%s(dev=%p):\n", __func__, dev);
1384
1385 eqos->dev = dev;
1386 eqos->config = (void *)dev_get_driver_data(dev);
1387
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001388 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001389 if (eqos->regs == FDT_ADDR_T_NONE) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001390 pr_err("dev_read_addr() failed\n");
Stephen Warren50709602016-10-21 14:46:47 -06001391 return -ENODEV;
1392 }
1393 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1394 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1395 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1396 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1397
Rasmus Villemoes2a9e76d2022-05-11 16:58:41 +02001398 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1399
Stephen Warren50709602016-10-21 14:46:47 -06001400 ret = eqos_probe_resources_core(dev);
1401 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001402 pr_err("eqos_probe_resources_core() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001403 return ret;
1404 }
1405
Christophe Roullier6beb7802019-05-17 15:08:44 +02001406 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001407 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001408 pr_err("eqos_probe_resources() failed: %d\n", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001409 goto err_remove_resources_core;
1410 }
1411
Marek Vasut30b28c42021-11-13 03:23:52 +01001412 ret = eqos->config->ops->eqos_start_clks(dev);
1413 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001414 pr_err("eqos_start_clks() failed: %d\n", ret);
Marek Vasut30b28c42021-11-13 03:23:52 +01001415 goto err_remove_resources_tegra;
1416 }
1417
Ye Liad122b72020-05-03 22:41:15 +08001418#ifdef CONFIG_DM_ETH_PHY
1419 eqos->mii = eth_phy_get_mdio_bus(dev);
1420#endif
Stephen Warren50709602016-10-21 14:46:47 -06001421 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001422 eqos->mii = mdio_alloc();
1423 if (!eqos->mii) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001424 pr_err("mdio_alloc() failed\n");
Ye Liad122b72020-05-03 22:41:15 +08001425 ret = -ENOMEM;
Marek Vasut30b28c42021-11-13 03:23:52 +01001426 goto err_stop_clks;
Ye Liad122b72020-05-03 22:41:15 +08001427 }
1428 eqos->mii->read = eqos_mdio_read;
1429 eqos->mii->write = eqos_mdio_write;
1430 eqos->mii->priv = eqos;
1431 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001432
Ye Liad122b72020-05-03 22:41:15 +08001433 ret = mdio_register(eqos->mii);
1434 if (ret < 0) {
Heinrich Schuchardtccb51b42024-04-02 10:39:34 +02001435 pr_err("mdio_register() failed: %d\n", ret);
Ye Liad122b72020-05-03 22:41:15 +08001436 goto err_free_mdio;
1437 }
Stephen Warren50709602016-10-21 14:46:47 -06001438 }
1439
Ye Liad122b72020-05-03 22:41:15 +08001440#ifdef CONFIG_DM_ETH_PHY
1441 eth_phy_set_mdio_bus(dev, eqos->mii);
1442#endif
1443
Stephen Warren50709602016-10-21 14:46:47 -06001444 debug("%s: OK\n", __func__);
1445 return 0;
1446
1447err_free_mdio:
1448 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001449err_stop_clks:
1450 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001451err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001452 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001453err_remove_resources_core:
1454 eqos_remove_resources_core(dev);
1455
1456 debug("%s: returns %d\n", __func__, ret);
1457 return ret;
1458}
1459
1460static int eqos_remove(struct udevice *dev)
1461{
1462 struct eqos_priv *eqos = dev_get_priv(dev);
1463
1464 debug("%s(dev=%p):\n", __func__, dev);
1465
1466 mdio_unregister(eqos->mii);
1467 mdio_free(eqos->mii);
Marek Vasut30b28c42021-11-13 03:23:52 +01001468 eqos->config->ops->eqos_stop_clks(dev);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001469 eqos->config->ops->eqos_remove_resources(dev);
1470
Rasmus Villemoes50fe5262022-05-11 16:12:50 +02001471 eqos_remove_resources_core(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001472
1473 debug("%s: OK\n", __func__);
1474 return 0;
1475}
1476
Peng Fanc0a59952022-07-26 16:41:14 +08001477int eqos_null_ops(struct udevice *dev)
Patrick Delaunay1bc6ce72021-07-20 20:09:56 +02001478{
1479 return 0;
1480}
1481
Stephen Warren50709602016-10-21 14:46:47 -06001482static const struct eth_ops eqos_ops = {
1483 .start = eqos_start,
1484 .stop = eqos_stop,
1485 .send = eqos_send,
1486 .recv = eqos_recv,
1487 .free_pkt = eqos_free_pkt,
1488 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08001489 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06001490};
1491
Christophe Roullier6beb7802019-05-17 15:08:44 +02001492static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01001493 .eqos_inval_desc = eqos_inval_desc_generic,
1494 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001495 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1496 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1497 .eqos_probe_resources = eqos_probe_resources_tegra186,
1498 .eqos_remove_resources = eqos_remove_resources_tegra186,
1499 .eqos_stop_resets = eqos_stop_resets_tegra186,
1500 .eqos_start_resets = eqos_start_resets_tegra186,
1501 .eqos_stop_clks = eqos_stop_clks_tegra186,
1502 .eqos_start_clks = eqos_start_clks_tegra186,
1503 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1504 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1505 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
Patrice Chotard088d3ca2022-08-02 10:55:25 +02001506 .eqos_get_enetaddr = eqos_null_ops,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001507 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1508};
1509
Patrick Delaunay68083902020-06-08 11:27:19 +02001510static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06001511 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001512 .mdio_wait = 10,
1513 .swr_wait = 10,
1514 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1515 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01001516 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001517 .interface = eqos_get_interface_tegra186,
1518 .ops = &eqos_tegra186_ops
1519};
1520
Stephen Warren50709602016-10-21 14:46:47 -06001521static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02001522#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06001523 {
1524 .compatible = "nvidia,tegra186-eqos",
1525 .data = (ulong)&eqos_tegra186_config
1526 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001527#endif
1528#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001529 {
Christophe Roullier25a16862024-03-26 13:07:31 +01001530 .compatible = "st,stm32mp13-dwmac",
1531 .data = (ulong)&eqos_stm32mp13_config
1532 },
1533 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02001534 .compatible = "st,stm32mp1-dwmac",
Marek Vasut944ba372024-03-26 13:07:23 +01001535 .data = (ulong)&eqos_stm32mp15_config
Christophe Roullier6beb7802019-05-17 15:08:44 +02001536 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001537#endif
1538#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08001539 {
Marek Vasut7af11382022-02-26 04:36:37 +01001540 .compatible = "nxp,imx8mp-dwmac-eqos",
Fugang Duan37aae5f2020-05-03 22:41:17 +08001541 .data = (ulong)&eqos_imx_config
1542 },
Sébastien Szymanski28b7fc42023-10-17 11:44:58 +02001543 {
1544 .compatible = "nxp,imx93-dwmac-eqos",
1545 .data = (ulong)&eqos_imx_config
1546 },
Patrick Delaunay68083902020-06-08 11:27:19 +02001547#endif
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001548#if IS_ENABLED(CONFIG_DWC_ETH_QOS_ROCKCHIP)
1549 {
1550 .compatible = "rockchip,rk3568-gmac",
1551 .data = (ulong)&eqos_rockchip_config
1552 },
Jonas Karlman1b615702023-10-01 19:17:20 +00001553 {
1554 .compatible = "rockchip,rk3588-gmac",
1555 .data = (ulong)&eqos_rockchip_config
1556 },
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001557#endif
Sumit Garg7c3be942023-02-01 19:28:55 +05301558#if IS_ENABLED(CONFIG_DWC_ETH_QOS_QCOM)
1559 {
1560 .compatible = "qcom,qcs404-ethqos",
1561 .data = (ulong)&eqos_qcom_config
1562 },
1563#endif
Yanhong Wang1f502ee2023-06-15 17:36:43 +08001564#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STARFIVE)
1565 {
1566 .compatible = "starfive,jh7110-dwmac",
1567 .data = (ulong)&eqos_jh7110_config
1568 },
1569#endif
Stephen Warren50709602016-10-21 14:46:47 -06001570 { }
1571};
1572
1573U_BOOT_DRIVER(eth_eqos) = {
1574 .name = "eth_eqos",
1575 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08001576 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06001577 .probe = eqos_probe,
1578 .remove = eqos_remove,
1579 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001580 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001581 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06001582};