blob: 0da7fe8157bea20e633906f3100afdb947970b4c [file] [log] [blame]
Lee Leahyb0005132015-05-12 18:19:47 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
Lee Leahy1d14b3e2015-05-12 18:23:27 -07005 * Copyright (C) 2015 Intel Corporation.
Lee Leahyb0005132015-05-12 18:19:47 -07006 *
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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Lee Leahyb0005132015-05-12 18:19:47 -070015 */
16
17/*
18 * Helper functions for dealing with power management registers
19 * and the differences between PCH variants.
20 */
21
Duncan Laurief0ba2252016-10-25 20:03:56 -070022#define __SIMPLE_DEVICE__
23
Furquan Shaikh3efeeb22017-10-14 19:24:43 -070024#include <arch/acpi.h>
Lee Leahyb0005132015-05-12 18:19:47 -070025#include <arch/io.h>
26#include <device/device.h>
27#include <device/pci.h>
28#include <device/pci_def.h>
29#include <console/console.h>
Shaunak Sahad3476802017-07-08 01:08:40 -070030#include <intelblocks/pmclib.h>
Aaron Durbin38613d02016-07-14 00:56:58 -050031#include <halt.h>
Ravi Sarawadi1483d1f2017-09-28 17:06:01 -070032#include <intelblocks/lpc_lib.h>
Furquan Shaikh97e0a652016-08-18 21:42:36 -070033#include <rules.h>
Lee Leahy1d14b3e2015-05-12 18:23:27 -070034#include <stdlib.h>
Duncan Laurief0ba2252016-10-25 20:03:56 -070035#include <soc/gpe.h>
Lee Leahy1d14b3e2015-05-12 18:23:27 -070036#include <soc/gpio.h>
Lee Leahyb0005132015-05-12 18:19:47 -070037#include <soc/iomap.h>
Lee Leahyb0005132015-05-12 18:19:47 -070038#include <soc/pci_devs.h>
39#include <soc/pm.h>
Lee Leahy1d14b3e2015-05-12 18:23:27 -070040#include <soc/pmc.h>
41#include <soc/smbus.h>
Duncan Laurie2f3736e2016-11-03 10:33:43 -070042#include <timer.h>
Aaron Durbin0990fbf2017-09-15 15:23:04 -060043#include <vboot/vbnv.h>
Duncan Laurief0ba2252016-10-25 20:03:56 -070044#include "chip.h"
Lee Leahyb0005132015-05-12 18:19:47 -070045
Lee Leahyb0005132015-05-12 18:19:47 -070046/*
47 * SMI
48 */
49
Shaunak Sahad3476802017-07-08 01:08:40 -070050const char *const *soc_smi_sts_array(size_t *smi_arr)
Lee Leahyb0005132015-05-12 18:19:47 -070051{
Shaunak Sahad3476802017-07-08 01:08:40 -070052 static const char *const smi_sts_bits[] = {
Lee Leahyb0005132015-05-12 18:19:47 -070053 [2] = "BIOS",
54 [3] = "LEGACY_USB",
55 [4] = "SLP_SMI",
56 [5] = "APM",
57 [6] = "SWSMI_TMR",
58 [8] = "PM1",
59 [9] = "GPE0",
60 [10] = "GPI",
61 [11] = "MCSMI",
62 [12] = "DEVMON",
63 [13] = "TCO",
64 [14] = "PERIODIC",
65 [15] = "SERIRQ_SMI",
66 [16] = "SMBUS_SMI",
67 [17] = "LEGACY_USB2",
68 [18] = "INTEL_USB2",
69 [20] = "PCI_EXP_SMI",
70 [21] = "MONITOR",
71 [26] = "SPI",
Duncan Laurie8d019022016-10-25 19:58:27 -070072 [27] = "GPIO_UNLOCK",
73 [28] = "ESPI_SMI",
Lee Leahyb0005132015-05-12 18:19:47 -070074 };
75
Shaunak Sahad3476802017-07-08 01:08:40 -070076 *smi_arr = ARRAY_SIZE(smi_sts_bits);
77 return smi_sts_bits;
Lee Leahyb0005132015-05-12 18:19:47 -070078}
79
Lee Leahyb0005132015-05-12 18:19:47 -070080/*
81 * TCO
82 */
83
Shaunak Sahad3476802017-07-08 01:08:40 -070084const char *const *soc_tco_sts_array(size_t *tco_arr)
Lee Leahyb0005132015-05-12 18:19:47 -070085{
Shaunak Sahad3476802017-07-08 01:08:40 -070086 static const char *const tco_sts_bits[] = {
Lee Leahyb0005132015-05-12 18:19:47 -070087 [0] = "NMI2SMI",
88 [1] = "SW_TCO",
89 [2] = "TCO_INT",
90 [3] = "TIMEOUT",
91 [7] = "NEWCENTURY",
92 [8] = "BIOSWR",
93 [9] = "DMISCI",
94 [10] = "DMISMI",
95 [12] = "DMISERR",
96 [13] = "SLVSEL",
97 [16] = "INTRD_DET",
98 [17] = "SECOND_TO",
99 [18] = "BOOT",
100 [20] = "SMLINK_SLV"
101 };
102
Shaunak Sahad3476802017-07-08 01:08:40 -0700103 *tco_arr = ARRAY_SIZE(tco_sts_bits);
104 return tco_sts_bits;
Lee Leahyb0005132015-05-12 18:19:47 -0700105}
106
Lee Leahyb0005132015-05-12 18:19:47 -0700107/*
108 * GPE0
109 */
110
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700111const char *const *soc_std_gpe_sts_array(size_t *gpe_arr)
Lee Leahyb0005132015-05-12 18:19:47 -0700112{
Shaunak Sahad3476802017-07-08 01:08:40 -0700113 static const char *const gpe_sts_bits[] = {
Lee Leahyb0005132015-05-12 18:19:47 -0700114 [1] = "HOTPLUG",
115 [2] = "SWGPE",
116 [6] = "TCO_SCI",
117 [7] = "SMB_WAK",
118 [9] = "PCI_EXP",
119 [10] = "BATLOW",
120 [11] = "PME",
121 [12] = "ME",
122 [13] = "PME_B0",
Aaron Durbin7f788492015-07-24 17:10:31 -0500123 [14] = "eSPI",
124 [15] = "GPIO Tier-2",
125 [16] = "LAN_WAKE",
Lee Leahyb0005132015-05-12 18:19:47 -0700126 [18] = "WADT"
127 };
128
Shaunak Sahad3476802017-07-08 01:08:40 -0700129 *gpe_arr = ARRAY_SIZE(gpe_sts_bits);
130 return gpe_sts_bits;
Lee Leahyb0005132015-05-12 18:19:47 -0700131}
132
133int acpi_sci_irq(void)
134{
Lee Leahy1d14b3e2015-05-12 18:23:27 -0700135 int scis = pci_read_config32(PCH_DEV_PMC, ACTL) & SCI_IRQ_SEL;
Lee Leahyb0005132015-05-12 18:19:47 -0700136 int sci_irq = 9;
137
138 /* Determine how SCI is routed. */
139 switch (scis) {
140 case SCIS_IRQ9:
141 case SCIS_IRQ10:
142 case SCIS_IRQ11:
143 sci_irq = scis - SCIS_IRQ9 + 9;
144 break;
145 case SCIS_IRQ20:
146 case SCIS_IRQ21:
147 case SCIS_IRQ22:
148 case SCIS_IRQ23:
149 sci_irq = scis - SCIS_IRQ20 + 20;
150 break;
151 default:
152 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
153 sci_irq = 9;
154 break;
155 }
156
157 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
158 return sci_irq;
159}
Lee Leahy1d14b3e2015-05-12 18:23:27 -0700160
161uint8_t *pmc_mmio_regs(void)
162{
163 uint32_t reg32;
164
165 reg32 = pci_read_config32(PCH_DEV_PMC, PWRMBASE);
166
167 /* 4KiB alignment. */
168 reg32 &= ~0xfff;
169
Shaunak Sahad3476802017-07-08 01:08:40 -0700170 return (void *)(uintptr_t) reg32;
Lee Leahy1d14b3e2015-05-12 18:23:27 -0700171}
172
Barnali Sarkar49eca132016-08-12 00:05:27 +0530173uint16_t smbus_tco_regs(void)
Lee Leahy1d14b3e2015-05-12 18:23:27 -0700174{
175 uint16_t reg16;
176
177 reg16 = pci_read_config16(PCH_DEV_SMBUS, TCOBASE);
178
179 reg16 &= ~0x1f;
180
181 return reg16;
182}
Aaron Durbin38613d02016-07-14 00:56:58 -0500183
Shaunak Sahad3476802017-07-08 01:08:40 -0700184uint32_t soc_reset_tco_status(void)
Aaron Durbin38613d02016-07-14 00:56:58 -0500185{
Shaunak Sahad3476802017-07-08 01:08:40 -0700186 u16 tco1_sts;
187 u16 tco2_sts;
188 u16 tcobase;
Furquan Shaikh97e0a652016-08-18 21:42:36 -0700189
Shaunak Sahad3476802017-07-08 01:08:40 -0700190 tcobase = smbus_tco_regs();
191
192 /* TCO Status 2 register */
193 tco2_sts = inw(tcobase + TCO2_STS);
194 tco2_sts |= (TCO2_STS_SECOND_TO | TCO2_STS_BOOT);
195 outw(tco2_sts, tcobase + TCO2_STS);
196
197 /* TCO Status 1 register */
198 tco1_sts = inw(tcobase + TCO1_STS);
199
200 /* Clear SECOND_TO_STS bit */
201 if (tco2_sts & TCO2_STS_SECOND_TO)
202 outw(tco2_sts & ~TCO2_STS_SECOND_TO, tcobase + TCO2_STS);
203
204 return (tco2_sts << 16) | tco1_sts;
Aaron Durbin38613d02016-07-14 00:56:58 -0500205}
Duncan Laurief0ba2252016-10-25 20:03:56 -0700206
Shaunak Sahad3476802017-07-08 01:08:40 -0700207uintptr_t soc_read_pmc_base(void)
208{
209 return (uintptr_t) (pmc_mmio_regs());
210}
211
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700212void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
Duncan Laurief0ba2252016-10-25 20:03:56 -0700213{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500214 DEVTREE_CONST struct soc_intel_skylake_config *config;
Duncan Laurief0ba2252016-10-25 20:03:56 -0700215
216 /* Look up the device in devicetree */
Shaunak Sahad3476802017-07-08 01:08:40 -0700217 DEVTREE_CONST struct device *dev = dev_find_slot(0, PCH_DEVFN_PMC);
Duncan Laurief0ba2252016-10-25 20:03:56 -0700218 if (!dev || !dev->chip_info) {
219 printk(BIOS_ERR, "BUG! Could not find SOC devicetree config\n");
Shaunak Sahaec1a24c2017-10-06 12:50:38 -0700220 return;
Duncan Laurief0ba2252016-10-25 20:03:56 -0700221 }
222 config = dev->chip_info;
Duncan Laurief0ba2252016-10-25 20:03:56 -0700223
Shaunak Sahad3476802017-07-08 01:08:40 -0700224 /* Assign to out variable */
225 *dw0 = config->gpe0_dw0;
226 *dw1 = config->gpe0_dw1;
227 *dw2 = config->gpe0_dw2;
Duncan Laurief0ba2252016-10-25 20:03:56 -0700228}
Aaron Durbind1fc8c12017-09-15 12:37:05 -0600229
230int rtc_failure(void)
231{
232 u8 reg8;
233 int rtc_failed;
234 /* PMC Controller Device 0x1F, Func 02 */
235 device_t dev = PCH_DEV_PMC;
236 reg8 = pci_read_config8(dev, GEN_PMCON_B);
237 rtc_failed = reg8 & RTC_BATTERY_DEAD;
238 if (rtc_failed) {
239 reg8 &= ~RTC_BATTERY_DEAD;
240 pci_write_config8(dev, GEN_PMCON_B, reg8);
241 printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed);
242 }
243
244 return !!rtc_failed;
245}
Aaron Durbin0990fbf2017-09-15 15:23:04 -0600246
247int vbnv_cmos_failed(void)
248{
249 return rtc_failure();
250}
Furquan Shaikh3efeeb22017-10-14 19:24:43 -0700251
252/* Return 0, 3, or 5 to indicate the previous sleep state. */
253int soc_prev_sleep_state(const struct chipset_power_state *ps,
254 int prev_sleep_state)
255{
256 /*
257 * Check for any power failure to determine if this a wake from
258 * S5 because the PCH does not set the WAK_STS bit when waking
259 * from a true G3 state.
260 */
261 if (!(ps->pm1_sts & WAK_STS) &&
262 (ps->gen_pmcon_b & (PWR_FLR | SUS_PWR_FLR)))
263 prev_sleep_state = ACPI_S5;
264
265 /*
266 * If waking from S3 determine if deep S3 is enabled. If not,
267 * need to check both deep sleep well and normal suspend well.
268 * Otherwise just check deep sleep well.
269 */
270 if (prev_sleep_state == ACPI_S3) {
271 /* PWR_FLR represents deep sleep power well loss. */
272 uint32_t mask = PWR_FLR;
273
274 /* If deep s3 isn't enabled check the suspend well too. */
275 if (!deep_s3_enabled())
276 mask |= SUS_PWR_FLR;
277
278 if (ps->gen_pmcon_b & mask)
279 prev_sleep_state = ACPI_S5;
280 }
281 return prev_sleep_state;
282}
283
284void soc_fill_power_state(struct chipset_power_state *ps)
285{
286 uint16_t tcobase;
287 uint8_t *pmc;
288
289 tcobase = smbus_tco_regs();
290
291 ps->tco1_sts = inw(tcobase + TCO1_STS);
292 ps->tco2_sts = inw(tcobase + TCO2_STS);
293
294 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n",
295 ps->tco1_sts, ps->tco2_sts);
296
297 ps->gen_pmcon_a = pci_read_config32(PCH_DEV_PMC, GEN_PMCON_A);
298 ps->gen_pmcon_b = pci_read_config32(PCH_DEV_PMC, GEN_PMCON_B);
299
300 pmc = pmc_mmio_regs();
301 ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
302 ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
303
304 printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
305 ps->gen_pmcon_a, ps->gen_pmcon_b);
306
307 printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
308 ps->gblrst_cause[0], ps->gblrst_cause[1]);
309}
310
311int acpi_get_sleep_type(void)
312{
313 struct chipset_power_state *ps;
314
315 ps = pmc_get_power_state();
316 return ps->prev_sleep_state;
317}