blob: 681068d5f3bdb9ef3da4d88b7094be901160c3f8 [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>
Dinesh Gehlot8a2c9042023-01-17 05:12:07 +000011#include <console/console.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020012#include <device/mmio.h>
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070013#include <device/device.h>
14#include <device/pci.h>
Dinesh Gehlot8a2c9042023-01-17 05:12:07 +000015#include <gpio.h>
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070016#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>
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070020#include <soc/iomap.h>
21#include <soc/lpc.h>
22#include <soc/pci_devs.h>
23#include <soc/pm.h>
24#include <soc/smbus.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020025#include <security/vboot/vbnv.h>
Elyes HAOUASadd76f92019-03-21 09:55:49 +010026
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070027#include "chip.h"
28
29/*
30 * SMI
31 */
32
33const char *const *soc_smi_sts_array(size_t *a)
34{
35 static const char *const smi_sts_bits[] = {
36 [BIOS_STS_BIT] = "BIOS",
37 [LEGACY_USB_STS_BIT] = "LEGACY_USB",
38 [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI",
39 [APM_STS_BIT] = "APM",
40 [SWSMI_TMR_STS_BIT] = "SWSMI_TMR",
41 [PM1_STS_BIT] = "PM1",
42 [GPE0_STS_BIT] = "GPE0",
43 [GPIO_STS_BIT] = "GPI",
44 [MCSMI_STS_BIT] = "MCSMI",
45 [DEVMON_STS_BIT] = "DEVMON",
46 [TCO_STS_BIT] = "TCO",
47 [PERIODIC_STS_BIT] = "PERIODIC",
48 [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI",
49 [SMBUS_SMI_STS_BIT] = "SMBUS_SMI",
50 [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI",
51 [MONITOR_STS_BIT] = "MONITOR",
52 [SPI_SMI_STS_BIT] = "SPI",
53 [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK",
54 [ESPI_SMI_STS_BIT] = "ESPI_SMI",
55 };
56
57 *a = ARRAY_SIZE(smi_sts_bits);
58 return smi_sts_bits;
59}
60
61/*
62 * TCO
63 */
64
65const char *const *soc_tco_sts_array(size_t *a)
66{
67 static const char *const tco_sts_bits[] = {
68 [0] = "NMI2SMI",
69 [1] = "SW_TCO",
70 [2] = "TCO_INT",
71 [3] = "TIMEOUT",
72 [7] = "NEWCENTURY",
73 [8] = "BIOSWR",
74 [9] = "DMISCI",
75 [10] = "DMISMI",
76 [12] = "DMISERR",
77 [13] = "SLVSEL",
78 [16] = "INTRD_DET",
79 [17] = "SECOND_TO",
80 [18] = "BOOT",
81 [20] = "SMLINK_SLV"
82 };
83
84 *a = ARRAY_SIZE(tco_sts_bits);
85 return tco_sts_bits;
86}
87
88/*
89 * GPE0
90 */
91
Furquan Shaikhc4e652f2017-10-11 14:44:29 -070092const char *const *soc_std_gpe_sts_array(size_t *a)
Lijian Zhaob3dfcb82017-08-16 22:18:52 -070093{
94 static const char *const gpe_sts_bits[] = {
95 [1] = "HOTPLUG",
96 [2] = "SWGPE",
97 [6] = "TCO_SCI",
98 [7] = "SMB_WAK",
99 [9] = "PCI_EXP",
100 [10] = "BATLOW",
101 [11] = "PME",
102 [12] = "ME",
103 [13] = "PME_B0",
104 [14] = "eSPI",
105 [15] = "GPIO Tier-2",
106 [16] = "LAN_WAKE",
107 [18] = "WADT"
108 };
109
110 *a = ARRAY_SIZE(gpe_sts_bits);
111 return gpe_sts_bits;
112}
113
Lijian Zhao26be35a2018-04-17 16:13:39 -0700114void pmc_set_disb(void)
115{
116 /* Set the DISB after DRAM init */
117 uint8_t disb_val;
118 /* Only care about bits [23:16] of register GEN_PMCON_A */
Angel Pons9a1853a2021-02-19 17:56:29 +0100119 uint8_t *addr = (uint8_t *)(pmc_mmio_regs() + GEN_PMCON_A + 2);
Lijian Zhao26be35a2018-04-17 16:13:39 -0700120
121 disb_val = read8(addr);
122 disb_val |= (DISB >> 16);
123
124 /* Don't clear bits that are write-1-to-clear */
125 disb_val &= ~((MS4V | SUS_PWR_FLR) >> 16);
126 write8(addr, disb_val);
127}
128
Subrata Banik0baad612017-11-23 13:58:34 +0530129/*
130 * PMC controller gets hidden from PCI bus
131 * during FSP-Silicon init call. Hence PWRMBASE
132 * can't be accessible using PCI configuration space
133 * read/write.
134 */
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700135uint8_t *pmc_mmio_regs(void)
136{
Subrata Banik0baad612017-11-23 13:58:34 +0530137 return (void *)(uintptr_t)PCH_PWRM_BASE_ADDRESS;
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700138}
139
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700140uintptr_t soc_read_pmc_base(void)
141{
142 return (uintptr_t)pmc_mmio_regs();
143}
144
Michael Niewöhner93d215c2019-11-02 12:14:06 +0100145uint32_t *soc_pmc_etr_addr(void)
146{
147 return (uint32_t *)(soc_read_pmc_base() + ETR);
148}
149
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700150void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700151{
152 DEVTREE_CONST struct soc_intel_cannonlake_config *config;
Kyösti Mälkkid5f645c2019-09-28 00:20:27 +0300153 config = config_of_soc();
Lijian Zhaob3dfcb82017-08-16 22:18:52 -0700154
155 /* Assign to out variable */
156 *dw0 = config->gpe0_dw0;
157 *dw1 = config->gpe0_dw1;
158 *dw2 = config->gpe0_dw2;
159}
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600160
161static int rtc_failed(uint32_t gen_pmcon_b)
162{
163 return !!(gen_pmcon_b & RTC_BATTERY_DEAD);
164}
165
166int soc_get_rtc_failed(void)
167{
Kyösti Mälkki27872372021-01-21 16:05:26 +0200168 const struct chipset_power_state *ps;
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600169
Fabio Aiutofdcf6982022-09-11 12:25:13 +0200170 if (acpi_fetch_pm_state(&ps, PS_CLAIMER_RTC) < 0)
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600171 return 1;
Aaron Durbinbcd0bda2017-09-15 12:33:24 -0600172
173 return rtc_failed(ps->gen_pmcon_b);
174}
Aaron Durbin0990fbf2017-09-15 15:23:04 -0600175
176int vbnv_cmos_failed(void)
177{
178 return rtc_failed(read32(pmc_mmio_regs() + GEN_PMCON_B));
179}
V Sowmya5fe77af2019-03-06 16:52:57 +0530180
181static inline int deep_s3_enabled(void)
182{
183 uint32_t deep_s3_pol;
184
185 deep_s3_pol = read32(pmc_mmio_regs() + S3_PWRGATE_POL);
186 return !!(deep_s3_pol & (S3DC_GATE_SUS | S3AC_GATE_SUS));
187}
188
189/* Return 0, 3, or 5 to indicate the previous sleep state. */
Angel Ponsf5d090d2021-02-19 17:49:00 +0100190int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state)
V Sowmya5fe77af2019-03-06 16:52:57 +0530191{
V Sowmya5fe77af2019-03-06 16:52:57 +0530192 /*
193 * Check for any power failure to determine if this a wake from
Angel Ponsf5d090d2021-02-19 17:49:00 +0100194 * S5 because the PCH does not set the WAK_STS bit when waking
195 * from a true G3 state.
196 */
Subrata Banik6d641552023-01-16 15:05:02 +0530197 if (!(ps->pm1_sts & WAK_STS) && (ps->gen_pmcon_a & (PWR_FLR | SUS_PWR_FLR)))
V Sowmya5fe77af2019-03-06 16:52:57 +0530198 prev_sleep_state = ACPI_S5;
199
200 /*
201 * If waking from S3 determine if deep S3 is enabled. If not,
202 * need to check both deep sleep well and normal suspend well.
203 * Otherwise just check deep sleep well.
204 */
205 if (prev_sleep_state == ACPI_S3) {
206 /* PWR_FLR represents deep sleep power well loss. */
207 uint32_t mask = PWR_FLR;
208
209 /* If deep s3 isn't enabled check the suspend well too. */
210 if (!deep_s3_enabled())
211 mask |= SUS_PWR_FLR;
212
Furquan Shaikh6e401cf2019-03-14 15:44:19 -0700213 if (ps->gen_pmcon_a & mask)
V Sowmya5fe77af2019-03-06 16:52:57 +0530214 prev_sleep_state = ACPI_S5;
215 }
216
217 return prev_sleep_state;
218}
219
220void soc_fill_power_state(struct chipset_power_state *ps)
221{
222 uint8_t *pmc;
223
224 ps->tco1_sts = tco_read_reg(TCO1_STS);
225 ps->tco2_sts = tco_read_reg(TCO2_STS);
226
Angel Ponsf5d090d2021-02-19 17:49:00 +0100227 printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts);
V Sowmya5fe77af2019-03-06 16:52:57 +0530228
229 pmc = pmc_mmio_regs();
230 ps->gen_pmcon_a = read32(pmc + GEN_PMCON_A);
231 ps->gen_pmcon_b = read32(pmc + GEN_PMCON_B);
232 ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
233 ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
Angel Ponsc3aa6592022-09-09 15:20:56 +0200234 ps->hpr_cause0 = read32(pmc + HPR_CAUSE0);
V Sowmya5fe77af2019-03-06 16:52:57 +0530235
236 printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
237 ps->gen_pmcon_a, ps->gen_pmcon_b);
238
239 printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
240 ps->gblrst_cause[0], ps->gblrst_cause[1]);
Angel Ponsc3aa6592022-09-09 15:20:56 +0200241
242 printk(BIOS_DEBUG, "HPR_CAUSE0: %08x\n", ps->hpr_cause0);
V Sowmya5fe77af2019-03-06 16:52:57 +0530243}
Eugene Myersebc84232020-01-21 16:46:16 -0500244
245/* STM Support */
246uint16_t get_pmbase(void)
247{
Elyes Haouas9018dee2022-11-18 15:07:33 +0100248 return (uint16_t)ACPI_BASE_ADDRESS;
Eugene Myersebc84232020-01-21 16:46:16 -0500249}
Angel Pons505e3832021-04-17 13:02:37 +0200250
251/*
252 * Set which power state system will be after reapplying
253 * the power (from G3 State)
254 */
255void pmc_soc_set_afterg3_en(const bool on)
256{
257 uint8_t reg8;
258 uint8_t *const pmcbase = pmc_mmio_regs();
259
260 reg8 = read8(pmcbase + GEN_PMCON_A);
261 if (on)
262 reg8 &= ~SLEEP_AFTER_POWER_FAIL;
263 else
264 reg8 |= SLEEP_AFTER_POWER_FAIL;
265 write8(pmcbase + GEN_PMCON_A, reg8);
266}