blob: 1b93eb67f4442d1300e931ad16470b5e97992328 [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050018 */
19
20#include <stddef.h>
21#include <arch/cpu.h>
22#include <arch/io.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050023#include <arch/stages.h>
Aaron Durbin00bf3db2014-01-09 10:33:23 -060024#include <arch/early_variables.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050025#include <console/console.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060026#include <cbfs.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050027#include <cbmem.h>
28#include <cpu/x86/mtrr.h>
Aaron Durbin3e0eea12013-10-28 11:20:35 -050029#if CONFIG_EC_GOOGLE_CHROMEEC
30#include <ec/google/chromeec/ec.h>
31#endif
Aaron Durbina8e9b632013-10-30 15:46:07 -050032#include <elog.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050033#include <romstage_handoff.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060034#include <stage_cache.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050035#include <timestamp.h>
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +020036#include <tpm.h>
Aaron Durbinebf7ec52013-11-14 13:47:08 -060037#include <vendorcode/google/chromeos/chromeos.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070038#include <soc/gpio.h>
39#include <soc/iomap.h>
40#include <soc/lpc.h>
41#include <soc/pci_devs.h>
42#include <soc/pmc.h>
43#include <soc/reset.h>
44#include <soc/romstage.h>
45#include <soc/smm.h>
46#include <soc/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050047
48/* The cache-as-ram assembly file calls romstage_main() after setting up
49 * cache-as-ram. romstage_main() will then call the mainboards's
50 * mainboard_romstage_entry() function. That function then calls
51 * romstage_common() below. The reason for the back and forth is to provide
52 * common entry point from cache-as-ram while still allowing for code sharing.
53 * Because we can't use global variables the stack is used for allocations --
54 * thus the need to call back and forth. */
55
56static void *setup_stack_and_mttrs(void);
57
58static void program_base_addresses(void)
59{
60 uint32_t reg;
61 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
62
63 /* Memory Mapped IO registers. */
64 reg = PMC_BASE_ADDRESS | 2;
65 pci_write_config32(lpc_dev, PBASE, reg);
66 reg = IO_BASE_ADDRESS | 2;
67 pci_write_config32(lpc_dev, IOBASE, reg);
68 reg = ILB_BASE_ADDRESS | 2;
69 pci_write_config32(lpc_dev, IBASE, reg);
70 reg = SPI_BASE_ADDRESS | 2;
71 pci_write_config32(lpc_dev, SBASE, reg);
72 reg = MPHY_BASE_ADDRESS | 2;
73 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050074 reg = PUNIT_BASE_ADDRESS | 2;
75 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050076 reg = RCBA_BASE_ADDRESS | 1;
77 pci_write_config32(lpc_dev, RCBA, reg);
78
79 /* IO Port Registers. */
80 reg = ACPI_BASE_ADDRESS | 2;
81 pci_write_config32(lpc_dev, ABASE, reg);
82 reg = GPIO_BASE_ADDRESS | 2;
83 pci_write_config32(lpc_dev, GBASE, reg);
84}
85
Aaron Durbin6f9947a2013-11-18 11:16:20 -060086static void spi_init(void)
87{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080088 u32 *scs = (u32 *)(SPI_BASE_ADDRESS + SCS);
89 u32 *bcr = (u32 *)(SPI_BASE_ADDRESS + BCR);
Aaron Durbin4177db52014-02-05 14:55:26 -060090 uint32_t reg;
91
92 /* Disable generating SMI when setting WPD bit. */
93 write32(scs, read32(scs) & ~SMIWPEN);
94 /*
95 * Enable caching and prefetching in the SPI controller. Disable
96 * the SMM-only BIOS write and set WPD bit.
97 */
98 reg = (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH | BCR_WPD;
99 reg &= ~EISS;
100 write32(bcr, reg);
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600101}
102
Aaron Durbin794bddf2013-09-27 11:38:36 -0500103/* Entry from cache-as-ram.inc. */
104void * asmlinkage romstage_main(unsigned long bist,
105 uint32_t tsc_low, uint32_t tsc_hi)
106{
107 struct romstage_params rp = {
108 .bist = bist,
109 .mrc_params = NULL,
110 };
111
112 /* Save initial timestamp from bootblock. */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200113 timestamp_init((((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
114
Aaron Durbin794bddf2013-09-27 11:38:36 -0500115 /* Save romstage begin */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200116 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500117
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500118 program_base_addresses();
119
Aaron Durbinfd039f72013-10-04 11:11:52 -0500120 tco_disable();
121
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500122 byt_config_com1_and_enable();
123
124 console_init();
125
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600126 spi_init();
127
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500128 set_max_freq();
129
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500130 punit_init();
131
Aaron Durbinecf90862013-09-24 12:36:14 -0500132 gfx_init();
133
Aaron Durbin3e0eea12013-10-28 11:20:35 -0500134#if CONFIG_EC_GOOGLE_CHROMEEC
135 /* Ensure the EC is in the right mode for recovery */
136 google_chromeec_early_init();
137#endif
138
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500139 /* Call into mainboard. */
140 mainboard_romstage_entry(&rp);
141
142 return setup_stack_and_mttrs();
143}
144
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600145static struct chipset_power_state power_state CAR_GLOBAL;
146
Aaron Durbin41607a42015-06-09 13:54:10 -0500147static void migrate_power_state(int is_recovery)
Aaron Durbin6e328932013-11-06 12:04:50 -0600148{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600149 struct chipset_power_state *ps_cbmem;
150 struct chipset_power_state *ps_car;
151
152 ps_car = car_get_var_ptr(&power_state);
153 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
154
155 if (ps_cbmem == NULL) {
156 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
157 return;
158 }
159 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
160}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200161ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600162
163static struct chipset_power_state *fill_power_state(void)
164{
165 struct chipset_power_state *ps = car_get_var_ptr(&power_state);
166
167 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
168 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
169 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
170 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
171 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
172 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800173 ps->prsts = read32((u32 *)(PMC_BASE_ADDRESS + PRSTS));
174 ps->gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1));
175 ps->gen_pmcon2 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON2));
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600176
177 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
178 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
179 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
180 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
181 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
182 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
183
184 return ps;
185}
186
187/* Return 0, 3, or 5 to indicate the previous sleep state. */
188static int chipset_prev_sleep_state(struct chipset_power_state *ps)
189{
Aaron Durbin6e328932013-11-06 12:04:50 -0600190 /* Default to S0. */
191 int prev_sleep_state = 0;
192
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600193 if (ps->pm1_sts & WAK_STS) {
194 switch ((ps->pm1_cnt & SLP_TYP) >> SLP_TYP_SHIFT) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600195 #if CONFIG_HAVE_ACPI_RESUME
196 case SLP_TYP_S3:
197 prev_sleep_state = 3;
198 break;
199 #endif
200 case SLP_TYP_S5:
201 prev_sleep_state = 5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600202 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600203 }
204 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600205 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600206 }
207
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600208 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600209 prev_sleep_state = 5;
210 }
211
Aaron Durbin6e328932013-11-06 12:04:50 -0600212 return prev_sleep_state;
213}
214
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500215/* Entry from the mainboard. */
216void romstage_common(struct romstage_params *params)
217{
218 struct romstage_handoff *handoff;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600219 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600220 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500221
Kyösti Mälkki41759272014-12-31 21:11:51 +0200222 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500223
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600224 ps = fill_power_state();
225 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600226
227 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
228
Aaron Durbin4177db52014-02-05 14:55:26 -0600229#if CONFIG_ELOG_BOOT_COUNT
230 if (prev_sleep_state != 3)
231 boot_count_increment();
232#endif
233
234
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500235 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600236 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500237
Kyösti Mälkki41759272014-12-31 21:11:51 +0200238 timestamp_add_now(TS_AFTER_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500239
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500240 handoff = romstage_handoff_find_or_add();
241 if (handoff != NULL)
Aaron Durbin6e328932013-11-06 12:04:50 -0600242 handoff->s3_resume = (prev_sleep_state == 3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500243 else
244 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
245
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200246 if (CONFIG_LPC_TPM) {
247 init_tpm(prev_sleep_state == 3);
248 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500249}
250
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500251void asmlinkage romstage_after_car(void)
252{
Aaron Durbin794bddf2013-09-27 11:38:36 -0500253 timestamp_add_now(TS_END_ROMSTAGE);
254
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500255 /* Load the ramstage. */
256 copy_and_run();
257 while (1);
258}
259
260static inline uint32_t *stack_push(u32 *stack, u32 value)
261{
262 stack = &stack[-1];
263 *stack = value;
264 return stack;
265}
266
267/* Romstage needs quite a bit of stack for decompressing images since the lzma
268 * lib keeps its state on the stack during romstage. */
269static unsigned long choose_top_of_stack(void)
270{
271 unsigned long stack_top;
272 const unsigned long romstage_ram_stack_size = 0x5000;
273
274 /* cbmem_add() does a find() before add(). */
275 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
276 romstage_ram_stack_size);
277 stack_top += romstage_ram_stack_size;
278 return stack_top;
279}
280
281/* setup_stack_and_mttrs() determines the stack to use after
282 * cache-as-ram is torn down as well as the MTRR settings to use. */
283static void *setup_stack_and_mttrs(void)
284{
285 unsigned long top_of_stack;
286 int num_mtrrs;
287 uint32_t *slot;
288 uint32_t mtrr_mask_upper;
289 uint32_t top_of_ram;
290
291 /* Top of stack needs to be aligned to a 4-byte boundary. */
292 top_of_stack = choose_top_of_stack() & ~3;
293 slot = (void *)top_of_stack;
294 num_mtrrs = 0;
295
296 /* The upper bits of the MTRR mask need to set according to the number
297 * of physical address bits. */
298 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
299
300 /* The order for each MTRR is value then base with upper 32-bits of
301 * each value coming before the lower 32-bits. The reasoning for
302 * this ordering is to create a stack layout like the following:
303 * +0: Number of MTRRs
304 * +4: MTRR base 0 31:0
305 * +8: MTRR base 0 63:32
306 * +12: MTRR mask 0 31:0
307 * +16: MTRR mask 0 63:32
308 * +20: MTRR base 1 31:0
309 * +24: MTRR base 1 63:32
310 * +28: MTRR mask 1 31:0
311 * +32: MTRR mask 1 63:32
312 */
313
314 /* Cache the ROM as WP just below 4GiB. */
315 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
316 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
317 slot = stack_push(slot, 0); /* upper base */
318 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
319 num_mtrrs++;
320
321 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
322 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
323 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
324 slot = stack_push(slot, 0); /* upper base */
325 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
326 num_mtrrs++;
327
328 top_of_ram = (uint32_t)cbmem_top();
329 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
330 * start of the TSEG region. It is required to be 8MiB aligned. Set
331 * this area as cacheable so it can be used later for ramstage before
332 * setting up the entire RAM as cacheable. */
333 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
334 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
335 slot = stack_push(slot, 0); /* upper base */
336 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
337 num_mtrrs++;
338
339 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
340 * region resides. However, it is not restricted to SMM mode until
341 * SMM has been relocated. By setting the region to cacheable it
342 * provides faster access when relocating the SMM handler as well
343 * as using the TSEG region for other purposes. */
344 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
345 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
346 slot = stack_push(slot, 0); /* upper base */
347 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
348 num_mtrrs++;
349
350 /* Save the number of MTRRs to setup. Return the stack location
351 * pointing to the number of MTRRs. */
352 slot = stack_push(slot, num_mtrrs);
353
354 return slot;
355}
Aaron Durbindc249f62013-10-10 21:03:50 -0500356
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600357void ramstage_cache_invalid(void)
Aaron Durbindc249f62013-10-10 21:03:50 -0500358{
359#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
360 /* Perform cold reset on invalid ramstage cache. */
361 cold_reset();
362#endif
363}
Shawn Nematbakhsh565d4092014-03-14 14:06:45 -0700364
365#if CONFIG_CHROMEOS
366int vboot_get_sw_write_protect(void)
367{
368 u8 status;
369 /* Return unprotected status if status read fails. */
370 return (early_spi_read_wpsr(&status) ? 0 : !!(status & 0x80));
371}
372#endif