blob: 07281a6ce9d2fa204e30c1cfee3e1b5258739485 [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>
Stefan Roeseef760252012-05-07 12:04:25 +020017#include <linux/compiler.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053018#include <linux/err.h>
19#include <asm/io.h>
20#include "designware.h"
21
Simon Glass75577ba2015-04-05 16:07:41 -060022DECLARE_GLOBAL_DATA_PTR;
23
Alexey Brodkin92a190a2014-01-22 20:54:06 +040024#if !defined(CONFIG_PHYLIB)
25# error "DesignWare Ether MAC requires PHYLIB - missing CONFIG_PHYLIB"
26#endif
27
28static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
29{
30 struct eth_mac_regs *mac_p = bus->priv;
31 ulong start;
32 u16 miiaddr;
33 int timeout = CONFIG_MDIO_TIMEOUT;
34
35 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
36 ((reg << MIIREGSHIFT) & MII_REGMSK);
37
38 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
39
40 start = get_timer(0);
41 while (get_timer(start) < timeout) {
42 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
43 return readl(&mac_p->miidata);
44 udelay(10);
45 };
46
Simon Glass64dcd252015-04-05 16:07:40 -060047 return -ETIMEDOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040048}
49
50static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
51 u16 val)
52{
53 struct eth_mac_regs *mac_p = bus->priv;
54 ulong start;
55 u16 miiaddr;
Simon Glass64dcd252015-04-05 16:07:40 -060056 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040057
58 writel(val, &mac_p->miidata);
59 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
60 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
61
62 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
63
64 start = get_timer(0);
65 while (get_timer(start) < timeout) {
66 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
67 ret = 0;
68 break;
69 }
70 udelay(10);
71 };
72
73 return ret;
74}
75
Simon Glass64dcd252015-04-05 16:07:40 -060076static int dw_mdio_init(const char *name, struct eth_mac_regs *mac_regs_p)
Alexey Brodkin92a190a2014-01-22 20:54:06 +040077{
78 struct mii_dev *bus = mdio_alloc();
79
80 if (!bus) {
81 printf("Failed to allocate MDIO bus\n");
Simon Glass64dcd252015-04-05 16:07:40 -060082 return -ENOMEM;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040083 }
84
85 bus->read = dw_mdio_read;
86 bus->write = dw_mdio_write;
Simon Glass64dcd252015-04-05 16:07:40 -060087 snprintf(bus->name, sizeof(bus->name), name);
Alexey Brodkin92a190a2014-01-22 20:54:06 +040088
89 bus->priv = (void *)mac_regs_p;
90
91 return mdio_register(bus);
92}
Vipin Kumar13edd172012-03-26 00:09:56 +000093
Simon Glass64dcd252015-04-05 16:07:40 -060094static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053095{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053096 struct eth_dma_regs *dma_p = priv->dma_regs_p;
97 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
98 char *txbuffs = &priv->txbuffs[0];
99 struct dmamacdescr *desc_p;
100 u32 idx;
101
102 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
103 desc_p = &desc_table_p[idx];
104 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
105 desc_p->dmamac_next = &desc_table_p[idx + 1];
106
107#if defined(CONFIG_DW_ALTDESCRIPTOR)
108 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
109 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
110 DESC_TXSTS_TXCHECKINSCTRL | \
111 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
112
113 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
114 desc_p->dmamac_cntl = 0;
115 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
116#else
117 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
118 desc_p->txrx_status = 0;
119#endif
120 }
121
122 /* Correcting the last pointer of the chain */
123 desc_p->dmamac_next = &desc_table_p[0];
124
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400125 /* Flush all Tx buffer descriptors at once */
126 flush_dcache_range((unsigned int)priv->tx_mac_descrtable,
127 (unsigned int)priv->tx_mac_descrtable +
128 sizeof(priv->tx_mac_descrtable));
129
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530130 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400131 priv->tx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530132}
133
Simon Glass64dcd252015-04-05 16:07:40 -0600134static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530135{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530136 struct eth_dma_regs *dma_p = priv->dma_regs_p;
137 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
138 char *rxbuffs = &priv->rxbuffs[0];
139 struct dmamacdescr *desc_p;
140 u32 idx;
141
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400142 /* Before passing buffers to GMAC we need to make sure zeros
143 * written there right after "priv" structure allocation were
144 * flushed into RAM.
145 * Otherwise there's a chance to get some of them flushed in RAM when
146 * GMAC is already pushing data to RAM via DMA. This way incoming from
147 * GMAC data will be corrupted. */
148 flush_dcache_range((unsigned int)rxbuffs, (unsigned int)rxbuffs +
149 RX_TOTAL_BUFSIZE);
150
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530151 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
152 desc_p = &desc_table_p[idx];
153 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
154 desc_p->dmamac_next = &desc_table_p[idx + 1];
155
156 desc_p->dmamac_cntl =
157 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
158 DESC_RXCTRL_RXCHAIN;
159
160 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
161 }
162
163 /* Correcting the last pointer of the chain */
164 desc_p->dmamac_next = &desc_table_p[0];
165
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400166 /* Flush all Rx buffer descriptors at once */
167 flush_dcache_range((unsigned int)priv->rx_mac_descrtable,
168 (unsigned int)priv->rx_mac_descrtable +
169 sizeof(priv->rx_mac_descrtable));
170
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530171 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400172 priv->rx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530173}
174
Simon Glass64dcd252015-04-05 16:07:40 -0600175static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530176{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530177 struct eth_mac_regs *mac_p = priv->mac_regs_p;
178 u32 macid_lo, macid_hi;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530179
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400180 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
181 (mac_id[3] << 24);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530182 macid_hi = mac_id[4] + (mac_id[5] << 8);
183
184 writel(macid_hi, &mac_p->macaddr0hi);
185 writel(macid_lo, &mac_p->macaddr0lo);
186
187 return 0;
188}
189
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400190static void dw_adjust_link(struct eth_mac_regs *mac_p,
191 struct phy_device *phydev)
192{
193 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
194
195 if (!phydev->link) {
196 printf("%s: No link.\n", phydev->dev->name);
197 return;
198 }
199
200 if (phydev->speed != 1000)
201 conf |= MII_PORTSELECT;
202
203 if (phydev->speed == 100)
204 conf |= FES_100;
205
206 if (phydev->duplex)
207 conf |= FULLDPLXMODE;
208
209 writel(conf, &mac_p->conf);
210
211 printf("Speed: %d, %s duplex%s\n", phydev->speed,
212 (phydev->duplex) ? "full" : "half",
213 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
214}
215
Simon Glass64dcd252015-04-05 16:07:40 -0600216static void _dw_eth_halt(struct dw_eth_dev *priv)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400217{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400218 struct eth_mac_regs *mac_p = priv->mac_regs_p;
219 struct eth_dma_regs *dma_p = priv->dma_regs_p;
220
221 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
222 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
223
224 phy_shutdown(priv->phydev);
225}
226
Simon Glass64dcd252015-04-05 16:07:40 -0600227static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530228{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530229 struct eth_mac_regs *mac_p = priv->mac_regs_p;
230 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400231 unsigned int start;
Simon Glass64dcd252015-04-05 16:07:40 -0600232 int ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530233
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400234 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumar13edd172012-03-26 00:09:56 +0000235
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400236 start = get_timer(0);
237 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin875143f2015-01-13 17:10:24 +0300238 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
239 printf("DMA reset timeout\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600240 return -ETIMEDOUT;
Alexey Brodkin875143f2015-01-13 17:10:24 +0300241 }
Stefan Roeseef760252012-05-07 12:04:25 +0200242
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400243 mdelay(100);
244 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530245
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400246 /* Soft reset above clears HW address registers.
247 * So we have to set it here once again */
Simon Glass64dcd252015-04-05 16:07:40 -0600248 _dw_write_hwaddr(priv, enetaddr);
Vipin KUMARc7f6dbe2012-03-26 00:09:52 +0000249
Simon Glass64dcd252015-04-05 16:07:40 -0600250 rx_descs_init(priv);
251 tx_descs_init(priv);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530252
Ian Campbell49692c52014-05-08 22:26:35 +0100253 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530254
Sonic Zhangd2279222015-01-29 14:38:50 +0800255#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400256 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
257 &dma_p->opmode);
Sonic Zhangd2279222015-01-29 14:38:50 +0800258#else
259 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
260 &dma_p->opmode);
261#endif
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530262
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400263 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530264
Sonic Zhang2ddaf132015-01-29 13:37:31 +0800265#ifdef CONFIG_DW_AXI_BURST_LEN
266 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
267#endif
268
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400269 /* Start up the PHY */
Simon Glass64dcd252015-04-05 16:07:40 -0600270 ret = phy_startup(priv->phydev);
271 if (ret) {
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400272 printf("Could not initialize PHY %s\n",
273 priv->phydev->dev->name);
Simon Glass64dcd252015-04-05 16:07:40 -0600274 return ret;
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530275 }
276
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400277 dw_adjust_link(mac_p, priv->phydev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530278
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400279 if (!priv->phydev->link)
Simon Glass64dcd252015-04-05 16:07:40 -0600280 return -EIO;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530281
Armando Viscontiaa510052012-03-26 00:09:55 +0000282 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530283
284 return 0;
285}
286
Simon Glass64dcd252015-04-05 16:07:40 -0600287static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530288{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530289 struct eth_dma_regs *dma_p = priv->dma_regs_p;
290 u32 desc_num = priv->tx_currdescnum;
291 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Marek Vasut96cec172014-09-15 01:05:23 +0200292 uint32_t desc_start = (uint32_t)desc_p;
293 uint32_t desc_end = desc_start +
294 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
295 uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
296 uint32_t data_end = data_start +
297 roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell964ea7c2014-05-08 22:26:33 +0100298 /*
299 * Strictly we only need to invalidate the "txrx_status" field
300 * for the following check, but on some platforms we cannot
Marek Vasut96cec172014-09-15 01:05:23 +0200301 * invalidate only 4 bytes, so we flush the entire descriptor,
302 * which is 16 bytes in total. This is safe because the
303 * individual descriptors in the array are each aligned to
304 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell964ea7c2014-05-08 22:26:33 +0100305 */
Marek Vasut96cec172014-09-15 01:05:23 +0200306 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400307
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530308 /* Check if the descriptor is owned by CPU */
309 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
310 printf("CPU not owner of tx frame\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600311 return -EPERM;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530312 }
313
Marek Vasut96cec172014-09-15 01:05:23 +0200314 memcpy(desc_p->dmamac_addr, packet, length);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530315
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400316 /* Flush data to be sent */
Marek Vasut96cec172014-09-15 01:05:23 +0200317 flush_dcache_range(data_start, data_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400318
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530319#if defined(CONFIG_DW_ALTDESCRIPTOR)
320 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
321 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
322 DESC_TXCTRL_SIZE1MASK;
323
324 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
325 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
326#else
327 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
328 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
329 DESC_TXCTRL_TXFIRST;
330
331 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
332#endif
333
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400334 /* Flush modified buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200335 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400336
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530337 /* Test the wrap-around condition. */
338 if (++desc_num >= CONFIG_TX_DESCR_NUM)
339 desc_num = 0;
340
341 priv->tx_currdescnum = desc_num;
342
343 /* Start the transmission */
344 writel(POLL_DATA, &dma_p->txpolldemand);
345
346 return 0;
347}
348
Simon Glass75577ba2015-04-05 16:07:41 -0600349static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530350{
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400351 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530352 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass75577ba2015-04-05 16:07:41 -0600353 int length = -EAGAIN;
Marek Vasut96cec172014-09-15 01:05:23 +0200354 uint32_t desc_start = (uint32_t)desc_p;
355 uint32_t desc_end = desc_start +
356 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
357 uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
358 uint32_t data_end;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530359
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400360 /* Invalidate entire buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200361 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400362
363 status = desc_p->txrx_status;
364
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530365 /* Check if the owner is the CPU */
366 if (!(status & DESC_RXSTS_OWNBYDMA)) {
367
368 length = (status & DESC_RXSTS_FRMLENMSK) >> \
369 DESC_RXSTS_FRMLENSHFT;
370
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400371 /* Invalidate received data */
Marek Vasut96cec172014-09-15 01:05:23 +0200372 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
373 invalidate_dcache_range(data_start, data_end);
Simon Glass75577ba2015-04-05 16:07:41 -0600374 *packetp = desc_p->dmamac_addr;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530375 }
376
Simon Glass75577ba2015-04-05 16:07:41 -0600377 return length;
378}
379
380static int _dw_free_pkt(struct dw_eth_dev *priv)
381{
382 u32 desc_num = priv->rx_currdescnum;
383 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
384 uint32_t desc_start = (uint32_t)desc_p;
385 uint32_t desc_end = desc_start +
386 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
387
388 /*
389 * Make the current descriptor valid again and go to
390 * the next one
391 */
392 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
393
394 /* Flush only status field - others weren't changed */
395 flush_dcache_range(desc_start, desc_end);
396
397 /* Test the wrap-around condition. */
398 if (++desc_num >= CONFIG_RX_DESCR_NUM)
399 desc_num = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530400 priv->rx_currdescnum = desc_num;
401
Simon Glass75577ba2015-04-05 16:07:41 -0600402 return 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530403}
404
Simon Glass64dcd252015-04-05 16:07:40 -0600405static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530406{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400407 struct phy_device *phydev;
408 int mask = 0xffffffff;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530409
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400410#ifdef CONFIG_PHY_ADDR
411 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530412#endif
413
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400414 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
415 if (!phydev)
Simon Glass64dcd252015-04-05 16:07:40 -0600416 return -ENODEV;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530417
Ian Campbell15e82e52014-04-28 20:14:05 +0100418 phy_connect_dev(phydev, dev);
419
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400420 phydev->supported &= PHY_GBIT_FEATURES;
421 phydev->advertising = phydev->supported;
422
423 priv->phydev = phydev;
424 phy_config(phydev);
425
Simon Glass64dcd252015-04-05 16:07:40 -0600426 return 0;
427}
428
Simon Glass75577ba2015-04-05 16:07:41 -0600429#ifndef CONFIG_DM_ETH
Simon Glass64dcd252015-04-05 16:07:40 -0600430static int dw_eth_init(struct eth_device *dev, bd_t *bis)
431{
432 return _dw_eth_init(dev->priv, dev->enetaddr);
433}
434
435static int dw_eth_send(struct eth_device *dev, void *packet, int length)
436{
437 return _dw_eth_send(dev->priv, packet, length);
438}
439
440static int dw_eth_recv(struct eth_device *dev)
441{
Simon Glass75577ba2015-04-05 16:07:41 -0600442 uchar *packet;
443 int length;
444
445 length = _dw_eth_recv(dev->priv, &packet);
446 if (length == -EAGAIN)
447 return 0;
448 net_process_received_packet(packet, length);
449
450 _dw_free_pkt(dev->priv);
451
452 return 0;
Simon Glass64dcd252015-04-05 16:07:40 -0600453}
454
455static void dw_eth_halt(struct eth_device *dev)
456{
457 return _dw_eth_halt(dev->priv);
458}
459
460static int dw_write_hwaddr(struct eth_device *dev)
461{
462 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530463}
464
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400465int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530466{
467 struct eth_device *dev;
468 struct dw_eth_dev *priv;
469
470 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
471 if (!dev)
472 return -ENOMEM;
473
474 /*
475 * Since the priv structure contains the descriptors which need a strict
476 * buswidth alignment, memalign is used to allocate memory
477 */
Ian Campbell1c848a22014-05-08 22:26:32 +0100478 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
479 sizeof(struct dw_eth_dev));
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530480 if (!priv) {
481 free(dev);
482 return -ENOMEM;
483 }
484
485 memset(dev, 0, sizeof(struct eth_device));
486 memset(priv, 0, sizeof(struct dw_eth_dev));
487
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400488 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530489 dev->iobase = (int)base_addr;
490 dev->priv = priv;
491
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530492 priv->dev = dev;
493 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
494 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
495 DW_DMA_BASE_OFFSET);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530496
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530497 dev->init = dw_eth_init;
498 dev->send = dw_eth_send;
499 dev->recv = dw_eth_recv;
500 dev->halt = dw_eth_halt;
501 dev->write_hwaddr = dw_write_hwaddr;
502
503 eth_register(dev);
504
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400505 priv->interface = interface;
506
507 dw_mdio_init(dev->name, priv->mac_regs_p);
508 priv->bus = miiphy_get_dev_by_name(dev->name);
509
Simon Glass64dcd252015-04-05 16:07:40 -0600510 return dw_phy_init(priv, dev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530511}
Simon Glass75577ba2015-04-05 16:07:41 -0600512#endif
513
514#ifdef CONFIG_DM_ETH
515static int designware_eth_start(struct udevice *dev)
516{
517 struct eth_pdata *pdata = dev_get_platdata(dev);
518
519 return _dw_eth_init(dev->priv, pdata->enetaddr);
520}
521
522static int designware_eth_send(struct udevice *dev, void *packet, int length)
523{
524 struct dw_eth_dev *priv = dev_get_priv(dev);
525
526 return _dw_eth_send(priv, packet, length);
527}
528
529static int designware_eth_recv(struct udevice *dev, uchar **packetp)
530{
531 struct dw_eth_dev *priv = dev_get_priv(dev);
532
533 return _dw_eth_recv(priv, packetp);
534}
535
536static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
537 int length)
538{
539 struct dw_eth_dev *priv = dev_get_priv(dev);
540
541 return _dw_free_pkt(priv);
542}
543
544static void designware_eth_stop(struct udevice *dev)
545{
546 struct dw_eth_dev *priv = dev_get_priv(dev);
547
548 return _dw_eth_halt(priv);
549}
550
551static int designware_eth_write_hwaddr(struct udevice *dev)
552{
553 struct eth_pdata *pdata = dev_get_platdata(dev);
554 struct dw_eth_dev *priv = dev_get_priv(dev);
555
556 return _dw_write_hwaddr(priv, pdata->enetaddr);
557}
558
559static int designware_eth_probe(struct udevice *dev)
560{
561 struct eth_pdata *pdata = dev_get_platdata(dev);
562 struct dw_eth_dev *priv = dev_get_priv(dev);
563 int ret;
564
565 debug("%s, iobase=%lx, priv=%p\n", __func__, pdata->iobase, priv);
566 priv->mac_regs_p = (struct eth_mac_regs *)pdata->iobase;
567 priv->dma_regs_p = (struct eth_dma_regs *)(pdata->iobase +
568 DW_DMA_BASE_OFFSET);
569 priv->interface = pdata->phy_interface;
570
571 dw_mdio_init(dev->name, priv->mac_regs_p);
572 priv->bus = miiphy_get_dev_by_name(dev->name);
573
574 ret = dw_phy_init(priv, dev);
575 debug("%s, ret=%d\n", __func__, ret);
576
577 return ret;
578}
579
580static const struct eth_ops designware_eth_ops = {
581 .start = designware_eth_start,
582 .send = designware_eth_send,
583 .recv = designware_eth_recv,
584 .free_pkt = designware_eth_free_pkt,
585 .stop = designware_eth_stop,
586 .write_hwaddr = designware_eth_write_hwaddr,
587};
588
589static int designware_eth_ofdata_to_platdata(struct udevice *dev)
590{
591 struct eth_pdata *pdata = dev_get_platdata(dev);
592 const char *phy_mode;
593
594 pdata->iobase = dev_get_addr(dev);
595 pdata->phy_interface = -1;
596 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
597 if (phy_mode)
598 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
599 if (pdata->phy_interface == -1) {
600 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
601 return -EINVAL;
602 }
603
604 return 0;
605}
606
607static const struct udevice_id designware_eth_ids[] = {
608 { .compatible = "allwinner,sun7i-a20-gmac" },
609 { }
610};
611
612U_BOOT_DRIVER(eth_sandbox) = {
613 .name = "eth_designware",
614 .id = UCLASS_ETH,
615 .of_match = designware_eth_ids,
616 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
617 .probe = designware_eth_probe,
618 .ops = &designware_eth_ops,
619 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
620 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
621 .flags = DM_FLAG_ALLOC_PRIV_DMA,
622};
623#endif