This patch converts the Geode GX2 boards to CAR.
[coreboot.git] / src / mainboard / wyse / s50 / romstage.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Nils Jacobs
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <stdint.h>
23 #include <device/pci_def.h>
24 #include <arch/io.h>
25 #include <device/pnp_def.h>
26 #include <arch/hlt.h>
27 #include <console/console.h>
28 #include "lib/ramtest.c"
29 #include "cpu/x86/bist.h"
30 #include "cpu/x86/msr.h"
31 #include <cpu/amd/gx2def.h>
32
33 #include "southbridge/amd/cs5536/cs5536_early_smbus.c"
34 #include "southbridge/amd/cs5536/cs5536_early_setup.c"
35
36 static inline int spd_read_byte(unsigned device, unsigned address)
37 {
38         return smbus_read_byte(device, address);
39 }
40
41 #include "northbridge/amd/gx2/raminit.h"
42
43         /* This is needed because ROMCC doesn`t now the ctz bitop */
44 static inline unsigned int ctz(unsigned int n)
45 {
46         int zeros;
47
48         n = (n ^ (n - 1)) >> 1;
49         for (zeros = 0; n; zeros++)
50         {
51           n >>= 1;
52         }
53         return zeros;
54 }
55
56 static void sdram_set_spd_registers(const struct mem_controller *ctrl) 
57 {
58         /* Total size of DIMM = 2^row address (byte 3) * 2^col address (byte 4) *
59          *                      component Banks (byte 17) * module banks, side (byte 5) *
60          *                      width in bits (byte 6,7)
61          *                    = Density per side (byte 31) * number of sides (byte 5) */
62         /* 1. Initialize GLMC registers base on SPD values, do one DIMM for now */
63         msr_t msr;
64         unsigned char module_banks, val;
65         uint16_t dimm_size;
66
67         msr = rdmsr(MC_CF07_DATA);
68
69         /* get module banks (sides) per dimm, SPD byte 5 */
70         module_banks = spd_read_byte(0xA0, 5);
71         if (module_banks < 1 || module_banks > 2)
72                 print_err("Module banks per dimm\n");
73         module_banks >>= 1;
74         msr.hi &= ~(1 << CF07_UPPER_D0_MB_SHIFT);
75         msr.hi |= (module_banks << CF07_UPPER_D0_MB_SHIFT);
76
77         /* get component banks per module bank, SPD byte 17 */
78         val = spd_read_byte(0xA0, 17);
79         if (val < 2 || val > 4)
80                 print_err("Component banks per module bank\n");
81         val >>= 2;
82         msr.hi &= ~(0x1 << CF07_UPPER_D0_CB_SHIFT);
83         msr.hi |=  (val << CF07_UPPER_D0_CB_SHIFT);
84
85         dimm_size = spd_read_byte(0xA0, 31);
86         dimm_size |= (dimm_size << 8);  /* align so 1GB(bit0) is bit 8, this is a little weird to get gcc to not optimize this out */
87         dimm_size &= 0x01FC;    /* and off 2GB DIMM size : not supported and the 1GB size we just moved up to bit 8 as well as all the extra on top */
88         /*       Module Density * Module Banks */
89         dimm_size <<= (0 >> CF07_UPPER_D0_MB_SHIFT) & 1;        /* shift to multiply by # DIMM banks */
90         if (dimm_size != 0) {
91         dimm_size = ctz(dimm_size);
92         }
93         if (dimm_size > 7) {    /* 7 is 512MB only support 512MB per DIMM */
94                 print_err("Only support up to 512MB \n");
95                 hlt();
96         }
97         msr.hi |= dimm_size << CF07_UPPER_D0_SZ_SHIFT;
98
99         /* page size = 2^col address */
100         val = spd_read_byte(0xA0, 4);
101         val -= 7;
102         msr.hi &= ~(0x7 << CF07_UPPER_D0_PSZ_SHIFT);
103         msr.hi |=  (val << CF07_UPPER_D0_PSZ_SHIFT);
104
105         print_debug("computed msr.hi ");
106         print_debug_hex32(msr.hi);
107         print_debug("\n");
108
109         msr.lo = 0x00003400;
110         wrmsr(MC_CF07_DATA, msr);
111
112         msr = rdmsr(MC_CF8F_DATA);
113         msr.hi = 0x18000008;
114         msr.lo = 0x296332a3;
115         wrmsr(MC_CF8F_DATA, msr);               
116
117 }
118
119 #include "northbridge/amd/gx2/raminit.c"
120 #include "lib/generic_sdram.c"
121 #include "northbridge/amd/gx2/pll_reset.c"
122 #include "cpu/amd/model_gx2/cpureginit.c"
123 #include "cpu/amd/model_gx2/syspreinit.c"
124 #include "cpu/amd/model_lx/msrinit.c"
125
126 void main(unsigned long bist)
127 {
128         static const struct mem_controller memctrl [] = {
129                 {.channel0 = {(0xa<<3)|0, (0xa<<3)|1}}
130         };
131
132         SystemPreInit();
133
134         cs5536_early_setup();
135         
136         /* disable the power button */
137         outl(0x00, PMS_IO_BASE + 0x40);
138
139         /* cs5536_disable_internal_uart  disable them. Set them up now... */
140         cs5536_setup_onchipuart(1);
141
142         uart_init();
143         console_init();
144
145         /* Halt if there was a built in self test failure */
146         report_bist_failure(bist);
147
148         pll_reset();
149
150         cpuRegInit();
151         print_err("done cpuRegInit\n");
152
153         sdram_initialize(1, memctrl);
154         print_err("ram setup done\n");
155
156         msr_init();
157
158         /* Check all of memory */
159         /*ram_check(0x00000000, 640*1024);*/
160         print_err("ram check done\n");
161 }
162