]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/mpc512x_fec.c
Merge branch 'next' of ../master
[karo-tx-uboot.git] / drivers / net / mpc512x_fec.c
1 /*
2  * (C) Copyright 2003-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * Derived from the MPC8xx FEC driver.
6  * Adapted for MPC512x by Grzegorz Bernacki <gjb@semihalf.com>
7  */
8
9 #include <common.h>
10 #include <malloc.h>
11 #include <net.h>
12 #include <netdev.h>
13 #include <miiphy.h>
14 #include <asm/io.h>
15 #include "mpc512x_fec.h"
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 #define DEBUG 0
20
21 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
22         defined(CONFIG_MPC512x_FEC)
23
24 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
25 #error "CONFIG_MII has to be defined!"
26 #endif
27
28 #if (DEBUG & 0x40)
29 static u32 local_crc32(char *string, unsigned int crc_value, int len);
30 #endif
31
32 int fec512x_miiphy_read(char *devname, u8 phyAddr, u8 regAddr, u16 * retVal);
33 int fec512x_miiphy_write(char *devname, u8 phyAddr, u8 regAddr, u16 data);
34 int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis);
35
36 static uchar rx_buff[FEC_BUFFER_SIZE];
37 static int rx_buff_idx = 0;
38
39 /********************************************************************/
40 #if (DEBUG & 0x2)
41 static void mpc512x_fec_phydump (char *devname)
42 {
43         u16 phyStatus, i;
44         u8 phyAddr = CONFIG_PHY_ADDR;
45         u8 reg_mask[] = {
46                 /* regs to print: 0...8, 21,27,31 */
47                 1, 1, 1, 1,  1, 1, 1, 1,     1, 0, 0, 0,  0, 0, 0, 0,
48                 0, 0, 0, 0,  0, 1, 0, 0,     0, 0, 0, 1,  0, 0, 0, 1,
49         };
50
51         for (i = 0; i < 32; i++) {
52                 if (reg_mask[i]) {
53                         miiphy_read (devname, phyAddr, i, &phyStatus);
54                         printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
55                 }
56         }
57 }
58 #endif
59
60 /********************************************************************/
61 static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec)
62 {
63         int ix;
64
65         /*
66          * Receive BDs init
67          */
68         for (ix = 0; ix < FEC_RBD_NUM; ix++) {
69                 fec->bdBase->rbd[ix].dataPointer =
70                                 (u32)&fec->bdBase->recv_frames[ix];
71                 fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
72                 fec->bdBase->rbd[ix].dataLength = 0;
73         }
74
75         /*
76          * have the last RBD to close the ring
77          */
78         fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
79         fec->rbdIndex = 0;
80
81         /*
82          * Trasmit BDs init
83          */
84         for (ix = 0; ix < FEC_TBD_NUM; ix++) {
85                 fec->bdBase->tbd[ix].status = 0;
86         }
87
88         /*
89          * Have the last TBD to close the ring
90          */
91         fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
92
93         /*
94          * Initialize some indices
95          */
96         fec->tbdIndex = 0;
97         fec->usedTbdIndex = 0;
98         fec->cleanTbdNum = FEC_TBD_NUM;
99
100         return 0;
101 }
102
103 /********************************************************************/
104 static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
105 {
106         /*
107          * Reset buffer descriptor as empty
108          */
109         if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
110                 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
111         else
112                 pRbd->status = FEC_RBD_EMPTY;
113
114         pRbd->dataLength = 0;
115
116         /*
117          * Increment BD count
118          */
119         fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
120
121         /*
122          * Now, we have an empty RxBD, notify FEC
123          * Set Descriptor polling active
124          */
125         out_be32(&fec->eth->r_des_active, 0x01000000);
126 }
127
128 /********************************************************************/
129 static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
130 {
131         volatile FEC_TBD *pUsedTbd;
132
133 #if (DEBUG & 0x1)
134         printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
135                 fec->cleanTbdNum, fec->usedTbdIndex);
136 #endif
137
138         /*
139          * process all the consumed TBDs
140          */
141         while (fec->cleanTbdNum < FEC_TBD_NUM) {
142                 pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
143                 if (pUsedTbd->status & FEC_TBD_READY) {
144 #if (DEBUG & 0x20)
145                         printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
146 #endif
147                         return;
148                 }
149
150                 /*
151                  * clean this buffer descriptor
152                  */
153                 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
154                         pUsedTbd->status = FEC_TBD_WRAP;
155                 else
156                         pUsedTbd->status = 0;
157
158                 /*
159                  * update some indeces for a correct handling of the TBD ring
160                  */
161                 fec->cleanTbdNum++;
162                 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
163         }
164 }
165
166 /********************************************************************/
167 static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
168 {
169         u8 currByte;                    /* byte for which to compute the CRC */
170         int byte;                       /* loop - counter */
171         int bit;                        /* loop - counter */
172         u32 crc = 0xffffffff;           /* initial value */
173
174         /*
175          * The algorithm used is the following:
176          * we loop on each of the six bytes of the provided address,
177          * and we compute the CRC by left-shifting the previous
178          * value by one position, so that each bit in the current
179          * byte of the address may contribute the calculation. If
180          * the latter and the MSB in the CRC are different, then
181          * the CRC value so computed is also ex-ored with the
182          * "polynomium generator". The current byte of the address
183          * is also shifted right by one bit at each iteration.
184          * This is because the CRC generatore in hardware is implemented
185          * as a shift-register with as many ex-ores as the radixes
186          * in the polynomium. This suggests that we represent the
187          * polynomiumm itself as a 32-bit constant.
188          */
189         for (byte = 0; byte < 6; byte++) {
190                 currByte = mac[byte];
191                 for (bit = 0; bit < 8; bit++) {
192                         if ((currByte & 0x01) ^ (crc & 0x01)) {
193                                 crc >>= 1;
194                                 crc = crc ^ 0xedb88320;
195                         } else {
196                                 crc >>= 1;
197                         }
198                         currByte >>= 1;
199                 }
200         }
201
202         crc = crc >> 26;
203
204         /*
205          * Set individual hash table register
206          */
207         if (crc >= 32) {
208                 out_be32(&fec->eth->iaddr1, (1 << (crc - 32)));
209                 out_be32(&fec->eth->iaddr2, 0);
210         } else {
211                 out_be32(&fec->eth->iaddr1, 0);
212                 out_be32(&fec->eth->iaddr2, (1 << crc));
213         }
214
215         /*
216          * Set physical address
217          */
218         out_be32(&fec->eth->paddr1, (mac[0] << 24) + (mac[1] << 16) +
219                                     (mac[2] <<  8) + mac[3]);
220         out_be32(&fec->eth->paddr2, (mac[4] << 24) + (mac[5] << 16) +
221                                      0x8808);
222 }
223
224 /********************************************************************/
225 static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
226 {
227         mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
228
229 #if (DEBUG & 0x1)
230         printf ("mpc512x_fec_init... Begin\n");
231 #endif
232
233         /* Set interrupt mask register */
234         out_be32(&fec->eth->imask, 0x00000000);
235
236         /* Clear FEC-Lite interrupt event register(IEVENT) */
237         out_be32(&fec->eth->ievent, 0xffffffff);
238
239         /* Set transmit fifo watermark register(X_WMRK), default = 64 */
240         out_be32(&fec->eth->x_wmrk, 0x0);
241
242         /* Set Opcode/Pause Duration Register */
243         out_be32(&fec->eth->op_pause, 0x00010020);
244
245         /* Frame length=1522; MII mode */
246         out_be32(&fec->eth->r_cntrl, (FEC_MAX_FRAME_LEN << 16) | 0x24);
247
248         /* Half-duplex, heartbeat disabled */
249         out_be32(&fec->eth->x_cntrl, 0x00000000);
250
251         /* Enable MIB counters */
252         out_be32(&fec->eth->mib_control, 0x0);
253
254         /* Setup recv fifo start and buff size */
255         out_be32(&fec->eth->r_fstart, 0x500);
256         out_be32(&fec->eth->r_buff_size, FEC_BUFFER_SIZE);
257
258         /* Setup BD base addresses */
259         out_be32(&fec->eth->r_des_start, (u32)fec->bdBase->rbd);
260         out_be32(&fec->eth->x_des_start, (u32)fec->bdBase->tbd);
261
262         /* DMA Control */
263         out_be32(&fec->eth->dma_control, 0xc0000000);
264
265         /* Enable FEC */
266         setbits_be32(&fec->eth->ecntrl, 0x00000006);
267
268         /* Initilize addresses and status words of BDs */
269         mpc512x_fec_bd_init (fec);
270
271          /* Descriptor polling active */
272         out_be32(&fec->eth->r_des_active, 0x01000000);
273
274 #if (DEBUG & 0x1)
275         printf("mpc512x_fec_init... Done \n");
276 #endif
277         return 1;
278 }
279
280 /********************************************************************/
281 int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
282 {
283         mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
284         const u8 phyAddr = CONFIG_PHY_ADDR;     /* Only one PHY */
285         int timeout = 1;
286         u16 phyStatus;
287
288 #if (DEBUG & 0x1)
289         printf ("mpc512x_fec_init_phy... Begin\n");
290 #endif
291
292         /*
293          * Clear FEC-Lite interrupt event register(IEVENT)
294          */
295         out_be32(&fec->eth->ievent, 0xffffffff);
296
297         /*
298          * Set interrupt mask register
299          */
300         out_be32(&fec->eth->imask, 0x00000000);
301
302         if (fec->xcv_type != SEVENWIRE) {
303                 /*
304                  * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
305                  * and do not drop the Preamble.
306                  */
307                 out_be32(&fec->eth->mii_speed,
308                          (((gd->ips_clk / 1000000) / 5) + 1) << 1);
309
310                 /*
311                  * Reset PHY, then delay 300ns
312                  */
313                 miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
314                 udelay (1000);
315
316                 if (fec->xcv_type == MII10) {
317                 /*
318                  * Force 10Base-T, FDX operation
319                  */
320 #if (DEBUG & 0x2)
321                         printf ("Forcing 10 Mbps ethernet link... ");
322 #endif
323                         miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
324
325                         miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
326
327                         timeout = 20;
328                         do {    /* wait for link status to go down */
329                                 udelay (10000);
330                                 if ((timeout--) == 0) {
331 #if (DEBUG & 0x2)
332                                         printf ("hmmm, should not have waited...");
333 #endif
334                                         break;
335                                 }
336                                 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
337 #if (DEBUG & 0x2)
338                                 printf ("=");
339 #endif
340                         } while ((phyStatus & 0x0004)); /* !link up */
341
342                         timeout = 1000;
343                         do {    /* wait for link status to come back up */
344                                 udelay (10000);
345                                 if ((timeout--) == 0) {
346                                         printf ("failed. Link is down.\n");
347                                         break;
348                                 }
349                                 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
350 #if (DEBUG & 0x2)
351                                 printf ("+");
352 #endif
353                         } while (!(phyStatus & 0x0004)); /* !link up */
354
355 #if (DEBUG & 0x2)
356                         printf ("done.\n");
357 #endif
358                 } else {        /* MII100 */
359                         /*
360                          * Set the auto-negotiation advertisement register bits
361                          */
362                         miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
363
364                         /*
365                          * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
366                          */
367                         miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
368
369                         /*
370                          * Wait for AN completion
371                          */
372                         timeout = 2500;
373                         do {
374                                 udelay (1000);
375
376                                 if ((timeout--) == 0) {
377 #if (DEBUG & 0x2)
378                                         printf ("PHY auto neg 0 failed...\n");
379 #endif
380                                         return -1;
381                                 }
382
383                                 if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
384 #if (DEBUG & 0x2)
385                                         printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
386 #endif
387                                         return -1;
388                                 }
389                         } while (!(phyStatus & 0x0004));
390
391 #if (DEBUG & 0x2)
392                         printf ("PHY auto neg complete! \n");
393 #endif
394                 }
395         }
396
397 #if (DEBUG & 0x2)
398         if (fec->xcv_type != SEVENWIRE)
399                 mpc512x_fec_phydump (dev->name);
400 #endif
401
402 #if (DEBUG & 0x1)
403         printf ("mpc512x_fec_init_phy... Done \n");
404 #endif
405         return 1;
406 }
407
408 /********************************************************************/
409 static void mpc512x_fec_halt (struct eth_device *dev)
410 {
411         mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
412         int counter = 0xffff;
413
414 #if (DEBUG & 0x2)
415         if (fec->xcv_type != SEVENWIRE)
416                 mpc512x_fec_phydump (dev->name);
417 #endif
418
419         /*
420          * mask FEC chip interrupts
421          */
422         out_be32(&fec->eth->imask, 0);
423
424         /*
425          * issue graceful stop command to the FEC transmitter if necessary
426          */
427         setbits_be32(&fec->eth->x_cntrl, 0x00000001);
428
429         /*
430          * wait for graceful stop to register
431          */
432         while ((counter--) && (!(in_be32(&fec->eth->ievent) & 0x10000000)))
433                 ;
434
435         /*
436          * Disable the Ethernet Controller
437          */
438         clrbits_be32(&fec->eth->ecntrl, 0x00000002);
439
440         /*
441          * Issue a reset command to the FEC chip
442          */
443         setbits_be32(&fec->eth->ecntrl, 0x1);
444
445         /*
446          * wait at least 16 clock cycles
447          */
448         udelay (10);
449 #if (DEBUG & 0x3)
450         printf ("Ethernet task stopped\n");
451 #endif
452 }
453
454 /********************************************************************/
455
456 static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
457                 int data_length)
458 {
459         /*
460          * This routine transmits one frame.  This routine only accepts
461          * 6-byte Ethernet addresses.
462          */
463         mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
464         volatile FEC_TBD *pTbd;
465
466 #if (DEBUG & 0x20)
467         printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
468 #endif
469
470         /*
471          * Clear Tx BD ring at first
472          */
473         mpc512x_fec_tbd_scrub (fec);
474
475         /*
476          * Check for valid length of data.
477          */
478         if ((data_length > 1500) || (data_length <= 0)) {
479                 return -1;
480         }
481
482         /*
483          * Check the number of vacant TxBDs.
484          */
485         if (fec->cleanTbdNum < 1) {
486 #if (DEBUG & 0x20)
487                 printf ("No available TxBDs ...\n");
488 #endif
489                 return -1;
490         }
491
492         /*
493          * Get the first TxBD to send the mac header
494          */
495         pTbd = &fec->bdBase->tbd[fec->tbdIndex];
496         pTbd->dataLength = data_length;
497         pTbd->dataPointer = (u32)eth_data;
498         pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
499         fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
500
501         /* Activate transmit Buffer Descriptor polling */
502         out_be32(&fec->eth->x_des_active, 0x01000000);
503
504 #if (DEBUG & 0x8)
505         printf ( "+" );
506 #endif
507
508         fec->cleanTbdNum -= 1;
509
510         /*
511          * wait until frame is sent .
512          */
513         while (pTbd->status & FEC_TBD_READY) {
514                 udelay (10);
515 #if (DEBUG & 0x8)
516                 printf ("TDB status = %04x\n", pTbd->status);
517 #endif
518         }
519
520         return 0;
521 }
522
523
524 /********************************************************************/
525 static int mpc512x_fec_recv (struct eth_device *dev)
526 {
527         /*
528          * This command pulls one frame from the card
529          */
530         mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
531         volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex];
532         unsigned long ievent;
533         int frame_length = 0;
534
535 #if (DEBUG & 0x1)
536         printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
537 #endif
538 #if (DEBUG & 0x8)
539         printf( "-" );
540 #endif
541
542         /*
543          * Check if any critical events have happened
544          */
545         ievent = in_be32(&fec->eth->ievent);
546         out_be32(&fec->eth->ievent, ievent);
547         if (ievent & 0x20060000) {
548                 /* BABT, Rx/Tx FIFO errors */
549                 mpc512x_fec_halt (dev);
550                 mpc512x_fec_init (dev, NULL);
551                 return 0;
552         }
553         if (ievent & 0x80000000) {
554                 /* Heartbeat error */
555                 setbits_be32(&fec->eth->x_cntrl, 0x00000001);
556         }
557         if (ievent & 0x10000000) {
558                 /* Graceful stop complete */
559                 if (in_be32(&fec->eth->x_cntrl) & 0x00000001) {
560                         mpc512x_fec_halt (dev);
561                         clrbits_be32(&fec->eth->x_cntrl, 0x00000001);;
562                         mpc512x_fec_init (dev, NULL);
563                 }
564         }
565
566         if (!(pRbd->status & FEC_RBD_EMPTY)) {
567                 if (!(pRbd->status & FEC_RBD_ERR) &&
568                         ((pRbd->dataLength - 4) > 14)) {
569
570                         /*
571                          * Get buffer size
572                          */
573                         if (pRbd->status & FEC_RBD_LAST)
574                                 frame_length = pRbd->dataLength - 4;
575                         else
576                                 frame_length = pRbd->dataLength;
577 #if (DEBUG & 0x20)
578                         {
579                                 int i;
580                                 printf ("recv data length 0x%08x data hdr: ",
581                                         pRbd->dataLength);
582                                 for (i = 0; i < 14; i++)
583                                         printf ("%x ", *((u8*)pRbd->dataPointer + i));
584                                 printf("\n");
585                         }
586 #endif
587                         /*
588                          *  Fill the buffer and pass it to upper layers
589                          */
590                         memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer,
591                                 frame_length - rx_buff_idx);
592                         rx_buff_idx = frame_length;
593
594                         if (pRbd->status & FEC_RBD_LAST) {
595                                 NetReceive ((uchar*)rx_buff, frame_length);
596                                 rx_buff_idx = 0;
597                         }
598                 }
599
600                 /*
601                  * Reset buffer descriptor as empty
602                  */
603                 mpc512x_fec_rbd_clean (fec, pRbd);
604         }
605
606         /* Try to fill Buffer Descriptors */
607         out_be32(&fec->eth->r_des_active, 0x01000000);
608
609         return frame_length;
610 }
611
612 /********************************************************************/
613 int mpc512x_fec_initialize (bd_t * bis)
614 {
615         volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
616         mpc512x_fec_priv *fec;
617         struct eth_device *dev;
618         int i;
619         char *tmp, *end, env_enetaddr[6];
620         void * bd;
621
622         fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
623         dev = (struct eth_device *) malloc (sizeof(*dev));
624         memset (dev, 0, sizeof *dev);
625
626         fec->eth = &im->fec;
627
628 # ifndef CONFIG_FEC_10MBIT
629         fec->xcv_type = MII100;
630 # else
631         fec->xcv_type = MII10;
632 # endif
633         dev->priv = (void *)fec;
634         dev->iobase = (int)&im->fec;
635         dev->init = mpc512x_fec_init;
636         dev->halt = mpc512x_fec_halt;
637         dev->send = mpc512x_fec_send;
638         dev->recv = mpc512x_fec_recv;
639
640         sprintf (dev->name, "FEC ETHERNET");
641         eth_register (dev);
642
643 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
644         miiphy_register (dev->name,
645                         fec512x_miiphy_read, fec512x_miiphy_write);
646 #endif
647
648         /* Clean up space FEC's MIB and FIFO RAM ...*/
649         memset ((void *)&im->fec.mib,  0x00, sizeof(im->fec.mib));
650         memset ((void *)&im->fec.fifo, 0x00, sizeof(im->fec.fifo));
651
652         /*
653          * Malloc space for BDs  (must be quad word-aligned)
654          * this pointer is lost, so cannot be freed
655          */
656         bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f);
657         fec->bdBase = (mpc512x_buff_descs*)((u32)bd & 0xfffffff0);
658         memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f);
659
660         /*
661          * Set interrupt mask register
662          */
663         out_be32(&fec->eth->imask, 0x00000000);
664
665         /*
666          * Clear FEC-Lite interrupt event register(IEVENT)
667          */
668         out_be32(&fec->eth->ievent, 0xffffffff);
669
670         /*
671          * Try to set the mac address now. The fec mac address is
672          * a garbage after reset. When not using fec for booting
673          * the Linux fec driver will try to work with this garbage.
674          */
675         tmp = getenv ("ethaddr");
676         if (tmp) {
677                 for (i=0; i<6; i++) {
678                         env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
679                         if (tmp)
680                                 tmp = (*end) ? end+1 : end;
681                 }
682                 mpc512x_fec_set_hwaddr (fec, env_enetaddr);
683                 out_be32(&fec->eth->gaddr1, 0x00000000);
684                 out_be32(&fec->eth->gaddr2, 0x00000000);
685         }
686
687         mpc512x_fec_init_phy (dev, bis);
688
689         return 1;
690 }
691
692 /* MII-interface related functions */
693 /********************************************************************/
694 int fec512x_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal)
695 {
696         volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
697         volatile fec512x_t *eth = &im->fec;
698         u32 reg;                /* convenient holder for the PHY register */
699         u32 phy;                /* convenient holder for the PHY */
700         int timeout = 0xffff;
701
702         /*
703          * reading from any PHY's register is done by properly
704          * programming the FEC's MII data register.
705          */
706         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
707         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
708
709         out_be32(&eth->mii_data, FEC_MII_DATA_ST |
710                                  FEC_MII_DATA_OP_RD |
711                                  FEC_MII_DATA_TA |
712                                  phy | reg);
713
714         /*
715          * wait for the related interrupt
716          */
717         while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
718                 ;
719
720         if (timeout == 0) {
721 #if (DEBUG & 0x2)
722                 printf ("Read MDIO failed...\n");
723 #endif
724                 return -1;
725         }
726
727         /*
728          * clear mii interrupt bit
729          */
730         out_be32(&eth->ievent, 0x00800000);
731
732         /*
733          * it's now safe to read the PHY's register
734          */
735         *retVal = (u16) in_be32(&eth->mii_data);
736
737         return 0;
738 }
739
740 /********************************************************************/
741 int fec512x_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data)
742 {
743         volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
744         volatile fec512x_t *eth = &im->fec;
745         u32 reg;                /* convenient holder for the PHY register */
746         u32 phy;                /* convenient holder for the PHY */
747         int timeout = 0xffff;
748
749         reg = regAddr << FEC_MII_DATA_RA_SHIFT;
750         phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
751
752         out_be32(&eth->mii_data, FEC_MII_DATA_ST |
753                                  FEC_MII_DATA_OP_WR |
754                                  FEC_MII_DATA_TA |
755                                  phy | reg | data);
756
757         /*
758          * wait for the MII interrupt
759          */
760         while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
761                 ;
762
763         if (timeout == 0) {
764 #if (DEBUG & 0x2)
765                 printf ("Write MDIO failed...\n");
766 #endif
767                 return -1;
768         }
769
770         /*
771          * clear MII interrupt bit
772          */
773         out_be32(&eth->ievent, 0x00800000);
774
775         return 0;
776 }
777
778 #if (DEBUG & 0x40)
779 static u32 local_crc32 (char *string, unsigned int crc_value, int len)
780 {
781         int i;
782         char c;
783         unsigned int crc, count;
784
785         /*
786          * crc32 algorithm
787          */
788         /*
789          * crc = 0xffffffff; * The initialized value should be 0xffffffff
790          */
791         crc = crc_value;
792
793         for (i = len; --i >= 0;) {
794                 c = *string++;
795                 for (count = 0; count < 8; count++) {
796                         if ((c & 0x01) ^ (crc & 0x01)) {
797                                 crc >>= 1;
798                                 crc = crc ^ 0xedb88320;
799                         } else {
800                                 crc >>= 1;
801                         }
802                         c >>= 1;
803                 }
804         }
805
806         /*
807          * In big endian system, do byte swaping for crc value
808          */
809          /**/ return crc;
810 }
811 #endif  /* DEBUG */
812
813 #endif /* CONFIG_MPC512x_FEC */