2 * (C) Copyright 2003-2005
5 * This file is based on mpc4200fec.c,
6 * (C) Copyright Motorola, Inc., 2000
11 #include <mpc5xxx_sdma.h>
16 #include "mpc5xxx_fec.h"
18 DECLARE_GLOBAL_DATA_PTR;
20 /* #define DEBUG 0x28 */
22 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
23 defined(CONFIG_MPC5xxx_FEC)
25 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
26 #error "CONFIG_MII has to be defined!"
30 static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec);
31 static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec);
35 static uint32 local_crc32(char *string, unsigned int crc_value, int len);
39 uint8 data[1500]; /* actual data */
40 int length; /* actual length */
41 int used; /* buffer in use or not */
42 uint8 head[16]; /* MAC header(6 + 6 + 2) + 2(aligned) */
45 int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal);
46 int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data);
48 /********************************************************************/
50 static void mpc5xxx_fec_phydump (char *devname)
53 uint8 phyAddr = CONFIG_PHY_ADDR;
55 #if CONFIG_PHY_TYPE == 0x79c874 /* AMD Am79C874 */
56 /* regs to print: 0...7, 16...19, 21, 23, 24 */
57 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
58 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
60 /* regs to print: 0...8, 16...20 */
61 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
62 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 for (i = 0; i < 32; i++) {
68 miiphy_read(devname, phyAddr, i, &phyStatus);
69 printf("Mii reg %d: 0x%04x\n", i, phyStatus);
75 /********************************************************************/
76 static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
82 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
84 data = (char *)malloc(FEC_MAX_PKT_SIZE);
86 printf ("RBD INIT FAILED\n");
89 fec->rbdBase[ix].dataPointer = (uint32)data;
91 fec->rbdBase[ix].status = FEC_RBD_EMPTY;
92 fec->rbdBase[ix].dataLength = 0;
97 * have the last RBD to close the ring
99 fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP;
105 /********************************************************************/
106 static void mpc5xxx_fec_tbd_init(mpc5xxx_fec_priv *fec)
110 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
111 fec->tbdBase[ix].status = 0;
115 * Have the last TBD to close the ring
117 fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP;
120 * Initialize some indices
123 fec->usedTbdIndex = 0;
124 fec->cleanTbdNum = FEC_TBD_NUM;
127 /********************************************************************/
128 static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, volatile FEC_RBD * pRbd)
131 * Reset buffer descriptor as empty
133 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
134 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
136 pRbd->status = FEC_RBD_EMPTY;
138 pRbd->dataLength = 0;
141 * Now, we have an empty RxBD, restart the SmartDMA receive task
143 SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
148 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
151 /********************************************************************/
152 static void mpc5xxx_fec_tbd_scrub(mpc5xxx_fec_priv *fec)
154 volatile FEC_TBD *pUsedTbd;
157 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
158 fec->cleanTbdNum, fec->usedTbdIndex);
162 * process all the consumed TBDs
164 while (fec->cleanTbdNum < FEC_TBD_NUM) {
165 pUsedTbd = &fec->tbdBase[fec->usedTbdIndex];
166 if (pUsedTbd->status & FEC_TBD_READY) {
168 printf("Cannot clean TBD %d, in use\n", fec->cleanTbdNum);
174 * clean this buffer descriptor
176 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
177 pUsedTbd->status = FEC_TBD_WRAP;
179 pUsedTbd->status = 0;
182 * update some indeces for a correct handling of the TBD ring
185 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
189 /********************************************************************/
190 static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac)
192 uint8 currByte; /* byte for which to compute the CRC */
193 int byte; /* loop - counter */
194 int bit; /* loop - counter */
195 uint32 crc = 0xffffffff; /* initial value */
198 * The algorithm used is the following:
199 * we loop on each of the six bytes of the provided address,
200 * and we compute the CRC by left-shifting the previous
201 * value by one position, so that each bit in the current
202 * byte of the address may contribute the calculation. If
203 * the latter and the MSB in the CRC are different, then
204 * the CRC value so computed is also ex-ored with the
205 * "polynomium generator". The current byte of the address
206 * is also shifted right by one bit at each iteration.
207 * This is because the CRC generatore in hardware is implemented
208 * as a shift-register with as many ex-ores as the radixes
209 * in the polynomium. This suggests that we represent the
210 * polynomiumm itself as a 32-bit constant.
212 for (byte = 0; byte < 6; byte++) {
213 currByte = mac[byte];
214 for (bit = 0; bit < 8; bit++) {
215 if ((currByte & 0x01) ^ (crc & 0x01)) {
217 crc = crc ^ 0xedb88320;
228 * Set individual hash table register
231 fec->eth->iaddr1 = (1 << (crc - 32));
232 fec->eth->iaddr2 = 0;
234 fec->eth->iaddr1 = 0;
235 fec->eth->iaddr2 = (1 << crc);
239 * Set physical address
241 fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
242 fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
245 /********************************************************************/
246 static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
248 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
249 struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
252 printf ("mpc5xxx_fec_init... Begin\n");
256 * Initialize RxBD/TxBD rings
258 mpc5xxx_fec_rbd_init(fec);
259 mpc5xxx_fec_tbd_init(fec);
262 * Clear FEC-Lite interrupt event register(IEVENT)
264 fec->eth->ievent = 0xffffffff;
267 * Set interrupt mask register
269 fec->eth->imask = 0x00000000;
272 * Set FEC-Lite receive control register(R_CNTRL):
274 if (fec->xcv_type == SEVENWIRE) {
276 * Frame length=1518; 7-wire mode
278 fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
281 * Frame length=1518; MII mode;
283 fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
286 fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
287 if (fec->xcv_type != SEVENWIRE) {
289 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
290 * and do not drop the Preamble.
292 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
296 * Set Opcode/Pause Duration Register
298 fec->eth->op_pause = 0x00010020; /*FIXME 0xffff0020; */
301 * Set Rx FIFO alarm and granularity value
303 fec->eth->rfifo_cntrl = 0x0c000000
304 | (fec->eth->rfifo_cntrl & ~0x0f000000);
305 fec->eth->rfifo_alarm = 0x0000030c;
307 if (fec->eth->rfifo_status & 0x00700000 ) {
308 printf("mpc5xxx_fec_init() RFIFO error\n");
313 * Set Tx FIFO granularity value
315 fec->eth->tfifo_cntrl = 0x0c000000
316 | (fec->eth->tfifo_cntrl & ~0x0f000000);
318 printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
319 printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
323 * Set transmit fifo watermark register(X_WMRK), default = 64
325 fec->eth->tfifo_alarm = 0x00000080;
326 fec->eth->x_wmrk = 0x2;
329 * Set individual address filter for unicast address
330 * and set physical address registers.
332 mpc5xxx_fec_set_hwaddr(fec, (char *)dev->enetaddr);
335 * Set multicast address filter
337 fec->eth->gaddr1 = 0x00000000;
338 fec->eth->gaddr2 = 0x00000000;
341 * Turn ON cheater FSM: ????
343 fec->eth->xmit_fsm = 0x03000000;
345 #if defined(CONFIG_MPC5200)
347 * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
348 * work w/ the current receive task.
350 sdma->PtdCntrl |= 0x00000001;
354 * Set priority of different initiators
356 sdma->IPR0 = 7; /* always */
357 sdma->IPR3 = 6; /* Eth RX */
358 sdma->IPR4 = 5; /* Eth Tx */
361 * Clear SmartDMA task interrupt pending bits
363 SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
366 * Initialize SmartDMA parameters stored in SRAM
368 *(volatile int *)FEC_TBD_BASE = (int)fec->tbdBase;
369 *(volatile int *)FEC_RBD_BASE = (int)fec->rbdBase;
370 *(volatile int *)FEC_TBD_NEXT = (int)fec->tbdBase;
371 *(volatile int *)FEC_RBD_NEXT = (int)fec->rbdBase;
374 * Enable FEC-Lite controller
376 fec->eth->ecntrl |= 0x00000006;
379 if (fec->xcv_type != SEVENWIRE)
380 mpc5xxx_fec_phydump (dev->name);
384 * Enable SmartDMA receive task
386 SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
389 printf("mpc5xxx_fec_init... Done \n");
395 /********************************************************************/
396 static int mpc5xxx_fec_init_phy(struct eth_device *dev, bd_t * bis)
398 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
399 const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
402 printf ("mpc5xxx_fec_init_phy... Begin\n");
406 * Initialize GPIO pins
408 if (fec->xcv_type == SEVENWIRE) {
409 /* 10MBit with 7-wire operation */
410 #if defined(CONFIG_TOTAL5200)
411 /* 7-wire and USB2 on Ethernet */
412 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00030000;
413 #else /* !CONFIG_TOTAL5200 */
415 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
416 #endif /* CONFIG_TOTAL5200 */
418 /* 100MBit with MD operation */
419 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
423 * Clear FEC-Lite interrupt event register(IEVENT)
425 fec->eth->ievent = 0xffffffff;
428 * Set interrupt mask register
430 fec->eth->imask = 0x00000000;
433 * In original Promess-provided code PHY initialization is disabled with the
434 * following comment: "Phy initialization is DISABLED for now. There was a
435 * problem with running 100 Mbps on PRO board". Thus we temporarily disable
436 * PHY initialization for the Motion-PRO board, until a proper fix is found.
439 if (fec->xcv_type != SEVENWIRE) {
441 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
442 * and do not drop the Preamble.
444 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
447 if (fec->xcv_type != SEVENWIRE) {
449 * Initialize PHY(LXT971A):
451 * Generally, on power up, the LXT971A reads its configuration
452 * pins to check for forced operation, If not cofigured for
453 * forced operation, it uses auto-negotiation/parallel detection
454 * to automatically determine line operating conditions.
455 * If the PHY device on the other side of the link supports
456 * auto-negotiation, the LXT971A auto-negotiates with it
457 * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
458 * support auto-negotiation, the LXT971A automatically detects
459 * the presence of either link pulses(10Mbps PHY) or Idle
460 * symbols(100Mbps) and sets its operating conditions accordingly.
462 * When auto-negotiation is controlled by software, the following
463 * steps are recommended.
466 * The physical address is dependent on hardware configuration.
473 * Reset PHY, then delay 300ns
475 miiphy_write(dev->name, phyAddr, 0x0, 0x8000);
478 #if defined(CONFIG_UC101) || defined(CONFIG_MUCMC52)
479 /* Set the LED configuration Register for the UC101
481 miiphy_write(dev->name, phyAddr, 0x14, 0x4122);
483 if (fec->xcv_type == MII10) {
485 * Force 10Base-T, FDX operation
488 printf("Forcing 10 Mbps ethernet link... ");
490 miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
492 miiphy_write(dev->name, fec, phyAddr, 0x0, 0x0100);
494 miiphy_write(dev->name, phyAddr, 0x0, 0x0180);
497 do { /* wait for link status to go down */
499 if ((timeout--) == 0) {
501 printf("hmmm, should not have waited...");
505 miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
509 } while ((phyStatus & 0x0004)); /* !link up */
512 do { /* wait for link status to come back up */
514 if ((timeout--) == 0) {
515 printf("failed. Link is down.\n");
518 miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
522 } while (!(phyStatus & 0x0004)); /* !link up */
527 } else { /* MII100 */
529 * Set the auto-negotiation advertisement register bits
531 miiphy_write(dev->name, phyAddr, 0x4, 0x01e1);
534 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
536 miiphy_write(dev->name, phyAddr, 0x0, 0x1200);
539 * Wait for AN completion
545 if ((timeout--) == 0) {
547 printf("PHY auto neg 0 failed...\n");
552 if (miiphy_read(dev->name, phyAddr, 0x1, &phyStatus) != 0) {
554 printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
558 } while (!(phyStatus & 0x0004));
561 printf("PHY auto neg complete! \n");
568 if (fec->xcv_type != SEVENWIRE)
569 mpc5xxx_fec_phydump (dev->name);
574 printf("mpc5xxx_fec_init_phy... Done \n");
580 /********************************************************************/
581 static void mpc5xxx_fec_halt(struct eth_device *dev)
583 #if defined(CONFIG_MPC5200)
584 struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
586 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
587 int counter = 0xffff;
590 if (fec->xcv_type != SEVENWIRE)
591 mpc5xxx_fec_phydump (dev->name);
595 * mask FEC chip interrupts
600 * issue graceful stop command to the FEC transmitter if necessary
602 fec->eth->x_cntrl |= 0x00000001;
605 * wait for graceful stop to register
607 while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
610 * Disable SmartDMA tasks
612 SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
613 SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
615 #if defined(CONFIG_MPC5200)
617 * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
618 * done. It doesn't work w/ the current receive task.
620 sdma->PtdCntrl &= ~0x00000001;
624 * Disable the Ethernet Controller
626 fec->eth->ecntrl &= 0xfffffffd;
629 * Clear FIFO status registers
631 fec->eth->rfifo_status &= 0x00700000;
632 fec->eth->tfifo_status &= 0x00700000;
634 fec->eth->reset_cntrl = 0x01000000;
637 * Issue a reset command to the FEC chip
639 fec->eth->ecntrl |= 0x1;
642 * wait at least 16 clock cycles
647 printf("Ethernet task stopped\n");
652 /********************************************************************/
654 static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec)
656 uint16 phyAddr = CONFIG_PHY_ADDR;
659 if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
660 || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
662 miiphy_read(devname, phyAddr, 0x1, &phyStatus);
663 printf("\nphyStatus: 0x%04x\n", phyStatus);
664 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
665 printf("ievent: 0x%08x\n", fec->eth->ievent);
666 printf("x_status: 0x%08x\n", fec->eth->x_status);
667 printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
669 printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
670 printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
671 printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
672 printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
673 printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
674 printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
678 static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec)
680 uint16 phyAddr = CONFIG_PHY_ADDR;
683 if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
684 || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
686 miiphy_read(devname, phyAddr, 0x1, &phyStatus);
687 printf("\nphyStatus: 0x%04x\n", phyStatus);
688 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
689 printf("ievent: 0x%08x\n", fec->eth->ievent);
690 printf("x_status: 0x%08x\n", fec->eth->x_status);
691 printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
693 printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
694 printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
695 printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
696 printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
697 printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
698 printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
703 /********************************************************************/
705 static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
709 * This routine transmits one frame. This routine only accepts
710 * 6-byte Ethernet addresses.
712 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
713 volatile FEC_TBD *pTbd;
716 printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
717 tfifo_print(dev->name, fec);
721 * Clear Tx BD ring at first
723 mpc5xxx_fec_tbd_scrub(fec);
726 * Check for valid length of data.
728 if ((data_length > 1500) || (data_length <= 0)) {
733 * Check the number of vacant TxBDs.
735 if (fec->cleanTbdNum < 1) {
737 printf("No available TxBDs ...\n");
743 * Get the first TxBD to send the mac header
745 pTbd = &fec->tbdBase[fec->tbdIndex];
746 pTbd->dataLength = data_length;
747 pTbd->dataPointer = (uint32)eth_data;
748 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
749 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
752 printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
758 if (fec->xcv_type != SEVENWIRE) {
760 miiphy_read(dev->name, 0, 0x1, &phyStatus);
764 * Enable SmartDMA transmit task
768 tfifo_print(dev->name, fec);
770 SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
772 tfifo_print(dev->name, fec);
778 fec->cleanTbdNum -= 1;
780 #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
781 printf ("smartDMA ethernet Tx task enabled\n");
784 * wait until frame is sent .
786 while (pTbd->status & FEC_TBD_READY) {
789 printf ("TDB status = %04x\n", pTbd->status);
797 /********************************************************************/
798 static int mpc5xxx_fec_recv(struct eth_device *dev)
801 * This command pulls one frame from the card
803 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
804 volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
805 unsigned long ievent;
806 int frame_length, len = 0;
808 uchar buff[FEC_MAX_PKT_SIZE];
811 printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
818 * Check if any critical events have happened
820 ievent = fec->eth->ievent;
821 fec->eth->ievent = ievent;
822 if (ievent & 0x20060000) {
823 /* BABT, Rx/Tx FIFO errors */
824 mpc5xxx_fec_halt(dev);
825 mpc5xxx_fec_init(dev, NULL);
828 if (ievent & 0x80000000) {
829 /* Heartbeat error */
830 fec->eth->x_cntrl |= 0x00000001;
832 if (ievent & 0x10000000) {
833 /* Graceful stop complete */
834 if (fec->eth->x_cntrl & 0x00000001) {
835 mpc5xxx_fec_halt(dev);
836 fec->eth->x_cntrl &= ~0x00000001;
837 mpc5xxx_fec_init(dev, NULL);
841 if (!(pRbd->status & FEC_RBD_EMPTY)) {
842 if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
843 ((pRbd->dataLength - 4) > 14)) {
846 * Get buffer address and size
848 frame = (NBUF *)pRbd->dataPointer;
849 frame_length = pRbd->dataLength - 4;
854 printf("recv data hdr:");
855 for (i = 0; i < 14; i++)
856 printf("%x ", *(frame->head + i));
861 * Fill the buffer and pass it to upper layers
863 memcpy(buff, frame->head, 14);
864 memcpy(buff + 14, frame->data, frame_length);
865 NetReceive(buff, frame_length);
869 * Reset buffer descriptor as empty
871 mpc5xxx_fec_rbd_clean(fec, pRbd);
873 SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
878 /********************************************************************/
879 int mpc5xxx_fec_initialize(bd_t * bis)
881 mpc5xxx_fec_priv *fec;
882 struct eth_device *dev;
884 char env_enetaddr[6];
887 fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
888 dev = (struct eth_device *)malloc(sizeof(*dev));
889 memset(dev, 0, sizeof *dev);
891 fec->eth = (ethernet_regs *)MPC5XXX_FEC;
892 fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
893 fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
894 #if defined(CONFIG_CANMB) || \
895 defined(CONFIG_CM5200) || \
896 defined(CONFIG_HMI1001) || \
897 defined(CONFIG_ICECUBE) || \
898 defined(CONFIG_INKA4X0) || \
899 defined(CONFIG_JUPITER) || \
900 defined(CONFIG_MCC200) || \
901 defined(CONFIG_MOTIONPRO) || \
902 defined(CONFIG_MUCMC52) || \
903 defined(CONFIG_O2DNT) || \
904 defined(CONFIG_PM520) || \
905 defined(CONFIG_TOP5200) || \
906 defined(CONFIG_TQM5200) || \
907 defined(CONFIG_UC101) || \
908 defined(CONFIG_V38B) || \
909 defined(CONFIG_MUNICES)
910 # ifndef CONFIG_FEC_10MBIT
911 fec->xcv_type = MII100;
913 fec->xcv_type = MII10;
915 #elif defined(CONFIG_TOTAL5200)
916 fec->xcv_type = SEVENWIRE;
918 #error fec->xcv_type not initialized.
921 dev->priv = (void *)fec;
922 dev->iobase = MPC5XXX_FEC;
923 dev->init = mpc5xxx_fec_init;
924 dev->halt = mpc5xxx_fec_halt;
925 dev->send = mpc5xxx_fec_send;
926 dev->recv = mpc5xxx_fec_recv;
928 sprintf(dev->name, "FEC ETHERNET");
931 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
932 miiphy_register (dev->name,
933 fec5xxx_miiphy_read, fec5xxx_miiphy_write);
937 * Try to set the mac address now. The fec mac address is
938 * a garbage after reset. When not using fec for booting
939 * the Linux fec driver will try to work with this garbage.
941 tmp = getenv("ethaddr");
943 for (i=0; i<6; i++) {
944 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
946 tmp = (*end) ? end+1 : end;
948 mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
951 mpc5xxx_fec_init_phy(dev, bis);
956 /* MII-interface related functions */
957 /********************************************************************/
958 int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
960 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
961 uint32 reg; /* convenient holder for the PHY register */
962 uint32 phy; /* convenient holder for the PHY */
963 int timeout = 0xffff;
966 * reading from any PHY's register is done by properly
967 * programming the FEC's MII data register.
969 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
970 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
972 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
975 * wait for the related interrupt
977 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
981 printf ("Read MDIO failed...\n");
987 * clear mii interrupt bit
989 eth->ievent = 0x00800000;
992 * it's now safe to read the PHY's register
994 *retVal = (uint16) eth->mii_data;
999 /********************************************************************/
1000 int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
1002 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
1003 uint32 reg; /* convenient holder for the PHY register */
1004 uint32 phy; /* convenient holder for the PHY */
1005 int timeout = 0xffff;
1007 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
1008 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
1010 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
1011 FEC_MII_DATA_TA | phy | reg | data);
1014 * wait for the MII interrupt
1016 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
1020 printf ("Write MDIO failed...\n");
1026 * clear MII interrupt bit
1028 eth->ievent = 0x00800000;
1034 static uint32 local_crc32(char *string, unsigned int crc_value, int len)
1038 unsigned int crc, count;
1044 * crc = 0xffffffff; * The initialized value should be 0xffffffff
1048 for (i = len; --i >= 0;) {
1050 for (count = 0; count < 8; count++) {
1051 if ((c & 0x01) ^ (crc & 0x01)) {
1053 crc = crc ^ 0xedb88320;
1062 * In big endian system, do byte swaping for crc value
1068 #endif /* CONFIG_MPC5xxx_FEC */