blob: b1ab622b7256ceae745e2e22f29a05d21d21bbfb [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
Duncan Lauriec88c54c2014-04-30 16:36:13 -07003#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07005#include <cbmem.h>
6#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_def.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070010#include <string.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070011#include <soc/iomap.h>
12#include <soc/lpc.h>
13#include <soc/pci_devs.h>
14#include <soc/pm.h>
15#include <soc/romstage.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070016
Arthur Heymans7ea4e022018-12-29 14:20:00 +010017static struct chipset_power_state power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070018
Aaron Durbin41607a42015-06-09 13:54:10 -050019static void migrate_power_state(int is_recovery)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070020{
21 struct chipset_power_state *ps_cbmem;
22 struct chipset_power_state *ps_car;
23
Arthur Heymans7ea4e022018-12-29 14:20:00 +010024 ps_car = &power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070025 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
26
27 if (ps_cbmem == NULL) {
28 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
29 return;
30 }
31 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
32}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +030033CBMEM_CREATION_HOOK(migrate_power_state);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034
35/* Return 0, 3, or 5 to indicate the previous sleep state. */
Kyösti Mälkki540902c2021-01-22 08:02:50 +020036static int prev_sleep_state(const struct chipset_power_state *ps)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070037{
38 /* Default to S0. */
Aaron Durbin9e6d1432016-07-13 23:21:41 -050039 int prev_sleep_state = ACPI_S0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070040
41 if (ps->pm1_sts & WAK_STS) {
Aaron Durbin9e6d1432016-07-13 23:21:41 -050042 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
43 case ACPI_S3:
Julius Wernercd49cce2019-03-05 16:53:33 -080044 if (CONFIG(HAVE_ACPI_RESUME))
Aaron Durbin9e6d1432016-07-13 23:21:41 -050045 prev_sleep_state = ACPI_S3;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070046 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -050047 case ACPI_S5:
48 prev_sleep_state = ACPI_S5;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070049 break;
50 }
51 /* Clear SLP_TYP. */
52 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
53 }
54
55 if (ps->gen_pmcon3 & (PWR_FLR | SUS_PWR_FLR))
Aaron Durbin9e6d1432016-07-13 23:21:41 -050056 prev_sleep_state = ACPI_S5;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070057
58 return prev_sleep_state;
59}
60
61static void dump_power_state(struct chipset_power_state *ps)
62{
63 printk(BIOS_DEBUG, "PM1_STS: %04x\n", ps->pm1_sts);
64 printk(BIOS_DEBUG, "PM1_EN: %04x\n", ps->pm1_en);
65 printk(BIOS_DEBUG, "PM1_CNT: %08x\n", ps->pm1_cnt);
Duncan Laurie047f03a2014-08-11 09:54:19 -070066 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n",
67 ps->tco1_sts, ps->tco2_sts);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070068
69 printk(BIOS_DEBUG, "GPE0_STS: %08x %08x %08x %08x\n",
70 ps->gpe0_sts[0], ps->gpe0_sts[1],
71 ps->gpe0_sts[2], ps->gpe0_sts[3]);
72 printk(BIOS_DEBUG, "GPE0_EN: %08x %08x %08x %08x\n",
73 ps->gpe0_en[0], ps->gpe0_en[1],
74 ps->gpe0_en[2], ps->gpe0_en[3]);
75
76 printk(BIOS_DEBUG, "GEN_PMCON: %04x %04x %04x\n",
77 ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3);
78
79 printk(BIOS_DEBUG, "Previous Sleep State: S%d\n",
80 ps->prev_sleep_state);
81}
82
83/* Fill power state structure from ACPI PM registers */
84struct chipset_power_state *fill_power_state(void)
85{
Arthur Heymans7ea4e022018-12-29 14:20:00 +010086 struct chipset_power_state *ps = &power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070087
88 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
89 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
90 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
Duncan Laurie047f03a2014-08-11 09:54:19 -070091 ps->tco1_sts = inw(ACPI_BASE_ADDRESS + TCO1_STS);
92 ps->tco2_sts = inw(ACPI_BASE_ADDRESS + TCO2_STS);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070093 ps->gpe0_sts[0] = inl(ACPI_BASE_ADDRESS + GPE0_STS(0));
94 ps->gpe0_sts[1] = inl(ACPI_BASE_ADDRESS + GPE0_STS(1));
95 ps->gpe0_sts[2] = inl(ACPI_BASE_ADDRESS + GPE0_STS(2));
96 ps->gpe0_sts[3] = inl(ACPI_BASE_ADDRESS + GPE0_STS(3));
97 ps->gpe0_en[0] = inl(ACPI_BASE_ADDRESS + GPE0_EN(0));
98 ps->gpe0_en[1] = inl(ACPI_BASE_ADDRESS + GPE0_EN(1));
99 ps->gpe0_en[2] = inl(ACPI_BASE_ADDRESS + GPE0_EN(2));
100 ps->gpe0_en[3] = inl(ACPI_BASE_ADDRESS + GPE0_EN(3));
101
102 ps->gen_pmcon1 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_1);
103 ps->gen_pmcon2 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_2);
104 ps->gen_pmcon3 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_3);
105
106 ps->prev_sleep_state = prev_sleep_state(ps);
107
108 dump_power_state(ps);
109
110 return ps;
111}