blob: 63e36aaa36fdaa1b4461099d66bcef0b0cc46228 [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020019#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020021#include <bootblock_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050022#include <console/console.h>
23#include <cbmem.h>
24#include <cpu/x86/mtrr.h>
Kyösti Mälkki7cdb0472019-08-08 11:16:06 +030025#include <cpu/x86/smm.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080026#if CONFIG(EC_GOOGLE_CHROMEEC)
Aaron Durbin3e0eea12013-10-28 11:20:35 -050027#include <ec/google/chromeec/ec.h>
28#endif
Aaron Durbina8e9b632013-10-30 15:46:07 -050029#include <elog.h>
Kyösti Mälkki65e8f642016-06-27 11:27:56 +030030#include <program_loading.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050031#include <romstage_handoff.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060032#include <stage_cache.h>
Aaron Durbinafe8aee2016-11-29 21:37:42 -060033#include <string.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>
Julius Werner18ea2d32014-10-07 16:42:17 -070036#include <soc/gpio.h>
37#include <soc/iomap.h>
38#include <soc/lpc.h>
Kyösti Mälkki8e23bac2019-08-17 06:47:50 +030039#include <soc/msr.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070040#include <soc/pci_devs.h>
41#include <soc/pmc.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070042#include <soc/romstage.h>
43#include <soc/smm.h>
44#include <soc/spi.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050045
46/* The cache-as-ram assembly file calls romstage_main() after setting up
47 * cache-as-ram. romstage_main() will then call the mainboards's
48 * mainboard_romstage_entry() function. That function then calls
49 * romstage_common() below. The reason for the back and forth is to provide
50 * common entry point from cache-as-ram while still allowing for code sharing.
51 * Because we can't use global variables the stack is used for allocations --
52 * thus the need to call back and forth. */
53
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030054static struct postcar_frame early_mtrrs;
55
56static void fill_postcar_frame(struct postcar_frame *pcf);
57
58/* prepare_and_run_postcar() determines the stack to use after
59 * cache-as-ram is torn down as well as the MTRR settings to use. */
60static void prepare_and_run_postcar(struct postcar_frame *pcf)
61{
62 if (postcar_frame_init(pcf, 0))
63 die("Unable to initialize postcar frame.\n");
64
65 fill_postcar_frame(pcf);
66
Kyösti Mälkki544878b2019-08-09 11:41:15 +030067 postcar_frame_common_mtrrs(pcf);
68
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030069 run_postcar_phase(pcf);
70 /* We do not return here. */
71}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050072
73static void program_base_addresses(void)
74{
75 uint32_t reg;
76 const uint32_t lpc_dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
77
78 /* Memory Mapped IO registers. */
79 reg = PMC_BASE_ADDRESS | 2;
80 pci_write_config32(lpc_dev, PBASE, reg);
81 reg = IO_BASE_ADDRESS | 2;
82 pci_write_config32(lpc_dev, IOBASE, reg);
83 reg = ILB_BASE_ADDRESS | 2;
84 pci_write_config32(lpc_dev, IBASE, reg);
85 reg = SPI_BASE_ADDRESS | 2;
86 pci_write_config32(lpc_dev, SBASE, reg);
87 reg = MPHY_BASE_ADDRESS | 2;
88 pci_write_config32(lpc_dev, MPBASE, reg);
Aaron Durbina64ef622013-10-03 12:56:37 -050089 reg = PUNIT_BASE_ADDRESS | 2;
90 pci_write_config32(lpc_dev, PUBASE, reg);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050091 reg = RCBA_BASE_ADDRESS | 1;
92 pci_write_config32(lpc_dev, RCBA, reg);
93
94 /* IO Port Registers. */
95 reg = ACPI_BASE_ADDRESS | 2;
96 pci_write_config32(lpc_dev, ABASE, reg);
97 reg = GPIO_BASE_ADDRESS | 2;
98 pci_write_config32(lpc_dev, GBASE, reg);
99}
100
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600101static void spi_init(void)
102{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 u32 *scs = (u32 *)(SPI_BASE_ADDRESS + SCS);
104 u32 *bcr = (u32 *)(SPI_BASE_ADDRESS + BCR);
Aaron Durbin4177db52014-02-05 14:55:26 -0600105 uint32_t reg;
106
107 /* Disable generating SMI when setting WPD bit. */
108 write32(scs, read32(scs) & ~SMIWPEN);
109 /*
110 * Enable caching and prefetching in the SPI controller. Disable
111 * the SMM-only BIOS write and set WPD bit.
112 */
113 reg = (read32(bcr) & ~SRC_MASK) | SRC_CACHE_PREFETCH | BCR_WPD;
114 reg &= ~EISS;
115 write32(bcr, reg);
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600116}
117
Aaron Durbin794bddf2013-09-27 11:38:36 -0500118/* Entry from cache-as-ram.inc. */
Kyösti Mälkki157b1892019-08-16 14:02:25 +0300119static void romstage_main(uint64_t tsc)
Aaron Durbin794bddf2013-09-27 11:38:36 -0500120{
121 struct romstage_params rp = {
Aaron Durbin794bddf2013-09-27 11:38:36 -0500122 .mrc_params = NULL,
123 };
124
125 /* Save initial timestamp from bootblock. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200126 timestamp_init(tsc);
Kyösti Mälkki41759272014-12-31 21:11:51 +0200127
Aaron Durbin794bddf2013-09-27 11:38:36 -0500128 /* Save romstage begin */
Kyösti Mälkki41759272014-12-31 21:11:51 +0200129 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500130
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500131 program_base_addresses();
132
Aaron Durbinfd039f72013-10-04 11:11:52 -0500133 tco_disable();
134
Kyösti Mälkki8e23bac2019-08-17 06:47:50 +0300135 if (CONFIG(ENABLE_BUILTIN_COM1))
136 byt_config_com1_and_enable();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500137
138 console_init();
139
Aaron Durbin6f9947a2013-11-18 11:16:20 -0600140 spi_init();
141
Aaron Durbinbb3ee832013-10-07 17:12:20 -0500142 set_max_freq();
143
Aaron Durbin189aa3e2013-10-04 11:17:45 -0500144 punit_init();
145
Aaron Durbinecf90862013-09-24 12:36:14 -0500146 gfx_init();
147
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500148 /* Call into mainboard. */
Kyösti Mälkkic2741852019-08-16 15:13:00 +0300149 mainboard_romstage_entry_rp(&rp);
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500150
Kyösti Mälkki7cdb0472019-08-08 11:16:06 +0300151 if (CONFIG(SMM_TSEG))
152 smm_list_regions();
153
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300154 prepare_and_run_postcar(&early_mtrrs);
155 /* We do not return here. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200156}
157
158/* This wrapper enables easy transition towards C_ENVIRONMENT_BOOTBLOCK,
159 * keeping changes in cache_as_ram.S easy to manage.
160 */
161asmlinkage void bootblock_c_entry_bist(uint64_t base_timestamp, uint32_t bist)
162{
Kyösti Mälkki157b1892019-08-16 14:02:25 +0300163 romstage_main(base_timestamp);
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500164}
165
Arthur Heymanse2286782018-12-29 14:01:12 +0100166static struct chipset_power_state power_state;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600167
Aaron Durbin41607a42015-06-09 13:54:10 -0500168static void migrate_power_state(int is_recovery)
Aaron Durbin6e328932013-11-06 12:04:50 -0600169{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600170 struct chipset_power_state *ps_cbmem;
171 struct chipset_power_state *ps_car;
172
Arthur Heymanse2286782018-12-29 14:01:12 +0100173 ps_car = &power_state;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600174 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
175
176 if (ps_cbmem == NULL) {
177 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
178 return;
179 }
180 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
181}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200182ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600183
184static struct chipset_power_state *fill_power_state(void)
185{
Arthur Heymanse2286782018-12-29 14:01:12 +0100186 struct chipset_power_state *ps = &power_state;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600187
188 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
189 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
190 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
191 ps->gpe0_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS);
192 ps->gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN);
193 ps->tco_sts = inl(ACPI_BASE_ADDRESS + TCO_STS);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800194 ps->prsts = read32((u32 *)(PMC_BASE_ADDRESS + PRSTS));
195 ps->gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1));
196 ps->gen_pmcon2 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON2));
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600197
198 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
199 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
200 printk(BIOS_DEBUG, "gpe0_sts: %08x gpe0_en: %08x tco_sts: %08x\n",
201 ps->gpe0_sts, ps->gpe0_en, ps->tco_sts);
202 printk(BIOS_DEBUG, "prsts: %08x gen_pmcon1: %08x gen_pmcon2: %08x\n",
203 ps->prsts, ps->gen_pmcon1, ps->gen_pmcon2);
204
205 return ps;
206}
207
208/* Return 0, 3, or 5 to indicate the previous sleep state. */
209static int chipset_prev_sleep_state(struct chipset_power_state *ps)
210{
Aaron Durbin6e328932013-11-06 12:04:50 -0600211 /* Default to S0. */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500212 int prev_sleep_state = ACPI_S0;
Aaron Durbin6e328932013-11-06 12:04:50 -0600213
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600214 if (ps->pm1_sts & WAK_STS) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500215 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
216 case ACPI_S3:
Julius Wernercd49cce2019-03-05 16:53:33 -0800217 if (CONFIG(HAVE_ACPI_RESUME))
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500218 prev_sleep_state = ACPI_S3;
Aaron Durbin6e328932013-11-06 12:04:50 -0600219 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500220 case ACPI_S5:
221 prev_sleep_state = ACPI_S5;
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600222 break;
Aaron Durbin6e328932013-11-06 12:04:50 -0600223 }
224 /* Clear SLP_TYP. */
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600225 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600226 }
227
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600228 if (ps->gen_pmcon1 & (PWR_FLR | SUS_PWR_FLR)) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500229 prev_sleep_state = ACPI_S5;
Aaron Durbin6e328932013-11-06 12:04:50 -0600230 }
231
Aaron Durbin6e328932013-11-06 12:04:50 -0600232 return prev_sleep_state;
233}
234
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500235/* Entry from the mainboard. */
236void romstage_common(struct romstage_params *params)
237{
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600238 struct chipset_power_state *ps;
Aaron Durbin6e328932013-11-06 12:04:50 -0600239 int prev_sleep_state;
Aaron Durbin5f8ad562013-10-08 16:54:18 -0500240
Kyösti Mälkki41759272014-12-31 21:11:51 +0200241 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500242
Aaron Durbin00bf3db2014-01-09 10:33:23 -0600243 ps = fill_power_state();
244 prev_sleep_state = chipset_prev_sleep_state(ps);
Aaron Durbin6e328932013-11-06 12:04:50 -0600245
246 printk(BIOS_DEBUG, "prev_sleep_state = S%d\n", prev_sleep_state);
247
Julius Wernercd49cce2019-03-05 16:53:33 -0800248#if CONFIG(ELOG_BOOT_COUNT)
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500249 if (prev_sleep_state != ACPI_S3)
Aaron Durbin4177db52014-02-05 14:55:26 -0600250 boot_count_increment();
251#endif
252
253
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500254 /* Initialize RAM */
Aaron Durbin6e328932013-11-06 12:04:50 -0600255 raminit(params->mrc_params, prev_sleep_state);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500256
Kyösti Mälkki41759272014-12-31 21:11:51 +0200257 timestamp_add_now(TS_AFTER_INITRAM);
Aaron Durbin794bddf2013-09-27 11:38:36 -0500258
Aaron Durbin77e13992016-11-29 17:43:04 -0600259 romstage_handoff_init(prev_sleep_state == ACPI_S3);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500260}
261
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300262static void fill_postcar_frame(struct postcar_frame *pcf)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500263{
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100264 uintptr_t top_of_ram;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500265
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100266 /* Cache at least 8 MiB below the top of ram, and at most 8 MiB
267 * above top of the ram. This satisfies MTRR alignment requirement
268 * with different TSEG size configurations.
269 */
270 top_of_ram = ALIGN_DOWN((uintptr_t)cbmem_top(), 8*MiB);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300271 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 16*MiB,
Arthur Heymansf6cfbf32018-11-29 14:08:15 +0100272 MTRR_TYPE_WRBACK);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500273}