blob: 63d8806fd0cd9cc53b48c6b5ec0a89f18514b026 [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{
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" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700126 { PIRQ_EHCI, "EHCI" },
127 { PIRQ_XHCI, "XHCI" },
128 { PIRQ_SATA, "SATA" },
129 { PIRQ_GPIO, "GPIO" },
130 { PIRQ_I2C0, "I2C0" },
131 { PIRQ_I2C1, "I2C1" },
132 { PIRQ_I2C2, "I2C2" },
133 { PIRQ_I2C3, "I2C3" },
134 { PIRQ_UART0, "UART0" },
135 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700136};
137
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700138/*
139 * Structure to simplify code obtaining the total of used wide IO
140 * registers and the size assigned to each.
141 */
142static struct wide_io_ioport_and_bits {
143 uint32_t enable;
144 uint16_t port;
145 uint8_t alt;
146} wio_io_en[TOTAL_WIDEIO_PORTS] = {
147 {
148 LPC_WIDEIO0_ENABLE,
149 LPC_WIDEIO_GENERIC_PORT,
150 LPC_ALT_WIDEIO0_ENABLE
151 },
152 {
153 LPC_WIDEIO1_ENABLE,
154 LPC_WIDEIO1_GENERIC_PORT,
155 LPC_ALT_WIDEIO1_ENABLE
156 },
157 {
158 LPC_WIDEIO2_ENABLE,
159 LPC_WIDEIO2_GENERIC_PORT,
160 LPC_ALT_WIDEIO2_ENABLE
161 }
162};
163
Richard Spiegel376dc822017-12-01 08:24:26 -0700164const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
165{
166 *size = ARRAY_SIZE(irq_association);
167 return irq_association;
168}
169
Richard Spiegelebf3aa82017-11-24 07:47:42 -0700170/**
171 * @brief Find the size of a particular wide IO
172 *
173 * @param index = index of desired wide IO
174 *
175 * @return size of desired wide IO
176 */
177uint16_t sb_wideio_size(int index)
178{
179 uint32_t enable_register;
180 uint16_t size = 0;
181 uint8_t alternate_register;
182
183 if (index >= TOTAL_WIDEIO_PORTS)
184 return size;
185 enable_register = pci_read_config32(SOC_LPC_DEV,
186 LPC_IO_OR_MEM_DECODE_ENABLE);
187 alternate_register = pci_read_config8(SOC_LPC_DEV,
188 LPC_ALT_WIDEIO_RANGE_ENABLE);
189 if (enable_register & wio_io_en[index].enable)
190 size = (alternate_register & wio_io_en[index].alt) ?
191 16 : 512;
192 return size;
193}
194
195/**
196 * @brief Identify if any LPC wide IO is covering the IO range
197 *
198 * @param start = start of IO range
199 * @param size = size of IO range
200 *
201 * @return Index of wide IO covering the range or error
202 */
203int sb_find_wideio_range(uint16_t start, uint16_t size)
204{
205 uint32_t enable_register;
206 int i, index = WIDEIO_RANGE_ERROR;
207 uint16_t end, current_size, start_wideio, end_wideio;
208
209 end = start + size;
210 enable_register = pci_read_config32(SOC_LPC_DEV,
211 LPC_IO_OR_MEM_DECODE_ENABLE);
212 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
213 current_size = sb_wideio_size(i);
214 if (current_size == 0)
215 continue;
216 start_wideio = pci_read_config16(SOC_LPC_DEV,
217 wio_io_en[i].port);
218 end_wideio = start_wideio + current_size;
219 if ((start >= start_wideio) && (end <= end_wideio)) {
220 index = i;
221 break;
222 }
223 }
224 return index;
225}
226
227/**
228 * @brief Program a LPC wide IO to support an IO range
229 *
230 * @param start = start of range to be routed through wide IO
231 * @param size = size of range to be routed through wide IO
232 *
233 * @return Index of wide IO register used or error
234 */
235int sb_set_wideio_range(uint16_t start, uint16_t size)
236{
237 int i, index = WIDEIO_RANGE_ERROR;
238 uint32_t enable_register;
239 uint8_t alternate_register;
240
241 enable_register = pci_read_config32(SOC_LPC_DEV,
242 LPC_IO_OR_MEM_DECODE_ENABLE);
243 alternate_register = pci_read_config8(SOC_LPC_DEV,
244 LPC_ALT_WIDEIO_RANGE_ENABLE);
245 for (i = 0; i < TOTAL_WIDEIO_PORTS; i++) {
246 if (enable_register & wio_io_en[i].enable)
247 continue;
248 index = i;
249 pci_write_config16(SOC_LPC_DEV, wio_io_en[i].port, start);
250 enable_register |= wio_io_en[i].enable;
251 pci_write_config32(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE,
252 enable_register);
253 if (size <= 16)
254 alternate_register |= wio_io_en[i].alt;
255 else
256 alternate_register &= ~wio_io_en[i].alt;
257 pci_write_config8(SOC_LPC_DEV,
258 LPC_ALT_WIDEIO_RANGE_ENABLE,
259 alternate_register);
260 break;
261 }
262 return index;
263}
264
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600265static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700266{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600267 uint8_t byte;
268 uint8_t *register_pointer = (uint8_t *)(uintptr_t)AOAC_MMIO_BASE
269 + aoac_device_control_register;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700270
271 /* Power on the UART and AMBA devices */
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600272 byte = read8(register_pointer);
273 byte |= FCH_AOAC_PWR_ON_DEV;
274 write8(register_pointer, byte);
275}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700276
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600277static bool is_aoac_device_enabled(int aoac_device_status_register)
278{
279 uint8_t byte;
280 byte = read8((uint8_t *)(uintptr_t)AOAC_MMIO_BASE
281 + aoac_device_status_register);
282 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
283 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
284 return true;
285 else
286 return false;
287}
288
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700289void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600290{
291 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700292 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600293
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700294 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
295 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700296
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700297 /* Wait for AOAC devices to indicate power and clock OK */
298 do {
299 udelay(100);
300 status = true;
301 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
302 status &= is_aoac_device_enabled(aoac_devs[i].status);
303 } while (!status);
304}
305
Richard Spiegelbec44f22017-11-24 07:41:29 -0700306void sb_pci_port80(void)
307{
308 u8 byte;
309
310 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
311 byte &= ~DECODE_IO_PORT_ENABLE4_H; /* disable lpc port 80 */
312 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
313}
314
315void sb_lpc_port80(void)
316{
317 u8 byte;
318
319 /* Enable LPC controller */
320 outb(PM_LPC_GATING, PM_INDEX);
321 byte = inb(PM_DATA);
322 byte |= PM_LPC_ENABLE;
323 outb(PM_LPC_GATING, PM_INDEX);
324 outb(byte, PM_DATA);
325
326 /* Enable port 80 LPC decode in pci function 3 configuration space. */
327 byte = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH);
328 byte |= DECODE_IO_PORT_ENABLE4_H; /* enable port 80 */
329 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DEC_EN_HIGH, byte);
330}
331
332void sb_lpc_decode(void)
333{
334 u32 tmp = 0;
335
336 /* Enable I/O decode to LPC bus */
337 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
338 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
339 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
340 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
341 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
342 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
343 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
344 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
345 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
346 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
347 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
348 | DECODE_ENABLE_ADLIB_PORT;
349
350 pci_write_config32(SOC_LPC_DEV, LPC_IO_PORT_DECODE_ENABLE, tmp);
351}
352
Garrett Kirkendall9858bd22018-03-07 15:38:14 -0600353void sb_acpi_mmio_decode(void)
354{
355 uint8_t byte;
356
357 /* Enable ACPI MMIO range 0xfed80000 - 0xfed81fff */
358 outb(PM_ISA_CONTROL, PM_INDEX);
359 byte = inb(PM_DATA);
360 byte |= MMIO_EN;
361 outb(PM_ISA_CONTROL, PM_INDEX);
362 outb(byte, PM_DATA);
363}
364
Raul E Rangel5b058232018-06-28 16:31:45 -0600365static void sb_enable_cf9_io(void)
366{
367 uint32_t reg = pm_read32(PM_DECODE_EN);
368
369 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
370}
371
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600372static void sb_enable_legacy_io(void)
373{
374 uint32_t reg = pm_read32(PM_DECODE_EN);
375
376 pm_write32(PM_DECODE_EN, reg | LEGACY_IO_EN);
377}
378
Richard Spiegelbec44f22017-11-24 07:41:29 -0700379void sb_clk_output_48Mhz(void)
380{
381 u32 ctrl;
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600382 u32 *misc_clk_cntl_1_ptr = (u32 *)(uintptr_t)(MISC_MMIO_BASE
383 + MISC_MISC_CLK_CNTL_1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700384
385 /*
386 * Enable the X14M_25M_48M_OSC pin and leaving it at it's default so
387 * 48Mhz will be on ball AP13 (FT3b package)
388 */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600389 ctrl = read32(misc_clk_cntl_1_ptr);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700390
391 /* clear the OSCOUT1_ClkOutputEnb to enable the 48 Mhz clock */
Garrett Kirkendalld2558302018-03-06 09:05:20 -0600392 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
393 write32(misc_clk_cntl_1_ptr, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700394}
395
396static uintptr_t sb_spibase(void)
397{
398 u32 base, enables;
399
400 /* Make sure the base address is predictable */
401 base = pci_read_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER);
402 enables = base & 0xf;
403 base &= ~0x3f;
404
405 if (!base) {
406 base = SPI_BASE_ADDRESS;
407 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER,
408 base | enables | SPI_ROM_ENABLE);
409 /* PCI_COMMAND_MEMORY is read-only and enabled. */
410 }
411 return (uintptr_t)base;
412}
413
414void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
415{
416 uintptr_t base = sb_spibase();
417 write16((void *)base + SPI100_SPEED_CONFIG,
418 (norm << SPI_NORM_SPEED_NEW_SH) |
419 (fast << SPI_FAST_SPEED_NEW_SH) |
420 (alt << SPI_ALT_SPEED_NEW_SH) |
421 (tpm << SPI_TPM_SPEED_NEW_SH));
422 write16((void *)base + SPI100_ENABLE, SPI_USE_SPI100);
423}
424
425void sb_disable_4dw_burst(void)
426{
427 uintptr_t base = sb_spibase();
428 write16((void *)base + SPI100_HOST_PREF_CONFIG,
429 read16((void *)base + SPI100_HOST_PREF_CONFIG)
430 & ~SPI_RD4DW_EN_HOST);
431}
432
Richard Spiegelbec44f22017-11-24 07:41:29 -0700433void sb_read_mode(u32 mode)
434{
435 uintptr_t base = sb_spibase();
436 write32((void *)base + SPI_CNTRL0,
437 (read32((void *)base + SPI_CNTRL0)
438 & ~SPI_READ_MODE_MASK) | mode);
439}
440
Garrett Kirkendall65753062018-03-07 16:12:11 -0600441/*
442 * Enable FCH to decode TPM associated Memory and IO regions
443 *
444 * Enable decoding of TPM cycles defined in TPM 1.2 spec
445 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
446 * 0x7e and 0xef-0xee.
447 * This function should be called if TPM is connected in any way to the FCH and
448 * conforms to the regions decoded.
449 * Absent any other routing configuration the TPM cycles will be claimed by the
450 * LPC bus
451 */
452void sb_tpm_decode(void)
453{
454 u32 value;
455
456 value = pci_read_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE);
457 value |= TPM_12_EN | TPM_LEGACY_EN;
458 pci_write_config32(SOC_LPC_DEV, LPC_TRUSTED_PLATFORM_MODULE, value);
459}
460
461/*
462 * Enable FCH to decode TPM associated Memory and IO regions to SPI
463 *
464 * This should be used if TPM is connected to SPI bus.
465 * Assumes SPI address space is already configured via a call to sb_spibase().
466 */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700467void sb_tpm_decode_spi(void)
468{
Garrett Kirkendall65753062018-03-07 16:12:11 -0600469 /* Enable TPM decoding to FCH */
470 sb_tpm_decode();
471
472 /* Route TPM accesses to SPI */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700473 u32 spibase = pci_read_config32(SOC_LPC_DEV,
474 SPIROM_BASE_ADDRESS_REGISTER);
475 pci_write_config32(SOC_LPC_DEV, SPIROM_BASE_ADDRESS_REGISTER, spibase
476 | ROUTE_TPM_2_SPI);
477}
478
479/*
480 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
481 *
482 * Hardware should enable LPC ROM by pin straps. This function does not
483 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
484 *
485 * The southbridge power-on default is to map 512K ROM space.
486 *
487 */
488void sb_enable_rom(void)
489{
490 u8 reg8;
491
492 /*
493 * Decode variable LPC ROM address ranges 1 and 2.
494 * Bits 3-4 are not defined in any publicly available datasheet
495 */
496 reg8 = pci_read_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
497 reg8 |= (1 << 3) | (1 << 4);
498 pci_write_config8(SOC_LPC_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, reg8);
499
500 /*
501 * LPC ROM address range 1:
502 * Enable LPC ROM range mirroring start at 0x000e(0000).
503 */
504 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_START, 0x000e);
505
506 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
507 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE1_END, 0x000f);
508
509 /*
510 * LPC ROM address range 2:
511 *
512 * Enable LPC ROM range start at:
513 * 0xfff8(0000): 512KB
514 * 0xfff0(0000): 1MB
515 * 0xffe0(0000): 2MB
516 * 0xffc0(0000): 4MB
517 */
518 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START, 0x10000
519 - (CONFIG_COREBOOT_ROMSIZE_KB >> 6));
520
521 /* Enable LPC ROM range end at 0xffff(ffff). */
522 pci_write_config16(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_END, 0xffff);
523}
524
Marc Jonescfb16802018-04-20 16:27:41 -0600525static void sb_lpc_early_setup(void)
526{
527 uint32_t dword;
528
529 /* Enable SPI prefetch */
530 dword = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
531 dword |= SPI_FROM_HOST_PREFETCH_EN | SPI_FROM_USB_PREFETCH_EN;
532 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, dword);
533
534 if (IS_ENABLED(CONFIG_STONEYRIDGE_LEGACY_FREE)) {
535 /* Decode SIOs at 2E/2F and 4E/4F */
536 dword = pci_read_config32(SOC_LPC_DEV,
537 LPC_IO_OR_MEM_DECODE_ENABLE);
538 dword |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
539 pci_write_config32(SOC_LPC_DEV,
540 LPC_IO_OR_MEM_DECODE_ENABLE, dword);
541 }
542}
543
Richard Spiegelbec44f22017-11-24 07:41:29 -0700544void bootblock_fch_early_init(void)
545{
546 sb_enable_rom();
547 sb_lpc_port80();
548 sb_lpc_decode();
Marc Jonescfb16802018-04-20 16:27:41 -0600549 sb_lpc_early_setup();
Garrett Kirkendall64294eb2018-03-16 13:00:46 -0500550 sb_spibase();
Marc Jonescfb16802018-04-20 16:27:41 -0600551 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Garrett Kirkendalle7513e0d2018-03-14 12:01:36 -0500552 sb_acpi_mmio_decode();
Raul E Rangel5b058232018-06-28 16:31:45 -0600553 sb_enable_cf9_io();
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600554 sb_enable_legacy_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700555 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700556}
557
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600558void sb_enable(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600559{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600560 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600561}
562
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600563static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600564{
Marshall Dawson91b80412017-09-27 16:44:40 -0600565 u32 reg;
566
Marc Jones24484842017-05-04 21:17:45 -0600567 /* We use some of these ports in SMM regardless of whether or not
568 * ACPI tables are generated. Enable these ports indiscriminately.
569 */
570
571 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
572 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
573 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
574 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
575 /* CpuControl is in \_PR.CP00, 6 bytes */
576 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
577
578 if (IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600579 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600580 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600581 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600582
583 /* SMI on SlpTyp requires sending SMI before completion
584 * response of the I/O write. The BKDG also specifies
585 * clearing ForceStpClkRetry for SMI trapping.
586 */
587 reg = pm_read32(PM_PCI_CTRL);
588 reg |= FORCE_SLPSTATE_RETRY;
589 reg &= ~FORCE_STPCLK_RETRY;
590 pm_write32(PM_PCI_CTRL, reg);
591
592 /* Disable SlpTyp feature */
593 reg = pm_read8(PM_RST_CTRL1);
594 reg &= ~SLPTYPE_CONTROL_EN;
595 pm_write8(PM_RST_CTRL1, reg);
596
597 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600598 } else {
599 pm_write16(PM_ACPI_SMI_CMD, 0);
600 }
601
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700602 /* Decode ACPI registers and enable standard features */
603 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
604 PM_ACPI_GLOBAL_EN |
605 PM_ACPI_RTC_EN_EN |
606 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600607}
608
Marshall Dawson70f051f2018-03-20 10:27:41 -0600609static void print_num_status_bits(int num_bits, uint32_t status,
610 const char *const bit_names[])
611{
612 int i;
613
614 if (!status)
615 return;
616
617 for (i = num_bits - 1; i >= 0; i--) {
618 if (status & (1 << i)) {
619 if (bit_names[i])
620 printk(BIOS_DEBUG, "%s ", bit_names[i]);
621 else
622 printk(BIOS_DEBUG, "BIT%d ", i);
623 }
624 }
625}
626
627static uint16_t reset_pm1_status(void)
628{
Richard Spiegel572f4982018-05-25 15:49:33 -0700629 uint16_t pm1_sts = inw(ACPI_PM1_STS);
630 outw(pm1_sts, ACPI_PM1_STS);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600631 return pm1_sts;
632}
633
634static uint16_t print_pm1_status(uint16_t pm1_sts)
635{
636 static const char *const pm1_sts_bits[] = {
637 [0] = "TMROF",
638 [4] = "BMSTATUS",
639 [5] = "GBL",
640 [8] = "PWRBTN",
641 [10] = "RTC",
642 [14] = "PCIEXPWAK",
643 [15] = "WAK",
644 };
645
646 if (!pm1_sts)
647 return 0;
648
649 printk(BIOS_SPEW, "PM1_STS: ");
650 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
651 printk(BIOS_SPEW, "\n");
652
653 return pm1_sts;
654}
655
656static void sb_log_pm1_status(uint16_t pm1_sts)
657{
658 if (!IS_ENABLED(CONFIG_ELOG))
659 return;
660
Daniel Kurtzb6fdd222018-05-24 15:52:45 -0600661 if (pm1_sts & WAK_STS)
662 elog_add_event_byte(ELOG_TYPE_ACPI_WAKE,
663 acpi_is_wakeup_s3() ? ACPI_S3 : ACPI_S5);
664
Marshall Dawson70f051f2018-03-20 10:27:41 -0600665 if (pm1_sts & PWRBTN_STS)
666 elog_add_event_wake(ELOG_WAKE_SOURCE_PWRBTN, 0);
667
668 if (pm1_sts & RTC_STS)
669 elog_add_event_wake(ELOG_WAKE_SOURCE_RTC, 0);
670
671 if (pm1_sts & PCIEXPWAK_STS)
672 elog_add_event_wake(ELOG_WAKE_SOURCE_PCIE, 0);
673}
674
Richard Spiegel572f4982018-05-25 15:49:33 -0700675static void sb_save_sws(uint16_t pm1_status)
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700676{
Richard Spiegel35282a02018-06-14 14:57:54 -0700677 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700678 uint32_t reg32;
Richard Spiegel35282a02018-06-14 14:57:54 -0700679 uint16_t reg16;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700680
Richard Spiegel35282a02018-06-14 14:57:54 -0700681 sws = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(struct soc_power_reg));
682 if (sws == NULL)
683 return;
684 sws->pm1_sts = pm1_status;
685 sws->pm1_en = inw(ACPI_PM1_EN);
Richard Spiegel572f4982018-05-25 15:49:33 -0700686 reg32 = inl(ACPI_GPE0_STS);
687 outl(ACPI_GPE0_STS, reg32);
Richard Spiegel35282a02018-06-14 14:57:54 -0700688 sws->gpe0_sts = reg32;
689 sws->gpe0_en = inl(ACPI_GPE0_EN);
690 reg16 = inw(ACPI_PM1_CNT_BLK);
691 reg16 &= SLP_TYP;
692 sws->wake_from = reg16 >> SLP_TYP_SHIFT;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700693}
694
Marshall Dawson70f051f2018-03-20 10:27:41 -0600695static void sb_clear_pm1_status(void)
696{
697 uint16_t pm1_sts = reset_pm1_status();
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700698
699 sb_save_sws(pm1_sts);
Marshall Dawson70f051f2018-03-20 10:27:41 -0600700 sb_log_pm1_status(pm1_sts);
701 print_pm1_status(pm1_sts);
702}
703
Richard Spiegel572f4982018-05-25 15:49:33 -0700704static int get_index_bit(uint32_t value, uint16_t limit)
705{
706 uint16_t i;
707 uint32_t t;
708
Richard Spiegelef73cb82018-06-19 07:40:18 -0700709 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700710 return -1;
711
712 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
713 t = (1 << limit) - 1;
714 if ((value & t) == 0)
715 return -1;
716 t = 1;
717 for (i = 0; i < limit; i++) {
718 if (value & t)
719 break;
720 t <<= 1;
721 }
722 return i;
723}
724
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700725static void set_nvs_sws(void *unused)
726{
Richard Spiegel35282a02018-06-14 14:57:54 -0700727 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700728 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700729 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700730
Richard Spiegel35282a02018-06-14 14:57:54 -0700731 sws = cbmem_find(CBMEM_ID_POWER_STATE);
732 if (sws == NULL)
733 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700734 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
735 if (gnvs == NULL)
736 return;
737
Richard Spiegel35282a02018-06-14 14:57:54 -0700738 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700739 if (index < 0)
740 gnvs->pm1i = ~0ULL;
741 else
742 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700743
Richard Spiegel35282a02018-06-14 14:57:54 -0700744 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700745 if (index < 0)
746 gnvs->gpei = ~0ULL;
747 else
748 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700749}
750
751BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
752
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600753void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600754{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600755 sb_init_acpi_ports();
Marshall Dawson70f051f2018-03-20 10:27:41 -0600756 sb_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600757}
758
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600759void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600760{
Richard Spiegel6a389142018-03-05 14:28:10 -0700761 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
762
Richard Spiegel6a389142018-03-05 14:28:10 -0700763 if (IS_ENABLED(CONFIG_MAINBOARD_POWER_RESTORE))
764 restored_power = PM_RESTORE_S0_IF_PREV_S0;
765 pm_write8(PM_RTC_SHADOW, restored_power);
Marc Jones24484842017-05-04 21:17:45 -0600766}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600767
768/*
769 * Update the PCI devices with a valid IRQ number
770 * that is set in the mainboard PCI_IRQ structures.
771 */
772static void set_pci_irqs(void *unused)
773{
774 /* Write PCI_INTR regs 0xC00/0xC01 */
775 write_pci_int_table();
776
777 /* Write IRQs for all devicetree enabled devices */
778 write_pci_cfg_irqs();
779}
780
781/*
782 * Hook this function into the PCI state machine
783 * on entry into BS_DEV_ENABLE.
784 */
785BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);