blob: 91548e3edfc29338c9257eeafb3df55e65a7c82f [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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stddef.h>
21#include <arch/cpu.h>
22#include <arch/io.h>
23#include <arch/cbfs.h>
24#include <arch/stages.h>
Aaron Durbin00bf3db2014-01-09 10:33:23 -060025#include <arch/early_variables.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050026#include <console/console.h>
27#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 Durbindc249f62013-10-10 21:03:50 -050033#include <ramstage_cache.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050034#include <romstage_handoff.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050035#include <timestamp.h>
Aaron Durbinebf7ec52013-11-14 13:47:08 -060036#include <vendorcode/google/chromeos/chromeos.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050037#include <baytrail/gpio.h>
38#include <baytrail/iomap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050039#include <baytrail/lpc.h>
40#include <baytrail/pci_devs.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060041#include <baytrail/pmc.h>
Aaron Durbindc249f62013-10-10 21:03:50 -050042#include <baytrail/reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050043#include <baytrail/romstage.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050044#include <baytrail/smm.h>
Aaron Durbin6f9947a2013-11-18 11:16:20 -060045#include <baytrail/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050046
47/* The cache-as-ram assembly file calls romstage_main() after setting up
48 * cache-as-ram. romstage_main() will then call the mainboards's
49 * mainboard_romstage_entry() function. That function then calls
50 * romstage_common() below. The reason for the back and forth is to provide
51 * common entry point from cache-as-ram while still allowing for code sharing.
52 * Because we can't use global variables the stack is used for allocations --
53 * thus the need to call back and forth. */
54
55static void *setup_stack_and_mttrs(void);
56
57static void program_base_addresses(void)
58{
59 uint32_t reg;
60 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
61
62 /* Memory Mapped IO registers. */
63 reg = PMC_BASE_ADDRESS | 2;
64 pci_write_config32(lpc_dev, PBASE, reg);
65 reg = IO_BASE_ADDRESS | 2;
66 pci_write_config32(lpc_dev, IOBASE, reg);
67 reg = ILB_BASE_ADDRESS | 2;
68 pci_write_config32(lpc_dev, IBASE, reg);
69 reg = SPI_BASE_ADDRESS | 2;
70 pci_write_config32(lpc_dev, SBASE, reg);
71 reg = MPHY_BASE_ADDRESS | 2;
72 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050073 reg = PUNIT_BASE_ADDRESS | 2;
74 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050075 reg = RCBA_BASE_ADDRESS | 1;
76 pci_write_config32(lpc_dev, RCBA, reg);
77
78 /* IO Port Registers. */
79 reg = ACPI_BASE_ADDRESS | 2;
80 pci_write_config32(lpc_dev, ABASE, reg);
81 reg = GPIO_BASE_ADDRESS | 2;
82 pci_write_config32(lpc_dev, GBASE, reg);
83}
84
Aaron Durbin6f9947a2013-11-18 11:16:20 -060085static void spi_init(void)
86{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080087 u32 *scs = (u32 *)(SPI_BASE_ADDRESS + SCS);
88 u32 *bcr = (u32 *)(SPI_BASE_ADDRESS + BCR);
Aaron Durbin4177db52014-02-05 14:55:26 -060089 uint32_t reg;
90
91 /* Disable generating SMI when setting WPD bit. */
92 write32(scs, read32(scs) & ~SMIWPEN);
93 /*
94 * Enable caching and prefetching in the SPI controller. Disable
95 * the SMM-only BIOS write and set WPD bit.
96 */
97 reg = (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH | BCR_WPD;
98 reg &= ~EISS;
99 write32(bcr, reg);
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600100}
101
Aaron Durbin794bddf2013-09-27 11:38:36 -0500102/* Entry from cache-as-ram.inc. */
103void * asmlinkage romstage_main(unsigned long bist,
104 uint32_t tsc_low, uint32_t tsc_hi)
105{
106 struct romstage_params rp = {
107 .bist = bist,
108 .mrc_params = NULL,
109 };
110
111 /* Save initial timestamp from bootblock. */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200112 timestamp_init((((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
113
Aaron Durbin794bddf2013-09-27 11:38:36 -0500114 /* Save romstage begin */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200115 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500116
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500117 program_base_addresses();
118
Aaron Durbinfd039f72013-10-04 11:11:52 -0500119 tco_disable();
120
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500121 byt_config_com1_and_enable();
122
123 console_init();
124
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600125 spi_init();
126
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500127 set_max_freq();
128
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500129 punit_init();
130
Aaron Durbinecf90862013-09-24 12:36:14 -0500131 gfx_init();
132
Aaron Durbin3e0eea12013-10-28 11:20:35 -0500133#if CONFIG_EC_GOOGLE_CHROMEEC
134 /* Ensure the EC is in the right mode for recovery */
135 google_chromeec_early_init();
136#endif
137
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500138 /* Call into mainboard. */
139 mainboard_romstage_entry(&rp);
140
141 return setup_stack_and_mttrs();
142}
143
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600144static struct chipset_power_state power_state CAR_GLOBAL;
145
146static void migrate_power_state(void)
Aaron Durbin6e328932013-11-06 12:04:50 -0600147{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600148 struct chipset_power_state *ps_cbmem;
149 struct chipset_power_state *ps_car;
150
151 ps_car = car_get_var_ptr(&power_state);
152 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
153
154 if (ps_cbmem == NULL) {
155 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
156 return;
157 }
158 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
159}
160CAR_MIGRATE(migrate_power_state);
161
162static struct chipset_power_state *fill_power_state(void)
163{
164 struct chipset_power_state *ps = car_get_var_ptr(&power_state);
165
166 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
167 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
168 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
169 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
170 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
171 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800172 ps->prsts = read32((u32 *)(PMC_BASE_ADDRESS + PRSTS));
173 ps->gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1));
174 ps->gen_pmcon2 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON2));
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600175
176 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
177 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
178 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
179 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
180 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
181 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
182
183 return ps;
184}
185
186/* Return 0, 3, or 5 to indicate the previous sleep state. */
187static int chipset_prev_sleep_state(struct chipset_power_state *ps)
188{
Aaron Durbin6e328932013-11-06 12:04:50 -0600189 /* Default to S0. */
190 int prev_sleep_state = 0;
191
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600192 if (ps->pm1_sts & WAK_STS) {
193 switch ((ps->pm1_cnt & SLP_TYP) >> SLP_TYP_SHIFT) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600194 #if CONFIG_HAVE_ACPI_RESUME
195 case SLP_TYP_S3:
196 prev_sleep_state = 3;
197 break;
198 #endif
199 case SLP_TYP_S5:
200 prev_sleep_state = 5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600201 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600202 }
203 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600204 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600205 }
206
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600207 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600208 prev_sleep_state = 5;
209 }
210
Aaron Durbin6e328932013-11-06 12:04:50 -0600211 return prev_sleep_state;
212}
213
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600214static inline void chromeos_init(int prev_sleep_state)
215{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600216#if CONFIG_CHROMEOS
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600217 /* Normalize the sleep state to what init_chromeos() wants for S3: 2. */
218 init_chromeos(prev_sleep_state == 3 ? 2 : 0);
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600219#endif
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600220}
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600221
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500222/* Entry from the mainboard. */
223void romstage_common(struct romstage_params *params)
224{
225 struct romstage_handoff *handoff;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600226 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600227 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500228
Kyösti Mälkki41759272014-12-31 21:11:51 +0200229 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500230
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600231 ps = fill_power_state();
232 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600233
234 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
235
Aaron Durbin4177db52014-02-05 14:55:26 -0600236#if CONFIG_ELOG_BOOT_COUNT
237 if (prev_sleep_state != 3)
238 boot_count_increment();
239#endif
240
241
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500242 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600243 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500244
Kyösti Mälkki41759272014-12-31 21:11:51 +0200245 timestamp_add_now(TS_AFTER_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500246
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500247 handoff = romstage_handoff_find_or_add();
248 if (handoff != NULL)
Aaron Durbin6e328932013-11-06 12:04:50 -0600249 handoff->s3_resume = (prev_sleep_state == 3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500250 else
251 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
252
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600253 chromeos_init(prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500254}
255
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500256void asmlinkage romstage_after_car(void)
257{
Aaron Durbin794bddf2013-09-27 11:38:36 -0500258 timestamp_add_now(TS_END_ROMSTAGE);
259
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500260 /* Load the ramstage. */
261 copy_and_run();
262 while (1);
263}
264
265static inline uint32_t *stack_push(u32 *stack, u32 value)
266{
267 stack = &stack[-1];
268 *stack = value;
269 return stack;
270}
271
272/* Romstage needs quite a bit of stack for decompressing images since the lzma
273 * lib keeps its state on the stack during romstage. */
274static unsigned long choose_top_of_stack(void)
275{
276 unsigned long stack_top;
277 const unsigned long romstage_ram_stack_size = 0x5000;
278
279 /* cbmem_add() does a find() before add(). */
280 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
281 romstage_ram_stack_size);
282 stack_top += romstage_ram_stack_size;
283 return stack_top;
284}
285
286/* setup_stack_and_mttrs() determines the stack to use after
287 * cache-as-ram is torn down as well as the MTRR settings to use. */
288static void *setup_stack_and_mttrs(void)
289{
290 unsigned long top_of_stack;
291 int num_mtrrs;
292 uint32_t *slot;
293 uint32_t mtrr_mask_upper;
294 uint32_t top_of_ram;
295
296 /* Top of stack needs to be aligned to a 4-byte boundary. */
297 top_of_stack = choose_top_of_stack() & ~3;
298 slot = (void *)top_of_stack;
299 num_mtrrs = 0;
300
301 /* The upper bits of the MTRR mask need to set according to the number
302 * of physical address bits. */
303 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
304
305 /* The order for each MTRR is value then base with upper 32-bits of
306 * each value coming before the lower 32-bits. The reasoning for
307 * this ordering is to create a stack layout like the following:
308 * +0: Number of MTRRs
309 * +4: MTRR base 0 31:0
310 * +8: MTRR base 0 63:32
311 * +12: MTRR mask 0 31:0
312 * +16: MTRR mask 0 63:32
313 * +20: MTRR base 1 31:0
314 * +24: MTRR base 1 63:32
315 * +28: MTRR mask 1 31:0
316 * +32: MTRR mask 1 63:32
317 */
318
319 /* Cache the ROM as WP just below 4GiB. */
320 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
321 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
322 slot = stack_push(slot, 0); /* upper base */
323 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
324 num_mtrrs++;
325
326 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
327 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
328 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
329 slot = stack_push(slot, 0); /* upper base */
330 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
331 num_mtrrs++;
332
333 top_of_ram = (uint32_t)cbmem_top();
334 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
335 * start of the TSEG region. It is required to be 8MiB aligned. Set
336 * this area as cacheable so it can be used later for ramstage before
337 * setting up the entire RAM as cacheable. */
338 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
339 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
340 slot = stack_push(slot, 0); /* upper base */
341 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
342 num_mtrrs++;
343
344 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
345 * region resides. However, it is not restricted to SMM mode until
346 * SMM has been relocated. By setting the region to cacheable it
347 * provides faster access when relocating the SMM handler as well
348 * as using the TSEG region for other purposes. */
349 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
350 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
351 slot = stack_push(slot, 0); /* upper base */
352 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
353 num_mtrrs++;
354
355 /* Save the number of MTRRs to setup. Return the stack location
356 * pointing to the number of MTRRs. */
357 slot = stack_push(slot, num_mtrrs);
358
359 return slot;
360}
Aaron Durbindc249f62013-10-10 21:03:50 -0500361
Aaron Durbindc249f62013-10-10 21:03:50 -0500362void ramstage_cache_invalid(struct ramstage_cache *cache)
363{
364#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
365 /* Perform cold reset on invalid ramstage cache. */
366 cold_reset();
367#endif
368}
Shawn Nematbakhsh565d4092014-03-14 14:06:45 -0700369
370#if CONFIG_CHROMEOS
371int vboot_get_sw_write_protect(void)
372{
373 u8 status;
374 /* Return unprotected status if status read fails. */
375 return (early_spi_read_wpsr(&status) ? 0 : !!(status & 0x80));
376}
377#endif