blob: 138dc339df070f71d2ec1611cf92aeca2560ac10 [file] [log] [blame]
Angel Ponsba38f372020-04-05 15:46:45 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Lee Leahy77ff0b12015-05-05 15:07:29 -07002
Lee Leahy32471722015-04-20 15:20:28 -07003#include <arch/hlt.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -07004#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02005#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -07007#include <console/console.h>
8#include <cpu/x86/cache.h>
9#include <cpu/x86/smm.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +030010#include <cpu/intel/em64t100_save_state.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070011#include <device/pci_def.h>
12#include <elog.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070013#include <soc/nvs.h>
Lee Leahy32471722015-04-20 15:20:28 -070014#include <soc/pci_devs.h>
15#include <soc/pm.h>
16#include <spi-generic.h>
17#include <stdint.h>
Lee Leahy32471722015-04-20 15:20:28 -070018#include <soc/gpio.h>
Matt DeVillieraa3b5e292018-12-25 22:10:48 -060019#include <smmstore.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070020
Lee Leahy77ff0b12015-05-05 15:07:29 -070021int southbridge_io_trap_handler(int smif)
22{
23 switch (smif) {
24 case 0x32:
25 printk(BIOS_DEBUG, "OS Init\n");
Lee Leahy32471722015-04-20 15:20:28 -070026 /*
27 * gnvs->smif:
Lee Leahy77ff0b12015-05-05 15:07:29 -070028 * On success, the IO Trap Handler returns 0
29 * On failure, the IO Trap Handler returns a value != 0
30 */
31 gnvs->smif = 0;
32 return 1; /* IO trap handled */
33 }
34
35 /* Not handled */
36 return 0;
37}
38
39void southbridge_smi_set_eos(void)
40{
41 enable_smi(EOS);
42}
43
Lee Leahy77ff0b12015-05-05 15:07:29 -070044static void busmaster_disable_on_bus(int bus)
45{
46 int slot, func;
47 unsigned int val;
48 unsigned char hdr;
49
50 for (slot = 0; slot < 0x20; slot++) {
51 for (func = 0; func < 8; func++) {
Elyes HAOUAS066e61f2020-04-29 10:28:20 +020052 u16 reg16;
Elyes HAOUASc8a649c2018-06-10 23:36:44 +020053 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Lee Leahy77ff0b12015-05-05 15:07:29 -070054
55 val = pci_read_config32(dev, PCI_VENDOR_ID);
56
57 if (val == 0xffffffff || val == 0x00000000 ||
58 val == 0x0000ffff || val == 0xffff0000)
59 continue;
60
61 /* Disable Bus Mastering for this one device */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +020062 reg16 = pci_read_config16(dev, PCI_COMMAND);
63 reg16 &= ~PCI_COMMAND_MASTER;
64 pci_write_config16(dev, PCI_COMMAND, reg16);
Lee Leahy77ff0b12015-05-05 15:07:29 -070065
66 /* If this is a bridge, then follow it. */
67 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
68 hdr &= 0x7f;
Angel Ponsaee7ab22020-03-19 00:31:58 +010069 if (hdr == PCI_HEADER_TYPE_BRIDGE || hdr == PCI_HEADER_TYPE_CARDBUS) {
Lee Leahy77ff0b12015-05-05 15:07:29 -070070 unsigned int buses;
71 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
72 busmaster_disable_on_bus((buses >> 8) & 0xff);
73 }
74 }
75 }
76}
77
Lee Leahy32471722015-04-20 15:20:28 -070078static void tristate_gpios(uint32_t val)
79{
80 /* Tri-state eMMC */
Angel Ponsaee7ab22020-03-19 00:31:58 +010081 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_CMD_MMIO_OFFSET, val);
82 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D0_MMIO_OFFSET, val);
83 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D1_MMIO_OFFSET, val);
84 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D2_MMIO_OFFSET, val);
85 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D3_MMIO_OFFSET, val);
86 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + MMC1_D4_SD_WE_MMIO_OFFSET, val);
87 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + MMC1_D5_MMIO_OFFSET, val);
88 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + MMC1_D6_MMIO_OFFSET, val);
89 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + MMC1_D7_MMIO_OFFSET, val);
90 write32((void *)COMMUNITY_GPSOUTHEAST_BASE + MMC1_RCLK_OFFSET, val);
Lee Leahy32471722015-04-20 15:20:28 -070091
92 /* Tri-state HDMI */
Angel Ponsaee7ab22020-03-19 00:31:58 +010093 write32((void *)COMMUNITY_GPNORTH_BASE + HV_DDI2_DDC_SDA_MMIO_OFFSET, val);
94 write32((void *)COMMUNITY_GPNORTH_BASE + HV_DDI2_DDC_SCL_MMIO_OFFSET, val);
Ravi Sarawadia5d98882015-08-11 14:06:15 -070095
96 /* Tri-state CFIO 139 and 140 */
Angel Ponsaee7ab22020-03-19 00:31:58 +010097 write32((void *)COMMUNITY_GPSOUTHWEST_BASE + CFIO_139_MMIO_OFFSET, val);
98 write32((void *)COMMUNITY_GPSOUTHWEST_BASE + CFIO_140_MMIO_OFFSET, val);
Lee Leahy32471722015-04-20 15:20:28 -070099}
100
Lee Leahy77ff0b12015-05-05 15:07:29 -0700101static void southbridge_smi_sleep(void)
102{
103 uint32_t reg32;
104 uint8_t slp_typ;
105 uint16_t pmbase = get_pmbase();
106
107 /* First, disable further SMIs */
108 disable_smi(SLP_SMI_EN);
109
110 /* Figure out SLP_TYP */
111 reg32 = inl(pmbase + PM1_CNT);
112 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500113 slp_typ = acpi_sleep_from_pm1(reg32);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700114
115 /* Do any mainboard sleep handling */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500116 mainboard_smi_sleep(slp_typ);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700117
Lee Leahy77ff0b12015-05-05 15:07:29 -0700118 /* Log S3, S4, and S5 entry */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500119 if (slp_typ >= ACPI_S3)
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200120 elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
121
Angel Ponsaee7ab22020-03-19 00:31:58 +0100122 /* Clear pending GPE events */
Lee Leahy32471722015-04-20 15:20:28 -0700123 clear_gpe_status();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700124
Lee Leahy32471722015-04-20 15:20:28 -0700125 /* Next, do the deed. */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700126 switch (slp_typ) {
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500127 case ACPI_S0:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700128 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
129 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500130 case ACPI_S1:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700131 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
132 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500133 case ACPI_S3:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700134 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
135
136 /* Invalidate the cache before going to S3 */
137 wbinvd();
138 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500139 case ACPI_S4:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700140 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
141 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500142 case ACPI_S5:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700143 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
144
145 /* Disable all GPE */
146 disable_all_gpe();
147
Angel Ponsaee7ab22020-03-19 00:31:58 +0100148 /* Also iterates over all bridges on bus 0 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700149 busmaster_disable_on_bus(0);
150 break;
151 default:
152 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
153 break;
154 }
Angel Ponsaee7ab22020-03-19 00:31:58 +0100155
Lee Leahy32471722015-04-20 15:20:28 -0700156 /* Clear pending wake status bit to avoid immediate wake */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100157 write32((void *)(0xfed88000 + 0x0200), read32((void *)(0xfed88000 + 0x0200)));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700158
Lee Leahy32471722015-04-20 15:20:28 -0700159 /* Tri-state specific GPIOS to avoid leakage during S3/S5 */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500160 if ((slp_typ == ACPI_S3) || (slp_typ == ACPI_S5))
Lee Leahy32471722015-04-20 15:20:28 -0700161 tristate_gpios(PAD_CONTROL_REG0_TRISTATE);
162
163 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100164 * Write back to the SLP register to cause the originally intended event again.
165 * We need to set BIT13 (SLP_EN) though to make the sleep happen.
Lee Leahy77ff0b12015-05-05 15:07:29 -0700166 */
167 enable_pm1_control(SLP_EN);
168
169 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500170 if (slp_typ >= ACPI_S3)
Lee Leahy32471722015-04-20 15:20:28 -0700171 hlt();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700172
Lee Leahy32471722015-04-20 15:20:28 -0700173 /*
174 * In most sleep states, the code flow of this function ends at
Lee Leahy77ff0b12015-05-05 15:07:29 -0700175 * the line above. However, if we entered sleep state S1 and wake
176 * up again, we will continue to execute code in this function.
177 */
178 reg32 = inl(pmbase + PM1_CNT);
179 if (reg32 & SCI_EN) {
180 /* The OS is not an ACPI OS, so we set the state to S0 */
181 disable_pm1_control(SLP_EN | SLP_TYP);
182 }
183}
184
185/*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100186 * Look for Synchronous IO SMI and use save state from that core in case
187 * we are not running on the same core that initiated the IO transaction.
Lee Leahy77ff0b12015-05-05 15:07:29 -0700188 */
189static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
190{
191 em64t100_smm_state_save_area_t *state;
192 int node;
193
194 /* Check all nodes looking for the one that issued the IO */
195 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
196 state = smm_get_save_state(node);
197
198 /* Check for Synchronous IO (bit0==1) */
199 if (!(state->io_misc_info & (1 << 0)))
200 continue;
201
202 /* Make sure it was a write (bit4==0) */
203 if (state->io_misc_info & (1 << 4))
204 continue;
205
206 /* Check for APMC IO port */
207 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
208 continue;
209
210 /* Check AX against the requested command */
211 if ((state->rax & 0xff) != cmd)
212 continue;
213
214 return state;
215 }
216
217 return NULL;
218}
219
Lee Leahy77ff0b12015-05-05 15:07:29 -0700220static void southbridge_smi_gsmi(void)
221{
222 u32 *ret, *param;
223 uint8_t sub_command;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100224 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700225
226 if (!io_smi)
227 return;
228
229 /* Command and return value in EAX */
Lee Leahy32471722015-04-20 15:20:28 -0700230 ret = (u32 *)&io_smi->rax;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700231 sub_command = (uint8_t)(*ret >> 8);
232
233 /* Parameter buffer in EBX */
Lee Leahy32471722015-04-20 15:20:28 -0700234 param = (u32 *)&io_smi->rbx;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700235
236 /* drivers/elog/gsmi.c */
237 *ret = gsmi_exec(sub_command, param);
238}
Lee Leahy77ff0b12015-05-05 15:07:29 -0700239
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600240static void southbridge_smi_store(void)
241{
242 u8 sub_command, ret;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100243 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_SMMSTORE);
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600244 uint32_t reg_ebx;
245
246 if (!io_smi)
247 return;
248 /* Command and return value in EAX */
249 sub_command = (io_smi->rax >> 8) & 0xff;
250
251 /* Parameter buffer in EBX */
252 reg_ebx = io_smi->rbx;
253
254 /* drivers/smmstore/smi.c */
255 ret = smmstore_exec(sub_command, (void *)reg_ebx);
256 io_smi->rax = ret;
257}
258
Lee Leahy77ff0b12015-05-05 15:07:29 -0700259static void southbridge_smi_apmc(void)
260{
261 uint8_t reg8;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700262
Kyösti Mälkki9a1620f2021-01-08 13:27:33 +0200263 reg8 = apm_get_apmc();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700264 switch (reg8) {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700265 case APM_CNT_ACPI_DISABLE:
266 disable_pm1_control(SCI_EN);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700267 break;
268 case APM_CNT_ACPI_ENABLE:
269 enable_pm1_control(SCI_EN);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700270 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100271 case APM_CNT_ELOG_GSMI:
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200272 if (CONFIG(ELOG_GSMI))
273 southbridge_smi_gsmi();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700274 break;
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600275 case APM_CNT_SMMSTORE:
276 if (CONFIG(SMMSTORE))
277 southbridge_smi_store();
278 break;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700279 }
280
281 mainboard_smi_apmc(reg8);
282}
283
284static void southbridge_smi_pm1(void)
285{
286 uint16_t pm1_sts = clear_pm1_status();
287
Angel Ponsaee7ab22020-03-19 00:31:58 +0100288 /* While OSPM is not active, poweroff immediately on a power button event */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700289 if (pm1_sts & PWRBTN_STS) {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100290 /* Power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200291 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700292 disable_pm1_control(-1UL);
293 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
294 }
295}
296
297static void southbridge_smi_gpe0(void)
298{
299 clear_gpe_status();
300}
301
302static void southbridge_smi_tco(void)
303{
304 uint32_t tco_sts = clear_tco_status();
305
306 /* Any TCO event? */
307 if (!tco_sts)
308 return;
309
310 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
311 /* Handle TCO timeout */
312 printk(BIOS_DEBUG, "TCO Timeout.\n");
313 }
314}
315
316static void southbridge_smi_periodic(void)
317{
318 uint32_t reg32;
319
320 reg32 = inl(get_pmbase() + SMI_EN);
321
322 /* Are periodic SMIs enabled? */
323 if ((reg32 & PERIODIC_EN) == 0)
324 return;
325
326 printk(BIOS_DEBUG, "Periodic SMI.\n");
327}
328
329typedef void (*smi_handler_t)(void);
330
331static const smi_handler_t southbridge_smi[32] = {
Lee Leahy32471722015-04-20 15:20:28 -0700332 NULL, /* [0] reserved */
333 NULL, /* [1] reserved */
334 NULL, /* [2] BIOS_STS */
335 NULL, /* [3] LEGACY_USB_STS */
336 southbridge_smi_sleep, /* [4] SLP_SMI_STS */
337 southbridge_smi_apmc, /* [5] APM_STS */
338 NULL, /* [6] SWSMI_TMR_STS */
339 NULL, /* [7] reserved */
340 southbridge_smi_pm1, /* [8] PM1_STS */
341 southbridge_smi_gpe0, /* [9] GPE0_STS */
342 NULL, /* [10] reserved */
343 NULL, /* [11] reserved */
344 NULL, /* [12] reserved */
345 southbridge_smi_tco, /* [13] TCO_STS */
346 southbridge_smi_periodic, /* [14] PERIODIC_STS */
347 NULL, /* [15] SERIRQ_SMI_STS */
348 NULL, /* [16] SMBUS_SMI_STS */
349 NULL, /* [17] LEGACY_USB2_STS */
350 NULL, /* [18] INTEL_USB2_STS */
351 NULL, /* [19] reserved */
352 NULL, /* [20] PCI_EXP_SMI_STS */
353 NULL, /* [21] reserved */
354 NULL, /* [22] reserved */
355 NULL, /* [23] reserved */
356 NULL, /* [24] reserved */
357 NULL, /* [25] reserved */
358 NULL, /* [26] SPI_STS */
359 NULL, /* [27] reserved */
360 NULL, /* [28] PUNIT */
361 NULL, /* [29] GUNIT */
362 NULL, /* [30] reserved */
363 NULL /* [31] reserved */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700364};
365
366void southbridge_smi_handler(void)
367{
368 int i;
369 uint32_t smi_sts;
370
Lee Leahy32471722015-04-20 15:20:28 -0700371 /*
372 * We need to clear the SMI status registers, or we won't see what's
Lee Leahy77ff0b12015-05-05 15:07:29 -0700373 * happening in the following calls.
374 */
375 smi_sts = clear_smi_status();
376
377 /* Call SMI sub handler for each of the status bits */
378 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
379 if (!(smi_sts & (1 << i)))
380 continue;
381
382 if (southbridge_smi[i] != NULL) {
383 southbridge_smi[i]();
384 } else {
385 printk(BIOS_DEBUG,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100386 "SMI_STS[%d] occurred, but no handler available.\n", i);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700387 }
388 }
389
Lee Leahy32471722015-04-20 15:20:28 -0700390 /*
391 * The GPIO SMI events do not have a status bit in SMI_STS. Therefore,
392 * these events need to be cleared and checked unconditionally.
393 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700394 mainboard_smi_gpi(clear_alt_status());
395}