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