Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Helper functions for dealing with power management registers |
| 18 | * and the differences between PCH variants. |
| 19 | */ |
| 20 | |
| 21 | #include <arch/io.h> |
| 22 | #include <device/device.h> |
| 23 | #include <device/pci.h> |
| 24 | #include <device/pci_def.h> |
| 25 | #include <console/console.h> |
Julius Werner | 4ee4bd5 | 2014-10-20 13:46:39 -0700 | [diff] [blame] | 26 | #include <soc/iomap.h> |
| 27 | #include <soc/lpc.h> |
| 28 | #include <soc/pci_devs.h> |
| 29 | #include <soc/pm.h> |
| 30 | #include <soc/gpio.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 31 | #include <security/vboot/vbnv.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 32 | |
| 33 | /* Print status bits with descriptive names */ |
| 34 | static void print_status_bits(u32 status, const char *bit_names[]) |
| 35 | { |
| 36 | int i; |
| 37 | |
| 38 | if (!status) |
| 39 | return; |
| 40 | |
Lee Leahy | 26b7cd0 | 2017-03-16 18:47:55 -0700 | [diff] [blame] | 41 | for (i = 31; i >= 0; i--) { |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 42 | if (status & (1 << i)) { |
| 43 | if (bit_names[i]) |
| 44 | printk(BIOS_DEBUG, "%s ", bit_names[i]); |
| 45 | else |
| 46 | printk(BIOS_DEBUG, "BIT%d ", i); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* Print status bits as GPIO numbers */ |
| 52 | static void print_gpio_status(u32 status, int start) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | if (!status) |
| 57 | return; |
| 58 | |
Lee Leahy | 26b7cd0 | 2017-03-16 18:47:55 -0700 | [diff] [blame] | 59 | for (i = 31; i >= 0; i--) { |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 60 | if (status & (1 << i)) |
| 61 | printk(BIOS_DEBUG, "GPIO%d ", start + i); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * PM1_CNT |
| 68 | */ |
| 69 | |
| 70 | /* Enable events in PM1 control register */ |
| 71 | void enable_pm1_control(u32 mask) |
| 72 | { |
| 73 | u32 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT); |
| 74 | pm1_cnt |= mask; |
| 75 | outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT); |
| 76 | } |
| 77 | |
| 78 | /* Disable events in PM1 control register */ |
| 79 | void disable_pm1_control(u32 mask) |
| 80 | { |
| 81 | u32 pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT); |
| 82 | pm1_cnt &= ~mask; |
| 83 | outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * PM1 |
| 89 | */ |
| 90 | |
| 91 | /* Clear and return PM1 status register */ |
| 92 | static u16 reset_pm1_status(void) |
| 93 | { |
| 94 | u16 pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS); |
| 95 | outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS); |
| 96 | return pm1_sts; |
| 97 | } |
| 98 | |
| 99 | /* Print PM1 status bits */ |
| 100 | static u16 print_pm1_status(u16 pm1_sts) |
| 101 | { |
| 102 | const char *pm1_sts_bits[] = { |
| 103 | [0] = "TMROF", |
| 104 | [4] = "BM", |
| 105 | [5] = "GBL", |
| 106 | [8] = "PWRBTN", |
| 107 | [10] = "RTC", |
| 108 | [11] = "PRBTNOR", |
| 109 | [14] = "PCIEXPWAK", |
| 110 | [15] = "WAK", |
| 111 | }; |
| 112 | |
| 113 | if (!pm1_sts) |
| 114 | return 0; |
| 115 | |
| 116 | printk(BIOS_SPEW, "PM1_STS: "); |
| 117 | print_status_bits(pm1_sts, pm1_sts_bits); |
| 118 | printk(BIOS_SPEW, "\n"); |
| 119 | |
| 120 | return pm1_sts; |
| 121 | } |
| 122 | |
| 123 | /* Print, clear, and return PM1 status */ |
| 124 | u16 clear_pm1_status(void) |
| 125 | { |
| 126 | return print_pm1_status(reset_pm1_status()); |
| 127 | } |
| 128 | |
| 129 | /* Set the PM1 register to events */ |
| 130 | void enable_pm1(u16 events) |
| 131 | { |
| 132 | outw(events, ACPI_BASE_ADDRESS + PM1_EN); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /* |
| 137 | * SMI |
| 138 | */ |
| 139 | |
| 140 | /* Clear and return SMI status register */ |
| 141 | static u32 reset_smi_status(void) |
| 142 | { |
| 143 | u32 smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS); |
| 144 | outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS); |
| 145 | return smi_sts; |
| 146 | } |
| 147 | |
| 148 | /* Print SMI status bits */ |
| 149 | static u32 print_smi_status(u32 smi_sts) |
| 150 | { |
| 151 | const char *smi_sts_bits[] = { |
| 152 | [2] = "BIOS", |
| 153 | [3] = "LEGACY_USB", |
| 154 | [4] = "SLP_SMI", |
| 155 | [5] = "APM", |
| 156 | [6] = "SWSMI_TMR", |
| 157 | [8] = "PM1", |
| 158 | [9] = "GPE0", |
| 159 | [10] = "GPI", |
| 160 | [11] = "MCSMI", |
| 161 | [12] = "DEVMON", |
| 162 | [13] = "TCO", |
| 163 | [14] = "PERIODIC", |
| 164 | [15] = "SERIRQ_SMI", |
| 165 | [16] = "SMBUS_SMI", |
| 166 | [17] = "LEGACY_USB2", |
| 167 | [18] = "INTEL_USB2", |
| 168 | [20] = "PCI_EXP_SMI", |
| 169 | [21] = "MONITOR", |
| 170 | [26] = "SPI", |
| 171 | [27] = "GPIO_UNLOCK" |
| 172 | }; |
| 173 | |
| 174 | if (!smi_sts) |
| 175 | return 0; |
| 176 | |
| 177 | printk(BIOS_DEBUG, "SMI_STS: "); |
| 178 | print_status_bits(smi_sts, smi_sts_bits); |
| 179 | printk(BIOS_DEBUG, "\n"); |
| 180 | |
| 181 | return smi_sts; |
| 182 | } |
| 183 | |
| 184 | /* Print, clear, and return SMI status */ |
| 185 | u32 clear_smi_status(void) |
| 186 | { |
| 187 | return print_smi_status(reset_smi_status()); |
| 188 | } |
| 189 | |
| 190 | /* Enable SMI event */ |
| 191 | void enable_smi(u32 mask) |
| 192 | { |
| 193 | u32 smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN); |
| 194 | smi_en |= mask; |
| 195 | outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN); |
| 196 | } |
| 197 | |
| 198 | /* Disable SMI event */ |
| 199 | void disable_smi(u32 mask) |
| 200 | { |
| 201 | u32 smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN); |
| 202 | smi_en &= ~mask; |
| 203 | outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * ALT_GP_SMI |
| 209 | */ |
| 210 | |
| 211 | /* Clear GPIO SMI status and return events that are enabled and active */ |
| 212 | static u32 reset_alt_smi_status(void) |
| 213 | { |
| 214 | u32 alt_sts, alt_en; |
| 215 | |
| 216 | /* Low Power variant moves this to GPIO region as dword */ |
| 217 | alt_sts = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_STS); |
| 218 | outl(alt_sts, GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_STS); |
| 219 | alt_en = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN); |
| 220 | |
| 221 | /* Only report enabled events */ |
| 222 | return alt_sts & alt_en; |
| 223 | } |
| 224 | |
| 225 | /* Print GPIO SMI status bits */ |
| 226 | static u32 print_alt_smi_status(u32 alt_sts) |
| 227 | { |
| 228 | if (!alt_sts) |
| 229 | return 0; |
| 230 | |
| 231 | printk(BIOS_DEBUG, "ALT_STS: "); |
| 232 | |
| 233 | /* First 16 events are GPIO 32-47 */ |
| 234 | print_gpio_status(alt_sts & 0xffff, 32); |
| 235 | |
| 236 | printk(BIOS_DEBUG, "\n"); |
| 237 | |
| 238 | return alt_sts; |
| 239 | } |
| 240 | |
| 241 | /* Print, clear, and return GPIO SMI status */ |
| 242 | u32 clear_alt_smi_status(void) |
| 243 | { |
| 244 | return print_alt_smi_status(reset_alt_smi_status()); |
| 245 | } |
| 246 | |
| 247 | /* Enable GPIO SMI events */ |
| 248 | void enable_alt_smi(u32 mask) |
| 249 | { |
| 250 | u32 alt_en; |
| 251 | |
| 252 | alt_en = inl(GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN); |
| 253 | alt_en |= mask; |
| 254 | outl(alt_en, GPIO_BASE_ADDRESS + GPIO_ALT_GPI_SMI_EN); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* |
| 259 | * TCO |
| 260 | */ |
| 261 | |
| 262 | /* Clear TCO status and return events that are enabled and active */ |
| 263 | static u32 reset_tco_status(void) |
| 264 | { |
| 265 | u32 tcobase = ACPI_BASE_ADDRESS + 0x60; |
| 266 | u32 tco_sts = inl(tcobase + 0x04); |
| 267 | u32 tco_en = inl(ACPI_BASE_ADDRESS + 0x68); |
| 268 | |
| 269 | /* Don't clear BOOT_STS before SECOND_TO_STS */ |
| 270 | outl(tco_sts & ~(1 << 18), tcobase + 0x04); |
| 271 | |
| 272 | /* Clear BOOT_STS */ |
| 273 | if (tco_sts & (1 << 18)) |
| 274 | outl(tco_sts & (1 << 18), tcobase + 0x04); |
| 275 | |
| 276 | return tco_sts & tco_en; |
| 277 | } |
| 278 | |
| 279 | /* Print TCO status bits */ |
| 280 | static u32 print_tco_status(u32 tco_sts) |
| 281 | { |
| 282 | const char *tco_sts_bits[] = { |
| 283 | [0] = "NMI2SMI", |
| 284 | [1] = "SW_TCO", |
| 285 | [2] = "TCO_INT", |
| 286 | [3] = "TIMEOUT", |
| 287 | [7] = "NEWCENTURY", |
| 288 | [8] = "BIOSWR", |
| 289 | [9] = "DMISCI", |
| 290 | [10] = "DMISMI", |
| 291 | [12] = "DMISERR", |
| 292 | [13] = "SLVSEL", |
| 293 | [16] = "INTRD_DET", |
| 294 | [17] = "SECOND_TO", |
| 295 | [18] = "BOOT", |
| 296 | [20] = "SMLINK_SLV" |
| 297 | }; |
| 298 | |
| 299 | if (!tco_sts) |
| 300 | return 0; |
| 301 | |
| 302 | printk(BIOS_DEBUG, "TCO_STS: "); |
| 303 | print_status_bits(tco_sts, tco_sts_bits); |
| 304 | printk(BIOS_DEBUG, "\n"); |
| 305 | |
| 306 | return tco_sts; |
| 307 | } |
| 308 | |
| 309 | /* Print, clear, and return TCO status */ |
| 310 | u32 clear_tco_status(void) |
| 311 | { |
| 312 | return print_tco_status(reset_tco_status()); |
| 313 | } |
| 314 | |
| 315 | /* Enable TCO SCI */ |
| 316 | void enable_tco_sci(void) |
| 317 | { |
| 318 | /* Clear pending events */ |
| 319 | outl(ACPI_BASE_ADDRESS + GPE0_STS(3), TCOSCI_STS); |
| 320 | |
| 321 | /* Enable TCO SCI events */ |
| 322 | enable_gpe(TCOSCI_EN); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | /* |
| 327 | * GPE0 |
| 328 | */ |
| 329 | |
| 330 | /* Clear a GPE0 status and return events that are enabled and active */ |
| 331 | static u32 reset_gpe(u16 sts_reg, u16 en_reg) |
| 332 | { |
| 333 | u32 gpe0_sts = inl(ACPI_BASE_ADDRESS + sts_reg); |
| 334 | u32 gpe0_en = inl(ACPI_BASE_ADDRESS + en_reg); |
| 335 | |
| 336 | outl(gpe0_sts, ACPI_BASE_ADDRESS + sts_reg); |
| 337 | |
| 338 | /* Only report enabled events */ |
| 339 | return gpe0_sts & gpe0_en; |
| 340 | } |
| 341 | |
| 342 | /* Print GPE0 status bits */ |
| 343 | static u32 print_gpe_status(u32 gpe0_sts, const char *bit_names[]) |
| 344 | { |
| 345 | if (!gpe0_sts) |
| 346 | return 0; |
| 347 | |
| 348 | printk(BIOS_DEBUG, "GPE0_STS: "); |
| 349 | print_status_bits(gpe0_sts, bit_names); |
| 350 | printk(BIOS_DEBUG, "\n"); |
| 351 | |
| 352 | return gpe0_sts; |
| 353 | } |
| 354 | |
| 355 | /* Print GPE0 GPIO status bits */ |
| 356 | static u32 print_gpe_gpio(u32 gpe0_sts, int start) |
| 357 | { |
| 358 | if (!gpe0_sts) |
| 359 | return 0; |
| 360 | |
| 361 | printk(BIOS_DEBUG, "GPE0_STS: "); |
| 362 | print_gpio_status(gpe0_sts, start); |
| 363 | printk(BIOS_DEBUG, "\n"); |
| 364 | |
| 365 | return gpe0_sts; |
| 366 | } |
| 367 | |
| 368 | /* Clear all GPE status and return "standard" GPE event status */ |
| 369 | u32 clear_gpe_status(void) |
| 370 | { |
| 371 | const char *gpe0_sts_3_bits[] = { |
| 372 | [1] = "HOTPLUG", |
| 373 | [2] = "SWGPE", |
| 374 | [6] = "TCO_SCI", |
| 375 | [7] = "SMB_WAK", |
| 376 | [9] = "PCI_EXP", |
| 377 | [10] = "BATLOW", |
| 378 | [11] = "PME", |
| 379 | [12] = "ME", |
| 380 | [13] = "PME_B0", |
| 381 | [16] = "GPIO27", |
| 382 | [18] = "WADT" |
| 383 | }; |
| 384 | |
| 385 | print_gpe_gpio(reset_gpe(GPE0_STS(GPE_31_0), GPE0_EN(GPE_31_0)), 0); |
| 386 | print_gpe_gpio(reset_gpe(GPE0_STS(GPE_63_32), GPE0_EN(GPE_63_32)), 32); |
| 387 | print_gpe_gpio(reset_gpe(GPE0_STS(GPE_94_64), GPE0_EN(GPE_94_64)), 64); |
| 388 | return print_gpe_status(reset_gpe(GPE0_STS(GPE_STD), GPE0_EN(GPE_STD)), |
| 389 | gpe0_sts_3_bits); |
| 390 | } |
| 391 | |
| 392 | /* Enable all requested GPE */ |
| 393 | void enable_all_gpe(u32 set1, u32 set2, u32 set3, u32 set4) |
| 394 | { |
| 395 | outl(set1, ACPI_BASE_ADDRESS + GPE0_EN(GPE_31_0)); |
| 396 | outl(set2, ACPI_BASE_ADDRESS + GPE0_EN(GPE_63_32)); |
| 397 | outl(set3, ACPI_BASE_ADDRESS + GPE0_EN(GPE_94_64)); |
| 398 | outl(set4, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD)); |
| 399 | } |
| 400 | |
| 401 | /* Disable all GPE */ |
| 402 | void disable_all_gpe(void) |
| 403 | { |
| 404 | enable_all_gpe(0, 0, 0, 0); |
| 405 | } |
| 406 | |
| 407 | /* Enable a standard GPE */ |
| 408 | void enable_gpe(u32 mask) |
| 409 | { |
| 410 | u32 gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD)); |
| 411 | gpe0_en |= mask; |
| 412 | outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD)); |
| 413 | } |
| 414 | |
| 415 | /* Disable a standard GPE */ |
| 416 | void disable_gpe(u32 mask) |
| 417 | { |
| 418 | u32 gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD)); |
| 419 | gpe0_en &= ~mask; |
| 420 | outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD)); |
| 421 | } |
| 422 | |
| 423 | int acpi_sci_irq(void) |
| 424 | { |
| 425 | int scis = pci_read_config32(PCH_DEV_LPC, ACPI_CNTL) & SCI_IRQ_SEL; |
| 426 | int sci_irq = 9; |
| 427 | |
| 428 | /* Determine how SCI is routed. */ |
| 429 | switch (scis) { |
| 430 | case SCIS_IRQ9: |
| 431 | case SCIS_IRQ10: |
| 432 | case SCIS_IRQ11: |
| 433 | sci_irq = scis - SCIS_IRQ9 + 9; |
| 434 | break; |
| 435 | case SCIS_IRQ20: |
| 436 | case SCIS_IRQ21: |
| 437 | case SCIS_IRQ22: |
| 438 | case SCIS_IRQ23: |
| 439 | sci_irq = scis - SCIS_IRQ20 + 20; |
| 440 | break; |
| 441 | default: |
| 442 | printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n"); |
| 443 | sci_irq = 9; |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq); |
| 448 | return sci_irq; |
| 449 | } |
Aaron Durbin | b9d9b79 | 2017-09-15 11:51:58 -0600 | [diff] [blame] | 450 | |
| 451 | int rtc_failure(void) |
| 452 | { |
| 453 | u8 reg8; |
| 454 | int rtc_failed; |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame^] | 455 | #if defined(__SIMPLE_DEVICE__) |
| 456 | pci_devfn_t dev = PCH_DEV_LPC; |
| 457 | #else |
| 458 | struct device *dev = PCH_DEV_LPC; |
| 459 | #endif |
Aaron Durbin | b9d9b79 | 2017-09-15 11:51:58 -0600 | [diff] [blame] | 460 | |
| 461 | reg8 = pci_read_config8(dev, GEN_PMCON_3); |
| 462 | rtc_failed = reg8 & RTC_BATTERY_DEAD; |
| 463 | if (rtc_failed) { |
| 464 | reg8 &= ~RTC_BATTERY_DEAD; |
| 465 | pci_write_config8(dev, GEN_PMCON_3, reg8); |
| 466 | printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed); |
| 467 | } |
| 468 | |
| 469 | return !!rtc_failed; |
| 470 | } |
Aaron Durbin | 0990fbf | 2017-09-15 15:23:04 -0600 | [diff] [blame] | 471 | |
| 472 | int vbnv_cmos_failed(void) |
| 473 | { |
| 474 | return rtc_failure(); |
| 475 | } |