blob: f098c1c32912a0e2797372587b05797603ab6813 [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
Richard Spiegel376dc822017-12-01 08:24:26 -07004 * Copyright (C) 2010-2017 Advanced Micro Devices, Inc.
Marc Jones24484842017-05-04 21:17:45 -06005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <console/console.h>
17
18#include <arch/io.h>
Marshall Dawson8a906df2017-06-13 14:19:02 -060019#include <bootstate.h>
Marshall Dawsone9b862e2017-09-22 15:14:46 -060020#include <cpu/x86/smm.h>
Marc Jones24484842017-05-04 21:17:45 -060021#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <device/pci_ops.h>
25#include <cbmem.h>
Marshall Dawson70f051f2018-03-20 10:27:41 -060026#include <elog.h>
Richard Spiegel2bbc3dc2017-12-06 16:14:58 -070027#include <amdblocks/amd_pci_util.h>
Richard Spiegel71081072018-07-26 10:51:38 -070028#include <amdblocks/agesawrapper.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060029#include <soc/southbridge.h>
Marc Jones24484842017-05-04 21:17:45 -060030#include <soc/smi.h>
Richard Spiegel376dc822017-12-01 08:24:26 -070031#include <soc/amd_pci_int_defs.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{
Richard Spiegelbb18b432018-08-03 10:37:28 -070075 const struct device *dev = dev_find_slot(0, SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070076 params->Xhci0Enable = IS_ENABLED(CONFIG_STONEYRIDGE_XHCI_ENABLE);
Richard Spiegelbb18b432018-08-03 10:37:28 -070077 if (dev && dev->enabled) {
78 params->SataEnable = sb_sata_enable();
79 params->IdeEnable = sb_ide_enable();
80 } else {
81 params->SataEnable = FALSE;
82 params->IdeEnable = FALSE;
83 }
Marshall Dawson2942db62017-12-14 10:00:27 -070084}
85
86void SetFchEnvParams(FCH_INTERFACE *params)
87{
Richard Spiegelbb18b432018-08-03 10:37:28 -070088 const struct device *dev = dev_find_slot(0, SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070089 params->AzaliaController = AzEnable;
90 params->SataClass = CONFIG_STONEYRIDGE_SATA_MODE;
Richard Spiegelbb18b432018-08-03 10:37:28 -070091 if (dev && dev->enabled) {
92 params->SataEnable = is_sata_config();
93 params->IdeEnable = !params->SataEnable;
94 params->SataIdeMode = (CONFIG_STONEYRIDGE_SATA_MODE ==
95 SataLegacyIde);
96 } else {
97 params->SataEnable = FALSE;
98 params->IdeEnable = FALSE;
99 params->SataIdeMode = FALSE;
100 }
Marshall Dawson2942db62017-12-14 10:00:27 -0700101}
102
103void SetFchMidParams(FCH_INTERFACE *params)
104{
105 SetFchEnvParams(params);
106}
Marc Jones24484842017-05-04 21:17:45 -0600107
Richard Spiegel376dc822017-12-01 08:24:26 -0700108/*
109 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100110 * provides a visible association with the index, therefore helping
Richard Spiegel376dc822017-12-01 08:24:26 -0700111 * maintainability of table. If a new index/name is defined in
112 * amd_pci_int_defs.h, just add the pair at the end of this table.
113 * Order is not important.
114 */
115const static struct irq_idx_name irq_association[] = {
Richard Spiegele89d4442017-12-08 07:52:42 -0700116 { PIRQ_A, "INTA#" },
117 { PIRQ_B, "INTB#" },
118 { PIRQ_C, "INTC#" },
119 { PIRQ_D, "INTD#" },
120 { PIRQ_E, "INTE#" },
121 { PIRQ_F, "INTF#" },
122 { PIRQ_G, "INTG#" },
123 { PIRQ_H, "INTH#" },
124 { PIRQ_MISC, "Misc" },
125 { PIRQ_MISC0, "Misc0" },
126 { PIRQ_MISC1, "Misc1" },
127 { PIRQ_MISC2, "Misc2" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700128 { PIRQ_SIRQA, "Ser IRQ INTA" },
129 { PIRQ_SIRQB, "Ser IRQ INTB" },
130 { PIRQ_SIRQC, "Ser IRQ INTC" },
131 { PIRQ_SIRQD, "Ser IRQ INTD" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700132 { PIRQ_SCI, "SCI" },
133 { PIRQ_SMBUS, "SMBUS" },
134 { PIRQ_ASF, "ASF" },
135 { PIRQ_HDA, "HDA" },
136 { PIRQ_FC, "FC" },
137 { PIRQ_PMON, "PerMon" },
138 { PIRQ_SD, "SD" },
139 { PIRQ_SDIO, "SDIOt" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700140 { PIRQ_EHCI, "EHCI" },
141 { PIRQ_XHCI, "XHCI" },
142 { PIRQ_SATA, "SATA" },
143 { PIRQ_GPIO, "GPIO" },
144 { PIRQ_I2C0, "I2C0" },
145 { PIRQ_I2C1, "I2C1" },
146 { PIRQ_I2C2, "I2C2" },
147 { PIRQ_I2C3, "I2C3" },
148 { PIRQ_UART0, "UART0" },
149 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700150};
151
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700152/*
153 * Structure to simplify code obtaining the total of used wide IO
154 * registers and the size assigned to each.
155 */
156static struct wide_io_ioport_and_bits {
157 uint32_t enable;
158 uint16_t port;
159 uint8_t alt;
160} wio_io_en[TOTAL_WIDEIO_PORTS] = {
161 {
162 LPC_WIDEIO0_ENABLE,
163 LPC_WIDEIO_GENERIC_PORT,
164 LPC_ALT_WIDEIO0_ENABLE
165 },
166 {
167 LPC_WIDEIO1_ENABLE,
168 LPC_WIDEIO1_GENERIC_PORT,
169 LPC_ALT_WIDEIO1_ENABLE
170 },
171 {
172 LPC_WIDEIO2_ENABLE,
173 LPC_WIDEIO2_GENERIC_PORT,
174 LPC_ALT_WIDEIO2_ENABLE
175 }
176};
177
Richard Spiegel376dc822017-12-01 08:24:26 -0700178const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
179{
180 *size = ARRAY_SIZE(irq_association);
181 return irq_association;
182}
183
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700184/**
185 * @brief Find the size of a particular wide IO
186 *
187 * @param index = index of desired wide IO
188 *
189 * @return size of desired wide IO
190 */
191uint16_t sb_wideio_size(int index)
192{
193 uint32_t enable_register;
194 uint16_t size = 0;
195 uint8_t alternate_register;
196
197 if (index >= TOTAL_WIDEIO_PORTS)
198 return size;
199 enable_register = pci_read_config32(SOC_LPC_DEV,
200 LPC_IO_OR_MEM_DECODE_ENABLE);
201 alternate_register = pci_read_config8(SOC_LPC_DEV,
202 LPC_ALT_WIDEIO_RANGE_ENABLE);
203 if (enable_register & wio_io_en[index].enable)
204 size = (alternate_register & wio_io_en[index].alt) ?
205 16 : 512;
206 return size;
207}
208
209/**
210 * @brief Identify if any LPC wide IO is covering the IO range
211 *
212 * @param start = start of IO range
213 * @param size = size of IO range
214 *
215 * @return Index of wide IO covering the range or error
216 */
217int sb_find_wideio_range(uint16_t start, uint16_t size)
218{
219 uint32_t enable_register;
220 int i, index = WIDEIO_RANGE_ERROR;
221 uint16_t end, current_size, start_wideio, end_wideio;
222
223 end = start + size;
224 enable_register = pci_read_config32(SOC_LPC_DEV,
225 LPC_IO_OR_MEM_DECODE_ENABLE);
226 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 Spiegelbec44f22017-11-24 07:41:29 -0700393void sb_clk_output_48Mhz(void)
394{
395 u32 ctrl;
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600396 u32 *misc_clk_cntl_1_ptr = (u32 *)(uintptr_t)(MISC_MMIO_BASE
397 + MISC_MISC_CLK_CNTL_1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700398
399 /*
400 * Enable the X14M_25M_48M_OSC pin and leaving it at it's default so
401 * 48Mhz will be on ball AP13 (FT3b package)
402 */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600403 ctrl = read32(misc_clk_cntl_1_ptr);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700404
405 /* clear the OSCOUT1_ClkOutputEnb to enable the 48 Mhz clock */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600406 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
407 write32(misc_clk_cntl_1_ptr, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700408}
409
410static uintptr_t sb_spibase(void)
411{
412 u32 base, enables;
413
414 /* Make sure the base address is predictable */
415 base = pci_read_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER);
416 enables = base & 0xf;
417 base &= ~0x3f;
418
419 if (!base) {
420 base = SPI_BASE_ADDRESS;
421 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER,
422 base | enables | SPI_ROM_ENABLE);
423 /* PCI_COMMAND_MEMORY is read-only and enabled. */
424 }
425 return (uintptr_t)base;
426}
427
428void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
429{
430 uintptr_t base = sb_spibase();
431 write16((void *)base + SPI100_SPEED_CONFIG,
432 (norm << SPI_NORM_SPEED_NEW_SH) |
433 (fast << SPI_FAST_SPEED_NEW_SH) |
434 (alt << SPI_ALT_SPEED_NEW_SH) |
435 (tpm << SPI_TPM_SPEED_NEW_SH));
436 write16((void *)base + SPI100_ENABLE, SPI_USE_SPI100);
437}
438
439void sb_disable_4dw_burst(void)
440{
441 uintptr_t base = sb_spibase();
442 write16((void *)base + SPI100_HOST_PREF_CONFIG,
443 read16((void *)base + SPI100_HOST_PREF_CONFIG)
444 & ~SPI_RD4DW_EN_HOST);
445}
446
Richard Spiegelbec44f22017-11-24 07:41:29 -0700447void sb_read_mode(u32 mode)
448{
449 uintptr_t base = sb_spibase();
450 write32((void *)base + SPI_CNTRL0,
451 (read32((void *)base + SPI_CNTRL0)
452 & ~SPI_READ_MODE_MASK) | mode);
453}
454
Garrett Kirkendall65753062018-03-07 16:12:11 -0600455/*
456 * Enable FCH to decode TPM associated Memory and IO regions
457 *
458 * Enable decoding of TPM cycles defined in TPM 1.2 spec
459 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
460 * 0x7e and 0xef-0xee.
461 * This function should be called if TPM is connected in any way to the FCH and
462 * conforms to the regions decoded.
463 * Absent any other routing configuration the TPM cycles will be claimed by the
464 * LPC bus
465 */
466void sb_tpm_decode(void)
467{
468 u32 value;
469
470 value = pci_read_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE);
471 value |= TPM_12_EN | TPM_LEGACY_EN;
472 pci_write_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE, value);
473}
474
475/*
476 * Enable FCH to decode TPM associated Memory and IO regions to SPI
477 *
478 * This should be used if TPM is connected to SPI bus.
479 * Assumes SPI address space is already configured via a call to sb_spibase().
480 */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700481void sb_tpm_decode_spi(void)
482{
Garrett Kirkendall65753062018-03-07 16:12:11 -0600483 /* Enable TPM decoding to FCH */
484 sb_tpm_decode();
485
486 /* Route TPM accesses to SPI */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700487 u32 spibase = pci_read_config32(SOC_LPC_DEV,
488 SPIROM_BASE_ADDRESS_REGISTER);
489 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER, spibase
490 | ROUTE_TPM_2_SPI);
491}
492
493/*
494 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
495 *
496 * Hardware should enable LPC ROM by pin straps. This function does not
497 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
498 *
499 * The southbridge power-on default is to map 512K ROM space.
500 *
501 */
502void sb_enable_rom(void)
503{
504 u8 reg8;
505
506 /*
507 * Decode variable LPC ROM address ranges 1 and 2.
508 * Bits 3-4 are not defined in any publicly available datasheet
509 */
510 reg8 = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
511 reg8 |= (1 << 3) | (1 << 4);
512 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, reg8);
513
514 /*
515 * LPC ROM address range 1:
516 * Enable LPC ROM range mirroring start at 0x000e(0000).
517 */
518 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_START, 0x000e);
519
520 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
521 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_END, 0x000f);
522
523 /*
524 * LPC ROM address range 2:
525 *
526 * Enable LPC ROM range start at:
527 * 0xfff8(0000): 512KB
528 * 0xfff0(0000): 1MB
529 * 0xffe0(0000): 2MB
530 * 0xffc0(0000): 4MB
531 */
532 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START, 0x10000
533 - (CONFIG_COREBOOT_ROMSIZE_KB >> 6));
534
535 /* Enable LPC ROM range end at 0xffff(ffff). */
536 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_END, 0xffff);
537}
538
Marc Jonescfb16802018-04-20 16:27:41 -0600539static void sb_lpc_early_setup(void)
540{
541 uint32_t dword;
542
543 /* Enable SPI prefetch */
544 dword = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
545 dword |= SPI_FROM_HOST_PREFETCH_EN | SPI_FROM_USB_PREFETCH_EN;
546 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, dword);
547
548 if (IS_ENABLED(CONFIG_STONEYRIDGE_LEGACY_FREE)) {
549 /* Decode SIOs at 2E/2F and 4E/4F */
550 dword = pci_read_config32(SOC_LPC_DEV,
551 LPC_IO_OR_MEM_DECODE_ENABLE);
552 dword |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
553 pci_write_config32(SOC_LPC_DEV,
554 LPC_IO_OR_MEM_DECODE_ENABLE, dword);
555 }
556}
557
Richard Spiegelbec44f22017-11-24 07:41:29 -0700558void bootblock_fch_early_init(void)
559{
560 sb_enable_rom();
561 sb_lpc_port80();
562 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600563 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500564 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600565 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500566 sb_acpi_mmio_decode();
Raul E Rangel5b058232018-06-28 16:31:45 -0600567 sb_enable_cf9_io();
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600568 sb_enable_legacy_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700569 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700570}
571
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600572void sb_enable(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600573{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600574 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600575}
576
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600577static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600578{
Marshall Dawson91b80412017-09-27 16:44:40 -0600579 u32 reg;
580
Marc Jones24484842017-05-04 21:17:45 -0600581 /* We use some of these ports in SMM regardless of whether or not
582 * ACPI tables are generated. Enable these ports indiscriminately.
583 */
584
585 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
586 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
587 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
588 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
589 /* CpuControl is in \_PR.CP00, 6 bytes */
590 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
591
592 if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600593 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600594 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600595 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600596
597 /* SMI on SlpTyp requires sending SMI before completion
598 * response of the I/O write. The BKDG also specifies
599 * clearing ForceStpClkRetry for SMI trapping.
600 */
601 reg = pm_read32(PM_PCI_CTRL);
602 reg |= FORCE_SLPSTATE_RETRY;
603 reg &= ~FORCE_STPCLK_RETRY;
604 pm_write32(PM_PCI_CTRL, reg);
605
606 /* Disable SlpTyp feature */
607 reg = pm_read8(PM_RST_CTRL1);
608 reg &= ~SLPTYPE_CONTROL_EN;
609 pm_write8(PM_RST_CTRL1, reg);
610
611 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600612 } else {
613 pm_write16(PM_ACPI_SMI_CMD, 0);
614 }
615
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700616 /* Decode ACPI registers and enable standard features */
617 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
618 PM_ACPI_GLOBAL_EN |
619 PM_ACPI_RTC_EN_EN |
620 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600621}
622
Marshall Dawson70f051f2018-03-20 10:27:41 -0600623static void print_num_status_bits(int num_bits, uint32_t status,
624 const char *const bit_names[])
625{
626 int i;
627
628 if (!status)
629 return;
630
631 for (i = num_bits - 1; i >= 0; i--) {
632 if (status & (1 << i)) {
633 if (bit_names[i])
634 printk(BIOS_DEBUG, "%s ", bit_names[i]);
635 else
636 printk(BIOS_DEBUG, "BIT%d ", i);
637 }
638 }
639}
640
641static uint16_t reset_pm1_status(void)
642{
Richard Spiegel572f4982018-05-25 15:49:33 -0700643 uint16_t pm1_sts = inw(ACPI_PM1_STS);
644 outw(pm1_sts, ACPI_PM1_STS);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600645 return pm1_sts;
646}
647
648static uint16_t print_pm1_status(uint16_t pm1_sts)
649{
650 static const char *const pm1_sts_bits[] = {
651 [0] = "TMROF",
652 [4] = "BMSTATUS",
653 [5] = "GBL",
654 [8] = "PWRBTN",
655 [10] = "RTC",
656 [14] = "PCIEXPWAK",
657 [15] = "WAK",
658 };
659
660 if (!pm1_sts)
661 return 0;
662
663 printk(BIOS_SPEW, "PM1_STS: ");
664 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
665 printk(BIOS_SPEW, "\n");
666
667 return pm1_sts;
668}
669
670static void sb_log_pm1_status(uint16_t pm1_sts)
671{
672 if (!IS_ENABLED(CONFIG_ELOG))
673 return;
674
Daniel Kurtzb6fdd222018-05-24 15:52:45 -0600675 if (pm1_sts & WAK_STS)
676 elog_add_event_byte(ELOG_TYPE_ACPI_WAKE,
677 acpi_is_wakeup_s3() ? ACPI_S3 : ACPI_S5);
678
Marshall Dawson70f051f2018-03-20 10:27:41 -0600679 if (pm1_sts & PWRBTN_STS)
680 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
681
682 if (pm1_sts & RTC_STS)
683 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
684
685 if (pm1_sts & PCIEXPWAK_STS)
686 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
687}
688
Richard Spiegel572f4982018-05-25 15:49:33 -0700689static void sb_save_sws(uint16_t pm1_status)
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700690{
Richard Spiegel35282a02018-06-14 14:57:54 -0700691 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700692 uint32_t reg32;
Richard Spiegel35282a02018-06-14 14:57:54 -0700693 uint16_t reg16;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700694
Richard Spiegel35282a02018-06-14 14:57:54 -0700695 sws = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(struct soc_power_reg));
696 if (sws == NULL)
697 return;
698 sws->pm1_sts = pm1_status;
699 sws->pm1_en = inw(ACPI_PM1_EN);
Richard Spiegel572f4982018-05-25 15:49:33 -0700700 reg32 = inl(ACPI_GPE0_STS);
701 outl(ACPI_GPE0_STS, reg32);
Richard Spiegel35282a02018-06-14 14:57:54 -0700702 sws->gpe0_sts = reg32;
703 sws->gpe0_en = inl(ACPI_GPE0_EN);
704 reg16 = inw(ACPI_PM1_CNT_BLK);
705 reg16 &= SLP_TYP;
706 sws->wake_from = reg16 >> SLP_TYP_SHIFT;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700707}
708
Marshall Dawson70f051f2018-03-20 10:27:41 -0600709static void sb_clear_pm1_status(void)
710{
711 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700712
713 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600714 sb_log_pm1_status(pm1_sts);
715 print_pm1_status(pm1_sts);
716}
717
Richard Spiegel572f4982018-05-25 15:49:33 -0700718static int get_index_bit(uint32_t value, uint16_t limit)
719{
720 uint16_t i;
721 uint32_t t;
722
Richard Spiegelef73cb82018-06-19 07:40:18 -0700723 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700724 return -1;
725
726 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
727 t = (1 << limit) - 1;
728 if ((value & t) == 0)
729 return -1;
730 t = 1;
731 for (i = 0; i < limit; i++) {
732 if (value & t)
733 break;
734 t <<= 1;
735 }
736 return i;
737}
738
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700739static void set_nvs_sws(void *unused)
740{
Richard Spiegel35282a02018-06-14 14:57:54 -0700741 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700742 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700743 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700744
Richard Spiegel35282a02018-06-14 14:57:54 -0700745 sws = cbmem_find(CBMEM_ID_POWER_STATE);
746 if (sws == NULL)
747 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700748 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
749 if (gnvs == NULL)
750 return;
751
Richard Spiegel35282a02018-06-14 14:57:54 -0700752 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700753 if (index < 0)
754 gnvs->pm1i = ~0ULL;
755 else
756 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700757
Richard Spiegel35282a02018-06-14 14:57:54 -0700758 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700759 if (index < 0)
760 gnvs->gpei = ~0ULL;
761 else
762 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700763}
764
765BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
766
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600767void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600768{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600769 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600770 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600771}
772
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600773void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600774{
Richard Spiegel6a389142018-03-05 14:28:10 -0700775 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
776
Richard Spiegel6a389142018-03-05 14:28:10 -0700777 if (IS_ENABLED(CONFIG_MAINBOARD_POWER_RESTORE))
778 restored_power = PM_RESTORE_S0_IF_PREV_S0;
779 pm_write8(PM_RTC_SHADOW, restored_power);
Marc Jones24484842017-05-04 21:17:45 -0600780}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600781
782/*
783 * Update the PCI devices with a valid IRQ number
784 * that is set in the mainboard PCI_IRQ structures.
785 */
786static void set_pci_irqs(void *unused)
787{
788 /* Write PCI_INTR regs 0xC00/0xC01 */
789 write_pci_int_table();
790
791 /* Write IRQs for all devicetree enabled devices */
792 write_pci_cfg_irqs();
793}
794
795/*
796 * Hook this function into the PCI state machine
797 * on entry into BS_DEV_ENABLE.
798 */
799BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);