This patch is from 2009-10-20
[coreboot.git] / src / mainboard / msi / ms9185 / romstage.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2006 Tyan
5  * Copyright (C) 2006 AMD
6  * Written by Yinghai Lu <yinghailu@gmail.com> for Tyan and AMD.
7  *
8  * Copyright (C) 2006 MSI
9  * Written by bxshi <bingxunshi@gmail.com> for MSI.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 #define ASSEMBLY 1
27 #define __PRE_RAM__
28
29 #define RAMINIT_SYSINFO 1
30 #define CACHE_AS_RAM_ADDRESS_DEBUG 0
31
32 #define SET_NB_CFG_54 1
33
34 //used by raminit
35 #define QRANK_DIMM_SUPPORT 1
36
37 //used by incoherent_ht
38 //#define K8_ALLOCATE_IO_RANGE 1
39
40 //used by init_cpus and fidvid
41 #define K8_SET_FIDVID 1
42 //if we want to wait for core1 done before DQS training, set it to 0
43 #define K8_SET_FIDVID_CORE0_ONLY 1
44
45 #include <stdint.h>
46 #include <string.h>
47 #include <device/pci_def.h>
48 #include <device/pci_ids.h>
49 #include <arch/io.h>
50 #include <device/pnp_def.h>
51 #include <arch/romcc_io.h>
52 #include <cpu/x86/lapic.h>
53 #include "option_table.h"
54 #include "pc80/mc146818rtc_early.c"
55 #include "pc80/serial.c"
56 #include "arch/i386/lib/console.c"
57
58 #if 0
59 static void post_code(uint8_t value) {
60 #if 1
61         int i;
62         for(i=0;i<0x80000;i++) {
63                 outb(value, 0x80);
64         }
65 #endif
66 }
67 #endif
68
69 #include <cpu/amd/model_fxx_rev.h>
70 #include "southbridge/broadcom/bcm5785/bcm5785_early_smbus.c"
71 #include "northbridge/amd/amdk8/raminit.h"
72 #include "cpu/amd/model_fxx/apic_timer.c"
73 #include "lib/delay.c"
74
75
76 #include "cpu/x86/lapic/boot_cpu.c"
77 #include "northbridge/amd/amdk8/reset_test.c"
78 #include "northbridge/amd/amdk8/debug.c"
79 #include "superio/nsc/pc87417/pc87417_early_serial.c"
80 #include "cpu/amd/mtrr/amd_earlymtrr.c"
81 #include "cpu/x86/bist.h"
82
83 #include "northbridge/amd/amdk8/setup_resource_map.c"
84
85 #define SERIAL_DEV PNP_DEV(0x2e, PC87417_SP1)
86 #define RTC_DEV PNP_DEV(0x2e, PC87417_RTC)
87 #include "southbridge/broadcom/bcm5785/bcm5785_early_setup.c"
88 static void memreset_setup(void)
89 {
90 }
91
92 static void memreset(int controllers, const struct mem_controller *ctrl)
93 {
94 }
95
96 static inline void activate_spd_rom(const struct mem_controller *ctrl)
97 {
98 #define SMBUS_SWITCH1 0x70
99 #define SMBUS_SWITCH2 0x72
100         unsigned device = (ctrl->channel0[0]) >> 8;
101         smbus_send_byte(SMBUS_SWITCH1, device & 0x0f);
102         smbus_send_byte(SMBUS_SWITCH2, (device >> 4) & 0x0f );
103 }
104
105 #if 0
106 static inline void change_i2c_mux(unsigned device)
107 {
108 #define SMBUS_SWITCH1 0x70
109 #define SMBUS_SWITCH2 0x72
110         smbus_send_byte(SMBUS_SWITCH1, device & 0x0f);
111         smbus_send_byte(SMBUS_SWITCH2, (device >> 4) & 0x0f );
112 }
113 #endif
114
115
116
117 static inline int spd_read_byte(unsigned device, unsigned address)
118 {
119         return smbus_read_byte(device, address);
120 }
121
122 #include "northbridge/amd/amdk8/amdk8_f.h"
123 #include "northbridge/amd/amdk8/coherent_ht.c"
124
125 #include "northbridge/amd/amdk8/incoherent_ht.c"
126
127 #include "northbridge/amd/amdk8/raminit_f.c"
128
129 #include "lib/generic_sdram.c"
130
131  /* msi does not want the default */
132 #include "resourcemap.c"
133
134 #include "cpu/amd/dualcore/dualcore.c"
135
136 #define RC0 (0x10<<8)
137 #define RC1 (0x01<<8)
138
139 #define DIMM0 0x50
140 #define DIMM1 0x51
141 #define DIMM2 0x52
142 #define DIMM3 0x53
143 #define DIMM4 0x54
144 #define DIMM5 0x55
145 #define DIMM6 0x56
146 #define DIMM7 0x57
147
148
149 #include "cpu/amd/car/copy_and_run.c"
150 #include "cpu/amd/car/post_cache_as_ram.c"
151
152 #include "cpu/amd/model_fxx/init_cpus.c"
153
154 #include "cpu/amd/model_fxx/fidvid.c"
155
156 #if CONFIG_USE_FALLBACK_IMAGE == 1
157
158 #include "northbridge/amd/amdk8/early_ht.c"
159
160 void failover_process(unsigned long bist, unsigned long cpu_init_detectedx)
161 {
162
163         /* Is this a cpu only reset? Is this a secondary cpu? */
164         if ((cpu_init_detectedx) || (!boot_cpu())) {
165                 if (last_boot_normal()) { // RTC already inited
166                         goto normal_image;
167                 } else {
168                         goto fallback_image;
169                 }
170         }
171         /* Nothing special needs to be done to find bus 0 */
172         /* Allow the HT devices to be found */
173
174         enumerate_ht_chain();
175
176         bcm5785_enable_rom();
177
178         bcm5785_enable_lpc();
179
180         //enable RTC
181         pc87417_enable_dev(RTC_DEV);
182
183         /* Is this a deliberate reset by the bios */
184 //        post_code(0x22);
185         if (bios_reset_detected() && last_boot_normal()) {
186                 goto normal_image;
187         }
188         /* This is the primary cpu how should I boot? */
189         else if (do_normal_boot()) {
190                 goto normal_image;
191         }
192         else {
193                 goto fallback_image;
194         }
195  normal_image:
196 //        post_code(0x23);
197         __asm__ volatile ("jmp __normal_image"
198                 : /* outputs */
199                 : "a" (bist), "b" (cpu_init_detectedx) /* inputs */
200                 );
201
202  fallback_image:
203 //        post_code(0x25);
204         ;
205
206 }
207 #endif
208
209 void real_main(unsigned long bist, unsigned long cpu_init_detectedx);
210
211 void cache_as_ram_main(unsigned long bist, unsigned long cpu_init_detectedx)
212 {
213
214 #if CONFIG_USE_FALLBACK_IMAGE == 1
215        failover_process(bist, cpu_init_detectedx);
216 #endif
217        real_main(bist, cpu_init_detectedx);
218
219 }
220
221 void real_main(unsigned long bist, unsigned long cpu_init_detectedx)
222 {
223        static const uint16_t spd_addr[] = {
224                        //first node
225                         RC0|DIMM0, RC0|DIMM2, RC0|DIMM4, RC0|DIMM6,
226                         RC0|DIMM1, RC0|DIMM3, RC0|DIMM5, RC0|DIMM7,
227 #if CONFIG_MAX_PHYSICAL_CPUS > 1
228                        //second node
229                        RC1|DIMM0, RC1|DIMM2, RC1|DIMM4, RC1|DIMM6,
230                        RC1|DIMM1, RC1|DIMM3, RC1|DIMM5, RC1|DIMM7,
231 #endif
232
233        };
234
235        struct sys_info *sysinfo = (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
236
237         int needs_reset;
238         unsigned bsp_apicid = 0;
239
240         if (bist == 0) {
241                bsp_apicid = init_cpus(cpu_init_detectedx, sysinfo);
242         }
243
244 //     post_code(0x32);
245
246        pc87417_enable_serial(SERIAL_DEV, CONFIG_TTYS0_BASE);
247         uart_init();
248         console_init();
249
250 //     dump_mem(CONFIG_DCACHE_RAM_BASE+CONFIG_DCACHE_RAM_SIZE-0x200, CONFIG_DCACHE_RAM_BASE+CONFIG_DCACHE_RAM_SIZE);
251
252        /* Halt if there was a built in self test failure */
253        report_bist_failure(bist);
254
255        print_debug("*sysinfo range: ["); print_debug_hex32(sysinfo); print_debug(",");  print_debug_hex32((unsigned long)sysinfo+sizeof(struct sys_info)); print_debug(")\r\n");
256
257        setup_ms9185_resource_map();
258 #if 0
259         dump_pci_device(PCI_DEV(0, 0x18, 0));
260        dump_pci_device(PCI_DEV(0, 0x19, 0));
261 #endif
262
263        print_debug("bsp_apicid="); print_debug_hex8(bsp_apicid); print_debug("\r\n");
264
265        setup_coherent_ht_domain();
266
267        wait_all_core0_started();
268 #if CONFIG_LOGICAL_CPUS==1
269         // It is said that we should start core1 after all core0 launched
270        /* becase optimize_link_coherent_ht is moved out from setup_coherent_ht_domain,
271         * So here need to make sure last core0 is started, esp for two way system,
272         * (there may be apic id conflicts in that case)
273         */
274         start_other_cores();
275 //bx_a010-     wait_all_other_cores_started(bsp_apicid);
276 #endif
277
278        /* it will set up chains and store link pair for optimization later */
279         ht_setup_chains_x(sysinfo); // it will init sblnk and sbbusn, nodes, sbdn
280
281        bcm5785_early_setup();
282
283
284 #if 0
285        //it your CPU min fid is 1G, you can change HT to 1G and FID to max one time.
286         needs_reset = optimize_link_coherent_ht();
287         needs_reset |= optimize_link_incoherent_ht(sysinfo);
288 #endif
289
290 #if K8_SET_FIDVID == 1
291
292         {
293                 msr_t msr;
294                 msr=rdmsr(0xc0010042);
295                 print_debug("begin msr fid, vid "); print_debug_hex32( msr.hi ); print_debug_hex32(msr.lo); print_debug("\r\n");
296
297         }
298
299        enable_fid_change();
300
301        enable_fid_change_on_sb(sysinfo->sbbusn, sysinfo->sbdn);
302
303         init_fidvid_bsp(bsp_apicid);
304
305         // show final fid and vid
306         {
307                 msr_t msr;
308                 msr=rdmsr(0xc0010042);
309                 print_debug("end   msr fid, vid "); print_debug_hex32( msr.hi ); print_debug_hex32(msr.lo); print_debug("\r\n");
310
311         }
312 #endif
313
314 #if 1
315        needs_reset = optimize_link_coherent_ht();
316        needs_reset |= optimize_link_incoherent_ht(sysinfo);
317
318         // fidvid change will issue one LDTSTOP and the HT change will be effective too
319         if (needs_reset) {
320                 print_info("ht reset -\r\n");
321                 soft_reset();
322         }
323 #endif
324        allow_all_aps_stop(bsp_apicid);
325
326         //It's the time to set ctrl in sysinfo now;
327        fill_mem_ctrl(sysinfo->nodes, sysinfo->ctrl, spd_addr);
328
329        enable_smbus();
330
331 #if 0
332        int i;
333        for(i=0;i<2;i++) {
334                activate_spd_rom(sysinfo->ctrl+i);
335                dump_smbus_registers();
336        }
337 #endif
338
339 #if 0
340        int i;
341         for(i=1;i<256;i<<=1) {
342                 change_i2c_mux(i);
343                 dump_smbus_registers();
344         }
345 #endif
346
347        memreset_setup();
348
349        //do we need apci timer, tsc...., only debug need it for better output
350         /* all ap stopped? */
351 //        init_timer(); // Need to use TMICT to synconize FID/VID
352
353        sdram_initialize(sysinfo->nodes, sysinfo->ctrl, sysinfo);
354
355 #if 0
356         print_pci_devices();
357 #endif
358
359 #if 0
360 //        dump_pci_devices();
361         dump_pci_device_index_wait(PCI_DEV(0, 0x18, 2), 0x98);
362        dump_pci_device_index_wait(PCI_DEV(0, 0x19, 2), 0x98);
363 #endif
364
365        post_cache_as_ram();
366
367
368 }