blob: f3424497c4cbfb679aebf7b92d200254bf1ebce3 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth5c354b92019-04-22 14:55:16 -06002
3#include <console/console.h>
4#include <device/mmio.h>
5#include <bootstate.h>
Raul E Rangelbe2b5ac2020-06-04 16:36:33 -06006#include <cpu/amd/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -06007#include <cpu/x86/smm.h>
Marshall Dawson39a4ac12019-06-20 16:28:33 -06008#include <cpu/x86/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -06009#include <device/device.h>
10#include <device/pci.h>
Martin Roth5c354b92019-04-22 14:55:16 -060011#include <device/pci_ops.h>
12#include <cbmem.h>
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +030013#include <acpi/acpi_gnvs.h>
Martin Roth5c354b92019-04-22 14:55:16 -060014#include <amdblocks/amd_pci_util.h>
Martin Roth5c354b92019-04-22 14:55:16 -060015#include <amdblocks/reset.h>
16#include <amdblocks/acpimmio.h>
Furquan Shaikh702cf302020-05-09 18:30:51 -070017#include <amdblocks/espi.h>
Martin Roth5c354b92019-04-22 14:55:16 -060018#include <amdblocks/lpc.h>
19#include <amdblocks/acpi.h>
Felix Held875e5aa2020-12-01 00:34:11 +010020#include <amdblocks/smbus.h>
Furquan Shaikh13b81582020-05-09 17:24:42 -070021#include <amdblocks/spi.h>
Aaron Durbinaa8f1652020-08-17 17:41:28 -060022#include <soc/acpi.h>
Marshall Dawson39a4ac12019-06-20 16:28:33 -060023#include <soc/cpu.h>
Felix Held5b01f2b2020-05-20 16:01:43 +020024#include <soc/i2c.h>
Martin Roth5c354b92019-04-22 14:55:16 -060025#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060026#include <soc/smi.h>
Felix Held9412b3e2020-06-18 15:54:43 +020027#include <soc/uart.h>
Martin Roth5c354b92019-04-22 14:55:16 -060028#include <soc/amd_pci_int_defs.h>
Martin Roth5c354b92019-04-22 14:55:16 -060029#include <soc/pci_devs.h>
Martin Roth5c354b92019-04-22 14:55:16 -060030#include <soc/nvs.h>
31#include <types.h>
Furquan Shaikh69c28112020-04-28 18:57:52 -070032#include "chip.h"
Martin Roth5c354b92019-04-22 14:55:16 -060033
Martin Roth5c354b92019-04-22 14:55:16 -060034/*
35 * Table of APIC register index and associated IRQ name. Using IDX_XXX_NAME
36 * provides a visible association with the index, therefore helping
37 * maintainability of table. If a new index/name is defined in
38 * amd_pci_int_defs.h, just add the pair at the end of this table.
39 * Order is not important.
40 */
41const static struct irq_idx_name irq_association[] = {
42 { PIRQ_A, "INTA#" },
43 { PIRQ_B, "INTB#" },
44 { PIRQ_C, "INTC#" },
45 { PIRQ_D, "INTD#" },
46 { PIRQ_E, "INTE#" },
Marshall Dawson39a4ac12019-06-20 16:28:33 -060047 { PIRQ_F, "INTF#/GENINT2" },
Martin Roth5c354b92019-04-22 14:55:16 -060048 { PIRQ_G, "INTG#" },
49 { PIRQ_H, "INTH#" },
50 { PIRQ_MISC, "Misc" },
51 { PIRQ_MISC0, "Misc0" },
52 { PIRQ_MISC1, "Misc1" },
53 { PIRQ_MISC2, "Misc2" },
54 { PIRQ_SIRQA, "Ser IRQ INTA" },
55 { PIRQ_SIRQB, "Ser IRQ INTB" },
56 { PIRQ_SIRQC, "Ser IRQ INTC" },
57 { PIRQ_SIRQD, "Ser IRQ INTD" },
58 { PIRQ_SCI, "SCI" },
59 { PIRQ_SMBUS, "SMBUS" },
60 { PIRQ_ASF, "ASF" },
Martin Roth5c354b92019-04-22 14:55:16 -060061 { PIRQ_PMON, "PerMon" },
62 { PIRQ_SD, "SD" },
Marshall Dawson39a4ac12019-06-20 16:28:33 -060063 { PIRQ_SDIO, "SDIO" },
64 { PIRQ_CIR, "CIR" },
65 { PIRQ_GPIOA, "GPIOa" },
66 { PIRQ_GPIOB, "GPIOb" },
67 { PIRQ_GPIOC, "GPIOc" },
Martin Roth5c354b92019-04-22 14:55:16 -060068 { PIRQ_SATA, "SATA" },
Marshall Dawson39a4ac12019-06-20 16:28:33 -060069 { PIRQ_EMMC, "eMMC" },
70 { PIRQ_GPP0, "GPP0" },
71 { PIRQ_GPP1, "GPP1" },
72 { PIRQ_GPP2, "GPP2" },
73 { PIRQ_GPP3, "GPP3" },
Martin Roth5c354b92019-04-22 14:55:16 -060074 { PIRQ_GPIO, "GPIO" },
75 { PIRQ_I2C0, "I2C0" },
76 { PIRQ_I2C1, "I2C1" },
77 { PIRQ_I2C2, "I2C2" },
78 { PIRQ_I2C3, "I2C3" },
79 { PIRQ_UART0, "UART0" },
80 { PIRQ_UART1, "UART1" },
Marshall Dawson39a4ac12019-06-20 16:28:33 -060081 { PIRQ_I2C4, "I2C4" },
82 { PIRQ_I2C5, "I2C5" },
83 { PIRQ_UART2, "UART2" },
84 { PIRQ_UART3, "UART3" },
Martin Roth5c354b92019-04-22 14:55:16 -060085};
86
87const struct irq_idx_name *sb_get_apic_reg_association(size_t *size)
88{
89 *size = ARRAY_SIZE(irq_association);
90 return irq_association;
91}
92
Martin Roth5c354b92019-04-22 14:55:16 -060093static void sb_enable_cf9_io(void)
94{
95 uint32_t reg = pm_read32(PM_DECODE_EN);
96
97 pm_write32(PM_DECODE_EN, reg | CF9_IO_EN);
98}
99
100static void sb_enable_legacy_io(void)
101{
102 uint32_t reg = pm_read32(PM_DECODE_EN);
103
104 pm_write32(PM_DECODE_EN, reg | LEGACY_IO_EN);
105}
106
Marshall Dawson0bd08062019-06-20 11:03:06 -0600107void sb_clk_output_48Mhz(void)
Martin Roth5c354b92019-04-22 14:55:16 -0600108{
109 u32 ctrl;
110
Martin Roth5c354b92019-04-22 14:55:16 -0600111 ctrl = misc_read32(MISC_CLK_CNTL1);
Marshall Dawson0bd08062019-06-20 11:03:06 -0600112 ctrl |= BP_X48M0_OUTPUT_EN;
Martin Roth5c354b92019-04-22 14:55:16 -0600113 misc_write32(MISC_CLK_CNTL1, ctrl);
114}
115
Furquan Shaikh702cf302020-05-09 18:30:51 -0700116static void lpc_configure_decodes(void)
117{
118 if (CONFIG(POST_IO) && (CONFIG_POST_IO_PORT == 0x80))
119 lpc_enable_port80();
120}
121
Martin Roth5c354b92019-04-22 14:55:16 -0600122/* Before console init */
Marshall Dawson39a4ac12019-06-20 16:28:33 -0600123void fch_pre_init(void)
Martin Roth5c354b92019-04-22 14:55:16 -0600124{
Furquan Shaikhed8ceab2020-05-09 17:35:02 -0700125 lpc_early_init();
Furquan Shaikh702cf302020-05-09 18:30:51 -0700126
127 if (!CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
128 lpc_configure_decodes();
129
Furquan Shaikh13b81582020-05-09 17:24:42 -0700130 fch_spi_early_init();
Michał Żygowski73a544d2019-11-24 14:16:34 +0100131 enable_acpimmio_decode_pm04();
Martin Roth5c354b92019-04-22 14:55:16 -0600132 fch_smbus_init();
133 sb_enable_cf9_io();
Martin Roth5c354b92019-04-22 14:55:16 -0600134 sb_enable_legacy_io();
135 enable_aoac_devices();
Marshall Dawson39a4ac12019-06-20 16:28:33 -0600136 sb_reset_i2c_slaves();
Raul E Rangel4f5936b2020-06-11 16:27:49 -0600137
138 /*
139 * On reset Range_0 defaults to enabled. We want to start with a clean
140 * slate to not have things unexpectedly enabled.
141 */
142 clear_uart_legacy_config();
143
Felix Held097e4492020-06-16 15:35:20 +0200144 if (CONFIG(PICASSO_CONSOLE_UART))
Marshall Dawsonc0b8d0d2019-06-20 10:29:29 -0600145 set_uart_config(CONFIG_UART_FOR_CONSOLE);
Martin Roth5c354b92019-04-22 14:55:16 -0600146}
147
148static void print_num_status_bits(int num_bits, uint32_t status,
149 const char *const bit_names[])
150{
151 int i;
152
153 if (!status)
154 return;
155
156 for (i = num_bits - 1; i >= 0; i--) {
157 if (status & (1 << i)) {
158 if (bit_names[i])
159 printk(BIOS_DEBUG, "%s ", bit_names[i]);
160 else
161 printk(BIOS_DEBUG, "BIT%d ", i);
162 }
163 }
164}
165
166static void sb_print_pmxc0_status(void)
167{
168 /* PMxC0 S5/Reset Status shows the source of previous reset. */
169 uint32_t pmxc0_status = pm_read32(PM_RST_STATUS);
170
171 static const char *const pmxc0_status_bits[32] = {
172 [0] = "ThermalTrip",
173 [1] = "FourSecondPwrBtn",
174 [2] = "Shutdown",
175 [3] = "ThermalTripFromTemp",
176 [4] = "RemotePowerDownFromASF",
177 [5] = "ShutDownFan0",
178 [16] = "UserRst",
179 [17] = "SoftPciRst",
180 [18] = "DoInit",
181 [19] = "DoReset",
182 [20] = "DoFullReset",
183 [21] = "SleepReset",
184 [22] = "KbReset",
185 [23] = "LtReset",
186 [24] = "FailBootRst",
187 [25] = "WatchdogIssueReset",
188 [26] = "RemoteResetFromASF",
189 [27] = "SyncFlood",
190 [28] = "HangReset",
191 [29] = "EcWatchdogRst",
192 };
193
194 printk(BIOS_DEBUG, "PMxC0 STATUS: 0x%x ", pmxc0_status);
195 print_num_status_bits(ARRAY_SIZE(pmxc0_status_bits), pmxc0_status,
196 pmxc0_status_bits);
197 printk(BIOS_DEBUG, "\n");
198}
199
200/* After console init */
Marshall Dawson39a4ac12019-06-20 16:28:33 -0600201void fch_early_init(void)
Martin Roth5c354b92019-04-22 14:55:16 -0600202{
203 sb_print_pmxc0_status();
Marshall Dawson39a4ac12019-06-20 16:28:33 -0600204 i2c_soc_early_init();
Aaron Durbin1d0b99b2020-04-11 11:58:57 -0600205
206 if (CONFIG(DISABLE_SPI_FLASH_ROM_SHARING))
207 lpc_disable_spi_rom_sharing();
Furquan Shaikh702cf302020-05-09 18:30:51 -0700208
209 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI)) {
210 espi_setup();
211 espi_configure_decodes();
212 }
Martin Roth5c354b92019-04-22 14:55:16 -0600213}
214
215void sb_enable(struct device *dev)
216{
217 printk(BIOS_DEBUG, "%s\n", __func__);
218}
219
220static void sb_init_acpi_ports(void)
221{
222 u32 reg;
223
224 /* We use some of these ports in SMM regardless of whether or not
225 * ACPI tables are generated. Enable these ports indiscriminately.
226 */
227
228 pm_write16(PM_EVT_BLK, ACPI_PM_EVT_BLK);
229 pm_write16(PM1_CNT_BLK, ACPI_PM1_CNT_BLK);
230 pm_write16(PM_TMR_BLK, ACPI_PM_TMR_BLK);
231 pm_write16(PM_GPE0_BLK, ACPI_GPE0_BLK);
Marshall Dawson39a4ac12019-06-20 16:28:33 -0600232
Martin Roth5c354b92019-04-22 14:55:16 -0600233 if (CONFIG(HAVE_SMI_HANDLER)) {
234 /* APMC - SMI Command Port */
235 pm_write16(PM_ACPI_SMI_CMD, APM_CNT);
236 configure_smi(SMITYPE_SMI_CMD_PORT, SMI_MODE_SMI);
237
238 /* SMI on SlpTyp requires sending SMI before completion
239 * response of the I/O write. The BKDG also specifies
240 * clearing ForceStpClkRetry for SMI trapping.
241 */
242 reg = pm_read32(PM_PCI_CTRL);
243 reg |= FORCE_SLPSTATE_RETRY;
Martin Roth5c354b92019-04-22 14:55:16 -0600244 pm_write32(PM_PCI_CTRL, reg);
245
246 /* Disable SlpTyp feature */
247 reg = pm_read8(PM_RST_CTRL1);
248 reg &= ~SLPTYPE_CONTROL_EN;
249 pm_write8(PM_RST_CTRL1, reg);
250
251 configure_smi(SMITYPE_SLP_TYP, SMI_MODE_SMI);
252 } else {
253 pm_write16(PM_ACPI_SMI_CMD, 0);
254 }
255
256 /* Decode ACPI registers and enable standard features */
257 pm_write8(PM_ACPI_CONF, PM_ACPI_DECODE_STD |
258 PM_ACPI_GLOBAL_EN |
259 PM_ACPI_RTC_EN_EN |
260 PM_ACPI_TIMER_EN_EN);
261}
262
Martin Roth5c354b92019-04-22 14:55:16 -0600263static void set_nvs_sws(void *unused)
264{
Aaron Durbinaa8f1652020-08-17 17:41:28 -0600265 struct chipset_state *state;
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300266 struct global_nvs *gnvs;
Martin Roth5c354b92019-04-22 14:55:16 -0600267
Aaron Durbin404a5c32020-08-14 16:27:27 -0600268 state = cbmem_find(CBMEM_ID_POWER_STATE);
269 if (state == NULL)
Martin Roth5c354b92019-04-22 14:55:16 -0600270 return;
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +0300271 gnvs = acpi_get_gnvs();
Martin Roth5c354b92019-04-22 14:55:16 -0600272 if (gnvs == NULL)
273 return;
274
Aaron Durbinaa8f1652020-08-17 17:41:28 -0600275 acpi_fill_gnvs(gnvs, &state->gpe_state);
Martin Roth5c354b92019-04-22 14:55:16 -0600276}
277
278BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_nvs_sws, NULL);
279
Matt Papageorgeab83b432020-06-26 08:47:00 -0500280/*
281 * A-Link to AHB bridge, part of the AMBA fabric. These are internal clocks
282 * and unneeded for Raven/Picasso so gate them to save power.
283 */
284static void al2ahb_clock_gate(void)
285{
286 uint8_t al2ahb_val;
287 uintptr_t al2ahb_base = ALINK_AHB_ADDRESS;
288
289 al2ahb_val = read8((void *)(al2ahb_base + AL2AHB_CONTROL_CLK_OFFSET));
290 al2ahb_val |= AL2AHB_CLK_GATE_EN;
291 write8((void *)(al2ahb_base + AL2AHB_CONTROL_CLK_OFFSET), al2ahb_val);
292 al2ahb_val = read8((void *)(al2ahb_base + AL2AHB_CONTROL_HCLK_OFFSET));
293 al2ahb_val |= AL2AHB_HCLK_GATE_EN;
294 write8((void *)(al2ahb_base + AL2AHB_CONTROL_HCLK_OFFSET), al2ahb_val);
295}
296
Felix Held82a0a632020-08-28 01:40:20 +0200297/* configure the genral purpose PCIe clock outputs according to the devicetree settings */
298static void gpp_clk_setup(void)
299{
300 const struct soc_amd_picasso_config *cfg = config_of_soc();
301
302 /* look-up table to be able to iterate over the PCIe clock output settings */
303 const uint8_t gpp_clk_shift_lut[GPP_CLK_OUTPUT_COUNT] = {
304 GPP_CLK0_REQ_SHIFT,
305 GPP_CLK1_REQ_SHIFT,
306 GPP_CLK2_REQ_SHIFT,
307 GPP_CLK3_REQ_SHIFT,
308 GPP_CLK4_REQ_SHIFT,
309 GPP_CLK5_REQ_SHIFT,
310 GPP_CLK6_REQ_SHIFT,
311 };
312
313 uint32_t gpp_clk_ctl = misc_read32(GPP_CLK_CNTRL);
314
315 for (int i = 0; i < GPP_CLK_OUTPUT_COUNT; i++) {
316 gpp_clk_ctl &= ~GPP_CLK_REQ_MASK(gpp_clk_shift_lut[i]);
317 /*
318 * The remapping of values is done so that the default of the enum used for the
319 * devicetree settings is the clock being enabled, so that a missing devicetree
320 * configuration for this will result in an always active clock and not an
321 * inactive PCIe clock output.
322 */
323 switch (cfg->gpp_clk_config[i]) {
324 case GPP_CLK_REQ:
325 gpp_clk_ctl |= GPP_CLK_REQ_EXT(gpp_clk_shift_lut[i]);
326 break;
327 case GPP_CLK_OFF:
328 gpp_clk_ctl |= GPP_CLK_REQ_OFF(gpp_clk_shift_lut[i]);
329 break;
330 case GPP_CLK_ON:
331 default:
332 gpp_clk_ctl |= GPP_CLK_REQ_ON(gpp_clk_shift_lut[i]);
333 }
334 }
335
336 misc_write32(GPP_CLK_CNTRL, gpp_clk_ctl);
337}
338
Martin Roth5c354b92019-04-22 14:55:16 -0600339void southbridge_init(void *chip_info)
340{
Aaron Durbinaa8f1652020-08-17 17:41:28 -0600341 struct chipset_state *state;
Aaron Durbin404a5c32020-08-14 16:27:27 -0600342
Aaron Durbin09f60ff2020-04-09 15:24:50 -0600343 i2c_soc_init();
Martin Roth5c354b92019-04-22 14:55:16 -0600344 sb_init_acpi_ports();
Aaron Durbin404a5c32020-08-14 16:27:27 -0600345
Aaron Durbinc30981c2020-08-14 16:54:44 -0600346 state = cbmem_find(CBMEM_ID_POWER_STATE);
Aaron Durbinaa8f1652020-08-17 17:41:28 -0600347 if (state) {
348 acpi_pm_gpe_add_events_print_events(&state->gpe_state);
349 gpio_add_events(&state->gpio_state);
350 }
Aaron Durbin404a5c32020-08-14 16:27:27 -0600351 acpi_clear_pm_gpe_status();
352
Matt Papageorgeab83b432020-06-26 08:47:00 -0500353 al2ahb_clock_gate();
Felix Held82a0a632020-08-28 01:40:20 +0200354
355 gpp_clk_setup();
Martin Roth5c354b92019-04-22 14:55:16 -0600356}
357
Martin Roth5c354b92019-04-22 14:55:16 -0600358void southbridge_final(void *chip_info)
359{
360 uint8_t restored_power = PM_S5_AT_POWER_RECOVERY;
361
362 if (CONFIG(MAINBOARD_POWER_RESTORE))
363 restored_power = PM_RESTORE_S0_IF_PREV_S0;
364 pm_write8(PM_RTC_SHADOW, restored_power);
Martin Roth5c354b92019-04-22 14:55:16 -0600365}
366
367/*
368 * Update the PCI devices with a valid IRQ number
369 * that is set in the mainboard PCI_IRQ structures.
370 */
371static void set_pci_irqs(void *unused)
372{
373 /* Write PCI_INTR regs 0xC00/0xC01 */
374 write_pci_int_table();
375
376 /* Write IRQs for all devicetree enabled devices */
377 write_pci_cfg_irqs();
378}
379
380/*
381 * Hook this function into the PCI state machine
382 * on entry into BS_DEV_ENABLE.
383 */
384BOOT_STATE_INIT_ENTRY(BS_DEV_ENABLE, BS_ON_ENTRY, set_pci_irqs, NULL);