* Patch by Mark Jonas: Remove config.tmp files only when
[kernel/u-boot.git] / 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         fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
290         if (fec->xcv_type != SEVENWIRE) {
291                 /*
292                  * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
293                  * and do not drop the Preamble.
294                  */
295                 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
296         }
297
298         /*
299          * Set Opcode/Pause Duration Register
300          */
301         fec->eth->op_pause = 0x00010020;        /*FIXME0xffff0020; */
302
303         /*
304          * Set Rx FIFO alarm and granularity value
305          */
306         fec->eth->rfifo_cntrl = 0x0c000000;
307         fec->eth->rfifo_alarm = 0x0000030c;
308 #if (DEBUG & 0x22)
309         if (fec->eth->rfifo_status & 0x00700000 ) {
310                 printf("mpc5xxx_fec_init() RFIFO error\n");
311         }
312 #endif
313
314         /*
315          * Set Tx FIFO granularity value
316          */
317         fec->eth->tfifo_cntrl = 0x0c000000;
318 #if (DEBUG & 0x2)
319         printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
320         printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
321 #endif
322
323         /*
324          * Set transmit fifo watermark register(X_WMRK), default = 64
325          */
326         fec->eth->tfifo_alarm = 0x00000080;
327         fec->eth->x_wmrk = 0x2;
328
329         /*
330          * Set individual address filter for unicast address
331          * and set physical address registers.
332          */
333         mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
334
335         /*
336          * Set multicast address filter
337          */
338         fec->eth->gaddr1 = 0x00000000;
339         fec->eth->gaddr2 = 0x00000000;
340
341         /*
342          * Turn ON cheater FSM: ????
343          */
344         fec->eth->xmit_fsm = 0x03000000;
345
346 #if defined(CONFIG_MPC5200)
347         /*
348          * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
349          * work w/ the current receive task.
350          */
351          sdma->PtdCntrl |= 0x00000001;
352 #endif
353
354         /*
355          * Set priority of different initiators
356          */
357         sdma->IPR0 = 7;         /* always */
358         sdma->IPR3 = 6;         /* Eth RX */
359         sdma->IPR4 = 5;         /* Eth Tx */
360
361         /*
362          * Clear SmartDMA task interrupt pending bits
363          */
364         SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
365
366         /*
367          * Initialize SmartDMA parameters stored in SRAM
368          */
369         *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
370         *(int *)FEC_RBD_BASE = (int)fec->rbdBase;
371         *(int *)FEC_TBD_NEXT = (int)fec->tbdBase;
372         *(int *)FEC_RBD_NEXT = (int)fec->rbdBase;
373
374         if (fec->xcv_type != SEVENWIRE) {
375                 /*
376                  * Initialize PHY(LXT971A):
377                  *
378                  *   Generally, on power up, the LXT971A reads its configuration
379                  *   pins to check for forced operation, If not cofigured for
380                  *   forced operation, it uses auto-negotiation/parallel detection
381                  *   to automatically determine line operating conditions.
382                  *   If the PHY device on the other side of the link supports
383                  *   auto-negotiation, the LXT971A auto-negotiates with it
384                  *   using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
385                  *   support auto-negotiation, the LXT971A automatically detects
386                  *   the presence of either link pulses(10Mbps PHY) or Idle
387                  *   symbols(100Mbps) and sets its operating conditions accordingly.
388                  *
389                  *   When auto-negotiation is controlled by software, the following
390                  *   steps are recommended.
391                  *
392                  * Note:
393                  *   The physical address is dependent on hardware configuration.
394                  *
395                  */
396                 int timeout = 1;
397                 uint16 phyStatus;
398
399                 /*
400                  * Reset PHY, then delay 300ns
401                  */
402                 miiphy_write(phyAddr, 0x0, 0x8000);
403                 udelay(1000);
404
405                 if (fec->xcv_type == MII10) {
406                         /*
407                          * Force 10Base-T, FDX operation
408                          */
409 #if (DEBUG & 0x2)
410                         printf("Forcing 10 Mbps ethernet link... ");
411 #endif
412                         miiphy_read(phyAddr, 0x1, &phyStatus);
413                         /*
414                         miiphy_write(fec, phyAddr, 0x0, 0x0100);
415                         */
416                         miiphy_write(phyAddr, 0x0, 0x0180);
417
418                         timeout = 20;
419                         do {    /* wait for link status to go down */
420                                 udelay(10000);
421                                 if ((timeout--) == 0) {
422 #if (DEBUG & 0x2)
423                                         printf("hmmm, should not have waited...");
424 #endif
425                                         break;
426                                 }
427                                 miiphy_read(phyAddr, 0x1, &phyStatus);
428 #if (DEBUG & 0x2)
429                                 printf("=");
430 #endif
431                         } while ((phyStatus & 0x0004)); /* !link up */
432
433                         timeout = 1000;
434                         do {    /* wait for link status to come back up */
435                                 udelay(10000);
436                                 if ((timeout--) == 0) {
437                                         printf("failed. Link is down.\n");
438                                         break;
439                                 }
440                                 miiphy_read(phyAddr, 0x1, &phyStatus);
441 #if (DEBUG & 0x2)
442                                 printf("+");
443 #endif
444                         } while (!(phyStatus & 0x0004));        /* !link up */
445
446 #if (DEBUG & 0x2)
447                         printf ("done.\n");
448 #endif
449                 } else {        /* MII100 */
450                         /*
451                          * Set the auto-negotiation advertisement register bits
452                          */
453                         miiphy_write(phyAddr, 0x4, 0x01e1);
454
455                         /*
456                          * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
457                          */
458                         miiphy_write(phyAddr, 0x0, 0x1200);
459
460                         /*
461                          * Wait for AN completion
462                          */
463                         timeout = 5000;
464                         do {
465                                 udelay(1000);
466
467                                 if ((timeout--) == 0) {
468 #if (DEBUG & 0x2)
469                                         printf("PHY auto neg 0 failed...\n");
470 #endif
471                                         return -1;
472                                 }
473
474                                 if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
475 #if (DEBUG & 0x2)
476                                         printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
477 #endif
478                                         return -1;
479                                 }
480                         } while (!(phyStatus & 0x0004));
481
482 #if (DEBUG & 0x2)
483                         printf("PHY auto neg complete! \n");
484 #endif
485                 }
486
487         }
488
489         /*
490          * Enable FEC-Lite controller
491          */
492         fec->eth->ecntrl |= 0x00000006;
493
494 #if (DEBUG & 0x2)
495         if (fec->xcv_type != SEVENWIRE)
496                 mpc5xxx_fec_phydump ();
497 #endif
498
499         /*
500          * Enable SmartDMA receive task
501          */
502         SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
503
504 #if (DEBUG & 0x1)
505         printf("mpc5xxx_fec_init... Done \n");
506 #endif
507
508         return 1;
509 }
510
511 /********************************************************************/
512 static void mpc5xxx_fec_halt(struct eth_device *dev)
513 {
514 #if defined(CONFIG_MPC5200)
515         struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
516 #endif
517         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
518         int counter = 0xffff;
519
520 #if (DEBUG & 0x2)
521         if (fec->xcv_type != SEVENWIRE)
522                 mpc5xxx_fec_phydump ();
523 #endif
524
525         /*
526          * mask FEC chip interrupts
527          */
528         fec->eth->imask = 0;
529
530         /*
531          * issue graceful stop command to the FEC transmitter if necessary
532          */
533         fec->eth->x_cntrl |= 0x00000001;
534
535         /*
536          * wait for graceful stop to register
537          */
538         while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
539
540         /*
541          * Disable SmartDMA tasks
542          */
543         SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
544         SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
545
546 #if defined(CONFIG_MPC5200)
547         /*
548          * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
549          * done. It doesn't work w/ the current receive task.
550          */
551          sdma->PtdCntrl &= ~0x00000001;
552 #endif
553
554         /*
555          * Disable the Ethernet Controller
556          */
557         fec->eth->ecntrl &= 0xfffffffd;
558
559         /*
560          * Clear FIFO status registers
561          */
562         fec->eth->rfifo_status &= 0x00700000;
563         fec->eth->tfifo_status &= 0x00700000;
564
565         fec->eth->reset_cntrl = 0x01000000;
566
567         /*
568          * Issue a reset command to the FEC chip
569          */
570         fec->eth->ecntrl |= 0x1;
571
572         /*
573          * wait at least 16 clock cycles
574          */
575         udelay(10);
576
577 #if (DEBUG & 0x3)
578         printf("Ethernet task stopped\n");
579 #endif
580 }
581
582 #if (DEBUG & 0x60)
583 /********************************************************************/
584
585 static void tfifo_print(mpc5xxx_fec_priv *fec)
586 {
587         uint16 phyAddr = CONFIG_PHY_ADDR;
588         uint16 phyStatus;
589
590         if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
591                 || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
592
593                 miiphy_read(phyAddr, 0x1, &phyStatus);
594                 printf("\nphyStatus: 0x%04x\n", phyStatus);
595                 printf("ecntrl:   0x%08x\n", fec->eth->ecntrl);
596                 printf("ievent:   0x%08x\n", fec->eth->ievent);
597                 printf("x_status: 0x%08x\n", fec->eth->x_status);
598                 printf("tfifo: status  0x%08x\n", fec->eth->tfifo_status);
599
600                 printf("       control 0x%08x\n", fec->eth->tfifo_cntrl);
601                 printf("       lrfp    0x%08x\n", fec->eth->tfifo_lrf_ptr);
602                 printf("       lwfp    0x%08x\n", fec->eth->tfifo_lwf_ptr);
603                 printf("       alarm   0x%08x\n", fec->eth->tfifo_alarm);
604                 printf("       readptr 0x%08x\n", fec->eth->tfifo_rdptr);
605                 printf("       writptr 0x%08x\n", fec->eth->tfifo_wrptr);
606         }
607 }
608
609 static void rfifo_print(mpc5xxx_fec_priv *fec)
610 {
611         uint16 phyAddr = CONFIG_PHY_ADDR;
612         uint16 phyStatus;
613
614         if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
615                 || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
616
617                 miiphy_read(phyAddr, 0x1, &phyStatus);
618                 printf("\nphyStatus: 0x%04x\n", phyStatus);
619                 printf("ecntrl:   0x%08x\n", fec->eth->ecntrl);
620                 printf("ievent:   0x%08x\n", fec->eth->ievent);
621                 printf("x_status: 0x%08x\n", fec->eth->x_status);
622                 printf("rfifo: status  0x%08x\n", fec->eth->rfifo_status);
623
624                 printf("       control 0x%08x\n", fec->eth->rfifo_cntrl);
625                 printf("       lrfp    0x%08x\n", fec->eth->rfifo_lrf_ptr);
626                 printf("       lwfp    0x%08x\n", fec->eth->rfifo_lwf_ptr);
627                 printf("       alarm   0x%08x\n", fec->eth->rfifo_alarm);
628                 printf("       readptr 0x%08x\n", fec->eth->rfifo_rdptr);
629                 printf("       writptr 0x%08x\n", fec->eth->rfifo_wrptr);
630         }
631 }
632 #endif /* DEBUG */
633
634 /********************************************************************/
635
636 static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
637                 int data_length)
638 {
639         /*
640          * This routine transmits one frame.  This routine only accepts
641          * 6-byte Ethernet addresses.
642          */
643         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
644         FEC_TBD *pTbd;
645
646 #if (DEBUG & 0x20)
647         printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
648         tfifo_print(fec);
649 #endif
650
651         /*
652          * Clear Tx BD ring at first
653          */
654         mpc5xxx_fec_tbd_scrub(fec);
655
656         /*
657          * Check for valid length of data.
658          */
659         if ((data_length > 1500) || (data_length <= 0)) {
660                 return -1;
661         }
662
663         /*
664          * Check the number of vacant TxBDs.
665          */
666         if (fec->cleanTbdNum < 1) {
667 #if (DEBUG & 0x20)
668                 printf("No available TxBDs ...\n");
669 #endif
670                 return -1;
671         }
672
673         /*
674          * Get the first TxBD to send the mac header
675          */
676         pTbd = &fec->tbdBase[fec->tbdIndex];
677         pTbd->dataLength = data_length;
678         pTbd->dataPointer = (uint32)eth_data;
679         pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
680         fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
681
682 #if (DEBUG & 0x100)
683         printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
684 #endif
685
686         /*
687          * Kick the MII i/f
688          */
689         if (fec->xcv_type != SEVENWIRE) {
690                 uint16 phyStatus;
691                 miiphy_read(0, 0x1, &phyStatus);
692         }
693
694         /*
695          * Enable SmartDMA transmit task
696          */
697
698 #if (DEBUG & 0x20)
699         tfifo_print(fec);
700 #endif
701         SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
702 #if (DEBUG & 0x20)
703         tfifo_print(fec);
704 #endif
705 #if (DEBUG & 0x8)
706         printf( "+" );
707 #endif
708
709         fec->cleanTbdNum -= 1;
710
711 #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
712         printf ("smartDMA ethernet Tx task enabled\n");
713 #endif
714         /*
715          * wait until frame is sent .
716          */
717         while (pTbd->status & FEC_TBD_READY) {
718                 udelay(10);
719 #if (DEBUG & 0x8)
720                 printf ("TDB status = %04x\n", pTbd->status);
721 #endif
722         }
723
724         return 0;
725 }
726
727
728 /********************************************************************/
729 static int mpc5xxx_fec_recv(struct eth_device *dev)
730 {
731         /*
732          * This command pulls one frame from the card
733          */
734         mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
735         FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
736         unsigned long ievent;
737         int frame_length, len = 0;
738         NBUF *frame;
739         char buff[FEC_MAX_PKT_SIZE];
740
741 #if (DEBUG & 0x1)
742         printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
743 #endif
744 #if (DEBUG & 0x8)
745         printf( "-" );
746 #endif
747
748         /*
749          * Check if any critical events have happened
750          */
751         ievent = fec->eth->ievent;
752         fec->eth->ievent = ievent;
753         if (ievent & 0x20060000) {
754                 /* BABT, Rx/Tx FIFO errors */
755                 mpc5xxx_fec_halt(dev);
756                 mpc5xxx_fec_init(dev, NULL);
757                 return 0;
758         }
759         if (ievent & 0x80000000) {
760                 /* Heartbeat error */
761                 fec->eth->x_cntrl |= 0x00000001;
762         }
763         if (ievent & 0x10000000) {
764                 /* Graceful stop complete */
765                 if (fec->eth->x_cntrl & 0x00000001) {
766                         mpc5xxx_fec_halt(dev);
767                         fec->eth->x_cntrl &= ~0x00000001;
768                         mpc5xxx_fec_init(dev, NULL);
769                 }
770         }
771
772         if (!(pRbd->status & FEC_RBD_EMPTY)) {
773                 if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
774                         ((pRbd->dataLength - 4) > 14)) {
775
776                         /*
777                          * Get buffer address and size
778                          */
779                         frame = (NBUF *)pRbd->dataPointer;
780                         frame_length = pRbd->dataLength - 4;
781
782 #if (DEBUG & 0x20)
783                         {
784                                 int i;
785                                 printf("recv data hdr:");
786                                 for (i = 0; i < 14; i++)
787                                         printf("%x ", *(frame->head + i));
788                                 printf("\n");
789                         }
790 #endif
791                         /*
792                          *  Fill the buffer and pass it to upper layers
793                          */
794                         memcpy(buff, frame->head, 14);
795                         memcpy(buff + 14, frame->data, frame_length);
796                         NetReceive(buff, frame_length);
797                         len = frame_length;
798                 }
799                 /*
800                  * Reset buffer descriptor as empty
801                  */
802                 mpc5xxx_fec_rbd_clean(fec, pRbd);
803         }
804         SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
805         return len;
806 }
807
808
809 /********************************************************************/
810 int mpc5xxx_fec_initialize(bd_t * bis)
811 {
812         mpc5xxx_fec_priv *fec;
813         struct eth_device *dev;
814         char *tmp, *end;
815         char env_enetaddr[6];
816         int i;
817
818         fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
819         dev = (struct eth_device *)malloc(sizeof(*dev));
820         memset(dev, 0, sizeof *dev);
821
822         fec->eth = (ethernet_regs *)MPC5XXX_FEC;
823         fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
824         fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
825 #if defined(CONFIG_ICECUBE) || \
826     defined(CONFIG_PM520)   || \
827     defined(CONFIG_TOP5200)
828 # ifndef CONFIG_FEC_10MBIT
829         fec->xcv_type = MII100;
830 # else
831         fec->xcv_type = MII10;
832 # endif
833 #else
834 #error fec->xcv_type not initialized.
835 #endif
836
837         dev->priv = (void *)fec;
838         dev->iobase = MPC5XXX_FEC;
839         dev->init = mpc5xxx_fec_init;
840         dev->halt = mpc5xxx_fec_halt;
841         dev->send = mpc5xxx_fec_send;
842         dev->recv = mpc5xxx_fec_recv;
843
844         sprintf(dev->name, "FEC ETHERNET");
845         eth_register(dev);
846
847         /*
848          * Try to set the mac address now. The fec mac address is
849          * a garbage after reset. When not using fec for booting
850          * the Linux fec driver will try to work with this garbage.
851          */
852         tmp = getenv("ethaddr");
853         if (tmp) {
854                 for (i=0; i<6; i++) {
855                         env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
856                         if (tmp)
857                                 tmp = (*end) ? end+1 : end;
858                 }
859                 mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
860         }
861
862         return 1;
863 }
864
865 /* MII-interface related functions */
866 /********************************************************************/
867 int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
868 {
869         ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
870         uint32 reg;             /* convenient holder for the PHY register */
871         uint32 phy;             /* convenient holder for the PHY */
872         int timeout = 0xffff;
873
874         /*
875          * reading from any PHY's register is done by properly
876          * programming the FEC's MII data register.
877          */
878         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
879         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
880
881         eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
882
883         /*
884          * wait for the related interrupt
885          */
886         while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
887
888         if (timeout == 0) {
889 #if (DEBUG & 0x2)
890                 printf ("Read MDIO failed...\n");
891 #endif
892                 return -1;
893         }
894
895         /*
896          * clear mii interrupt bit
897          */
898         eth->ievent = 0x00800000;
899
900         /*
901          * it's now safe to read the PHY's register
902          */
903         *retVal = (uint16) eth->mii_data;
904
905         return 0;
906 }
907
908 /********************************************************************/
909 int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
910 {
911         ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
912         uint32 reg;             /* convenient holder for the PHY register */
913         uint32 phy;             /* convenient holder for the PHY */
914         int timeout = 0xffff;
915
916         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
917         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
918
919         eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
920                         FEC_MII_DATA_TA | phy | reg | data);
921
922         /*
923          * wait for the MII interrupt
924          */
925         while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
926
927         if (timeout == 0) {
928 #if (DEBUG & 0x2)
929                 printf ("Write MDIO failed...\n");
930 #endif
931                 return -1;
932         }
933
934         /*
935          * clear MII interrupt bit
936          */
937         eth->ievent = 0x00800000;
938
939         return 0;
940 }
941
942 #if (DEBUG & 0x40)
943 static uint32 local_crc32(char *string, unsigned int crc_value, int len)
944 {
945         int i;
946         char c;
947         unsigned int crc, count;
948
949         /*
950          * crc32 algorithm
951          */
952         /*
953          * crc = 0xffffffff; * The initialized value should be 0xffffffff
954          */
955         crc = crc_value;
956
957         for (i = len; --i >= 0;) {
958                 c = *string++;
959                 for (count = 0; count < 8; count++) {
960                         if ((c & 0x01) ^ (crc & 0x01)) {
961                                 crc >>= 1;
962                                 crc = crc ^ 0xedb88320;
963                         } else {
964                                 crc >>= 1;
965                         }
966                         c >>= 1;
967                 }
968         }
969
970         /*
971          * In big endian system, do byte swaping for crc value
972          */
973          /**/ return crc;
974 }
975 #endif  /* DEBUG */
976
977 #endif /* CONFIG_MPC5xxx_FEC */