2 * (C) Copyright 2003-2007
5 * Derived from the MPC8xx FEC driver.
16 DECLARE_GLOBAL_DATA_PTR;
20 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
21 defined(CONFIG_MPC512x_FEC)
23 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
24 #error "CONFIG_MII has to be defined!"
28 static uint32 local_crc32(char *string, unsigned int crc_value, int len);
31 int fec512x_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal);
32 int fec512x_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data);
33 int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis);
35 /********************************************************************/
37 static void mpc512x_fec_phydump (char *devname)
40 uint8 phyAddr = CONFIG_PHY_ADDR;
42 /* regs to print: 0...8, 21,27,31 */
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
47 for (i = 0; i < 32; i++) {
49 miiphy_read (devname, phyAddr, i, &phyStatus);
50 printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
56 /********************************************************************/
57 static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec)
64 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
65 fec->bdBase->rbd[ix].dataPointer = (uint32)&fec->bdBase->recv_frames[ix];
66 fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
67 fec->bdBase->rbd[ix].dataLength = 0;
71 * have the last RBD to close the ring
73 fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
79 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
80 fec->bdBase->tbd[ix].status = 0;
84 * Have the last TBD to close the ring
86 fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
89 * Initialize some indices
92 fec->usedTbdIndex = 0;
93 fec->cleanTbdNum = FEC_TBD_NUM;
98 /********************************************************************/
99 static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
102 * Reset buffer descriptor as empty
104 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
105 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
107 pRbd->status = FEC_RBD_EMPTY;
109 pRbd->dataLength = 0;
114 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
117 * Now, we have an empty RxBD, notify FEC
119 fec->eth->r_des_active = 0x01000000; /* Descriptor polling active */
122 /********************************************************************/
123 static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
125 volatile FEC_TBD *pUsedTbd;
128 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
129 fec->cleanTbdNum, fec->usedTbdIndex);
133 * process all the consumed TBDs
135 while (fec->cleanTbdNum < FEC_TBD_NUM) {
136 pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
137 if (pUsedTbd->status & FEC_TBD_READY) {
139 printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
145 * clean this buffer descriptor
147 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
148 pUsedTbd->status = FEC_TBD_WRAP;
150 pUsedTbd->status = 0;
153 * update some indeces for a correct handling of the TBD ring
156 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
160 /********************************************************************/
161 static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
163 uint8 currByte; /* byte for which to compute the CRC */
164 int byte; /* loop - counter */
165 int bit; /* loop - counter */
166 uint32 crc = 0xffffffff; /* initial value */
169 * The algorithm used is the following:
170 * we loop on each of the six bytes of the provided address,
171 * and we compute the CRC by left-shifting the previous
172 * value by one position, so that each bit in the current
173 * byte of the address may contribute the calculation. If
174 * the latter and the MSB in the CRC are different, then
175 * the CRC value so computed is also ex-ored with the
176 * "polynomium generator". The current byte of the address
177 * is also shifted right by one bit at each iteration.
178 * This is because the CRC generatore in hardware is implemented
179 * as a shift-register with as many ex-ores as the radixes
180 * in the polynomium. This suggests that we represent the
181 * polynomiumm itself as a 32-bit constant.
183 for (byte = 0; byte < 6; byte++) {
184 currByte = mac[byte];
185 for (bit = 0; bit < 8; bit++) {
186 if ((currByte & 0x01) ^ (crc & 0x01)) {
188 crc = crc ^ 0xedb88320;
199 * Set individual hash table register
202 fec->eth->iaddr1 = (1 << (crc - 32));
203 fec->eth->iaddr2 = 0;
205 fec->eth->iaddr1 = 0;
206 fec->eth->iaddr2 = (1 << crc);
210 * Set physical address
212 fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
213 fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
216 /********************************************************************/
217 static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
219 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
222 printf ("mpc512x_fec_init... Begin\n");
225 /* Set interrupt mask register */
226 fec->eth->imask = 0x00000000;
228 /* Clear FEC-Lite interrupt event register(IEVENT) */
229 fec->eth->ievent = 0xffffffff;
231 /* Set transmit fifo watermark register(X_WMRK), default = 64 */
232 fec->eth->x_wmrk = 0x0;
234 /* Set Opcode/Pause Duration Register */
235 fec->eth->op_pause = 0x00010020;
237 /* Frame length=1518; MII mode */
238 fec->eth->r_cntrl = 0x05ee000c;
240 /* Half-duplex, heartbeat disabled */
241 fec->eth->x_cntrl = 0x00000000;
243 /* Enable MIB counters */
244 fec->eth->mib_control = 0x0;
246 /* Setup recv fifo start and buff size */
247 fec->eth->r_fstart = 0x500;
248 fec->eth->r_buff_size = 0x5e0;
250 /* Setup BD base addresses */
251 fec->eth->r_des_start = (uint32)fec->bdBase->rbd;
252 fec->eth->x_des_start = (uint32)fec->bdBase->tbd;
255 fec->eth->dma_control = 0xc0000000;
258 fec->eth->ecntrl |= 0x00000006;
260 /* Initilize addresses and status words of BDs */
261 mpc512x_fec_bd_init (fec);
263 /* Descriptor polling active */
264 fec->eth->r_des_active = 0x01000000;
267 printf("mpc512x_fec_init... Done \n");
272 /********************************************************************/
273 int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
275 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
276 const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
281 printf ("mpc512x_fec_init_phy... Begin\n");
285 * Clear FEC-Lite interrupt event register(IEVENT)
287 fec->eth->ievent = 0xffffffff;
290 * Set interrupt mask register
292 fec->eth->imask = 0x00000000;
294 if (fec->xcv_type != SEVENWIRE) {
296 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
297 * and do not drop the Preamble.
299 fec->eth->mii_speed = (((gd->ipb_clk / 1000000) / 5) + 1) << 1;
302 * Reset PHY, then delay 300ns
304 miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
307 if (fec->xcv_type == MII10) {
309 * Force 10Base-T, FDX operation
312 printf ("Forcing 10 Mbps ethernet link... ");
314 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
316 miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
319 do { /* wait for link status to go down */
321 if ((timeout--) == 0) {
323 printf ("hmmm, should not have waited...");
327 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
331 } while ((phyStatus & 0x0004)); /* !link up */
334 do { /* wait for link status to come back up */
336 if ((timeout--) == 0) {
337 printf ("failed. Link is down.\n");
340 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
344 } while (!(phyStatus & 0x0004)); /* !link up */
349 } else { /* MII100 */
351 * Set the auto-negotiation advertisement register bits
353 miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
356 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
358 miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
361 * Wait for AN completion
367 if ((timeout--) == 0) {
369 printf ("PHY auto neg 0 failed...\n");
374 if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
376 printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
380 } while (!(phyStatus & 0x0004));
383 printf ("PHY auto neg complete! \n");
389 if (fec->xcv_type != SEVENWIRE)
390 mpc512x_fec_phydump (dev->name);
394 printf ("mpc512x_fec_init_phy... Done \n");
399 /********************************************************************/
400 static void mpc512x_fec_halt (struct eth_device *dev)
402 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
403 int counter = 0xffff;
406 if (fec->xcv_type != SEVENWIRE)
407 mpc512x_fec_phydump (dev->name);
411 * mask FEC chip interrupts
416 * issue graceful stop command to the FEC transmitter if necessary
418 fec->eth->x_cntrl |= 0x00000001;
421 * wait for graceful stop to register
423 while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
426 * Disable the Ethernet Controller
428 fec->eth->ecntrl &= 0xfffffffd;
431 * Issue a reset command to the FEC chip
433 fec->eth->ecntrl |= 0x1;
436 * wait at least 16 clock cycles
440 printf ("Ethernet task stopped\n");
444 /********************************************************************/
446 static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
450 * This routine transmits one frame. This routine only accepts
451 * 6-byte Ethernet addresses.
453 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
454 volatile FEC_TBD *pTbd;
457 printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
461 * Clear Tx BD ring at first
463 mpc512x_fec_tbd_scrub (fec);
466 * Check for valid length of data.
468 if ((data_length > 1500) || (data_length <= 0)) {
473 * Check the number of vacant TxBDs.
475 if (fec->cleanTbdNum < 1) {
477 printf ("No available TxBDs ...\n");
483 * Get the first TxBD to send the mac header
485 pTbd = &fec->bdBase->tbd[fec->tbdIndex];
486 pTbd->dataLength = data_length;
487 pTbd->dataPointer = (uint32)eth_data;
488 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
489 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
491 /* Activate transmit Buffer Descriptor polling */
492 fec->eth->x_des_active = 0x01000000; /* Descriptor polling active */
498 fec->cleanTbdNum -= 1;
501 * wait until frame is sent .
503 while (pTbd->status & FEC_TBD_READY) {
506 printf ("TDB status = %04x\n", pTbd->status);
514 /********************************************************************/
515 static int mpc512x_fec_recv (struct eth_device *dev)
518 * This command pulls one frame from the card
520 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
521 volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex];
522 unsigned long ievent;
523 int frame_length, len = 0;
524 uchar buff[FEC_MAX_PKT_SIZE];
527 printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
534 * Check if any critical events have happened
536 ievent = fec->eth->ievent;
537 fec->eth->ievent = ievent;
538 if (ievent & 0x20060000) {
539 /* BABT, Rx/Tx FIFO errors */
540 mpc512x_fec_halt (dev);
541 mpc512x_fec_init (dev, NULL);
544 if (ievent & 0x80000000) {
545 /* Heartbeat error */
546 fec->eth->x_cntrl |= 0x00000001;
548 if (ievent & 0x10000000) {
549 /* Graceful stop complete */
550 if (fec->eth->x_cntrl & 0x00000001) {
551 mpc512x_fec_halt (dev);
552 fec->eth->x_cntrl &= ~0x00000001;
553 mpc512x_fec_init (dev, NULL);
557 if (!(pRbd->status & FEC_RBD_EMPTY)) {
558 if ((pRbd->status & FEC_RBD_LAST) &&
559 !(pRbd->status & FEC_RBD_ERR) &&
560 ((pRbd->dataLength - 4) > 14)) {
565 frame_length = pRbd->dataLength - 4;
570 printf ("recv data hdr:");
571 for (i = 0; i < 14; i++)
572 printf ("%x ", *((uint8*)pRbd->dataPointer + i));
578 * Fill the buffer and pass it to upper layers
580 memcpy (buff, (void*)pRbd->dataPointer, frame_length);
581 NetReceive ((uchar*)buff, frame_length);
586 * Reset buffer descriptor as empty
588 mpc512x_fec_rbd_clean (fec, pRbd);
591 /* Try to fill Buffer Descriptors */
592 fec->eth->r_des_active = 0x01000000; /* Descriptor polling active */
596 /********************************************************************/
597 int mpc512x_fec_initialize (bd_t * bis)
600 immap_t *im = (immap_t*) CFG_IMMR;
601 mpc512x_fec_priv *fec;
602 struct eth_device *dev;
604 char *tmp, *end, env_enetaddr[6];
608 fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
609 dev = (struct eth_device *) malloc (sizeof(*dev));
610 memset (dev, 0, sizeof *dev);
612 fec->eth = (ethernet_regs *) MPC512X_FEC;
614 # ifndef CONFIG_FEC_10MBIT
615 fec->xcv_type = MII100;
617 fec->xcv_type = MII10;
619 dev->priv = (void *)fec;
620 dev->iobase = MPC512X_FEC;
621 dev->init = mpc512x_fec_init;
622 dev->halt = mpc512x_fec_halt;
623 dev->send = mpc512x_fec_send;
624 dev->recv = mpc512x_fec_recv;
626 sprintf (dev->name, "FEC ETHERNET");
629 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
630 miiphy_register (dev->name,
631 fec512x_miiphy_read, fec512x_miiphy_write);
635 * Initialize I\O pins
637 reg = (uint32 *) &(im->io_ctrl.regs[PSC0_0_IDX]);
639 for (i = 0; i < 15; i++)
640 reg[i] = IOCTRL_MUX_FEC | 0x00000001;
642 im->io_ctrl.regs[SPDIF_TXCLOCK_IDX] = IOCTRL_MUX_FEC | 0x00000001;
643 im->io_ctrl.regs[SPDIF_TX_IDX] = IOCTRL_MUX_FEC | 0x00000001;
644 im->io_ctrl.regs[SPDIF_RX_IDX] = IOCTRL_MUX_FEC | 0x00000001;
646 /* Clean up space FEC's MIB and FIFO RAM ...*/
647 memset ((void *) MPC512X_FEC + 0x200, 0x00, 0x400);
650 * Malloc space for BDs (must be quad word-aligned)
651 * this pointer is lost, so cannot be freed
653 bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f);
654 fec->bdBase = (mpc512x_buff_descs*)((uint32)bd & 0xfffffff0);
655 memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f);
658 * Set interrupt mask register
660 fec->eth->imask = 0x00000000;
663 * Clear FEC-Lite interrupt event register(IEVENT)
665 fec->eth->ievent = 0xffffffff;
668 * Try to set the mac address now. The fec mac address is
669 * a garbage after reset. When not using fec for booting
670 * the Linux fec driver will try to work with this garbage.
672 tmp = getenv ("ethaddr");
674 for (i=0; i<6; i++) {
675 env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
677 tmp = (*end) ? end+1 : end;
679 mpc512x_fec_set_hwaddr (fec, env_enetaddr);
680 fec->eth->gaddr1 = 0x00000000;
681 fec->eth->gaddr2 = 0x00000000;
684 mpc512x_fec_init_phy (dev, bis);
689 /* MII-interface related functions */
690 /********************************************************************/
691 int fec512x_miiphy_read (char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
693 ethernet_regs *eth = (ethernet_regs *) MPC512X_FEC;
694 uint32 reg; /* convenient holder for the PHY register */
695 uint32 phy; /* convenient holder for the PHY */
696 int timeout = 0xffff;
699 * reading from any PHY's register is done by properly
700 * programming the FEC's MII data register.
702 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
703 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
705 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
708 * wait for the related interrupt
710 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
714 printf ("Read MDIO failed...\n");
720 * clear mii interrupt bit
722 eth->ievent = 0x00800000;
725 * it's now safe to read the PHY's register
727 *retVal = (uint16) eth->mii_data;
732 /********************************************************************/
733 int fec512x_miiphy_write (char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
735 ethernet_regs *eth = (ethernet_regs *) MPC512X_FEC;
736 uint32 reg; /* convenient holder for the PHY register */
737 uint32 phy; /* convenient holder for the PHY */
738 int timeout = 0xffff;
740 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
741 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
743 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
744 FEC_MII_DATA_TA | phy | reg | data);
747 * wait for the MII interrupt
749 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
753 printf ("Write MDIO failed...\n");
759 * clear MII interrupt bit
761 eth->ievent = 0x00800000;
767 static uint32 local_crc32 (char *string, unsigned int crc_value, int len)
771 unsigned int crc, count;
777 * crc = 0xffffffff; * The initialized value should be 0xffffffff
781 for (i = len; --i >= 0;) {
783 for (count = 0; count < 8; count++) {
784 if ((c & 0x01) ^ (crc & 0x01)) {
786 crc = crc ^ 0xedb88320;
795 * In big endian system, do byte swaping for crc value
801 #endif /* CONFIG_MPC512x_FEC */