blob: 09fd6346064badf54e8ad0fe2e82dad89f3bad91 [file] [log] [blame]
Nico Huberefe1fed2013-04-29 18:00:57 +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 <device/pci_def.h>
27#include <device/pnp_def.h>
28#include <cpu/x86/lapic.h>
29#include <pc80/mc146818rtc.h>
Kyösti Mälkki6722f8d2014-06-16 09:14:49 +030030#include <arch/acpi.h>
Nico Huberefe1fed2013-04-29 18:00:57 +020031#include <cbmem.h>
32#include <console/console.h>
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
42static void pch_enable_lpc(void)
43{
44 /* Set COM3/COM1 decode ranges: 0x3e8/0x3f8 */
45 pci_write_config16(PCH_LPC_DEV, LPC_IO_DEC, 0x0070);
46
47 /* Enable KBC on 0x06/0x64 (KBC),
48 * EC on 0x62/0x66 (MC),
49 * EC on 0x20c-0x20f (GAMEH),
50 * Super I/O on 0x2e/0x2f (CNF1),
51 * COM1/COM3 decode ranges. */
52 pci_write_config16(PCH_LPC_DEV, LPC_EN,
53 KBC_LPC_EN | MC_LPC_EN |
54 CNF1_LPC_EN | GAMEH_LPC_EN |
55 COMA_LPC_EN | COMB_LPC_EN);
56}
57
58static void rcba_config(void)
59{
60 u32 reg32;
61
62 /*
63 * D31IP_TTIP THRT INTC -> PIRQC
64 * D31IP_SIP2 SATA2 NOINT
65 * D31IP_SMIP SMBUS INTC -> PIRQC
66 * D31IP_SIP SATA INTB -> PIRQD (MSI)
67 * D29IP_E1P EHCI1 INTA -> PIRQH
68 * D28IP_P8IP Slot? INTD -> PIRQD
69 * D28IP_P7IP PCIEx1 INTC -> PIRQC
70 * D28IP_P6IP 1394 INTB -> PIRQB (MSI)
71 * D28IP_P5IP GbEPHY INTA -> PIRQA
72 * D28IP_P4IP ETH2 INTD -> PIRQD (MSI)
73 * D28IP_P3IP ETH1 INTC -> PIRQC (MSI)
74 * D28IP_P2IP Slot? INTB -> PIRQB
75 * D28IP_P1IP Slot? INTA -> PIRQA
76 * D27IP_ZIP HDA INTA -> PIRQG (MSI)
77 * D26IP_E2P EHCI2 INTA -> PIRQA
78 * D25IP_LIP ETH0 INTA -> PIRQE (MSI)
79 * D22IP_KTIP MEI NOINT
80 * D22IP_IDERIP MEI NOINT
81 * D22IP_MEI2IP MEI NOINT
82 * D22IP_MEI1IP MEI NOINT
83 * D20IP_XHCIIP XHCI INTA -> PIRQA (MSI)
84 * GFX INTA -> PIRQA (MSI)
85 * PEGx16 INTA -> PIRQA
86 * INTB -> PIRQB
87 * INTC -> PIRQC
88 * INTD -> PIRQD
89 */
90
91 /* Device interrupt pin register (board specific) */
92 RCBA32(D31IP) = (INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
93 (INTC << D31IP_SMIP) | (INTB << D31IP_SIP);
94 RCBA32(D29IP) = (INTA << D29IP_E1P);
95 RCBA32(D28IP) = (INTA << D28IP_P1IP) | (INTB << D28IP_P2IP) |
96 (INTC << D28IP_P3IP) | (INTD << D28IP_P4IP) |
97 (INTA << D28IP_P5IP) | (INTB << D28IP_P6IP) |
98 (INTC << D28IP_P7IP) | (INTD << D28IP_P8IP);
99 RCBA32(D27IP) = (INTA << D27IP_ZIP);
100 RCBA32(D26IP) = (INTA << D26IP_E2P);
101 RCBA32(D25IP) = (INTA << D25IP_LIP);
102 RCBA32(D22IP) = (NOINT << D22IP_MEI1IP);
103 RCBA32(D20IP) = (INTA << D20IP_XHCIIP);
104
105 /* Device interrupt route registers */
106 DIR_ROUTE(D31IR, PIRQA, PIRQD, PIRQC, PIRQA);
107 DIR_ROUTE(D29IR, PIRQH, PIRQD, PIRQA, PIRQC);
108 DIR_ROUTE(D28IR, PIRQA, PIRQB, PIRQC, PIRQD);
109 DIR_ROUTE(D27IR, PIRQG, PIRQB, PIRQC, PIRQD);
110 DIR_ROUTE(D26IR, PIRQA, PIRQF, PIRQC, PIRQD);
111 DIR_ROUTE(D25IR, PIRQE, PIRQF, PIRQG, PIRQH);
112 DIR_ROUTE(D22IR, PIRQA, PIRQD, PIRQC, PIRQB);
113 DIR_ROUTE(D20IR, PIRQA, PIRQB, PIRQC, PIRQD);
114
115 /* Enable IOAPIC (generic) */
116 RCBA16(OIC) = 0x0100;
117 /* PCH BWG says to read back the IOAPIC enable register */
118 (void) RCBA16(OIC);
119
120 /* Disable unused devices (board specific) */
121 reg32 = RCBA32(FD);
122 reg32 |= PCH_DISABLE_ALWAYS;
123 /* Disable PCI bridge so MRC does not probe this bus */
124 reg32 |= PCH_DISABLE_P2P;
125 RCBA32(FD) = reg32;
126}
127
128static void pnp_enter_ext_func_mode(device_t dev)
129{
130 u16 port = dev >> 8;
131 outb(0x87, port);
132 outb(0x87, port);
133}
134
135static void pnp_exit_ext_func_mode(device_t dev)
136{
137 u16 port = dev >> 8;
138 outb(0xaa, port);
139}
140
141static void superio_gpio_config(void)
142{
143 device_t dev = PNP_DEV(0x2e, 0x9);
144 pnp_enter_ext_func_mode(dev);
145 pnp_set_logical_device(dev);
146 /* Values can only be changed, when devices are enabled. */
147 pnp_write_config(dev, 0x30, 0x03); /* Enable GPIO2+3 */
148 pnp_write_config(dev, 0xe3, 0xdd); /* GPIO2 bits 1, 5 are output */
149 pnp_write_config(dev, 0xe4, 0x22); /* GPIO2 bits 1, 5 are 1 */
150 pnp_exit_ext_func_mode(dev);
151}
152
153void main(unsigned long bist)
154{
155 int boot_mode = 0;
156 int cbmem_was_initted;
157 u32 pm1_cnt;
158 u16 pm1_sts;
159
Nico Huberefe1fed2013-04-29 18:00:57 +0200160 struct pei_data pei_data = {
Edward O'Callaghan6f49f692014-05-24 02:04:52 +1000161 .pei_version = PEI_VERSION,
162 .mchbar = DEFAULT_MCHBAR,
163 .dmibar = DEFAULT_DMIBAR,
164 .epbar = DEFAULT_EPBAR,
165 .pciexbar = CONFIG_MMCONF_BASE_ADDRESS,
166 .smbusbar = SMBUS_IO_BASE,
167 .wdbbar = 0x4000000,
168 .wdbsize = 0x1000,
169 .hpet_address = CONFIG_HPET_ADDRESS,
170 .rcba = DEFAULT_RCBABASE,
171 .pmbase = DEFAULT_PMBASE,
172 .gpiobase = DEFAULT_GPIOBASE,
173 .thermalbase = 0xfed08000,
174 .system_type = 0, // 0 Mobile, 1 Desktop/Server
175 .tseg_size = CONFIG_SMM_TSEG_SIZE,
176 .spd_addresses = { 0xA0, 0x00,0xA4,0x00 },
177 .ts_addresses = { 0x00, 0x00, 0x00, 0x00 },
178 .ec_present = 1,
179 .gbe_enable = 1,
180 .ddr3lv_support = 0,
Nico Huberefe1fed2013-04-29 18:00:57 +0200181 // 0 = leave channel enabled
182 // 1 = disable dimm 0 on channel
183 // 2 = disable dimm 1 on channel
184 // 3 = disable dimm 0+1 on channel
Edward O'Callaghan6f49f692014-05-24 02:04:52 +1000185 .dimm_channel0_disabled = 2,
186 .dimm_channel1_disabled = 2,
187 .max_ddr3_freq = 1600,
188 .usb_port_config = {
Nico Huberefe1fed2013-04-29 18:00:57 +0200189 /* enabled usb oc pin length */
190 { 1, 0, 0x0040 }, /* P0: lower left USB 3.0 (OC0) */
191 { 1, 0, 0x0040 }, /* P1: upper left USB 3.0 (OC0) */
192 { 1, 0, 0x0040 }, /* P2: lower right USB 3.0 (OC0) */
193 { 1, 0, 0x0040 }, /* P3: upper right USB 3.0 (OC0) */
194 { 1, 0, 0x0040 }, /* P4: lower USB 2.0 (OC0) */
195 { 1, 0, 0x0040 }, /* P5: upper USB 2.0 (OC0) */
196 { 1, 0, 0x0040 }, /* P6: front panel USB 2.0 (OC0) */
197 { 1, 0, 0x0040 }, /* P7: front panel USB 2.0 (OC0) */
198 { 1, 4, 0x0040 }, /* P8: internal USB 2.0 (OC4) */
199 { 1, 4, 0x0040 }, /* P9: internal USB 2.0 (OC4) */
200 { 1, 4, 0x0040 }, /* P10: internal USB 2.0 (OC4) */
201 { 1, 4, 0x0040 }, /* P11: internal USB 2.0 (OC4) */
202 { 1, 4, 0x0040 }, /* P12: internal USB 2.0 (OC4) */
203 { 1, 4, 0x0040 }, /* P13: internal USB 2.0 (OC4) */
204 },
Edward O'Callaghan6f49f692014-05-24 02:04:52 +1000205 .usb3 = {
206 .mode = 3, /* Smart Auto? */
207 .hs_port_switch_mask = 0xf, /* All four ports. */
208 .preboot_support = 1, /* preOS driver? */
209 .xhci_streams = 1, /* Enable. */
Nico Huberefe1fed2013-04-29 18:00:57 +0200210 },
Edward O'Callaghan6f49f692014-05-24 02:04:52 +1000211 .pcie_init = 1,
Nico Huberefe1fed2013-04-29 18:00:57 +0200212 };
213
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300214 timestamp_init(get_initial_timestamp());
215 timestamp_add_now(TS_START_ROMSTAGE);
Nico Huberefe1fed2013-04-29 18:00:57 +0200216
217 if (bist == 0)
218 enable_lapic();
219
220 pch_enable_lpc();
221
222 /* Enable GPIOs */
223 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
224 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
225 setup_pch_gpios(&ktqm77_gpio_map);
226 superio_gpio_config();
227
228 /* Initialize console device(s) */
229 console_init();
230
231 /* Halt if there was a built in self test failure */
232 report_bist_failure(bist);
233
234 if (MCHBAR16(SSKPD) == 0xCAFE) {
235 printk(BIOS_DEBUG, "soft reset detected\n");
236 boot_mode = 1;
237
238 /* System is not happy after keyboard reset... */
239 printk(BIOS_DEBUG, "Issuing CF9 warm reset\n");
240 outb(0x6, 0xcf9);
241 hlt();
242 }
243
244 /* Perform some early chipset initialization required
245 * before RAM initialization can work
246 */
247 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
248 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
249
250 /* Enable PEG10 (1x16) */
251 pci_write_config32(PCI_DEV(0, 0, 0), DEVEN,
252 pci_read_config32(PCI_DEV(0, 0, 0), DEVEN) |
253 DEVEN_PEG10);
254
255 /* Check PM1_STS[15] to see if we are waking from Sx */
256 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
257
258 /* Read PM1_CNT[12:10] to determine which Sx state */
259 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
260
261 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
Kyösti Mälkki6722f8d2014-06-16 09:14:49 +0300262 if (acpi_s3_resume_allowed()) {
263 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
264 boot_mode = 2;
265 /* Clear SLP_TYPE. This will break stage2 but
266 * we care for that when we get there.
267 */
268 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
269 } else {
270 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
271 }
Nico Huberefe1fed2013-04-29 18:00:57 +0200272 }
273
274 post_code(0x38);
275 /* Enable SPD ROMs and DDR-III DRAM */
276 enable_smbus();
277
278 /* Prepare USB controller early in S3 resume */
279 if (boot_mode == 2)
280 enable_usb_bar();
281
282 post_code(0x39);
283
284 post_code(0x3a);
285 pei_data.boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300286 timestamp_add_now(TS_BEFORE_INITRAM);
Nico Huberefe1fed2013-04-29 18:00:57 +0200287 sdram_initialize(&pei_data);
288
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300289 timestamp_add_now(TS_AFTER_INITRAM);
Nico Huberefe1fed2013-04-29 18:00:57 +0200290 post_code(0x3c);
291
292 rcba_config();
293 post_code(0x3d);
294
295 quick_ram_check();
296 post_code(0x3e);
297
298 MCHBAR16(SSKPD) = 0xCAFE;
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +0200299 cbmem_was_initted = !cbmem_recovery(boot_mode==2);
Kyösti Mälkki78938482014-01-04 11:02:45 +0200300 if (boot_mode!=2)
301 save_mrc_data(&pei_data);
Nico Huberefe1fed2013-04-29 18:00:57 +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);
Nico Huberefe1fed2013-04-29 18:00:57 +0200328 timestamp_add_now(TS_END_ROMSTAGE);
Nico Huberefe1fed2013-04-29 18:00:57 +0200329}