blob: 0cd1a9d28de92054c947e5c60c1a919a51fce394 [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
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070017#include <cbmem.h>
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_def.h>
22#include <reg_script.h>
23#include <stdint.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070024#include <string.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070025#include <soc/iomap.h>
26#include <soc/lpc.h>
27#include <soc/pci_devs.h>
28#include <soc/pm.h>
29#include <soc/romstage.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070030
Arthur Heymans7ea4e022018-12-29 14:20:00 +010031static struct chipset_power_state power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070032
Aaron Durbin41607a42015-06-09 13:54:10 -050033static void migrate_power_state(int is_recovery)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034{
35 struct chipset_power_state *ps_cbmem;
36 struct chipset_power_state *ps_car;
37
Arthur Heymans7ea4e022018-12-29 14:20:00 +010038 ps_car = &power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070039 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
40
41 if (ps_cbmem == NULL) {
42 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
43 return;
44 }
45 memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem));
46}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +020047ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070048
49/* Return 0, 3, or 5 to indicate the previous sleep state. */
50static int prev_sleep_state(struct chipset_power_state *ps)
51{
52 /* Default to S0. */
Aaron Durbin9e6d1432016-07-13 23:21:41 -050053 int prev_sleep_state = ACPI_S0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070054
55 if (ps->pm1_sts & WAK_STS) {
Aaron Durbin9e6d1432016-07-13 23:21:41 -050056 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
57 case ACPI_S3:
Julius Wernercd49cce2019-03-05 16:53:33 -080058 if (CONFIG(HAVE_ACPI_RESUME))
Aaron Durbin9e6d1432016-07-13 23:21:41 -050059 prev_sleep_state = ACPI_S3;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070060 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -050061 case ACPI_S5:
62 prev_sleep_state = ACPI_S5;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070063 break;
64 }
65 /* Clear SLP_TYP. */
66 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
67 }
68
69 if (ps->gen_pmcon3 & (PWR_FLR | SUS_PWR_FLR))
Aaron Durbin9e6d1432016-07-13 23:21:41 -050070 prev_sleep_state = ACPI_S5;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070071
72 return prev_sleep_state;
73}
74
75static void dump_power_state(struct chipset_power_state *ps)
76{
77 printk(BIOS_DEBUG, "PM1_STS: %04x\n", ps->pm1_sts);
78 printk(BIOS_DEBUG, "PM1_EN: %04x\n", ps->pm1_en);
79 printk(BIOS_DEBUG, "PM1_CNT: %08x\n", ps->pm1_cnt);
Duncan Laurie047f03a2014-08-11 09:54:19 -070080 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n",
81 ps->tco1_sts, ps->tco2_sts);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070082
83 printk(BIOS_DEBUG, "GPE0_STS: %08x %08x %08x %08x\n",
84 ps->gpe0_sts[0], ps->gpe0_sts[1],
85 ps->gpe0_sts[2], ps->gpe0_sts[3]);
86 printk(BIOS_DEBUG, "GPE0_EN: %08x %08x %08x %08x\n",
87 ps->gpe0_en[0], ps->gpe0_en[1],
88 ps->gpe0_en[2], ps->gpe0_en[3]);
89
90 printk(BIOS_DEBUG, "GEN_PMCON: %04x %04x %04x\n",
91 ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3);
92
93 printk(BIOS_DEBUG, "Previous Sleep State: S%d\n",
94 ps->prev_sleep_state);
95}
96
97/* Fill power state structure from ACPI PM registers */
98struct chipset_power_state *fill_power_state(void)
99{
Arthur Heymans7ea4e022018-12-29 14:20:00 +0100100 struct chipset_power_state *ps = &power_state;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700101
102 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
103 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
104 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
Duncan Laurie047f03a2014-08-11 09:54:19 -0700105 ps->tco1_sts = inw(ACPI_BASE_ADDRESS + TCO1_STS);
106 ps->tco2_sts = inw(ACPI_BASE_ADDRESS + TCO2_STS);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700107 ps->gpe0_sts[0] = inl(ACPI_BASE_ADDRESS + GPE0_STS(0));
108 ps->gpe0_sts[1] = inl(ACPI_BASE_ADDRESS + GPE0_STS(1));
109 ps->gpe0_sts[2] = inl(ACPI_BASE_ADDRESS + GPE0_STS(2));
110 ps->gpe0_sts[3] = inl(ACPI_BASE_ADDRESS + GPE0_STS(3));
111 ps->gpe0_en[0] = inl(ACPI_BASE_ADDRESS + GPE0_EN(0));
112 ps->gpe0_en[1] = inl(ACPI_BASE_ADDRESS + GPE0_EN(1));
113 ps->gpe0_en[2] = inl(ACPI_BASE_ADDRESS + GPE0_EN(2));
114 ps->gpe0_en[3] = inl(ACPI_BASE_ADDRESS + GPE0_EN(3));
115
116 ps->gen_pmcon1 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_1);
117 ps->gen_pmcon2 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_2);
118 ps->gen_pmcon3 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_3);
119
120 ps->prev_sleep_state = prev_sleep_state(ps);
121
122 dump_power_state(ps);
123
124 return ps;
125}