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