blob: f01f5344d74bbec098493df4994075b38ff93bec [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>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060028#include <soc/southbridge.h>
Marc Jones24484842017-05-04 21:17:45 -060029#include <soc/smi.h>
Richard Spiegel376dc822017-12-01 08:24:26 -070030#include <soc/amd_pci_int_defs.h>
Marc Jones24484842017-05-04 21:17:45 -060031#include <fchec.h>
Richard Spiegelbec44f22017-11-24 07:41:29 -070032#include <delay.h>
33#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070034#include <agesa_headers.h>
Richard Spiegeldbee8ae2018-05-09 17:34:04 -070035#include <soc/nvs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070036
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070037/*
38 * Table of devices that need their AOAC registers enabled and waited
39 * upon (usually about .55 milliseconds). Instead of individual delays
40 * waiting for each device to become available, a single delay will be
Richard Spiegel6dfbb592018-03-15 15:45:44 -070041 * executed.
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070042 */
43const static struct stoneyridge_aoac aoac_devs[] = {
44 { (FCH_AOAC_D3_CONTROL_UART0 + CONFIG_UART_FOR_CONSOLE * 2),
45 (FCH_AOAC_D3_STATE_UART0 + CONFIG_UART_FOR_CONSOLE * 2) },
46 { FCH_AOAC_D3_CONTROL_AMBA, FCH_AOAC_D3_STATE_AMBA },
47 { FCH_AOAC_D3_CONTROL_I2C0, FCH_AOAC_D3_STATE_I2C0 },
48 { FCH_AOAC_D3_CONTROL_I2C1, FCH_AOAC_D3_STATE_I2C1 },
49 { FCH_AOAC_D3_CONTROL_I2C2, FCH_AOAC_D3_STATE_I2C2 },
50 { FCH_AOAC_D3_CONTROL_I2C3, FCH_AOAC_D3_STATE_I2C3 }
51};
52
Marshall Dawson2942db62017-12-14 10:00:27 -070053static int is_sata_config(void)
54{
55 return !((CONFIG_STONEYRIDGE_SATA_MODE == SataNativeIde)
56 || (CONFIG_STONEYRIDGE_SATA_MODE == SataLegacyIde));
57}
58
Richard Spiegel7ea8e022018-01-16 14:40:10 -070059static inline int sb_sata_enable(void)
60{
61 /* True if IDE or AHCI. */
62 return (CONFIG_STONEYRIDGE_SATA_MODE == SataNativeIde) ||
63 (CONFIG_STONEYRIDGE_SATA_MODE == SataAhci);
64}
65
66static inline int sb_ide_enable(void)
67{
68 /* True if IDE or LEGACY IDE. */
69 return (CONFIG_STONEYRIDGE_SATA_MODE == SataNativeIde) ||
70 (CONFIG_STONEYRIDGE_SATA_MODE == SataLegacyIde);
71}
72
Marshall Dawson2942db62017-12-14 10:00:27 -070073void SetFchResetParams(FCH_RESET_INTERFACE *params)
74{
75 params->Xhci0Enable = IS_ENABLED(CONFIG_STONEYRIDGE_XHCI_ENABLE);
Richard Spiegela318d282018-01-16 13:25:40 -070076 params->SataEnable = sb_sata_enable();
77 params->IdeEnable = sb_ide_enable();
Marshall Dawson2942db62017-12-14 10:00:27 -070078}
79
80void SetFchEnvParams(FCH_INTERFACE *params)
81{
82 params->AzaliaController = AzEnable;
83 params->SataClass = CONFIG_STONEYRIDGE_SATA_MODE;
84 params->SataEnable = is_sata_config();
85 params->IdeEnable = !params->SataEnable;
86 params->SataIdeMode = (CONFIG_STONEYRIDGE_SATA_MODE == SataLegacyIde);
87}
88
89void SetFchMidParams(FCH_INTERFACE *params)
90{
91 SetFchEnvParams(params);
92}
Marc Jones24484842017-05-04 21:17:45 -060093
Richard Spiegel376dc822017-12-01 08:24:26 -070094/*
95 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +010096 * provides a visible association with the index, therefore helping
Richard Spiegel376dc822017-12-01 08:24:26 -070097 * maintainability of table. If a new index/name is defined in
98 * amd_pci_int_defs.h, just add the pair at the end of this table.
99 * Order is not important.
100 */
101const static struct irq_idx_name irq_association[] = {
Richard Spiegele89d4442017-12-08 07:52:42 -0700102 { PIRQ_A, "INTA#" },
103 { PIRQ_B, "INTB#" },
104 { PIRQ_C, "INTC#" },
105 { PIRQ_D, "INTD#" },
106 { PIRQ_E, "INTE#" },
107 { PIRQ_F, "INTF#" },
108 { PIRQ_G, "INTG#" },
109 { PIRQ_H, "INTH#" },
110 { PIRQ_MISC, "Misc" },
111 { PIRQ_MISC0, "Misc0" },
112 { PIRQ_MISC1, "Misc1" },
113 { PIRQ_MISC2, "Misc2" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700114 { PIRQ_SIRQA, "Ser IRQ INTA" },
115 { PIRQ_SIRQB, "Ser IRQ INTB" },
116 { PIRQ_SIRQC, "Ser IRQ INTC" },
117 { PIRQ_SIRQD, "Ser IRQ INTD" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700118 { PIRQ_SCI, "SCI" },
119 { PIRQ_SMBUS, "SMBUS" },
120 { PIRQ_ASF, "ASF" },
121 { PIRQ_HDA, "HDA" },
122 { PIRQ_FC, "FC" },
123 { PIRQ_PMON, "PerMon" },
124 { PIRQ_SD, "SD" },
125 { PIRQ_SDIO, "SDIOt" },
126 { PIRQ_IMC0, "IMC INT0" },
127 { PIRQ_IMC1, "IMC INT1" },
128 { PIRQ_IMC2, "IMC INT2" },
129 { PIRQ_IMC3, "IMC INT3" },
130 { PIRQ_IMC4, "IMC INT4" },
131 { PIRQ_IMC5, "IMC INT5" },
132 { PIRQ_EHCI, "EHCI" },
133 { PIRQ_XHCI, "XHCI" },
134 { PIRQ_SATA, "SATA" },
135 { PIRQ_GPIO, "GPIO" },
136 { PIRQ_I2C0, "I2C0" },
137 { PIRQ_I2C1, "I2C1" },
138 { PIRQ_I2C2, "I2C2" },
139 { PIRQ_I2C3, "I2C3" },
140 { PIRQ_UART0, "UART0" },
141 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700142};
143
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700144/*
145 * Structure to simplify code obtaining the total of used wide IO
146 * registers and the size assigned to each.
147 */
148static struct wide_io_ioport_and_bits {
149 uint32_t enable;
150 uint16_t port;
151 uint8_t alt;
152} wio_io_en[TOTAL_WIDEIO_PORTS] = {
153 {
154 LPC_WIDEIO0_ENABLE,
155 LPC_WIDEIO_GENERIC_PORT,
156 LPC_ALT_WIDEIO0_ENABLE
157 },
158 {
159 LPC_WIDEIO1_ENABLE,
160 LPC_WIDEIO1_GENERIC_PORT,
161 LPC_ALT_WIDEIO1_ENABLE
162 },
163 {
164 LPC_WIDEIO2_ENABLE,
165 LPC_WIDEIO2_GENERIC_PORT,
166 LPC_ALT_WIDEIO2_ENABLE
167 }
168};
169
Richard Spiegel376dc822017-12-01 08:24:26 -0700170const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
171{
172 *size = ARRAY_SIZE(irq_association);
173 return irq_association;
174}
175
Richard Spiegel6fcb9b02018-04-18 08:06:33 -0700176void sb_program_gpios(const struct soc_amd_gpio *gpio_ptr, size_t size)
Richard Spiegele539c852017-12-25 18:25:58 -0700177{
178 void *tmp_ptr;
Richard Spiegele539c852017-12-25 18:25:58 -0700179 uint8_t control, mux, index;
180
Richard Spiegele539c852017-12-25 18:25:58 -0700181 for (index = 0; index < size; index++) {
182 mux = gpio_ptr[index].function;
183 control = gpio_ptr[index].control;
184 tmp_ptr = (void *)(gpio_ptr[index].gpio + AMD_GPIO_MUX);
185 write8(tmp_ptr, mux & AMD_GPIO_MUX_MASK);
186
187 /*
188 * Get the address of AMD_GPIO_CONTROL (dword) relative
189 * to the desired pin and program bits 16-23.
190 */
191 tmp_ptr = (void *)(gpio_ptr[index].gpio * sizeof(uint32_t) +
192 AMD_GPIO_CONTROL + 2);
193 write8(tmp_ptr, control);
194 }
Richard Spiegele539c852017-12-25 18:25:58 -0700195}
196
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700197/**
198 * @brief Find the size of a particular wide IO
199 *
200 * @param index = index of desired wide IO
201 *
202 * @return size of desired wide IO
203 */
204uint16_t sb_wideio_size(int index)
205{
206 uint32_t enable_register;
207 uint16_t size = 0;
208 uint8_t alternate_register;
209
210 if (index >= TOTAL_WIDEIO_PORTS)
211 return size;
212 enable_register = pci_read_config32(SOC_LPC_DEV,
213 LPC_IO_OR_MEM_DECODE_ENABLE);
214 alternate_register = pci_read_config8(SOC_LPC_DEV,
215 LPC_ALT_WIDEIO_RANGE_ENABLE);
216 if (enable_register & wio_io_en[index].enable)
217 size = (alternate_register & wio_io_en[index].alt) ?
218 16 : 512;
219 return size;
220}
221
222/**
223 * @brief Identify if any LPC wide IO is covering the IO range
224 *
225 * @param start = start of IO range
226 * @param size = size of IO range
227 *
228 * @return Index of wide IO covering the range or error
229 */
230int sb_find_wideio_range(uint16_t start, uint16_t size)
231{
232 uint32_t enable_register;
233 int i, index = WIDEIO_RANGE_ERROR;
234 uint16_t end, current_size, start_wideio, end_wideio;
235
236 end = start + size;
237 enable_register = pci_read_config32(SOC_LPC_DEV,
238 LPC_IO_OR_MEM_DECODE_ENABLE);
239 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
240 current_size = sb_wideio_size(i);
241 if (current_size == 0)
242 continue;
243 start_wideio = pci_read_config16(SOC_LPC_DEV,
244 wio_io_en[i].port);
245 end_wideio = start_wideio + current_size;
246 if ((start >= start_wideio) && (end <= end_wideio)) {
247 index = i;
248 break;
249 }
250 }
251 return index;
252}
253
254/**
255 * @brief Program a LPC wide IO to support an IO range
256 *
257 * @param start = start of range to be routed through wide IO
258 * @param size = size of range to be routed through wide IO
259 *
260 * @return Index of wide IO register used or error
261 */
262int sb_set_wideio_range(uint16_t start, uint16_t size)
263{
264 int i, index = WIDEIO_RANGE_ERROR;
265 uint32_t enable_register;
266 uint8_t alternate_register;
267
268 enable_register = pci_read_config32(SOC_LPC_DEV,
269 LPC_IO_OR_MEM_DECODE_ENABLE);
270 alternate_register = pci_read_config8(SOC_LPC_DEV,
271 LPC_ALT_WIDEIO_RANGE_ENABLE);
272 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
273 if (enable_register & wio_io_en[i].enable)
274 continue;
275 index = i;
276 pci_write_config16(SOC_LPC_DEV, wio_io_en[i].port, start);
277 enable_register |= wio_io_en[i].enable;
278 pci_write_config32(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE,
279 enable_register);
280 if (size <= 16)
281 alternate_register |= wio_io_en[i].alt;
282 else
283 alternate_register &= ~wio_io_en[i].alt;
284 pci_write_config8(SOC_LPC_DEV,
285 LPC_ALT_WIDEIO_RANGE_ENABLE,
286 alternate_register);
287 break;
288 }
289 return index;
290}
291
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600292static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700293{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600294 uint8_t byte;
295 uint8_t *register_pointer = (uint8_t *)(uintptr_t)AOAC_MMIO_BASE
296 + aoac_device_control_register;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700297
298 /* Power on the UART and AMBA devices */
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600299 byte = read8(register_pointer);
300 byte |= FCH_AOAC_PWR_ON_DEV;
301 write8(register_pointer, byte);
302}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700303
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600304static bool is_aoac_device_enabled(int aoac_device_status_register)
305{
306 uint8_t byte;
307 byte = read8((uint8_t *)(uintptr_t)AOAC_MMIO_BASE
308 + aoac_device_status_register);
309 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
310 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
311 return true;
312 else
313 return false;
314}
315
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700316void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600317{
318 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700319 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600320
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700321 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
322 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700323
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700324 /* Wait for AOAC devices to indicate power and clock OK */
325 do {
326 udelay(100);
327 status = true;
328 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
329 status &= is_aoac_device_enabled(aoac_devs[i].status);
330 } while (!status);
331}
332
Richard Spiegelbec44f22017-11-24 07:41:29 -0700333void sb_pci_port80(void)
334{
335 u8 byte;
336
337 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
338 byte &= ~DECODE_IO_PORT_ENABLE4_H; /* disable lpc port 80 */
339 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
340}
341
342void sb_lpc_port80(void)
343{
344 u8 byte;
345
346 /* Enable LPC controller */
347 outb(PM_LPC_GATING, PM_INDEX);
348 byte = inb(PM_DATA);
349 byte |= PM_LPC_ENABLE;
350 outb(PM_LPC_GATING, PM_INDEX);
351 outb(byte, PM_DATA);
352
353 /* Enable port 80 LPC decode in pci function 3 configuration space. */
354 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
355 byte |= DECODE_IO_PORT_ENABLE4_H; /* enable port 80 */
356 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
357}
358
359void sb_lpc_decode(void)
360{
361 u32 tmp = 0;
362
363 /* Enable I/O decode to LPC bus */
364 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
365 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
366 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
367 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
368 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
369 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
370 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
371 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
372 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
373 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
374 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
375 | DECODE_ENABLE_ADLIB_PORT;
376
377 pci_write_config32(SOC_LPC_DEV, LPC_IO_PORT_DECODE_ENABLE, tmp);
378}
379
Garrett Kirkendall9858bd22018-03-07 15:38:14 -0600380void sb_acpi_mmio_decode(void)
381{
382 uint8_t byte;
383
384 /* Enable ACPI MMIO range 0xfed80000 - 0xfed81fff */
385 outb(PM_ISA_CONTROL, PM_INDEX);
386 byte = inb(PM_DATA);
387 byte |= MMIO_EN;
388 outb(PM_ISA_CONTROL, PM_INDEX);
389 outb(byte, PM_DATA);
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
396 + MISC_MISC_CLK_CNTL_1);
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);
415 enables = base & 0xf;
416 base &= ~0x3f;
417
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();
430 write16((void *)base + SPI100_SPEED_CONFIG,
431 (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));
435 write16((void *)base + SPI100_ENABLE, SPI_USE_SPI100);
436}
437
438void sb_disable_4dw_burst(void)
439{
440 uintptr_t base = sb_spibase();
441 write16((void *)base + SPI100_HOST_PREF_CONFIG,
442 read16((void *)base + SPI100_HOST_PREF_CONFIG)
443 & ~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();
449 write32((void *)base + SPI_CNTRL0,
450 (read32((void *)base + SPI_CNTRL0)
451 & ~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
Richard Spiegelbec44f22017-11-24 07:41:29 -0700557void bootblock_fch_early_init(void)
558{
559 sb_enable_rom();
560 sb_lpc_port80();
561 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600562 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500563 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600564 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500565 sb_acpi_mmio_decode();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700566 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700567}
568
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600569void sb_enable(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600570{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600571 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600572}
573
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600574static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600575{
Marshall Dawson91b80412017-09-27 16:44:40 -0600576 u32 reg;
577
Marc Jones24484842017-05-04 21:17:45 -0600578 /* We use some of these ports in SMM regardless of whether or not
579 * ACPI tables are generated. Enable these ports indiscriminately.
580 */
581
582 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
583 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
584 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
585 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
586 /* CpuControl is in \_PR.CP00, 6 bytes */
587 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
588
589 if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600590 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600591 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600592 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600593
594 /* SMI on SlpTyp requires sending SMI before completion
595 * response of the I/O write. The BKDG also specifies
596 * clearing ForceStpClkRetry for SMI trapping.
597 */
598 reg = pm_read32(PM_PCI_CTRL);
599 reg |= FORCE_SLPSTATE_RETRY;
600 reg &= ~FORCE_STPCLK_RETRY;
601 pm_write32(PM_PCI_CTRL, reg);
602
603 /* Disable SlpTyp feature */
604 reg = pm_read8(PM_RST_CTRL1);
605 reg &= ~SLPTYPE_CONTROL_EN;
606 pm_write8(PM_RST_CTRL1, reg);
607
608 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600609 } else {
610 pm_write16(PM_ACPI_SMI_CMD, 0);
611 }
612
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700613 /* Decode ACPI registers and enable standard features */
614 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
615 PM_ACPI_GLOBAL_EN |
616 PM_ACPI_RTC_EN_EN |
617 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600618}
619
Marshall Dawson70f051f2018-03-20 10:27:41 -0600620static void print_num_status_bits(int num_bits, uint32_t status,
621 const char *const bit_names[])
622{
623 int i;
624
625 if (!status)
626 return;
627
628 for (i = num_bits - 1; i >= 0; i--) {
629 if (status & (1 << i)) {
630 if (bit_names[i])
631 printk(BIOS_DEBUG, "%s ", bit_names[i]);
632 else
633 printk(BIOS_DEBUG, "BIT%d ", i);
634 }
635 }
636}
637
638static uint16_t reset_pm1_status(void)
639{
640 uint16_t pm1_sts = inw(ACPI_PM_EVT_BLK);
641 outw(pm1_sts, ACPI_PM_EVT_BLK);
642 return pm1_sts;
643}
644
645static uint16_t print_pm1_status(uint16_t pm1_sts)
646{
647 static const char *const pm1_sts_bits[] = {
648 [0] = "TMROF",
649 [4] = "BMSTATUS",
650 [5] = "GBL",
651 [8] = "PWRBTN",
652 [10] = "RTC",
653 [14] = "PCIEXPWAK",
654 [15] = "WAK",
655 };
656
657 if (!pm1_sts)
658 return 0;
659
660 printk(BIOS_SPEW, "PM1_STS: ");
661 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
662 printk(BIOS_SPEW, "\n");
663
664 return pm1_sts;
665}
666
667static void sb_log_pm1_status(uint16_t pm1_sts)
668{
669 if (!IS_ENABLED(CONFIG_ELOG))
670 return;
671
672 if (pm1_sts & PWRBTN_STS)
673 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
674
675 if (pm1_sts & RTC_STS)
676 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
677
678 if (pm1_sts & PCIEXPWAK_STS)
679 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
680}
681
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700682struct soc_amd_sws {
683 uint32_t pm1i;
684 uint32_t gevent;
685};
686
687static struct soc_amd_sws sws;
688
689static void sb_save_sws(uint32_t pm1_status)
690{
691 uint32_t reg32;
692
693 sws.pm1i = pm1_status;
694 reg32 = inl(ACPI_GPE0_BLK);
695 outl(ACPI_GPE0_BLK, reg32);
696 reg32 &= inl(ACPI_GPE0_BLK + sizeof(uint32_t));
697 sws.gevent = reg32;
698}
699
Marshall Dawson70f051f2018-03-20 10:27:41 -0600700static void sb_clear_pm1_status(void)
701{
702 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700703
704 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600705 sb_log_pm1_status(pm1_sts);
706 print_pm1_status(pm1_sts);
707}
708
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700709static void set_nvs_sws(void *unused)
710{
711 struct global_nvs_t *gnvs;
712
713 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
714 if (gnvs == NULL)
715 return;
716
717 gnvs->pm1i = sws.pm1i;
718 gnvs->gpei = sws.gevent;
719
720 printk(BIOS_DEBUG, "Loaded _SWS parameters PM1 0x%08x, EVENT 0x%08x into nvs\n",
721 sws.pm1i, sws.gevent);
722}
723
724BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
725
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600726void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600727{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600728 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600729 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600730}
731
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600732void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600733{
Richard Spiegel6a389142018-03-05 14:28:10 -0700734 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
735
Richard Spiegel38f19402017-09-29 11:39:46 -0700736 if (IS_ENABLED(CONFIG_STONEYRIDGE_IMC_FWM)) {
737 agesawrapper_fchecfancontrolservice();
738 if (!IS_ENABLED(CONFIG_ACPI_ENABLE_THERMAL_ZONE))
739 enable_imc_thermal_zone();
740 }
Richard Spiegel6a389142018-03-05 14:28:10 -0700741 if (IS_ENABLED(CONFIG_MAINBOARD_POWER_RESTORE))
742 restored_power = PM_RESTORE_S0_IF_PREV_S0;
743 pm_write8(PM_RTC_SHADOW, restored_power);
Marc Jones24484842017-05-04 21:17:45 -0600744}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600745
746/*
747 * Update the PCI devices with a valid IRQ number
748 * that is set in the mainboard PCI_IRQ structures.
749 */
750static void set_pci_irqs(void *unused)
751{
752 /* Write PCI_INTR regs 0xC00/0xC01 */
753 write_pci_int_table();
754
755 /* Write IRQs for all devicetree enabled devices */
756 write_pci_cfg_irqs();
757}
758
759/*
760 * Hook this function into the PCI state machine
761 * on entry into BS_DEV_ENABLE.
762 */
763BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);