blob: c8d66ac3f2bf1863f3e21ceda814a6d7120d9d1c [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
Richard Spiegel376dc822017-12-01 08:24:26 -07004 * Copyright (C) 2010-2017 Advanced Micro Devices, Inc.
Marc Jones24484842017-05-04 21:17:45 -06005 *
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.
14 */
15
16#include <console/console.h>
17
18#include <arch/io.h>
Marshall Dawson8a906df2017-06-13 14:19:02 -060019#include <bootstate.h>
Marshall Dawsone9b862e2017-09-22 15:14:46 -060020#include <cpu/x86/smm.h>
Marc Jones24484842017-05-04 21:17:45 -060021#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <device/pci_ops.h>
25#include <cbmem.h>
Marshall Dawson70f051f2018-03-20 10:27:41 -060026#include <elog.h>
Richard Spiegel2bbc3dc2017-12-06 16:14:58 -070027#include <amdblocks/amd_pci_util.h>
Richard Spiegel71081072018-07-26 10:51:38 -070028#include <amdblocks/agesawrapper.h>
Nico Huber73c11192018-10-06 18:20:47 +020029#include <amdblocks/reset.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060030#include <soc/southbridge.h>
Richard Spiegelb40e1932018-10-24 12:51:21 -070031#include <soc/smbus.h>
Marc Jones24484842017-05-04 21:17:45 -060032#include <soc/smi.h>
Richard Spiegel376dc822017-12-01 08:24:26 -070033#include <soc/amd_pci_int_defs.h>
Richard Spiegelbec44f22017-11-24 07:41:29 -070034#include <delay.h>
35#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070036#include <agesa_headers.h>
Richard Spiegeldbee8ae2018-05-09 17:34:04 -070037#include <soc/nvs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070038
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070039/*
40 * Table of devices that need their AOAC registers enabled and waited
41 * upon (usually about .55 milliseconds). Instead of individual delays
42 * waiting for each device to become available, a single delay will be
Richard Spiegel6dfbb592018-03-15 15:45:44 -070043 * executed.
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070044 */
45const static struct stoneyridge_aoac aoac_devs[] = {
46 { (FCH_AOAC_D3_CONTROL_UART0 + CONFIG_UART_FOR_CONSOLE * 2),
47 (FCH_AOAC_D3_STATE_UART0 + CONFIG_UART_FOR_CONSOLE * 2) },
48 { FCH_AOAC_D3_CONTROL_AMBA, FCH_AOAC_D3_STATE_AMBA },
49 { FCH_AOAC_D3_CONTROL_I2C0, FCH_AOAC_D3_STATE_I2C0 },
50 { FCH_AOAC_D3_CONTROL_I2C1, FCH_AOAC_D3_STATE_I2C1 },
51 { FCH_AOAC_D3_CONTROL_I2C2, FCH_AOAC_D3_STATE_I2C2 },
52 { FCH_AOAC_D3_CONTROL_I2C3, FCH_AOAC_D3_STATE_I2C3 }
53};
54
Marshall Dawson2942db62017-12-14 10:00:27 -070055static int is_sata_config(void)
56{
Richard Spiegelbdd272a2018-10-16 13:53:05 -070057 return !((SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE)
58 || (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE));
Marshall Dawson2942db62017-12-14 10:00:27 -070059}
60
Richard Spiegel7ea8e022018-01-16 14:40:10 -070061static inline int sb_sata_enable(void)
62{
63 /* True if IDE or AHCI. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070064 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
65 (SataAhci == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070066}
67
68static inline int sb_ide_enable(void)
69{
70 /* True if IDE or LEGACY IDE. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070071 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
72 (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070073}
74
Marshall Dawson2942db62017-12-14 10:00:27 -070075void SetFchResetParams(FCH_RESET_INTERFACE *params)
76{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030077 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070078 params->Xhci0Enable = IS_ENABLED(CONFIG_STONEYRIDGE_XHCI_ENABLE);
Richard Spiegelbb18b432018-08-03 10:37:28 -070079 if (dev && dev->enabled) {
80 params->SataEnable = sb_sata_enable();
81 params->IdeEnable = sb_ide_enable();
82 } else {
83 params->SataEnable = FALSE;
84 params->IdeEnable = FALSE;
85 }
Marshall Dawson2942db62017-12-14 10:00:27 -070086}
87
88void SetFchEnvParams(FCH_INTERFACE *params)
89{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030090 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070091 params->AzaliaController = AzEnable;
92 params->SataClass = CONFIG_STONEYRIDGE_SATA_MODE;
Richard Spiegelbb18b432018-08-03 10:37:28 -070093 if (dev && dev->enabled) {
94 params->SataEnable = is_sata_config();
95 params->IdeEnable = !params->SataEnable;
96 params->SataIdeMode = (CONFIG_STONEYRIDGE_SATA_MODE ==
97 SataLegacyIde);
98 } else {
99 params->SataEnable = FALSE;
100 params->IdeEnable = FALSE;
101 params->SataIdeMode = FALSE;
102 }
Marshall Dawson2942db62017-12-14 10:00:27 -0700103}
104
105void SetFchMidParams(FCH_INTERFACE *params)
106{
107 SetFchEnvParams(params);
108}
Marc Jones24484842017-05-04 21:17:45 -0600109
Richard Spiegel376dc822017-12-01 08:24:26 -0700110/*
111 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100112 * provides a visible association with the index, therefore helping
Richard Spiegel376dc822017-12-01 08:24:26 -0700113 * maintainability of table. If a new index/name is defined in
114 * amd_pci_int_defs.h, just add the pair at the end of this table.
115 * Order is not important.
116 */
117const static struct irq_idx_name irq_association[] = {
Richard Spiegele89d4442017-12-08 07:52:42 -0700118 { PIRQ_A, "INTA#" },
119 { PIRQ_B, "INTB#" },
120 { PIRQ_C, "INTC#" },
121 { PIRQ_D, "INTD#" },
122 { PIRQ_E, "INTE#" },
123 { PIRQ_F, "INTF#" },
124 { PIRQ_G, "INTG#" },
125 { PIRQ_H, "INTH#" },
126 { PIRQ_MISC, "Misc" },
127 { PIRQ_MISC0, "Misc0" },
128 { PIRQ_MISC1, "Misc1" },
129 { PIRQ_MISC2, "Misc2" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700130 { PIRQ_SIRQA, "Ser IRQ INTA" },
131 { PIRQ_SIRQB, "Ser IRQ INTB" },
132 { PIRQ_SIRQC, "Ser IRQ INTC" },
133 { PIRQ_SIRQD, "Ser IRQ INTD" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700134 { PIRQ_SCI, "SCI" },
135 { PIRQ_SMBUS, "SMBUS" },
136 { PIRQ_ASF, "ASF" },
137 { PIRQ_HDA, "HDA" },
138 { PIRQ_FC, "FC" },
139 { PIRQ_PMON, "PerMon" },
140 { PIRQ_SD, "SD" },
141 { PIRQ_SDIO, "SDIOt" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700142 { PIRQ_EHCI, "EHCI" },
143 { PIRQ_XHCI, "XHCI" },
144 { PIRQ_SATA, "SATA" },
145 { PIRQ_GPIO, "GPIO" },
146 { PIRQ_I2C0, "I2C0" },
147 { PIRQ_I2C1, "I2C1" },
148 { PIRQ_I2C2, "I2C2" },
149 { PIRQ_I2C3, "I2C3" },
150 { PIRQ_UART0, "UART0" },
151 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700152};
153
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700154/*
155 * Structure to simplify code obtaining the total of used wide IO
156 * registers and the size assigned to each.
157 */
158static struct wide_io_ioport_and_bits {
159 uint32_t enable;
160 uint16_t port;
161 uint8_t alt;
162} wio_io_en[TOTAL_WIDEIO_PORTS] = {
163 {
164 LPC_WIDEIO0_ENABLE,
165 LPC_WIDEIO_GENERIC_PORT,
166 LPC_ALT_WIDEIO0_ENABLE
167 },
168 {
169 LPC_WIDEIO1_ENABLE,
170 LPC_WIDEIO1_GENERIC_PORT,
171 LPC_ALT_WIDEIO1_ENABLE
172 },
173 {
174 LPC_WIDEIO2_ENABLE,
175 LPC_WIDEIO2_GENERIC_PORT,
176 LPC_ALT_WIDEIO2_ENABLE
177 }
178};
179
Richard Spiegel376dc822017-12-01 08:24:26 -0700180const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
181{
182 *size = ARRAY_SIZE(irq_association);
183 return irq_association;
184}
185
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700186/**
187 * @brief Find the size of a particular wide IO
188 *
189 * @param index = index of desired wide IO
190 *
191 * @return size of desired wide IO
192 */
193uint16_t sb_wideio_size(int index)
194{
195 uint32_t enable_register;
196 uint16_t size = 0;
197 uint8_t alternate_register;
198
199 if (index >= TOTAL_WIDEIO_PORTS)
200 return size;
201 enable_register = pci_read_config32(SOC_LPC_DEV,
202 LPC_IO_OR_MEM_DECODE_ENABLE);
203 alternate_register = pci_read_config8(SOC_LPC_DEV,
204 LPC_ALT_WIDEIO_RANGE_ENABLE);
205 if (enable_register & wio_io_en[index].enable)
206 size = (alternate_register & wio_io_en[index].alt) ?
207 16 : 512;
208 return size;
209}
210
211/**
212 * @brief Identify if any LPC wide IO is covering the IO range
213 *
214 * @param start = start of IO range
215 * @param size = size of IO range
216 *
217 * @return Index of wide IO covering the range or error
218 */
219int sb_find_wideio_range(uint16_t start, uint16_t size)
220{
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700221 int i, index = WIDEIO_RANGE_ERROR;
222 uint16_t end, current_size, start_wideio, end_wideio;
223
224 end = start + size;
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700225 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
226 current_size = sb_wideio_size(i);
227 if (current_size == 0)
228 continue;
229 start_wideio = pci_read_config16(SOC_LPC_DEV,
230 wio_io_en[i].port);
231 end_wideio = start_wideio + current_size;
232 if ((start >= start_wideio) && (end <= end_wideio)) {
233 index = i;
234 break;
235 }
236 }
237 return index;
238}
239
240/**
241 * @brief Program a LPC wide IO to support an IO range
242 *
243 * @param start = start of range to be routed through wide IO
244 * @param size = size of range to be routed through wide IO
245 *
246 * @return Index of wide IO register used or error
247 */
248int sb_set_wideio_range(uint16_t start, uint16_t size)
249{
250 int i, index = WIDEIO_RANGE_ERROR;
251 uint32_t enable_register;
252 uint8_t alternate_register;
253
254 enable_register = pci_read_config32(SOC_LPC_DEV,
255 LPC_IO_OR_MEM_DECODE_ENABLE);
256 alternate_register = pci_read_config8(SOC_LPC_DEV,
257 LPC_ALT_WIDEIO_RANGE_ENABLE);
258 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
259 if (enable_register & wio_io_en[i].enable)
260 continue;
261 index = i;
262 pci_write_config16(SOC_LPC_DEV, wio_io_en[i].port, start);
263 enable_register |= wio_io_en[i].enable;
264 pci_write_config32(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE,
265 enable_register);
266 if (size <= 16)
267 alternate_register |= wio_io_en[i].alt;
268 else
269 alternate_register &= ~wio_io_en[i].alt;
270 pci_write_config8(SOC_LPC_DEV,
271 LPC_ALT_WIDEIO_RANGE_ENABLE,
272 alternate_register);
273 break;
274 }
275 return index;
276}
277
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600278static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700279{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600280 uint8_t byte;
281 uint8_t *register_pointer = (uint8_t *)(uintptr_t)AOAC_MMIO_BASE
282 + aoac_device_control_register;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700283
284 /* Power on the UART and AMBA devices */
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600285 byte = read8(register_pointer);
286 byte |= FCH_AOAC_PWR_ON_DEV;
287 write8(register_pointer, byte);
288}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700289
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600290static bool is_aoac_device_enabled(int aoac_device_status_register)
291{
292 uint8_t byte;
293 byte = read8((uint8_t *)(uintptr_t)AOAC_MMIO_BASE
294 + aoac_device_status_register);
295 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
296 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
297 return true;
298 else
299 return false;
300}
301
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700302void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600303{
304 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700305 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600306
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700307 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
308 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700309
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700310 /* Wait for AOAC devices to indicate power and clock OK */
311 do {
312 udelay(100);
313 status = true;
314 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
315 status &= is_aoac_device_enabled(aoac_devs[i].status);
316 } while (!status);
317}
318
Richard Spiegelbec44f22017-11-24 07:41:29 -0700319void sb_pci_port80(void)
320{
321 u8 byte;
322
323 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
324 byte &= ~DECODE_IO_PORT_ENABLE4_H; /* disable lpc port 80 */
325 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
326}
327
328void sb_lpc_port80(void)
329{
330 u8 byte;
331
332 /* Enable LPC controller */
333 outb(PM_LPC_GATING, PM_INDEX);
334 byte = inb(PM_DATA);
335 byte |= PM_LPC_ENABLE;
336 outb(PM_LPC_GATING, PM_INDEX);
337 outb(byte, PM_DATA);
338
339 /* Enable port 80 LPC decode in pci function 3 configuration space. */
340 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
341 byte |= DECODE_IO_PORT_ENABLE4_H; /* enable port 80 */
342 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
343}
344
345void sb_lpc_decode(void)
346{
347 u32 tmp = 0;
348
349 /* Enable I/O decode to LPC bus */
350 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
351 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
352 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
353 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
354 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
355 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
356 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
357 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
358 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
359 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
360 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
361 | DECODE_ENABLE_ADLIB_PORT;
362
363 pci_write_config32(SOC_LPC_DEV, LPC_IO_PORT_DECODE_ENABLE, tmp);
364}
365
Garrett Kirkendall9858bd22018-03-07 15:38:14 -0600366void sb_acpi_mmio_decode(void)
367{
368 uint8_t byte;
369
370 /* Enable ACPI MMIO range 0xfed80000 - 0xfed81fff */
371 outb(PM_ISA_CONTROL, PM_INDEX);
372 byte = inb(PM_DATA);
373 byte |= MMIO_EN;
374 outb(PM_ISA_CONTROL, PM_INDEX);
375 outb(byte, PM_DATA);
376}
377
Raul E Rangel5b058232018-06-28 16:31:45 -0600378static void sb_enable_cf9_io(void)
379{
380 uint32_t reg = pm_read32(PM_DECODE_EN);
381
382 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
383}
384
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600385static void sb_enable_legacy_io(void)
386{
387 uint32_t reg = pm_read32(PM_DECODE_EN);
388
389 pm_write32(PM_DECODE_EN, reg | LEGACY_IO_EN);
390}
391
Richard Spiegelbec44f22017-11-24 07:41:29 -0700392void sb_clk_output_48Mhz(void)
393{
394 u32 ctrl;
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600395 u32 *misc_clk_cntl_1_ptr = (u32 *)(uintptr_t)(MISC_MMIO_BASE
Richard Spiegel62052212018-10-17 13:32:58 -0700396 + MISC_CLK_CNTL1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700397
398 /*
399 * Enable the X14M_25M_48M_OSC pin and leaving it at it's default so
400 * 48Mhz will be on ball AP13 (FT3b package)
401 */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600402 ctrl = read32(misc_clk_cntl_1_ptr);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700403
404 /* clear the OSCOUT1_ClkOutputEnb to enable the 48 Mhz clock */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600405 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
406 write32(misc_clk_cntl_1_ptr, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700407}
408
409static uintptr_t sb_spibase(void)
410{
411 u32 base, enables;
412
413 /* Make sure the base address is predictable */
414 base = pci_read_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER);
Patrick Georgi4fbefc52018-10-23 14:35:37 +0200415 enables = base & SPI_PRESERVE_BITS;
416 base &= ~(SPI_PRESERVE_BITS | SPI_BASE_RESERVED);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700417
418 if (!base) {
419 base = SPI_BASE_ADDRESS;
420 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER,
421 base | enables | SPI_ROM_ENABLE);
422 /* PCI_COMMAND_MEMORY is read-only and enabled. */
423 }
424 return (uintptr_t)base;
425}
426
427void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
428{
429 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700430 write16((void *)(base + SPI100_SPEED_CONFIG),
Richard Spiegelbec44f22017-11-24 07:41:29 -0700431 (norm << SPI_NORM_SPEED_NEW_SH) |
432 (fast << SPI_FAST_SPEED_NEW_SH) |
433 (alt << SPI_ALT_SPEED_NEW_SH) |
434 (tpm << SPI_TPM_SPEED_NEW_SH));
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700435 write16((void *)(base + SPI100_ENABLE), SPI_USE_SPI100);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700436}
437
438void sb_disable_4dw_burst(void)
439{
440 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700441 write16((void *)(base + SPI100_HOST_PREF_CONFIG),
442 read16((void *)(base + SPI100_HOST_PREF_CONFIG))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700443 & ~SPI_RD4DW_EN_HOST);
444}
445
Richard Spiegelbec44f22017-11-24 07:41:29 -0700446void sb_read_mode(u32 mode)
447{
448 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700449 write32((void *)(base + SPI_CNTRL0),
450 (read32((void *)(base + SPI_CNTRL0))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700451 & ~SPI_READ_MODE_MASK) | mode);
452}
453
Garrett Kirkendall65753062018-03-07 16:12:11 -0600454/*
455 * Enable FCH to decode TPM associated Memory and IO regions
456 *
457 * Enable decoding of TPM cycles defined in TPM 1.2 spec
458 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
459 * 0x7e and 0xef-0xee.
460 * This function should be called if TPM is connected in any way to the FCH and
461 * conforms to the regions decoded.
462 * Absent any other routing configuration the TPM cycles will be claimed by the
463 * LPC bus
464 */
465void sb_tpm_decode(void)
466{
467 u32 value;
468
469 value = pci_read_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE);
470 value |= TPM_12_EN | TPM_LEGACY_EN;
471 pci_write_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE, value);
472}
473
474/*
475 * Enable FCH to decode TPM associated Memory and IO regions to SPI
476 *
477 * This should be used if TPM is connected to SPI bus.
478 * Assumes SPI address space is already configured via a call to sb_spibase().
479 */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700480void sb_tpm_decode_spi(void)
481{
Garrett Kirkendall65753062018-03-07 16:12:11 -0600482 /* Enable TPM decoding to FCH */
483 sb_tpm_decode();
484
485 /* Route TPM accesses to SPI */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700486 u32 spibase = pci_read_config32(SOC_LPC_DEV,
487 SPIROM_BASE_ADDRESS_REGISTER);
488 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER, spibase
489 | ROUTE_TPM_2_SPI);
490}
491
492/*
493 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
494 *
495 * Hardware should enable LPC ROM by pin straps. This function does not
496 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
497 *
498 * The southbridge power-on default is to map 512K ROM space.
499 *
500 */
501void sb_enable_rom(void)
502{
503 u8 reg8;
504
505 /*
506 * Decode variable LPC ROM address ranges 1 and 2.
507 * Bits 3-4 are not defined in any publicly available datasheet
508 */
509 reg8 = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
510 reg8 |= (1 << 3) | (1 << 4);
511 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, reg8);
512
513 /*
514 * LPC ROM address range 1:
515 * Enable LPC ROM range mirroring start at 0x000e(0000).
516 */
517 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_START, 0x000e);
518
519 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
520 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_END, 0x000f);
521
522 /*
523 * LPC ROM address range 2:
524 *
525 * Enable LPC ROM range start at:
526 * 0xfff8(0000): 512KB
527 * 0xfff0(0000): 1MB
528 * 0xffe0(0000): 2MB
529 * 0xffc0(0000): 4MB
530 */
531 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START, 0x10000
532 - (CONFIG_COREBOOT_ROMSIZE_KB >> 6));
533
534 /* Enable LPC ROM range end at 0xffff(ffff). */
535 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_END, 0xffff);
536}
537
Marc Jonescfb16802018-04-20 16:27:41 -0600538static void sb_lpc_early_setup(void)
539{
540 uint32_t dword;
541
542 /* Enable SPI prefetch */
543 dword = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
544 dword |= SPI_FROM_HOST_PREFETCH_EN | SPI_FROM_USB_PREFETCH_EN;
545 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, dword);
546
547 if (IS_ENABLED(CONFIG_STONEYRIDGE_LEGACY_FREE)) {
548 /* Decode SIOs at 2E/2F and 4E/4F */
549 dword = pci_read_config32(SOC_LPC_DEV,
550 LPC_IO_OR_MEM_DECODE_ENABLE);
551 dword |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
552 pci_write_config32(SOC_LPC_DEV,
553 LPC_IO_OR_MEM_DECODE_ENABLE, dword);
554 }
555}
556
Raul E Rangel79053412018-08-06 10:40:02 -0600557static void setup_spread_spectrum(int *reboot)
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600558{
559 uint16_t rstcfg = pm_read16(PWR_RESET_CFG);
560
561 rstcfg &= ~TOGGLE_ALL_PWR_GOOD;
562 pm_write16(PWR_RESET_CFG, rstcfg);
563
564 uint32_t cntl1 = misc_read32(MISC_CLK_CNTL1);
565
566 if (cntl1 & CG1PLL_FBDIV_TEST) {
567 printk(BIOS_DEBUG, "Spread spectrum is ready\n");
568 misc_write32(MISC_CGPLL_CONFIG1,
569 misc_read32(MISC_CGPLL_CONFIG1) |
570 CG1PLL_SPREAD_SPECTRUM_ENABLE);
571
572 return;
573 }
574
575 printk(BIOS_DEBUG, "Setting up spread spectrum\n");
576
577 uint32_t cfg6 = misc_read32(MISC_CGPLL_CONFIG6);
578 cfg6 &= ~CG1PLL_LF_MODE_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600579 cfg6 |= (0x0f8 << CG1PLL_LF_MODE_SHIFT) & CG1PLL_LF_MODE_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600580 misc_write32(MISC_CGPLL_CONFIG6, cfg6);
581
582 uint32_t cfg3 = misc_read32(MISC_CGPLL_CONFIG3);
583 cfg3 &= ~CG1PLL_REFDIV_MASK;
584 cfg3 |= (0x003 << CG1PLL_REFDIV_SHIFT) & CG1PLL_REFDIV_MASK;
585 cfg3 &= ~CG1PLL_FBDIV_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600586 cfg3 |= (0x04b << CG1PLL_FBDIV_SHIFT) & CG1PLL_FBDIV_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600587 misc_write32(MISC_CGPLL_CONFIG3, cfg3);
588
589 uint32_t cfg5 = misc_read32(MISC_CGPLL_CONFIG5);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600590 cfg5 &= ~SS_AMOUNT_NFRAC_SLIP_MASK;
591 cfg5 |= (0x2 << SS_AMOUNT_NFRAC_SLIP_SHIFT) & SS_AMOUNT_NFRAC_SLIP_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600592 misc_write32(MISC_CGPLL_CONFIG5, cfg5);
593
594 uint32_t cfg4 = misc_read32(MISC_CGPLL_CONFIG4);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600595 cfg4 &= ~SS_AMOUNT_DSFRAC_MASK;
596 cfg4 |= (0xd000 << SS_AMOUNT_DSFRAC_SHIFT) & SS_AMOUNT_DSFRAC_MASK;
597 cfg4 &= ~SS_STEP_SIZE_DSFRAC_MASK;
598 cfg4 |= (0x02d5 << SS_STEP_SIZE_DSFRAC_SHIFT)
599 & SS_STEP_SIZE_DSFRAC_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600600 misc_write32(MISC_CGPLL_CONFIG4, cfg4);
601
602 rstcfg |= TOGGLE_ALL_PWR_GOOD;
603 pm_write16(PWR_RESET_CFG, rstcfg);
604
605 cntl1 |= CG1PLL_FBDIV_TEST;
606 misc_write32(MISC_CLK_CNTL1, cntl1);
607
Raul E Rangel79053412018-08-06 10:40:02 -0600608 *reboot = 1;
609}
610
611static void setup_misc(int *reboot)
612{
613 /* Undocumented register */
614 uint32_t reg = misc_read32(0x50);
615 if (!(reg & BIT(16))) {
616 reg |= BIT(16);
617
618 misc_write32(0x50, reg);
619 *reboot = 1;
620 }
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600621}
622
Richard Spiegelb40e1932018-10-24 12:51:21 -0700623static void fch_smbus_init(void)
624{
625 pm_write8(SMB_ASF_IO_BASE, SMB_BASE_ADDR >> 8);
626 smbus_write8(SMBUS_MMIO_BASE, SMBTIMING, SMB_SPEED_400KHZ);
627 /* Clear all SMBUS status bits */
628 smbus_write8(SMBUS_MMIO_BASE, SMBHSTSTAT, SMBHST_STAT_CLEAR);
629 smbus_write8(SMBUS_MMIO_BASE, SMBSLVSTAT, SMBSLV_STAT_CLEAR);
630 smbus_write8(ASF_MMIO_BASE, SMBHSTSTAT, SMBHST_STAT_CLEAR);
631 smbus_write8(ASF_MMIO_BASE, SMBSLVSTAT, SMBSLV_STAT_CLEAR);
632}
633
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600634/* Before console init */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700635void bootblock_fch_early_init(void)
636{
Raul E Rangel79053412018-08-06 10:40:02 -0600637 int reboot = 0;
638
Richard Spiegelbec44f22017-11-24 07:41:29 -0700639 sb_enable_rom();
640 sb_lpc_port80();
641 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600642 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500643 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600644 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500645 sb_acpi_mmio_decode();
Richard Spiegelb40e1932018-10-24 12:51:21 -0700646 fch_smbus_init();
Raul E Rangel5b058232018-06-28 16:31:45 -0600647 sb_enable_cf9_io();
Raul E Rangel79053412018-08-06 10:40:02 -0600648 setup_spread_spectrum(&reboot);
649 setup_misc(&reboot);
650
651 if (reboot)
Nico Huber73c11192018-10-06 18:20:47 +0200652 warm_reset();
Raul E Rangel79053412018-08-06 10:40:02 -0600653
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600654 sb_enable_legacy_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700655 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700656}
657
Edward Hillcc680342018-08-10 16:20:02 -0600658static void print_num_status_bits(int num_bits, uint32_t status,
659 const char *const bit_names[])
660{
661 int i;
662
663 if (!status)
664 return;
665
666 for (i = num_bits - 1; i >= 0; i--) {
667 if (status & (1 << i)) {
668 if (bit_names[i])
669 printk(BIOS_DEBUG, "%s ", bit_names[i]);
670 else
671 printk(BIOS_DEBUG, "BIT%d ", i);
672 }
673 }
674}
675
676static void sb_print_pmxc0_status(void)
677{
678 /* PMxC0 S5/Reset Status shows the source of previous reset. */
679 uint32_t pmxc0_status = pm_read32(PM_RST_STATUS);
680
Edward Hill917b4002018-10-02 14:17:19 -0600681 static const char *const pmxc0_status_bits[32] = {
Edward Hillcc680342018-08-10 16:20:02 -0600682 [0] = "ThermalTrip",
683 [1] = "FourSecondPwrBtn",
684 [2] = "Shutdown",
685 [3] = "ThermalTripFromTemp",
686 [4] = "RemotePowerDownFromASF",
687 [5] = "ShutDownFan0",
688 [16] = "UserRst",
689 [17] = "SoftPciRst",
690 [18] = "DoInit",
691 [19] = "DoReset",
692 [20] = "DoFullReset",
693 [21] = "SleepReset",
694 [22] = "KbReset",
695 [23] = "LtReset",
696 [24] = "FailBootRst",
697 [25] = "WatchdogIssueReset",
698 [26] = "RemoteResetFromASF",
699 [27] = "SyncFlood",
700 [28] = "HangReset",
701 [29] = "EcWatchdogRst",
Edward Hillcc680342018-08-10 16:20:02 -0600702 };
703
Edward Hill917b4002018-10-02 14:17:19 -0600704 printk(BIOS_DEBUG, "PMxC0 STATUS: 0x%x ", pmxc0_status);
Edward Hillcc680342018-08-10 16:20:02 -0600705 print_num_status_bits(ARRAY_SIZE(pmxc0_status_bits), pmxc0_status,
706 pmxc0_status_bits);
Edward Hill917b4002018-10-02 14:17:19 -0600707 printk(BIOS_DEBUG, "\n");
Edward Hillcc680342018-08-10 16:20:02 -0600708}
709
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600710/* After console init */
Edward Hillcc680342018-08-10 16:20:02 -0600711void bootblock_fch_init(void)
712{
713 sb_print_pmxc0_status();
714}
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600715
Elyes HAOUASc5ad2672018-12-05 10:58:34 +0100716void sb_enable(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600717{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600718 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600719}
720
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600721static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600722{
Marshall Dawson91b80412017-09-27 16:44:40 -0600723 u32 reg;
724
Marc Jones24484842017-05-04 21:17:45 -0600725 /* We use some of these ports in SMM regardless of whether or not
726 * ACPI tables are generated. Enable these ports indiscriminately.
727 */
728
729 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
730 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
731 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
732 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
733 /* CpuControl is in \_PR.CP00, 6 bytes */
734 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
735
736 if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600737 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600738 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600739 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600740
741 /* SMI on SlpTyp requires sending SMI before completion
742 * response of the I/O write. The BKDG also specifies
743 * clearing ForceStpClkRetry for SMI trapping.
744 */
745 reg = pm_read32(PM_PCI_CTRL);
746 reg |= FORCE_SLPSTATE_RETRY;
747 reg &= ~FORCE_STPCLK_RETRY;
748 pm_write32(PM_PCI_CTRL, reg);
749
750 /* Disable SlpTyp feature */
751 reg = pm_read8(PM_RST_CTRL1);
752 reg &= ~SLPTYPE_CONTROL_EN;
753 pm_write8(PM_RST_CTRL1, reg);
754
755 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600756 } else {
757 pm_write16(PM_ACPI_SMI_CMD, 0);
758 }
759
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700760 /* Decode ACPI registers and enable standard features */
761 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
762 PM_ACPI_GLOBAL_EN |
763 PM_ACPI_RTC_EN_EN |
764 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600765}
766
Marshall Dawson70f051f2018-03-20 10:27:41 -0600767static uint16_t reset_pm1_status(void)
768{
Richard Spiegele24d7952018-10-26 13:25:01 -0700769 uint16_t pm1_sts = acpi_read16(MMIO_ACPI_PM1_STS);
770 acpi_write16(MMIO_ACPI_PM1_STS, pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600771 return pm1_sts;
772}
773
774static uint16_t print_pm1_status(uint16_t pm1_sts)
775{
Edward Hill917b4002018-10-02 14:17:19 -0600776 static const char *const pm1_sts_bits[16] = {
Marshall Dawson70f051f2018-03-20 10:27:41 -0600777 [0] = "TMROF",
778 [4] = "BMSTATUS",
779 [5] = "GBL",
780 [8] = "PWRBTN",
781 [10] = "RTC",
782 [14] = "PCIEXPWAK",
783 [15] = "WAK",
784 };
785
786 if (!pm1_sts)
787 return 0;
788
Edward Hill917b4002018-10-02 14:17:19 -0600789 printk(BIOS_DEBUG, "PM1_STS: ");
Marshall Dawson70f051f2018-03-20 10:27:41 -0600790 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
Edward Hill917b4002018-10-02 14:17:19 -0600791 printk(BIOS_DEBUG, "\n");
Marshall Dawson70f051f2018-03-20 10:27:41 -0600792
793 return pm1_sts;
794}
795
796static void sb_log_pm1_status(uint16_t pm1_sts)
797{
798 if (!IS_ENABLED(CONFIG_ELOG))
799 return;
800
Daniel Kurtzb6fdd222018-05-24 15:52:45 -0600801 if (pm1_sts & WAK_STS)
802 elog_add_event_byte(ELOG_TYPE_ACPI_WAKE,
803 acpi_is_wakeup_s3() ? ACPI_S3 : ACPI_S5);
804
Marshall Dawson70f051f2018-03-20 10:27:41 -0600805 if (pm1_sts & PWRBTN_STS)
806 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
807
808 if (pm1_sts & RTC_STS)
809 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
810
811 if (pm1_sts & PCIEXPWAK_STS)
812 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
813}
814
Richard Spiegel572f4982018-05-25 15:49:33 -0700815static void sb_save_sws(uint16_t pm1_status)
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700816{
Richard Spiegel35282a02018-06-14 14:57:54 -0700817 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700818 uint32_t reg32;
Richard Spiegel35282a02018-06-14 14:57:54 -0700819 uint16_t reg16;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700820
Richard Spiegel35282a02018-06-14 14:57:54 -0700821 sws = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(struct soc_power_reg));
822 if (sws == NULL)
823 return;
824 sws->pm1_sts = pm1_status;
Richard Spiegele24d7952018-10-26 13:25:01 -0700825 sws->pm1_en = acpi_read16(MMIO_ACPI_PM1_EN);
826 reg32 = acpi_read32(MMIO_ACPI_GPE0_STS);
827 acpi_write32(MMIO_ACPI_GPE0_STS, reg32);
Richard Spiegel35282a02018-06-14 14:57:54 -0700828 sws->gpe0_sts = reg32;
Richard Spiegele24d7952018-10-26 13:25:01 -0700829 sws->gpe0_en = acpi_read32(MMIO_ACPI_GPE0_EN);
830 reg16 = acpi_read16(MMIO_ACPI_PM1_CNT_BLK);
Richard Spiegel35282a02018-06-14 14:57:54 -0700831 reg16 &= SLP_TYP;
832 sws->wake_from = reg16 >> SLP_TYP_SHIFT;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700833}
834
Marshall Dawson70f051f2018-03-20 10:27:41 -0600835static void sb_clear_pm1_status(void)
836{
837 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700838
839 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600840 sb_log_pm1_status(pm1_sts);
841 print_pm1_status(pm1_sts);
842}
843
Richard Spiegel572f4982018-05-25 15:49:33 -0700844static int get_index_bit(uint32_t value, uint16_t limit)
845{
846 uint16_t i;
847 uint32_t t;
848
Richard Spiegelef73cb82018-06-19 07:40:18 -0700849 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700850 return -1;
851
852 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
853 t = (1 << limit) - 1;
854 if ((value & t) == 0)
855 return -1;
856 t = 1;
857 for (i = 0; i < limit; i++) {
858 if (value & t)
859 break;
860 t <<= 1;
861 }
862 return i;
863}
864
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700865static void set_nvs_sws(void *unused)
866{
Richard Spiegel35282a02018-06-14 14:57:54 -0700867 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700868 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700869 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700870
Richard Spiegel35282a02018-06-14 14:57:54 -0700871 sws = cbmem_find(CBMEM_ID_POWER_STATE);
872 if (sws == NULL)
873 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700874 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
875 if (gnvs == NULL)
876 return;
877
Richard Spiegel35282a02018-06-14 14:57:54 -0700878 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700879 if (index < 0)
880 gnvs->pm1i = ~0ULL;
881 else
882 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700883
Richard Spiegel35282a02018-06-14 14:57:54 -0700884 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700885 if (index < 0)
886 gnvs->gpei = ~0ULL;
887 else
888 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700889}
890
891BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
892
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600893void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600894{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600895 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600896 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600897}
898
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600899static void set_sb_final_nvs(void)
900{
901 uintptr_t amdfw_rom;
902 uintptr_t xhci_fw;
903 uintptr_t fwaddr;
904 size_t fwsize;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700905 const struct device *sd, *sata;
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600906
907 struct global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
908 if (gnvs == NULL)
909 return;
910
911 gnvs->aoac.ic0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C0);
912 gnvs->aoac.ic1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C1);
913 gnvs->aoac.ic2e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C2);
914 gnvs->aoac.ic3e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C3);
915 gnvs->aoac.ut0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART0);
916 gnvs->aoac.ut1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART1);
917 gnvs->aoac.ehce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB2);
918 gnvs->aoac.xhce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB3);
919 /* Rely on these being in sync with devicetree */
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300920 sd = pcidev_path_on_root(SD_DEVFN);
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600921 gnvs->aoac.st_e = sd && sd->enabled ? 1 : 0;
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300922 sata = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600923 gnvs->aoac.sd_e = sata && sata->enabled ? 1 : 0;
924 gnvs->aoac.espi = 1;
925
926 amdfw_rom = 0x20000 - (0x80000 << CONFIG_AMD_FWM_POSITION_INDEX);
927 xhci_fw = read32((void *)(amdfw_rom + XHCI_FW_SIG_OFFSET));
928
929 fwaddr = 2 + read16((void *)(xhci_fw + XHCI_FW_ADDR_OFFSET
930 + XHCI_FW_BOOTRAM_SIZE));
931 fwsize = read16((void *)(xhci_fw + XHCI_FW_SIZE_OFFSET
932 + XHCI_FW_BOOTRAM_SIZE));
933 gnvs->fw00 = 0;
934 gnvs->fw01 = ((32 * KiB) << 16) + 0;
935 gnvs->fw02 = fwaddr + XHCI_FW_BOOTRAM_SIZE;
936 gnvs->fw03 = fwsize << 16;
937
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600938 gnvs->eh10 = pci_read_config32(SOC_EHCI1_DEV, PCI_BASE_ADDRESS_0)
939 & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
940}
941
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600942void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600943{
Richard Spiegel6a389142018-03-05 14:28:10 -0700944 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
945
Richard Spiegel6a389142018-03-05 14:28:10 -0700946 if (IS_ENABLED(CONFIG_MAINBOARD_POWER_RESTORE))
947 restored_power = PM_RESTORE_S0_IF_PREV_S0;
948 pm_write8(PM_RTC_SHADOW, restored_power);
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600949
950 set_sb_final_nvs();
Marc Jones24484842017-05-04 21:17:45 -0600951}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600952
953/*
954 * Update the PCI devices with a valid IRQ number
955 * that is set in the mainboard PCI_IRQ structures.
956 */
957static void set_pci_irqs(void *unused)
958{
959 /* Write PCI_INTR regs 0xC00/0xC01 */
960 write_pci_int_table();
961
962 /* Write IRQs for all devicetree enabled devices */
963 write_pci_cfg_irqs();
964}
965
966/*
967 * Hook this function into the PCI state machine
968 * on entry into BS_DEV_ENABLE.
969 */
970BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);