blob: 0cf113b9f306db561692596054ff3b56b0375516 [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
116 /* Enable upper 128bytes of CMOS (generic) */
117 RCBA32(RC) = (1 << 2);
118
119 /* Disable unused devices (board specific) */
120 reg32 = RCBA32(FD);
121 reg32 |= PCH_DISABLE_ALWAYS;
122 RCBA32(FD) = reg32;
123}
124
125// FIXME, this function is generic code that should go to sb/... or
126// nb/../early_init.c
127static void early_pch_init(void)
128{
129 u8 reg8;
130
131 // reset rtc power status
132 reg8 = pci_read_config8(PCH_LPC_DEV, 0xa4);
133 reg8 &= ~(1 << 2);
134 pci_write_config8(PCH_LPC_DEV, 0xa4, reg8);
135
136 // SATA - enable AHCI
137 pci_write_config16(PCH_SATA_DEV, 0x90, 0x0060);
138}
139
140static void setup_sio_gpios(void)
141{
142 const u16 port = 0x164e;
143 const u16 runtime_port = 0x180;
144
145 /* Turn on configuration mode. */
146 outb(0x55, port);
147
148 /* Set the GPIO direction, polarity, and type. */
149 sio1007_setreg(port, 0x31, 1 << 0, 1 << 0);
150 sio1007_setreg(port, 0x32, 0 << 0, 1 << 0);
151 sio1007_setreg(port, 0x33, 0 << 0, 1 << 0);
152
153 /* Set the base address for the runtime register block. */
154 sio1007_setreg(port, 0x30, runtime_port >> 4, 0xff);
155 sio1007_setreg(port, 0x21, runtime_port >> 12, 0xff);
156
157 /* Turn on address decoding for it. */
158 sio1007_setreg(port, 0x3a, 1 << 1, 1 << 1);
159
160 /* Set the value of GPIO 10 by changing GP1, bit 0. */
161 u8 byte;
162 byte = inb(runtime_port + 0xc);
163 byte |= (1 << 0);
164 outb(byte, runtime_port + 0xc);
165
166 /* Turn off address decoding for it. */
167 sio1007_setreg(port, 0x3a, 0 << 1, 1 << 1);
168
169 /* Turn off configuration mode. */
170 outb(0xaa, port);
171}
172
173void main(unsigned long bist)
174{
175 int boot_mode = 0;
176 int cbmem_was_initted;
177 u32 pm1_cnt;
178 u16 pm1_sts;
179
180#if CONFIG_COLLECT_TIMESTAMPS
181 tsc_t start_romstage_time;
182 tsc_t before_dram_time;
183 tsc_t after_dram_time;
184 tsc_t base_time = {
185 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
186 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
187 };
188#endif
189 struct pei_data pei_data = {
190 pei_version: PEI_VERSION,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700191 mchbar: DEFAULT_MCHBAR,
192 dmibar: DEFAULT_DMIBAR,
193 epbar: DEFAULT_EPBAR,
194 pciexbar: CONFIG_MMCONF_BASE_ADDRESS,
195 smbusbar: SMBUS_IO_BASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200196 wdbbar: 0x4000000,
197 wdbsize: 0x1000,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700198 hpet_address: HPET_ADDR,
199 rcba: DEFAULT_RCBABASE,
200 pmbase: DEFAULT_PMBASE,
201 gpiobase: DEFAULT_GPIOBASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200202 thermalbase: 0xfed08000,
203 system_type: 0, // 0 Mobile, 1 Desktop/Server
204 tseg_size: CONFIG_SMM_TSEG_SIZE,
205 spd_addresses: { 0xa0, 0x00, 0xa4, 0x00 },
206 ts_addresses: { 0x00, 0x00, 0x00, 0x00 },
207 ec_present: 0,
208 // 0 = leave channel enabled
209 // 1 = disable dimm 0 on channel
210 // 2 = disable dimm 1 on channel
211 // 3 = disable dimm 0+1 on channel
212 dimm_channel0_disabled: 2,
213 dimm_channel1_disabled: 2,
214 max_ddr3_freq: 1600,
215 usb_port_config: {
216 { 1, 0, 0x0040 }, /* P0: Front port (OC0) */
217 { 1, 1, 0x0040 }, /* P1: Back port (OC1) */
218 { 1, 0, 0x0040 }, /* P2: MINIPCIE1 (no OC) */
219 { 1, 0, 0x0040 }, /* P3: MMC (no OC) */
220 { 1, 2, 0x0040 }, /* P4: Front port (OC2) */
221 { 0, 0, 0x0000 }, /* P5: Empty */
222 { 0, 0, 0x0000 }, /* P6: Empty */
223 { 0, 0, 0x0000 }, /* P7: Empty */
224 { 1, 4, 0x0040 }, /* P8: Back port (OC4) */
225 { 1, 4, 0x0040 }, /* P9: MINIPCIE3 (no OC) */
226 { 1, 4, 0x0040 }, /* P10: BLUETOOTH (no OC) */
227 { 0, 4, 0x0000 }, /* P11: Empty */
228 { 1, 6, 0x0040 }, /* P12: Back port (OC6) */
229 { 1, 5, 0x0040 }, /* P13: Back port (OC5) */
230 },
231 };
232
233#if CONFIG_COLLECT_TIMESTAMPS
234 start_romstage_time = rdtsc();
235#endif
236
237 if (bist == 0)
238 enable_lapic();
239
240 pch_enable_lpc();
241
242 /* Enable GPIOs */
243 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
244 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
245 setup_pch_gpios(&link_gpio_map);
246 setup_sio_gpios();
247
248 /* Early SuperIO setup */
249 console_init();
250
251 /* Halt if there was a built in self test failure */
252 report_bist_failure(bist);
253
254 if (MCHBAR16(SSKPD) == 0xCAFE) {
255 printk(BIOS_DEBUG, "soft reset detected\n");
256 boot_mode = 1;
257
258 /* System is not happy after keyboard reset... */
259 printk(BIOS_DEBUG, "Issuing CF9 warm reset\n");
260 outb(0x6, 0xcf9);
261 hlt();
262 }
263
264 /* Perform some early chipset initialization required
265 * before RAM initialization can work
266 */
267 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
268 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
269
270 /* Check PM1_STS[15] to see if we are waking from Sx */
271 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
272
273 /* Read PM1_CNT[12:10] to determine which Sx state */
274 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
275
276 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
277#if CONFIG_HAVE_ACPI_RESUME
278 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
279 boot_mode = 2;
280 /* Clear SLP_TYPE. This will break stage2 but
281 * we care for that when we get there.
282 */
283 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
284#else
285 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
286#endif
287 }
288
289 post_code(0x38);
290 /* Enable SPD ROMs and DDR-III DRAM */
291 enable_smbus();
292
293 /* Prepare USB controller early in S3 resume */
294 if (boot_mode == 2)
295 enable_usb_bar();
296
297 post_code(0x3a);
298 pei_data.boot_mode = boot_mode;
299#if CONFIG_COLLECT_TIMESTAMPS
300 before_dram_time = rdtsc();
301#endif
302 sdram_initialize(&pei_data);
303
304#if CONFIG_COLLECT_TIMESTAMPS
305 after_dram_time = rdtsc();
306#endif
307 post_code(0x3b);
308 /* Perform some initialization that must run before stage2 */
309 early_pch_init();
310 post_code(0x3c);
311
312 /* This should probably go away. Until now it is required
313 * and mainboard specific
314 */
315 rcba_config();
316 post_code(0x3d);
317
318 /* Initialize the internal PCIe links before we go into stage2 */
319 sandybridge_late_initialization();
320
321 post_code(0x3e);
322 quick_ram_check();
323
324 MCHBAR16(SSKPD) = 0xCAFE;
325#if CONFIG_EARLY_CBMEM_INIT
326 cbmem_was_initted = !cbmem_initialize();
327#else
328 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
329 - HIGH_MEMORY_SIZE));
330#endif
331
332#if CONFIG_HAVE_ACPI_RESUME
333 /* If there is no high memory area, we didn't boot before, so
334 * this is not a resume. In that case we just create the cbmem toc.
335 */
336
337 *(u32 *)CBMEM_BOOT_MODE = 0;
338 *(u32 *)CBMEM_RESUME_BACKUP = 0;
339
340 if ((boot_mode == 2) && cbmem_was_initted) {
341 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
342 if (resume_backup_memory) {
343 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
344 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
345 }
346 /* Magic for S3 resume */
347 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
348 } else if (boot_mode == 2) {
349 /* Failed S3 resume, reset to come up cleanly */
350 outb(0x6, 0xcf9);
351 hlt();
352 } else {
353 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
354 }
355#endif
356 post_code(0x3f);
357#if CONFIG_CHROMEOS
358 init_chromeos(boot_mode);
359#endif
360#if CONFIG_COLLECT_TIMESTAMPS
361 timestamp_init(base_time);
362 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
363 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
364 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
365 timestamp_add_now(TS_END_ROMSTAGE);
366#endif
367#if CONFIG_CONSOLE_CBMEM
368 /* Keep this the last thing this function does. */
369 cbmemc_reinit();
370#endif
371}