blob: 682045cea2cfb058beeae01ea527e3e6d49b3518 [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
11#include <common.h>
Patrice Chotardeebcf8c2017-11-29 09:06:11 +010012#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass90e627b2015-04-05 16:07:41 -060014#include <dm.h>
Simon Glasse50c4d12015-04-05 16:07:40 -060015#include <errno.h>
Jonas Karlman2603bbc2024-01-18 07:19:45 +000016#include <eth_phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053018#include <miiphy.h>
19#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <net.h>
Bin Menged89bd72015-09-11 03:24:35 -070021#include <pci.h>
Ley Foon Tan27d5c002018-06-14 18:45:23 +080022#include <reset.h>
Baruch Siachc00982a2023-10-25 11:08:44 +030023#include <phys2bus.h>
Simon Glass274e0b02020-05-10 11:39:56 -060024#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010026#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <dm/devres.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010028#include <dm/lists.h>
Stefan Roesed27e86c2012-05-07 12:04:25 +020029#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053031#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080032#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053033#include <asm/io.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060034#include <linux/printk.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080035#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053036#include "designware.h"
37
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040038static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
39{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010040 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
41 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040042 ulong start;
43 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050044 int timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040045
46 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
47 ((reg << MIIREGSHIFT) & MII_REGMSK);
48
49 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
50
51 start = get_timer(0);
52 while (get_timer(start) < timeout) {
53 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
54 return readl(&mac_p->miidata);
55 udelay(10);
56 };
57
Simon Glasse50c4d12015-04-05 16:07:40 -060058 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040059}
60
61static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
62 u16 val)
63{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010064 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
65 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040066 ulong start;
67 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050068 int ret = -ETIMEDOUT, timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040069
70 writel(val, &mac_p->miidata);
71 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
72 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
73
74 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
75
76 start = get_timer(0);
77 while (get_timer(start) < timeout) {
78 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
79 ret = 0;
80 break;
81 }
82 udelay(10);
83 };
84
85 return ret;
86}
87
Tom Rinie4bb4a22022-11-27 10:25:07 -050088#if CONFIG_IS_ENABLED(DM_GPIO)
Neil Armstrong1188a6d2021-04-21 10:58:01 +020089static int __dw_mdio_reset(struct udevice *dev)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010090{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010091 struct dw_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070092 struct dw_eth_pdata *pdata = dev_get_plat(dev);
Sjoerd Simons6eb44622016-02-28 22:24:55 +010093 int ret;
94
95 if (!dm_gpio_is_valid(&priv->reset_gpio))
96 return 0;
97
98 /* reset the phy */
99 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
100 if (ret)
101 return ret;
102
103 udelay(pdata->reset_delays[0]);
104
105 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
106 if (ret)
107 return ret;
108
109 udelay(pdata->reset_delays[1]);
110
111 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
112 if (ret)
113 return ret;
114
115 udelay(pdata->reset_delays[2]);
116
117 return 0;
118}
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200119
120static int dw_mdio_reset(struct mii_dev *bus)
121{
122 struct udevice *dev = bus->priv;
123
124 return __dw_mdio_reset(dev);
125}
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100126#endif
127
Neil Armstrong47318c92021-02-24 15:02:39 +0100128#if IS_ENABLED(CONFIG_DM_MDIO)
129int designware_eth_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
130{
131 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
132
133 return dw_mdio_read(pdata->mii_bus, addr, devad, reg);
134}
135
136int designware_eth_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg, u16 val)
137{
138 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
139
140 return dw_mdio_write(pdata->mii_bus, addr, devad, reg, val);
141}
142
143#if CONFIG_IS_ENABLED(DM_GPIO)
144int designware_eth_mdio_reset(struct udevice *mdio_dev)
145{
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200146 struct mdio_perdev_priv *mdio_pdata = dev_get_uclass_priv(mdio_dev);
147 struct udevice *dev = mdio_pdata->mii_bus->priv;
Neil Armstrong47318c92021-02-24 15:02:39 +0100148
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200149 return __dw_mdio_reset(dev->parent);
Neil Armstrong47318c92021-02-24 15:02:39 +0100150}
151#endif
152
153static const struct mdio_ops designware_eth_mdio_ops = {
154 .read = designware_eth_mdio_read,
155 .write = designware_eth_mdio_write,
156#if CONFIG_IS_ENABLED(DM_GPIO)
157 .reset = designware_eth_mdio_reset,
158#endif
159};
160
161static int designware_eth_mdio_probe(struct udevice *dev)
162{
163 /* Use the priv data of parent */
164 dev_set_priv(dev, dev_get_priv(dev->parent));
165
166 return 0;
167}
168
169U_BOOT_DRIVER(designware_eth_mdio) = {
170 .name = "eth_designware_mdio",
171 .id = UCLASS_MDIO,
172 .probe = designware_eth_mdio_probe,
173 .ops = &designware_eth_mdio_ops,
174 .plat_auto = sizeof(struct mdio_perdev_priv),
175};
176#endif
177
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100178static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400179{
180 struct mii_dev *bus = mdio_alloc();
181
182 if (!bus) {
183 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600184 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400185 }
186
187 bus->read = dw_mdio_read;
188 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000189 snprintf(bus->name, sizeof(bus->name), "%s", name);
Tom Rinie4bb4a22022-11-27 10:25:07 -0500190#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100191 bus->reset = dw_mdio_reset;
192#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400193
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100194 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400195
196 return mdio_register(bus);
197}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000198
Neil Armstrong47318c92021-02-24 15:02:39 +0100199#if IS_ENABLED(CONFIG_DM_MDIO)
200static int dw_dm_mdio_init(const char *name, void *priv)
201{
202 struct udevice *dev = priv;
203 ofnode node;
204 int ret;
205
206 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
207 const char *subnode_name = ofnode_get_name(node);
208 struct udevice *mdiodev;
209
210 if (strcmp(subnode_name, "mdio"))
211 continue;
212
213 ret = device_bind_driver_to_node(dev, "eth_designware_mdio",
214 subnode_name, node, &mdiodev);
215 if (ret)
216 debug("%s: not able to bind mdio device node\n", __func__);
217
218 return 0;
219 }
220
221 printf("%s: mdio node is missing, registering legacy mdio bus", __func__);
222
223 return dw_mdio_init(name, priv);
224}
225#endif
226
Simon Glasse50c4d12015-04-05 16:07:40 -0600227static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530228{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530229 struct eth_dma_regs *dma_p = priv->dma_regs_p;
230 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
231 char *txbuffs = &priv->txbuffs[0];
232 struct dmamacdescr *desc_p;
233 u32 idx;
234
Tom Rini364d0022023-01-10 11:19:45 -0500235 for (idx = 0; idx < CFG_TX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530236 desc_p = &desc_table_p[idx];
Baruch Siachc00982a2023-10-25 11:08:44 +0300237 desc_p->dmamac_addr = dev_phys_to_bus(priv->dev,
238 (ulong)&txbuffs[idx * CFG_ETH_BUFSIZE]);
239 desc_p->dmamac_next = dev_phys_to_bus(priv->dev,
240 (ulong)&desc_table_p[idx + 1]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530241
242#if defined(CONFIG_DW_ALTDESCRIPTOR)
243 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100244 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
245 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530246 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
247
248 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
249 desc_p->dmamac_cntl = 0;
250 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
251#else
252 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
253 desc_p->txrx_status = 0;
254#endif
255 }
256
257 /* Correcting the last pointer of the chain */
Baruch Siachc00982a2023-10-25 11:08:44 +0300258 desc_p->dmamac_next = dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530259
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400260 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200261 flush_dcache_range((ulong)priv->tx_mac_descrtable,
262 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400263 sizeof(priv->tx_mac_descrtable));
264
Baruch Siachc00982a2023-10-25 11:08:44 +0300265 writel(dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]),
266 &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400267 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530268}
269
Simon Glasse50c4d12015-04-05 16:07:40 -0600270static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530271{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530272 struct eth_dma_regs *dma_p = priv->dma_regs_p;
273 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
274 char *rxbuffs = &priv->rxbuffs[0];
275 struct dmamacdescr *desc_p;
276 u32 idx;
277
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400278 /* Before passing buffers to GMAC we need to make sure zeros
279 * written there right after "priv" structure allocation were
280 * flushed into RAM.
281 * Otherwise there's a chance to get some of them flushed in RAM when
282 * GMAC is already pushing data to RAM via DMA. This way incoming from
283 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200284 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400285
Tom Rini364d0022023-01-10 11:19:45 -0500286 for (idx = 0; idx < CFG_RX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530287 desc_p = &desc_table_p[idx];
Baruch Siachc00982a2023-10-25 11:08:44 +0300288 desc_p->dmamac_addr = dev_phys_to_bus(priv->dev,
289 (ulong)&rxbuffs[idx * CFG_ETH_BUFSIZE]);
290 desc_p->dmamac_next = dev_phys_to_bus(priv->dev,
291 (ulong)&desc_table_p[idx + 1]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530292
293 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100294 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530295 DESC_RXCTRL_RXCHAIN;
296
297 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
298 }
299
300 /* Correcting the last pointer of the chain */
Baruch Siachc00982a2023-10-25 11:08:44 +0300301 desc_p->dmamac_next = dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530302
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400303 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200304 flush_dcache_range((ulong)priv->rx_mac_descrtable,
305 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400306 sizeof(priv->rx_mac_descrtable));
307
Baruch Siachc00982a2023-10-25 11:08:44 +0300308 writel(dev_phys_to_bus(priv->dev, (ulong)&desc_table_p[0]),
309 &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400310 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530311}
312
Simon Glasse50c4d12015-04-05 16:07:40 -0600313static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530314{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400315 struct eth_mac_regs *mac_p = priv->mac_regs_p;
316 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400317
318 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
319 (mac_id[3] << 24);
320 macid_hi = mac_id[4] + (mac_id[5] << 8);
321
322 writel(macid_hi, &mac_p->macaddr0hi);
323 writel(macid_lo, &mac_p->macaddr0lo);
324
325 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530326}
327
Simon Glass4afa85e2017-01-11 11:46:08 +0100328static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
329 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530330{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400331 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530332
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400333 if (!phydev->link) {
334 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100335 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400336 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530337
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400338 if (phydev->speed != 1000)
339 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300340 else
341 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530342
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400343 if (phydev->speed == 100)
344 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530345
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400346 if (phydev->duplex)
347 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000348
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400349 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530350
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400351 printf("Speed: %d, %s duplex%s\n", phydev->speed,
352 (phydev->duplex) ? "full" : "half",
353 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100354
Jim Liu4ef2a112024-04-08 16:50:17 +0800355#ifdef CONFIG_ARCH_NPCM8XX
356 /* Pass all Multicast Frames */
357 setbits_le32(&mac_p->framefilt, BIT(4));
358
359#endif
Simon Glass4afa85e2017-01-11 11:46:08 +0100360 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530361}
362
Simon Glasse50c4d12015-04-05 16:07:40 -0600363static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530364{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530365 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400366 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530367
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400368 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
369 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530370
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400371 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530372}
373
Simon Glassc154fc02017-01-11 11:46:10 +0100374int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530375{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530376 struct eth_mac_regs *mac_p = priv->mac_regs_p;
377 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400378 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600379 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530380
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400381 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000382
Quentin Schulz7f920dd2018-06-04 12:17:33 +0200383 /*
384 * When a MII PHY is used, we must set the PS bit for the DMA
385 * reset to succeed.
386 */
387 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
388 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
389 else
390 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
391
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400392 start = get_timer(0);
393 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Tom Rini364d0022023-01-10 11:19:45 -0500394 if (get_timer(start) >= CFG_MACRESET_TIMEOUT) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300395 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600396 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300397 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200398
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400399 mdelay(100);
400 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530401
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800402 /*
403 * Soft reset above clears HW address registers.
404 * So we have to set it here once again.
405 */
406 _dw_write_hwaddr(priv, enetaddr);
407
Simon Glasse50c4d12015-04-05 16:07:40 -0600408 rx_descs_init(priv);
409 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530410
Ian Campbell4164b742014-05-08 22:26:35 +0100411 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530412
Sonic Zhangb917b622015-01-29 14:38:50 +0800413#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400414 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
415 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800416#else
417 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
418 &dma_p->opmode);
419#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530420
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400421 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530422
Sonic Zhang962c95c2015-01-29 13:37:31 +0800423#ifdef CONFIG_DW_AXI_BURST_LEN
424 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
425#endif
426
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400427 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600428 ret = phy_startup(priv->phydev);
429 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400430 printf("Could not initialize PHY %s\n",
431 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600432 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530433 }
434
Simon Glass4afa85e2017-01-11 11:46:08 +0100435 ret = dw_adjust_link(priv, mac_p, priv->phydev);
436 if (ret)
437 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530438
Simon Glass3240e942017-01-11 11:46:09 +0100439 return 0;
440}
441
Simon Glassc154fc02017-01-11 11:46:10 +0100442int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100443{
444 struct eth_mac_regs *mac_p = priv->mac_regs_p;
445
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400446 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600447 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530448
Armando Visconti038c9d52012-03-26 00:09:55 +0000449 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530450
451 return 0;
452}
453
Florian Fainelli65f686b2017-12-09 14:59:55 -0800454#define ETH_ZLEN 60
455
Simon Glasse50c4d12015-04-05 16:07:40 -0600456static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530457{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530458 struct eth_dma_regs *dma_p = priv->dma_regs_p;
459 u32 desc_num = priv->tx_currdescnum;
460 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200461 ulong desc_start = (ulong)desc_p;
462 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200463 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300464 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200465 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100466 /*
467 * Strictly we only need to invalidate the "txrx_status" field
468 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200469 * invalidate only 4 bytes, so we flush the entire descriptor,
470 * which is 16 bytes in total. This is safe because the
471 * individual descriptors in the array are each aligned to
472 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100473 */
Marek Vasut15193042014-09-15 01:05:23 +0200474 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400475
Vipin KUMAR1f873122010-06-29 10:53:34 +0530476 /* Check if the descriptor is owned by CPU */
477 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
478 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600479 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530480 }
481
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200482 memcpy((void *)data_start, packet, length);
Simon Goldschmidt80385de2018-11-17 10:24:42 +0100483 if (length < ETH_ZLEN) {
484 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
485 length = ETH_ZLEN;
486 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530487
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400488 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200489 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400490
Vipin KUMAR1f873122010-06-29 10:53:34 +0530491#if defined(CONFIG_DW_ALTDESCRIPTOR)
492 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100493 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
494 ((length << DESC_TXCTRL_SIZE1SHFT) &
495 DESC_TXCTRL_SIZE1MASK);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530496
497 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
498 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
499#else
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100500 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
501 ((length << DESC_TXCTRL_SIZE1SHFT) &
502 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
503 DESC_TXCTRL_TXFIRST;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530504
505 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
506#endif
507
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400508 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200509 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400510
Vipin KUMAR1f873122010-06-29 10:53:34 +0530511 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500512 if (++desc_num >= CFG_TX_DESCR_NUM)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530513 desc_num = 0;
514
515 priv->tx_currdescnum = desc_num;
516
517 /* Start the transmission */
518 writel(POLL_DATA, &dma_p->txpolldemand);
519
520 return 0;
521}
522
Simon Glass90e627b2015-04-05 16:07:41 -0600523static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530524{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400525 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530526 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600527 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200528 ulong desc_start = (ulong)desc_p;
529 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200530 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300531 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200532 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530533
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400534 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200535 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400536
537 status = desc_p->txrx_status;
538
Vipin KUMAR1f873122010-06-29 10:53:34 +0530539 /* Check if the owner is the CPU */
540 if (!(status & DESC_RXSTS_OWNBYDMA)) {
541
Marek Vasut4ab539a2015-12-20 03:59:23 +0100542 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530543 DESC_RXSTS_FRMLENSHFT;
544
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400545 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200546 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
547 invalidate_dcache_range(data_start, data_end);
Baruch Siachc00982a2023-10-25 11:08:44 +0300548 *packetp = (uchar *)(ulong)dev_bus_to_phys(priv->dev,
549 desc_p->dmamac_addr);
Simon Glass90e627b2015-04-05 16:07:41 -0600550 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400551
Simon Glass90e627b2015-04-05 16:07:41 -0600552 return length;
553}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530554
Simon Glass90e627b2015-04-05 16:07:41 -0600555static int _dw_free_pkt(struct dw_eth_dev *priv)
556{
557 u32 desc_num = priv->rx_currdescnum;
558 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200559 ulong desc_start = (ulong)desc_p;
560 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600561 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Jim Liu1f031a02024-04-08 16:49:02 +0800562 ulong data_start = desc_p->dmamac_addr;
563 ulong data_end = data_start + roundup(CFG_ETH_BUFSIZE, ARCH_DMA_MINALIGN);
564
565 /* Invalidate the descriptor buffer data */
566 invalidate_dcache_range(data_start, data_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530567
Simon Glass90e627b2015-04-05 16:07:41 -0600568 /*
569 * Make the current descriptor valid again and go to
570 * the next one
571 */
572 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400573
Simon Glass90e627b2015-04-05 16:07:41 -0600574 /* Flush only status field - others weren't changed */
575 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530576
Simon Glass90e627b2015-04-05 16:07:41 -0600577 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500578 if (++desc_num >= CFG_RX_DESCR_NUM)
Simon Glass90e627b2015-04-05 16:07:41 -0600579 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530580 priv->rx_currdescnum = desc_num;
581
Simon Glass90e627b2015-04-05 16:07:41 -0600582 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530583}
584
Simon Glasse50c4d12015-04-05 16:07:40 -0600585static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530586{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400587 struct phy_device *phydev;
Neil Armstrong47318c92021-02-24 15:02:39 +0100588 int ret;
589
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000590 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
591 eth_phy_set_mdio_bus(dev, NULL);
592
Tom Rinie4bb4a22022-11-27 10:25:07 -0500593#if IS_ENABLED(CONFIG_DM_MDIO)
Neil Armstrong47318c92021-02-24 15:02:39 +0100594 phydev = dm_eth_phy_connect(dev);
595 if (!phydev)
596 return -ENODEV;
597#else
598 int phy_addr = -1;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530599
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000600 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
601 phy_addr = eth_phy_get_addr(dev);
602
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400603#ifdef CONFIG_PHY_ADDR
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200604 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530605#endif
606
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200607 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400608 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600609 return -ENODEV;
Neil Armstrong47318c92021-02-24 15:02:39 +0100610#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530611
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400612 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300613 if (priv->max_speed) {
614 ret = phy_set_supported(phydev, priv->max_speed);
615 if (ret)
616 return ret;
617 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400618 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530619
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400620 priv->phydev = phydev;
621 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530622
Simon Glasse50c4d12015-04-05 16:07:40 -0600623 return 0;
624}
Simon Glass90e627b2015-04-05 16:07:41 -0600625
Simon Glass90e627b2015-04-05 16:07:41 -0600626static int designware_eth_start(struct udevice *dev)
627{
Simon Glassfa20e932020-12-03 16:55:20 -0700628 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100629 struct dw_eth_dev *priv = dev_get_priv(dev);
630 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600631
Simon Glassc154fc02017-01-11 11:46:10 +0100632 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100633 if (ret)
634 return ret;
635 ret = designware_eth_enable(priv);
636 if (ret)
637 return ret;
638
639 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600640}
641
Simon Glassc154fc02017-01-11 11:46:10 +0100642int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600643{
644 struct dw_eth_dev *priv = dev_get_priv(dev);
645
646 return _dw_eth_send(priv, packet, length);
647}
648
Simon Glassc154fc02017-01-11 11:46:10 +0100649int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600650{
651 struct dw_eth_dev *priv = dev_get_priv(dev);
652
653 return _dw_eth_recv(priv, packetp);
654}
655
Simon Glassc154fc02017-01-11 11:46:10 +0100656int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600657{
658 struct dw_eth_dev *priv = dev_get_priv(dev);
659
660 return _dw_free_pkt(priv);
661}
662
Simon Glassc154fc02017-01-11 11:46:10 +0100663void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600664{
665 struct dw_eth_dev *priv = dev_get_priv(dev);
666
667 return _dw_eth_halt(priv);
668}
669
Simon Glassc154fc02017-01-11 11:46:10 +0100670int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600671{
Simon Glassfa20e932020-12-03 16:55:20 -0700672 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600673 struct dw_eth_dev *priv = dev_get_priv(dev);
674
675 return _dw_write_hwaddr(priv, pdata->enetaddr);
676}
677
Bin Menged89bd72015-09-11 03:24:35 -0700678static int designware_eth_bind(struct udevice *dev)
679{
Simon Glass900f0da2021-08-01 18:54:34 -0600680 if (IS_ENABLED(CONFIG_PCI)) {
681 static int num_cards;
682 char name[20];
Bin Menged89bd72015-09-11 03:24:35 -0700683
Simon Glass900f0da2021-08-01 18:54:34 -0600684 /* Create a unique device name for PCI type devices */
685 if (device_is_on_pci_bus(dev)) {
686 sprintf(name, "eth_designware#%u", num_cards++);
687 device_set_name(dev, name);
688 }
Bin Menged89bd72015-09-11 03:24:35 -0700689 }
Bin Menged89bd72015-09-11 03:24:35 -0700690
691 return 0;
692}
693
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100694int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600695{
Simon Glassfa20e932020-12-03 16:55:20 -0700696 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600697 struct dw_eth_dev *priv = dev_get_priv(dev);
Nils Le Roux56b37e72023-12-02 10:39:49 +0100698 phys_addr_t iobase = pdata->iobase;
699 void *ioaddr;
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200700 int ret, err;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800701 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100702#ifdef CONFIG_CLK
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200703 int i, clock_nb;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100704
705 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200706 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
707 0);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100708 if (clock_nb > 0) {
709 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
710 GFP_KERNEL);
711 if (!priv->clocks)
712 return -ENOMEM;
713
714 for (i = 0; i < clock_nb; i++) {
715 err = clk_get_by_index(dev, i, &priv->clocks[i]);
716 if (err < 0)
717 break;
718
719 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev11e754e2018-02-06 17:12:09 +0300720 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100721 pr_err("failed to enable clock %d\n", i);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100722 goto clk_err;
723 }
724 priv->clock_count++;
725 }
726 } else if (clock_nb != -ENOENT) {
727 pr_err("failed to get clock phandle(%d)\n", clock_nb);
728 return clock_nb;
729 }
730#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600731
Jacob Chen7ceacea2017-03-27 16:54:17 +0800732#if defined(CONFIG_DM_REGULATOR)
733 struct udevice *phy_supply;
734
735 ret = device_get_supply_regulator(dev, "phy-supply",
736 &phy_supply);
737 if (ret) {
738 debug("%s: No phy supply\n", dev->name);
739 } else {
740 ret = regulator_set_enable(phy_supply, true);
741 if (ret) {
742 puts("Error enabling phy supply\n");
743 return ret;
744 }
745 }
746#endif
747
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800748 ret = reset_get_bulk(dev, &reset_bulk);
749 if (ret)
750 dev_warn(dev, "Can't get reset: %d\n", ret);
751 else
752 reset_deassert_bulk(&reset_bulk);
753
Bin Menged89bd72015-09-11 03:24:35 -0700754 /*
755 * If we are on PCI bus, either directly attached to a PCI root port,
Simon Glass71fa5b42020-12-03 16:55:18 -0700756 * or via a PCI bridge, fill in plat before we probe the hardware.
Bin Menged89bd72015-09-11 03:24:35 -0700757 */
Simon Glass900f0da2021-08-01 18:54:34 -0600758 if (IS_ENABLED(CONFIG_PCI) && device_is_on_pci_bus(dev)) {
Nils Le Roux56b37e72023-12-02 10:39:49 +0100759 u32 pcibase;
Bin Menged89bd72015-09-11 03:24:35 -0700760
Nils Le Roux56b37e72023-12-02 10:39:49 +0100761 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &pcibase);
762 pcibase &= PCI_BASE_ADDRESS_MEM_MASK;
763
764 iobase = dm_pci_mem_to_phys(dev, pcibase);
Bin Menged89bd72015-09-11 03:24:35 -0700765 pdata->iobase = iobase;
766 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
767 }
Bin Menged89bd72015-09-11 03:24:35 -0700768
Nils Le Roux56b37e72023-12-02 10:39:49 +0100769 debug("%s, iobase=%pa, priv=%p\n", __func__, &iobase, priv);
770 ioaddr = phys_to_virt(iobase);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200771 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
772 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600773 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300774 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600775
Neil Armstrong47318c92021-02-24 15:02:39 +0100776#if IS_ENABLED(CONFIG_DM_MDIO)
777 ret = dw_dm_mdio_init(dev->name, dev);
778#else
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200779 ret = dw_mdio_init(dev->name, dev);
Neil Armstrong47318c92021-02-24 15:02:39 +0100780#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200781 if (ret) {
782 err = ret;
783 goto mdio_err;
784 }
Simon Glass90e627b2015-04-05 16:07:41 -0600785 priv->bus = miiphy_get_dev_by_name(dev->name);
Baruch Siachc00982a2023-10-25 11:08:44 +0300786 priv->dev = dev;
Simon Glass90e627b2015-04-05 16:07:41 -0600787
788 ret = dw_phy_init(priv, dev);
789 debug("%s, ret=%d\n", __func__, ret);
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200790 if (!ret)
791 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600792
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200793 /* continue here for cleanup if no PHY found */
794 err = ret;
795 mdio_unregister(priv->bus);
796 mdio_free(priv->bus);
797mdio_err:
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100798
799#ifdef CONFIG_CLK
800clk_err:
801 ret = clk_release_all(priv->clocks, priv->clock_count);
802 if (ret)
803 pr_err("failed to disable all clocks\n");
804
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100805#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200806 return err;
Simon Glass90e627b2015-04-05 16:07:41 -0600807}
808
Bin Mengf0f02772015-10-07 21:32:38 -0700809static int designware_eth_remove(struct udevice *dev)
810{
811 struct dw_eth_dev *priv = dev_get_priv(dev);
812
813 free(priv->phydev);
814 mdio_unregister(priv->bus);
815 mdio_free(priv->bus);
816
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100817#ifdef CONFIG_CLK
818 return clk_release_all(priv->clocks, priv->clock_count);
819#else
Bin Mengf0f02772015-10-07 21:32:38 -0700820 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100821#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700822}
823
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100824const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600825 .start = designware_eth_start,
826 .send = designware_eth_send,
827 .recv = designware_eth_recv,
828 .free_pkt = designware_eth_free_pkt,
829 .stop = designware_eth_stop,
830 .write_hwaddr = designware_eth_write_hwaddr,
831};
832
Simon Glassaad29ae2020-12-03 16:55:21 -0700833int designware_eth_of_to_plat(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600834{
Simon Glassfa20e932020-12-03 16:55:20 -0700835 struct dw_eth_pdata *dw_pdata = dev_get_plat(dev);
Simon Glassfa4689a2019-12-06 21:41:35 -0700836#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100837 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300838#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100839 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glassfa4689a2019-12-06 21:41:35 -0700840#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100841 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300842#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100843 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600844
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200845 pdata->iobase = dev_read_addr(dev);
Marek BehĂșnbc194772022-04-07 00:33:01 +0200846 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200847 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Simon Glass90e627b2015-04-05 16:07:41 -0600848 return -EINVAL;
Simon Glass90e627b2015-04-05 16:07:41 -0600849
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200850 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300851
Simon Glassfa4689a2019-12-06 21:41:35 -0700852#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich150005b2017-06-07 18:46:01 +0200853 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100854 reset_flags |= GPIOD_ACTIVE_LOW;
855
856 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
857 &priv->reset_gpio, reset_flags);
858 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200859 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
860 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100861 } else if (ret == -ENOENT) {
862 ret = 0;
863 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300864#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100865
866 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600867}
868
869static const struct udevice_id designware_eth_ids[] = {
870 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200871 { .compatible = "amlogic,meson6-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100872 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev5738e942019-10-07 19:10:50 +0300873 { .compatible = "snps,arc-dwmac-3.70a" },
Kongyang Liu1fbf86c2024-04-20 15:00:27 +0800874 { .compatible = "sophgo,cv1800b-dwmac" },
Simon Glass90e627b2015-04-05 16:07:41 -0600875 { }
876};
877
Marek Vasut7e7e6172015-07-25 18:42:34 +0200878U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600879 .name = "eth_designware",
880 .id = UCLASS_ETH,
881 .of_match = designware_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700882 .of_to_plat = designware_eth_of_to_plat,
Bin Menged89bd72015-09-11 03:24:35 -0700883 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600884 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700885 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600886 .ops = &designware_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700887 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -0700888 .plat_auto = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600889 .flags = DM_FLAG_ALLOC_PRIV_DMA,
890};
Bin Menged89bd72015-09-11 03:24:35 -0700891
892static struct pci_device_id supported[] = {
893 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
894 { }
895};
896
897U_BOOT_PCI_DEVICE(eth_designware, supported);