blob: 172d8cda202441130355d4dc109d0253778fca64 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070013 */
14
15/*
16 * Helper functions for dealing with power management registers
17 * and the differences between PCH variants.
18 */
19
Joel Kitching1d93b882018-09-26 17:58:14 +080020#include <arch/acpi.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070021#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070023#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_def.h>
26#include <console/console.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070027#include <soc/iomap.h>
28#include <soc/lpc.h>
29#include <soc/pci_devs.h>
30#include <soc/pm.h>
31#include <soc/gpio.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020032#include <security/vboot/vbnv.h>
Joel Kitching1d93b882018-09-26 17:58:14 +080033#include <security/vboot/vboot_common.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034
35/* Print status bits with descriptive names */
36static void print_status_bits(u32 status, const char *bit_names[])
37{
38 int i;
39
40 if (!status)
41 return;
42
Lee Leahy26b7cd02017-03-16 18:47:55 -070043 for (i = 31; i >= 0; i--) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044 if (status & (1 << i)) {
45 if (bit_names[i])
46 printk(BIOS_DEBUG, "%s ", bit_names[i]);
47 else
48 printk(BIOS_DEBUG, "BIT%d ", i);
49 }
50 }
51}
52
53/* Print status bits as GPIO numbers */
54static void print_gpio_status(u32 status, int start)
55{
56 int i;
57
58 if (!status)
59 return;
60
Lee Leahy26b7cd02017-03-16 18:47:55 -070061 for (i = 31; i >= 0; i--) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -070062 if (status & (1 << i))
63 printk(BIOS_DEBUG, "GPIO%d ", start + i);
64 }
65}
66
67
68/*
69 * PM1_CNT
70 */
71
72/* Enable events in PM1 control register */
73void enable_pm1_control(u32 mask)
74{
75 u32 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
76 pm1_cnt |= mask;
77 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
78}
79
80/* Disable events in PM1 control register */
81void disable_pm1_control(u32 mask)
82{
83 u32 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
84 pm1_cnt &= ~mask;
85 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
86}
87
88
89/*
90 * PM1
91 */
92
93/* Clear and return PM1 status register */
94static u16 reset_pm1_status(void)
95{
96 u16 pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
97 outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
98 return pm1_sts;
99}
100
101/* Print PM1 status bits */
102static u16 print_pm1_status(u16 pm1_sts)
103{
104 const char *pm1_sts_bits[] = {
105 [0] = "TMROF",
106 [4] = "BM",
107 [5] = "GBL",
108 [8] = "PWRBTN",
109 [10] = "RTC",
110 [11] = "PRBTNOR",
111 [14] = "PCIEXPWAK",
112 [15] = "WAK",
113 };
114
115 if (!pm1_sts)
116 return 0;
117
118 printk(BIOS_SPEW, "PM1_STS: ");
119 print_status_bits(pm1_sts, pm1_sts_bits);
120 printk(BIOS_SPEW, "\n");
121
122 return pm1_sts;
123}
124
125/* Print, clear, and return PM1 status */
126u16 clear_pm1_status(void)
127{
128 return print_pm1_status(reset_pm1_status());
129}
130
131/* Set the PM1 register to events */
132void enable_pm1(u16 events)
133{
134 outw(events, ACPI_BASE_ADDRESS + PM1_EN);
135}
136
137
138/*
139 * SMI
140 */
141
142/* Clear and return SMI status register */
143static u32 reset_smi_status(void)
144{
145 u32 smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
146 outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
147 return smi_sts;
148}
149
150/* Print SMI status bits */
151static u32 print_smi_status(u32 smi_sts)
152{
153 const char *smi_sts_bits[] = {
154 [2] = "BIOS",
155 [3] = "LEGACY_USB",
156 [4] = "SLP_SMI",
157 [5] = "APM",
158 [6] = "SWSMI_TMR",
159 [8] = "PM1",
160 [9] = "GPE0",
161 [10] = "GPI",
162 [11] = "MCSMI",
163 [12] = "DEVMON",
164 [13] = "TCO",
165 [14] = "PERIODIC",
166 [15] = "SERIRQ_SMI",
167 [16] = "SMBUS_SMI",
168 [17] = "LEGACY_USB2",
169 [18] = "INTEL_USB2",
170 [20] = "PCI_EXP_SMI",
171 [21] = "MONITOR",
172 [26] = "SPI",
173 [27] = "GPIO_UNLOCK"
174 };
175
176 if (!smi_sts)
177 return 0;
178
179 printk(BIOS_DEBUG, "SMI_STS: ");
180 print_status_bits(smi_sts, smi_sts_bits);
181 printk(BIOS_DEBUG, "\n");
182
183 return smi_sts;
184}
185
186/* Print, clear, and return SMI status */
187u32 clear_smi_status(void)
188{
189 return print_smi_status(reset_smi_status());
190}
191
192/* Enable SMI event */
193void enable_smi(u32 mask)
194{
195 u32 smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
196 smi_en |= mask;
197 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
198}
199
200/* Disable SMI event */
201void disable_smi(u32 mask)
202{
203 u32 smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
204 smi_en &= ~mask;
205 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
206}
207
208
209/*
210 * ALT_GP_SMI
211 */
212
213/* Clear GPIO SMI status and return events that are enabled and active */
214static u32 reset_alt_smi_status(void)
215{
216 u32 alt_sts, alt_en;
217
218 /* Low Power variant moves this to GPIO region as dword */
219 alt_sts = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_STS);
220 outl(alt_sts, GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_STS);
221 alt_en = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN);
222
223 /* Only report enabled events */
224 return alt_sts & alt_en;
225}
226
227/* Print GPIO SMI status bits */
228static u32 print_alt_smi_status(u32 alt_sts)
229{
230 if (!alt_sts)
231 return 0;
232
233 printk(BIOS_DEBUG, "ALT_STS: ");
234
235 /* First 16 events are GPIO 32-47 */
236 print_gpio_status(alt_sts & 0xffff, 32);
237
238 printk(BIOS_DEBUG, "\n");
239
240 return alt_sts;
241}
242
243/* Print, clear, and return GPIO SMI status */
244u32 clear_alt_smi_status(void)
245{
246 return print_alt_smi_status(reset_alt_smi_status());
247}
248
249/* Enable GPIO SMI events */
250void enable_alt_smi(u32 mask)
251{
252 u32 alt_en;
253
254 alt_en = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN);
255 alt_en |= mask;
256 outl(alt_en, GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN);
257}
258
259
260/*
261 * TCO
262 */
263
264/* Clear TCO status and return events that are enabled and active */
265static u32 reset_tco_status(void)
266{
267 u32 tcobase = ACPI_BASE_ADDRESS + 0x60;
268 u32 tco_sts = inl(tcobase + 0x04);
269 u32 tco_en = inl(ACPI_BASE_ADDRESS + 0x68);
270
271 /* Don't clear BOOT_STS before SECOND_TO_STS */
272 outl(tco_sts & ~(1 << 18), tcobase + 0x04);
273
274 /* Clear BOOT_STS */
275 if (tco_sts & (1 << 18))
276 outl(tco_sts & (1 << 18), tcobase + 0x04);
277
278 return tco_sts & tco_en;
279}
280
281/* Print TCO status bits */
282static u32 print_tco_status(u32 tco_sts)
283{
284 const char *tco_sts_bits[] = {
285 [0] = "NMI2SMI",
286 [1] = "SW_TCO",
287 [2] = "TCO_INT",
288 [3] = "TIMEOUT",
289 [7] = "NEWCENTURY",
290 [8] = "BIOSWR",
291 [9] = "DMISCI",
292 [10] = "DMISMI",
293 [12] = "DMISERR",
294 [13] = "SLVSEL",
295 [16] = "INTRD_DET",
296 [17] = "SECOND_TO",
297 [18] = "BOOT",
298 [20] = "SMLINK_SLV"
299 };
300
301 if (!tco_sts)
302 return 0;
303
304 printk(BIOS_DEBUG, "TCO_STS: ");
305 print_status_bits(tco_sts, tco_sts_bits);
306 printk(BIOS_DEBUG, "\n");
307
308 return tco_sts;
309}
310
311/* Print, clear, and return TCO status */
312u32 clear_tco_status(void)
313{
314 return print_tco_status(reset_tco_status());
315}
316
317/* Enable TCO SCI */
318void enable_tco_sci(void)
319{
320 /* Clear pending events */
321 outl(ACPI_BASE_ADDRESS + GPE0_STS(3), TCOSCI_STS);
322
323 /* Enable TCO SCI events */
324 enable_gpe(TCOSCI_EN);
325}
326
327
328/*
329 * GPE0
330 */
331
332/* Clear a GPE0 status and return events that are enabled and active */
333static u32 reset_gpe(u16 sts_reg, u16 en_reg)
334{
335 u32 gpe0_sts = inl(ACPI_BASE_ADDRESS + sts_reg);
336 u32 gpe0_en = inl(ACPI_BASE_ADDRESS + en_reg);
337
338 outl(gpe0_sts, ACPI_BASE_ADDRESS + sts_reg);
339
340 /* Only report enabled events */
341 return gpe0_sts & gpe0_en;
342}
343
344/* Print GPE0 status bits */
345static u32 print_gpe_status(u32 gpe0_sts, const char *bit_names[])
346{
347 if (!gpe0_sts)
348 return 0;
349
350 printk(BIOS_DEBUG, "GPE0_STS: ");
351 print_status_bits(gpe0_sts, bit_names);
352 printk(BIOS_DEBUG, "\n");
353
354 return gpe0_sts;
355}
356
357/* Print GPE0 GPIO status bits */
358static u32 print_gpe_gpio(u32 gpe0_sts, int start)
359{
360 if (!gpe0_sts)
361 return 0;
362
363 printk(BIOS_DEBUG, "GPE0_STS: ");
364 print_gpio_status(gpe0_sts, start);
365 printk(BIOS_DEBUG, "\n");
366
367 return gpe0_sts;
368}
369
370/* Clear all GPE status and return "standard" GPE event status */
371u32 clear_gpe_status(void)
372{
373 const char *gpe0_sts_3_bits[] = {
374 [1] = "HOTPLUG",
375 [2] = "SWGPE",
376 [6] = "TCO_SCI",
377 [7] = "SMB_WAK",
378 [9] = "PCI_EXP",
379 [10] = "BATLOW",
380 [11] = "PME",
381 [12] = "ME",
382 [13] = "PME_B0",
383 [16] = "GPIO27",
384 [18] = "WADT"
385 };
386
387 print_gpe_gpio(reset_gpe(GPE0_STS(GPE_31_0), GPE0_EN(GPE_31_0)), 0);
388 print_gpe_gpio(reset_gpe(GPE0_STS(GPE_63_32), GPE0_EN(GPE_63_32)), 32);
389 print_gpe_gpio(reset_gpe(GPE0_STS(GPE_94_64), GPE0_EN(GPE_94_64)), 64);
390 return print_gpe_status(reset_gpe(GPE0_STS(GPE_STD), GPE0_EN(GPE_STD)),
391 gpe0_sts_3_bits);
392}
393
394/* Enable all requested GPE */
395void enable_all_gpe(u32 set1, u32 set2, u32 set3, u32 set4)
396{
397 outl(set1, ACPI_BASE_ADDRESS + GPE0_EN(GPE_31_0));
398 outl(set2, ACPI_BASE_ADDRESS + GPE0_EN(GPE_63_32));
399 outl(set3, ACPI_BASE_ADDRESS + GPE0_EN(GPE_94_64));
400 outl(set4, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
401}
402
403/* Disable all GPE */
404void disable_all_gpe(void)
405{
406 enable_all_gpe(0, 0, 0, 0);
407}
408
409/* Enable a standard GPE */
410void enable_gpe(u32 mask)
411{
412 u32 gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
413 gpe0_en |= mask;
414 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
415}
416
417/* Disable a standard GPE */
418void disable_gpe(u32 mask)
419{
420 u32 gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
421 gpe0_en &= ~mask;
422 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
423}
424
425int acpi_sci_irq(void)
426{
427 int scis = pci_read_config32(PCH_DEV_LPC, ACPI_CNTL) & SCI_IRQ_SEL;
428 int sci_irq = 9;
429
430 /* Determine how SCI is routed. */
431 switch (scis) {
432 case SCIS_IRQ9:
433 case SCIS_IRQ10:
434 case SCIS_IRQ11:
435 sci_irq = scis - SCIS_IRQ9 + 9;
436 break;
437 case SCIS_IRQ20:
438 case SCIS_IRQ21:
439 case SCIS_IRQ22:
440 case SCIS_IRQ23:
441 sci_irq = scis - SCIS_IRQ20 + 20;
442 break;
443 default:
444 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
445 sci_irq = 9;
446 break;
447 }
448
449 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
450 return sci_irq;
451}
Aaron Durbinb9d9b792017-09-15 11:51:58 -0600452
Joel Kitching1d93b882018-09-26 17:58:14 +0800453int vboot_platform_is_resuming(void)
454{
455 if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
456 return 0;
457
458 return acpi_sleep_from_pm1(inl(ACPI_BASE_ADDRESS + PM1_CNT)) == ACPI_S3;
459}
Eugene Myersebc84232020-01-21 16:46:16 -0500460
461/* STM Support */
462uint16_t get_pmbase(void)
463{
464 return (uint16_t) ACPI_BASE_ADDRESS;
465}