blob: aa5b3621f10c879d7210ed819f72763aff01ac45 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Shaunak Saha9dffbdd2017-03-08 19:27:17 -08002
Kyösti Mälkki27872372021-01-21 16:05:26 +02003#include <acpi/acpi_pm.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -08004#include <arch/io.h>
Bill XIE516c0a52020-02-24 23:08:35 +08005#include <bootmode.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02006#include <device/mmio.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -08007#include <cbmem.h>
Arthur Heymans08c646c2020-11-19 13:56:41 +01008#include <cpu/x86/smm.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -08009#include <console/console.h>
10#include <halt.h>
11#include <intelblocks/pmclib.h>
12#include <intelblocks/gpio.h>
Subrata Banik7bc4dc52018-05-17 18:40:32 +053013#include <intelblocks/tco.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020014#include <option.h>
Bill XIE516c0a52020-02-24 23:08:35 +080015#include <security/vboot/vboot_common.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -080016#include <soc/pm.h>
Nico Huber6bbabef2019-08-05 21:24:00 +020017#include <stdint.h>
Shaunak Sahaf0738722017-10-02 15:01:33 -070018#include <string.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -080019#include <timer.h>
Shaunak Saha9dffbdd2017-03-08 19:27:17 -080020
Arthur Heymansea6dd742019-05-25 10:32:31 +020021static struct chipset_power_state power_state;
Shaunak Sahaf0738722017-10-02 15:01:33 -070022
V Sowmya186250f2020-09-02 16:40:24 +053023/* List of Minimum Assertion durations in microseconds */
24enum min_assert_dur {
25 MinAssertDur0s = 0,
26 MinAssertDur60us = 60,
27 MinAssertDur1ms = 1000,
28 MinAssertDur50ms = 50000,
29 MinAssertDur98ms = 98000,
30 MinAssertDur500ms = 500000,
31 MinAssertDur1s = 1000000,
32 MinAssertDur2s = 2000000,
33 MinAssertDur3s = 3000000,
34 MinAssertDur4s = 4000000,
35};
36
37/* Signal Assertion duration values */
38struct cfg_assert_dur {
39 /* Minimum assertion duration of SLP_A signal */
40 enum min_assert_dur slp_a;
41
42 /* Minimum assertion duration of SLP_4 signal */
43 enum min_assert_dur slp_s4;
44
45 /* Minimum assertion duration of SLP_3 signal */
46 enum min_assert_dur slp_s3;
47
48 /* PCH PM Power Cycle duration */
49 enum min_assert_dur pm_pwr_cyc_dur;
50};
51
52/* Default value of PchPmPwrCycDur */
53#define PCH_PM_PWR_CYC_DUR 0
54
Shaunak Sahaf0738722017-10-02 15:01:33 -070055struct chipset_power_state *pmc_get_power_state(void)
56{
57 struct chipset_power_state *ptr = NULL;
58
59 if (cbmem_possibly_online())
Kyösti Mälkki27872372021-01-21 16:05:26 +020060 ptr = acpi_get_pm_state();
Shaunak Sahaf0738722017-10-02 15:01:33 -070061
62 /* cbmem is online but ptr is not populated yet */
63 if (ptr == NULL && !(ENV_RAMSTAGE || ENV_POSTCAR))
Arthur Heymansea6dd742019-05-25 10:32:31 +020064 return &power_state;
Shaunak Sahaf0738722017-10-02 15:01:33 -070065
66 return ptr;
67}
68
69static void migrate_power_state(int is_recovery)
70{
71 struct chipset_power_state *ps_cbmem;
Shaunak Sahaf0738722017-10-02 15:01:33 -070072
Shaunak Sahaf0738722017-10-02 15:01:33 -070073 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
74
75 if (ps_cbmem == NULL) {
76 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
77 return;
78 }
Arthur Heymansea6dd742019-05-25 10:32:31 +020079 memcpy(ps_cbmem, &power_state, sizeof(*ps_cbmem));
Shaunak Sahaf0738722017-10-02 15:01:33 -070080}
81ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
82
Shaunak Saha9dffbdd2017-03-08 19:27:17 -080083static void print_num_status_bits(int num_bits, uint32_t status,
84 const char *const bit_names[])
85{
86 int i;
87
88 if (!status)
89 return;
90
91 for (i = num_bits - 1; i >= 0; i--) {
92 if (status & (1 << i)) {
93 if (bit_names[i])
94 printk(BIOS_DEBUG, "%s ", bit_names[i]);
95 else
96 printk(BIOS_DEBUG, "BIT%d ", i);
97 }
98 }
99}
100
Aaron Durbin64031672018-04-21 14:45:32 -0600101__weak uint32_t soc_get_smi_status(uint32_t generic_sts)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800102{
103 return generic_sts;
104}
105
Bora Guvendik3cb0e272018-09-28 16:19:00 -0700106int acpi_get_sleep_type(void)
107{
108 struct chipset_power_state *ps;
John Zhao0ccfc0c2018-10-16 10:48:00 -0700109 int prev_sleep_state = ACPI_S0;
Bora Guvendik3cb0e272018-09-28 16:19:00 -0700110
111 ps = pmc_get_power_state();
John Zhao0ccfc0c2018-10-16 10:48:00 -0700112 if (ps)
113 prev_sleep_state = ps->prev_sleep_state;
114
115 return prev_sleep_state;
Bora Guvendik3cb0e272018-09-28 16:19:00 -0700116}
117
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800118static uint32_t pmc_reset_smi_status(void)
119{
120 uint32_t smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
121 outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
122
123 return soc_get_smi_status(smi_sts);
124}
125
126static uint32_t print_smi_status(uint32_t smi_sts)
127{
128 size_t array_size;
129 const char *const *smi_arr;
130
131 if (!smi_sts)
132 return 0;
133
134 printk(BIOS_DEBUG, "SMI_STS: ");
135
136 smi_arr = soc_smi_sts_array(&array_size);
137
138 print_num_status_bits(array_size, smi_sts, smi_arr);
139 printk(BIOS_DEBUG, "\n");
140
141 return smi_sts;
142}
143
Shaunak Saha25cc76f2017-09-28 15:13:05 -0700144/*
145 * Update supplied events in PM1_EN register. This does not disable any already
146 * set events.
147 */
148void pmc_update_pm1_enable(u16 events)
149{
150 u16 pm1_en = pmc_read_pm1_enable();
151 pm1_en |= events;
152 pmc_enable_pm1(pm1_en);
153}
154
155/* Read events set in PM1_EN register. */
156uint16_t pmc_read_pm1_enable(void)
157{
158 return inw(ACPI_BASE_ADDRESS + PM1_EN);
159}
160
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800161uint32_t pmc_clear_smi_status(void)
162{
163 uint32_t sts = pmc_reset_smi_status();
164
165 return print_smi_status(sts);
166}
167
168uint32_t pmc_get_smi_en(void)
169{
170 return inl(ACPI_BASE_ADDRESS + SMI_EN);
171}
172
173void pmc_enable_smi(uint32_t mask)
174{
175 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
176 smi_en |= mask;
177 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
178}
179
180void pmc_disable_smi(uint32_t mask)
181{
182 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
183 smi_en &= ~mask;
184 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
185}
186
187/* PM1 */
188void pmc_enable_pm1(uint16_t events)
189{
190 outw(events, ACPI_BASE_ADDRESS + PM1_EN);
191}
192
Furquan Shaikhab620182017-10-16 22:19:13 -0700193uint32_t pmc_read_pm1_control(void)
194{
195 return inl(ACPI_BASE_ADDRESS + PM1_CNT);
196}
197
198void pmc_write_pm1_control(uint32_t pm1_cnt)
199{
200 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
201}
202
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800203void pmc_enable_pm1_control(uint32_t mask)
204{
Furquan Shaikhab620182017-10-16 22:19:13 -0700205 uint32_t pm1_cnt = pmc_read_pm1_control();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800206 pm1_cnt |= mask;
Furquan Shaikhab620182017-10-16 22:19:13 -0700207 pmc_write_pm1_control(pm1_cnt);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800208}
209
210void pmc_disable_pm1_control(uint32_t mask)
211{
Furquan Shaikhab620182017-10-16 22:19:13 -0700212 uint32_t pm1_cnt = pmc_read_pm1_control();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800213 pm1_cnt &= ~mask;
Furquan Shaikhab620182017-10-16 22:19:13 -0700214 pmc_write_pm1_control(pm1_cnt);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800215}
216
217static uint16_t reset_pm1_status(void)
218{
219 uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
220 outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
221 return pm1_sts;
222}
223
224static uint16_t print_pm1_status(uint16_t pm1_sts)
225{
226 static const char *const pm1_sts_bits[] = {
227 [0] = "TMROF",
228 [5] = "GBL",
229 [8] = "PWRBTN",
230 [10] = "RTC",
231 [11] = "PRBTNOR",
232 [13] = "USB",
233 [14] = "PCIEXPWAK",
234 [15] = "WAK",
235 };
236
237 if (!pm1_sts)
238 return 0;
239
240 printk(BIOS_SPEW, "PM1_STS: ");
241 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
242 printk(BIOS_SPEW, "\n");
243
244 return pm1_sts;
245}
246
247uint16_t pmc_clear_pm1_status(void)
248{
249 return print_pm1_status(reset_pm1_status());
250}
251
252/* TCO */
253
254static uint32_t print_tco_status(uint32_t tco_sts)
255{
256 size_t array_size;
257 const char *const *tco_arr;
258
259 if (!tco_sts)
260 return 0;
261
262 printk(BIOS_DEBUG, "TCO_STS: ");
263
264 tco_arr = soc_tco_sts_array(&array_size);
265
266 print_num_status_bits(array_size, tco_sts, tco_arr);
267 printk(BIOS_DEBUG, "\n");
268
269 return tco_sts;
270}
271
272uint32_t pmc_clear_tco_status(void)
273{
Subrata Banik7bc4dc52018-05-17 18:40:32 +0530274 return print_tco_status(tco_reset_status());
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800275}
276
277/* GPE */
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700278static void pmc_enable_gpe(int gpe, uint32_t mask)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800279{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700280 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
281 gpe0_en |= mask;
282 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800283}
284
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700285static void pmc_disable_gpe(int gpe, uint32_t mask)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800286{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700287 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
288 gpe0_en &= ~mask;
289 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
290}
291
292void pmc_enable_std_gpe(uint32_t mask)
293{
294 pmc_enable_gpe(GPE_STD, mask);
295}
296
297void pmc_disable_std_gpe(uint32_t mask)
298{
299 pmc_disable_gpe(GPE_STD, mask);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800300}
301
302void pmc_disable_all_gpe(void)
303{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700304 int i;
305 for (i = 0; i < GPE0_REG_MAX; i++)
306 pmc_disable_gpe(i, ~0);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800307}
308
309/* Clear the gpio gpe0 status bits in ACPI registers */
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700310static void pmc_clear_gpi_gpe_status(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800311{
312 int i;
313
314 for (i = 0; i < GPE0_REG_MAX; i++) {
315 /* This is reserved GPE block and specific to chipset */
316 if (i == GPE_STD)
317 continue;
318 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
319 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(i));
320 }
321}
322
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700323static uint32_t reset_std_gpe_status(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800324{
325 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
326 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
327 return gpe_sts;
328}
329
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700330static uint32_t print_std_gpe_sts(uint32_t gpe_sts)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800331{
332 size_t array_size;
333 const char *const *sts_arr;
334
335 if (!gpe_sts)
336 return gpe_sts;
337
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700338 printk(BIOS_DEBUG, "GPE0 STD STS: ");
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800339
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700340 sts_arr = soc_std_gpe_sts_array(&array_size);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800341 print_num_status_bits(array_size, gpe_sts, sts_arr);
342 printk(BIOS_DEBUG, "\n");
343
344 return gpe_sts;
345}
346
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700347static void pmc_clear_std_gpe_status(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800348{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700349 print_std_gpe_sts(reset_std_gpe_status());
350}
351
352void pmc_clear_all_gpe_status(void)
353{
354 pmc_clear_std_gpe_status();
355 pmc_clear_gpi_gpe_status();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800356}
357
Aaron Durbin64031672018-04-21 14:45:32 -0600358__weak
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800359void soc_clear_pm_registers(uintptr_t pmc_bar)
360{
361}
362
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700363void pmc_clear_prsts(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800364{
365 uint32_t prsts;
366 uintptr_t pmc_bar;
367
368 /* Read PMC base address from soc */
369 pmc_bar = soc_read_pmc_base();
370
371 prsts = read32((void *)(pmc_bar + PRSTS));
372 write32((void *)(pmc_bar + PRSTS), prsts);
373
374 soc_clear_pm_registers(pmc_bar);
375}
376
Aaron Durbin64031672018-04-21 14:45:32 -0600377__weak
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800378int soc_prev_sleep_state(const struct chipset_power_state *ps,
379 int prev_sleep_state)
380{
381 return prev_sleep_state;
382}
383
384/*
385 * Returns prev_sleep_state and also prints all power management registers.
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100386 * Calls soc_prev_sleep_state which may be implemented by SOC.
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800387 */
388static int pmc_prev_sleep_state(const struct chipset_power_state *ps)
389{
390 /* Default to S0. */
391 int prev_sleep_state = ACPI_S0;
392
393 if (ps->pm1_sts & WAK_STS) {
394 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
395 case ACPI_S3:
Julius Wernercd49cce2019-03-05 16:53:33 -0800396 if (CONFIG(HAVE_ACPI_RESUME))
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800397 prev_sleep_state = ACPI_S3;
398 break;
399 case ACPI_S5:
400 prev_sleep_state = ACPI_S5;
401 break;
402 }
403
404 /* Clear SLP_TYP. */
Furquan Shaikhab620182017-10-16 22:19:13 -0700405 pmc_write_pm1_control(ps->pm1_cnt & ~(SLP_TYP));
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800406 }
407 return soc_prev_sleep_state(ps, prev_sleep_state);
408}
409
Furquan Shaikhe48fb542017-10-17 16:00:10 -0700410void pmc_fill_pm_reg_info(struct chipset_power_state *ps)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800411{
412 int i;
413
Furquan Shaikhe48fb542017-10-17 16:00:10 -0700414 memset(ps, 0, sizeof(*ps));
415
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800416 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
417 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
Furquan Shaikhab620182017-10-16 22:19:13 -0700418 ps->pm1_cnt = pmc_read_pm1_control();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800419
420 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
421 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
422
423 for (i = 0; i < GPE0_REG_MAX; i++) {
424 ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
425 ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
426 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
427 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
428 }
429
430 soc_fill_power_state(ps);
Furquan Shaikhe48fb542017-10-17 16:00:10 -0700431}
432
433/* Reads and prints ACPI specific PM registers */
434int pmc_fill_power_state(struct chipset_power_state *ps)
435{
436 pmc_fill_pm_reg_info(ps);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800437
438 ps->prev_sleep_state = pmc_prev_sleep_state(ps);
439 printk(BIOS_DEBUG, "prev_sleep_state %d\n", ps->prev_sleep_state);
440
441 return ps->prev_sleep_state;
442}
443
Julius Wernercd49cce2019-03-05 16:53:33 -0800444#if CONFIG(PMC_GLOBAL_RESET_ENABLE_LOCK)
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100445void pmc_global_reset_disable_and_lock(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800446{
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100447 uint32_t *etr = soc_pmc_etr_addr();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800448 uint32_t reg;
449
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100450 reg = read32(etr);
451 reg = (reg & ~CF9_GLB_RST) | CF9_LOCK;
452 write32(etr, reg);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800453}
454
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800455void pmc_global_reset_enable(bool enable)
456{
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100457 uint32_t *etr = soc_pmc_etr_addr();
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800458 uint32_t reg;
459
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100460 reg = read32(etr);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800461 reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
Michael Niewöhner1c6ea922019-11-02 12:20:53 +0100462 write32(etr, reg);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800463}
Julien Viard de Galbert2912e8e2018-08-14 16:15:26 +0200464#endif // CONFIG_PMC_GLOBAL_RESET_ENABLE_LOCK
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800465
Bill XIE516c0a52020-02-24 23:08:35 +0800466int platform_is_resuming(void)
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800467{
468 if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
469 return 0;
470
Furquan Shaikhab620182017-10-16 22:19:13 -0700471 return acpi_sleep_from_pm1(pmc_read_pm1_control()) == ACPI_S3;
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800472}
473
Furquan Shaikh76cedd22020-05-02 10:24:23 -0700474/* Read and clear GPE status (defined in acpi/acpi.h) */
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800475int acpi_get_gpe(int gpe)
476{
477 int bank;
478 uint32_t mask, sts;
479 struct stopwatch sw;
480 int rc = 0;
481
482 if (gpe < 0 || gpe > GPE_MAX)
483 return -1;
484
485 bank = gpe / 32;
486 mask = 1 << (gpe % 32);
487
488 /* Wait up to 1ms for GPE status to clear */
489 stopwatch_init_msecs_expire(&sw, 1);
490 do {
491 if (stopwatch_expired(&sw))
492 return rc;
493
494 sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(bank));
495 if (sts & mask) {
496 outl(mask, ACPI_BASE_ADDRESS + GPE0_STS(bank));
497 rc = 1;
498 }
499 } while (sts & mask);
500
501 return rc;
502}
503
504/*
505 * The PM1 control is set to S5 when vboot requests a reboot because the power
506 * state code above may not have collected its data yet. Therefore, set it to
507 * S5 when vboot requests a reboot. That's necessary if vboot fails in the
508 * resume path and requests a reboot. This prevents a reboot loop where the
509 * error is continually hit on the failing vboot resume path.
510 */
511void vboot_platform_prepare_reboot(void)
512{
Furquan Shaikhab620182017-10-16 22:19:13 -0700513 uint32_t pm1_cnt;
514 pm1_cnt = (pmc_read_pm1_control() & ~(SLP_TYP)) |
515 (SLP_TYP_S5 << SLP_TYP_SHIFT);
516 pmc_write_pm1_control(pm1_cnt);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800517}
518
519void poweroff(void)
520{
521 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
522
523 /*
524 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
525 * to transition to S5 state. If halt is called in SMM, then it prevents
526 * the SMI handler from being triggered and system never enters S5.
527 */
528 if (!ENV_SMM)
529 halt();
530}
531
532void pmc_gpe_init(void)
533{
534 uint32_t gpio_cfg = 0;
535 uint32_t gpio_cfg_reg;
536 uint8_t dw0, dw1, dw2;
537
538 /* Read PMC base address from soc. This is implemented in soc */
539 uintptr_t pmc_bar = soc_read_pmc_base();
540
541 /*
542 * Get the dwX values for pmc gpe settings.
543 */
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700544 soc_get_gpi_gpe_configs(&dw0, &dw1, &dw2);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800545
546 const uint32_t gpio_cfg_mask =
547 (GPE0_DWX_MASK << GPE0_DW_SHIFT(0)) |
548 (GPE0_DWX_MASK << GPE0_DW_SHIFT(1)) |
549 (GPE0_DWX_MASK << GPE0_DW_SHIFT(2));
550
551 /* Making sure that bad values don't bleed into the other fields */
552 dw0 &= GPE0_DWX_MASK;
553 dw1 &= GPE0_DWX_MASK;
554 dw2 &= GPE0_DWX_MASK;
555
556 /*
557 * Route the GPIOs to the GPE0 block. Determine that all values
558 * are different, and if they aren't use the reset values.
559 */
560 if (dw0 == dw1 || dw1 == dw2) {
561 printk(BIOS_INFO, "PMC: Using default GPE route.\n");
562 gpio_cfg = read32((void *)pmc_bar + GPIO_GPE_CFG);
563
564 dw0 = (gpio_cfg >> GPE0_DW_SHIFT(0)) & GPE0_DWX_MASK;
565 dw1 = (gpio_cfg >> GPE0_DW_SHIFT(1)) & GPE0_DWX_MASK;
566 dw2 = (gpio_cfg >> GPE0_DW_SHIFT(2)) & GPE0_DWX_MASK;
567 } else {
568 gpio_cfg |= (uint32_t) dw0 << GPE0_DW_SHIFT(0);
569 gpio_cfg |= (uint32_t) dw1 << GPE0_DW_SHIFT(1);
570 gpio_cfg |= (uint32_t) dw2 << GPE0_DW_SHIFT(2);
571 }
572
573 gpio_cfg_reg = read32((void *)pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask;
574 gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask;
575
576 write32((void *)pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg);
577
578 /* Set the routes in the GPIO communities as well. */
579 gpio_route_gpe(dw0, dw1, dw2);
Shaunak Saha9dffbdd2017-03-08 19:27:17 -0800580}
Nico Huberef19ce52019-08-05 19:19:59 +0200581
582void pmc_set_power_failure_state(const bool target_on)
583{
Nico Huberef19ce52019-08-05 19:19:59 +0200584 bool on;
585
Angel Pons88dcb312021-04-26 17:10:28 +0200586 const unsigned int state = get_uint_option("power_on_after_fail",
Angel Pons62719a32021-04-19 13:15:28 +0200587 CONFIG_MAINBOARD_POWER_FAILURE_STATE);
Nico Huber6bbabef2019-08-05 21:24:00 +0200588
Nico Huberef19ce52019-08-05 19:19:59 +0200589 switch (state) {
590 case MAINBOARD_POWER_STATE_OFF:
591 printk(BIOS_INFO, "Set power off after power failure.\n");
592 on = false;
593 break;
594 case MAINBOARD_POWER_STATE_ON:
595 printk(BIOS_INFO, "Set power on after power failure.\n");
596 on = true;
597 break;
598 case MAINBOARD_POWER_STATE_PREVIOUS:
599 printk(BIOS_INFO, "Keep power state after power failure.\n");
600 on = target_on;
601 break;
602 default:
603 printk(BIOS_WARNING, "WARNING: Unknown power-failure state: %d\n", state);
604 on = false;
605 break;
606 }
607
608 pmc_soc_set_afterg3_en(on);
609}
V Sowmya186250f2020-09-02 16:40:24 +0530610
611/* This function returns the highest assertion duration of the SLP_Sx assertion widths */
612static enum min_assert_dur get_high_assert_width(const struct cfg_assert_dur *cfg_assert_dur)
613{
614 enum min_assert_dur max_assert_dur = cfg_assert_dur->slp_s4;
615
616 if (max_assert_dur < cfg_assert_dur->slp_s3)
617 max_assert_dur = cfg_assert_dur->slp_s3;
618
619 if (max_assert_dur < cfg_assert_dur->slp_a)
620 max_assert_dur = cfg_assert_dur->slp_a;
621
622 return max_assert_dur;
623}
624
625/* This function converts assertion durations from register-encoded to microseconds */
626static void get_min_assert_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
627 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur,
628 struct cfg_assert_dur *cfg_assert_dur)
629{
630 /*
631 * Ensure slp_x_dur_list[] elements in the devicetree config are in sync with
632 * FSP encoded values.
633 */
634
635 /* slp_s4_assert_dur_list : 1s, 1s(default), 2s, 3s, 4s */
636 const enum min_assert_dur slp_s4_assert_dur_list[] = {
637 MinAssertDur1s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
638 };
639
640 /* slp_s3_assert_dur_list: 50ms, 60us, 1ms, 50ms (Default), 2s */
641 const enum min_assert_dur slp_s3_assert_dur_list[] = {
642 MinAssertDur50ms, MinAssertDur60us, MinAssertDur1ms, MinAssertDur50ms,
643 MinAssertDur2s
644 };
645
646 /* slp_a_assert_dur_list: 2s, 0s, 4s, 98ms, 2s(Default) */
647 const enum min_assert_dur slp_a_assert_dur_list[] = {
648 MinAssertDur2s, MinAssertDur0s, MinAssertDur4s, MinAssertDur98ms, MinAssertDur2s
649 };
650
651 /* pm_pwr_cyc_dur_list: 4s(Default), 1s, 2s, 3s, 4s */
652 const enum min_assert_dur pm_pwr_cyc_dur_list[] = {
653 MinAssertDur4s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
654 };
655
656 /* Get signal assertion width */
657 if (slp_s4_min_assert < ARRAY_SIZE(slp_s4_assert_dur_list))
658 cfg_assert_dur->slp_s4 = slp_s4_assert_dur_list[slp_s4_min_assert];
659
660 if (slp_s3_min_assert < ARRAY_SIZE(slp_s3_assert_dur_list))
661 cfg_assert_dur->slp_s3 = slp_s3_assert_dur_list[slp_s3_min_assert];
662
663 if (slp_a_min_assert < ARRAY_SIZE(slp_a_assert_dur_list))
664 cfg_assert_dur->slp_a = slp_a_assert_dur_list[slp_a_min_assert];
665
666 if (pm_pwr_cyc_dur < ARRAY_SIZE(pm_pwr_cyc_dur_list))
667 cfg_assert_dur->pm_pwr_cyc_dur = pm_pwr_cyc_dur_list[pm_pwr_cyc_dur];
668}
669
670/*
671 * This function ensures that the duration programmed in the PchPmPwrCycDur will never be
672 * smaller than the SLP_Sx assertion widths.
673 * If the pm_pwr_cyc_dur is less than any of the SLP_Sx assertion widths then it returns the
674 * default value PCH_PM_PWR_CYC_DUR.
675 */
676uint8_t get_pm_pwr_cyc_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
677 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur)
678{
679 /* Set default values for the minimum assertion duration */
680 struct cfg_assert_dur cfg_assert_dur = {
681 .slp_a = MinAssertDur2s,
682 .slp_s4 = MinAssertDur1s,
683 .slp_s3 = MinAssertDur50ms,
684 .pm_pwr_cyc_dur = MinAssertDur4s
685 };
686
687 enum min_assert_dur high_assert_width;
688
689 /* Convert assertion durations from register-encoded to microseconds */
690 get_min_assert_dur(slp_s4_min_assert, slp_s3_min_assert, slp_a_min_assert,
691 pm_pwr_cyc_dur, &cfg_assert_dur);
692
693 /* Get the highest assertion duration among PCH EDS specified signals for pwr_cyc_dur */
694 high_assert_width = get_high_assert_width(&cfg_assert_dur);
695
696 if (cfg_assert_dur.pm_pwr_cyc_dur >= high_assert_width)
697 return pm_pwr_cyc_dur;
698
699 printk(BIOS_DEBUG,
700 "Set PmPwrCycDur to 4s as configured PmPwrCycDur (%d) violates PCH EDS "
701 "spec\n", pm_pwr_cyc_dur);
702
703 return PCH_PM_PWR_CYC_DUR;
704}
Subrata Banik3e959d82020-09-28 17:50:00 +0530705
706#if CONFIG(PMC_LOW_POWER_MODE_PROGRAM)
707void pmc_disable_acpi_timer(void)
708{
709 uint8_t *pmcbase = pmc_mmio_regs();
710
711 setbits8(pmcbase + PCH_PWRM_ACPI_TMR_CTL, ACPI_TIM_DIS);
712}
Subrata Banik3e959d82020-09-28 17:50:00 +0530713#endif /* PMC_LOW_POWER_MODE_PROGRAM */
Arthur Heymans08c646c2020-11-19 13:56:41 +0100714
715void pmc_set_acpi_mode(void)
716{
Arthur Heymans8461cec2020-11-19 14:22:24 +0100717 if (!CONFIG(NO_SMM) && !acpi_is_wakeup_s3()) {
Arthur Heymans08c646c2020-11-19 13:56:41 +0100718 apm_control(APM_CNT_ACPI_DISABLE);
719 }
720}