blob: d2588cf766f85c77d1fa0bfba62c916879c59255 [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>
5#include <console/console.h>
6#include <cpu/x86/cache.h>
7#include <cpu/x86/smm.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +03008#include <cpu/intel/em64t100_save_state.h>
Dinesh Gehlotba09eb72023-01-17 05:06:10 +00009#include <device/mmio.h>
10#include <device/pci_ops.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070011#include <device/pci_def.h>
12#include <elog.h>
Dinesh Gehlotba09eb72023-01-17 05:06:10 +000013#include <gpio.h>
Kyösti Mälkki85556ac2023-05-02 12:53:00 +030014#include <smmstore.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070015#include <soc/nvs.h>
Lee Leahy32471722015-04-20 15:20:28 -070016#include <soc/pci_devs.h>
17#include <soc/pm.h>
18#include <spi-generic.h>
19#include <stdint.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070020
Lee Leahy77ff0b12015-05-05 15:07:29 -070021void southbridge_smi_set_eos(void)
22{
23 enable_smi(EOS);
24}
25
Lee Leahy77ff0b12015-05-05 15:07:29 -070026static void busmaster_disable_on_bus(int bus)
27{
28 int slot, func;
29 unsigned int val;
30 unsigned char hdr;
31
32 for (slot = 0; slot < 0x20; slot++) {
33 for (func = 0; func < 8; func++) {
Elyes HAOUAS066e61f2020-04-29 10:28:20 +020034 u16 reg16;
Elyes HAOUASc8a649c2018-06-10 23:36:44 +020035 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Lee Leahy77ff0b12015-05-05 15:07:29 -070036
37 val = pci_read_config32(dev, PCI_VENDOR_ID);
38
39 if (val == 0xffffffff || val == 0x00000000 ||
40 val == 0x0000ffff || val == 0xffff0000)
41 continue;
42
43 /* Disable Bus Mastering for this one device */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +020044 reg16 = pci_read_config16(dev, PCI_COMMAND);
45 reg16 &= ~PCI_COMMAND_MASTER;
46 pci_write_config16(dev, PCI_COMMAND, reg16);
Lee Leahy77ff0b12015-05-05 15:07:29 -070047
48 /* If this is a bridge, then follow it. */
49 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
50 hdr &= 0x7f;
Angel Ponsaee7ab22020-03-19 00:31:58 +010051 if (hdr == PCI_HEADER_TYPE_BRIDGE || hdr == PCI_HEADER_TYPE_CARDBUS) {
Lee Leahy77ff0b12015-05-05 15:07:29 -070052 unsigned int buses;
53 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
54 busmaster_disable_on_bus((buses >> 8) & 0xff);
55 }
56 }
57 }
58}
59
Lee Leahy32471722015-04-20 15:20:28 -070060static void tristate_gpios(uint32_t val)
61{
62 /* Tri-state eMMC */
Elyes Haouasa5064512023-09-17 10:22:02 +020063 write32p(COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_CMD_MMIO_OFFSET, val);
64 write32p(COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D0_MMIO_OFFSET, val);
65 write32p(COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D1_MMIO_OFFSET, val);
66 write32p(COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D2_MMIO_OFFSET, val);
67 write32p(COMMUNITY_GPSOUTHEAST_BASE + SDMMC1_D3_MMIO_OFFSET, val);
68 write32p(COMMUNITY_GPSOUTHEAST_BASE + MMC1_D4_SD_WE_MMIO_OFFSET, val);
69 write32p(COMMUNITY_GPSOUTHEAST_BASE + MMC1_D5_MMIO_OFFSET, val);
70 write32p(COMMUNITY_GPSOUTHEAST_BASE + MMC1_D6_MMIO_OFFSET, val);
71 write32p(COMMUNITY_GPSOUTHEAST_BASE + MMC1_D7_MMIO_OFFSET, val);
72 write32p(COMMUNITY_GPSOUTHEAST_BASE + MMC1_RCLK_OFFSET, val);
Lee Leahy32471722015-04-20 15:20:28 -070073
74 /* Tri-state HDMI */
Elyes Haouasa5064512023-09-17 10:22:02 +020075 write32p(COMMUNITY_GPNORTH_BASE + HV_DDI2_DDC_SDA_MMIO_OFFSET, val);
76 write32p(COMMUNITY_GPNORTH_BASE + HV_DDI2_DDC_SCL_MMIO_OFFSET, val);
Ravi Sarawadia5d98882015-08-11 14:06:15 -070077
78 /* Tri-state CFIO 139 and 140 */
Elyes Haouasa5064512023-09-17 10:22:02 +020079 write32p(COMMUNITY_GPSOUTHWEST_BASE + CFIO_139_MMIO_OFFSET, val);
80 write32p(COMMUNITY_GPSOUTHWEST_BASE + CFIO_140_MMIO_OFFSET, val);
Lee Leahy32471722015-04-20 15:20:28 -070081}
82
Lee Leahy77ff0b12015-05-05 15:07:29 -070083static void southbridge_smi_sleep(void)
84{
85 uint32_t reg32;
86 uint8_t slp_typ;
87 uint16_t pmbase = get_pmbase();
88
89 /* First, disable further SMIs */
90 disable_smi(SLP_SMI_EN);
91
92 /* Figure out SLP_TYP */
93 reg32 = inl(pmbase + PM1_CNT);
94 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbin1b6196d2016-07-13 23:20:26 -050095 slp_typ = acpi_sleep_from_pm1(reg32);
Lee Leahy77ff0b12015-05-05 15:07:29 -070096
97 /* Do any mainboard sleep handling */
Aaron Durbin1b6196d2016-07-13 23:20:26 -050098 mainboard_smi_sleep(slp_typ);
Lee Leahy77ff0b12015-05-05 15:07:29 -070099
Lee Leahy77ff0b12015-05-05 15:07:29 -0700100 /* Log S3, S4, and S5 entry */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500101 if (slp_typ >= ACPI_S3)
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200102 elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
103
Angel Ponsaee7ab22020-03-19 00:31:58 +0100104 /* Clear pending GPE events */
Lee Leahy32471722015-04-20 15:20:28 -0700105 clear_gpe_status();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700106
Lee Leahy32471722015-04-20 15:20:28 -0700107 /* Next, do the deed. */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700108 switch (slp_typ) {
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500109 case ACPI_S0:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700110 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
111 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500112 case ACPI_S1:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700113 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
114 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500115 case ACPI_S3:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700116 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
117
118 /* Invalidate the cache before going to S3 */
119 wbinvd();
120 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500121 case ACPI_S4:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700122 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
123 break;
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500124 case ACPI_S5:
Lee Leahy77ff0b12015-05-05 15:07:29 -0700125 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
126
127 /* Disable all GPE */
128 disable_all_gpe();
129
Angel Ponsaee7ab22020-03-19 00:31:58 +0100130 /* Also iterates over all bridges on bus 0 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700131 busmaster_disable_on_bus(0);
132 break;
133 default:
134 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
135 break;
136 }
Angel Ponsaee7ab22020-03-19 00:31:58 +0100137
Lee Leahy32471722015-04-20 15:20:28 -0700138 /* Clear pending wake status bit to avoid immediate wake */
Elyes Haouasa5064512023-09-17 10:22:02 +0200139 write32p(0xfed88000 + 0x0200, read32p(0xfed88000 + 0x0200));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700140
Lee Leahy32471722015-04-20 15:20:28 -0700141 /* Tri-state specific GPIOS to avoid leakage during S3/S5 */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500142 if ((slp_typ == ACPI_S3) || (slp_typ == ACPI_S5))
Lee Leahy32471722015-04-20 15:20:28 -0700143 tristate_gpios(PAD_CONTROL_REG0_TRISTATE);
144
145 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100146 * Write back to the SLP register to cause the originally intended event again.
147 * We need to set BIT13 (SLP_EN) though to make the sleep happen.
Lee Leahy77ff0b12015-05-05 15:07:29 -0700148 */
149 enable_pm1_control(SLP_EN);
150
151 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbin1b6196d2016-07-13 23:20:26 -0500152 if (slp_typ >= ACPI_S3)
Lee Leahy32471722015-04-20 15:20:28 -0700153 hlt();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700154
Lee Leahy32471722015-04-20 15:20:28 -0700155 /*
156 * In most sleep states, the code flow of this function ends at
Lee Leahy77ff0b12015-05-05 15:07:29 -0700157 * the line above. However, if we entered sleep state S1 and wake
158 * up again, we will continue to execute code in this function.
159 */
160 reg32 = inl(pmbase + PM1_CNT);
161 if (reg32 & SCI_EN) {
162 /* The OS is not an ACPI OS, so we set the state to S0 */
163 disable_pm1_control(SLP_EN | SLP_TYP);
164 }
165}
166
167/*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100168 * Look for Synchronous IO SMI and use save state from that core in case
169 * we are not running on the same core that initiated the IO transaction.
Lee Leahy77ff0b12015-05-05 15:07:29 -0700170 */
171static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
172{
173 em64t100_smm_state_save_area_t *state;
174 int node;
175
176 /* Check all nodes looking for the one that issued the IO */
177 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
178 state = smm_get_save_state(node);
179
180 /* Check for Synchronous IO (bit0==1) */
181 if (!(state->io_misc_info & (1 << 0)))
182 continue;
183
184 /* Make sure it was a write (bit4==0) */
185 if (state->io_misc_info & (1 << 4))
186 continue;
187
188 /* Check for APMC IO port */
189 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
190 continue;
191
192 /* Check AX against the requested command */
193 if ((state->rax & 0xff) != cmd)
194 continue;
195
196 return state;
197 }
198
199 return NULL;
200}
201
Lee Leahy77ff0b12015-05-05 15:07:29 -0700202static void southbridge_smi_gsmi(void)
203{
204 u32 *ret, *param;
205 uint8_t sub_command;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100206 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700207
208 if (!io_smi)
209 return;
210
211 /* Command and return value in EAX */
Lee Leahy32471722015-04-20 15:20:28 -0700212 ret = (u32 *)&io_smi->rax;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700213 sub_command = (uint8_t)(*ret >> 8);
214
215 /* Parameter buffer in EBX */
Lee Leahy32471722015-04-20 15:20:28 -0700216 param = (u32 *)&io_smi->rbx;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700217
218 /* drivers/elog/gsmi.c */
219 *ret = gsmi_exec(sub_command, param);
220}
Lee Leahy77ff0b12015-05-05 15:07:29 -0700221
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600222static void southbridge_smi_store(void)
223{
224 u8 sub_command, ret;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100225 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_SMMSTORE);
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600226 uint32_t reg_ebx;
227
228 if (!io_smi)
229 return;
230 /* Command and return value in EAX */
231 sub_command = (io_smi->rax >> 8) & 0xff;
232
233 /* Parameter buffer in EBX */
234 reg_ebx = io_smi->rbx;
235
236 /* drivers/smmstore/smi.c */
237 ret = smmstore_exec(sub_command, (void *)reg_ebx);
238 io_smi->rax = ret;
239}
240
Lee Leahy77ff0b12015-05-05 15:07:29 -0700241static void southbridge_smi_apmc(void)
242{
243 uint8_t reg8;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700244
Kyösti Mälkki9a1620f2021-01-08 13:27:33 +0200245 reg8 = apm_get_apmc();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700246 switch (reg8) {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700247 case APM_CNT_ACPI_DISABLE:
248 disable_pm1_control(SCI_EN);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700249 break;
250 case APM_CNT_ACPI_ENABLE:
251 enable_pm1_control(SCI_EN);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700252 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100253 case APM_CNT_ELOG_GSMI:
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200254 if (CONFIG(ELOG_GSMI))
255 southbridge_smi_gsmi();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700256 break;
Matt DeVillieraa3b5e292018-12-25 22:10:48 -0600257 case APM_CNT_SMMSTORE:
258 if (CONFIG(SMMSTORE))
259 southbridge_smi_store();
260 break;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700261 }
262
263 mainboard_smi_apmc(reg8);
264}
265
266static void southbridge_smi_pm1(void)
267{
268 uint16_t pm1_sts = clear_pm1_status();
269
Angel Ponsaee7ab22020-03-19 00:31:58 +0100270 /* While OSPM is not active, poweroff immediately on a power button event */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700271 if (pm1_sts & PWRBTN_STS) {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100272 /* Power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200273 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700274 disable_pm1_control(-1UL);
275 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
276 }
277}
278
279static void southbridge_smi_gpe0(void)
280{
281 clear_gpe_status();
282}
283
284static void southbridge_smi_tco(void)
285{
286 uint32_t tco_sts = clear_tco_status();
287
288 /* Any TCO event? */
289 if (!tco_sts)
290 return;
291
292 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
293 /* Handle TCO timeout */
294 printk(BIOS_DEBUG, "TCO Timeout.\n");
295 }
296}
297
298static void southbridge_smi_periodic(void)
299{
300 uint32_t reg32;
301
302 reg32 = inl(get_pmbase() + SMI_EN);
303
304 /* Are periodic SMIs enabled? */
305 if ((reg32 & PERIODIC_EN) == 0)
306 return;
307
308 printk(BIOS_DEBUG, "Periodic SMI.\n");
309}
310
311typedef void (*smi_handler_t)(void);
312
313static const smi_handler_t southbridge_smi[32] = {
Lee Leahy32471722015-04-20 15:20:28 -0700314 NULL, /* [0] reserved */
315 NULL, /* [1] reserved */
316 NULL, /* [2] BIOS_STS */
317 NULL, /* [3] LEGACY_USB_STS */
318 southbridge_smi_sleep, /* [4] SLP_SMI_STS */
319 southbridge_smi_apmc, /* [5] APM_STS */
320 NULL, /* [6] SWSMI_TMR_STS */
321 NULL, /* [7] reserved */
322 southbridge_smi_pm1, /* [8] PM1_STS */
323 southbridge_smi_gpe0, /* [9] GPE0_STS */
324 NULL, /* [10] reserved */
325 NULL, /* [11] reserved */
326 NULL, /* [12] reserved */
327 southbridge_smi_tco, /* [13] TCO_STS */
328 southbridge_smi_periodic, /* [14] PERIODIC_STS */
329 NULL, /* [15] SERIRQ_SMI_STS */
330 NULL, /* [16] SMBUS_SMI_STS */
331 NULL, /* [17] LEGACY_USB2_STS */
332 NULL, /* [18] INTEL_USB2_STS */
333 NULL, /* [19] reserved */
334 NULL, /* [20] PCI_EXP_SMI_STS */
335 NULL, /* [21] reserved */
336 NULL, /* [22] reserved */
337 NULL, /* [23] reserved */
338 NULL, /* [24] reserved */
339 NULL, /* [25] reserved */
340 NULL, /* [26] SPI_STS */
341 NULL, /* [27] reserved */
342 NULL, /* [28] PUNIT */
343 NULL, /* [29] GUNIT */
344 NULL, /* [30] reserved */
345 NULL /* [31] reserved */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700346};
347
348void southbridge_smi_handler(void)
349{
350 int i;
351 uint32_t smi_sts;
352
Lee Leahy32471722015-04-20 15:20:28 -0700353 /*
354 * We need to clear the SMI status registers, or we won't see what's
Lee Leahy77ff0b12015-05-05 15:07:29 -0700355 * happening in the following calls.
356 */
357 smi_sts = clear_smi_status();
358
359 /* Call SMI sub handler for each of the status bits */
360 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
361 if (!(smi_sts & (1 << i)))
362 continue;
363
364 if (southbridge_smi[i] != NULL) {
365 southbridge_smi[i]();
366 } else {
367 printk(BIOS_DEBUG,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100368 "SMI_STS[%d] occurred, but no handler available.\n", i);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700369 }
370 }
371
Lee Leahy32471722015-04-20 15:20:28 -0700372 /*
373 * The GPIO SMI events do not have a status bit in SMI_STS. Therefore,
374 * these events need to be cleared and checked unconditionally.
375 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700376 mainboard_smi_gpi(clear_alt_status());
377}