blob: 8798fa79d4c193f32bde7b9e6396c8e41fbb887a [file] [log] [blame]
Simon Glass43aae572019-12-08 17:40:15 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 Google LLC
4 *
5 * Portions taken from coreboot
6 */
7
Simon Glass43aae572019-12-08 17:40:15 -07008#include <dm.h>
9#include <ec_commands.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass43aae572019-12-08 17:40:15 -070011#include <log.h>
12#include <spi_flash.h>
13#include <spl.h>
14#include <syscon.h>
Simon Glass50461092020-04-08 16:57:35 -060015#include <acpi/acpi_s3.h>
Simon Glass43aae572019-12-08 17:40:15 -070016#include <asm/cpu.h>
17#include <asm/cpu_common.h>
18#include <asm/cpu_x86.h>
19#include <asm/fast_spi.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass43aae572019-12-08 17:40:15 -070021#include <asm/intel_pinctrl.h>
22#include <asm/intel_regs.h>
23#include <asm/io.h>
24#include <asm/msr.h>
25#include <asm/mtrr.h>
26#include <asm/pci.h>
27#include <asm/arch/cpu.h>
28#include <asm/arch/gpio.h>
29#include <asm/arch/iomap.h>
30#include <asm/arch/lpc.h>
31#include <asm/arch/pch.h>
32#include <asm/arch/systemagent.h>
Simon Glass43aae572019-12-08 17:40:15 -070033#include <asm/fsp2/fsp_api.h>
34#include <linux/sizes.h>
35#include <power/acpi_pmc.h>
36
Simon Glass43aae572019-12-08 17:40:15 -070037static int fast_spi_cache_bios_region(void)
38{
39 uint map_size, offset;
40 ulong map_base, base;
41 int ret;
42
43 ret = fast_spi_early_init(PCH_DEV_SPI, IOMAP_SPI_BASE);
44 if (ret)
45 return log_msg_ret("early_init", ret);
46
47 ret = fast_spi_get_bios_mmap(PCH_DEV_SPI, &map_base, &map_size,
48 &offset);
49 if (ret)
50 return log_msg_ret("get_mmap", ret);
51
52 base = SZ_4G - map_size;
53 mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size);
54 log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size);
55
56 return 0;
57}
58
Simon Glass43aae572019-12-08 17:40:15 -070059static void google_chromeec_ioport_range(uint *out_basep, uint *out_sizep)
60{
61 uint base;
62 uint size;
63
64 if (IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC_MEC)) {
65 base = MEC_EMI_BASE;
66 size = MEC_EMI_SIZE;
67 } else {
68 base = EC_HOST_CMD_REGION0;
69 size = 2 * EC_HOST_CMD_REGION_SIZE;
70 /* Make sure MEMMAP region follows host cmd region */
71 assert(base + size == EC_LPC_ADDR_MEMMAP);
72 size += EC_MEMMAP_SIZE;
73 }
74
75 *out_basep = base;
76 *out_sizep = size;
77}
78
79static void early_ec_init(void)
80{
81 uint base, size;
82
83 /*
84 * Set up LPC decoding for the Chrome OS EC I/O port ranges:
85 * - Ports 62/66, 60/64, and 200->208
86 * - Chrome OS EC communication I/O ports
87 */
88 lpc_enable_fixed_io_ranges(LPC_IOE_EC_62_66 | LPC_IOE_KBC_60_64 |
89 LPC_IOE_LGE_200);
90 google_chromeec_ioport_range(&base, &size);
91 lpc_open_pmio_window(base, size);
92}
93
94static int arch_cpu_init_tpl(void)
95{
96 struct udevice *pmc, *sa, *p2sb, *serial, *spi, *lpc;
97 int ret;
98
99 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &pmc);
100 if (ret)
101 return log_msg_ret("PMC", ret);
102
103 /* Clear global reset promotion bit */
104 ret = pmc_global_reset_set_enable(pmc, false);
105 if (ret)
106 return log_msg_ret("disable global reset", ret);
107
108 enable_pm_timer_emulation(pmc);
109
110 ret = uclass_first_device_err(UCLASS_P2SB, &p2sb);
111 if (ret)
112 return log_msg_ret("p2sb", ret);
113 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &sa);
114 if (ret)
115 return log_msg_ret("northbridge", ret);
116 gd->baudrate = CONFIG_BAUDRATE;
117 ret = uclass_first_device_err(UCLASS_SERIAL, &serial);
118 if (ret)
119 return log_msg_ret("serial", ret);
120 if (CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT)) {
121 ret = uclass_first_device_err(UCLASS_SPI, &spi);
122 if (ret)
123 return log_msg_ret("SPI", ret);
124 } else {
125 /* Alternative code if we don't have SPI in TPL */
126 if (IS_ENABLED(CONFIG_APL_BOOT_FROM_FAST_SPI_FLASH))
127 printf("Warning: Enable APL_SPI_FLASHBOOT to use SPI-flash driver in TPL");
128 ret = fast_spi_cache_bios_region();
129 if (ret)
130 return log_msg_ret("BIOS cache", ret);
131 }
132 ret = pmc_disable_tco(pmc);
133 if (ret)
134 return log_msg_ret("disable TCO", ret);
135 ret = pmc_gpe_init(pmc);
136 if (ret)
137 return log_msg_ret("pmc_gpe", ret);
138 ret = uclass_first_device_err(UCLASS_LPC, &lpc);
139 if (ret)
140 return log_msg_ret("lpc", ret);
141
142 early_ec_init();
143
144 return 0;
145}
146
147/*
148 * Enables several BARs and devices which are needed for memory init
149 * - MCH_BASE_ADDR is needed in order to talk to the memory controller
150 * - HPET is enabled because FSP wants to store a pointer to global data in the
151 * HPET comparator register
152 */
153static int arch_cpu_init_spl(void)
154{
155 struct udevice *pmc, *p2sb;
156 int ret;
157
158 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &pmc);
159 if (ret)
160 return log_msg_ret("Could not probe PMC", ret);
161 ret = uclass_first_device_err(UCLASS_P2SB, &p2sb);
162 if (ret)
163 return log_msg_ret("Cannot set up p2sb", ret);
164
165 lpc_io_setup_comm_a_b();
166
167 /* TODO(sjg@chromium.org): Enable upper RTC bank here */
168
169 ret = pmc_init(pmc);
170 if (ret < 0)
171 return log_msg_ret("Could not init PMC", ret);
Simon Glasse6ad2022020-07-09 18:43:16 -0600172 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
173 ret = pmc_prev_sleep_state(pmc);
174 if (ret < 0)
175 return log_msg_ret("Could not get PMC sleep state",
176 ret);
177 gd->arch.prev_sleep_state = ret;
178 }
Simon Glass43aae572019-12-08 17:40:15 -0700179
180 return 0;
181}
182
183int arch_cpu_init(void)
184{
185 int ret = 0;
186
187 if (spl_phase() == PHASE_TPL)
188 ret = arch_cpu_init_tpl();
189 else if (spl_phase() == PHASE_SPL)
190 ret = arch_cpu_init_spl();
191 if (ret)
192 printf("%s: Error %d\n", __func__, ret);
193
194 return ret;
195}