blob: ac5afabffdfa8238a99fe9688978ebcf752a0f40 [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{
Aaron Durbin4177db52014-02-05 14:55:26 -060087 const unsigned long scs = SPI_BASE_ADDRESS + SCS;
Aaron Durbin6f9947a2013-11-18 11:16:20 -060088 const unsigned long bcr = 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 -0500102static inline void mark_ts(struct romstage_params *rp, uint64_t ts)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500103{
Aaron Durbin794bddf2013-09-27 11:38:36 -0500104 struct romstage_timestamps *rt = &rp->ts;
105
106 rt->times[rt->count] = ts;
107 rt->count++;
108}
109
110/* Entry from cache-as-ram.inc. */
111void * asmlinkage romstage_main(unsigned long bist,
112 uint32_t tsc_low, uint32_t tsc_hi)
113{
114 struct romstage_params rp = {
115 .bist = bist,
116 .mrc_params = NULL,
117 };
118
119 /* Save initial timestamp from bootblock. */
120 mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
121 /* Save romstage begin */
122 mark_ts(&rp, timestamp_get());
123
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500124 program_base_addresses();
125
Aaron Durbinfd039f72013-10-04 11:11:52 -0500126 tco_disable();
127
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500128 byt_config_com1_and_enable();
129
130 console_init();
131
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600132 spi_init();
133
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500134 set_max_freq();
135
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500136 punit_init();
137
Aaron Durbinecf90862013-09-24 12:36:14 -0500138 gfx_init();
139
Aaron Durbin3e0eea12013-10-28 11:20:35 -0500140#if CONFIG_EC_GOOGLE_CHROMEEC
141 /* Ensure the EC is in the right mode for recovery */
142 google_chromeec_early_init();
143#endif
144
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500145 /* Call into mainboard. */
146 mainboard_romstage_entry(&rp);
147
148 return setup_stack_and_mttrs();
149}
150
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600151static struct chipset_power_state power_state CAR_GLOBAL;
152
153static void migrate_power_state(void)
Aaron Durbin6e328932013-11-06 12:04:50 -0600154{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600155 struct chipset_power_state *ps_cbmem;
156 struct chipset_power_state *ps_car;
157
158 ps_car = car_get_var_ptr(&power_state);
159 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
160
161 if (ps_cbmem == NULL) {
162 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
163 return;
164 }
165 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
166}
167CAR_MIGRATE(migrate_power_state);
168
169static struct chipset_power_state *fill_power_state(void)
170{
171 struct chipset_power_state *ps = car_get_var_ptr(&power_state);
172
173 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
174 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
175 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
176 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
177 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
178 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
179 ps->prsts = read32(PMC_BASE_ADDRESS + PRSTS);
180 ps->gen_pmcon1 = read32(PMC_BASE_ADDRESS + GEN_PMCON1);
181 ps->gen_pmcon2 = read32(PMC_BASE_ADDRESS + GEN_PMCON2);
182
183 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
184 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
185 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
186 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
187 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
188 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
189
190 return ps;
191}
192
193/* Return 0, 3, or 5 to indicate the previous sleep state. */
194static int chipset_prev_sleep_state(struct chipset_power_state *ps)
195{
Aaron Durbin6e328932013-11-06 12:04:50 -0600196 /* Default to S0. */
197 int prev_sleep_state = 0;
198
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600199 if (ps->pm1_sts & WAK_STS) {
200 switch ((ps->pm1_cnt & SLP_TYP) >> SLP_TYP_SHIFT) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600201 #if CONFIG_HAVE_ACPI_RESUME
202 case SLP_TYP_S3:
203 prev_sleep_state = 3;
204 break;
205 #endif
206 case SLP_TYP_S5:
207 prev_sleep_state = 5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600208 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600209 }
210 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600211 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600212 }
213
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600214 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600215 prev_sleep_state = 5;
216 }
217
Aaron Durbin6e328932013-11-06 12:04:50 -0600218 return prev_sleep_state;
219}
220
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600221static inline void chromeos_init(int prev_sleep_state)
222{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600223#if CONFIG_CHROMEOS
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600224 /* Normalize the sleep state to what init_chromeos() wants for S3: 2. */
225 init_chromeos(prev_sleep_state == 3 ? 2 : 0);
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600226#endif
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600227}
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600228
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500229/* Entry from the mainboard. */
230void romstage_common(struct romstage_params *params)
231{
232 struct romstage_handoff *handoff;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600233 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600234 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500235
Aaron Durbin794bddf2013-09-27 11:38:36 -0500236 mark_ts(params, timestamp_get());
237
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600238 ps = fill_power_state();
239 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600240
241 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
242
Aaron Durbin4177db52014-02-05 14:55:26 -0600243#if CONFIG_ELOG_BOOT_COUNT
244 if (prev_sleep_state != 3)
245 boot_count_increment();
246#endif
247
248
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500249 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600250 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500251
Aaron Durbin794bddf2013-09-27 11:38:36 -0500252 mark_ts(params, timestamp_get());
253
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500254 handoff = romstage_handoff_find_or_add();
255 if (handoff != NULL)
Aaron Durbin6e328932013-11-06 12:04:50 -0600256 handoff->s3_resume = (prev_sleep_state == 3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500257 else
258 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
259
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600260 chromeos_init(prev_sleep_state);
261
Aaron Durbin794bddf2013-09-27 11:38:36 -0500262 /* Save timestamp information. */
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700263 timestamp_init(params->ts.times[0]);
264 timestamp_add(TS_START_ROMSTAGE, params->ts.times[1]);
265 timestamp_add(TS_BEFORE_INITRAM, params->ts.times[2]);
266 timestamp_add(TS_AFTER_INITRAM, params->ts.times[3]);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500267}
268
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500269void asmlinkage romstage_after_car(void)
270{
Aaron Durbin794bddf2013-09-27 11:38:36 -0500271 timestamp_add_now(TS_END_ROMSTAGE);
272
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500273 /* Load the ramstage. */
274 copy_and_run();
275 while (1);
276}
277
278static inline uint32_t *stack_push(u32 *stack, u32 value)
279{
280 stack = &stack[-1];
281 *stack = value;
282 return stack;
283}
284
285/* Romstage needs quite a bit of stack for decompressing images since the lzma
286 * lib keeps its state on the stack during romstage. */
287static unsigned long choose_top_of_stack(void)
288{
289 unsigned long stack_top;
290 const unsigned long romstage_ram_stack_size = 0x5000;
291
292 /* cbmem_add() does a find() before add(). */
293 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
294 romstage_ram_stack_size);
295 stack_top += romstage_ram_stack_size;
296 return stack_top;
297}
298
299/* setup_stack_and_mttrs() determines the stack to use after
300 * cache-as-ram is torn down as well as the MTRR settings to use. */
301static void *setup_stack_and_mttrs(void)
302{
303 unsigned long top_of_stack;
304 int num_mtrrs;
305 uint32_t *slot;
306 uint32_t mtrr_mask_upper;
307 uint32_t top_of_ram;
308
309 /* Top of stack needs to be aligned to a 4-byte boundary. */
310 top_of_stack = choose_top_of_stack() & ~3;
311 slot = (void *)top_of_stack;
312 num_mtrrs = 0;
313
314 /* The upper bits of the MTRR mask need to set according to the number
315 * of physical address bits. */
316 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
317
318 /* The order for each MTRR is value then base with upper 32-bits of
319 * each value coming before the lower 32-bits. The reasoning for
320 * this ordering is to create a stack layout like the following:
321 * +0: Number of MTRRs
322 * +4: MTRR base 0 31:0
323 * +8: MTRR base 0 63:32
324 * +12: MTRR mask 0 31:0
325 * +16: MTRR mask 0 63:32
326 * +20: MTRR base 1 31:0
327 * +24: MTRR base 1 63:32
328 * +28: MTRR mask 1 31:0
329 * +32: MTRR mask 1 63:32
330 */
331
332 /* Cache the ROM as WP just below 4GiB. */
333 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
334 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
335 slot = stack_push(slot, 0); /* upper base */
336 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
337 num_mtrrs++;
338
339 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
340 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
341 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
342 slot = stack_push(slot, 0); /* upper base */
343 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
344 num_mtrrs++;
345
346 top_of_ram = (uint32_t)cbmem_top();
347 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
348 * start of the TSEG region. It is required to be 8MiB aligned. Set
349 * this area as cacheable so it can be used later for ramstage before
350 * setting up the entire RAM as cacheable. */
351 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
352 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
353 slot = stack_push(slot, 0); /* upper base */
354 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
355 num_mtrrs++;
356
357 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
358 * region resides. However, it is not restricted to SMM mode until
359 * SMM has been relocated. By setting the region to cacheable it
360 * provides faster access when relocating the SMM handler as well
361 * as using the TSEG region for other purposes. */
362 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
363 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
364 slot = stack_push(slot, 0); /* upper base */
365 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
366 num_mtrrs++;
367
368 /* Save the number of MTRRs to setup. Return the stack location
369 * pointing to the number of MTRRs. */
370 slot = stack_push(slot, num_mtrrs);
371
372 return slot;
373}
Aaron Durbindc249f62013-10-10 21:03:50 -0500374
Aaron Durbindc249f62013-10-10 21:03:50 -0500375void ramstage_cache_invalid(struct ramstage_cache *cache)
376{
377#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
378 /* Perform cold reset on invalid ramstage cache. */
379 cold_reset();
380#endif
381}
Shawn Nematbakhsh565d4092014-03-14 14:06:45 -0700382
383#if CONFIG_CHROMEOS
384int vboot_get_sw_write_protect(void)
385{
386 u8 status;
387 /* Return unprotected status if status read fails. */
388 return (early_spi_read_wpsr(&status) ? 0 : !!(status & 0x80));
389}
390#endif