blob: cf125210d88cb66d50252a9fa3023b71a96051c0 [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>
Stefan Roesed27e86c2012-05-07 12:04:25 +020018#include <linux/compiler.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053019#include <linux/err.h>
Florian Fainelli65f686b2017-12-09 14:59:55 -080020#include <linux/kernel.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053021#include <asm/io.h>
Jacob Chen7ceacea2017-03-27 16:54:17 +080022#include <power/regulator.h>
Vipin KUMAR1f873122010-06-29 10:53:34 +053023#include "designware.h"
24
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040025static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
26{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010027#ifdef CONFIG_DM_ETH
28 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
29 struct eth_mac_regs *mac_p = priv->mac_regs_p;
30#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040031 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010032#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040033 ulong start;
34 u16 miiaddr;
35 int timeout = CONFIG_MDIO_TIMEOUT;
36
37 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
38 ((reg << MIIREGSHIFT) & MII_REGMSK);
39
40 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
41
42 start = get_timer(0);
43 while (get_timer(start) < timeout) {
44 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
45 return readl(&mac_p->miidata);
46 udelay(10);
47 };
48
Simon Glasse50c4d12015-04-05 16:07:40 -060049 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040050}
51
52static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
53 u16 val)
54{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010055#ifdef CONFIG_DM_ETH
56 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
57 struct eth_mac_regs *mac_p = priv->mac_regs_p;
58#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040059 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010060#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040061 ulong start;
62 u16 miiaddr;
Simon Glasse50c4d12015-04-05 16:07:40 -060063 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040064
65 writel(val, &mac_p->miidata);
66 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
67 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
68
69 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
70
71 start = get_timer(0);
72 while (get_timer(start) < timeout) {
73 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
74 ret = 0;
75 break;
76 }
77 udelay(10);
78 };
79
80 return ret;
81}
82
Alexey Brodkin57a37bc2016-06-27 13:17:51 +030083#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010084static int dw_mdio_reset(struct mii_dev *bus)
85{
86 struct udevice *dev = bus->priv;
87 struct dw_eth_dev *priv = dev_get_priv(dev);
88 struct dw_eth_pdata *pdata = dev_get_platdata(dev);
89 int ret;
90
91 if (!dm_gpio_is_valid(&priv->reset_gpio))
92 return 0;
93
94 /* reset the phy */
95 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
96 if (ret)
97 return ret;
98
99 udelay(pdata->reset_delays[0]);
100
101 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
102 if (ret)
103 return ret;
104
105 udelay(pdata->reset_delays[1]);
106
107 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
108 if (ret)
109 return ret;
110
111 udelay(pdata->reset_delays[2]);
112
113 return 0;
114}
115#endif
116
117static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400118{
119 struct mii_dev *bus = mdio_alloc();
120
121 if (!bus) {
122 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600123 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400124 }
125
126 bus->read = dw_mdio_read;
127 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000128 snprintf(bus->name, sizeof(bus->name), "%s", name);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300129#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100130 bus->reset = dw_mdio_reset;
131#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400132
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100133 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400134
135 return mdio_register(bus);
136}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000137
Simon Glasse50c4d12015-04-05 16:07:40 -0600138static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530139{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530140 struct eth_dma_regs *dma_p = priv->dma_regs_p;
141 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
142 char *txbuffs = &priv->txbuffs[0];
143 struct dmamacdescr *desc_p;
144 u32 idx;
145
146 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
147 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200148 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
149 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530150
151#if defined(CONFIG_DW_ALTDESCRIPTOR)
152 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100153 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
154 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530155 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
156
157 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
158 desc_p->dmamac_cntl = 0;
159 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
160#else
161 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
162 desc_p->txrx_status = 0;
163#endif
164 }
165
166 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200167 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530168
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400169 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200170 flush_dcache_range((ulong)priv->tx_mac_descrtable,
171 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400172 sizeof(priv->tx_mac_descrtable));
173
Vipin KUMAR1f873122010-06-29 10:53:34 +0530174 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400175 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530176}
177
Simon Glasse50c4d12015-04-05 16:07:40 -0600178static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530179{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530180 struct eth_dma_regs *dma_p = priv->dma_regs_p;
181 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
182 char *rxbuffs = &priv->rxbuffs[0];
183 struct dmamacdescr *desc_p;
184 u32 idx;
185
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400186 /* Before passing buffers to GMAC we need to make sure zeros
187 * written there right after "priv" structure allocation were
188 * flushed into RAM.
189 * Otherwise there's a chance to get some of them flushed in RAM when
190 * GMAC is already pushing data to RAM via DMA. This way incoming from
191 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200192 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400193
Vipin KUMAR1f873122010-06-29 10:53:34 +0530194 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
195 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200196 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
197 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530198
199 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100200 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530201 DESC_RXCTRL_RXCHAIN;
202
203 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
204 }
205
206 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200207 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530208
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400209 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200210 flush_dcache_range((ulong)priv->rx_mac_descrtable,
211 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400212 sizeof(priv->rx_mac_descrtable));
213
Vipin KUMAR1f873122010-06-29 10:53:34 +0530214 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400215 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530216}
217
Simon Glasse50c4d12015-04-05 16:07:40 -0600218static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530219{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400220 struct eth_mac_regs *mac_p = priv->mac_regs_p;
221 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400222
223 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
224 (mac_id[3] << 24);
225 macid_hi = mac_id[4] + (mac_id[5] << 8);
226
227 writel(macid_hi, &mac_p->macaddr0hi);
228 writel(macid_lo, &mac_p->macaddr0lo);
229
230 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530231}
232
Simon Glass4afa85e2017-01-11 11:46:08 +0100233static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
234 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530235{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400236 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530237
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400238 if (!phydev->link) {
239 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100240 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400241 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530242
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400243 if (phydev->speed != 1000)
244 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300245 else
246 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530247
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400248 if (phydev->speed == 100)
249 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530250
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400251 if (phydev->duplex)
252 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000253
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400254 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530255
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400256 printf("Speed: %d, %s duplex%s\n", phydev->speed,
257 (phydev->duplex) ? "full" : "half",
258 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100259
260 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530261}
262
Simon Glasse50c4d12015-04-05 16:07:40 -0600263static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530264{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530265 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400266 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530267
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400268 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
269 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530270
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400271 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530272}
273
Simon Glassc154fc02017-01-11 11:46:10 +0100274int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530275{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530276 struct eth_mac_regs *mac_p = priv->mac_regs_p;
277 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400278 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600279 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530280
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400281 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000282
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400283 start = get_timer(0);
284 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300285 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
286 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600287 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300288 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200289
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400290 mdelay(100);
291 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530292
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800293 /*
294 * Soft reset above clears HW address registers.
295 * So we have to set it here once again.
296 */
297 _dw_write_hwaddr(priv, enetaddr);
298
Simon Glasse50c4d12015-04-05 16:07:40 -0600299 rx_descs_init(priv);
300 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530301
Ian Campbell4164b742014-05-08 22:26:35 +0100302 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530303
Sonic Zhangb917b622015-01-29 14:38:50 +0800304#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400305 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
306 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800307#else
308 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
309 &dma_p->opmode);
310#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530311
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400312 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530313
Sonic Zhang962c95c2015-01-29 13:37:31 +0800314#ifdef CONFIG_DW_AXI_BURST_LEN
315 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
316#endif
317
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400318 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600319 ret = phy_startup(priv->phydev);
320 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400321 printf("Could not initialize PHY %s\n",
322 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600323 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530324 }
325
Simon Glass4afa85e2017-01-11 11:46:08 +0100326 ret = dw_adjust_link(priv, mac_p, priv->phydev);
327 if (ret)
328 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530329
Simon Glass3240e942017-01-11 11:46:09 +0100330 return 0;
331}
332
Simon Glassc154fc02017-01-11 11:46:10 +0100333int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100334{
335 struct eth_mac_regs *mac_p = priv->mac_regs_p;
336
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400337 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600338 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530339
Armando Visconti038c9d52012-03-26 00:09:55 +0000340 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530341
342 return 0;
343}
344
Florian Fainelli65f686b2017-12-09 14:59:55 -0800345#define ETH_ZLEN 60
346
Simon Glasse50c4d12015-04-05 16:07:40 -0600347static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530348{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530349 struct eth_dma_regs *dma_p = priv->dma_regs_p;
350 u32 desc_num = priv->tx_currdescnum;
351 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200352 ulong desc_start = (ulong)desc_p;
353 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200354 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200355 ulong data_start = desc_p->dmamac_addr;
356 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100357 /*
358 * Strictly we only need to invalidate the "txrx_status" field
359 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200360 * invalidate only 4 bytes, so we flush the entire descriptor,
361 * which is 16 bytes in total. This is safe because the
362 * individual descriptors in the array are each aligned to
363 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100364 */
Marek Vasut15193042014-09-15 01:05:23 +0200365 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400366
Vipin KUMAR1f873122010-06-29 10:53:34 +0530367 /* Check if the descriptor is owned by CPU */
368 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
369 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600370 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530371 }
372
Florian Fainelli65f686b2017-12-09 14:59:55 -0800373 length = max(length, ETH_ZLEN);
374
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200375 memcpy((void *)data_start, packet, length);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530376
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400377 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200378 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400379
Vipin KUMAR1f873122010-06-29 10:53:34 +0530380#if defined(CONFIG_DW_ALTDESCRIPTOR)
381 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Marek Vasut4ab539a2015-12-20 03:59:23 +0100382 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
Vipin KUMAR1f873122010-06-29 10:53:34 +0530383 DESC_TXCTRL_SIZE1MASK;
384
385 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
386 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
387#else
Marek Vasut4ab539a2015-12-20 03:59:23 +0100388 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
389 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530390 DESC_TXCTRL_TXFIRST;
391
392 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
393#endif
394
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400395 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200396 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400397
Vipin KUMAR1f873122010-06-29 10:53:34 +0530398 /* Test the wrap-around condition. */
399 if (++desc_num >= CONFIG_TX_DESCR_NUM)
400 desc_num = 0;
401
402 priv->tx_currdescnum = desc_num;
403
404 /* Start the transmission */
405 writel(POLL_DATA, &dma_p->txpolldemand);
406
407 return 0;
408}
409
Simon Glass90e627b2015-04-05 16:07:41 -0600410static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530411{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400412 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530413 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600414 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200415 ulong desc_start = (ulong)desc_p;
416 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200417 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200418 ulong data_start = desc_p->dmamac_addr;
419 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530420
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400421 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200422 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400423
424 status = desc_p->txrx_status;
425
Vipin KUMAR1f873122010-06-29 10:53:34 +0530426 /* Check if the owner is the CPU */
427 if (!(status & DESC_RXSTS_OWNBYDMA)) {
428
Marek Vasut4ab539a2015-12-20 03:59:23 +0100429 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530430 DESC_RXSTS_FRMLENSHFT;
431
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400432 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200433 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
434 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200435 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Simon Glass90e627b2015-04-05 16:07:41 -0600436 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400437
Simon Glass90e627b2015-04-05 16:07:41 -0600438 return length;
439}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530440
Simon Glass90e627b2015-04-05 16:07:41 -0600441static int _dw_free_pkt(struct dw_eth_dev *priv)
442{
443 u32 desc_num = priv->rx_currdescnum;
444 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200445 ulong desc_start = (ulong)desc_p;
446 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600447 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530448
Simon Glass90e627b2015-04-05 16:07:41 -0600449 /*
450 * Make the current descriptor valid again and go to
451 * the next one
452 */
453 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400454
Simon Glass90e627b2015-04-05 16:07:41 -0600455 /* Flush only status field - others weren't changed */
456 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530457
Simon Glass90e627b2015-04-05 16:07:41 -0600458 /* Test the wrap-around condition. */
459 if (++desc_num >= CONFIG_RX_DESCR_NUM)
460 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530461 priv->rx_currdescnum = desc_num;
462
Simon Glass90e627b2015-04-05 16:07:41 -0600463 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530464}
465
Simon Glasse50c4d12015-04-05 16:07:40 -0600466static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530467{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400468 struct phy_device *phydev;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300469 int mask = 0xffffffff, ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530470
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400471#ifdef CONFIG_PHY_ADDR
472 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530473#endif
474
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400475 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
476 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600477 return -ENODEV;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530478
Ian Campbell7d555502014-04-28 20:14:05 +0100479 phy_connect_dev(phydev, dev);
480
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400481 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300482 if (priv->max_speed) {
483 ret = phy_set_supported(phydev, priv->max_speed);
484 if (ret)
485 return ret;
486 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400487 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530488
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400489 priv->phydev = phydev;
490 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530491
Simon Glasse50c4d12015-04-05 16:07:40 -0600492 return 0;
493}
494
Simon Glass90e627b2015-04-05 16:07:41 -0600495#ifndef CONFIG_DM_ETH
Simon Glasse50c4d12015-04-05 16:07:40 -0600496static int dw_eth_init(struct eth_device *dev, bd_t *bis)
497{
Simon Glass3240e942017-01-11 11:46:09 +0100498 int ret;
499
Simon Glassc154fc02017-01-11 11:46:10 +0100500 ret = designware_eth_init(dev->priv, dev->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100501 if (!ret)
502 ret = designware_eth_enable(dev->priv);
503
504 return ret;
Simon Glasse50c4d12015-04-05 16:07:40 -0600505}
506
507static int dw_eth_send(struct eth_device *dev, void *packet, int length)
508{
509 return _dw_eth_send(dev->priv, packet, length);
510}
511
512static int dw_eth_recv(struct eth_device *dev)
513{
Simon Glass90e627b2015-04-05 16:07:41 -0600514 uchar *packet;
515 int length;
516
517 length = _dw_eth_recv(dev->priv, &packet);
518 if (length == -EAGAIN)
519 return 0;
520 net_process_received_packet(packet, length);
521
522 _dw_free_pkt(dev->priv);
523
524 return 0;
Simon Glasse50c4d12015-04-05 16:07:40 -0600525}
526
527static void dw_eth_halt(struct eth_device *dev)
528{
529 return _dw_eth_halt(dev->priv);
530}
531
532static int dw_write_hwaddr(struct eth_device *dev)
533{
534 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530535}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530536
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400537int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530538{
539 struct eth_device *dev;
540 struct dw_eth_dev *priv;
541
542 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
543 if (!dev)
544 return -ENOMEM;
545
546 /*
547 * Since the priv structure contains the descriptors which need a strict
548 * buswidth alignment, memalign is used to allocate memory
549 */
Ian Campbell07c92fc2014-05-08 22:26:32 +0100550 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
551 sizeof(struct dw_eth_dev));
Vipin KUMAR1f873122010-06-29 10:53:34 +0530552 if (!priv) {
553 free(dev);
554 return -ENOMEM;
555 }
556
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200557 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
558 printf("designware: buffers are outside DMA memory\n");
559 return -EINVAL;
560 }
561
Vipin KUMAR1f873122010-06-29 10:53:34 +0530562 memset(dev, 0, sizeof(struct eth_device));
563 memset(priv, 0, sizeof(struct dw_eth_dev));
564
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400565 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530566 dev->iobase = (int)base_addr;
567 dev->priv = priv;
568
Vipin KUMAR1f873122010-06-29 10:53:34 +0530569 priv->dev = dev;
570 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
571 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
572 DW_DMA_BASE_OFFSET);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530573
Vipin KUMAR1f873122010-06-29 10:53:34 +0530574 dev->init = dw_eth_init;
575 dev->send = dw_eth_send;
576 dev->recv = dw_eth_recv;
577 dev->halt = dw_eth_halt;
578 dev->write_hwaddr = dw_write_hwaddr;
579
580 eth_register(dev);
581
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400582 priv->interface = interface;
583
584 dw_mdio_init(dev->name, priv->mac_regs_p);
585 priv->bus = miiphy_get_dev_by_name(dev->name);
586
Simon Glasse50c4d12015-04-05 16:07:40 -0600587 return dw_phy_init(priv, dev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530588}
Simon Glass90e627b2015-04-05 16:07:41 -0600589#endif
590
591#ifdef CONFIG_DM_ETH
592static int designware_eth_start(struct udevice *dev)
593{
594 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100595 struct dw_eth_dev *priv = dev_get_priv(dev);
596 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600597
Simon Glassc154fc02017-01-11 11:46:10 +0100598 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100599 if (ret)
600 return ret;
601 ret = designware_eth_enable(priv);
602 if (ret)
603 return ret;
604
605 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600606}
607
Simon Glassc154fc02017-01-11 11:46:10 +0100608int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600609{
610 struct dw_eth_dev *priv = dev_get_priv(dev);
611
612 return _dw_eth_send(priv, packet, length);
613}
614
Simon Glassc154fc02017-01-11 11:46:10 +0100615int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600616{
617 struct dw_eth_dev *priv = dev_get_priv(dev);
618
619 return _dw_eth_recv(priv, packetp);
620}
621
Simon Glassc154fc02017-01-11 11:46:10 +0100622int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600623{
624 struct dw_eth_dev *priv = dev_get_priv(dev);
625
626 return _dw_free_pkt(priv);
627}
628
Simon Glassc154fc02017-01-11 11:46:10 +0100629void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600630{
631 struct dw_eth_dev *priv = dev_get_priv(dev);
632
633 return _dw_eth_halt(priv);
634}
635
Simon Glassc154fc02017-01-11 11:46:10 +0100636int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600637{
638 struct eth_pdata *pdata = dev_get_platdata(dev);
639 struct dw_eth_dev *priv = dev_get_priv(dev);
640
641 return _dw_write_hwaddr(priv, pdata->enetaddr);
642}
643
Bin Menged89bd72015-09-11 03:24:35 -0700644static int designware_eth_bind(struct udevice *dev)
645{
646#ifdef CONFIG_DM_PCI
647 static int num_cards;
648 char name[20];
649
650 /* Create a unique device name for PCI type devices */
651 if (device_is_on_pci_bus(dev)) {
652 sprintf(name, "eth_designware#%u", num_cards++);
653 device_set_name(dev, name);
654 }
655#endif
656
657 return 0;
658}
659
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100660int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600661{
662 struct eth_pdata *pdata = dev_get_platdata(dev);
663 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengdfc90f52015-09-03 05:37:29 -0700664 u32 iobase = pdata->iobase;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200665 ulong ioaddr;
Simon Glass90e627b2015-04-05 16:07:41 -0600666 int ret;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100667#ifdef CONFIG_CLK
668 int i, err, clock_nb;
669
670 priv->clock_count = 0;
671 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
672 if (clock_nb > 0) {
673 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
674 GFP_KERNEL);
675 if (!priv->clocks)
676 return -ENOMEM;
677
678 for (i = 0; i < clock_nb; i++) {
679 err = clk_get_by_index(dev, i, &priv->clocks[i]);
680 if (err < 0)
681 break;
682
683 err = clk_enable(&priv->clocks[i]);
Eugeniy Paltsev11e754e2018-02-06 17:12:09 +0300684 if (err && err != -ENOSYS && err != -ENOTSUPP) {
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100685 pr_err("failed to enable clock %d\n", i);
686 clk_free(&priv->clocks[i]);
687 goto clk_err;
688 }
689 priv->clock_count++;
690 }
691 } else if (clock_nb != -ENOENT) {
692 pr_err("failed to get clock phandle(%d)\n", clock_nb);
693 return clock_nb;
694 }
695#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600696
Jacob Chen7ceacea2017-03-27 16:54:17 +0800697#if defined(CONFIG_DM_REGULATOR)
698 struct udevice *phy_supply;
699
700 ret = device_get_supply_regulator(dev, "phy-supply",
701 &phy_supply);
702 if (ret) {
703 debug("%s: No phy supply\n", dev->name);
704 } else {
705 ret = regulator_set_enable(phy_supply, true);
706 if (ret) {
707 puts("Error enabling phy supply\n");
708 return ret;
709 }
710 }
711#endif
712
Bin Menged89bd72015-09-11 03:24:35 -0700713#ifdef CONFIG_DM_PCI
714 /*
715 * If we are on PCI bus, either directly attached to a PCI root port,
716 * or via a PCI bridge, fill in platdata before we probe the hardware.
717 */
718 if (device_is_on_pci_bus(dev)) {
Bin Menged89bd72015-09-11 03:24:35 -0700719 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
720 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6c3300c2016-02-02 05:58:00 -0800721 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Menged89bd72015-09-11 03:24:35 -0700722
723 pdata->iobase = iobase;
724 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
725 }
726#endif
727
Bin Mengdfc90f52015-09-03 05:37:29 -0700728 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200729 ioaddr = iobase;
730 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
731 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600732 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300733 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600734
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100735 dw_mdio_init(dev->name, dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600736 priv->bus = miiphy_get_dev_by_name(dev->name);
737
738 ret = dw_phy_init(priv, dev);
739 debug("%s, ret=%d\n", __func__, ret);
740
741 return ret;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100742
743#ifdef CONFIG_CLK
744clk_err:
745 ret = clk_release_all(priv->clocks, priv->clock_count);
746 if (ret)
747 pr_err("failed to disable all clocks\n");
748
749 return err;
750#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600751}
752
Bin Mengf0f02772015-10-07 21:32:38 -0700753static int designware_eth_remove(struct udevice *dev)
754{
755 struct dw_eth_dev *priv = dev_get_priv(dev);
756
757 free(priv->phydev);
758 mdio_unregister(priv->bus);
759 mdio_free(priv->bus);
760
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100761#ifdef CONFIG_CLK
762 return clk_release_all(priv->clocks, priv->clock_count);
763#else
Bin Mengf0f02772015-10-07 21:32:38 -0700764 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100765#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700766}
767
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100768const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600769 .start = designware_eth_start,
770 .send = designware_eth_send,
771 .recv = designware_eth_recv,
772 .free_pkt = designware_eth_free_pkt,
773 .stop = designware_eth_stop,
774 .write_hwaddr = designware_eth_write_hwaddr,
775};
776
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100777int designware_eth_ofdata_to_platdata(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600778{
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100779 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300780#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100781 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300782#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100783 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass90e627b2015-04-05 16:07:41 -0600784 const char *phy_mode;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300785#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100786 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300787#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100788 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600789
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200790 pdata->iobase = dev_read_addr(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600791 pdata->phy_interface = -1;
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200792 phy_mode = dev_read_string(dev, "phy-mode");
Simon Glass90e627b2015-04-05 16:07:41 -0600793 if (phy_mode)
794 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
795 if (pdata->phy_interface == -1) {
796 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
797 return -EINVAL;
798 }
799
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200800 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300801
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300802#ifdef CONFIG_DM_GPIO
Philipp Tomsich150005b2017-06-07 18:46:01 +0200803 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100804 reset_flags |= GPIOD_ACTIVE_LOW;
805
806 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
807 &priv->reset_gpio, reset_flags);
808 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200809 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
810 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100811 } else if (ret == -ENOENT) {
812 ret = 0;
813 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300814#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100815
816 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600817}
818
819static const struct udevice_id designware_eth_ids[] = {
820 { .compatible = "allwinner,sun7i-a20-gmac" },
Marek Vasutfcab73c2015-07-25 18:38:44 +0200821 { .compatible = "altr,socfpga-stmmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200822 { .compatible = "amlogic,meson6-dwmac" },
Heiner Kallweit83fdbe42017-01-27 21:25:59 +0100823 { .compatible = "amlogic,meson-gx-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100824 { .compatible = "st,stm32-dwmac" },
Simon Glass90e627b2015-04-05 16:07:41 -0600825 { }
826};
827
Marek Vasut7e7e6172015-07-25 18:42:34 +0200828U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600829 .name = "eth_designware",
830 .id = UCLASS_ETH,
831 .of_match = designware_eth_ids,
832 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Menged89bd72015-09-11 03:24:35 -0700833 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600834 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700835 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600836 .ops = &designware_eth_ops,
837 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100838 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600839 .flags = DM_FLAG_ALLOC_PRIV_DMA,
840};
Bin Menged89bd72015-09-11 03:24:35 -0700841
842static struct pci_device_id supported[] = {
843 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
844 { }
845};
846
847U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass90e627b2015-04-05 16:07:41 -0600848#endif