Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / northbridge / amd / amdk8 / raminit.c
index f7a12e77f8502ef90692a05f4b8e19a473d085dc..7ad1b8004c2c7b2b423012e34ad6843875518a78 100644 (file)
@@ -4,73 +4,54 @@
        2005.02 yhlu add E0 memory hole support
 */
 
-#include <cpu/x86/mem.h>
 #include <cpu/x86/cache.h>
 #include <cpu/x86/mtrr.h>
+#include <stdlib.h>
+#include <reset.h>
 #include "raminit.h"
 #include "amdk8.h"
 
-#if (CONFIG_LB_MEM_TOPK & (CONFIG_LB_MEM_TOPK -1)) != 0
-# error "CONFIG_LB_MEM_TOPK must be a power of 2"
+#if (CONFIG_RAMTOP & (CONFIG_RAMTOP -1)) != 0
+# error "CONFIG_RAMTOP must be a power of 2"
 #endif
 
-#ifndef K8_4RANK_DIMM_SUPPORT
-#define K8_4RANK_DIMM_SUPPORT 0
+#ifndef QRANK_DIMM_SUPPORT
+#define QRANK_DIMM_SUPPORT 0
 #endif
 
-#if defined (__GNUC__)
-static void hard_reset(void);
-#endif
-
-#if 1
 static void setup_resource_map(const unsigned int *register_values, int max)
 {
        int i;
-//     print_debug("setting up resource map....");
-#if 0
-       print_debug("\r\n");
-#endif
-       for(i = 0; i < max; i += 3) {
+//     printk(BIOS_DEBUG, "setting up resource map....");
+       for (i = 0; i < max; i += 3) {
                device_t dev;
                unsigned where;
                unsigned long reg;
-#if 0
-       #if CONFIG_USE_INIT
-               prink_debug("%08x <- %08x\r\n", register_values[i], register_values[i+2]);
-       #else
-               print_debug_hex32(register_values[i]);
-               print_debug(" <-");
-               print_debug_hex32(register_values[i+2]);
-               print_debug("\r\n");
-       #endif
-#endif
                dev = register_values[i] & ~0xfff;
                where = register_values[i] & 0xfff;
                reg = pci_read_config32(dev, where);
                reg &= register_values[i+1];
                reg |= register_values[i+2];
                pci_write_config32(dev, where, reg);
-#if 0
-               reg = pci_read_config32(register_values[i]);
-               reg &= register_values[i+1];
-               reg |= register_values[i+2] & ~register_values[i+1];
-               pci_write_config32(register_values[i], reg);
-#endif
        }
-//     print_debug("done.\r\n");
+//     printk(BIOS_DEBUG, "done.\n");
 }
-#endif
 
 static int controller_present(const struct mem_controller *ctrl)
 {
-        return pci_read_config32(ctrl->f0, 0) == 0x11001022;
+       return pci_read_config32(ctrl->f0, 0) == 0x11001022;
 }
 
+#if RAMINIT_SYSINFO==1
+static void sdram_set_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
+#else
 static void sdram_set_registers(const struct mem_controller *ctrl)
+#endif
 {
        static const unsigned int register_values[] = {
 
-       /* Careful set limit registers before base registers which contain the enables */
+       /* Careful set limit registers before base registers which
+          contain the enables */
        /* DRAM Limit i Registers
         * F1:0x44 i = 0
         * F1:0x4C i = 1
@@ -190,7 +171,7 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
         * [29:21] Address Mask (33-25)
         *         The bits with an address mask of 1 are excluded from address comparison
         * [31:30] Reserved
-        * 
+        *
         */
        PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
        PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
@@ -203,7 +184,7 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
        /* DRAM Bank Address Mapping Register
         * F2:0x80
         * Specify the memory module size
-        * [ 2: 0] CS1/0 
+        * [ 2: 0] CS1/0
         * [ 6: 4] CS3/2
         * [10: 8] CS5/4
         * [14:12] CS7/6
@@ -214,7 +195,7 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
         *         100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
         *         101 = 1Gbyte   (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
         *         110 = 2Gbyte   (Rows = 14 & Col = 12)
-        *         111 = reserved 
+        *         111 = reserved
         * [ 3: 3] Reserved
         * [ 7: 7] Reserved
         * [11:11] Reserved
@@ -330,8 +311,8 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
         *         0 = Disabled
         *         1 = Enabled
         * [ 3: 3] Disable DQS Hystersis  (FIXME handle this one carefully)
-        *         0 = Enable DQS input filter 
-        *         1 = Disable DQS input filtering 
+        *         0 = Enable DQS input filter
+        *         1 = Disable DQS input filtering
         * [ 7: 4] Reserved
         * [ 8: 8] DRAM_Init
         *         0 = Initialization done or not yet started.
@@ -388,12 +369,12 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
         *         111 = Oldest entry in DCQ can be bypassed 7 times
         * [31:28] Reserved
         */
-       PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000, 
-       (4 << 25)|(0 << 24)| 
-       (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)| 
-       (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)| 
-       (2 << 14)|(0 << 13)|(0 << 12)| 
-       (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)| 
+       PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
+       (4 << 25)|(0 << 24)|
+       (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
+       (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
+       (2 << 14)|(0 << 13)|(0 << 12)|
+       (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
        (0 << 3) |(0 << 1) |(0 << 0),
        /* DRAM Config High Register
         * F2:0x94
@@ -478,6 +459,14 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
         * [31:26] Reserved
         */
        PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
+       /* MCA NB Status Low reg */
+       PCI_ADDR(0, 0x18, 3, 0x48), 0x00f00000, 0x00000000,
+       /* MCA NB Status high reg */
+       PCI_ADDR(0, 0x18, 3, 0x4c), 0x01801e8c, 0x00000000,
+       /* MCA NB address Low reg */
+       PCI_ADDR(0, 0x18, 3, 0x50), 0x00000007, 0x00000000,
+       /* MCA NB address high reg */
+       PCI_ADDR(0, 0x18, 3, 0x54), 0xffffff00, 0x00000000,
        /* DRAM Scrub Control Register
         * F3:0x58
         * [ 4: 0] DRAM Scrube Rate
@@ -531,48 +520,26 @@ static void sdram_set_registers(const struct mem_controller *ctrl)
        int i;
        int max;
 
-#if 1
-        if (!controller_present(ctrl)) {
-//                print_debug("No memory controller present\r\n");
-                return;
-        }
-#endif
-       print_spew("setting up CPU");
-       print_spew_hex8(ctrl->node_id);
-       print_spew(" northbridge registers\r\n");
-       max = sizeof(register_values)/sizeof(register_values[0]);
-       for(i = 0; i < max; i += 3) {
+       if (!controller_present(ctrl)) {
+//             printk(BIOS_DEBUG, "No memory controller present\n");
+               return;
+       }
+       printk(BIOS_SPEW, "setting up CPU%02x northbridge registers\n", ctrl->node_id);
+       max = ARRAY_SIZE(register_values);
+       for (i = 0; i < max; i += 3) {
                device_t dev;
                unsigned where;
                unsigned long reg;
-#if 0
-        #if CONFIG_USE_INIT
-                prink_debug("%08x <- %08x\r\n", register_values[i], register_values[i+2]);
-        #else
-               print_spew_hex32(register_values[i]);
-               print_spew(" <-");
-               print_spew_hex32(register_values[i+2]);
-               print_spew("\r\n");
-       #endif
-#endif
                dev = (register_values[i] & ~0xfff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
                where = register_values[i] & 0xfff;
                reg = pci_read_config32(dev, where);
                reg &= register_values[i+1];
                reg |= register_values[i+2];
                pci_write_config32(dev, where, reg);
-#if 0
-
-               reg = pci_read_config32(register_values[i]);
-               reg &= register_values[i+1];
-               reg |= register_values[i+2];
-               pci_write_config32(register_values[i], reg);
-#endif
        }
-       print_spew("done.\r\n");
+       printk(BIOS_SPEW, "done.\n");
 }
 
-
 static void hw_enable_ecc(const struct mem_controller *ctrl)
 {
        uint32_t dcl, nbcap;
@@ -582,11 +549,11 @@ static void hw_enable_ecc(const struct mem_controller *ctrl)
        if (nbcap & NBCAP_ECC) {
                dcl |= DCL_DimmEccEn;
        }
-       if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
+       if (CONFIG_HAVE_OPTION_TABLE &&
+           read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
                dcl &= ~DCL_DimmEccEn;
        }
        pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
-       
 }
 
 static int is_dual_channel(const struct mem_controller *ctrl)
@@ -598,11 +565,13 @@ static int is_dual_channel(const struct mem_controller *ctrl)
 
 static int is_opteron(const struct mem_controller *ctrl)
 {
-       /* Test to see if I am an Opteron.  
-        * FIXME Testing dual channel capability is correct for now
-        * but a beter test is probably required.
+       /* Test to see if I am an Opteron.  Socket 939 based Athlon64
+        * have dual channel capability, too, so we need a better test
+        * for Opterons.
+        * However, all code uses is_opteron() to find out whether to
+        * use dual channel, so if we really check for opteron here, we
+        * need to fix up all code using this function, too.
         */
-#warning "FIXME implement a better test for opterons"
        uint32_t nbcap;
        nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
        return !!(nbcap & NBCAP_128Bit);
@@ -616,7 +585,7 @@ static int is_registered(const struct mem_controller *ctrl)
         */
        uint32_t dcl;
        dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
-       return !(dcl & DCL_UnBufDimm);
+       return !(dcl & DCL_UnBuffDimm);
 }
 
 struct dimm_size {
@@ -624,7 +593,7 @@ struct dimm_size {
        unsigned long side2;
        unsigned long rows;
        unsigned long col;
-#if K8_4RANK_DIMM_SUPPORT == 1
+#if QRANK_DIMM_SUPPORT == 1
        unsigned long rank;
 #endif
 };
@@ -638,7 +607,7 @@ static struct dimm_size spd_get_dimm_size(unsigned device)
        sz.side2 = 0;
        sz.rows = 0;
        sz.col = 0;
-#if K8_4RANK_DIMM_SUPPORT == 1
+#if QRANK_DIMM_SUPPORT == 1
        sz.rank = 0;
 #endif
 
@@ -668,7 +637,7 @@ static struct dimm_size spd_get_dimm_size(unsigned device)
        if (value < 0) goto hw_err;
        value &= 0xff;
        value <<= 8;
-       
+
        low = spd_read_byte(device, 6); /* (low byte) */
        if (low < 0) goto hw_err;
        value = value | (low & 0xff);
@@ -682,7 +651,7 @@ static struct dimm_size spd_get_dimm_size(unsigned device)
        if ((value != 2) && (value != 4 )) {
                goto val_err;
        }
-#if K8_4RANK_DIMM_SUPPORT == 1
+#if QRANK_DIMM_SUPPORT == 1
        sz.rank = value;
 #endif
 
@@ -704,14 +673,14 @@ static struct dimm_size spd_get_dimm_size(unsigned device)
        goto out;
 
  val_err:
-       die("Bad SPD value\r\n");
+       die("Bad SPD value\n");
        /* If an hw_error occurs report that I have no memory */
 hw_err:
        sz.side1 = 0;
        sz.side2 = 0;
        sz.rows = 0;
        sz.col = 0;
-#if K8_4RANK_DIMM_SUPPORT == 1
+#if QRANK_DIMM_SUPPORT == 1
        sz.rank = 0;
 #endif
  out:
@@ -727,20 +696,20 @@ static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz
        if (sz.side1 != sz.side2) {
                sz.side2 = 0;
        }
-       
+
        /* For each base register.
         * Place the dimm size in 32 MB quantities in the bits 31 - 21.
         * The initialize dimm size is in bits.
         * Set the base enable bit0.
         */
-       
+
        base0 = base1 = 0;
 
        /* Make certain side1 of the dimm is at least 32MB */
        if (sz.side1 >= (25 +3)) {
                base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
        }
-       
+
        /* Make certain side2 of the dimm is at least 32MB */
        if (sz.side2 >= (25 + 3)) {
                base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
@@ -759,8 +728,8 @@ static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz
        /* Set the appropriate DIMM base address register */
        pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
        pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
-#if K8_4RANK_DIMM_SUPPORT == 1
-       if(sz.rank == 4) {
+#if QRANK_DIMM_SUPPORT == 1
+       if (sz.rank == 4) {
                pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), base0);
                pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), base1);
        }
@@ -770,8 +739,8 @@ static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz
        if (base0) {
                dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
                dch |= DCH_MEMCLK_EN0 << index;
-#if K8_4RANK_DIMM_SUPPORT == 1
-               if(sz.rank == 4) {
+#if QRANK_DIMM_SUPPORT == 1
+               if (sz.rank == 4) {
                        dch |= DCH_MEMCLK_EN0 << (index + 2);
                }
 #endif
@@ -789,46 +758,45 @@ static void set_dimm_map(const struct mem_controller *ctrl, struct dimm_size sz,
        };
 
        uint32_t map;
-       uint32_t dch;
 
        map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
        map &= ~(0xf << (index * 4));
-#if K8_4RANK_DIMM_SUPPORT == 1
-        if(sz.rank == 4) {
-                map &= ~(0xf << ( (index + 2) * 4));
-        }
+#if QRANK_DIMM_SUPPORT == 1
+       if (sz.rank == 4) {
+               map &= ~(0xf << ( (index + 2) * 4));
+       }
 #endif
 
 
        /* Make certain side1 of the dimm is at least 32MB */
        if (sz.side1 >= (25 +3)) {
-               if(is_cpu_pre_d0()) {
+               if (is_cpu_pre_d0()) {
                        map |= (sz.side1 - (25 + 3)) << (index *4);
-#if K8_4RANK_DIMM_SUPPORT == 1
-                       if(sz.rank == 4) {
-                             map |= (sz.side1 - (25 + 3)) << ( (index + 2) * 4);
-                               }
+#if QRANK_DIMM_SUPPORT == 1
+                       if (sz.rank == 4) {
+                               map |= (sz.side1 - (25 + 3)) << ( (index + 2) * 4);
+                       }
 #endif
                }
                else {
                        map |= cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << (index*4);
-#if K8_4RANK_DIMM_SUPPORT == 1
-                       if(sz.rank == 4) {
-                              map |=  cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << ( (index + 2) * 4);
-                               }
+#if QRANK_DIMM_SUPPORT == 1
+                       if (sz.rank == 4) {
+                               map |=  cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << ( (index + 2) * 4);
+                       }
 #endif
                }
        }
 
        pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
-       
+
 }
 
 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
 {
        int i;
-       
-       for(i = 0; i < DIMM_SOCKETS; i++) {
+
+       for (i = 0; i < DIMM_SOCKETS; i++) {
                struct dimm_size sz;
                if (!(dimm_mask & (1 << i))) {
                        continue;
@@ -866,7 +834,7 @@ static void route_dram_accesses(const struct mem_controller *ctrl,
 
        limit_reg = 0x44 + index;
        base_reg = 0x40 + index;
-       for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
+       for (device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
                pci_write_config32(device, limit_reg, limit);
                pci_write_config32(device, base_reg, base);
        }
@@ -880,29 +848,32 @@ static void set_top_mem(unsigned tom_k, unsigned hole_startk)
        }
 
        /* Report the amount of memory. */
-       print_spew("RAM: 0x");
-       print_spew_hex32(tom_k);
-       print_spew(" KB\r\n");
+       printk(BIOS_DEBUG, "RAM end at 0x%08x kB\n", tom_k);
 
        /* Now set top of memory */
        msr_t msr;
-       if(tom_k > (4*1024*1024)) {
+       if (tom_k > (4*1024*1024)) {
+               printk(BIOS_SPEW, "Handling memory mapped above 4 GB\n");
+               printk(BIOS_SPEW, "Upper RAM end at 0x%08x kB\n", tom_k);
                msr.lo = (tom_k & 0x003fffff) << 10;
                msr.hi = (tom_k & 0xffc00000) >> 22;
                wrmsr(TOP_MEM2, msr);
+               printk(BIOS_SPEW, "Correcting memory amount mapped below 4 GB\n");
        }
 
        /* Leave a 64M hole between TOP_MEM and TOP_MEM2
         * so I can see my rom chip and other I/O devices.
         */
        if (tom_k >= 0x003f0000) {
-#if HW_MEM_HOLE_SIZEK != 0
-                if(hole_startk != 0) {
-                        tom_k = hole_startk;
-                } else
+#if CONFIG_HW_MEM_HOLE_SIZEK != 0
+               if (hole_startk != 0) {
+                       tom_k = hole_startk;
+               } else
 #endif
-                tom_k = 0x3f0000;
+               tom_k = 0x3f0000;
+               printk(BIOS_SPEW, "Adjusting lower RAM end\n");
        }
+       printk(BIOS_SPEW, "Lower RAM end at 0x%08x kB\n", tom_k);
        msr.lo = (tom_k & 0x003fffff) << 10;
        msr.hi = (tom_k & 0xffc00000) >> 22;
        wrmsr(TOP_MEM, msr);
@@ -911,29 +882,29 @@ static void set_top_mem(unsigned tom_k, unsigned hole_startk)
 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
 {
        /* 35 - 25 */
-       static const uint8_t csbase_low_shift[] = { 
+       static const uint8_t csbase_low_shift[] = {
        /* 32MB */      (13 - 4),
        /* 64MB */      (14 - 4),
-       /* 128MB */     (14 - 4), 
+       /* 128MB */     (14 - 4),
        /* 256MB */     (15 - 4),
        /* 512MB */     (15 - 4),
        /* 1GB */       (16 - 4),
-       /* 2GB */       (16 - 4), 
+       /* 2GB */       (16 - 4),
        };
 
-        static const uint8_t csbase_low_d0_shift[] = {
-        /* 32MB */      (13 - 4),
-        /* 64MB */      (14 - 4),
-        /* 128MB */     (14 - 4),
+       static const uint8_t csbase_low_d0_shift[] = {
+       /* 32MB */      (13 - 4),
+       /* 64MB */      (14 - 4),
+       /* 128MB */     (14 - 4),
        /* 128MB */     (15 - 4),
-        /* 256MB */     (15 - 4),
-        /* 512MB */     (15 - 4),
-        /* 256MB */     (16 - 4),
-        /* 512MB */     (16 - 4),
-        /* 1GB */       (16 - 4),
+       /* 256MB */     (15 - 4),
+       /* 512MB */     (15 - 4),
+       /* 256MB */     (16 - 4),
+       /* 512MB */     (16 - 4),
+       /* 1GB */       (16 - 4),
        /* 1GB */       (17 - 4),
-        /* 2GB */       (17 - 4),
-        };
+       /* 2GB */       (17 - 4),
+       };
 
        /* cs_base_high is not changed */
 
@@ -950,13 +921,13 @@ static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
        chip_selects = 0;
        common_size = 0;
        common_cs_mode = 0;
-       for(index = 0; index < 8; index++) {
+       for (index = 0; index < 8; index++) {
                unsigned size;
                unsigned cs_mode;
                uint32_t value;
-               
+
                value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
-               
+
                /* Is it enabled? */
                if (!(value & 1)) {
                        continue;
@@ -972,15 +943,15 @@ static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
                }
 
                value = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
-                cs_mode =( value >> ((index>>1)*4)) & 0xf;
-                if(cs_mode == 0 ) continue;
-                if(common_cs_mode == 0) {
-                       common_cs_mode = cs_mode;
-                }
-                /* The size differed fail */
-                if(common_cs_mode != cs_mode) {
-                        return 0;
-                }
+               cs_mode =( value >> ((index>>1)*4)) & 0xf;
+               if (cs_mode == 0 ) continue;
+               if (common_cs_mode == 0) {
+                       common_cs_mode = cs_mode;
+               }
+               /* The cs_mode differed fail */
+               if (common_cs_mode != cs_mode) {
+                       return 0;
+               }
        }
 
        /* Chip selects can only be interleaved when there is
@@ -992,36 +963,36 @@ static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
        }
 
        /* Find the bits of csbase that we need to interleave on */
-       if(is_cpu_pre_d0()){
+       if (is_cpu_pre_d0()){
                csbase_inc = 1 << csbase_low_shift[common_cs_mode];
-               if(is_dual_channel(ctrl)) {
-                /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
-                       if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
-//                                     print_debug("8 4GB chip selects cannot be interleaved\r\n");
-                               return 0;
-                       }  
+               if (is_dual_channel(ctrl)) {
+               /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
+                       if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
+//                                     printk(BIOS_DEBUG, "8 4GB chip selects cannot be interleaved\n");
+                               return 0;
+                       }
                        csbase_inc <<=1;
                }
        }
        else {
                csbase_inc = 1 << csbase_low_d0_shift[common_cs_mode];
-               if(is_dual_channel(ctrl)) {
-                       if( (bits==3) && (common_cs_mode > 8)) {
-//                             print_debug("8 cs_mode>8 chip selects cannot be interleaved\r\n");
-                               return 0;
+               if (is_dual_channel(ctrl)) {
+                       if ( (bits==3) && (common_cs_mode > 8)) {
+//                             printk(BIOS_DEBUG, "8 cs_mode>8 chip selects cannot be interleaved\n");
+                               return 0;
                        }
                        csbase_inc <<=1;
-                }   
+               }
        }
 
-       /* Compute the initial values for csbase and csbask. 
+       /* Compute the initial values for csbase and csbask.
         * In csbase just set the enable bit and the base to zero.
         * In csmask set the mask bits for the size and page level interleave.
         */
        csbase = 0 | 1;
        csmask = (((common_size  << bits) - 1) << 21);
        csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
-       for(index = 0; index < 8; index++) {
+       for (index = 0; index < 8; index++) {
                uint32_t value;
 
                value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
@@ -1033,8 +1004,8 @@ static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
                pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
                csbase += csbase_inc;
        }
-       
-       print_spew("Interleaved\r\n");
+
+       printk(BIOS_SPEW, "Interleaved\n");
 
        /* Return the memory size in K */
        return common_size << (15 + bits);
@@ -1046,14 +1017,14 @@ static unsigned long order_chip_selects(const struct mem_controller *ctrl)
 
        /* Remember which registers we have used in the high 8 bits of tom */
        tom = 0;
-       for(;;) {
-               /* Find the largest remaining canidate */
-               unsigned index, canidate;
+       for (;;) {
+               /* Find the largest remaining candidate */
+               unsigned index, candidate;
                uint32_t csbase, csmask;
                unsigned size;
                csbase = 0;
-               canidate = 0;
-               for(index = 0; index < 8; index++) {
+               candidate = 0;
+               for (index = 0; index < 8; index++) {
                        uint32_t value;
                        value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
 
@@ -1061,21 +1032,22 @@ static unsigned long order_chip_selects(const struct mem_controller *ctrl)
                        if (!(value & 1)) {
                                continue;
                        }
-                       
+
                        /* Is it greater? */
                        if (value <= csbase) {
                                continue;
                        }
-                       
+
                        /* Has it already been selected */
                        if (tom & (1 << (index + 24))) {
                                continue;
                        }
-                       /* I have a new canidate */
+                       /* I have a new candidate */
                        csbase = value;
-                       canidate = index;
+                       candidate = index;
                }
-               /* See if I have found a new canidate */
+
+               /* See if I have found a new candidate */
                if (csbase == 0) {
                        break;
                }
@@ -1084,7 +1056,7 @@ static unsigned long order_chip_selects(const struct mem_controller *ctrl)
                size = csbase >> 21;
 
                /* Remember I have used this register */
-               tom |= (1 << (canidate + 24));
+               tom |= (1 << (candidate + 24));
 
                /* Recompute the cs base register value */
                csbase = (tom << 21) | 1;
@@ -1097,22 +1069,22 @@ static unsigned long order_chip_selects(const struct mem_controller *ctrl)
                csmask |= 0xfe00;               /* For now don't optimize */
 
                /* Write the new base register */
-               pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
+               pci_write_config32(ctrl->f2, DRAM_CSBASE + (candidate << 2), csbase);
                /* Write the new mask register */
-               pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
-               
+               pci_write_config32(ctrl->f2, DRAM_CSMASK + (candidate << 2), csmask);
+
        }
        /* Return the memory size in K */
        return (tom & ~0xff000000) << 15;
 }
 
-unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
+static unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
 {
        unsigned node_id;
        unsigned end_k;
        /* Find the last memory address used */
        end_k = 0;
-       for(node_id = 0; node_id < max_node_id; node_id++) {
+       for (node_id = 0; node_id < max_node_id; node_id++) {
                uint32_t limit, base;
                unsigned index;
                index = node_id << 3;
@@ -1130,15 +1102,18 @@ static void order_dimms(const struct mem_controller *ctrl)
 {
        unsigned long tom_k, base_k;
 
-       if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
+       if ((!CONFIG_HAVE_OPTION_TABLE) ||
+           read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
                tom_k = interleave_chip_selects(ctrl);
        } else {
-               print_debug("Interleaving disabled\r\n");
+               printk(BIOS_DEBUG, "Interleaving disabled\n");
                tom_k = 0;
        }
+
        if (!tom_k) {
                tom_k = order_chip_selects(ctrl);
        }
+
        /* Compute the memory base address */
        base_k = memory_end_k(ctrl, ctrl->node_id);
        tom_k += base_k;
@@ -1148,24 +1123,24 @@ static void order_dimms(const struct mem_controller *ctrl)
 
 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
 {
-       print_debug("disabling dimm"); 
-       print_debug_hex8(index); 
-       print_debug("\r\n");
+       printk(BIOS_DEBUG, "disabling dimm %02x\n", index);
        pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
        pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
        dimm_mask &= ~(1 << index);
        return dimm_mask;
 }
 
-static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask)
+static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl,
+                                       long dimm_mask)
 {
        int i;
        int registered;
        int unbuffered;
+       int has_dualch = is_opteron(ctrl);
        uint32_t dcl;
        unbuffered = 0;
        registered = 0;
-       for(i = 0; (i < DIMM_SOCKETS); i++) {
+       for (i = 0; (i < DIMM_SOCKETS); i++) {
                int value;
                if (!(dimm_mask & (1 << i))) {
                        continue;
@@ -1174,10 +1149,11 @@ static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long
                if (value < 0) {
                        return -1;
                }
+
                /* Registered dimm ? */
                if (value & (1 << 1)) {
                        registered = 1;
-               } 
+               }
                /* Otherwise it must be an unbuffered dimm */
                else {
                        unbuffered = 1;
@@ -1186,26 +1162,31 @@ static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long
        if (unbuffered && registered) {
                die("Mixed buffered and registered dimms not supported");
        }
-#if 1
-       //By yhlu for debug Athlon64 939 can do dual channel, but it use unbuffer DIMM
-       if (unbuffered && is_opteron(ctrl)) {
-               die("Unbuffered Dimms not supported on Opteron");
-       }
-#endif
 
        dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
-       dcl &= ~DCL_UnBufDimm;
+       dcl &= ~DCL_UnBuffDimm;
        if (unbuffered) {
-               dcl |= DCL_UnBufDimm;
+               if ((has_dualch) && (!is_cpu_pre_d0())) {
+                       dcl |= DCL_UnBuffDimm; /* set DCL_DualDIMMen too? */
+
+                       /* set DCL_En2T if you have non-equal DDR mem types! */
+
+                       if ((cpuid_eax(1) & 0x30) == 0x30) {
+                               /* CS[7:4] is copy of CS[3:0], should be set for 939 socket */
+                               dcl |= DCL_UpperCSMap;
+                       }
+               } else {
+                       dcl |= DCL_UnBuffDimm;
+               }
        }
        pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
-#if 0
+
        if (is_registered(ctrl)) {
-               print_debug("Registered\r\n");
+               printk(BIOS_SPEW, "Registered\n");
        } else {
-               print_debug("Unbuffered\r\n");
+               printk(BIOS_SPEW, "Unbuffered\n");
        }
-#endif
+
        return dimm_mask;
 }
 
@@ -1214,7 +1195,7 @@ static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
        unsigned dimm_mask;
        int i;
        dimm_mask = 0;
-       for(i = 0; i < DIMM_SOCKETS; i++) {
+       for (i = 0; i < DIMM_SOCKETS; i++) {
                int byte;
                unsigned device;
                device = ctrl->channel0[i];
@@ -1264,7 +1245,7 @@ static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_ma
        };
        /* If the dimms are not in pairs do not do dual channels */
        if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
-               ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) { 
+               ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
                goto single_channel;
        }
        /* If the cpu is not capable of doing dual channels don't do dual channels */
@@ -1272,7 +1253,7 @@ static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_ma
        if (!(nbcap & NBCAP_128Bit)) {
                goto single_channel;
        }
-       for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
+       for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
                unsigned device0, device1;
                int value0, value1;
                int j;
@@ -1282,7 +1263,7 @@ static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_ma
                }
                device0 = ctrl->channel0[i];
                device1 = ctrl->channel1[i];
-               for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
+               for (j = 0; j < ARRAY_SIZE(addresses); j++) {
                        unsigned addr;
                        addr = addresses[j];
                        value0 = spd_read_byte(device0, addr);
@@ -1298,7 +1279,7 @@ static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_ma
                        }
                }
        }
-       print_spew("Enabling dual channel memory\r\n");
+       printk(BIOS_SPEW, "Enabling dual channel memory\n");
        uint32_t dcl;
        dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
        dcl &= ~DCL_32ByteEn;
@@ -1318,6 +1299,9 @@ struct mem_param {
        uint32_t dch_memclk;
        uint16_t dch_tref4k, dch_tref8k;
        uint8_t  dtl_twr;
+       uint8_t  dtl_twtr;
+       uint8_t  dtl_trwt[3][3]; /* first index is CAS_LAT 2/2.5/3 and 128/registered64/64 */
+       uint8_t  rdpreamble[4]; /* 0 is for registered, 1 for 1-2 DIMMS, 2 and 3 for 3 or 4 unreg dimm slots */
        char name[9];
 };
 
@@ -1325,7 +1309,7 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
 {
        static const struct mem_param speed[] = {
                {
-                       .name       = "100Mhz\r\n",
+                       .name       = "100Mhz",
                        .cycle_time = 0xa0,
                        .divisor    = (10 <<1),
                        .tRC        = 0x46,
@@ -1334,9 +1318,12 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
                        .dch_tref4k = DTH_TREF_100MHZ_4K,
                        .dch_tref8k = DTH_TREF_100MHZ_8K,
                        .dtl_twr    = 2,
+                       .dtl_twtr   = 1,
+                       .dtl_trwt   = { { 2, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
+                       .rdpreamble = { ((9 << 1) + 0), ((9 << 1) + 0), ((9 << 1) + 0), ((9 << 1) + 0) }
                },
                {
-                       .name       = "133Mhz\r\n",
+                       .name       = "133Mhz",
                        .cycle_time = 0x75,
                        .divisor    = (7<<1)+1,
                        .tRC        = 0x41,
@@ -1345,9 +1332,12 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
                        .dch_tref4k = DTH_TREF_133MHZ_4K,
                        .dch_tref8k = DTH_TREF_133MHZ_8K,
                        .dtl_twr    = 2,
+                       .dtl_twtr   = 1,
+                       .dtl_trwt   = { { 2, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
+                       .rdpreamble = { ((8 << 1) + 0), ((7 << 1) + 0), ((7 << 1) + 1), ((7 << 1) + 0) }
                },
                {
-                       .name       = "166Mhz\r\n",
+                       .name       = "166Mhz",
                        .cycle_time = 0x60,
                        .divisor    = (6<<1),
                        .tRC        = 0x3C,
@@ -1356,9 +1346,12 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
                        .dch_tref4k = DTH_TREF_166MHZ_4K,
                        .dch_tref8k = DTH_TREF_166MHZ_8K,
                        .dtl_twr    = 3,
+                       .dtl_twtr   = 1,
+                       .dtl_trwt   = { { 3, 2, 3 }, { 3, 3, 4 }, { 4, 3, 4 }},
+                       .rdpreamble = { ((7 << 1) + 1), ((6 << 1) + 0), ((6 << 1) + 1), ((6 << 1) + 0) }
                },
                {
-                       .name       = "200Mhz\r\n",
+                       .name       = "200Mhz",
                        .cycle_time = 0x50,
                        .divisor    = (5<<1),
                        .tRC        = 0x37,
@@ -1367,13 +1360,16 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
                        .dch_tref4k = DTH_TREF_200MHZ_4K,
                        .dch_tref8k = DTH_TREF_200MHZ_8K,
                        .dtl_twr    = 3,
+                       .dtl_twtr   = 2,
+                       .dtl_trwt   = { { 0, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
+                       .rdpreamble = { ((7 << 1) + 0), ((5 << 1) + 0), ((5 << 1) + 1), ((5 << 1) + 1) }
                },
                {
                        .cycle_time = 0x00,
                },
        };
        const struct mem_param *param;
-       for(param = &speed[0]; param->cycle_time ; param++) {
+       for (param = &speed[0]; param->cycle_time ; param++) {
                if (min_cycle_time > (param+1)->cycle_time) {
                        break;
                }
@@ -1381,10 +1377,7 @@ static const struct mem_param *get_mem_param(unsigned min_cycle_time)
        if (!param->cycle_time) {
                die("min_cycle_time to low");
        }
-       print_spew(param->name);
-#ifdef DRAM_MIN_CYCLE_TIME
-       print_debug(param->name);
-#endif
+       printk(BIOS_SPEW, "%s\n", param->name);
        return param;
 }
 
@@ -1408,12 +1401,12 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
                [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
        };
 
-
        value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
+
        min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
        bios_cycle_time = min_cycle_times[
                read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
-       if (bios_cycle_time > min_cycle_time) {
+       if (CONFIG_HAVE_OPTION_TABLE && bios_cycle_time > min_cycle_time) {
                min_cycle_time = bios_cycle_time;
        }
        min_latency = 2;
@@ -1421,7 +1414,7 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
        /* Compute the least latency with the fastest clock supported
         * by both the memory controller and the dimms.
         */
-       for(i = 0; i < DIMM_SOCKETS; i++) {
+       for (i = 0; i < DIMM_SOCKETS; i++) {
                int new_cycle_time, new_latency;
                int index;
                int latencies;
@@ -1452,20 +1445,20 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
                latency = log2(latencies) -2;
 
                /* Loop through and find a fast clock with a low latency */
-               for(index = 0; index < 3; index++, latency++) {
-                       int value;
+               for (index = 0; index < 3; index++, latency++) {
+                       int spd_value;
                        if ((latency < 2) || (latency > 4) ||
                                (!(latencies & (1 << latency)))) {
                                continue;
                        }
-                       value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
-                       if (value < 0) {
+                       spd_value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
+                       if (spd_value < 0) {
                                goto hw_error;
                        }
 
                        /* Only increase the latency if we decreas the clock */
-                       if ((value >= min_cycle_time) && (value < new_cycle_time)) {
-                               new_cycle_time = value;
+                       if ((spd_value >= min_cycle_time) && (spd_value < new_cycle_time)) {
+                               new_cycle_time = spd_value;
                                new_latency = latency;
                        }
                }
@@ -1484,15 +1477,16 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
        /* Make a second pass through the dimms and disable
         * any that cannot support the selected memclk and cas latency.
         */
-       
-       for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
+
+       for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
                int latencies;
                int latency;
                int index;
-               int value;
+               int spd_value;
                if (!(dimm_mask & (1 << i))) {
                        continue;
                }
+
                latencies = spd_read_byte(ctrl->channel0[i], 18);
                if (latencies < 0) goto hw_error;
                if (latencies == 0) {
@@ -1503,7 +1497,7 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
                latency = log2(latencies) -2;
 
                /* Walk through searching for the selected latency */
-               for(index = 0; index < 3; index++, latency++) {
+               for (index = 0; index < 3; index++, latency++) {
                        if (!(latencies & (1 << latency))) {
                                continue;
                        }
@@ -1514,15 +1508,15 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
                if ((latency != min_latency) || (index >= 3)) {
                        goto dimm_err;
                }
-               
+
                /* Read the min_cycle_time for this latency */
-               value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
-               if (value < 0) goto hw_error;
-               
-               /* All is good if the selected clock speed 
+               spd_value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
+               if (spd_value < 0) goto hw_error;
+
+               /* All is good if the selected clock speed
                 * is what I need or slower.
                 */
-               if (value <= min_cycle_time) {
+               if (spd_value <= min_cycle_time) {
                        continue;
                }
                /* Otherwise I have an error, disable the dimm */
@@ -1531,26 +1525,26 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
        }
 #if 0
 //down speed for full load 4 rank support
-#if K8_4RANK_DIMM_SUPPORT
-       if(dimm_mask == (3|(3<<DIMM_SOCKETS)) ) {
+#if QRANK_DIMM_SUPPORT
+       if (dimm_mask == (3|(3<<DIMM_SOCKETS)) ) {
                int ranks = 4;
-               for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
+               for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
                        int val;
-                       if (!(dimm_mask & (1 << i))) {
-                               continue;
-                       }
-                       val = spd_read_byte(ctrl->channel0[i], 5);
-                       if(val!=ranks) {
+                       if (!(dimm_mask & (1 << i))) {
+                               continue;
+                       }
+                       val = spd_read_byte(ctrl->channel0[i], 5);
+                       if (val!=ranks) {
                                ranks = val;
                                break;
                        }
                }
-               if(ranks==4) {
-                       if(min_cycle_time <= 0x50 ) {
+               if (ranks==4) {
+                       if (min_cycle_time <= 0x50 ) {
                                min_cycle_time = 0x60;
                        }
                }
-               
+
        }
 #endif
 #endif
@@ -1562,8 +1556,8 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
        value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
 #if 0
        /* Improves DQS centering by correcting for case when core speed multiplier and MEMCLK speed result in odd clock divisor, by selecting the next lowest memory speed, required only at DDR400 and higher speeds with certain DIMM loadings ---- cheating???*/
-       if(!is_cpu_pre_e0()) {
-               if(min_cycle_time==0x50) {
+       if (!is_cpu_pre_e0()) {
+               if (min_cycle_time==0x50) {
                        value |= 1<<31;
                }
        }
@@ -1573,12 +1567,13 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
        pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
 
        static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
+
        /* Update DRAM Timing Low with our selected cas latency */
        value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
        value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
        value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
        pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
-       
+
        result.dimm_mask = dimm_mask;
        return result;
  hw_error:
@@ -1797,7 +1792,7 @@ static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_pa
 {
        uint32_t dcl;
        int value;
-#if K8_4RANK_DIMM_SUPPORT == 1
+#if QRANK_DIMM_SUPPORT == 1
        int rank;
 #endif
        int dimm;
@@ -1806,16 +1801,16 @@ static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_pa
                return -1;
        }
 
-#if K8_4RANK_DIMM_SUPPORT == 1
-       rank = spd_read_byte(ctrl->channel0[i], 5);       /* number of physical banks */
+#if QRANK_DIMM_SUPPORT == 1
+       rank = spd_read_byte(ctrl->channel0[i], 5);     /* number of physical banks */
        if (rank < 0) {
-               return -1;      
+               return -1;
        }
 #endif
 
        dimm = 1<<(DCL_x4DIMM_SHIFT+i);
-#if K8_4RANK_DIMM_SUPPORT == 1
-       if(rank==4) {
+#if QRANK_DIMM_SUPPORT == 1
+       if (rank==4) {
                dimm |= 1<<(DCL_x4DIMM_SHIFT+i+2);
        }
 #endif
@@ -1849,7 +1844,7 @@ static int count_dimms(const struct mem_controller *ctrl)
        int dimms;
        unsigned index;
        dimms = 0;
-       for(index = 0; index < 8; index += 2) {
+       for (index = 0; index < 8; index += 2) {
                uint32_t csbase;
                csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
                if (csbase & 1) {
@@ -1862,79 +1857,51 @@ static int count_dimms(const struct mem_controller *ctrl)
 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
 {
        uint32_t dth;
-       unsigned clocks;
-       clocks = 1; /* AMD says hard code this */
+
        dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
        dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
-       dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
+       dth |= ((param->dtl_twtr - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
        pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
 }
 
 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
 {
        uint32_t dth, dtl;
-       unsigned divisor;
        unsigned latency;
        unsigned clocks;
+       int lat, mtype;
 
        clocks = 0;
        dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
        latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
-       divisor = param->divisor;
 
        if (is_opteron(ctrl)) {
-               if (latency == DTL_CL_2) {
-                       if (divisor == ((6 << 0) + 0)) {
-                               /* 166Mhz */
-                               clocks = 3;
-                       }
-                       else if (divisor > ((6 << 0)+0)) {
-                               /* 100Mhz && 133Mhz */
-                               clocks = 2;
-                       }
-               }
-               else if (latency == DTL_CL_2_5) {
-                       clocks = 3;
-               }
-               else if (latency == DTL_CL_3) {
-                       if (divisor == ((6 << 0)+0)) {
-                               /* 166Mhz */
-                               clocks = 4;
-                       }
-                       else if (divisor > ((6 << 0)+0)) {
-                               /* 100Mhz && 133Mhz */
-                               clocks = 3;
-                       }
-               }
+               mtype = 0; /* dual channel */
+       } else if (is_registered(ctrl)) {
+               mtype = 1; /* registered 64bit interface */
+       } else {
+               mtype = 2; /* unbuffered 64bit interface */
        }
-       else /* Athlon64 */ {
-               if (is_registered(ctrl)) {
-                       if (latency == DTL_CL_2) {
-                               clocks = 2;
-                       }
-                       else if (latency == DTL_CL_2_5) {
-                               clocks = 3;
-                       }
-                       else if (latency == DTL_CL_3) {
-                               clocks = 3;
-                       }
-               }
-               else /* Unbuffered */{
-                       if (latency == DTL_CL_2) {
-                               clocks = 3;
-                       }
-                       else if (latency == DTL_CL_2_5) {
-                               clocks = 4;
-                       }
-                       else if (latency == DTL_CL_3) {
-                               clocks = 4;
-                       }
-               }
+
+       switch (latency) {
+               case DTL_CL_2:
+                       lat = 0;
+                       break;
+               case DTL_CL_2_5:
+                       lat = 1;
+                       break;
+               case DTL_CL_3:
+                       lat = 2;
+                       break;
+               default:
+                       die("Unknown LAT for Trwt");
        }
+
+       clocks = param->dtl_trwt[lat][mtype];
        if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
-               die("Unknown Trwt\r\n");
+               die("Unknown Trwt\n");
        }
-       
+
        dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
        dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
        dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
@@ -1962,83 +1929,38 @@ static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *
 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
 {
        uint32_t dch;
-       unsigned divisor;
        unsigned rdpreamble;
-       divisor = param->divisor;
-       dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
-       dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
-       rdpreamble = 0;
-       if (is_registered(ctrl)) {
-               if (divisor == ((10 << 1)+0)) {
-                       /* 100Mhz, 9ns */
-                       rdpreamble = ((9 << 1)+ 0);
-               }
-               else if (divisor == ((7 << 1)+1)) {
-                       /* 133Mhz, 8ns */
-                       rdpreamble = ((8 << 1)+0);
-               }
-               else if (divisor == ((6 << 1)+0)) {
-                       /* 166Mhz, 7.5ns */
-                       rdpreamble = ((7 << 1)+1);
-               }
-               else if (divisor == ((5 << 1)+0)) {
-                       /* 200Mhz,  7ns */
-                       rdpreamble = ((7 << 1)+0);
+       int slots, i;
+
+       slots = 0;
+
+       for (i = 0; i < 4; i++) {
+               if (ctrl->channel0[i]) {
+                       slots += 1;
                }
        }
-       else {
-               int slots;
-               int i;
-               slots = 0;
-               for(i = 0; i < 4; i++) {
-                       if (ctrl->channel0[i]) {
-                               slots += 1;
-                       }
-               }
-               if (divisor == ((10 << 1)+0)) {
-                       /* 100Mhz */
-                       if (slots <= 2) {
-                               /* 9ns */
-                               rdpreamble = ((9 << 1)+0);
-                       } else {
-                               /* 14ns */
-                               rdpreamble = ((14 << 1)+0);
-                       }
-               }
-               else if (divisor == ((7 << 1)+1)) {
-                       /* 133Mhz */
-                       if (slots <= 2) {
-                               /* 7ns */
-                               rdpreamble = ((7 << 1)+0);
-                       } else {
-                               /* 11 ns */
-                               rdpreamble = ((11 << 1)+0);
-                       }
-               }
-               else if (divisor == ((6 << 1)+0)) {
-                       /* 166Mhz */
-                       if (slots <= 2) {
-                               /* 6ns */
-                               rdpreamble = ((7 << 1)+0);
-                       } else {
-                               /* 9ns */
-                               rdpreamble = ((9 << 1)+0);
-                       }
-               }
-               else if (divisor == ((5 << 1)+0)) {
-                       /* 200Mhz */
-                       if (slots <= 2) {
-                               /* 5ns */
-                               rdpreamble = ((5 << 1)+0);
-                       } else {
-                               /* 7ns */
-                               rdpreamble = ((7 << 1)+0);
-                       }
-               }
+
+       /* map to index to param.rdpreamble array */
+       if (is_registered(ctrl)) {
+               i = 0;
+       } else if (slots < 3) {
+               i = 1;
+       } else if (slots == 3) {
+               i = 2;
+       } else if (slots == 4) {
+               i = 3;
+       } else {
+               die("Unknown rdpreamble for this nr of slots");
        }
+
+       dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
+       dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
+       rdpreamble = param->rdpreamble[i];
+
        if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
                die("Unknown rdpreamble");
        }
+
        dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
        pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
 }
@@ -2058,7 +1980,7 @@ static void set_max_async_latency(const struct mem_controller *ctrl, const struc
                if (dimms == 4) {
                        /* 9ns */
                        async_lat = 9;
-               } 
+               }
                else {
                        /* 8ns */
                        async_lat = 8;
@@ -2095,9 +2017,9 @@ static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct
 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
 {
        int i;
-       
+
        init_Tref(ctrl, param);
-       for(i = 0; i < DIMM_SOCKETS; i++) {
+       for (i = 0; i < DIMM_SOCKETS; i++) {
                int rc;
                if (!(dimm_mask & (1 << i))) {
                        continue;
@@ -2112,7 +2034,7 @@ static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct
 
                /* DRAM Timing High Register */
                if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
-       
+
 
                /* DRAM Config Low */
                if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
@@ -2139,14 +2061,18 @@ static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct
        return dimm_mask;
 }
 
-static void sdram_set_spd_registers(const struct mem_controller *ctrl) 
+#if RAMINIT_SYSINFO==1
+static void sdram_set_spd_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
+#else
+static void sdram_set_spd_registers(const struct mem_controller *ctrl)
+#endif
 {
        struct spd_set_memclk_result result;
        const struct mem_param *param;
        long dimm_mask;
 #if 1
        if (!controller_present(ctrl)) {
-//             print_debug("No memory controller present\r\n");
+//             printk(BIOS_DEBUG, "No memory controller present\n");
                return;
        }
 #endif
@@ -2154,22 +2080,22 @@ static void sdram_set_spd_registers(const struct mem_controller *ctrl)
        activate_spd_rom(ctrl);
        dimm_mask = spd_detect_dimms(ctrl);
        if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
-               print_debug("No memory for this cpu\r\n");
+               printk(BIOS_DEBUG, "No memory for this cpu\n");
                return;
        }
-       dimm_mask = spd_enable_2channels(ctrl, dimm_mask);        
-       if (dimm_mask < 0) 
+       dimm_mask = spd_enable_2channels(ctrl, dimm_mask);
+       if (dimm_mask < 0)
                goto hw_spd_err;
-       dimm_mask = spd_set_ram_size(ctrl , dimm_mask);           
-       if (dimm_mask < 0) 
+       dimm_mask = spd_set_ram_size(ctrl , dimm_mask);
+       if (dimm_mask < 0)
                goto hw_spd_err;
-       dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask); 
-       if (dimm_mask < 0) 
+       dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask);
+       if (dimm_mask < 0)
                goto hw_spd_err;
        result = spd_set_memclk(ctrl, dimm_mask);
        param     = result.param;
        dimm_mask = result.dimm_mask;
-       if (dimm_mask < 0) 
+       if (dimm_mask < 0)
                goto hw_spd_err;
        dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
        if (dimm_mask < 0)
@@ -2178,127 +2104,139 @@ static void sdram_set_spd_registers(const struct mem_controller *ctrl)
        return;
  hw_spd_err:
        /* Unrecoverable error reading SPD data */
-       print_err("SPD error - reset\r\n");
+       printk(BIOS_ERR, "SPD error - reset\n");
        hard_reset();
        return;
 }
 
-#if HW_MEM_HOLE_SIZEK != 0
+#if CONFIG_HW_MEM_HOLE_SIZEK != 0
 static uint32_t hoist_memory(int controllers, const struct mem_controller *ctrl,unsigned hole_startk, int i)
 {
-        int ii;
-        uint32_t carry_over;
-        device_t dev;
-        uint32_t base, limit;
-        uint32_t basek;
-        uint32_t hoist;
-        int j;
-
-        carry_over = (4*1024*1024) - hole_startk;
-
-        for(ii=controllers - 1;ii>i;ii--) {
-                base  = pci_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
-                if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
-                        continue;
-                }
+       int ii;
+       uint32_t carry_over;
+       device_t dev;
+       uint32_t base, limit;
+       uint32_t basek;
+       uint32_t hoist;
+       int j;
+
+       carry_over = (4*1024*1024) - hole_startk;
+
+       for (ii=controllers - 1;ii>i;ii--) {
+               base  = pci_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
+               if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
+                       continue;
+               }
                limit = pci_read_config32(ctrl[0].f1, 0x44 + (ii << 3));
-                for(j = 0; j < controllers; j++) {
-                        pci_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit + (carry_over << 2));
-                        pci_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base + (carry_over << 2));
-                }
-        }
-        limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
-        for(j = 0; j < controllers; j++) {
-                pci_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit + (carry_over << 2));
-        }
-        dev = ctrl[i].f1;
-        base  = pci_read_config32(dev, 0x40 + (i << 3));
-        basek  = (base & 0xffff0000) >> 2;
-        if(basek == hole_startk) {
-                //don't need set memhole here, because hole off set will be 0, overflow
-                //so need to change base reg instead, new basek will be 4*1024*1024
-                base &= 0x0000ffff;
-                base |= (4*1024*1024)<<2;
-                for(j = 0; j < controllers; j++) {
-                        pci_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
-                }
-        }
+               for (j = 0; j < controllers; j++) {
+                       pci_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit + (carry_over << 2));
+                       pci_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base + (carry_over << 2));
+               }
+       }
+       limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
+       for (j = 0; j < controllers; j++) {
+               pci_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit + (carry_over << 2));
+       }
+       dev = ctrl[i].f1;
+       base  = pci_read_config32(dev, 0x40 + (i << 3));
+       basek  = (base & 0xffff0000) >> 2;
+       if (basek == hole_startk) {
+               //don't need set memhole here, because hole off set will be 0, overflow
+               //so need to change base reg instead, new basek will be 4*1024*1024
+               base &= 0x0000ffff;
+               base |= (4*1024*1024)<<2;
+               for (j = 0; j < controllers; j++) {
+                       pci_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
+               }
+       }
        else {
-               hoist = /* hole start address */
-                       ((hole_startk << 10) & 0xff000000) +
-                       /* hole address to memory controller address */
-                       (((basek + carry_over) >> 6) & 0x0000ff00) +
-                       /* enable */
-                       1;
-               pci_write_config32(dev, 0xf0, hoist);
+               hoist = /* hole start address */
+                       ((hole_startk << 10) & 0xff000000) +
+                       /* hole address to memory controller address */
+                       (((basek + carry_over) >> 6) & 0x0000ff00) +
+                       /* enable */
+                       1;
+               pci_write_config32(dev, 0xf0, hoist);
        }
 
-        return carry_over;
+       return carry_over;
 }
 
 static void set_hw_mem_hole(int controllers, const struct mem_controller *ctrl)
 {
 
-        uint32_t hole_startk;
-        int i;
-
-        hole_startk = 4*1024*1024 - HW_MEM_HOLE_SIZEK;
-
-#if HW_MEM_HOLE_SIZE_AUTO_INC == 1 
-        //We need to double check if the hole_startk is valid, if it is equal to basek, we need to decrease it some
-        uint32_t basek_pri;
-        for(i=0; i<controllers; i++) {
-                        uint32_t base;
-                        unsigned base_k;
-                        base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
-                        if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
-                                continue;
-                        }
-                        base_k = (base & 0xffff0000) >> 2;
-                        if(base_k == hole_startk) {
-                                hole_startk -= (base_k - basek_pri)>>1; // decrease mem hole startk to make sure it is on middle of privous node
-                                break; //only one hole
-                        }
-                        basek_pri = base_k;
-        }
+       uint32_t hole_startk;
+       int i;
+
+       hole_startk = 4*1024*1024 - CONFIG_HW_MEM_HOLE_SIZEK;
 
+       printk(BIOS_SPEW, "Handling memory hole at 0x%08x (default)\n", hole_startk);
+#if CONFIG_HW_MEM_HOLE_SIZE_AUTO_INC == 1
+       /* We need to double check if hole_startk is valid.
+        * If it is equal to the dram base address in K (base_k),
+        * we need to decrease it.
+        */
+       uint32_t basek_pri;
+       for (i=0; i<controllers; i++) {
+                       uint32_t base;
+                       unsigned base_k;
+                       base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
+                       if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
+                               continue;
+                       }
+                       base_k = (base & 0xffff0000) >> 2;
+                       if (base_k == hole_startk) {
+                               /* decrease memory hole startk to make sure it is
+                                * in the middle of the previous node
+                                */
+                               hole_startk -= (base_k - basek_pri)>>1;
+                               break; /* only one hole */
+                       }
+                       basek_pri = base_k;
+       }
+
+       printk(BIOS_SPEW, "Handling memory hole at 0x%08x (adjusted)\n", hole_startk);
 #endif
-        //find node index that need do set hole
-        for(i=0; i<controllers; i++) {
-                        uint32_t base, limit;
-                        unsigned base_k, limit_k;
-                        base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
-                        if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
-                                continue;
-                        }
-                        limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
-                        base_k = (base & 0xffff0000) >> 2;
-                        limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
+       /* Find node number that needs the memory hole configured */
+       for (i=0; i<controllers; i++) {
+                       uint32_t base, limit;
+                       unsigned base_k, limit_k;
+                       base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
+                       if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
+                               continue;
+                       }
+                       limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
+                       base_k = (base & 0xffff0000) >> 2;
+                       limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
                        if ((base_k <= hole_startk) && (limit_k > hole_startk)) {
-                                unsigned end_k;
-                                hoist_memory(controllers, ctrl, hole_startk, i);
-                                end_k = memory_end_k(ctrl, controllers);
-                                set_top_mem(end_k, hole_startk);
-                                break; //only one hole
-                        }
-        }
+                               unsigned end_k;
+                               hoist_memory(controllers, ctrl, hole_startk, i);
+                               end_k = memory_end_k(ctrl, controllers);
+                               set_top_mem(end_k, hole_startk);
+                               break; /* only one hole */
+                       }
+       }
 
 }
 
 #endif
 
 #define TIMEOUT_LOOPS 300000
+#if RAMINIT_SYSINFO == 1
+static void sdram_enable(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
+#else
 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
+#endif
 {
        int i;
 
        /* Error if I don't have memory */
        if (memory_end_k(ctrl, controllers) == 0) {
-               die("No memory\r\n");
+               die("No memory\n");
        }
 
        /* Before enabling memory start the memory clocks */
-       for(i = 0; i < controllers; i++) {
+       for (i = 0; i < controllers; i++) {
                uint32_t dch;
                if (!controller_present(ctrl + i))
                        continue;
@@ -2316,10 +2254,11 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                }
        }
 
+       /* We need to wait a minimum of 20 MEMCLKS to enable the InitDram */
        /* And if necessary toggle the the reset on the dimms by hand */
        memreset(controllers, ctrl);
 
-       for(i = 0; i < controllers; i++) {
+       for (i = 0; i < controllers; i++) {
                uint32_t dcl, dch;
                if (!controller_present(ctrl + i))
                        continue;
@@ -2333,7 +2272,7 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
                if (dcl & DCL_DimmEccEn) {
                        uint32_t mnc;
-                       print_spew("ECC enabled\r\n");
+                       printk(BIOS_SPEW, "ECC enabled\n");
                        mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
                        mnc |= MNC_ECC_EN;
                        if (dcl & DCL_128BitEn) {
@@ -2349,9 +2288,9 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                dcl &= ~DCL_QFC_EN;
                dcl |= DCL_DramInit;
                pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
-
        }
-       for(i = 0; i < controllers; i++) {
+
+       for (i = 0; i < controllers; i++) {
                uint32_t dcl, dch;
                if (!controller_present(ctrl + i))
                        continue;
@@ -2361,18 +2300,17 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                        continue;
                }
 
-               print_debug("Initializing memory: ");
-
+               printk(BIOS_DEBUG, "Initializing memory: ");
                int loops = 0;
                do {
                        dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
-                       loops += 1;
+                       loops++;
                        if ((loops & 1023) == 0) {
-                               print_debug(".");
+                               printk(BIOS_DEBUG, ".");
                        }
                } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
                if (loops >= TIMEOUT_LOOPS) {
-                       print_debug(" failed\r\n");
+                       printk(BIOS_DEBUG, " failed\n");
                        continue;
                }
 
@@ -2385,14 +2323,14 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                        } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
                }
 
-               print_debug(" done\r\n");
+               printk(BIOS_DEBUG, " done\n");
        }
 
-#if HW_MEM_HOLE_SIZEK != 0
-         // init hw mem hole here
-        /* DramHoleValid bit only can be set after MemClrStatus is set by Hardware */
-       if(!is_cpu_pre_e0())
-               set_hw_mem_hole(controllers, ctrl);
+#if CONFIG_HW_MEM_HOLE_SIZEK != 0
+        // init hw mem hole here
+       /* DramHoleValid bit only can be set after MemClrStatus is set by Hardware */
+       if (!is_cpu_pre_e0())
+               set_hw_mem_hole(controllers, ctrl);
 #endif
 
        //FIXME add enable node interleaving here -- yhlu
@@ -2404,75 +2342,31 @@ static void sdram_enable(int controllers, const struct mem_controller *ctrl)
                5. for node interleaving we need to set mem hole to every node ( need recalcute hole offset in f0 for every node)
        */
 
-#if USE_DCACHE_RAM == 0
-       /* Make certain the first 1M of memory is intialized */
-       print_debug("Clearing initial memory region: ");
-
-       /* Use write combine caching while we setup the  first 1M */
-       cache_lbmem(MTRR_TYPE_WRCOMB);
-
-       /* clear memory 1meg */
-       clear_memory((void *)0, CONFIG_LB_MEM_TOPK << 10);
-
-       /* The first 1M is now setup, use it */
-       cache_lbmem(MTRR_TYPE_WRBACK);
-       
-       print_debug(" done\r\n");
-#endif
 }
 
-static int mem_inited(int controllers, const struct mem_controller *ctrl)
-{
-        int i;
-
-        unsigned mask = 0;
-        unsigned mask_inited = 0;
-
-        for(i = 0; i < controllers; i++) {
-                uint32_t dcl;
-                if (!controller_present(ctrl + i))
-                        continue;
-
-                mask |= (1<<i);
-                dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
-
-                if (!is_cpu_pre_c0()) { // B3
-
-                        if(  (dcl & DCL_MemClrStatus)  && (dcl & DCL_DramEnable) ) {
-                                mask_inited |= (1<<i);
-                        }
-                }
-        }
-
-        if(mask == mask_inited) return 1;
-
-        return 0;
-
-}
-#if USE_DCACHE_RAM == 1
 static void set_sysinfo_in_ram(unsigned val)
 {
 }
 
-static void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a, const uint16_t *spd_addr)
+static void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a,
+                         const uint16_t *spd_addr)
 {
-        int i;
-        int j;
-        struct mem_controller *ctrl;
-        for(i=0;i<controllers; i++) {
-                ctrl = &ctrl_a[i];
-                ctrl->node_id = i;
-                ctrl->f0 = PCI_DEV(0, 0x18+i, 0);
-                ctrl->f1 = PCI_DEV(0, 0x18+i, 1);
-                ctrl->f2 = PCI_DEV(0, 0x18+i, 2);
-                ctrl->f3 = PCI_DEV(0, 0x18+i, 3);
-
-                if(spd_addr == (void *)0) continue;
-
-                for(j=0;j<DIMM_SOCKETS;j++) {
-                        ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
-                        ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];
-                }
-        }
+       int i;
+       int j;
+       struct mem_controller *ctrl;
+       for (i=0;i<controllers; i++) {
+               ctrl = &ctrl_a[i];
+               ctrl->node_id = i;
+               ctrl->f0 = PCI_DEV(0, 0x18+i, 0);
+               ctrl->f1 = PCI_DEV(0, 0x18+i, 1);
+               ctrl->f2 = PCI_DEV(0, 0x18+i, 2);
+               ctrl->f3 = PCI_DEV(0, 0x18+i, 3);
+
+               if (spd_addr == (void *)0) continue;
+
+               for (j=0;j<DIMM_SOCKETS;j++) {
+                       ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
+                       ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];
+               }
+       }
 }
-#endif