Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / northbridge / amd / lx / raminit.c
index a8555823f6d7af713157ee62f4065d060ac82a82..9014fb1de3d6fc976210680540aaa43a3f83eec9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This file is part of the LinuxBIOS project.
+ * This file is part of the coreboot project.
  *
  * Copyright (C) 2007 Advanced Micro Devices, Inc.
  *
 #include <spd.h>
 #include "southbridge/amd/cs5536/cs5536.h"
 
-static const unsigned char NumColAddr[] = { 
-       0x00, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x07, 
-       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F 
+static const unsigned char NumColAddr[] = {
+       0x00, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x07,
+       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
 };
 
+static void banner(const char *s)
+{
+       printk(BIOS_DEBUG, " * %s\n", s);
+}
+
+static void hcf(void)
+{
+       print_emerg("DIE\n");
+       /* this guarantees we flush the UART fifos (if any) and also
+        * ensures that things, in general, keep going so no debug output
+        * is lost
+        */
+       while (1)
+               print_emerg_char(0);
+}
+
 static void auto_size_dimm(unsigned int dimm)
 {
        uint32_t dimm_setting;
@@ -36,33 +52,37 @@ static void auto_size_dimm(unsigned int dimm)
 
        dimm_setting = 0;
 
+       banner("Check present");
        /* Check that we have a dimm */
        if (spd_read_byte(dimm, SPD_MEMORY_TYPE) == 0xFF) {
                return;
        }
 
+       banner("MODBANKS");
        /* Field: Module Banks per DIMM */
        /* EEPROM byte usage: (5) Number of DIMM Banks */
        spd_byte = spd_read_byte(dimm, SPD_NUM_DIMM_BANKS);
-       if ((MIN_MOD_BANKS > spd_byte) && (spd_byte > MAX_MOD_BANKS)) {
-               print_debug("Number of module banks not compatible\n");
-               POST_CODE(ERROR_BANK_SET);
-               __asm__ __volatile__("hlt\n");
+       if ((MIN_MOD_BANKS > spd_byte) || (spd_byte > MAX_MOD_BANKS)) {
+               print_emerg("Number of module banks not compatible\n");
+               post_code(ERROR_BANK_SET);
+               hcf();
        }
        dimm_setting |= (spd_byte >> 1) << CF07_UPPER_D0_MB_SHIFT;
+       banner("FIELDBANKS");
 
        /* Field: Banks per SDRAM device */
        /* EEPROM byte usage: (17) Number of Banks on SDRAM Device */
        spd_byte = spd_read_byte(dimm, SPD_NUM_BANKS_PER_SDRAM);
-       if ((MIN_DEV_BANKS > spd_byte) && (spd_byte > MAX_DEV_BANKS)) {
-               print_debug("Number of device banks not compatible\n");
-               POST_CODE(ERROR_BANK_SET);
-               __asm__ __volatile__("hlt\n");
+       if ((MIN_DEV_BANKS > spd_byte) || (spd_byte > MAX_DEV_BANKS)) {
+               print_emerg("Number of device banks not compatible\n");
+               post_code(ERROR_BANK_SET);
+               hcf();
        }
        dimm_setting |= (spd_byte >> 2) << CF07_UPPER_D0_CB_SHIFT;
+       banner("SPDNUMROWS");
 
        /*; Field: DIMM size
-        *; EEPROM byte usage: (3)  Number or Row Addresses
+        *; EEPROM byte usage: (3)  Number of Row Addresses
         *;                                       (4)  Number of Column Addresses
         *;                                       (5)  Number of DIMM Banks
         *;                                       (31) Module Bank Density
@@ -70,24 +90,29 @@ static void auto_size_dimm(unsigned int dimm)
         */
        if ((spd_read_byte(dimm, SPD_NUM_ROWS) & 0xF0)
            || (spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF0)) {
-               print_debug("Assymetirc DIMM not compatible\n");
-               POST_CODE(ERROR_UNSUPPORTED_DIMM);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("Assymetirc DIMM not compatible\n");
+               post_code(ERROR_UNSUPPORTED_DIMM);
+               hcf();
        }
+       banner("SPDBANKDENSITY");
 
        dimm_size = spd_read_byte(dimm, SPD_BANK_DENSITY);
+       banner("DIMMSIZE");
        dimm_size |= (dimm_size << 8);  /* align so 1GB(bit0) is bit 8, this is a little weird to get gcc to not optimize this out */
        dimm_size &= 0x01FC;    /* and off 2GB DIMM size : not supported and the 1GB size we just moved up to bit 8 as well as all the extra on top */
 
        /*       Module Density * Module Banks */
        dimm_size <<= (dimm_setting >> CF07_UPPER_D0_MB_SHIFT) & 1;     /* shift to multiply by # DIMM banks */
+       banner("BEFORT CTZ");
        dimm_size = __builtin_ctz(dimm_size);
+       banner("TEST DIMM SIZE>8");
        if (dimm_size > 8) {    /* 8 is 1GB only support 1GB per DIMM */
-               print_debug("Only support up to 1 GB per DIMM\n");
-               POST_CODE(ERROR_DENSITY_DIMM);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("Only support up to 1 GB per DIMM\n");
+               post_code(ERROR_DENSITY_DIMM);
+               hcf();
        }
        dimm_setting |= dimm_size << CF07_UPPER_D0_SZ_SHIFT;
+       banner("PAGESIZE");
 
 /*; Field: PAGE size
 *; EEPROM byte usage: (4)  Number of Column Addresses
@@ -113,18 +138,22 @@ static void auto_size_dimm(unsigned int dimm)
 *;it adds 3 to get 10, then does 2^10=1K.  Get it?*/
 
        spd_byte = NumColAddr[spd_read_byte(dimm, SPD_NUM_COLUMNS) & 0xF];
+       banner("MAXCOLADDR");
        if (spd_byte > MAX_COL_ADDR) {
-               print_debug("DIMM page size not compatible\n");
-               POST_CODE(ERROR_SET_PAGE);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("DIMM page size not compatible\n");
+               post_code(ERROR_SET_PAGE);
+               hcf();
        }
+       banner(">12address test");
        spd_byte -= 7;
        if (spd_byte > 5) {     /* if the value is above 6 it means >12 address lines */
                spd_byte = 7;   /* which means >32k so set to disabled */
        }
        dimm_setting |= spd_byte << CF07_UPPER_D0_PSZ_SHIFT;    /* 0=1k,1=2k,2=4k,etc */
 
+       banner("RDMSR CF07");
        msr = rdmsr(MC_CF07_DATA);
+       banner("WRMSR CF07");
        if (dimm == DIMM0) {
                msr.hi &= 0xFFFF0000;
                msr.hi |= dimm_setting;
@@ -133,6 +162,7 @@ static void auto_size_dimm(unsigned int dimm)
                msr.hi |= dimm_setting << 16;
        }
        wrmsr(MC_CF07_DATA, msr);
+       banner("ALL DONE");
 }
 
 static void checkDDRMax(void)
@@ -151,10 +181,10 @@ static void checkDDRMax(void)
        }
 
        /* I don't think you need this check.
-          if (spd_byte0 < 0xA0 || spd_byte0 < 0xA0){
-          print_debug("DIMM overclocked. Check GeodeLink Speed\n");
-          POST_CODE(POST_PLL_MEM_FAIL);
-          __asm__       __volatile__("hlt\n");
+          if (spd_byte0 >= 0xA0 || spd_byte1 >= 0xA0){
+          print_emerg("DIMM overclocked. Check GeodeLink Speed\n");
+          post_code(POST_PLL_MEM_FAIL);
+          hcf();
           } */
 
        /* Use the slowest DIMM */
@@ -163,13 +193,13 @@ static void checkDDRMax(void)
        }
 
        /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
-       speed = 2 * ((10000 / (((spd_byte0 >> 4) * 10) + (spd_byte0 & 0x0F))));
+       speed = 20000 / (((spd_byte0 >> 4) * 10) + (spd_byte0 & 0x0F));
 
        /* current speed > max speed? */
        if (GeodeLinkSpeed() > speed) {
-               print_debug("DIMM overclocked. Check GeodeLink Speed\r\n");
-               POST_CODE(POST_PLL_MEM_FAIL);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("DIMM overclocked. Check GeodeLink Speed\n");
+               post_code(POST_PLL_MEM_FAIL);
+               hcf();
        }
 }
 
@@ -201,7 +231,7 @@ static void set_refresh_rate(void)
        }
 
        msr = rdmsr(MC_CF07_DATA);
-       msr.lo |= ((rate0 * (GeodeLinkSpeed() / 2)) / 16) 
+       msr.lo |= ((rate0 * (GeodeLinkSpeed() / 2)) / 16)
                        << CF07_LOWER_REF_INT_SHIFT;
        wrmsr(MC_CF07_DATA, msr);
 }
@@ -231,7 +261,7 @@ static void setCAS(void)
 ;*     Destroys: We really use everything !
 ;*****************************************************************************/
        uint16_t glspeed, dimm_speed;
-       uint8_t spd_byte, casmap0, casmap1;
+       uint8_t spd_byte, casmap0, casmap1, casmap_shift;
        msr_t msr;
 
        glspeed = GeodeLinkSpeed();
@@ -243,28 +273,26 @@ static void setCAS(void)
                spd_byte = spd_read_byte(DIMM0, SPD_SDRAM_CYCLE_TIME_2ND);
                if (spd_byte != 0) {
                        /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
-                       dimm_speed = 2 * (10000 / (((spd_byte >> 4) * 10) +
-                                               (spd_byte & 0x0F)));
+                       dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
                        if (dimm_speed >= glspeed) {
+                               casmap_shift = 1; /* -.5 is a shift of 1 */
                                /* IF -1 timing is supported, check -1 timing > GeodeLink */
                                spd_byte = spd_read_byte(DIMM0, SPD_SDRAM_CYCLE_TIME_3RD);
                                if (spd_byte != 0) {
                                        /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
-                                       dimm_speed = 2 * (10000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F)));
-                                       if (dimm_speed <= glspeed) {
-                                               /* set we can use -.5 timing but not -1 */
-                                               spd_byte = 31 - __builtin_clz((uint32_t) casmap0);
-                                               /* just want bits in the lower byte since we have to cast to a 32 */
-                                               casmap0 &= 0xFF << (--spd_byte);
+                                       dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
+                                       if (dimm_speed >= glspeed) {
+                                               casmap_shift = 2; /* -1 is a shift of 2 */
                                        }
-                               }       /*MIN_CYCLE_10 !=0 */
+                               }       /* SPD_SDRAM_CYCLE_TIME_3RD (-1) !=0 */
                        } else {
-                               /* Timing_05 < GLspeed, can't use -.5 or -1 timing */
-                               spd_byte = 31 - __builtin_clz((uint32_t) casmap0);
-                               /* just want bits in the lower byte since we have to cast to a 32 */
-                               casmap0 &= 0xFF << (spd_byte);
+                               casmap_shift = 0;
                        }
-               }               /*MIN_CYCLE_05 !=0 */
+               }       /* SPD_SDRAM_CYCLE_TIME_2ND (-.5) !=0 */
+               /* set the casmap based on the shift to limit possible CAS settings */
+               spd_byte = 31 - __builtin_clz((uint32_t) casmap0);
+               /* just want bits in the lower byte since we have to cast to a 32 */
+               casmap0 &= 0xFF << (spd_byte - casmap_shift);
        } else {                /* No DIMM */
                casmap0 = 0;
        }
@@ -276,27 +304,27 @@ static void setCAS(void)
                spd_byte = spd_read_byte(DIMM1, SPD_SDRAM_CYCLE_TIME_2ND);
                if (spd_byte != 0) {
                        /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
-                       dimm_speed = 2 * (10000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F)));
+                       dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
                        if (dimm_speed >= glspeed) {
+                               casmap_shift = 1; /* -.5 is a shift of 1 */
                                /* IF -1 timing is supported, check -1 timing > GeodeLink */
                                spd_byte = spd_read_byte(DIMM1, SPD_SDRAM_CYCLE_TIME_3RD);
                                if (spd_byte != 0) {
                                        /* Turn SPD ns time into MHZ. Check what the asm does to this math. */
-                                       dimm_speed = 2 * (10000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F)));
-                                       if (dimm_speed <= glspeed) {
-                                               /* set we can use -.5 timing but not -1 */
-                                               spd_byte = 31 - __builtin_clz((uint32_t) casmap1);
-                                               /* just want bits in the lower byte since we have to cast to a 32 */
-                                               casmap1 &= 0xFF << (--spd_byte);
+                                       dimm_speed = 20000 / (((spd_byte >> 4) * 10) + (spd_byte & 0x0F));
+                                       if (dimm_speed >= glspeed) {
+                                               casmap_shift = 2; /* -1 is a shift of 2 */
                                        }
-                               }       /*MIN_CYCLE_10 !=0 */
+                                       /* note that the -1 result doesn't need to change the available CAS map */
+                               }       /* SPD_SDRAM_CYCLE_TIME_3RD (-1) !=0 */
                        } else {
-                               /* Timing_05 < GLspeed, can't use -.5 or -1 timing */
-                               spd_byte = 31 - __builtin_clz((uint32_t) casmap1);
-                               /* just want bits in the lower byte since we have to cast to a 32 */
-                               casmap1 &= 0xFF << (spd_byte);
+                               casmap_shift = 0;
                        }
-               }               /*MIN_CYCLE_05 !=0 */
+               }       /* SPD_SDRAM_CYCLE_TIME_2ND (-.5) !=0 */
+               /* set the casmap based on the shift to limit possible CAS settings */
+               spd_byte = 31 - __builtin_clz((uint32_t) casmap1);
+               /* just want bits in the lower byte since we have to cast to a 32 */
+               casmap1 &= 0xFF << (spd_byte - casmap_shift);
        } else {                /* No DIMM */
                casmap1 = 0;
        }
@@ -309,9 +337,9 @@ static void setCAS(void)
        } else if ((casmap0 &= casmap1)) {
                spd_byte = CASDDR[__builtin_ctz((uint32_t) casmap0)];
        } else {
-               print_debug("DIMM CAS Latencies not compatible\r\n");
-               POST_CODE(ERROR_DIFF_DIMMS);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("DIMM CAS Latencies not compatible\n");
+               post_code(ERROR_DIFF_DIMMS);
+               hcf();
        }
 
        msr = rdmsr(MC_CF8F_DATA);
@@ -483,6 +511,8 @@ static void set_extended_mode_registers(void)
        wrmsr(MC_CF07_DATA, msr);
 }
 
+#undef TLA_MEMORY_DEBUG
+#ifdef TLA_MEMORY_DEBUG
 static void EnableMTest(void)
 {
        msr_t msr;
@@ -501,8 +531,9 @@ static void EnableMTest(void)
        msr.lo |= CFCLK_LOWER_TRISTATE_DIS_SET;
        wrmsr(MC_CFCLK_DBUG, msr);
 
-       print_debug("Enabled MTest for TLA debug\r\n");
+       print_info("Enabled MTest for TLA debug\n");
 }
+#endif
 
 static void sdram_set_registers(const struct mem_controller *ctrl)
 {
@@ -538,43 +569,53 @@ static void sdram_set_spd_registers(const struct mem_controller *ctrl)
 {
        uint8_t spd_byte;
 
-       POST_CODE(POST_MEM_SETUP);      // post_70h
+       banner("sdram_set_spd_register");
+       post_code(POST_MEM_SETUP);      // post_70h
 
        spd_byte = spd_read_byte(DIMM0, SPD_MODULE_ATTRIBUTES);
+       banner("Check DIMM 0");
        /* Check DIMM is not Register and not Buffered DIMMs. */
        if ((spd_byte != 0xFF) && (spd_byte & 3)) {
-               print_debug("DIMM0 NOT COMPATIBLE\r\n");
-               POST_CODE(ERROR_UNSUPPORTED_DIMM);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("DIMM0 NOT COMPATIBLE\n");
+               post_code(ERROR_UNSUPPORTED_DIMM);
+               hcf();
        }
+       banner("Check DIMM 1");
        spd_byte = spd_read_byte(DIMM1, SPD_MODULE_ATTRIBUTES);
        if ((spd_byte != 0xFF) && (spd_byte & 3)) {
-               print_debug("DIMM1 NOT COMPATIBLE\n");
-               POST_CODE(ERROR_UNSUPPORTED_DIMM);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("DIMM1 NOT COMPATIBLE\n");
+               post_code(ERROR_UNSUPPORTED_DIMM);
+               hcf();
        }
 
-       POST_CODE(POST_MEM_SETUP2);     // post_72h
+       post_code(POST_MEM_SETUP2);     // post_72h
+       banner("Check DDR MAX");
 
        /* Check that the memory is not overclocked. */
        checkDDRMax();
 
        /* Size the DIMMS */
-       POST_CODE(POST_MEM_SETUP3);     // post_73h
+       post_code(POST_MEM_SETUP3);     // post_73h
+       banner("AUTOSIZE DIMM 0");
        auto_size_dimm(DIMM0);
-       POST_CODE(POST_MEM_SETUP4);     // post_74h
+       post_code(POST_MEM_SETUP4);     // post_74h
+       banner("AUTOSIZE DIMM 1");
        auto_size_dimm(DIMM1);
 
        /* Set CAS latency */
-       POST_CODE(POST_MEM_SETUP5);     // post_75h
+       banner("set cas latency");
+       post_code(POST_MEM_SETUP5);     // post_75h
        setCAS();
 
        /* Set all the other latencies here (tRAS, tRP....) */
+       banner("set all latency");
        set_latencies();
 
        /* Set Extended Mode Registers */
+       banner("set emrs");
        set_extended_mode_registers();
 
+       banner("set ref rate");
        /* Set Memory Refresh Rate */
        set_refresh_rate();
 
@@ -599,18 +640,20 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
 ;* 9) MRS w/ memory config & reset DLL clear
 ;* 8) DDR SDRAM ready for normal operation
 ;********************************************************************/
-       POST_CODE(POST_MEM_ENABLE);     // post_76h
+       post_code(POST_MEM_ENABLE);     // post_76h
 
+#ifdef TLA_MEMORY_DEBUG
        /* Only enable MTest for TLA memory debug */
-       /*EnableMTest(); */
+       EnableMTest();
+#endif
 
        /* If both Page Size = "Not Installed" we have a problems and should halt. */
        msr = rdmsr(MC_CF07_DATA);
-       if ((msr.hi & ((7 << CF07_UPPER_D1_PSZ_SHIFT) | (7 << CF07_UPPER_D0_PSZ_SHIFT))) == 
+       if ((msr.hi & ((7 << CF07_UPPER_D1_PSZ_SHIFT) | (7 << CF07_UPPER_D0_PSZ_SHIFT))) ==
                        ((7 << CF07_UPPER_D1_PSZ_SHIFT) | (7 << CF07_UPPER_D0_PSZ_SHIFT))) {
-               print_debug("No memory in the system\r\n");
-               POST_CODE(ERROR_NO_DIMMS);
-               __asm__ __volatile__("hlt\n");
+               print_emerg("No memory in the system\n");
+               post_code(ERROR_NO_DIMMS);
+               hcf();
        }
 
        /*      Set CKEs */
@@ -718,8 +761,8 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
        msr.lo |= (209 << 8);   /* bits[15:8] = 209 */
        wrmsr(msrnum, msr);
 
-       print_debug("DRAM controller init done.\n");
-       POST_CODE(POST_MEM_SETUP_GOOD); //0x7E
+       print_emerg("DRAM controller init done.\n");
+       post_code(POST_MEM_SETUP_GOOD); //0x7E
 
        /* make sure there is nothing stale in the cache */
        /* CAR stack is in the cache __asm__ __volatile__("wbinvd\n"); */
@@ -750,6 +793,6 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                msr.lo |= 1;
                wrmsr(msrnum, msr);
        }
-       print_debug("RAM DLL lock\n");
+       print_info("RAM DLL lock\n");
 
 }