blob: 20b86e74cecf31d769fb5aeb9cfb975973c3a95a [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>
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>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010024#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <dm/devres.h>
Neil Armstrong47318c92021-02-24 15:02:39 +010026#include <dm/lists.h>
Stefan Roesed27e86c2012-05-07 12:04:25 +020027#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053029#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080030#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053031#include <asm/io.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060032#include <linux/printk.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080033#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053034#include "designware.h"
35
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040036static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
37{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010038 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
39 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040040 ulong start;
41 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050042 int timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040043
44 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
45 ((reg << MIIREGSHIFT) & MII_REGMSK);
46
47 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
48
49 start = get_timer(0);
50 while (get_timer(start) < timeout) {
51 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
52 return readl(&mac_p->miidata);
53 udelay(10);
54 };
55
Simon Glasse50c4d12015-04-05 16:07:40 -060056 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040057}
58
59static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
60 u16 val)
61{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010062 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
63 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040064 ulong start;
65 u16 miiaddr;
Tom Rini364d0022023-01-10 11:19:45 -050066 int ret = -ETIMEDOUT, timeout = CFG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040067
68 writel(val, &mac_p->miidata);
69 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
70 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
71
72 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
73
74 start = get_timer(0);
75 while (get_timer(start) < timeout) {
76 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
77 ret = 0;
78 break;
79 }
80 udelay(10);
81 };
82
83 return ret;
84}
85
Tom Rinie4bb4a22022-11-27 10:25:07 -050086#if CONFIG_IS_ENABLED(DM_GPIO)
Neil Armstrong1188a6d2021-04-21 10:58:01 +020087static int __dw_mdio_reset(struct udevice *dev)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010088{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010089 struct dw_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070090 struct dw_eth_pdata *pdata = dev_get_plat(dev);
Sjoerd Simons6eb44622016-02-28 22:24:55 +010091 int ret;
92
93 if (!dm_gpio_is_valid(&priv->reset_gpio))
94 return 0;
95
96 /* reset the phy */
97 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
98 if (ret)
99 return ret;
100
101 udelay(pdata->reset_delays[0]);
102
103 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
104 if (ret)
105 return ret;
106
107 udelay(pdata->reset_delays[1]);
108
109 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
110 if (ret)
111 return ret;
112
113 udelay(pdata->reset_delays[2]);
114
115 return 0;
116}
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200117
118static int dw_mdio_reset(struct mii_dev *bus)
119{
120 struct udevice *dev = bus->priv;
121
122 return __dw_mdio_reset(dev);
123}
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100124#endif
125
Neil Armstrong47318c92021-02-24 15:02:39 +0100126#if IS_ENABLED(CONFIG_DM_MDIO)
127int designware_eth_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
128{
129 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
130
131 return dw_mdio_read(pdata->mii_bus, addr, devad, reg);
132}
133
134int designware_eth_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg, u16 val)
135{
136 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
137
138 return dw_mdio_write(pdata->mii_bus, addr, devad, reg, val);
139}
140
141#if CONFIG_IS_ENABLED(DM_GPIO)
142int designware_eth_mdio_reset(struct udevice *mdio_dev)
143{
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200144 struct mdio_perdev_priv *mdio_pdata = dev_get_uclass_priv(mdio_dev);
145 struct udevice *dev = mdio_pdata->mii_bus->priv;
Neil Armstrong47318c92021-02-24 15:02:39 +0100146
Neil Armstrong1188a6d2021-04-21 10:58:01 +0200147 return __dw_mdio_reset(dev->parent);
Neil Armstrong47318c92021-02-24 15:02:39 +0100148}
149#endif
150
151static const struct mdio_ops designware_eth_mdio_ops = {
152 .read = designware_eth_mdio_read,
153 .write = designware_eth_mdio_write,
154#if CONFIG_IS_ENABLED(DM_GPIO)
155 .reset = designware_eth_mdio_reset,
156#endif
157};
158
159static int designware_eth_mdio_probe(struct udevice *dev)
160{
161 /* Use the priv data of parent */
162 dev_set_priv(dev, dev_get_priv(dev->parent));
163
164 return 0;
165}
166
167U_BOOT_DRIVER(designware_eth_mdio) = {
168 .name = "eth_designware_mdio",
169 .id = UCLASS_MDIO,
170 .probe = designware_eth_mdio_probe,
171 .ops = &designware_eth_mdio_ops,
172 .plat_auto = sizeof(struct mdio_perdev_priv),
173};
174#endif
175
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100176static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400177{
178 struct mii_dev *bus = mdio_alloc();
179
180 if (!bus) {
181 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600182 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400183 }
184
185 bus->read = dw_mdio_read;
186 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000187 snprintf(bus->name, sizeof(bus->name), "%s", name);
Tom Rinie4bb4a22022-11-27 10:25:07 -0500188#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100189 bus->reset = dw_mdio_reset;
190#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400191
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100192 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400193
194 return mdio_register(bus);
195}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000196
Neil Armstrong47318c92021-02-24 15:02:39 +0100197#if IS_ENABLED(CONFIG_DM_MDIO)
198static int dw_dm_mdio_init(const char *name, void *priv)
199{
200 struct udevice *dev = priv;
201 ofnode node;
202 int ret;
203
204 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
205 const char *subnode_name = ofnode_get_name(node);
206 struct udevice *mdiodev;
207
208 if (strcmp(subnode_name, "mdio"))
209 continue;
210
211 ret = device_bind_driver_to_node(dev, "eth_designware_mdio",
212 subnode_name, node, &mdiodev);
213 if (ret)
214 debug("%s: not able to bind mdio device node\n", __func__);
215
216 return 0;
217 }
218
219 printf("%s: mdio node is missing, registering legacy mdio bus", __func__);
220
221 return dw_mdio_init(name, priv);
222}
223#endif
224
Simon Glasse50c4d12015-04-05 16:07:40 -0600225static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530226{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530227 struct eth_dma_regs *dma_p = priv->dma_regs_p;
228 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
229 char *txbuffs = &priv->txbuffs[0];
230 struct dmamacdescr *desc_p;
231 u32 idx;
232
Tom Rini364d0022023-01-10 11:19:45 -0500233 for (idx = 0; idx < CFG_TX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530234 desc_p = &desc_table_p[idx];
Tom Rini364d0022023-01-10 11:19:45 -0500235 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CFG_ETH_BUFSIZE];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200236 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530237
238#if defined(CONFIG_DW_ALTDESCRIPTOR)
239 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100240 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
241 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530242 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
243
244 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
245 desc_p->dmamac_cntl = 0;
246 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
247#else
248 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
249 desc_p->txrx_status = 0;
250#endif
251 }
252
253 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200254 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530255
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400256 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200257 flush_dcache_range((ulong)priv->tx_mac_descrtable,
258 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400259 sizeof(priv->tx_mac_descrtable));
260
Vipin KUMAR1f873122010-06-29 10:53:34 +0530261 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400262 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530263}
264
Simon Glasse50c4d12015-04-05 16:07:40 -0600265static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530266{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530267 struct eth_dma_regs *dma_p = priv->dma_regs_p;
268 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
269 char *rxbuffs = &priv->rxbuffs[0];
270 struct dmamacdescr *desc_p;
271 u32 idx;
272
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400273 /* Before passing buffers to GMAC we need to make sure zeros
274 * written there right after "priv" structure allocation were
275 * flushed into RAM.
276 * Otherwise there's a chance to get some of them flushed in RAM when
277 * GMAC is already pushing data to RAM via DMA. This way incoming from
278 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200279 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400280
Tom Rini364d0022023-01-10 11:19:45 -0500281 for (idx = 0; idx < CFG_RX_DESCR_NUM; idx++) {
Vipin KUMAR1f873122010-06-29 10:53:34 +0530282 desc_p = &desc_table_p[idx];
Tom Rini364d0022023-01-10 11:19:45 -0500283 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CFG_ETH_BUFSIZE];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200284 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530285
286 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100287 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530288 DESC_RXCTRL_RXCHAIN;
289
290 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
291 }
292
293 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200294 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530295
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400296 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200297 flush_dcache_range((ulong)priv->rx_mac_descrtable,
298 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400299 sizeof(priv->rx_mac_descrtable));
300
Vipin KUMAR1f873122010-06-29 10:53:34 +0530301 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400302 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530303}
304
Simon Glasse50c4d12015-04-05 16:07:40 -0600305static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530306{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400307 struct eth_mac_regs *mac_p = priv->mac_regs_p;
308 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400309
310 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
311 (mac_id[3] << 24);
312 macid_hi = mac_id[4] + (mac_id[5] << 8);
313
314 writel(macid_hi, &mac_p->macaddr0hi);
315 writel(macid_lo, &mac_p->macaddr0lo);
316
317 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530318}
319
Simon Glass4afa85e2017-01-11 11:46:08 +0100320static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
321 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530322{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400323 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530324
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400325 if (!phydev->link) {
326 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100327 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400328 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530329
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400330 if (phydev->speed != 1000)
331 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300332 else
333 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530334
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400335 if (phydev->speed == 100)
336 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530337
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400338 if (phydev->duplex)
339 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000340
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400341 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530342
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400343 printf("Speed: %d, %s duplex%s\n", phydev->speed,
344 (phydev->duplex) ? "full" : "half",
345 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100346
347 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530348}
349
Simon Glasse50c4d12015-04-05 16:07:40 -0600350static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530351{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530352 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400353 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530354
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400355 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
356 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530357
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400358 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530359}
360
Simon Glassc154fc02017-01-11 11:46:10 +0100361int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530362{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530363 struct eth_mac_regs *mac_p = priv->mac_regs_p;
364 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400365 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600366 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530367
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400368 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000369
Quentin Schulz7f920dd2018-06-04 12:17:33 +0200370 /*
371 * When a MII PHY is used, we must set the PS bit for the DMA
372 * reset to succeed.
373 */
374 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
375 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
376 else
377 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
378
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400379 start = get_timer(0);
380 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Tom Rini364d0022023-01-10 11:19:45 -0500381 if (get_timer(start) >= CFG_MACRESET_TIMEOUT) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300382 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600383 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300384 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200385
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400386 mdelay(100);
387 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530388
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800389 /*
390 * Soft reset above clears HW address registers.
391 * So we have to set it here once again.
392 */
393 _dw_write_hwaddr(priv, enetaddr);
394
Simon Glasse50c4d12015-04-05 16:07:40 -0600395 rx_descs_init(priv);
396 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530397
Ian Campbell4164b742014-05-08 22:26:35 +0100398 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530399
Sonic Zhangb917b622015-01-29 14:38:50 +0800400#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400401 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
402 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800403#else
404 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
405 &dma_p->opmode);
406#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530407
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400408 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530409
Sonic Zhang962c95c2015-01-29 13:37:31 +0800410#ifdef CONFIG_DW_AXI_BURST_LEN
411 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
412#endif
413
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400414 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600415 ret = phy_startup(priv->phydev);
416 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400417 printf("Could not initialize PHY %s\n",
418 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600419 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530420 }
421
Simon Glass4afa85e2017-01-11 11:46:08 +0100422 ret = dw_adjust_link(priv, mac_p, priv->phydev);
423 if (ret)
424 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530425
Simon Glass3240e942017-01-11 11:46:09 +0100426 return 0;
427}
428
Simon Glassc154fc02017-01-11 11:46:10 +0100429int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100430{
431 struct eth_mac_regs *mac_p = priv->mac_regs_p;
432
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400433 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600434 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530435
Armando Visconti038c9d52012-03-26 00:09:55 +0000436 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530437
438 return 0;
439}
440
Florian Fainelli65f686b2017-12-09 14:59:55 -0800441#define ETH_ZLEN 60
442
Simon Glasse50c4d12015-04-05 16:07:40 -0600443static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530444{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530445 struct eth_dma_regs *dma_p = priv->dma_regs_p;
446 u32 desc_num = priv->tx_currdescnum;
447 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200448 ulong desc_start = (ulong)desc_p;
449 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200450 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200451 ulong data_start = desc_p->dmamac_addr;
452 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100453 /*
454 * Strictly we only need to invalidate the "txrx_status" field
455 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200456 * invalidate only 4 bytes, so we flush the entire descriptor,
457 * which is 16 bytes in total. This is safe because the
458 * individual descriptors in the array are each aligned to
459 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100460 */
Marek Vasut15193042014-09-15 01:05:23 +0200461 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400462
Vipin KUMAR1f873122010-06-29 10:53:34 +0530463 /* Check if the descriptor is owned by CPU */
464 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
465 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600466 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530467 }
468
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200469 memcpy((void *)data_start, packet, length);
Simon Goldschmidt80385de2018-11-17 10:24:42 +0100470 if (length < ETH_ZLEN) {
471 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
472 length = ETH_ZLEN;
473 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530474
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400475 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200476 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400477
Vipin KUMAR1f873122010-06-29 10:53:34 +0530478#if defined(CONFIG_DW_ALTDESCRIPTOR)
479 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100480 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
481 ((length << DESC_TXCTRL_SIZE1SHFT) &
482 DESC_TXCTRL_SIZE1MASK);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530483
484 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
485 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
486#else
Simon Goldschmidte2d0a7c2018-11-17 10:24:41 +0100487 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
488 ((length << DESC_TXCTRL_SIZE1SHFT) &
489 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
490 DESC_TXCTRL_TXFIRST;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530491
492 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
493#endif
494
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400495 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200496 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400497
Vipin KUMAR1f873122010-06-29 10:53:34 +0530498 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500499 if (++desc_num >= CFG_TX_DESCR_NUM)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530500 desc_num = 0;
501
502 priv->tx_currdescnum = desc_num;
503
504 /* Start the transmission */
505 writel(POLL_DATA, &dma_p->txpolldemand);
506
507 return 0;
508}
509
Simon Glass90e627b2015-04-05 16:07:41 -0600510static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530511{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400512 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530513 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600514 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200515 ulong desc_start = (ulong)desc_p;
516 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200517 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200518 ulong data_start = desc_p->dmamac_addr;
519 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530520
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400521 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200522 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400523
524 status = desc_p->txrx_status;
525
Vipin KUMAR1f873122010-06-29 10:53:34 +0530526 /* Check if the owner is the CPU */
527 if (!(status & DESC_RXSTS_OWNBYDMA)) {
528
Marek Vasut4ab539a2015-12-20 03:59:23 +0100529 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530530 DESC_RXSTS_FRMLENSHFT;
531
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400532 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200533 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
534 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200535 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Simon Glass90e627b2015-04-05 16:07:41 -0600536 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400537
Simon Glass90e627b2015-04-05 16:07:41 -0600538 return length;
539}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530540
Simon Glass90e627b2015-04-05 16:07:41 -0600541static int _dw_free_pkt(struct dw_eth_dev *priv)
542{
543 u32 desc_num = priv->rx_currdescnum;
544 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200545 ulong desc_start = (ulong)desc_p;
546 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600547 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530548
Simon Glass90e627b2015-04-05 16:07:41 -0600549 /*
550 * Make the current descriptor valid again and go to
551 * the next one
552 */
553 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400554
Simon Glass90e627b2015-04-05 16:07:41 -0600555 /* Flush only status field - others weren't changed */
556 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530557
Simon Glass90e627b2015-04-05 16:07:41 -0600558 /* Test the wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500559 if (++desc_num >= CFG_RX_DESCR_NUM)
Simon Glass90e627b2015-04-05 16:07:41 -0600560 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530561 priv->rx_currdescnum = desc_num;
562
Simon Glass90e627b2015-04-05 16:07:41 -0600563 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530564}
565
Simon Glasse50c4d12015-04-05 16:07:40 -0600566static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530567{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400568 struct phy_device *phydev;
Neil Armstrong47318c92021-02-24 15:02:39 +0100569 int ret;
570
Tom Rinie4bb4a22022-11-27 10:25:07 -0500571#if IS_ENABLED(CONFIG_DM_MDIO)
Neil Armstrong47318c92021-02-24 15:02:39 +0100572 phydev = dm_eth_phy_connect(dev);
573 if (!phydev)
574 return -ENODEV;
575#else
576 int phy_addr = -1;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530577
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400578#ifdef CONFIG_PHY_ADDR
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200579 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530580#endif
581
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200582 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400583 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600584 return -ENODEV;
Neil Armstrong47318c92021-02-24 15:02:39 +0100585#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530586
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400587 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300588 if (priv->max_speed) {
589 ret = phy_set_supported(phydev, priv->max_speed);
590 if (ret)
591 return ret;
592 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400593 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530594
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400595 priv->phydev = phydev;
596 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530597
Simon Glasse50c4d12015-04-05 16:07:40 -0600598 return 0;
599}
Simon Glass90e627b2015-04-05 16:07:41 -0600600
Simon Glass90e627b2015-04-05 16:07:41 -0600601static int designware_eth_start(struct udevice *dev)
602{
Simon Glassfa20e932020-12-03 16:55:20 -0700603 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100604 struct dw_eth_dev *priv = dev_get_priv(dev);
605 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600606
Simon Glassc154fc02017-01-11 11:46:10 +0100607 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100608 if (ret)
609 return ret;
610 ret = designware_eth_enable(priv);
611 if (ret)
612 return ret;
613
614 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600615}
616
Simon Glassc154fc02017-01-11 11:46:10 +0100617int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600618{
619 struct dw_eth_dev *priv = dev_get_priv(dev);
620
621 return _dw_eth_send(priv, packet, length);
622}
623
Simon Glassc154fc02017-01-11 11:46:10 +0100624int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600625{
626 struct dw_eth_dev *priv = dev_get_priv(dev);
627
628 return _dw_eth_recv(priv, packetp);
629}
630
Simon Glassc154fc02017-01-11 11:46:10 +0100631int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600632{
633 struct dw_eth_dev *priv = dev_get_priv(dev);
634
635 return _dw_free_pkt(priv);
636}
637
Simon Glassc154fc02017-01-11 11:46:10 +0100638void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600639{
640 struct dw_eth_dev *priv = dev_get_priv(dev);
641
642 return _dw_eth_halt(priv);
643}
644
Simon Glassc154fc02017-01-11 11:46:10 +0100645int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600646{
Simon Glassfa20e932020-12-03 16:55:20 -0700647 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600648 struct dw_eth_dev *priv = dev_get_priv(dev);
649
650 return _dw_write_hwaddr(priv, pdata->enetaddr);
651}
652
Bin Menged89bd72015-09-11 03:24:35 -0700653static int designware_eth_bind(struct udevice *dev)
654{
Simon Glass900f0da2021-08-01 18:54:34 -0600655 if (IS_ENABLED(CONFIG_PCI)) {
656 static int num_cards;
657 char name[20];
Bin Menged89bd72015-09-11 03:24:35 -0700658
Simon Glass900f0da2021-08-01 18:54:34 -0600659 /* Create a unique device name for PCI type devices */
660 if (device_is_on_pci_bus(dev)) {
661 sprintf(name, "eth_designware#%u", num_cards++);
662 device_set_name(dev, name);
663 }
Bin Menged89bd72015-09-11 03:24:35 -0700664 }
Bin Menged89bd72015-09-11 03:24:35 -0700665
666 return 0;
667}
668
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100669int designware_eth_probe(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);
Bin Mengdfc90f52015-09-03 05:37:29 -0700673 u32 iobase = pdata->iobase;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200674 ulong ioaddr;
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200675 int ret, err;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800676 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100677#ifdef CONFIG_CLK
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200678 int i, clock_nb;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100679
680 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200681 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
682 0);
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100683 if (clock_nb > 0) {
684 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
685 GFP_KERNEL);
686 if (!priv->clocks)
687 return -ENOMEM;
688
689 for (i = 0; i < clock_nb; i++) {
690 err = clk_get_by_index(dev, i, &priv->clocks[i]);
691 if (err < 0)
692 break;
693
694 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev11e754e2018-02-06 17:12:09 +0300695 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100696 pr_err("failed to enable clock %d\n", i);
697 clk_free(&priv->clocks[i]);
698 goto clk_err;
699 }
700 priv->clock_count++;
701 }
702 } else if (clock_nb != -ENOENT) {
703 pr_err("failed to get clock phandle(%d)\n", clock_nb);
704 return clock_nb;
705 }
706#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600707
Jacob Chen7ceacea2017-03-27 16:54:17 +0800708#if defined(CONFIG_DM_REGULATOR)
709 struct udevice *phy_supply;
710
711 ret = device_get_supply_regulator(dev, "phy-supply",
712 &phy_supply);
713 if (ret) {
714 debug("%s: No phy supply\n", dev->name);
715 } else {
716 ret = regulator_set_enable(phy_supply, true);
717 if (ret) {
718 puts("Error enabling phy supply\n");
719 return ret;
720 }
721 }
722#endif
723
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800724 ret = reset_get_bulk(dev, &reset_bulk);
725 if (ret)
726 dev_warn(dev, "Can't get reset: %d\n", ret);
727 else
728 reset_deassert_bulk(&reset_bulk);
729
Bin Menged89bd72015-09-11 03:24:35 -0700730 /*
731 * If we are on PCI bus, either directly attached to a PCI root port,
Simon Glass71fa5b42020-12-03 16:55:18 -0700732 * or via a PCI bridge, fill in plat before we probe the hardware.
Bin Menged89bd72015-09-11 03:24:35 -0700733 */
Simon Glass900f0da2021-08-01 18:54:34 -0600734 if (IS_ENABLED(CONFIG_PCI) && device_is_on_pci_bus(dev)) {
Bin Menged89bd72015-09-11 03:24:35 -0700735 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
736 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6c3300c2016-02-02 05:58:00 -0800737 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Menged89bd72015-09-11 03:24:35 -0700738
739 pdata->iobase = iobase;
740 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
741 }
Bin Menged89bd72015-09-11 03:24:35 -0700742
Bin Mengdfc90f52015-09-03 05:37:29 -0700743 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200744 ioaddr = iobase;
745 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
746 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600747 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300748 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600749
Neil Armstrong47318c92021-02-24 15:02:39 +0100750#if IS_ENABLED(CONFIG_DM_MDIO)
751 ret = dw_dm_mdio_init(dev->name, dev);
752#else
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200753 ret = dw_mdio_init(dev->name, dev);
Neil Armstrong47318c92021-02-24 15:02:39 +0100754#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200755 if (ret) {
756 err = ret;
757 goto mdio_err;
758 }
Simon Glass90e627b2015-04-05 16:07:41 -0600759 priv->bus = miiphy_get_dev_by_name(dev->name);
760
761 ret = dw_phy_init(priv, dev);
762 debug("%s, ret=%d\n", __func__, ret);
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200763 if (!ret)
764 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600765
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200766 /* continue here for cleanup if no PHY found */
767 err = ret;
768 mdio_unregister(priv->bus);
769 mdio_free(priv->bus);
770mdio_err:
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100771
772#ifdef CONFIG_CLK
773clk_err:
774 ret = clk_release_all(priv->clocks, priv->clock_count);
775 if (ret)
776 pr_err("failed to disable all clocks\n");
777
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100778#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200779 return err;
Simon Glass90e627b2015-04-05 16:07:41 -0600780}
781
Bin Mengf0f02772015-10-07 21:32:38 -0700782static int designware_eth_remove(struct udevice *dev)
783{
784 struct dw_eth_dev *priv = dev_get_priv(dev);
785
786 free(priv->phydev);
787 mdio_unregister(priv->bus);
788 mdio_free(priv->bus);
789
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100790#ifdef CONFIG_CLK
791 return clk_release_all(priv->clocks, priv->clock_count);
792#else
Bin Mengf0f02772015-10-07 21:32:38 -0700793 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100794#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700795}
796
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100797const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600798 .start = designware_eth_start,
799 .send = designware_eth_send,
800 .recv = designware_eth_recv,
801 .free_pkt = designware_eth_free_pkt,
802 .stop = designware_eth_stop,
803 .write_hwaddr = designware_eth_write_hwaddr,
804};
805
Simon Glassaad29ae2020-12-03 16:55:21 -0700806int designware_eth_of_to_plat(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600807{
Simon Glassfa20e932020-12-03 16:55:20 -0700808 struct dw_eth_pdata *dw_pdata = dev_get_plat(dev);
Simon Glassfa4689a2019-12-06 21:41:35 -0700809#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100810 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300811#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100812 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glassfa4689a2019-12-06 21:41:35 -0700813#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100814 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300815#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100816 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600817
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200818 pdata->iobase = dev_read_addr(dev);
Marek BehĂșnbc194772022-04-07 00:33:01 +0200819 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200820 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Simon Glass90e627b2015-04-05 16:07:41 -0600821 return -EINVAL;
Simon Glass90e627b2015-04-05 16:07:41 -0600822
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200823 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300824
Simon Glassfa4689a2019-12-06 21:41:35 -0700825#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich150005b2017-06-07 18:46:01 +0200826 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100827 reset_flags |= GPIOD_ACTIVE_LOW;
828
829 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
830 &priv->reset_gpio, reset_flags);
831 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200832 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
833 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100834 } else if (ret == -ENOENT) {
835 ret = 0;
836 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300837#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100838
839 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600840}
841
842static const struct udevice_id designware_eth_ids[] = {
843 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200844 { .compatible = "amlogic,meson6-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100845 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev5738e942019-10-07 19:10:50 +0300846 { .compatible = "snps,arc-dwmac-3.70a" },
Simon Glass90e627b2015-04-05 16:07:41 -0600847 { }
848};
849
Marek Vasut7e7e6172015-07-25 18:42:34 +0200850U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600851 .name = "eth_designware",
852 .id = UCLASS_ETH,
853 .of_match = designware_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700854 .of_to_plat = designware_eth_of_to_plat,
Bin Menged89bd72015-09-11 03:24:35 -0700855 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600856 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700857 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600858 .ops = &designware_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700859 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -0700860 .plat_auto = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600861 .flags = DM_FLAG_ALLOC_PRIV_DMA,
862};
Bin Menged89bd72015-09-11 03:24:35 -0700863
864static struct pci_device_id supported[] = {
865 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
866 { }
867};
868
869U_BOOT_PCI_DEVICE(eth_designware, supported);