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