blob: baac277a84dba0d722a95036900492095b891675 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR5b1b1882010-06-29 10:53:34 +05302/*
3 * (C) Copyright 2010
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMAR5b1b1882010-06-29 10:53:34 +05305 */
6
7/*
Simon Glass64dcd252015-04-05 16:07:40 -06008 * Designware ethernet IP driver for U-Boot
Vipin KUMAR5b1b1882010-06-29 10:53:34 +05309 */
10
11#include <common.h>
Patrice Chotardba1f9662017-11-29 09:06:11 +010012#include <clk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass75577ba2015-04-05 16:07:41 -060014#include <dm.h>
Simon Glass64dcd252015-04-05 16:07:40 -060015#include <errno.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053016#include <miiphy.h>
17#include <malloc.h>
Bin Meng8b7ee662015-09-11 03:24:35 -070018#include <pci.h>
Ley Foon Tan495c70f2018-06-14 18:45:23 +080019#include <reset.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070021#include <dm/devres.h>
Stefan Roeseef760252012-05-07 12:04:25 +020022#include <linux/compiler.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053023#include <linux/err.h>
Florian Fainelli7a9ca9d2017-12-09 14:59:55 -080024#include <linux/kernel.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053025#include <asm/io.h>
Jacob Chen6ec922f2017-03-27 16:54:17 +080026#include <power/regulator.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053027#include "designware.h"
28
Alexey Brodkin92a190a2014-01-22 20:54:06 +040029static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
30{
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010031#ifdef CONFIG_DM_ETH
32 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
33 struct eth_mac_regs *mac_p = priv->mac_regs_p;
34#else
Alexey Brodkin92a190a2014-01-22 20:54:06 +040035 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010036#endif
Alexey Brodkin92a190a2014-01-22 20:54:06 +040037 ulong start;
38 u16 miiaddr;
39 int timeout = CONFIG_MDIO_TIMEOUT;
40
41 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
42 ((reg << MIIREGSHIFT) & MII_REGMSK);
43
44 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
45
46 start = get_timer(0);
47 while (get_timer(start) < timeout) {
48 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
49 return readl(&mac_p->miidata);
50 udelay(10);
51 };
52
Simon Glass64dcd252015-04-05 16:07:40 -060053 return -ETIMEDOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040054}
55
56static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
57 u16 val)
58{
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010059#ifdef CONFIG_DM_ETH
60 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
61 struct eth_mac_regs *mac_p = priv->mac_regs_p;
62#else
Alexey Brodkin92a190a2014-01-22 20:54:06 +040063 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010064#endif
Alexey Brodkin92a190a2014-01-22 20:54:06 +040065 ulong start;
66 u16 miiaddr;
Simon Glass64dcd252015-04-05 16:07:40 -060067 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040068
69 writel(val, &mac_p->miidata);
70 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
71 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
72
73 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
74
75 start = get_timer(0);
76 while (get_timer(start) < timeout) {
77 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
78 ret = 0;
79 break;
80 }
81 udelay(10);
82 };
83
84 return ret;
85}
86
Simon Glassbcee8d62019-12-06 21:41:35 -070087#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010088static int dw_mdio_reset(struct mii_dev *bus)
89{
90 struct udevice *dev = bus->priv;
91 struct dw_eth_dev *priv = dev_get_priv(dev);
92 struct dw_eth_pdata *pdata = dev_get_platdata(dev);
93 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}
119#endif
120
121static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400122{
123 struct mii_dev *bus = mdio_alloc();
124
125 if (!bus) {
126 printf("Failed to allocate MDIO bus\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600127 return -ENOMEM;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400128 }
129
130 bus->read = dw_mdio_read;
131 bus->write = dw_mdio_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000132 snprintf(bus->name, sizeof(bus->name), "%s", name);
Simon Glassbcee8d62019-12-06 21:41:35 -0700133#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100134 bus->reset = dw_mdio_reset;
135#endif
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400136
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100137 bus->priv = priv;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400138
139 return mdio_register(bus);
140}
Vipin Kumar13edd172012-03-26 00:09:56 +0000141
Simon Glass64dcd252015-04-05 16:07:40 -0600142static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530143{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530144 struct eth_dma_regs *dma_p = priv->dma_regs_p;
145 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
146 char *txbuffs = &priv->txbuffs[0];
147 struct dmamacdescr *desc_p;
148 u32 idx;
149
150 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
151 desc_p = &desc_table_p[idx];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200152 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
153 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530154
155#if defined(CONFIG_DW_ALTDESCRIPTOR)
156 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut2b261092015-12-20 03:59:23 +0100157 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
158 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530159 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
160
161 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
162 desc_p->dmamac_cntl = 0;
163 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
164#else
165 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
166 desc_p->txrx_status = 0;
167#endif
168 }
169
170 /* Correcting the last pointer of the chain */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200171 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530172
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400173 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200174 flush_dcache_range((ulong)priv->tx_mac_descrtable,
175 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400176 sizeof(priv->tx_mac_descrtable));
177
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530178 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400179 priv->tx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530180}
181
Simon Glass64dcd252015-04-05 16:07:40 -0600182static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530183{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530184 struct eth_dma_regs *dma_p = priv->dma_regs_p;
185 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
186 char *rxbuffs = &priv->rxbuffs[0];
187 struct dmamacdescr *desc_p;
188 u32 idx;
189
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400190 /* Before passing buffers to GMAC we need to make sure zeros
191 * written there right after "priv" structure allocation were
192 * flushed into RAM.
193 * Otherwise there's a chance to get some of them flushed in RAM when
194 * GMAC is already pushing data to RAM via DMA. This way incoming from
195 * GMAC data will be corrupted. */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200196 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400197
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530198 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
199 desc_p = &desc_table_p[idx];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200200 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
201 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530202
203 desc_p->dmamac_cntl =
Marek Vasut2b261092015-12-20 03:59:23 +0100204 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530205 DESC_RXCTRL_RXCHAIN;
206
207 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
208 }
209
210 /* Correcting the last pointer of the chain */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200211 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530212
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400213 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200214 flush_dcache_range((ulong)priv->rx_mac_descrtable,
215 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400216 sizeof(priv->rx_mac_descrtable));
217
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530218 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400219 priv->rx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530220}
221
Simon Glass64dcd252015-04-05 16:07:40 -0600222static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530223{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530224 struct eth_mac_regs *mac_p = priv->mac_regs_p;
225 u32 macid_lo, macid_hi;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530226
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400227 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
228 (mac_id[3] << 24);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530229 macid_hi = mac_id[4] + (mac_id[5] << 8);
230
231 writel(macid_hi, &mac_p->macaddr0hi);
232 writel(macid_lo, &mac_p->macaddr0lo);
233
234 return 0;
235}
236
Simon Glass0ea38db2017-01-11 11:46:08 +0100237static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
238 struct phy_device *phydev)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400239{
240 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
241
242 if (!phydev->link) {
243 printf("%s: No link.\n", phydev->dev->name);
Simon Glass0ea38db2017-01-11 11:46:08 +0100244 return 0;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400245 }
246
247 if (phydev->speed != 1000)
248 conf |= MII_PORTSELECT;
Alexey Brodkinb884c3f2016-01-13 16:59:36 +0300249 else
250 conf &= ~MII_PORTSELECT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400251
252 if (phydev->speed == 100)
253 conf |= FES_100;
254
255 if (phydev->duplex)
256 conf |= FULLDPLXMODE;
257
258 writel(conf, &mac_p->conf);
259
260 printf("Speed: %d, %s duplex%s\n", phydev->speed,
261 (phydev->duplex) ? "full" : "half",
262 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass0ea38db2017-01-11 11:46:08 +0100263
264 return 0;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400265}
266
Simon Glass64dcd252015-04-05 16:07:40 -0600267static void _dw_eth_halt(struct dw_eth_dev *priv)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400268{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400269 struct eth_mac_regs *mac_p = priv->mac_regs_p;
270 struct eth_dma_regs *dma_p = priv->dma_regs_p;
271
272 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
273 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
274
275 phy_shutdown(priv->phydev);
276}
277
Simon Glasse72ced22017-01-11 11:46:10 +0100278int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530279{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530280 struct eth_mac_regs *mac_p = priv->mac_regs_p;
281 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400282 unsigned int start;
Simon Glass64dcd252015-04-05 16:07:40 -0600283 int ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530284
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400285 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumar13edd172012-03-26 00:09:56 +0000286
Quentin Schulzc6122192018-06-04 12:17:33 +0200287 /*
288 * When a MII PHY is used, we must set the PS bit for the DMA
289 * reset to succeed.
290 */
291 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
292 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
293 else
294 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
295
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400296 start = get_timer(0);
297 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin875143f2015-01-13 17:10:24 +0300298 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
299 printf("DMA reset timeout\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600300 return -ETIMEDOUT;
Alexey Brodkin875143f2015-01-13 17:10:24 +0300301 }
Stefan Roeseef760252012-05-07 12:04:25 +0200302
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400303 mdelay(100);
304 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530305
Bin Mengf3edfd32015-06-15 18:40:19 +0800306 /*
307 * Soft reset above clears HW address registers.
308 * So we have to set it here once again.
309 */
310 _dw_write_hwaddr(priv, enetaddr);
311
Simon Glass64dcd252015-04-05 16:07:40 -0600312 rx_descs_init(priv);
313 tx_descs_init(priv);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530314
Ian Campbell49692c52014-05-08 22:26:35 +0100315 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530316
Sonic Zhangd2279222015-01-29 14:38:50 +0800317#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400318 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
319 &dma_p->opmode);
Sonic Zhangd2279222015-01-29 14:38:50 +0800320#else
321 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
322 &dma_p->opmode);
323#endif
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530324
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400325 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530326
Sonic Zhang2ddaf132015-01-29 13:37:31 +0800327#ifdef CONFIG_DW_AXI_BURST_LEN
328 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
329#endif
330
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400331 /* Start up the PHY */
Simon Glass64dcd252015-04-05 16:07:40 -0600332 ret = phy_startup(priv->phydev);
333 if (ret) {
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400334 printf("Could not initialize PHY %s\n",
335 priv->phydev->dev->name);
Simon Glass64dcd252015-04-05 16:07:40 -0600336 return ret;
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530337 }
338
Simon Glass0ea38db2017-01-11 11:46:08 +0100339 ret = dw_adjust_link(priv, mac_p, priv->phydev);
340 if (ret)
341 return ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530342
Simon Glassf63f28e2017-01-11 11:46:09 +0100343 return 0;
344}
345
Simon Glasse72ced22017-01-11 11:46:10 +0100346int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glassf63f28e2017-01-11 11:46:09 +0100347{
348 struct eth_mac_regs *mac_p = priv->mac_regs_p;
349
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400350 if (!priv->phydev->link)
Simon Glass64dcd252015-04-05 16:07:40 -0600351 return -EIO;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530352
Armando Viscontiaa510052012-03-26 00:09:55 +0000353 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530354
355 return 0;
356}
357
Florian Fainelli7a9ca9d2017-12-09 14:59:55 -0800358#define ETH_ZLEN 60
359
Simon Glass64dcd252015-04-05 16:07:40 -0600360static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530361{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530362 struct eth_dma_regs *dma_p = priv->dma_regs_p;
363 u32 desc_num = priv->tx_currdescnum;
364 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200365 ulong desc_start = (ulong)desc_p;
366 ulong desc_end = desc_start +
Marek Vasut96cec172014-09-15 01:05:23 +0200367 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200368 ulong data_start = desc_p->dmamac_addr;
369 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell964ea7c2014-05-08 22:26:33 +0100370 /*
371 * Strictly we only need to invalidate the "txrx_status" field
372 * for the following check, but on some platforms we cannot
Marek Vasut96cec172014-09-15 01:05:23 +0200373 * invalidate only 4 bytes, so we flush the entire descriptor,
374 * which is 16 bytes in total. This is safe because the
375 * individual descriptors in the array are each aligned to
376 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell964ea7c2014-05-08 22:26:33 +0100377 */
Marek Vasut96cec172014-09-15 01:05:23 +0200378 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400379
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530380 /* Check if the descriptor is owned by CPU */
381 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
382 printf("CPU not owner of tx frame\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600383 return -EPERM;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530384 }
385
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200386 memcpy((void *)data_start, packet, length);
Simon Goldschmidt7efb75b2018-11-17 10:24:42 +0100387 if (length < ETH_ZLEN) {
388 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
389 length = ETH_ZLEN;
390 }
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530391
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400392 /* Flush data to be sent */
Marek Vasut96cec172014-09-15 01:05:23 +0200393 flush_dcache_range(data_start, data_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400394
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530395#if defined(CONFIG_DW_ALTDESCRIPTOR)
396 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidtae8ac8d2018-11-17 10:24:41 +0100397 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
398 ((length << DESC_TXCTRL_SIZE1SHFT) &
399 DESC_TXCTRL_SIZE1MASK);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530400
401 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
402 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
403#else
Simon Goldschmidtae8ac8d2018-11-17 10:24:41 +0100404 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
405 ((length << DESC_TXCTRL_SIZE1SHFT) &
406 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
407 DESC_TXCTRL_TXFIRST;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530408
409 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
410#endif
411
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400412 /* Flush modified buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200413 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400414
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530415 /* Test the wrap-around condition. */
416 if (++desc_num >= CONFIG_TX_DESCR_NUM)
417 desc_num = 0;
418
419 priv->tx_currdescnum = desc_num;
420
421 /* Start the transmission */
422 writel(POLL_DATA, &dma_p->txpolldemand);
423
424 return 0;
425}
426
Simon Glass75577ba2015-04-05 16:07:41 -0600427static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530428{
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400429 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530430 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass75577ba2015-04-05 16:07:41 -0600431 int length = -EAGAIN;
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200432 ulong desc_start = (ulong)desc_p;
433 ulong desc_end = desc_start +
Marek Vasut96cec172014-09-15 01:05:23 +0200434 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200435 ulong data_start = desc_p->dmamac_addr;
436 ulong data_end;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530437
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400438 /* Invalidate entire buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200439 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400440
441 status = desc_p->txrx_status;
442
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530443 /* Check if the owner is the CPU */
444 if (!(status & DESC_RXSTS_OWNBYDMA)) {
445
Marek Vasut2b261092015-12-20 03:59:23 +0100446 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530447 DESC_RXSTS_FRMLENSHFT;
448
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400449 /* Invalidate received data */
Marek Vasut96cec172014-09-15 01:05:23 +0200450 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
451 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200452 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530453 }
454
Simon Glass75577ba2015-04-05 16:07:41 -0600455 return length;
456}
457
458static int _dw_free_pkt(struct dw_eth_dev *priv)
459{
460 u32 desc_num = priv->rx_currdescnum;
461 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200462 ulong desc_start = (ulong)desc_p;
463 ulong desc_end = desc_start +
Simon Glass75577ba2015-04-05 16:07:41 -0600464 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
465
466 /*
467 * Make the current descriptor valid again and go to
468 * the next one
469 */
470 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
471
472 /* Flush only status field - others weren't changed */
473 flush_dcache_range(desc_start, desc_end);
474
475 /* Test the wrap-around condition. */
476 if (++desc_num >= CONFIG_RX_DESCR_NUM)
477 desc_num = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530478 priv->rx_currdescnum = desc_num;
479
Simon Glass75577ba2015-04-05 16:07:41 -0600480 return 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530481}
482
Simon Glass64dcd252015-04-05 16:07:40 -0600483static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530484{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400485 struct phy_device *phydev;
Simon Goldschmidt5dce9df2019-07-15 21:53:05 +0200486 int phy_addr = -1, ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530487
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400488#ifdef CONFIG_PHY_ADDR
Simon Goldschmidt5dce9df2019-07-15 21:53:05 +0200489 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530490#endif
491
Simon Goldschmidt5dce9df2019-07-15 21:53:05 +0200492 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400493 if (!phydev)
Simon Glass64dcd252015-04-05 16:07:40 -0600494 return -ENODEV;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530495
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400496 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300497 if (priv->max_speed) {
498 ret = phy_set_supported(phydev, priv->max_speed);
499 if (ret)
500 return ret;
501 }
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400502 phydev->advertising = phydev->supported;
503
504 priv->phydev = phydev;
505 phy_config(phydev);
506
Simon Glass64dcd252015-04-05 16:07:40 -0600507 return 0;
508}
509
Simon Glass75577ba2015-04-05 16:07:41 -0600510#ifndef CONFIG_DM_ETH
Simon Glass64dcd252015-04-05 16:07:40 -0600511static int dw_eth_init(struct eth_device *dev, bd_t *bis)
512{
Simon Glassf63f28e2017-01-11 11:46:09 +0100513 int ret;
514
Simon Glasse72ced22017-01-11 11:46:10 +0100515 ret = designware_eth_init(dev->priv, dev->enetaddr);
Simon Glassf63f28e2017-01-11 11:46:09 +0100516 if (!ret)
517 ret = designware_eth_enable(dev->priv);
518
519 return ret;
Simon Glass64dcd252015-04-05 16:07:40 -0600520}
521
522static int dw_eth_send(struct eth_device *dev, void *packet, int length)
523{
524 return _dw_eth_send(dev->priv, packet, length);
525}
526
527static int dw_eth_recv(struct eth_device *dev)
528{
Simon Glass75577ba2015-04-05 16:07:41 -0600529 uchar *packet;
530 int length;
531
532 length = _dw_eth_recv(dev->priv, &packet);
533 if (length == -EAGAIN)
534 return 0;
535 net_process_received_packet(packet, length);
536
537 _dw_free_pkt(dev->priv);
538
539 return 0;
Simon Glass64dcd252015-04-05 16:07:40 -0600540}
541
542static void dw_eth_halt(struct eth_device *dev)
543{
544 return _dw_eth_halt(dev->priv);
545}
546
547static int dw_write_hwaddr(struct eth_device *dev)
548{
549 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530550}
551
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400552int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530553{
554 struct eth_device *dev;
555 struct dw_eth_dev *priv;
556
557 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
558 if (!dev)
559 return -ENOMEM;
560
561 /*
562 * Since the priv structure contains the descriptors which need a strict
563 * buswidth alignment, memalign is used to allocate memory
564 */
Ian Campbell1c848a22014-05-08 22:26:32 +0100565 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
566 sizeof(struct dw_eth_dev));
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530567 if (!priv) {
568 free(dev);
569 return -ENOMEM;
570 }
571
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200572 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
573 printf("designware: buffers are outside DMA memory\n");
574 return -EINVAL;
575 }
576
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530577 memset(dev, 0, sizeof(struct eth_device));
578 memset(priv, 0, sizeof(struct dw_eth_dev));
579
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400580 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530581 dev->iobase = (int)base_addr;
582 dev->priv = priv;
583
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530584 priv->dev = dev;
585 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
586 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
587 DW_DMA_BASE_OFFSET);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530588
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530589 dev->init = dw_eth_init;
590 dev->send = dw_eth_send;
591 dev->recv = dw_eth_recv;
592 dev->halt = dw_eth_halt;
593 dev->write_hwaddr = dw_write_hwaddr;
594
595 eth_register(dev);
596
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400597 priv->interface = interface;
598
599 dw_mdio_init(dev->name, priv->mac_regs_p);
600 priv->bus = miiphy_get_dev_by_name(dev->name);
601
Simon Glass64dcd252015-04-05 16:07:40 -0600602 return dw_phy_init(priv, dev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530603}
Simon Glass75577ba2015-04-05 16:07:41 -0600604#endif
605
606#ifdef CONFIG_DM_ETH
607static int designware_eth_start(struct udevice *dev)
608{
609 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glassf63f28e2017-01-11 11:46:09 +0100610 struct dw_eth_dev *priv = dev_get_priv(dev);
611 int ret;
Simon Glass75577ba2015-04-05 16:07:41 -0600612
Simon Glasse72ced22017-01-11 11:46:10 +0100613 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glassf63f28e2017-01-11 11:46:09 +0100614 if (ret)
615 return ret;
616 ret = designware_eth_enable(priv);
617 if (ret)
618 return ret;
619
620 return 0;
Simon Glass75577ba2015-04-05 16:07:41 -0600621}
622
Simon Glasse72ced22017-01-11 11:46:10 +0100623int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass75577ba2015-04-05 16:07:41 -0600624{
625 struct dw_eth_dev *priv = dev_get_priv(dev);
626
627 return _dw_eth_send(priv, packet, length);
628}
629
Simon Glasse72ced22017-01-11 11:46:10 +0100630int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass75577ba2015-04-05 16:07:41 -0600631{
632 struct dw_eth_dev *priv = dev_get_priv(dev);
633
634 return _dw_eth_recv(priv, packetp);
635}
636
Simon Glasse72ced22017-01-11 11:46:10 +0100637int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass75577ba2015-04-05 16:07:41 -0600638{
639 struct dw_eth_dev *priv = dev_get_priv(dev);
640
641 return _dw_free_pkt(priv);
642}
643
Simon Glasse72ced22017-01-11 11:46:10 +0100644void designware_eth_stop(struct udevice *dev)
Simon Glass75577ba2015-04-05 16:07:41 -0600645{
646 struct dw_eth_dev *priv = dev_get_priv(dev);
647
648 return _dw_eth_halt(priv);
649}
650
Simon Glasse72ced22017-01-11 11:46:10 +0100651int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass75577ba2015-04-05 16:07:41 -0600652{
653 struct eth_pdata *pdata = dev_get_platdata(dev);
654 struct dw_eth_dev *priv = dev_get_priv(dev);
655
656 return _dw_write_hwaddr(priv, pdata->enetaddr);
657}
658
Bin Meng8b7ee662015-09-11 03:24:35 -0700659static int designware_eth_bind(struct udevice *dev)
660{
661#ifdef CONFIG_DM_PCI
662 static int num_cards;
663 char name[20];
664
665 /* Create a unique device name for PCI type devices */
666 if (device_is_on_pci_bus(dev)) {
667 sprintf(name, "eth_designware#%u", num_cards++);
668 device_set_name(dev, name);
669 }
670#endif
671
672 return 0;
673}
674
Sjoerd Simonsb9e08d02017-01-11 11:46:07 +0100675int designware_eth_probe(struct udevice *dev)
Simon Glass75577ba2015-04-05 16:07:41 -0600676{
677 struct eth_pdata *pdata = dev_get_platdata(dev);
678 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengf0dc73c2015-09-03 05:37:29 -0700679 u32 iobase = pdata->iobase;
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200680 ulong ioaddr;
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200681 int ret, err;
Ley Foon Tan495c70f2018-06-14 18:45:23 +0800682 struct reset_ctl_bulk reset_bulk;
Patrice Chotardba1f9662017-11-29 09:06:11 +0100683#ifdef CONFIG_CLK
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200684 int i, clock_nb;
Patrice Chotardba1f9662017-11-29 09:06:11 +0100685
686 priv->clock_count = 0;
687 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
688 if (clock_nb > 0) {
689 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
690 GFP_KERNEL);
691 if (!priv->clocks)
692 return -ENOMEM;
693
694 for (i = 0; i < clock_nb; i++) {
695 err = clk_get_by_index(dev, i, &priv->clocks[i]);
696 if (err < 0)
697 break;
698
699 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev1693a572018-02-06 17:12:09 +0300700 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardba1f9662017-11-29 09:06:11 +0100701 pr_err("failed to enable clock %d\n", i);
702 clk_free(&priv->clocks[i]);
703 goto clk_err;
704 }
705 priv->clock_count++;
706 }
707 } else if (clock_nb != -ENOENT) {
708 pr_err("failed to get clock phandle(%d)\n", clock_nb);
709 return clock_nb;
710 }
711#endif
Simon Glass75577ba2015-04-05 16:07:41 -0600712
Jacob Chen6ec922f2017-03-27 16:54:17 +0800713#if defined(CONFIG_DM_REGULATOR)
714 struct udevice *phy_supply;
715
716 ret = device_get_supply_regulator(dev, "phy-supply",
717 &phy_supply);
718 if (ret) {
719 debug("%s: No phy supply\n", dev->name);
720 } else {
721 ret = regulator_set_enable(phy_supply, true);
722 if (ret) {
723 puts("Error enabling phy supply\n");
724 return ret;
725 }
726 }
727#endif
728
Ley Foon Tan495c70f2018-06-14 18:45:23 +0800729 ret = reset_get_bulk(dev, &reset_bulk);
730 if (ret)
731 dev_warn(dev, "Can't get reset: %d\n", ret);
732 else
733 reset_deassert_bulk(&reset_bulk);
734
Bin Meng8b7ee662015-09-11 03:24:35 -0700735#ifdef CONFIG_DM_PCI
736 /*
737 * If we are on PCI bus, either directly attached to a PCI root port,
738 * or via a PCI bridge, fill in platdata before we probe the hardware.
739 */
740 if (device_is_on_pci_bus(dev)) {
Bin Meng8b7ee662015-09-11 03:24:35 -0700741 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
742 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6758a6c2016-02-02 05:58:00 -0800743 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Meng8b7ee662015-09-11 03:24:35 -0700744
745 pdata->iobase = iobase;
746 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
747 }
748#endif
749
Bin Mengf0dc73c2015-09-03 05:37:29 -0700750 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200751 ioaddr = iobase;
752 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
753 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass75577ba2015-04-05 16:07:41 -0600754 priv->interface = pdata->phy_interface;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300755 priv->max_speed = pdata->max_speed;
Simon Glass75577ba2015-04-05 16:07:41 -0600756
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200757 ret = dw_mdio_init(dev->name, dev);
758 if (ret) {
759 err = ret;
760 goto mdio_err;
761 }
Simon Glass75577ba2015-04-05 16:07:41 -0600762 priv->bus = miiphy_get_dev_by_name(dev->name);
763
764 ret = dw_phy_init(priv, dev);
765 debug("%s, ret=%d\n", __func__, ret);
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200766 if (!ret)
767 return 0;
Simon Glass75577ba2015-04-05 16:07:41 -0600768
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200769 /* continue here for cleanup if no PHY found */
770 err = ret;
771 mdio_unregister(priv->bus);
772 mdio_free(priv->bus);
773mdio_err:
Patrice Chotardba1f9662017-11-29 09:06:11 +0100774
775#ifdef CONFIG_CLK
776clk_err:
777 ret = clk_release_all(priv->clocks, priv->clock_count);
778 if (ret)
779 pr_err("failed to disable all clocks\n");
780
Patrice Chotardba1f9662017-11-29 09:06:11 +0100781#endif
Simon Goldschmidt4ee587e2019-07-12 21:07:03 +0200782 return err;
Simon Glass75577ba2015-04-05 16:07:41 -0600783}
784
Bin Meng5d2459f2015-10-07 21:32:38 -0700785static int designware_eth_remove(struct udevice *dev)
786{
787 struct dw_eth_dev *priv = dev_get_priv(dev);
788
789 free(priv->phydev);
790 mdio_unregister(priv->bus);
791 mdio_free(priv->bus);
792
Patrice Chotardba1f9662017-11-29 09:06:11 +0100793#ifdef CONFIG_CLK
794 return clk_release_all(priv->clocks, priv->clock_count);
795#else
Bin Meng5d2459f2015-10-07 21:32:38 -0700796 return 0;
Patrice Chotardba1f9662017-11-29 09:06:11 +0100797#endif
Bin Meng5d2459f2015-10-07 21:32:38 -0700798}
799
Sjoerd Simonsb9e08d02017-01-11 11:46:07 +0100800const struct eth_ops designware_eth_ops = {
Simon Glass75577ba2015-04-05 16:07:41 -0600801 .start = designware_eth_start,
802 .send = designware_eth_send,
803 .recv = designware_eth_recv,
804 .free_pkt = designware_eth_free_pkt,
805 .stop = designware_eth_stop,
806 .write_hwaddr = designware_eth_write_hwaddr,
807};
808
Sjoerd Simonsb9e08d02017-01-11 11:46:07 +0100809int designware_eth_ofdata_to_platdata(struct udevice *dev)
Simon Glass75577ba2015-04-05 16:07:41 -0600810{
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100811 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Simon Glassbcee8d62019-12-06 21:41:35 -0700812#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100813 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300814#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100815 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass75577ba2015-04-05 16:07:41 -0600816 const char *phy_mode;
Simon Glassbcee8d62019-12-06 21:41:35 -0700817#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100818 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300819#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100820 int ret = 0;
Simon Glass75577ba2015-04-05 16:07:41 -0600821
Philipp Tomsich15050f12017-09-11 22:04:13 +0200822 pdata->iobase = dev_read_addr(dev);
Simon Glass75577ba2015-04-05 16:07:41 -0600823 pdata->phy_interface = -1;
Philipp Tomsich15050f12017-09-11 22:04:13 +0200824 phy_mode = dev_read_string(dev, "phy-mode");
Simon Glass75577ba2015-04-05 16:07:41 -0600825 if (phy_mode)
826 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
827 if (pdata->phy_interface == -1) {
828 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
829 return -EINVAL;
830 }
831
Philipp Tomsich15050f12017-09-11 22:04:13 +0200832 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300833
Simon Glassbcee8d62019-12-06 21:41:35 -0700834#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich7ad326a2017-06-07 18:46:01 +0200835 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100836 reset_flags |= GPIOD_ACTIVE_LOW;
837
838 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
839 &priv->reset_gpio, reset_flags);
840 if (ret == 0) {
Philipp Tomsich7ad326a2017-06-07 18:46:01 +0200841 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
842 dw_pdata->reset_delays, 3);
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100843 } else if (ret == -ENOENT) {
844 ret = 0;
845 }
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300846#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100847
848 return ret;
Simon Glass75577ba2015-04-05 16:07:41 -0600849}
850
851static const struct udevice_id designware_eth_ids[] = {
852 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvanicfe25562016-08-16 11:49:50 +0200853 { .compatible = "amlogic,meson6-dwmac" },
Heiner Kallweit655217d2017-01-27 21:25:59 +0100854 { .compatible = "amlogic,meson-gx-dwmac" },
Neil Armstrongec353ad2018-09-10 16:44:14 +0200855 { .compatible = "amlogic,meson-gxbb-dwmac" },
Neil Armstrong71a38a82018-11-08 17:16:11 +0100856 { .compatible = "amlogic,meson-axg-dwmac" },
Michael Kurzb20b70f2017-01-22 16:04:27 +0100857 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev2a723232019-10-07 19:10:50 +0300858 { .compatible = "snps,arc-dwmac-3.70a" },
Simon Glass75577ba2015-04-05 16:07:41 -0600859 { }
860};
861
Marek Vasut9f76f102015-07-25 18:42:34 +0200862U_BOOT_DRIVER(eth_designware) = {
Simon Glass75577ba2015-04-05 16:07:41 -0600863 .name = "eth_designware",
864 .id = UCLASS_ETH,
865 .of_match = designware_eth_ids,
866 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Meng8b7ee662015-09-11 03:24:35 -0700867 .bind = designware_eth_bind,
Simon Glass75577ba2015-04-05 16:07:41 -0600868 .probe = designware_eth_probe,
Bin Meng5d2459f2015-10-07 21:32:38 -0700869 .remove = designware_eth_remove,
Simon Glass75577ba2015-04-05 16:07:41 -0600870 .ops = &designware_eth_ops,
871 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100872 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass75577ba2015-04-05 16:07:41 -0600873 .flags = DM_FLAG_ALLOC_PRIV_DMA,
874};
Bin Meng8b7ee662015-09-11 03:24:35 -0700875
876static struct pci_device_id supported[] = {
877 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
878 { }
879};
880
881U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass75577ba2015-04-05 16:07:41 -0600882#endif