2 * (C) Copyright 2003-2007
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Derived from the MPC8xx FEC driver.
6 * Adapted for MPC512x by Grzegorz Bernacki <gjb@semihalf.com>
15 #include "mpc512x_fec.h"
17 DECLARE_GLOBAL_DATA_PTR;
21 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
22 defined(CONFIG_MPC512x_FEC)
24 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
25 #error "CONFIG_MII has to be defined!"
29 static uint32 local_crc32(char *string, unsigned int crc_value, int len);
32 int fec512x_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal);
33 int fec512x_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data);
34 int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis);
36 static uchar rx_buff[FEC_BUFFER_SIZE];
37 static int rx_buff_idx = 0;
39 /********************************************************************/
41 static void mpc512x_fec_phydump (char *devname)
44 uint8 phyAddr = CONFIG_PHY_ADDR;
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,
51 for (i = 0; i < 32; i++) {
53 miiphy_read (devname, phyAddr, i, &phyStatus);
54 printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
60 /********************************************************************/
61 static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec)
68 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
69 fec->bdBase->rbd[ix].dataPointer = (uint32)&fec->bdBase->recv_frames[ix];
70 fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
71 fec->bdBase->rbd[ix].dataLength = 0;
75 * have the last RBD to close the ring
77 fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
83 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
84 fec->bdBase->tbd[ix].status = 0;
88 * Have the last TBD to close the ring
90 fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
93 * Initialize some indices
96 fec->usedTbdIndex = 0;
97 fec->cleanTbdNum = FEC_TBD_NUM;
102 /********************************************************************/
103 static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
106 * Reset buffer descriptor as empty
108 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
109 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
111 pRbd->status = FEC_RBD_EMPTY;
113 pRbd->dataLength = 0;
118 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
121 * Now, we have an empty RxBD, notify FEC
123 fec->eth->r_des_active = 0x01000000; /* Descriptor polling active */
126 /********************************************************************/
127 static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
129 volatile FEC_TBD *pUsedTbd;
132 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
133 fec->cleanTbdNum, fec->usedTbdIndex);
137 * process all the consumed TBDs
139 while (fec->cleanTbdNum < FEC_TBD_NUM) {
140 pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
141 if (pUsedTbd->status & FEC_TBD_READY) {
143 printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
149 * clean this buffer descriptor
151 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
152 pUsedTbd->status = FEC_TBD_WRAP;
154 pUsedTbd->status = 0;
157 * update some indeces for a correct handling of the TBD ring
160 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
164 /********************************************************************/
165 static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
167 uint8 currByte; /* byte for which to compute the CRC */
168 int byte; /* loop - counter */
169 int bit; /* loop - counter */
170 uint32 crc = 0xffffffff; /* initial value */
173 * The algorithm used is the following:
174 * we loop on each of the six bytes of the provided address,
175 * and we compute the CRC by left-shifting the previous
176 * value by one position, so that each bit in the current
177 * byte of the address may contribute the calculation. If
178 * the latter and the MSB in the CRC are different, then
179 * the CRC value so computed is also ex-ored with the
180 * "polynomium generator". The current byte of the address
181 * is also shifted right by one bit at each iteration.
182 * This is because the CRC generatore in hardware is implemented
183 * as a shift-register with as many ex-ores as the radixes
184 * in the polynomium. This suggests that we represent the
185 * polynomiumm itself as a 32-bit constant.
187 for (byte = 0; byte < 6; byte++) {
188 currByte = mac[byte];
189 for (bit = 0; bit < 8; bit++) {
190 if ((currByte & 0x01) ^ (crc & 0x01)) {
192 crc = crc ^ 0xedb88320;
203 * Set individual hash table register
206 fec->eth->iaddr1 = (1 << (crc - 32));
207 fec->eth->iaddr2 = 0;
209 fec->eth->iaddr1 = 0;
210 fec->eth->iaddr2 = (1 << crc);
214 * Set physical address
216 fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
217 fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
220 /********************************************************************/
221 static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
223 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
226 printf ("mpc512x_fec_init... Begin\n");
229 /* Set interrupt mask register */
230 fec->eth->imask = 0x00000000;
232 /* Clear FEC-Lite interrupt event register(IEVENT) */
233 fec->eth->ievent = 0xffffffff;
235 /* Set transmit fifo watermark register(X_WMRK), default = 64 */
236 fec->eth->x_wmrk = 0x0;
238 /* Set Opcode/Pause Duration Register */
239 fec->eth->op_pause = 0x00010020;
241 /* Frame length=1522; MII mode */
242 fec->eth->r_cntrl = (FEC_MAX_FRAME_LEN << 16) | 0x24;
244 /* Half-duplex, heartbeat disabled */
245 fec->eth->x_cntrl = 0x00000000;
247 /* Enable MIB counters */
248 fec->eth->mib_control = 0x0;
250 /* Setup recv fifo start and buff size */
251 fec->eth->r_fstart = 0x500;
252 fec->eth->r_buff_size = FEC_BUFFER_SIZE;
254 /* Setup BD base addresses */
255 fec->eth->r_des_start = (uint32)fec->bdBase->rbd;
256 fec->eth->x_des_start = (uint32)fec->bdBase->tbd;
259 fec->eth->dma_control = 0xc0000000;
262 fec->eth->ecntrl |= 0x00000006;
264 /* Initilize addresses and status words of BDs */
265 mpc512x_fec_bd_init (fec);
267 /* Descriptor polling active */
268 fec->eth->r_des_active = 0x01000000;
271 printf("mpc512x_fec_init... Done \n");
276 /********************************************************************/
277 int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
279 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
280 const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
285 printf ("mpc512x_fec_init_phy... Begin\n");
289 * Clear FEC-Lite interrupt event register(IEVENT)
291 fec->eth->ievent = 0xffffffff;
294 * Set interrupt mask register
296 fec->eth->imask = 0x00000000;
298 if (fec->xcv_type != SEVENWIRE) {
300 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
301 * and do not drop the Preamble.
303 fec->eth->mii_speed = (((gd->ips_clk / 1000000) / 5) + 1) << 1;
306 * Reset PHY, then delay 300ns
308 miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
311 if (fec->xcv_type == MII10) {
313 * Force 10Base-T, FDX operation
316 printf ("Forcing 10 Mbps ethernet link... ");
318 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
320 miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
323 do { /* wait for link status to go down */
325 if ((timeout--) == 0) {
327 printf ("hmmm, should not have waited...");
331 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
335 } while ((phyStatus & 0x0004)); /* !link up */
338 do { /* wait for link status to come back up */
340 if ((timeout--) == 0) {
341 printf ("failed. Link is down.\n");
344 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
348 } while (!(phyStatus & 0x0004)); /* !link up */
353 } else { /* MII100 */
355 * Set the auto-negotiation advertisement register bits
357 miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
360 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
362 miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
365 * Wait for AN completion
371 if ((timeout--) == 0) {
373 printf ("PHY auto neg 0 failed...\n");
378 if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
380 printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
384 } while (!(phyStatus & 0x0004));
387 printf ("PHY auto neg complete! \n");
393 if (fec->xcv_type != SEVENWIRE)
394 mpc512x_fec_phydump (dev->name);
398 printf ("mpc512x_fec_init_phy... Done \n");
403 /********************************************************************/
404 static void mpc512x_fec_halt (struct eth_device *dev)
406 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
407 int counter = 0xffff;
410 if (fec->xcv_type != SEVENWIRE)
411 mpc512x_fec_phydump (dev->name);
415 * mask FEC chip interrupts
420 * issue graceful stop command to the FEC transmitter if necessary
422 fec->eth->x_cntrl |= 0x00000001;
425 * wait for graceful stop to register
427 while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
430 * Disable the Ethernet Controller
432 fec->eth->ecntrl &= 0xfffffffd;
435 * Issue a reset command to the FEC chip
437 fec->eth->ecntrl |= 0x1;
440 * wait at least 16 clock cycles
444 printf ("Ethernet task stopped\n");
448 /********************************************************************/
450 static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
454 * This routine transmits one frame. This routine only accepts
455 * 6-byte Ethernet addresses.
457 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
458 volatile FEC_TBD *pTbd;
461 printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
465 * Clear Tx BD ring at first
467 mpc512x_fec_tbd_scrub (fec);
470 * Check for valid length of data.
472 if ((data_length > 1500) || (data_length <= 0)) {
477 * Check the number of vacant TxBDs.
479 if (fec->cleanTbdNum < 1) {
481 printf ("No available TxBDs ...\n");
487 * Get the first TxBD to send the mac header
489 pTbd = &fec->bdBase->tbd[fec->tbdIndex];
490 pTbd->dataLength = data_length;
491 pTbd->dataPointer = (uint32)eth_data;
492 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
493 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
495 /* Activate transmit Buffer Descriptor polling */
496 fec->eth->x_des_active = 0x01000000; /* Descriptor polling active */
502 fec->cleanTbdNum -= 1;
505 * wait until frame is sent .
507 while (pTbd->status & FEC_TBD_READY) {
510 printf ("TDB status = %04x\n", pTbd->status);
518 /********************************************************************/
519 static int mpc512x_fec_recv (struct eth_device *dev)
522 * This command pulls one frame from the card
524 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
525 volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex];
526 unsigned long ievent;
527 int frame_length = 0;
530 printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
537 * Check if any critical events have happened
539 ievent = fec->eth->ievent;
540 fec->eth->ievent = ievent;
541 if (ievent & 0x20060000) {
542 /* BABT, Rx/Tx FIFO errors */
543 mpc512x_fec_halt (dev);
544 mpc512x_fec_init (dev, NULL);
547 if (ievent & 0x80000000) {
548 /* Heartbeat error */
549 fec->eth->x_cntrl |= 0x00000001;
551 if (ievent & 0x10000000) {
552 /* Graceful stop complete */
553 if (fec->eth->x_cntrl & 0x00000001) {
554 mpc512x_fec_halt (dev);
555 fec->eth->x_cntrl &= ~0x00000001;
556 mpc512x_fec_init (dev, NULL);
560 if (!(pRbd->status & FEC_RBD_EMPTY)) {
561 if (!(pRbd->status & FEC_RBD_ERR) &&
562 ((pRbd->dataLength - 4) > 14)) {
567 if (pRbd->status & FEC_RBD_LAST)
568 frame_length = pRbd->dataLength - 4;
570 frame_length = pRbd->dataLength;
574 printf ("recv data length 0x%08x data hdr: ",
576 for (i = 0; i < 14; i++)
577 printf ("%x ", *((uint8*)pRbd->dataPointer + i));
582 * Fill the buffer and pass it to upper layers
584 memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer,
585 frame_length - rx_buff_idx);
586 rx_buff_idx = frame_length;
588 if (pRbd->status & FEC_RBD_LAST) {
589 NetReceive ((uchar*)rx_buff, frame_length);
595 * Reset buffer descriptor as empty
597 mpc512x_fec_rbd_clean (fec, pRbd);
600 /* Try to fill Buffer Descriptors */
601 fec->eth->r_des_active = 0x01000000; /* Descriptor polling active */
605 /********************************************************************/
606 int mpc512x_fec_initialize (bd_t * bis)
608 mpc512x_fec_priv *fec;
609 struct eth_device *dev;
611 char *tmp, *end, env_enetaddr[6];
614 fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
615 dev = (struct eth_device *) malloc (sizeof(*dev));
616 memset (dev, 0, sizeof *dev);
618 fec->eth = (ethernet_regs *) MPC512X_FEC;
620 # ifndef CONFIG_FEC_10MBIT
621 fec->xcv_type = MII100;
623 fec->xcv_type = MII10;
625 dev->priv = (void *)fec;
626 dev->iobase = MPC512X_FEC;
627 dev->init = mpc512x_fec_init;
628 dev->halt = mpc512x_fec_halt;
629 dev->send = mpc512x_fec_send;
630 dev->recv = mpc512x_fec_recv;
632 sprintf (dev->name, "FEC ETHERNET");
635 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
636 miiphy_register (dev->name,
637 fec512x_miiphy_read, fec512x_miiphy_write);
640 /* Clean up space FEC's MIB and FIFO RAM ...*/
641 memset ((void *) MPC512X_FEC + 0x200, 0x00, 0x400);
644 * Malloc space for BDs (must be quad word-aligned)
645 * this pointer is lost, so cannot be freed
647 bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f);
648 fec->bdBase = (mpc512x_buff_descs*)((uint32)bd & 0xfffffff0);
649 memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f);
652 * Set interrupt mask register
654 fec->eth->imask = 0x00000000;
657 * Clear FEC-Lite interrupt event register(IEVENT)
659 fec->eth->ievent = 0xffffffff;
662 * Try to set the mac address now. The fec mac address is
663 * a garbage after reset. When not using fec for booting
664 * the Linux fec driver will try to work with this garbage.
666 tmp = getenv ("ethaddr");
668 for (i=0; i<6; i++) {
669 env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
671 tmp = (*end) ? end+1 : end;
673 mpc512x_fec_set_hwaddr (fec, env_enetaddr);
674 fec->eth->gaddr1 = 0x00000000;
675 fec->eth->gaddr2 = 0x00000000;
678 mpc512x_fec_init_phy (dev, bis);
683 /* MII-interface related functions */
684 /********************************************************************/
685 int fec512x_miiphy_read (char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
687 ethernet_regs *eth = (ethernet_regs *) MPC512X_FEC;
688 uint32 reg; /* convenient holder for the PHY register */
689 uint32 phy; /* convenient holder for the PHY */
690 int timeout = 0xffff;
693 * reading from any PHY's register is done by properly
694 * programming the FEC's MII data register.
696 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
697 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
699 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
702 * wait for the related interrupt
704 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
708 printf ("Read MDIO failed...\n");
714 * clear mii interrupt bit
716 eth->ievent = 0x00800000;
719 * it's now safe to read the PHY's register
721 *retVal = (uint16) eth->mii_data;
726 /********************************************************************/
727 int fec512x_miiphy_write (char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
729 ethernet_regs *eth = (ethernet_regs *) MPC512X_FEC;
730 uint32 reg; /* convenient holder for the PHY register */
731 uint32 phy; /* convenient holder for the PHY */
732 int timeout = 0xffff;
734 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
735 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
737 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
738 FEC_MII_DATA_TA | phy | reg | data);
741 * wait for the MII interrupt
743 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
747 printf ("Write MDIO failed...\n");
753 * clear MII interrupt bit
755 eth->ievent = 0x00800000;
761 static uint32 local_crc32 (char *string, unsigned int crc_value, int len)
765 unsigned int crc, count;
771 * crc = 0xffffffff; * The initialized value should be 0xffffffff
775 for (i = len; --i >= 0;) {
777 for (count = 0; count < 8; count++) {
778 if ((c & 0x01) ^ (crc & 0x01)) {
780 crc = crc ^ 0xedb88320;
789 * In big endian system, do byte swaping for crc value
795 #endif /* CONFIG_MPC512x_FEC */