* Add start-up delay to make sure power has stabilized before
[kernel/u-boot.git] / cpu / mpc5xxx / fec.c
index b8e2560..50499e2 100644 (file)
 #include "sdma.h"
 #include "fec.h"
 
-#define DEBUG  0x8
+/* #define DEBUG       0x28 */
 
 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
-       defined(CONFIG_MPC5XXX_FEC)
+       defined(CONFIG_MPC5xxx_FEC)
 
 #if (DEBUG & 0x60)
 static void tfifo_print(mpc5xxx_fec_priv *fec);
@@ -28,25 +28,60 @@ static void rfifo_print(mpc5xxx_fec_priv *fec);
 static uint32 local_crc32(char *string, unsigned int crc_value, int len);
 #endif
 
+typedef struct {
+    uint8 data[1500];           /* actual data */
+    int length;                 /* actual length */
+    int used;                   /* buffer in use or not */
+    uint8 head[16];             /* MAC header(6 + 6 + 2) + 2(aligned) */
+} NBUF;
+
+/********************************************************************/
+#if (DEBUG & 0x2)
+static void mpc5xxx_fec_phydump (void)
+{
+       uint16 phyStatus, i;
+       uint8 phyAddr = CONFIG_PHY_ADDR;
+       uint8 reg_mask[] = {
+#if CONFIG_PHY_TYPE == 0x79c874        /* AMD Am79C874 */
+               /* regs to print: 0...7, 16...19, 21, 23, 24 */
+               1, 1, 1, 1,  1, 1, 1, 1,     0, 0, 0, 0,  0, 0, 0, 0,
+               1, 1, 1, 1,  0, 1, 0, 1,     1, 0, 0, 0,  0, 0, 0, 0,
+#else
+               /* regs to print: 0...8, 16...20 */
+               1, 1, 1, 1,  1, 1, 1, 1,     1, 0, 0, 0,  0, 0, 0, 0,
+               1, 1, 1, 1,  1, 0, 0, 0,     0, 0, 0, 0,  0, 0, 0, 0,
+#endif
+       };
+
+       for (i = 0; i < 32; i++) {
+               if (reg_mask[i]) {
+                       miiphy_read(phyAddr, i, &phyStatus);
+                       printf("Mii reg %d: 0x%04x\n", i, phyStatus);
+               }
+       }
+}
+#endif
+
 /********************************************************************/
 static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
 {
        int ix;
        char *data;
+       static int once = 0;
 
-       /*
-        * the receive ring is located right after the transmit one
-        */
        for (ix = 0; ix < FEC_RBD_NUM; ix++) {
-               data = (char *)malloc(FEC_MAX_PKT_SIZE);
-               if (data == NULL) {
-                       printf ("RBD INIT FAILED\n");
-                       return -1;
+               if (!once) {
+                       data = (char *)malloc(FEC_MAX_PKT_SIZE);
+                       if (data == NULL) {
+                               printf ("RBD INIT FAILED\n");
+                               return -1;
+                       }
+                       fec->rbdBase[ix].dataPointer = (uint32)data;
                }
                fec->rbdBase[ix].status = FEC_RBD_EMPTY;
                fec->rbdBase[ix].dataLength = 0;
-               fec->rbdBase[ix].dataPointer = (uint32)data;
        }
+       once ++;
 
        /*
         * have the last RBD to close the ring
@@ -200,9 +235,10 @@ static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac)
 /********************************************************************/
 static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
 {
+       DECLARE_GLOBAL_DATA_PTR;
        mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
        struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
-       const uint8 phyAddr = 0;        /* Only one PHY */
+       const uint8 phyAddr = CONFIG_PHY_ADDR;  /* Only one PHY */
 
 #if (DEBUG & 0x1)
        printf ("mpc5xxx_fec_init... Begin\n");
@@ -261,10 +297,10 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
                fec->eth->x_cntrl = 0x00000004; /* full-duplex, heartbeat disabled */
 
                /*
-                * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock(25Mhz)
+                * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
                 * and do not drop the Preamble.
                 */
-               fec->eth->mii_speed = (0x5 << 1);       /* No MII for 7-wire mode */
+               fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
        }
 
        /*
@@ -336,11 +372,6 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
        SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
 
        /*
-        * Set SmartDMA intMask register to enable SmartDMA task interrupts
-        */
-       SDMA_INT_ENABLE(FEC_RECV_TASK_NO);
-
-       /*
         * Initialize SmartDMA parameters stored in SRAM
         */
        *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
@@ -383,7 +414,9 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
                        /*
                         * Force 10Base-T, FDX operation
                         */
+#if (DEBUG & 0x2)
                        printf("Forcing 10 Mbps ethernet link... ");
+#endif
                        miiphy_read(phyAddr, 0x1, &phyStatus);
                        /*
                        miiphy_write(fec, phyAddr, 0x0, 0x0100);
@@ -418,7 +451,9 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
 #endif
                        } while (!(phyStatus & 0x0004));        /* !link up */
 
+#if (DEBUG & 0x2)
                        printf ("done.\n");
+#endif
                } else {        /* MII100 */
                        /*
                         * Set the auto-negotiation advertisement register bits
@@ -464,21 +499,11 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
         */
        fec->eth->ecntrl |= 0x00000006;
 
-       if (fec->xcv_type != SEVENWIRE) {
 #if (DEBUG & 0x2)
-               uint16 phyStatus, i;
-               uint8 phyAddr = 0;
-
-               for (i = 0; i < 9; i++) {
-                       miiphy_read(phyAddr, i, &phyStatus);
-                       printf("Mii reg %d: 0x%04x\n", i, phyStatus);
-               }
-               for (i = 16; i < 21; i++) {
-                       miiphy_read(phyAddr, i, &phyStatus);
-                       printf("Mii reg %d: 0x%04x\n", i, phyStatus);
-               }
+       if (fec->xcv_type != SEVENWIRE)
+               mpc5xxx_fec_phydump ();
 #endif
-       }
+
        /*
         * Enable SmartDMA receive task
         */
@@ -488,33 +513,23 @@ static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
        printf("mpc5xxx_fec_init... Done \n");
 #endif
 
-       return 0;
+       return 1;
 }
 
 /********************************************************************/
 static void mpc5xxx_fec_halt(struct eth_device *dev)
 {
-       mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
+#if defined(CONFIG_MPC5200)
        struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
+#endif
+       mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
        int counter = 0xffff;
 
 #if (DEBUG & 0x2)
-       if (fec->xcv_type != SEVENWIRE) {
-               uint16 phyStatus, i;
-               uint8 phyAddr = 0;
-
-               for (i = 0; i < 9; i++) {
-                       miiphy_read(phyAddr, i, &phyStatus);
-                       printf("Mii reg %d: 0x%04x\n", i, phyStatus);
-               }
-               for (i = 16; i < 21; i++) {
-                       miiphy_read(phyAddr, i, &phyStatus);
-                       printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
-               }
-       }
+       if (fec->xcv_type != SEVENWIRE)
+               mpc5xxx_fec_phydump ();
 #endif
 
-
        /*
         * mask FEC chip interrupts
         */
@@ -530,8 +545,6 @@ static void mpc5xxx_fec_halt(struct eth_device *dev)
         */
        while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
 
-       SDMA_INT_DISABLE (FEC_RECV_TASK_NO);
-
        /*
         * Disable SmartDMA tasks
         */
@@ -579,7 +592,7 @@ static void mpc5xxx_fec_halt(struct eth_device *dev)
 
 static void tfifo_print(mpc5xxx_fec_priv *fec)
 {
-       uint16 phyAddr = 0;
+       uint16 phyAddr = CONFIG_PHY_ADDR;
        uint16 phyStatus;
 
        if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
@@ -603,7 +616,7 @@ static void tfifo_print(mpc5xxx_fec_priv *fec)
 
 static void rfifo_print(mpc5xxx_fec_priv *fec)
 {
-       uint16 phyAddr = 0;
+       uint16 phyAddr = CONFIG_PHY_ADDR;
        uint16 phyStatus;
 
        if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
@@ -671,7 +684,7 @@ static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
        pTbd = &fec->tbdBase[fec->tbdIndex];
        pTbd->dataLength = data_length;
        pTbd->dataPointer = (uint32)eth_data;
-       pTbd->status |= FEC_TBD_READY;
+       pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
        fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
 
 #if (DEBUG & 0x100)
@@ -729,8 +742,9 @@ static int mpc5xxx_fec_recv(struct eth_device *dev)
        mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
        FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
        unsigned long ievent;
-       int frame_length;
-       char *frame;
+       int frame_length, len = 0;
+       NBUF *frame;
+       char buff[FEC_MAX_PKT_SIZE];
 
 #if (DEBUG & 0x1)
        printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
@@ -763,41 +777,40 @@ static int mpc5xxx_fec_recv(struct eth_device *dev)
                }
        }
 
-       /*
-        * Do we have data in Rx FIFO?
-        */
-       if ((pRbd->status & FEC_RBD_EMPTY) || !(pRbd->status & FEC_RBD_LAST)){
-               return 0;
-       }
+       if (!(pRbd->status & FEC_RBD_EMPTY)) {
+               if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
+                       ((pRbd->dataLength - 4) > 14)) {
 
-       /*
-        * Pass the packet up only if reception was Ok
-        */
-       if ((pRbd->dataLength <= 14) || (pRbd->status & FEC_RBD_ERR)) {
-               mpc5xxx_fec_rbd_clean(fec, pRbd);
-#if (DEBUG & 0x8)
-               printf( "X0" );
+                       /*
+                        * Get buffer address and size
+                        */
+                       frame = (NBUF *)pRbd->dataPointer;
+                       frame_length = pRbd->dataLength - 4;
+
+#if (DEBUG & 0x20)
+                       {
+                               int i;
+                               printf("recv data hdr:");
+                               for (i = 0; i < 14; i++)
+                                       printf("%x ", *(frame->head + i));
+                               printf("\n");
+                       }
 #endif
-               return 0;
+                       /*
+                        *  Fill the buffer and pass it to upper layers
+                        */
+                       memcpy(buff, frame->head, 14);
+                       memcpy(buff + 14, frame->data, frame_length);
+                       NetReceive(buff, frame_length);
+                       len = frame_length;
+               }
+               /*
+                * Reset buffer descriptor as empty
+                */
+               mpc5xxx_fec_rbd_clean(fec, pRbd);
        }
-
-       /*
-        * Get buffer address and size
-        */
-       frame = (char *)pRbd->dataPointer;
-       frame_length = pRbd->dataLength;
-
-       /*
-        * Pass the buffer to upper layers
-        */
-       NetReceive(frame, frame_length);
-
-       /*
-        * Reset buffer descriptor as empty
-        */
-       mpc5xxx_fec_rbd_clean(fec, pRbd);
-
-       return frame_length;
+       SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
+       return len;
 }
 
 
@@ -806,15 +819,27 @@ int mpc5xxx_fec_initialize(bd_t * bis)
 {
        mpc5xxx_fec_priv *fec;
        struct eth_device *dev;
+       char *tmp, *end;
+       char env_enetaddr[6];
+       int i;
 
        fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
        dev = (struct eth_device *)malloc(sizeof(*dev));
+       memset(dev, 0, sizeof *dev);
 
        fec->eth = (ethernet_regs *)MPC5XXX_FEC;
        fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
        fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
-#ifdef CONFIG_ICECUBE
+#if defined(CONFIG_ICECUBE) ||
+    defined(CONFIG_PM520)   ||
+    defined(CONFIG_TOP5200)
+# ifndef CONFIG_FEC_10MBIT
        fec->xcv_type = MII100;
+# else
+       fec->xcv_type = MII10;
+# endif
+#else
+#error fec->xcv_type not initialized.
 #endif
 
        dev->priv = (void *)fec;
@@ -824,8 +849,24 @@ int mpc5xxx_fec_initialize(bd_t * bis)
        dev->send = mpc5xxx_fec_send;
        dev->recv = mpc5xxx_fec_recv;
 
+       sprintf(dev->name, "FEC ETHERNET");
        eth_register(dev);
 
+       /*
+        * Try to set the mac address now. The fec mac address is
+        * a garbage after reset. When not using fec for booting
+        * the Linux fec driver will try to work with this garbage.
+        */
+       tmp = getenv("ethaddr");
+       if (tmp) {
+               for (i=0; i<6; i++) {
+                       env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
+                       if (tmp)
+                               tmp = (*end) ? end+1 : end;
+               }
+               mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
+       }
+
        return 1;
 }
 
@@ -941,4 +982,4 @@ static uint32 local_crc32(char *string, unsigned int crc_value, int len)
 }
 #endif /* DEBUG */
 
-#endif /* CONFIG_MPC5XXX_FEC */
+#endif /* CONFIG_MPC5xxx_FEC */