Angel Pons | ba38f37 | 2020-04-05 15:46:45 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi.h> |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 4 | #include <arch/io.h> |
Frans Hendriks | 2098963 | 2019-05-16 11:46:22 +0200 | [diff] [blame] | 5 | #include <assert.h> |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 6 | #include <bootmode.h> |
Kyösti Mälkki | 21d6a27 | 2019-11-05 18:50:38 +0200 | [diff] [blame] | 7 | #include <device/device.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 8 | #include <device/mmio.h> |
Kyösti Mälkki | 21d6a27 | 2019-11-05 18:50:38 +0200 | [diff] [blame] | 9 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 10 | #include <device/pci_ops.h> |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 11 | #include <console/console.h> |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 12 | #include <soc/iomap.h> |
| 13 | #include <soc/lpc.h> |
| 14 | #include <soc/pci_devs.h> |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 15 | #include <soc/pm.h> |
| 16 | #include <stdint.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 17 | #include <security/vboot/vbnv.h> |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 18 | |
Aaron Durbin | b19e33f | 2017-09-15 14:32:13 -0600 | [diff] [blame] | 19 | #if defined(__SIMPLE_DEVICE__) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 20 | |
Elyes HAOUAS | c8a649c | 2018-06-10 23:36:44 +0200 | [diff] [blame] | 21 | static const pci_devfn_t pcu_dev = PCI_DEV(0, PCU_DEV, 0); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 22 | |
Elyes HAOUAS | c8a649c | 2018-06-10 23:36:44 +0200 | [diff] [blame] | 23 | static inline pci_devfn_t get_pcu_dev(void) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 24 | { |
| 25 | return pcu_dev; |
| 26 | } |
| 27 | |
Kyösti Mälkki | 21d6a27 | 2019-11-05 18:50:38 +0200 | [diff] [blame] | 28 | #else /* __SIMPLE_DEVICE__ */ |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 29 | |
Elyes HAOUAS | c8a649c | 2018-06-10 23:36:44 +0200 | [diff] [blame] | 30 | static struct device *pcu_dev; |
| 31 | static struct device *get_pcu_dev(void) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 32 | { |
| 33 | if (pcu_dev == NULL) |
Kyösti Mälkki | c70eed1 | 2018-05-22 02:18:00 +0300 | [diff] [blame] | 34 | pcu_dev = pcidev_on_root(PCU_DEV, 0); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 35 | return pcu_dev; |
| 36 | } |
Kyösti Mälkki | 21d6a27 | 2019-11-05 18:50:38 +0200 | [diff] [blame] | 37 | #endif /* __SIMPLE_DEVICE__ */ |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 38 | |
| 39 | uint16_t get_pmbase(void) |
| 40 | { |
| 41 | return pci_read_config16(get_pcu_dev(), ABASE) & 0xfff8; |
| 42 | } |
| 43 | |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 44 | static void print_num_status_bits(int num_bits, uint32_t status, const char *const bit_names[]) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | |
| 48 | if (!status) |
| 49 | return; |
| 50 | |
| 51 | for (i = num_bits - 1; i >= 0; i--) { |
| 52 | if (status & (1 << i)) { |
| 53 | if (bit_names[i]) |
| 54 | printk(BIOS_DEBUG, "%s ", bit_names[i]); |
| 55 | else |
| 56 | printk(BIOS_DEBUG, "BIT%d ", i); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 61 | static uint32_t print_smi_status(uint32_t smi_sts) |
| 62 | { |
Elyes HAOUAS | 39303d5 | 2018-07-08 12:40:45 +0200 | [diff] [blame] | 63 | static const char *const smi_sts_bits[] = { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 64 | [2] = "BIOS", |
| 65 | [4] = "SLP_SMI", |
| 66 | [5] = "APM", |
| 67 | [6] = "SWSMI_TMR", |
| 68 | [8] = "PM1", |
| 69 | [9] = "GPE0", |
| 70 | [12] = "DEVMON", |
| 71 | [13] = "TCO", |
| 72 | [14] = "PERIODIC", |
| 73 | [15] = "ILB", |
| 74 | [16] = "SMBUS_SMI", |
| 75 | [17] = "LEGACY_USB2", |
| 76 | [18] = "INTEL_USB2", |
| 77 | [20] = "PCI_EXP_SMI", |
| 78 | [26] = "SPI", |
| 79 | [28] = "PUNIT", |
| 80 | [29] = "GUNIT", |
| 81 | }; |
| 82 | |
| 83 | if (!smi_sts) |
| 84 | return 0; |
| 85 | |
| 86 | printk(BIOS_DEBUG, "SMI_STS: "); |
Ravi Sarawadi | d077b58 | 2015-09-09 14:12:16 -0700 | [diff] [blame] | 87 | print_num_status_bits(30, smi_sts, smi_sts_bits); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 88 | printk(BIOS_DEBUG, "\n"); |
| 89 | |
| 90 | return smi_sts; |
| 91 | } |
| 92 | |
| 93 | static uint32_t reset_smi_status(void) |
| 94 | { |
| 95 | uint16_t pmbase = get_pmbase(); |
| 96 | uint32_t smi_sts = inl(pmbase + SMI_STS); |
| 97 | outl(smi_sts, pmbase + SMI_STS); |
| 98 | return smi_sts; |
| 99 | } |
| 100 | |
| 101 | uint32_t clear_smi_status(void) |
| 102 | { |
| 103 | return print_smi_status(reset_smi_status()); |
| 104 | } |
| 105 | |
| 106 | void enable_smi(uint32_t mask) |
| 107 | { |
| 108 | uint16_t pmbase = get_pmbase(); |
| 109 | uint32_t smi_en = inl(pmbase + SMI_EN); |
| 110 | smi_en |= mask; |
| 111 | outl(smi_en, pmbase + SMI_EN); |
| 112 | } |
| 113 | |
| 114 | void disable_smi(uint32_t mask) |
| 115 | { |
| 116 | uint16_t pmbase = get_pmbase(); |
| 117 | uint32_t smi_en = inl(pmbase + SMI_EN); |
| 118 | smi_en &= ~mask; |
| 119 | outl(smi_en, pmbase + SMI_EN); |
| 120 | } |
| 121 | |
| 122 | void enable_pm1_control(uint32_t mask) |
| 123 | { |
| 124 | uint16_t pmbase = get_pmbase(); |
| 125 | uint32_t pm1_cnt = inl(pmbase + PM1_CNT); |
| 126 | pm1_cnt |= mask; |
| 127 | outl(pm1_cnt, pmbase + PM1_CNT); |
| 128 | } |
| 129 | |
| 130 | void disable_pm1_control(uint32_t mask) |
| 131 | { |
| 132 | uint16_t pmbase = get_pmbase(); |
| 133 | uint32_t pm1_cnt = inl(pmbase + PM1_CNT); |
| 134 | pm1_cnt &= ~mask; |
| 135 | outl(pm1_cnt, pmbase + PM1_CNT); |
| 136 | } |
| 137 | |
| 138 | static uint16_t reset_pm1_status(void) |
| 139 | { |
| 140 | uint16_t pmbase = get_pmbase(); |
| 141 | uint16_t pm1_sts = inw(pmbase + PM1_STS); |
| 142 | outw(pm1_sts, pmbase + PM1_STS); |
| 143 | return pm1_sts; |
| 144 | } |
| 145 | |
| 146 | static uint16_t print_pm1_status(uint16_t pm1_sts) |
| 147 | { |
Elyes HAOUAS | 39303d5 | 2018-07-08 12:40:45 +0200 | [diff] [blame] | 148 | static const char *const pm1_sts_bits[] = { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 149 | [0] = "TMROF", |
| 150 | [5] = "GBL", |
| 151 | [8] = "PWRBTN", |
| 152 | [10] = "RTC", |
| 153 | [11] = "PRBTNOR", |
| 154 | [13] = "USB", |
| 155 | [14] = "PCIEXPWAK", |
| 156 | [15] = "WAK", |
| 157 | }; |
| 158 | |
| 159 | if (!pm1_sts) |
| 160 | return 0; |
| 161 | |
| 162 | printk(BIOS_SPEW, "PM1_STS: "); |
Ravi Sarawadi | d077b58 | 2015-09-09 14:12:16 -0700 | [diff] [blame] | 163 | print_num_status_bits(16, pm1_sts, pm1_sts_bits); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 164 | printk(BIOS_SPEW, "\n"); |
| 165 | |
| 166 | return pm1_sts; |
| 167 | } |
| 168 | |
| 169 | uint16_t clear_pm1_status(void) |
| 170 | { |
| 171 | return print_pm1_status(reset_pm1_status()); |
| 172 | } |
| 173 | |
| 174 | void enable_pm1(uint16_t events) |
| 175 | { |
| 176 | outw(events, get_pmbase() + PM1_EN); |
| 177 | } |
| 178 | |
| 179 | static uint32_t print_tco_status(uint32_t tco_sts) |
| 180 | { |
Elyes HAOUAS | 39303d5 | 2018-07-08 12:40:45 +0200 | [diff] [blame] | 181 | static const char *const tco_sts_bits[] = { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 182 | [3] = "TIMEOUT", |
| 183 | [17] = "SECOND_TO", |
| 184 | }; |
| 185 | |
| 186 | if (!tco_sts) |
| 187 | return 0; |
| 188 | |
| 189 | printk(BIOS_DEBUG, "TCO_STS: "); |
Ravi Sarawadi | d077b58 | 2015-09-09 14:12:16 -0700 | [diff] [blame] | 190 | print_num_status_bits(18, tco_sts, tco_sts_bits); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 191 | printk(BIOS_DEBUG, "\n"); |
| 192 | |
| 193 | return tco_sts; |
| 194 | } |
| 195 | |
| 196 | static uint32_t reset_tco_status(void) |
| 197 | { |
| 198 | uint16_t pmbase = get_pmbase(); |
| 199 | uint32_t tco_sts = inl(pmbase + TCO_STS); |
| 200 | uint32_t tco_en = inl(pmbase + TCO1_CNT); |
| 201 | |
| 202 | outl(tco_sts, pmbase + TCO_STS); |
| 203 | return tco_sts & tco_en; |
| 204 | } |
| 205 | |
| 206 | uint32_t clear_tco_status(void) |
| 207 | { |
| 208 | return print_tco_status(reset_tco_status()); |
| 209 | } |
| 210 | |
| 211 | void enable_gpe(uint32_t mask) |
| 212 | { |
| 213 | uint16_t pmbase = get_pmbase(); |
| 214 | uint32_t gpe0_en = inl(pmbase + GPE0_EN); |
| 215 | gpe0_en |= mask; |
| 216 | outl(gpe0_en, pmbase + GPE0_EN); |
| 217 | } |
| 218 | |
| 219 | void disable_gpe(uint32_t mask) |
| 220 | { |
| 221 | uint16_t pmbase = get_pmbase(); |
| 222 | uint32_t gpe0_en = inl(pmbase + GPE0_EN); |
| 223 | gpe0_en &= ~mask; |
| 224 | outl(gpe0_en, pmbase + GPE0_EN); |
| 225 | } |
| 226 | |
| 227 | void disable_all_gpe(void) |
| 228 | { |
| 229 | disable_gpe(~0); |
| 230 | } |
| 231 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 232 | static uint32_t reset_gpe_status(void) |
| 233 | { |
| 234 | uint16_t pmbase = get_pmbase(); |
| 235 | uint32_t gpe_sts = inl(pmbase + GPE0_STS); |
| 236 | outl(gpe_sts, pmbase + GPE0_STS); |
| 237 | return gpe_sts; |
| 238 | } |
| 239 | |
| 240 | static uint32_t print_gpe_sts(uint32_t gpe_sts) |
| 241 | { |
Elyes HAOUAS | 39303d5 | 2018-07-08 12:40:45 +0200 | [diff] [blame] | 242 | static const char *const gpe_sts_bits[] = { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 243 | [1] = "HOTPLUG", |
| 244 | [2] = "SWGPE", |
| 245 | [3] = "PCIE_WAKE0", |
| 246 | [4] = "PUNIT", |
| 247 | [5] = "GUNIT", |
| 248 | [6] = "PCIE_WAKE1", |
| 249 | [7] = "PCIE_WAKE2", |
| 250 | [8] = "PCIE_WAKE3", |
| 251 | [9] = "PCI_EXP", |
| 252 | [10] = "BATLOW", |
| 253 | [13] = "PME_B0", |
| 254 | [16] = "SUS_GPIO_0", |
| 255 | [17] = "SUS_GPIO_1", |
| 256 | [18] = "SUS_GPIO_2", |
| 257 | [19] = "SUS_GPIO_3", |
| 258 | [20] = "SUS_GPIO_4", |
| 259 | [21] = "SUS_GPIO_5", |
| 260 | [22] = "SUS_GPIO_6", |
| 261 | [23] = "SUS_GPIO_7", |
| 262 | [24] = "CORE_GPIO_0", |
| 263 | [25] = "CORE_GPIO_1", |
| 264 | [26] = "CORE_GPIO_2", |
| 265 | [27] = "CORE_GPIO_3", |
| 266 | [28] = "CORE_GPIO_4", |
| 267 | [29] = "CORE_GPIO_5", |
| 268 | [30] = "CORE_GPIO_6", |
| 269 | [31] = "CORE_GPIO_7", |
| 270 | }; |
| 271 | |
| 272 | if (!gpe_sts) |
| 273 | return gpe_sts; |
| 274 | |
| 275 | printk(BIOS_DEBUG, "GPE0a_STS: "); |
Ravi Sarawadi | d077b58 | 2015-09-09 14:12:16 -0700 | [diff] [blame] | 276 | print_num_status_bits(32, gpe_sts, gpe_sts_bits); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 277 | printk(BIOS_DEBUG, "\n"); |
| 278 | |
| 279 | return gpe_sts; |
| 280 | } |
| 281 | |
| 282 | uint32_t clear_gpe_status(void) |
| 283 | { |
| 284 | return print_gpe_sts(reset_gpe_status()); |
| 285 | } |
| 286 | |
| 287 | static uint32_t reset_alt_status(void) |
| 288 | { |
| 289 | uint16_t pmbase = get_pmbase(); |
| 290 | uint32_t alt_gpio_smi = inl(pmbase + ALT_GPIO_SMI); |
| 291 | outl(alt_gpio_smi, pmbase + ALT_GPIO_SMI); |
| 292 | return alt_gpio_smi; |
| 293 | } |
| 294 | |
| 295 | static uint32_t print_alt_sts(uint32_t alt_gpio_smi) |
| 296 | { |
| 297 | uint32_t alt_gpio_sts; |
Elyes HAOUAS | 39303d5 | 2018-07-08 12:40:45 +0200 | [diff] [blame] | 298 | static const char *const alt_gpio_smi_sts_bits[] = { |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 299 | [0] = "SUS_GPIO_0", |
| 300 | [1] = "SUS_GPIO_1", |
| 301 | [2] = "SUS_GPIO_2", |
| 302 | [3] = "SUS_GPIO_3", |
| 303 | [4] = "SUS_GPIO_4", |
| 304 | [5] = "SUS_GPIO_5", |
| 305 | [6] = "SUS_GPIO_6", |
| 306 | [7] = "SUS_GPIO_7", |
| 307 | [8] = "CORE_GPIO_0", |
| 308 | [9] = "CORE_GPIO_1", |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 309 | [10] = "CORE_GPIO_2", |
| 310 | [11] = "CORE_GPIO_3", |
| 311 | [12] = "CORE_GPIO_4", |
| 312 | [13] = "CORE_GPIO_5", |
| 313 | [14] = "CORE_GPIO_6", |
| 314 | [15] = "CORE_GPIO_7", |
| 315 | }; |
| 316 | |
| 317 | /* Status bits are in the upper 16 bits. */ |
| 318 | alt_gpio_sts = alt_gpio_smi >> 16; |
| 319 | if (!alt_gpio_sts) |
| 320 | return alt_gpio_smi; |
| 321 | |
| 322 | printk(BIOS_DEBUG, "ALT_GPIO_SMI: "); |
| 323 | print_num_status_bits(16, alt_gpio_sts, alt_gpio_smi_sts_bits); |
| 324 | printk(BIOS_DEBUG, "\n"); |
| 325 | |
| 326 | return alt_gpio_smi; |
| 327 | } |
| 328 | |
| 329 | uint32_t clear_alt_status(void) |
| 330 | { |
| 331 | return print_alt_sts(reset_alt_status()); |
| 332 | } |
| 333 | |
| 334 | void clear_pmc_status(void) |
| 335 | { |
| 336 | uint32_t prsts; |
| 337 | uint32_t gen_pmcon1; |
| 338 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 339 | prsts = read32((void *)(PMC_BASE_ADDRESS + PRSTS)); |
| 340 | gen_pmcon1 = read32((void *)(PMC_BASE_ADDRESS + GEN_PMCON1)); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 341 | |
| 342 | /* Clear the status bits. The RPS field is cleared on a 0 write. */ |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 343 | write32((void *)(PMC_BASE_ADDRESS + GEN_PMCON1), gen_pmcon1 & ~RPS); |
| 344 | write32((void *)(PMC_BASE_ADDRESS + PRSTS), prsts); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 345 | } |
Aaron Durbin | b19e33f | 2017-09-15 14:32:13 -0600 | [diff] [blame] | 346 | |
| 347 | int rtc_failure(void) |
| 348 | { |
| 349 | uint32_t gen_pmcon1; |
| 350 | int rtc_fail; |
Aaron Durbin | b19e33f | 2017-09-15 14:32:13 -0600 | [diff] [blame] | 351 | |
Frans Hendriks | 2098963 | 2019-05-16 11:46:22 +0200 | [diff] [blame] | 352 | /* not usable in ramstage as GEN_PMCON1 gets reset */ |
| 353 | if (ENV_RAMSTAGE) |
| 354 | dead_code(); |
| 355 | |
| 356 | gen_pmcon1 = read32((u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1)); |
Aaron Durbin | b19e33f | 2017-09-15 14:32:13 -0600 | [diff] [blame] | 357 | |
| 358 | rtc_fail = !!(gen_pmcon1 & RPS); |
Aaron Durbin | b19e33f | 2017-09-15 14:32:13 -0600 | [diff] [blame] | 359 | if (rtc_fail) |
| 360 | printk(BIOS_DEBUG, "RTC failure.\n"); |
| 361 | |
| 362 | return rtc_fail; |
| 363 | } |
Aaron Durbin | 0990fbf | 2017-09-15 15:23:04 -0600 | [diff] [blame] | 364 | |
| 365 | int vbnv_cmos_failed(void) |
| 366 | { |
| 367 | return rtc_failure(); |
| 368 | } |
Joel Kitching | 1d93b88 | 2018-09-26 17:58:14 +0800 | [diff] [blame] | 369 | |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 370 | int platform_is_resuming(void) |
Joel Kitching | 1d93b88 | 2018-09-26 17:58:14 +0800 | [diff] [blame] | 371 | { |
| 372 | if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS)) |
| 373 | return 0; |
| 374 | |
| 375 | return acpi_sleep_from_pm1(inl(ACPI_BASE_ADDRESS + PM1_CNT)) == ACPI_S3; |
| 376 | } |