blob: 8436c65d174868c81438d265c4f97a72029e8951 [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 Durbin6f9947a2013-11-18 11:16:20 -060044#include <baytrail/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050045
Aaron Durbin794bddf2013-09-27 11:38:36 -050046static inline uint64_t timestamp_get(void)
47{
48 return rdtscll();
49}
50
51static inline tsc_t ts64_to_tsc(uint64_t ts)
52{
53 tsc_t tsc = {
54 .lo = ts,
55 .hi = ts >> 32,
56 };
57 return tsc;
58}
59
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050060/* The cache-as-ram assembly file calls romstage_main() after setting up
61 * cache-as-ram. romstage_main() will then call the mainboards's
62 * mainboard_romstage_entry() function. That function then calls
63 * romstage_common() below. The reason for the back and forth is to provide
64 * common entry point from cache-as-ram while still allowing for code sharing.
65 * Because we can't use global variables the stack is used for allocations --
66 * thus the need to call back and forth. */
67
68static void *setup_stack_and_mttrs(void);
69
70static void program_base_addresses(void)
71{
72 uint32_t reg;
73 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
74
75 /* Memory Mapped IO registers. */
76 reg = PMC_BASE_ADDRESS | 2;
77 pci_write_config32(lpc_dev, PBASE, reg);
78 reg = IO_BASE_ADDRESS | 2;
79 pci_write_config32(lpc_dev, IOBASE, reg);
80 reg = ILB_BASE_ADDRESS | 2;
81 pci_write_config32(lpc_dev, IBASE, reg);
82 reg = SPI_BASE_ADDRESS | 2;
83 pci_write_config32(lpc_dev, SBASE, reg);
84 reg = MPHY_BASE_ADDRESS | 2;
85 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050086 reg = PUNIT_BASE_ADDRESS | 2;
87 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050088 reg = RCBA_BASE_ADDRESS | 1;
89 pci_write_config32(lpc_dev, RCBA, reg);
90
91 /* IO Port Registers. */
92 reg = ACPI_BASE_ADDRESS | 2;
93 pci_write_config32(lpc_dev, ABASE, reg);
94 reg = GPIO_BASE_ADDRESS | 2;
95 pci_write_config32(lpc_dev, GBASE, reg);
96}
97
Aaron Durbin6f9947a2013-11-18 11:16:20 -060098static void spi_init(void)
99{
100 const unsigned long bcr = SPI_BASE_ADDRESS + BCR;
101 /* Enable caching and prefetching in the SPI controller. */
102 write32(bcr, (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH);
103}
104
Aaron Durbin794bddf2013-09-27 11:38:36 -0500105static inline void mark_ts(struct romstage_params *rp, uint64_t ts)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500106{
Aaron Durbin794bddf2013-09-27 11:38:36 -0500107 struct romstage_timestamps *rt = &rp->ts;
108
109 rt->times[rt->count] = ts;
110 rt->count++;
111}
112
113/* Entry from cache-as-ram.inc. */
114void * asmlinkage romstage_main(unsigned long bist,
115 uint32_t tsc_low, uint32_t tsc_hi)
116{
117 struct romstage_params rp = {
118 .bist = bist,
119 .mrc_params = NULL,
120 };
121
122 /* Save initial timestamp from bootblock. */
123 mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
124 /* Save romstage begin */
125 mark_ts(&rp, timestamp_get());
126
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500127 program_base_addresses();
128
Aaron Durbinfd039f72013-10-04 11:11:52 -0500129 tco_disable();
130
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500131 byt_config_com1_and_enable();
132
133 console_init();
134
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600135 spi_init();
136
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500137 set_max_freq();
138
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500139 punit_init();
140
Aaron Durbinecf90862013-09-24 12:36:14 -0500141 gfx_init();
142
Aaron Durbin3e0eea12013-10-28 11:20:35 -0500143#if CONFIG_EC_GOOGLE_CHROMEEC
144 /* Ensure the EC is in the right mode for recovery */
145 google_chromeec_early_init();
146#endif
147
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500148 /* Call into mainboard. */
149 mainboard_romstage_entry(&rp);
150
151 return setup_stack_and_mttrs();
152}
153
Aaron Durbin6e328932013-11-06 12:04:50 -0600154/* Return 0, 3, or 5 to indicate the previous sleep state. */
155static int chipset_prev_sleep_state(void)
156{
157 uint16_t pm1_sts;
158 uint32_t pm1_cnt;
159 uint32_t gen_pmcon1;
160 /* Default to S0. */
161 int prev_sleep_state = 0;
162
163 pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
164 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
165
166 if (pm1_sts & WAK_STS) {
167 switch ((pm1_cnt & SLP_TYP) >> SLP_TYP_SHIFT) {
168 #if CONFIG_HAVE_ACPI_RESUME
169 case SLP_TYP_S3:
170 prev_sleep_state = 3;
171 break;
172 #endif
173 case SLP_TYP_S5:
174 prev_sleep_state = 5;
175 }
176 /* Clear SLP_TYP. */
177 outl(pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
178 }
179
180 gen_pmcon1 = read32(PMC_BASE_ADDRESS + GEN_PMCON1);
181 if (gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
182 /* Clear power failure bits. */
183 write32(PMC_BASE_ADDRESS + GEN_PMCON1, gen_pmcon1);
184 prev_sleep_state = 5;
185 }
186
187 printk(BIOS_DEBUG, "pm1_sts = %04x pm1_cnt = %08x gen_pmcon1 = %08x\n",
188 pm1_sts, pm1_cnt, gen_pmcon1);
189
190 return prev_sleep_state;
191}
192
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600193#if CONFIG_CHROMEOS
194static inline void chromeos_init(int prev_sleep_state)
195{
196 /* Normalize the sleep state to what init_chromeos() wants for S3: 2. */
197 init_chromeos(prev_sleep_state == 3 ? 2 : 0);
198}
199#else
200static inline void chromeos_init(int prev_sleep_state) {}
201#endif
202
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500203/* Entry from the mainboard. */
204void romstage_common(struct romstage_params *params)
205{
206 struct romstage_handoff *handoff;
Aaron Durbin6e328932013-11-06 12:04:50 -0600207 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500208
Aaron Durbin794bddf2013-09-27 11:38:36 -0500209 mark_ts(params, timestamp_get());
210
Aaron Durbina8e9b632013-10-30 15:46:07 -0500211#if CONFIG_ELOG_BOOT_COUNT
212 boot_count_increment();
213#endif
214
Aaron Durbin6e328932013-11-06 12:04:50 -0600215 prev_sleep_state = chipset_prev_sleep_state();
216
217 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
218
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500219 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600220 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500221
Aaron Durbin794bddf2013-09-27 11:38:36 -0500222 mark_ts(params, timestamp_get());
223
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500224 handoff = romstage_handoff_find_or_add();
225 if (handoff != NULL)
Aaron Durbin6e328932013-11-06 12:04:50 -0600226 handoff->s3_resume = (prev_sleep_state == 3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500227 else
228 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
229
Aaron Durbinebf7ec52013-11-14 13:47:08 -0600230 chromeos_init(prev_sleep_state);
231
Aaron Durbin794bddf2013-09-27 11:38:36 -0500232 /* Save timestamp information. */
233 timestamp_init(ts64_to_tsc(params->ts.times[0]));
234 timestamp_add(TS_START_ROMSTAGE, ts64_to_tsc(params->ts.times[1]));
235 timestamp_add(TS_BEFORE_INITRAM, ts64_to_tsc(params->ts.times[2]));
236 timestamp_add(TS_AFTER_INITRAM, ts64_to_tsc(params->ts.times[3]));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500237}
238
239static void open_up_spi(void)
240{
241 const uintptr_t sbase = SPI_BASE_ADDRESS;
242
243 /* Disable generating SMI when setting WPD bit. */
244 write32(sbase + 0xf8, read32(sbase + 0xf8) & ~(1 << 7));
245 /* Disable the SMM-only BIOS write and set WPD bit. */
246 write32(sbase + 0xfc, 1 | (read32(sbase + 0xfc) & ~(1 << 5)));
247}
248
249void asmlinkage romstage_after_car(void)
250{
251 /* Allow BIOS to program SPI part. */
252 open_up_spi();
253
Aaron Durbin794bddf2013-09-27 11:38:36 -0500254 timestamp_add_now(TS_END_ROMSTAGE);
255
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500256 /* Load the ramstage. */
257 copy_and_run();
258 while (1);
259}
260
261static inline uint32_t *stack_push(u32 *stack, u32 value)
262{
263 stack = &stack[-1];
264 *stack = value;
265 return stack;
266}
267
268/* Romstage needs quite a bit of stack for decompressing images since the lzma
269 * lib keeps its state on the stack during romstage. */
270static unsigned long choose_top_of_stack(void)
271{
272 unsigned long stack_top;
273 const unsigned long romstage_ram_stack_size = 0x5000;
274
275 /* cbmem_add() does a find() before add(). */
276 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
277 romstage_ram_stack_size);
278 stack_top += romstage_ram_stack_size;
279 return stack_top;
280}
281
282/* setup_stack_and_mttrs() determines the stack to use after
283 * cache-as-ram is torn down as well as the MTRR settings to use. */
284static void *setup_stack_and_mttrs(void)
285{
286 unsigned long top_of_stack;
287 int num_mtrrs;
288 uint32_t *slot;
289 uint32_t mtrr_mask_upper;
290 uint32_t top_of_ram;
291
292 /* Top of stack needs to be aligned to a 4-byte boundary. */
293 top_of_stack = choose_top_of_stack() & ~3;
294 slot = (void *)top_of_stack;
295 num_mtrrs = 0;
296
297 /* The upper bits of the MTRR mask need to set according to the number
298 * of physical address bits. */
299 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
300
301 /* The order for each MTRR is value then base with upper 32-bits of
302 * each value coming before the lower 32-bits. The reasoning for
303 * this ordering is to create a stack layout like the following:
304 * +0: Number of MTRRs
305 * +4: MTRR base 0 31:0
306 * +8: MTRR base 0 63:32
307 * +12: MTRR mask 0 31:0
308 * +16: MTRR mask 0 63:32
309 * +20: MTRR base 1 31:0
310 * +24: MTRR base 1 63:32
311 * +28: MTRR mask 1 31:0
312 * +32: MTRR mask 1 63:32
313 */
314
315 /* Cache the ROM as WP just below 4GiB. */
316 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
317 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
318 slot = stack_push(slot, 0); /* upper base */
319 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
320 num_mtrrs++;
321
322 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
323 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
324 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
325 slot = stack_push(slot, 0); /* upper base */
326 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
327 num_mtrrs++;
328
329 top_of_ram = (uint32_t)cbmem_top();
330 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
331 * start of the TSEG region. It is required to be 8MiB aligned. Set
332 * this area as cacheable so it can be used later for ramstage before
333 * setting up the entire RAM as cacheable. */
334 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
335 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
336 slot = stack_push(slot, 0); /* upper base */
337 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
338 num_mtrrs++;
339
340 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
341 * region resides. However, it is not restricted to SMM mode until
342 * SMM has been relocated. By setting the region to cacheable it
343 * provides faster access when relocating the SMM handler as well
344 * as using the TSEG region for other purposes. */
345 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
346 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
347 slot = stack_push(slot, 0); /* upper base */
348 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
349 num_mtrrs++;
350
351 /* Save the number of MTRRs to setup. Return the stack location
352 * pointing to the number of MTRRs. */
353 slot = stack_push(slot, num_mtrrs);
354
355 return slot;
356}
Aaron Durbindc249f62013-10-10 21:03:50 -0500357
358struct ramstage_cache *ramstage_cache_location(long *size)
359{
360 char *smm_base;
361 /* 1MiB cache size */
Aaron Durbin7837be62013-10-21 22:32:00 -0500362 const long cache_size = CONFIG_SMM_RESERVED_SIZE;
Aaron Durbindc249f62013-10-10 21:03:50 -0500363
364 /* Ramstage cache lives in TSEG region which is the definition of
365 * cbmem_top(). */
366 smm_base = cbmem_top();
367 *size = cache_size;
Aaron Durbin7837be62013-10-21 22:32:00 -0500368 return (void *)&smm_base[smm_region_size() - cache_size];
Aaron Durbindc249f62013-10-10 21:03:50 -0500369}
370
371void ramstage_cache_invalid(struct ramstage_cache *cache)
372{
373#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
374 /* Perform cold reset on invalid ramstage cache. */
375 cold_reset();
376#endif
377}
378