mb/starlabs/*: Correct config for SATA DEVSLP GPIO
[coreboot2.git] / src / soc / intel / alderlake / pmutil.c
blobc4591007e78745eb6754430ffaf3006dff6032dd
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * Helper functions for dealing with power management registers
5 * and the differences between PCH variants.
6 */
8 /*
9 * This file is created based on Intel Alder Lake Processor PCH Datasheet
10 * Document number: 621483
11 * Chapter number: 4
14 #define __SIMPLE_DEVICE__
16 #include <acpi/acpi_pm.h>
17 #include <console/console.h>
18 #include <device/mmio.h>
19 #include <device/device.h>
20 #include <device/pci.h>
21 #include <gpio.h>
22 #include <intelblocks/pmclib.h>
23 #include <intelblocks/rtc.h>
24 #include <intelblocks/tco.h>
25 #include <intelpch/espi.h>
26 #include <security/vboot/vbnv.h>
27 #include <soc/gpe.h>
28 #include <soc/iomap.h>
29 #include <soc/pci_devs.h>
30 #include <soc/pm.h>
31 #include <soc/smbus.h>
32 #include <soc/soc_chip.h>
33 #include <static.h>
34 #include <types.h>
37 * SMI
40 const char *const *soc_smi_sts_array(size_t *a)
42 static const char *const smi_sts_bits[] = {
43 [BIOS_STS_BIT] = "BIOS",
44 [LEGACY_USB_STS_BIT] = "LEGACY_USB",
45 [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI",
46 [APM_STS_BIT] = "APM",
47 [SWSMI_TMR_STS_BIT] = "SWSMI_TMR",
48 [PM1_STS_BIT] = "PM1",
49 [GPE0_STS_BIT] = "GPE0",
50 [GPIO_STS_BIT] = "GPI",
51 [MCSMI_STS_BIT] = "MCSMI",
52 [DEVMON_STS_BIT] = "DEVMON",
53 [TCO_STS_BIT] = "TCO",
54 [PERIODIC_STS_BIT] = "PERIODIC",
55 [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI",
56 [SMBUS_SMI_STS_BIT] = "SMBUS_SMI",
57 [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI",
58 [MONITOR_STS_BIT] = "MONITOR",
59 [SPI_SMI_STS_BIT] = "SPI",
60 [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK",
61 [ESPI_SMI_STS_BIT] = "ESPI_SMI",
64 *a = ARRAY_SIZE(smi_sts_bits);
65 return smi_sts_bits;
69 * TCO
72 const char *const *soc_tco_sts_array(size_t *a)
74 static const char *const tco_sts_bits[] = {
75 [0] = "NMI2SMI",
76 [1] = "SW_TCO",
77 [2] = "TCO_INT",
78 [3] = "TIMEOUT",
79 [7] = "NEWCENTURY",
80 [8] = "BIOSWR",
81 [9] = "DMISCI",
82 [10] = "DMISMI",
83 [12] = "DMISERR",
84 [13] = "SLVSEL",
85 [16] = "INTRD_DET",
86 [17] = "SECOND_TO",
87 [18] = "BOOT",
88 [20] = "SMLINK_SLV"
91 *a = ARRAY_SIZE(tco_sts_bits);
92 return tco_sts_bits;
96 * GPE0
99 const char *const *soc_std_gpe_sts_array(size_t *a)
101 static const char *const gpe_sts_bits[] = {
102 [1] = "HOTPLUG",
103 [2] = "SWGPE",
104 [6] = "TCO_SCI",
105 [7] = "SMB_WAK",
106 [9] = "PCI_EXP",
107 [10] = "BATLOW",
108 [11] = "PME",
109 [12] = "ME",
110 [13] = "PME_B0",
111 [14] = "eSPI",
112 [15] = "GPIO Tier-2",
113 [16] = "LAN_WAKE",
114 [18] = "WADT"
117 *a = ARRAY_SIZE(gpe_sts_bits);
118 return gpe_sts_bits;
121 void pmc_set_disb(void)
123 /* Set the DISB after DRAM init */
124 uint8_t disb_val;
125 /* Only care about bits [23:16] of register GEN_PMCON_A */
126 uint8_t *addr = (uint8_t *)(pmc_mmio_regs() + GEN_PMCON_A + 2);
128 disb_val = read8(addr);
129 disb_val |= (DISB >> 16);
131 /* Don't clear bits that are write-1-to-clear */
132 disb_val &= ~((MS4V | SUS_PWR_FLR) >> 16);
133 write8(addr, disb_val);
137 * PMC controller gets hidden from PCI bus
138 * during FSP-Silicon init call. Hence PWRMBASE
139 * can't be accessible using PCI configuration space
140 * read/write.
142 uint8_t *pmc_mmio_regs(void)
144 return (void *)(uintptr_t)PCH_PWRM_BASE_ADDRESS;
147 uintptr_t soc_read_pmc_base(void)
149 return (uintptr_t)pmc_mmio_regs();
152 uint32_t *soc_pmc_etr_addr(void)
154 return (uint32_t *)(soc_read_pmc_base() + ETR);
157 static void pmc_gpe0_different_values(const struct soc_intel_alderlake_config *config)
159 bool all_zero = (config->pmc_gpe0_dw0 == 0) &&
160 (config->pmc_gpe0_dw1 == 0) &&
161 (config->pmc_gpe0_dw2 == 0);
163 /* Check if all values are different AND not all zero */
164 bool all_different = (config->pmc_gpe0_dw0 != config->pmc_gpe0_dw1) &&
165 (config->pmc_gpe0_dw0 != config->pmc_gpe0_dw2) &&
166 (config->pmc_gpe0_dw1 != config->pmc_gpe0_dw2);
168 assert(all_different || all_zero);
171 void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
173 DEVTREE_CONST struct soc_intel_alderlake_config *config;
175 config = config_of_soc();
177 pmc_gpe0_different_values(config);
179 /* Assign to out variable */
180 *dw0 = config->pmc_gpe0_dw0;
181 *dw1 = config->pmc_gpe0_dw1;
182 *dw2 = config->pmc_gpe0_dw2;
185 static int rtc_failed(uint32_t gen_pmcon_b)
187 return !!(gen_pmcon_b & RTC_BATTERY_DEAD);
190 int soc_get_rtc_failed(void)
192 const struct chipset_power_state *ps;
194 if (acpi_fetch_pm_state(&ps, PS_CLAIMER_RTC) < 0)
195 return 1;
197 return rtc_failed(ps->gen_pmcon_b);
200 int vbnv_cmos_failed(void)
202 return rtc_failed(read32(pmc_mmio_regs() + GEN_PMCON_B));
205 static inline int deep_s3_enabled(void)
207 uint32_t deep_s3_pol;
209 deep_s3_pol = read32(pmc_mmio_regs() + S3_PWRGATE_POL);
210 return !!(deep_s3_pol & (S3DC_GATE_SUS | S3AC_GATE_SUS));
213 /* Return 0, 3, or 5 to indicate the previous sleep state. */
214 int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state)
217 * Check for any power failure to determine if this a wake from
218 * S5 because the PCH does not set the WAK_STS bit when waking
219 * from a true G3 state.
221 if (!(ps->pm1_sts & WAK_STS) && (ps->gen_pmcon_a & (PWR_FLR | SUS_PWR_FLR)))
222 prev_sleep_state = ACPI_S5;
225 * If waking from S3 determine if deep S3 is enabled. If not,
226 * need to check both deep sleep well and normal suspend well.
227 * Otherwise just check deep sleep well.
229 if (prev_sleep_state == ACPI_S3) {
230 /* PWR_FLR represents deep sleep power well loss. */
231 uint32_t mask = PWR_FLR;
233 /* If deep s3 isn't enabled check the suspend well too. */
234 if (!deep_s3_enabled())
235 mask |= SUS_PWR_FLR;
237 if (ps->gen_pmcon_a & mask)
238 prev_sleep_state = ACPI_S5;
241 return prev_sleep_state;
244 void soc_fill_power_state(struct chipset_power_state *ps)
246 uint8_t *pmc;
248 ps->tco1_sts = tco_read_reg(TCO1_STS);
249 ps->tco2_sts = tco_read_reg(TCO2_STS);
251 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts);
253 pmc = pmc_mmio_regs();
254 ps->gen_pmcon_a = read32(pmc + GEN_PMCON_A);
255 ps->gen_pmcon_b = read32(pmc + GEN_PMCON_B);
256 ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
257 ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
258 ps->hpr_cause0 = read32(pmc + HPR_CAUSE0);
260 printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
261 ps->gen_pmcon_a, ps->gen_pmcon_b);
263 printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
264 ps->gblrst_cause[0], ps->gblrst_cause[1]);
266 printk(BIOS_DEBUG, "HPR_CAUSE0: %08x\n", ps->hpr_cause0);
269 /* STM Support */
270 uint16_t get_pmbase(void)
272 return (uint16_t)ACPI_BASE_ADDRESS;
276 * Set which power state system will be after reapplying
277 * the power (from G3 State)
279 void pmc_soc_set_afterg3_en(const bool on)
281 uint8_t reg8;
282 uint8_t *const pmcbase = pmc_mmio_regs();
284 reg8 = read8(pmcbase + GEN_PMCON_A);
285 if (on)
286 reg8 &= ~SLEEP_AFTER_POWER_FAIL;
287 else
288 reg8 |= SLEEP_AFTER_POWER_FAIL;
289 write8(pmcbase + GEN_PMCON_A, reg8);