blob: 6d5307128d866ca23f9e9fbbc0f8945aa12f0460 [file] [log] [blame]
Vipin KUMAR1f873122010-06-29 10:53:34 +05301/*
2 * (C) Copyright 2010
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR1f873122010-06-29 10:53:34 +05306 */
7
8/*
Simon Glasse50c4d12015-04-05 16:07:40 -06009 * Designware ethernet IP driver for U-Boot
Vipin KUMAR1f873122010-06-29 10:53:34 +053010 */
11
12#include <common.h>
Patrice Chotardeebcf8c2017-11-29 09:06:11 +010013#include <clk.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>
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
Simon Glass90e627b2015-04-05 16:07:41 -060026DECLARE_GLOBAL_DATA_PTR;
27
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040028static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
29{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010030#ifdef CONFIG_DM_ETH
31 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
32 struct eth_mac_regs *mac_p = priv->mac_regs_p;
33#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040034 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010035#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040036 ulong start;
37 u16 miiaddr;
38 int timeout = CONFIG_MDIO_TIMEOUT;
39
40 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
41 ((reg << MIIREGSHIFT) & MII_REGMSK);
42
43 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
44
45 start = get_timer(0);
46 while (get_timer(start) < timeout) {
47 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
48 return readl(&mac_p->miidata);
49 udelay(10);
50 };
51
Simon Glasse50c4d12015-04-05 16:07:40 -060052 return -ETIMEDOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040053}
54
55static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
56 u16 val)
57{
Sjoerd Simons6eb44622016-02-28 22:24:55 +010058#ifdef CONFIG_DM_ETH
59 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
60 struct eth_mac_regs *mac_p = priv->mac_regs_p;
61#else
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040062 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons6eb44622016-02-28 22:24:55 +010063#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040064 ulong start;
65 u16 miiaddr;
Simon Glasse50c4d12015-04-05 16:07:40 -060066 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +040067
68 writel(val, &mac_p->miidata);
69 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
70 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
71
72 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
73
74 start = get_timer(0);
75 while (get_timer(start) < timeout) {
76 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
77 ret = 0;
78 break;
79 }
80 udelay(10);
81 };
82
83 return ret;
84}
85
Alexey Brodkin57a37bc2016-06-27 13:17:51 +030086#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +010087static int dw_mdio_reset(struct mii_dev *bus)
88{
89 struct udevice *dev = bus->priv;
90 struct dw_eth_dev *priv = dev_get_priv(dev);
91 struct dw_eth_pdata *pdata = dev_get_platdata(dev);
92 int ret;
93
94 if (!dm_gpio_is_valid(&priv->reset_gpio))
95 return 0;
96
97 /* reset the phy */
98 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
99 if (ret)
100 return ret;
101
102 udelay(pdata->reset_delays[0]);
103
104 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
105 if (ret)
106 return ret;
107
108 udelay(pdata->reset_delays[1]);
109
110 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
111 if (ret)
112 return ret;
113
114 udelay(pdata->reset_delays[2]);
115
116 return 0;
117}
118#endif
119
120static int dw_mdio_init(const char *name, void *priv)
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400121{
122 struct mii_dev *bus = mdio_alloc();
123
124 if (!bus) {
125 printf("Failed to allocate MDIO bus\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600126 return -ENOMEM;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400127 }
128
129 bus->read = dw_mdio_read;
130 bus->write = dw_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000131 snprintf(bus->name, sizeof(bus->name), "%s", name);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300132#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100133 bus->reset = dw_mdio_reset;
134#endif
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400135
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100136 bus->priv = priv;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400137
138 return mdio_register(bus);
139}
Vipin Kumarb6c59992012-03-26 00:09:56 +0000140
Simon Glasse50c4d12015-04-05 16:07:40 -0600141static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530142{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530143 struct eth_dma_regs *dma_p = priv->dma_regs_p;
144 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
145 char *txbuffs = &priv->txbuffs[0];
146 struct dmamacdescr *desc_p;
147 u32 idx;
148
149 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
150 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200151 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
152 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530153
154#if defined(CONFIG_DW_ALTDESCRIPTOR)
155 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut4ab539a2015-12-20 03:59:23 +0100156 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
157 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530158 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
159
160 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
161 desc_p->dmamac_cntl = 0;
162 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
163#else
164 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
165 desc_p->txrx_status = 0;
166#endif
167 }
168
169 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200170 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530171
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400172 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200173 flush_dcache_range((ulong)priv->tx_mac_descrtable,
174 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400175 sizeof(priv->tx_mac_descrtable));
176
Vipin KUMAR1f873122010-06-29 10:53:34 +0530177 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400178 priv->tx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530179}
180
Simon Glasse50c4d12015-04-05 16:07:40 -0600181static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530182{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530183 struct eth_dma_regs *dma_p = priv->dma_regs_p;
184 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
185 char *rxbuffs = &priv->rxbuffs[0];
186 struct dmamacdescr *desc_p;
187 u32 idx;
188
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400189 /* Before passing buffers to GMAC we need to make sure zeros
190 * written there right after "priv" structure allocation were
191 * flushed into RAM.
192 * Otherwise there's a chance to get some of them flushed in RAM when
193 * GMAC is already pushing data to RAM via DMA. This way incoming from
194 * GMAC data will be corrupted. */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200195 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400196
Vipin KUMAR1f873122010-06-29 10:53:34 +0530197 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
198 desc_p = &desc_table_p[idx];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200199 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
200 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530201
202 desc_p->dmamac_cntl =
Marek Vasut4ab539a2015-12-20 03:59:23 +0100203 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530204 DESC_RXCTRL_RXCHAIN;
205
206 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
207 }
208
209 /* Correcting the last pointer of the chain */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200210 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR1f873122010-06-29 10:53:34 +0530211
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400212 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200213 flush_dcache_range((ulong)priv->rx_mac_descrtable,
214 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400215 sizeof(priv->rx_mac_descrtable));
216
Vipin KUMAR1f873122010-06-29 10:53:34 +0530217 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin4695ddd2014-01-13 13:28:38 +0400218 priv->rx_currdescnum = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530219}
220
Simon Glasse50c4d12015-04-05 16:07:40 -0600221static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530222{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400223 struct eth_mac_regs *mac_p = priv->mac_regs_p;
224 u32 macid_lo, macid_hi;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400225
226 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
227 (mac_id[3] << 24);
228 macid_hi = mac_id[4] + (mac_id[5] << 8);
229
230 writel(macid_hi, &mac_p->macaddr0hi);
231 writel(macid_lo, &mac_p->macaddr0lo);
232
233 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530234}
235
Simon Glass4afa85e2017-01-11 11:46:08 +0100236static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
237 struct phy_device *phydev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530238{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400239 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530240
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400241 if (!phydev->link) {
242 printf("%s: No link.\n", phydev->dev->name);
Simon Glass4afa85e2017-01-11 11:46:08 +0100243 return 0;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400244 }
Vipin KUMAR1f873122010-06-29 10:53:34 +0530245
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400246 if (phydev->speed != 1000)
247 conf |= MII_PORTSELECT;
Alexey Brodkina5e88192016-01-13 16:59:36 +0300248 else
249 conf &= ~MII_PORTSELECT;
Vipin Kumarf567e412012-12-13 17:22:51 +0530250
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400251 if (phydev->speed == 100)
252 conf |= FES_100;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530253
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400254 if (phydev->duplex)
255 conf |= FULLDPLXMODE;
Amit Virdi470e8842012-03-26 00:09:59 +0000256
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400257 writel(conf, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530258
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400259 printf("Speed: %d, %s duplex%s\n", phydev->speed,
260 (phydev->duplex) ? "full" : "half",
261 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Simon Glass4afa85e2017-01-11 11:46:08 +0100262
263 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530264}
265
Simon Glasse50c4d12015-04-05 16:07:40 -0600266static void _dw_eth_halt(struct dw_eth_dev *priv)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530267{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530268 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400269 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530270
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400271 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
272 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530273
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400274 phy_shutdown(priv->phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530275}
276
Simon Glassc154fc02017-01-11 11:46:10 +0100277int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530278{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530279 struct eth_mac_regs *mac_p = priv->mac_regs_p;
280 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400281 unsigned int start;
Simon Glasse50c4d12015-04-05 16:07:40 -0600282 int ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530283
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400284 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumarb6c59992012-03-26 00:09:56 +0000285
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400286 start = get_timer(0);
287 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300288 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
289 printf("DMA reset timeout\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600290 return -ETIMEDOUT;
Alexey Brodkin71eccc32015-01-13 17:10:24 +0300291 }
Stefan Roesed27e86c2012-05-07 12:04:25 +0200292
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400293 mdelay(100);
294 };
Vipin KUMAR1f873122010-06-29 10:53:34 +0530295
Bin Meng2ddfa2a2015-06-15 18:40:19 +0800296 /*
297 * Soft reset above clears HW address registers.
298 * So we have to set it here once again.
299 */
300 _dw_write_hwaddr(priv, enetaddr);
301
Simon Glasse50c4d12015-04-05 16:07:40 -0600302 rx_descs_init(priv);
303 tx_descs_init(priv);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530304
Ian Campbell4164b742014-05-08 22:26:35 +0100305 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530306
Sonic Zhangb917b622015-01-29 14:38:50 +0800307#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400308 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
309 &dma_p->opmode);
Sonic Zhangb917b622015-01-29 14:38:50 +0800310#else
311 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
312 &dma_p->opmode);
313#endif
Vipin KUMAR1f873122010-06-29 10:53:34 +0530314
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400315 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin Kumar7443d602012-05-07 13:06:44 +0530316
Sonic Zhang962c95c2015-01-29 13:37:31 +0800317#ifdef CONFIG_DW_AXI_BURST_LEN
318 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
319#endif
320
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400321 /* Start up the PHY */
Simon Glasse50c4d12015-04-05 16:07:40 -0600322 ret = phy_startup(priv->phydev);
323 if (ret) {
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400324 printf("Could not initialize PHY %s\n",
325 priv->phydev->dev->name);
Simon Glasse50c4d12015-04-05 16:07:40 -0600326 return ret;
Vipin Kumar7443d602012-05-07 13:06:44 +0530327 }
328
Simon Glass4afa85e2017-01-11 11:46:08 +0100329 ret = dw_adjust_link(priv, mac_p, priv->phydev);
330 if (ret)
331 return ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530332
Simon Glass3240e942017-01-11 11:46:09 +0100333 return 0;
334}
335
Simon Glassc154fc02017-01-11 11:46:10 +0100336int designware_eth_enable(struct dw_eth_dev *priv)
Simon Glass3240e942017-01-11 11:46:09 +0100337{
338 struct eth_mac_regs *mac_p = priv->mac_regs_p;
339
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400340 if (!priv->phydev->link)
Simon Glasse50c4d12015-04-05 16:07:40 -0600341 return -EIO;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530342
Armando Visconti038c9d52012-03-26 00:09:55 +0000343 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530344
345 return 0;
346}
347
Florian Fainelli65f686b2017-12-09 14:59:55 -0800348#define ETH_ZLEN 60
349
Simon Glasse50c4d12015-04-05 16:07:40 -0600350static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530351{
Vipin KUMAR1f873122010-06-29 10:53:34 +0530352 struct eth_dma_regs *dma_p = priv->dma_regs_p;
353 u32 desc_num = priv->tx_currdescnum;
354 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200355 ulong desc_start = (ulong)desc_p;
356 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200357 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200358 ulong data_start = desc_p->dmamac_addr;
359 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell0e690fd2014-05-08 22:26:33 +0100360 /*
361 * Strictly we only need to invalidate the "txrx_status" field
362 * for the following check, but on some platforms we cannot
Marek Vasut15193042014-09-15 01:05:23 +0200363 * invalidate only 4 bytes, so we flush the entire descriptor,
364 * which is 16 bytes in total. This is safe because the
365 * individual descriptors in the array are each aligned to
366 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell0e690fd2014-05-08 22:26:33 +0100367 */
Marek Vasut15193042014-09-15 01:05:23 +0200368 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400369
Vipin KUMAR1f873122010-06-29 10:53:34 +0530370 /* Check if the descriptor is owned by CPU */
371 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
372 printf("CPU not owner of tx frame\n");
Simon Glasse50c4d12015-04-05 16:07:40 -0600373 return -EPERM;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530374 }
375
Florian Fainelli65f686b2017-12-09 14:59:55 -0800376 length = max(length, ETH_ZLEN);
377
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200378 memcpy((void *)data_start, packet, length);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530379
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400380 /* Flush data to be sent */
Marek Vasut15193042014-09-15 01:05:23 +0200381 flush_dcache_range(data_start, data_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400382
Vipin KUMAR1f873122010-06-29 10:53:34 +0530383#if defined(CONFIG_DW_ALTDESCRIPTOR)
384 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Marek Vasut4ab539a2015-12-20 03:59:23 +0100385 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
Vipin KUMAR1f873122010-06-29 10:53:34 +0530386 DESC_TXCTRL_SIZE1MASK;
387
388 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
389 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
390#else
Marek Vasut4ab539a2015-12-20 03:59:23 +0100391 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
392 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
Vipin KUMAR1f873122010-06-29 10:53:34 +0530393 DESC_TXCTRL_TXFIRST;
394
395 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
396#endif
397
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400398 /* Flush modified buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200399 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400400
Vipin KUMAR1f873122010-06-29 10:53:34 +0530401 /* Test the wrap-around condition. */
402 if (++desc_num >= CONFIG_TX_DESCR_NUM)
403 desc_num = 0;
404
405 priv->tx_currdescnum = desc_num;
406
407 /* Start the transmission */
408 writel(POLL_DATA, &dma_p->txpolldemand);
409
410 return 0;
411}
412
Simon Glass90e627b2015-04-05 16:07:41 -0600413static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530414{
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400415 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530416 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass90e627b2015-04-05 16:07:41 -0600417 int length = -EAGAIN;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200418 ulong desc_start = (ulong)desc_p;
419 ulong desc_end = desc_start +
Marek Vasut15193042014-09-15 01:05:23 +0200420 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200421 ulong data_start = desc_p->dmamac_addr;
422 ulong data_end;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530423
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400424 /* Invalidate entire buffer descriptor */
Marek Vasut15193042014-09-15 01:05:23 +0200425 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400426
427 status = desc_p->txrx_status;
428
Vipin KUMAR1f873122010-06-29 10:53:34 +0530429 /* Check if the owner is the CPU */
430 if (!(status & DESC_RXSTS_OWNBYDMA)) {
431
Marek Vasut4ab539a2015-12-20 03:59:23 +0100432 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR1f873122010-06-29 10:53:34 +0530433 DESC_RXSTS_FRMLENSHFT;
434
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400435 /* Invalidate received data */
Marek Vasut15193042014-09-15 01:05:23 +0200436 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
437 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200438 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Simon Glass90e627b2015-04-05 16:07:41 -0600439 }
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400440
Simon Glass90e627b2015-04-05 16:07:41 -0600441 return length;
442}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530443
Simon Glass90e627b2015-04-05 16:07:41 -0600444static int _dw_free_pkt(struct dw_eth_dev *priv)
445{
446 u32 desc_num = priv->rx_currdescnum;
447 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200448 ulong desc_start = (ulong)desc_p;
449 ulong desc_end = desc_start +
Simon Glass90e627b2015-04-05 16:07:41 -0600450 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530451
Simon Glass90e627b2015-04-05 16:07:41 -0600452 /*
453 * Make the current descriptor valid again and go to
454 * the next one
455 */
456 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
Alexey Brodkin0d3b22e2014-01-22 20:49:09 +0400457
Simon Glass90e627b2015-04-05 16:07:41 -0600458 /* Flush only status field - others weren't changed */
459 flush_dcache_range(desc_start, desc_end);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530460
Simon Glass90e627b2015-04-05 16:07:41 -0600461 /* Test the wrap-around condition. */
462 if (++desc_num >= CONFIG_RX_DESCR_NUM)
463 desc_num = 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530464 priv->rx_currdescnum = desc_num;
465
Simon Glass90e627b2015-04-05 16:07:41 -0600466 return 0;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530467}
468
Simon Glasse50c4d12015-04-05 16:07:40 -0600469static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530470{
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400471 struct phy_device *phydev;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300472 int mask = 0xffffffff, ret;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530473
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400474#ifdef CONFIG_PHY_ADDR
475 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530476#endif
477
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400478 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
479 if (!phydev)
Simon Glasse50c4d12015-04-05 16:07:40 -0600480 return -ENODEV;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530481
Ian Campbell7d555502014-04-28 20:14:05 +0100482 phy_connect_dev(phydev, dev);
483
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400484 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300485 if (priv->max_speed) {
486 ret = phy_set_supported(phydev, priv->max_speed);
487 if (ret)
488 return ret;
489 }
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400490 phydev->advertising = phydev->supported;
Vipin KUMAR1f873122010-06-29 10:53:34 +0530491
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400492 priv->phydev = phydev;
493 phy_config(phydev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530494
Simon Glasse50c4d12015-04-05 16:07:40 -0600495 return 0;
496}
497
Simon Glass90e627b2015-04-05 16:07:41 -0600498#ifndef CONFIG_DM_ETH
Simon Glasse50c4d12015-04-05 16:07:40 -0600499static int dw_eth_init(struct eth_device *dev, bd_t *bis)
500{
Simon Glass3240e942017-01-11 11:46:09 +0100501 int ret;
502
Simon Glassc154fc02017-01-11 11:46:10 +0100503 ret = designware_eth_init(dev->priv, dev->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100504 if (!ret)
505 ret = designware_eth_enable(dev->priv);
506
507 return ret;
Simon Glasse50c4d12015-04-05 16:07:40 -0600508}
509
510static int dw_eth_send(struct eth_device *dev, void *packet, int length)
511{
512 return _dw_eth_send(dev->priv, packet, length);
513}
514
515static int dw_eth_recv(struct eth_device *dev)
516{
Simon Glass90e627b2015-04-05 16:07:41 -0600517 uchar *packet;
518 int length;
519
520 length = _dw_eth_recv(dev->priv, &packet);
521 if (length == -EAGAIN)
522 return 0;
523 net_process_received_packet(packet, length);
524
525 _dw_free_pkt(dev->priv);
526
527 return 0;
Simon Glasse50c4d12015-04-05 16:07:40 -0600528}
529
530static void dw_eth_halt(struct eth_device *dev)
531{
532 return _dw_eth_halt(dev->priv);
533}
534
535static int dw_write_hwaddr(struct eth_device *dev)
536{
537 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530538}
Vipin KUMAR1f873122010-06-29 10:53:34 +0530539
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400540int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR1f873122010-06-29 10:53:34 +0530541{
542 struct eth_device *dev;
543 struct dw_eth_dev *priv;
544
545 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
546 if (!dev)
547 return -ENOMEM;
548
549 /*
550 * Since the priv structure contains the descriptors which need a strict
551 * buswidth alignment, memalign is used to allocate memory
552 */
Ian Campbell07c92fc2014-05-08 22:26:32 +0100553 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
554 sizeof(struct dw_eth_dev));
Vipin KUMAR1f873122010-06-29 10:53:34 +0530555 if (!priv) {
556 free(dev);
557 return -ENOMEM;
558 }
559
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200560 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
561 printf("designware: buffers are outside DMA memory\n");
562 return -EINVAL;
563 }
564
Vipin KUMAR1f873122010-06-29 10:53:34 +0530565 memset(dev, 0, sizeof(struct eth_device));
566 memset(priv, 0, sizeof(struct dw_eth_dev));
567
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400568 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530569 dev->iobase = (int)base_addr;
570 dev->priv = priv;
571
Vipin KUMAR1f873122010-06-29 10:53:34 +0530572 priv->dev = dev;
573 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
574 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
575 DW_DMA_BASE_OFFSET);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530576
Vipin KUMAR1f873122010-06-29 10:53:34 +0530577 dev->init = dw_eth_init;
578 dev->send = dw_eth_send;
579 dev->recv = dw_eth_recv;
580 dev->halt = dw_eth_halt;
581 dev->write_hwaddr = dw_write_hwaddr;
582
583 eth_register(dev);
584
Alexey Brodkin9a0b1302014-01-22 20:54:06 +0400585 priv->interface = interface;
586
587 dw_mdio_init(dev->name, priv->mac_regs_p);
588 priv->bus = miiphy_get_dev_by_name(dev->name);
589
Simon Glasse50c4d12015-04-05 16:07:40 -0600590 return dw_phy_init(priv, dev);
Vipin KUMAR1f873122010-06-29 10:53:34 +0530591}
Simon Glass90e627b2015-04-05 16:07:41 -0600592#endif
593
594#ifdef CONFIG_DM_ETH
595static int designware_eth_start(struct udevice *dev)
596{
597 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glass3240e942017-01-11 11:46:09 +0100598 struct dw_eth_dev *priv = dev_get_priv(dev);
599 int ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600600
Simon Glassc154fc02017-01-11 11:46:10 +0100601 ret = designware_eth_init(priv, pdata->enetaddr);
Simon Glass3240e942017-01-11 11:46:09 +0100602 if (ret)
603 return ret;
604 ret = designware_eth_enable(priv);
605 if (ret)
606 return ret;
607
608 return 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600609}
610
Simon Glassc154fc02017-01-11 11:46:10 +0100611int designware_eth_send(struct udevice *dev, void *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600612{
613 struct dw_eth_dev *priv = dev_get_priv(dev);
614
615 return _dw_eth_send(priv, packet, length);
616}
617
Simon Glassc154fc02017-01-11 11:46:10 +0100618int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass90e627b2015-04-05 16:07:41 -0600619{
620 struct dw_eth_dev *priv = dev_get_priv(dev);
621
622 return _dw_eth_recv(priv, packetp);
623}
624
Simon Glassc154fc02017-01-11 11:46:10 +0100625int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
Simon Glass90e627b2015-04-05 16:07:41 -0600626{
627 struct dw_eth_dev *priv = dev_get_priv(dev);
628
629 return _dw_free_pkt(priv);
630}
631
Simon Glassc154fc02017-01-11 11:46:10 +0100632void designware_eth_stop(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600633{
634 struct dw_eth_dev *priv = dev_get_priv(dev);
635
636 return _dw_eth_halt(priv);
637}
638
Simon Glassc154fc02017-01-11 11:46:10 +0100639int designware_eth_write_hwaddr(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600640{
641 struct eth_pdata *pdata = dev_get_platdata(dev);
642 struct dw_eth_dev *priv = dev_get_priv(dev);
643
644 return _dw_write_hwaddr(priv, pdata->enetaddr);
645}
646
Bin Menged89bd72015-09-11 03:24:35 -0700647static int designware_eth_bind(struct udevice *dev)
648{
649#ifdef CONFIG_DM_PCI
650 static int num_cards;
651 char name[20];
652
653 /* Create a unique device name for PCI type devices */
654 if (device_is_on_pci_bus(dev)) {
655 sprintf(name, "eth_designware#%u", num_cards++);
656 device_set_name(dev, name);
657 }
658#endif
659
660 return 0;
661}
662
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100663int designware_eth_probe(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600664{
665 struct eth_pdata *pdata = dev_get_platdata(dev);
666 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengdfc90f52015-09-03 05:37:29 -0700667 u32 iobase = pdata->iobase;
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200668 ulong ioaddr;
Simon Glass90e627b2015-04-05 16:07:41 -0600669 int ret;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100670#ifdef CONFIG_CLK
671 int i, err, clock_nb;
672
673 priv->clock_count = 0;
674 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
675 if (clock_nb > 0) {
676 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
677 GFP_KERNEL);
678 if (!priv->clocks)
679 return -ENOMEM;
680
681 for (i = 0; i < clock_nb; i++) {
682 err = clk_get_by_index(dev, i, &priv->clocks[i]);
683 if (err < 0)
684 break;
685
686 err = clk_enable(&priv->clocks[i]);
687 if (err) {
688 pr_err("failed to enable clock %d\n", i);
689 clk_free(&priv->clocks[i]);
690 goto clk_err;
691 }
692 priv->clock_count++;
693 }
694 } else if (clock_nb != -ENOENT) {
695 pr_err("failed to get clock phandle(%d)\n", clock_nb);
696 return clock_nb;
697 }
698#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600699
Jacob Chen7ceacea2017-03-27 16:54:17 +0800700#if defined(CONFIG_DM_REGULATOR)
701 struct udevice *phy_supply;
702
703 ret = device_get_supply_regulator(dev, "phy-supply",
704 &phy_supply);
705 if (ret) {
706 debug("%s: No phy supply\n", dev->name);
707 } else {
708 ret = regulator_set_enable(phy_supply, true);
709 if (ret) {
710 puts("Error enabling phy supply\n");
711 return ret;
712 }
713 }
714#endif
715
Bin Menged89bd72015-09-11 03:24:35 -0700716#ifdef CONFIG_DM_PCI
717 /*
718 * If we are on PCI bus, either directly attached to a PCI root port,
719 * or via a PCI bridge, fill in platdata before we probe the hardware.
720 */
721 if (device_is_on_pci_bus(dev)) {
Bin Menged89bd72015-09-11 03:24:35 -0700722 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
723 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6c3300c2016-02-02 05:58:00 -0800724 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Menged89bd72015-09-11 03:24:35 -0700725
726 pdata->iobase = iobase;
727 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
728 }
729#endif
730
Bin Mengdfc90f52015-09-03 05:37:29 -0700731 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani3bfa65c2016-05-08 08:30:15 +0200732 ioaddr = iobase;
733 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
734 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass90e627b2015-04-05 16:07:41 -0600735 priv->interface = pdata->phy_interface;
Alexey Brodkina3d38742016-01-13 16:59:37 +0300736 priv->max_speed = pdata->max_speed;
Simon Glass90e627b2015-04-05 16:07:41 -0600737
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100738 dw_mdio_init(dev->name, dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600739 priv->bus = miiphy_get_dev_by_name(dev->name);
740
741 ret = dw_phy_init(priv, dev);
742 debug("%s, ret=%d\n", __func__, ret);
743
744 return ret;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100745
746#ifdef CONFIG_CLK
747clk_err:
748 ret = clk_release_all(priv->clocks, priv->clock_count);
749 if (ret)
750 pr_err("failed to disable all clocks\n");
751
752 return err;
753#endif
Simon Glass90e627b2015-04-05 16:07:41 -0600754}
755
Bin Mengf0f02772015-10-07 21:32:38 -0700756static int designware_eth_remove(struct udevice *dev)
757{
758 struct dw_eth_dev *priv = dev_get_priv(dev);
759
760 free(priv->phydev);
761 mdio_unregister(priv->bus);
762 mdio_free(priv->bus);
763
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100764#ifdef CONFIG_CLK
765 return clk_release_all(priv->clocks, priv->clock_count);
766#else
Bin Mengf0f02772015-10-07 21:32:38 -0700767 return 0;
Patrice Chotardeebcf8c2017-11-29 09:06:11 +0100768#endif
Bin Mengf0f02772015-10-07 21:32:38 -0700769}
770
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100771const struct eth_ops designware_eth_ops = {
Simon Glass90e627b2015-04-05 16:07:41 -0600772 .start = designware_eth_start,
773 .send = designware_eth_send,
774 .recv = designware_eth_recv,
775 .free_pkt = designware_eth_free_pkt,
776 .stop = designware_eth_stop,
777 .write_hwaddr = designware_eth_write_hwaddr,
778};
779
Sjoerd Simons9cf8fd02017-01-11 11:46:07 +0100780int designware_eth_ofdata_to_platdata(struct udevice *dev)
Simon Glass90e627b2015-04-05 16:07:41 -0600781{
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100782 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300783#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100784 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300785#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100786 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass90e627b2015-04-05 16:07:41 -0600787 const char *phy_mode;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300788#ifdef CONFIG_DM_GPIO
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100789 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300790#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100791 int ret = 0;
Simon Glass90e627b2015-04-05 16:07:41 -0600792
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200793 pdata->iobase = dev_read_addr(dev);
Simon Glass90e627b2015-04-05 16:07:41 -0600794 pdata->phy_interface = -1;
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200795 phy_mode = dev_read_string(dev, "phy-mode");
Simon Glass90e627b2015-04-05 16:07:41 -0600796 if (phy_mode)
797 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
798 if (pdata->phy_interface == -1) {
799 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
800 return -EINVAL;
801 }
802
Philipp Tomsichdcf87632017-09-11 22:04:13 +0200803 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
Alexey Brodkina3d38742016-01-13 16:59:37 +0300804
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300805#ifdef CONFIG_DM_GPIO
Philipp Tomsich150005b2017-06-07 18:46:01 +0200806 if (dev_read_bool(dev, "snps,reset-active-low"))
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100807 reset_flags |= GPIOD_ACTIVE_LOW;
808
809 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
810 &priv->reset_gpio, reset_flags);
811 if (ret == 0) {
Philipp Tomsich150005b2017-06-07 18:46:01 +0200812 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
813 dw_pdata->reset_delays, 3);
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100814 } else if (ret == -ENOENT) {
815 ret = 0;
816 }
Alexey Brodkin57a37bc2016-06-27 13:17:51 +0300817#endif
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100818
819 return ret;
Simon Glass90e627b2015-04-05 16:07:41 -0600820}
821
822static const struct udevice_id designware_eth_ids[] = {
823 { .compatible = "allwinner,sun7i-a20-gmac" },
Marek Vasutfcab73c2015-07-25 18:38:44 +0200824 { .compatible = "altr,socfpga-stmmac" },
Beniamino Galvani2fc2ef52016-08-16 11:49:50 +0200825 { .compatible = "amlogic,meson6-dwmac" },
Heiner Kallweit83fdbe42017-01-27 21:25:59 +0100826 { .compatible = "amlogic,meson-gx-dwmac" },
Michael Kurz812962b2017-01-22 16:04:27 +0100827 { .compatible = "st,stm32-dwmac" },
Simon Glass90e627b2015-04-05 16:07:41 -0600828 { }
829};
830
Marek Vasut7e7e6172015-07-25 18:42:34 +0200831U_BOOT_DRIVER(eth_designware) = {
Simon Glass90e627b2015-04-05 16:07:41 -0600832 .name = "eth_designware",
833 .id = UCLASS_ETH,
834 .of_match = designware_eth_ids,
835 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Menged89bd72015-09-11 03:24:35 -0700836 .bind = designware_eth_bind,
Simon Glass90e627b2015-04-05 16:07:41 -0600837 .probe = designware_eth_probe,
Bin Mengf0f02772015-10-07 21:32:38 -0700838 .remove = designware_eth_remove,
Simon Glass90e627b2015-04-05 16:07:41 -0600839 .ops = &designware_eth_ops,
840 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons6eb44622016-02-28 22:24:55 +0100841 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass90e627b2015-04-05 16:07:41 -0600842 .flags = DM_FLAG_ALLOC_PRIV_DMA,
843};
Bin Menged89bd72015-09-11 03:24:35 -0700844
845static struct pci_device_id supported[] = {
846 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
847 { }
848};
849
850U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass90e627b2015-04-05 16:07:41 -0600851#endif