blob: 591a8f7c9f612d3db5545630a73e0e591de17aef [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 Spiegelebf3aa82017-11-24 07:47:42 -0700176/**
177 * @brief Find the size of a particular wide IO
178 *
179 * @param index = index of desired wide IO
180 *
181 * @return size of desired wide IO
182 */
183uint16_t sb_wideio_size(int index)
184{
185 uint32_t enable_register;
186 uint16_t size = 0;
187 uint8_t alternate_register;
188
189 if (index >= TOTAL_WIDEIO_PORTS)
190 return size;
191 enable_register = pci_read_config32(SOC_LPC_DEV,
192 LPC_IO_OR_MEM_DECODE_ENABLE);
193 alternate_register = pci_read_config8(SOC_LPC_DEV,
194 LPC_ALT_WIDEIO_RANGE_ENABLE);
195 if (enable_register & wio_io_en[index].enable)
196 size = (alternate_register & wio_io_en[index].alt) ?
197 16 : 512;
198 return size;
199}
200
201/**
202 * @brief Identify if any LPC wide IO is covering the IO range
203 *
204 * @param start = start of IO range
205 * @param size = size of IO range
206 *
207 * @return Index of wide IO covering the range or error
208 */
209int sb_find_wideio_range(uint16_t start, uint16_t size)
210{
211 uint32_t enable_register;
212 int i, index = WIDEIO_RANGE_ERROR;
213 uint16_t end, current_size, start_wideio, end_wideio;
214
215 end = start + size;
216 enable_register = pci_read_config32(SOC_LPC_DEV,
217 LPC_IO_OR_MEM_DECODE_ENABLE);
218 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
219 current_size = sb_wideio_size(i);
220 if (current_size == 0)
221 continue;
222 start_wideio = pci_read_config16(SOC_LPC_DEV,
223 wio_io_en[i].port);
224 end_wideio = start_wideio + current_size;
225 if ((start >= start_wideio) && (end <= end_wideio)) {
226 index = i;
227 break;
228 }
229 }
230 return index;
231}
232
233/**
234 * @brief Program a LPC wide IO to support an IO range
235 *
236 * @param start = start of range to be routed through wide IO
237 * @param size = size of range to be routed through wide IO
238 *
239 * @return Index of wide IO register used or error
240 */
241int sb_set_wideio_range(uint16_t start, uint16_t size)
242{
243 int i, index = WIDEIO_RANGE_ERROR;
244 uint32_t enable_register;
245 uint8_t alternate_register;
246
247 enable_register = pci_read_config32(SOC_LPC_DEV,
248 LPC_IO_OR_MEM_DECODE_ENABLE);
249 alternate_register = pci_read_config8(SOC_LPC_DEV,
250 LPC_ALT_WIDEIO_RANGE_ENABLE);
251 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
252 if (enable_register & wio_io_en[i].enable)
253 continue;
254 index = i;
255 pci_write_config16(SOC_LPC_DEV, wio_io_en[i].port, start);
256 enable_register |= wio_io_en[i].enable;
257 pci_write_config32(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE,
258 enable_register);
259 if (size <= 16)
260 alternate_register |= wio_io_en[i].alt;
261 else
262 alternate_register &= ~wio_io_en[i].alt;
263 pci_write_config8(SOC_LPC_DEV,
264 LPC_ALT_WIDEIO_RANGE_ENABLE,
265 alternate_register);
266 break;
267 }
268 return index;
269}
270
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600271static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700272{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600273 uint8_t byte;
274 uint8_t *register_pointer = (uint8_t *)(uintptr_t)AOAC_MMIO_BASE
275 + aoac_device_control_register;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700276
277 /* Power on the UART and AMBA devices */
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600278 byte = read8(register_pointer);
279 byte |= FCH_AOAC_PWR_ON_DEV;
280 write8(register_pointer, byte);
281}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700282
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600283static bool is_aoac_device_enabled(int aoac_device_status_register)
284{
285 uint8_t byte;
286 byte = read8((uint8_t *)(uintptr_t)AOAC_MMIO_BASE
287 + aoac_device_status_register);
288 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
289 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
290 return true;
291 else
292 return false;
293}
294
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700295void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600296{
297 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700298 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600299
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700300 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
301 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700302
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700303 /* Wait for AOAC devices to indicate power and clock OK */
304 do {
305 udelay(100);
306 status = true;
307 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
308 status &= is_aoac_device_enabled(aoac_devs[i].status);
309 } while (!status);
310}
311
Richard Spiegelbec44f22017-11-24 07:41:29 -0700312void sb_pci_port80(void)
313{
314 u8 byte;
315
316 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
317 byte &= ~DECODE_IO_PORT_ENABLE4_H; /* disable lpc port 80 */
318 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
319}
320
321void sb_lpc_port80(void)
322{
323 u8 byte;
324
325 /* Enable LPC controller */
326 outb(PM_LPC_GATING, PM_INDEX);
327 byte = inb(PM_DATA);
328 byte |= PM_LPC_ENABLE;
329 outb(PM_LPC_GATING, PM_INDEX);
330 outb(byte, PM_DATA);
331
332 /* Enable port 80 LPC decode in pci function 3 configuration space. */
333 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
334 byte |= DECODE_IO_PORT_ENABLE4_H; /* enable port 80 */
335 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
336}
337
338void sb_lpc_decode(void)
339{
340 u32 tmp = 0;
341
342 /* Enable I/O decode to LPC bus */
343 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
344 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
345 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
346 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
347 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
348 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
349 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
350 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
351 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
352 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
353 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
354 | DECODE_ENABLE_ADLIB_PORT;
355
356 pci_write_config32(SOC_LPC_DEV, LPC_IO_PORT_DECODE_ENABLE, tmp);
357}
358
Garrett Kirkendall9858bd22018-03-07 15:38:14 -0600359void sb_acpi_mmio_decode(void)
360{
361 uint8_t byte;
362
363 /* Enable ACPI MMIO range 0xfed80000 - 0xfed81fff */
364 outb(PM_ISA_CONTROL, PM_INDEX);
365 byte = inb(PM_DATA);
366 byte |= MMIO_EN;
367 outb(PM_ISA_CONTROL, PM_INDEX);
368 outb(byte, PM_DATA);
369}
370
Raul E Rangel5b058232018-06-28 16:31:45 -0600371static void sb_enable_cf9_io(void)
372{
373 uint32_t reg = pm_read32(PM_DECODE_EN);
374
375 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
376}
377
Richard Spiegelbec44f22017-11-24 07:41:29 -0700378void sb_clk_output_48Mhz(void)
379{
380 u32 ctrl;
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600381 u32 *misc_clk_cntl_1_ptr = (u32 *)(uintptr_t)(MISC_MMIO_BASE
382 + MISC_MISC_CLK_CNTL_1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700383
384 /*
385 * Enable the X14M_25M_48M_OSC pin and leaving it at it's default so
386 * 48Mhz will be on ball AP13 (FT3b package)
387 */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600388 ctrl = read32(misc_clk_cntl_1_ptr);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700389
390 /* clear the OSCOUT1_ClkOutputEnb to enable the 48 Mhz clock */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600391 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
392 write32(misc_clk_cntl_1_ptr, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700393}
394
395static uintptr_t sb_spibase(void)
396{
397 u32 base, enables;
398
399 /* Make sure the base address is predictable */
400 base = pci_read_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER);
401 enables = base & 0xf;
402 base &= ~0x3f;
403
404 if (!base) {
405 base = SPI_BASE_ADDRESS;
406 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER,
407 base | enables | SPI_ROM_ENABLE);
408 /* PCI_COMMAND_MEMORY is read-only and enabled. */
409 }
410 return (uintptr_t)base;
411}
412
413void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
414{
415 uintptr_t base = sb_spibase();
416 write16((void *)base + SPI100_SPEED_CONFIG,
417 (norm << SPI_NORM_SPEED_NEW_SH) |
418 (fast << SPI_FAST_SPEED_NEW_SH) |
419 (alt << SPI_ALT_SPEED_NEW_SH) |
420 (tpm << SPI_TPM_SPEED_NEW_SH));
421 write16((void *)base + SPI100_ENABLE, SPI_USE_SPI100);
422}
423
424void sb_disable_4dw_burst(void)
425{
426 uintptr_t base = sb_spibase();
427 write16((void *)base + SPI100_HOST_PREF_CONFIG,
428 read16((void *)base + SPI100_HOST_PREF_CONFIG)
429 & ~SPI_RD4DW_EN_HOST);
430}
431
Richard Spiegelbec44f22017-11-24 07:41:29 -0700432void sb_read_mode(u32 mode)
433{
434 uintptr_t base = sb_spibase();
435 write32((void *)base + SPI_CNTRL0,
436 (read32((void *)base + SPI_CNTRL0)
437 & ~SPI_READ_MODE_MASK) | mode);
438}
439
Garrett Kirkendall65753062018-03-07 16:12:11 -0600440/*
441 * Enable FCH to decode TPM associated Memory and IO regions
442 *
443 * Enable decoding of TPM cycles defined in TPM 1.2 spec
444 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
445 * 0x7e and 0xef-0xee.
446 * This function should be called if TPM is connected in any way to the FCH and
447 * conforms to the regions decoded.
448 * Absent any other routing configuration the TPM cycles will be claimed by the
449 * LPC bus
450 */
451void sb_tpm_decode(void)
452{
453 u32 value;
454
455 value = pci_read_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE);
456 value |= TPM_12_EN | TPM_LEGACY_EN;
457 pci_write_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE, value);
458}
459
460/*
461 * Enable FCH to decode TPM associated Memory and IO regions to SPI
462 *
463 * This should be used if TPM is connected to SPI bus.
464 * Assumes SPI address space is already configured via a call to sb_spibase().
465 */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700466void sb_tpm_decode_spi(void)
467{
Garrett Kirkendall65753062018-03-07 16:12:11 -0600468 /* Enable TPM decoding to FCH */
469 sb_tpm_decode();
470
471 /* Route TPM accesses to SPI */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700472 u32 spibase = pci_read_config32(SOC_LPC_DEV,
473 SPIROM_BASE_ADDRESS_REGISTER);
474 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER, spibase
475 | ROUTE_TPM_2_SPI);
476}
477
478/*
479 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
480 *
481 * Hardware should enable LPC ROM by pin straps. This function does not
482 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
483 *
484 * The southbridge power-on default is to map 512K ROM space.
485 *
486 */
487void sb_enable_rom(void)
488{
489 u8 reg8;
490
491 /*
492 * Decode variable LPC ROM address ranges 1 and 2.
493 * Bits 3-4 are not defined in any publicly available datasheet
494 */
495 reg8 = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
496 reg8 |= (1 << 3) | (1 << 4);
497 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, reg8);
498
499 /*
500 * LPC ROM address range 1:
501 * Enable LPC ROM range mirroring start at 0x000e(0000).
502 */
503 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_START, 0x000e);
504
505 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
506 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_END, 0x000f);
507
508 /*
509 * LPC ROM address range 2:
510 *
511 * Enable LPC ROM range start at:
512 * 0xfff8(0000): 512KB
513 * 0xfff0(0000): 1MB
514 * 0xffe0(0000): 2MB
515 * 0xffc0(0000): 4MB
516 */
517 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START, 0x10000
518 - (CONFIG_COREBOOT_ROMSIZE_KB >> 6));
519
520 /* Enable LPC ROM range end at 0xffff(ffff). */
521 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_END, 0xffff);
522}
523
Marc Jonescfb16802018-04-20 16:27:41 -0600524static void sb_lpc_early_setup(void)
525{
526 uint32_t dword;
527
528 /* Enable SPI prefetch */
529 dword = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
530 dword |= SPI_FROM_HOST_PREFETCH_EN | SPI_FROM_USB_PREFETCH_EN;
531 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, dword);
532
533 if (IS_ENABLED(CONFIG_STONEYRIDGE_LEGACY_FREE)) {
534 /* Decode SIOs at 2E/2F and 4E/4F */
535 dword = pci_read_config32(SOC_LPC_DEV,
536 LPC_IO_OR_MEM_DECODE_ENABLE);
537 dword |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
538 pci_write_config32(SOC_LPC_DEV,
539 LPC_IO_OR_MEM_DECODE_ENABLE, dword);
540 }
541}
542
Richard Spiegelbec44f22017-11-24 07:41:29 -0700543void bootblock_fch_early_init(void)
544{
545 sb_enable_rom();
546 sb_lpc_port80();
547 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600548 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500549 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600550 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500551 sb_acpi_mmio_decode();
Raul E Rangel5b058232018-06-28 16:31:45 -0600552 sb_enable_cf9_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700553 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700554}
555
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600556void sb_enable(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600557{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600558 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600559}
560
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600561static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600562{
Marshall Dawson91b80412017-09-27 16:44:40 -0600563 u32 reg;
564
Marc Jones24484842017-05-04 21:17:45 -0600565 /* We use some of these ports in SMM regardless of whether or not
566 * ACPI tables are generated. Enable these ports indiscriminately.
567 */
568
569 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
570 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
571 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
572 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
573 /* CpuControl is in \_PR.CP00, 6 bytes */
574 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
575
576 if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600577 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600578 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600579 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600580
581 /* SMI on SlpTyp requires sending SMI before completion
582 * response of the I/O write. The BKDG also specifies
583 * clearing ForceStpClkRetry for SMI trapping.
584 */
585 reg = pm_read32(PM_PCI_CTRL);
586 reg |= FORCE_SLPSTATE_RETRY;
587 reg &= ~FORCE_STPCLK_RETRY;
588 pm_write32(PM_PCI_CTRL, reg);
589
590 /* Disable SlpTyp feature */
591 reg = pm_read8(PM_RST_CTRL1);
592 reg &= ~SLPTYPE_CONTROL_EN;
593 pm_write8(PM_RST_CTRL1, reg);
594
595 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600596 } else {
597 pm_write16(PM_ACPI_SMI_CMD, 0);
598 }
599
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700600 /* Decode ACPI registers and enable standard features */
601 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
602 PM_ACPI_GLOBAL_EN |
603 PM_ACPI_RTC_EN_EN |
604 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600605}
606
Marshall Dawson70f051f2018-03-20 10:27:41 -0600607static void print_num_status_bits(int num_bits, uint32_t status,
608 const char *const bit_names[])
609{
610 int i;
611
612 if (!status)
613 return;
614
615 for (i = num_bits - 1; i >= 0; i--) {
616 if (status & (1 << i)) {
617 if (bit_names[i])
618 printk(BIOS_DEBUG, "%s ", bit_names[i]);
619 else
620 printk(BIOS_DEBUG, "BIT%d ", i);
621 }
622 }
623}
624
625static uint16_t reset_pm1_status(void)
626{
Richard Spiegel572f4982018-05-25 15:49:33 -0700627 uint16_t pm1_sts = inw(ACPI_PM1_STS);
628 outw(pm1_sts, ACPI_PM1_STS);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600629 return pm1_sts;
630}
631
632static uint16_t print_pm1_status(uint16_t pm1_sts)
633{
634 static const char *const pm1_sts_bits[] = {
635 [0] = "TMROF",
636 [4] = "BMSTATUS",
637 [5] = "GBL",
638 [8] = "PWRBTN",
639 [10] = "RTC",
640 [14] = "PCIEXPWAK",
641 [15] = "WAK",
642 };
643
644 if (!pm1_sts)
645 return 0;
646
647 printk(BIOS_SPEW, "PM1_STS: ");
648 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
649 printk(BIOS_SPEW, "\n");
650
651 return pm1_sts;
652}
653
654static void sb_log_pm1_status(uint16_t pm1_sts)
655{
656 if (!IS_ENABLED(CONFIG_ELOG))
657 return;
658
Daniel Kurtzb6fdd222018-05-24 15:52:45 -0600659 if (pm1_sts & WAK_STS)
660 elog_add_event_byte(ELOG_TYPE_ACPI_WAKE,
661 acpi_is_wakeup_s3() ? ACPI_S3 : ACPI_S5);
662
Marshall Dawson70f051f2018-03-20 10:27:41 -0600663 if (pm1_sts & PWRBTN_STS)
664 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
665
666 if (pm1_sts & RTC_STS)
667 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
668
669 if (pm1_sts & PCIEXPWAK_STS)
670 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
671}
672
Richard Spiegel572f4982018-05-25 15:49:33 -0700673static void sb_save_sws(uint16_t pm1_status)
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700674{
Richard Spiegel35282a02018-06-14 14:57:54 -0700675 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700676 uint32_t reg32;
Richard Spiegel35282a02018-06-14 14:57:54 -0700677 uint16_t reg16;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700678
Richard Spiegel35282a02018-06-14 14:57:54 -0700679 sws = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(struct soc_power_reg));
680 if (sws == NULL)
681 return;
682 sws->pm1_sts = pm1_status;
683 sws->pm1_en = inw(ACPI_PM1_EN);
Richard Spiegel572f4982018-05-25 15:49:33 -0700684 reg32 = inl(ACPI_GPE0_STS);
685 outl(ACPI_GPE0_STS, reg32);
Richard Spiegel35282a02018-06-14 14:57:54 -0700686 sws->gpe0_sts = reg32;
687 sws->gpe0_en = inl(ACPI_GPE0_EN);
688 reg16 = inw(ACPI_PM1_CNT_BLK);
689 reg16 &= SLP_TYP;
690 sws->wake_from = reg16 >> SLP_TYP_SHIFT;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700691}
692
Marshall Dawson70f051f2018-03-20 10:27:41 -0600693static void sb_clear_pm1_status(void)
694{
695 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700696
697 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600698 sb_log_pm1_status(pm1_sts);
699 print_pm1_status(pm1_sts);
700}
701
Richard Spiegel572f4982018-05-25 15:49:33 -0700702static int get_index_bit(uint32_t value, uint16_t limit)
703{
704 uint16_t i;
705 uint32_t t;
706
Richard Spiegelef73cb82018-06-19 07:40:18 -0700707 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700708 return -1;
709
710 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
711 t = (1 << limit) - 1;
712 if ((value & t) == 0)
713 return -1;
714 t = 1;
715 for (i = 0; i < limit; i++) {
716 if (value & t)
717 break;
718 t <<= 1;
719 }
720 return i;
721}
722
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700723static void set_nvs_sws(void *unused)
724{
Richard Spiegel35282a02018-06-14 14:57:54 -0700725 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700726 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700727 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700728
Richard Spiegel35282a02018-06-14 14:57:54 -0700729 sws = cbmem_find(CBMEM_ID_POWER_STATE);
730 if (sws == NULL)
731 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700732 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
733 if (gnvs == NULL)
734 return;
735
Richard Spiegel35282a02018-06-14 14:57:54 -0700736 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700737 if (index < 0)
738 gnvs->pm1i = ~0ULL;
739 else
740 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700741
Richard Spiegel35282a02018-06-14 14:57:54 -0700742 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700743 if (index < 0)
744 gnvs->gpei = ~0ULL;
745 else
746 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700747}
748
749BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
750
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600751void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600752{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600753 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600754 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600755}
756
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600757void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600758{
Richard Spiegel6a389142018-03-05 14:28:10 -0700759 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
760
Richard Spiegel38f19402017-09-29 11:39:46 -0700761 if (IS_ENABLED(CONFIG_STONEYRIDGE_IMC_FWM)) {
762 agesawrapper_fchecfancontrolservice();
763 if (!IS_ENABLED(CONFIG_ACPI_ENABLE_THERMAL_ZONE))
764 enable_imc_thermal_zone();
765 }
Richard Spiegel6a389142018-03-05 14:28:10 -0700766 if (IS_ENABLED(CONFIG_MAINBOARD_POWER_RESTORE))
767 restored_power = PM_RESTORE_S0_IF_PREV_S0;
768 pm_write8(PM_RTC_SHADOW, restored_power);
Marc Jones24484842017-05-04 21:17:45 -0600769}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600770
771/*
772 * Update the PCI devices with a valid IRQ number
773 * that is set in the mainboard PCI_IRQ structures.
774 */
775static void set_pci_irqs(void *unused)
776{
777 /* Write PCI_INTR regs 0xC00/0xC01 */
778 write_pci_int_table();
779
780 /* Write IRQs for all devicetree enabled devices */
781 write_pci_cfg_irqs();
782}
783
784/*
785 * Hook this function into the PCI state machine
786 * on entry into BS_DEV_ENABLE.
787 */
788BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);