blob: 9c546947e34f13fd542fd43339ea0f58c53c7303 [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020019#include <device/mmio.h>
Marshall Dawson8a906df2017-06-13 14:19:02 -060020#include <bootstate.h>
Marshall Dawsone9b862e2017-09-22 15:14:46 -060021#include <cpu/x86/smm.h>
Marc Jones24484842017-05-04 21:17:45 -060022#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include <device/pci_ops.h>
26#include <cbmem.h>
Marshall Dawson70f051f2018-03-20 10:27:41 -060027#include <elog.h>
Richard Spiegel2bbc3dc2017-12-06 16:14:58 -070028#include <amdblocks/amd_pci_util.h>
Richard Spiegel71081072018-07-26 10:51:38 -070029#include <amdblocks/agesawrapper.h>
Nico Huber73c11192018-10-06 18:20:47 +020030#include <amdblocks/reset.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060031#include <soc/southbridge.h>
Richard Spiegelb40e1932018-10-24 12:51:21 -070032#include <soc/smbus.h>
Marc Jones24484842017-05-04 21:17:45 -060033#include <soc/smi.h>
Richard Spiegel376dc822017-12-01 08:24:26 -070034#include <soc/amd_pci_int_defs.h>
Richard Spiegelbec44f22017-11-24 07:41:29 -070035#include <delay.h>
36#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070037#include <agesa_headers.h>
Richard Spiegeldbee8ae2018-05-09 17:34:04 -070038#include <soc/nvs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070039
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070040/*
41 * Table of devices that need their AOAC registers enabled and waited
42 * upon (usually about .55 milliseconds). Instead of individual delays
43 * waiting for each device to become available, a single delay will be
Richard Spiegel6dfbb592018-03-15 15:45:44 -070044 * executed.
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070045 */
46const static struct stoneyridge_aoac aoac_devs[] = {
47 { (FCH_AOAC_D3_CONTROL_UART0 + CONFIG_UART_FOR_CONSOLE * 2),
48 (FCH_AOAC_D3_STATE_UART0 + CONFIG_UART_FOR_CONSOLE * 2) },
49 { FCH_AOAC_D3_CONTROL_AMBA, FCH_AOAC_D3_STATE_AMBA },
50 { FCH_AOAC_D3_CONTROL_I2C0, FCH_AOAC_D3_STATE_I2C0 },
51 { FCH_AOAC_D3_CONTROL_I2C1, FCH_AOAC_D3_STATE_I2C1 },
52 { FCH_AOAC_D3_CONTROL_I2C2, FCH_AOAC_D3_STATE_I2C2 },
53 { FCH_AOAC_D3_CONTROL_I2C3, FCH_AOAC_D3_STATE_I2C3 }
54};
55
Marshall Dawson2942db62017-12-14 10:00:27 -070056static int is_sata_config(void)
57{
Richard Spiegelbdd272a2018-10-16 13:53:05 -070058 return !((SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE)
59 || (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE));
Marshall Dawson2942db62017-12-14 10:00:27 -070060}
61
Richard Spiegel7ea8e022018-01-16 14:40:10 -070062static inline int sb_sata_enable(void)
63{
64 /* True if IDE or AHCI. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070065 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
66 (SataAhci == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070067}
68
69static inline int sb_ide_enable(void)
70{
71 /* True if IDE or LEGACY IDE. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070072 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
73 (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070074}
75
Marshall Dawson2942db62017-12-14 10:00:27 -070076void SetFchResetParams(FCH_RESET_INTERFACE *params)
77{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030078 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Julius Wernercd49cce2019-03-05 16:53:33 -080079 params->Xhci0Enable = CONFIG(STONEYRIDGE_XHCI_ENABLE);
Richard Spiegelbb18b432018-08-03 10:37:28 -070080 if (dev && dev->enabled) {
81 params->SataEnable = sb_sata_enable();
82 params->IdeEnable = sb_ide_enable();
83 } else {
84 params->SataEnable = FALSE;
85 params->IdeEnable = FALSE;
86 }
Marshall Dawson2942db62017-12-14 10:00:27 -070087}
88
89void SetFchEnvParams(FCH_INTERFACE *params)
90{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030091 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070092 params->AzaliaController = AzEnable;
93 params->SataClass = CONFIG_STONEYRIDGE_SATA_MODE;
Richard Spiegelbb18b432018-08-03 10:37:28 -070094 if (dev && dev->enabled) {
95 params->SataEnable = is_sata_config();
96 params->IdeEnable = !params->SataEnable;
97 params->SataIdeMode = (CONFIG_STONEYRIDGE_SATA_MODE ==
98 SataLegacyIde);
99 } else {
100 params->SataEnable = FALSE;
101 params->IdeEnable = FALSE;
102 params->SataIdeMode = FALSE;
103 }
Marshall Dawson2942db62017-12-14 10:00:27 -0700104}
105
106void SetFchMidParams(FCH_INTERFACE *params)
107{
108 SetFchEnvParams(params);
109}
Marc Jones24484842017-05-04 21:17:45 -0600110
Richard Spiegel376dc822017-12-01 08:24:26 -0700111/*
112 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100113 * provides a visible association with the index, therefore helping
Richard Spiegel376dc822017-12-01 08:24:26 -0700114 * maintainability of table. If a new index/name is defined in
115 * amd_pci_int_defs.h, just add the pair at the end of this table.
116 * Order is not important.
117 */
118const static struct irq_idx_name irq_association[] = {
Richard Spiegele89d4442017-12-08 07:52:42 -0700119 { PIRQ_A, "INTA#" },
120 { PIRQ_B, "INTB#" },
121 { PIRQ_C, "INTC#" },
122 { PIRQ_D, "INTD#" },
123 { PIRQ_E, "INTE#" },
124 { PIRQ_F, "INTF#" },
125 { PIRQ_G, "INTG#" },
126 { PIRQ_H, "INTH#" },
127 { PIRQ_MISC, "Misc" },
128 { PIRQ_MISC0, "Misc0" },
129 { PIRQ_MISC1, "Misc1" },
130 { PIRQ_MISC2, "Misc2" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700131 { PIRQ_SIRQA, "Ser IRQ INTA" },
132 { PIRQ_SIRQB, "Ser IRQ INTB" },
133 { PIRQ_SIRQC, "Ser IRQ INTC" },
134 { PIRQ_SIRQD, "Ser IRQ INTD" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700135 { PIRQ_SCI, "SCI" },
136 { PIRQ_SMBUS, "SMBUS" },
137 { PIRQ_ASF, "ASF" },
138 { PIRQ_HDA, "HDA" },
139 { PIRQ_FC, "FC" },
140 { PIRQ_PMON, "PerMon" },
141 { PIRQ_SD, "SD" },
142 { PIRQ_SDIO, "SDIOt" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700143 { PIRQ_EHCI, "EHCI" },
144 { PIRQ_XHCI, "XHCI" },
145 { PIRQ_SATA, "SATA" },
146 { PIRQ_GPIO, "GPIO" },
147 { PIRQ_I2C0, "I2C0" },
148 { PIRQ_I2C1, "I2C1" },
149 { PIRQ_I2C2, "I2C2" },
150 { PIRQ_I2C3, "I2C3" },
151 { PIRQ_UART0, "UART0" },
152 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700153};
154
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700155/*
156 * Structure to simplify code obtaining the total of used wide IO
157 * registers and the size assigned to each.
158 */
159static struct wide_io_ioport_and_bits {
160 uint32_t enable;
161 uint16_t port;
162 uint8_t alt;
163} wio_io_en[TOTAL_WIDEIO_PORTS] = {
164 {
165 LPC_WIDEIO0_ENABLE,
166 LPC_WIDEIO_GENERIC_PORT,
167 LPC_ALT_WIDEIO0_ENABLE
168 },
169 {
170 LPC_WIDEIO1_ENABLE,
171 LPC_WIDEIO1_GENERIC_PORT,
172 LPC_ALT_WIDEIO1_ENABLE
173 },
174 {
175 LPC_WIDEIO2_ENABLE,
176 LPC_WIDEIO2_GENERIC_PORT,
177 LPC_ALT_WIDEIO2_ENABLE
178 }
179};
180
Richard Spiegel376dc822017-12-01 08:24:26 -0700181const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
182{
183 *size = ARRAY_SIZE(irq_association);
184 return irq_association;
185}
186
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700187/**
188 * @brief Find the size of a particular wide IO
189 *
190 * @param index = index of desired wide IO
191 *
192 * @return size of desired wide IO
193 */
194uint16_t sb_wideio_size(int index)
195{
196 uint32_t enable_register;
197 uint16_t size = 0;
198 uint8_t alternate_register;
199
200 if (index >= TOTAL_WIDEIO_PORTS)
201 return size;
202 enable_register = pci_read_config32(SOC_LPC_DEV,
203 LPC_IO_OR_MEM_DECODE_ENABLE);
204 alternate_register = pci_read_config8(SOC_LPC_DEV,
205 LPC_ALT_WIDEIO_RANGE_ENABLE);
206 if (enable_register & wio_io_en[index].enable)
207 size = (alternate_register & wio_io_en[index].alt) ?
208 16 : 512;
209 return size;
210}
211
212/**
213 * @brief Identify if any LPC wide IO is covering the IO range
214 *
215 * @param start = start of IO range
216 * @param size = size of IO range
217 *
218 * @return Index of wide IO covering the range or error
219 */
220int sb_find_wideio_range(uint16_t start, uint16_t size)
221{
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700222 int i, index = WIDEIO_RANGE_ERROR;
223 uint16_t end, current_size, start_wideio, end_wideio;
224
225 end = start + size;
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700226 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
227 current_size = sb_wideio_size(i);
228 if (current_size == 0)
229 continue;
230 start_wideio = pci_read_config16(SOC_LPC_DEV,
231 wio_io_en[i].port);
232 end_wideio = start_wideio + current_size;
233 if ((start >= start_wideio) && (end <= end_wideio)) {
234 index = i;
235 break;
236 }
237 }
238 return index;
239}
240
241/**
242 * @brief Program a LPC wide IO to support an IO range
243 *
244 * @param start = start of range to be routed through wide IO
245 * @param size = size of range to be routed through wide IO
246 *
247 * @return Index of wide IO register used or error
248 */
249int sb_set_wideio_range(uint16_t start, uint16_t size)
250{
251 int i, index = WIDEIO_RANGE_ERROR;
252 uint32_t enable_register;
253 uint8_t alternate_register;
254
255 enable_register = pci_read_config32(SOC_LPC_DEV,
256 LPC_IO_OR_MEM_DECODE_ENABLE);
257 alternate_register = pci_read_config8(SOC_LPC_DEV,
258 LPC_ALT_WIDEIO_RANGE_ENABLE);
259 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
260 if (enable_register & wio_io_en[i].enable)
261 continue;
262 index = i;
263 pci_write_config16(SOC_LPC_DEV, wio_io_en[i].port, start);
264 enable_register |= wio_io_en[i].enable;
265 pci_write_config32(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE,
266 enable_register);
267 if (size <= 16)
268 alternate_register |= wio_io_en[i].alt;
269 else
270 alternate_register &= ~wio_io_en[i].alt;
271 pci_write_config8(SOC_LPC_DEV,
272 LPC_ALT_WIDEIO_RANGE_ENABLE,
273 alternate_register);
274 break;
275 }
276 return index;
277}
278
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600279static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700280{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600281 uint8_t byte;
282 uint8_t *register_pointer = (uint8_t *)(uintptr_t)AOAC_MMIO_BASE
283 + aoac_device_control_register;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700284
285 /* Power on the UART and AMBA devices */
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600286 byte = read8(register_pointer);
287 byte |= FCH_AOAC_PWR_ON_DEV;
288 write8(register_pointer, byte);
289}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700290
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600291static bool is_aoac_device_enabled(int aoac_device_status_register)
292{
293 uint8_t byte;
294 byte = read8((uint8_t *)(uintptr_t)AOAC_MMIO_BASE
295 + aoac_device_status_register);
296 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
297 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
298 return true;
299 else
300 return false;
301}
302
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700303void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600304{
305 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700306 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600307
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700308 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
309 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700310
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700311 /* Wait for AOAC devices to indicate power and clock OK */
312 do {
313 udelay(100);
314 status = true;
315 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
316 status &= is_aoac_device_enabled(aoac_devs[i].status);
317 } while (!status);
318}
319
Richard Spiegelbec44f22017-11-24 07:41:29 -0700320void sb_pci_port80(void)
321{
322 u8 byte;
323
324 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
325 byte &= ~DECODE_IO_PORT_ENABLE4_H; /* disable lpc port 80 */
326 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
327}
328
329void sb_lpc_port80(void)
330{
331 u8 byte;
332
333 /* Enable LPC controller */
334 outb(PM_LPC_GATING, PM_INDEX);
335 byte = inb(PM_DATA);
336 byte |= PM_LPC_ENABLE;
337 outb(PM_LPC_GATING, PM_INDEX);
338 outb(byte, PM_DATA);
339
340 /* Enable port 80 LPC decode in pci function 3 configuration space. */
341 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
342 byte |= DECODE_IO_PORT_ENABLE4_H; /* enable port 80 */
343 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
344}
345
346void sb_lpc_decode(void)
347{
348 u32 tmp = 0;
349
350 /* Enable I/O decode to LPC bus */
351 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
352 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
353 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
354 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
355 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
356 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
357 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
358 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
359 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
360 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
361 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
362 | DECODE_ENABLE_ADLIB_PORT;
363
364 pci_write_config32(SOC_LPC_DEV, LPC_IO_PORT_DECODE_ENABLE, tmp);
365}
366
Garrett Kirkendall9858bd22018-03-07 15:38:14 -0600367void sb_acpi_mmio_decode(void)
368{
369 uint8_t byte;
370
371 /* Enable ACPI MMIO range 0xfed80000 - 0xfed81fff */
372 outb(PM_ISA_CONTROL, PM_INDEX);
373 byte = inb(PM_DATA);
374 byte |= MMIO_EN;
375 outb(PM_ISA_CONTROL, PM_INDEX);
376 outb(byte, PM_DATA);
377}
378
Raul E Rangel5b058232018-06-28 16:31:45 -0600379static void sb_enable_cf9_io(void)
380{
381 uint32_t reg = pm_read32(PM_DECODE_EN);
382
383 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
384}
385
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600386static void sb_enable_legacy_io(void)
387{
388 uint32_t reg = pm_read32(PM_DECODE_EN);
389
390 pm_write32(PM_DECODE_EN, reg | LEGACY_IO_EN);
391}
392
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700393void sb_clk_output_48Mhz(u32 osc)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700394{
395 u32 ctrl;
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600396 u32 *misc_clk_cntl_1_ptr = (u32 *)(uintptr_t)(MISC_MMIO_BASE
Richard Spiegel62052212018-10-17 13:32:58 -0700397 + MISC_CLK_CNTL1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700398
399 /*
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700400 * Clear the disable for OSCOUT1 (signal typically named XnnM_25M_48M)
401 * or OSCOUT2 (USBCLK/25M_48M_OSC). The frequency defaults to 48MHz.
Richard Spiegelbec44f22017-11-24 07:41:29 -0700402 */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600403 ctrl = read32(misc_clk_cntl_1_ptr);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700404
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700405 switch (osc) {
406 case 1:
407 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
408 break;
409 case 2:
410 ctrl &= ~OSCOUT2_CLK_OUTPUT_ENB;
411 break;
412 default:
413 return; /* do nothing if invalid */
414 }
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600415 write32(misc_clk_cntl_1_ptr, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700416}
417
418static uintptr_t sb_spibase(void)
419{
420 u32 base, enables;
421
422 /* Make sure the base address is predictable */
423 base = pci_read_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER);
Patrick Georgi4fbefc52018-10-23 14:35:37 +0200424 enables = base & SPI_PRESERVE_BITS;
425 base &= ~(SPI_PRESERVE_BITS | SPI_BASE_RESERVED);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700426
427 if (!base) {
428 base = SPI_BASE_ADDRESS;
429 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER,
430 base | enables | SPI_ROM_ENABLE);
431 /* PCI_COMMAND_MEMORY is read-only and enabled. */
432 }
433 return (uintptr_t)base;
434}
435
436void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
437{
438 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700439 write16((void *)(base + SPI100_SPEED_CONFIG),
Richard Spiegelbec44f22017-11-24 07:41:29 -0700440 (norm << SPI_NORM_SPEED_NEW_SH) |
441 (fast << SPI_FAST_SPEED_NEW_SH) |
442 (alt << SPI_ALT_SPEED_NEW_SH) |
443 (tpm << SPI_TPM_SPEED_NEW_SH));
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700444 write16((void *)(base + SPI100_ENABLE), SPI_USE_SPI100);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700445}
446
447void sb_disable_4dw_burst(void)
448{
449 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700450 write16((void *)(base + SPI100_HOST_PREF_CONFIG),
451 read16((void *)(base + SPI100_HOST_PREF_CONFIG))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700452 & ~SPI_RD4DW_EN_HOST);
453}
454
Richard Spiegelbec44f22017-11-24 07:41:29 -0700455void sb_read_mode(u32 mode)
456{
457 uintptr_t base = sb_spibase();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700458 write32((void *)(base + SPI_CNTRL0),
459 (read32((void *)(base + SPI_CNTRL0))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700460 & ~SPI_READ_MODE_MASK) | mode);
461}
462
Garrett Kirkendall65753062018-03-07 16:12:11 -0600463/*
464 * Enable FCH to decode TPM associated Memory and IO regions
465 *
466 * Enable decoding of TPM cycles defined in TPM 1.2 spec
467 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
468 * 0x7e and 0xef-0xee.
469 * This function should be called if TPM is connected in any way to the FCH and
470 * conforms to the regions decoded.
471 * Absent any other routing configuration the TPM cycles will be claimed by the
472 * LPC bus
473 */
474void sb_tpm_decode(void)
475{
476 u32 value;
477
478 value = pci_read_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE);
479 value |= TPM_12_EN | TPM_LEGACY_EN;
480 pci_write_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE, value);
481}
482
483/*
484 * Enable FCH to decode TPM associated Memory and IO regions to SPI
485 *
486 * This should be used if TPM is connected to SPI bus.
487 * Assumes SPI address space is already configured via a call to sb_spibase().
488 */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700489void sb_tpm_decode_spi(void)
490{
Garrett Kirkendall65753062018-03-07 16:12:11 -0600491 /* Enable TPM decoding to FCH */
492 sb_tpm_decode();
493
494 /* Route TPM accesses to SPI */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700495 u32 spibase = pci_read_config32(SOC_LPC_DEV,
496 SPIROM_BASE_ADDRESS_REGISTER);
497 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER, spibase
498 | ROUTE_TPM_2_SPI);
499}
500
501/*
502 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
503 *
504 * Hardware should enable LPC ROM by pin straps. This function does not
505 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
506 *
507 * The southbridge power-on default is to map 512K ROM space.
508 *
509 */
510void sb_enable_rom(void)
511{
512 u8 reg8;
513
514 /*
515 * Decode variable LPC ROM address ranges 1 and 2.
516 * Bits 3-4 are not defined in any publicly available datasheet
517 */
518 reg8 = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
519 reg8 |= (1 << 3) | (1 << 4);
520 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, reg8);
521
522 /*
523 * LPC ROM address range 1:
524 * Enable LPC ROM range mirroring start at 0x000e(0000).
525 */
526 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_START, 0x000e);
527
528 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
529 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_END, 0x000f);
530
531 /*
532 * LPC ROM address range 2:
533 *
534 * Enable LPC ROM range start at:
535 * 0xfff8(0000): 512KB
536 * 0xfff0(0000): 1MB
537 * 0xffe0(0000): 2MB
538 * 0xffc0(0000): 4MB
539 */
540 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START, 0x10000
541 - (CONFIG_COREBOOT_ROMSIZE_KB >> 6));
542
543 /* Enable LPC ROM range end at 0xffff(ffff). */
544 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_END, 0xffff);
545}
546
Marc Jonescfb16802018-04-20 16:27:41 -0600547static void sb_lpc_early_setup(void)
548{
549 uint32_t dword;
550
551 /* Enable SPI prefetch */
552 dword = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
553 dword |= SPI_FROM_HOST_PREFETCH_EN | SPI_FROM_USB_PREFETCH_EN;
554 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, dword);
555
Julius Wernercd49cce2019-03-05 16:53:33 -0800556 if (CONFIG(STONEYRIDGE_LEGACY_FREE)) {
Marc Jonescfb16802018-04-20 16:27:41 -0600557 /* Decode SIOs at 2E/2F and 4E/4F */
558 dword = pci_read_config32(SOC_LPC_DEV,
559 LPC_IO_OR_MEM_DECODE_ENABLE);
560 dword |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
561 pci_write_config32(SOC_LPC_DEV,
562 LPC_IO_OR_MEM_DECODE_ENABLE, dword);
563 }
564}
565
Raul E Rangel79053412018-08-06 10:40:02 -0600566static void setup_spread_spectrum(int *reboot)
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600567{
568 uint16_t rstcfg = pm_read16(PWR_RESET_CFG);
569
570 rstcfg &= ~TOGGLE_ALL_PWR_GOOD;
571 pm_write16(PWR_RESET_CFG, rstcfg);
572
573 uint32_t cntl1 = misc_read32(MISC_CLK_CNTL1);
574
575 if (cntl1 & CG1PLL_FBDIV_TEST) {
576 printk(BIOS_DEBUG, "Spread spectrum is ready\n");
577 misc_write32(MISC_CGPLL_CONFIG1,
578 misc_read32(MISC_CGPLL_CONFIG1) |
579 CG1PLL_SPREAD_SPECTRUM_ENABLE);
580
581 return;
582 }
583
584 printk(BIOS_DEBUG, "Setting up spread spectrum\n");
585
586 uint32_t cfg6 = misc_read32(MISC_CGPLL_CONFIG6);
587 cfg6 &= ~CG1PLL_LF_MODE_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600588 cfg6 |= (0x0f8 << CG1PLL_LF_MODE_SHIFT) & CG1PLL_LF_MODE_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600589 misc_write32(MISC_CGPLL_CONFIG6, cfg6);
590
591 uint32_t cfg3 = misc_read32(MISC_CGPLL_CONFIG3);
592 cfg3 &= ~CG1PLL_REFDIV_MASK;
593 cfg3 |= (0x003 << CG1PLL_REFDIV_SHIFT) & CG1PLL_REFDIV_MASK;
594 cfg3 &= ~CG1PLL_FBDIV_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600595 cfg3 |= (0x04b << CG1PLL_FBDIV_SHIFT) & CG1PLL_FBDIV_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600596 misc_write32(MISC_CGPLL_CONFIG3, cfg3);
597
598 uint32_t cfg5 = misc_read32(MISC_CGPLL_CONFIG5);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600599 cfg5 &= ~SS_AMOUNT_NFRAC_SLIP_MASK;
600 cfg5 |= (0x2 << SS_AMOUNT_NFRAC_SLIP_SHIFT) & SS_AMOUNT_NFRAC_SLIP_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600601 misc_write32(MISC_CGPLL_CONFIG5, cfg5);
602
603 uint32_t cfg4 = misc_read32(MISC_CGPLL_CONFIG4);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600604 cfg4 &= ~SS_AMOUNT_DSFRAC_MASK;
605 cfg4 |= (0xd000 << SS_AMOUNT_DSFRAC_SHIFT) & SS_AMOUNT_DSFRAC_MASK;
606 cfg4 &= ~SS_STEP_SIZE_DSFRAC_MASK;
607 cfg4 |= (0x02d5 << SS_STEP_SIZE_DSFRAC_SHIFT)
608 & SS_STEP_SIZE_DSFRAC_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600609 misc_write32(MISC_CGPLL_CONFIG4, cfg4);
610
611 rstcfg |= TOGGLE_ALL_PWR_GOOD;
612 pm_write16(PWR_RESET_CFG, rstcfg);
613
614 cntl1 |= CG1PLL_FBDIV_TEST;
615 misc_write32(MISC_CLK_CNTL1, cntl1);
616
Raul E Rangel79053412018-08-06 10:40:02 -0600617 *reboot = 1;
618}
619
620static void setup_misc(int *reboot)
621{
622 /* Undocumented register */
623 uint32_t reg = misc_read32(0x50);
624 if (!(reg & BIT(16))) {
625 reg |= BIT(16);
626
627 misc_write32(0x50, reg);
628 *reboot = 1;
629 }
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600630}
631
Richard Spiegelb40e1932018-10-24 12:51:21 -0700632static void fch_smbus_init(void)
633{
634 pm_write8(SMB_ASF_IO_BASE, SMB_BASE_ADDR >> 8);
635 smbus_write8(SMBUS_MMIO_BASE, SMBTIMING, SMB_SPEED_400KHZ);
636 /* Clear all SMBUS status bits */
637 smbus_write8(SMBUS_MMIO_BASE, SMBHSTSTAT, SMBHST_STAT_CLEAR);
638 smbus_write8(SMBUS_MMIO_BASE, SMBSLVSTAT, SMBSLV_STAT_CLEAR);
639 smbus_write8(ASF_MMIO_BASE, SMBHSTSTAT, SMBHST_STAT_CLEAR);
640 smbus_write8(ASF_MMIO_BASE, SMBSLVSTAT, SMBSLV_STAT_CLEAR);
641}
642
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600643/* Before console init */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700644void bootblock_fch_early_init(void)
645{
Raul E Rangel79053412018-08-06 10:40:02 -0600646 int reboot = 0;
647
Richard Spiegelbec44f22017-11-24 07:41:29 -0700648 sb_enable_rom();
649 sb_lpc_port80();
650 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600651 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500652 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600653 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500654 sb_acpi_mmio_decode();
Richard Spiegelb40e1932018-10-24 12:51:21 -0700655 fch_smbus_init();
Raul E Rangel5b058232018-06-28 16:31:45 -0600656 sb_enable_cf9_io();
Raul E Rangel79053412018-08-06 10:40:02 -0600657 setup_spread_spectrum(&reboot);
658 setup_misc(&reboot);
659
660 if (reboot)
Nico Huber73c11192018-10-06 18:20:47 +0200661 warm_reset();
Raul E Rangel79053412018-08-06 10:40:02 -0600662
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600663 sb_enable_legacy_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700664 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700665}
666
Edward Hillcc680342018-08-10 16:20:02 -0600667static void print_num_status_bits(int num_bits, uint32_t status,
668 const char *const bit_names[])
669{
670 int i;
671
672 if (!status)
673 return;
674
675 for (i = num_bits - 1; i >= 0; i--) {
676 if (status & (1 << i)) {
677 if (bit_names[i])
678 printk(BIOS_DEBUG, "%s ", bit_names[i]);
679 else
680 printk(BIOS_DEBUG, "BIT%d ", i);
681 }
682 }
683}
684
685static void sb_print_pmxc0_status(void)
686{
687 /* PMxC0 S5/Reset Status shows the source of previous reset. */
688 uint32_t pmxc0_status = pm_read32(PM_RST_STATUS);
689
Edward Hill917b4002018-10-02 14:17:19 -0600690 static const char *const pmxc0_status_bits[32] = {
Edward Hillcc680342018-08-10 16:20:02 -0600691 [0] = "ThermalTrip",
692 [1] = "FourSecondPwrBtn",
693 [2] = "Shutdown",
694 [3] = "ThermalTripFromTemp",
695 [4] = "RemotePowerDownFromASF",
696 [5] = "ShutDownFan0",
697 [16] = "UserRst",
698 [17] = "SoftPciRst",
699 [18] = "DoInit",
700 [19] = "DoReset",
701 [20] = "DoFullReset",
702 [21] = "SleepReset",
703 [22] = "KbReset",
704 [23] = "LtReset",
705 [24] = "FailBootRst",
706 [25] = "WatchdogIssueReset",
707 [26] = "RemoteResetFromASF",
708 [27] = "SyncFlood",
709 [28] = "HangReset",
710 [29] = "EcWatchdogRst",
Edward Hillcc680342018-08-10 16:20:02 -0600711 };
712
Edward Hill917b4002018-10-02 14:17:19 -0600713 printk(BIOS_DEBUG, "PMxC0 STATUS: 0x%x ", pmxc0_status);
Edward Hillcc680342018-08-10 16:20:02 -0600714 print_num_status_bits(ARRAY_SIZE(pmxc0_status_bits), pmxc0_status,
715 pmxc0_status_bits);
Edward Hill917b4002018-10-02 14:17:19 -0600716 printk(BIOS_DEBUG, "\n");
Edward Hillcc680342018-08-10 16:20:02 -0600717}
718
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600719/* After console init */
Edward Hillcc680342018-08-10 16:20:02 -0600720void bootblock_fch_init(void)
721{
722 sb_print_pmxc0_status();
723}
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600724
Elyes HAOUASc5ad2672018-12-05 10:58:34 +0100725void sb_enable(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600726{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600727 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600728}
729
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600730static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600731{
Marshall Dawson91b80412017-09-27 16:44:40 -0600732 u32 reg;
733
Marc Jones24484842017-05-04 21:17:45 -0600734 /* We use some of these ports in SMM regardless of whether or not
735 * ACPI tables are generated. Enable these ports indiscriminately.
736 */
737
738 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
739 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
740 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
741 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
742 /* CpuControl is in \_PR.CP00, 6 bytes */
743 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
744
Julius Wernercd49cce2019-03-05 16:53:33 -0800745 if (CONFIG(HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600746 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600747 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600748 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600749
750 /* SMI on SlpTyp requires sending SMI before completion
751 * response of the I/O write. The BKDG also specifies
752 * clearing ForceStpClkRetry for SMI trapping.
753 */
754 reg = pm_read32(PM_PCI_CTRL);
755 reg |= FORCE_SLPSTATE_RETRY;
756 reg &= ~FORCE_STPCLK_RETRY;
757 pm_write32(PM_PCI_CTRL, reg);
758
759 /* Disable SlpTyp feature */
760 reg = pm_read8(PM_RST_CTRL1);
761 reg &= ~SLPTYPE_CONTROL_EN;
762 pm_write8(PM_RST_CTRL1, reg);
763
764 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600765 } else {
766 pm_write16(PM_ACPI_SMI_CMD, 0);
767 }
768
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700769 /* Decode ACPI registers and enable standard features */
770 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
771 PM_ACPI_GLOBAL_EN |
772 PM_ACPI_RTC_EN_EN |
773 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600774}
775
Marshall Dawson70f051f2018-03-20 10:27:41 -0600776static uint16_t reset_pm1_status(void)
777{
Richard Spiegele24d7952018-10-26 13:25:01 -0700778 uint16_t pm1_sts = acpi_read16(MMIO_ACPI_PM1_STS);
779 acpi_write16(MMIO_ACPI_PM1_STS, pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600780 return pm1_sts;
781}
782
783static uint16_t print_pm1_status(uint16_t pm1_sts)
784{
Edward Hill917b4002018-10-02 14:17:19 -0600785 static const char *const pm1_sts_bits[16] = {
Marshall Dawson70f051f2018-03-20 10:27:41 -0600786 [0] = "TMROF",
787 [4] = "BMSTATUS",
788 [5] = "GBL",
789 [8] = "PWRBTN",
790 [10] = "RTC",
791 [14] = "PCIEXPWAK",
792 [15] = "WAK",
793 };
794
795 if (!pm1_sts)
796 return 0;
797
Edward Hill917b4002018-10-02 14:17:19 -0600798 printk(BIOS_DEBUG, "PM1_STS: ");
Marshall Dawson70f051f2018-03-20 10:27:41 -0600799 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
Edward Hill917b4002018-10-02 14:17:19 -0600800 printk(BIOS_DEBUG, "\n");
Marshall Dawson70f051f2018-03-20 10:27:41 -0600801
802 return pm1_sts;
803}
804
805static void sb_log_pm1_status(uint16_t pm1_sts)
806{
Julius Wernercd49cce2019-03-05 16:53:33 -0800807 if (!CONFIG(ELOG))
Marshall Dawson70f051f2018-03-20 10:27:41 -0600808 return;
809
Daniel Kurtzb6fdd222018-05-24 15:52:45 -0600810 if (pm1_sts & WAK_STS)
811 elog_add_event_byte(ELOG_TYPE_ACPI_WAKE,
812 acpi_is_wakeup_s3() ? ACPI_S3 : ACPI_S5);
813
Marshall Dawson70f051f2018-03-20 10:27:41 -0600814 if (pm1_sts & PWRBTN_STS)
815 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
816
817 if (pm1_sts & RTC_STS)
818 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
819
820 if (pm1_sts & PCIEXPWAK_STS)
821 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
822}
823
Richard Spiegel572f4982018-05-25 15:49:33 -0700824static void sb_save_sws(uint16_t pm1_status)
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700825{
Richard Spiegel35282a02018-06-14 14:57:54 -0700826 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700827 uint32_t reg32;
Richard Spiegel35282a02018-06-14 14:57:54 -0700828 uint16_t reg16;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700829
Richard Spiegel35282a02018-06-14 14:57:54 -0700830 sws = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(struct soc_power_reg));
831 if (sws == NULL)
832 return;
833 sws->pm1_sts = pm1_status;
Richard Spiegele24d7952018-10-26 13:25:01 -0700834 sws->pm1_en = acpi_read16(MMIO_ACPI_PM1_EN);
835 reg32 = acpi_read32(MMIO_ACPI_GPE0_STS);
836 acpi_write32(MMIO_ACPI_GPE0_STS, reg32);
Richard Spiegel35282a02018-06-14 14:57:54 -0700837 sws->gpe0_sts = reg32;
Richard Spiegele24d7952018-10-26 13:25:01 -0700838 sws->gpe0_en = acpi_read32(MMIO_ACPI_GPE0_EN);
839 reg16 = acpi_read16(MMIO_ACPI_PM1_CNT_BLK);
Richard Spiegel35282a02018-06-14 14:57:54 -0700840 reg16 &= SLP_TYP;
841 sws->wake_from = reg16 >> SLP_TYP_SHIFT;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700842}
843
Marshall Dawson70f051f2018-03-20 10:27:41 -0600844static void sb_clear_pm1_status(void)
845{
846 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700847
848 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600849 sb_log_pm1_status(pm1_sts);
850 print_pm1_status(pm1_sts);
851}
852
Richard Spiegel572f4982018-05-25 15:49:33 -0700853static int get_index_bit(uint32_t value, uint16_t limit)
854{
855 uint16_t i;
856 uint32_t t;
857
Richard Spiegelef73cb82018-06-19 07:40:18 -0700858 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700859 return -1;
860
861 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
862 t = (1 << limit) - 1;
863 if ((value & t) == 0)
864 return -1;
865 t = 1;
866 for (i = 0; i < limit; i++) {
867 if (value & t)
868 break;
869 t <<= 1;
870 }
871 return i;
872}
873
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700874static void set_nvs_sws(void *unused)
875{
Richard Spiegel35282a02018-06-14 14:57:54 -0700876 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700877 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700878 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700879
Richard Spiegel35282a02018-06-14 14:57:54 -0700880 sws = cbmem_find(CBMEM_ID_POWER_STATE);
881 if (sws == NULL)
882 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700883 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
884 if (gnvs == NULL)
885 return;
886
Richard Spiegel35282a02018-06-14 14:57:54 -0700887 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700888 if (index < 0)
889 gnvs->pm1i = ~0ULL;
890 else
891 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700892
Richard Spiegel35282a02018-06-14 14:57:54 -0700893 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700894 if (index < 0)
895 gnvs->gpei = ~0ULL;
896 else
897 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700898}
899
900BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
901
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600902void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600903{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600904 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600905 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600906}
907
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600908static void set_sb_final_nvs(void)
909{
910 uintptr_t amdfw_rom;
911 uintptr_t xhci_fw;
912 uintptr_t fwaddr;
913 size_t fwsize;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700914 const struct device *sd, *sata;
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600915
916 struct global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
917 if (gnvs == NULL)
918 return;
919
920 gnvs->aoac.ic0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C0);
921 gnvs->aoac.ic1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C1);
922 gnvs->aoac.ic2e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C2);
923 gnvs->aoac.ic3e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C3);
924 gnvs->aoac.ut0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART0);
925 gnvs->aoac.ut1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART1);
926 gnvs->aoac.ehce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB2);
927 gnvs->aoac.xhce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB3);
928 /* Rely on these being in sync with devicetree */
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300929 sd = pcidev_path_on_root(SD_DEVFN);
Marshall Dawson6d3b7e62019-04-18 17:01:01 -0600930 gnvs->aoac.sd_e = sd && sd->enabled ? 1 : 0;
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300931 sata = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson6d3b7e62019-04-18 17:01:01 -0600932 gnvs->aoac.st_e = sata && sata->enabled ? 1 : 0;
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600933 gnvs->aoac.espi = 1;
934
935 amdfw_rom = 0x20000 - (0x80000 << CONFIG_AMD_FWM_POSITION_INDEX);
936 xhci_fw = read32((void *)(amdfw_rom + XHCI_FW_SIG_OFFSET));
937
938 fwaddr = 2 + read16((void *)(xhci_fw + XHCI_FW_ADDR_OFFSET
939 + XHCI_FW_BOOTRAM_SIZE));
940 fwsize = read16((void *)(xhci_fw + XHCI_FW_SIZE_OFFSET
941 + XHCI_FW_BOOTRAM_SIZE));
942 gnvs->fw00 = 0;
943 gnvs->fw01 = ((32 * KiB) << 16) + 0;
944 gnvs->fw02 = fwaddr + XHCI_FW_BOOTRAM_SIZE;
945 gnvs->fw03 = fwsize << 16;
946
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600947 gnvs->eh10 = pci_read_config32(SOC_EHCI1_DEV, PCI_BASE_ADDRESS_0)
948 & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
949}
950
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600951void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600952{
Richard Spiegel6a389142018-03-05 14:28:10 -0700953 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
954
Julius Wernercd49cce2019-03-05 16:53:33 -0800955 if (CONFIG(MAINBOARD_POWER_RESTORE))
Richard Spiegel6a389142018-03-05 14:28:10 -0700956 restored_power = PM_RESTORE_S0_IF_PREV_S0;
957 pm_write8(PM_RTC_SHADOW, restored_power);
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600958
959 set_sb_final_nvs();
Marc Jones24484842017-05-04 21:17:45 -0600960}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600961
962/*
963 * Update the PCI devices with a valid IRQ number
964 * that is set in the mainboard PCI_IRQ structures.
965 */
966static void set_pci_irqs(void *unused)
967{
968 /* Write PCI_INTR regs 0xC00/0xC01 */
969 write_pci_int_table();
970
971 /* Write IRQs for all devicetree enabled devices */
972 write_pci_cfg_irqs();
973}
974
975/*
976 * Hook this function into the PCI state machine
977 * on entry into BS_DEV_ENABLE.
978 */
979BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);