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