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