Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * Copyright (C) 2015-2016 Intel Corp. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 18 | #define __SIMPLE_DEVICE__ |
| 19 | |
Duncan Laurie | 2e79009 | 2016-09-19 12:05:49 -0700 | [diff] [blame] | 20 | #include <arch/acpi.h> |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 21 | #include <arch/io.h> |
| 22 | #include <console/console.h> |
Shaunak Saha | 60b4618 | 2016-08-02 17:25:13 -0700 | [diff] [blame] | 23 | #include <cbmem.h> |
Andrey Petrov | 3b63753 | 2016-11-30 17:39:16 -0800 | [diff] [blame] | 24 | #include <cpu/x86/msr.h> |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 25 | #include <rules.h> |
| 26 | #include <device/pci_def.h> |
Aaron Durbin | c2b7779 | 2016-07-14 00:26:50 -0500 | [diff] [blame] | 27 | #include <halt.h> |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 28 | #include <soc/iomap.h> |
Andrey Petrov | 3b63753 | 2016-11-30 17:39:16 -0800 | [diff] [blame] | 29 | #include <soc/cpu.h> |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 30 | #include <soc/pci_devs.h> |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 31 | #include <soc/pm.h> |
| 32 | #include <device/device.h> |
| 33 | #include <device/pci.h> |
Duncan Laurie | 2f3736e | 2016-11-03 10:33:43 -0700 | [diff] [blame] | 34 | #include <timer.h> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 35 | #include <vboot/vboot_common.h> |
Duncan Laurie | a673d1c | 2016-09-19 12:02:54 -0700 | [diff] [blame] | 36 | #include "chip.h" |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 37 | |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 38 | static uintptr_t read_pmc_mmio_bar(void) |
| 39 | { |
Lijian Zhao | 9108680 | 2016-09-06 18:15:29 -0700 | [diff] [blame] | 40 | return PMC_BAR0; |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 41 | } |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 42 | |
Shaunak Saha | 9a0c9ac | 2016-06-27 23:00:15 -0700 | [diff] [blame] | 43 | uintptr_t get_pmc_mmio_bar(void) |
| 44 | { |
| 45 | return read_pmc_mmio_bar(); |
| 46 | } |
| 47 | |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 48 | static void print_num_status_bits(int num_bits, uint32_t status, |
| 49 | const char * const bit_names[]) |
| 50 | { |
| 51 | int i; |
| 52 | |
| 53 | if (!status) |
| 54 | return; |
| 55 | |
| 56 | for (i = num_bits - 1; i >= 0; i--) { |
| 57 | if (status & (1 << i)) { |
| 58 | if (bit_names[i]) |
| 59 | printk(BIOS_DEBUG, "%s ", bit_names[i]); |
| 60 | else |
| 61 | printk(BIOS_DEBUG, "BIT%d ", i); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static uint32_t print_smi_status(uint32_t smi_sts) |
| 67 | { |
| 68 | static const char * const smi_sts_bits[] = { |
Aaron Durbin | 7929dd0 | 2016-06-10 18:01:45 -0500 | [diff] [blame] | 69 | [BIOS_SMI_STS] = "BIOS", |
| 70 | [LEGACY_USB_SMI_STS] = "LEGACY USB", |
| 71 | [SLP_SMI_STS] = "SLP_SMI", |
| 72 | [APM_SMI_STS] = "APM", |
| 73 | [SWSMI_TMR_SMI_STS] = "SWSMI_TMR", |
Aaron Durbin | a554b71 | 2016-06-10 18:04:21 -0500 | [diff] [blame] | 74 | [FAKE_PM1_SMI_STS] = "PM1", |
Lee Leahy | 320b7ca | 2017-03-09 09:42:48 -0800 | [diff] [blame^] | 75 | [GPIO_SMI_STS] = "GPIO_SMI", |
| 76 | [GPIO_UNLOCK_SMI_STS] = "GPIO_UNLOCK_SSMI", |
Aaron Durbin | 7929dd0 | 2016-06-10 18:01:45 -0500 | [diff] [blame] | 77 | [MC_SMI_STS] = "MCSMI", |
| 78 | [TCO_SMI_STS] = "TCO", |
| 79 | [PERIODIC_SMI_STS] = "PERIODIC", |
| 80 | [SERIRQ_SMI_STS] = "SERIRQ", |
| 81 | [SMBUS_SMI_STS] = "SMBUS_SMI", |
| 82 | [XHCI_SMI_STS] = "XHCI", |
| 83 | [HSMBUS_SMI_STS] = "HOST_SMBUS", |
| 84 | [SCS_SMI_STS] = "SCS", |
| 85 | [PCIE_SMI_STS] = "PCI_EXP_SMI", |
| 86 | [SCC2_SMI_STS] = "SCC2", |
| 87 | [SPI_SSMI_STS] = "SPI_SSMI", |
| 88 | [SPI_SMI_STS] = "SPI", |
| 89 | [PMC_OCP_SMI_STS] = "OCP_CSE", |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | if (!smi_sts) |
| 93 | return 0; |
| 94 | |
| 95 | printk(BIOS_DEBUG, "SMI_STS: "); |
| 96 | print_num_status_bits(ARRAY_SIZE(smi_sts_bits), smi_sts, smi_sts_bits); |
| 97 | printk(BIOS_DEBUG, "\n"); |
| 98 | |
| 99 | return smi_sts; |
| 100 | } |
| 101 | |
| 102 | static uint32_t reset_smi_status(void) |
| 103 | { |
| 104 | uint32_t smi_sts = inl(ACPI_PMIO_BASE + SMI_STS); |
| 105 | outl(smi_sts, ACPI_PMIO_BASE + SMI_STS); |
| 106 | return smi_sts; |
| 107 | } |
| 108 | |
| 109 | uint32_t clear_smi_status(void) |
| 110 | { |
Aaron Durbin | a554b71 | 2016-06-10 18:04:21 -0500 | [diff] [blame] | 111 | uint32_t sts = reset_smi_status(); |
| 112 | |
| 113 | /* |
| 114 | * Check for power button status if nothing else is indicating an SMI |
| 115 | * and SMIs aren't turned into SCIs. Apparently, there is no PM1 status |
| 116 | * bit in the SMI status register. That makes things difficult for |
| 117 | * determining if the power button caused an SMI. |
| 118 | */ |
| 119 | if (sts == 0 && !(inl(ACPI_PMIO_BASE + PM1_CNT) & SCI_EN)) { |
| 120 | uint16_t pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS); |
| 121 | |
| 122 | /* Fake PM1 status bit if power button pressed. */ |
| 123 | if (pm1_sts & PWRBTN_STS) |
| 124 | sts |= (1 << FAKE_PM1_SMI_STS); |
| 125 | } |
| 126 | |
| 127 | return print_smi_status(sts); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | uint32_t get_smi_en(void) |
| 131 | { |
| 132 | return inl(ACPI_PMIO_BASE + SMI_EN); |
| 133 | } |
| 134 | |
| 135 | void enable_smi(uint32_t mask) |
| 136 | { |
| 137 | uint32_t smi_en = inl(ACPI_PMIO_BASE + SMI_EN); |
| 138 | smi_en |= mask; |
| 139 | outl(smi_en, ACPI_PMIO_BASE + SMI_EN); |
| 140 | } |
| 141 | |
| 142 | void disable_smi(uint32_t mask) |
| 143 | { |
| 144 | uint32_t smi_en = inl(ACPI_PMIO_BASE + SMI_EN); |
| 145 | smi_en &= ~mask; |
| 146 | outl(smi_en, ACPI_PMIO_BASE + SMI_EN); |
| 147 | } |
| 148 | |
| 149 | void enable_pm1_control(uint32_t mask) |
| 150 | { |
| 151 | uint32_t pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT); |
| 152 | pm1_cnt |= mask; |
| 153 | outl(pm1_cnt, ACPI_PMIO_BASE + PM1_CNT); |
| 154 | } |
| 155 | |
| 156 | void disable_pm1_control(uint32_t mask) |
| 157 | { |
| 158 | uint32_t pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT); |
| 159 | pm1_cnt &= ~mask; |
| 160 | outl(pm1_cnt, ACPI_PMIO_BASE + PM1_CNT); |
| 161 | } |
| 162 | |
| 163 | static uint16_t reset_pm1_status(void) |
| 164 | { |
| 165 | uint16_t pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS); |
| 166 | outw(pm1_sts, ACPI_PMIO_BASE + PM1_STS); |
| 167 | return pm1_sts; |
| 168 | } |
| 169 | |
| 170 | static uint16_t print_pm1_status(uint16_t pm1_sts) |
| 171 | { |
| 172 | static const char * const pm1_sts_bits[] = { |
| 173 | [0] = "TMROF", |
| 174 | [5] = "GBL", |
| 175 | [8] = "PWRBTN", |
| 176 | [10] = "RTC", |
| 177 | [11] = "PRBTNOR", |
| 178 | [13] = "USB", |
| 179 | [14] = "PCIEXPWAK", |
| 180 | [15] = "WAK", |
| 181 | }; |
| 182 | |
| 183 | if (!pm1_sts) |
| 184 | return 0; |
| 185 | |
| 186 | printk(BIOS_SPEW, "PM1_STS: "); |
| 187 | print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits); |
| 188 | printk(BIOS_SPEW, "\n"); |
| 189 | |
| 190 | return pm1_sts; |
| 191 | } |
| 192 | |
| 193 | uint16_t clear_pm1_status(void) |
| 194 | { |
| 195 | return print_pm1_status(reset_pm1_status()); |
| 196 | } |
| 197 | |
| 198 | void enable_pm1(uint16_t events) |
| 199 | { |
| 200 | outw(events, ACPI_PMIO_BASE + PM1_EN); |
| 201 | } |
| 202 | |
| 203 | static uint32_t print_tco_status(uint32_t tco_sts) |
| 204 | { |
| 205 | static const char * const tco_sts_bits[] = { |
| 206 | [3] = "TIMEOUT", |
| 207 | [17] = "SECOND_TO", |
| 208 | }; |
| 209 | |
| 210 | if (!tco_sts) |
| 211 | return 0; |
| 212 | |
| 213 | printk(BIOS_DEBUG, "TCO_STS: "); |
| 214 | print_num_status_bits(ARRAY_SIZE(tco_sts_bits), tco_sts, tco_sts_bits); |
| 215 | printk(BIOS_DEBUG, "\n"); |
| 216 | |
| 217 | return tco_sts; |
| 218 | } |
| 219 | |
| 220 | static uint32_t reset_tco_status(void) |
| 221 | { |
| 222 | uint32_t tco_sts = inl(ACPI_PMIO_BASE + TCO_STS); |
| 223 | uint32_t tco_en = inl(ACPI_PMIO_BASE + TCO1_CNT); |
| 224 | |
| 225 | outl(tco_sts, ACPI_PMIO_BASE + TCO_STS); |
| 226 | return tco_sts & tco_en; |
| 227 | } |
| 228 | |
| 229 | uint32_t clear_tco_status(void) |
| 230 | { |
| 231 | return print_tco_status(reset_tco_status()); |
| 232 | } |
| 233 | |
| 234 | void enable_gpe(uint32_t mask) |
| 235 | { |
| 236 | uint32_t gpe0a_en = inl(ACPI_PMIO_BASE + GPE0_EN(0)); |
| 237 | gpe0a_en |= mask; |
| 238 | outl(gpe0a_en, ACPI_PMIO_BASE + GPE0_EN(0)); |
| 239 | } |
| 240 | |
| 241 | void disable_gpe(uint32_t mask) |
| 242 | { |
| 243 | uint32_t gpe0a_en = inl(ACPI_PMIO_BASE + GPE0_EN(0)); |
| 244 | gpe0a_en &= ~mask; |
| 245 | outl(gpe0a_en, ACPI_PMIO_BASE + GPE0_EN(0)); |
| 246 | } |
| 247 | |
| 248 | void disable_all_gpe(void) |
| 249 | { |
| 250 | disable_gpe(~0); |
| 251 | } |
| 252 | |
Shaunak Saha | d6bb549 | 2016-08-22 21:55:23 -0700 | [diff] [blame] | 253 | /* Clear the gpio gpe0 status bits in ACPI registers */ |
| 254 | void clear_gpi_gpe_sts(void) |
| 255 | { |
| 256 | int i; |
| 257 | |
| 258 | for (i = 1; i < GPE0_REG_MAX; i++) { |
| 259 | uint32_t gpe_sts = inl(ACPI_PMIO_BASE + GPE0_STS(i)); |
| 260 | outl(gpe_sts, ACPI_PMIO_BASE + GPE0_STS(i)); |
| 261 | } |
| 262 | } |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 263 | |
| 264 | static uint32_t reset_gpe_status(void) |
| 265 | { |
| 266 | uint32_t gpe_sts = inl(ACPI_PMIO_BASE + GPE0_STS(0)); |
| 267 | outl(gpe_sts, ACPI_PMIO_BASE + GPE0_STS(0)); |
| 268 | return gpe_sts; |
| 269 | } |
| 270 | |
| 271 | static uint32_t print_gpe_sts(uint32_t gpe_sts) |
| 272 | { |
| 273 | static const char * const gpe_sts_bits[] = { |
| 274 | [0] = "PCIE_SCI", |
| 275 | [2] = "SWGPE", |
| 276 | [3] = "PCIE_WAKE0", |
| 277 | [4] = "PUNIT", |
| 278 | [6] = "PCIE_WAKE1", |
| 279 | [7] = "PCIE_WAKE2", |
| 280 | [8] = "PCIE_WAKE3", |
| 281 | [9] = "PCI_EXP", |
| 282 | [10] = "BATLOW", |
| 283 | [11] = "CSE_PME", |
| 284 | [12] = "XDCI_PME", |
| 285 | [13] = "XHCI_PME", |
| 286 | [14] = "AVS_PME", |
| 287 | [15] = "GPIO_TIER1_SCI", |
| 288 | [16] = "SMB_WAK", |
| 289 | [17] = "SATA_PME", |
| 290 | }; |
| 291 | |
| 292 | if (!gpe_sts) |
| 293 | return gpe_sts; |
| 294 | |
| 295 | printk(BIOS_DEBUG, "GPE0a_STS: "); |
| 296 | print_num_status_bits(ARRAY_SIZE(gpe_sts_bits), gpe_sts, gpe_sts_bits); |
| 297 | printk(BIOS_DEBUG, "\n"); |
| 298 | |
| 299 | return gpe_sts; |
| 300 | } |
| 301 | |
| 302 | uint32_t clear_gpe_status(void) |
| 303 | { |
| 304 | return print_gpe_sts(reset_gpe_status()); |
| 305 | } |
| 306 | |
Duncan Laurie | 2e79009 | 2016-09-19 12:05:49 -0700 | [diff] [blame] | 307 | /* Read and clear GPE status (defined in arch/acpi.h) */ |
| 308 | int acpi_get_gpe(int gpe) |
| 309 | { |
| 310 | int bank; |
| 311 | uint32_t mask, sts; |
Duncan Laurie | 2f3736e | 2016-11-03 10:33:43 -0700 | [diff] [blame] | 312 | struct stopwatch sw; |
| 313 | int rc = 0; |
Duncan Laurie | 2e79009 | 2016-09-19 12:05:49 -0700 | [diff] [blame] | 314 | |
| 315 | if (gpe < 0 || gpe > GPE0_DW3_31) |
| 316 | return -1; |
| 317 | |
| 318 | bank = gpe / 32; |
| 319 | mask = 1 << (gpe % 32); |
| 320 | |
Duncan Laurie | 2f3736e | 2016-11-03 10:33:43 -0700 | [diff] [blame] | 321 | /* Wait up to 1ms for GPE status to clear */ |
| 322 | stopwatch_init_msecs_expire(&sw, 1); |
| 323 | do { |
| 324 | if (stopwatch_expired(&sw)) |
| 325 | return rc; |
| 326 | |
| 327 | sts = inl(ACPI_PMIO_BASE + GPE0_STS(bank)); |
| 328 | if (sts & mask) { |
| 329 | outl(mask, ACPI_PMIO_BASE + GPE0_STS(bank)); |
| 330 | rc = 1; |
| 331 | } |
| 332 | } while (sts & mask); |
| 333 | |
| 334 | return rc; |
Duncan Laurie | 2e79009 | 2016-09-19 12:05:49 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 337 | void clear_pmc_status(void) |
| 338 | { |
| 339 | uint32_t prsts; |
| 340 | uint32_t gen_pmcon1; |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 341 | uintptr_t pmc_bar0 = read_pmc_mmio_bar(); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 342 | |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 343 | prsts = read32((void *)(pmc_bar0 + PRSTS)); |
| 344 | gen_pmcon1 = read32((void *)(pmc_bar0 + GEN_PMCON1)); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 345 | |
| 346 | /* Clear the status bits. The RPS field is cleared on a 0 write. */ |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 347 | write32((void *)(pmc_bar0 + GEN_PMCON1), gen_pmcon1 & ~RPS); |
| 348 | write32((void *)(pmc_bar0 + PRSTS), prsts); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
| 352 | /* Return 0, 3, or 5 to indicate the previous sleep state. */ |
| 353 | int chipset_prev_sleep_state(struct chipset_power_state *ps) |
| 354 | { |
| 355 | /* Default to S0. */ |
Aaron Durbin | ed35b7c | 2016-07-13 23:17:38 -0500 | [diff] [blame] | 356 | int prev_sleep_state = ACPI_S0; |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 357 | |
| 358 | if (ps->pm1_sts & WAK_STS) { |
Aaron Durbin | ed35b7c | 2016-07-13 23:17:38 -0500 | [diff] [blame] | 359 | switch (acpi_sleep_from_pm1(ps->pm1_cnt)) { |
| 360 | case ACPI_S3: |
| 361 | if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) |
| 362 | prev_sleep_state = ACPI_S3; |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 363 | break; |
Aaron Durbin | ed35b7c | 2016-07-13 23:17:38 -0500 | [diff] [blame] | 364 | case ACPI_S5: |
| 365 | prev_sleep_state = ACPI_S5; |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 366 | break; |
| 367 | } |
Hannah Williams | 5992afa | 2016-06-23 09:50:28 -0700 | [diff] [blame] | 368 | |
| 369 | /* Clear SLP_TYP. */ |
| 370 | outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_PMIO_BASE + PM1_CNT); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 371 | } |
| 372 | return prev_sleep_state; |
| 373 | } |
| 374 | |
Shaunak Saha | 60b4618 | 2016-08-02 17:25:13 -0700 | [diff] [blame] | 375 | /* |
| 376 | * This function re-writes the gpe0 register values in power state |
| 377 | * cbmem variable. After system wakes from sleep state internal PMC logic |
| 378 | * writes default values in GPE_CFG register which gives a wrong offset to |
| 379 | * calculate the wake reason. So we need to set it again to the routing |
| 380 | * table as per the devicetree. |
| 381 | */ |
| 382 | void fixup_power_state(void) |
| 383 | { |
| 384 | int i; |
| 385 | struct chipset_power_state *ps; |
| 386 | |
| 387 | ps = cbmem_find(CBMEM_ID_POWER_STATE); |
| 388 | if (ps == NULL) |
| 389 | return; |
| 390 | |
| 391 | for (i = 0; i < GPE0_REG_MAX; i++) { |
| 392 | ps->gpe0_sts[i] = inl(ACPI_PMIO_BASE + GPE0_STS(i)); |
| 393 | ps->gpe0_en[i] = inl(ACPI_PMIO_BASE + GPE0_EN(i)); |
| 394 | printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n", |
| 395 | i, ps->gpe0_sts[i], i, ps->gpe0_en[i]); |
| 396 | } |
| 397 | } |
| 398 | |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 399 | /* returns prev_sleep_state */ |
| 400 | int fill_power_state(struct chipset_power_state *ps) |
| 401 | { |
| 402 | int i; |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 403 | uintptr_t pmc_bar0 = read_pmc_mmio_bar(); |
| 404 | |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 405 | ps->pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS); |
| 406 | ps->pm1_en = inw(ACPI_PMIO_BASE + PM1_EN); |
| 407 | ps->pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT); |
| 408 | ps->tco_sts = inl(ACPI_PMIO_BASE + TCO_STS); |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 409 | ps->prsts = read32((void *)(pmc_bar0 + PRSTS)); |
Lee Leahy | 320b7ca | 2017-03-09 09:42:48 -0800 | [diff] [blame^] | 410 | ps->gen_pmcon1 = read32((void *)(pmc_bar0 + GEN_PMCON1)); |
Alexandru Gagniuc | a633980 | 2016-04-05 12:40:24 -0700 | [diff] [blame] | 411 | ps->gen_pmcon2 = read32((void *)(pmc_bar0 + GEN_PMCON2)); |
| 412 | ps->gen_pmcon3 = read32((void *)(pmc_bar0 + GEN_PMCON3)); |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 413 | |
| 414 | ps->prev_sleep_state = chipset_prev_sleep_state(ps); |
| 415 | |
| 416 | printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n", |
| 417 | ps->pm1_sts, ps->pm1_en, ps->pm1_cnt); |
| 418 | printk(BIOS_DEBUG, "prsts: %08x tco_sts: %08x\n", |
| 419 | ps->prsts, ps->tco_sts); |
| 420 | printk(BIOS_DEBUG, |
| 421 | "gen_pmcon1: %08x gen_pmcon2: %08x gen_pmcon3: %08x\n", |
| 422 | ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3); |
| 423 | printk(BIOS_DEBUG, "smi_en: %08x smi_sts: %08x\n", |
| 424 | inl(ACPI_PMIO_BASE + SMI_EN), inl(ACPI_PMIO_BASE + SMI_STS)); |
Lee Leahy | 320b7ca | 2017-03-09 09:42:48 -0800 | [diff] [blame^] | 425 | for (i = 0; i < GPE0_REG_MAX; i++) { |
Hannah Williams | 01bc897 | 2016-02-04 20:13:34 -0800 | [diff] [blame] | 426 | ps->gpe0_sts[i] = inl(ACPI_PMIO_BASE + GPE0_STS(i)); |
| 427 | ps->gpe0_en[i] = inl(ACPI_PMIO_BASE + GPE0_EN(i)); |
| 428 | printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n", |
| 429 | i, ps->gpe0_sts[i], i, ps->gpe0_en[i]); |
| 430 | } |
| 431 | printk(BIOS_DEBUG, "prev_sleep_state %d\n", ps->prev_sleep_state); |
| 432 | return ps->prev_sleep_state; |
| 433 | } |
Aaron Durbin | bef75e7 | 2016-05-26 11:00:44 -0500 | [diff] [blame] | 434 | |
| 435 | int vboot_platform_is_resuming(void) |
| 436 | { |
Aaron Durbin | bef75e7 | 2016-05-26 11:00:44 -0500 | [diff] [blame] | 437 | if (!(inw(ACPI_PMIO_BASE + PM1_STS) & WAK_STS)) |
| 438 | return 0; |
| 439 | |
Aaron Durbin | ed35b7c | 2016-07-13 23:17:38 -0500 | [diff] [blame] | 440 | return acpi_sleep_from_pm1(inl(ACPI_PMIO_BASE + PM1_CNT)) == ACPI_S3; |
Aaron Durbin | bef75e7 | 2016-05-26 11:00:44 -0500 | [diff] [blame] | 441 | } |
Andrey Petrov | 0f593c2 | 2016-06-17 15:30:13 -0700 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * If possible, lock 0xcf9. Once the register is locked, it can't be changed. |
| 445 | * This lock is reset on cold boot, hard reset, soft reset and Sx. |
| 446 | */ |
| 447 | void global_reset_lock(void) |
| 448 | { |
| 449 | uintptr_t etr = read_pmc_mmio_bar() + ETR; |
| 450 | uint32_t reg; |
| 451 | |
| 452 | reg = read32((void *)etr); |
| 453 | if (reg & CF9_LOCK) |
| 454 | return; |
| 455 | reg |= CF9_LOCK; |
| 456 | write32((void *)etr, reg); |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Enable or disable global reset. If global reset is enabled, hard reset and |
| 461 | * soft reset will trigger global reset, where both host and TXE are reset. |
| 462 | * This is cleared on cold boot, hard reset, soft reset and Sx. |
| 463 | */ |
| 464 | void global_reset_enable(bool enable) |
| 465 | { |
| 466 | uintptr_t etr = read_pmc_mmio_bar() + ETR; |
| 467 | uint32_t reg; |
| 468 | |
| 469 | reg = read32((void *)etr); |
| 470 | reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST; |
| 471 | write32((void *)etr, reg); |
| 472 | } |
Furquan Shaikh | 4c1cb42 | 2016-06-23 14:00:05 -0700 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * The PM1 control is set to S5 when vboot requests a reboot because the power |
| 476 | * state code above may not have collected its data yet. Therefore, set it to |
| 477 | * S5 when vboot requests a reboot. That's necessary if vboot fails in the |
| 478 | * resume path and requests a reboot. This prevents a reboot loop where the |
| 479 | * error is continually hit on the failing vboot resume path. |
| 480 | */ |
| 481 | void vboot_platform_prepare_reboot(void) |
| 482 | { |
| 483 | const uint16_t port = ACPI_PMIO_BASE + PM1_CNT; |
| 484 | outl((inl(port) & ~(SLP_TYP)) | (SLP_TYP_S5 << SLP_TYP_SHIFT), port); |
| 485 | } |
Aaron Durbin | c2b7779 | 2016-07-14 00:26:50 -0500 | [diff] [blame] | 486 | |
| 487 | void poweroff(void) |
| 488 | { |
| 489 | enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT)); |
Furquan Shaikh | 3828e55 | 2016-08-18 21:31:50 -0700 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM |
| 493 | * to transition to S5 state. If halt is called in SMM, then it prevents |
| 494 | * the SMI handler from being triggered and system never enters S5. |
| 495 | */ |
| 496 | if (!ENV_SMM) |
| 497 | halt(); |
Aaron Durbin | c2b7779 | 2016-07-14 00:26:50 -0500 | [diff] [blame] | 498 | } |
Duncan Laurie | a673d1c | 2016-09-19 12:02:54 -0700 | [diff] [blame] | 499 | |
| 500 | void pmc_gpe_init(void) |
| 501 | { |
| 502 | uint32_t gpio_cfg = 0; |
| 503 | uint32_t gpio_cfg_reg; |
| 504 | uint8_t dw1, dw2, dw3; |
| 505 | ROMSTAGE_CONST struct soc_intel_apollolake_config *config; |
| 506 | |
| 507 | /* Look up the device in devicetree */ |
| 508 | ROMSTAGE_CONST struct device *dev = dev_find_slot(0, NB_DEVFN); |
| 509 | if (!dev || !dev->chip_info) { |
| 510 | printk(BIOS_ERR, "BUG! Could not find SOC devicetree config\n"); |
| 511 | return; |
| 512 | } |
| 513 | config = dev->chip_info; |
| 514 | |
| 515 | uintptr_t pmc_bar = get_pmc_mmio_bar(); |
| 516 | |
| 517 | const uint32_t gpio_cfg_mask = |
| 518 | (GPE0_DWX_MASK << GPE0_DW1_SHIFT) | |
| 519 | (GPE0_DWX_MASK << GPE0_DW2_SHIFT) | |
| 520 | (GPE0_DWX_MASK << GPE0_DW3_SHIFT); |
| 521 | |
| 522 | /* Assign to local variable */ |
| 523 | dw1 = config->gpe0_dw1; |
| 524 | dw2 = config->gpe0_dw2; |
| 525 | dw3 = config->gpe0_dw3; |
| 526 | |
| 527 | /* Making sure that bad values don't bleed into the other fields */ |
| 528 | dw1 &= GPE0_DWX_MASK; |
| 529 | dw2 &= GPE0_DWX_MASK; |
| 530 | dw3 &= GPE0_DWX_MASK; |
| 531 | |
| 532 | /* Route the GPIOs to the GPE0 block. Determine that all values |
| 533 | * are different, and if they aren't use the reset values. |
| 534 | * DW0 is reserved/unused */ |
| 535 | if (dw1 == dw2 || dw2 == dw3) { |
| 536 | printk(BIOS_INFO, "PMC: Using default GPE route.\n"); |
| 537 | gpio_cfg = read32((void *)pmc_bar + GPIO_GPE_CFG); |
| 538 | |
| 539 | dw1 = (gpio_cfg >> GPE0_DW1_SHIFT) & GPE0_DWX_MASK; |
| 540 | dw2 = (gpio_cfg >> GPE0_DW2_SHIFT) & GPE0_DWX_MASK; |
| 541 | dw3 = (gpio_cfg >> GPE0_DW3_SHIFT) & GPE0_DWX_MASK; |
| 542 | } else { |
| 543 | gpio_cfg |= (uint32_t)dw1 << GPE0_DW1_SHIFT; |
| 544 | gpio_cfg |= (uint32_t)dw2 << GPE0_DW2_SHIFT; |
| 545 | gpio_cfg |= (uint32_t)dw3 << GPE0_DW3_SHIFT; |
| 546 | } |
| 547 | |
| 548 | gpio_cfg_reg = read32((void *)pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask; |
| 549 | gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask; |
| 550 | |
| 551 | write32((void *)pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg); |
| 552 | |
| 553 | /* Set the routes in the GPIO communities as well. */ |
| 554 | gpio_route_gpe(dw1, dw2, dw3); |
| 555 | } |
Andrey Petrov | 3b63753 | 2016-11-30 17:39:16 -0800 | [diff] [blame] | 556 | |
| 557 | void enable_pm_timer_emulation(void) |
| 558 | { |
| 559 | /* ACPI PM timer emulation */ |
| 560 | msr_t msr; |
| 561 | /* |
| 562 | * The derived frequency is calculated as follows: |
| 563 | * (CTC_FREQ * msr[63:32]) >> 32 = target frequency. |
| 564 | * Back solve the multiplier so the 3.579545MHz ACPI timer |
| 565 | * frequency is used. |
| 566 | */ |
| 567 | msr.hi = (3579545ULL << 32) / CTC_FREQ; |
| 568 | /* Set PM1 timer IO port and enable*/ |
| 569 | msr.lo = EMULATE_PM_TMR_EN | (ACPI_PMIO_BASE + R_ACPI_PM1_TMR); |
| 570 | wrmsr(MSR_EMULATE_PM_TMR, msr); |
| 571 | } |