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