Debian Codegraph @

src/soc/intel/broadwell/pch/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 #include <acpi/acpi.h>
9 #include <arch/io.h>
10 #include <bootmode.h>
11 #include <console/console.h>
12 #include <device/device.h>
13 #include <device/pci_ops.h>
14 #include <device/pci.h>
15 #include <halt.h>
16 #include <security/vboot/vbnv.h>
17 #include <soc/iomap.h>
18 #include <soc/lpc.h>
19 #include <soc/pci_devs.h>
20 #include <soc/pm.h>
21 #include <stdint.h>
22
23 #define GPIO_ALT_GPI_SMI_STS 0x50
24 #define GPIO_ALT_GPI_SMI_EN 0x54
25
26 static inline uint16_t get_gpiobase(void)
27 {
28 return GPIO_BASE_ADDRESS;
29 }
30
31 /* Print status bits with descriptive names */
32 static void print_status_bits(u32 status, const char *bit_names[])
33 {
34 int i;
35
36 if (!status)
37 return;
38
39 for (i = 31; i >= 0; i--) {
40 if (status & (1 << i)) {
41 if (bit_names[i])
42 printk(BIOS_DEBUG, "%s ", bit_names[i]);
43 else
44 printk(BIOS_DEBUG, "BIT%d ", i);
45 }
46 }
47 }
48
49 /* Print status bits as GPIO numbers */
50 static void print_gpio_status(u32 status, int start)
51 {
52 int i;
53
54 if (!status)
55 return;
56
57 for (i = 31; i >= 0; i--) {
58 if (status & (1 << i))
59 printk(BIOS_DEBUG, "GPIO%d ", start + i);
60 }
61 }
62
63 /*
64 * PM1_CNT
65 */
66
67 /* Enable events in PM1 control register */
68 void enable_pm1_control(u32 mask)
69 {
70 u32 pm1_cnt = inl(get_pmbase() + PM1_CNT);
71 pm1_cnt |= mask;
72 outl(pm1_cnt, get_pmbase() + PM1_CNT);
73 }
74
75 /* Disable events in PM1 control register */
76 void disable_pm1_control(u32 mask)
77 {
78 u32 pm1_cnt = inl(get_pmbase() + PM1_CNT);
79 pm1_cnt &= ~mask;
80 outl(pm1_cnt, get_pmbase() + PM1_CNT);
81 }
82
83 /*
84 * PM1
85 */
86
87 /* Clear and return PM1 status register */
88 static u16 reset_pm1_status(void)
89 {
90 u16 pm1_sts = inw(get_pmbase() + PM1_STS);
91 outw(pm1_sts, get_pmbase() + PM1_STS);
92 return pm1_sts;
93 }
94
95 /* Print PM1 status bits */
96 static u16 print_pm1_status(u16 pm1_sts)
97 {
98 const char *pm1_sts_bits[] = {
99 [0] = "TMROF",
100 [4] = "BM",
101 [5] = "GBL",
102 [8] = "PWRBTN",
103 [10] = "RTC",
104 [11] = "PRBTNOR",
105 [14] = "PCIEXPWAK",
106 [15] = "WAK",
107 };
108
109 if (!pm1_sts)
110 return 0;
111
112 printk(BIOS_SPEW, "PM1_STS: ");
113 print_status_bits(pm1_sts, pm1_sts_bits);
114 printk(BIOS_SPEW, "\n");
115
116 return pm1_sts;
117 }
118
119 /* Print, clear, and return PM1 status */
120 u16 clear_pm1_status(void)
121 {
122 return print_pm1_status(reset_pm1_status());
123 }
124
125 /* Set the PM1 register to events */
126 void enable_pm1(u16 events)
127 {
128 outw(events, get_pmbase() + PM1_EN);
129 }
130
131 /*
132 * SMI
133 */
134
135 /* Clear and return SMI status register */
136 static u32 reset_smi_status(void)
137 {
138 u32 smi_sts = inl(get_pmbase() + SMI_STS);
139 outl(smi_sts, get_pmbase() + SMI_STS);
140 return smi_sts;
141 }
142
143 /* Print SMI status bits */
144 static u32 print_smi_status(u32 smi_sts)
145 {
146 const char *smi_sts_bits[] = {
147 [2] = "BIOS",
148 [3] = "LEGACY_USB",
149 [4] = "SLP_SMI",
150 [5] = "APM",
151 [6] = "SWSMI_TMR",
152 [8] = "PM1",
153 [9] = "GPE0",
154 [10] = "GPI",
155 [11] = "MCSMI",
156 [12] = "DEVMON",
157 [13] = "TCO",
158 [14] = "PERIODIC",
159 [15] = "SERIRQ_SMI",
160 [16] = "SMBUS_SMI",
161 [17] = "LEGACY_USB2",
162 [18] = "INTEL_USB2",
163 [20] = "PCI_EXP_SMI",
164 [21] = "MONITOR",
165 [26] = "SPI",
166 [27] = "GPIO_UNLOCK"
167 };
168
169 if (!smi_sts)
170 return 0;
171
172 printk(BIOS_DEBUG, "SMI_STS: ");
173 print_status_bits(smi_sts, smi_sts_bits);
174 printk(BIOS_DEBUG, "\n");
175
176 return smi_sts;
177 }
178
179 /* Print, clear, and return SMI status */
180 u32 clear_smi_status(void)
181 {
182 return print_smi_status(reset_smi_status());
183 }
184
185 /* Enable SMI event */
186 void enable_smi(u32 mask)
187 {
188 u32 smi_en = inl(get_pmbase() + SMI_EN);
189 smi_en |= mask;
190 outl(smi_en, get_pmbase() + SMI_EN);
191 }
192
193 /* Disable SMI event */
194 void disable_smi(u32 mask)
195 {
196 u32 smi_en = inl(get_pmbase() + SMI_EN);
197 smi_en &= ~mask;
198 outl(smi_en, get_pmbase() + SMI_EN);
199 }
200
201 /*
202 * ALT_GP_SMI
203 */
204
205 /* Clear GPIO SMI status and return events that are enabled and active */
206 static u32 reset_alt_smi_status(void)
207 {
208 u32 alt_sts, alt_en;
209
210 /* Low Power variant moves this to GPIO region as dword */
211 alt_sts = inl(get_gpiobase() + GPIO_ALT_GPI_SMI_STS);
212 outl(alt_sts, get_gpiobase() + GPIO_ALT_GPI_SMI_STS);
213 alt_en = inl(get_gpiobase() + GPIO_ALT_GPI_SMI_EN);
214
215 /* Only report enabled events */
216 return alt_sts & alt_en;
217 }
218
219 /* Print GPIO SMI status bits */
220 static u32 print_alt_smi_status(u32 alt_sts)
221 {
222 if (!alt_sts)
223 return 0;
224
225 printk(BIOS_DEBUG, "ALT_STS: ");
226
227 /* First 16 events are GPIO 32-47 */
228 print_gpio_status(alt_sts & 0xffff, 32);
229
230 printk(BIOS_DEBUG, "\n");
231
232 return alt_sts;
233 }
234
235 /* Print, clear, and return GPIO SMI status */
236 u32 clear_alt_smi_status(void)
237 {
238 return print_alt_smi_status(reset_alt_smi_status());
239 }
240
241 /* Enable GPIO SMI events */
242 void enable_alt_smi(u32 mask)
243 {
244 u32 alt_en;
245
246 alt_en = inl(get_gpiobase() + GPIO_ALT_GPI_SMI_EN);
247 alt_en |= mask;
248 outl(alt_en, get_gpiobase() + GPIO_ALT_GPI_SMI_EN);
249 }
250
251 /*
252 * TCO
253 */
254
255 /* Clear TCO status and return events that are enabled and active */
256 static u32 reset_tco_status(void)
257 {
258 u32 tcobase = get_pmbase() + 0x60;
259 u32 tco_sts = inl(tcobase + 0x04);
260 u32 tco_en = inl(get_pmbase() + 0x68);
261
262 /* Don't clear BOOT_STS before SECOND_TO_STS */
263 outl(tco_sts & ~(1 << 18), tcobase + 0x04);
264
265 /* Clear BOOT_STS */
266 if (tco_sts & (1 << 18))
267 outl(tco_sts & (1 << 18), tcobase + 0x04);
268
269 return tco_sts & tco_en;
270 }
271
272 /* Print TCO status bits */
273 static u32 print_tco_status(u32 tco_sts)
274 {
275 const char *tco_sts_bits[] = {
276 [0] = "NMI2SMI",
277 [1] = "SW_TCO",
278 [2] = "TCO_INT",
279 [3] = "TIMEOUT",
280 [7] = "NEWCENTURY",
281 [8] = "BIOSWR",
282 [9] = "DMISCI",
283 [10] = "DMISMI",
284 [12] = "DMISERR",
285 [13] = "SLVSEL",
286 [16] = "INTRD_DET",
287 [17] = "SECOND_TO",
288 [18] = "BOOT",
289 [20] = "SMLINK_SLV"
290 };
291
292 if (!tco_sts)
293 return 0;
294
295 printk(BIOS_DEBUG, "TCO_STS: ");
296 print_status_bits(tco_sts, tco_sts_bits);
297 printk(BIOS_DEBUG, "\n");
298
299 return tco_sts;
300 }
301
302 /* Print, clear, and return TCO status */
303 u32 clear_tco_status(void)
304 {
305 return print_tco_status(reset_tco_status());
306 }
307
308 /* Enable TCO SCI */
309 void enable_tco_sci(void)
310 {
311 /* Clear pending events */
312 outl(TCOSCI_STS, get_pmbase() + GPE0_STS(3));
313
314 /* Enable TCO SCI events */
315 enable_gpe(TCOSCI_EN);
316 }
317
318 /*
319 * GPE0
320 */
321
322 /* Clear a GPE0 status and return events that are enabled and active */
323 static u32 reset_gpe_status(u16 sts_reg, u16 en_reg)
324 {
325 u32 gpe0_sts = inl(get_pmbase() + sts_reg);
326 u32 gpe0_en = inl(get_pmbase() + en_reg);
327
328 outl(gpe0_sts, get_pmbase() + sts_reg);
329
330 /* Only report enabled events */
331 return gpe0_sts & gpe0_en;
332 }
333
334 /* Print GPE0 status bits */
335 static u32 print_gpe_status(u32 gpe0_sts, const char *bit_names[])
336 {
337 if (!gpe0_sts)
338 return 0;
339
340 printk(BIOS_DEBUG, "GPE0_STS: ");
341 print_status_bits(gpe0_sts, bit_names);
342 printk(BIOS_DEBUG, "\n");
343
344 return gpe0_sts;
345 }
346
347 /* Print GPE0 GPIO status bits */
348 static u32 print_gpe_gpio(u32 gpe0_sts, int start)
349 {
350 if (!gpe0_sts)
351 return 0;
352
353 printk(BIOS_DEBUG, "GPE0_STS: ");
354 print_gpio_status(gpe0_sts, start);
355 printk(BIOS_DEBUG, "\n");
356
357 return gpe0_sts;
358 }
359
360 /* Clear all GPE status and return "standard" GPE event status */
361 u32 clear_gpe_status(void)
362 {
363 const char *gpe0_sts_3_bits[] = {
364 [1] = "HOTPLUG",
365 [2] = "SWGPE",
366 [6] = "TCO_SCI",
367 [7] = "SMB_WAK",
368 [9] = "PCI_EXP",
369 [10] = "BATLOW",
370 [11] = "PME",
371 [12] = "ME",
372 [13] = "PME_B0",
373 [16] = "GPIO27",
374 [18] = "WADT"
375 };
376
377 print_gpe_gpio(reset_gpe_status(GPE0_STS(GPE_31_0), GPE0_EN(GPE_31_0)), 0);
378 print_gpe_gpio(reset_gpe_status(GPE0_STS(GPE_63_32), GPE0_EN(GPE_63_32)), 32);
379 print_gpe_gpio(reset_gpe_status(GPE0_STS(GPE_94_64), GPE0_EN(GPE_94_64)), 64);
380 return print_gpe_status(reset_gpe_status(GPE0_STS(GPE_STD), GPE0_EN(GPE_STD)),
381 gpe0_sts_3_bits);
382 }
383
384 /* Enable all requested GPE */
385 void enable_all_gpe(u32 set1, u32 set2, u32 set3, u32 set4)
386 {
387 u16 pmbase = get_pmbase();
388
389 outl(set1, pmbase + GPE0_EN(GPE_31_0));
390 outl(set2, pmbase + GPE0_EN(GPE_63_32));
391 outl(set3, pmbase + GPE0_EN(GPE_94_64));
392 outl(set4, pmbase + GPE0_EN(GPE_STD));
393 }
394
395 /* Disable all GPE */
396 void disable_all_gpe(void)
397 {
398 enable_all_gpe(0, 0, 0, 0);
399 }
400
401 /* Enable a standard GPE */
402 void enable_gpe(u32 mask)
403 {
404 u32 gpe0_en = inl(get_pmbase() + GPE0_EN(GPE_STD));
405 gpe0_en |= mask;
406 outl(gpe0_en, get_pmbase() + GPE0_EN(GPE_STD));
407 }
408
409 /* Disable a standard GPE */
410 void disable_gpe(u32 mask)
411 {
412 u32 gpe0_en = inl(get_pmbase() + GPE0_EN(GPE_STD));
413 gpe0_en &= ~mask;
414 outl(gpe0_en, get_pmbase() + GPE0_EN(GPE_STD));
415 }
416
417 int platform_is_resuming(void)
418 {
419 if (!(inw(get_pmbase() + PM1_STS) & WAK_STS))
420 return 0;
421
422 return acpi_sleep_from_pm1(inl(get_pmbase() + PM1_CNT)) == ACPI_S3;
423 }
424
425 void poweroff(void)
426 {
427 uint32_t pm1_cnt;
428
429 /* Go to S5 */
430 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
431 pm1_cnt |= (0xf << 10);
432 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
433 }
434
435 /* STM Support */
436 uint16_t get_pmbase(void)
437 {
438 return (uint16_t)ACPI_BASE_ADDRESS;
439 }