Angel Pons | f94ac9a | 2020-04-05 15:46:48 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 3 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 4 | #include <arch/io.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 5 | #include <device/pci_ops.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 6 | #include <cbmem.h> |
| 7 | #include <console/console.h> |
| 8 | #include <device/device.h> |
| 9 | #include <device/pci.h> |
| 10 | #include <device/pci_def.h> |
| 11 | #include <reg_script.h> |
| 12 | #include <stdint.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 13 | #include <string.h> |
Julius Werner | 4ee4bd5 | 2014-10-20 13:46:39 -0700 | [diff] [blame] | 14 | #include <soc/iomap.h> |
| 15 | #include <soc/lpc.h> |
| 16 | #include <soc/pci_devs.h> |
| 17 | #include <soc/pm.h> |
| 18 | #include <soc/romstage.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 19 | |
Arthur Heymans | 7ea4e02 | 2018-12-29 14:20:00 +0100 | [diff] [blame] | 20 | static struct chipset_power_state power_state; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 21 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 22 | static void migrate_power_state(int is_recovery) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 23 | { |
| 24 | struct chipset_power_state *ps_cbmem; |
| 25 | struct chipset_power_state *ps_car; |
| 26 | |
Arthur Heymans | 7ea4e02 | 2018-12-29 14:20:00 +0100 | [diff] [blame] | 27 | ps_car = &power_state; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 28 | ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem)); |
| 29 | |
| 30 | if (ps_cbmem == NULL) { |
| 31 | printk(BIOS_DEBUG, "Not adding power state to cbmem!\n"); |
| 32 | return; |
| 33 | } |
| 34 | memcpy(ps_cbmem, ps_car, sizeof(*ps_cbmem)); |
| 35 | } |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 36 | ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 37 | |
| 38 | /* Return 0, 3, or 5 to indicate the previous sleep state. */ |
| 39 | static int prev_sleep_state(struct chipset_power_state *ps) |
| 40 | { |
| 41 | /* Default to S0. */ |
Aaron Durbin | 9e6d143 | 2016-07-13 23:21:41 -0500 | [diff] [blame] | 42 | int prev_sleep_state = ACPI_S0; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 43 | |
| 44 | if (ps->pm1_sts & WAK_STS) { |
Aaron Durbin | 9e6d143 | 2016-07-13 23:21:41 -0500 | [diff] [blame] | 45 | switch (acpi_sleep_from_pm1(ps->pm1_cnt)) { |
| 46 | case ACPI_S3: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 47 | if (CONFIG(HAVE_ACPI_RESUME)) |
Aaron Durbin | 9e6d143 | 2016-07-13 23:21:41 -0500 | [diff] [blame] | 48 | prev_sleep_state = ACPI_S3; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 49 | break; |
Aaron Durbin | 9e6d143 | 2016-07-13 23:21:41 -0500 | [diff] [blame] | 50 | case ACPI_S5: |
| 51 | prev_sleep_state = ACPI_S5; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | /* Clear SLP_TYP. */ |
| 55 | outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT); |
| 56 | } |
| 57 | |
| 58 | if (ps->gen_pmcon3 & (PWR_FLR | SUS_PWR_FLR)) |
Aaron Durbin | 9e6d143 | 2016-07-13 23:21:41 -0500 | [diff] [blame] | 59 | prev_sleep_state = ACPI_S5; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 60 | |
| 61 | return prev_sleep_state; |
| 62 | } |
| 63 | |
| 64 | static void dump_power_state(struct chipset_power_state *ps) |
| 65 | { |
| 66 | printk(BIOS_DEBUG, "PM1_STS: %04x\n", ps->pm1_sts); |
| 67 | printk(BIOS_DEBUG, "PM1_EN: %04x\n", ps->pm1_en); |
| 68 | printk(BIOS_DEBUG, "PM1_CNT: %08x\n", ps->pm1_cnt); |
Duncan Laurie | 047f03a | 2014-08-11 09:54:19 -0700 | [diff] [blame] | 69 | printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", |
| 70 | ps->tco1_sts, ps->tco2_sts); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 71 | |
| 72 | printk(BIOS_DEBUG, "GPE0_STS: %08x %08x %08x %08x\n", |
| 73 | ps->gpe0_sts[0], ps->gpe0_sts[1], |
| 74 | ps->gpe0_sts[2], ps->gpe0_sts[3]); |
| 75 | printk(BIOS_DEBUG, "GPE0_EN: %08x %08x %08x %08x\n", |
| 76 | ps->gpe0_en[0], ps->gpe0_en[1], |
| 77 | ps->gpe0_en[2], ps->gpe0_en[3]); |
| 78 | |
| 79 | printk(BIOS_DEBUG, "GEN_PMCON: %04x %04x %04x\n", |
| 80 | ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3); |
| 81 | |
| 82 | printk(BIOS_DEBUG, "Previous Sleep State: S%d\n", |
| 83 | ps->prev_sleep_state); |
| 84 | } |
| 85 | |
| 86 | /* Fill power state structure from ACPI PM registers */ |
| 87 | struct chipset_power_state *fill_power_state(void) |
| 88 | { |
Arthur Heymans | 7ea4e02 | 2018-12-29 14:20:00 +0100 | [diff] [blame] | 89 | struct chipset_power_state *ps = &power_state; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 90 | |
| 91 | ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS); |
| 92 | ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN); |
| 93 | ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT); |
Duncan Laurie | 047f03a | 2014-08-11 09:54:19 -0700 | [diff] [blame] | 94 | ps->tco1_sts = inw(ACPI_BASE_ADDRESS + TCO1_STS); |
| 95 | ps->tco2_sts = inw(ACPI_BASE_ADDRESS + TCO2_STS); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 96 | ps->gpe0_sts[0] = inl(ACPI_BASE_ADDRESS + GPE0_STS(0)); |
| 97 | ps->gpe0_sts[1] = inl(ACPI_BASE_ADDRESS + GPE0_STS(1)); |
| 98 | ps->gpe0_sts[2] = inl(ACPI_BASE_ADDRESS + GPE0_STS(2)); |
| 99 | ps->gpe0_sts[3] = inl(ACPI_BASE_ADDRESS + GPE0_STS(3)); |
| 100 | ps->gpe0_en[0] = inl(ACPI_BASE_ADDRESS + GPE0_EN(0)); |
| 101 | ps->gpe0_en[1] = inl(ACPI_BASE_ADDRESS + GPE0_EN(1)); |
| 102 | ps->gpe0_en[2] = inl(ACPI_BASE_ADDRESS + GPE0_EN(2)); |
| 103 | ps->gpe0_en[3] = inl(ACPI_BASE_ADDRESS + GPE0_EN(3)); |
| 104 | |
| 105 | ps->gen_pmcon1 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_1); |
| 106 | ps->gen_pmcon2 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_2); |
| 107 | ps->gen_pmcon3 = pci_read_config16(PCH_DEV_LPC, GEN_PMCON_3); |
| 108 | |
| 109 | ps->prev_sleep_state = prev_sleep_state(ps); |
| 110 | |
| 111 | dump_power_state(ps); |
| 112 | |
| 113 | return ps; |
| 114 | } |