Merge with git://www.denx.de/git/u-boot.git
[oweals/u-boot.git] / cpu / mpc5xxx / fec.c
1 /*
2  * (C) Copyright 2003-2005
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * This file is based on mpc4200fec.c,
6  * (C) Copyright Motorola, Inc., 2000
7  */
8
9 #include <common.h>
10 #include <mpc5xxx.h>
11 #include <malloc.h>
12 #include <net.h>
13 #include <miiphy.h>
14 #include "sdma.h"
15 #include "fec.h"
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 /* #define DEBUG        0x28 */
20
21 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
22         defined(CONFIG_MPC5xxx_FEC)
23
24 #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
25 #error "CONFIG_MII has to be defined!"
26 #endif
27
28 #if (DEBUG & 0x60)
29 static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec);
30 static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec);
31 #endif /* DEBUG */
32
33 #if (DEBUG & 0x40)
34 static uint32 local_crc32(char *string, unsigned int crc_value, int len);
35 #endif
36
37 typedef struct {
38     uint8 data[1500];           /* actual data */
39     int length;                 /* actual length */
40     int used;                   /* buffer in use or not */
41     uint8 head[16];             /* MAC header(6 + 6 + 2) + 2(aligned) */
42 } NBUF;
43
44 int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal);
45 int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data);
46
47 /********************************************************************/
48 #if (DEBUG & 0x2)
49 static void mpc5xxx_fec_phydump (char *devname)
50 {
51         uint16 phyStatus, i;
52         uint8 phyAddr = CONFIG_PHY_ADDR;
53         uint8 reg_mask[] = {
54 #if CONFIG_PHY_TYPE == 0x79c874 /* AMD Am79C874 */
55                 /* regs to print: 0...7, 16...19, 21, 23, 24 */
56                 1, 1, 1, 1,  1, 1, 1, 1,     0, 0, 0, 0,  0, 0, 0, 0,
57                 1, 1, 1, 1,  0, 1, 0, 1,     1, 0, 0, 0,  0, 0, 0, 0,
58 #else
59                 /* regs to print: 0...8, 16...20 */
60                 1, 1, 1, 1,  1, 1, 1, 1,     1, 0, 0, 0,  0, 0, 0, 0,
61                 1, 1, 1, 1,  1, 0, 0, 0,     0, 0, 0, 0,  0, 0, 0, 0,
62 #endif
63         };
64
65         for (i = 0; i < 32; i++) {
66                 if (reg_mask[i]) {
67                         miiphy_read(devname, phyAddr, i, &phyStatus);
68                         printf("Mii reg %d: 0x%04x\n", i, phyStatus);
69                 }
70         }
71 }
72 #endif
73
74 /********************************************************************/
75 static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
76 {
77         int ix;
78         char *data;
79         static int once = 0;
80
81         for (ix = 0; ix < FEC_RBD_NUM; ix++) {
82                 if (!once) {
83                         data = (char *)malloc(FEC_MAX_PKT_SIZE);
84                         if (data == NULL) {
85                                 printf ("RBD INIT FAILED\n");
86                                 return -1;
87                         }
88                         fec->rbdBase[ix].dataPointer = (uint32)data;
89                 }
90                 fec->rbdBase[ix].status = FEC_RBD_EMPTY;
91                 fec->rbdBase[ix].dataLength = 0;
92         }
93         once ++;
94
95         /*
96          * have the last RBD to close the ring
97          */
98         fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP;
99         fec->rbdIndex = 0;
100
101         return 0;
102 }
103
104 /********************************************************************/
105 static void mpc5xxx_fec_tbd_init(mpc5xxx_fec_priv *fec)
106 {
107         int ix;
108
109         for (ix = 0; ix < FEC_TBD_NUM; ix++) {
110                 fec->tbdBase[ix].status = 0;
111         }
112
113         /*
114          * Have the last TBD to close the ring
115          */
116         fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP;
117
118         /*
119          * Initialize some indices
120          */
121         fec->tbdIndex = 0;
122         fec->usedTbdIndex = 0;
123         fec->cleanTbdNum = FEC_TBD_NUM;
124 }
125
126 /********************************************************************/
127 static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, volatile FEC_RBD * pRbd)
128 {
129         /*
130          * Reset buffer descriptor as empty
131          */
132         if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
133                 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
134         else
135                 pRbd->status = FEC_RBD_EMPTY;
136
137         pRbd->dataLength = 0;
138
139         /*
140          * Now, we have an empty RxBD, restart the SmartDMA receive task
141          */
142         SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
143
144         /*
145          * Increment BD count
146          */
147         fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
148 }
149
150 /********************************************************************/
151 static void mpc5xxx_fec_tbd_scrub(mpc5xxx_fec_priv *fec)
152 {
153         volatile FEC_TBD *pUsedTbd;
154
155 #if (DEBUG & 0x1)
156         printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
157                 fec->cleanTbdNum, fec->usedTbdIndex);
158 #endif
159
160         /*
161          * process all the consumed TBDs
162          */
163         while (fec->cleanTbdNum < FEC_TBD_NUM) {
164                 pUsedTbd = &fec->tbdBase[fec->usedTbdIndex];
165                 if (pUsedTbd->status & FEC_TBD_READY) {
166 #if (DEBUG & 0x20)
167                         printf("Cannot clean TBD %d, in use\n", fec->cleanTbdNum);
168 #endif
169                         return;
170                 }
171
172                 /*
173                  * clean this buffer descriptor
174                  */
175                 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
176                         pUsedTbd->status = FEC_TBD_WRAP;
177                 else
178                         pUsedTbd->status = 0;
179
180                 /*
181                  * update some indeces for a correct handling of the TBD ring
182                  */
183                 fec->cleanTbdNum++;
184                 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
185         }
186 }
187
188 /********************************************************************/
189 static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac)
190 {
191         uint8 currByte;                 /* byte for which to compute the CRC */
192         int byte;                       /* loop - counter */
193         int bit;                        /* loop - counter */
194         uint32 crc = 0xffffffff;        /* initial value */
195
196         /*
197          * The algorithm used is the following:
198          * we loop on each of the six bytes of the provided address,
199          * and we compute the CRC by left-shifting the previous
200          * value by one position, so that each bit in the current
201          * byte of the address may contribute the calculation. If
202          * the latter and the MSB in the CRC are different, then
203          * the CRC value so computed is also ex-ored with the
204          * "polynomium generator". The current byte of the address
205          * is also shifted right by one bit at each iteration.
206          * This is because the CRC generatore in hardware is implemented
207          * as a shift-register with as many ex-ores as the radixes
208          * in the polynomium. This suggests that we represent the
209          * polynomiumm itself as a 32-bit constant.
210          */
211         for (byte = 0; byte < 6; byte++) {
212                 currByte = mac[byte];
213                 for (bit = 0; bit < 8; bit++) {
214                         if ((currByte & 0x01) ^ (crc & 0x01)) {
215                                 crc >>= 1;
216                                 crc = crc ^ 0xedb88320;
217                         } else {
218                                 crc >>= 1;
219                         }
220                         currByte >>= 1;
221                 }
222         }
223
224         crc = crc >> 26;
225
226         /*
227          * Set individual hash table register
228          */
229         if (crc >= 32) {
230                 fec->eth->iaddr1 = (1 << (crc - 32));
231                 fec->eth->iaddr2 = 0;
232         } else {
233                 fec->eth->iaddr1 = 0;
234                 fec->eth->iaddr2 = (1 << crc);
235         }
236
237         /*
238          * Set physical address
239          */
240         fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
241         fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
242 }
243
244 /********************************************************************/
245 static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
246 {
247         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
248         struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
249
250 #if (DEBUG & 0x1)
251         printf ("mpc5xxx_fec_init... Begin\n");
252 #endif
253
254         /*
255          * Initialize RxBD/TxBD rings
256          */
257         mpc5xxx_fec_rbd_init(fec);
258         mpc5xxx_fec_tbd_init(fec);
259
260         /*
261          * Clear FEC-Lite interrupt event register(IEVENT)
262          */
263         fec->eth->ievent = 0xffffffff;
264
265         /*
266          * Set interrupt mask register
267          */
268         fec->eth->imask = 0x00000000;
269
270         /*
271          * Set FEC-Lite receive control register(R_CNTRL):
272          */
273         if (fec->xcv_type == SEVENWIRE) {
274                 /*
275                  * Frame length=1518; 7-wire mode
276                  */
277                 fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
278         } else {
279                 /*
280                  * Frame length=1518; MII mode;
281                  */
282                 fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
283         }
284
285         fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
286         if (fec->xcv_type != SEVENWIRE) {
287                 /*
288                  * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
289                  * and do not drop the Preamble.
290                  */
291                 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
292         }
293
294         /*
295          * Set Opcode/Pause Duration Register
296          */
297         fec->eth->op_pause = 0x00010020;        /*FIXME0xffff0020; */
298
299         /*
300          * Set Rx FIFO alarm and granularity value
301          */
302         fec->eth->rfifo_cntrl = 0x0c000000
303                                 | (fec->eth->rfifo_cntrl & ~0x0f000000);
304         fec->eth->rfifo_alarm = 0x0000030c;
305 #if (DEBUG & 0x22)
306         if (fec->eth->rfifo_status & 0x00700000 ) {
307                 printf("mpc5xxx_fec_init() RFIFO error\n");
308         }
309 #endif
310
311         /*
312          * Set Tx FIFO granularity value
313          */
314         fec->eth->tfifo_cntrl = 0x0c000000
315                                 | (fec->eth->tfifo_cntrl & ~0x0f000000);
316 #if (DEBUG & 0x2)
317         printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
318         printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
319 #endif
320
321         /*
322          * Set transmit fifo watermark register(X_WMRK), default = 64
323          */
324         fec->eth->tfifo_alarm = 0x00000080;
325         fec->eth->x_wmrk = 0x2;
326
327         /*
328          * Set individual address filter for unicast address
329          * and set physical address registers.
330          */
331         mpc5xxx_fec_set_hwaddr(fec, (char *)dev->enetaddr);
332
333         /*
334          * Set multicast address filter
335          */
336         fec->eth->gaddr1 = 0x00000000;
337         fec->eth->gaddr2 = 0x00000000;
338
339         /*
340          * Turn ON cheater FSM: ????
341          */
342         fec->eth->xmit_fsm = 0x03000000;
343
344 #if defined(CONFIG_MPC5200)
345         /*
346          * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
347          * work w/ the current receive task.
348          */
349          sdma->PtdCntrl |= 0x00000001;
350 #endif
351
352         /*
353          * Set priority of different initiators
354          */
355         sdma->IPR0 = 7;         /* always */
356         sdma->IPR3 = 6;         /* Eth RX */
357         sdma->IPR4 = 5;         /* Eth Tx */
358
359         /*
360          * Clear SmartDMA task interrupt pending bits
361          */
362         SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
363
364         /*
365          * Initialize SmartDMA parameters stored in SRAM
366          */
367         *(volatile int *)FEC_TBD_BASE = (int)fec->tbdBase;
368         *(volatile int *)FEC_RBD_BASE = (int)fec->rbdBase;
369         *(volatile int *)FEC_TBD_NEXT = (int)fec->tbdBase;
370         *(volatile int *)FEC_RBD_NEXT = (int)fec->rbdBase;
371
372         /*
373          * Enable FEC-Lite controller
374          */
375         fec->eth->ecntrl |= 0x00000006;
376
377 #if (DEBUG & 0x2)
378         if (fec->xcv_type != SEVENWIRE)
379                 mpc5xxx_fec_phydump (dev->name);
380 #endif
381
382         /*
383          * Enable SmartDMA receive task
384          */
385         SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
386
387 #if (DEBUG & 0x1)
388         printf("mpc5xxx_fec_init... Done \n");
389 #endif
390
391         return 1;
392 }
393
394 /********************************************************************/
395 static int mpc5xxx_fec_init_phy(struct eth_device *dev, bd_t * bis)
396 {
397         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
398 #ifndef CONFIG_MOTIONPRO
399         const uint8 phyAddr = CONFIG_PHY_ADDR;  /* Only one PHY */
400 #endif /* !CONFIG_MOTIONPRO */
401
402 #if (DEBUG & 0x1)
403         printf ("mpc5xxx_fec_init_phy... Begin\n");
404 #endif
405
406         /*
407          * Initialize GPIO pins
408          */
409         if (fec->xcv_type == SEVENWIRE) {
410                 /*  10MBit with 7-wire operation */
411 #if defined(CONFIG_TOTAL5200)
412                 /* 7-wire and USB2 on Ethernet */
413                 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00030000;
414 #else   /* !CONFIG_TOTAL5200 */
415                 /* 7-wire only */
416                 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
417 #endif  /* CONFIG_TOTAL5200 */
418         } else {
419                 /* 100MBit with MD operation */
420                 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
421         }
422
423         /*
424          * Clear FEC-Lite interrupt event register(IEVENT)
425          */
426         fec->eth->ievent = 0xffffffff;
427
428         /*
429          * Set interrupt mask register
430          */
431         fec->eth->imask = 0x00000000;
432
433 /*
434  * In original Promess-provided code PHY initialization is disabled with the
435  * following comment: "Phy initialization is DISABLED for now.  There was a
436  * problem with running 100 Mbps on PRO board". Thus we temporarily disable
437  * PHY initialization for the Motion-PRO board, until a proper fix is found.
438  */
439
440 #ifndef CONFIG_MOTIONPRO
441         if (fec->xcv_type != SEVENWIRE) {
442                 /*
443                  * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
444                  * and do not drop the Preamble.
445                  */
446                 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
447         }
448
449         if (fec->xcv_type != SEVENWIRE) {
450                 /*
451                  * Initialize PHY(LXT971A):
452                  *
453                  *   Generally, on power up, the LXT971A reads its configuration
454                  *   pins to check for forced operation, If not cofigured for
455                  *   forced operation, it uses auto-negotiation/parallel detection
456                  *   to automatically determine line operating conditions.
457                  *   If the PHY device on the other side of the link supports
458                  *   auto-negotiation, the LXT971A auto-negotiates with it
459                  *   using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
460                  *   support auto-negotiation, the LXT971A automatically detects
461                  *   the presence of either link pulses(10Mbps PHY) or Idle
462                  *   symbols(100Mbps) and sets its operating conditions accordingly.
463                  *
464                  *   When auto-negotiation is controlled by software, the following
465                  *   steps are recommended.
466                  *
467                  * Note:
468                  *   The physical address is dependent on hardware configuration.
469                  *
470                  */
471                 int timeout = 1;
472                 uint16 phyStatus;
473
474                 /*
475                  * Reset PHY, then delay 300ns
476                  */
477                 miiphy_write(dev->name, phyAddr, 0x0, 0x8000);
478                 udelay(1000);
479
480 #if defined(CONFIG_UC101)
481                 /* Set the LED configuration Register for the UC101 Board */
482                 miiphy_write(dev->name, phyAddr, 0x14, 0x4122);
483 #endif
484                 if (fec->xcv_type == MII10) {
485                         /*
486                          * Force 10Base-T, FDX operation
487                          */
488 #if (DEBUG & 0x2)
489                         printf("Forcing 10 Mbps ethernet link... ");
490 #endif
491                         miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
492                         /*
493                         miiphy_write(dev->name, fec, phyAddr, 0x0, 0x0100);
494                         */
495                         miiphy_write(dev->name, phyAddr, 0x0, 0x0180);
496
497                         timeout = 20;
498                         do {    /* wait for link status to go down */
499                                 udelay(10000);
500                                 if ((timeout--) == 0) {
501 #if (DEBUG & 0x2)
502                                         printf("hmmm, should not have waited...");
503 #endif
504                                         break;
505                                 }
506                                 miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
507 #if (DEBUG & 0x2)
508                                 printf("=");
509 #endif
510                         } while ((phyStatus & 0x0004)); /* !link up */
511
512                         timeout = 1000;
513                         do {    /* wait for link status to come back up */
514                                 udelay(10000);
515                                 if ((timeout--) == 0) {
516                                         printf("failed. Link is down.\n");
517                                         break;
518                                 }
519                                 miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
520 #if (DEBUG & 0x2)
521                                 printf("+");
522 #endif
523                         } while (!(phyStatus & 0x0004));        /* !link up */
524
525 #if (DEBUG & 0x2)
526                         printf ("done.\n");
527 #endif
528                 } else {        /* MII100 */
529                         /*
530                          * Set the auto-negotiation advertisement register bits
531                          */
532                         miiphy_write(dev->name, phyAddr, 0x4, 0x01e1);
533
534                         /*
535                          * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
536                          */
537                         miiphy_write(dev->name, phyAddr, 0x0, 0x1200);
538
539                         /*
540                          * Wait for AN completion
541                          */
542                         timeout = 5000;
543                         do {
544                                 udelay(1000);
545
546                                 if ((timeout--) == 0) {
547 #if (DEBUG & 0x2)
548                                         printf("PHY auto neg 0 failed...\n");
549 #endif
550                                         return -1;
551                                 }
552
553                                 if (miiphy_read(dev->name, phyAddr, 0x1, &phyStatus) != 0) {
554 #if (DEBUG & 0x2)
555                                         printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
556 #endif
557                                         return -1;
558                                 }
559                         } while (!(phyStatus & 0x0004));
560
561 #if (DEBUG & 0x2)
562                         printf("PHY auto neg complete! \n");
563 #endif
564                 }
565
566         }
567 #endif /* !CONFIG_MOTIONPRO */
568
569 #if (DEBUG & 0x2)
570         if (fec->xcv_type != SEVENWIRE)
571                 mpc5xxx_fec_phydump (dev->name);
572 #endif
573
574
575 #if (DEBUG & 0x1)
576         printf("mpc5xxx_fec_init_phy... Done \n");
577 #endif
578
579         return 1;
580 }
581
582 /********************************************************************/
583 static void mpc5xxx_fec_halt(struct eth_device *dev)
584 {
585 #if defined(CONFIG_MPC5200)
586         struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
587 #endif
588         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
589         int counter = 0xffff;
590
591 #if (DEBUG & 0x2)
592         if (fec->xcv_type != SEVENWIRE)
593                 mpc5xxx_fec_phydump (dev->name);
594 #endif
595
596         /*
597          * mask FEC chip interrupts
598          */
599         fec->eth->imask = 0;
600
601         /*
602          * issue graceful stop command to the FEC transmitter if necessary
603          */
604         fec->eth->x_cntrl |= 0x00000001;
605
606         /*
607          * wait for graceful stop to register
608          */
609         while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
610
611         /*
612          * Disable SmartDMA tasks
613          */
614         SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
615         SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
616
617 #if defined(CONFIG_MPC5200)
618         /*
619          * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
620          * done. It doesn't work w/ the current receive task.
621          */
622          sdma->PtdCntrl &= ~0x00000001;
623 #endif
624
625         /*
626          * Disable the Ethernet Controller
627          */
628         fec->eth->ecntrl &= 0xfffffffd;
629
630         /*
631          * Clear FIFO status registers
632          */
633         fec->eth->rfifo_status &= 0x00700000;
634         fec->eth->tfifo_status &= 0x00700000;
635
636         fec->eth->reset_cntrl = 0x01000000;
637
638         /*
639          * Issue a reset command to the FEC chip
640          */
641         fec->eth->ecntrl |= 0x1;
642
643         /*
644          * wait at least 16 clock cycles
645          */
646         udelay(10);
647
648 #if (DEBUG & 0x3)
649         printf("Ethernet task stopped\n");
650 #endif
651 }
652
653 #if (DEBUG & 0x60)
654 /********************************************************************/
655
656 static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec)
657 {
658         uint16 phyAddr = CONFIG_PHY_ADDR;
659         uint16 phyStatus;
660
661         if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
662                 || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
663
664                 miiphy_read(devname, phyAddr, 0x1, &phyStatus);
665                 printf("\nphyStatus: 0x%04x\n", phyStatus);
666                 printf("ecntrl:   0x%08x\n", fec->eth->ecntrl);
667                 printf("ievent:   0x%08x\n", fec->eth->ievent);
668                 printf("x_status: 0x%08x\n", fec->eth->x_status);
669                 printf("tfifo: status  0x%08x\n", fec->eth->tfifo_status);
670
671                 printf("       control 0x%08x\n", fec->eth->tfifo_cntrl);
672                 printf("       lrfp    0x%08x\n", fec->eth->tfifo_lrf_ptr);
673                 printf("       lwfp    0x%08x\n", fec->eth->tfifo_lwf_ptr);
674                 printf("       alarm   0x%08x\n", fec->eth->tfifo_alarm);
675                 printf("       readptr 0x%08x\n", fec->eth->tfifo_rdptr);
676                 printf("       writptr 0x%08x\n", fec->eth->tfifo_wrptr);
677         }
678 }
679
680 static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec)
681 {
682         uint16 phyAddr = CONFIG_PHY_ADDR;
683         uint16 phyStatus;
684
685         if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
686                 || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
687
688                 miiphy_read(devname, phyAddr, 0x1, &phyStatus);
689                 printf("\nphyStatus: 0x%04x\n", phyStatus);
690                 printf("ecntrl:   0x%08x\n", fec->eth->ecntrl);
691                 printf("ievent:   0x%08x\n", fec->eth->ievent);
692                 printf("x_status: 0x%08x\n", fec->eth->x_status);
693                 printf("rfifo: status  0x%08x\n", fec->eth->rfifo_status);
694
695                 printf("       control 0x%08x\n", fec->eth->rfifo_cntrl);
696                 printf("       lrfp    0x%08x\n", fec->eth->rfifo_lrf_ptr);
697                 printf("       lwfp    0x%08x\n", fec->eth->rfifo_lwf_ptr);
698                 printf("       alarm   0x%08x\n", fec->eth->rfifo_alarm);
699                 printf("       readptr 0x%08x\n", fec->eth->rfifo_rdptr);
700                 printf("       writptr 0x%08x\n", fec->eth->rfifo_wrptr);
701         }
702 }
703 #endif /* DEBUG */
704
705 /********************************************************************/
706
707 static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
708                 int data_length)
709 {
710         /*
711          * This routine transmits one frame.  This routine only accepts
712          * 6-byte Ethernet addresses.
713          */
714         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
715         volatile FEC_TBD *pTbd;
716
717 #if (DEBUG & 0x20)
718         printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
719         tfifo_print(dev->name, fec);
720 #endif
721
722         /*
723          * Clear Tx BD ring at first
724          */
725         mpc5xxx_fec_tbd_scrub(fec);
726
727         /*
728          * Check for valid length of data.
729          */
730         if ((data_length > 1500) || (data_length <= 0)) {
731                 return -1;
732         }
733
734         /*
735          * Check the number of vacant TxBDs.
736          */
737         if (fec->cleanTbdNum < 1) {
738 #if (DEBUG & 0x20)
739                 printf("No available TxBDs ...\n");
740 #endif
741                 return -1;
742         }
743
744         /*
745          * Get the first TxBD to send the mac header
746          */
747         pTbd = &fec->tbdBase[fec->tbdIndex];
748         pTbd->dataLength = data_length;
749         pTbd->dataPointer = (uint32)eth_data;
750         pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
751         fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
752
753 #if (DEBUG & 0x100)
754         printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
755 #endif
756
757         /*
758          * Kick the MII i/f
759          */
760         if (fec->xcv_type != SEVENWIRE) {
761                 uint16 phyStatus;
762                 miiphy_read(dev->name, 0, 0x1, &phyStatus);
763         }
764
765         /*
766          * Enable SmartDMA transmit task
767          */
768
769 #if (DEBUG & 0x20)
770         tfifo_print(dev->name, fec);
771 #endif
772         SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
773 #if (DEBUG & 0x20)
774         tfifo_print(dev->name, fec);
775 #endif
776 #if (DEBUG & 0x8)
777         printf( "+" );
778 #endif
779
780         fec->cleanTbdNum -= 1;
781
782 #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
783         printf ("smartDMA ethernet Tx task enabled\n");
784 #endif
785         /*
786          * wait until frame is sent .
787          */
788         while (pTbd->status & FEC_TBD_READY) {
789                 udelay(10);
790 #if (DEBUG & 0x8)
791                 printf ("TDB status = %04x\n", pTbd->status);
792 #endif
793         }
794
795         return 0;
796 }
797
798
799 /********************************************************************/
800 static int mpc5xxx_fec_recv(struct eth_device *dev)
801 {
802         /*
803          * This command pulls one frame from the card
804          */
805         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
806         volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
807         unsigned long ievent;
808         int frame_length, len = 0;
809         NBUF *frame;
810         uchar buff[FEC_MAX_PKT_SIZE];
811
812 #if (DEBUG & 0x1)
813         printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
814 #endif
815 #if (DEBUG & 0x8)
816         printf( "-" );
817 #endif
818
819         /*
820          * Check if any critical events have happened
821          */
822         ievent = fec->eth->ievent;
823         fec->eth->ievent = ievent;
824         if (ievent & 0x20060000) {
825                 /* BABT, Rx/Tx FIFO errors */
826                 mpc5xxx_fec_halt(dev);
827                 mpc5xxx_fec_init(dev, NULL);
828                 return 0;
829         }
830         if (ievent & 0x80000000) {
831                 /* Heartbeat error */
832                 fec->eth->x_cntrl |= 0x00000001;
833         }
834         if (ievent & 0x10000000) {
835                 /* Graceful stop complete */
836                 if (fec->eth->x_cntrl & 0x00000001) {
837                         mpc5xxx_fec_halt(dev);
838                         fec->eth->x_cntrl &= ~0x00000001;
839                         mpc5xxx_fec_init(dev, NULL);
840                 }
841         }
842
843         if (!(pRbd->status & FEC_RBD_EMPTY)) {
844                 if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
845                         ((pRbd->dataLength - 4) > 14)) {
846
847                         /*
848                          * Get buffer address and size
849                          */
850                         frame = (NBUF *)pRbd->dataPointer;
851                         frame_length = pRbd->dataLength - 4;
852
853 #if (DEBUG & 0x20)
854                         {
855                                 int i;
856                                 printf("recv data hdr:");
857                                 for (i = 0; i < 14; i++)
858                                         printf("%x ", *(frame->head + i));
859                                 printf("\n");
860                         }
861 #endif
862                         /*
863                          *  Fill the buffer and pass it to upper layers
864                          */
865                         memcpy(buff, frame->head, 14);
866                         memcpy(buff + 14, frame->data, frame_length);
867                         NetReceive(buff, frame_length);
868                         len = frame_length;
869                 }
870                 /*
871                  * Reset buffer descriptor as empty
872                  */
873                 mpc5xxx_fec_rbd_clean(fec, pRbd);
874         }
875         SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
876         return len;
877 }
878
879
880 /********************************************************************/
881 int mpc5xxx_fec_initialize(bd_t * bis)
882 {
883         mpc5xxx_fec_priv *fec;
884         struct eth_device *dev;
885         char *tmp, *end;
886         char env_enetaddr[6];
887         int i;
888
889         fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
890         dev = (struct eth_device *)malloc(sizeof(*dev));
891         memset(dev, 0, sizeof *dev);
892
893         fec->eth = (ethernet_regs *)MPC5XXX_FEC;
894         fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
895         fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
896 #if defined(CONFIG_CANMB)    || defined(CONFIG_HMI1001) || \
897     defined(CONFIG_ICECUBE)  || defined(CONFIG_INKA4X0) || \
898     defined(CONFIG_JUPITER)  || defined(CONFIG_MCC200)  || \
899     defined(CONFIG_MOTIONPRO)|| defined(CONFIG_O2DNT)   || \
900     defined(CONFIG_PM520)    || defined(CONFIG_TOP5200) || \
901     defined(CONFIG_TQM5200)  || defined(CONFIG_UC101)   || \
902     defined(CONFIG_V38B)
903 # ifndef CONFIG_FEC_10MBIT
904         fec->xcv_type = MII100;
905 # else
906         fec->xcv_type = MII10;
907 # endif
908 #elif defined(CONFIG_TOTAL5200)
909         fec->xcv_type = SEVENWIRE;
910 #else
911 #error fec->xcv_type not initialized.
912 #endif
913
914         dev->priv = (void *)fec;
915         dev->iobase = MPC5XXX_FEC;
916         dev->init = mpc5xxx_fec_init;
917         dev->halt = mpc5xxx_fec_halt;
918         dev->send = mpc5xxx_fec_send;
919         dev->recv = mpc5xxx_fec_recv;
920
921         sprintf(dev->name, "FEC ETHERNET");
922         eth_register(dev);
923
924 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
925         miiphy_register (dev->name,
926                         fec5xxx_miiphy_read, fec5xxx_miiphy_write);
927 #endif
928
929         /*
930          * Try to set the mac address now. The fec mac address is
931          * a garbage after reset. When not using fec for booting
932          * the Linux fec driver will try to work with this garbage.
933          */
934         tmp = getenv("ethaddr");
935         if (tmp) {
936                 for (i=0; i<6; i++) {
937                         env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
938                         if (tmp)
939                                 tmp = (*end) ? end+1 : end;
940                 }
941                 mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
942         }
943
944         mpc5xxx_fec_init_phy(dev, bis);
945
946         return 1;
947 }
948
949 /* MII-interface related functions */
950 /********************************************************************/
951 int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
952 {
953         ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
954         uint32 reg;             /* convenient holder for the PHY register */
955         uint32 phy;             /* convenient holder for the PHY */
956         int timeout = 0xffff;
957
958         /*
959          * reading from any PHY's register is done by properly
960          * programming the FEC's MII data register.
961          */
962         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
963         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
964
965         eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
966
967         /*
968          * wait for the related interrupt
969          */
970         while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
971
972         if (timeout == 0) {
973 #if (DEBUG & 0x2)
974                 printf ("Read MDIO failed...\n");
975 #endif
976                 return -1;
977         }
978
979         /*
980          * clear mii interrupt bit
981          */
982         eth->ievent = 0x00800000;
983
984         /*
985          * it's now safe to read the PHY's register
986          */
987         *retVal = (uint16) eth->mii_data;
988
989         return 0;
990 }
991
992 /********************************************************************/
993 int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
994 {
995         ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
996         uint32 reg;             /* convenient holder for the PHY register */
997         uint32 phy;             /* convenient holder for the PHY */
998         int timeout = 0xffff;
999
1000         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
1001         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
1002
1003         eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
1004                         FEC_MII_DATA_TA | phy | reg | data);
1005
1006         /*
1007          * wait for the MII interrupt
1008          */
1009         while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
1010
1011         if (timeout == 0) {
1012 #if (DEBUG & 0x2)
1013                 printf ("Write MDIO failed...\n");
1014 #endif
1015                 return -1;
1016         }
1017
1018         /*
1019          * clear MII interrupt bit
1020          */
1021         eth->ievent = 0x00800000;
1022
1023         return 0;
1024 }
1025
1026 #if (DEBUG & 0x40)
1027 static uint32 local_crc32(char *string, unsigned int crc_value, int len)
1028 {
1029         int i;
1030         char c;
1031         unsigned int crc, count;
1032
1033         /*
1034          * crc32 algorithm
1035          */
1036         /*
1037          * crc = 0xffffffff; * The initialized value should be 0xffffffff
1038          */
1039         crc = crc_value;
1040
1041         for (i = len; --i >= 0;) {
1042                 c = *string++;
1043                 for (count = 0; count < 8; count++) {
1044                         if ((c & 0x01) ^ (crc & 0x01)) {
1045                                 crc >>= 1;
1046                                 crc = crc ^ 0xedb88320;
1047                         } else {
1048                                 crc >>= 1;
1049                         }
1050                         c >>= 1;
1051                 }
1052         }
1053
1054         /*
1055          * In big endian system, do byte swaping for crc value
1056          */
1057          /**/ return crc;
1058 }
1059 #endif  /* DEBUG */
1060
1061 #endif /* CONFIG_MPC5xxx_FEC */