]> git.sur5r.net Git - u-boot/blobdiff - cpu/ppc4xx/sdram.c
Change initdram() return type to phys_size_t
[u-boot] / cpu / ppc4xx / sdram.c
index 7c931f03988ac6c1dd7bf61d052e240e6aa2cd6c..7d60ad667fd7353dfe950b898ba16cfb1634ff54 100644 (file)
@@ -1,5 +1,11 @@
 /*
- * (C) Copyright 2002
+ * (C) Copyright 2005-2007
+ * Stefan Roese, DENX Software Engineering, sr@denx.de.
+ *
+ * (C) Copyright 2006
+ * DAVE Srl <www.dave-tech.it>
+ *
+ * (C) Copyright 2002-2004
  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  *
  * See file CREDITS for list of people who contributed to this
@@ -12,7 +18,7 @@
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
 #include <common.h>
 #include <ppc4xx.h>
 #include <asm/processor.h>
-#include <pci.h>
-
+#include "sdram.h"
+#include "ecc.h"
 
 #ifdef CONFIG_SDRAM_BANK0
 
+#ifndef CONFIG_440
 
-#define MAGIC0 0x00000000
-#define MAGIC1 0x11111111
-#define MAGIC2 0x22222222
-#define MAGIC3 0x33333333
-#define MAGIC4 0x44444444
-#define MAGIC5 0x55555555
-#define MAGIC6 0x66666666
-
-#define ADDR_ZERO 0x00000000
-#define ADDR_400  0x00000400
-#define ADDR_01MB 0x00100000
-#define ADDR_08MB 0x00800000
-#define ADDR_16MB 0x01000000
-#define ADDR_32MB 0x02000000
-#define ADDR_64MB 0x04000000
-#define ADDR_128MB 0x08000000
+#ifndef CFG_SDRAM_TABLE
+sdram_conf_t mb0cf[] = {
+       {(128 << 20), 13, 0x000A4001},      /* (0-128MB) Address Mode 3, 13x10(4) */
+       {(64 << 20),  13, 0x00084001},      /* (0-64MB) Address Mode 3, 13x9(4)   */
+       {(32 << 20),  12, 0x00062001},      /* (0-32MB) Address Mode 2, 12x9(4)   */
+       {(16 << 20),  12, 0x00046001},      /* (0-16MB) Address Mode 4, 12x8(4)   */
+       {(4 << 20),   11, 0x00008001},      /* (0-4MB) Address Mode 5, 11x8(2)    */
+};
+#else
+sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE;
+#endif
 
-#define mtsdram0(reg, data)  mtdcr(memcfga,reg);mtdcr(memcfgd,data)
+#define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
 
-
-/*-----------------------------------------------------------------------
- */
-void sdram_init(void)
+#ifdef CFG_SDRAM_CASL
+static ulong ns2clks(ulong ns)
 {
-       ulong speed;
-       ulong sdtr1;
-       ulong rtr;
+       ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10);
 
-       /*
-        * Determine SDRAM speed
-        */
-       speed = get_bus_freq(0); /* parameter not used on ppc4xx */
+       return ((ns * 10) + bus_period_x_10) / bus_period_x_10;
+}
+#endif /* CFG_SDRAM_CASL */
 
-       /*
-        * Support for 100MHz and 133MHz SDRAM
+static ulong compute_sdtr1(ulong speed)
+{
+#ifdef CFG_SDRAM_CASL
+       ulong tmp;
+       ulong sdtr1 = 0;
+
+       /* CASL */
+       if (CFG_SDRAM_CASL < 2)
+               sdtr1 |= (1 << SDRAM0_TR_CASL);
+       else
+               if (CFG_SDRAM_CASL > 4)
+                       sdtr1 |= (3 << SDRAM0_TR_CASL);
+               else
+                       sdtr1 |= ((CFG_SDRAM_CASL-1) << SDRAM0_TR_CASL);
+
+       /* PTA */
+       tmp = ns2clks(CFG_SDRAM_PTA);
+       if ((tmp >= 2) && (tmp <= 4))
+               sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA);
+       else
+               sdtr1 |= ((4-1) << SDRAM0_TR_PTA);
+
+       /* CTP */
+       tmp = ns2clks(CFG_SDRAM_CTP);
+       if ((tmp >= 2) && (tmp <= 4))
+               sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP);
+       else
+               sdtr1 |= ((4-1) << SDRAM0_TR_CTP);
+
+       /* LDF */
+       tmp = ns2clks(CFG_SDRAM_LDF);
+       if ((tmp >= 2) && (tmp <= 4))
+               sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF);
+       else
+               sdtr1 |= ((2-1) << SDRAM0_TR_LDF);
+
+       /* RFTA */
+       tmp = ns2clks(CFG_SDRAM_RFTA);
+       if ((tmp >= 4) && (tmp <= 10))
+               sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA);
+       else
+               sdtr1 |= ((10-4) << SDRAM0_TR_RFTA);
+
+       /* RCD */
+       tmp = ns2clks(CFG_SDRAM_RCD);
+       if ((tmp >= 2) && (tmp <= 4))
+               sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD);
+       else
+               sdtr1 |= ((4-1) << SDRAM0_TR_RCD);
+
+       return sdtr1;
+#else /* CFG_SDRAM_CASL */
+       /*
+        * If no values are configured in the board config file
+        * use the default values, which seem to be ok for most
+        * boards.
+        *
+        * REMARK:
+        * For new board ports we strongly recommend to define the
+        * correct values for the used SDRAM chips in your board
+        * config file (see PPChameleonEVB.h)
         */
        if (speed > 100000000) {
                /*
                 * 133 MHz SDRAM
                 */
-               sdtr1 = 0x01074015;
-               rtr = 0x07f00000;
+               return 0x01074015;
        } else {
                /*
                 * default: 100 MHz SDRAM
                 */
-               sdtr1 = 0x0086400d;
-               rtr = 0x05f00000;
+               return 0x0086400d;
        }
+#endif /* CFG_SDRAM_CASL */
+}
 
-       /*
-        * Disable memory controller.
-        */
-       mtsdram0(mem_mcopt1, 0x00000000);
-
-       /*
-        * Set MB0CF for bank 0. (0-128MB) Address Mode 3 since 13x10(4)
-        */
-       mtsdram0(mem_mb0cf, 0x000A4001);
-
-       mtsdram0(mem_sdtr1, sdtr1);
-       mtsdram0(mem_rtr, rtr);
-
-       /*
-        * Wait for 200us
-        */
-       udelay(200);
+/* refresh is expressed in ms */
+static ulong compute_rtr(ulong speed, ulong rows, ulong refresh)
+{
+#ifdef CFG_SDRAM_CASL
+       ulong tmp;
 
-       /*
-        * Set memory controller options reg, MCOPT1.
-        * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
-        * read/prefetch.
-        */
-       mtsdram0(mem_mcopt1, 0x80800000);
+       tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000);
+       tmp /= 1000000;
 
-       /*
-        * Wait for 10ms
-        */
-       udelay(10000);
-
-       /*
-        * Test if 128 MByte are equipped (mirror test)
-        */
-       *(volatile ulong *)ADDR_ZERO = MAGIC0;
-       *(volatile ulong *)ADDR_08MB = MAGIC1;
-       *(volatile ulong *)ADDR_16MB = MAGIC2;
-       *(volatile ulong *)ADDR_32MB = MAGIC3;
-       *(volatile ulong *)ADDR_64MB = MAGIC4;
-
-       if ((*(volatile ulong *)ADDR_ZERO == MAGIC0) &&
-           (*(volatile ulong *)ADDR_08MB == MAGIC1) &&
-           (*(volatile ulong *)ADDR_16MB == MAGIC2) &&
-           (*(volatile ulong *)ADDR_32MB == MAGIC3)) {
+       return ((tmp & 0x00003FF8) << 16);
+#else /* CFG_SDRAM_CASL */
+       if (speed > 100000000) {
                /*
-                * OK, 128MB detected -> all done
+                * 133 MHz SDRAM
                 */
-               return;
+               return 0x07f00000;
+       } else {
+               /*
+                * default: 100 MHz SDRAM
+                */
+               return 0x05f00000;
        }
+#endif /* CFG_SDRAM_CASL */
+}
 
-       /*
-        * Now test for 64 MByte...
-        */
+/*
+ * Autodetect onboard SDRAM on 405 platforms
+ */
+phys_size_t initdram(int board_type)
+{
+       ulong speed;
+       ulong sdtr1;
+       int i;
 
        /*
-        * Disable memory controller.
+        * Determine SDRAM speed
         */
-       mtsdram0(mem_mcopt1, 0x00000000);
+       speed = get_bus_freq(0); /* parameter not used on ppc4xx */
 
        /*
-        * Set MB0CF for bank 0. (0-64MB) Address Mode 3 since 13x9(4)
+        * sdtr1 (register SDRAM0_TR) must take into account timings listed
+        * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into
+        * account actual SDRAM size. So we can set up sdtr1 according to what
+        * is specified in board configuration file while rtr dependds on SDRAM
+        * size we are assuming before detection.
         */
-       mtsdram0(mem_mb0cf, 0x00084001);
+       sdtr1 = compute_sdtr1(speed);
 
-       mtsdram0(mem_sdtr1, sdtr1);
-       mtsdram0(mem_rtr, rtr);
-
-       /*
-        * Wait for 200us
-        */
-       udelay(200);
+       for (i=0; i<N_MB0CF; i++) {
+               /*
+                * Disable memory controller.
+                */
+               mtsdram(mem_mcopt1, 0x00000000);
 
-       /*
-        * Set memory controller options reg, MCOPT1.
-        * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
-        * read/prefetch.
-        */
-       mtsdram0(mem_mcopt1, 0x80800000);
+               /*
+                * Set MB0CF for bank 0.
+                */
+               mtsdram(mem_mb0cf, mb0cf[i].reg);
+               mtsdram(mem_sdtr1, sdtr1);
+               mtsdram(mem_rtr, compute_rtr(speed, mb0cf[i].rows, 64));
 
-       /*
-        * Wait for 10ms
-        */
-       udelay(10000);
+               udelay(200);
 
-       /*
-        * Test if 64 MByte are equipped (mirror test)
-        */
-       *(volatile ulong *)ADDR_ZERO = MAGIC0;
-       *(volatile ulong *)ADDR_08MB = MAGIC1;
-       *(volatile ulong *)ADDR_16MB = MAGIC2;
-       *(volatile ulong *)ADDR_32MB = MAGIC3;
-
-       if ((*(volatile ulong *)ADDR_ZERO == MAGIC0) &&
-           (*(volatile ulong *)ADDR_08MB == MAGIC1) &&
-           (*(volatile ulong *)ADDR_16MB == MAGIC2)) {
                /*
-                * OK, 64MB detected -> all done
+                * Set memory controller options reg, MCOPT1.
+                * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
+                * read/prefetch.
                 */
-               return;
+               mtsdram(mem_mcopt1, 0x80800000);
+
+               udelay(10000);
+
+               if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
+                       /*
+                        * OK, size detected.  Enable second bank if
+                        * defined (assumes same type as bank 0)
+                        */
+#ifdef CONFIG_SDRAM_BANK1
+                       u32 b1cr = mb0cf[i].size | mb0cf[i].reg;
+
+                       mtsdram(mem_mcopt1, 0x00000000);
+                       mtsdram(mem_mb1cf, b1cr); /* SDRAM0_B1CR */
+                       mtsdram(mem_mcopt1, 0x80800000);
+                       udelay(10000);
+
+                       /*
+                        * Check if 2nd bank is really available.
+                        * If the size not equal to the size of the first
+                        * bank, then disable the 2nd bank completely.
+                        */
+                       if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) !=
+                           mb0cf[i].size) {
+                               mtsdram(mem_mb1cf, 0);
+                               mtsdram(mem_mcopt1, 0);
+                       }
+#endif
+
+                       /*
+                        * OK, size detected -> all done
+                        */
+                       return mb0cf[i].size;
+               }
        }
 
-       /*
-        * Now test for 32 MByte...
-        */
+       return 0;
+}
 
-       /*
-        * Disable memory controller.
-        */
-       mtsdram0(mem_mcopt1, 0x00000000);
+#else /* CONFIG_440 */
 
-       /*
-        * Set MB0CF for bank 0. (0-32MB) Address Mode 2 since 12x9(4)
-        */
-       mtsdram0(mem_mb0cf, 0x00062001);
+/*
+ * Define some default values. Those can be overwritten in the
+ * board config file.
+ */
 
-       /*
-        * Set memory controller options reg, MCOPT1.
-        * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
-        * read/prefetch.
-        */
-       mtsdram0(mem_mcopt1, 0x80800000);
+#ifndef CFG_SDRAM_TABLE
+sdram_conf_t mb0cf[] = {
+       {(256 << 20), 13, 0x000C4001},  /* 256MB mode 3, 13x10(4)       */
+       {(64 << 20),  12, 0x00082001}   /* 64MB mode 2, 12x9(4)         */
+};
+#else
+sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE;
+#endif
 
-       /*
-        * Wait for 10ms
-        */
-       udelay(10000);
+#ifndef CFG_SDRAM0_TR0
+#define        CFG_SDRAM0_TR0          0x41094012
+#endif
 
-       /*
-        * Test if 32 MByte are equipped (mirror test)
-        */
-       *(volatile ulong *)ADDR_ZERO = MAGIC0;
-       *(volatile ulong *)ADDR_400  = MAGIC1;
-       *(volatile ulong *)ADDR_08MB = MAGIC2;
-       *(volatile ulong *)ADDR_16MB = MAGIC3;
-
-       if ((*(volatile ulong *)ADDR_ZERO == MAGIC0) &&
-           (*(volatile ulong *)ADDR_400  == MAGIC1) &&
-           (*(volatile ulong *)ADDR_08MB == MAGIC2)) {
-               /*
-                * OK, 32MB detected -> all done
-                */
-               return;
-       }
+#define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
 
-       /*
-        * Now test for 16 MByte...
-        */
-       /*
-        * Disable memory controller.
-        */
-       mtsdram0(mem_mcopt1, 0x00000000);
+#define NUM_TRIES 64
+#define NUM_READS 10
 
-       /*
-        * Set MB0CF for bank 0. (0-16MB) Address Mode 4 since 12x8(4)
-        */
-       mtsdram0(mem_mb0cf, 0x00046001);
+static void sdram_tr1_set(int ram_address, int* tr1_value)
+{
+       int i;
+       int j, k;
+       volatile unsigned int* ram_pointer = (unsigned int *)ram_address;
+       int first_good = -1, last_bad = 0x1ff;
+
+       unsigned long test[NUM_TRIES] = {
+               0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
+               0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
+               0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
+               0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
+               0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
+               0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
+               0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
+               0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
+               0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
+               0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
+               0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
+               0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
+               0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
+               0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
+               0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
+               0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
+
+       /* go through all possible SDRAM0_TR1[RDCT] values */
+       for (i=0; i<=0x1ff; i++) {
+               /* set the current value for TR1 */
+               mtsdram(mem_tr1, (0x80800800 | i));
+
+               /* write values */
+               for (j=0; j<NUM_TRIES; j++) {
+                       ram_pointer[j] = test[j];
+
+                       /* clear any cache at ram location */
+                       __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
+               }
+
+               /* read values back */
+               for (j=0; j<NUM_TRIES; j++) {
+                       for (k=0; k<NUM_READS; k++) {
+                               /* clear any cache at ram location */
+                               __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
+
+                               if (ram_pointer[j] != test[j])
+                                       break;
+                       }
+
+                       /* read error */
+                       if (k != NUM_READS)
+                               break;
+               }
+
+               /* we have a SDRAM0_TR1[RDCT] that is part of the window */
+               if (j == NUM_TRIES) {
+                       if (first_good == -1)
+                               first_good = i;         /* found beginning of window */
+               } else { /* bad read */
+                       /* if we have not had a good read then don't care */
+                       if (first_good != -1) {
+                               /* first failure after a good read */
+                               last_bad = i-1;
+                               break;
+                       }
+               }
+       }
 
-       /*
-        * Set memory controller options reg, MCOPT1.
-        * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
-        * read/prefetch.
-        */
-       mtsdram0(mem_mcopt1, 0x80800000);
+       /* return the current value for TR1 */
+       *tr1_value = (first_good + last_bad) / 2;
+}
 
-       /*
-        * Wait for 10ms
-        */
-       udelay(10000);
+/*
+ * Autodetect onboard DDR SDRAM on 440 platforms
+ *
+ * NOTE: Some of the hardcoded values are hardware dependant,
+ *      so this should be extended for other future boards
+ *      using this routine!
+ */
+phys_size_t initdram(int board_type)
+{
+       int i;
+       int tr1_bank1;
 
+#if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
+    defined(CONFIG_440GR) || defined(CONFIG_440SP)
        /*
-        * Test if 16 MByte are equipped (mirror test)
+        * Soft-reset SDRAM controller.
         */
-       *(volatile ulong *)ADDR_ZERO = MAGIC0;
-       *(volatile ulong *)ADDR_400  = MAGIC1;
-       *(volatile ulong *)ADDR_01MB = MAGIC5;
-       *(volatile ulong *)ADDR_08MB = MAGIC2;
-/*     *(volatile ulong *)ADDR_16MB = MAGIC3;*/
-
-       if ((*(volatile ulong *)ADDR_ZERO == MAGIC0) &&
-           (*(volatile ulong *)ADDR_400  == MAGIC1) &&
-           (*(volatile ulong *)ADDR_01MB == MAGIC5) &&
-           (*(volatile ulong *)ADDR_08MB == MAGIC2)) {
+       mtsdr(sdr_srst, SDR0_SRST_DMC);
+       mtsdr(sdr_srst, 0x00000000);
+#endif
+
+       for (i=0; i<N_MB0CF; i++) {
                /*
-                * OK, 16MB detected -> all done
+                * Disable memory controller.
                 */
-               return;
-       }
+               mtsdram(mem_cfg0, 0x00000000);
 
-       /*
-        * Setup for 4 MByte...
-        */
-
-       /*
-        * Disable memory controller.
-        */
-       mtsdram0(mem_mcopt1, 0x00000000);
+               /*
+                * Setup some default
+                */
+               mtsdram(mem_uabba, 0x00000000); /* ubba=0 (default)             */
+               mtsdram(mem_slio, 0x00000000);  /* rdre=0 wrre=0 rarw=0         */
+               mtsdram(mem_devopt, 0x00000000); /* dll=0 ds=0 (normal)         */
+               mtsdram(mem_wddctr, 0x00000000); /* wrcp=0 dcd=0                */
+               mtsdram(mem_clktr, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0    */
 
-       /*
-        * Set MB0CF for bank 0. (0-4MB) Address Mode 5 since 11x8(2)
-        */
-       mtsdram0(mem_mb0cf, 0x00008001);
+               /*
+                * Following for CAS Latency = 2.5 @ 133 MHz PLB
+                */
+               mtsdram(mem_b0cr, mb0cf[i].reg);
+               mtsdram(mem_tr0, CFG_SDRAM0_TR0);
+               mtsdram(mem_tr1, 0x80800800);   /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/
+               mtsdram(mem_rtr, 0x04100000);   /* Interval 7.8µs @ 133MHz PLB  */
+               mtsdram(mem_cfg1, 0x00000000);  /* Self-refresh exit, disable PM*/
+               udelay(400);                    /* Delay 200 usecs (min)        */
 
-       /*
-        * Set memory controller options reg, MCOPT1.
-        * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
-        * read/prefetch.
-        */
-       mtsdram0(mem_mcopt1, 0x80800000);
+               /*
+                * Enable the controller, then wait for DCEN to complete
+                */
+               mtsdram(mem_cfg0, 0x82000000);  /* DCEN=1, PMUD=0, 64-bit       */
+               udelay(10000);
+
+               if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
+                       /*
+                        * Optimize TR1 to current hardware environment
+                        */
+                       sdram_tr1_set(0x00000000, &tr1_bank1);
+                       mtsdram(mem_tr1, (tr1_bank1 | 0x80800800));
+
+#ifdef CONFIG_SDRAM_ECC
+                       ecc_init(0, mb0cf[i].size);
+#endif
+
+                       /*
+                        * OK, size detected -> all done
+                        */
+                       return mb0cf[i].size;
+               }
+       }
 
-       /*
-        * Wait for 10ms
-        */
-       udelay(10000);
+       return 0;                               /* nothing found !              */
 }
 
+#endif /* CONFIG_440 */
+
 #endif /* CONFIG_SDRAM_BANK0 */