2 * (C) Copyright 2003-2009
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 u32 local_crc32(char *string, unsigned int crc_value, int len);
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);
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 u8 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 =
70 (u32)&fec->bdBase->recv_frames[ix];
71 fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
72 fec->bdBase->rbd[ix].dataLength = 0;
76 * have the last RBD to close the ring
78 fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
84 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
85 fec->bdBase->tbd[ix].status = 0;
89 * Have the last TBD to close the ring
91 fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
94 * Initialize some indices
97 fec->usedTbdIndex = 0;
98 fec->cleanTbdNum = FEC_TBD_NUM;
103 /********************************************************************/
104 static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
107 * Reset buffer descriptor as empty
109 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
110 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
112 pRbd->status = FEC_RBD_EMPTY;
114 pRbd->dataLength = 0;
119 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
122 * Now, we have an empty RxBD, notify FEC
123 * Set Descriptor polling active
125 out_be32(&fec->eth->r_des_active, 0x01000000);
128 /********************************************************************/
129 static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
131 volatile FEC_TBD *pUsedTbd;
134 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
135 fec->cleanTbdNum, fec->usedTbdIndex);
139 * process all the consumed TBDs
141 while (fec->cleanTbdNum < FEC_TBD_NUM) {
142 pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
143 if (pUsedTbd->status & FEC_TBD_READY) {
145 printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
151 * clean this buffer descriptor
153 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
154 pUsedTbd->status = FEC_TBD_WRAP;
156 pUsedTbd->status = 0;
159 * update some indeces for a correct handling of the TBD ring
162 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
166 /********************************************************************/
167 static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
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 */
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.
189 for (byte = 0; byte < 6; byte++) {
190 currByte = mac[byte];
191 for (bit = 0; bit < 8; bit++) {
192 if ((currByte & 0x01) ^ (crc & 0x01)) {
194 crc = crc ^ 0xedb88320;
205 * Set individual hash table register
208 out_be32(&fec->eth->iaddr1, (1 << (crc - 32)));
209 out_be32(&fec->eth->iaddr2, 0);
211 out_be32(&fec->eth->iaddr1, 0);
212 out_be32(&fec->eth->iaddr2, (1 << crc));
216 * Set physical address
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) +
224 /********************************************************************/
225 static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
227 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
230 printf ("mpc512x_fec_init... Begin\n");
233 /* Set interrupt mask register */
234 out_be32(&fec->eth->imask, 0x00000000);
236 /* Clear FEC-Lite interrupt event register(IEVENT) */
237 out_be32(&fec->eth->ievent, 0xffffffff);
239 /* Set transmit fifo watermark register(X_WMRK), default = 64 */
240 out_be32(&fec->eth->x_wmrk, 0x0);
242 /* Set Opcode/Pause Duration Register */
243 out_be32(&fec->eth->op_pause, 0x00010020);
245 /* Frame length=1522; MII mode */
246 out_be32(&fec->eth->r_cntrl, (FEC_MAX_FRAME_LEN << 16) | 0x24);
248 /* Half-duplex, heartbeat disabled */
249 out_be32(&fec->eth->x_cntrl, 0x00000000);
251 /* Enable MIB counters */
252 out_be32(&fec->eth->mib_control, 0x0);
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);
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);
263 out_be32(&fec->eth->dma_control, 0xc0000000);
266 setbits_be32(&fec->eth->ecntrl, 0x00000006);
268 /* Initilize addresses and status words of BDs */
269 mpc512x_fec_bd_init (fec);
271 /* Descriptor polling active */
272 out_be32(&fec->eth->r_des_active, 0x01000000);
275 printf("mpc512x_fec_init... Done \n");
280 /********************************************************************/
281 int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
283 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
284 const u8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
289 printf ("mpc512x_fec_init_phy... Begin\n");
293 * Clear FEC-Lite interrupt event register(IEVENT)
295 out_be32(&fec->eth->ievent, 0xffffffff);
298 * Set interrupt mask register
300 out_be32(&fec->eth->imask, 0x00000000);
302 if (fec->xcv_type != SEVENWIRE) {
304 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
305 * and do not drop the Preamble.
307 out_be32(&fec->eth->mii_speed,
308 (((gd->ips_clk / 1000000) / 5) + 1) << 1);
311 * Reset PHY, then delay 300ns
313 miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
316 if (fec->xcv_type == MII10) {
318 * Force 10Base-T, FDX operation
321 printf ("Forcing 10 Mbps ethernet link... ");
323 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
325 miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
328 do { /* wait for link status to go down */
330 if ((timeout--) == 0) {
332 printf ("hmmm, should not have waited...");
336 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
340 } while ((phyStatus & 0x0004)); /* !link up */
343 do { /* wait for link status to come back up */
345 if ((timeout--) == 0) {
346 printf ("failed. Link is down.\n");
349 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
353 } while (!(phyStatus & 0x0004)); /* !link up */
358 } else { /* MII100 */
360 * Set the auto-negotiation advertisement register bits
362 miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
365 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
367 miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
370 * Wait for AN completion
376 if ((timeout--) == 0) {
378 printf ("PHY auto neg 0 failed...\n");
383 if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
385 printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
389 } while (!(phyStatus & 0x0004));
392 printf ("PHY auto neg complete! \n");
398 if (fec->xcv_type != SEVENWIRE)
399 mpc512x_fec_phydump (dev->name);
403 printf ("mpc512x_fec_init_phy... Done \n");
408 /********************************************************************/
409 static void mpc512x_fec_halt (struct eth_device *dev)
411 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
412 int counter = 0xffff;
415 if (fec->xcv_type != SEVENWIRE)
416 mpc512x_fec_phydump (dev->name);
420 * mask FEC chip interrupts
422 out_be32(&fec->eth->imask, 0);
425 * issue graceful stop command to the FEC transmitter if necessary
427 setbits_be32(&fec->eth->x_cntrl, 0x00000001);
430 * wait for graceful stop to register
432 while ((counter--) && (!(in_be32(&fec->eth->ievent) & 0x10000000)))
436 * Disable the Ethernet Controller
438 clrbits_be32(&fec->eth->ecntrl, 0x00000002);
441 * Issue a reset command to the FEC chip
443 setbits_be32(&fec->eth->ecntrl, 0x1);
446 * wait at least 16 clock cycles
450 printf ("Ethernet task stopped\n");
454 /********************************************************************/
456 static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
460 * This routine transmits one frame. This routine only accepts
461 * 6-byte Ethernet addresses.
463 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
464 volatile FEC_TBD *pTbd;
467 printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
471 * Clear Tx BD ring at first
473 mpc512x_fec_tbd_scrub (fec);
476 * Check for valid length of data.
478 if ((data_length > 1500) || (data_length <= 0)) {
483 * Check the number of vacant TxBDs.
485 if (fec->cleanTbdNum < 1) {
487 printf ("No available TxBDs ...\n");
493 * Get the first TxBD to send the mac header
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;
501 /* Activate transmit Buffer Descriptor polling */
502 out_be32(&fec->eth->x_des_active, 0x01000000);
508 fec->cleanTbdNum -= 1;
511 * wait until frame is sent .
513 while (pTbd->status & FEC_TBD_READY) {
516 printf ("TDB status = %04x\n", pTbd->status);
524 /********************************************************************/
525 static int mpc512x_fec_recv (struct eth_device *dev)
528 * This command pulls one frame from the card
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;
536 printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
543 * Check if any critical events have happened
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);
553 if (ievent & 0x80000000) {
554 /* Heartbeat error */
555 setbits_be32(&fec->eth->x_cntrl, 0x00000001);
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);
566 if (!(pRbd->status & FEC_RBD_EMPTY)) {
567 if (!(pRbd->status & FEC_RBD_ERR) &&
568 ((pRbd->dataLength - 4) > 14)) {
573 if (pRbd->status & FEC_RBD_LAST)
574 frame_length = pRbd->dataLength - 4;
576 frame_length = pRbd->dataLength;
580 printf ("recv data length 0x%08x data hdr: ",
582 for (i = 0; i < 14; i++)
583 printf ("%x ", *((u8*)pRbd->dataPointer + i));
588 * Fill the buffer and pass it to upper layers
590 memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer,
591 frame_length - rx_buff_idx);
592 rx_buff_idx = frame_length;
594 if (pRbd->status & FEC_RBD_LAST) {
595 NetReceive ((uchar*)rx_buff, frame_length);
601 * Reset buffer descriptor as empty
603 mpc512x_fec_rbd_clean (fec, pRbd);
606 /* Try to fill Buffer Descriptors */
607 out_be32(&fec->eth->r_des_active, 0x01000000);
612 /********************************************************************/
613 int mpc512x_fec_initialize (bd_t * bis)
615 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
616 mpc512x_fec_priv *fec;
617 struct eth_device *dev;
619 char *tmp, *end, env_enetaddr[6];
622 fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
623 dev = (struct eth_device *) malloc (sizeof(*dev));
624 memset (dev, 0, sizeof *dev);
628 # ifndef CONFIG_FEC_10MBIT
629 fec->xcv_type = MII100;
631 fec->xcv_type = MII10;
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;
640 sprintf (dev->name, "FEC ETHERNET");
643 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
644 miiphy_register (dev->name,
645 fec512x_miiphy_read, fec512x_miiphy_write);
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));
653 * Malloc space for BDs (must be quad word-aligned)
654 * this pointer is lost, so cannot be freed
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);
661 * Set interrupt mask register
663 out_be32(&fec->eth->imask, 0x00000000);
666 * Clear FEC-Lite interrupt event register(IEVENT)
668 out_be32(&fec->eth->ievent, 0xffffffff);
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.
675 tmp = getenv ("ethaddr");
677 for (i=0; i<6; i++) {
678 env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
680 tmp = (*end) ? end+1 : end;
682 mpc512x_fec_set_hwaddr (fec, env_enetaddr);
683 out_be32(&fec->eth->gaddr1, 0x00000000);
684 out_be32(&fec->eth->gaddr2, 0x00000000);
687 mpc512x_fec_init_phy (dev, bis);
692 /* MII-interface related functions */
693 /********************************************************************/
694 int fec512x_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal)
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;
703 * reading from any PHY's register is done by properly
704 * programming the FEC's MII data register.
706 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
707 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
709 out_be32(ð->mii_data, FEC_MII_DATA_ST |
715 * wait for the related interrupt
717 while ((timeout--) && (!(in_be32(ð->ievent) & 0x00800000)))
722 printf ("Read MDIO failed...\n");
728 * clear mii interrupt bit
730 out_be32(ð->ievent, 0x00800000);
733 * it's now safe to read the PHY's register
735 *retVal = (u16) in_be32(ð->mii_data);
740 /********************************************************************/
741 int fec512x_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data)
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;
749 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
750 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
752 out_be32(ð->mii_data, FEC_MII_DATA_ST |
758 * wait for the MII interrupt
760 while ((timeout--) && (!(in_be32(ð->ievent) & 0x00800000)))
765 printf ("Write MDIO failed...\n");
771 * clear MII interrupt bit
773 out_be32(ð->ievent, 0x00800000);
779 static u32 local_crc32 (char *string, unsigned int crc_value, int len)
783 unsigned int crc, count;
789 * crc = 0xffffffff; * The initialized value should be 0xffffffff
793 for (i = len; --i >= 0;) {
795 for (count = 0; count < 8; count++) {
796 if ((c & 0x01) ^ (crc & 0x01)) {
798 crc = crc ^ 0xedb88320;
807 * In big endian system, do byte swaping for crc value
813 #endif /* CONFIG_MPC512x_FEC */