]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/fec_mxc.c
07b2a5a2cc0e30f5e4d229d8b575c7c9d4070f32
[karo-tx-uboot.git] / drivers / net / fec_mxc.c
1 /*
2  * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
3  * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
4  * (C) Copyright 2008 Armadeus Systems nc
5  * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6  * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
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 #include <common.h>
25 #include <malloc.h>
26 #include <net.h>
27 #include <miiphy.h>
28
29 #include <asm/arch/sys_proto.h>
30 #include <asm/arch/clock.h>
31 #include <asm/arch/imx-regs.h>
32 #include <asm/io.h>
33 #include <asm/errno.h>
34 #include <linux/compiler.h>
35
36 #include "fec_mxc.h"
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 /*
41  * Timeout the transfer after 5 mS. This is usually a bit more, since
42  * the code in the tightloops this timeout is used in adds some overhead.
43  */
44 #define FEC_XFER_TIMEOUT        5000
45
46 #ifndef CONFIG_MII
47 #error "CONFIG_MII has to be defined!"
48 #endif
49
50 #ifndef CONFIG_FEC_XCV_TYPE
51 #define CONFIG_FEC_XCV_TYPE MII100
52 #endif
53
54 /*
55  * The i.MX28 operates with packets in big endian. We need to swap them before
56  * sending and after receiving.
57  */
58 #ifdef CONFIG_MX28
59 #define CONFIG_FEC_MXC_SWAP_PACKET
60 #endif
61
62 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
63
64 /* Check various alignment issues at compile time */
65 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
66 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
67 #endif
68
69 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
70         (PKTALIGN % ARCH_DMA_MINALIGN != 0))
71 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
72 #endif
73
74 #undef DEBUG
75
76 struct nbuf {
77         uint8_t data[1500];     /**< actual data */
78         int length;             /**< actual length */
79         int used;               /**< buffer in use or not */
80         uint8_t head[16];       /**< MAC header(6 + 6 + 2) + 2(aligned) */
81 };
82
83 static int rx_idx;
84
85 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
86 static void swap_packet(uint32_t *packet, int length)
87 {
88         int i;
89
90         for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
91                 packet[i] = __swab32(packet[i]);
92 }
93 #endif
94
95 /*
96  * MII-interface related functions
97  */
98 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
99                 uint8_t regAddr)
100 {
101         uint32_t reg;           /* convenient holder for the PHY register */
102         uint32_t phy;           /* convenient holder for the PHY */
103         ulong start;
104         int val;
105
106         /*
107          * reading from any PHY's register is done by properly
108          * programming the FEC's MII data register.
109          */
110         writel(FEC_IEVENT_MII, &eth->ievent);
111         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
112         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
113
114         writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
115                         phy | reg, &eth->mii_data);
116
117         /*
118          * wait for the related interrupt
119          */
120         start = get_timer(0);
121         while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
122                 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
123                         if (readl(&eth->ievent) & FEC_IEVENT_MII)
124                                 break;
125                         printf("Read MDIO failed...\n");
126                         return -1;
127                 }
128         }
129
130         /*
131          * clear mii interrupt bit
132          */
133         writel(FEC_IEVENT_MII, &eth->ievent);
134
135         /*
136          * it's now safe to read the PHY's register
137          */
138         val = (unsigned short)readl(&eth->mii_data);
139         debug("%s: phy: %02x reg:%02x val:%#06x\n", __func__, phyAddr,
140                         regAddr, val);
141         return val;
142 }
143
144 static void fec_mii_setspeed(struct fec_priv *fec)
145 {
146         /*
147          * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
148          * and do not drop the Preamble.
149          */
150         writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
151                         &fec->eth->mii_speed);
152         debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
153 }
154
155 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
156                 uint8_t regAddr, uint16_t data)
157 {
158         uint32_t reg;           /* convenient holder for the PHY register */
159         uint32_t phy;           /* convenient holder for the PHY */
160         ulong start;
161
162         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
163         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
164
165         writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
166                 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
167
168         /*
169          * wait for the MII interrupt
170          */
171         start = get_timer(0);
172         while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
173                 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
174                         if (readl(&eth->ievent) & FEC_IEVENT_MII)
175                                 break;
176                         printf("Write MDIO failed...\n");
177                         return -1;
178                 }
179         }
180
181         /*
182          * clear MII interrupt bit
183          */
184         writel(FEC_IEVENT_MII, &eth->ievent);
185         debug("%s: phy: %02x reg:%02x val:%#06x\n", __func__, phyAddr,
186                         regAddr, data);
187
188         return 0;
189 }
190
191 int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
192 {
193         return fec_mdio_read(bus->priv, phyAddr, regAddr);
194 }
195
196 int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
197                 u16 data)
198 {
199         return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
200 }
201
202 #ifndef CONFIG_PHYLIB
203 static int miiphy_restart_aneg(struct eth_device *dev)
204 {
205         int ret = 0;
206 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
207         struct fec_priv *fec = (struct fec_priv *)dev->priv;
208         struct ethernet_regs *eth = fec->bus->priv;
209
210         /*
211          * Wake up from sleep if necessary
212          * Reset PHY, then delay 300ns
213          */
214 #ifdef CONFIG_MX27
215         fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
216 #endif
217         fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
218         udelay(1000);
219
220         /*
221          * Set the auto-negotiation advertisement register bits
222          */
223         fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
224                         LPA_100FULL | LPA_100HALF | LPA_10FULL |
225                         LPA_10HALF | PHY_ANLPAR_PSB_802_3);
226         fec_mdio_write(eth, fec->phy_id, MII_BMCR,
227                         BMCR_ANENABLE | BMCR_ANRESTART);
228
229         if (fec->mii_postcall)
230                 ret = fec->mii_postcall(fec->phy_id);
231
232 #endif
233         return ret;
234 }
235
236 static int miiphy_wait_aneg(struct eth_device *dev)
237 {
238         uint32_t start;
239         int status;
240         struct fec_priv *fec = (struct fec_priv *)dev->priv;
241         struct ethernet_regs *eth = fec->bus->priv;
242
243         /*
244          * Wait for AN completion
245          */
246         start = get_timer(0);
247         do {
248                 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
249                         printf("%s: Autonegotiation timeout\n", dev->name);
250                         return -1;
251                 }
252
253                 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
254                 if (status < 0) {
255                         printf("%s: Autonegotiation failed. status: %d\n",
256                                         dev->name, status);
257                         return -1;
258                 }
259         } while (!(status & BMSR_LSTATUS));
260
261         return 0;
262 }
263 #endif
264
265 static inline void fec_rx_task_enable(struct fec_priv *fec)
266 {
267         writel(1 << 24, &fec->eth->r_des_active);
268 }
269
270 static inline void fec_rx_task_disable(struct fec_priv *fec)
271 {
272 }
273
274 static inline void fec_tx_task_enable(struct fec_priv *fec)
275 {
276         writel(1 << 24, &fec->eth->x_des_active);
277 }
278
279 static inline void fec_tx_task_disable(struct fec_priv *fec)
280 {
281 }
282
283 /**
284  * Initialize receive task's buffer descriptors
285  * @param[in] fec all we know about the device yet
286  * @param[in] count receive buffer count to be allocated
287  * @param[in] dsize desired size of each receive buffer
288  * @return 0 on success
289  *
290  * For this task we need additional memory for the data buffers. And each
291  * data buffer requires some alignment. Thy must be aligned to a specific
292  * boundary each.
293  */
294 static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
295 {
296         uint32_t size;
297         int i;
298
299         /*
300          * Allocate memory for the buffers. This allocation respects the
301          * alignment
302          */
303         size = roundup(dsize, ARCH_DMA_MINALIGN);
304         for (i = 0; i < count; i++) {
305                 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
306                 if (data_ptr == 0) {
307                         uint8_t *data = memalign(ARCH_DMA_MINALIGN,
308                                                  size);
309                         if (!data) {
310                                 printf("%s: error allocating rxbuf %d\n",
311                                        __func__, i);
312                                 goto err;
313                         }
314                         writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
315                 } /* needs allocation */
316                 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
317                 writew(0, &fec->rbd_base[i].data_length);
318         }
319
320         /* Mark the last RBD to close the ring. */
321         writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
322         fec->rbd_index = 0;
323
324         return 0;
325
326 err:
327         for (; i >= 0; i--) {
328                 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
329                 free((void *)data_ptr);
330         }
331
332         return -ENOMEM;
333 }
334
335 /**
336  * Initialize transmit task's buffer descriptors
337  * @param[in] fec all we know about the device yet
338  *
339  * Transmit buffers are created externally. We only have to init the BDs here.\n
340  * Note: There is a race condition in the hardware. When only one BD is in
341  * use it must be marked with the WRAP bit to use it for every transmitt.
342  * This bit in combination with the READY bit results into double transmit
343  * of each data buffer. It seems the state machine checks READY earlier then
344  * resetting it after the first transfer.
345  * Using two BDs solves this issue.
346  */
347 static void fec_tbd_init(struct fec_priv *fec)
348 {
349         unsigned addr = (unsigned)fec->tbd_base;
350         unsigned size = roundup(2 * sizeof(struct fec_bd),
351                                 ARCH_DMA_MINALIGN);
352         writew(0x0000, &fec->tbd_base[0].status);
353         writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
354         fec->tbd_index = 0;
355         flush_dcache_range(addr, addr + size);
356 }
357
358 /**
359  * Mark the given read buffer descriptor as free
360  * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
361  * @param[in] pRbd buffer descriptor to mark free again
362  */
363 static void fec_rbd_clean(int last, struct fec_bd *pRbd)
364 {
365         unsigned short flags = FEC_RBD_EMPTY;
366         if (last)
367                 flags |= FEC_RBD_WRAP;
368         writew(flags, &pRbd->status);
369         writew(0, &pRbd->data_length);
370 }
371
372 static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
373                                                 unsigned char *mac)
374 {
375         imx_get_mac_from_fuse(dev_id, mac);
376         return !is_valid_ether_addr(mac);
377 }
378
379 static int fec_set_hwaddr(struct eth_device *dev)
380 {
381         uchar *mac = dev->enetaddr;
382         struct fec_priv *fec = (struct fec_priv *)dev->priv;
383
384         writel(0, &fec->eth->iaddr1);
385         writel(0, &fec->eth->iaddr2);
386         writel(0, &fec->eth->gaddr1);
387         writel(0, &fec->eth->gaddr2);
388
389         /*
390          * Set physical address
391          */
392         writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
393                         &fec->eth->paddr1);
394         writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
395
396         return 0;
397 }
398
399 static void fec_eth_phy_config(struct eth_device *dev)
400 {
401 #ifdef CONFIG_PHYLIB
402         struct fec_priv *fec = (struct fec_priv *)dev->priv;
403         struct phy_device *phydev;
404
405         if (fec->phy_id < 0) {
406                 int phy_id;
407
408                 for (phy_id = 0; phy_id < 32; phy_id++) {
409                         debug("%s: Probing PHY ID %02x\n", __func__, phy_id);
410                         phydev = phy_connect(fec->bus, phy_id, dev,
411                                         PHY_INTERFACE_MODE_RGMII);
412
413                         if (phydev)
414                                 break;
415                 }
416         } else {
417                 phydev = phy_connect(fec->bus, fec->phy_id, dev,
418                                 PHY_INTERFACE_MODE_RGMII);
419         }
420         if (phydev) {
421                 fec->phydev = phydev;
422                 phy_config(phydev);
423         }
424 #endif
425 }
426
427 /*
428  * Do initial configuration of the FEC registers
429  */
430 static void fec_reg_setup(struct fec_priv *fec)
431 {
432         uint32_t rcntrl;
433
434         /*
435          * Set interrupt mask register
436          */
437         writel(0x00000000, &fec->eth->imask);
438
439         /*
440          * Clear FEC-Lite interrupt event register(IEVENT)
441          */
442         writel(0xffffffff, &fec->eth->ievent);
443
444
445         /*
446          * Set FEC-Lite receive control register(R_CNTRL):
447          */
448
449         /* Start with frame length = 1518, common for all modes. */
450         rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
451         if (fec->xcv_type != SEVENWIRE)         /* xMII modes */
452                 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
453         if (fec->xcv_type == RGMII)
454                 rcntrl |= FEC_RCNTRL_RGMII;
455         else if (fec->xcv_type == RMII)
456                 rcntrl |= FEC_RCNTRL_RMII;
457
458         writel(rcntrl, &fec->eth->r_cntrl);
459 }
460
461 /**
462  * Start the FEC engine
463  * @param[in] dev Our device to handle
464  */
465 static int fec_open(struct eth_device *edev)
466 {
467         struct fec_priv *fec = edev->priv;
468         int speed;
469         uint32_t addr, size;
470         int i;
471
472         debug("fec_open: fec_open(dev)\n");
473         /* full-duplex, heartbeat disabled */
474         writel(1 << 2, &fec->eth->x_cntrl);
475         fec->rbd_index = 0;
476
477         /* Invalidate all descriptors */
478         for (i = 0; i < FEC_RBD_NUM - 1; i++)
479                 fec_rbd_clean(0, &fec->rbd_base[i]);
480         fec_rbd_clean(1, &fec->rbd_base[i]);
481
482         /* Flush the descriptors into RAM */
483         size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
484                         ARCH_DMA_MINALIGN);
485         addr = (uint32_t)fec->rbd_base;
486         flush_dcache_range(addr, addr + size);
487
488 #ifdef FEC_QUIRK_ENET_MAC
489         /* Enable ENET HW endian SWAP */
490         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
491                 &fec->eth->ecntrl);
492         /* Enable ENET store and forward mode */
493         writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
494                 &fec->eth->x_wmrk);
495 #endif
496         /*
497          * Enable FEC-Lite controller
498          */
499         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
500                 &fec->eth->ecntrl);
501 #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
502         udelay(100);
503         /*
504          * setup the MII gasket for RMII mode
505          */
506
507         /* disable the gasket */
508         writew(0, &fec->eth->miigsk_enr);
509
510         /* wait for the gasket to be disabled */
511         while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
512                 udelay(2);
513
514         /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
515         writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
516
517         /* re-enable the gasket */
518         writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
519
520         /* wait until MII gasket is ready */
521         int max_loops = 10;
522         while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
523                 if (--max_loops <= 0) {
524                         printf("WAIT for MII Gasket ready timed out\n");
525                         break;
526                 }
527         }
528 #endif
529
530 #ifdef CONFIG_PHYLIB
531         if (!fec->phydev)
532                 fec_eth_phy_config(edev);
533         if (fec->phydev) {
534                 /* Start up the PHY */
535                 int ret = phy_startup(fec->phydev);
536
537                 if (ret) {
538                         printf("Could not initialize PHY %s\n",
539                                fec->phydev->dev->name);
540                         return ret;
541                 }
542                 speed = fec->phydev->speed;
543         } else {
544                 speed = _100BASET;
545         }
546 #else
547         miiphy_wait_aneg(edev);
548         speed = miiphy_speed(edev->name, fec->phy_id);
549         miiphy_duplex(edev->name, fec->phy_id);
550 #endif
551
552 #ifdef FEC_QUIRK_ENET_MAC
553         {
554                 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
555                 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
556
557                 if (speed == _1000BASET)
558                         ecr |= FEC_ECNTRL_SPEED;
559                 else if (speed != _100BASET)
560                         rcr |= FEC_RCNTRL_RMII_10T;
561                 writel(ecr, &fec->eth->ecntrl);
562                 writel(rcr, &fec->eth->r_cntrl);
563         }
564 #endif
565         debug("%s:Speed=%i\n", __func__, speed);
566
567         /*
568          * Enable SmartDMA receive task
569          */
570         fec_rx_task_enable(fec);
571
572 //      udelay(100000);
573         return 0;
574 }
575
576 static int fec_init(struct eth_device *dev, bd_t* bd)
577 {
578         struct fec_priv *fec = dev->priv;
579         uint32_t *mib_ptr = (uint32_t *)&fec->eth->rmon_t_drop;
580         uint32_t size;
581         int i, ret;
582
583         /* Initialize MAC address */
584         fec_set_hwaddr(dev);
585
586         /*
587          * Allocate transmit descriptors, there are two in total. This
588          * allocation respects cache alignment.
589          */
590         if (!fec->tbd_base) {
591                 size = roundup(2 * sizeof(struct fec_bd),
592                                 ARCH_DMA_MINALIGN);
593                 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
594                 if (!fec->tbd_base) {
595                         ret = -ENOMEM;
596                         goto err1;
597                 }
598                 memset(fec->tbd_base, 0, size);
599                 fec_tbd_init(fec);
600                 flush_dcache_range((unsigned)fec->tbd_base, size);
601         }
602
603         /*
604          * Allocate receive descriptors. This allocation respects cache
605          * alignment.
606          */
607         if (!fec->rbd_base) {
608                 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
609                                 ARCH_DMA_MINALIGN);
610                 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
611                 if (!fec->rbd_base) {
612                         ret = -ENOMEM;
613                         goto err2;
614                 }
615                 memset(fec->rbd_base, 0, size);
616                 /*
617                  * Initialize RxBD ring
618                  */
619                 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
620                         ret = -ENOMEM;
621                         goto err3;
622                 }
623                 flush_dcache_range((unsigned)fec->rbd_base,
624                                    (unsigned)fec->rbd_base + size);
625         }
626
627         fec_reg_setup(fec);
628
629         if (fec->xcv_type != SEVENWIRE)
630                 fec_mii_setspeed(fec);
631
632         /*
633          * Set Opcode/Pause Duration Register
634          */
635         writel(0x00010020, &fec->eth->op_pause);        /* FIXME 0xffff0020; */
636         writel(0x2, &fec->eth->x_wmrk);
637         /*
638          * Set multicast address filter
639          */
640         writel(0x00000000, &fec->eth->gaddr1);
641         writel(0x00000000, &fec->eth->gaddr2);
642
643
644         /* clear MIB RAM */
645         for (i = 0; i <= 0xfc >> 2; i++)
646                 writel(0, &mib_ptr[i]);
647
648         /* FIFO receive start register */
649         writel(0x520, &fec->eth->r_fstart);
650
651         /* size and address of each buffer */
652         writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
653         writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
654         writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
655
656 #ifndef CONFIG_PHYLIB
657         if (fec->xcv_type != SEVENWIRE)
658                 miiphy_restart_aneg(dev);
659 #endif
660         fec_open(dev);
661         return 0;
662
663 err3:
664         free(fec->rbd_base);
665 err2:
666         free(fec->tbd_base);
667 err1:
668         return ret;
669 }
670
671 /**
672  * Halt the FEC engine
673  * @param[in] dev Our device to handle
674  */
675 static void fec_halt(struct eth_device *dev)
676 {
677         struct fec_priv *fec = (struct fec_priv *)dev->priv;
678         int counter = 1000;
679
680         /*
681          * issue graceful stop command to the FEC transmitter if necessary
682          */
683         writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
684                         &fec->eth->x_cntrl);
685
686         debug("eth_halt: wait for stop regs\n");
687         /*
688          * wait for graceful stop to register
689          */
690         while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
691                 udelay(100);
692
693         /*
694          * Disable SmartDMA tasks
695          */
696         fec_tx_task_disable(fec);
697         fec_rx_task_disable(fec);
698
699         /*
700          * Disable the Ethernet Controller
701          * Note: this will also reset the BD index counter!
702          */
703         writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
704                         &fec->eth->ecntrl);
705         fec->rbd_index = 0;
706         fec->tbd_index = 0;
707         debug("eth_halt: done\n");
708 }
709
710 /**
711  * Transmit one frame
712  * @param[in] dev Our ethernet device to handle
713  * @param[in] packet Pointer to the data to be transmitted
714  * @param[in] length Data count in bytes
715  * @return 0 on success
716  */
717 static int fec_send(struct eth_device *dev, void *packet, int length)
718 {
719         unsigned int status;
720         uint32_t size, end;
721         uint32_t addr;
722         int timeout = FEC_XFER_TIMEOUT;
723
724         /*
725          * This routine transmits one frame.  This routine only accepts
726          * 6-byte Ethernet addresses.
727          */
728         struct fec_priv *fec = dev->priv;
729
730         /*
731          * Check for valid length of data.
732          */
733         if ((length > 1500) || (length <= 0)) {
734                 printf("Payload (%d) too large\n", length);
735                 return -1;
736         }
737
738         /*
739          * Setup the transmit buffer. We are always using the first buffer for
740          * transmission, the second will be empty and only used to stop the DMA
741          * engine. We also flush the packet to RAM here to avoid cache trouble.
742          */
743 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
744         swap_packet((uint32_t *)packet, length);
745 #endif
746
747         addr = (uint32_t)packet;
748         end = roundup(addr + length, ARCH_DMA_MINALIGN);
749         addr &= ~(ARCH_DMA_MINALIGN - 1);
750         flush_dcache_range(addr, end);
751
752         writew(length, &fec->tbd_base[fec->tbd_index].data_length);
753         writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
754
755         /*
756          * update BD's status now
757          * This block:
758          * - is always the last in a chain (means no chain)
759          * - should transmit the CRC
760          * - might be the last BD in the list, so the address counter should
761          *   wrap (-> keep the WRAP flag)
762          */
763         status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
764         status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
765         writew(status, &fec->tbd_base[fec->tbd_index].status);
766
767         /*
768          * Flush data cache. This code flushes both TX descriptors to RAM.
769          * After this code, the descriptors will be safely in RAM and we
770          * can start DMA.
771          */
772         size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
773         addr = (uint32_t)fec->tbd_base;
774         flush_dcache_range(addr, addr + size);
775
776         /*
777          * Enable SmartDMA transmit task
778          */
779         fec_tx_task_enable(fec);
780
781         /*
782          * Wait until frame is sent. On each turn of the wait cycle, we must
783          * invalidate data cache to see what's really in RAM. Also, we need
784          * barrier here.
785          */
786         invalidate_dcache_range(addr, addr + size);
787         while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
788                 if (--timeout < 0)
789                         return -ETIMEDOUT;
790                 udelay(1);
791                 invalidate_dcache_range(addr, addr + size);
792         }
793
794         debug("fec_send: status 0x%04x index %d\n",
795                         readw(&fec->tbd_base[fec->tbd_index].status),
796                         fec->tbd_index);
797         /* for next transmission use the other buffer */
798         if (fec->tbd_index)
799                 fec->tbd_index = 0;
800         else
801                 fec->tbd_index = 1;
802
803         return 0;
804 }
805
806 /**
807  * Pull one frame from the card
808  * @param[in] dev Our ethernet device to handle
809  * @return Length of packet read
810  */
811 static int fec_recv(struct eth_device *dev)
812 {
813         struct fec_priv *fec = (struct fec_priv *)dev->priv;
814         struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
815         unsigned long ievent;
816         int frame_length, len = 0;
817         struct nbuf *frame;
818         uint16_t bd_status;
819         uint32_t addr, size, end;
820         int i;
821
822         /*
823          * Check if any critical events have happened
824          */
825         ievent = readl(&fec->eth->ievent);
826         if (ievent)
827                 writel(ievent, &fec->eth->ievent);
828
829         if (ievent)
830                 debug("fec_recv: ievent 0x%lx\n", ievent);
831         if (ievent & FEC_IEVENT_BABR) {
832                 fec_halt(dev);
833                 fec_init(dev, fec->bd);
834                 printf("some error: 0x%08lx\n", ievent);
835                 return 0;
836         }
837         if (ievent & FEC_IEVENT_HBERR) {
838                 /* Heartbeat error */
839                 writel(0x00000001 | readl(&fec->eth->x_cntrl),
840                                 &fec->eth->x_cntrl);
841         }
842         if (ievent & FEC_IEVENT_GRA) {
843                 /* Graceful stop complete */
844                 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
845                         fec_halt(dev);
846                         writel(~0x00000001 & readl(&fec->eth->x_cntrl),
847                                         &fec->eth->x_cntrl);
848                         fec_init(dev, fec->bd);
849                 }
850         }
851
852         /*
853          * Read the buffer status. Before the status can be read, the data cache
854          * must be invalidated, because the data in RAM might have been changed
855          * by DMA. The descriptors are properly aligned to cachelines so there's
856          * no need to worry they'd overlap.
857          *
858          * WARNING: By invalidating the descriptor here, we also invalidate
859          * the descriptors surrounding this one. Therefore we can NOT change the
860          * contents of this descriptor nor the surrounding ones. The problem is
861          * that in order to mark the descriptor as processed, we need to change
862          * the descriptor. The solution is to mark the whole cache line when all
863          * descriptors in the cache line are processed.
864          */
865         addr = (uint32_t)rbd;
866         addr &= ~(ARCH_DMA_MINALIGN - 1);
867         size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
868         invalidate_dcache_range(addr, addr + size);
869
870         bd_status = readw(&rbd->status);
871         if (!(bd_status & FEC_RBD_EMPTY)) {
872                 debug("fec_recv: status 0x%04x len %u\n", bd_status,
873                         readw(&rbd->data_length) - 4);
874                 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
875                         ((readw(&rbd->data_length) - 4) > 14)) {
876                         /*
877                          * Get buffer address and size
878                          */
879                         frame = (struct nbuf *)readl(&rbd->data_pointer);
880                         frame_length = readw(&rbd->data_length) - 4;
881
882                         /*
883                          * Invalidate data cache over the buffer
884                          */
885                         addr = (uint32_t)frame;
886                         end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
887                         addr &= ~(ARCH_DMA_MINALIGN - 1);
888                         invalidate_dcache_range(addr, end);
889
890                         /*
891                          *  Fill the buffer and pass it to upper layers
892                          */
893 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
894                         swap_packet((uint32_t *)frame->data, frame_length);
895 #endif
896                         memcpy((void *)NetRxPackets[rx_idx], frame->data, frame_length);
897                         NetReceive(NetRxPackets[rx_idx], frame_length);
898                         rx_idx = (rx_idx + 1) % PKTBUFSRX;
899                         len = frame_length;
900                 } else {
901                         if (bd_status & FEC_RBD_ERR)
902                                 printf("error frame: 0x%08lx 0x%08x\n",
903                                                 (ulong)rbd->data_pointer,
904                                                 bd_status);
905                 }
906
907                 /*
908                  * Free the current buffer, restart the engine and move forward
909                  * to the next buffer. Here we check if the whole cacheline of
910                  * descriptors was already processed and if so, we mark it free
911                  * as whole.
912                  */
913                 size = RXDESC_PER_CACHELINE - 1;
914                 if ((fec->rbd_index & size) == size) {
915                         i = fec->rbd_index - size;
916                         addr = (uint32_t)&fec->rbd_base[i];
917                         for (; i <= fec->rbd_index ; i++) {
918                                 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
919                                               &fec->rbd_base[i]);
920                         }
921                         flush_dcache_range(addr,
922                                 addr + ARCH_DMA_MINALIGN);
923                 }
924
925                 fec_rx_task_enable(fec);
926                 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
927                 debug("fec_recv: stop\n");
928         }
929
930         return len;
931 }
932
933 static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
934 {
935         struct eth_device *edev;
936         struct fec_priv *fec;
937         struct mii_dev *bus;
938         unsigned char ethaddr[6];
939         uint32_t start;
940         int ret = 0;
941
942         /* create and fill edev struct */
943         edev = calloc(sizeof(struct eth_device), 1);
944         if (!edev) {
945                 puts("fec_mxc: not enough malloc memory for eth_device\n");
946                 ret = -ENOMEM;
947                 goto err1;
948         }
949
950         fec = calloc(sizeof(struct fec_priv), 1);
951         if (!fec) {
952                 puts("fec_mxc: not enough malloc memory for fec_priv\n");
953                 ret = -ENOMEM;
954                 goto err2;
955         }
956
957         edev->priv = fec;
958         edev->init = fec_init;
959         edev->send = fec_send;
960         edev->recv = fec_recv;
961         edev->halt = fec_halt;
962         edev->write_hwaddr = fec_set_hwaddr;
963
964         fec->eth = (struct ethernet_regs *)base_addr;
965         fec->bd = bd;
966
967         fec->xcv_type = CONFIG_FEC_XCV_TYPE;
968
969         /* Reset chip. */
970         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
971         start = get_timer(0);
972         while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
973                 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
974                         printf("FEC MXC: Timeout reseting chip\n");
975                         goto err3;
976                 }
977                 udelay(10);
978         }
979
980         fec_reg_setup(fec);
981         fec_mii_setspeed(fec);
982
983         if (dev_id == -1) {
984                 sprintf(edev->name, "FEC");
985                 fec->dev_id = 0;
986         } else {
987                 sprintf(edev->name, "FEC%i", dev_id);
988                 fec->dev_id = dev_id;
989         }
990         fec->phy_id = phy_id;
991
992         bus = mdio_alloc();
993         if (!bus) {
994                 printf("mdio_alloc failed\n");
995                 ret = -ENOMEM;
996                 goto err3;
997         }
998         bus->read = fec_phy_read;
999         bus->write = fec_phy_write;
1000         sprintf(bus->name, edev->name);
1001 #ifdef CONFIG_MX28
1002         /*
1003          * The i.MX28 has two ethernet interfaces, but they are not equal.
1004          * Only the first one can access the MDIO bus.
1005          */
1006         bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
1007 #else
1008         bus->priv = fec->eth;
1009 #endif
1010         ret = mdio_register(bus);
1011         if (ret) {
1012                 printf("mdio_register failed\n");
1013                 free(bus);
1014                 ret = -ENOMEM;
1015                 goto err3;
1016         }
1017         fec->bus = bus;
1018         eth_register(edev);
1019
1020         if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1021                 if (dev_id < 0)
1022                         debug("got MAC address from fuse: %pM\n", ethaddr);
1023                 else
1024                         debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
1025                 memcpy(edev->enetaddr, ethaddr, 6);
1026         }
1027         /* Configure phy */
1028         fec_eth_phy_config(edev);
1029         return ret;
1030
1031 err3:
1032         free(fec);
1033 err2:
1034         free(edev);
1035 err1:
1036         return ret;
1037 }
1038
1039 #ifndef CONFIG_FEC_MXC_MULTI
1040 int fecmxc_initialize(bd_t *bd)
1041 {
1042         int lout = 1;
1043
1044         debug("eth_init: fec_probe(PHY %02x FEC: %08x)\n",
1045                 CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1046         lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1047
1048         return lout;
1049 }
1050 #endif
1051
1052 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1053 {
1054         int lout = 1;
1055
1056         debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1057         lout = fec_probe(bd, dev_id, phy_id, addr);
1058
1059         return lout;
1060 }
1061
1062 #ifndef CONFIG_PHYLIB
1063 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1064 {
1065         struct fec_priv *fec = (struct fec_priv *)dev->priv;
1066         fec->mii_postcall = cb;
1067         return 0;
1068 }
1069 #endif