]> git.sur5r.net Git - u-boot/blob - drivers/net/designware.c
net/designware: Consecutive writes must have delay
[u-boot] / drivers / net / designware.c
1 /*
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>
31 #include <linux/err.h>
32 #include <asm/io.h>
33 #include "designware.h"
34
35 static void tx_descs_init(struct eth_device *dev)
36 {
37         struct dw_eth_dev *priv = dev->priv;
38         struct eth_dma_regs *dma_p = priv->dma_regs_p;
39         struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
40         char *txbuffs = &priv->txbuffs[0];
41         struct dmamacdescr *desc_p;
42         u32 idx;
43
44         for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
45                 desc_p = &desc_table_p[idx];
46                 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
47                 desc_p->dmamac_next = &desc_table_p[idx + 1];
48
49 #if defined(CONFIG_DW_ALTDESCRIPTOR)
50                 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
51                                 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
52                                 DESC_TXSTS_TXCHECKINSCTRL | \
53                                 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
54
55                 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
56                 desc_p->dmamac_cntl = 0;
57                 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
58 #else
59                 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
60                 desc_p->txrx_status = 0;
61 #endif
62         }
63
64         /* Correcting the last pointer of the chain */
65         desc_p->dmamac_next = &desc_table_p[0];
66
67         writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
68 }
69
70 static void rx_descs_init(struct eth_device *dev)
71 {
72         struct dw_eth_dev *priv = dev->priv;
73         struct eth_dma_regs *dma_p = priv->dma_regs_p;
74         struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
75         char *rxbuffs = &priv->rxbuffs[0];
76         struct dmamacdescr *desc_p;
77         u32 idx;
78
79         for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
80                 desc_p = &desc_table_p[idx];
81                 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
82                 desc_p->dmamac_next = &desc_table_p[idx + 1];
83
84                 desc_p->dmamac_cntl =
85                         (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
86                                       DESC_RXCTRL_RXCHAIN;
87
88                 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
89         }
90
91         /* Correcting the last pointer of the chain */
92         desc_p->dmamac_next = &desc_table_p[0];
93
94         writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
95 }
96
97 static void descs_init(struct eth_device *dev)
98 {
99         tx_descs_init(dev);
100         rx_descs_init(dev);
101 }
102
103 static int mac_reset(struct eth_device *dev)
104 {
105         struct dw_eth_dev *priv = dev->priv;
106         struct eth_mac_regs *mac_p = priv->mac_regs_p;
107         struct eth_dma_regs *dma_p = priv->dma_regs_p;
108
109         int timeout = CONFIG_MACRESET_TIMEOUT;
110
111         writel(DMAMAC_SRST, &dma_p->busmode);
112         writel(MII_PORTSELECT, &mac_p->conf);
113
114         do {
115                 if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
116                         return 0;
117                 udelay(1000);
118         } while (timeout--);
119
120         return -1;
121 }
122
123 static int dw_write_hwaddr(struct eth_device *dev)
124 {
125         struct dw_eth_dev *priv = dev->priv;
126         struct eth_mac_regs *mac_p = priv->mac_regs_p;
127         u32 macid_lo, macid_hi;
128         u8 *mac_id = &dev->enetaddr[0];
129
130         macid_lo = mac_id[0] + (mac_id[1] << 8) + \
131                    (mac_id[2] << 16) + (mac_id[3] << 24);
132         macid_hi = mac_id[4] + (mac_id[5] << 8);
133
134         writel(macid_hi, &mac_p->macaddr0hi);
135         writel(macid_lo, &mac_p->macaddr0lo);
136
137         return 0;
138 }
139
140 static int dw_eth_init(struct eth_device *dev, bd_t *bis)
141 {
142         struct dw_eth_dev *priv = dev->priv;
143         struct eth_mac_regs *mac_p = priv->mac_regs_p;
144         struct eth_dma_regs *dma_p = priv->dma_regs_p;
145         u32 conf;
146
147         /* Reset ethernet hardware */
148         if (mac_reset(dev) < 0)
149                 return -1;
150
151         /* Resore the HW MAC address as it has been lost during MAC reset */
152         dw_write_hwaddr(dev);
153
154         writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
155                         &dma_p->busmode);
156
157         writel(FLUSHTXFIFO | readl(&dma_p->opmode), &dma_p->opmode);
158         writel(STOREFORWARD | TXSECONDFRAME, &dma_p->opmode);
159
160         conf = FRAMEBURSTENABLE | DISABLERXOWN;
161
162         if (priv->speed != SPEED_1000M)
163                 conf |= MII_PORTSELECT;
164
165         if (priv->duplex == FULL_DUPLEX)
166                 conf |= FULLDPLXMODE;
167
168         writel(conf, &mac_p->conf);
169
170         descs_init(dev);
171
172         /*
173          * Start/Enable xfer at dma as well as mac level
174          */
175         writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
176         writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
177
178         writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
179
180         return 0;
181 }
182
183 static int dw_eth_send(struct eth_device *dev, volatile void *packet,
184                 int length)
185 {
186         struct dw_eth_dev *priv = dev->priv;
187         struct eth_dma_regs *dma_p = priv->dma_regs_p;
188         u32 desc_num = priv->tx_currdescnum;
189         struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
190
191         /* Check if the descriptor is owned by CPU */
192         if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
193                 printf("CPU not owner of tx frame\n");
194                 return -1;
195         }
196
197         memcpy((void *)desc_p->dmamac_addr, (void *)packet, length);
198
199 #if defined(CONFIG_DW_ALTDESCRIPTOR)
200         desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
201         desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
202                                DESC_TXCTRL_SIZE1MASK;
203
204         desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
205         desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
206 #else
207         desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
208                                DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
209                                DESC_TXCTRL_TXFIRST;
210
211         desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
212 #endif
213
214         /* Test the wrap-around condition. */
215         if (++desc_num >= CONFIG_TX_DESCR_NUM)
216                 desc_num = 0;
217
218         priv->tx_currdescnum = desc_num;
219
220         /* Start the transmission */
221         writel(POLL_DATA, &dma_p->txpolldemand);
222
223         return 0;
224 }
225
226 static int dw_eth_recv(struct eth_device *dev)
227 {
228         struct dw_eth_dev *priv = dev->priv;
229         u32 desc_num = priv->rx_currdescnum;
230         struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
231
232         u32 status = desc_p->txrx_status;
233         int length = 0;
234
235         /* Check  if the owner is the CPU */
236         if (!(status & DESC_RXSTS_OWNBYDMA)) {
237
238                 length = (status & DESC_RXSTS_FRMLENMSK) >> \
239                          DESC_RXSTS_FRMLENSHFT;
240
241                 NetReceive(desc_p->dmamac_addr, length);
242
243                 /*
244                  * Make the current descriptor valid again and go to
245                  * the next one
246                  */
247                 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
248
249                 /* Test the wrap-around condition. */
250                 if (++desc_num >= CONFIG_RX_DESCR_NUM)
251                         desc_num = 0;
252         }
253
254         priv->rx_currdescnum = desc_num;
255
256         return length;
257 }
258
259 static void dw_eth_halt(struct eth_device *dev)
260 {
261         struct dw_eth_dev *priv = dev->priv;
262
263         mac_reset(dev);
264         priv->tx_currdescnum = priv->rx_currdescnum = 0;
265 }
266
267 static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
268 {
269         struct dw_eth_dev *priv = dev->priv;
270         struct eth_mac_regs *mac_p = priv->mac_regs_p;
271         u32 miiaddr;
272         int timeout = CONFIG_MDIO_TIMEOUT;
273
274         miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
275                   ((reg << MIIREGSHIFT) & MII_REGMSK);
276
277         writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
278
279         do {
280                 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
281                         *val = readl(&mac_p->miidata);
282                         return 0;
283                 }
284                 udelay(1000);
285         } while (timeout--);
286
287         return -1;
288 }
289
290 static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
291 {
292         struct dw_eth_dev *priv = dev->priv;
293         struct eth_mac_regs *mac_p = priv->mac_regs_p;
294         u32 miiaddr;
295         int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
296         u16 value;
297
298         writel(val, &mac_p->miidata);
299         miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
300                   ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
301
302         writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
303
304         do {
305                 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
306                         ret = 0;
307                         break;
308                 }
309                 udelay(1000);
310         } while (timeout--);
311
312         /* Needed as a fix for ST-Phy */
313         eth_mdio_read(dev, addr, reg, &value);
314
315         return ret;
316 }
317
318 #if defined(CONFIG_DW_SEARCH_PHY)
319 static int find_phy(struct eth_device *dev)
320 {
321         int phy_addr = 0;
322         u16 ctrl, oldctrl;
323
324         do {
325                 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
326                 oldctrl = ctrl & BMCR_ANENABLE;
327
328                 ctrl ^= BMCR_ANENABLE;
329                 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
330                 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
331                 ctrl &= BMCR_ANENABLE;
332
333                 if (ctrl == oldctrl) {
334                         phy_addr++;
335                 } else {
336                         ctrl ^= BMCR_ANENABLE;
337                         eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
338
339                         return phy_addr;
340                 }
341         } while (phy_addr < 32);
342
343         return -1;
344 }
345 #endif
346
347 static int dw_reset_phy(struct eth_device *dev)
348 {
349         struct dw_eth_dev *priv = dev->priv;
350         u16 ctrl;
351         int timeout = CONFIG_PHYRESET_TIMEOUT;
352         u32 phy_addr = priv->address;
353
354         eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
355         do {
356                 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
357                 if (!(ctrl & BMCR_RESET))
358                         break;
359                 udelay(1000);
360         } while (timeout--);
361
362         if (timeout < 0)
363                 return -1;
364
365 #ifdef CONFIG_PHY_RESET_DELAY
366         udelay(CONFIG_PHY_RESET_DELAY);
367 #endif
368         return 0;
369 }
370
371 static int configure_phy(struct eth_device *dev)
372 {
373         struct dw_eth_dev *priv = dev->priv;
374         int phy_addr;
375         u16 bmcr;
376 #if defined(CONFIG_DW_AUTONEG)
377         u16 bmsr;
378         u32 timeout;
379         u16 anlpar, btsr;
380 #else
381         u16 ctrl;
382 #endif
383
384 #if defined(CONFIG_DW_SEARCH_PHY)
385         phy_addr = find_phy(dev);
386         if (phy_addr >= 0)
387                 priv->address = phy_addr;
388         else
389                 return -1;
390 #else
391         phy_addr = priv->address;
392 #endif
393         if (dw_reset_phy(dev) < 0)
394                 return -1;
395
396 #if defined(CONFIG_DW_AUTONEG)
397         bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_SPEED100 | \
398                BMCR_FULLDPLX | BMCR_SPEED1000;
399 #else
400         bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
401
402 #if defined(CONFIG_DW_SPEED10M)
403         bmcr &= ~BMCR_SPEED100;
404 #endif
405 #if defined(CONFIG_DW_DUPLEXHALF)
406         bmcr &= ~BMCR_FULLDPLX;
407 #endif
408 #endif
409         if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
410                 return -1;
411
412         /* Read the phy status register and populate priv structure */
413 #if defined(CONFIG_DW_AUTONEG)
414         timeout = CONFIG_AUTONEG_TIMEOUT;
415         do {
416                 eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
417                 if (bmsr & BMSR_ANEGCOMPLETE)
418                         break;
419                 udelay(1000);
420         } while (timeout--);
421
422         eth_mdio_read(dev, phy_addr, MII_LPA, &anlpar);
423         eth_mdio_read(dev, phy_addr, MII_STAT1000, &btsr);
424
425         if (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
426                 priv->speed = SPEED_1000M;
427                 if (btsr & PHY_1000BTSR_1000FD)
428                         priv->duplex = FULL_DUPLEX;
429                 else
430                         priv->duplex = HALF_DUPLEX;
431         } else {
432                 if (anlpar & LPA_100)
433                         priv->speed = SPEED_100M;
434                 else
435                         priv->speed = SPEED_10M;
436
437                 if (anlpar & (LPA_10FULL | LPA_100FULL))
438                         priv->duplex = FULL_DUPLEX;
439                 else
440                         priv->duplex = HALF_DUPLEX;
441         }
442 #else
443         if (eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl) < 0)
444                 return -1;
445
446         if (ctrl & BMCR_FULLDPLX)
447                 priv->duplex = FULL_DUPLEX;
448         else
449                 priv->duplex = HALF_DUPLEX;
450
451         if (ctrl & BMCR_SPEED1000)
452                 priv->speed = SPEED_1000M;
453         else if (ctrl & BMCR_SPEED100)
454                 priv->speed = SPEED_100M;
455         else
456                 priv->speed = SPEED_10M;
457 #endif
458         return 0;
459 }
460
461 #if defined(CONFIG_MII)
462 static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
463 {
464         struct eth_device *dev;
465
466         dev = eth_get_dev_by_name(devname);
467         if (dev)
468                 eth_mdio_read(dev, addr, reg, val);
469
470         return 0;
471 }
472
473 static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
474 {
475         struct eth_device *dev;
476
477         dev = eth_get_dev_by_name(devname);
478         if (dev)
479                 eth_mdio_write(dev, addr, reg, val);
480
481         return 0;
482 }
483 #endif
484
485 int designware_initialize(u32 id, ulong base_addr, u32 phy_addr)
486 {
487         struct eth_device *dev;
488         struct dw_eth_dev *priv;
489
490         dev = (struct eth_device *) malloc(sizeof(struct eth_device));
491         if (!dev)
492                 return -ENOMEM;
493
494         /*
495          * Since the priv structure contains the descriptors which need a strict
496          * buswidth alignment, memalign is used to allocate memory
497          */
498         priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
499         if (!priv) {
500                 free(dev);
501                 return -ENOMEM;
502         }
503
504         memset(dev, 0, sizeof(struct eth_device));
505         memset(priv, 0, sizeof(struct dw_eth_dev));
506
507         sprintf(dev->name, "mii%d", id);
508         dev->iobase = (int)base_addr;
509         dev->priv = priv;
510
511         eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
512
513         priv->dev = dev;
514         priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
515         priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
516                         DW_DMA_BASE_OFFSET);
517         priv->address = phy_addr;
518
519         if (mac_reset(dev) < 0)
520                 return -1;
521
522         if (configure_phy(dev) < 0) {
523                 printf("Phy could not be configured\n");
524                 return -1;
525         }
526
527         dev->init = dw_eth_init;
528         dev->send = dw_eth_send;
529         dev->recv = dw_eth_recv;
530         dev->halt = dw_eth_halt;
531         dev->write_hwaddr = dw_write_hwaddr;
532
533         eth_register(dev);
534
535 #if defined(CONFIG_MII)
536         miiphy_register(dev->name, dw_mii_read, dw_mii_write);
537 #endif
538         return 1;
539 }