b52f1f2e8fa0e5fef62b695e97c8354293650219
[coreboot.git] / src / mainboard / gigabyte / ga_2761gxdk / cache_as_ram_auto.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2007 AMD
5  * Written by Yinghai Lu <yinghailu@amd.com> for AMD.
6  * Copyright (C) 2007 Silicon Integrated Systems Corp. (SiS)
7  * Written by Morgan Tsai <my_tsai@sis.com> for SiS.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #define ASSEMBLY 1
25 #define __ROMCC__
26
27 #define RAMINIT_SYSINFO 1
28
29 #define K8_ALLOCATE_IO_RANGE 1
30 //#define K8_SCAN_PCI_BUS 1
31
32
33 #define QRANK_DIMM_SUPPORT 1
34
35 #if CONFIG_LOGICAL_CPUS==1
36 #define SET_NB_CFG_54 1
37 #endif
38
39 //used by init_cpus and fidvid
40 #define K8_SET_FIDVID 1
41 //if we want to wait for core1 done before DQS training, set it to 0
42 #define K8_SET_FIDVID_CORE0_ONLY 1
43
44 #if K8_REV_F_SUPPORT == 1
45 #define K8_REV_F_SUPPORT_F0_F1_WORKAROUND 0
46 #endif
47
48 #define DBGP_DEFAULT 7
49
50 #include <stdint.h>
51 #include <device/pci_def.h>
52 #include <device/pci_ids.h>
53 #include <arch/io.h>
54 #include <device/pnp_def.h>
55 #include <arch/romcc_io.h>
56 #include <cpu/x86/lapic.h>
57 #include "option_table.h"
58 #include "pc80/mc146818rtc_early.c"
59
60 #if USE_FAILOVER_IMAGE==0
61 #include "pc80/serial.c"
62 #include "arch/i386/lib/console.c"
63 #if CONFIG_USBDEBUG_DIRECT
64 #include "southbridge/sis/sis966/sis966_enable_usbdebug_direct.c"
65 #include "pc80/usbdebug_direct_serial.c"
66 #endif
67 #include "ram/ramtest.c"
68
69 #include <cpu/amd/model_fxx_rev.h>
70
71 #include "southbridge/sis/sis966/sis966_early_smbus.c"
72 #include "southbridge/sis/sis966/sis966_enable_rom.c"
73 #include "northbridge/amd/amdk8/raminit.h"
74 #include "cpu/amd/model_fxx/apic_timer.c"
75 #include "lib/delay.c"
76
77 #endif
78
79 #include "cpu/x86/lapic/boot_cpu.c"
80 #include "northbridge/amd/amdk8/reset_test.c"
81 #include "superio/ite/it8716f/it8716f_early_serial.c"
82 #include "superio/ite/it8716f/it8716f_early_init.c"
83
84 #if USE_FAILOVER_IMAGE==0
85
86 #include "cpu/x86/bist.h"
87
88 #if CONFIG_USE_INIT == 0
89         #include "lib/memcpy.c"
90 #endif
91
92 #include "northbridge/amd/amdk8/debug.c"
93
94 #include "cpu/amd/mtrr/amd_earlymtrr.c"
95
96 #include "northbridge/amd/amdk8/setup_resource_map.c"
97
98 #define SERIAL_DEV PNP_DEV(0x2e, IT8716F_SP1)
99
100 #include "southbridge/sis/sis966/sis966_early_ctrl.c"
101
102 static void memreset_setup(void)
103 {
104 }
105
106 static void memreset(int controllers, const struct mem_controller *ctrl)
107 {
108 }
109
110 static inline void activate_spd_rom(const struct mem_controller *ctrl)
111 {
112         /* nothing to do */
113 }
114
115 static inline int spd_read_byte(unsigned device, unsigned address)
116 {
117         return smbus_read_byte(device, address);
118 }
119
120 #include "northbridge/amd/amdk8/amdk8_f.h"
121 #include "northbridge/amd/amdk8/coherent_ht.c"
122
123 #include "northbridge/amd/amdk8/incoherent_ht.c"
124
125 #include "northbridge/amd/amdk8/raminit_f.c"
126
127 #include "sdram/generic_sdram.c"
128
129 #include "resourcemap.c"
130
131 #include "cpu/amd/dualcore/dualcore.c"
132
133 #define SIS966_NUM 1
134 #define SIS966_USE_NIC 1
135 #define SIS966_USE_AZA 1
136
137 #define SIS966_PCI_E_X_0 0
138
139 #define SIS966_MB_SETUP \
140         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+37, 0x00, 0x44,/* GPIO38 PCI_REQ3 */ \
141         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+38, 0x00, 0x44,/* GPIO39 PCI_GNT3 */ \
142         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+39, 0x00, 0x44,/* GPIO40 PCI_GNT2 */ \
143         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+40, 0x00, 0x44,/* GPIO41 PCI_REQ2 */ \
144         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+59, 0x00, 0x60,/* GPIP60 FANCTL0 */ \
145         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+60, 0x00, 0x60,/* GPIO61 FANCTL1 */
146
147 #include "southbridge/sis/sis966/sis966_early_setup_ss.h"
148 #include "southbridge/sis/sis966/sis966_early_setup_car.c"
149
150 #include "cpu/amd/car/copy_and_run.c"
151
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 #endif
159
160 #if ((HAVE_FAILOVER_BOOT==1) && (USE_FAILOVER_IMAGE == 1)) || ((HAVE_FAILOVER_BOOT==0) && (USE_FALLBACK_IMAGE == 1))
161
162 #include "southbridge/sis/sis966/sis966_enable_rom.c"
163 #include "northbridge/amd/amdk8/early_ht.c"
164
165
166 static void sio_setup(void)
167 {
168
169         unsigned value;
170         uint32_t dword;
171         uint8_t byte;
172
173         byte = pci_read_config8(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0x7b);
174         byte |= 0x20;
175         pci_write_config8(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0x7b, byte);
176
177         dword = pci_read_config32(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0xa0);
178         dword |= (1<<0);
179         pci_write_config32(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0xa0, dword);
180
181         dword = pci_read_config32(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0xa4);
182         dword |= (1<<16);
183         pci_write_config32(PCI_DEV(0, SIS966_DEVN_BASE+1 , 0), 0xa4, dword);
184 }
185
186 void failover_process(unsigned long bist, unsigned long cpu_init_detectedx)
187 {
188         unsigned last_boot_normal_x = last_boot_normal();
189
190         /* Is this a cpu only reset? or Is this a secondary cpu? */
191         if ((cpu_init_detectedx) || (!boot_cpu())) {
192                 if (last_boot_normal_x) {
193                         goto normal_image;
194                 } else {
195                         goto fallback_image;
196                 }
197         }
198
199         /* Nothing special needs to be done to find bus 0 */
200         /* Allow the HT devices to be found */
201
202         enumerate_ht_chain();
203
204         sio_setup();
205
206         /* Setup the sis966 */
207         sis966_enable_rom();
208
209         /* Is this a deliberate reset by the bios */
210         if (bios_reset_detected() && last_boot_normal_x) {
211                 goto normal_image;
212         }
213         /* This is the primary cpu how should I boot? */
214         else if (do_normal_boot()) {
215                 goto normal_image;
216         }
217         else {
218                 goto fallback_image;
219         }
220  normal_image:
221         __asm__ volatile ("jmp __normal_image"
222                 : /* outputs */
223                 : "a" (bist), "b" (cpu_init_detectedx) /* inputs */
224                 );
225
226  fallback_image:
227 #if HAVE_FAILOVER_BOOT==1
228         __asm__ volatile ("jmp __fallback_image"
229                 : /* outputs */
230                 : "a" (bist), "b" (cpu_init_detectedx) /* inputs */
231                 )
232 #endif
233         ;
234 }
235 #endif
236 void real_main(unsigned long bist, unsigned long cpu_init_detectedx);
237
238 void cache_as_ram_main(unsigned long bist, unsigned long cpu_init_detectedx)
239 {
240 #if HAVE_FAILOVER_BOOT==1
241     #if USE_FAILOVER_IMAGE==1
242         failover_process(bist, cpu_init_detectedx);
243     #else
244         real_main(bist, cpu_init_detectedx);
245     #endif
246 #else
247     #if USE_FALLBACK_IMAGE == 1
248         failover_process(bist, cpu_init_detectedx);
249     #endif
250         real_main(bist, cpu_init_detectedx);
251 #endif
252 }
253
254 #if USE_FAILOVER_IMAGE==0
255
256 void real_main(unsigned long bist, unsigned long cpu_init_detectedx)
257 {
258         static const uint16_t spd_addr [] = {
259                         (0xa<<3)|0, (0xa<<3)|2, 0, 0,
260                         (0xa<<3)|1, (0xa<<3)|3, 0, 0,
261 #if CONFIG_MAX_PHYSICAL_CPUS > 1
262                         (0xa<<3)|4, (0xa<<3)|6, 0, 0,
263                         (0xa<<3)|5, (0xa<<3)|7, 0, 0,
264 #endif
265         };
266
267         struct sys_info *sysinfo = (DCACHE_RAM_BASE + DCACHE_RAM_SIZE - DCACHE_RAM_GLOBAL_VAR_SIZE);
268
269         int needs_reset = 0;
270         unsigned bsp_apicid = 0;
271
272         if (bist == 0) {
273                 bsp_apicid = init_cpus(cpu_init_detectedx, sysinfo);
274         }
275
276         pnp_enter_ext_func_mode(SERIAL_DEV);
277         pnp_write_config(SERIAL_DEV, 0x23, 0);
278         it8716f_enable_dev(SERIAL_DEV, TTYS0_BASE);
279         pnp_exit_ext_func_mode(SERIAL_DEV);
280
281         setup_mb_resource_map();
282
283         uart_init();
284
285         /* Halt if there was a built in self test failure */
286         report_bist_failure(bist);
287
288
289 #if CONFIG_USBDEBUG_DIRECT
290         sis966_enable_usbdebug_direct(DBGP_DEFAULT);
291         early_usbdebug_direct_init();
292 #endif
293         console_init();
294         print_debug("*sysinfo range: ["); print_debug_hex32(sysinfo); print_debug(",");  print_debug_hex32((unsigned long)sysinfo+sizeof(struct sys_info)); print_debug(")\r\n");
295
296         print_debug("bsp_apicid="); print_debug_hex8(bsp_apicid); print_debug("\r\n");
297
298 #if MEM_TRAIN_SEQ == 1
299         set_sysinfo_in_ram(0); // in BSP so could hold all ap until sysinfo is in ram
300 #endif
301         setup_coherent_ht_domain(); // routing table and start other core0
302
303         wait_all_core0_started();
304 #if CONFIG_LOGICAL_CPUS==1
305         // It is said that we should start core1 after all core0 launched
306         /* becase optimize_link_coherent_ht is moved out from setup_coherent_ht_domain,
307          * So here need to make sure last core0 is started, esp for two way system,
308          * (there may be apic id conflicts in that case)
309          */
310         start_other_cores();
311         wait_all_other_cores_started(bsp_apicid);
312 #endif
313
314         /* it will set up chains and store link pair for optimization later */
315         ht_setup_chains_x(sysinfo); // it will init sblnk and sbbusn, nodes, sbdn
316
317 #if K8_SET_FIDVID == 1
318
319         {
320                 msr_t msr;
321                 msr=rdmsr(0xc0010042);
322                 print_debug("begin msr fid, vid "); print_debug_hex32( msr.hi ); print_debug_hex32(msr.lo); print_debug("\r\n");
323
324         }
325
326         enable_fid_change();
327
328         enable_fid_change_on_sb(sysinfo->sbbusn, sysinfo->sbdn);
329
330         init_fidvid_bsp(bsp_apicid);
331
332         // show final fid and vid
333         {
334                 msr_t msr;
335                 msr=rdmsr(0xc0010042);
336                 print_debug("end   msr fid, vid "); print_debug_hex32( msr.hi ); print_debug_hex32(msr.lo); print_debug("\r\n");
337
338         }
339 #endif
340
341         needs_reset |= optimize_link_coherent_ht();
342         needs_reset |= optimize_link_incoherent_ht(sysinfo);
343
344         // fidvid change will issue one LDTSTOP and the HT change will be effective too
345         if (needs_reset) {
346                 print_info("ht reset -\r\n");
347                 soft_reset();
348         }
349         allow_all_aps_stop(bsp_apicid);
350
351         //It's the time to set ctrl in sysinfo now;
352         fill_mem_ctrl(sysinfo->nodes, sysinfo->ctrl, spd_addr);
353
354         sis_init_stage1();
355         enable_smbus();
356
357         memreset_setup();
358
359         //do we need apci timer, tsc...., only debug need it for better output
360         /* all ap stopped? */
361 //        init_timer(); // Need to use TMICT to synconize FID/VID
362
363         sdram_initialize(sysinfo->nodes, sysinfo->ctrl, sysinfo);
364
365         sis_init_stage2();
366         post_cache_as_ram(); // bsp swtich stack to ram and copy sysinfo ram now
367
368 }
369
370
371 #endif