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