blob: 91637d51ae4959a1cb1f1b3a0d5e9d5b36c150a1 [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>
25#include <console/console.h>
26#include <cbmem.h>
27#include <cpu/x86/mtrr.h>
Aaron Durbin3e0eea12013-10-28 11:20:35 -050028#if CONFIG_EC_GOOGLE_CHROMEEC
29#include <ec/google/chromeec/ec.h>
30#endif
Aaron Durbina8e9b632013-10-30 15:46:07 -050031#include <elog.h>
Aaron Durbindc249f62013-10-10 21:03:50 -050032#include <ramstage_cache.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050033#include <romstage_handoff.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050034#include <timestamp.h>
Aaron Durbinebf7ec52013-11-14 13:47:08 -060035#include <vendorcode/google/chromeos/chromeos.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050036#include <baytrail/gpio.h>
37#include <baytrail/iomap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050038#include <baytrail/lpc.h>
39#include <baytrail/pci_devs.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060040#include <baytrail/pmc.h>
Aaron Durbindc249f62013-10-10 21:03:50 -050041#include <baytrail/reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050042#include <baytrail/romstage.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050043#include <baytrail/smm.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050044
Aaron Durbin794bddf2013-09-27 11:38:36 -050045static inline uint64_t timestamp_get(void)
46{
47 return rdtscll();
48}
49
50static inline tsc_t ts64_to_tsc(uint64_t ts)
51{
52 tsc_t tsc = {
53 .lo = ts,
54 .hi = ts >> 32,
55 };
56 return tsc;
57}
58
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050059/* The cache-as-ram assembly file calls romstage_main() after setting up
60 * cache-as-ram. romstage_main() will then call the mainboards's
61 * mainboard_romstage_entry() function. That function then calls
62 * romstage_common() below. The reason for the back and forth is to provide
63 * common entry point from cache-as-ram while still allowing for code sharing.
64 * Because we can't use global variables the stack is used for allocations --
65 * thus the need to call back and forth. */
66
67static void *setup_stack_and_mttrs(void);
68
69static void program_base_addresses(void)
70{
71 uint32_t reg;
72 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
73
74 /* Memory Mapped IO registers. */
75 reg = PMC_BASE_ADDRESS | 2;
76 pci_write_config32(lpc_dev, PBASE, reg);
77 reg = IO_BASE_ADDRESS | 2;
78 pci_write_config32(lpc_dev, IOBASE, reg);
79 reg = ILB_BASE_ADDRESS | 2;
80 pci_write_config32(lpc_dev, IBASE, reg);
81 reg = SPI_BASE_ADDRESS | 2;
82 pci_write_config32(lpc_dev, SBASE, reg);
83 reg = MPHY_BASE_ADDRESS | 2;
84 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050085 reg = PUNIT_BASE_ADDRESS | 2;
86 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050087 reg = RCBA_BASE_ADDRESS | 1;
88 pci_write_config32(lpc_dev, RCBA, reg);
89
90 /* IO Port Registers. */
91 reg = ACPI_BASE_ADDRESS | 2;
92 pci_write_config32(lpc_dev, ABASE, reg);
93 reg = GPIO_BASE_ADDRESS | 2;
94 pci_write_config32(lpc_dev, GBASE, reg);
95}
96
Aaron Durbin794bddf2013-09-27 11:38:36 -050097static inline void mark_ts(struct romstage_params *rp, uint64_t ts)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050098{
Aaron Durbin794bddf2013-09-27 11:38:36 -050099 struct romstage_timestamps *rt = &rp->ts;
100
101 rt->times[rt->count] = ts;
102 rt->count++;
103}
104
105/* Entry from cache-as-ram.inc. */
106void * asmlinkage romstage_main(unsigned long bist,
107 uint32_t tsc_low, uint32_t tsc_hi)
108{
109 struct romstage_params rp = {
110 .bist = bist,
111 .mrc_params = NULL,
112 };
113
114 /* Save initial timestamp from bootblock. */
115 mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
116 /* Save romstage begin */
117 mark_ts(&rp, timestamp_get());
118
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500119 program_base_addresses();
120
Aaron Durbinfd039f72013-10-04 11:11:52 -0500121 tco_disable();
122
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500123 byt_config_com1_and_enable();
124
125 console_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 Durbin6e328932013-11-06 12:04:50 -0600144/* Return 0, 3, or 5 to indicate the previous sleep state. */
145static int chipset_prev_sleep_state(void)
146{
147 uint16_t pm1_sts;
148 uint32_t pm1_cnt;
149 uint32_t gen_pmcon1;
150 /* Default to S0. */
151 int prev_sleep_state = 0;
152
153 pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
154 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
155
156 if (pm1_sts & WAK_STS) {
157 switch ((pm1_cnt & SLP_TYP) >> SLP_TYP_SHIFT) {
158 #if CONFIG_HAVE_ACPI_RESUME
159 case SLP_TYP_S3:
160 prev_sleep_state = 3;
161 break;
162 #endif
163 case SLP_TYP_S5:
164 prev_sleep_state = 5;
165 }
166 /* Clear SLP_TYP. */
167 outl(pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
168 }
169
170 gen_pmcon1 = read32(PMC_BASE_ADDRESS + GEN_PMCON1);
171 if (gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
172 /* Clear power failure bits. */
173 write32(PMC_BASE_ADDRESS + GEN_PMCON1, gen_pmcon1);
174 prev_sleep_state = 5;
175 }
176
177 printk(BIOS_DEBUG, "pm1_sts = %04x pm1_cnt = %08x gen_pmcon1 = %08x\n",
178 pm1_sts, pm1_cnt, gen_pmcon1);
179
180 return prev_sleep_state;
181}
182
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600183#if CONFIG_CHROMEOS
184static inline void chromeos_init(int prev_sleep_state)
185{
186 /* Normalize the sleep state to what init_chromeos() wants for S3: 2. */
187 init_chromeos(prev_sleep_state == 3 ? 2 : 0);
188}
189#else
190static inline void chromeos_init(int prev_sleep_state) {}
191#endif
192
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500193/* Entry from the mainboard. */
194void romstage_common(struct romstage_params *params)
195{
196 struct romstage_handoff *handoff;
Aaron Durbin6e328932013-11-06 12:04:50 -0600197 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500198
Aaron Durbin794bddf2013-09-27 11:38:36 -0500199 mark_ts(params, timestamp_get());
200
Aaron Durbina8e9b632013-10-30 15:46:07 -0500201#if CONFIG_ELOG_BOOT_COUNT
202 boot_count_increment();
203#endif
204
Aaron Durbin6e328932013-11-06 12:04:50 -0600205 prev_sleep_state = chipset_prev_sleep_state();
206
207 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
208
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500209 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600210 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500211
Aaron Durbin794bddf2013-09-27 11:38:36 -0500212 mark_ts(params, timestamp_get());
213
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500214 handoff = romstage_handoff_find_or_add();
215 if (handoff != NULL)
Aaron Durbin6e328932013-11-06 12:04:50 -0600216 handoff->s3_resume = (prev_sleep_state == 3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500217 else
218 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
219
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600220 chromeos_init(prev_sleep_state);
221
Aaron Durbin794bddf2013-09-27 11:38:36 -0500222 /* Save timestamp information. */
223 timestamp_init(ts64_to_tsc(params->ts.times[0]));
224 timestamp_add(TS_START_ROMSTAGE, ts64_to_tsc(params->ts.times[1]));
225 timestamp_add(TS_BEFORE_INITRAM, ts64_to_tsc(params->ts.times[2]));
226 timestamp_add(TS_AFTER_INITRAM, ts64_to_tsc(params->ts.times[3]));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500227}
228
229static void open_up_spi(void)
230{
231 const uintptr_t sbase = SPI_BASE_ADDRESS;
232
233 /* Disable generating SMI when setting WPD bit. */
234 write32(sbase + 0xf8, read32(sbase + 0xf8) & ~(1 << 7));
235 /* Disable the SMM-only BIOS write and set WPD bit. */
236 write32(sbase + 0xfc, 1 | (read32(sbase + 0xfc) & ~(1 << 5)));
237}
238
239void asmlinkage romstage_after_car(void)
240{
241 /* Allow BIOS to program SPI part. */
242 open_up_spi();
243
Aaron Durbin794bddf2013-09-27 11:38:36 -0500244 timestamp_add_now(TS_END_ROMSTAGE);
245
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500246 /* Load the ramstage. */
247 copy_and_run();
248 while (1);
249}
250
251static inline uint32_t *stack_push(u32 *stack, u32 value)
252{
253 stack = &stack[-1];
254 *stack = value;
255 return stack;
256}
257
258/* Romstage needs quite a bit of stack for decompressing images since the lzma
259 * lib keeps its state on the stack during romstage. */
260static unsigned long choose_top_of_stack(void)
261{
262 unsigned long stack_top;
263 const unsigned long romstage_ram_stack_size = 0x5000;
264
265 /* cbmem_add() does a find() before add(). */
266 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
267 romstage_ram_stack_size);
268 stack_top += romstage_ram_stack_size;
269 return stack_top;
270}
271
272/* setup_stack_and_mttrs() determines the stack to use after
273 * cache-as-ram is torn down as well as the MTRR settings to use. */
274static void *setup_stack_and_mttrs(void)
275{
276 unsigned long top_of_stack;
277 int num_mtrrs;
278 uint32_t *slot;
279 uint32_t mtrr_mask_upper;
280 uint32_t top_of_ram;
281
282 /* Top of stack needs to be aligned to a 4-byte boundary. */
283 top_of_stack = choose_top_of_stack() & ~3;
284 slot = (void *)top_of_stack;
285 num_mtrrs = 0;
286
287 /* The upper bits of the MTRR mask need to set according to the number
288 * of physical address bits. */
289 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
290
291 /* The order for each MTRR is value then base with upper 32-bits of
292 * each value coming before the lower 32-bits. The reasoning for
293 * this ordering is to create a stack layout like the following:
294 * +0: Number of MTRRs
295 * +4: MTRR base 0 31:0
296 * +8: MTRR base 0 63:32
297 * +12: MTRR mask 0 31:0
298 * +16: MTRR mask 0 63:32
299 * +20: MTRR base 1 31:0
300 * +24: MTRR base 1 63:32
301 * +28: MTRR mask 1 31:0
302 * +32: MTRR mask 1 63:32
303 */
304
305 /* Cache the ROM as WP just below 4GiB. */
306 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
307 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
308 slot = stack_push(slot, 0); /* upper base */
309 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
310 num_mtrrs++;
311
312 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
313 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
314 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
315 slot = stack_push(slot, 0); /* upper base */
316 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
317 num_mtrrs++;
318
319 top_of_ram = (uint32_t)cbmem_top();
320 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
321 * start of the TSEG region. It is required to be 8MiB aligned. Set
322 * this area as cacheable so it can be used later for ramstage before
323 * setting up the entire RAM as cacheable. */
324 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
325 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
326 slot = stack_push(slot, 0); /* upper base */
327 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
328 num_mtrrs++;
329
330 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
331 * region resides. However, it is not restricted to SMM mode until
332 * SMM has been relocated. By setting the region to cacheable it
333 * provides faster access when relocating the SMM handler as well
334 * as using the TSEG region for other purposes. */
335 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
336 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
337 slot = stack_push(slot, 0); /* upper base */
338 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
339 num_mtrrs++;
340
341 /* Save the number of MTRRs to setup. Return the stack location
342 * pointing to the number of MTRRs. */
343 slot = stack_push(slot, num_mtrrs);
344
345 return slot;
346}
Aaron Durbindc249f62013-10-10 21:03:50 -0500347
348struct ramstage_cache *ramstage_cache_location(long *size)
349{
350 char *smm_base;
351 /* 1MiB cache size */
Aaron Durbin7837be62013-10-21 22:32:00 -0500352 const long cache_size = CONFIG_SMM_RESERVED_SIZE;
Aaron Durbindc249f62013-10-10 21:03:50 -0500353
354 /* Ramstage cache lives in TSEG region which is the definition of
355 * cbmem_top(). */
356 smm_base = cbmem_top();
357 *size = cache_size;
Aaron Durbin7837be62013-10-21 22:32:00 -0500358 return (void *)&smm_base[smm_region_size() - cache_size];
Aaron Durbindc249f62013-10-10 21:03:50 -0500359}
360
361void ramstage_cache_invalid(struct ramstage_cache *cache)
362{
363#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
364 /* Perform cold reset on invalid ramstage cache. */
365 cold_reset();
366#endif
367}
368