blob: 46f6601fa32e4873f360613f003615d9393b7539 [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Designware ethernet IP driver for u-boot
26 */
27
28#include <common.h>
29#include <miiphy.h>
30#include <malloc.h>
Stefan Roeseef760252012-05-07 12:04:25 +020031#include <linux/compiler.h>
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053032#include <linux/err.h>
33#include <asm/io.h>
34#include "designware.h"
35
Vipin Kumar13edd172012-03-26 00:09:56 +000036static int configure_phy(struct eth_device *dev);
37
Vipin KUMAR5b1b1882010-06-29 10:53:34 +053038static void tx_descs_init(struct eth_device *dev)
39{
40 struct dw_eth_dev *priv = dev->priv;
41 struct eth_dma_regs *dma_p = priv->dma_regs_p;
42 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
43 char *txbuffs = &priv->txbuffs[0];
44 struct dmamacdescr *desc_p;
45 u32 idx;
46
47 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
48 desc_p = &desc_table_p[idx];
49 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
50 desc_p->dmamac_next = &desc_table_p[idx + 1];
51
52#if defined(CONFIG_DW_ALTDESCRIPTOR)
53 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
54 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
55 DESC_TXSTS_TXCHECKINSCTRL | \
56 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
57
58 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
59 desc_p->dmamac_cntl = 0;
60 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
61#else
62 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
63 desc_p->txrx_status = 0;
64#endif
65 }
66
67 /* Correcting the last pointer of the chain */
68 desc_p->dmamac_next = &desc_table_p[0];
69
70 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
71}
72
73static void rx_descs_init(struct eth_device *dev)
74{
75 struct dw_eth_dev *priv = dev->priv;
76 struct eth_dma_regs *dma_p = priv->dma_regs_p;
77 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
78 char *rxbuffs = &priv->rxbuffs[0];
79 struct dmamacdescr *desc_p;
80 u32 idx;
81
82 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
83 desc_p = &desc_table_p[idx];
84 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
85 desc_p->dmamac_next = &desc_table_p[idx + 1];
86
87 desc_p->dmamac_cntl =
88 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
89 DESC_RXCTRL_RXCHAIN;
90
91 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
92 }
93
94 /* Correcting the last pointer of the chain */
95 desc_p->dmamac_next = &desc_table_p[0];
96
97 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
98}
99
100static void descs_init(struct eth_device *dev)
101{
102 tx_descs_init(dev);
103 rx_descs_init(dev);
104}
105
106static int mac_reset(struct eth_device *dev)
107{
108 struct dw_eth_dev *priv = dev->priv;
109 struct eth_mac_regs *mac_p = priv->mac_regs_p;
110 struct eth_dma_regs *dma_p = priv->dma_regs_p;
111
Amit Virdicafabe12012-03-26 00:09:59 +0000112 ulong start;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530113 int timeout = CONFIG_MACRESET_TIMEOUT;
114
115 writel(DMAMAC_SRST, &dma_p->busmode);
Vipin Kumar70919152012-12-13 17:22:51 +0530116
117 if (priv->interface != PHY_INTERFACE_MODE_RGMII)
118 writel(MII_PORTSELECT, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530119
Amit Virdicafabe12012-03-26 00:09:59 +0000120 start = get_timer(0);
121 while (get_timer(start) < timeout) {
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530122 if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
123 return 0;
Amit Virdicafabe12012-03-26 00:09:59 +0000124
125 /* Try again after 10usec */
126 udelay(10);
127 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530128
129 return -1;
130}
131
132static int dw_write_hwaddr(struct eth_device *dev)
133{
134 struct dw_eth_dev *priv = dev->priv;
135 struct eth_mac_regs *mac_p = priv->mac_regs_p;
136 u32 macid_lo, macid_hi;
137 u8 *mac_id = &dev->enetaddr[0];
138
139 macid_lo = mac_id[0] + (mac_id[1] << 8) + \
140 (mac_id[2] << 16) + (mac_id[3] << 24);
141 macid_hi = mac_id[4] + (mac_id[5] << 8);
142
143 writel(macid_hi, &mac_p->macaddr0hi);
144 writel(macid_lo, &mac_p->macaddr0lo);
145
146 return 0;
147}
148
149static int dw_eth_init(struct eth_device *dev, bd_t *bis)
150{
151 struct dw_eth_dev *priv = dev->priv;
152 struct eth_mac_regs *mac_p = priv->mac_regs_p;
153 struct eth_dma_regs *dma_p = priv->dma_regs_p;
154 u32 conf;
155
Vipin Kumar13edd172012-03-26 00:09:56 +0000156 if (priv->phy_configured != 1)
157 configure_phy(dev);
158
Stefan Roeseef760252012-05-07 12:04:25 +0200159 /* Print link status only once */
160 if (!priv->link_printed) {
161 printf("ENET Speed is %d Mbps - %s duplex connection\n",
162 priv->speed, (priv->duplex == HALF) ? "HALF" : "FULL");
163 priv->link_printed = 1;
164 }
165
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530166 /* Reset ethernet hardware */
167 if (mac_reset(dev) < 0)
168 return -1;
169
Vipin KUMARc7f6dbe2012-03-26 00:09:52 +0000170 /* Resore the HW MAC address as it has been lost during MAC reset */
171 dw_write_hwaddr(dev);
172
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530173 writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
174 &dma_p->busmode);
175
Dinh Nguyen66f119e2012-06-08 05:26:52 +0000176 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD |
177 TXSECONDFRAME, &dma_p->opmode);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530178
179 conf = FRAMEBURSTENABLE | DISABLERXOWN;
180
Stefan Roeseef760252012-05-07 12:04:25 +0200181 if (priv->speed != 1000)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530182 conf |= MII_PORTSELECT;
183
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530184 if ((priv->interface != PHY_INTERFACE_MODE_MII) &&
185 (priv->interface != PHY_INTERFACE_MODE_GMII)) {
186
Stefan Roeseef760252012-05-07 12:04:25 +0200187 if (priv->speed == 100)
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530188 conf |= FES_100;
189 }
190
Stefan Roeseef760252012-05-07 12:04:25 +0200191 if (priv->duplex == FULL)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530192 conf |= FULLDPLXMODE;
193
194 writel(conf, &mac_p->conf);
195
196 descs_init(dev);
197
198 /*
199 * Start/Enable xfer at dma as well as mac level
200 */
201 writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
202 writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
203
Armando Viscontiaa510052012-03-26 00:09:55 +0000204 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530205
206 return 0;
207}
208
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000209static int dw_eth_send(struct eth_device *dev, void *packet, int length)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530210{
211 struct dw_eth_dev *priv = dev->priv;
212 struct eth_dma_regs *dma_p = priv->dma_regs_p;
213 u32 desc_num = priv->tx_currdescnum;
214 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
215
216 /* Check if the descriptor is owned by CPU */
217 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
218 printf("CPU not owner of tx frame\n");
219 return -1;
220 }
221
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000222 memcpy((void *)desc_p->dmamac_addr, packet, length);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530223
224#if defined(CONFIG_DW_ALTDESCRIPTOR)
225 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
226 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
227 DESC_TXCTRL_SIZE1MASK;
228
229 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
230 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
231#else
232 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
233 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
234 DESC_TXCTRL_TXFIRST;
235
236 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
237#endif
238
239 /* Test the wrap-around condition. */
240 if (++desc_num >= CONFIG_TX_DESCR_NUM)
241 desc_num = 0;
242
243 priv->tx_currdescnum = desc_num;
244
245 /* Start the transmission */
246 writel(POLL_DATA, &dma_p->txpolldemand);
247
248 return 0;
249}
250
251static int dw_eth_recv(struct eth_device *dev)
252{
253 struct dw_eth_dev *priv = dev->priv;
254 u32 desc_num = priv->rx_currdescnum;
255 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
256
257 u32 status = desc_p->txrx_status;
258 int length = 0;
259
260 /* Check if the owner is the CPU */
261 if (!(status & DESC_RXSTS_OWNBYDMA)) {
262
263 length = (status & DESC_RXSTS_FRMLENMSK) >> \
264 DESC_RXSTS_FRMLENSHFT;
265
266 NetReceive(desc_p->dmamac_addr, length);
267
268 /*
269 * Make the current descriptor valid again and go to
270 * the next one
271 */
272 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
273
274 /* Test the wrap-around condition. */
275 if (++desc_num >= CONFIG_RX_DESCR_NUM)
276 desc_num = 0;
277 }
278
279 priv->rx_currdescnum = desc_num;
280
281 return length;
282}
283
284static void dw_eth_halt(struct eth_device *dev)
285{
286 struct dw_eth_dev *priv = dev->priv;
287
288 mac_reset(dev);
289 priv->tx_currdescnum = priv->rx_currdescnum = 0;
290}
291
292static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
293{
294 struct dw_eth_dev *priv = dev->priv;
295 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Amit Virdicafabe12012-03-26 00:09:59 +0000296 ulong start;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530297 u32 miiaddr;
298 int timeout = CONFIG_MDIO_TIMEOUT;
299
300 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
301 ((reg << MIIREGSHIFT) & MII_REGMSK);
302
303 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
304
Amit Virdicafabe12012-03-26 00:09:59 +0000305 start = get_timer(0);
306 while (get_timer(start) < timeout) {
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530307 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
308 *val = readl(&mac_p->miidata);
309 return 0;
310 }
Amit Virdicafabe12012-03-26 00:09:59 +0000311
312 /* Try again after 10usec */
313 udelay(10);
314 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530315
316 return -1;
317}
318
319static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
320{
321 struct dw_eth_dev *priv = dev->priv;
322 struct eth_mac_regs *mac_p = priv->mac_regs_p;
Amit Virdicafabe12012-03-26 00:09:59 +0000323 ulong start;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530324 u32 miiaddr;
325 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
326 u16 value;
327
328 writel(val, &mac_p->miidata);
329 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
330 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
331
332 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
333
Amit Virdicafabe12012-03-26 00:09:59 +0000334 start = get_timer(0);
335 while (get_timer(start) < timeout) {
Vipin KUMARc7f6dbe2012-03-26 00:09:52 +0000336 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530337 ret = 0;
Vipin KUMARc7f6dbe2012-03-26 00:09:52 +0000338 break;
339 }
Amit Virdicafabe12012-03-26 00:09:59 +0000340
341 /* Try again after 10usec */
342 udelay(10);
343 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530344
345 /* Needed as a fix for ST-Phy */
346 eth_mdio_read(dev, addr, reg, &value);
347
348 return ret;
349}
350
351#if defined(CONFIG_DW_SEARCH_PHY)
352static int find_phy(struct eth_device *dev)
353{
354 int phy_addr = 0;
355 u16 ctrl, oldctrl;
356
357 do {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500358 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
359 oldctrl = ctrl & BMCR_ANENABLE;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530360
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500361 ctrl ^= BMCR_ANENABLE;
362 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
363 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
364 ctrl &= BMCR_ANENABLE;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530365
366 if (ctrl == oldctrl) {
367 phy_addr++;
368 } else {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500369 ctrl ^= BMCR_ANENABLE;
370 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530371
372 return phy_addr;
373 }
374 } while (phy_addr < 32);
375
376 return -1;
377}
378#endif
379
380static int dw_reset_phy(struct eth_device *dev)
381{
382 struct dw_eth_dev *priv = dev->priv;
383 u16 ctrl;
Amit Virdicafabe12012-03-26 00:09:59 +0000384 ulong start;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530385 int timeout = CONFIG_PHYRESET_TIMEOUT;
386 u32 phy_addr = priv->address;
387
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500388 eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
Amit Virdicafabe12012-03-26 00:09:59 +0000389
390 start = get_timer(0);
391 while (get_timer(start) < timeout) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500392 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
393 if (!(ctrl & BMCR_RESET))
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530394 break;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530395
Amit Virdicafabe12012-03-26 00:09:59 +0000396 /* Try again after 10usec */
397 udelay(10);
398 };
399
400 if (get_timer(start) >= CONFIG_PHYRESET_TIMEOUT)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530401 return -1;
402
403#ifdef CONFIG_PHY_RESET_DELAY
404 udelay(CONFIG_PHY_RESET_DELAY);
405#endif
406 return 0;
407}
408
Stefan Roeseef760252012-05-07 12:04:25 +0200409/*
410 * Add weak default function for board specific PHY configuration
411 */
412int __weak designware_board_phy_init(struct eth_device *dev, int phy_addr,
413 int (*mii_write)(struct eth_device *, u8, u8, u16),
414 int dw_reset_phy(struct eth_device *))
415{
416 return 0;
417}
418
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530419static int configure_phy(struct eth_device *dev)
420{
421 struct dw_eth_dev *priv = dev->priv;
422 int phy_addr;
Mike Frysingeree7f5bf2011-06-02 05:19:37 +0000423 u16 bmcr;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530424#if defined(CONFIG_DW_AUTONEG)
425 u16 bmsr;
426 u32 timeout;
Amit Virdicafabe12012-03-26 00:09:59 +0000427 ulong start;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530428#endif
429
430#if defined(CONFIG_DW_SEARCH_PHY)
431 phy_addr = find_phy(dev);
Vipin KUMAR024333c2012-03-26 00:09:54 +0000432 if (phy_addr >= 0)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530433 priv->address = phy_addr;
434 else
435 return -1;
Mike Frysingerf0ece9e2011-06-02 05:19:38 +0000436#else
437 phy_addr = priv->address;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530438#endif
Stefan Roeseef760252012-05-07 12:04:25 +0200439
440 /*
441 * Some boards need board specific PHY initialization. This is
442 * after the main driver init code but before the auto negotiation
443 * is run.
444 */
445 if (designware_board_phy_init(dev, phy_addr,
446 eth_mdio_write, dw_reset_phy) < 0)
447 return -1;
448
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530449 if (dw_reset_phy(dev) < 0)
450 return -1;
451
452#if defined(CONFIG_DW_AUTONEG)
Armando Visconti20a5dde2012-03-26 00:09:58 +0000453 /* Set Auto-Neg Advertisement capabilities to 10/100 half/full */
454 eth_mdio_write(dev, phy_addr, MII_ADVERTISE, 0x1E1);
455
Vikas Manochae25c90b2012-03-26 00:09:57 +0000456 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530457#else
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500458 bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530459
460#if defined(CONFIG_DW_SPEED10M)
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500461 bmcr &= ~BMCR_SPEED100;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530462#endif
463#if defined(CONFIG_DW_DUPLEXHALF)
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500464 bmcr &= ~BMCR_FULLDPLX;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530465#endif
466#endif
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500467 if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530468 return -1;
469
470 /* Read the phy status register and populate priv structure */
471#if defined(CONFIG_DW_AUTONEG)
472 timeout = CONFIG_AUTONEG_TIMEOUT;
Amit Virdicafabe12012-03-26 00:09:59 +0000473 start = get_timer(0);
Stefan Roeseef760252012-05-07 12:04:25 +0200474 puts("Waiting for PHY auto negotiation to complete");
Amit Virdicafabe12012-03-26 00:09:59 +0000475 while (get_timer(start) < timeout) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500476 eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
Stefan Roeseef760252012-05-07 12:04:25 +0200477 if (bmsr & BMSR_ANEGCOMPLETE) {
478 priv->phy_configured = 1;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530479 break;
Stefan Roeseef760252012-05-07 12:04:25 +0200480 }
Amit Virdicafabe12012-03-26 00:09:59 +0000481
Stefan Roeseef760252012-05-07 12:04:25 +0200482 /* Print dot all 1s to show progress */
483 if ((get_timer(start) % 1000) == 0)
484 putc('.');
485
486 /* Try again after 1msec */
487 udelay(1000);
Amit Virdicafabe12012-03-26 00:09:59 +0000488 };
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530489
Stefan Roeseef760252012-05-07 12:04:25 +0200490 if (!(bmsr & BMSR_ANEGCOMPLETE))
491 puts(" TIMEOUT!\n");
492 else
493 puts(" done\n");
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530494#else
Vipin Kumar13edd172012-03-26 00:09:56 +0000495 priv->phy_configured = 1;
Stefan Roeseef760252012-05-07 12:04:25 +0200496#endif
497
498 priv->speed = miiphy_speed(dev->name, phy_addr);
499 priv->duplex = miiphy_duplex(dev->name, phy_addr);
Vipin Kumar13edd172012-03-26 00:09:56 +0000500
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530501 return 0;
502}
503
504#if defined(CONFIG_MII)
Mike Frysinger5700bb62010-07-27 18:35:08 -0400505static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530506{
507 struct eth_device *dev;
508
509 dev = eth_get_dev_by_name(devname);
510 if (dev)
511 eth_mdio_read(dev, addr, reg, val);
512
513 return 0;
514}
515
Mike Frysinger5700bb62010-07-27 18:35:08 -0400516static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530517{
518 struct eth_device *dev;
519
520 dev = eth_get_dev_by_name(devname);
521 if (dev)
522 eth_mdio_write(dev, addr, reg, val);
523
524 return 0;
525}
526#endif
527
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530528int designware_initialize(u32 id, ulong base_addr, u32 phy_addr, u32 interface)
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530529{
530 struct eth_device *dev;
531 struct dw_eth_dev *priv;
532
533 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
534 if (!dev)
535 return -ENOMEM;
536
537 /*
538 * Since the priv structure contains the descriptors which need a strict
539 * buswidth alignment, memalign is used to allocate memory
540 */
541 priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
542 if (!priv) {
543 free(dev);
544 return -ENOMEM;
545 }
546
547 memset(dev, 0, sizeof(struct eth_device));
548 memset(priv, 0, sizeof(struct dw_eth_dev));
549
550 sprintf(dev->name, "mii%d", id);
551 dev->iobase = (int)base_addr;
552 dev->priv = priv;
553
Simon Glass7616e782011-06-13 16:13:10 -0700554 eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530555
556 priv->dev = dev;
557 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
558 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
559 DW_DMA_BASE_OFFSET);
560 priv->address = phy_addr;
Vipin Kumar13edd172012-03-26 00:09:56 +0000561 priv->phy_configured = 0;
Vipin Kumar9afc1af2012-05-07 13:06:44 +0530562 priv->interface = interface;
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530563
Vipin KUMAR5b1b1882010-06-29 10:53:34 +0530564 dev->init = dw_eth_init;
565 dev->send = dw_eth_send;
566 dev->recv = dw_eth_recv;
567 dev->halt = dw_eth_halt;
568 dev->write_hwaddr = dw_write_hwaddr;
569
570 eth_register(dev);
571
572#if defined(CONFIG_MII)
573 miiphy_register(dev->name, dw_mii_read, dw_mii_write);
574#endif
575 return 1;
576}