blob: 4bba76ec9d321854a473748baa144e3215833a8c [file] [log] [blame]
Stefan Reinauer155e9b52012-04-27 23:19:58 +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 <cbfs.h>
32#include <cbmem.h>
33#include <console/console.h>
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 "option_table.h"
42#include "gpio.h"
43#if CONFIG_CONSOLE_SERIAL8250
44#include "superio/smsc/lpc47n207/lpc47n207.h"
45#include "superio/smsc/lpc47n207/early_serial.c"
46#endif
47#if CONFIG_CHROMEOS
48#include <vendorcode/google/chromeos/chromeos.h>
49#endif
50
51static void pch_enable_lpc(void)
52{
53 /* Set COM1/COM2 decode range */
54 pci_write_config16(PCH_LPC_DEV, LPC_IO_DEC, 0x0010);
55
56#if CONFIG_CONSOLE_SERIAL8250
57 /* Enable SuperIO + EC + KBC + COM1 + lpc47n207 config*/
58 pci_write_config16(PCH_LPC_DEV, LPC_EN, CNF1_LPC_EN | MC_LPC_EN |
59 KBC_LPC_EN | CNF2_LPC_EN | COMA_LPC_EN);
60
61 /* map full 256 bytes at 0x1600 to the LPC bus */
62 pci_write_config32(PCH_LPC_DEV, LPC_GEN1_DEC, 0xfc1601);
63
64 try_enabling_LPC47N207_uart();
65#else
66 /* Enable SuperIO + EC + KBC */
67 pci_write_config16(PCH_LPC_DEV, LPC_EN, CNF1_LPC_EN | MC_LPC_EN |
68 KBC_LPC_EN);
69#endif
70}
71
72static void rcba_config(void)
73{
74 u32 reg32;
75
76 /*
77 * GFX INTA -> PIRQA (MSI)
78 * D28IP_P1IP WLAN INTA -> PIRQB
79 * D28IP_P4IP ETH0 INTB -> PIRQC (MSI)
80 * D29IP_E1P EHCI1 INTA -> PIRQD
81 * D26IP_E2P EHCI2 INTA -> PIRQB
82 * D31IP_SIP SATA INTA -> PIRQA (MSI)
83 * D31IP_SMIP SMBUS INTC -> PIRQH
84 * D31IP_TTIP THRT INTB -> PIRQG
85 * D27IP_ZIP HDA INTA -> PIRQG (MSI)
86 *
87 * LIGHTSENSOR -> PIRQE (Edge Triggered)
88 * TRACKPAD -> PIRQF (Edge Triggered)
89 */
90
91 /* Device interrupt pin register (board specific) */
92 RCBA32(D31IP) = (INTB << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
93 (INTC << D31IP_SMIP) | (INTA << D31IP_SIP);
94 RCBA32(D30IP) = (NOINT << D30IP_PIP);
95 RCBA32(D29IP) = (INTA << D29IP_E1P);
96 RCBA32(D28IP) = (INTA << D28IP_P1IP) | (INTC << D28IP_P3IP) |
97 (INTB << D28IP_P4IP);
98 RCBA32(D27IP) = (INTA << D27IP_ZIP);
99 RCBA32(D26IP) = (INTA << D26IP_E2P);
100 RCBA32(D25IP) = (NOINT << D25IP_LIP);
101 RCBA32(D22IP) = (NOINT << D22IP_MEI1IP);
102
103 /* Device interrupt route registers */
104 DIR_ROUTE(D31IR, PIRQA, PIRQG, PIRQH, PIRQB);
105 DIR_ROUTE(D29IR, PIRQD, PIRQE, PIRQG, PIRQH);
106 DIR_ROUTE(D28IR, PIRQB, PIRQC, PIRQD, PIRQE);
107 DIR_ROUTE(D27IR, PIRQG, PIRQH, PIRQA, PIRQB);
108 DIR_ROUTE(D26IR, PIRQB, PIRQC, PIRQD, PIRQA);
109 DIR_ROUTE(D25IR, PIRQA, PIRQB, PIRQC, PIRQD);
110 DIR_ROUTE(D22IR, PIRQA, PIRQB, PIRQC, PIRQD);
111
112 /* Enable IOAPIC (generic) */
113 RCBA16(OIC) = 0x0100;
114 /* PCH BWG says to read back the IOAPIC enable register */
115 (void) RCBA16(OIC);
116
Stefan Reinauer155e9b52012-04-27 23:19:58 +0200117 /* Disable unused devices (board specific) */
118 reg32 = RCBA32(FD);
119 reg32 |= PCH_DISABLE_ALWAYS;
120 RCBA32(FD) = reg32;
121}
122
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);
131}
132
133void main(unsigned long bist)
134{
135 int boot_mode = 0;
136 int cbmem_was_initted;
137 u32 pm1_cnt;
138 u16 pm1_sts;
139
140#if CONFIG_COLLECT_TIMESTAMPS
141 tsc_t start_romstage_time;
142 tsc_t before_dram_time;
143 tsc_t after_dram_time;
144 tsc_t base_time = {
145 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
146 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
147 };
148#endif
149
150 struct pei_data pei_data = {
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700151 .mchbar = DEFAULT_MCHBAR,
152 .dmibar = DEFAULT_DMIBAR,
153 .epbar = DEFAULT_EPBAR,
154 .pciexbar = CONFIG_MMCONF_BASE_ADDRESS,
155 .smbusbar = SMBUS_IO_BASE,
Stefan Reinauer155e9b52012-04-27 23:19:58 +0200156 .wdbbar = 0x4000000,
157 .wdbsize = 0x1000,
Stefan Reinauere6063fe2012-04-30 14:57:51 -0700158 .hpet_address = HPET_ADDR,
159 .rcba = DEFAULT_RCBABASE,
160 .pmbase = DEFAULT_PMBASE,
161 .gpiobase = DEFAULT_GPIOBASE,
Stefan Reinauer155e9b52012-04-27 23:19:58 +0200162 .thermalbase = 0xfed08000,
163 .system_type = 0, // 0 Mobile, 1 Desktop/Server
164 .tseg_size = CONFIG_SMM_TSEG_SIZE,
165 .spd_addresses = { 0x50, 0x00,0xf0,0x00 },
166 .ts_addresses = { 0x30, 0x00, 0x00, 0x00 },
167 .ec_present = 1,
168 // 0 = leave channel enabled
169 // 1 = disable dimm 0 on channel
170 // 2 = disable dimm 1 on channel
171 // 3 = disable dimm 0+1 on channel
172 .dimm_channel0_disabled = 2,
173 .dimm_channel1_disabled = 2,
174 .usb_port_config = {
175 { 1, 0, 0x0080 }, /* P0: Port 0 (OC0) */
176 { 1, 1, 0x0080 }, /* P1: Port 1 (OC1) */
177 { 1, 0, 0x0040 }, /* P2: MINIPCIE1 (no OC) */
178 { 1, 0, 0x0040 }, /* P3: MMC (no OC) */
179 { 0, 0, 0x0000 }, /* P4: Empty */
180 { 0, 0, 0x0000 }, /* P5: Empty */
181 { 0, 0, 0x0000 }, /* P6: Empty */
182 { 0, 0, 0x0000 }, /* P7: Empty */
183 { 1, 4, 0x0040 }, /* P8: MINIPCIE2 (no OC) */
184 { 0, 4, 0x0000 }, /* P9: Empty */
185 { 0, 4, 0x0000 }, /* P10: Empty */
186 { 1, 4, 0x0040 }, /* P11: Camera (no OC) */
187 { 0, 4, 0x0000 }, /* P12: Empty */
188 { 0, 4, 0x0000 }, /* P13: Empty */
189 },
190 .spd_data = {
191 }
192 };
193
194 typedef const uint8_t spd_blob[256];
195 struct cbfs_file *spd_file;
196 spd_blob *spd_data;
197
198
199#if CONFIG_COLLECT_TIMESTAMPS
200 start_romstage_time = rdtsc();
201#endif
202
203 if (bist == 0)
204 enable_lapic();
205
206 pch_enable_lpc();
207
208 /* Enable GPIOs */
209 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
210 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
211 setup_pch_gpios(&lumpy_gpio_map);
212
213 console_init();
214
215#if CONFIG_CHROMEOS
216 save_chromeos_gpios();
217#endif
218
219 /* Halt if there was a built in self test failure */
220 report_bist_failure(bist);
221
222 if (MCHBAR16(SSKPD) == 0xCAFE) {
223 printk(BIOS_DEBUG, "soft reset detected\n");
224 boot_mode = 1;
225
226 /* System is not happy after keyboard reset... */
227 printk(BIOS_DEBUG, "Issuing CF9 warm reset\n");
228 outb(0x6, 0xcf9);
229 hlt();
230 }
231
232 /* Perform some early chipset initialization required
233 * before RAM initialization can work
234 */
235 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
236 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
237
238 /* Check PM1_STS[15] to see if we are waking from Sx */
239 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
240
241 /* Read PM1_CNT[12:10] to determine which Sx state */
242 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
243
244 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
245#if CONFIG_HAVE_ACPI_RESUME
246 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
247 boot_mode = 2;
248 /* Clear SLP_TYPE. This will break stage2 but
249 * we care for that when we get there.
250 */
251 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
252#else
253 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
254#endif
255 }
256
257 post_code(0x38);
258 /* Enable SPD ROMs and DDR-III DRAM */
259 enable_smbus();
260
261 /* Prepare USB controller early in S3 resume */
262 if (boot_mode == 2)
263 enable_usb_bar();
264
265 u32 gp_lvl2 = inl(DEFAULT_GPIOBASE + 0x38);
266 u8 gpio33, gpio41, gpio49;
267 gpio33 = (gp_lvl2 >> (33-32)) & 1;
268 gpio41 = (gp_lvl2 >> (41-32)) & 1;
269 gpio49 = (gp_lvl2 >> (49-32)) & 1;
270 printk(BIOS_DEBUG, "Memory Straps:\n");
271 printk(BIOS_DEBUG, " - memory capacity %dGB\n",
272 gpio33 ? 2 : 1);
273 printk(BIOS_DEBUG, " - die revision %d\n",
274 gpio41 ? 2 : 1);
275 printk(BIOS_DEBUG, " - vendor %s\n",
276 gpio49 ? "Samsung" : "Other");
277
278 int spd_index = 0;
279
280 switch ((gpio49 << 2) | (gpio41 << 1) | gpio33) {
281 case 0: // Other 1G Rev 1
282 spd_index = 0;
283 break;
284 case 2: // Other 1G Rev 2
285 spd_index = 1;
286 break;
287 case 1: // Other 2G Rev 1
288 case 3: // Other 2G Rev 2
289 spd_index = 2;
290 break;
291 case 4: // Samsung 1G Rev 1
292 spd_index = 3;
293 break;
294 case 6: // Samsung 1G Rev 2
295 spd_index = 4;
296 break;
297 case 5: // Samsung 2G Rev 1
298 case 7: // Samsung 2G Rev 2
299 spd_index = 5;
300 break;
301 }
302
303 spd_file = cbfs_find("spd.bin");
304 if (!spd_file)
305 die("SPD data not found.");
306 if (spd_file->len < (spd_index + 1) * 256)
307 die("Missing SPD data.");
308 spd_data = (spd_blob *)CBFS_SUBHEADER(spd_file);
309 // leave onboard dimm address at f0, and copy spd data there.
310 memcpy(pei_data.spd_data[0], spd_data[spd_index], 256);
311
312 post_code(0x39);
313 pei_data.boot_mode = boot_mode;
314#if CONFIG_COLLECT_TIMESTAMPS
315 before_dram_time = rdtsc();
316#endif
317 sdram_initialize(&pei_data);
318
319#if CONFIG_COLLECT_TIMESTAMPS
320 after_dram_time = rdtsc();
321#endif
322 post_code(0x3a);
323 /* Perform some initialization that must run before stage2 */
324 early_pch_init();
325 post_code(0x3b);
326
327 rcba_config();
328 post_code(0x3c);
329
Stefan Reinauer155e9b52012-04-27 23:19:58 +0200330 quick_ram_check();
Stefan Reinauerafcaac22012-06-18 15:43:50 -0700331 post_code(0x3e);
Stefan Reinauer155e9b52012-04-27 23:19:58 +0200332
333 MCHBAR16(SSKPD) = 0xCAFE;
334
335#if CONFIG_EARLY_CBMEM_INIT
336 cbmem_was_initted = !cbmem_initialize();
337#else
338 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
339 - HIGH_MEMORY_SIZE));
340#endif
341
342#if CONFIG_HAVE_ACPI_RESUME
343 /* If there is no high memory area, we didn't boot before, so
344 * this is not a resume. In that case we just create the cbmem toc.
345 */
346
347 *(u32 *)CBMEM_BOOT_MODE = 0;
348 *(u32 *)CBMEM_RESUME_BACKUP = 0;
349
350 if ((boot_mode == 2) && cbmem_was_initted) {
351 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
352 if (resume_backup_memory) {
353 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
354 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
355 }
356 /* Magic for S3 resume */
357 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
358 } else if (boot_mode == 2) {
359 /* Failed S3 resume, reset to come up cleanly */
360 outb(0x6, 0xcf9);
361 hlt();
362 } else {
363 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
364 }
365#endif
366 post_code(0x3f);
367#if CONFIG_CHROMEOS
368 init_chromeos(boot_mode);
369#endif
370#if CONFIG_COLLECT_TIMESTAMPS
371 timestamp_init(base_time);
372 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
373 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
374 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
375 timestamp_add_now(TS_END_ROMSTAGE);
376#endif
377#if CONFIG_CONSOLE_CBMEM
378 /* Keep this the last thing this function does. */
379 cbmemc_reinit();
380#endif
381}