blob: a79d2622b18d454ac9155ee69ae05ecbcba08a5c [file] [log] [blame]
Angel Ponsf5627e82020-04-05 15:46:52 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Lijian Zhaob3dfcb82017-08-16 22:18:52 -07002
3/*
4 * Helper functions for dealing with power management registers
5 * and the differences between PCH variants.
6 */
7
8#define __SIMPLE_DEVICE__
9
Kyösti Mälkki27872372021-01-21 16:05:26 +020010#include <acpi/acpi_pm.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020011#include <device/mmio.h>
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070012#include <device/device.h>
13#include <device/pci.h>
14#include <device/pci_def.h>
15#include <console/console.h>
16#include <intelblocks/pmclib.h>
Aaron Durbinbcd0bda2017-09-15 12:33:24 -060017#include <intelblocks/rtc.h>
Subrata Banik7bc4dc52018-05-17 18:40:32 +053018#include <intelblocks/tco.h>
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070019#include <soc/gpe.h>
20#include <soc/gpio.h>
21#include <soc/iomap.h>
22#include <soc/lpc.h>
23#include <soc/pci_devs.h>
24#include <soc/pm.h>
25#include <soc/smbus.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020026#include <security/vboot/vbnv.h>
Elyes HAOUASadd76f92019-03-21 09:55:49 +010027
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070028#include "chip.h"
29
30/*
31 * SMI
32 */
33
34const char *const *soc_smi_sts_array(size_t *a)
35{
36 static const char *const smi_sts_bits[] = {
37 [BIOS_STS_BIT] = "BIOS",
38 [LEGACY_USB_STS_BIT] = "LEGACY_USB",
39 [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI",
40 [APM_STS_BIT] = "APM",
41 [SWSMI_TMR_STS_BIT] = "SWSMI_TMR",
42 [PM1_STS_BIT] = "PM1",
43 [GPE0_STS_BIT] = "GPE0",
44 [GPIO_STS_BIT] = "GPI",
45 [MCSMI_STS_BIT] = "MCSMI",
46 [DEVMON_STS_BIT] = "DEVMON",
47 [TCO_STS_BIT] = "TCO",
48 [PERIODIC_STS_BIT] = "PERIODIC",
49 [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI",
50 [SMBUS_SMI_STS_BIT] = "SMBUS_SMI",
51 [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI",
52 [MONITOR_STS_BIT] = "MONITOR",
53 [SPI_SMI_STS_BIT] = "SPI",
54 [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK",
55 [ESPI_SMI_STS_BIT] = "ESPI_SMI",
56 };
57
58 *a = ARRAY_SIZE(smi_sts_bits);
59 return smi_sts_bits;
60}
61
62/*
63 * TCO
64 */
65
66const char *const *soc_tco_sts_array(size_t *a)
67{
68 static const char *const tco_sts_bits[] = {
69 [0] = "NMI2SMI",
70 [1] = "SW_TCO",
71 [2] = "TCO_INT",
72 [3] = "TIMEOUT",
73 [7] = "NEWCENTURY",
74 [8] = "BIOSWR",
75 [9] = "DMISCI",
76 [10] = "DMISMI",
77 [12] = "DMISERR",
78 [13] = "SLVSEL",
79 [16] = "INTRD_DET",
80 [17] = "SECOND_TO",
81 [18] = "BOOT",
82 [20] = "SMLINK_SLV"
83 };
84
85 *a = ARRAY_SIZE(tco_sts_bits);
86 return tco_sts_bits;
87}
88
89/*
90 * GPE0
91 */
92
Furquan Shaikhc4e652f2017-10-11 14:44:29 -070093const char *const *soc_std_gpe_sts_array(size_t *a)
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070094{
95 static const char *const gpe_sts_bits[] = {
96 [1] = "HOTPLUG",
97 [2] = "SWGPE",
98 [6] = "TCO_SCI",
99 [7] = "SMB_WAK",
100 [9] = "PCI_EXP",
101 [10] = "BATLOW",
102 [11] = "PME",
103 [12] = "ME",
104 [13] = "PME_B0",
105 [14] = "eSPI",
106 [15] = "GPIO Tier-2",
107 [16] = "LAN_WAKE",
108 [18] = "WADT"
109 };
110
111 *a = ARRAY_SIZE(gpe_sts_bits);
112 return gpe_sts_bits;
113}
114
Lijian Zhao26be35a2018-04-17 16:13:39 -0700115void pmc_set_disb(void)
116{
117 /* Set the DISB after DRAM init */
118 uint8_t disb_val;
119 /* Only care about bits [23:16] of register GEN_PMCON_A */
Angel Pons9a1853a2021-02-19 17:56:29 +0100120 uint8_t *addr = (uint8_t *)(pmc_mmio_regs() + GEN_PMCON_A + 2);
Lijian Zhao26be35a2018-04-17 16:13:39 -0700121
122 disb_val = read8(addr);
123 disb_val |= (DISB >> 16);
124
125 /* Don't clear bits that are write-1-to-clear */
126 disb_val &= ~((MS4V | SUS_PWR_FLR) >> 16);
127 write8(addr, disb_val);
128}
129
Krishna Prasad Bhat2de19032019-03-14 23:23:22 +0530130void pmc_clear_pmcon_sts(void)
131{
132 uint32_t reg_val;
133 uint8_t *addr;
134 addr = pmc_mmio_regs();
135
136 reg_val = read32(addr + GEN_PMCON_A);
137 /* Clear SUS_PWR_FLR, GBL_RST_STS, HOST_RST_STS, PWR_FLR bits
138 * while retaining MS4V write-1-to-clear bit */
139 reg_val &= ~(MS4V);
140
141 write32((addr + GEN_PMCON_A), reg_val);
142}
143
Subrata Banik0baad612017-11-23 13:58:34 +0530144/*
145 * PMC controller gets hidden from PCI bus
146 * during FSP-Silicon init call. Hence PWRMBASE
147 * can't be accessible using PCI configuration space
148 * read/write.
149 */
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700150uint8_t *pmc_mmio_regs(void)
151{
Subrata Banik0baad612017-11-23 13:58:34 +0530152 return (void *)(uintptr_t)PCH_PWRM_BASE_ADDRESS;
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700153}
154
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700155uintptr_t soc_read_pmc_base(void)
156{
157 return (uintptr_t)pmc_mmio_regs();
158}
159
Michael Niewöhner93d215c2019-11-02 12:14:06 +0100160uint32_t *soc_pmc_etr_addr(void)
161{
162 return (uint32_t *)(soc_read_pmc_base() + ETR);
163}
164
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700165void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700166{
167 DEVTREE_CONST struct soc_intel_cannonlake_config *config;
Kyösti Mälkkid5f645c2019-09-28 00:20:27 +0300168 config = config_of_soc();
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700169
170 /* Assign to out variable */
171 *dw0 = config->gpe0_dw0;
172 *dw1 = config->gpe0_dw1;
173 *dw2 = config->gpe0_dw2;
174}
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600175
176static int rtc_failed(uint32_t gen_pmcon_b)
177{
178 return !!(gen_pmcon_b & RTC_BATTERY_DEAD);
179}
180
181int soc_get_rtc_failed(void)
182{
Kyösti Mälkki27872372021-01-21 16:05:26 +0200183 const struct chipset_power_state *ps;
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600184
Kyösti Mälkki27872372021-01-21 16:05:26 +0200185 if (acpi_pm_state_for_rtc(&ps) < 0)
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600186 return 1;
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600187
188 return rtc_failed(ps->gen_pmcon_b);
189}
Aaron Durbin0990fbf2017-09-15 15:23:04 -0600190
191int vbnv_cmos_failed(void)
192{
193 return rtc_failed(read32(pmc_mmio_regs() + GEN_PMCON_B));
194}
V Sowmya5fe77af2019-03-06 16:52:57 +0530195
196static inline int deep_s3_enabled(void)
197{
198 uint32_t deep_s3_pol;
199
200 deep_s3_pol = read32(pmc_mmio_regs() + S3_PWRGATE_POL);
201 return !!(deep_s3_pol & (S3DC_GATE_SUS | S3AC_GATE_SUS));
202}
203
204/* Return 0, 3, or 5 to indicate the previous sleep state. */
Angel Ponsf5d090d2021-02-19 17:49:00 +0100205int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state)
V Sowmya5fe77af2019-03-06 16:52:57 +0530206{
V Sowmya5fe77af2019-03-06 16:52:57 +0530207 /*
208 * Check for any power failure to determine if this a wake from
Angel Ponsf5d090d2021-02-19 17:49:00 +0100209 * S5 because the PCH does not set the WAK_STS bit when waking
210 * from a true G3 state.
211 */
Furquan Shaikh6e401cf2019-03-14 15:44:19 -0700212 if (ps->gen_pmcon_a & (PWR_FLR | SUS_PWR_FLR))
V Sowmya5fe77af2019-03-06 16:52:57 +0530213 prev_sleep_state = ACPI_S5;
214
215 /*
216 * If waking from S3 determine if deep S3 is enabled. If not,
217 * need to check both deep sleep well and normal suspend well.
218 * Otherwise just check deep sleep well.
219 */
220 if (prev_sleep_state == ACPI_S3) {
221 /* PWR_FLR represents deep sleep power well loss. */
222 uint32_t mask = PWR_FLR;
223
224 /* If deep s3 isn't enabled check the suspend well too. */
225 if (!deep_s3_enabled())
226 mask |= SUS_PWR_FLR;
227
Furquan Shaikh6e401cf2019-03-14 15:44:19 -0700228 if (ps->gen_pmcon_a & mask)
V Sowmya5fe77af2019-03-06 16:52:57 +0530229 prev_sleep_state = ACPI_S5;
230 }
231
232 return prev_sleep_state;
233}
234
235void soc_fill_power_state(struct chipset_power_state *ps)
236{
237 uint8_t *pmc;
238
239 ps->tco1_sts = tco_read_reg(TCO1_STS);
240 ps->tco2_sts = tco_read_reg(TCO2_STS);
241
Angel Ponsf5d090d2021-02-19 17:49:00 +0100242 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts);
V Sowmya5fe77af2019-03-06 16:52:57 +0530243
244 pmc = pmc_mmio_regs();
245 ps->gen_pmcon_a = read32(pmc + GEN_PMCON_A);
246 ps->gen_pmcon_b = read32(pmc + GEN_PMCON_B);
247 ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
248 ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
249
250 printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
251 ps->gen_pmcon_a, ps->gen_pmcon_b);
252
253 printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
254 ps->gblrst_cause[0], ps->gblrst_cause[1]);
255}
Eugene Myersebc84232020-01-21 16:46:16 -0500256
257/* STM Support */
258uint16_t get_pmbase(void)
259{
260 return (uint16_t) ACPI_BASE_ADDRESS;
261}
Angel Pons505e3832021-04-17 13:02:37 +0200262
263/*
264 * Set which power state system will be after reapplying
265 * the power (from G3 State)
266 */
267void pmc_soc_set_afterg3_en(const bool on)
268{
269 uint8_t reg8;
270 uint8_t *const pmcbase = pmc_mmio_regs();
271
272 reg8 = read8(pmcbase + GEN_PMCON_A);
273 if (on)
274 reg8 &= ~SLEEP_AFTER_POWER_FAIL;
275 else
276 reg8 |= SLEEP_AFTER_POWER_FAIL;
277 write8(pmcbase + GEN_PMCON_A, reg8);
278}