blob: 75e3b0d8e683e6d2738a26659e7515b3af320d8f [file] [log] [blame]
Stefan Reinauer6651da32012-04-27 23:16:30 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2010 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <string.h>
23#include <lib.h>
24#include <timestamp.h>
25#include <arch/io.h>
26#include <arch/romcc_io.h>
27#include <device/pci_def.h>
28#include <device/pnp_def.h>
29#include <cpu/x86/lapic.h>
30#include <pc80/mc146818rtc.h>
31#include <cbmem.h>
32#include <console/console.h>
33#include "superio/smsc/sio1007/early_serial.c"
34#include "northbridge/intel/sandybridge/sandybridge.h"
35#include "northbridge/intel/sandybridge/raminit.h"
36#include "southbridge/intel/bd82x6x/pch.h"
37#include "southbridge/intel/bd82x6x/gpio.h"
38#include <arch/cpu.h>
39#include <cpu/x86/bist.h>
40#include <cpu/x86/msr.h>
41#include "gpio.h"
42#if CONFIG_CHROMEOS
43#include <vendorcode/google/chromeos/chromeos.h>
44#endif
45
46static void pch_enable_lpc(void)
47{
48 device_t dev = PCH_LPC_DEV;
49 int i;
50
51 /* Set COM1/COM2 decode range */
52 pci_write_config16(dev, LPC_IO_DEC, 0x0010);
53
54 /* Enable SuperIO + COM1 + PS/2 Keyboard/Mouse */
55 u16 lpc_config = CNF1_LPC_EN | CNF2_LPC_EN | COMA_LPC_EN | KBC_LPC_EN;
56 pci_write_config16(dev, LPC_EN, lpc_config);
57
58 /* Map 256 bytes at 0x1600 to the LPC bus. */
59 pci_write_config32(dev, LPC_GEN1_DEC, 0xfc1601);
60
61 /* Map a range for the runtime registers to the LPC bus. */
62 pci_write_config32(dev, LPC_GEN2_DEC, 0xc0181);
63
64 for (i = 0; i < ARRAY_SIZE(sio1007_lpc_ports); i++) {
65 if (sio1007_enable_uart_at(sio1007_lpc_ports[i])) {
66 /* Keep COMA UART enable bit on. */
67 pci_write_config16(dev, LPC_EN,
68 lpc_config | COMA_LPC_EN);
69 break;
70 }
71 }
72}
73
74static void rcba_config(void)
75{
76 u32 reg32;
77
78 /*
79 * GFX INTA -> PIRQA (MSI)
80 * D28IP_P1IP WLAN INTA -> PIRQB
81 * D28IP_P4IP ETH0 INTB -> PIRQC
82 * D29IP_E1P EHCI1 INTA -> PIRQD
83 * D26IP_E2P EHCI2 INTA -> PIRQE
84 * D31IP_SIP SATA INTA -> PIRQF (MSI)
85 * D31IP_SMIP SMBUS INTB -> PIRQG
86 * D31IP_TTIP THRT INTC -> PIRQH
87 * D27IP_ZIP HDA INTA -> PIRQG (MSI)
88 */
89
90 /* Device interrupt pin register (board specific) */
91 RCBA32(D31IP) = (INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
92 (INTB << D31IP_SMIP) | (INTA << D31IP_SIP);
93 RCBA32(D30IP) = (NOINT << D30IP_PIP);
94 RCBA32(D29IP) = (INTA << D29IP_E1P);
95 RCBA32(D28IP) = (INTA << D28IP_P1IP) | (INTC << D28IP_P3IP) |
96 (INTB << D28IP_P4IP);
97 RCBA32(D27IP) = (INTA << D27IP_ZIP);
98 RCBA32(D26IP) = (INTA << D26IP_E2P);
99 RCBA32(D25IP) = (NOINT << D25IP_LIP);
100 RCBA32(D22IP) = (NOINT << D22IP_MEI1IP);
101
102 /* Device interrupt route registers */
103 DIR_ROUTE(D31IR, PIRQF, PIRQG, PIRQH, PIRQA);
104 DIR_ROUTE(D29IR, PIRQD, PIRQE, PIRQF, PIRQG);
105 DIR_ROUTE(D28IR, PIRQB, PIRQC, PIRQD, PIRQE);
106 DIR_ROUTE(D27IR, PIRQG, PIRQH, PIRQA, PIRQB);
107 DIR_ROUTE(D26IR, PIRQE, PIRQF, PIRQG, PIRQH);
108 DIR_ROUTE(D25IR, PIRQA, PIRQB, PIRQC, PIRQD);
109 DIR_ROUTE(D22IR, PIRQA, PIRQB, PIRQC, PIRQD);
110
111 /* Enable IOAPIC (generic) */
112 RCBA16(OIC) = 0x0100;
113 /* PCH BWG says to read back the IOAPIC enable register */
114 (void) RCBA16(OIC);
115
Stefan Reinauer6651da32012-04-27 23:16:30 +0200116 /* Disable unused devices (board specific) */
117 reg32 = RCBA32(FD);
118 reg32 |= PCH_DISABLE_ALWAYS;
119 RCBA32(FD) = reg32;
120}
121
122// FIXME, this function is generic code that should go to sb/... or
123// nb/../early_init.c
124static void early_pch_init(void)
125{
126 u8 reg8;
127
128 // reset rtc power status
129 reg8 = pci_read_config8(PCH_LPC_DEV, 0xa4);
130 reg8 &= ~(1 << 2);
131 pci_write_config8(PCH_LPC_DEV, 0xa4, reg8);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200132}
133
134static void setup_sio_gpios(void)
135{
136 const u16 port = 0x164e;
137 const u16 runtime_port = 0x180;
138
139 /* Turn on configuration mode. */
140 outb(0x55, port);
141
142 /* Set the GPIO direction, polarity, and type. */
143 sio1007_setreg(port, 0x31, 1 << 0, 1 << 0);
144 sio1007_setreg(port, 0x32, 0 << 0, 1 << 0);
145 sio1007_setreg(port, 0x33, 0 << 0, 1 << 0);
146
147 /* Set the base address for the runtime register block. */
148 sio1007_setreg(port, 0x30, runtime_port >> 4, 0xff);
149 sio1007_setreg(port, 0x21, runtime_port >> 12, 0xff);
150
151 /* Turn on address decoding for it. */
152 sio1007_setreg(port, 0x3a, 1 << 1, 1 << 1);
153
154 /* Set the value of GPIO 10 by changing GP1, bit 0. */
155 u8 byte;
156 byte = inb(runtime_port + 0xc);
157 byte |= (1 << 0);
158 outb(byte, runtime_port + 0xc);
159
160 /* Turn off address decoding for it. */
161 sio1007_setreg(port, 0x3a, 0 << 1, 1 << 1);
162
163 /* Turn off configuration mode. */
164 outb(0xaa, port);
165}
166
167void main(unsigned long bist)
168{
169 int boot_mode = 0;
170 int cbmem_was_initted;
171 u32 pm1_cnt;
172 u16 pm1_sts;
173
174#if CONFIG_COLLECT_TIMESTAMPS
175 tsc_t start_romstage_time;
176 tsc_t before_dram_time;
177 tsc_t after_dram_time;
178 tsc_t base_time = {
179 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
180 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
181 };
182#endif
183 struct pei_data pei_data = {
184 pei_version: PEI_VERSION,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700185 mchbar: DEFAULT_MCHBAR,
186 dmibar: DEFAULT_DMIBAR,
187 epbar: DEFAULT_EPBAR,
188 pciexbar: CONFIG_MMCONF_BASE_ADDRESS,
189 smbusbar: SMBUS_IO_BASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200190 wdbbar: 0x4000000,
191 wdbsize: 0x1000,
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200192 hpet_address: CONFIG_HPET_ADDRESS,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700193 rcba: DEFAULT_RCBABASE,
194 pmbase: DEFAULT_PMBASE,
195 gpiobase: DEFAULT_GPIOBASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200196 thermalbase: 0xfed08000,
197 system_type: 0, // 0 Mobile, 1 Desktop/Server
198 tseg_size: CONFIG_SMM_TSEG_SIZE,
199 spd_addresses: { 0xa0, 0x00, 0xa4, 0x00 },
200 ts_addresses: { 0x00, 0x00, 0x00, 0x00 },
201 ec_present: 0,
202 // 0 = leave channel enabled
203 // 1 = disable dimm 0 on channel
204 // 2 = disable dimm 1 on channel
205 // 3 = disable dimm 0+1 on channel
206 dimm_channel0_disabled: 2,
207 dimm_channel1_disabled: 2,
208 max_ddr3_freq: 1600,
209 usb_port_config: {
210 { 1, 0, 0x0040 }, /* P0: Front port (OC0) */
211 { 1, 1, 0x0040 }, /* P1: Back port (OC1) */
212 { 1, 0, 0x0040 }, /* P2: MINIPCIE1 (no OC) */
213 { 1, 0, 0x0040 }, /* P3: MMC (no OC) */
214 { 1, 2, 0x0040 }, /* P4: Front port (OC2) */
215 { 0, 0, 0x0000 }, /* P5: Empty */
216 { 0, 0, 0x0000 }, /* P6: Empty */
217 { 0, 0, 0x0000 }, /* P7: Empty */
218 { 1, 4, 0x0040 }, /* P8: Back port (OC4) */
219 { 1, 4, 0x0040 }, /* P9: MINIPCIE3 (no OC) */
220 { 1, 4, 0x0040 }, /* P10: BLUETOOTH (no OC) */
221 { 0, 4, 0x0000 }, /* P11: Empty */
222 { 1, 6, 0x0040 }, /* P12: Back port (OC6) */
223 { 1, 5, 0x0040 }, /* P13: Back port (OC5) */
224 },
225 };
226
227#if CONFIG_COLLECT_TIMESTAMPS
228 start_romstage_time = rdtsc();
229#endif
230
231 if (bist == 0)
232 enable_lapic();
233
234 pch_enable_lpc();
235
236 /* Enable GPIOs */
237 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
238 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
Gabe Black599e2042012-03-30 14:33:02 -0700239 setup_pch_gpios(&emeraldlake2_gpio_map);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200240 setup_sio_gpios();
241
242 /* Early SuperIO setup */
243 console_init();
244
245 /* Halt if there was a built in self test failure */
246 report_bist_failure(bist);
247
248 if (MCHBAR16(SSKPD) == 0xCAFE) {
249 printk(BIOS_DEBUG, "soft reset detected\n");
250 boot_mode = 1;
251
252 /* System is not happy after keyboard reset... */
253 printk(BIOS_DEBUG, "Issuing CF9 warm reset\n");
254 outb(0x6, 0xcf9);
255 hlt();
256 }
257
258 /* Perform some early chipset initialization required
259 * before RAM initialization can work
260 */
261 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
262 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
263
264 /* Check PM1_STS[15] to see if we are waking from Sx */
265 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
266
267 /* Read PM1_CNT[12:10] to determine which Sx state */
268 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
269
270 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
271#if CONFIG_HAVE_ACPI_RESUME
272 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
273 boot_mode = 2;
274 /* Clear SLP_TYPE. This will break stage2 but
275 * we care for that when we get there.
276 */
277 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
278#else
279 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
280#endif
281 }
282
283 post_code(0x38);
284 /* Enable SPD ROMs and DDR-III DRAM */
285 enable_smbus();
286
287 /* Prepare USB controller early in S3 resume */
288 if (boot_mode == 2)
289 enable_usb_bar();
290
291 post_code(0x3a);
292 pei_data.boot_mode = boot_mode;
293#if CONFIG_COLLECT_TIMESTAMPS
294 before_dram_time = rdtsc();
295#endif
296 sdram_initialize(&pei_data);
297
298#if CONFIG_COLLECT_TIMESTAMPS
299 after_dram_time = rdtsc();
300#endif
301 post_code(0x3b);
302 /* Perform some initialization that must run before stage2 */
303 early_pch_init();
304 post_code(0x3c);
305
306 /* This should probably go away. Until now it is required
307 * and mainboard specific
308 */
309 rcba_config();
310 post_code(0x3d);
311
Stefan Reinauer6651da32012-04-27 23:16:30 +0200312 quick_ram_check();
Stefan Reinauerafcaac22012-06-18 15:43:50 -0700313 post_code(0x3e);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200314
315 MCHBAR16(SSKPD) = 0xCAFE;
316#if CONFIG_EARLY_CBMEM_INIT
317 cbmem_was_initted = !cbmem_initialize();
318#else
319 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
320 - HIGH_MEMORY_SIZE));
321#endif
322
323#if CONFIG_HAVE_ACPI_RESUME
324 /* If there is no high memory area, we didn't boot before, so
325 * this is not a resume. In that case we just create the cbmem toc.
326 */
327
328 *(u32 *)CBMEM_BOOT_MODE = 0;
329 *(u32 *)CBMEM_RESUME_BACKUP = 0;
330
331 if ((boot_mode == 2) && cbmem_was_initted) {
332 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
333 if (resume_backup_memory) {
334 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
335 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
336 }
337 /* Magic for S3 resume */
338 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
339 } else if (boot_mode == 2) {
340 /* Failed S3 resume, reset to come up cleanly */
341 outb(0x6, 0xcf9);
342 hlt();
343 } else {
344 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
345 }
346#endif
347 post_code(0x3f);
348#if CONFIG_CHROMEOS
349 init_chromeos(boot_mode);
350#endif
351#if CONFIG_COLLECT_TIMESTAMPS
352 timestamp_init(base_time);
353 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
354 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
355 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
356 timestamp_add_now(TS_END_ROMSTAGE);
357#endif
358#if CONFIG_CONSOLE_CBMEM
359 /* Keep this the last thing this function does. */
360 cbmemc_reinit();
361#endif
362}