Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | /* |
| 4 | * Helper functions for dealing with power management registers |
| 5 | * and the differences between PCH variants. |
| 6 | */ |
| 7 | |
| 8 | #define __SIMPLE_DEVICE__ |
| 9 | |
Kyösti Mälkki | 2787237 | 2021-01-21 16:05:26 +0200 | [diff] [blame] | 10 | #include <acpi/acpi_pm.h> |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 11 | #include <console/console.h> |
| 12 | #include <device/device.h> |
| 13 | #include <device/mmio.h> |
| 14 | #include <device/pci.h> |
| 15 | #include <device/pci_def.h> |
| 16 | #include <intelblocks/pmclib.h> |
| 17 | #include <intelblocks/rtc.h> |
| 18 | #include <intelblocks/tco.h> |
| 19 | #include <security/vboot/vbnv.h> |
| 20 | #include <soc/espi.h> |
| 21 | #include <soc/gpe.h> |
| 22 | #include <soc/gpio.h> |
| 23 | #include <soc/iomap.h> |
| 24 | #include <soc/pci_devs.h> |
| 25 | #include <soc/pm.h> |
| 26 | #include <soc/smbus.h> |
| 27 | #include <soc/soc_chip.h> |
| 28 | |
| 29 | /* |
| 30 | * SMI |
| 31 | */ |
| 32 | |
| 33 | const char *const *soc_smi_sts_array(size_t *a) |
| 34 | { |
| 35 | static const char *const smi_sts_bits[] = { |
| 36 | [BIOS_STS_BIT] = "BIOS", |
| 37 | [LEGACY_USB_STS_BIT] = "LEGACY_USB", |
| 38 | [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI", |
| 39 | [APM_STS_BIT] = "APM", |
| 40 | [SWSMI_TMR_STS_BIT] = "SWSMI_TMR", |
| 41 | [PM1_STS_BIT] = "PM1", |
| 42 | [GPE0_STS_BIT] = "GPE0", |
| 43 | [GPIO_STS_BIT] = "GPI", |
| 44 | [MCSMI_STS_BIT] = "MCSMI", |
| 45 | [DEVMON_STS_BIT] = "DEVMON", |
| 46 | [TCO_STS_BIT] = "TCO", |
| 47 | [PERIODIC_STS_BIT] = "PERIODIC", |
| 48 | [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI", |
| 49 | [SMBUS_SMI_STS_BIT] = "SMBUS_SMI", |
| 50 | [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI", |
| 51 | [MONITOR_STS_BIT] = "MONITOR", |
| 52 | [SPI_SMI_STS_BIT] = "SPI", |
| 53 | [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK", |
| 54 | [ESPI_SMI_STS_BIT] = "ESPI_SMI", |
| 55 | }; |
| 56 | |
| 57 | *a = ARRAY_SIZE(smi_sts_bits); |
| 58 | return smi_sts_bits; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * TCO |
| 63 | */ |
| 64 | |
| 65 | const char *const *soc_tco_sts_array(size_t *a) |
| 66 | { |
| 67 | static const char *const tco_sts_bits[] = { |
| 68 | [0] = "NMI2SMI", |
| 69 | [1] = "SW_TCO", |
| 70 | [2] = "TCO_INT", |
| 71 | [3] = "TIMEOUT", |
| 72 | [7] = "NEWCENTURY", |
| 73 | [8] = "BIOSWR", |
| 74 | [9] = "DMISCI", |
| 75 | [10] = "DMISMI", |
| 76 | [12] = "DMISERR", |
| 77 | [13] = "SLVSEL", |
| 78 | [16] = "INTRD_DET", |
| 79 | [17] = "SECOND_TO", |
| 80 | [18] = "BOOT", |
| 81 | [20] = "SMLINK_SLV" |
| 82 | }; |
| 83 | |
| 84 | *a = ARRAY_SIZE(tco_sts_bits); |
| 85 | return tco_sts_bits; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * GPE0 |
| 90 | */ |
| 91 | |
| 92 | const char *const *soc_std_gpe_sts_array(size_t *a) |
| 93 | { |
| 94 | static const char *const gpe_sts_bits[] = { |
| 95 | [1] = "HOTPLUG", |
| 96 | [2] = "SWGPE", |
| 97 | [6] = "TCO_SCI", |
| 98 | [7] = "SMB_WAK", |
| 99 | [9] = "PCI_EXP", |
| 100 | [10] = "BATLOW", |
| 101 | [11] = "PME", |
| 102 | [12] = "ME", |
| 103 | [13] = "PME_B0", |
| 104 | [14] = "eSPI", |
| 105 | [15] = "GPIO Tier-2", |
| 106 | [16] = "LAN_WAKE", |
| 107 | [18] = "WADT" |
| 108 | }; |
| 109 | |
| 110 | *a = ARRAY_SIZE(gpe_sts_bits); |
| 111 | return gpe_sts_bits; |
| 112 | } |
| 113 | |
| 114 | void pmc_set_disb(void) |
| 115 | { |
| 116 | /* Set the DISB after DRAM init */ |
| 117 | uint8_t disb_val; |
| 118 | /* Only care about bits [23:16] of register GEN_PMCON_A */ |
| 119 | uint8_t *addr = (uint8_t *)(pmc_mmio_regs() + GEN_PMCON_A + 2); |
| 120 | |
| 121 | disb_val = read8(addr); |
| 122 | disb_val |= (DISB >> 16); |
| 123 | |
| 124 | /* Don't clear bits that are write-1-to-clear */ |
| 125 | disb_val &= ~((MS4V | SUS_PWR_FLR) >> 16); |
| 126 | write8(addr, disb_val); |
| 127 | } |
| 128 | |
| 129 | void pmc_clear_pmcon_sts(void) |
| 130 | { |
| 131 | uint32_t reg_val; |
| 132 | uint8_t *addr; |
| 133 | addr = pmc_mmio_regs(); |
| 134 | |
| 135 | reg_val = read32(addr + GEN_PMCON_A); |
| 136 | /* Clear SUS_PWR_FLR, GBL_RST_STS, HOST_RST_STS, PWR_FLR bits |
| 137 | * while retaining MS4V write-1-to-clear bit */ |
| 138 | reg_val &= ~(MS4V); |
| 139 | |
| 140 | write32((addr + GEN_PMCON_A), reg_val); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * PMC controller gets hidden from PCI bus |
| 145 | * during FSP-Silicon init call. Hence PWRMBASE |
| 146 | * can't be accessible using PCI configuration space |
| 147 | * read/write. |
| 148 | */ |
| 149 | uint8_t *pmc_mmio_regs(void) |
| 150 | { |
| 151 | return (void *)(uintptr_t)PCH_PWRM_BASE_ADDRESS; |
| 152 | } |
| 153 | |
| 154 | uintptr_t soc_read_pmc_base(void) |
| 155 | { |
| 156 | return (uintptr_t)pmc_mmio_regs(); |
| 157 | } |
| 158 | |
| 159 | uint32_t *soc_pmc_etr_addr(void) |
| 160 | { |
| 161 | return (uint32_t *)(soc_read_pmc_base() + ETR); |
| 162 | } |
| 163 | |
| 164 | void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2) |
| 165 | { |
| 166 | DEVTREE_CONST struct soc_intel_elkhartlake_config *config; |
| 167 | |
| 168 | config = config_of_soc(); |
| 169 | |
| 170 | /* Assign to out variable */ |
| 171 | *dw0 = config->pmc_gpe0_dw0; |
| 172 | *dw1 = config->pmc_gpe0_dw1; |
| 173 | *dw2 = config->pmc_gpe0_dw2; |
| 174 | } |
| 175 | |
| 176 | static int rtc_failed(uint32_t gen_pmcon_b) |
| 177 | { |
| 178 | return !!(gen_pmcon_b & RTC_BATTERY_DEAD); |
| 179 | } |
| 180 | |
| 181 | int soc_get_rtc_failed(void) |
| 182 | { |
Kyösti Mälkki | 2787237 | 2021-01-21 16:05:26 +0200 | [diff] [blame] | 183 | const struct chipset_power_state *ps; |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 184 | |
Kyösti Mälkki | 2787237 | 2021-01-21 16:05:26 +0200 | [diff] [blame] | 185 | if (acpi_pm_state_for_rtc(&ps) < 0) |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 186 | return 1; |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 187 | |
| 188 | return rtc_failed(ps->gen_pmcon_b); |
| 189 | } |
| 190 | |
| 191 | int vbnv_cmos_failed(void) |
| 192 | { |
| 193 | return rtc_failed(read32(pmc_mmio_regs() + GEN_PMCON_B)); |
| 194 | } |
| 195 | |
| 196 | static inline int deep_s3_enabled(void) |
| 197 | { |
| 198 | uint32_t deep_s3_pol; |
| 199 | |
| 200 | deep_s3_pol = read32(pmc_mmio_regs() + S3_PWRGATE_POL); |
| 201 | return !!(deep_s3_pol & (S3DC_GATE_SUS | S3AC_GATE_SUS)); |
| 202 | } |
| 203 | |
| 204 | /* Return 0, 3, or 5 to indicate the previous sleep state. */ |
Angel Pons | f5d090d | 2021-02-19 17:49:00 +0100 | [diff] [blame] | 205 | int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state) |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 206 | { |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Check for any power failure to determine if this a wake from |
Angel Pons | f5d090d | 2021-02-19 17:49:00 +0100 | [diff] [blame] | 209 | * S5 because the PCH does not set the WAK_STS bit when waking |
| 210 | * from a true G3 state. |
| 211 | */ |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 212 | if (ps->gen_pmcon_a & (PWR_FLR | SUS_PWR_FLR)) |
| 213 | prev_sleep_state = ACPI_S5; |
| 214 | |
| 215 | /* |
| 216 | * If waking from S3 determine if deep S3 is enabled. If not, |
| 217 | * need to check both deep sleep well and normal suspend well. |
| 218 | * Otherwise just check deep sleep well. |
| 219 | */ |
| 220 | if (prev_sleep_state == ACPI_S3) { |
| 221 | /* PWR_FLR represents deep sleep power well loss. */ |
| 222 | uint32_t mask = PWR_FLR; |
| 223 | |
| 224 | /* If deep s3 isn't enabled check the suspend well too. */ |
| 225 | if (!deep_s3_enabled()) |
| 226 | mask |= SUS_PWR_FLR; |
| 227 | |
| 228 | if (ps->gen_pmcon_a & mask) |
| 229 | prev_sleep_state = ACPI_S5; |
| 230 | } |
| 231 | |
| 232 | return prev_sleep_state; |
| 233 | } |
| 234 | |
| 235 | void soc_fill_power_state(struct chipset_power_state *ps) |
| 236 | { |
| 237 | uint8_t *pmc; |
| 238 | |
| 239 | ps->tco1_sts = tco_read_reg(TCO1_STS); |
| 240 | ps->tco2_sts = tco_read_reg(TCO2_STS); |
| 241 | |
Angel Pons | f5d090d | 2021-02-19 17:49:00 +0100 | [diff] [blame] | 242 | printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts); |
Tan, Lean Sheng | 05dfe31 | 2020-08-25 20:40:17 -0700 | [diff] [blame] | 243 | |
| 244 | pmc = pmc_mmio_regs(); |
| 245 | ps->gen_pmcon_a = read32(pmc + GEN_PMCON_A); |
| 246 | ps->gen_pmcon_b = read32(pmc + GEN_PMCON_B); |
| 247 | ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0); |
| 248 | ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1); |
| 249 | |
| 250 | printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n", |
| 251 | ps->gen_pmcon_a, ps->gen_pmcon_b); |
| 252 | |
| 253 | printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n", |
| 254 | ps->gblrst_cause[0], ps->gblrst_cause[1]); |
| 255 | } |
| 256 | |
| 257 | /* STM Support */ |
| 258 | uint16_t get_pmbase(void) |
| 259 | { |
| 260 | return (uint16_t) ACPI_BASE_ADDRESS; |
| 261 | } |
Angel Pons | 505e383 | 2021-04-17 13:02:37 +0200 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Set which power state system will be after reapplying |
| 265 | * the power (from G3 State) |
| 266 | */ |
| 267 | void pmc_soc_set_afterg3_en(const bool on) |
| 268 | { |
| 269 | uint8_t reg8; |
| 270 | uint8_t *const pmcbase = pmc_mmio_regs(); |
| 271 | |
| 272 | reg8 = read8(pmcbase + GEN_PMCON_A); |
| 273 | if (on) |
| 274 | reg8 &= ~SLEEP_AFTER_POWER_FAIL; |
| 275 | else |
| 276 | reg8 |= SLEEP_AFTER_POWER_FAIL; |
| 277 | write8(pmcbase + GEN_PMCON_A, reg8); |
| 278 | } |