soc/intel/pantherlake: Remove soc_info.[hc] interface
[coreboot2.git] / src / soc / intel / common / block / pmc / pmclib.c
blob0b3863a0f063cd5602ba447d4d43158863b567c3
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpi_pm.h>
4 #include <arch/io.h>
5 #include <assert.h>
6 #include <bootmode.h>
7 #include <device/mmio.h>
8 #include <device/pci.h>
9 #include <cbmem.h>
10 #include <cpu/x86/smm.h>
11 #include <console/console.h>
12 #include <halt.h>
13 #include <intelblocks/pmc_ipc.h>
14 #include <intelblocks/pmclib.h>
15 #include <intelblocks/gpio.h>
16 #include <intelblocks/tco.h>
17 #include <option.h>
18 #include <security/vboot/vboot_common.h>
19 #include <soc/pci_devs.h>
20 #include <soc/pm.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <timer.h>
25 #define PMC_IPC_BIOS_RST_COMPLETE 0xd0
26 #define PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE 0
27 #define PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM BIT(0)
29 /* IPC command for accessing SoC registers */
30 #define PMC_IPC_CMD_SOC_REG_ACC 0xAA
31 #define PMC_IPC_CMD_SUBCMD_SOC_REG_RD 0x00
32 #define PMC_IPC_CMD_REGID_SOC_QDF 0x03
34 static struct chipset_power_state power_state;
36 /* List of Minimum Assertion durations in microseconds */
37 enum min_assert_dur {
38 MinAssertDur0s = 0,
39 MinAssertDur60us = 60,
40 MinAssertDur1ms = 1000,
41 MinAssertDur50ms = 50000,
42 MinAssertDur98ms = 98000,
43 MinAssertDur500ms = 500000,
44 MinAssertDur1s = 1000000,
45 MinAssertDur2s = 2000000,
46 MinAssertDur3s = 3000000,
47 MinAssertDur4s = 4000000,
50 /* Signal Assertion duration values */
51 struct cfg_assert_dur {
52 /* Minimum assertion duration of SLP_A signal */
53 enum min_assert_dur slp_a;
55 /* Minimum assertion duration of SLP_4 signal */
56 enum min_assert_dur slp_s4;
58 /* Minimum assertion duration of SLP_3 signal */
59 enum min_assert_dur slp_s3;
61 /* PCH PM Power Cycle duration */
62 enum min_assert_dur pm_pwr_cyc_dur;
65 /* Default value of PchPmPwrCycDur */
66 #define PCH_PM_PWR_CYC_DUR 0
68 struct chipset_power_state *pmc_get_power_state(void)
70 struct chipset_power_state *ptr = NULL;
72 if (ENV_HAS_CBMEM)
73 ptr = acpi_get_pm_state();
75 /* cbmem is online but ptr is not populated yet */
76 if (ptr == NULL && !(ENV_RAMSTAGE || ENV_POSTCAR))
77 return &power_state;
79 return ptr;
82 static void migrate_power_state(int is_recovery)
84 struct chipset_power_state *ps_cbmem;
86 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
88 if (ps_cbmem == NULL) {
89 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
90 return;
92 memcpy(ps_cbmem, &power_state, sizeof(*ps_cbmem));
94 CBMEM_CREATION_HOOK(migrate_power_state);
96 static void print_num_status_bits(int num_bits, uint32_t status,
97 const char *const *bit_names)
99 int i;
101 if (!status)
102 return;
104 for (i = num_bits - 1; i >= 0; i--) {
105 if (status & (1 << i)) {
106 if (bit_names[i])
107 printk(BIOS_DEBUG, "%s ", bit_names[i]);
108 else
109 printk(BIOS_DEBUG, "BIT%d ", i);
114 __weak uint32_t soc_get_smi_status(uint32_t generic_sts)
116 return generic_sts;
119 int acpi_get_sleep_type(void)
121 struct chipset_power_state *ps;
122 int prev_sleep_state = ACPI_S0;
124 ps = pmc_get_power_state();
125 if (ps)
126 prev_sleep_state = ps->prev_sleep_state;
128 return prev_sleep_state;
131 static uint32_t pmc_reset_smi_status(void)
133 uint32_t smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
134 outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
136 return soc_get_smi_status(smi_sts);
139 static uint32_t print_smi_status(uint32_t smi_sts)
141 size_t array_size;
142 const char *const *smi_arr;
144 if (!smi_sts)
145 return 0;
147 printk(BIOS_DEBUG, "SMI_STS: ");
149 smi_arr = soc_smi_sts_array(&array_size);
151 print_num_status_bits(array_size, smi_sts, smi_arr);
152 printk(BIOS_DEBUG, "\n");
154 return smi_sts;
158 * Update supplied events in PM1_EN register. This does not disable any already
159 * set events.
161 void pmc_update_pm1_enable(u16 events)
163 u16 pm1_en = pmc_read_pm1_enable();
164 pm1_en |= events;
165 pmc_enable_pm1(pm1_en);
168 /* Read events set in PM1_EN register. */
169 uint16_t pmc_read_pm1_enable(void)
171 return inw(ACPI_BASE_ADDRESS + PM1_EN);
174 uint32_t pmc_clear_smi_status(void)
176 uint32_t sts = pmc_reset_smi_status();
178 return print_smi_status(sts);
181 uint32_t pmc_get_smi_en(void)
183 return inl(ACPI_BASE_ADDRESS + SMI_EN);
186 void pmc_enable_smi(uint32_t mask)
188 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
189 smi_en |= mask;
190 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
193 void pmc_disable_smi(uint32_t mask)
195 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
196 smi_en &= ~mask;
197 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
200 /* PM1 */
201 void pmc_enable_pm1(uint16_t events)
203 outw(events, ACPI_BASE_ADDRESS + PM1_EN);
206 uint32_t pmc_read_pm1_control(void)
208 return inl(ACPI_BASE_ADDRESS + PM1_CNT);
211 void pmc_write_pm1_control(uint32_t pm1_cnt)
213 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
216 void pmc_enable_pm1_control(uint32_t mask)
218 uint32_t pm1_cnt = pmc_read_pm1_control();
219 pm1_cnt |= mask;
220 pmc_write_pm1_control(pm1_cnt);
223 void pmc_disable_pm1_control(uint32_t mask)
225 uint32_t pm1_cnt = pmc_read_pm1_control();
226 pm1_cnt &= ~mask;
227 pmc_write_pm1_control(pm1_cnt);
230 static uint16_t reset_pm1_status(void)
232 uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
233 outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
234 return pm1_sts;
237 static uint16_t print_pm1_status(uint16_t pm1_sts)
239 static const char *const pm1_sts_bits[] = {
240 [0] = "TMROF",
241 [5] = "GBL",
242 [8] = "PWRBTN",
243 [10] = "RTC",
244 [11] = "PRBTNOR",
245 [13] = "USB",
246 [14] = "PCIEXPWAK",
247 [15] = "WAK",
250 if (!pm1_sts)
251 return 0;
253 printk(BIOS_DEBUG, "PM1_STS: ");
254 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
255 printk(BIOS_DEBUG, "\n");
257 return pm1_sts;
260 uint16_t pmc_clear_pm1_status(void)
262 return print_pm1_status(reset_pm1_status());
265 /* TCO */
267 static uint32_t print_tco_status(uint32_t tco_sts)
269 size_t array_size;
270 const char *const *tco_arr;
272 if (!tco_sts)
273 return 0;
275 printk(BIOS_DEBUG, "TCO_STS: ");
277 tco_arr = soc_tco_sts_array(&array_size);
279 print_num_status_bits(array_size, tco_sts, tco_arr);
280 printk(BIOS_DEBUG, "\n");
282 return tco_sts;
285 uint32_t pmc_clear_tco_status(void)
287 return print_tco_status(tco_reset_status());
290 /* GPE */
291 static void pmc_enable_gpe(int gpe, uint32_t mask)
293 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
294 gpe0_en |= mask;
295 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
298 static void pmc_disable_gpe(int gpe, uint32_t mask)
300 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
301 gpe0_en &= ~mask;
302 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
305 void pmc_enable_std_gpe(uint32_t mask)
307 pmc_enable_gpe(GPE_STD, mask);
310 void pmc_disable_std_gpe(uint32_t mask)
312 pmc_disable_gpe(GPE_STD, mask);
315 void pmc_disable_all_gpe(void)
317 int i;
318 for (i = 0; i < GPE0_REG_MAX; i++)
319 pmc_disable_gpe(i, ~0);
322 /* Clear the gpio gpe0 status bits in ACPI registers */
323 static void pmc_clear_gpi_gpe_status(void)
325 int i;
327 for (i = 0; i < GPE0_REG_MAX; i++) {
328 /* This is reserved GPE block and specific to chipset */
329 if (i == GPE_STD)
330 continue;
331 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
332 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(i));
336 static uint32_t reset_std_gpe_status(void)
338 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
339 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
340 return gpe_sts;
343 static uint32_t print_std_gpe_sts(uint32_t gpe_sts)
345 size_t array_size;
346 const char *const *sts_arr;
348 if (!gpe_sts)
349 return gpe_sts;
351 printk(BIOS_DEBUG, "GPE0 STD STS: ");
353 sts_arr = soc_std_gpe_sts_array(&array_size);
354 print_num_status_bits(array_size, gpe_sts, sts_arr);
355 printk(BIOS_DEBUG, "\n");
357 return gpe_sts;
360 static void pmc_clear_std_gpe_status(void)
362 print_std_gpe_sts(reset_std_gpe_status());
365 void pmc_clear_all_gpe_status(void)
367 pmc_clear_std_gpe_status();
368 pmc_clear_gpi_gpe_status();
371 __weak
372 void soc_clear_pm_registers(uintptr_t pmc_bar)
376 void pmc_or_mmio32(uint32_t offset, uint32_t ormask)
378 uint32_t reg;
379 uintptr_t pmc_bar;
381 pmc_bar = soc_read_pmc_base();
382 reg = read32p(pmc_bar + offset);
383 reg |= ormask;
384 write32p(pmc_bar + offset, reg);
387 void pmc_clear_prsts(void)
389 uint32_t prsts;
390 uintptr_t pmc_bar;
392 /* Read PMC base address from soc */
393 pmc_bar = soc_read_pmc_base();
395 prsts = read32p(pmc_bar + PRSTS);
396 write32p(pmc_bar + PRSTS, prsts);
398 soc_clear_pm_registers(pmc_bar);
401 __weak
402 int soc_prev_sleep_state(const struct chipset_power_state *ps,
403 int prev_sleep_state)
405 return prev_sleep_state;
409 * Returns prev_sleep_state and also prints all power management registers.
410 * Calls soc_prev_sleep_state which may be implemented by SOC.
412 static int pmc_prev_sleep_state(const struct chipset_power_state *ps)
414 /* Default to S0. */
415 int prev_sleep_state = ACPI_S0;
417 if (ps->pm1_sts & WAK_STS) {
418 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
419 case ACPI_S3:
420 if (CONFIG(HAVE_ACPI_RESUME))
421 prev_sleep_state = ACPI_S3;
422 break;
423 case ACPI_S4:
424 prev_sleep_state = ACPI_S4;
425 break;
426 case ACPI_S5:
427 prev_sleep_state = ACPI_S5;
428 break;
431 /* Clear SLP_TYP. */
432 pmc_write_pm1_control(ps->pm1_cnt & ~(SLP_TYP));
435 prev_sleep_state = soc_prev_sleep_state(ps, prev_sleep_state);
437 /* Clear PMC PMCON_x register power failure status bits. */
438 pmc_clear_pmcon_pwr_failure_sts();
440 return prev_sleep_state;
443 void pmc_fill_pm_reg_info(struct chipset_power_state *ps)
445 int i;
447 memset(ps, 0, sizeof(*ps));
449 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
450 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
451 ps->pm1_cnt = pmc_read_pm1_control();
453 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
454 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
456 for (i = 0; i < GPE0_REG_MAX; i++) {
457 ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
458 ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
459 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
460 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
463 soc_fill_power_state(ps);
466 /* Reads and prints ACPI specific PM registers */
467 int pmc_fill_power_state(struct chipset_power_state *ps)
469 /* Define the sleep state string */
470 static const char * const acpi_sleep_states[] = {
471 [ACPI_S0] = "S0",
472 [ACPI_S1] = "S1",
473 [ACPI_S3] = "S3",
474 [ACPI_S4] = "S4",
475 [ACPI_S5] = "S5",
478 pmc_fill_pm_reg_info(ps);
480 ps->prev_sleep_state = pmc_prev_sleep_state(ps);
482 if (ps->prev_sleep_state < ARRAY_SIZE(acpi_sleep_states) &&
483 acpi_sleep_states[ps->prev_sleep_state] != NULL)
484 printk(BIOS_DEBUG, "prev_sleep_state %d (%s)\n", ps->prev_sleep_state,
485 acpi_sleep_states[ps->prev_sleep_state]);
486 else
487 printk(BIOS_DEBUG, "prev_sleep_state %d (unknown state)\n", ps->prev_sleep_state);
489 return ps->prev_sleep_state;
492 #if CONFIG(PMC_GLOBAL_RESET_ENABLE_LOCK)
493 void pmc_global_reset_disable_and_lock(void)
495 uint32_t *etr = soc_pmc_etr_addr();
496 uint32_t reg;
498 reg = read32(etr);
499 reg = (reg & ~CF9_GLB_RST) | CF9_LOCK;
500 write32(etr, reg);
503 void pmc_global_reset_enable(bool enable)
505 uint32_t *etr = soc_pmc_etr_addr();
506 uint32_t reg;
508 reg = read32(etr);
509 reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
510 write32(etr, reg);
512 #endif // CONFIG_PMC_GLOBAL_RESET_ENABLE_LOCK
514 int platform_is_resuming(void)
516 /* Read power state from PMC data structure */
517 if (ENV_RAMSTAGE)
518 return acpi_get_sleep_type() == ACPI_S3;
520 /* Read power state from PMC ABASE */
521 if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
522 return 0;
524 return acpi_sleep_from_pm1(pmc_read_pm1_control()) == ACPI_S3;
527 /* Read and clear GPE status (defined in acpi/acpi.h) */
528 int acpi_get_gpe(int gpe)
530 int bank;
531 uint32_t mask, sts;
532 struct stopwatch sw;
533 int rc = 0;
535 if (gpe < 0 || gpe > GPE_MAX)
536 return -1;
538 bank = gpe / 32;
539 mask = 1 << (gpe % 32);
541 /* Wait up to 1ms for GPE status to clear */
542 stopwatch_init_msecs_expire(&sw, 1);
543 do {
544 if (stopwatch_expired(&sw))
545 return rc;
547 sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(bank));
548 if (sts & mask) {
549 outl(mask, ACPI_BASE_ADDRESS + GPE0_STS(bank));
550 rc = 1;
552 } while (sts & mask);
554 return rc;
558 * The PM1 control is set to S5 when vboot requests a reboot because the power
559 * state code above may not have collected its data yet. Therefore, set it to
560 * S5 when vboot requests a reboot. That's necessary if vboot fails in the
561 * resume path and requests a reboot. This prevents a reboot loop where the
562 * error is continually hit on the failing vboot resume path.
564 void vboot_platform_prepare_reboot(void)
566 uint32_t pm1_cnt;
567 pm1_cnt = (pmc_read_pm1_control() & ~(SLP_TYP)) |
568 (SLP_TYP_S5 << SLP_TYP_SHIFT);
569 pmc_write_pm1_control(pm1_cnt);
572 void poweroff(void)
574 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
577 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
578 * to transition to S5 state. If halt is called in SMM, then it prevents
579 * the SMI handler from being triggered and system never enters S5.
581 if (!ENV_SMM)
582 halt();
585 void pmc_gpe_init(void)
587 uint32_t gpio_cfg = 0;
588 uint32_t gpio_cfg_reg;
589 uint8_t dw0 = 0, dw1 = 0, dw2 = 0;
591 /* Read PMC base address from soc. This is implemented in soc */
592 uintptr_t pmc_bar = soc_read_pmc_base();
595 * Get the dwX values for pmc gpe settings.
597 soc_get_gpi_gpe_configs(&dw0, &dw1, &dw2);
599 const uint32_t gpio_cfg_mask =
600 (GPE0_DWX_MASK << GPE0_DW_SHIFT(0)) |
601 (GPE0_DWX_MASK << GPE0_DW_SHIFT(1)) |
602 (GPE0_DWX_MASK << GPE0_DW_SHIFT(2));
604 /* Making sure that bad values don't bleed into the other fields */
605 dw0 &= GPE0_DWX_MASK;
606 dw1 &= GPE0_DWX_MASK;
607 dw2 &= GPE0_DWX_MASK;
610 * Route the GPIOs to the GPE0 block. Determine that all values
611 * are different, and if they aren't use the reset values.
613 if (dw0 == dw1 || dw1 == dw2) {
614 printk(BIOS_INFO, "PMC: Using default GPE route.\n");
615 gpio_cfg = read32p(pmc_bar + GPIO_GPE_CFG);
617 dw0 = (gpio_cfg >> GPE0_DW_SHIFT(0)) & GPE0_DWX_MASK;
618 dw1 = (gpio_cfg >> GPE0_DW_SHIFT(1)) & GPE0_DWX_MASK;
619 dw2 = (gpio_cfg >> GPE0_DW_SHIFT(2)) & GPE0_DWX_MASK;
620 } else {
621 gpio_cfg |= (uint32_t)dw0 << GPE0_DW_SHIFT(0);
622 gpio_cfg |= (uint32_t)dw1 << GPE0_DW_SHIFT(1);
623 gpio_cfg |= (uint32_t)dw2 << GPE0_DW_SHIFT(2);
626 gpio_cfg_reg = read32p(pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask;
627 gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask;
629 write32p(pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg);
631 /* Set the routes in the GPIO communities as well. */
632 gpio_route_gpe(dw0, dw1, dw2);
635 static void pmc_clear_pmcon_pwr_failure_sts_mmio(void)
637 uint8_t *addr = pmc_mmio_regs();
640 * Clear PMC GEN_PMCON_A register power failure status bits:
641 * SUS_PWR_FLR, PWR_FLR bits
642 * while retaining MS4V write-1-to-clear bit
644 * Note: clearing `GBL_RST_STS` bit earlier than FSP-M/MRC having an adverse effect
645 * on the PMC sleep type register which results in calculating wrong
646 * `prev_sleep_state` post a global reset, hence, just clearing the power failure
647 * status bits rather than clearing the complete PMC PMCON_A register.
649 clrbits32((addr + GEN_PMCON_A), (MS4V | GBL_RST_STS));
652 static void pmc_clear_pmcon_pwr_failure_sts_pci(void)
654 #if defined(__SIMPLE_DEVICE__)
655 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(PCH_DEVFN_PMC), PCI_FUNC(PCH_DEVFN_PMC));
656 #else
657 struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
658 if (!dev)
659 return;
660 #endif
662 pci_or_config32(dev, GEN_PMCON_B, (SUS_PWR_FLR | PWR_FLR));
666 * Clear PMC GEN_PMCON_X register power failure status bits:
667 * SUS_PWR_FLR, PWR_FLR bits (keep the other bits intact)
669 void pmc_clear_pmcon_pwr_failure_sts(void)
671 if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
672 pmc_clear_pmcon_pwr_failure_sts_mmio();
673 else
674 pmc_clear_pmcon_pwr_failure_sts_pci();
677 #if ENV_RAMSTAGE
678 static void pmc_clear_pmcon_sts_mmio(void)
680 uint8_t *addr = pmc_mmio_regs();
682 clrbits32((addr + GEN_PMCON_A), MS4V);
685 static void pmc_clear_pmcon_sts_pci(void)
687 struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
688 if (!dev)
689 return;
691 pci_and_config32(dev, GEN_PMCON_A, ~MS4V);
695 * Clear PMC GEN_PMCON_A register status bits:
696 * SUS_PWR_FLR, GBL_RST_STS, HOST_RST_STS, PWR_FLR bits
697 * while retaining MS4V write-1-to-clear bit
699 void pmc_clear_pmcon_sts(void)
702 * Accessing PMC GEN_PMCON_A register differs between different Intel chipsets.
703 * Typically, there are two possible ways to perform GEN_PMCON_A register programming
704 * (like `pmc_clear_pmcon_sts()`) as:
705 * 1. Using PCI configuration space when GEN_PMCON_A is a PCI configuration register.
706 * 2. Using MMIO access when GEN_PMCON_A is a memory mapped register.
708 if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
709 pmc_clear_pmcon_sts_mmio();
710 else
711 pmc_clear_pmcon_sts_pci();
713 #endif
715 void pmc_set_power_failure_state(const bool target_on)
717 const unsigned int state = get_uint_option("power_on_after_fail",
718 CONFIG_MAINBOARD_POWER_FAILURE_STATE);
721 * On the shutdown path (target_on == false), we only need to
722 * update the register for MAINBOARD_POWER_STATE_PREVIOUS. For
723 * all other cases, we don't write the register to avoid clob-
724 * bering the value set on the boot path. This is necessary,
725 * for instance, when we can't access the option backend in SMM.
728 switch (state) {
729 case MAINBOARD_POWER_STATE_OFF:
730 if (!target_on)
731 break;
732 printk(BIOS_INFO, "Set power off after power failure.\n");
733 pmc_soc_set_afterg3_en(false);
734 break;
735 case MAINBOARD_POWER_STATE_ON:
736 if (!target_on)
737 break;
738 printk(BIOS_INFO, "Set power on after power failure.\n");
739 pmc_soc_set_afterg3_en(true);
740 break;
741 case MAINBOARD_POWER_STATE_PREVIOUS:
742 printk(BIOS_INFO, "Keep power state after power failure.\n");
743 pmc_soc_set_afterg3_en(target_on);
744 break;
745 default:
746 printk(BIOS_WARNING, "Unknown power-failure state: %d\n", state);
747 break;
751 /* This function returns the highest assertion duration of the SLP_Sx assertion widths */
752 static enum min_assert_dur get_high_assert_width(const struct cfg_assert_dur *cfg_assert_dur)
754 enum min_assert_dur max_assert_dur = cfg_assert_dur->slp_s4;
756 if (max_assert_dur < cfg_assert_dur->slp_s3)
757 max_assert_dur = cfg_assert_dur->slp_s3;
759 if (max_assert_dur < cfg_assert_dur->slp_a)
760 max_assert_dur = cfg_assert_dur->slp_a;
762 return max_assert_dur;
765 /* This function converts assertion durations from register-encoded to microseconds */
766 static void get_min_assert_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
767 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur,
768 struct cfg_assert_dur *cfg_assert_dur)
771 * Ensure slp_x_dur_list[] elements in the devicetree config are in sync with
772 * FSP encoded values.
775 /* slp_s4_assert_dur_list : 1s, 1s(default), 2s, 3s, 4s */
776 const enum min_assert_dur slp_s4_assert_dur_list[] = {
777 MinAssertDur1s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
780 /* slp_s3_assert_dur_list: 50ms, 60us, 1ms, 50ms (Default), 2s */
781 const enum min_assert_dur slp_s3_assert_dur_list[] = {
782 MinAssertDur50ms, MinAssertDur60us, MinAssertDur1ms, MinAssertDur50ms,
783 MinAssertDur2s
786 /* slp_a_assert_dur_list: 2s, 0s, 4s, 98ms, 2s(Default) */
787 const enum min_assert_dur slp_a_assert_dur_list[] = {
788 MinAssertDur2s, MinAssertDur0s, MinAssertDur4s, MinAssertDur98ms, MinAssertDur2s
791 /* pm_pwr_cyc_dur_list: 4s(Default), 1s, 2s, 3s, 4s */
792 const enum min_assert_dur pm_pwr_cyc_dur_list[] = {
793 MinAssertDur4s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
796 /* Get signal assertion width */
797 if (slp_s4_min_assert < ARRAY_SIZE(slp_s4_assert_dur_list))
798 cfg_assert_dur->slp_s4 = slp_s4_assert_dur_list[slp_s4_min_assert];
800 if (slp_s3_min_assert < ARRAY_SIZE(slp_s3_assert_dur_list))
801 cfg_assert_dur->slp_s3 = slp_s3_assert_dur_list[slp_s3_min_assert];
803 if (slp_a_min_assert < ARRAY_SIZE(slp_a_assert_dur_list))
804 cfg_assert_dur->slp_a = slp_a_assert_dur_list[slp_a_min_assert];
806 if (pm_pwr_cyc_dur < ARRAY_SIZE(pm_pwr_cyc_dur_list))
807 cfg_assert_dur->pm_pwr_cyc_dur = pm_pwr_cyc_dur_list[pm_pwr_cyc_dur];
811 * This function ensures that the duration programmed in the PchPmPwrCycDur will never be
812 * smaller than the SLP_Sx assertion widths.
813 * If the pm_pwr_cyc_dur is less than any of the SLP_Sx assertion widths then it returns the
814 * default value PCH_PM_PWR_CYC_DUR.
816 uint8_t get_pm_pwr_cyc_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
817 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur)
819 /* Set default values for the minimum assertion duration */
820 struct cfg_assert_dur cfg_assert_dur = {
821 .slp_a = MinAssertDur2s,
822 .slp_s4 = MinAssertDur1s,
823 .slp_s3 = MinAssertDur50ms,
824 .pm_pwr_cyc_dur = MinAssertDur4s
827 enum min_assert_dur high_assert_width;
829 /* Convert assertion durations from register-encoded to microseconds */
830 get_min_assert_dur(slp_s4_min_assert, slp_s3_min_assert, slp_a_min_assert,
831 pm_pwr_cyc_dur, &cfg_assert_dur);
833 /* Get the highest assertion duration among PCH EDS specified signals for pwr_cyc_dur */
834 high_assert_width = get_high_assert_width(&cfg_assert_dur);
836 if (cfg_assert_dur.pm_pwr_cyc_dur >= high_assert_width)
837 return pm_pwr_cyc_dur;
839 printk(BIOS_DEBUG,
840 "Set PmPwrCycDur to 4s as configured PmPwrCycDur (%d) violates PCH EDS "
841 "spec\n", pm_pwr_cyc_dur);
843 return PCH_PM_PWR_CYC_DUR;
846 void pmc_set_acpi_mode(void)
848 if (!CONFIG(NO_SMM) && !acpi_is_wakeup_s3()) {
849 apm_control(APM_CNT_ACPI_DISABLE);
853 enum pch_pmc_xtal pmc_get_xtal_freq(void)
855 if (!CONFIG(SOC_INTEL_COMMON_BLOCK_PMC_EPOC))
856 dead_code();
858 uint32_t xtal_freq = 0;
859 const uint32_t epoc = read32p(soc_read_pmc_base() + PCH_PMC_EPOC);
861 /* XTAL frequency in bits 21, 20, 17 */
862 xtal_freq |= !!(epoc & (1 << 21)) << 2;
863 xtal_freq |= !!(epoc & (1 << 20)) << 1;
864 xtal_freq |= !!(epoc & (1 << 17)) << 0;
865 switch (xtal_freq) {
866 case 0:
867 return XTAL_24_MHZ;
868 case 1:
869 return XTAL_19_2_MHZ;
870 case 2:
871 return XTAL_38_4_MHZ;
872 default:
873 printk(BIOS_ERR, "Unknown EPOC XTAL frequency setting %u\n", xtal_freq);
874 return XTAL_UNKNOWN_FREQ;
878 void pmc_send_bios_reset_pci_enum_done(void)
880 struct pmc_ipc_buffer req = { 0 };
881 struct pmc_ipc_buffer rsp;
882 uint32_t cmd;
884 req.buf[0] = PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM;
885 cmd = pmc_make_ipc_cmd(PMC_IPC_BIOS_RST_COMPLETE,
886 PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE, 0);
887 if (pmc_send_ipc_cmd(cmd, &req, &rsp) != CB_SUCCESS)
888 printk(BIOS_ERR, "PMC: Failed sending PCI Enumeration Done Command\n");
892 * This function reads and prints SoC QDF information using PMC interface
893 * if SOC_QDF_DYNAMIC_READ_PMC config is enabled.
895 void pmc_dump_soc_qdf_info(void)
897 struct pmc_ipc_buffer req = { 0 };
898 struct pmc_ipc_buffer rsp;
899 uint32_t cmd_reg;
900 int r;
901 char qdf_info[5];
903 if (!CONFIG(SOC_QDF_DYNAMIC_READ_PMC))
904 return;
906 req.buf[0] = PMC_IPC_CMD_REGID_SOC_QDF;
907 cmd_reg = pmc_make_ipc_cmd(PMC_IPC_CMD_SOC_REG_ACC,
908 PMC_IPC_CMD_SUBCMD_SOC_REG_RD,
909 PMC_IPC_BUF_COUNT);
911 r = pmc_send_ipc_cmd(cmd_reg, &req, &rsp);
913 if (r < 0 || rsp.buf[0] == 0) {
914 printk(BIOS_ERR, "%s: pmc_send_ipc_cmd failed or QDF not available.\n",
915 __func__);
916 return;
919 qdf_info[0] = ((rsp.buf[0] >> 24) & 0xFF);
920 qdf_info[1] = ((rsp.buf[0] >> 16) & 0xFF);
921 qdf_info[2] = ((rsp.buf[0] >> 8) & 0xFF);
922 qdf_info[3] = (rsp.buf[0] & 0xFF);
923 qdf_info[4] = '\0';
924 printk(BIOS_INFO, "SoC QDF: %s\n", qdf_info);