blob: 6e965bc3cd04c1a2f05bbaf412bc463035e0afb7 [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>
28#include <romstage_handoff.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050029#include <timestamp.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050030#include <baytrail/gpio.h>
31#include <baytrail/iomap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050032#include <baytrail/lpc.h>
33#include <baytrail/pci_devs.h>
34#include <baytrail/romstage.h>
35
Aaron Durbin794bddf2013-09-27 11:38:36 -050036static inline uint64_t timestamp_get(void)
37{
38 return rdtscll();
39}
40
41static inline tsc_t ts64_to_tsc(uint64_t ts)
42{
43 tsc_t tsc = {
44 .lo = ts,
45 .hi = ts >> 32,
46 };
47 return tsc;
48}
49
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050050/* The cache-as-ram assembly file calls romstage_main() after setting up
51 * cache-as-ram. romstage_main() will then call the mainboards's
52 * mainboard_romstage_entry() function. That function then calls
53 * romstage_common() below. The reason for the back and forth is to provide
54 * common entry point from cache-as-ram while still allowing for code sharing.
55 * Because we can't use global variables the stack is used for allocations --
56 * thus the need to call back and forth. */
57
58static void *setup_stack_and_mttrs(void);
59
60static void program_base_addresses(void)
61{
62 uint32_t reg;
63 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
64
65 /* Memory Mapped IO registers. */
66 reg = PMC_BASE_ADDRESS | 2;
67 pci_write_config32(lpc_dev, PBASE, reg);
68 reg = IO_BASE_ADDRESS | 2;
69 pci_write_config32(lpc_dev, IOBASE, reg);
70 reg = ILB_BASE_ADDRESS | 2;
71 pci_write_config32(lpc_dev, IBASE, reg);
72 reg = SPI_BASE_ADDRESS | 2;
73 pci_write_config32(lpc_dev, SBASE, reg);
74 reg = MPHY_BASE_ADDRESS | 2;
75 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050076 reg = PUNIT_BASE_ADDRESS | 2;
77 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050078 reg = RCBA_BASE_ADDRESS | 1;
79 pci_write_config32(lpc_dev, RCBA, reg);
80
81 /* IO Port Registers. */
82 reg = ACPI_BASE_ADDRESS | 2;
83 pci_write_config32(lpc_dev, ABASE, reg);
84 reg = GPIO_BASE_ADDRESS | 2;
85 pci_write_config32(lpc_dev, GBASE, reg);
86}
87
Aaron Durbin794bddf2013-09-27 11:38:36 -050088static inline void mark_ts(struct romstage_params *rp, uint64_t ts)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050089{
Aaron Durbin794bddf2013-09-27 11:38:36 -050090 struct romstage_timestamps *rt = &rp->ts;
91
92 rt->times[rt->count] = ts;
93 rt->count++;
94}
95
96/* Entry from cache-as-ram.inc. */
97void * asmlinkage romstage_main(unsigned long bist,
98 uint32_t tsc_low, uint32_t tsc_hi)
99{
100 struct romstage_params rp = {
101 .bist = bist,
102 .mrc_params = NULL,
103 };
104
105 /* Save initial timestamp from bootblock. */
106 mark_ts(&rp, (((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
107 /* Save romstage begin */
108 mark_ts(&rp, timestamp_get());
109
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500110 /* Call into mainboard. */
Aaron Durbin794bddf2013-09-27 11:38:36 -0500111 mainboard_romstage_entry(&rp);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500112
113 return setup_stack_and_mttrs();
114}
115
116/* Entry from the mainboard. */
Aaron Durbin794bddf2013-09-27 11:38:36 -0500117void romstage_common(struct romstage_params *params)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500118{
119 struct romstage_handoff *handoff;
120
121 program_base_addresses();
122
Aaron Durbinfd039f72013-10-04 11:11:52 -0500123 tco_disable();
124
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500125 byt_config_com1_and_enable();
126
127 console_init();
128
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500129 punit_init();
130
Aaron Durbinecf90862013-09-24 12:36:14 -0500131 gfx_init();
132
Aaron Durbin794bddf2013-09-27 11:38:36 -0500133 mark_ts(params, timestamp_get());
134
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500135 /* Initialize RAM */
136 raminit(params->mrc_params, 5);
137
Aaron Durbin794bddf2013-09-27 11:38:36 -0500138 mark_ts(params, timestamp_get());
139
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500140 handoff = romstage_handoff_find_or_add();
141 if (handoff != NULL)
142 handoff->s3_resume = 0;
143 else
144 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
145
Aaron Durbin794bddf2013-09-27 11:38:36 -0500146 /* Save timestamp information. */
147 timestamp_init(ts64_to_tsc(params->ts.times[0]));
148 timestamp_add(TS_START_ROMSTAGE, ts64_to_tsc(params->ts.times[1]));
149 timestamp_add(TS_BEFORE_INITRAM, ts64_to_tsc(params->ts.times[2]));
150 timestamp_add(TS_AFTER_INITRAM, ts64_to_tsc(params->ts.times[3]));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500151}
152
153static void open_up_spi(void)
154{
155 const uintptr_t sbase = SPI_BASE_ADDRESS;
156
157 /* Disable generating SMI when setting WPD bit. */
158 write32(sbase + 0xf8, read32(sbase + 0xf8) & ~(1 << 7));
159 /* Disable the SMM-only BIOS write and set WPD bit. */
160 write32(sbase + 0xfc, 1 | (read32(sbase + 0xfc) & ~(1 << 5)));
161}
162
163void asmlinkage romstage_after_car(void)
164{
165 /* Allow BIOS to program SPI part. */
166 open_up_spi();
167
Aaron Durbin794bddf2013-09-27 11:38:36 -0500168 timestamp_add_now(TS_END_ROMSTAGE);
169
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500170 /* Load the ramstage. */
171 copy_and_run();
172 while (1);
173}
174
175static inline uint32_t *stack_push(u32 *stack, u32 value)
176{
177 stack = &stack[-1];
178 *stack = value;
179 return stack;
180}
181
182/* Romstage needs quite a bit of stack for decompressing images since the lzma
183 * lib keeps its state on the stack during romstage. */
184static unsigned long choose_top_of_stack(void)
185{
186 unsigned long stack_top;
187 const unsigned long romstage_ram_stack_size = 0x5000;
188
189 /* cbmem_add() does a find() before add(). */
190 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
191 romstage_ram_stack_size);
192 stack_top += romstage_ram_stack_size;
193 return stack_top;
194}
195
196/* setup_stack_and_mttrs() determines the stack to use after
197 * cache-as-ram is torn down as well as the MTRR settings to use. */
198static void *setup_stack_and_mttrs(void)
199{
200 unsigned long top_of_stack;
201 int num_mtrrs;
202 uint32_t *slot;
203 uint32_t mtrr_mask_upper;
204 uint32_t top_of_ram;
205
206 /* Top of stack needs to be aligned to a 4-byte boundary. */
207 top_of_stack = choose_top_of_stack() & ~3;
208 slot = (void *)top_of_stack;
209 num_mtrrs = 0;
210
211 /* The upper bits of the MTRR mask need to set according to the number
212 * of physical address bits. */
213 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
214
215 /* The order for each MTRR is value then base with upper 32-bits of
216 * each value coming before the lower 32-bits. The reasoning for
217 * this ordering is to create a stack layout like the following:
218 * +0: Number of MTRRs
219 * +4: MTRR base 0 31:0
220 * +8: MTRR base 0 63:32
221 * +12: MTRR mask 0 31:0
222 * +16: MTRR mask 0 63:32
223 * +20: MTRR base 1 31:0
224 * +24: MTRR base 1 63:32
225 * +28: MTRR mask 1 31:0
226 * +32: MTRR mask 1 63:32
227 */
228
229 /* Cache the ROM as WP just below 4GiB. */
230 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
231 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
232 slot = stack_push(slot, 0); /* upper base */
233 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
234 num_mtrrs++;
235
236 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
237 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
238 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
239 slot = stack_push(slot, 0); /* upper base */
240 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
241 num_mtrrs++;
242
243 top_of_ram = (uint32_t)cbmem_top();
244 /* Cache 8MiB below the top of ram. The top of ram under 4GiB is the
245 * start of the TSEG region. It is required to be 8MiB aligned. Set
246 * this area as cacheable so it can be used later for ramstage before
247 * setting up the entire RAM as cacheable. */
248 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
249 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
250 slot = stack_push(slot, 0); /* upper base */
251 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
252 num_mtrrs++;
253
254 /* Cache 8MiB at the top of ram. Top of ram is where the TSEG
255 * region resides. However, it is not restricted to SMM mode until
256 * SMM has been relocated. By setting the region to cacheable it
257 * provides faster access when relocating the SMM handler as well
258 * as using the TSEG region for other purposes. */
259 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
260 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
261 slot = stack_push(slot, 0); /* upper base */
262 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
263 num_mtrrs++;
264
265 /* Save the number of MTRRs to setup. Return the stack location
266 * pointing to the number of MTRRs. */
267 slot = stack_push(slot, num_mtrrs);
268
269 return slot;
270}