Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / mainboard / olpc / btest / 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 /* sdram parameters for OLPC:
39         row address = 13
40         col address = 9
41         banks = 4
42         dimm0size=128MB
43         d0_MB=1 (module banks)
44         d0_cb=4 (component banks)
45         do_psz=4KB      (page size)
46         Trc=10 (clocks) (ref2act)
47         Tras=7 (act2pre)
48         Trcd=3 (act2cmd)
49         Trp=3   (pre2act)
50         Trrd=2 (act2act)
51         Tref=17.8ms
52   */
53 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
54 {
55         /* Total size of DIMM = 2^row address (byte 3) * 2^col address (byte 4) *
56          *                      component Banks (byte 17) * module banks, side (byte 5) *
57          *                      width in bits (byte 6,7)
58          *                    = Density per side (byte 31) * number of sides (byte 5) */
59         /* 1. Initialize GLMC registers base on SPD values, do one DIMM for now */
60         msr_t msr;
61         unsigned char module_banks, val;
62
63         msr = rdmsr(MC_CF07_DATA);
64
65         /* get module banks (sides) per dimm, SPD byte 5 */
66         module_banks = 1;
67         module_banks >>= 1;
68         msr.hi &= ~(1 << CF07_UPPER_D0_MB_SHIFT);
69         msr.hi |= (module_banks << CF07_UPPER_D0_MB_SHIFT);
70
71         /* get component banks per module bank, SPD byte 17 */
72         val = 4;
73         val >>= 2;
74         msr.hi &= ~(0x1 << CF07_UPPER_D0_CB_SHIFT);
75         msr.hi |=  (val << CF07_UPPER_D0_CB_SHIFT);
76
77         /* get the module bank density, SPD byte 31  */
78         /* this is multiples of 8 MB */
79         /* actually it is 2^x*4, where x is the value you put in */
80         /* for OLPC, set default size */
81         /* dimm size - hardcoded 128Mb */
82         val = 5;
83         msr.hi &= ~(0xf << CF07_UPPER_D0_SZ_SHIFT);
84         msr.hi |=  (val << CF07_UPPER_D0_SZ_SHIFT);
85
86         /* page size = 2^col address */
87         val = 2; /* 4096 bytes */
88         msr.hi &= ~(0x7 << CF07_UPPER_D0_PSZ_SHIFT);
89         msr.hi |=  (val << CF07_UPPER_D0_PSZ_SHIFT);
90
91         print_debug("computed msr.hi ");
92         print_debug_hex32(msr.hi);
93         print_debug("\n");
94
95         /* this is a standard value, DOES NOT PROBABLY MATCH FROM ABOVE */
96         /* well, it may be close. It's about 200,000 ticks */
97         msr.lo = 0x00003000;
98         wrmsr(MC_CF07_DATA, msr);
99
100         /* timing and mode ... */
101
102         msr = rdmsr(0x20000019);
103
104         /* per standard bios settings */
105
106         msr.hi = 0x18000108;
107         msr.lo =
108                         (6<<28) |               // cas_lat
109                         (10<<24)|               // ref2act
110                         (7<<20)|                // act2pre
111                         (3<<16)|                // pre2act
112                         (3<<12)|                // act2cmd
113                         (2<<8)|                 // act2act
114                         (2<<6)|                 // dplwr
115                         (2<<4)|                 // dplrd
116                         (3);                    // dal
117         /* the msr value reported by quanta is very, very different.
118          * we will go with that value for now.
119          */
120         msr.lo = 0x286332a3;
121
122         wrmsr(0x20000019, msr);
123
124 }
125
126 #include "northbridge/amd/gx2/raminit.c"
127 #include "lib/generic_sdram.c"
128
129 #define PLLMSRhi 0x00001490
130 #define PLLMSRlo 0x02000030
131 #define PLLMSRlo1 ((0xde << 16) | (1 << 26) | (1 << 24))
132 #define PLLMSRlo2 ((1<<14) |(1<<13) | (1<<0))
133 #include "northbridge/amd/gx2/pll_reset.c"
134 #include "cpu/amd/model_gx2/cpureginit.c"
135 #include "cpu/amd/model_gx2/syspreinit.c"
136 static void msr_init(void)
137 {
138         __builtin_wrmsr(0x1808,  0x10f3bf00, 0x22fffc02);
139
140         __builtin_wrmsr(0x10000020, 0xfff80, 0x20000000);
141         __builtin_wrmsr(0x10000021, 0x80fffe0, 0x20000000);
142
143         __builtin_wrmsr(0x40000020, 0xfff80, 0x20000000);
144         __builtin_wrmsr(0x40000021, 0x80fffe0, 0x20000000);
145 }
146
147 static void gpio_init(void)
148 {
149         unsigned long m;
150
151         /* Make sure events enable for gpio 12 is off */
152
153         m = inl(GPIOL_EVENTS_ENABLE);
154         m &= ~GPIOL_12_SET;
155         m |= GPIOL_12_CLEAR;
156         outl(m, GPIOL_EVENTS_ENABLE);
157 }
158
159 static void main(unsigned long bist)
160 {
161         static const struct mem_controller memctrl [] = {
162                 {.channel0 = {(0xa<<3)|0, (0xa<<3)|1}}
163         };
164
165         SystemPreInit();
166         msr_init();
167
168         cs5536_early_setup();
169
170         /* NOTE: must do this AFTER the early_setup!
171          * it is counting on some early MSR setup
172          * for cs5536
173          */
174         cs5536_setup_onchipuart(1);
175         gpio_init();
176         uart_init();
177         console_init();
178
179         pll_reset();
180
181         cpuRegInit();
182         print_err("done cpuRegInit\n");
183
184         sdram_initialize(1, memctrl);
185
186         /* Check all of memory */
187         //ram_check(0x00000000, 640*1024);
188 }
189