Merge branch 'mpc86xx'
[platform/kernel/u-boot.git] / cpu / ppc4xx / 4xx_enet.c
index 859b72d..fab65af 100644 (file)
 #error "CONFIG_MII has to be defined!"
 #endif
 
+#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
+#error "CONFIG_NET_MULTI has to be defined for NetConsole"
+#endif
+
 #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
 #define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
 
 #define ENET_MAX_MTU          PKTSIZE
 #define ENET_MAX_MTU_ALIGNED   PKTSIZE_ALIGN
 
-/* define the number of channels implemented */
-#define EMAC_RXCHL     EMAC_NUM_DEV
-#define EMAC_TXCHL     EMAC_NUM_DEV
-
 /*-----------------------------------------------------------------------------+
  * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
  * Interrupt Controller).
@@ -142,6 +142,19 @@ static uint32_t mal_ier;
 struct eth_device *emac0_dev = NULL;
 #endif
 
+/*
+ * Get count of EMAC devices (doesn't have to be the max. possible number
+ * supported by the cpu)
+ */
+#if defined(CONFIG_HAS_ETH3)
+#define LAST_EMAC_NUM  4
+#elif defined(CONFIG_HAS_ETH2)
+#define LAST_EMAC_NUM  3
+#elif defined(CONFIG_HAS_ETH1)
+#define LAST_EMAC_NUM  2
+#else
+#define LAST_EMAC_NUM  1
+#endif
 
 /*-----------------------------------------------------------------------------+
  * Prototypes and externals.
@@ -154,6 +167,11 @@ static void mal_err (struct eth_device *dev, unsigned long isr,
                     unsigned long mal_errr);
 static void emac_err (struct eth_device *dev, unsigned long isr);
 
+extern int phy_setup_aneg (char *devname, unsigned char addr);
+extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
+               unsigned char reg, unsigned short *value);
+extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
+               unsigned char reg, unsigned short value);
 
 /*-----------------------------------------------------------------------------+
 | ppc_4xx_eth_halt
@@ -163,6 +181,9 @@ static void ppc_4xx_eth_halt (struct eth_device *dev)
 {
        EMAC_4XX_HW_PST hw_p = dev->priv;
        uint32_t failsafe = 10000;
+#if defined(CONFIG_440SPE)
+       unsigned long mfr;
+#endif
 
        out32 (EMAC_IER + hw_p->hw_addr, 0x00000000);   /* disable emac interrupts */
 
@@ -184,8 +205,23 @@ static void ppc_4xx_eth_halt (struct eth_device *dev)
        }
 
        /* EMAC RESET */
+#if defined(CONFIG_440SPE)
+       /* provide clocks for EMAC internal loopback  */
+       mfsdr (sdr_mfr, mfr);
+       mfr |= 0x08000000;
+       mtsdr(sdr_mfr, mfr);
+#endif
+
        out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
 
+#if defined(CONFIG_440SPE)
+       /* remove clocks for EMAC internal loopback  */
+       mfsdr (sdr_mfr, mfr);
+       mfr &= ~0x08000000;
+       mtsdr(sdr_mfr, mfr);
+#endif
+
+
 #ifndef CONFIG_NETCONSOLE
        hw_p->print_speed = 1;  /* print speed message again next time */
 #endif
@@ -193,9 +229,6 @@ static void ppc_4xx_eth_halt (struct eth_device *dev)
        return;
 }
 
-extern int phy_setup_aneg (unsigned char addr);
-extern int miiphy_reset (unsigned char addr);
-
 #if defined (CONFIG_440GX)
 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
 {
@@ -286,7 +319,7 @@ int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
        return ((int)pfc1);
 
 }
-#endif
+#endif /* CONFIG_440_GX */
 
 static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
 {
@@ -299,10 +332,16 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
        unsigned mode_reg;
        unsigned short devnum;
        unsigned short reg_short;
-#if defined(CONFIG_440GX)
+#if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
        sys_info_t sysinfo;
-       int ethgroup;
+#if defined(CONFIG_440GX) || defined(CONFIG_440SPE)
+       int ethgroup = -1;
+#endif
 #endif
+#if defined(CONFIG_440SPE)
+       unsigned long mfr;
+#endif
+
 
        EMAC_4XX_HW_PST hw_p = dev->priv;
 
@@ -313,7 +352,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                return -1;
        }
 
-#if defined(CONFIG_440GX)
+#if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
        /* Need to get the OPB frequency so we can access the PHY */
        get_sys_info (&sysinfo);
 #endif
@@ -343,6 +382,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
        hw_p->stats.pkts_tx = 0;
        hw_p->stats.pkts_rx = 0;
        hw_p->stats.pkts_handled = 0;
+       hw_p->print_speed = 1;  /* print speed message again next time */
 #endif
 
        hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
@@ -356,7 +396,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
        hw_p->tx_i_index = 0;   /* Transmit Interrupt Queue Index */
        hw_p->tx_u_index = 0;   /* Transmit User Queue Index */
 
-#if defined(CONFIG_440)
+#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
        /* set RMII mode */
        /* NOTE: 440GX spec states that mode is mutually exclusive */
        /* NOTE: Therefore, disable all other EMACS, since we handle */
@@ -384,11 +424,17 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
 #endif
 
        out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
-#endif /* defined(CONFIG_440) */
+#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
 
        __asm__ volatile ("eieio");
 
        /* reset emac so we have access to the phy */
+#if defined(CONFIG_440SPE)
+       /* provide clocks for EMAC internal loopback  */
+       mfsdr (sdr_mfr, mfr);
+       mfr |= 0x08000000;
+       mtsdr(sdr_mfr, mfr);
+#endif
 
        out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
        __asm__ volatile ("eieio");
@@ -399,7 +445,14 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                failsafe--;
        }
 
-#if defined(CONFIG_440GX)
+#if defined(CONFIG_440SPE)
+       /* remove clocks for EMAC internal loopback  */
+       mfsdr (sdr_mfr, mfr);
+       mfr &= ~0x08000000;
+       mtsdr(sdr_mfr, mfr);
+#endif
+
+#if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
        /* Whack the M1 register */
        mode_reg = 0x0;
        mode_reg &= ~0x00000038;
@@ -414,7 +467,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                mode_reg |= EMAC_M1_OBCI_GT100;
 
        out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
-#endif /*  defined(CONFIG_440GX) */
+#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
 
        /* wait for PHY to complete auto negotiation */
        reg_short = 0;
@@ -449,9 +502,9 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
         * otherwise, just check the speeds & feeds
         */
        if (hw_p->first_init == 0) {
-               miiphy_reset (reg);
+               miiphy_reset (dev->name, reg);
 
-#if defined(CONFIG_440GX)
+#if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
 #if defined(CONFIG_CIS8201_PHY)
                /*
                 * Cicada 8201 PHY needs to have an extended register whacked
@@ -459,9 +512,9 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                 */
                if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
 #if defined(CONFIG_CIS8201_SHORT_ETCH)
-                       miiphy_write (reg, 23, 0x1300);
+                       miiphy_write (dev->name, reg, 23, 0x1300);
 #else
-                       miiphy_write (reg, 23, 0x1000);
+                       miiphy_write (dev->name, reg, 23, 0x1000);
 #endif
                        /*
                         * Vitesse VSC8201/Cicada CIS8201 errata:
@@ -469,26 +522,26 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                         * This work around (provided by Vitesse) changes
                         * the default timer convergence from 8ms to 12ms
                         */
-                       miiphy_write (reg, 0x1f, 0x2a30);
-                       miiphy_write (reg, 0x08, 0x0200);
-                       miiphy_write (reg, 0x1f, 0x52b5);
-                       miiphy_write (reg, 0x02, 0x0004);
-                       miiphy_write (reg, 0x01, 0x0671);
-                       miiphy_write (reg, 0x00, 0x8fae);
-                       miiphy_write (reg, 0x1f, 0x2a30);
-                       miiphy_write (reg, 0x08, 0x0000);
-                       miiphy_write (reg, 0x1f, 0x0000);
+                       miiphy_write (dev->name, reg, 0x1f, 0x2a30);
+                       miiphy_write (dev->name, reg, 0x08, 0x0200);
+                       miiphy_write (dev->name, reg, 0x1f, 0x52b5);
+                       miiphy_write (dev->name, reg, 0x02, 0x0004);
+                       miiphy_write (dev->name, reg, 0x01, 0x0671);
+                       miiphy_write (dev->name, reg, 0x00, 0x8fae);
+                       miiphy_write (dev->name, reg, 0x1f, 0x2a30);
+                       miiphy_write (dev->name, reg, 0x08, 0x0000);
+                       miiphy_write (dev->name, reg, 0x1f, 0x0000);
                        /* end Vitesse/Cicada errata */
                }
 #endif
 #endif
                /* Start/Restart autonegotiation */
-               phy_setup_aneg (reg);
+               phy_setup_aneg (dev->name, reg);
                udelay (1000);
        }
 #endif /* defined(CONFIG_PHY_RESET) */
 
-       miiphy_read (reg, PHY_BMSR, &reg_short);
+       miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
 
        /*
         * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
@@ -510,7 +563,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                                putc ('.');
                        }
                        udelay (1000);  /* 1 ms */
-                       miiphy_read (reg, PHY_BMSR, &reg_short);
+                       miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
 
                }
                puts (" done\n");
@@ -518,8 +571,8 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
        }
 #endif /* #ifndef CONFIG_CS8952_PHY */
 
-       speed = miiphy_speed (reg);
-       duplex = miiphy_duplex (reg);
+       speed = miiphy_speed (dev->name, reg);
+       duplex = miiphy_duplex (dev->name, reg);
 
        if (hw_p->print_speed) {
                hw_p->print_speed = 0;
@@ -527,7 +580,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
                        (int) speed, (duplex == HALF) ? "HALF" : "FULL");
        }
 
-#if defined(CONFIG_440)
+#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
 #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
        mfsdr(sdr_mfr, reg);
        if (speed == 100) {
@@ -555,10 +608,10 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
 
                out32 (RGMII_SSR, reg);
        }
-#endif /* defined(CONFIG_440) */
+#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
 
        /* set the Mal configuration reg */
-#if defined(CONFIG_440GX)
+#if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
        mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
               MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
 #else
@@ -741,9 +794,15 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
        mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
 
        /* set speed */
-       if (speed == _1000BASET)
+       if (speed == _1000BASET) {
+#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
+               unsigned long pfc1;
+               mfsdr (sdr_pfc1, pfc1);
+               pfc1 |= SDR0_PFC1_EM_1000;
+               mtsdr (sdr_pfc1, pfc1);
+#endif
                mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
-       else if (speed == _100BASET)
+       else if (speed == _100BASET)
                mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
        else
                mode_reg = mode_reg & ~0x00C00000;      /* 10 MBPS */
@@ -763,7 +822,7 @@ static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
 
        /* set receive  low/high water mark register */
 #if defined(CONFIG_440)
-       /* 440GP has a 64 byte burst length */
+       /* 440s has a 64 byte burst length */
        out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
 #else
        /* 405s have a 16 byte burst length */
@@ -868,8 +927,21 @@ static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
        }
 }
 
+
 #if defined (CONFIG_440)
 
+#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
+/*
+ * Hack: On 440SP all enet irq sources are located on UIC1
+ * Needs some cleanup. --sr
+ */
+#define UIC0MSR                uic1msr
+#define UIC0SR         uic1sr
+#else
+#define UIC0MSR                uic0msr
+#define UIC0SR         uic0sr
+#endif
+
 int enetInt (struct eth_device *dev)
 {
        int serviced;
@@ -896,20 +968,17 @@ int enetInt (struct eth_device *dev)
 
        hw_p = dev->priv;
 
-
        /* enter loop that stays in interrupt code until nothing to service */
        do {
                serviced = 0;
 
-               my_uic0msr = mfdcr (uic0msr);
+               my_uic0msr = mfdcr (UIC0MSR);
                my_uic1msr = mfdcr (uic1msr);
 #if defined(CONFIG_440GX)
                my_uic2msr = mfdcr (uic2msr);
 #endif
                if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
-                   && !(my_uic1msr &
-                        (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE |
-                         UIC_MRDE))) {
+                   && !(my_uic1msr & (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
                        /* not for us */
                        return (rc);
                }
@@ -947,12 +1016,13 @@ int enetInt (struct eth_device *dev)
                        }
                        if ((hw_p->emac_ier & emac_isr)
                            || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
-                               mtdcr (uic0sr, UIC_MRE | UIC_MTE);      /* Clear */
+                               mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
                                mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE);        /* Clear */
                                return (rc);    /* we had errors so get out */
                        }
                }
 
+#if !defined(CONFIG_440SP)
                if (hw_p->devnum == 1) {
                        if (UIC_ETH1 & my_uic1msr) {    /* look for EMAC errors */
                                emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
@@ -964,7 +1034,7 @@ int enetInt (struct eth_device *dev)
                        }
                        if ((hw_p->emac_ier & emac_isr)
                            || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
-                               mtdcr (uic0sr, UIC_MRE | UIC_MTE);      /* Clear */
+                               mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
                                mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE);        /* Clear */
                                return (rc);    /* we had errors so get out */
                        }
@@ -981,7 +1051,7 @@ int enetInt (struct eth_device *dev)
                        }
                        if ((hw_p->emac_ier & emac_isr)
                            || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
-                               mtdcr (uic0sr, UIC_MRE | UIC_MTE);      /* Clear */
+                               mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
                                mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
                                mtdcr (uic2sr, UIC_ETH2);
                                return (rc);    /* we had errors so get out */
@@ -999,18 +1069,20 @@ int enetInt (struct eth_device *dev)
                        }
                        if ((hw_p->emac_ier & emac_isr)
                            || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
-                               mtdcr (uic0sr, UIC_MRE | UIC_MTE);      /* Clear */
+                               mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
                                mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
                                mtdcr (uic2sr, UIC_ETH3);
                                return (rc);    /* we had errors so get out */
                        }
                }
 #endif /* CONFIG_440GX */
+#endif /* !CONFIG_440SP */
+
                /* handle MAX TX EOB interrupt from a tx */
                if (my_uic0msr & UIC_MTE) {
                        mal_rx_eob = mfdcr (maltxeobisr);
                        mtdcr (maltxeobisr, mal_rx_eob);
-                       mtdcr (uic0sr, UIC_MTE);
+                       mtdcr (UIC0SR, UIC_MTE);
                }
                /* handle MAL RX EOB  interupt from a receive */
                /* check for EOB on valid channels            */
@@ -1025,7 +1097,8 @@ int enetInt (struct eth_device *dev)
                                rc = 0;
                        }
                }
-               mtdcr (uic0sr, UIC_MRE);        /* Clear */
+
+               mtdcr (UIC0SR, UIC_MRE);        /* Clear */
                mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
                switch (hw_p->devnum) {
                case 0:
@@ -1329,23 +1402,22 @@ int ppc_4xx_eth_initialize (bd_t * bis)
 #endif
        /* set phy num and mode */
        bis->bi_phynum[0] = CONFIG_PHY_ADDR;
+       bis->bi_phymode[0] = 0;
+
 #if defined(CONFIG_PHY1_ADDR)
        bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
+       bis->bi_phymode[1] = 0;
 #endif
 #if defined(CONFIG_440GX)
        bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
        bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
-       bis->bi_phymode[0] = 0;
-       bis->bi_phymode[1] = 0;
        bis->bi_phymode[2] = 2;
        bis->bi_phymode[3] = 2;
 
-#if defined (CONFIG_440GX)
        ppc_4xx_eth_setup_bridge(0, bis);
 #endif
-#endif
 
-       for (eth_num = 0; eth_num < EMAC_NUM_DEV; eth_num++) {
+       for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
 
                /* See if we can actually bring up the interface, otherwise, skip it */
                switch (eth_num) {
@@ -1440,9 +1512,15 @@ int ppc_4xx_eth_initialize (bd_t * bis)
 
                if (0 == virgin) {
                        /* set the MAL IER ??? names may change with new spec ??? */
+#if defined(CONFIG_440SPE)
+                       mal_ier =
+                               MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
+                               MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
+#else
                        mal_ier =
                                MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
                                MAL_IER_OPBE | MAL_IER_PLBE;
+#endif
                        mtdcr (malesr, 0xffffffff);     /* clear pending interrupts */
                        mtdcr (maltxdeir, 0xffffffff);  /* clear pending interrupts */
                        mtdcr (malrxdeir, 0xffffffff);  /* clear pending interrupts */
@@ -1473,6 +1551,12 @@ int ppc_4xx_eth_initialize (bd_t * bis)
                emac0_dev = dev;
 #endif
 
+#if defined(CONFIG_NET_MULTI)
+#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
+               miiphy_register (dev->name,
+                                emac4xx_miiphy_read, emac4xx_miiphy_write);
+#endif
+#endif
        }                       /* end for each supported device */
        return (1);
 }
@@ -1507,6 +1591,16 @@ int eth_rx(void)
 {
        return (ppc_4xx_eth_rx(emac0_dev));
 }
+
+int emac4xx_miiphy_initialize (bd_t * bis)
+{
+#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
+       miiphy_register ("ppc_4xx_eth0",
+                        emac4xx_miiphy_read, emac4xx_miiphy_write);
+#endif
+
+       return 0;
+}
 #endif /* !defined(CONFIG_NET_MULTI) */
 
 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */