blob: c222197b114ba377d3b2aec0f8068d1827f53643 [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
355 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530356}
357
Simon Glasse50c4d12015-04-05 16:07:40 -0600358static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530359{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530360 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400361 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530362
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400363 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
364 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530365
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400366 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530367}
368
Simon Glassc154fc02017-01-11 11:46:10 +0100369int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530370{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530371 struct eth_mac_regs *mac_p = priv->mac_regs_p;
372 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400373 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600374 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530375
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400376 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000377
Quentin Schulz7f920dd2018-06-04 12:17:33 +0200378 /*
379 * When a MII PHY is used, we must set the PS bit for the DMA
380 * reset to succeed.
381 */
382 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
383 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
384 else
385 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
386
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400387 start = get_timer(0);
388 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Tom Rini364d0022023-01-10 11:19:45 -0500389 if (get_timer(start) >= CFG_MACRESET_TIMEOUT) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300390 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600391 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300392 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200393
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400394 mdelay(100);
395 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530396
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800397 /*
398 * Soft reset above clears HW address registers.
399 * So we have to set it here once again.
400 */
401 _dw_write_hwaddr(priv, enetaddr);
402
Simon Glasse50c4d12015-04-05 16:07:40 -0600403 rx_descs_init(priv);
404 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530405
Ian Campbell4164b742014-05-08 22:26:35 +0100406 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530407
Sonic Zhangb917b622015-01-29 14:38:50 +0800408#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400409 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
410 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800411#else
412 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
413 &dma_p->opmode);
414#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530415
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400416 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530417
Sonic Zhang962c95c2015-01-29 13:37:31 +0800418#ifdef CONFIG_DW_AXI_BURST_LEN
419 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
420#endif
421
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400422 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600423 ret = phy_startup(priv->phydev);
424 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400425 printf("Could not initialize PHY %s\n",
426 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600427 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530428 }
429
Simon Glass4afa85e2017-01-11 11:46:08 +0100430 ret = dw_adjust_link(priv, mac_p, priv->phydev);
431 if (ret)
432 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530433
Simon Glass3240e942017-01-11 11:46:09 +0100434 return 0;
435}
436
Simon Glassc154fc02017-01-11 11:46:10 +0100437int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100438{
439 struct eth_mac_regs *mac_p = priv->mac_regs_p;
440
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400441 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600442 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530443
Armando Visconti038c9d52012-03-26 00:09:55 +0000444 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530445
446 return 0;
447}
448
Florian Fainelli65f686b2017-12-09 14:59:55 -0800449#define ETH_ZLEN 60
450
Simon Glasse50c4d12015-04-05 16:07:40 -0600451static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530452{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530453 struct eth_dma_regs *dma_p = priv->dma_regs_p;
454 u32 desc_num = priv->tx_currdescnum;
455 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200456 ulong desc_start = (ulong)desc_p;
457 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200458 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300459 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200460 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100461 /*
462 * Strictly we only need to invalidate the "txrx_status" field
463 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200464 * invalidate only 4 bytes, so we flush the entire descriptor,
465 * which is 16 bytes in total. This is safe because the
466 * individual descriptors in the array are each aligned to
467 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100468 */
Marek Vasut15193042014-09-15 01:05:23 +0200469 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400470
Vipin KUMAR1f873122010-06-29 10:53:34 +0530471 /* Check if the descriptor is owned by CPU */
472 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
473 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600474 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530475 }
476
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200477 memcpy((void *)data_start, packet, length);
Simon Goldschmidt80385de2018-11-17 10:24:42 +0100478 if (length < ETH_ZLEN) {
479 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
480 length = ETH_ZLEN;
481 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530482
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400483 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200484 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400485
Vipin KUMAR1f873122010-06-29 10:53:34 +0530486#if defined(CONFIG_DW_ALTDESCRIPTOR)
487 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100488 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
489 ((length << DESC_TXCTRL_SIZE1SHFT) &
490 DESC_TXCTRL_SIZE1MASK);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530491
492 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
493 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
494#else
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100495 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
496 ((length << DESC_TXCTRL_SIZE1SHFT) &
497 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
498 DESC_TXCTRL_TXFIRST;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530499
500 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
501#endif
502
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400503 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200504 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400505
Vipin KUMAR1f873122010-06-29 10:53:34 +0530506 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500507 if (++desc_num >= CFG_TX_DESCR_NUM)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530508 desc_num = 0;
509
510 priv->tx_currdescnum = desc_num;
511
512 /* Start the transmission */
513 writel(POLL_DATA, &dma_p->txpolldemand);
514
515 return 0;
516}
517
Simon Glass90e627b2015-04-05 16:07:41 -0600518static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530519{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400520 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530521 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600522 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200523 ulong desc_start = (ulong)desc_p;
524 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200525 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Baruch Siachc00982a2023-10-25 11:08:44 +0300526 ulong data_start = dev_bus_to_phys(priv->dev, desc_p->dmamac_addr);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200527 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530528
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400529 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200530 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400531
532 status = desc_p->txrx_status;
533
Vipin KUMAR1f873122010-06-29 10:53:34 +0530534 /* Check if the owner is the CPU */
535 if (!(status & DESC_RXSTS_OWNBYDMA)) {
536
Marek Vasut4ab539a2015-12-20 03:59:23 +0100537 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530538 DESC_RXSTS_FRMLENSHFT;
539
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400540 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200541 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
542 invalidate_dcache_range(data_start, data_end);
Baruch Siachc00982a2023-10-25 11:08:44 +0300543 *packetp = (uchar *)(ulong)dev_bus_to_phys(priv->dev,
544 desc_p->dmamac_addr);
Simon Glass90e627b2015-04-05 16:07:41 -0600545 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400546
Simon Glass90e627b2015-04-05 16:07:41 -0600547 return length;
548}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530549
Simon Glass90e627b2015-04-05 16:07:41 -0600550static int _dw_free_pkt(struct dw_eth_dev *priv)
551{
552 u32 desc_num = priv->rx_currdescnum;
553 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200554 ulong desc_start = (ulong)desc_p;
555 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600556 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530557
Simon Glass90e627b2015-04-05 16:07:41 -0600558 /*
559 * Make the current descriptor valid again and go to
560 * the next one
561 */
562 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400563
Simon Glass90e627b2015-04-05 16:07:41 -0600564 /* Flush only status field - others weren't changed */
565 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530566
Simon Glass90e627b2015-04-05 16:07:41 -0600567 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500568 if (++desc_num >= CFG_RX_DESCR_NUM)
Simon Glass90e627b2015-04-05 16:07:41 -0600569 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530570 priv->rx_currdescnum = desc_num;
571
Simon Glass90e627b2015-04-05 16:07:41 -0600572 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530573}
574
Simon Glasse50c4d12015-04-05 16:07:40 -0600575static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530576{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400577 struct phy_device *phydev;
Neil Armstrong47318c92021-02-24 15:02:39 +0100578 int ret;
579
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000580 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
581 eth_phy_set_mdio_bus(dev, NULL);
582
Tom Rinie4bb4a22022-11-27 10:25:07 -0500583#if IS_ENABLED(CONFIG_DM_MDIO)
Neil Armstrong47318c92021-02-24 15:02:39 +0100584 phydev = dm_eth_phy_connect(dev);
585 if (!phydev)
586 return -ENODEV;
587#else
588 int phy_addr = -1;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530589
Jonas Karlman2603bbc2024-01-18 07:19:45 +0000590 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
591 phy_addr = eth_phy_get_addr(dev);
592
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400593#ifdef CONFIG_PHY_ADDR
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200594 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530595#endif
596
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200597 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400598 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600599 return -ENODEV;
Neil Armstrong47318c92021-02-24 15:02:39 +0100600#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530601
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400602 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300603 if (priv->max_speed) {
604 ret = phy_set_supported(phydev, priv->max_speed);
605 if (ret)
606 return ret;
607 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400608 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530609
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400610 priv->phydev = phydev;
611 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530612
Simon Glasse50c4d12015-04-05 16:07:40 -0600613 return 0;
614}
Simon Glass90e627b2015-04-05 16:07:41 -0600615
Simon Glass90e627b2015-04-05 16:07:41 -0600616static int designware_eth_start(struct udevice *dev)
617{
Simon Glassfa20e932020-12-03 16:55:20 -0700618 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100619 struct dw_eth_dev *priv = dev_get_priv(dev);
620 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600621
Simon Glassc154fc02017-01-11 11:46:10 +0100622 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100623 if (ret)
624 return ret;
625 ret = designware_eth_enable(priv);
626 if (ret)
627 return ret;
628
629 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600630}
631
Simon Glassc154fc02017-01-11 11:46:10 +0100632int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600633{
634 struct dw_eth_dev *priv = dev_get_priv(dev);
635
636 return _dw_eth_send(priv, packet, length);
637}
638
Simon Glassc154fc02017-01-11 11:46:10 +0100639int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600640{
641 struct dw_eth_dev *priv = dev_get_priv(dev);
642
643 return _dw_eth_recv(priv, packetp);
644}
645
Simon Glassc154fc02017-01-11 11:46:10 +0100646int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600647{
648 struct dw_eth_dev *priv = dev_get_priv(dev);
649
650 return _dw_free_pkt(priv);
651}
652
Simon Glassc154fc02017-01-11 11:46:10 +0100653void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600654{
655 struct dw_eth_dev *priv = dev_get_priv(dev);
656
657 return _dw_eth_halt(priv);
658}
659
Simon Glassc154fc02017-01-11 11:46:10 +0100660int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600661{
Simon Glassfa20e932020-12-03 16:55:20 -0700662 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600663 struct dw_eth_dev *priv = dev_get_priv(dev);
664
665 return _dw_write_hwaddr(priv, pdata->enetaddr);
666}
667
Bin Menged89bd72015-09-11 03:24:35 -0700668static int designware_eth_bind(struct udevice *dev)
669{
Simon Glass900f0da2021-08-01 18:54:34 -0600670 if (IS_ENABLED(CONFIG_PCI)) {
671 static int num_cards;
672 char name[20];
Bin Menged89bd72015-09-11 03:24:35 -0700673
Simon Glass900f0da2021-08-01 18:54:34 -0600674 /* Create a unique device name for PCI type devices */
675 if (device_is_on_pci_bus(dev)) {
676 sprintf(name, "eth_designware#%u", num_cards++);
677 device_set_name(dev, name);
678 }
Bin Menged89bd72015-09-11 03:24:35 -0700679 }
Bin Menged89bd72015-09-11 03:24:35 -0700680
681 return 0;
682}
683
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100684int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600685{
Simon Glassfa20e932020-12-03 16:55:20 -0700686 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600687 struct dw_eth_dev *priv = dev_get_priv(dev);
Nils Le Roux56b37e72023-12-02 10:39:49 +0100688 phys_addr_t iobase = pdata->iobase;
689 void *ioaddr;
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200690 int ret, err;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800691 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100692#ifdef CONFIG_CLK
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200693 int i, clock_nb;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100694
695 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200696 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
697 0);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100698 if (clock_nb > 0) {
699 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
700 GFP_KERNEL);
701 if (!priv->clocks)
702 return -ENOMEM;
703
704 for (i = 0; i < clock_nb; i++) {
705 err = clk_get_by_index(dev, i, &priv->clocks[i]);
706 if (err < 0)
707 break;
708
709 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev11e754e2018-02-06 17:12:09 +0300710 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100711 pr_err("failed to enable clock %d\n", i);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100712 goto clk_err;
713 }
714 priv->clock_count++;
715 }
716 } else if (clock_nb != -ENOENT) {
717 pr_err("failed to get clock phandle(%d)\n", clock_nb);
718 return clock_nb;
719 }
720#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600721
Jacob Chen7ceacea2017-03-27 16:54:17 +0800722#if defined(CONFIG_DM_REGULATOR)
723 struct udevice *phy_supply;
724
725 ret = device_get_supply_regulator(dev, "phy-supply",
726 &phy_supply);
727 if (ret) {
728 debug("%s: No phy supply\n", dev->name);
729 } else {
730 ret = regulator_set_enable(phy_supply, true);
731 if (ret) {
732 puts("Error enabling phy supply\n");
733 return ret;
734 }
735 }
736#endif
737
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800738 ret = reset_get_bulk(dev, &reset_bulk);
739 if (ret)
740 dev_warn(dev, "Can't get reset: %d\n", ret);
741 else
742 reset_deassert_bulk(&reset_bulk);
743
Bin Menged89bd72015-09-11 03:24:35 -0700744 /*
745 * If we are on PCI bus, either directly attached to a PCI root port,
Simon Glass71fa5b42020-12-03 16:55:18 -0700746 * or via a PCI bridge, fill in plat before we probe the hardware.
Bin Menged89bd72015-09-11 03:24:35 -0700747 */
Simon Glass900f0da2021-08-01 18:54:34 -0600748 if (IS_ENABLED(CONFIG_PCI) && device_is_on_pci_bus(dev)) {
Nils Le Roux56b37e72023-12-02 10:39:49 +0100749 u32 pcibase;
Bin Menged89bd72015-09-11 03:24:35 -0700750
Nils Le Roux56b37e72023-12-02 10:39:49 +0100751 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &pcibase);
752 pcibase &= PCI_BASE_ADDRESS_MEM_MASK;
753
754 iobase = dm_pci_mem_to_phys(dev, pcibase);
Bin Menged89bd72015-09-11 03:24:35 -0700755 pdata->iobase = iobase;
756 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
757 }
Bin Menged89bd72015-09-11 03:24:35 -0700758
Nils Le Roux56b37e72023-12-02 10:39:49 +0100759 debug("%s, iobase=%pa, priv=%p\n", __func__, &iobase, priv);
760 ioaddr = phys_to_virt(iobase);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200761 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
762 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600763 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300764 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600765
Neil Armstrong47318c92021-02-24 15:02:39 +0100766#if IS_ENABLED(CONFIG_DM_MDIO)
767 ret = dw_dm_mdio_init(dev->name, dev);
768#else
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200769 ret = dw_mdio_init(dev->name, dev);
Neil Armstrong47318c92021-02-24 15:02:39 +0100770#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200771 if (ret) {
772 err = ret;
773 goto mdio_err;
774 }
Simon Glass90e627b2015-04-05 16:07:41 -0600775 priv->bus = miiphy_get_dev_by_name(dev->name);
Baruch Siachc00982a2023-10-25 11:08:44 +0300776 priv->dev = dev;
Simon Glass90e627b2015-04-05 16:07:41 -0600777
778 ret = dw_phy_init(priv, dev);
779 debug("%s, ret=%d\n", __func__, ret);
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200780 if (!ret)
781 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600782
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200783 /* continue here for cleanup if no PHY found */
784 err = ret;
785 mdio_unregister(priv->bus);
786 mdio_free(priv->bus);
787mdio_err:
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100788
789#ifdef CONFIG_CLK
790clk_err:
791 ret = clk_release_all(priv->clocks, priv->clock_count);
792 if (ret)
793 pr_err("failed to disable all clocks\n");
794
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100795#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200796 return err;
Simon Glass90e627b2015-04-05 16:07:41 -0600797}
798
Bin Mengf0f02772015-10-07 21:32:38 -0700799static int designware_eth_remove(struct udevice *dev)
800{
801 struct dw_eth_dev *priv = dev_get_priv(dev);
802
803 free(priv->phydev);
804 mdio_unregister(priv->bus);
805 mdio_free(priv->bus);
806
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100807#ifdef CONFIG_CLK
808 return clk_release_all(priv->clocks, priv->clock_count);
809#else
Bin Mengf0f02772015-10-07 21:32:38 -0700810 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100811#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700812}
813
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100814const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600815 .start = designware_eth_start,
816 .send = designware_eth_send,
817 .recv = designware_eth_recv,
818 .free_pkt = designware_eth_free_pkt,
819 .stop = designware_eth_stop,
820 .write_hwaddr = designware_eth_write_hwaddr,
821};
822
Simon Glassaad29ae2020-12-03 16:55:21 -0700823int designware_eth_of_to_plat(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600824{
Simon Glassfa20e932020-12-03 16:55:20 -0700825 struct dw_eth_pdata *dw_pdata = dev_get_plat(dev);
Simon Glassfa4689a2019-12-06 21:41:35 -0700826#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100827 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300828#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100829 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glassfa4689a2019-12-06 21:41:35 -0700830#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100831 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300832#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100833 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600834
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200835 pdata->iobase = dev_read_addr(dev);
Marek BehĂșnbc194772022-04-07 00:33:01 +0200836 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200837 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Simon Glass90e627b2015-04-05 16:07:41 -0600838 return -EINVAL;
Simon Glass90e627b2015-04-05 16:07:41 -0600839
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200840 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300841
Simon Glassfa4689a2019-12-06 21:41:35 -0700842#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich150005b2017-06-07 18:46:01 +0200843 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100844 reset_flags |= GPIOD_ACTIVE_LOW;
845
846 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
847 &priv->reset_gpio, reset_flags);
848 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200849 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
850 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100851 } else if (ret == -ENOENT) {
852 ret = 0;
853 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300854#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100855
856 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600857}
858
859static const struct udevice_id designware_eth_ids[] = {
860 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200861 { .compatible = "amlogic,meson6-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100862 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev5738e942019-10-07 19:10:50 +0300863 { .compatible = "snps,arc-dwmac-3.70a" },
Simon Glass90e627b2015-04-05 16:07:41 -0600864 { }
865};
866
Marek Vasut7e7e6172015-07-25 18:42:34 +0200867U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600868 .name = "eth_designware",
869 .id = UCLASS_ETH,
870 .of_match = designware_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700871 .of_to_plat = designware_eth_of_to_plat,
Bin Menged89bd72015-09-11 03:24:35 -0700872 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600873 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700874 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600875 .ops = &designware_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700876 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -0700877 .plat_auto = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600878 .flags = DM_FLAG_ALLOC_PRIV_DMA,
879};
Bin Menged89bd72015-09-11 03:24:35 -0700880
881static struct pci_device_id supported[] = {
882 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
883 { }
884};
885
886U_BOOT_PCI_DEVICE(eth_designware, supported);