7237568b15caa4de5756aeab2d0463be4c7cade1
[coreboot.git] / src / mainboard / asus / m2v / romstage.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) 2008 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 unsigned int get_sbdn(unsigned bus);
26
27 #if CONFIG_K8_REV_F_SUPPORT == 1
28 #define K8_REV_F_SUPPORT_F0_F1_WORKAROUND 0
29 #endif
30
31 #include <stdint.h>
32 #include <string.h>
33 #include <device/pci_def.h>
34 #include <arch/io.h>
35 #include <device/pnp_def.h>
36 #include <arch/romcc_io.h>
37 #include <cpu/amd/mtrr.h>
38 #include <cpu/x86/lapic.h>
39 #include <pc80/mc146818rtc.h>
40 #include <console/console.h>
41 #include <cpu/amd/model_fxx_rev.h>
42 #include "northbridge/amd/amdk8/raminit.h"
43 #include "cpu/amd/model_fxx/apic_timer.c"
44 #include "lib/delay.c"
45 #include "northbridge/amd/amdk8/reset_test.c"
46 #include "northbridge/amd/amdk8/debug.c"
47 #include "superio/ite/it8712f/it8712f_early_serial.c"
48 #include "southbridge/via/vt8237r/vt8237r_early_smbus.c"
49 #include "cpu/x86/mtrr/earlymtrr.c"
50 #include "cpu/x86/bist.h"
51 #include "northbridge/amd/amdk8/setup_resource_map.c"
52
53 #define SERIAL_DEV PNP_DEV(0x2e, IT8712F_SP1)
54 #define WATCHDOG_DEV PNP_DEV(0x2e, IT8712F_GPIO)
55
56 #define IT8712F_GPIO_BASE               0x0a20
57
58 static void memreset(int controllers, const struct mem_controller *ctrl)
59 {
60 }
61
62 static inline int spd_read_byte(unsigned device, unsigned address)
63 {
64         return smbus_read_byte(device, address);
65 }
66
67 static void activate_spd_rom(const struct mem_controller *ctrl)
68 {
69 }
70
71 // defines S3_NVRAM_EARLY:
72 #include "southbridge/via/k8t890/k8t890_early_car.c"
73
74 #include "northbridge/amd/amdk8/amdk8.h"
75 #include "northbridge/amd/amdk8/incoherent_ht.c"
76 #include "northbridge/amd/amdk8/coherent_ht.c"
77 #include "northbridge/amd/amdk8/raminit_f.c"
78 #include "lib/generic_sdram.c"
79
80 #include "cpu/amd/dualcore/dualcore.c"
81
82 #include "cpu/amd/car/post_cache_as_ram.c"
83 #include "cpu/amd/model_fxx/init_cpus.c"
84 #include "cpu/amd/model_fxx/fidvid.c"
85 #include "northbridge/amd/amdk8/resourcemap.c"
86
87 void soft_reset(void)
88 {
89         uint8_t tmp;
90
91         set_bios_reset();
92         print_debug("soft reset\n");
93
94         /* PCI reset */
95         tmp = pci_read_config8(PCI_DEV(0, 0x11, 0), 0x4f);
96         tmp |= 0x01;
97         /* FIXME from S3 set bit1 to disable USB reset VT8237A/S */
98         pci_write_config8(PCI_DEV(0, 0x11, 0), 0x4f, tmp);
99
100         while (1) {
101                 /* daisy daisy ... */
102                 hlt();
103         }
104 }
105
106 unsigned int get_sbdn(unsigned bus)
107 {
108         device_t dev;
109
110         dev = pci_locate_device_on_bus(PCI_ID(PCI_VENDOR_ID_VIA,
111                                         PCI_DEVICE_ID_VIA_VT8237R_LPC), bus);
112         return (dev >> 15) & 0x1f;
113 }
114
115 struct gpio_init_val {
116         u8 addr;
117         u8 val;
118 };
119
120 static const struct gpio_init_val gpio_init_data[] = {
121         /* multi-function pin selection */
122         { 0x25, 0x00 },
123         { 0x28, 0x00 }, /* gp46 is infrared receive input */
124         { 0x29, 0x40 }, /* reserved value?!? */
125         { 0x2a, 0x00 },
126         { 0x2c, 0x1d }, /* pin91 is VIN7 instead of PCIRSTIN# */
127         /* gpio i/o port base */
128         { 0x62, IT8712F_GPIO_BASE >> 8 },
129         { 0x63, IT8712F_GPIO_BASE & 0xff },
130         /* 0xb8 - 0xbc: gpio pull-up enable */
131         { 0xb8, 0x00 },
132         /* 0xc0 - 0xc4: gpio alternate function select */
133         { 0xc0, 0x00 },
134         { 0xc3, 0x00 },
135         { 0xc4, 0xc0 },
136         /* 0xc8 - 0xcc: gpio output enable */
137         { 0xc8, 0x00 },
138         { 0xcb, 0x00 },
139         { 0xcc, 0xc0 },
140         /* end of list */
141         { 0, 0 },
142 };
143
144 static void m2v_it8712f_gpio_init(void)
145 {
146         const struct gpio_init_val *giv;
147
148         printk(BIOS_SPEW, "it8712f gpio init...\n");
149
150         /*
151          * it8712f gpio config
152          *
153          * Most importantly this switches pin 91 from
154          * PCIRSTIN# to VIN7.
155          * Note that only PCIRST3# and PCIRST5# are affected
156          * by PCIRSTIN#, the PCIRST1#, PCIRST2#, PCIRST4# are always
157          * direct buffers of #LRESET (low pin count bus reset).
158          * If this is not done All PCIRST are in reset state and the
159          * pcie slots don't initialize.
160          *
161          * pci reset handling:
162          * pin 91: VIN7 (alternate PCIRSTIN#)
163          * pin 48: PCIRST5# / gpio port 5 bit 0
164          * pin 84: PCIRST4# / gpio port 1 bit 0
165          * pin 31: PCIRST1# / gpio port 1 bit 4
166          * pin 33: PCIRST2# / gpio port 1 bit 2
167          * pin 34: PCIRST3# / gpio port 1 bit 1
168          *
169          * PCIRST[0-5]# are connected as follows:
170          * pcirst1# -> pci bus
171          * pcirst2# -> ide bus
172          * pcirst3# -> pcie devices
173          * pcirst4# -> pcie graphics
174          * pcirst5# -> maybe n/c (untested)
175          *
176          * For software control of PCIRST[1-5]#:
177          * 0x2a=0x17 (deselect pcirst# hardwiring, enable 0x25 control)
178          * 0x25=0x17 (select gpio function)
179          * 0xc0=0x17, 0xc8=0x17 gpio port 1 select & output enable
180          * 0xc4=0xc1, 0xcc=0xc1 gpio port 5 select & output enable
181          */
182         it8712f_enter_conf();
183         giv = gpio_init_data;
184         while (giv->addr) {
185                 printk(BIOS_SPEW, "it8712f gpio: %02x=%02x\n",
186                                 giv->addr, giv->val);
187                 it8712f_sio_write(IT8712F_GPIO, giv->addr, giv->val);
188                 giv++;
189         }
190         it8712f_exit_conf();
191
192         printk(BIOS_INFO, "it8712f gpio: Setting DDR2 voltage to 1.80V\n");
193         /*
194          * upper two bits of gpio_base+4 control ddr2 voltage:
195          * 11: 1.80V
196          * 01: 1.85V
197          * 10: 1.90V
198          * 00: 1.95V
199          *
200          * The lower six bits are inputs and normally read back as 1
201          * (except bit 2, which seems rather random).
202          */
203         outb(0xff, IT8712F_GPIO_BASE+4);
204 }
205
206 static void m2v_bus_init(void)
207 {
208         device_t dev;
209
210         printk(BIOS_SPEW, "m2v_bus_init\n");
211
212         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
213                                 PCI_DEVICE_ID_VIA_K8T890CF_0), 0);
214         pci_write_config8(dev, K8T890_MULTIPLE_FN_EN, 0x01);
215
216         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
217                                 PCI_DEVICE_ID_VIA_K8T890CF_5), 0);
218         /*
219          * bit | meaning
220          * 6   | 0: hide scratch register function 0:0.6 (we don't use it)
221          * 5   | 1: enable pcie bridge 0:2.0
222          * 4   | 0: hide   pcie bridge 0:3.3 (not connected)
223          * 3   | 1: enable pcie bridge 0:3.2
224          * 2   | 1: enable pcie bridge 0:3.1
225          * 1   | 1: enable pcie bridge 0:3.0
226          */
227         pci_write_config8(dev, 0xf0, 0x2e);
228 }
229
230 void cache_as_ram_main(unsigned long bist, unsigned long cpu_init_detectedx)
231 {
232         static const uint16_t spd_addr[] = {
233                 // Node 0
234                 (0xa << 3) | 0, (0xa << 3) | 2, 0, 0,
235                 (0xa << 3) | 1, (0xa << 3) | 3, 0, 0,
236                 // Node 1
237                 (0xa << 3) | 4, (0xa << 3) | 6, 0, 0,
238                 (0xa << 3) | 5, (0xa << 3) | 7, 0, 0,
239         };
240         unsigned bsp_apicid = 0;
241         int needs_reset = 0;
242         struct sys_info *sysinfo =
243             (struct sys_info *)(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
244
245         it8712f_24mhz_clkin();
246         it8712f_enable_serial(SERIAL_DEV, CONFIG_TTYS0_BASE);
247         it8712f_kill_watchdog();
248         uart_init();
249         console_init();
250         enable_rom_decode();
251         m2v_bus_init();
252         m2v_it8712f_gpio_init();
253
254         printk(BIOS_INFO, "now booting... \n");
255
256         if (bist == 0)
257                 bsp_apicid = init_cpus(cpu_init_detectedx, sysinfo);
258
259         /* Halt if there was a built in self test failure. */
260         report_bist_failure(bist);
261         setup_default_resource_map();
262         setup_coherent_ht_domain();
263         wait_all_core0_started();
264
265         printk(BIOS_INFO, "now booting... All core 0 started\n");
266
267 #if CONFIG_LOGICAL_CPUS==1
268         /* It is said that we should start core1 after all core0 launched. */
269         start_other_cores();
270         wait_all_other_cores_started(bsp_apicid);
271 #endif
272         init_timer();
273         ht_setup_chains_x(sysinfo); /* Init sblnk and sbbusn, nodes, sbdn. */
274
275         needs_reset = optimize_link_coherent_ht();
276         print_debug_hex8(needs_reset);
277         needs_reset |= optimize_link_incoherent_ht(sysinfo);
278         print_debug_hex8(needs_reset);
279         needs_reset |= k8t890_early_setup_ht();
280         print_debug_hex8(needs_reset);
281
282         if (needs_reset) {
283                 printk(BIOS_DEBUG, "ht reset -\n");
284                 soft_reset();
285                 printk(BIOS_DEBUG, "FAILED!\n");
286         }
287
288         /* the HT settings needs to be OK, because link freq chnage may cause HT disconnect */
289         /* allow LDT STOP asserts */
290         vt8237_sb_enable_fid_vid();
291
292         enable_fid_change();
293         print_debug("after enable_fid_change\n");
294
295         init_fidvid_bsp(bsp_apicid);
296
297         /* Stop the APs so we can start them later in init. */
298         allow_all_aps_stop(bsp_apicid);
299
300         /* It's the time to set ctrl now. */
301         fill_mem_ctrl(sysinfo->nodes, sysinfo->ctrl, spd_addr);
302         enable_smbus();
303         sdram_initialize(sysinfo->nodes, sysinfo->ctrl, sysinfo);
304         post_cache_as_ram();
305 }
306