blob: 18c9353d10ccb5c46eba893f1478000342988939 [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.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050014 */
15
16#include <stddef.h>
17#include <arch/cpu.h>
18#include <arch/io.h>
Aaron Durbin00bf3db2014-01-09 10:33:23 -060019#include <arch/early_variables.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050020#include <console/console.h>
21#include <cbmem.h>
22#include <cpu/x86/mtrr.h>
Martin Rothe6ff1592017-06-24 21:34:29 -060023#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
Aaron Durbin3e0eea12013-10-28 11:20:35 -050024#include <ec/google/chromeec/ec.h>
25#endif
Aaron Durbina8e9b632013-10-30 15:46:07 -050026#include <elog.h>
Kyösti Mälkki65e8f642016-06-27 11:27:56 +030027#include <program_loading.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050028#include <romstage_handoff.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060029#include <stage_cache.h>
Aaron Durbinafe8aee2016-11-29 21:37:42 -060030#include <string.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050031#include <timestamp.h>
Aaron Durbinebf7ec52013-11-14 13:47:08 -060032#include <vendorcode/google/chromeos/chromeos.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070033#include <soc/gpio.h>
34#include <soc/iomap.h>
35#include <soc/lpc.h>
36#include <soc/pci_devs.h>
37#include <soc/pmc.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070038#include <soc/romstage.h>
39#include <soc/smm.h>
40#include <soc/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050041
42/* The cache-as-ram assembly file calls romstage_main() after setting up
43 * cache-as-ram. romstage_main() will then call the mainboards's
44 * mainboard_romstage_entry() function. That function then calls
45 * romstage_common() below. The reason for the back and forth is to provide
46 * common entry point from cache-as-ram while still allowing for code sharing.
47 * Because we can't use global variables the stack is used for allocations --
48 * thus the need to call back and forth. */
49
Elyes HAOUASbc8762e2018-04-25 15:50:27 +020050static void *setup_stack_and_mtrrs(void);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050051
52static void program_base_addresses(void)
53{
54 uint32_t reg;
55 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
56
57 /* Memory Mapped IO registers. */
58 reg = PMC_BASE_ADDRESS | 2;
59 pci_write_config32(lpc_dev, PBASE, reg);
60 reg = IO_BASE_ADDRESS | 2;
61 pci_write_config32(lpc_dev, IOBASE, reg);
62 reg = ILB_BASE_ADDRESS | 2;
63 pci_write_config32(lpc_dev, IBASE, reg);
64 reg = SPI_BASE_ADDRESS | 2;
65 pci_write_config32(lpc_dev, SBASE, reg);
66 reg = MPHY_BASE_ADDRESS | 2;
67 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050068 reg = PUNIT_BASE_ADDRESS | 2;
69 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050070 reg = RCBA_BASE_ADDRESS | 1;
71 pci_write_config32(lpc_dev, RCBA, reg);
72
73 /* IO Port Registers. */
74 reg = ACPI_BASE_ADDRESS | 2;
75 pci_write_config32(lpc_dev, ABASE, reg);
76 reg = GPIO_BASE_ADDRESS | 2;
77 pci_write_config32(lpc_dev, GBASE, reg);
78}
79
Aaron Durbin6f9947a2013-11-18 11:16:20 -060080static void spi_init(void)
81{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080082 u32 *scs = (u32 *)(SPI_BASE_ADDRESS + SCS);
83 u32 *bcr = (u32 *)(SPI_BASE_ADDRESS + BCR);
Aaron Durbin4177db52014-02-05 14:55:26 -060084 uint32_t reg;
85
86 /* Disable generating SMI when setting WPD bit. */
87 write32(scs, read32(scs) & ~SMIWPEN);
88 /*
89 * Enable caching and prefetching in the SPI controller. Disable
90 * the SMM-only BIOS write and set WPD bit.
91 */
92 reg = (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH | BCR_WPD;
93 reg &= ~EISS;
94 write32(bcr, reg);
Aaron Durbin6f9947a2013-11-18 11:16:20 -060095}
96
Aaron Durbin794bddf2013-09-27 11:38:36 -050097/* Entry from cache-as-ram.inc. */
Elyes HAOUAS88607a42018-10-05 10:36:45 +020098void *asmlinkage romstage_main(unsigned long bist, uint32_t tsc_low,
99 uint32_t tsc_hi)
Aaron Durbin794bddf2013-09-27 11:38:36 -0500100{
101 struct romstage_params rp = {
102 .bist = bist,
103 .mrc_params = NULL,
104 };
105
106 /* Save initial timestamp from bootblock. */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200107 timestamp_init((((uint64_t)tsc_hi) << 32) | (uint64_t)tsc_low);
108
Aaron Durbin794bddf2013-09-27 11:38:36 -0500109 /* Save romstage begin */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200110 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500111
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500112 program_base_addresses();
113
Aaron Durbinfd039f72013-10-04 11:11:52 -0500114 tco_disable();
115
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500116 byt_config_com1_and_enable();
117
118 console_init();
119
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600120 spi_init();
121
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500122 set_max_freq();
123
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500124 punit_init();
125
Aaron Durbinecf90862013-09-24 12:36:14 -0500126 gfx_init();
127
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500128 /* Call into mainboard. */
129 mainboard_romstage_entry(&rp);
130
Elyes HAOUASbc8762e2018-04-25 15:50:27 +0200131 return setup_stack_and_mtrrs();
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500132}
133
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600134static struct chipset_power_state power_state CAR_GLOBAL;
135
Aaron Durbin41607a42015-06-09 13:54:10 -0500136static void migrate_power_state(int is_recovery)
Aaron Durbin6e328932013-11-06 12:04:50 -0600137{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600138 struct chipset_power_state *ps_cbmem;
139 struct chipset_power_state *ps_car;
140
141 ps_car = car_get_var_ptr(&power_state);
142 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
143
144 if (ps_cbmem == NULL) {
145 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
146 return;
147 }
148 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
149}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200150ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600151
152static struct chipset_power_state *fill_power_state(void)
153{
154 struct chipset_power_state *ps = car_get_var_ptr(&power_state);
155
156 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
157 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
158 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
159 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
160 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
161 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800162 ps->prsts = read32((u32 *)(PMC_BASE_ADDRESS + PRSTS));
163 ps->gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1));
164 ps->gen_pmcon2 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON2));
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600165
166 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
167 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
168 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
169 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
170 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
171 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
172
173 return ps;
174}
175
176/* Return 0, 3, or 5 to indicate the previous sleep state. */
177static int chipset_prev_sleep_state(struct chipset_power_state *ps)
178{
Aaron Durbin6e328932013-11-06 12:04:50 -0600179 /* Default to S0. */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500180 int prev_sleep_state = ACPI_S0;
Aaron Durbin6e328932013-11-06 12:04:50 -0600181
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600182 if (ps->pm1_sts & WAK_STS) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500183 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
184 case ACPI_S3:
185 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME))
186 prev_sleep_state = ACPI_S3;
Aaron Durbin6e328932013-11-06 12:04:50 -0600187 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500188 case ACPI_S5:
189 prev_sleep_state = ACPI_S5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600190 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600191 }
192 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600193 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600194 }
195
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600196 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500197 prev_sleep_state = ACPI_S5;
Aaron Durbin6e328932013-11-06 12:04:50 -0600198 }
199
Aaron Durbin6e328932013-11-06 12:04:50 -0600200 return prev_sleep_state;
201}
202
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500203/* Entry from the mainboard. */
204void romstage_common(struct romstage_params *params)
205{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600206 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600207 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500208
Kyösti Mälkki41759272014-12-31 21:11:51 +0200209 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500210
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600211 ps = fill_power_state();
212 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600213
214 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
215
Martin Rothe6ff1592017-06-24 21:34:29 -0600216#if IS_ENABLED(CONFIG_ELOG_BOOT_COUNT)
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500217 if (prev_sleep_state != ACPI_S3)
Aaron Durbin4177db52014-02-05 14:55:26 -0600218 boot_count_increment();
219#endif
220
221
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500222 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600223 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500224
Kyösti Mälkki41759272014-12-31 21:11:51 +0200225 timestamp_add_now(TS_AFTER_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500226
Aaron Durbin77e13992016-11-29 17:43:04 -0600227 romstage_handoff_init(prev_sleep_state == ACPI_S3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500228}
229
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500230void asmlinkage romstage_after_car(void)
231{
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500232 /* Load the ramstage. */
Kyösti Mälkki65e8f642016-06-27 11:27:56 +0300233 run_ramstage();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500234 while (1);
235}
236
237static inline uint32_t *stack_push(u32 *stack, u32 value)
238{
239 stack = &stack[-1];
240 *stack = value;
241 return stack;
242}
243
Elyes HAOUASbc8762e2018-04-25 15:50:27 +0200244/* setup_stack_and_mtrrs() determines the stack to use after
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500245 * cache-as-ram is torn down as well as the MTRR settings to use. */
Elyes HAOUASbc8762e2018-04-25 15:50:27 +0200246static void *setup_stack_and_mtrrs(void)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500247{
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500248 int num_mtrrs;
249 uint32_t *slot;
250 uint32_t mtrr_mask_upper;
251 uint32_t top_of_ram;
252
253 /* Top of stack needs to be aligned to a 4-byte boundary. */
Kyösti Mälkkide011362016-11-17 22:39:29 +0200254 slot = (void *)romstage_ram_stack_top();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500255 num_mtrrs = 0;
256
257 /* The upper bits of the MTRR mask need to set according to the number
258 * of physical address bits. */
259 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
260
261 /* The order for each MTRR is value then base with upper 32-bits of
262 * each value coming before the lower 32-bits. The reasoning for
263 * this ordering is to create a stack layout like the following:
264 * +0: Number of MTRRs
265 * +4: MTRR base 0 31:0
266 * +8: MTRR base 0 63:32
267 * +12: MTRR mask 0 31:0
268 * +16: MTRR mask 0 63:32
269 * +20: MTRR base 1 31:0
270 * +24: MTRR base 1 63:32
271 * +28: MTRR mask 1 31:0
272 * +32: MTRR mask 1 63:32
273 */
274
275 /* Cache the ROM as WP just below 4GiB. */
276 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700277 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500278 slot = stack_push(slot, 0); /* upper base */
279 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
280 num_mtrrs++;
281
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300282 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500283 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300284 slot = stack_push(slot, ~(CACHE_TMP_RAMTOP - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500285 slot = stack_push(slot, 0); /* upper base */
286 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
287 num_mtrrs++;
288
289 top_of_ram = (uint32_t)cbmem_top();
Elyes HAOUAS038e7242016-07-29 18:31:16 +0200290 /* Cache 8MiB below the top of ram. The top of RAM under 4GiB is the
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500291 * start of the TSEG region. It is required to be 8MiB aligned. Set
292 * this area as cacheable so it can be used later for ramstage before
293 * setting up the entire RAM as cacheable. */
294 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700295 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500296 slot = stack_push(slot, 0); /* upper base */
297 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
298 num_mtrrs++;
299
Elyes HAOUAS038e7242016-07-29 18:31:16 +0200300 /* Cache 8MiB at the top of ram. Top of RAM is where the TSEG
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500301 * region resides. However, it is not restricted to SMM mode until
302 * SMM has been relocated. By setting the region to cacheable it
303 * provides faster access when relocating the SMM handler as well
304 * as using the TSEG region for other purposes. */
305 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700306 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500307 slot = stack_push(slot, 0); /* upper base */
308 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
309 num_mtrrs++;
310
311 /* Save the number of MTRRs to setup. Return the stack location
312 * pointing to the number of MTRRs. */
313 slot = stack_push(slot, num_mtrrs);
314
315 return slot;
316}