blob: 10a23f2737fdd16e803d06cbe7ff87097c88b308 [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020017#include <device/mmio.h>
Marshall Dawson8a906df2017-06-13 14:19:02 -060018#include <bootstate.h>
Marshall Dawsone9b862e2017-09-22 15:14:46 -060019#include <cpu/x86/smm.h>
Marc Jones24484842017-05-04 21:17:45 -060020#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
23#include <device/pci_ops.h>
24#include <cbmem.h>
Richard Spiegel2bbc3dc2017-12-06 16:14:58 -070025#include <amdblocks/amd_pci_util.h>
Richard Spiegel71081072018-07-26 10:51:38 -070026#include <amdblocks/agesawrapper.h>
Nico Huber73c11192018-10-06 18:20:47 +020027#include <amdblocks/reset.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060028#include <amdblocks/acpimmio.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060029#include <amdblocks/lpc.h>
Marshall Dawson4ee83b22019-05-03 11:44:22 -060030#include <amdblocks/acpi.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060031#include <soc/southbridge.h>
Richard Spiegelb40e1932018-10-24 12:51:21 -070032#include <soc/smbus.h>
Marc Jones24484842017-05-04 21:17:45 -060033#include <soc/smi.h>
Richard Spiegel376dc822017-12-01 08:24:26 -070034#include <soc/amd_pci_int_defs.h>
Richard Spiegelbec44f22017-11-24 07:41:29 -070035#include <delay.h>
36#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070037#include <agesa_headers.h>
Richard Spiegeldbee8ae2018-05-09 17:34:04 -070038#include <soc/nvs.h>
Elyes HAOUAS27d02d82019-05-15 21:11:39 +020039#include <types.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070040
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070041/*
42 * Table of devices that need their AOAC registers enabled and waited
43 * upon (usually about .55 milliseconds). Instead of individual delays
44 * waiting for each device to become available, a single delay will be
Richard Spiegel6dfbb592018-03-15 15:45:44 -070045 * executed.
Richard Spiegel0e0e93c2018-03-13 10:19:51 -070046 */
47const static struct stoneyridge_aoac aoac_devs[] = {
48 { (FCH_AOAC_D3_CONTROL_UART0 + CONFIG_UART_FOR_CONSOLE * 2),
49 (FCH_AOAC_D3_STATE_UART0 + CONFIG_UART_FOR_CONSOLE * 2) },
50 { FCH_AOAC_D3_CONTROL_AMBA, FCH_AOAC_D3_STATE_AMBA },
51 { FCH_AOAC_D3_CONTROL_I2C0, FCH_AOAC_D3_STATE_I2C0 },
52 { FCH_AOAC_D3_CONTROL_I2C1, FCH_AOAC_D3_STATE_I2C1 },
53 { FCH_AOAC_D3_CONTROL_I2C2, FCH_AOAC_D3_STATE_I2C2 },
54 { FCH_AOAC_D3_CONTROL_I2C3, FCH_AOAC_D3_STATE_I2C3 }
55};
56
Marshall Dawson2942db62017-12-14 10:00:27 -070057static int is_sata_config(void)
58{
Richard Spiegelbdd272a2018-10-16 13:53:05 -070059 return !((SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE)
60 || (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE));
Marshall Dawson2942db62017-12-14 10:00:27 -070061}
62
Richard Spiegel7ea8e022018-01-16 14:40:10 -070063static inline int sb_sata_enable(void)
64{
65 /* True if IDE or AHCI. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070066 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
67 (SataAhci == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070068}
69
70static inline int sb_ide_enable(void)
71{
72 /* True if IDE or LEGACY IDE. */
Richard Spiegelbdd272a2018-10-16 13:53:05 -070073 return (SataNativeIde == CONFIG_STONEYRIDGE_SATA_MODE) ||
74 (SataLegacyIde == CONFIG_STONEYRIDGE_SATA_MODE);
Richard Spiegel7ea8e022018-01-16 14:40:10 -070075}
76
Marshall Dawson2942db62017-12-14 10:00:27 -070077void SetFchResetParams(FCH_RESET_INTERFACE *params)
78{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030079 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Julius Wernercd49cce2019-03-05 16:53:33 -080080 params->Xhci0Enable = CONFIG(STONEYRIDGE_XHCI_ENABLE);
Richard Spiegelbb18b432018-08-03 10:37:28 -070081 if (dev && dev->enabled) {
82 params->SataEnable = sb_sata_enable();
83 params->IdeEnable = sb_ide_enable();
84 } else {
85 params->SataEnable = FALSE;
86 params->IdeEnable = FALSE;
87 }
Marshall Dawson2942db62017-12-14 10:00:27 -070088}
89
90void SetFchEnvParams(FCH_INTERFACE *params)
91{
Kyösti Mälkkie7377552018-06-21 16:20:55 +030092 const struct device *dev = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson2942db62017-12-14 10:00:27 -070093 params->AzaliaController = AzEnable;
94 params->SataClass = CONFIG_STONEYRIDGE_SATA_MODE;
Richard Spiegelbb18b432018-08-03 10:37:28 -070095 if (dev && dev->enabled) {
96 params->SataEnable = is_sata_config();
97 params->IdeEnable = !params->SataEnable;
98 params->SataIdeMode = (CONFIG_STONEYRIDGE_SATA_MODE ==
99 SataLegacyIde);
100 } else {
101 params->SataEnable = FALSE;
102 params->IdeEnable = FALSE;
103 params->SataIdeMode = FALSE;
104 }
Marshall Dawson2942db62017-12-14 10:00:27 -0700105}
106
107void SetFchMidParams(FCH_INTERFACE *params)
108{
109 SetFchEnvParams(params);
110}
Marc Jones24484842017-05-04 21:17:45 -0600111
Richard Spiegel376dc822017-12-01 08:24:26 -0700112/*
113 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100114 * provides a visible association with the index, therefore helping
Richard Spiegel376dc822017-12-01 08:24:26 -0700115 * maintainability of table. If a new index/name is defined in
116 * amd_pci_int_defs.h, just add the pair at the end of this table.
117 * Order is not important.
118 */
119const static struct irq_idx_name irq_association[] = {
Richard Spiegele89d4442017-12-08 07:52:42 -0700120 { PIRQ_A, "INTA#" },
121 { PIRQ_B, "INTB#" },
122 { PIRQ_C, "INTC#" },
123 { PIRQ_D, "INTD#" },
124 { PIRQ_E, "INTE#" },
125 { PIRQ_F, "INTF#" },
126 { PIRQ_G, "INTG#" },
127 { PIRQ_H, "INTH#" },
128 { PIRQ_MISC, "Misc" },
129 { PIRQ_MISC0, "Misc0" },
130 { PIRQ_MISC1, "Misc1" },
131 { PIRQ_MISC2, "Misc2" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700132 { PIRQ_SIRQA, "Ser IRQ INTA" },
133 { PIRQ_SIRQB, "Ser IRQ INTB" },
134 { PIRQ_SIRQC, "Ser IRQ INTC" },
135 { PIRQ_SIRQD, "Ser IRQ INTD" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700136 { PIRQ_SCI, "SCI" },
137 { PIRQ_SMBUS, "SMBUS" },
138 { PIRQ_ASF, "ASF" },
139 { PIRQ_HDA, "HDA" },
140 { PIRQ_FC, "FC" },
141 { PIRQ_PMON, "PerMon" },
142 { PIRQ_SD, "SD" },
143 { PIRQ_SDIO, "SDIOt" },
Richard Spiegele89d4442017-12-08 07:52:42 -0700144 { PIRQ_EHCI, "EHCI" },
145 { PIRQ_XHCI, "XHCI" },
146 { PIRQ_SATA, "SATA" },
147 { PIRQ_GPIO, "GPIO" },
148 { PIRQ_I2C0, "I2C0" },
149 { PIRQ_I2C1, "I2C1" },
150 { PIRQ_I2C2, "I2C2" },
151 { PIRQ_I2C3, "I2C3" },
152 { PIRQ_UART0, "UART0" },
153 { PIRQ_UART1, "UART1" },
Richard Spiegel376dc822017-12-01 08:24:26 -0700154};
155
156const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
157{
158 *size = ARRAY_SIZE(irq_association);
159 return irq_association;
160}
161
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600162static void power_on_aoac_device(int aoac_device_control_register)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700163{
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600164 uint8_t byte;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700165
166 /* Power on the UART and AMBA devices */
Marshall Dawsonb435d442019-05-01 21:17:20 -0600167 byte = aoac_read8(aoac_device_control_register);
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600168 byte |= FCH_AOAC_PWR_ON_DEV;
Marshall Dawsonb435d442019-05-01 21:17:20 -0600169 aoac_write8(aoac_device_control_register, byte);
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600170}
Richard Spiegelbec44f22017-11-24 07:41:29 -0700171
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600172static bool is_aoac_device_enabled(int aoac_device_status_register)
173{
174 uint8_t byte;
Marshall Dawsonb435d442019-05-01 21:17:20 -0600175
176 byte = aoac_read8(aoac_device_status_register);
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600177 byte &= (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE);
178 if (byte == (FCH_AOAC_PWR_RST_STATE | FCH_AOAC_RST_CLK_OK_STATE))
179 return true;
180 else
181 return false;
182}
183
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700184void enable_aoac_devices(void)
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600185{
186 bool status;
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700187 int i;
Garrett Kirkendalla0ff6fc2018-03-06 09:23:47 -0600188
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700189 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
190 power_on_aoac_device(aoac_devs[i].enable);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700191
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700192 /* Wait for AOAC devices to indicate power and clock OK */
193 do {
194 udelay(100);
195 status = true;
196 for (i = 0; i < ARRAY_SIZE(aoac_devs); i++)
197 status &= is_aoac_device_enabled(aoac_devs[i].status);
198 } while (!status);
199}
200
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600201static void sb_enable_lpc(void)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700202{
203 u8 byte;
204
205 /* Enable LPC controller */
Marshall Dawson939bfcc2019-05-05 15:39:40 -0600206 byte = pm_io_read8(PM_LPC_GATING);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700207 byte |= PM_LPC_ENABLE;
Marshall Dawson939bfcc2019-05-05 15:39:40 -0600208 pm_io_write8(PM_LPC_GATING, byte);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700209}
210
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600211static void sb_lpc_decode(void)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700212{
213 u32 tmp = 0;
214
215 /* Enable I/O decode to LPC bus */
216 tmp = DECODE_ENABLE_PARALLEL_PORT0 | DECODE_ENABLE_PARALLEL_PORT2
217 | DECODE_ENABLE_PARALLEL_PORT4 | DECODE_ENABLE_SERIAL_PORT0
218 | DECODE_ENABLE_SERIAL_PORT1 | DECODE_ENABLE_SERIAL_PORT2
219 | DECODE_ENABLE_SERIAL_PORT3 | DECODE_ENABLE_SERIAL_PORT4
220 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT6
221 | DECODE_ENABLE_SERIAL_PORT7 | DECODE_ENABLE_AUDIO_PORT0
222 | DECODE_ENABLE_AUDIO_PORT1 | DECODE_ENABLE_AUDIO_PORT2
223 | DECODE_ENABLE_AUDIO_PORT3 | DECODE_ENABLE_MSS_PORT2
224 | DECODE_ENABLE_MSS_PORT3 | DECODE_ENABLE_FDC_PORT0
225 | DECODE_ENABLE_FDC_PORT1 | DECODE_ENABLE_GAME_PORT
226 | DECODE_ENABLE_KBC_PORT | DECODE_ENABLE_ACPIUC_PORT
227 | DECODE_ENABLE_ADLIB_PORT;
228
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600229 /* Decode SIOs at 2E/2F and 4E/4F */
230 if (CONFIG(STONEYRIDGE_LEGACY_FREE))
231 tmp |= DECODE_ALTERNATE_SIO_ENABLE | DECODE_SIO_ENABLE;
232
233 lpc_enable_decode(tmp);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700234}
235
Raul E Rangel5b058232018-06-28 16:31:45 -0600236static void sb_enable_cf9_io(void)
237{
238 uint32_t reg = pm_read32(PM_DECODE_EN);
239
240 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
241}
242
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600243static void sb_enable_legacy_io(void)
244{
245 uint32_t reg = pm_read32(PM_DECODE_EN);
246
247 pm_write32(PM_DECODE_EN, reg | LEGACY_IO_EN);
248}
249
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700250void sb_clk_output_48Mhz(u32 osc)
Richard Spiegelbec44f22017-11-24 07:41:29 -0700251{
252 u32 ctrl;
253
254 /*
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700255 * Clear the disable for OSCOUT1 (signal typically named XnnM_25M_48M)
256 * or OSCOUT2 (USBCLK/25M_48M_OSC). The frequency defaults to 48MHz.
Richard Spiegelbec44f22017-11-24 07:41:29 -0700257 */
Marshall Dawsonb4b9efc2019-05-01 17:33:42 -0600258 ctrl = misc_read32(MISC_CLK_CNTL1);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700259
Richard Spiegelc93d4ab2019-02-12 19:17:02 -0700260 switch (osc) {
261 case 1:
262 ctrl &= ~OSCOUT1_CLK_OUTPUT_ENB;
263 break;
264 case 2:
265 ctrl &= ~OSCOUT2_CLK_OUTPUT_ENB;
266 break;
267 default:
268 return; /* do nothing if invalid */
269 }
Marshall Dawsonb4b9efc2019-05-01 17:33:42 -0600270 misc_write32(MISC_CLK_CNTL1, ctrl);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700271}
272
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600273static uintptr_t sb_init_spi_base(void)
274{
275 uintptr_t base;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700276
277 /* Make sure the base address is predictable */
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600278 base = lpc_get_spibase();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700279
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600280 if (base)
281 return base;
282
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600283 lpc_set_spibase(SPI_BASE_ADDRESS, SPI_ROM_ENABLE);
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600284 return SPI_BASE_ADDRESS;
Richard Spiegelbec44f22017-11-24 07:41:29 -0700285}
286
287void sb_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
288{
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600289 uintptr_t base = sb_init_spi_base();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700290 write16((void *)(base + SPI100_SPEED_CONFIG),
Richard Spiegelbec44f22017-11-24 07:41:29 -0700291 (norm << SPI_NORM_SPEED_NEW_SH) |
292 (fast << SPI_FAST_SPEED_NEW_SH) |
293 (alt << SPI_ALT_SPEED_NEW_SH) |
294 (tpm << SPI_TPM_SPEED_NEW_SH));
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700295 write16((void *)(base + SPI100_ENABLE), SPI_USE_SPI100);
Richard Spiegelbec44f22017-11-24 07:41:29 -0700296}
297
298void sb_disable_4dw_burst(void)
299{
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600300 uintptr_t base = sb_init_spi_base();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700301 write16((void *)(base + SPI100_HOST_PREF_CONFIG),
302 read16((void *)(base + SPI100_HOST_PREF_CONFIG))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700303 & ~SPI_RD4DW_EN_HOST);
304}
305
Richard Spiegelbec44f22017-11-24 07:41:29 -0700306void sb_read_mode(u32 mode)
307{
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600308 uintptr_t base = sb_init_spi_base();
Richard Spiegel9f25e9d2018-10-29 08:01:53 -0700309 write32((void *)(base + SPI_CNTRL0),
310 (read32((void *)(base + SPI_CNTRL0))
Richard Spiegelbec44f22017-11-24 07:41:29 -0700311 & ~SPI_READ_MODE_MASK) | mode);
312}
313
Raul E Rangel79053412018-08-06 10:40:02 -0600314static void setup_spread_spectrum(int *reboot)
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600315{
316 uint16_t rstcfg = pm_read16(PWR_RESET_CFG);
317
318 rstcfg &= ~TOGGLE_ALL_PWR_GOOD;
319 pm_write16(PWR_RESET_CFG, rstcfg);
320
321 uint32_t cntl1 = misc_read32(MISC_CLK_CNTL1);
322
323 if (cntl1 & CG1PLL_FBDIV_TEST) {
324 printk(BIOS_DEBUG, "Spread spectrum is ready\n");
325 misc_write32(MISC_CGPLL_CONFIG1,
326 misc_read32(MISC_CGPLL_CONFIG1) |
327 CG1PLL_SPREAD_SPECTRUM_ENABLE);
328
329 return;
330 }
331
332 printk(BIOS_DEBUG, "Setting up spread spectrum\n");
333
334 uint32_t cfg6 = misc_read32(MISC_CGPLL_CONFIG6);
335 cfg6 &= ~CG1PLL_LF_MODE_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600336 cfg6 |= (0x0f8 << CG1PLL_LF_MODE_SHIFT) & CG1PLL_LF_MODE_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600337 misc_write32(MISC_CGPLL_CONFIG6, cfg6);
338
339 uint32_t cfg3 = misc_read32(MISC_CGPLL_CONFIG3);
340 cfg3 &= ~CG1PLL_REFDIV_MASK;
341 cfg3 |= (0x003 << CG1PLL_REFDIV_SHIFT) & CG1PLL_REFDIV_MASK;
342 cfg3 &= ~CG1PLL_FBDIV_MASK;
Marshall Dawsonecce8472018-10-05 15:41:03 -0600343 cfg3 |= (0x04b << CG1PLL_FBDIV_SHIFT) & CG1PLL_FBDIV_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600344 misc_write32(MISC_CGPLL_CONFIG3, cfg3);
345
346 uint32_t cfg5 = misc_read32(MISC_CGPLL_CONFIG5);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600347 cfg5 &= ~SS_AMOUNT_NFRAC_SLIP_MASK;
348 cfg5 |= (0x2 << SS_AMOUNT_NFRAC_SLIP_SHIFT) & SS_AMOUNT_NFRAC_SLIP_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600349 misc_write32(MISC_CGPLL_CONFIG5, cfg5);
350
351 uint32_t cfg4 = misc_read32(MISC_CGPLL_CONFIG4);
Marshall Dawsonedba21e2018-10-05 19:01:52 -0600352 cfg4 &= ~SS_AMOUNT_DSFRAC_MASK;
353 cfg4 |= (0xd000 << SS_AMOUNT_DSFRAC_SHIFT) & SS_AMOUNT_DSFRAC_MASK;
354 cfg4 &= ~SS_STEP_SIZE_DSFRAC_MASK;
355 cfg4 |= (0x02d5 << SS_STEP_SIZE_DSFRAC_SHIFT)
356 & SS_STEP_SIZE_DSFRAC_MASK;
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600357 misc_write32(MISC_CGPLL_CONFIG4, cfg4);
358
359 rstcfg |= TOGGLE_ALL_PWR_GOOD;
360 pm_write16(PWR_RESET_CFG, rstcfg);
361
362 cntl1 |= CG1PLL_FBDIV_TEST;
363 misc_write32(MISC_CLK_CNTL1, cntl1);
364
Raul E Rangel79053412018-08-06 10:40:02 -0600365 *reboot = 1;
366}
367
368static void setup_misc(int *reboot)
369{
370 /* Undocumented register */
371 uint32_t reg = misc_read32(0x50);
372 if (!(reg & BIT(16))) {
373 reg |= BIT(16);
374
375 misc_write32(0x50, reg);
376 *reboot = 1;
377 }
Raul E Rangel6b0fc802018-08-02 15:56:34 -0600378}
379
Richard Spiegelb40e1932018-10-24 12:51:21 -0700380static void fch_smbus_init(void)
381{
382 pm_write8(SMB_ASF_IO_BASE, SMB_BASE_ADDR >> 8);
Marshall Dawson753c2252019-05-05 14:08:59 -0600383 smbus_write8(SMBTIMING, SMB_SPEED_400KHZ);
Richard Spiegelb40e1932018-10-24 12:51:21 -0700384 /* Clear all SMBUS status bits */
Marshall Dawson753c2252019-05-05 14:08:59 -0600385 smbus_write8(SMBHSTSTAT, SMBHST_STAT_CLEAR);
386 smbus_write8(SMBSLVSTAT, SMBSLV_STAT_CLEAR);
387 asf_write8(SMBHSTSTAT, SMBHST_STAT_CLEAR);
388 asf_write8(SMBSLVSTAT, SMBSLV_STAT_CLEAR);
Richard Spiegelb40e1932018-10-24 12:51:21 -0700389}
390
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600391/* Before console init */
Richard Spiegelbec44f22017-11-24 07:41:29 -0700392void bootblock_fch_early_init(void)
393{
Raul E Rangel79053412018-08-06 10:40:02 -0600394 int reboot = 0;
395
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600396 lpc_enable_rom();
397 sb_enable_lpc();
398 lpc_enable_port80();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700399 sb_lpc_decode();
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600400 lpc_enable_spi_prefetch();
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600401 sb_init_spi_base();
Marc Jonescfb16802018-04-20 16:27:41 -0600402 sb_disable_4dw_burst(); /* Must be disabled on CZ(ST) */
Marshall Dawson69486ca2019-05-02 12:03:45 -0600403 enable_acpimmio_decode();
Richard Spiegelb40e1932018-10-24 12:51:21 -0700404 fch_smbus_init();
Raul E Rangel5b058232018-06-28 16:31:45 -0600405 sb_enable_cf9_io();
Raul E Rangel79053412018-08-06 10:40:02 -0600406 setup_spread_spectrum(&reboot);
407 setup_misc(&reboot);
408
409 if (reboot)
Nico Huber73c11192018-10-06 18:20:47 +0200410 warm_reset();
Raul E Rangel79053412018-08-06 10:40:02 -0600411
Raul E Rangel9abc3fe2018-06-28 16:31:45 -0600412 sb_enable_legacy_io();
Richard Spiegel0e0e93c2018-03-13 10:19:51 -0700413 enable_aoac_devices();
Richard Spiegelbec44f22017-11-24 07:41:29 -0700414}
415
Edward Hillcc680342018-08-10 16:20:02 -0600416static void print_num_status_bits(int num_bits, uint32_t status,
417 const char *const bit_names[])
418{
419 int i;
420
421 if (!status)
422 return;
423
424 for (i = num_bits - 1; i >= 0; i--) {
425 if (status & (1 << i)) {
426 if (bit_names[i])
427 printk(BIOS_DEBUG, "%s ", bit_names[i]);
428 else
429 printk(BIOS_DEBUG, "BIT%d ", i);
430 }
431 }
432}
433
434static void sb_print_pmxc0_status(void)
435{
436 /* PMxC0 S5/Reset Status shows the source of previous reset. */
437 uint32_t pmxc0_status = pm_read32(PM_RST_STATUS);
438
Edward Hill917b4002018-10-02 14:17:19 -0600439 static const char *const pmxc0_status_bits[32] = {
Edward Hillcc680342018-08-10 16:20:02 -0600440 [0] = "ThermalTrip",
441 [1] = "FourSecondPwrBtn",
442 [2] = "Shutdown",
443 [3] = "ThermalTripFromTemp",
444 [4] = "RemotePowerDownFromASF",
445 [5] = "ShutDownFan0",
446 [16] = "UserRst",
447 [17] = "SoftPciRst",
448 [18] = "DoInit",
449 [19] = "DoReset",
450 [20] = "DoFullReset",
451 [21] = "SleepReset",
452 [22] = "KbReset",
453 [23] = "LtReset",
454 [24] = "FailBootRst",
455 [25] = "WatchdogIssueReset",
456 [26] = "RemoteResetFromASF",
457 [27] = "SyncFlood",
458 [28] = "HangReset",
459 [29] = "EcWatchdogRst",
Edward Hillcc680342018-08-10 16:20:02 -0600460 };
461
Edward Hill917b4002018-10-02 14:17:19 -0600462 printk(BIOS_DEBUG, "PMxC0 STATUS: 0x%x ", pmxc0_status);
Edward Hillcc680342018-08-10 16:20:02 -0600463 print_num_status_bits(ARRAY_SIZE(pmxc0_status_bits), pmxc0_status,
464 pmxc0_status_bits);
Edward Hill917b4002018-10-02 14:17:19 -0600465 printk(BIOS_DEBUG, "\n");
Edward Hillcc680342018-08-10 16:20:02 -0600466}
467
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600468/* After console init */
Edward Hillcc680342018-08-10 16:20:02 -0600469void bootblock_fch_init(void)
470{
471 sb_print_pmxc0_status();
472}
Raul E Rangeld820f4b82018-08-13 10:39:03 -0600473
Elyes HAOUASc5ad2672018-12-05 10:58:34 +0100474void sb_enable(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600475{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600476 printk(BIOS_DEBUG, "%s\n", __func__);
Marc Jones24484842017-05-04 21:17:45 -0600477}
478
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600479static void sb_init_acpi_ports(void)
Marc Jones24484842017-05-04 21:17:45 -0600480{
Marshall Dawson91b80412017-09-27 16:44:40 -0600481 u32 reg;
482
Marc Jones24484842017-05-04 21:17:45 -0600483 /* We use some of these ports in SMM regardless of whether or not
484 * ACPI tables are generated. Enable these ports indiscriminately.
485 */
486
487 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
488 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
489 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
490 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
491 /* CpuControl is in \_PR.CP00, 6 bytes */
492 pm_write16(PM_CPU_CTRL, ACPI_CPU_CONTROL);
493
Julius Wernercd49cce2019-03-05 16:53:33 -0800494 if (CONFIG(HAVE_SMI_HANDLER)) {
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600495 /* APMC - SMI Command Port */
Marshall Dawsone9b862e2017-09-22 15:14:46 -0600496 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
Marshall Dawsona05fdcb2017-09-27 15:01:37 -0600497 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
Marshall Dawson91b80412017-09-27 16:44:40 -0600498
499 /* SMI on SlpTyp requires sending SMI before completion
500 * response of the I/O write. The BKDG also specifies
501 * clearing ForceStpClkRetry for SMI trapping.
502 */
503 reg = pm_read32(PM_PCI_CTRL);
504 reg |= FORCE_SLPSTATE_RETRY;
505 reg &= ~FORCE_STPCLK_RETRY;
506 pm_write32(PM_PCI_CTRL, reg);
507
508 /* Disable SlpTyp feature */
509 reg = pm_read8(PM_RST_CTRL1);
510 reg &= ~SLPTYPE_CONTROL_EN;
511 pm_write8(PM_RST_CTRL1, reg);
512
513 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
Marc Jones24484842017-05-04 21:17:45 -0600514 } else {
515 pm_write16(PM_ACPI_SMI_CMD, 0);
516 }
517
Marshall Dawson5e2e74f2017-11-10 09:59:56 -0700518 /* Decode ACPI registers and enable standard features */
519 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
520 PM_ACPI_GLOBAL_EN |
521 PM_ACPI_RTC_EN_EN |
522 PM_ACPI_TIMER_EN_EN);
Marc Jones24484842017-05-04 21:17:45 -0600523}
524
Richard Spiegel572f4982018-05-25 15:49:33 -0700525static int get_index_bit(uint32_t value, uint16_t limit)
526{
527 uint16_t i;
528 uint32_t t;
529
Richard Spiegelef73cb82018-06-19 07:40:18 -0700530 if (limit >= TOTAL_BITS(uint32_t))
Richard Spiegel572f4982018-05-25 15:49:33 -0700531 return -1;
532
533 /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */
534 t = (1 << limit) - 1;
535 if ((value & t) == 0)
536 return -1;
537 t = 1;
538 for (i = 0; i < limit; i++) {
539 if (value & t)
540 break;
541 t <<= 1;
542 }
543 return i;
544}
545
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700546static void set_nvs_sws(void *unused)
547{
Richard Spiegel35282a02018-06-14 14:57:54 -0700548 struct soc_power_reg *sws;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700549 struct global_nvs_t *gnvs;
Richard Spiegel572f4982018-05-25 15:49:33 -0700550 int index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700551
Richard Spiegel35282a02018-06-14 14:57:54 -0700552 sws = cbmem_find(CBMEM_ID_POWER_STATE);
553 if (sws == NULL)
554 return;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700555 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
556 if (gnvs == NULL)
557 return;
558
Richard Spiegel35282a02018-06-14 14:57:54 -0700559 index = get_index_bit(sws->pm1_sts & sws->pm1_en, PM1_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700560 if (index < 0)
561 gnvs->pm1i = ~0ULL;
562 else
563 gnvs->pm1i = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700564
Richard Spiegel35282a02018-06-14 14:57:54 -0700565 index = get_index_bit(sws->gpe0_sts & sws->gpe0_en, GPE0_LIMIT);
Richard Spiegel572f4982018-05-25 15:49:33 -0700566 if (index < 0)
567 gnvs->gpei = ~0ULL;
568 else
569 gnvs->gpei = index;
Richard Spiegeldbee8ae2018-05-09 17:34:04 -0700570}
571
572BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
573
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600574void southbridge_init(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600575{
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600576 sb_init_acpi_ports();
Marshall Dawson4ee83b22019-05-03 11:44:22 -0600577 acpi_clear_pm1_status();
Marc Jones24484842017-05-04 21:17:45 -0600578}
579
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600580static void set_sb_final_nvs(void)
581{
582 uintptr_t amdfw_rom;
583 uintptr_t xhci_fw;
584 uintptr_t fwaddr;
585 size_t fwsize;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700586 const struct device *sd, *sata;
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600587
588 struct global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
589 if (gnvs == NULL)
590 return;
591
592 gnvs->aoac.ic0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C0);
593 gnvs->aoac.ic1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C1);
594 gnvs->aoac.ic2e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C2);
595 gnvs->aoac.ic3e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_I2C3);
596 gnvs->aoac.ut0e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART0);
597 gnvs->aoac.ut1e = is_aoac_device_enabled(FCH_AOAC_D3_STATE_UART1);
598 gnvs->aoac.ehce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB2);
599 gnvs->aoac.xhce = is_aoac_device_enabled(FCH_AOAC_D3_STATE_USB3);
600 /* Rely on these being in sync with devicetree */
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300601 sd = pcidev_path_on_root(SD_DEVFN);
Marshall Dawson6d3b7e62019-04-18 17:01:01 -0600602 gnvs->aoac.sd_e = sd && sd->enabled ? 1 : 0;
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300603 sata = pcidev_path_on_root(SATA_DEVFN);
Marshall Dawson6d3b7e62019-04-18 17:01:01 -0600604 gnvs->aoac.st_e = sata && sata->enabled ? 1 : 0;
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600605 gnvs->aoac.espi = 1;
606
607 amdfw_rom = 0x20000 - (0x80000 << CONFIG_AMD_FWM_POSITION_INDEX);
608 xhci_fw = read32((void *)(amdfw_rom + XHCI_FW_SIG_OFFSET));
609
610 fwaddr = 2 + read16((void *)(xhci_fw + XHCI_FW_ADDR_OFFSET
611 + XHCI_FW_BOOTRAM_SIZE));
612 fwsize = read16((void *)(xhci_fw + XHCI_FW_SIZE_OFFSET
613 + XHCI_FW_BOOTRAM_SIZE));
614 gnvs->fw00 = 0;
615 gnvs->fw01 = ((32 * KiB) << 16) + 0;
616 gnvs->fw02 = fwaddr + XHCI_FW_BOOTRAM_SIZE;
617 gnvs->fw03 = fwsize << 16;
618
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600619 gnvs->eh10 = pci_read_config32(SOC_EHCI1_DEV, PCI_BASE_ADDRESS_0)
620 & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
621}
622
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600623void southbridge_final(void *chip_info)
Marc Jones24484842017-05-04 21:17:45 -0600624{
Richard Spiegel6a389142018-03-05 14:28:10 -0700625 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
626
Julius Wernercd49cce2019-03-05 16:53:33 -0800627 if (CONFIG(MAINBOARD_POWER_RESTORE))
Richard Spiegel6a389142018-03-05 14:28:10 -0700628 restored_power = PM_RESTORE_S0_IF_PREV_S0;
629 pm_write8(PM_RTC_SHADOW, restored_power);
Marshall Dawson1d9a46b2018-09-26 16:23:41 -0600630
631 set_sb_final_nvs();
Marc Jones24484842017-05-04 21:17:45 -0600632}
Marshall Dawson8a906df2017-06-13 14:19:02 -0600633
634/*
635 * Update the PCI devices with a valid IRQ number
636 * that is set in the mainboard PCI_IRQ structures.
637 */
638static void set_pci_irqs(void *unused)
639{
640 /* Write PCI_INTR regs 0xC00/0xC01 */
641 write_pci_int_table();
642
643 /* Write IRQs for all devicetree enabled devices */
644 write_pci_cfg_irqs();
645}
646
647/*
648 * Hook this function into the PCI state machine
649 * on entry into BS_DEV_ENABLE.
650 */
651BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);