ppc4xx: Consolidate pci_master_init() function
[oweals/u-boot.git] / cpu / mpc83xx / spd_sdram.c
index ee2d0385e457b6bcc8b720024aba63e3b8ef5096..0f611804a0712e1b41db6e2b5da35b3d3d0293ee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) Copyright 2006 Freescale Semiconductor, Inc.
+ * (C) Copyright 2006-2007 Freescale Semiconductor, Inc.
  *
  * (C) Copyright 2006
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 #include <asm/mmu.h>
 #include <spd_sdram.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 void board_add_ram_info(int use_default)
 {
-       volatile immap_t *immap = (immap_t *) CFG_IMMR;
+       volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
        volatile ddr83xx_t *ddr = &immap->ddr;
+       char buf[32];
 
        printf(" (DDR%d", ((ddr->sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK)
                           >> SDRAM_CFG_SDRAM_TYPE_SHIFT) - 1);
@@ -48,28 +51,21 @@ void board_add_ram_info(int use_default)
                puts(", 64-bit");
 
        if (ddr->sdram_cfg & SDRAM_CFG_ECC_EN)
-               puts(", ECC on)");
+               puts(", ECC on");
        else
-               puts(", ECC off)");
+               puts(", ECC off");
 
-#if defined(CFG_LB_SDRAM) && defined(CFG_LBC_SDRAM_SIZE)
+       printf(", %s MHz)", strmhz(buf, gd->mem_clk));
+
+#if defined(CONFIG_SYS_LB_SDRAM) && defined(CONFIG_SYS_LBC_SDRAM_SIZE)
        puts("\nSDRAM: ");
-       print_size (CFG_LBC_SDRAM_SIZE * 1024 * 1024, " (local bus)");
+       print_size (CONFIG_SYS_LBC_SDRAM_SIZE * 1024 * 1024, " (local bus)");
 #endif
 }
 
 #ifdef CONFIG_SPD_EEPROM
-
-DECLARE_GLOBAL_DATA_PTR;
-
-#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
-extern void dma_init(void);
-extern uint dma_check(void);
-extern int dma_xfer(void *dest, uint count, void *src);
-#endif
-
-#ifndef        CFG_READ_SPD
-#define CFG_READ_SPD   i2c_read
+#ifndef        CONFIG_SYS_READ_SPD
+#define CONFIG_SYS_READ_SPD    i2c_read
 #endif
 
 /*
@@ -78,12 +74,12 @@ extern int dma_xfer(void *dest, uint count, void *src);
 int
 picos_to_clk(int picos)
 {
-       unsigned int ddr_bus_clk;
+       unsigned int mem_bus_clk;
        int clks;
 
-       ddr_bus_clk = gd->ddr_clk >> 1;
-       clks = picos / (1000000000 / (ddr_bus_clk / 1000));
-       if (picos % (1000000000 / (ddr_bus_clk / 1000)) != 0)
+       mem_bus_clk = gd->mem_clk >> 1;
+       clks = picos / (1000000000 / (mem_bus_clk / 1000));
+       if (picos % (1000000000 / (mem_bus_clk / 1000)) != 0)
                clks++;
 
        return clks;
@@ -126,7 +122,7 @@ static void spd_debug(spd_eeprom_t *spd)
 
 long int spd_sdram()
 {
-       volatile immap_t *immap = (immap_t *)CFG_IMMR;
+       volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
        volatile ddr83xx_t *ddr = &immap->ddr;
        volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
        spd_eeprom_t spd;
@@ -155,7 +151,7 @@ long int spd_sdram()
        unsigned int pvr = get_pvr();
 
        /* Read SPD parameters with I2C */
-       CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
+       CONFIG_SYS_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
 #ifdef SPD_DEBUG
        spd_debug(&spd);
 #endif
@@ -191,13 +187,14 @@ long int spd_sdram()
                return 0;
        }
 
-#ifdef CFG_DDRCDR_VALUE
+#ifdef CONFIG_SYS_DDRCDR_VALUE
        /*
         * Adjust DDR II IO voltage biasing.  It just makes it work.
         */
        if(spd.mem_type == SPD_MEMTYPE_DDR2) {
-               immap->sysconf.ddrcdr = CFG_DDRCDR_VALUE;
+               immap->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
        }
+       udelay(50000);
 #endif
 
        /*
@@ -210,12 +207,13 @@ long int spd_sdram()
        }
 
        /* Setup DDR chip select register */
-#ifdef CFG_83XX_DDR_USES_CS0
+#ifdef CONFIG_SYS_83XX_DDR_USES_CS0
        ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
        ddr->cs_config[0] = ( 1 << 31
                            | (odt_rd_cfg << 20)
                            | (odt_wr_cfg << 16)
-                           | (spd.nrow_addr - 12) << 8
+                           | ((spd.nbanks == 8 ? 1 : 0) << 14)
+                           | ((spd.nrow_addr - 12) << 8)
                            | (spd.ncol_addr - 8) );
        debug("\n");
        debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
@@ -227,8 +225,9 @@ long int spd_sdram()
                ddr->cs_config[1] = ( 1<<31
                                    | (odt_rd_cfg << 20)
                                    | (odt_wr_cfg << 16)
-                                   | (spd.nrow_addr-12) << 8
-                                   | (spd.ncol_addr-8) );
+                                   | ((spd.nbanks == 8 ? 1 : 0) << 14)
+                                   | ((spd.nrow_addr - 12) << 8)
+                                   | (spd.ncol_addr - 8) );
                debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
                debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
        }
@@ -238,7 +237,8 @@ long int spd_sdram()
        ddr->cs_config[2] = ( 1 << 31
                            | (odt_rd_cfg << 20)
                            | (odt_wr_cfg << 16)
-                           | (spd.nrow_addr - 12) << 8
+                           | ((spd.nbanks == 8 ? 1 : 0) << 14)
+                           | ((spd.nrow_addr - 12) << 8)
                            | (spd.ncol_addr - 8) );
        debug("\n");
        debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
@@ -250,8 +250,9 @@ long int spd_sdram()
                ddr->cs_config[3] = ( 1<<31
                                    | (odt_rd_cfg << 20)
                                    | (odt_wr_cfg << 16)
-                                   | (spd.nrow_addr-12) << 8
-                                   | (spd.ncol_addr-8) );
+                                   | ((spd.nbanks == 8 ? 1 : 0) << 14)
+                                   | ((spd.nrow_addr - 12) << 8)
+                                   | (spd.ncol_addr - 8) );
                debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
                debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
        }
@@ -270,7 +271,7 @@ long int spd_sdram()
        /*
         * Set up LAWBAR for all of DDR.
         */
-       ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
+       ecm->bar = CONFIG_SYS_DDR_SDRAM_BASE & 0xfffff000;
        ecm->ar  = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
        debug("DDR:bar=0x%08x\n", ecm->bar);
        debug("DDR:ar=0x%08x\n", ecm->ar);
@@ -310,12 +311,25 @@ long int spd_sdram()
                        + (spd.clk_cycle & 0x0f));
        max_data_rate = max_bus_clk * 2;
 
-       debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
+       debug("DDR:Module maximum data rate is: %d MHz\n", max_data_rate);
 
-       ddrc_clk = gd->ddr_clk / 1000000;
+       ddrc_clk = gd->mem_clk / 1000000;
        effective_data_rate = 0;
 
-       if (max_data_rate >= 390 && max_data_rate < 460) { /* it is DDR 400 */
+       if (max_data_rate >= 460) { /* it is DDR2-800, 667, 533 */
+               if (spd.cas_lat & 0x08)
+                       caslat = 3;
+               else
+                       caslat = 4;
+               if (ddrc_clk <= 460 && ddrc_clk > 350)
+                       effective_data_rate = 400;
+               else if (ddrc_clk <=350 && ddrc_clk > 280)
+                       effective_data_rate = 333;
+               else if (ddrc_clk <= 280 && ddrc_clk > 230)
+                       effective_data_rate = 266;
+               else
+                       effective_data_rate = 200;
+       } else if (max_data_rate >= 390 && max_data_rate < 460) { /* it is DDR 400 */
                if (ddrc_clk <= 460 && ddrc_clk > 350) {
                        /* DDR controller clk at 350~460 */
                        effective_data_rate = 400; /* 5ns */
@@ -397,7 +411,7 @@ long int spd_sdram()
                }
        }
 
-       debug("DDR:Effective data rate is: %dMhz\n", effective_data_rate);
+       debug("DDR:Effective data rate is: %dMHz\n", effective_data_rate);
        debug("DDR:The MSB 1 of CAS Latency is: %d\n", caslat);
 
        /*
@@ -462,6 +476,8 @@ long int spd_sdram()
        } else {
                twr_clk = picos_to_clk(spd.twr * 250);
                twtr_clk = picos_to_clk(spd.twtr * 250);
+               if (twtr_clk < 2)
+                       twtr_clk = 2;
        }
 
        /*
@@ -506,7 +522,7 @@ long int spd_sdram()
        ddr->timing_cfg_1 =
            (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |    /* PRETOACT */
             ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) | /* ACTTOPRE */
-            (trcd_clk << 20 ) |                                /* ACTTORW */
+            (trcd_clk << 20 ) |                                /* ACTTORW */
             (caslat_ctrl << 16 ) |                             /* CASLAT */
             (trfc_low << 12 ) |                                /* REFEC */
             ((twr_clk & 0x07) << 8) |                          /* WRRREC */
@@ -525,7 +541,7 @@ long int spd_sdram()
        if (spd.mem_type == SPD_MEMTYPE_DDR2
            && (odt_wr_cfg || odt_rd_cfg)
            && (caslat < 4)) {
-               add_lat = trcd_clk - 1;
+               add_lat = 4 - caslat;
                if ((add_lat + caslat) < 4) {
                        add_lat = 0;
                }
@@ -562,6 +578,9 @@ long int spd_sdram()
 
                /* Convert SPD value from quarter nanos to picos. */
                trtp_clk = picos_to_clk(spd.trtp * 250);
+               if (trtp_clk < 2)
+                       trtp_clk = 2;
+               trtp_clk += add_lat;
 
                cke_min_clk = 3;        /* By the book. */
                four_act = picos_to_clk(37500); /* By the book. 1k pages? */
@@ -573,10 +592,12 @@ long int spd_sdram()
         */
        cpo = 0;
        if (spd.mem_type == SPD_MEMTYPE_DDR2) {
-               if (effective_data_rate == 266 || effective_data_rate == 333) {
-                       cpo = 0x7;              /* READ_LAT + 5/4 */
+               if (effective_data_rate == 266) {
+                       cpo = 0x4;              /* READ_LAT + 1/2 */
+               } else if (effective_data_rate == 333) {
+                       cpo = 0x6;              /* READ_LAT + 1 */
                } else if (effective_data_rate == 400) {
-                       cpo = 0x9;              /* READ_LAT + 7/4 */
+                       cpo = 0x7;              /* READ_LAT + 5/4 */
                } else {
                        /* Automatic calibration */
                        cpo = 0x1f;
@@ -597,7 +618,7 @@ long int spd_sdram()
        debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
 
        /* Check DIMM data bus width */
-       if (spd.dataw_lsb == 0x20) {
+       if (spd.dataw_lsb < 64) {
                if (spd.mem_type == SPD_MEMTYPE_DDR)
                        burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
                else
@@ -705,9 +726,11 @@ long int spd_sdram()
         * SDRAM Cfg 2
         */
        odt_cfg = 0;
+#ifndef CONFIG_NEVER_ASSERT_ODT_TO_CPU
        if (odt_rd_cfg | odt_wr_cfg) {
                odt_cfg = 0x2;          /* ODT to IOs during reads */
        }
+#endif
        if (spd.mem_type == SPD_MEMTYPE_DDR2) {
                ddr->sdram_cfg2 = (0
                            | (0 << 26) /* True DQS */
@@ -718,8 +741,8 @@ long int spd_sdram()
                debug("DDR: sdram_cfg2  = 0x%08x\n", ddr->sdram_cfg2);
        }
 
-#ifdef CFG_DDR_SDRAM_CLK_CNTL  /* Optional platform specific value */
-       ddr->sdram_clk_cntl = CFG_DDR_SDRAM_CLK_CNTL;
+#ifdef CONFIG_SYS_DDR_SDRAM_CLK_CNTL   /* Optional platform specific value */
+       ddr->sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
 #endif
        debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
 
@@ -757,7 +780,7 @@ long int spd_sdram()
                sdram_cfg |= SDRAM_CFG_RD_EN;
 
        /* The DIMM is 32bit width */
-       if (spd.dataw_lsb == 0x20) {
+       if (spd.dataw_lsb < 64) {
                if (spd.mem_type == SPD_MEMTYPE_DDR)
                        sdram_cfg |= SDRAM_CFG_32_BE | SDRAM_CFG_8_BE;
                if (spd.mem_type == SPD_MEMTYPE_DDR2)
@@ -800,7 +823,7 @@ long int spd_sdram()
 }
 #endif /* CONFIG_SPD_EEPROM */
 
-#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
+#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
 /*
  * Use timebase counter, get_timer() is not availabe
  * at this point of initialization yet.
@@ -833,55 +856,29 @@ static __inline__ unsigned long get_tbms (void)
 /*
  * Initialize all of memory for ECC, then enable errors.
  */
-/* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
 void ddr_enable_ecc(unsigned int dram_size)
 {
-       volatile immap_t *immap = (immap_t *)CFG_IMMR;
+       volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
        volatile ddr83xx_t *ddr= &immap->ddr;
        unsigned long t_start, t_end;
        register u64 *p;
        register uint size;
        unsigned int pattern[2];
-#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
-       uint i;
-#endif
+
        icache_enable();
        t_start = get_tbms();
        pattern[0] = 0xdeadbeef;
        pattern[1] = 0xdeadbeef;
 
-#if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
+#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
+       dma_meminit(pattern[0], dram_size);
+#else
        debug("ddr init: CPU FP write method\n");
        size = dram_size;
        for (p = 0; p < (u64*)(size); p++) {
                ppcDWstore((u32*)p, pattern);
        }
        __asm__ __volatile__ ("sync");
-#else
-       debug("ddr init: DMA method\n");
-       size = 0x2000;
-       for (p = 0; p < (u64*)(size); p++) {
-               ppcDWstore((u32*)p, pattern);
-       }
-       __asm__ __volatile__ ("sync");
-
-       /* Initialise DMA for direct transfer */
-       dma_init();
-       /* Start DMA to transfer */
-       dma_xfer((uint *)0x2000, 0x2000, (uint *)0); /* 8K */
-       dma_xfer((uint *)0x4000, 0x4000, (uint *)0); /* 16K */
-       dma_xfer((uint *)0x8000, 0x8000, (uint *)0); /* 32K */
-       dma_xfer((uint *)0x10000, 0x10000, (uint *)0); /* 64K */
-       dma_xfer((uint *)0x20000, 0x20000, (uint *)0); /* 128K */
-       dma_xfer((uint *)0x40000, 0x40000, (uint *)0); /* 256K */
-       dma_xfer((uint *)0x80000, 0x80000, (uint *)0); /* 512K */
-       dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
-       dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
-       dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
-
-       for (i = 1; i < dram_size / 0x800000; i++) {
-               dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
-       }
 #endif
 
        t_end = get_tbms();