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