blob: 9a18476b22c8cf3aec446633062d7e7f82a3fa3f [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
8#include <common.h>
Simon Glass43aae572019-12-08 17:40:15 -07009#include <dm.h>
10#include <ec_commands.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass43aae572019-12-08 17:40:15 -070012#include <log.h>
13#include <spi_flash.h>
14#include <spl.h>
15#include <syscon.h>
Simon Glass50461092020-04-08 16:57:35 -060016#include <acpi/acpi_s3.h>
Simon Glass43aae572019-12-08 17:40:15 -070017#include <asm/cpu.h>
18#include <asm/cpu_common.h>
19#include <asm/cpu_x86.h>
20#include <asm/fast_spi.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass43aae572019-12-08 17:40:15 -070022#include <asm/intel_pinctrl.h>
23#include <asm/intel_regs.h>
24#include <asm/io.h>
25#include <asm/msr.h>
26#include <asm/mtrr.h>
27#include <asm/pci.h>
28#include <asm/arch/cpu.h>
29#include <asm/arch/gpio.h>
30#include <asm/arch/iomap.h>
31#include <asm/arch/lpc.h>
32#include <asm/arch/pch.h>
33#include <asm/arch/systemagent.h>
34#include <asm/arch/uart.h>
35#include <asm/fsp2/fsp_api.h>
36#include <linux/sizes.h>
37#include <power/acpi_pmc.h>
38
39/* Define this here to avoid referencing any drivers for the debug UART 1 */
40#define PCH_DEV_P2SB PCI_BDF(0, 0x0d, 0)
41
42static void pch_uart_init(void)
43{
44 /*
45 * Set up the pinmux so that the UART rx/tx signals are connected
46 * outside the SoC.
47 *
48 * There are about 500 lines of code required to program the GPIO
49 * configuration for the UARTs. But it boils down to four writes, and
50 * for the debug UART we want the minimum possible amount of code before
51 * the UART is running. So just add the magic writes here. See
52 * apl_hostbridge_early_init_pinctrl() for the full horror.
53 */
54 if (PCI_FUNC(PCH_DEV_UART) == 1) {
55 writel(0x40000402, 0xd0c50650);
56 writel(0x3c47, 0xd0c50654);
57 writel(0x40000400, 0xd0c50658);
58 writel(0x3c48, 0xd0c5065c);
59 } else { /* UART2 */
60 writel(0x40000402, 0xd0c50670);
61 writel(0x3c4b, 0xd0c50674);
62 writel(0x40000400, 0xd0c50678);
63 writel(0x3c4c, 0xd0c5067c);
64 }
65
66#ifdef CONFIG_DEBUG_UART
67 apl_uart_init(PCH_DEV_UART, CONFIG_DEBUG_UART_BASE);
68#endif
69}
70
71static void p2sb_enable_bar(ulong bar)
72{
73 /* Enable PCR Base address in PCH */
74 pci_x86_write_config(PCH_DEV_P2SB, PCI_BASE_ADDRESS_0, bar,
75 PCI_SIZE_32);
76 pci_x86_write_config(PCH_DEV_P2SB, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32);
77
78 /* Enable P2SB MSE */
79 pci_x86_write_config(PCH_DEV_P2SB, PCI_COMMAND,
80 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY,
81 PCI_SIZE_8);
82}
83
84/*
85 * board_debug_uart_init() - Init the debug UART ready for use
86 *
87 * This is the minimum init needed to get the UART running. It avoids any
88 * drivers or complex code, so that the UART is running as soon as possible.
89 */
90void board_debug_uart_init(void)
91{
92 p2sb_enable_bar(IOMAP_P2SB_BAR);
93 pch_uart_init();
94}
95
96static int fast_spi_cache_bios_region(void)
97{
98 uint map_size, offset;
99 ulong map_base, base;
100 int ret;
101
102 ret = fast_spi_early_init(PCH_DEV_SPI, IOMAP_SPI_BASE);
103 if (ret)
104 return log_msg_ret("early_init", ret);
105
106 ret = fast_spi_get_bios_mmap(PCH_DEV_SPI, &map_base, &map_size,
107 &offset);
108 if (ret)
109 return log_msg_ret("get_mmap", ret);
110
111 base = SZ_4G - map_size;
112 mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size);
113 log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size);
114
115 return 0;
116}
117
Simon Glass43aae572019-12-08 17:40:15 -0700118static void google_chromeec_ioport_range(uint *out_basep, uint *out_sizep)
119{
120 uint base;
121 uint size;
122
123 if (IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC_MEC)) {
124 base = MEC_EMI_BASE;
125 size = MEC_EMI_SIZE;
126 } else {
127 base = EC_HOST_CMD_REGION0;
128 size = 2 * EC_HOST_CMD_REGION_SIZE;
129 /* Make sure MEMMAP region follows host cmd region */
130 assert(base + size == EC_LPC_ADDR_MEMMAP);
131 size += EC_MEMMAP_SIZE;
132 }
133
134 *out_basep = base;
135 *out_sizep = size;
136}
137
138static void early_ec_init(void)
139{
140 uint base, size;
141
142 /*
143 * Set up LPC decoding for the Chrome OS EC I/O port ranges:
144 * - Ports 62/66, 60/64, and 200->208
145 * - Chrome OS EC communication I/O ports
146 */
147 lpc_enable_fixed_io_ranges(LPC_IOE_EC_62_66 | LPC_IOE_KBC_60_64 |
148 LPC_IOE_LGE_200);
149 google_chromeec_ioport_range(&base, &size);
150 lpc_open_pmio_window(base, size);
151}
152
153static int arch_cpu_init_tpl(void)
154{
155 struct udevice *pmc, *sa, *p2sb, *serial, *spi, *lpc;
156 int ret;
157
158 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &pmc);
159 if (ret)
160 return log_msg_ret("PMC", ret);
161
162 /* Clear global reset promotion bit */
163 ret = pmc_global_reset_set_enable(pmc, false);
164 if (ret)
165 return log_msg_ret("disable global reset", ret);
166
167 enable_pm_timer_emulation(pmc);
168
169 ret = uclass_first_device_err(UCLASS_P2SB, &p2sb);
170 if (ret)
171 return log_msg_ret("p2sb", ret);
172 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &sa);
173 if (ret)
174 return log_msg_ret("northbridge", ret);
175 gd->baudrate = CONFIG_BAUDRATE;
176 ret = uclass_first_device_err(UCLASS_SERIAL, &serial);
177 if (ret)
178 return log_msg_ret("serial", ret);
179 if (CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT)) {
180 ret = uclass_first_device_err(UCLASS_SPI, &spi);
181 if (ret)
182 return log_msg_ret("SPI", ret);
183 } else {
184 /* Alternative code if we don't have SPI in TPL */
185 if (IS_ENABLED(CONFIG_APL_BOOT_FROM_FAST_SPI_FLASH))
186 printf("Warning: Enable APL_SPI_FLASHBOOT to use SPI-flash driver in TPL");
187 ret = fast_spi_cache_bios_region();
188 if (ret)
189 return log_msg_ret("BIOS cache", ret);
190 }
191 ret = pmc_disable_tco(pmc);
192 if (ret)
193 return log_msg_ret("disable TCO", ret);
194 ret = pmc_gpe_init(pmc);
195 if (ret)
196 return log_msg_ret("pmc_gpe", ret);
197 ret = uclass_first_device_err(UCLASS_LPC, &lpc);
198 if (ret)
199 return log_msg_ret("lpc", ret);
200
201 early_ec_init();
202
203 return 0;
204}
205
206/*
207 * Enables several BARs and devices which are needed for memory init
208 * - MCH_BASE_ADDR is needed in order to talk to the memory controller
209 * - HPET is enabled because FSP wants to store a pointer to global data in the
210 * HPET comparator register
211 */
212static int arch_cpu_init_spl(void)
213{
214 struct udevice *pmc, *p2sb;
215 int ret;
216
217 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &pmc);
218 if (ret)
219 return log_msg_ret("Could not probe PMC", ret);
220 ret = uclass_first_device_err(UCLASS_P2SB, &p2sb);
221 if (ret)
222 return log_msg_ret("Cannot set up p2sb", ret);
223
224 lpc_io_setup_comm_a_b();
225
226 /* TODO(sjg@chromium.org): Enable upper RTC bank here */
227
228 ret = pmc_init(pmc);
229 if (ret < 0)
230 return log_msg_ret("Could not init PMC", ret);
Simon Glasse6ad2022020-07-09 18:43:16 -0600231 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
232 ret = pmc_prev_sleep_state(pmc);
233 if (ret < 0)
234 return log_msg_ret("Could not get PMC sleep state",
235 ret);
236 gd->arch.prev_sleep_state = ret;
237 }
Simon Glass43aae572019-12-08 17:40:15 -0700238
239 return 0;
240}
241
242int arch_cpu_init(void)
243{
244 int ret = 0;
245
246 if (spl_phase() == PHASE_TPL)
247 ret = arch_cpu_init_tpl();
248 else if (spl_phase() == PHASE_SPL)
249 ret = arch_cpu_init_spl();
250 if (ret)
251 printf("%s: Error %d\n", __func__, ret);
252
253 return ret;
254}