blob: 8ba72e30b1de4305652483193e606680ea432090 [file] [log] [blame]
Vipin KUMAR5b1b1882010-06-29 10:53:34 +05301/*
2 * (C) Copyright 2010
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR5b1b1882010-06-29 10:53:34 +05306 */
7
8/*
Simon Glass64dcd252015-04-05 16:07:40 -06009 * Designware ethernet IP driver for U-Boot
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053010 */
11
12#include <common.h>
Simon Glass75577ba2015-04-05 16:07:41 -060013#include <dm.h>
Simon Glass64dcd252015-04-05 16:07:40 -060014#include <errno.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053015#include <miiphy.h>
16#include <malloc.h>
Bin Meng8b7ee662015-09-11 03:24:35 -070017#include <pci.h>
Stefan Roeseef760252012-05-07 12:04:25 +020018#include <linux/compiler.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053019#include <linux/err.h>
20#include <asm/io.h>
21#include "designware.h"
22
Simon Glass75577ba2015-04-05 16:07:41 -060023DECLARE_GLOBAL_DATA_PTR;
24
Alexey Brodkin92a190a2014-01-22 20:54:06 +040025static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
26{
Sjoerd Simons90b7fc92016-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 Brodkin92a190a2014-01-22 20:54:06 +040031 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010032#endif
Alexey Brodkin92a190a2014-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 Glass64dcd252015-04-05 16:07:40 -060049 return -ETIMEDOUT;
Alexey Brodkin92a190a2014-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 Simons90b7fc92016-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 Brodkin92a190a2014-01-22 20:54:06 +040059 struct eth_mac_regs *mac_p = bus->priv;
Sjoerd Simons90b7fc92016-02-28 22:24:55 +010060#endif
Alexey Brodkin92a190a2014-01-22 20:54:06 +040061 ulong start;
62 u16 miiaddr;
Simon Glass64dcd252015-04-05 16:07:40 -060063 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin92a190a2014-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 Brodkin66d027e2016-06-27 13:17:51 +030083#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons90b7fc92016-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 Brodkin92a190a2014-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 Glass64dcd252015-04-05 16:07:40 -0600123 return -ENOMEM;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400124 }
125
126 bus->read = dw_mdio_read;
127 bus->write = dw_mdio_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000128 snprintf(bus->name, sizeof(bus->name), "%s", name);
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300129#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100130 bus->reset = dw_mdio_reset;
131#endif
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400132
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100133 bus->priv = priv;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400134
135 return mdio_register(bus);
136}
Vipin Kumar13edd172012-03-26 00:09:56 +0000137
Simon Glass64dcd252015-04-05 16:07:40 -0600138static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530139{
Vipin KUMAR5b1b1882010-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 Galvani0e1a3e32016-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 KUMAR5b1b1882010-06-29 10:53:34 +0530150
151#if defined(CONFIG_DW_ALTDESCRIPTOR)
152 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut2b261092015-12-20 03:59:23 +0100153 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
154 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR5b1b1882010-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 Galvani0e1a3e32016-05-08 08:30:15 +0200167 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530168
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400169 /* Flush all Tx buffer descriptors at once */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200170 flush_dcache_range((ulong)priv->tx_mac_descrtable,
171 (ulong)priv->tx_mac_descrtable +
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400172 sizeof(priv->tx_mac_descrtable));
173
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530174 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400175 priv->tx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530176}
177
Simon Glass64dcd252015-04-05 16:07:40 -0600178static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530179{
Vipin KUMAR5b1b1882010-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 Brodkin50b0df82014-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 Galvani0e1a3e32016-05-08 08:30:15 +0200192 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400193
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530194 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
195 desc_p = &desc_table_p[idx];
Beniamino Galvani0e1a3e32016-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 KUMAR5b1b1882010-06-29 10:53:34 +0530198
199 desc_p->dmamac_cntl =
Marek Vasut2b261092015-12-20 03:59:23 +0100200 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR5b1b1882010-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 Galvani0e1a3e32016-05-08 08:30:15 +0200207 desc_p->dmamac_next = (ulong)&desc_table_p[0];
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530208
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400209 /* Flush all Rx buffer descriptors at once */
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200210 flush_dcache_range((ulong)priv->rx_mac_descrtable,
211 (ulong)priv->rx_mac_descrtable +
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400212 sizeof(priv->rx_mac_descrtable));
213
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530214 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400215 priv->rx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530216}
217
Simon Glass64dcd252015-04-05 16:07:40 -0600218static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530219{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530220 struct eth_mac_regs *mac_p = priv->mac_regs_p;
221 u32 macid_lo, macid_hi;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530222
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400223 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
224 (mac_id[3] << 24);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530225 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;
231}
232
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400233static void dw_adjust_link(struct eth_mac_regs *mac_p,
234 struct phy_device *phydev)
235{
236 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
237
238 if (!phydev->link) {
239 printf("%s: No link.\n", phydev->dev->name);
240 return;
241 }
242
243 if (phydev->speed != 1000)
244 conf |= MII_PORTSELECT;
Alexey Brodkinb884c3f2016-01-13 16:59:36 +0300245 else
246 conf &= ~MII_PORTSELECT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400247
248 if (phydev->speed == 100)
249 conf |= FES_100;
250
251 if (phydev->duplex)
252 conf |= FULLDPLXMODE;
253
254 writel(conf, &mac_p->conf);
255
256 printf("Speed: %d, %s duplex%s\n", phydev->speed,
257 (phydev->duplex) ? "full" : "half",
258 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
259}
260
Simon Glass64dcd252015-04-05 16:07:40 -0600261static void _dw_eth_halt(struct dw_eth_dev *priv)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400262{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400263 struct eth_mac_regs *mac_p = priv->mac_regs_p;
264 struct eth_dma_regs *dma_p = priv->dma_regs_p;
265
266 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
267 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
268
269 phy_shutdown(priv->phydev);
270}
271
Simon Glass64dcd252015-04-05 16:07:40 -0600272static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530273{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530274 struct eth_mac_regs *mac_p = priv->mac_regs_p;
275 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400276 unsigned int start;
Simon Glass64dcd252015-04-05 16:07:40 -0600277 int ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530278
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400279 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumar13edd172012-03-26 00:09:56 +0000280
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400281 start = get_timer(0);
282 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin875143f2015-01-13 17:10:24 +0300283 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
284 printf("DMA reset timeout\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600285 return -ETIMEDOUT;
Alexey Brodkin875143f2015-01-13 17:10:24 +0300286 }
Stefan Roeseef760252012-05-07 12:04:25 +0200287
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400288 mdelay(100);
289 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530290
Bin Mengf3edfd32015-06-15 18:40:19 +0800291 /*
292 * Soft reset above clears HW address registers.
293 * So we have to set it here once again.
294 */
295 _dw_write_hwaddr(priv, enetaddr);
296
Simon Glass64dcd252015-04-05 16:07:40 -0600297 rx_descs_init(priv);
298 tx_descs_init(priv);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530299
Ian Campbell49692c52014-05-08 22:26:35 +0100300 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530301
Sonic Zhangd2279222015-01-29 14:38:50 +0800302#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400303 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
304 &dma_p->opmode);
Sonic Zhangd2279222015-01-29 14:38:50 +0800305#else
306 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
307 &dma_p->opmode);
308#endif
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530309
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400310 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530311
Sonic Zhang2ddaf132015-01-29 13:37:31 +0800312#ifdef CONFIG_DW_AXI_BURST_LEN
313 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
314#endif
315
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400316 /* Start up the PHY */
Simon Glass64dcd252015-04-05 16:07:40 -0600317 ret = phy_startup(priv->phydev);
318 if (ret) {
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400319 printf("Could not initialize PHY %s\n",
320 priv->phydev->dev->name);
Simon Glass64dcd252015-04-05 16:07:40 -0600321 return ret;
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530322 }
323
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400324 dw_adjust_link(mac_p, priv->phydev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530325
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400326 if (!priv->phydev->link)
Simon Glass64dcd252015-04-05 16:07:40 -0600327 return -EIO;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530328
Armando Viscontiaa510052012-03-26 00:09:55 +0000329 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530330
331 return 0;
332}
333
Simon Glass64dcd252015-04-05 16:07:40 -0600334static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530335{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530336 struct eth_dma_regs *dma_p = priv->dma_regs_p;
337 u32 desc_num = priv->tx_currdescnum;
338 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200339 ulong desc_start = (ulong)desc_p;
340 ulong desc_end = desc_start +
Marek Vasut96cec172014-09-15 01:05:23 +0200341 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200342 ulong data_start = desc_p->dmamac_addr;
343 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell964ea7c2014-05-08 22:26:33 +0100344 /*
345 * Strictly we only need to invalidate the "txrx_status" field
346 * for the following check, but on some platforms we cannot
Marek Vasut96cec172014-09-15 01:05:23 +0200347 * invalidate only 4 bytes, so we flush the entire descriptor,
348 * which is 16 bytes in total. This is safe because the
349 * individual descriptors in the array are each aligned to
350 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell964ea7c2014-05-08 22:26:33 +0100351 */
Marek Vasut96cec172014-09-15 01:05:23 +0200352 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400353
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530354 /* Check if the descriptor is owned by CPU */
355 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
356 printf("CPU not owner of tx frame\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600357 return -EPERM;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530358 }
359
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200360 memcpy((void *)data_start, packet, length);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530361
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400362 /* Flush data to be sent */
Marek Vasut96cec172014-09-15 01:05:23 +0200363 flush_dcache_range(data_start, data_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400364
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530365#if defined(CONFIG_DW_ALTDESCRIPTOR)
366 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Marek Vasut2b261092015-12-20 03:59:23 +0100367 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530368 DESC_TXCTRL_SIZE1MASK;
369
370 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
371 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
372#else
Marek Vasut2b261092015-12-20 03:59:23 +0100373 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
374 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530375 DESC_TXCTRL_TXFIRST;
376
377 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
378#endif
379
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400380 /* Flush modified buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200381 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400382
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530383 /* Test the wrap-around condition. */
384 if (++desc_num >= CONFIG_TX_DESCR_NUM)
385 desc_num = 0;
386
387 priv->tx_currdescnum = desc_num;
388
389 /* Start the transmission */
390 writel(POLL_DATA, &dma_p->txpolldemand);
391
392 return 0;
393}
394
Simon Glass75577ba2015-04-05 16:07:41 -0600395static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530396{
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400397 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530398 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass75577ba2015-04-05 16:07:41 -0600399 int length = -EAGAIN;
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200400 ulong desc_start = (ulong)desc_p;
401 ulong desc_end = desc_start +
Marek Vasut96cec172014-09-15 01:05:23 +0200402 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200403 ulong data_start = desc_p->dmamac_addr;
404 ulong data_end;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530405
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400406 /* Invalidate entire buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200407 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400408
409 status = desc_p->txrx_status;
410
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530411 /* Check if the owner is the CPU */
412 if (!(status & DESC_RXSTS_OWNBYDMA)) {
413
Marek Vasut2b261092015-12-20 03:59:23 +0100414 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530415 DESC_RXSTS_FRMLENSHFT;
416
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400417 /* Invalidate received data */
Marek Vasut96cec172014-09-15 01:05:23 +0200418 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
419 invalidate_dcache_range(data_start, data_end);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200420 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530421 }
422
Simon Glass75577ba2015-04-05 16:07:41 -0600423 return length;
424}
425
426static int _dw_free_pkt(struct dw_eth_dev *priv)
427{
428 u32 desc_num = priv->rx_currdescnum;
429 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200430 ulong desc_start = (ulong)desc_p;
431 ulong desc_end = desc_start +
Simon Glass75577ba2015-04-05 16:07:41 -0600432 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
433
434 /*
435 * Make the current descriptor valid again and go to
436 * the next one
437 */
438 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
439
440 /* Flush only status field - others weren't changed */
441 flush_dcache_range(desc_start, desc_end);
442
443 /* Test the wrap-around condition. */
444 if (++desc_num >= CONFIG_RX_DESCR_NUM)
445 desc_num = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530446 priv->rx_currdescnum = desc_num;
447
Simon Glass75577ba2015-04-05 16:07:41 -0600448 return 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530449}
450
Simon Glass64dcd252015-04-05 16:07:40 -0600451static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530452{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400453 struct phy_device *phydev;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300454 int mask = 0xffffffff, ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530455
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400456#ifdef CONFIG_PHY_ADDR
457 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530458#endif
459
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400460 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
461 if (!phydev)
Simon Glass64dcd252015-04-05 16:07:40 -0600462 return -ENODEV;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530463
Ian Campbell15e82e52014-04-28 20:14:05 +0100464 phy_connect_dev(phydev, dev);
465
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400466 phydev->supported &= PHY_GBIT_FEATURES;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300467 if (priv->max_speed) {
468 ret = phy_set_supported(phydev, priv->max_speed);
469 if (ret)
470 return ret;
471 }
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400472 phydev->advertising = phydev->supported;
473
474 priv->phydev = phydev;
475 phy_config(phydev);
476
Simon Glass64dcd252015-04-05 16:07:40 -0600477 return 0;
478}
479
Simon Glass75577ba2015-04-05 16:07:41 -0600480#ifndef CONFIG_DM_ETH
Simon Glass64dcd252015-04-05 16:07:40 -0600481static int dw_eth_init(struct eth_device *dev, bd_t *bis)
482{
483 return _dw_eth_init(dev->priv, dev->enetaddr);
484}
485
486static int dw_eth_send(struct eth_device *dev, void *packet, int length)
487{
488 return _dw_eth_send(dev->priv, packet, length);
489}
490
491static int dw_eth_recv(struct eth_device *dev)
492{
Simon Glass75577ba2015-04-05 16:07:41 -0600493 uchar *packet;
494 int length;
495
496 length = _dw_eth_recv(dev->priv, &packet);
497 if (length == -EAGAIN)
498 return 0;
499 net_process_received_packet(packet, length);
500
501 _dw_free_pkt(dev->priv);
502
503 return 0;
Simon Glass64dcd252015-04-05 16:07:40 -0600504}
505
506static void dw_eth_halt(struct eth_device *dev)
507{
508 return _dw_eth_halt(dev->priv);
509}
510
511static int dw_write_hwaddr(struct eth_device *dev)
512{
513 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530514}
515
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400516int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530517{
518 struct eth_device *dev;
519 struct dw_eth_dev *priv;
520
521 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
522 if (!dev)
523 return -ENOMEM;
524
525 /*
526 * Since the priv structure contains the descriptors which need a strict
527 * buswidth alignment, memalign is used to allocate memory
528 */
Ian Campbell1c848a22014-05-08 22:26:32 +0100529 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
530 sizeof(struct dw_eth_dev));
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530531 if (!priv) {
532 free(dev);
533 return -ENOMEM;
534 }
535
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200536 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
537 printf("designware: buffers are outside DMA memory\n");
538 return -EINVAL;
539 }
540
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530541 memset(dev, 0, sizeof(struct eth_device));
542 memset(priv, 0, sizeof(struct dw_eth_dev));
543
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400544 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530545 dev->iobase = (int)base_addr;
546 dev->priv = priv;
547
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530548 priv->dev = dev;
549 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
550 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
551 DW_DMA_BASE_OFFSET);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530552
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530553 dev->init = dw_eth_init;
554 dev->send = dw_eth_send;
555 dev->recv = dw_eth_recv;
556 dev->halt = dw_eth_halt;
557 dev->write_hwaddr = dw_write_hwaddr;
558
559 eth_register(dev);
560
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400561 priv->interface = interface;
562
563 dw_mdio_init(dev->name, priv->mac_regs_p);
564 priv->bus = miiphy_get_dev_by_name(dev->name);
565
Simon Glass64dcd252015-04-05 16:07:40 -0600566 return dw_phy_init(priv, dev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530567}
Simon Glass75577ba2015-04-05 16:07:41 -0600568#endif
569
570#ifdef CONFIG_DM_ETH
571static int designware_eth_start(struct udevice *dev)
572{
573 struct eth_pdata *pdata = dev_get_platdata(dev);
574
575 return _dw_eth_init(dev->priv, pdata->enetaddr);
576}
577
578static int designware_eth_send(struct udevice *dev, void *packet, int length)
579{
580 struct dw_eth_dev *priv = dev_get_priv(dev);
581
582 return _dw_eth_send(priv, packet, length);
583}
584
Simon Glassa1ca92e2015-07-06 16:47:49 -0600585static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass75577ba2015-04-05 16:07:41 -0600586{
587 struct dw_eth_dev *priv = dev_get_priv(dev);
588
589 return _dw_eth_recv(priv, packetp);
590}
591
592static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
593 int length)
594{
595 struct dw_eth_dev *priv = dev_get_priv(dev);
596
597 return _dw_free_pkt(priv);
598}
599
600static void designware_eth_stop(struct udevice *dev)
601{
602 struct dw_eth_dev *priv = dev_get_priv(dev);
603
604 return _dw_eth_halt(priv);
605}
606
607static int designware_eth_write_hwaddr(struct udevice *dev)
608{
609 struct eth_pdata *pdata = dev_get_platdata(dev);
610 struct dw_eth_dev *priv = dev_get_priv(dev);
611
612 return _dw_write_hwaddr(priv, pdata->enetaddr);
613}
614
Bin Meng8b7ee662015-09-11 03:24:35 -0700615static int designware_eth_bind(struct udevice *dev)
616{
617#ifdef CONFIG_DM_PCI
618 static int num_cards;
619 char name[20];
620
621 /* Create a unique device name for PCI type devices */
622 if (device_is_on_pci_bus(dev)) {
623 sprintf(name, "eth_designware#%u", num_cards++);
624 device_set_name(dev, name);
625 }
626#endif
627
628 return 0;
629}
630
Simon Glass75577ba2015-04-05 16:07:41 -0600631static int designware_eth_probe(struct udevice *dev)
632{
633 struct eth_pdata *pdata = dev_get_platdata(dev);
634 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengf0dc73c2015-09-03 05:37:29 -0700635 u32 iobase = pdata->iobase;
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200636 ulong ioaddr;
Simon Glass75577ba2015-04-05 16:07:41 -0600637 int ret;
638
Bin Meng8b7ee662015-09-11 03:24:35 -0700639#ifdef CONFIG_DM_PCI
640 /*
641 * If we are on PCI bus, either directly attached to a PCI root port,
642 * or via a PCI bridge, fill in platdata before we probe the hardware.
643 */
644 if (device_is_on_pci_bus(dev)) {
Bin Meng8b7ee662015-09-11 03:24:35 -0700645 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
646 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
Bin Meng6758a6c2016-02-02 05:58:00 -0800647 iobase = dm_pci_mem_to_phys(dev, iobase);
Bin Meng8b7ee662015-09-11 03:24:35 -0700648
649 pdata->iobase = iobase;
650 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
651 }
652#endif
653
Bin Mengf0dc73c2015-09-03 05:37:29 -0700654 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
Beniamino Galvani0e1a3e32016-05-08 08:30:15 +0200655 ioaddr = iobase;
656 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
657 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
Simon Glass75577ba2015-04-05 16:07:41 -0600658 priv->interface = pdata->phy_interface;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300659 priv->max_speed = pdata->max_speed;
Simon Glass75577ba2015-04-05 16:07:41 -0600660
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100661 dw_mdio_init(dev->name, dev);
Simon Glass75577ba2015-04-05 16:07:41 -0600662 priv->bus = miiphy_get_dev_by_name(dev->name);
663
664 ret = dw_phy_init(priv, dev);
665 debug("%s, ret=%d\n", __func__, ret);
666
667 return ret;
668}
669
Bin Meng5d2459f2015-10-07 21:32:38 -0700670static int designware_eth_remove(struct udevice *dev)
671{
672 struct dw_eth_dev *priv = dev_get_priv(dev);
673
674 free(priv->phydev);
675 mdio_unregister(priv->bus);
676 mdio_free(priv->bus);
677
678 return 0;
679}
680
Simon Glass75577ba2015-04-05 16:07:41 -0600681static const struct eth_ops designware_eth_ops = {
682 .start = designware_eth_start,
683 .send = designware_eth_send,
684 .recv = designware_eth_recv,
685 .free_pkt = designware_eth_free_pkt,
686 .stop = designware_eth_stop,
687 .write_hwaddr = designware_eth_write_hwaddr,
688};
689
690static int designware_eth_ofdata_to_platdata(struct udevice *dev)
691{
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100692 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300693#ifdef CONFIG_DM_GPIO
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100694 struct dw_eth_dev *priv = dev_get_priv(dev);
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300695#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100696 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
Simon Glass75577ba2015-04-05 16:07:41 -0600697 const char *phy_mode;
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300698 const fdt32_t *cell;
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300699#ifdef CONFIG_DM_GPIO
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100700 int reset_flags = GPIOD_IS_OUT;
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300701#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100702 int ret = 0;
Simon Glass75577ba2015-04-05 16:07:41 -0600703
704 pdata->iobase = dev_get_addr(dev);
705 pdata->phy_interface = -1;
706 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
707 if (phy_mode)
708 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
709 if (pdata->phy_interface == -1) {
710 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
711 return -EINVAL;
712 }
713
Alexey Brodkin6968ec92016-01-13 16:59:37 +0300714 pdata->max_speed = 0;
715 cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL);
716 if (cell)
717 pdata->max_speed = fdt32_to_cpu(*cell);
718
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300719#ifdef CONFIG_DM_GPIO
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100720 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
721 "snps,reset-active-low"))
722 reset_flags |= GPIOD_ACTIVE_LOW;
723
724 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
725 &priv->reset_gpio, reset_flags);
726 if (ret == 0) {
727 ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
728 "snps,reset-delays-us", dw_pdata->reset_delays, 3);
729 } else if (ret == -ENOENT) {
730 ret = 0;
731 }
Alexey Brodkin66d027e2016-06-27 13:17:51 +0300732#endif
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100733
734 return ret;
Simon Glass75577ba2015-04-05 16:07:41 -0600735}
736
737static const struct udevice_id designware_eth_ids[] = {
738 { .compatible = "allwinner,sun7i-a20-gmac" },
Marek Vasutb9628592015-07-25 18:38:44 +0200739 { .compatible = "altr,socfpga-stmmac" },
Simon Glass75577ba2015-04-05 16:07:41 -0600740 { }
741};
742
Marek Vasut9f76f102015-07-25 18:42:34 +0200743U_BOOT_DRIVER(eth_designware) = {
Simon Glass75577ba2015-04-05 16:07:41 -0600744 .name = "eth_designware",
745 .id = UCLASS_ETH,
746 .of_match = designware_eth_ids,
747 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Meng8b7ee662015-09-11 03:24:35 -0700748 .bind = designware_eth_bind,
Simon Glass75577ba2015-04-05 16:07:41 -0600749 .probe = designware_eth_probe,
Bin Meng5d2459f2015-10-07 21:32:38 -0700750 .remove = designware_eth_remove,
Simon Glass75577ba2015-04-05 16:07:41 -0600751 .ops = &designware_eth_ops,
752 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
Sjoerd Simons90b7fc92016-02-28 22:24:55 +0100753 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
Simon Glass75577ba2015-04-05 16:07:41 -0600754 .flags = DM_FLAG_ALLOC_PRIV_DMA,
755};
Bin Meng8b7ee662015-09-11 03:24:35 -0700756
757static struct pci_device_id supported[] = {
758 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
759 { }
760};
761
762U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass75577ba2015-04-05 16:07:41 -0600763#endif