blob: baac277a84dba0d722a95036900492095b891675 [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
4 * Vipin Kumar, ST Micoelectronics, 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>
Vipin KUMAR1f873122010-06-29 10:53:34 +053016#include <miiphy.h>
17#include <malloc.h>
Bin Menged89bd72015-09-11 03:24:35 -070018#include <pci.h>
Ley Foon Tan27d5c002018-06-14 18:45:23 +080019#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Stefan Roesed27e86c2012-05-07 12:04:25 +020022#include <linux/compiler.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053023#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080024#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053025#include <asm/io.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080026#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053027#include "designware.h"
28
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040029static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
30{
Sjoerd Simons6eb44622016-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 Brodkin9a0b1302014-01-22 20:54:06 +040035 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010036#endif
Alexey Brodkin9a0b1302014-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 Glasse50c4d12015-04-05 16:07:40 -060053 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-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 Simons6eb44622016-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 Brodkin9a0b1302014-01-22 20:54:06 +040063 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010064#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040065 ulong start;
66 u16 miiaddr;
Simon Glasse50c4d12015-04-05 16:07:40 -060067 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040068
69 writel(val, &mac_p->miidata);
70 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
71 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
72
73 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
74
75 start = get_timer(0);
76 while (get_timer(start) < timeout) {
77 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
78 ret = 0;
79 break;
80 }
81 udelay(10);
82 };
83
84 return ret;
85}
86
Simon Glassfa4689a2019-12-06 21:41:35 -070087#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-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 Brodkin9a0b1302014-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 Glasse50c4d12015-04-05 16:07:40 -0600127 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400128 }
129
130 bus->read = dw_mdio_read;
131 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000132 snprintf(bus->name, sizeof(bus->name), "%s", name);
Simon Glassfa4689a2019-12-06 21:41:35 -0700133#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100134 bus->reset = dw_mdio_reset;
135#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400136
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100137 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400138
139 return mdio_register(bus);
140}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000141
Simon Glasse50c4d12015-04-05 16:07:40 -0600142static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530143{
Vipin KUMAR1f873122010-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 Galvani3bfa65c2016-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 KUMAR1f873122010-06-29 10:53:34 +0530154
155#if defined(CONFIG_DW_ALTDESCRIPTOR)
156 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100157 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
158 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-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 Galvani3bfa65c2016-05-08 08:30:15 +0200171 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530172
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400173 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200174 flush_dcache_range((ulong)priv->tx_mac_descrtable,
175 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400176 sizeof(priv->tx_mac_descrtable));
177
Vipin KUMAR1f873122010-06-29 10:53:34 +0530178 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400179 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530180}
181
Simon Glasse50c4d12015-04-05 16:07:40 -0600182static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530183{
Vipin KUMAR1f873122010-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 Brodkin0d3b22e2014-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 Galvani3bfa65c2016-05-08 08:30:15 +0200196 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400197
Vipin KUMAR1f873122010-06-29 10:53:34 +0530198 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
199 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-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 KUMAR1f873122010-06-29 10:53:34 +0530202
203 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100204 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-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 Galvani3bfa65c2016-05-08 08:30:15 +0200211 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530212
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400213 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200214 flush_dcache_range((ulong)priv->rx_mac_descrtable,
215 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400216 sizeof(priv->rx_mac_descrtable));
217
Vipin KUMAR1f873122010-06-29 10:53:34 +0530218 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400219 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530220}
221
Simon Glasse50c4d12015-04-05 16:07:40 -0600222static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530223{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400224 struct eth_mac_regs *mac_p = priv->mac_regs_p;
225 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400226
227 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
228 (mac_id[3] << 24);
229 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;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530235}
236
Simon Glass4afa85e2017-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)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530239{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400240 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530241
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400242 if (!phydev->link) {
243 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100244 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400245 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530246
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400247 if (phydev->speed != 1000)
248 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300249 else
250 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530251
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400252 if (phydev->speed == 100)
253 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530254
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400255 if (phydev->duplex)
256 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000257
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400258 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530259
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400260 printf("Speed: %d, %s duplex%s\n", phydev->speed,
261 (phydev->duplex) ? "full" : "half",
262 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100263
264 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530265}
266
Simon Glasse50c4d12015-04-05 16:07:40 -0600267static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530268{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530269 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400270 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530271
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400272 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
273 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530274
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400275 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530276}
277
Simon Glassc154fc02017-01-11 11:46:10 +0100278int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530279{
Vipin KUMAR1f873122010-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 Brodkin9a0b1302014-01-22 20:54:06 +0400282 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600283 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530284
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400285 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000286
Quentin Schulz7f920dd2018-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 Brodkin9a0b1302014-01-22 20:54:06 +0400296 start = get_timer(0);
297 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300298 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
299 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600300 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300301 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200302
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400303 mdelay(100);
304 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530305
Bin Meng2ddfa2a2015-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 Glasse50c4d12015-04-05 16:07:40 -0600312 rx_descs_init(priv);
313 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530314
Ian Campbell4164b742014-05-08 22:26:35 +0100315 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530316
Sonic Zhangb917b622015-01-29 14:38:50 +0800317#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400318 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
319 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800320#else
321 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
322 &dma_p->opmode);
323#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530324
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400325 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530326
Sonic Zhang962c95c2015-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 Brodkin9a0b1302014-01-22 20:54:06 +0400331 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600332 ret = phy_startup(priv->phydev);
333 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400334 printf("Could not initialize PHY %s\n",
335 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600336 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530337 }
338
Simon Glass4afa85e2017-01-11 11:46:08 +0100339 ret = dw_adjust_link(priv, mac_p, priv->phydev);
340 if (ret)
341 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530342
Simon Glass3240e942017-01-11 11:46:09 +0100343 return 0;
344}
345
Simon Glassc154fc02017-01-11 11:46:10 +0100346int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100347{
348 struct eth_mac_regs *mac_p = priv->mac_regs_p;
349
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400350 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600351 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530352
Armando Visconti038c9d52012-03-26 00:09:55 +0000353 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530354
355 return 0;
356}
357
Florian Fainelli65f686b2017-12-09 14:59:55 -0800358#define ETH_ZLEN 60
359
Simon Glasse50c4d12015-04-05 16:07:40 -0600360static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530361{
Vipin KUMAR1f873122010-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 Galvani3bfa65c2016-05-08 08:30:15 +0200365 ulong desc_start = (ulong)desc_p;
366 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200367 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-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 Campbell0e690fd2014-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 Vasut15193042014-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 Campbell0e690fd2014-05-08 22:26:33 +0100377 */
Marek Vasut15193042014-09-15 01:05:23 +0200378 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400379
Vipin KUMAR1f873122010-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 Glasse50c4d12015-04-05 16:07:40 -0600383 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530384 }
385
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200386 memcpy((void *)data_start, packet, length);
Simon Goldschmidt80385de2018-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 KUMAR1f873122010-06-29 10:53:34 +0530391
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400392 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200393 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400394
Vipin KUMAR1f873122010-06-29 10:53:34 +0530395#if defined(CONFIG_DW_ALTDESCRIPTOR)
396 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Simon Goldschmidte2d0a7c2018-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 KUMAR1f873122010-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 Goldschmidte2d0a7c2018-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 KUMAR1f873122010-06-29 10:53:34 +0530408
409 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
410#endif
411
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400412 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200413 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400414
Vipin KUMAR1f873122010-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 Glass90e627b2015-04-05 16:07:41 -0600427static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530428{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400429 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530430 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600431 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200432 ulong desc_start = (ulong)desc_p;
433 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200434 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200435 ulong data_start = desc_p->dmamac_addr;
436 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530437
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400438 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200439 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400440
441 status = desc_p->txrx_status;
442
Vipin KUMAR1f873122010-06-29 10:53:34 +0530443 /* Check if the owner is the CPU */
444 if (!(status & DESC_RXSTS_OWNBYDMA)) {
445
Marek Vasut4ab539a2015-12-20 03:59:23 +0100446 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530447 DESC_RXSTS_FRMLENSHFT;
448
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400449 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200450 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
451 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200452 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Simon Glass90e627b2015-04-05 16:07:41 -0600453 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400454
Simon Glass90e627b2015-04-05 16:07:41 -0600455 return length;
456}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530457
Simon Glass90e627b2015-04-05 16:07:41 -0600458static 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 Galvani3bfa65c2016-05-08 08:30:15 +0200462 ulong desc_start = (ulong)desc_p;
463 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600464 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530465
Simon Glass90e627b2015-04-05 16:07:41 -0600466 /*
467 * Make the current descriptor valid again and go to
468 * the next one
469 */
470 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400471
Simon Glass90e627b2015-04-05 16:07:41 -0600472 /* Flush only status field - others weren't changed */
473 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530474
Simon Glass90e627b2015-04-05 16:07:41 -0600475 /* Test the wrap-around condition. */
476 if (++desc_num >= CONFIG_RX_DESCR_NUM)
477 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530478 priv->rx_currdescnum = desc_num;
479
Simon Glass90e627b2015-04-05 16:07:41 -0600480 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530481}
482
Simon Glasse50c4d12015-04-05 16:07:40 -0600483static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530484{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400485 struct phy_device *phydev;
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200486 int phy_addr = -1, ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530487
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400488#ifdef CONFIG_PHY_ADDR
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200489 phy_addr = CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530490#endif
491
Simon Goldschmidte1922c72019-07-15 21:53:05 +0200492 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400493 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600494 return -ENODEV;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530495
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400496 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-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 Brodkin9a0b1302014-01-22 20:54:06 +0400502 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530503
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400504 priv->phydev = phydev;
505 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530506
Simon Glasse50c4d12015-04-05 16:07:40 -0600507 return 0;
508}
509
Simon Glass90e627b2015-04-05 16:07:41 -0600510#ifndef CONFIG_DM_ETH
Simon Glasse50c4d12015-04-05 16:07:40 -0600511static int dw_eth_init(struct eth_device *dev, bd_t *bis)
512{
Simon Glass3240e942017-01-11 11:46:09 +0100513 int ret;
514
Simon Glassc154fc02017-01-11 11:46:10 +0100515 ret = designware_eth_init(dev->priv, dev->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100516 if (!ret)
517 ret = designware_eth_enable(dev->priv);
518
519 return ret;
Simon Glasse50c4d12015-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 Glass90e627b2015-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 Glasse50c4d12015-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 KUMAR1f873122010-06-29 10:53:34 +0530550}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530551
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400552int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR1f873122010-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 Campbell07c92fc2014-05-08 22:26:32 +0100565 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
566 sizeof(struct dw_eth_dev));
Vipin KUMAR1f873122010-06-29 10:53:34 +0530567 if (!priv) {
568 free(dev);
569 return -ENOMEM;
570 }
571
Beniamino Galvani3bfa65c2016-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 KUMAR1f873122010-06-29 10:53:34 +0530577 memset(dev, 0, sizeof(struct eth_device));
578 memset(priv, 0, sizeof(struct dw_eth_dev));
579
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400580 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530581 dev->iobase = (int)base_addr;
582 dev->priv = priv;
583
Vipin KUMAR1f873122010-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 KUMAR1f873122010-06-29 10:53:34 +0530588
Vipin KUMAR1f873122010-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 Brodkin9a0b1302014-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 Glasse50c4d12015-04-05 16:07:40 -0600602 return dw_phy_init(priv, dev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530603}
Simon Glass90e627b2015-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 Glass3240e942017-01-11 11:46:09 +0100610 struct dw_eth_dev *priv = dev_get_priv(dev);
611 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600612
Simon Glassc154fc02017-01-11 11:46:10 +0100613 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-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 Glass90e627b2015-04-05 16:07:41 -0600621}
622
Simon Glassc154fc02017-01-11 11:46:10 +0100623int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-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 Glassc154fc02017-01-11 11:46:10 +0100630int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-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 Glassc154fc02017-01-11 11:46:10 +0100637int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-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 Glassc154fc02017-01-11 11:46:10 +0100644void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-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 Glassc154fc02017-01-11 11:46:10 +0100651int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-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 Menged89bd72015-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 Simons9cf8fd02017-01-11 11:46:07 +0100675int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-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 Mengdfc90f52015-09-03 05:37:29 -0700679 u32 iobase = pdata->iobase;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200680 ulong ioaddr;
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200681 int ret, err;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800682 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100683#ifdef CONFIG_CLK
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200684 int i, clock_nb;
Patrice Chotardeebcf8c2017-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 Paltsev11e754e2018-02-06 17:12:09 +0300700 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-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 Glass90e627b2015-04-05 16:07:41 -0600712
Jacob Chen7ceacea2017-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 Tan27d5c002018-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 Menged89bd72015-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 Menged89bd72015-09-11 03:24:35 -0700741 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
742 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6c3300c2016-02-02 05:58:00 -0800743 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Menged89bd72015-09-11 03:24:35 -0700744
745 pdata->iobase = iobase;
746 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
747 }
748#endif
749
Bin Mengdfc90f52015-09-03 05:37:29 -0700750 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani3bfa65c2016-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 Glass90e627b2015-04-05 16:07:41 -0600754 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300755 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600756
Simon Goldschmidt313b9662019-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 Glass90e627b2015-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 Goldschmidt313b9662019-07-12 21:07:03 +0200766 if (!ret)
767 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600768
Simon Goldschmidt313b9662019-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 Chotardeebcf8c2017-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 Chotardeebcf8c2017-11-29 09:06:11 +0100781#endif
Simon Goldschmidt313b9662019-07-12 21:07:03 +0200782 return err;
Simon Glass90e627b2015-04-05 16:07:41 -0600783}
784
Bin Mengf0f02772015-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 Chotardeebcf8c2017-11-29 09:06:11 +0100793#ifdef CONFIG_CLK
794 return clk_release_all(priv->clocks, priv->clock_count);
795#else
Bin Mengf0f02772015-10-07 21:32:38 -0700796 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100797#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700798}
799
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100800const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-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 Simons9cf8fd02017-01-11 11:46:07 +0100809int designware_eth_ofdata_to_platdata(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600810{
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100811 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Simon Glassfa4689a2019-12-06 21:41:35 -0700812#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100813 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300814#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100815 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass90e627b2015-04-05 16:07:41 -0600816 const char *phy_mode;
Simon Glassfa4689a2019-12-06 21:41:35 -0700817#if CONFIG_IS_ENABLED(DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100818 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300819#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100820 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600821
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200822 pdata->iobase = dev_read_addr(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600823 pdata->phy_interface = -1;
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200824 phy_mode = dev_read_string(dev, "phy-mode");
Simon Glass90e627b2015-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 Tomsichdcf87632017-09-11 22:04:13 +0200832 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300833
Simon Glassfa4689a2019-12-06 21:41:35 -0700834#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich150005b2017-06-07 18:46:01 +0200835 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-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 Tomsich150005b2017-06-07 18:46:01 +0200841 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
842 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100843 } else if (ret == -ENOENT) {
844 ret = 0;
845 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300846#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100847
848 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600849}
850
851static const struct udevice_id designware_eth_ids[] = {
852 { .compatible = "allwinner,sun7i-a20-gmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200853 { .compatible = "amlogic,meson6-dwmac" },
Heiner Kallweit83fdbe42017-01-27 21:25:59 +0100854 { .compatible = "amlogic,meson-gx-dwmac" },
Neil Armstrong195cb332018-09-10 16:44:14 +0200855 { .compatible = "amlogic,meson-gxbb-dwmac" },
Neil Armstrong02802c52018-11-08 17:16:11 +0100856 { .compatible = "amlogic,meson-axg-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100857 { .compatible = "st,stm32-dwmac" },
Eugeniy Paltsev5738e942019-10-07 19:10:50 +0300858 { .compatible = "snps,arc-dwmac-3.70a" },
Simon Glass90e627b2015-04-05 16:07:41 -0600859 { }
860};
861
Marek Vasut7e7e6172015-07-25 18:42:34 +0200862U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-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 Menged89bd72015-09-11 03:24:35 -0700867 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600868 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700869 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600870 .ops = &designware_eth_ops,
871 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100872 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600873 .flags = DM_FLAG_ALLOC_PRIV_DMA,
874};
Bin Menged89bd72015-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 Glass90e627b2015-04-05 16:07:41 -0600882#endif