blob: 094ba328efb1fa5f2872d84500725f714fc1c9bc [file] [log] [blame]
Martin Rothbf6b83a2015-10-11 10:37:02 +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 * Copyright (C) 2013 Sage Electronic Engineering, LLC.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Martin Rothbf6b83a2015-10-11 10:37:02 +020016 */
17
18
19#include <stdint.h>
20#include <string.h>
21#include <lib.h>
22#include <timestamp.h>
23#include <arch/io.h>
24#include <device/pci_def.h>
25#include <device/pnp_def.h>
26#include <cpu/x86/lapic.h>
27#include <pc80/mc146818rtc.h>
28#include <cbmem.h>
29#include <console/console.h>
30#include <halt.h>
31#include <reset.h>
32#include <superio/smsc/sio1007/chip.h>
33#include <fsp_util.h>
34#include <northbridge/intel/fsp_sandybridge/northbridge.h>
35#include <northbridge/intel/fsp_sandybridge/raminit.h>
36#include <southbridge/intel/fsp_bd82x6x/pch.h>
37#include <southbridge/intel/fsp_bd82x6x/gpio.h>
38#include <southbridge/intel/fsp_bd82x6x/me.h>
39#include <arch/cpu.h>
40#include <cpu/x86/msr.h>
41#include "gpio.h"
42#include <arch/stages.h>
43
44#define SIO_PORT 0x164e
45
46static inline void reset_system(void)
47{
48 hard_reset();
49 halt();
50}
51
52static void pch_enable_lpc(void)
53{
54 device_t dev = PCH_LPC_DEV;
55
56 /* Set COM1/COM2 decode range */
57 pci_write_config16(dev, LPC_IO_DEC, 0x0010);
58
59 /* Enable SuperIO + PS/2 Keyboard/Mouse */
60 u16 lpc_config = CNF1_LPC_EN | CNF2_LPC_EN | KBC_LPC_EN;
61 pci_write_config16(dev, LPC_EN, lpc_config);
62
63 /* Map 256 bytes at 0x1600 to the LPC bus. */
64 pci_write_config32(dev, LPC_GEN1_DEC, 0xfc1601);
65
66 /* Map a range for the runtime registers to the LPC bus. */
67 pci_write_config32(dev, LPC_GEN2_DEC, 0xc0181);
68
69 if (sio1007_enable_uart_at(SIO_PORT)) {
70 pci_write_config16(dev, LPC_EN,
71 lpc_config | COMA_LPC_EN);
72 }
73}
74
75static void setup_sio_gpios(void)
76{
77 const u16 port = SIO_PORT;
78 const u16 runtime_port = 0x180;
79
80 /* Turn on configuration mode. */
81 outb(0x55, port);
82
83 /* Set the GPIO direction, polarity, and type. */
84 sio1007_setreg(port, 0x31, 1 << 0, 1 << 0);
85 sio1007_setreg(port, 0x32, 0 << 0, 1 << 0);
86 sio1007_setreg(port, 0x33, 0 << 0, 1 << 0);
87
88 /* Set the base address for the runtime register block. */
89 sio1007_setreg(port, 0x30, runtime_port >> 4, 0xff);
90 sio1007_setreg(port, 0x21, runtime_port >> 12, 0xff);
91
92 /* Turn on address decoding for it. */
93 sio1007_setreg(port, 0x3a, 1 << 1, 1 << 1);
94
95 /*
96 * Enable the RS232 transiver.
97 * Set the value of GPIO 10 by changing GP1, bit 0.
98 */
99 u8 byte;
100 byte = inb(runtime_port + 0xc);
101 byte |= (1 << 0);
102 outb(byte, runtime_port + 0xc);
103
104 /* Turn off address decoding for it. */
105 sio1007_setreg(port, 0x3a, 0 << 1, 1 << 1);
106
107 /* Turn off configuration mode. */
108 outb(0xaa, port);
109}
110
111static void rcba_config(void)
112{
113 u32 reg32;
114
115 /*
116 * GFX INTA -> PIRQA (MSI)
117 * D31IP_SIP SATA INTB -> PIRQD
118 * D31IP_SMIP SMBUS INTC -> PIRQC
119 * D31IP_SIP SATA2 INTB -> PIRQD
120 * D31IP_TTIP THRT INTC -> PIRQC
121 * D29IP_E1P EHCI1 INTA -> PIRQD
122 * D28IP_P1IP INTA -> PIRQD
123 * D28IP_P2IP INTB -> PIRQC
124 * D28IP_P3IP INTC -> PIRQB
125 * D28IP_P4IP INTD -> PIRQA
126 * D28IP_P5IP INTA -> PIRQD
127 * D28IP_P6IP INTB -> PIRQC
128 * D28IP_P7IP INTC -> PIRQB
129 * D28IP_P8IP INTD -> PIRQA
130 * D27IP_ZIP HDA INTA -> PIRQD
131 * D26IP_E2P EHCI2 INTA -> PIRQD
132 * D20IP_XHCI XHCI INTA -> PIRQD (MSI)
133 */
134
135 /* Device interrupt pin register (board specific) */
136 RCBA32(D31IP) = (INTC << D31IP_TTIP) | (INTB << D31IP_SIP2) |
137 (INTC << D31IP_SMIP) | (INTB << D31IP_SIP);
138 RCBA32(D29IP) = (INTA << D29IP_E1P);
139 RCBA32(D28IP) = (INTD << D28IP_P8IP) | (INTC << D28IP_P7IP) |
140 (INTB << D28IP_P6IP) | (INTA << D28IP_P5IP) |
141 (INTD << D28IP_P4IP) | (INTC << D28IP_P3IP) |
142 (INTB << D28IP_P2IP) | (INTA << D28IP_P1IP);
143 RCBA32(D27IP) = (INTA << D27IP_ZIP);
144 RCBA32(D26IP) = (INTA << D26IP_E2P);
145 RCBA32(D25IP) = (INTA << D25IP_LIP);
146 RCBA32(D22IP) = (INTB << D22IP_KTIP) | (INTC << D22IP_IDERIP) |
147 (INTB << D22IP_MEI2IP) | (INTA << D22IP_MEI1IP);
148 RCBA32(D20IP) = (INTA << D20IP_XHCIIP);
149
150 /* Device interrupt route registers */
151 DIR_ROUTE(D31IR, PIRQA, PIRQD, PIRQC, PIRQA);
152 DIR_ROUTE(D29IR, PIRQH, PIRQD, PIRQA, PIRQC);
153 DIR_ROUTE(D28IR, PIRQA, PIRQB, PIRQC, PIRQD);
154 DIR_ROUTE(D27IR, PIRQG, PIRQB, PIRQC, PIRQD);
155 DIR_ROUTE(D26IR, PIRQF, PIRQA, PIRQC, PIRQD);
156 DIR_ROUTE(D25IR, PIRQE, PIRQF, PIRQG, PIRQH);
157 DIR_ROUTE(D22IR, PIRQA, PIRQD, PIRQC, PIRQB);
158 DIR_ROUTE(D20IR, PIRQD, PIRQE, PIRQF, PIRQG);
159
160 /* Enable IOAPIC (generic) */
161 RCBA16(OIC) = 0x0100;
162 /* PCH BWG says to read back the IOAPIC enable register */
163 (void) RCBA16(OIC);
164
165 /* Disable unused devices (board specific) */
166 reg32 = RCBA32(FD);
167 reg32 |= PCH_DISABLE_ALWAYS;
168 RCBA32(FD) = reg32;
169}
170
171void main(FSP_INFO_HEADER *fsp_info_header)
172{
173#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
174 int boot_mode = 0;
175#endif
176 u32 pm1_cnt;
177 u16 pm1_sts;
178
179 post_code(0x40);
180
181 timestamp_init(get_initial_timestamp());
182 timestamp_add_now(TS_START_ROMSTAGE);
183
184 pch_enable_lpc();
185
186 /* Enable GPIOs */
187 pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE|1);
188 pci_write_config8(PCH_LPC_DEV, GPIO_CNTL, 0x10);
189 setup_pch_gpios(&gpio_map);
190 setup_sio_gpios();
191
192 console_init();
193 post_code(0x41);
194
195 post_code(0x42);
196 sandybridge_sb_early_initialization();
197
198 post_code(0x43);
199 sandybridge_early_initialization(SANDYBRIDGE_MOBILE);
200 printk(BIOS_DEBUG, "Back from sandybridge_early_initialization()\n");
201
202 post_code(0x44);
203 /* Wait for ME to be ready */
204 intel_early_me_status();
205 intel_early_me_init();
206 intel_early_me_uma_size();
207
208 post_code(0x45);
209 /* Check PM1_STS[15] to see if we are waking from Sx */
210 pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
211
212 /* Read PM1_CNT[12:10] to determine which Sx state */
213 pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
214 post_code(0x46);
215 if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
216#if CONFIG_HAVE_ACPI_RESUME
217 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
218 boot_mode = 2;
219 /* Clear SLP_TYPE. This will break stage2 but
220 * we care for that when we get there.
221 */
222 outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT);
223#else
224 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
225#endif
226 }
227
228 post_code(0x48);
229
230 timestamp_add_now(TS_BEFORE_INITRAM);
231
232 /*
233 * Call early init to initialize memory and chipset. This function returns
234 * to the romstage_main_continue function with a pointer to the HOB
235 * structure.
236 */
237 printk(BIOS_DEBUG, "Starting the Intel FSP (early_init)\n");
238 fsp_early_init(fsp_info_header);
239 die("Uh Oh! fsp_early_init should not return here.\n");
240}
241
242/*******************************************************************************
243 * The FSP early_init function returns to this function.
244 * Memory is setup and the stack is set by the FSP.
245 ******************************************************************************/
246void romstage_main_continue(EFI_STATUS status, VOID *HobListPtr) {
247 int cbmem_was_initted;
248 u32 reg32;
249 void *cbmem_hob_ptr;
250
251 timestamp_add_now(TS_AFTER_INITRAM);
252
253 /*
254 * HD AUDIO is not used on this system, so we're using some registers
255 * in there as temporary registers to save TSC values. This is complete
256 * now, so disable the audio block.
257 */
258 reg32 = RCBA32(FD);
259 reg32 |= PCH_DISABLE_HD_AUDIO;
260 RCBA32(FD) = reg32;
261
262 post_code(0x49);
263
264#if CONFIG_USBDEBUG
265 /* FSP reconfigures USB, so reinit it to have debug */
266 early_usbdebug_init();
267#endif
268
269 /* For reference print FSP version */
270 u32 version = MCHBAR32(0x5034);
271 printk(BIOS_DEBUG, "FSP Version %d.%d.%d Build %d\n",
272 version >> 24 , (version >> 16) & 0xff,
273 (version >> 8) & 0xff, version & 0xff);
274 printk(BIOS_DEBUG, "FSP Status: 0x%0x\n", (u32)status);
275
276 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
277
278 printk(BIOS_SPEW, "FD & FD2 Settings:\n");
279 display_fd_settings();
280
281 report_memory_config();
282
283 post_code(0x4b);
284
285 early_pch_init();
286 post_code(0x4c);
287
288 rcba_config();
289 post_code(0x4d);
290
291 quick_ram_check();
292 post_code(0x4e);
293
294 cbmem_was_initted = !cbmem_recovery(0);
295
296 if(cbmem_was_initted) {
297 reset_system();
298 }
299
300 /* Save the HOB pointer in CBMEM to be used in ramstage. */
301 cbmem_hob_ptr = cbmem_add (CBMEM_ID_HOB_POINTER, sizeof(*HobListPtr));
302 *(u32*)cbmem_hob_ptr = (u32)HobListPtr;
303 post_code(0x4f);
304
305 /* Load the ramstage. */
306 copy_and_run();
307 while (1);
308}
309
310void romstage_fsp_rt_buffer_callback(FSP_INIT_RT_BUFFER *FspRtBuffer)
311{
312 /* No overrides needed */
313 return;
314}
315
316uint64_t get_initial_timestamp(void)
317{
318 return (uint64_t) pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0) << 4;
319}