Debian Codegraph @

src/soc/intel/cannonlake/pmutil.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * Helper functions for dealing with power management registers
5 * and the differences between PCH variants.
6 */
7
8 #define __SIMPLE_DEVICE__
9
10 #include <acpi/acpi_pm.h>
11 #include <console/console.h>
12 #include <device/mmio.h>
13 #include <device/device.h>
14 #include <device/pci.h>
15 #include <gpio.h>
16 #include <intelblocks/pmclib.h>
17 #include <intelblocks/rtc.h>
18 #include <intelblocks/tco.h>
19 #include <soc/gpe.h>
20 #include <soc/iomap.h>
21 #include <soc/lpc.h>
22 #include <soc/pci_devs.h>
23 #include <soc/pm.h>
24 #include <soc/smbus.h>
25 #include <security/vboot/vbnv.h>
26 #include <static.h>
27
28 #include "chip.h"
29
30 /*
31 * SMI
32 */
33
34 const char *const *soc_smi_sts_array(size_t *a)
35 {
36 static const char *const smi_sts_bits[] = {
37 [BIOS_STS_BIT] = "BIOS",
38 [LEGACY_USB_STS_BIT] = "LEGACY_USB",
39 [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI",
40 [APM_STS_BIT] = "APM",
41 [SWSMI_TMR_STS_BIT] = "SWSMI_TMR",
42 [PM1_STS_BIT] = "PM1",
43 [GPE0_STS_BIT] = "GPE0",
44 [GPIO_STS_BIT] = "GPI",
45 [MCSMI_STS_BIT] = "MCSMI",
46 [DEVMON_STS_BIT] = "DEVMON",
47 [TCO_STS_BIT] = "TCO",
48 [PERIODIC_STS_BIT] = "PERIODIC",
49 [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI",
50 [SMBUS_SMI_STS_BIT] = "SMBUS_SMI",
51 [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI",
52 [MONITOR_STS_BIT] = "MONITOR",
53 [SPI_SMI_STS_BIT] = "SPI",
54 [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK",
55 [ESPI_SMI_STS_BIT] = "ESPI_SMI",
56 };
57
58 *a = ARRAY_SIZE(smi_sts_bits);
59 return smi_sts_bits;
60 }
61
62 /*
63 * TCO
64 */
65
66 const char *const *soc_tco_sts_array(size_t *a)
67 {
68 static const char *const tco_sts_bits[] = {
69 [0] = "NMI2SMI",
70 [1] = "SW_TCO",
71 [2] = "TCO_INT",
72 [3] = "TIMEOUT",
73 [7] = "NEWCENTURY",
74 [8] = "BIOSWR",
75 [9] = "DMISCI",
76 [10] = "DMISMI",
77 [12] = "DMISERR",
78 [13] = "SLVSEL",
79 [16] = "INTRD_DET",
80 [17] = "SECOND_TO",
81 [18] = "BOOT",
82 [20] = "SMLINK_SLV"
83 };
84
85 *a = ARRAY_SIZE(tco_sts_bits);
86 return tco_sts_bits;
87 }
88
89 /*
90 * GPE0
91 */
92
93 const char *const *soc_std_gpe_sts_array(size_t *a)
94 {
95 static const char *const gpe_sts_bits[] = {
96 [1] = "HOTPLUG",
97 [2] = "SWGPE",
98 [6] = "TCO_SCI",
99 [7] = "SMB_WAK",
100 [9] = "PCI_EXP",
101 [10] = "BATLOW",
102 [11] = "PME",
103 [12] = "ME",
104 [13] = "PME_B0",
105 [14] = "eSPI",
106 [15] = "GPIO Tier-2",
107 [16] = "LAN_WAKE",
108 [18] = "WADT"
109 };
110
111 *a = ARRAY_SIZE(gpe_sts_bits);
112 return gpe_sts_bits;
113 }
114
115 void pmc_set_disb(void)
116 {
117 /* Set the DISB after DRAM init */
118 uint8_t disb_val;
119 /* Only care about bits [23:16] of register GEN_PMCON_A */
120 uint8_t *addr = (uint8_t *)(pmc_mmio_regs() + GEN_PMCON_A + 2);
121
122 disb_val = read8(addr);
123 disb_val |= (DISB >> 16);
124
125 /* Don't clear bits that are write-1-to-clear */
126 disb_val &= ~((MS4V | SUS_PWR_FLR) >> 16);
127 write8(addr, disb_val);
128 }
129
130 /*
131 * PMC controller gets hidden from PCI bus
132 * during FSP-Silicon init call. Hence PWRMBASE
133 * can't be accessible using PCI configuration space
134 * read/write.
135 */
136 uint8_t *pmc_mmio_regs(void)
137 {
138 return (void *)(uintptr_t)PCH_PWRM_BASE_ADDRESS;
139 }
140
141 uintptr_t soc_read_pmc_base(void)
142 {
143 return (uintptr_t)pmc_mmio_regs();
144 }
145
146 uint32_t *soc_pmc_etr_addr(void)
147 {
148 return (uint32_t *)(soc_read_pmc_base() + ETR);
149 }
150
151 static void gpe0_different_values(const struct soc_intel_cannonlake_config *config)
152 {
153 bool all_zero = (config->gpe0_dw0 == 0) &&
154 (config->gpe0_dw1 == 0) &&
155 (config->gpe0_dw2 == 0);
156
157 /* Check if all values are different AND not all zero */
158 bool all_different = (config->gpe0_dw0 != config->gpe0_dw1) &&
159 (config->gpe0_dw0 != config->gpe0_dw2) &&
160 (config->gpe0_dw1 != config->gpe0_dw2);
161
162 assert(all_different || all_zero);
163 }
164
165 void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
166 {
167 DEVTREE_CONST struct soc_intel_cannonlake_config *config;
168 config = config_of_soc();
169
170 gpe0_different_values(config);
171
172 /* Assign to out variable */
173 *dw0 = config->gpe0_dw0;
174 *dw1 = config->gpe0_dw1;
175 *dw2 = config->gpe0_dw2;
176 }
177
178 static int rtc_failed(uint32_t gen_pmcon_b)
179 {
180 return !!(gen_pmcon_b & RTC_BATTERY_DEAD);
181 }
182
183 int soc_get_rtc_failed(void)
184 {
185 const struct chipset_power_state *ps;
186
187 if (acpi_fetch_pm_state(&ps, PS_CLAIMER_RTC) < 0)
188 return 1;
189
190 return rtc_failed(ps->gen_pmcon_b);
191 }
192
193 int vbnv_cmos_failed(void)
194 {
195 return rtc_failed(read32(pmc_mmio_regs() + GEN_PMCON_B));
196 }
197
198 static inline int deep_s3_enabled(void)
199 {
200 uint32_t deep_s3_pol;
201
202 deep_s3_pol = read32(pmc_mmio_regs() + S3_PWRGATE_POL);
203 return !!(deep_s3_pol & (S3DC_GATE_SUS | S3AC_GATE_SUS));
204 }
205
206 /* Return 0, 3, or 5 to indicate the previous sleep state. */
207 int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state)
208 {
209 /*
210 * Check for any power failure to determine if this a wake from
211 * S5 because the PCH does not set the WAK_STS bit when waking
212 * from a true G3 state.
213 */
214 if (!(ps->pm1_sts & WAK_STS) && (ps->gen_pmcon_a & (PWR_FLR | SUS_PWR_FLR)))
215 prev_sleep_state = ACPI_S5;
216
217 /*
218 * If waking from S3 determine if deep S3 is enabled. If not,
219 * need to check both deep sleep well and normal suspend well.
220 * Otherwise just check deep sleep well.
221 */
222 if (prev_sleep_state == ACPI_S3) {
223 /* PWR_FLR represents deep sleep power well loss. */
224 uint32_t mask = PWR_FLR;
225
226 /* If deep s3 isn't enabled check the suspend well too. */
227 if (!deep_s3_enabled())
228 mask |= SUS_PWR_FLR;
229
230 if (ps->gen_pmcon_a & mask)
231 prev_sleep_state = ACPI_S5;
232 }
233
234 return prev_sleep_state;
235 }
236
237 void soc_fill_power_state(struct chipset_power_state *ps)
238 {
239 uint8_t *pmc;
240
241 ps->tco1_sts = tco_read_reg(TCO1_STS);
242 ps->tco2_sts = tco_read_reg(TCO2_STS);
243
244 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts);
245
246 pmc = pmc_mmio_regs();
247 ps->gen_pmcon_a = read32(pmc + GEN_PMCON_A);
248 ps->gen_pmcon_b = read32(pmc + GEN_PMCON_B);
249 ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
250 ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
251 ps->hpr_cause0 = read32(pmc + HPR_CAUSE0);
252
253 printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
254 ps->gen_pmcon_a, ps->gen_pmcon_b);
255
256 printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
257 ps->gblrst_cause[0], ps->gblrst_cause[1]);
258
259 printk(BIOS_DEBUG, "HPR_CAUSE0: %08x\n", ps->hpr_cause0);
260 }
261
262 /* STM Support */
263 uint16_t get_pmbase(void)
264 {
265 return (uint16_t)ACPI_BASE_ADDRESS;
266 }
267
268 /*
269 * Set which power state system will be after reapplying
270 * the power (from G3 State)
271 */
272 void pmc_soc_set_afterg3_en(const bool on)
273 {
274 uint8_t reg8;
275 uint8_t *const pmcbase = pmc_mmio_regs();
276
277 reg8 = read8(pmcbase + GEN_PMCON_A);
278 if (on)
279 reg8 &= ~SLEEP_AFTER_POWER_FAIL;
280 else
281 reg8 |= SLEEP_AFTER_POWER_FAIL;
282 write8(pmcbase + GEN_PMCON_A, reg8);
283 }