813b009471f34157992358e482b24c85932c242f
[coreboot.git] / src / mainboard / amd / rumba / romstage.c
1 #include <stdint.h>
2 #include <device/pci_def.h>
3 #include <arch/io.h>
4 #include <device/pnp_def.h>
5 #include <arch/romcc_io.h>
6 #include <arch/hlt.h>
7 #include "pc80/serial.c"
8 #include "console/console.c"
9 #include "lib/ramtest.c"
10 #include "superio/winbond/w83627hf/w83627hf_early_serial.c"
11 #include "cpu/x86/bist.h"
12 #include "cpu/x86/msr.h"
13 #include <cpu/amd/gx2def.h>
14
15 #define SERIAL_DEV PNP_DEV(0x2e, W83627HF_SP1)
16
17 #include "southbridge/amd/cs5536/cs5536_early_smbus.c"
18 #include "southbridge/amd/cs5536/cs5536_early_setup.c"
19
20 static inline int spd_read_byte(unsigned device, unsigned address)
21 {
22         return smbus_read_byte(device, address);
23 }
24
25 #include "northbridge/amd/gx2/raminit.h"
26
27 static inline unsigned int fls(unsigned int x)
28 {
29         int r;
30
31         __asm__("bsfl %1,%0\n\t"
32                 "jnz 1f\n\t"
33                 "movl $32,%0\n"
34                 "1:" : "=r" (r) : "g" (x));
35         return r;
36 }
37
38 static void sdram_set_spd_registers(const struct mem_controller *ctrl) 
39 {
40         /* Total size of DIMM = 2^row address (byte 3) * 2^col address (byte 4) *
41          *                      component Banks (byte 17) * module banks, side (byte 5) *
42          *                      width in bits (byte 6,7)
43          *                    = Density per side (byte 31) * number of sides (byte 5) */
44         /* 1. Initialize GLMC registers base on SPD values, do one DIMM for now */
45         msr_t msr;
46         unsigned char module_banks, val;
47
48         msr = rdmsr(MC_CF07_DATA);
49
50         /* get module banks (sides) per dimm, SPD byte 5 */
51         module_banks = spd_read_byte(0xA0, 5);
52         if (module_banks < 1 || module_banks > 2)
53                 print_err("Module banks per dimm\n");
54         module_banks >>= 1;
55         msr.hi &= ~(1 << CF07_UPPER_D0_MB_SHIFT);
56         msr.hi |= (module_banks << CF07_UPPER_D0_MB_SHIFT);
57
58         /* get component banks per module bank, SPD byte 17 */
59         val = spd_read_byte(0xA0, 17);
60         if (val < 2 || val > 4)
61                 print_err("Component banks per module bank\n");
62         val >>= 2;
63         msr.hi &= ~(0x1 << CF07_UPPER_D0_CB_SHIFT);
64         msr.hi |=  (val << CF07_UPPER_D0_CB_SHIFT);
65
66         /* get the module bank density, SPD byte 31  */
67         val = spd_read_byte(0xA0, 31);
68         val = fls(val);
69         val <<= module_banks;
70         msr.hi &= ~(0xf << CF07_UPPER_D0_SZ_SHIFT);
71         msr.hi |=  (val << CF07_UPPER_D0_SZ_SHIFT);
72
73         /* page size = 2^col address */
74         val = spd_read_byte(0xA0, 4);
75         val -= 7;
76         msr.hi &= ~(0x7 << CF07_UPPER_D0_PSZ_SHIFT);
77         msr.hi |=  (val << CF07_UPPER_D0_PSZ_SHIFT);
78
79         print_debug("computed msr.hi ");
80         print_debug_hex32(msr.hi);
81         print_debug("\n");
82
83         msr.lo = 0x00003000;
84         wrmsr(MC_CF07_DATA, msr);
85
86         msr = rdmsr(0x20000019);
87         msr.hi = 0x18000108;
88         msr.lo = 0x696332a3;
89         wrmsr(0x20000019, msr);         
90
91 }
92
93 #include "northbridge/amd/gx2/raminit.c"
94 #include "lib/generic_sdram.c"
95
96 #define PLLMSRhi 0x00001490
97 #define PLLMSRlo 0x02000030
98 #define PLLMSRlo1 ((0xde << 16) | (1 << 26) | (1 << 24))
99 #define PLLMSRlo2 ((1<<14) |(1<<13) | (1<<0))
100 #include "northbridge/amd/gx2/pll_reset.c"
101 #include "cpu/amd/model_gx2/cpureginit.c"
102 #include "cpu/amd/model_gx2/syspreinit.c"
103 static void msr_init(void)
104 {
105         /* total physical memory */
106         __builtin_wrmsr(0x1808,  0x10f3bf00, 0x22fffc02);
107
108         /* traditional memory 0kB-512kB, 512kB-1MB */
109         __builtin_wrmsr(0x10000020, 0xfff80, 0x20000000);
110         __builtin_wrmsr(0x10000021, 0x80fffe0, 0x20000000);
111
112         __builtin_wrmsr(0x40000020, 0xfff80, 0x20000000);
113         __builtin_wrmsr(0x40000021, 0x80fffe0, 0x20000000);
114
115         /* put code in northbridge[init].c here */
116 }
117
118 static void main(unsigned long bist)
119 {
120         static const struct mem_controller memctrl [] = {
121                 {.channel0 = {(0xa<<3)|0, (0xa<<3)|1}}
122         };
123
124         SystemPreInit();
125         
126
127         w83627hf_enable_serial(SERIAL_DEV, CONFIG_TTYS0_BASE);
128         uart_init();
129         console_init();
130
131         cs5536_early_setup();
132
133         pll_reset();
134
135         cpuRegInit();
136         print_err("done cpuRegInit\n");
137         
138         sdram_initialize(1, memctrl);
139
140         msr_init();
141
142         /* Check all of memory */
143         //ram_check(0x00000000, 640*1024);
144 }
145