blob: 8de670038b4cf789459aee8c393351fa0e955ae7 [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>
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020020#include <bootblock_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050021#include <console/console.h>
22#include <cbmem.h>
23#include <cpu/x86/mtrr.h>
Martin Rothe6ff1592017-06-24 21:34:29 -060024#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
Aaron Durbin3e0eea12013-10-28 11:20:35 -050025#include <ec/google/chromeec/ec.h>
26#endif
Aaron Durbina8e9b632013-10-30 15:46:07 -050027#include <elog.h>
Kyösti Mälkki65e8f642016-06-27 11:27:56 +030028#include <program_loading.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050029#include <romstage_handoff.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060030#include <stage_cache.h>
Aaron Durbinafe8aee2016-11-29 21:37:42 -060031#include <string.h>
Aaron Durbin794bddf2013-09-27 11:38:36 -050032#include <timestamp.h>
Aaron Durbinebf7ec52013-11-14 13:47:08 -060033#include <vendorcode/google/chromeos/chromeos.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070034#include <soc/gpio.h>
35#include <soc/iomap.h>
36#include <soc/lpc.h>
37#include <soc/pci_devs.h>
38#include <soc/pmc.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070039#include <soc/romstage.h>
40#include <soc/smm.h>
41#include <soc/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050042
43/* The cache-as-ram assembly file calls romstage_main() after setting up
44 * cache-as-ram. romstage_main() will then call the mainboards's
45 * mainboard_romstage_entry() function. That function then calls
46 * romstage_common() below. The reason for the back and forth is to provide
47 * common entry point from cache-as-ram while still allowing for code sharing.
48 * Because we can't use global variables the stack is used for allocations --
49 * thus the need to call back and forth. */
50
Arthur Heymansd5d20d02018-11-29 14:16:49 +010051static void platform_enter_postcar(void);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050052
53static void program_base_addresses(void)
54{
55 uint32_t reg;
56 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
57
58 /* Memory Mapped IO registers. */
59 reg = PMC_BASE_ADDRESS | 2;
60 pci_write_config32(lpc_dev, PBASE, reg);
61 reg = IO_BASE_ADDRESS | 2;
62 pci_write_config32(lpc_dev, IOBASE, reg);
63 reg = ILB_BASE_ADDRESS | 2;
64 pci_write_config32(lpc_dev, IBASE, reg);
65 reg = SPI_BASE_ADDRESS | 2;
66 pci_write_config32(lpc_dev, SBASE, reg);
67 reg = MPHY_BASE_ADDRESS | 2;
68 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050069 reg = PUNIT_BASE_ADDRESS | 2;
70 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050071 reg = RCBA_BASE_ADDRESS | 1;
72 pci_write_config32(lpc_dev, RCBA, reg);
73
74 /* IO Port Registers. */
75 reg = ACPI_BASE_ADDRESS | 2;
76 pci_write_config32(lpc_dev, ABASE, reg);
77 reg = GPIO_BASE_ADDRESS | 2;
78 pci_write_config32(lpc_dev, GBASE, reg);
79}
80
Aaron Durbin6f9947a2013-11-18 11:16:20 -060081static void spi_init(void)
82{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080083 u32 *scs = (u32 *)(SPI_BASE_ADDRESS + SCS);
84 u32 *bcr = (u32 *)(SPI_BASE_ADDRESS + BCR);
Aaron Durbin4177db52014-02-05 14:55:26 -060085 uint32_t reg;
86
87 /* Disable generating SMI when setting WPD bit. */
88 write32(scs, read32(scs) & ~SMIWPEN);
89 /*
90 * Enable caching and prefetching in the SPI controller. Disable
91 * the SMM-only BIOS write and set WPD bit.
92 */
93 reg = (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH | BCR_WPD;
94 reg &= ~EISS;
95 write32(bcr, reg);
Aaron Durbin6f9947a2013-11-18 11:16:20 -060096}
97
Aaron Durbin794bddf2013-09-27 11:38:36 -050098/* Entry from cache-as-ram.inc. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020099static void romstage_main(uint64_t tsc, uint32_t bist)
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älkkic641f7e2018-12-28 16:54:54 +0200107 timestamp_init(tsc);
Kyösti Mälkki41759272014-12-31 21:11:51 +0200108
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
Arthur Heymansd5d20d02018-11-29 14:16:49 +0100131 platform_enter_postcar();
132
133 /* We don't return here */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200134}
135
136/* This wrapper enables easy transition towards C_ENVIRONMENT_BOOTBLOCK,
137 * keeping changes in cache_as_ram.S easy to manage.
138 */
139asmlinkage void bootblock_c_entry_bist(uint64_t base_timestamp, uint32_t bist)
140{
141 romstage_main(base_timestamp, bist);
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500142}
143
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600144static struct chipset_power_state power_state CAR_GLOBAL;
145
Aaron Durbin41607a42015-06-09 13:54:10 -0500146static void migrate_power_state(int is_recovery)
Aaron Durbin6e328932013-11-06 12:04:50 -0600147{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600148 struct chipset_power_state *ps_cbmem;
149 struct chipset_power_state *ps_car;
150
151 ps_car = car_get_var_ptr(&power_state);
152 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
153
154 if (ps_cbmem == NULL) {
155 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
156 return;
157 }
158 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
159}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200160ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600161
162static struct chipset_power_state *fill_power_state(void)
163{
164 struct chipset_power_state *ps = car_get_var_ptr(&power_state);
165
166 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
167 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
168 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
169 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
170 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
171 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800172 ps->prsts = read32((u32 *)(PMC_BASE_ADDRESS + PRSTS));
173 ps->gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1));
174 ps->gen_pmcon2 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON2));
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600175
176 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
177 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
178 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
179 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
180 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
181 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
182
183 return ps;
184}
185
186/* Return 0, 3, or 5 to indicate the previous sleep state. */
187static int chipset_prev_sleep_state(struct chipset_power_state *ps)
188{
Aaron Durbin6e328932013-11-06 12:04:50 -0600189 /* Default to S0. */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500190 int prev_sleep_state = ACPI_S0;
Aaron Durbin6e328932013-11-06 12:04:50 -0600191
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600192 if (ps->pm1_sts & WAK_STS) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500193 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
194 case ACPI_S3:
195 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME))
196 prev_sleep_state = ACPI_S3;
Aaron Durbin6e328932013-11-06 12:04:50 -0600197 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500198 case ACPI_S5:
199 prev_sleep_state = ACPI_S5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600200 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600201 }
202 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600203 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600204 }
205
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600206 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500207 prev_sleep_state = ACPI_S5;
Aaron Durbin6e328932013-11-06 12:04:50 -0600208 }
209
Aaron Durbin6e328932013-11-06 12:04:50 -0600210 return prev_sleep_state;
211}
212
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500213/* Entry from the mainboard. */
214void romstage_common(struct romstage_params *params)
215{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600216 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600217 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500218
Kyösti Mälkki41759272014-12-31 21:11:51 +0200219 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500220
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600221 ps = fill_power_state();
222 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600223
224 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
225
Martin Rothe6ff1592017-06-24 21:34:29 -0600226#if IS_ENABLED(CONFIG_ELOG_BOOT_COUNT)
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500227 if (prev_sleep_state != ACPI_S3)
Aaron Durbin4177db52014-02-05 14:55:26 -0600228 boot_count_increment();
229#endif
230
231
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500232 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600233 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500234
Kyösti Mälkki41759272014-12-31 21:11:51 +0200235 timestamp_add_now(TS_AFTER_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500236
Aaron Durbin77e13992016-11-29 17:43:04 -0600237 romstage_handoff_init(prev_sleep_state == ACPI_S3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500238}
239
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100240#define ROMSTAGE_RAM_STACK_SIZE 0x5000
241
Elyes HAOUASbc8762e2018-04-25 15:50:27 +0200242/* setup_stack_and_mtrrs() determines the stack to use after
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500243 * cache-as-ram is torn down as well as the MTRR settings to use. */
Arthur Heymansd5d20d02018-11-29 14:16:49 +0100244static void platform_enter_postcar(void)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500245{
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100246 struct postcar_frame pcf;
247 uintptr_t top_of_ram;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500248
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100249 if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE))
250 die("Unable to initialize postcar frame.\n");
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500251 /* Cache the ROM as WP just below 4GiB. */
Nico Huber4c7eee22019-02-10 19:35:41 +0100252 postcar_frame_add_romcache(&pcf, MTRR_TYPE_WRPROT);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500253
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300254 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100255 postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500256
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100257 /* Cache at least 8 MiB below the top of ram, and at most 8 MiB
258 * above top of the ram. This satisfies MTRR alignment requirement
259 * with different TSEG size configurations.
260 */
261 top_of_ram = ALIGN_DOWN((uintptr_t)cbmem_top(), 8*MiB);
262 postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 16*MiB,
263 MTRR_TYPE_WRBACK);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500264
Arthur Heymansd5d20d02018-11-29 14:16:49 +0100265 run_postcar_phase(&pcf);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500266}