blob: 5af9bf45667f2c5ef218f0bc3e3119d6b5227203 [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 Durbin9a7d7bc2013-09-07 00:41:48 -050035#include <baytrail/gpio.h>
36#include <baytrail/iomap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050037#include <baytrail/lpc.h>
38#include <baytrail/pci_devs.h>
Aaron Durbindc249f62013-10-10 21:03:50 -050039#include <baytrail/reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050040#include <baytrail/romstage.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050041#include <baytrail/smm.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050042
Aaron Durbin794bddf2013-09-27 11:38:36 -050043static inline uint64_t timestamp_get(void)
44{
45 return rdtscll();
46}
47
48static inline tsc_t ts64_to_tsc(uint64_t ts)
49{
50 tsc_t tsc = {
51 .lo = ts,
52 .hi = ts >> 32,
53 };
54 return tsc;
55}
56
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050057/* The cache-as-ram assembly file calls romstage_main() after setting up
58 * cache-as-ram. romstage_main() will then call the mainboards's
59 * mainboard_romstage_entry() function. That function then calls
60 * romstage_common() below. The reason for the back and forth is to provide
61 * common entry point from cache-as-ram while still allowing for code sharing.
62 * Because we can't use global variables the stack is used for allocations --
63 * thus the need to call back and forth. */
64
65static void *setup_stack_and_mttrs(void);
66
67static void program_base_addresses(void)
68{
69 uint32_t reg;
70 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
71
72 /* Memory Mapped IO registers. */
73 reg = PMC_BASE_ADDRESS | 2;
74 pci_write_config32(lpc_dev, PBASE, reg);
75 reg = IO_BASE_ADDRESS | 2;
76 pci_write_config32(lpc_dev, IOBASE, reg);
77 reg = ILB_BASE_ADDRESS | 2;
78 pci_write_config32(lpc_dev, IBASE, reg);
79 reg = SPI_BASE_ADDRESS | 2;
80 pci_write_config32(lpc_dev, SBASE, reg);
81 reg = MPHY_BASE_ADDRESS | 2;
82 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050083 reg = PUNIT_BASE_ADDRESS | 2;
84 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050085 reg = RCBA_BASE_ADDRESS | 1;
86 pci_write_config32(lpc_dev, RCBA, reg);
87
88 /* IO Port Registers. */
89 reg = ACPI_BASE_ADDRESS | 2;
90 pci_write_config32(lpc_dev, ABASE, reg);
91 reg = GPIO_BASE_ADDRESS | 2;
92 pci_write_config32(lpc_dev, GBASE, reg);
93}
94
Aaron Durbin794bddf2013-09-27 11:38:36 -050095static inline void mark_ts(struct romstage_params *rp, uint64_t ts)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050096{
Aaron Durbin794bddf2013-09-27 11:38:36 -050097 struct romstage_timestamps *rt = &rp->ts;
98
99 rt->times[rt->count] = ts;
100 rt->count++;
101}
102
103/* 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. */
113 mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
114 /* Save romstage begin */
115 mark_ts(&rp, timestamp_get());
116
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 Durbinbb3ee832013-10-07 17:12:20 -0500125 set_max_freq();
126
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500127 punit_init();
128
Aaron Durbinecf90862013-09-24 12:36:14 -0500129 gfx_init();
130
Aaron Durbin3e0eea12013-10-28 11:20:35 -0500131#if CONFIG_EC_GOOGLE_CHROMEEC
132 /* Ensure the EC is in the right mode for recovery */
133 google_chromeec_early_init();
134#endif
135
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500136 /* Call into mainboard. */
137 mainboard_romstage_entry(&rp);
138
139 return setup_stack_and_mttrs();
140}
141
142/* Entry from the mainboard. */
143void romstage_common(struct romstage_params *params)
144{
145 struct romstage_handoff *handoff;
146
Aaron Durbin794bddf2013-09-27 11:38:36 -0500147 mark_ts(params, timestamp_get());
148
Aaron Durbina8e9b632013-10-30 15:46:07 -0500149#if CONFIG_ELOG_BOOT_COUNT
150 boot_count_increment();
151#endif
152
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500153 /* Initialize RAM */
154 raminit(params->mrc_params, 5);
155
Aaron Durbin794bddf2013-09-27 11:38:36 -0500156 mark_ts(params, timestamp_get());
157
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500158 handoff = romstage_handoff_find_or_add();
159 if (handoff != NULL)
160 handoff->s3_resume = 0;
161 else
162 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
163
Aaron Durbin794bddf2013-09-27 11:38:36 -0500164 /* Save timestamp information. */
165 timestamp_init(ts64_to_tsc(params->ts.times[0]));
166 timestamp_add(TS_START_ROMSTAGE, ts64_to_tsc(params->ts.times[1]));
167 timestamp_add(TS_BEFORE_INITRAM, ts64_to_tsc(params->ts.times[2]));
168 timestamp_add(TS_AFTER_INITRAM, ts64_to_tsc(params->ts.times[3]));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500169}
170
171static void open_up_spi(void)
172{
173 const uintptr_t sbase = SPI_BASE_ADDRESS;
174
175 /* Disable generating SMI when setting WPD bit. */
176 write32(sbase + 0xf8, read32(sbase + 0xf8) & ~(1 << 7));
177 /* Disable the SMM-only BIOS write and set WPD bit. */
178 write32(sbase + 0xfc, 1 | (read32(sbase + 0xfc) & ~(1 << 5)));
179}
180
181void asmlinkage romstage_after_car(void)
182{
183 /* Allow BIOS to program SPI part. */
184 open_up_spi();
185
Aaron Durbin794bddf2013-09-27 11:38:36 -0500186 timestamp_add_now(TS_END_ROMSTAGE);
187
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500188 /* Load the ramstage. */
189 copy_and_run();
190 while (1);
191}
192
193static inline uint32_t *stack_push(u32 *stack, u32 value)
194{
195 stack = &stack[-1];
196 *stack = value;
197 return stack;
198}
199
200/* Romstage needs quite a bit of stack for decompressing images since the lzma
201 * lib keeps its state on the stack during romstage. */
202static unsigned long choose_top_of_stack(void)
203{
204 unsigned long stack_top;
205 const unsigned long romstage_ram_stack_size = 0x5000;
206
207 /* cbmem_add() does a find() before add(). */
208 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
209 romstage_ram_stack_size);
210 stack_top += romstage_ram_stack_size;
211 return stack_top;
212}
213
214/* setup_stack_and_mttrs() determines the stack to use after
215 * cache-as-ram is torn down as well as the MTRR settings to use. */
216static void *setup_stack_and_mttrs(void)
217{
218 unsigned long top_of_stack;
219 int num_mtrrs;
220 uint32_t *slot;
221 uint32_t mtrr_mask_upper;
222 uint32_t top_of_ram;
223
224 /* Top of stack needs to be aligned to a 4-byte boundary. */
225 top_of_stack = choose_top_of_stack() & ~3;
226 slot = (void *)top_of_stack;
227 num_mtrrs = 0;
228
229 /* The upper bits of the MTRR mask need to set according to the number
230 * of physical address bits. */
231 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
232
233 /* The order for each MTRR is value then base with upper 32-bits of
234 * each value coming before the lower 32-bits. The reasoning for
235 * this ordering is to create a stack layout like the following:
236 * +0: Number of MTRRs
237 * +4: MTRR base 0 31:0
238 * +8: MTRR base 0 63:32
239 * +12: MTRR mask 0 31:0
240 * +16: MTRR mask 0 63:32
241 * +20: MTRR base 1 31:0
242 * +24: MTRR base 1 63:32
243 * +28: MTRR mask 1 31:0
244 * +32: MTRR mask 1 63:32
245 */
246
247 /* Cache the ROM as WP just below 4GiB. */
248 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
249 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
250 slot = stack_push(slot, 0); /* upper base */
251 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
252 num_mtrrs++;
253
254 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
255 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
256 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
257 slot = stack_push(slot, 0); /* upper base */
258 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
259 num_mtrrs++;
260
261 top_of_ram = (uint32_t)cbmem_top();
262 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
263 * start of the TSEG region. It is required to be 8MiB aligned. Set
264 * this area as cacheable so it can be used later for ramstage before
265 * setting up the entire RAM as cacheable. */
266 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
267 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
268 slot = stack_push(slot, 0); /* upper base */
269 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
270 num_mtrrs++;
271
272 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
273 * region resides. However, it is not restricted to SMM mode until
274 * SMM has been relocated. By setting the region to cacheable it
275 * provides faster access when relocating the SMM handler as well
276 * as using the TSEG region for other purposes. */
277 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
278 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
279 slot = stack_push(slot, 0); /* upper base */
280 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
281 num_mtrrs++;
282
283 /* Save the number of MTRRs to setup. Return the stack location
284 * pointing to the number of MTRRs. */
285 slot = stack_push(slot, num_mtrrs);
286
287 return slot;
288}
Aaron Durbindc249f62013-10-10 21:03:50 -0500289
290struct ramstage_cache *ramstage_cache_location(long *size)
291{
292 char *smm_base;
293 /* 1MiB cache size */
Aaron Durbin7837be62013-10-21 22:32:00 -0500294 const long cache_size = CONFIG_SMM_RESERVED_SIZE;
Aaron Durbindc249f62013-10-10 21:03:50 -0500295
296 /* Ramstage cache lives in TSEG region which is the definition of
297 * cbmem_top(). */
298 smm_base = cbmem_top();
299 *size = cache_size;
Aaron Durbin7837be62013-10-21 22:32:00 -0500300 return (void *)&smm_base[smm_region_size() - cache_size];
Aaron Durbindc249f62013-10-10 21:03:50 -0500301}
302
303void ramstage_cache_invalid(struct ramstage_cache *cache)
304{
305#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
306 /* Perform cold reset on invalid ramstage cache. */
307 cold_reset();
308#endif
309}
310