blob: 19db0a8114ae8bc0c37766f294486ea64980ad0b [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 Glass90e627b2015-04-05 16:07:41 -060013#include <dm.h>
Simon Glasse50c4d12015-04-05 16:07:40 -060014#include <errno.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053015#include <miiphy.h>
16#include <malloc.h>
Bin Menged89bd72015-09-11 03:24:35 -070017#include <pci.h>
Ley Foon Tan27d5c002018-06-14 18:45:23 +080018#include <reset.h>
Stefan Roesed27e86c2012-05-07 12:04:25 +020019#include <linux/compiler.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053020#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080021#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053022#include <asm/io.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080023#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053024#include "designware.h"
25
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040026static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
27{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010028#ifdef CONFIG_DM_ETH
29 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
30 struct eth_mac_regs *mac_p = priv->mac_regs_p;
31#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040032 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010033#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040034 ulong start;
35 u16 miiaddr;
36 int timeout = CONFIG_MDIO_TIMEOUT;
37
38 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
39 ((reg << MIIREGSHIFT) & MII_REGMSK);
40
41 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
42
43 start = get_timer(0);
44 while (get_timer(start) < timeout) {
45 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
46 return readl(&mac_p->miidata);
47 udelay(10);
48 };
49
Simon Glasse50c4d12015-04-05 16:07:40 -060050 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040051}
52
53static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
54 u16 val)
55{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010056#ifdef CONFIG_DM_ETH
57 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
58 struct eth_mac_regs *mac_p = priv->mac_regs_p;
59#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040060 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010061#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040062 ulong start;
63 u16 miiaddr;
Simon Glasse50c4d12015-04-05 16:07:40 -060064 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040065
66 writel(val, &mac_p->miidata);
67 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
68 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
69
70 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
71
72 start = get_timer(0);
73 while (get_timer(start) < timeout) {
74 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
75 ret = 0;
76 break;
77 }
78 udelay(10);
79 };
80
81 return ret;
82}
83
Alexey Brodkin57a37bc2016-06-27 13:17:51 +030084#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010085static int dw_mdio_reset(struct mii_dev *bus)
86{
87 struct udevice *dev = bus->priv;
88 struct dw_eth_dev *priv = dev_get_priv(dev);
89 struct dw_eth_pdata *pdata = dev_get_platdata(dev);
90 int ret;
91
92 if (!dm_gpio_is_valid(&priv->reset_gpio))
93 return 0;
94
95 /* reset the phy */
96 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
97 if (ret)
98 return ret;
99
100 udelay(pdata->reset_delays[0]);
101
102 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
103 if (ret)
104 return ret;
105
106 udelay(pdata->reset_delays[1]);
107
108 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
109 if (ret)
110 return ret;
111
112 udelay(pdata->reset_delays[2]);
113
114 return 0;
115}
116#endif
117
118static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400119{
120 struct mii_dev *bus = mdio_alloc();
121
122 if (!bus) {
123 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600124 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400125 }
126
127 bus->read = dw_mdio_read;
128 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000129 snprintf(bus->name, sizeof(bus->name), "%s", name);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300130#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100131 bus->reset = dw_mdio_reset;
132#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400133
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100134 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400135
136 return mdio_register(bus);
137}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000138
Simon Glasse50c4d12015-04-05 16:07:40 -0600139static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530140{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530141 struct eth_dma_regs *dma_p = priv->dma_regs_p;
142 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
143 char *txbuffs = &priv->txbuffs[0];
144 struct dmamacdescr *desc_p;
145 u32 idx;
146
147 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
148 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200149 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
150 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530151
152#if defined(CONFIG_DW_ALTDESCRIPTOR)
153 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100154 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
155 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530156 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
157
158 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
159 desc_p->dmamac_cntl = 0;
160 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
161#else
162 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
163 desc_p->txrx_status = 0;
164#endif
165 }
166
167 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200168 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530169
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400170 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200171 flush_dcache_range((ulong)priv->tx_mac_descrtable,
172 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400173 sizeof(priv->tx_mac_descrtable));
174
Vipin KUMAR1f873122010-06-29 10:53:34 +0530175 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400176 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530177}
178
Simon Glasse50c4d12015-04-05 16:07:40 -0600179static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530180{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530181 struct eth_dma_regs *dma_p = priv->dma_regs_p;
182 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
183 char *rxbuffs = &priv->rxbuffs[0];
184 struct dmamacdescr *desc_p;
185 u32 idx;
186
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400187 /* Before passing buffers to GMAC we need to make sure zeros
188 * written there right after "priv" structure allocation were
189 * flushed into RAM.
190 * Otherwise there's a chance to get some of them flushed in RAM when
191 * GMAC is already pushing data to RAM via DMA. This way incoming from
192 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200193 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400194
Vipin KUMAR1f873122010-06-29 10:53:34 +0530195 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
196 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200197 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
198 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530199
200 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100201 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530202 DESC_RXCTRL_RXCHAIN;
203
204 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
205 }
206
207 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200208 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530209
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400210 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200211 flush_dcache_range((ulong)priv->rx_mac_descrtable,
212 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400213 sizeof(priv->rx_mac_descrtable));
214
Vipin KUMAR1f873122010-06-29 10:53:34 +0530215 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400216 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530217}
218
Simon Glasse50c4d12015-04-05 16:07:40 -0600219static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530220{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400221 struct eth_mac_regs *mac_p = priv->mac_regs_p;
222 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400223
224 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
225 (mac_id[3] << 24);
226 macid_hi = mac_id[4] + (mac_id[5] << 8);
227
228 writel(macid_hi, &mac_p->macaddr0hi);
229 writel(macid_lo, &mac_p->macaddr0lo);
230
231 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530232}
233
Simon Glass4afa85e2017-01-11 11:46:08 +0100234static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
235 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530236{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400237 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530238
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400239 if (!phydev->link) {
240 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100241 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400242 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530243
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400244 if (phydev->speed != 1000)
245 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300246 else
247 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530248
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400249 if (phydev->speed == 100)
250 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530251
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400252 if (phydev->duplex)
253 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000254
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400255 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530256
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400257 printf("Speed: %d, %s duplex%s\n", phydev->speed,
258 (phydev->duplex) ? "full" : "half",
259 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100260
261 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530262}
263
Simon Glasse50c4d12015-04-05 16:07:40 -0600264static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530265{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530266 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400267 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530268
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400269 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
270 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530271
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400272 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530273}
274
Simon Glassc154fc02017-01-11 11:46:10 +0100275int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530276{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530277 struct eth_mac_regs *mac_p = priv->mac_regs_p;
278 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400279 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600280 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530281
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400282 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000283
Quentin Schulz7f920dd2018-06-04 12:17:33 +0200284 /*
285 * When a MII PHY is used, we must set the PS bit for the DMA
286 * reset to succeed.
287 */
288 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
289 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
290 else
291 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
292
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400293 start = get_timer(0);
294 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300295 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
296 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600297 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300298 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200299
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400300 mdelay(100);
301 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530302
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800303 /*
304 * Soft reset above clears HW address registers.
305 * So we have to set it here once again.
306 */
307 _dw_write_hwaddr(priv, enetaddr);
308
Simon Glasse50c4d12015-04-05 16:07:40 -0600309 rx_descs_init(priv);
310 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530311
Ian Campbell4164b742014-05-08 22:26:35 +0100312 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530313
Sonic Zhangb917b622015-01-29 14:38:50 +0800314#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400315 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
316 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800317#else
318 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
319 &dma_p->opmode);
320#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530321
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400322 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530323
Sonic Zhang962c95c2015-01-29 13:37:31 +0800324#ifdef CONFIG_DW_AXI_BURST_LEN
325 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
326#endif
327
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400328 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600329 ret = phy_startup(priv->phydev);
330 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400331 printf("Could not initialize PHY %s\n",
332 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600333 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530334 }
335
Simon Glass4afa85e2017-01-11 11:46:08 +0100336 ret = dw_adjust_link(priv, mac_p, priv->phydev);
337 if (ret)
338 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530339
Simon Glass3240e942017-01-11 11:46:09 +0100340 return 0;
341}
342
Simon Glassc154fc02017-01-11 11:46:10 +0100343int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100344{
345 struct eth_mac_regs *mac_p = priv->mac_regs_p;
346
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400347 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600348 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530349
Armando Visconti038c9d52012-03-26 00:09:55 +0000350 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530351
352 return 0;
353}
354
Florian Fainelli65f686b2017-12-09 14:59:55 -0800355#define ETH_ZLEN 60
356
Simon Glasse50c4d12015-04-05 16:07:40 -0600357static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530358{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530359 struct eth_dma_regs *dma_p = priv->dma_regs_p;
360 u32 desc_num = priv->tx_currdescnum;
361 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200362 ulong desc_start = (ulong)desc_p;
363 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200364 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200365 ulong data_start = desc_p->dmamac_addr;
366 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100367 /*
368 * Strictly we only need to invalidate the "txrx_status" field
369 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200370 * invalidate only 4 bytes, so we flush the entire descriptor,
371 * which is 16 bytes in total. This is safe because the
372 * individual descriptors in the array are each aligned to
373 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100374 */
Marek Vasut15193042014-09-15 01:05:23 +0200375 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400376
Vipin KUMAR1f873122010-06-29 10:53:34 +0530377 /* Check if the descriptor is owned by CPU */
378 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
379 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600380 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530381 }
382
Florian Fainelli65f686b2017-12-09 14:59:55 -0800383 length = max(length, ETH_ZLEN);
384
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200385 memcpy((void *)data_start, packet, length);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530386
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400387 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200388 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400389
Vipin KUMAR1f873122010-06-29 10:53:34 +0530390#if defined(CONFIG_DW_ALTDESCRIPTOR)
391 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Marek Vasut4ab539a2015-12-20 03:59:23 +0100392 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
Vipin KUMAR1f873122010-06-29 10:53:34 +0530393 DESC_TXCTRL_SIZE1MASK;
394
395 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
396 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
397#else
Marek Vasut4ab539a2015-12-20 03:59:23 +0100398 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
399 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530400 DESC_TXCTRL_TXFIRST;
401
402 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
403#endif
404
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400405 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200406 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400407
Vipin KUMAR1f873122010-06-29 10:53:34 +0530408 /* Test the wrap-around condition. */
409 if (++desc_num >= CONFIG_TX_DESCR_NUM)
410 desc_num = 0;
411
412 priv->tx_currdescnum = desc_num;
413
414 /* Start the transmission */
415 writel(POLL_DATA, &dma_p->txpolldemand);
416
417 return 0;
418}
419
Simon Glass90e627b2015-04-05 16:07:41 -0600420static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530421{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400422 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530423 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600424 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200425 ulong desc_start = (ulong)desc_p;
426 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200427 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200428 ulong data_start = desc_p->dmamac_addr;
429 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530430
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400431 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200432 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400433
434 status = desc_p->txrx_status;
435
Vipin KUMAR1f873122010-06-29 10:53:34 +0530436 /* Check if the owner is the CPU */
437 if (!(status & DESC_RXSTS_OWNBYDMA)) {
438
Marek Vasut4ab539a2015-12-20 03:59:23 +0100439 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530440 DESC_RXSTS_FRMLENSHFT;
441
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400442 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200443 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
444 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200445 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Simon Glass90e627b2015-04-05 16:07:41 -0600446 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400447
Simon Glass90e627b2015-04-05 16:07:41 -0600448 return length;
449}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530450
Simon Glass90e627b2015-04-05 16:07:41 -0600451static int _dw_free_pkt(struct dw_eth_dev *priv)
452{
453 u32 desc_num = priv->rx_currdescnum;
454 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200455 ulong desc_start = (ulong)desc_p;
456 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600457 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530458
Simon Glass90e627b2015-04-05 16:07:41 -0600459 /*
460 * Make the current descriptor valid again and go to
461 * the next one
462 */
463 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400464
Simon Glass90e627b2015-04-05 16:07:41 -0600465 /* Flush only status field - others weren't changed */
466 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530467
Simon Glass90e627b2015-04-05 16:07:41 -0600468 /* Test the wrap-around condition. */
469 if (++desc_num >= CONFIG_RX_DESCR_NUM)
470 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530471 priv->rx_currdescnum = desc_num;
472
Simon Glass90e627b2015-04-05 16:07:41 -0600473 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530474}
475
Simon Glasse50c4d12015-04-05 16:07:40 -0600476static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530477{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400478 struct phy_device *phydev;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300479 int mask = 0xffffffff, ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530480
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400481#ifdef CONFIG_PHY_ADDR
482 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530483#endif
484
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400485 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
486 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600487 return -ENODEV;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530488
Ian Campbell7d555502014-04-28 20:14:05 +0100489 phy_connect_dev(phydev, dev);
490
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400491 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300492 if (priv->max_speed) {
493 ret = phy_set_supported(phydev, priv->max_speed);
494 if (ret)
495 return ret;
496 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400497 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530498
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400499 priv->phydev = phydev;
500 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530501
Simon Glasse50c4d12015-04-05 16:07:40 -0600502 return 0;
503}
504
Simon Glass90e627b2015-04-05 16:07:41 -0600505#ifndef CONFIG_DM_ETH
Simon Glasse50c4d12015-04-05 16:07:40 -0600506static int dw_eth_init(struct eth_device *dev, bd_t *bis)
507{
Simon Glass3240e942017-01-11 11:46:09 +0100508 int ret;
509
Simon Glassc154fc02017-01-11 11:46:10 +0100510 ret = designware_eth_init(dev->priv, dev->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100511 if (!ret)
512 ret = designware_eth_enable(dev->priv);
513
514 return ret;
Simon Glasse50c4d12015-04-05 16:07:40 -0600515}
516
517static int dw_eth_send(struct eth_device *dev, void *packet, int length)
518{
519 return _dw_eth_send(dev->priv, packet, length);
520}
521
522static int dw_eth_recv(struct eth_device *dev)
523{
Simon Glass90e627b2015-04-05 16:07:41 -0600524 uchar *packet;
525 int length;
526
527 length = _dw_eth_recv(dev->priv, &packet);
528 if (length == -EAGAIN)
529 return 0;
530 net_process_received_packet(packet, length);
531
532 _dw_free_pkt(dev->priv);
533
534 return 0;
Simon Glasse50c4d12015-04-05 16:07:40 -0600535}
536
537static void dw_eth_halt(struct eth_device *dev)
538{
539 return _dw_eth_halt(dev->priv);
540}
541
542static int dw_write_hwaddr(struct eth_device *dev)
543{
544 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530545}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530546
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400547int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530548{
549 struct eth_device *dev;
550 struct dw_eth_dev *priv;
551
552 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
553 if (!dev)
554 return -ENOMEM;
555
556 /*
557 * Since the priv structure contains the descriptors which need a strict
558 * buswidth alignment, memalign is used to allocate memory
559 */
Ian Campbell07c92fc2014-05-08 22:26:32 +0100560 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
561 sizeof(struct dw_eth_dev));
Vipin KUMAR1f873122010-06-29 10:53:34 +0530562 if (!priv) {
563 free(dev);
564 return -ENOMEM;
565 }
566
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200567 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
568 printf("designware: buffers are outside DMA memory\n");
569 return -EINVAL;
570 }
571
Vipin KUMAR1f873122010-06-29 10:53:34 +0530572 memset(dev, 0, sizeof(struct eth_device));
573 memset(priv, 0, sizeof(struct dw_eth_dev));
574
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400575 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530576 dev->iobase = (int)base_addr;
577 dev->priv = priv;
578
Vipin KUMAR1f873122010-06-29 10:53:34 +0530579 priv->dev = dev;
580 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
581 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
582 DW_DMA_BASE_OFFSET);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530583
Vipin KUMAR1f873122010-06-29 10:53:34 +0530584 dev->init = dw_eth_init;
585 dev->send = dw_eth_send;
586 dev->recv = dw_eth_recv;
587 dev->halt = dw_eth_halt;
588 dev->write_hwaddr = dw_write_hwaddr;
589
590 eth_register(dev);
591
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400592 priv->interface = interface;
593
594 dw_mdio_init(dev->name, priv->mac_regs_p);
595 priv->bus = miiphy_get_dev_by_name(dev->name);
596
Simon Glasse50c4d12015-04-05 16:07:40 -0600597 return dw_phy_init(priv, dev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530598}
Simon Glass90e627b2015-04-05 16:07:41 -0600599#endif
600
601#ifdef CONFIG_DM_ETH
602static int designware_eth_start(struct udevice *dev)
603{
604 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100605 struct dw_eth_dev *priv = dev_get_priv(dev);
606 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600607
Simon Glassc154fc02017-01-11 11:46:10 +0100608 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100609 if (ret)
610 return ret;
611 ret = designware_eth_enable(priv);
612 if (ret)
613 return ret;
614
615 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600616}
617
Simon Glassc154fc02017-01-11 11:46:10 +0100618int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600619{
620 struct dw_eth_dev *priv = dev_get_priv(dev);
621
622 return _dw_eth_send(priv, packet, length);
623}
624
Simon Glassc154fc02017-01-11 11:46:10 +0100625int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600626{
627 struct dw_eth_dev *priv = dev_get_priv(dev);
628
629 return _dw_eth_recv(priv, packetp);
630}
631
Simon Glassc154fc02017-01-11 11:46:10 +0100632int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600633{
634 struct dw_eth_dev *priv = dev_get_priv(dev);
635
636 return _dw_free_pkt(priv);
637}
638
Simon Glassc154fc02017-01-11 11:46:10 +0100639void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600640{
641 struct dw_eth_dev *priv = dev_get_priv(dev);
642
643 return _dw_eth_halt(priv);
644}
645
Simon Glassc154fc02017-01-11 11:46:10 +0100646int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600647{
648 struct eth_pdata *pdata = dev_get_platdata(dev);
649 struct dw_eth_dev *priv = dev_get_priv(dev);
650
651 return _dw_write_hwaddr(priv, pdata->enetaddr);
652}
653
Bin Menged89bd72015-09-11 03:24:35 -0700654static int designware_eth_bind(struct udevice *dev)
655{
656#ifdef CONFIG_DM_PCI
657 static int num_cards;
658 char name[20];
659
660 /* Create a unique device name for PCI type devices */
661 if (device_is_on_pci_bus(dev)) {
662 sprintf(name, "eth_designware#%u", num_cards++);
663 device_set_name(dev, name);
664 }
665#endif
666
667 return 0;
668}
669
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100670int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600671{
672 struct eth_pdata *pdata = dev_get_platdata(dev);
673 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengdfc90f52015-09-03 05:37:29 -0700674 u32 iobase = pdata->iobase;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200675 ulong ioaddr;
Simon Glass90e627b2015-04-05 16:07:41 -0600676 int ret;
Ley Foon Tan27d5c002018-06-14 18:45:23 +0800677 struct reset_ctl_bulk reset_bulk;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100678#ifdef CONFIG_CLK
679 int i, err, clock_nb;
680
681 priv->clock_count = 0;
682 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
683 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#ifdef CONFIG_DM_PCI
731 /*
732 * If we are on PCI bus, either directly attached to a PCI root port,
733 * or via a PCI bridge, fill in platdata before we probe the hardware.
734 */
735 if (device_is_on_pci_bus(dev)) {
Bin Menged89bd72015-09-11 03:24:35 -0700736 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
737 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6c3300c2016-02-02 05:58:00 -0800738 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Menged89bd72015-09-11 03:24:35 -0700739
740 pdata->iobase = iobase;
741 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
742 }
743#endif
744
Bin Mengdfc90f52015-09-03 05:37:29 -0700745 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200746 ioaddr = iobase;
747 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
748 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600749 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300750 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600751
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100752 dw_mdio_init(dev->name, dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600753 priv->bus = miiphy_get_dev_by_name(dev->name);
754
755 ret = dw_phy_init(priv, dev);
756 debug("%s, ret=%d\n", __func__, ret);
757
758 return ret;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100759
760#ifdef CONFIG_CLK
761clk_err:
762 ret = clk_release_all(priv->clocks, priv->clock_count);
763 if (ret)
764 pr_err("failed to disable all clocks\n");
765
766 return err;
767#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600768}
769
Bin Mengf0f02772015-10-07 21:32:38 -0700770static int designware_eth_remove(struct udevice *dev)
771{
772 struct dw_eth_dev *priv = dev_get_priv(dev);
773
774 free(priv->phydev);
775 mdio_unregister(priv->bus);
776 mdio_free(priv->bus);
777
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100778#ifdef CONFIG_CLK
779 return clk_release_all(priv->clocks, priv->clock_count);
780#else
Bin Mengf0f02772015-10-07 21:32:38 -0700781 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100782#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700783}
784
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100785const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600786 .start = designware_eth_start,
787 .send = designware_eth_send,
788 .recv = designware_eth_recv,
789 .free_pkt = designware_eth_free_pkt,
790 .stop = designware_eth_stop,
791 .write_hwaddr = designware_eth_write_hwaddr,
792};
793
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100794int designware_eth_ofdata_to_platdata(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600795{
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100796 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300797#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100798 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300799#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100800 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass90e627b2015-04-05 16:07:41 -0600801 const char *phy_mode;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300802#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100803 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300804#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100805 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600806
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200807 pdata->iobase = dev_read_addr(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600808 pdata->phy_interface = -1;
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200809 phy_mode = dev_read_string(dev, "phy-mode");
Simon Glass90e627b2015-04-05 16:07:41 -0600810 if (phy_mode)
811 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
812 if (pdata->phy_interface == -1) {
813 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
814 return -EINVAL;
815 }
816
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200817 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300818
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300819#ifdef CONFIG_DM_GPIO
Philipp Tomsich150005b2017-06-07 18:46:01 +0200820 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100821 reset_flags |= GPIOD_ACTIVE_LOW;
822
823 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
824 &priv->reset_gpio, reset_flags);
825 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200826 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
827 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100828 } else if (ret == -ENOENT) {
829 ret = 0;
830 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300831#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100832
833 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600834}
835
836static const struct udevice_id designware_eth_ids[] = {
837 { .compatible = "allwinner,sun7i-a20-gmac" },
Marek Vasutfcab73c2015-07-25 18:38:44 +0200838 { .compatible = "altr,socfpga-stmmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200839 { .compatible = "amlogic,meson6-dwmac" },
Heiner Kallweit83fdbe42017-01-27 21:25:59 +0100840 { .compatible = "amlogic,meson-gx-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100841 { .compatible = "st,stm32-dwmac" },
Simon Glass90e627b2015-04-05 16:07:41 -0600842 { }
843};
844
Marek Vasut7e7e6172015-07-25 18:42:34 +0200845U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600846 .name = "eth_designware",
847 .id = UCLASS_ETH,
848 .of_match = designware_eth_ids,
849 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Menged89bd72015-09-11 03:24:35 -0700850 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600851 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700852 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600853 .ops = &designware_eth_ops,
854 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100855 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600856 .flags = DM_FLAG_ALLOC_PRIV_DMA,
857};
Bin Menged89bd72015-09-11 03:24:35 -0700858
859static struct pci_device_id supported[] = {
860 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
861 { }
862};
863
864U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass90e627b2015-04-05 16:07:41 -0600865#endif