blob: 07b0f49ef58104acc4279e11345e336698bfb2ac [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR1f873122010-06-29 10:53:34 +05302/*
3 * (C) Copyright 2010
Patrick Delaunaya6b185e2022-05-20 18:38:10 +02004 * Vipin Kumar, STMicroelectronics, vipin.kumar@st.com.
Vipin KUMAR1f873122010-06-29 10:53:34 +05305 */
6
7/*
Simon Glasse50c4d12015-04-05 16:07:40 -06008 * Designware ethernet IP driver for U-Boot
Vipin KUMAR1f873122010-06-29 10:53:34 +05309 */
10
Patrice Chotardeebcf8c2017-11-29 09:06:11 +010011#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass90e627b2015-04-05 16:07:41 -060013#include <dm.h>
Simon Glasse50c4d12015-04-05 16:07:40 -060014#include <errno.h>
Jonas Karlman2603bbc2024-01-18 07:19:45 +000015#include <eth_phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053017#include <miiphy.h>
18#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <net.h>
Bin Menged89bd72015-09-11 03:24:35 -070020#include <pci.h>
Ley Foon Tan27d5c002018-06-14 18:45:23 +080021#include <reset.h>
Baruch Siachc00982a2023-10-25 11:08:44 +030022#include <phys2bus.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010025#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <dm/devres.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010027#include <dm/lists.h>
Stefan Roesed27e86c2012-05-07 12:04:25 +020028#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053030#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080031#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053032#include <asm/io.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060033#include <linux/printk.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080034#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053035#include "designware.h"
36
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040037static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
38{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010039 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
40 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040041 ulong start;
42 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050043 int timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040044
45 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
46 ((reg << MIIREGSHIFT) & MII_REGMSK);
47
48 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
49
50 start = get_timer(0);
51 while (get_timer(start) < timeout) {
52 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
53 return readl(&mac_p->miidata);
54 udelay(10);
55 };
56
Simon Glasse50c4d12015-04-05 16:07:40 -060057 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040058}
59
60static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
61 u16 val)
62{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010063 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
64 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040065 ulong start;
66 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050067 int ret = -ETIMEDOUT, timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040068
69 writel(val, &mac_p->miidata);
70 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
71 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
72
73 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
74
75 start = get_timer(0);
76 while (get_timer(start) < timeout) {
77 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
78 ret = 0;
79 break;
80 }
81 udelay(10);
82 };
83
84 return ret;
85}
86
Tom Rinie4bb4a22022-11-27 10:25:07 -050087#if CONFIG_IS_ENABLED(DM_GPIO)
Neil Armstrong1188a6d2021-04-21 10:58:01 +020088static int __dw_mdio_reset(struct udevice *dev)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010089{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010090 struct dw_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070091 struct dw_eth_pdata *pdata = dev_get_plat(dev);
Sjoerd Simons6eb44622016-02-28 22:24:55 +010092 int ret;
93
94 if (!dm_gpio_is_valid(&priv->reset_gpio))
95 return 0;
96
97 /* reset the phy */
98 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
99 if (ret)
100 return ret;
101
102 udelay(pdata->reset_delays[0]);
103
104 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
105 if (ret)
106 return ret;
107
108 udelay(pdata->reset_delays[1]);
109
110 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
111 if (ret)
112 return ret;
113
114 udelay(pdata->reset_delays[2]);
115
116 return 0;
117}
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200118
119static int dw_mdio_reset(struct mii_dev *bus)
120{
121 struct udevice *dev = bus->priv;
122
123 return __dw_mdio_reset(dev);
124}
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100125#endif
126
Neil Armstrong47318c92021-02-24 15:02:39 +0100127#if IS_ENABLED(CONFIG_DM_MDIO)
128int designware_eth_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
129{
130 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
131
132 return dw_mdio_read(pdata->mii_bus, addr, devad, reg);
133}
134
135int designware_eth_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg, u16 val)
136{
137 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
138
139 return dw_mdio_write(pdata->mii_bus, addr, devad, reg, val);
140}
141
142#if CONFIG_IS_ENABLED(DM_GPIO)
143int designware_eth_mdio_reset(struct udevice *mdio_dev)
144{
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200145 struct mdio_perdev_priv *mdio_pdata = dev_get_uclass_priv(mdio_dev);
146 struct udevice *dev = mdio_pdata->mii_bus->priv;
Neil Armstrong47318c92021-02-24 15:02:39 +0100147
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200148 return __dw_mdio_reset(dev->parent);
Neil Armstrong47318c92021-02-24 15:02:39 +0100149}
150#endif
151
152static const struct mdio_ops designware_eth_mdio_ops = {
153 .read = designware_eth_mdio_read,
154 .write = designware_eth_mdio_write,
155#if CONFIG_IS_ENABLED(DM_GPIO)
156 .reset = designware_eth_mdio_reset,
157#endif
158};
159
160static int designware_eth_mdio_probe(struct udevice *dev)
161{
162 /* Use the priv data of parent */
163 dev_set_priv(dev, dev_get_priv(dev->parent));
164
165 return 0;
166}
167
168U_BOOT_DRIVER(designware_eth_mdio) = {
169 .name = "eth_designware_mdio",
170 .id = UCLASS_MDIO,
171 .probe = designware_eth_mdio_probe,
172 .ops = &designware_eth_mdio_ops,
173 .plat_auto = sizeof(struct mdio_perdev_priv),
174};
175#endif
176
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100177static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400178{
179 struct mii_dev *bus = mdio_alloc();
180
181 if (!bus) {
182 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600183 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400184 }
185
186 bus->read = dw_mdio_read;
187 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000188 snprintf(bus->name, sizeof(bus->name), "%s", name);
Tom Rinie4bb4a22022-11-27 10:25:07 -0500189#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100190 bus->reset = dw_mdio_reset;
191#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400192
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100193 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400194
195 return mdio_register(bus);
196}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000197
Neil Armstrong47318c92021-02-24 15:02:39 +0100198#if IS_ENABLED(CONFIG_DM_MDIO)
199static int dw_dm_mdio_init(const char *name, void *priv)
200{
201 struct udevice *dev = priv;
202 ofnode node;
203 int ret;
204
205 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
206 const char *subnode_name = ofnode_get_name(node);
207 struct udevice *mdiodev;
208
209 if (strcmp(subnode_name, "mdio"))
210 continue;
211
212 ret = device_bind_driver_to_node(dev, "eth_designware_mdio",
213 subnode_name, node, &mdiodev);
214 if (ret)
215 debug("%s: not able to bind mdio device node\n", __func__);
216
217 return 0;
218 }
219
220 printf("%s: mdio node is missing, registering legacy mdio bus", __func__);
221
222 return dw_mdio_init(name, priv);
223}
224#endif
225
Simon Glasse50c4d12015-04-05 16:07:40 -0600226static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530227{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530228 struct eth_dma_regs *dma_p = priv->dma_regs_p;
229 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
230 char *txbuffs = &priv->txbuffs[0];
231 struct dmamacdescr *desc_p;
232 u32 idx;
233
Tom Rini364d0022023-01-10 11:19:45 -0500234 for (idx = 0; idx < CFG_TX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530235 desc_p = &desc_table_p[idx];
Baruch Siachc00982a2023-10-25 11:08:44 +0300236 desc_p->dmamac_addr = dev_phys_to_bus(priv->dev,
237 (ulong)&txbuffs[idx * CFG_ETH_BUFSIZE]);
238 desc_p->dmamac_next = dev_phys_to_bus(priv->dev,
239 (ulong)&desc_table_p[idx + 1]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530240
241#if defined(CONFIG_DW_ALTDESCRIPTOR)
242 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100243 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
244 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530245 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
246
247 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
248 desc_p->dmamac_cntl = 0;
249 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
250#else
251 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
252 desc_p->txrx_status = 0;
253#endif
254 }
255
256 /* Correcting the last pointer of the chain */
Baruch Siachc00982a2023-10-25 11:08:44 +0300257 desc_p->dmamac_next = dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530258
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400259 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200260 flush_dcache_range((ulong)priv->tx_mac_descrtable,
261 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400262 sizeof(priv->tx_mac_descrtable));
263
Baruch Siachc00982a2023-10-25 11:08:44 +0300264 writel(dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]),
265 &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400266 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530267}
268
Simon Glasse50c4d12015-04-05 16:07:40 -0600269static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530270{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530271 struct eth_dma_regs *dma_p = priv->dma_regs_p;
272 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
273 char *rxbuffs = &priv->rxbuffs[0];
274 struct dmamacdescr *desc_p;
275 u32 idx;
276
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400277 /* Before passing buffers to GMAC we need to make sure zeros
278 * written there right after "priv" structure allocation were
279 * flushed into RAM.
280 * Otherwise there's a chance to get some of them flushed in RAM when
281 * GMAC is already pushing data to RAM via DMA. This way incoming from
282 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200283 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400284
Tom Rini364d0022023-01-10 11:19:45 -0500285 for (idx = 0; idx < CFG_RX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530286 desc_p = &desc_table_p[idx];
Baruch Siachc00982a2023-10-25 11:08:44 +0300287 desc_p->dmamac_addr = dev_phys_to_bus(priv->dev,
288 (ulong)&rxbuffs[idx * CFG_ETH_BUFSIZE]);
289 desc_p->dmamac_next = dev_phys_to_bus(priv->dev,
290 (ulong)&desc_table_p[idx + 1]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530291
292 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100293 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530294 DESC_RXCTRL_RXCHAIN;
295
296 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
297 }
298
299 /* Correcting the last pointer of the chain */
Baruch Siachc00982a2023-10-25 11:08:44 +0300300 desc_p->dmamac_next = dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530301
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400302 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200303 flush_dcache_range((ulong)priv->rx_mac_descrtable,
304 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400305 sizeof(priv->rx_mac_descrtable));
306
Baruch Siachc00982a2023-10-25 11:08:44 +0300307 writel(dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]),
308 &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400309 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530310}
311
Simon Glasse50c4d12015-04-05 16:07:40 -0600312static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530313{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400314 struct eth_mac_regs *mac_p = priv->mac_regs_p;
315 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400316
317 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
318 (mac_id[3] << 24);
319 macid_hi = mac_id[4] + (mac_id[5] << 8);
320
321 writel(macid_hi, &mac_p->macaddr0hi);
322 writel(macid_lo, &mac_p->macaddr0lo);
323
324 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530325}
326
Simon Glass4afa85e2017-01-11 11:46:08 +0100327static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
328 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530329{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400330 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530331
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400332 if (!phydev->link) {
333 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100334 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400335 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530336
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400337 if (phydev->speed != 1000)
338 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300339 else
340 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530341
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400342 if (phydev->speed == 100)
343 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530344
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400345 if (phydev->duplex)
346 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000347
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400348 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530349
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400350 printf("Speed: %d, %s duplex%s\n", phydev->speed,
351 (phydev->duplex) ? "full" : "half",
352 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100353
Jim Liu4ef2a112024-04-08 16:50:17 +0800354#ifdef CONFIG_ARCH_NPCM8XX
355 /* Pass all Multicast Frames */
356 setbits_le32(&mac_p->framefilt, BIT(4));
357
358#endif
Simon Glass4afa85e2017-01-11 11:46:08 +0100359 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530360}
361
Simon Glasse50c4d12015-04-05 16:07:40 -0600362static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530363{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530364 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400365 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530366
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400367 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
368 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530369
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400370 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530371}
372
Simon Glassc154fc02017-01-11 11:46:10 +0100373int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530374{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530375 struct eth_mac_regs *mac_p = priv->mac_regs_p;
376 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400377 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600378 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530379
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400380 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000381
Quentin Schulz7f920dd2018-06-04 12:17:33 +0200382 /*
383 * When a MII PHY is used, we must set the PS bit for the DMA
384 * reset to succeed.
385 */
386 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
387 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
388 else
389 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
390
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400391 start = get_timer(0);
392 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Tom Rini364d0022023-01-10 11:19:45 -0500393 if (get_timer(start) >= CFG_MACRESET_TIMEOUT) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300394 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600395 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300396 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200397
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400398 mdelay(100);
399 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530400
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800401 /*
402 * Soft reset above clears HW address registers.
403 * So we have to set it here once again.
404 */
405 _dw_write_hwaddr(priv, enetaddr);
406
Simon Glasse50c4d12015-04-05 16:07:40 -0600407 rx_descs_init(priv);
408 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530409
Ian Campbell4164b742014-05-08 22:26:35 +0100410 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530411
Sonic Zhangb917b622015-01-29 14:38:50 +0800412#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400413 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
414 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800415#else
416 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
417 &dma_p->opmode);
418#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530419
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400420 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530421
Sonic Zhang962c95c2015-01-29 13:37:31 +0800422#ifdef CONFIG_DW_AXI_BURST_LEN
423 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
424#endif
425
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400426 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600427 ret = phy_startup(priv->phydev);
428 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400429 printf("Could not initialize PHY %s\n",
430 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600431 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530432 }
433
Simon Glass4afa85e2017-01-11 11:46:08 +0100434 ret = dw_adjust_link(priv, mac_p, priv->phydev);
435 if (ret)
436 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530437
Simon Glass3240e942017-01-11 11:46:09 +0100438 return 0;
439}
440
Simon Glassc154fc02017-01-11 11:46:10 +0100441int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100442{
443 struct eth_mac_regs *mac_p = priv->mac_regs_p;
444
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400445 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600446 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530447
Armando Visconti038c9d52012-03-26 00:09:55 +0000448 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530449
450 return 0;
451}
452
Florian Fainelli65f686b2017-12-09 14:59:55 -0800453#define ETH_ZLEN 60
454
Simon Glasse50c4d12015-04-05 16:07:40 -0600455static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530456{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530457 struct eth_dma_regs *dma_p = priv->dma_regs_p;
458 u32 desc_num = priv->tx_currdescnum;
459 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200460 ulong desc_start = (ulong)desc_p;
461 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200462 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300463 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200464 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100465 /*
466 * Strictly we only need to invalidate the "txrx_status" field
467 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200468 * invalidate only 4 bytes, so we flush the entire descriptor,
469 * which is 16 bytes in total. This is safe because the
470 * individual descriptors in the array are each aligned to
471 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100472 */
Marek Vasut15193042014-09-15 01:05:23 +0200473 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400474
Vipin KUMAR1f873122010-06-29 10:53:34 +0530475 /* Check if the descriptor is owned by CPU */
476 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
477 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600478 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530479 }
480
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200481 memcpy((void *)data_start, packet, length);
Simon Goldschmidt80385de2018-11-17 10:24:42 +0100482 if (length < ETH_ZLEN) {
483 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
484 length = ETH_ZLEN;
485 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530486
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400487 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200488 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400489
Vipin KUMAR1f873122010-06-29 10:53:34 +0530490#if defined(CONFIG_DW_ALTDESCRIPTOR)
491 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100492 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
493 ((length << DESC_TXCTRL_SIZE1SHFT) &
494 DESC_TXCTRL_SIZE1MASK);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530495
496 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
497 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
498#else
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100499 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
500 ((length << DESC_TXCTRL_SIZE1SHFT) &
501 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
502 DESC_TXCTRL_TXFIRST;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530503
504 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
505#endif
506
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400507 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200508 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400509
Vipin KUMAR1f873122010-06-29 10:53:34 +0530510 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500511 if (++desc_num >= CFG_TX_DESCR_NUM)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530512 desc_num = 0;
513
514 priv->tx_currdescnum = desc_num;
515
516 /* Start the transmission */
517 writel(POLL_DATA, &dma_p->txpolldemand);
518
519 return 0;
520}
521
Simon Glass90e627b2015-04-05 16:07:41 -0600522static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530523{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400524 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530525 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600526 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200527 ulong desc_start = (ulong)desc_p;
528 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200529 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300530 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200531 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530532
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400533 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200534 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400535
536 status = desc_p->txrx_status;
537
Vipin KUMAR1f873122010-06-29 10:53:34 +0530538 /* Check if the owner is the CPU */
539 if (!(status & DESC_RXSTS_OWNBYDMA)) {
540
Marek Vasut4ab539a2015-12-20 03:59:23 +0100541 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530542 DESC_RXSTS_FRMLENSHFT;
543
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400544 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200545 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
546 invalidate_dcache_range(data_start, data_end);
Baruch Siachc00982a2023-10-25 11:08:44 +0300547 *packetp = (uchar *)(ulong)dev_bus_to_phys(priv->dev,
548 desc_p->dmamac_addr);
Simon Glass90e627b2015-04-05 16:07:41 -0600549 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400550
Simon Glass90e627b2015-04-05 16:07:41 -0600551 return length;
552}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530553
Simon Glass90e627b2015-04-05 16:07:41 -0600554static int _dw_free_pkt(struct dw_eth_dev *priv)
555{
556 u32 desc_num = priv->rx_currdescnum;
557 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200558 ulong desc_start = (ulong)desc_p;
559 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600560 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Jim Liu1f031a02024-04-08 16:49:02 +0800561 ulong data_start = desc_p->dmamac_addr;
562 ulong data_end = data_start + roundup(CFG_ETH_BUFSIZE, ARCH_DMA_MINALIGN);
563
564 /* Invalidate the descriptor buffer data */
565 invalidate_dcache_range(data_start, data_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530566
Simon Glass90e627b2015-04-05 16:07:41 -0600567 /*
568 * Make the current descriptor valid again and go to
569 * the next one
570 */
571 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400572
Simon Glass90e627b2015-04-05 16:07:41 -0600573 /* Flush only status field - others weren't changed */
574 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530575
Simon Glass90e627b2015-04-05 16:07:41 -0600576 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500577 if (++desc_num >= CFG_RX_DESCR_NUM)
Simon Glass90e627b2015-04-05 16:07:41 -0600578 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530579 priv->rx_currdescnum = desc_num;
580
Simon Glass90e627b2015-04-05 16:07:41 -0600581 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530582}
583
Simon Glasse50c4d12015-04-05 16:07:40 -0600584static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530585{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400586 struct phy_device *phydev;
Neil Armstrong47318c92021-02-24 15:02:39 +0100587 int ret;
588
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000589 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
590 eth_phy_set_mdio_bus(dev, NULL);
591
Tom Rinie4bb4a22022-11-27 10:25:07 -0500592#if IS_ENABLED(CONFIG_DM_MDIO)
Neil Armstrong47318c92021-02-24 15:02:39 +0100593 phydev = dm_eth_phy_connect(dev);
594 if (!phydev)
595 return -ENODEV;
596#else
597 int phy_addr = -1;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530598
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000599 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
600 phy_addr = eth_phy_get_addr(dev);
601
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400602#ifdef CONFIG_PHY_ADDR
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200603 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530604#endif
605
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200606 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400607 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600608 return -ENODEV;
Neil Armstrong47318c92021-02-24 15:02:39 +0100609#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530610
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400611 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300612 if (priv->max_speed) {
613 ret = phy_set_supported(phydev, priv->max_speed);
614 if (ret)
615 return ret;
616 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400617 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530618
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400619 priv->phydev = phydev;
620 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530621
Simon Glasse50c4d12015-04-05 16:07:40 -0600622 return 0;
623}
Simon Glass90e627b2015-04-05 16:07:41 -0600624
Simon Glass90e627b2015-04-05 16:07:41 -0600625static int designware_eth_start(struct udevice *dev)
626{
Simon Glassfa20e932020-12-03 16:55:20 -0700627 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100628 struct dw_eth_dev *priv = dev_get_priv(dev);
629 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600630
Simon Glassc154fc02017-01-11 11:46:10 +0100631 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100632 if (ret)
633 return ret;
634 ret = designware_eth_enable(priv);
635 if (ret)
636 return ret;
637
638 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600639}
640
Simon Glassc154fc02017-01-11 11:46:10 +0100641int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600642{
643 struct dw_eth_dev *priv = dev_get_priv(dev);
644
645 return _dw_eth_send(priv, packet, length);
646}
647
Simon Glassc154fc02017-01-11 11:46:10 +0100648int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600649{
650 struct dw_eth_dev *priv = dev_get_priv(dev);
651
652 return _dw_eth_recv(priv, packetp);
653}
654
Simon Glassc154fc02017-01-11 11:46:10 +0100655int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600656{
657 struct dw_eth_dev *priv = dev_get_priv(dev);
658
659 return _dw_free_pkt(priv);
660}
661
Simon Glassc154fc02017-01-11 11:46:10 +0100662void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600663{
664 struct dw_eth_dev *priv = dev_get_priv(dev);
665
666 return _dw_eth_halt(priv);
667}
668
Simon Glassc154fc02017-01-11 11:46:10 +0100669int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600670{
Simon Glassfa20e932020-12-03 16:55:20 -0700671 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600672 struct dw_eth_dev *priv = dev_get_priv(dev);
673
674 return _dw_write_hwaddr(priv, pdata->enetaddr);
675}
676
Bin Menged89bd72015-09-11 03:24:35 -0700677static int designware_eth_bind(struct udevice *dev)
678{
Simon Glass900f0da2021-08-01 18:54:34 -0600679 if (IS_ENABLED(CONFIG_PCI)) {
680 static int num_cards;
681 char name[20];
Bin Menged89bd72015-09-11 03:24:35 -0700682
Simon Glass900f0da2021-08-01 18:54:34 -0600683 /* Create a unique device name for PCI type devices */
684 if (device_is_on_pci_bus(dev)) {
685 sprintf(name, "eth_designware#%u", num_cards++);
686 device_set_name(dev, name);
687 }
Bin Menged89bd72015-09-11 03:24:35 -0700688 }
Bin Menged89bd72015-09-11 03:24:35 -0700689
690 return 0;
691}
692
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100693int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600694{
Simon Glassfa20e932020-12-03 16:55:20 -0700695 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600696 struct dw_eth_dev *priv = dev_get_priv(dev);
Nils Le Roux56b37e72023-12-02 10:39:49 +0100697 phys_addr_t iobase = pdata->iobase;
698 void *ioaddr;
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200699 int ret, err;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800700 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100701#ifdef CONFIG_CLK
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200702 int i, clock_nb;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100703
704 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200705 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
706 0);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100707 if (clock_nb > 0) {
708 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
709 GFP_KERNEL);
710 if (!priv->clocks)
711 return -ENOMEM;
712
713 for (i = 0; i < clock_nb; i++) {
714 err = clk_get_by_index(dev, i, &priv->clocks[i]);
715 if (err < 0)
716 break;
717
718 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev11e754e2018-02-06 17:12:09 +0300719 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100720 pr_err("failed to enable clock %d\n", i);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100721 goto clk_err;
722 }
723 priv->clock_count++;
724 }
725 } else if (clock_nb != -ENOENT) {
726 pr_err("failed to get clock phandle(%d)\n", clock_nb);
727 return clock_nb;
728 }
729#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600730
Jacob Chen7ceacea2017-03-27 16:54:17 +0800731#if defined(CONFIG_DM_REGULATOR)
732 struct udevice *phy_supply;
733
734 ret = device_get_supply_regulator(dev, "phy-supply",
735 &phy_supply);
736 if (ret) {
737 debug("%s: No phy supply\n", dev->name);
738 } else {
739 ret = regulator_set_enable(phy_supply, true);
740 if (ret) {
741 puts("Error enabling phy supply\n");
742 return ret;
743 }
744 }
745#endif
746
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800747 ret = reset_get_bulk(dev, &reset_bulk);
748 if (ret)
749 dev_warn(dev, "Can't get reset: %d\n", ret);
750 else
751 reset_deassert_bulk(&reset_bulk);
752
Bin Menged89bd72015-09-11 03:24:35 -0700753 /*
754 * If we are on PCI bus, either directly attached to a PCI root port,
Simon Glass71fa5b42020-12-03 16:55:18 -0700755 * or via a PCI bridge, fill in plat before we probe the hardware.
Bin Menged89bd72015-09-11 03:24:35 -0700756 */
Simon Glass900f0da2021-08-01 18:54:34 -0600757 if (IS_ENABLED(CONFIG_PCI) && device_is_on_pci_bus(dev)) {
Nils Le Roux56b37e72023-12-02 10:39:49 +0100758 u32 pcibase;
Bin Menged89bd72015-09-11 03:24:35 -0700759
Nils Le Roux56b37e72023-12-02 10:39:49 +0100760 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &pcibase);
761 pcibase &= PCI_BASE_ADDRESS_MEM_MASK;
762
763 iobase = dm_pci_mem_to_phys(dev, pcibase);
Bin Menged89bd72015-09-11 03:24:35 -0700764 pdata->iobase = iobase;
765 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
766 }
Bin Menged89bd72015-09-11 03:24:35 -0700767
Nils Le Roux56b37e72023-12-02 10:39:49 +0100768 debug("%s, iobase=%pa, priv=%p\n", __func__, &iobase, priv);
769 ioaddr = phys_to_virt(iobase);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200770 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
771 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600772 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300773 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600774
Neil Armstrong47318c92021-02-24 15:02:39 +0100775#if IS_ENABLED(CONFIG_DM_MDIO)
776 ret = dw_dm_mdio_init(dev->name, dev);
777#else
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200778 ret = dw_mdio_init(dev->name, dev);
Neil Armstrong47318c92021-02-24 15:02:39 +0100779#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200780 if (ret) {
781 err = ret;
782 goto mdio_err;
783 }
Simon Glass90e627b2015-04-05 16:07:41 -0600784 priv->bus = miiphy_get_dev_by_name(dev->name);
Baruch Siachc00982a2023-10-25 11:08:44 +0300785 priv->dev = dev;
Simon Glass90e627b2015-04-05 16:07:41 -0600786
787 ret = dw_phy_init(priv, dev);
788 debug("%s, ret=%d\n", __func__, ret);
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200789 if (!ret)
790 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600791
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200792 /* continue here for cleanup if no PHY found */
793 err = ret;
794 mdio_unregister(priv->bus);
795 mdio_free(priv->bus);
796mdio_err:
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100797
798#ifdef CONFIG_CLK
799clk_err:
800 ret = clk_release_all(priv->clocks, priv->clock_count);
801 if (ret)
802 pr_err("failed to disable all clocks\n");
803
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100804#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200805 return err;
Simon Glass90e627b2015-04-05 16:07:41 -0600806}
807
Bin Mengf0f02772015-10-07 21:32:38 -0700808static int designware_eth_remove(struct udevice *dev)
809{
810 struct dw_eth_dev *priv = dev_get_priv(dev);
811
812 free(priv->phydev);
813 mdio_unregister(priv->bus);
814 mdio_free(priv->bus);
815
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100816#ifdef CONFIG_CLK
817 return clk_release_all(priv->clocks, priv->clock_count);
818#else
Bin Mengf0f02772015-10-07 21:32:38 -0700819 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100820#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700821}
822
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100823const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600824 .start = designware_eth_start,
825 .send = designware_eth_send,
826 .recv = designware_eth_recv,
827 .free_pkt = designware_eth_free_pkt,
828 .stop = designware_eth_stop,
829 .write_hwaddr = designware_eth_write_hwaddr,
830};
831
Simon Glassaad29ae2020-12-03 16:55:21 -0700832int designware_eth_of_to_plat(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600833{
Simon Glassfa20e932020-12-03 16:55:20 -0700834 struct dw_eth_pdata *dw_pdata = dev_get_plat(dev);
Simon Glassfa4689a2019-12-06 21:41:35 -0700835#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100836 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300837#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100838 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glassfa4689a2019-12-06 21:41:35 -0700839#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100840 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300841#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100842 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600843
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200844 pdata->iobase = dev_read_addr(dev);
Marek BehĂșnbc194772022-04-07 00:33:01 +0200845 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200846 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Simon Glass90e627b2015-04-05 16:07:41 -0600847 return -EINVAL;
Simon Glass90e627b2015-04-05 16:07:41 -0600848
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200849 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300850
Simon Glassfa4689a2019-12-06 21:41:35 -0700851#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich150005b2017-06-07 18:46:01 +0200852 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100853 reset_flags |= GPIOD_ACTIVE_LOW;
854
855 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
856 &priv->reset_gpio, reset_flags);
857 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200858 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
859 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100860 } else if (ret == -ENOENT) {
861 ret = 0;
862 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300863#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100864
865 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600866}
867
868static const struct udevice_id designware_eth_ids[] = {
869 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200870 { .compatible = "amlogic,meson6-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100871 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev5738e942019-10-07 19:10:50 +0300872 { .compatible = "snps,arc-dwmac-3.70a" },
Kongyang Liu1fbf86c2024-04-20 15:00:27 +0800873 { .compatible = "sophgo,cv1800b-dwmac" },
Simon Glass90e627b2015-04-05 16:07:41 -0600874 { }
875};
876
Marek Vasut7e7e6172015-07-25 18:42:34 +0200877U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600878 .name = "eth_designware",
879 .id = UCLASS_ETH,
880 .of_match = designware_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700881 .of_to_plat = designware_eth_of_to_plat,
Bin Menged89bd72015-09-11 03:24:35 -0700882 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600883 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700884 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600885 .ops = &designware_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700886 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -0700887 .plat_auto = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600888 .flags = DM_FLAG_ALLOC_PRIV_DMA,
889};
Bin Menged89bd72015-09-11 03:24:35 -0700890
891static struct pci_device_id supported[] = {
892 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
893 { }
894};
895
896U_BOOT_PCI_DEVICE(eth_designware, supported);