blob: 68b6548309ef8b6a1a929de0b372f41656962a1f [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{
27 struct eth_mac_regs *mac_p = bus->priv;
28 ulong start;
29 u16 miiaddr;
30 int timeout = CONFIG_MDIO_TIMEOUT;
31
32 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
33 ((reg << MIIREGSHIFT) & MII_REGMSK);
34
35 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
36
37 start = get_timer(0);
38 while (get_timer(start) < timeout) {
39 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
40 return readl(&mac_p->miidata);
41 udelay(10);
42 };
43
Simon Glass64dcd252015-04-05 16:07:40 -060044 return -ETIMEDOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040045}
46
47static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
48 u16 val)
49{
50 struct eth_mac_regs *mac_p = bus->priv;
51 ulong start;
52 u16 miiaddr;
Simon Glass64dcd252015-04-05 16:07:40 -060053 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040054
55 writel(val, &mac_p->miidata);
56 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
57 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
58
59 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
60
61 start = get_timer(0);
62 while (get_timer(start) < timeout) {
63 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
64 ret = 0;
65 break;
66 }
67 udelay(10);
68 };
69
70 return ret;
71}
72
Simon Glass64dcd252015-04-05 16:07:40 -060073static int dw_mdio_init(const char *name, struct eth_mac_regs *mac_regs_p)
Alexey Brodkin92a190a2014-01-22 20:54:06 +040074{
75 struct mii_dev *bus = mdio_alloc();
76
77 if (!bus) {
78 printf("Failed to allocate MDIO bus\n");
Simon Glass64dcd252015-04-05 16:07:40 -060079 return -ENOMEM;
Alexey Brodkin92a190a2014-01-22 20:54:06 +040080 }
81
82 bus->read = dw_mdio_read;
83 bus->write = dw_mdio_write;
Ben Whitten192bc692015-12-30 13:05:58 +000084 snprintf(bus->name, sizeof(bus->name), "%s", name);
Alexey Brodkin92a190a2014-01-22 20:54:06 +040085
86 bus->priv = (void *)mac_regs_p;
87
88 return mdio_register(bus);
89}
Vipin Kumar13edd172012-03-26 00:09:56 +000090
Simon Glass64dcd252015-04-05 16:07:40 -060091static void tx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053092{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053093 struct eth_dma_regs *dma_p = priv->dma_regs_p;
94 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
95 char *txbuffs = &priv->txbuffs[0];
96 struct dmamacdescr *desc_p;
97 u32 idx;
98
99 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
100 desc_p = &desc_table_p[idx];
101 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
102 desc_p->dmamac_next = &desc_table_p[idx + 1];
103
104#if defined(CONFIG_DW_ALTDESCRIPTOR)
105 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
Marek Vasut2b261092015-12-20 03:59:23 +0100106 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
107 DESC_TXSTS_TXCHECKINSCTRL |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530108 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
109
110 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
111 desc_p->dmamac_cntl = 0;
112 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
113#else
114 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
115 desc_p->txrx_status = 0;
116#endif
117 }
118
119 /* Correcting the last pointer of the chain */
120 desc_p->dmamac_next = &desc_table_p[0];
121
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400122 /* Flush all Tx buffer descriptors at once */
123 flush_dcache_range((unsigned int)priv->tx_mac_descrtable,
124 (unsigned int)priv->tx_mac_descrtable +
125 sizeof(priv->tx_mac_descrtable));
126
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530127 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400128 priv->tx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530129}
130
Simon Glass64dcd252015-04-05 16:07:40 -0600131static void rx_descs_init(struct dw_eth_dev *priv)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530132{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530133 struct eth_dma_regs *dma_p = priv->dma_regs_p;
134 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
135 char *rxbuffs = &priv->rxbuffs[0];
136 struct dmamacdescr *desc_p;
137 u32 idx;
138
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400139 /* Before passing buffers to GMAC we need to make sure zeros
140 * written there right after "priv" structure allocation were
141 * flushed into RAM.
142 * Otherwise there's a chance to get some of them flushed in RAM when
143 * GMAC is already pushing data to RAM via DMA. This way incoming from
144 * GMAC data will be corrupted. */
145 flush_dcache_range((unsigned int)rxbuffs, (unsigned int)rxbuffs +
146 RX_TOTAL_BUFSIZE);
147
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530148 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
149 desc_p = &desc_table_p[idx];
150 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
151 desc_p->dmamac_next = &desc_table_p[idx + 1];
152
153 desc_p->dmamac_cntl =
Marek Vasut2b261092015-12-20 03:59:23 +0100154 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530155 DESC_RXCTRL_RXCHAIN;
156
157 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
158 }
159
160 /* Correcting the last pointer of the chain */
161 desc_p->dmamac_next = &desc_table_p[0];
162
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400163 /* Flush all Rx buffer descriptors at once */
164 flush_dcache_range((unsigned int)priv->rx_mac_descrtable,
165 (unsigned int)priv->rx_mac_descrtable +
166 sizeof(priv->rx_mac_descrtable));
167
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530168 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
Alexey Brodkin74cb7082014-01-13 13:28:38 +0400169 priv->rx_currdescnum = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530170}
171
Simon Glass64dcd252015-04-05 16:07:40 -0600172static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530173{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530174 struct eth_mac_regs *mac_p = priv->mac_regs_p;
175 u32 macid_lo, macid_hi;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530176
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400177 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
178 (mac_id[3] << 24);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530179 macid_hi = mac_id[4] + (mac_id[5] << 8);
180
181 writel(macid_hi, &mac_p->macaddr0hi);
182 writel(macid_lo, &mac_p->macaddr0lo);
183
184 return 0;
185}
186
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400187static void dw_adjust_link(struct eth_mac_regs *mac_p,
188 struct phy_device *phydev)
189{
190 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
191
192 if (!phydev->link) {
193 printf("%s: No link.\n", phydev->dev->name);
194 return;
195 }
196
197 if (phydev->speed != 1000)
198 conf |= MII_PORTSELECT;
199
200 if (phydev->speed == 100)
201 conf |= FES_100;
202
203 if (phydev->duplex)
204 conf |= FULLDPLXMODE;
205
206 writel(conf, &mac_p->conf);
207
208 printf("Speed: %d, %s duplex%s\n", phydev->speed,
209 (phydev->duplex) ? "full" : "half",
210 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
211}
212
Simon Glass64dcd252015-04-05 16:07:40 -0600213static void _dw_eth_halt(struct dw_eth_dev *priv)
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400214{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400215 struct eth_mac_regs *mac_p = priv->mac_regs_p;
216 struct eth_dma_regs *dma_p = priv->dma_regs_p;
217
218 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
219 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
220
221 phy_shutdown(priv->phydev);
222}
223
Simon Glass64dcd252015-04-05 16:07:40 -0600224static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530225{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530226 struct eth_mac_regs *mac_p = priv->mac_regs_p;
227 struct eth_dma_regs *dma_p = priv->dma_regs_p;
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400228 unsigned int start;
Simon Glass64dcd252015-04-05 16:07:40 -0600229 int ret;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530230
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400231 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
Vipin Kumar13edd172012-03-26 00:09:56 +0000232
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400233 start = get_timer(0);
234 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
Alexey Brodkin875143f2015-01-13 17:10:24 +0300235 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
236 printf("DMA reset timeout\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600237 return -ETIMEDOUT;
Alexey Brodkin875143f2015-01-13 17:10:24 +0300238 }
Stefan Roeseef760252012-05-07 12:04:25 +0200239
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400240 mdelay(100);
241 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530242
Bin Mengf3edfd32015-06-15 18:40:19 +0800243 /*
244 * Soft reset above clears HW address registers.
245 * So we have to set it here once again.
246 */
247 _dw_write_hwaddr(priv, enetaddr);
248
Simon Glass64dcd252015-04-05 16:07:40 -0600249 rx_descs_init(priv);
250 tx_descs_init(priv);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530251
Ian Campbell49692c52014-05-08 22:26:35 +0100252 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530253
Sonic Zhangd2279222015-01-29 14:38:50 +0800254#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400255 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
256 &dma_p->opmode);
Sonic Zhangd2279222015-01-29 14:38:50 +0800257#else
258 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
259 &dma_p->opmode);
260#endif
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530261
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400262 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530263
Sonic Zhang2ddaf132015-01-29 13:37:31 +0800264#ifdef CONFIG_DW_AXI_BURST_LEN
265 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
266#endif
267
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400268 /* Start up the PHY */
Simon Glass64dcd252015-04-05 16:07:40 -0600269 ret = phy_startup(priv->phydev);
270 if (ret) {
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400271 printf("Could not initialize PHY %s\n",
272 priv->phydev->dev->name);
Simon Glass64dcd252015-04-05 16:07:40 -0600273 return ret;
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530274 }
275
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400276 dw_adjust_link(mac_p, priv->phydev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530277
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400278 if (!priv->phydev->link)
Simon Glass64dcd252015-04-05 16:07:40 -0600279 return -EIO;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530280
Armando Viscontiaa510052012-03-26 00:09:55 +0000281 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530282
283 return 0;
284}
285
Simon Glass64dcd252015-04-05 16:07:40 -0600286static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530287{
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530288 struct eth_dma_regs *dma_p = priv->dma_regs_p;
289 u32 desc_num = priv->tx_currdescnum;
290 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
Marek Vasut96cec172014-09-15 01:05:23 +0200291 uint32_t desc_start = (uint32_t)desc_p;
292 uint32_t desc_end = desc_start +
293 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
294 uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
295 uint32_t data_end = data_start +
296 roundup(length, ARCH_DMA_MINALIGN);
Ian Campbell964ea7c2014-05-08 22:26:33 +0100297 /*
298 * Strictly we only need to invalidate the "txrx_status" field
299 * for the following check, but on some platforms we cannot
Marek Vasut96cec172014-09-15 01:05:23 +0200300 * invalidate only 4 bytes, so we flush the entire descriptor,
301 * which is 16 bytes in total. This is safe because the
302 * individual descriptors in the array are each aligned to
303 * ARCH_DMA_MINALIGN and padded appropriately.
Ian Campbell964ea7c2014-05-08 22:26:33 +0100304 */
Marek Vasut96cec172014-09-15 01:05:23 +0200305 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400306
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530307 /* Check if the descriptor is owned by CPU */
308 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
309 printf("CPU not owner of tx frame\n");
Simon Glass64dcd252015-04-05 16:07:40 -0600310 return -EPERM;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530311 }
312
Marek Vasut96cec172014-09-15 01:05:23 +0200313 memcpy(desc_p->dmamac_addr, packet, length);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530314
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400315 /* Flush data to be sent */
Marek Vasut96cec172014-09-15 01:05:23 +0200316 flush_dcache_range(data_start, data_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400317
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530318#if defined(CONFIG_DW_ALTDESCRIPTOR)
319 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
Marek Vasut2b261092015-12-20 03:59:23 +0100320 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530321 DESC_TXCTRL_SIZE1MASK;
322
323 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
324 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
325#else
Marek Vasut2b261092015-12-20 03:59:23 +0100326 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
327 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530328 DESC_TXCTRL_TXFIRST;
329
330 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
331#endif
332
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400333 /* Flush modified buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200334 flush_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400335
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530336 /* Test the wrap-around condition. */
337 if (++desc_num >= CONFIG_TX_DESCR_NUM)
338 desc_num = 0;
339
340 priv->tx_currdescnum = desc_num;
341
342 /* Start the transmission */
343 writel(POLL_DATA, &dma_p->txpolldemand);
344
345 return 0;
346}
347
Simon Glass75577ba2015-04-05 16:07:41 -0600348static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530349{
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400350 u32 status, desc_num = priv->rx_currdescnum;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530351 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
Simon Glass75577ba2015-04-05 16:07:41 -0600352 int length = -EAGAIN;
Marek Vasut96cec172014-09-15 01:05:23 +0200353 uint32_t desc_start = (uint32_t)desc_p;
354 uint32_t desc_end = desc_start +
355 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
356 uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
357 uint32_t data_end;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530358
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400359 /* Invalidate entire buffer descriptor */
Marek Vasut96cec172014-09-15 01:05:23 +0200360 invalidate_dcache_range(desc_start, desc_end);
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400361
362 status = desc_p->txrx_status;
363
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530364 /* Check if the owner is the CPU */
365 if (!(status & DESC_RXSTS_OWNBYDMA)) {
366
Marek Vasut2b261092015-12-20 03:59:23 +0100367 length = (status & DESC_RXSTS_FRMLENMSK) >>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530368 DESC_RXSTS_FRMLENSHFT;
369
Alexey Brodkin50b0df82014-01-22 20:49:09 +0400370 /* Invalidate received data */
Marek Vasut96cec172014-09-15 01:05:23 +0200371 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
372 invalidate_dcache_range(data_start, data_end);
Simon Glass75577ba2015-04-05 16:07:41 -0600373 *packetp = desc_p->dmamac_addr;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530374 }
375
Simon Glass75577ba2015-04-05 16:07:41 -0600376 return length;
377}
378
379static int _dw_free_pkt(struct dw_eth_dev *priv)
380{
381 u32 desc_num = priv->rx_currdescnum;
382 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
383 uint32_t desc_start = (uint32_t)desc_p;
384 uint32_t desc_end = desc_start +
385 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
386
387 /*
388 * Make the current descriptor valid again and go to
389 * the next one
390 */
391 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
392
393 /* Flush only status field - others weren't changed */
394 flush_dcache_range(desc_start, desc_end);
395
396 /* Test the wrap-around condition. */
397 if (++desc_num >= CONFIG_RX_DESCR_NUM)
398 desc_num = 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530399 priv->rx_currdescnum = desc_num;
400
Simon Glass75577ba2015-04-05 16:07:41 -0600401 return 0;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530402}
403
Simon Glass64dcd252015-04-05 16:07:40 -0600404static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530405{
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400406 struct phy_device *phydev;
407 int mask = 0xffffffff;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530408
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400409#ifdef CONFIG_PHY_ADDR
410 mask = 1 << CONFIG_PHY_ADDR;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530411#endif
412
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400413 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
414 if (!phydev)
Simon Glass64dcd252015-04-05 16:07:40 -0600415 return -ENODEV;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530416
Ian Campbell15e82e52014-04-28 20:14:05 +0100417 phy_connect_dev(phydev, dev);
418
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400419 phydev->supported &= PHY_GBIT_FEATURES;
420 phydev->advertising = phydev->supported;
421
422 priv->phydev = phydev;
423 phy_config(phydev);
424
Simon Glass64dcd252015-04-05 16:07:40 -0600425 return 0;
426}
427
Simon Glass75577ba2015-04-05 16:07:41 -0600428#ifndef CONFIG_DM_ETH
Simon Glass64dcd252015-04-05 16:07:40 -0600429static int dw_eth_init(struct eth_device *dev, bd_t *bis)
430{
431 return _dw_eth_init(dev->priv, dev->enetaddr);
432}
433
434static int dw_eth_send(struct eth_device *dev, void *packet, int length)
435{
436 return _dw_eth_send(dev->priv, packet, length);
437}
438
439static int dw_eth_recv(struct eth_device *dev)
440{
Simon Glass75577ba2015-04-05 16:07:41 -0600441 uchar *packet;
442 int length;
443
444 length = _dw_eth_recv(dev->priv, &packet);
445 if (length == -EAGAIN)
446 return 0;
447 net_process_received_packet(packet, length);
448
449 _dw_free_pkt(dev->priv);
450
451 return 0;
Simon Glass64dcd252015-04-05 16:07:40 -0600452}
453
454static void dw_eth_halt(struct eth_device *dev)
455{
456 return _dw_eth_halt(dev->priv);
457}
458
459static int dw_write_hwaddr(struct eth_device *dev)
460{
461 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530462}
463
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400464int designware_initialize(ulong base_addr, u32 interface)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530465{
466 struct eth_device *dev;
467 struct dw_eth_dev *priv;
468
469 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
470 if (!dev)
471 return -ENOMEM;
472
473 /*
474 * Since the priv structure contains the descriptors which need a strict
475 * buswidth alignment, memalign is used to allocate memory
476 */
Ian Campbell1c848a22014-05-08 22:26:32 +0100477 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
478 sizeof(struct dw_eth_dev));
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530479 if (!priv) {
480 free(dev);
481 return -ENOMEM;
482 }
483
484 memset(dev, 0, sizeof(struct eth_device));
485 memset(priv, 0, sizeof(struct dw_eth_dev));
486
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400487 sprintf(dev->name, "dwmac.%lx", base_addr);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530488 dev->iobase = (int)base_addr;
489 dev->priv = priv;
490
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530491 priv->dev = dev;
492 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
493 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
494 DW_DMA_BASE_OFFSET);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530495
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530496 dev->init = dw_eth_init;
497 dev->send = dw_eth_send;
498 dev->recv = dw_eth_recv;
499 dev->halt = dw_eth_halt;
500 dev->write_hwaddr = dw_write_hwaddr;
501
502 eth_register(dev);
503
Alexey Brodkin92a190a2014-01-22 20:54:06 +0400504 priv->interface = interface;
505
506 dw_mdio_init(dev->name, priv->mac_regs_p);
507 priv->bus = miiphy_get_dev_by_name(dev->name);
508
Simon Glass64dcd252015-04-05 16:07:40 -0600509 return dw_phy_init(priv, dev);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530510}
Simon Glass75577ba2015-04-05 16:07:41 -0600511#endif
512
513#ifdef CONFIG_DM_ETH
514static int designware_eth_start(struct udevice *dev)
515{
516 struct eth_pdata *pdata = dev_get_platdata(dev);
517
518 return _dw_eth_init(dev->priv, pdata->enetaddr);
519}
520
521static int designware_eth_send(struct udevice *dev, void *packet, int length)
522{
523 struct dw_eth_dev *priv = dev_get_priv(dev);
524
525 return _dw_eth_send(priv, packet, length);
526}
527
Simon Glassa1ca92e2015-07-06 16:47:49 -0600528static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Simon Glass75577ba2015-04-05 16:07:41 -0600529{
530 struct dw_eth_dev *priv = dev_get_priv(dev);
531
532 return _dw_eth_recv(priv, packetp);
533}
534
535static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
536 int length)
537{
538 struct dw_eth_dev *priv = dev_get_priv(dev);
539
540 return _dw_free_pkt(priv);
541}
542
543static void designware_eth_stop(struct udevice *dev)
544{
545 struct dw_eth_dev *priv = dev_get_priv(dev);
546
547 return _dw_eth_halt(priv);
548}
549
550static int designware_eth_write_hwaddr(struct udevice *dev)
551{
552 struct eth_pdata *pdata = dev_get_platdata(dev);
553 struct dw_eth_dev *priv = dev_get_priv(dev);
554
555 return _dw_write_hwaddr(priv, pdata->enetaddr);
556}
557
Bin Meng8b7ee662015-09-11 03:24:35 -0700558static int designware_eth_bind(struct udevice *dev)
559{
560#ifdef CONFIG_DM_PCI
561 static int num_cards;
562 char name[20];
563
564 /* Create a unique device name for PCI type devices */
565 if (device_is_on_pci_bus(dev)) {
566 sprintf(name, "eth_designware#%u", num_cards++);
567 device_set_name(dev, name);
568 }
569#endif
570
571 return 0;
572}
573
Simon Glass75577ba2015-04-05 16:07:41 -0600574static int designware_eth_probe(struct udevice *dev)
575{
576 struct eth_pdata *pdata = dev_get_platdata(dev);
577 struct dw_eth_dev *priv = dev_get_priv(dev);
Bin Mengf0dc73c2015-09-03 05:37:29 -0700578 u32 iobase = pdata->iobase;
Simon Glass75577ba2015-04-05 16:07:41 -0600579 int ret;
580
Bin Meng8b7ee662015-09-11 03:24:35 -0700581#ifdef CONFIG_DM_PCI
582 /*
583 * If we are on PCI bus, either directly attached to a PCI root port,
584 * or via a PCI bridge, fill in platdata before we probe the hardware.
585 */
586 if (device_is_on_pci_bus(dev)) {
Simon Glass21ccce12015-11-29 13:17:47 -0700587 pci_dev_t bdf = dm_pci_get_bdf(dev);
Bin Meng8b7ee662015-09-11 03:24:35 -0700588
589 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
590 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
591 iobase = pci_mem_to_phys(bdf, iobase);
592
593 pdata->iobase = iobase;
594 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
595 }
596#endif
597
Bin Mengf0dc73c2015-09-03 05:37:29 -0700598 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
599 priv->mac_regs_p = (struct eth_mac_regs *)iobase;
600 priv->dma_regs_p = (struct eth_dma_regs *)(iobase + DW_DMA_BASE_OFFSET);
Simon Glass75577ba2015-04-05 16:07:41 -0600601 priv->interface = pdata->phy_interface;
602
603 dw_mdio_init(dev->name, priv->mac_regs_p);
604 priv->bus = miiphy_get_dev_by_name(dev->name);
605
606 ret = dw_phy_init(priv, dev);
607 debug("%s, ret=%d\n", __func__, ret);
608
609 return ret;
610}
611
Bin Meng5d2459f2015-10-07 21:32:38 -0700612static int designware_eth_remove(struct udevice *dev)
613{
614 struct dw_eth_dev *priv = dev_get_priv(dev);
615
616 free(priv->phydev);
617 mdio_unregister(priv->bus);
618 mdio_free(priv->bus);
619
620 return 0;
621}
622
Simon Glass75577ba2015-04-05 16:07:41 -0600623static const struct eth_ops designware_eth_ops = {
624 .start = designware_eth_start,
625 .send = designware_eth_send,
626 .recv = designware_eth_recv,
627 .free_pkt = designware_eth_free_pkt,
628 .stop = designware_eth_stop,
629 .write_hwaddr = designware_eth_write_hwaddr,
630};
631
632static int designware_eth_ofdata_to_platdata(struct udevice *dev)
633{
634 struct eth_pdata *pdata = dev_get_platdata(dev);
635 const char *phy_mode;
636
637 pdata->iobase = dev_get_addr(dev);
638 pdata->phy_interface = -1;
639 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
640 if (phy_mode)
641 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
642 if (pdata->phy_interface == -1) {
643 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
644 return -EINVAL;
645 }
646
647 return 0;
648}
649
650static const struct udevice_id designware_eth_ids[] = {
651 { .compatible = "allwinner,sun7i-a20-gmac" },
Marek Vasutb9628592015-07-25 18:38:44 +0200652 { .compatible = "altr,socfpga-stmmac" },
Simon Glass75577ba2015-04-05 16:07:41 -0600653 { }
654};
655
Marek Vasut9f76f102015-07-25 18:42:34 +0200656U_BOOT_DRIVER(eth_designware) = {
Simon Glass75577ba2015-04-05 16:07:41 -0600657 .name = "eth_designware",
658 .id = UCLASS_ETH,
659 .of_match = designware_eth_ids,
660 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
Bin Meng8b7ee662015-09-11 03:24:35 -0700661 .bind = designware_eth_bind,
Simon Glass75577ba2015-04-05 16:07:41 -0600662 .probe = designware_eth_probe,
Bin Meng5d2459f2015-10-07 21:32:38 -0700663 .remove = designware_eth_remove,
Simon Glass75577ba2015-04-05 16:07:41 -0600664 .ops = &designware_eth_ops,
665 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
666 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
667 .flags = DM_FLAG_ALLOC_PRIV_DMA,
668};
Bin Meng8b7ee662015-09-11 03:24:35 -0700669
670static struct pci_device_id supported[] = {
671 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
672 { }
673};
674
675U_BOOT_PCI_DEVICE(eth_designware, supported);
Simon Glass75577ba2015-04-05 16:07:41 -0600676#endif