blob: 09dc60b23b75db259a0e4ff5233509789ce217ab [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>
Stefan Reinauer6651da32012-04-27 23:16:30 +020026#include <device/pci_def.h>
27#include <device/pnp_def.h>
28#include <cpu/x86/lapic.h>
29#include <pc80/mc146818rtc.h>
30#include <cbmem.h>
31#include <console/console.h>
32#include "superio/smsc/sio1007/early_serial.c"
33#include "northbridge/intel/sandybridge/sandybridge.h"
34#include "northbridge/intel/sandybridge/raminit.h"
35#include "southbridge/intel/bd82x6x/pch.h"
36#include "southbridge/intel/bd82x6x/gpio.h"
37#include <arch/cpu.h>
38#include <cpu/x86/bist.h>
39#include <cpu/x86/msr.h>
40#include "gpio.h"
41#if CONFIG_CHROMEOS
42#include <vendorcode/google/chromeos/chromeos.h>
43#endif
44
45static void pch_enable_lpc(void)
46{
47 device_t dev = PCH_LPC_DEV;
48 int i;
49
50 /* Set COM1/COM2 decode range */
51 pci_write_config16(dev, LPC_IO_DEC, 0x0010);
52
53 /* Enable SuperIO + COM1 + PS/2 Keyboard/Mouse */
54 u16 lpc_config = CNF1_LPC_EN | CNF2_LPC_EN | COMA_LPC_EN | KBC_LPC_EN;
55 pci_write_config16(dev, LPC_EN, lpc_config);
56
57 /* Map 256 bytes at 0x1600 to the LPC bus. */
58 pci_write_config32(dev, LPC_GEN1_DEC, 0xfc1601);
59
60 /* Map a range for the runtime registers to the LPC bus. */
61 pci_write_config32(dev, LPC_GEN2_DEC, 0xc0181);
62
63 for (i = 0; i < ARRAY_SIZE(sio1007_lpc_ports); i++) {
64 if (sio1007_enable_uart_at(sio1007_lpc_ports[i])) {
65 /* Keep COMA UART enable bit on. */
66 pci_write_config16(dev, LPC_EN,
67 lpc_config | COMA_LPC_EN);
68 break;
69 }
70 }
71}
72
73static void rcba_config(void)
74{
75 u32 reg32;
76
77 /*
78 * GFX INTA -> PIRQA (MSI)
79 * D28IP_P1IP WLAN INTA -> PIRQB
80 * D28IP_P4IP ETH0 INTB -> PIRQC
81 * D29IP_E1P EHCI1 INTA -> PIRQD
82 * D26IP_E2P EHCI2 INTA -> PIRQE
83 * D31IP_SIP SATA INTA -> PIRQF (MSI)
84 * D31IP_SMIP SMBUS INTB -> PIRQG
85 * D31IP_TTIP THRT INTC -> PIRQH
86 * D27IP_ZIP HDA INTA -> PIRQG (MSI)
87 */
88
89 /* Device interrupt pin register (board specific) */
90 RCBA32(D31IP) = (INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
91 (INTB << D31IP_SMIP) | (INTA << D31IP_SIP);
92 RCBA32(D30IP) = (NOINT << D30IP_PIP);
93 RCBA32(D29IP) = (INTA << D29IP_E1P);
94 RCBA32(D28IP) = (INTA << D28IP_P1IP) | (INTC << D28IP_P3IP) |
95 (INTB << D28IP_P4IP);
96 RCBA32(D27IP) = (INTA << D27IP_ZIP);
97 RCBA32(D26IP) = (INTA << D26IP_E2P);
98 RCBA32(D25IP) = (NOINT << D25IP_LIP);
99 RCBA32(D22IP) = (NOINT << D22IP_MEI1IP);
100
101 /* Device interrupt route registers */
102 DIR_ROUTE(D31IR, PIRQF, PIRQG, PIRQH, PIRQA);
103 DIR_ROUTE(D29IR, PIRQD, PIRQE, PIRQF, PIRQG);
104 DIR_ROUTE(D28IR, PIRQB, PIRQC, PIRQD, PIRQE);
105 DIR_ROUTE(D27IR, PIRQG, PIRQH, PIRQA, PIRQB);
106 DIR_ROUTE(D26IR, PIRQE, PIRQF, PIRQG, PIRQH);
107 DIR_ROUTE(D25IR, PIRQA, PIRQB, PIRQC, PIRQD);
108 DIR_ROUTE(D22IR, PIRQA, PIRQB, PIRQC, PIRQD);
109
110 /* Enable IOAPIC (generic) */
111 RCBA16(OIC) = 0x0100;
112 /* PCH BWG says to read back the IOAPIC enable register */
113 (void) RCBA16(OIC);
114
Stefan Reinauer6651da32012-04-27 23:16:30 +0200115 /* Disable unused devices (board specific) */
116 reg32 = RCBA32(FD);
117 reg32 |= PCH_DISABLE_ALWAYS;
118 RCBA32(FD) = reg32;
119}
120
121// FIXME, this function is generic code that should go to sb/... or
122// nb/../early_init.c
123static void early_pch_init(void)
124{
125 u8 reg8;
126
127 // reset rtc power status
128 reg8 = pci_read_config8(PCH_LPC_DEV, 0xa4);
129 reg8 &= ~(1 << 2);
130 pci_write_config8(PCH_LPC_DEV, 0xa4, reg8);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200131}
132
133static void setup_sio_gpios(void)
134{
135 const u16 port = 0x164e;
136 const u16 runtime_port = 0x180;
137
138 /* Turn on configuration mode. */
139 outb(0x55, port);
140
141 /* Set the GPIO direction, polarity, and type. */
142 sio1007_setreg(port, 0x31, 1 << 0, 1 << 0);
143 sio1007_setreg(port, 0x32, 0 << 0, 1 << 0);
144 sio1007_setreg(port, 0x33, 0 << 0, 1 << 0);
145
146 /* Set the base address for the runtime register block. */
147 sio1007_setreg(port, 0x30, runtime_port >> 4, 0xff);
148 sio1007_setreg(port, 0x21, runtime_port >> 12, 0xff);
149
150 /* Turn on address decoding for it. */
151 sio1007_setreg(port, 0x3a, 1 << 1, 1 << 1);
152
153 /* Set the value of GPIO 10 by changing GP1, bit 0. */
154 u8 byte;
155 byte = inb(runtime_port + 0xc);
156 byte |= (1 << 0);
157 outb(byte, runtime_port + 0xc);
158
159 /* Turn off address decoding for it. */
160 sio1007_setreg(port, 0x3a, 0 << 1, 1 << 1);
161
162 /* Turn off configuration mode. */
163 outb(0xaa, port);
164}
165
166void main(unsigned long bist)
167{
168 int boot_mode = 0;
169 int cbmem_was_initted;
170 u32 pm1_cnt;
171 u16 pm1_sts;
172
Stefan Reinauer6651da32012-04-27 23:16:30 +0200173 struct pei_data pei_data = {
174 pei_version: PEI_VERSION,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700175 mchbar: DEFAULT_MCHBAR,
176 dmibar: DEFAULT_DMIBAR,
177 epbar: DEFAULT_EPBAR,
178 pciexbar: CONFIG_MMCONF_BASE_ADDRESS,
179 smbusbar: SMBUS_IO_BASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200180 wdbbar: 0x4000000,
181 wdbsize: 0x1000,
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200182 hpet_address: CONFIG_HPET_ADDRESS,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700183 rcba: DEFAULT_RCBABASE,
184 pmbase: DEFAULT_PMBASE,
185 gpiobase: DEFAULT_GPIOBASE,
Stefan Reinauer6651da32012-04-27 23:16:30 +0200186 thermalbase: 0xfed08000,
187 system_type: 0, // 0 Mobile, 1 Desktop/Server
188 tseg_size: CONFIG_SMM_TSEG_SIZE,
189 spd_addresses: { 0xa0, 0x00, 0xa4, 0x00 },
190 ts_addresses: { 0x00, 0x00, 0x00, 0x00 },
191 ec_present: 0,
192 // 0 = leave channel enabled
193 // 1 = disable dimm 0 on channel
194 // 2 = disable dimm 1 on channel
195 // 3 = disable dimm 0+1 on channel
196 dimm_channel0_disabled: 2,
197 dimm_channel1_disabled: 2,
198 max_ddr3_freq: 1600,
199 usb_port_config: {
200 { 1, 0, 0x0040 }, /* P0: Front port (OC0) */
201 { 1, 1, 0x0040 }, /* P1: Back port (OC1) */
202 { 1, 0, 0x0040 }, /* P2: MINIPCIE1 (no OC) */
203 { 1, 0, 0x0040 }, /* P3: MMC (no OC) */
204 { 1, 2, 0x0040 }, /* P4: Front port (OC2) */
205 { 0, 0, 0x0000 }, /* P5: Empty */
206 { 0, 0, 0x0000 }, /* P6: Empty */
207 { 0, 0, 0x0000 }, /* P7: Empty */
208 { 1, 4, 0x0040 }, /* P8: Back port (OC4) */
209 { 1, 4, 0x0040 }, /* P9: MINIPCIE3 (no OC) */
210 { 1, 4, 0x0040 }, /* P10: BLUETOOTH (no OC) */
211 { 0, 4, 0x0000 }, /* P11: Empty */
212 { 1, 6, 0x0040 }, /* P12: Back port (OC6) */
213 { 1, 5, 0x0040 }, /* P13: Back port (OC5) */
214 },
215 };
216
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300217 timestamp_init(get_initial_timestamp());
218 timestamp_add_now(TS_START_ROMSTAGE);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200219
220 if (bist == 0)
221 enable_lapic();
222
223 pch_enable_lpc();
224
225 /* Enable GPIOs */
226 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
227 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
Gabe Black599e2042012-03-30 14:33:02 -0700228 setup_pch_gpios(&emeraldlake2_gpio_map);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200229 setup_sio_gpios();
230
231 /* Early SuperIO setup */
232 console_init();
233
234 /* Halt if there was a built in self test failure */
235 report_bist_failure(bist);
236
237 if (MCHBAR16(SSKPD) == 0xCAFE) {
238 printk(BIOS_DEBUG, "soft reset detected\n");
239 boot_mode = 1;
240
241 /* System is not happy after keyboard reset... */
242 printk(BIOS_DEBUG, "Issuing CF9 warm reset\n");
243 outb(0x6, 0xcf9);
244 hlt();
245 }
246
247 /* Perform some early chipset initialization required
248 * before RAM initialization can work
249 */
250 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
251 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
252
253 /* Check PM1_STS[15] to see if we are waking from Sx */
254 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
255
256 /* Read PM1_CNT[12:10] to determine which Sx state */
257 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
258
259 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
260#if CONFIG_HAVE_ACPI_RESUME
261 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
262 boot_mode = 2;
263 /* Clear SLP_TYPE. This will break stage2 but
264 * we care for that when we get there.
265 */
266 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
267#else
268 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
269#endif
270 }
271
272 post_code(0x38);
273 /* Enable SPD ROMs and DDR-III DRAM */
274 enable_smbus();
275
276 /* Prepare USB controller early in S3 resume */
277 if (boot_mode == 2)
278 enable_usb_bar();
279
280 post_code(0x3a);
281 pei_data.boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300282 timestamp_add_now(TS_BEFORE_INITRAM);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200283 sdram_initialize(&pei_data);
284
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300285 timestamp_add_now(TS_AFTER_INITRAM);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200286 post_code(0x3b);
287 /* Perform some initialization that must run before stage2 */
288 early_pch_init();
289 post_code(0x3c);
290
291 /* This should probably go away. Until now it is required
292 * and mainboard specific
293 */
294 rcba_config();
295 post_code(0x3d);
296
Stefan Reinauer6651da32012-04-27 23:16:30 +0200297 quick_ram_check();
Stefan Reinauerafcaac22012-06-18 15:43:50 -0700298 post_code(0x3e);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200299
300 MCHBAR16(SSKPD) = 0xCAFE;
Stefan Reinauer6651da32012-04-27 23:16:30 +0200301 cbmem_was_initted = !cbmem_initialize();
Stefan Reinauer6651da32012-04-27 23:16:30 +0200302
303#if CONFIG_HAVE_ACPI_RESUME
304 /* If there is no high memory area, we didn't boot before, so
305 * this is not a resume. In that case we just create the cbmem toc.
306 */
307
308 *(u32 *)CBMEM_BOOT_MODE = 0;
309 *(u32 *)CBMEM_RESUME_BACKUP = 0;
310
311 if ((boot_mode == 2) && cbmem_was_initted) {
312 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
313 if (resume_backup_memory) {
314 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
315 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
316 }
317 /* Magic for S3 resume */
318 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
319 } else if (boot_mode == 2) {
320 /* Failed S3 resume, reset to come up cleanly */
321 outb(0x6, 0xcf9);
322 hlt();
323 } else {
324 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
325 }
326#endif
327 post_code(0x3f);
328#if CONFIG_CHROMEOS
329 init_chromeos(boot_mode);
330#endif
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300331 timestamp_sync();
Stefan Reinauer6651da32012-04-27 23:16:30 +0200332 timestamp_add_now(TS_END_ROMSTAGE);
Stefan Reinauer6651da32012-04-27 23:16:30 +0200333#if CONFIG_CONSOLE_CBMEM
334 /* Keep this the last thing this function does. */
335 cbmemc_reinit();
336#endif
337}