This patch converts the Geode GX2 boards to CAR.
[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/hlt.h>
6 #include <console/console.h>
7 #include "lib/ramtest.c"
8 #include "superio/winbond/w83627hf/w83627hf_early_serial.c"
9 #include "cpu/x86/bist.h"
10 #include "cpu/x86/msr.h"
11 #include <cpu/amd/gx2def.h>
12
13 #define SERIAL_DEV PNP_DEV(0x2e, W83627HF_SP1)
14
15 #include "southbridge/amd/cs5536/cs5536_early_smbus.c"
16 #include "southbridge/amd/cs5536/cs5536_early_setup.c"
17
18 static inline int spd_read_byte(unsigned device, unsigned address)
19 {
20         return smbus_read_byte(device, address);
21 }
22
23 #include "northbridge/amd/gx2/raminit.h"
24
25 static inline unsigned int fls(unsigned int x)
26 {
27         int r;
28
29         __asm__("bsfl %1,%0\n\t"
30                 "jnz 1f\n\t"
31                 "movl $32,%0\n"
32                 "1:" : "=r" (r) : "g" (x));
33         return r;
34 }
35
36 /* sdram parameters for OLPC:
37         row address = 13
38         col address = 9
39         banks = 4
40         dimm0size=128MB
41         d0_MB=1 (module banks)
42         d0_cb=4 (component banks)
43         do_psz=4KB      (page size)
44         Trc=10 (clocks) (ref2act)
45         Tras=7 (act2pre)
46         Trcd=3 (act2cmd)
47         Trp=3   (pre2act)
48         Trrd=2 (act2act)
49         Tref=17.8ms
50   */
51 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
52 {
53         /* Total size of DIMM = 2^row address (byte 3) * 2^col address (byte 4) *
54          *                      component Banks (byte 17) * module banks, side (byte 5) *
55          *                      width in bits (byte 6,7)
56          *                    = Density per side (byte 31) * number of sides (byte 5) */
57         /* 1. Initialize GLMC registers base on SPD values, do one DIMM for now */
58         msr_t msr;
59         unsigned char module_banks, val;
60
61         msr = rdmsr(MC_CF07_DATA);
62
63         /* get module banks (sides) per dimm, SPD byte 5 */
64         module_banks = 1;
65         module_banks >>= 1;
66         msr.hi &= ~(1 << CF07_UPPER_D0_MB_SHIFT);
67         msr.hi |= (module_banks << CF07_UPPER_D0_MB_SHIFT);
68
69         /* get component banks per module bank, SPD byte 17 */
70         val = 4;
71         val >>= 2;
72         msr.hi &= ~(0x1 << CF07_UPPER_D0_CB_SHIFT);
73         msr.hi |=  (val << CF07_UPPER_D0_CB_SHIFT);
74
75         /* get the module bank density, SPD byte 31  */
76         /* this is multiples of 8 MB */
77         /* actually it is 2^x*4, where x is the value you put in */
78         /* for OLPC, set default size */
79         /* dimm size - hardcoded 128Mb */
80         val = 5;
81         msr.hi &= ~(0xf << CF07_UPPER_D0_SZ_SHIFT);
82         msr.hi |=  (val << CF07_UPPER_D0_SZ_SHIFT);
83
84         /* page size = 2^col address */
85         val = 2; /* 4096 bytes */
86         msr.hi &= ~(0x7 << CF07_UPPER_D0_PSZ_SHIFT);
87         msr.hi |=  (val << CF07_UPPER_D0_PSZ_SHIFT);
88
89         print_debug("computed msr.hi ");
90         print_debug_hex32(msr.hi);
91         print_debug("\n");
92
93         /* this is a standard value, DOES NOT PROBABLY MATCH FROM ABOVE */
94         /* well, it may be close. It's about 200,000 ticks */
95         msr.lo = 0x00003000;
96         wrmsr(MC_CF07_DATA, msr);
97
98         /* timing and mode ... */
99
100         msr = rdmsr(0x20000019);
101
102         /* per standard bios settings */
103
104         msr.hi = 0x18000108;
105         msr.lo =
106                         (6<<28) |               // cas_lat
107                         (10<<24)|               // ref2act
108                         (7<<20)|                // act2pre
109                         (3<<16)|                // pre2act
110                         (3<<12)|                // act2cmd
111                         (2<<8)|                 // act2act
112                         (2<<6)|                 // dplwr
113                         (2<<4)|                 // dplrd
114                         (3);                    // dal
115         /* the msr value reported by quanta is very, very different.
116          * we will go with that value for now.
117          */
118         msr.lo = 0x286332a3;
119
120         wrmsr(0x20000019, msr);
121
122 }
123
124 #include "northbridge/amd/gx2/raminit.c"
125 #include "lib/generic_sdram.c"
126
127 #define PLLMSRhi 0x00001490
128 #define PLLMSRlo 0x02000030
129 #define PLLMSRlo1 ((0xde << 16) | (1 << 26) | (1 << 24))
130 #define PLLMSRlo2 ((1<<14) |(1<<13) | (1<<0))
131 #include "northbridge/amd/gx2/pll_reset.c"
132 #include "cpu/amd/model_gx2/cpureginit.c"
133 #include "cpu/amd/model_gx2/syspreinit.c"
134 #include "cpu/amd/model_lx/msrinit.c"
135
136 static void gpio_init(void)
137 {
138         unsigned long m;
139
140         /* Make sure events enable for gpio 12 is off */
141
142         m = inl(GPIOL_EVENTS_ENABLE);
143         m &= ~GPIOL_12_SET;
144         m |= GPIOL_12_CLEAR;
145         outl(m, GPIOL_EVENTS_ENABLE);
146 }
147
148 void main(unsigned long bist)
149 {
150         static const struct mem_controller memctrl [] = {
151                 {.channel0 = {(0xa<<3)|0, (0xa<<3)|1}}
152         };
153
154         SystemPreInit();
155         msr_init();
156
157         cs5536_early_setup();
158
159         /* NOTE: must do this AFTER the early_setup!
160          * it is counting on some early MSR setup
161          * for cs5536
162          */
163         cs5536_setup_onchipuart(1);
164         gpio_init();
165         uart_init();
166         console_init();
167
168         /* Halt if there was a built in self test failure */
169         report_bist_failure(bist);
170
171         pll_reset();
172
173         cpuRegInit();
174         print_err("done cpuRegInit\n");
175
176         sdram_initialize(1, memctrl);
177
178         /* Check all of memory */
179         //ram_check(0x00000000, 640*1024);
180 }
181