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