b76d0119104715d99a96780c9a77223dd16b6990
[coreboot.git] / src / mainboard / asus / a8v-e_se / cache_as_ram_auto.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2006 AMD
5  * (Written by Yinghai Lu <yinghailu@amd.com> for AMD)
6  * Copyright (C) 2006 MSI
7  * (Written by Bingxun Shi <bingxunshi@gmail.com> for MSI)
8  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz> 
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23  */
24
25 #define ASSEMBLY 1
26 #define __ROMCC__
27
28 #define RAMINIT_SYSINFO 1
29
30 #define CACHE_AS_RAM_ADDRESS_DEBUG 0
31
32 unsigned int get_sbdn(unsigned bus);
33
34 /* Used by raminit. */
35 #define QRANK_DIMM_SUPPORT 1
36
37 /* Used by init_cpus and fidvid */
38 #define K8_SET_FIDVID 1
39
40 /* If we want to wait for core1 done before DQS training, set it to 0. */
41 #define K8_SET_FIDVID_CORE0_ONLY 1
42
43 /* #define DEBUG_SMBUS 1 */
44
45 #include <stdint.h>
46 #include <string.h>
47 #include <device/pci_def.h>
48 #include <arch/io.h>
49 #include <device/pnp_def.h>
50 #include <arch/romcc_io.h>
51 #include <cpu/x86/lapic.h>
52 #include "option_table.h"
53 #include "pc80/mc146818rtc_early.c"
54 #include "pc80/serial.c"
55 #include "arch/i386/lib/console.c"
56 #include <cpu/amd/model_fxx_rev.h>
57 #include "northbridge/amd/amdk8/raminit.h"
58 #include "cpu/amd/model_fxx/apic_timer.c"
59 #include "lib/delay.c"
60 #include "cpu/x86/lapic/boot_cpu.c"
61 #include "northbridge/amd/amdk8/reset_test.c"
62 #include "northbridge/amd/amdk8/debug.c"
63 #include "northbridge/amd/amdk8/early_ht.c"
64 #include "superio/winbond/w83627ehg/w83627ehg_early_serial.c"
65 #include "southbridge/via/vt8237r/vt8237r_early_smbus.c"
66 #include "cpu/amd/mtrr/amd_earlymtrr.c"
67 #include "cpu/x86/bist.h"
68 #include "northbridge/amd/amdk8/setup_resource_map.c"
69
70 #define SERIAL_DEV PNP_DEV(0x2e, W83627EHG_SP1)
71 #define GPIO_DEV PNP_DEV(0x2e, W83627EHG_GPIO_SUSLED)
72 #define ACPI_DEV PNP_DEV(0x2e, W83627EHG_ACPI)
73 #define RTC_DEV PNP_DEV(0x2e, W83627EHG_RTC)
74
75 static void memreset_setup(void)
76 {
77 }
78
79 static void memreset(int controllers, const struct mem_controller *ctrl)
80 {
81 }
82
83 static inline int spd_read_byte(unsigned device, unsigned address)
84 {
85         return smbus_read_byte(device, address);
86 }
87
88 void activate_spd_rom(const struct mem_controller *ctrl)
89 {
90 }
91
92 void soft_reset(void)
93 {
94         uint8_t tmp;
95
96         set_bios_reset();
97         print_debug("soft reset \r\n");
98
99         /* PCI reset */
100         tmp = pci_read_config8(PCI_DEV(0, 0x11, 0), 0x4f);
101         tmp |= 0x01;
102         pci_write_config8(PCI_DEV(0, 0x11, 0), 0x4f, tmp);
103
104         while (1) {
105                 /* daisy daisy ... */
106                 hlt();
107         }
108 }
109
110 #define K8_4RANK_DIMM_SUPPORT 1
111
112 #include "northbridge/amd/amdk8/amdk8.h"
113 #include "northbridge/amd/amdk8/raminit.c"
114 #include "northbridge/amd/amdk8/coherent_ht.c"
115 #include "northbridge/amd/amdk8/incoherent_ht.c"
116 #include "sdram/generic_sdram.c"
117 #include "cpu/amd/dualcore/dualcore.c"
118 #include "southbridge/via/k8t890/k8t890_early_car.c"
119 #include "cpu/amd/car/copy_and_run.c"
120 #include "cpu/amd/car/post_cache_as_ram.c"
121 #include "cpu/amd/model_fxx/init_cpus.c"
122 #include "cpu/amd/model_fxx/fidvid.c"
123 #include "northbridge/amd/amdk8/resourcemap.c"
124
125 void hard_reset(void)
126 {
127         print_info("NO HARD RESET. FIX ME!\n");
128 }
129
130 unsigned int get_sbdn(unsigned bus)
131 {
132         device_t dev;
133
134         dev = pci_locate_device_on_bus(PCI_ID(PCI_VENDOR_ID_VIA,
135                                         PCI_DEVICE_ID_VIA_VT8237R_LPC), bus);
136         return (dev >> 15) & 0x1f;
137 }
138
139 void sio_init(void)
140 {
141         u8 reg;
142
143         pnp_enter_ext_func_mode(SERIAL_DEV);
144         /* We have 24MHz input. */
145         reg = pnp_read_config(SERIAL_DEV, 0x24);
146         pnp_write_config(SERIAL_DEV, 0x24, (reg & ~0x40));
147         /* We have GPIO for KB/MS pin. */
148         reg = pnp_read_config(SERIAL_DEV, 0x2a);
149         pnp_write_config(SERIAL_DEV, 0x2a, (reg | 1));
150         /* We have all RESTOUT and even some reserved bits, too. */
151         reg = pnp_read_config(SERIAL_DEV, 0x2c);
152         pnp_write_config(SERIAL_DEV, 0x2c, (reg | 0xf0));
153         pnp_exit_ext_func_mode(SERIAL_DEV);
154
155         pnp_enter_ext_func_mode(ACPI_DEV);
156         pnp_set_logical_device(ACPI_DEV);
157         /*
158          * Set the delay rising time from PWROK_LP to PWROK_ST to
159          * 300 - 600ms, and 0 to vice versa.
160          */
161         reg = pnp_read_config(ACPI_DEV, 0xe6);
162         pnp_write_config(ACPI_DEV, 0xe6, (reg & 0xf0));
163         /* 1 Use external suspend clock source 32.768KHz. Undocumented?? */
164         reg = pnp_read_config(ACPI_DEV, 0xe4);
165         pnp_write_config(ACPI_DEV, 0xe4, (reg | 0x10));
166         pnp_exit_ext_func_mode(ACPI_DEV);
167
168         pnp_enter_ext_func_mode(GPIO_DEV);
169         pnp_set_logical_device(GPIO_DEV);
170         /* Set memory voltage to 2.75V, vcore offset + 100mV, 1.5V chipset voltage. */
171         pnp_write_config(GPIO_DEV, 0x30, 0x09); /* Enable GPIO 2 & GPIO 5. */
172         pnp_write_config(GPIO_DEV, 0xe2, 0x00); /* No inversion */
173         pnp_write_config(GPIO_DEV, 0xe5, 0x00); /* No inversion */
174         pnp_write_config(GPIO_DEV, 0xe3, 0x03); /* 0000 0011, 0=output 1=input */
175         pnp_write_config(GPIO_DEV, 0xe0, 0xde); /* 1101 1110, 0=output 1=input */
176         pnp_write_config(GPIO_DEV, 0xe1, 0x01); /* Set output val. */
177         pnp_write_config(GPIO_DEV, 0xe4, 0xb4); /* Set output val (1011 0100). */
178         pnp_exit_ext_func_mode(GPIO_DEV);
179 }
180
181 #if CONFIG_USE_FALLBACK_IMAGE == 1
182
183 void failover_process(unsigned long bist, unsigned long cpu_init_detectedx)
184 {
185         /* unsigned last_boot_normal_x = last_boot_normal(); */
186         /* FIXME */
187         unsigned last_boot_normal_x = 1;
188
189         sio_init();
190         w83627ehg_enable_serial(SERIAL_DEV, CONFIG_TTYS0_BASE);
191         uart_init();
192         console_init();
193         enable_rom_decode();
194
195         print_info("now booting... fallback\r\n");
196
197         /* Is this a CPU only reset? Or is this a secondary CPU? */
198         if ((cpu_init_detectedx) || (!boot_cpu())) {
199                 if (last_boot_normal_x)
200                         goto normal_image;
201                 else
202                         goto fallback_image;
203         }
204
205         /* Nothing special needs to be done to find bus 0. */
206         /* Allow the HT devices to be found. */
207         enumerate_ht_chain();
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         } else {
217                 goto fallback_image;
218         }
219
220 normal_image:
221         /* print_info("JMP normal image\r\n"); */
222
223         __asm__ __volatile__("jmp __normal_image":
224                              :"a" (bist), "b" (cpu_init_detectedx));
225
226 fallback_image:
227         ;
228 }
229 #endif
230
231 void real_main(unsigned long bist, unsigned long cpu_init_detectedx);
232
233 void cache_as_ram_main(unsigned long bist, unsigned long cpu_init_detectedx)
234 {
235 #if CONFIG_USE_FALLBACK_IMAGE == 1
236         failover_process(bist, cpu_init_detectedx);
237 #endif
238         real_main(bist, cpu_init_detectedx);
239 }
240
241 void real_main(unsigned long bist, unsigned long cpu_init_detectedx)
242 {
243         static const uint16_t spd_addr[] = {
244                 (0xa << 3) | 0, (0xa << 3) | 2, 0, 0,
245                 (0xa << 3) | 1, (0xa << 3) | 3, 0, 0,
246 #if CONFIG_MAX_PHYSICAL_CPUS > 1
247                 (0xa << 3) | 4, (0xa << 3) | 6, 0, 0,
248                 (0xa << 3) | 5, (0xa << 3) | 7, 0, 0,
249 #endif
250         };
251         unsigned bsp_apicid = 0;
252         int needs_reset = 0;
253         struct sys_info *sysinfo =
254             (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
255         char *p;
256
257         sio_init();
258         w83627ehg_enable_serial(SERIAL_DEV, CONFIG_TTYS0_BASE);
259         uart_init();
260         console_init();
261         enable_rom_decode();
262
263         print_info("now booting... real_main\r\n");
264
265         if (bist == 0)
266                 bsp_apicid = init_cpus(cpu_init_detectedx, sysinfo);
267
268         /* Halt if there was a built in self test failure. */
269         report_bist_failure(bist);
270
271         setup_default_resource_map();
272         setup_coherent_ht_domain();
273         wait_all_core0_started();
274
275         print_info("now booting... Core0 started\r\n");
276
277 #if CONFIG_LOGICAL_CPUS==1
278         /* It is said that we should start core1 after all core0 launched. */
279         start_other_cores();
280         wait_all_other_cores_started(bsp_apicid);
281 #endif
282         init_timer();
283         ht_setup_chains_x(sysinfo); /* Init sblnk and sbbusn, nodes, sbdn. */
284
285         needs_reset = optimize_link_coherent_ht();
286         needs_reset |= optimize_link_incoherent_ht(sysinfo);
287         needs_reset |= k8t890_early_setup_ht();
288
289         if (needs_reset) {
290                 print_debug("ht reset -\r\n");
291                 soft_reset();
292         }
293
294         /* the HT settings needs to be OK, because link freq chnage may cause HT disconnect */
295         enable_fid_change();
296         init_fidvid_bsp(bsp_apicid);
297
298         /* Stop the APs so we can start them later in init. */
299         allow_all_aps_stop(bsp_apicid);
300
301         /* It's the time to set ctrl now. */
302         fill_mem_ctrl(sysinfo->nodes, sysinfo->ctrl, spd_addr);
303
304         enable_smbus();
305         memreset_setup();
306         sdram_initialize(sysinfo->nodes, sysinfo->ctrl, sysinfo);
307         post_cache_as_ram();
308 }