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