coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
pmutil.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /*
4  * Helper functions for dealing with power management registers
5  * and the differences between PCH variants.
6  */
7 
8 #define __SIMPLE_DEVICE__
9 
10 #include <acpi/acpi.h>
11 #include <device/mmio.h>
12 #include <device/pci_ops.h>
13 #include <device/device.h>
14 #include <device/pci.h>
15 #include <device/pci_def.h>
16 #include <console/console.h>
17 #include <intelblocks/pmclib.h>
18 #include <intelblocks/lpc_lib.h>
19 #include <intelblocks/tco.h>
20 #include <soc/gpe.h>
21 #include <soc/gpio.h>
22 #include <soc/iomap.h>
23 #include <soc/pci_devs.h>
24 #include <soc/pm.h>
25 #include <soc/pmc.h>
26 #include <soc/smbus.h>
27 #include <security/vboot/vbnv.h>
28 
29 #include "chip.h"
30 
31 /*
32  * SMI
33  */
34 
35 const char *const *soc_smi_sts_array(size_t *smi_arr)
36 {
37  static const char *const smi_sts_bits[] = {
38  [BIOS_STS_BIT] = "BIOS",
39  [LEGACY_USB_STS_BIT] = "LEGACY_USB",
40  [SMI_ON_SLP_EN_STS_BIT] = "SLP_SMI",
41  [APM_STS_BIT] = "APM",
42  [SWSMI_TMR_STS_BIT] = "SWSMI_TMR",
43  [PM1_STS_BIT] = "PM1",
44  [GPE0_STS_BIT] = "GPE0",
45  [GPIO_STS_BIT] = "GPI",
46  [MCSMI_STS_BIT] = "MCSMI",
47  [DEVMON_STS_BIT] = "DEVMON",
48  [TCO_STS_BIT] = "TCO",
49  [PERIODIC_STS_BIT] = "PERIODIC",
50  [SERIRQ_SMI_STS_BIT] = "SERIRQ_SMI",
51  [SMBUS_SMI_STS_BIT] = "SMBUS_SMI",
52  [PCI_EXP_SMI_STS_BIT] = "PCI_EXP_SMI",
53  [MONITOR_STS_BIT] = "MONITOR",
54  [SPI_SMI_STS_BIT] = "SPI",
55  [GPIO_UNLOCK_SMI_STS_BIT] = "GPIO_UNLOCK",
56  [ESPI_SMI_STS_BIT] = "ESPI_SMI",
57  };
58 
59  *smi_arr = ARRAY_SIZE(smi_sts_bits);
60  return smi_sts_bits;
61 }
62 
63 /*
64  * TCO
65  */
66 
67 const char *const *soc_tco_sts_array(size_t *tco_arr)
68 {
69  static const char *const tco_sts_bits[] = {
70  [0] = "NMI2SMI",
71  [1] = "SW_TCO",
72  [2] = "TCO_INT",
73  [3] = "TIMEOUT",
74  [7] = "NEWCENTURY",
75  [8] = "BIOSWR",
76  [9] = "DMISCI",
77  [10] = "DMISMI",
78  [12] = "DMISERR",
79  [13] = "SLVSEL",
80  [16] = "INTRD_DET",
81  [17] = "SECOND_TO",
82  [18] = "BOOT",
83  [20] = "SMLINK_SLV"
84  };
85 
86  *tco_arr = ARRAY_SIZE(tco_sts_bits);
87  return tco_sts_bits;
88 }
89 
90 /*
91  * GPE0
92  */
93 
94 const char *const *soc_std_gpe_sts_array(size_t *gpe_arr)
95 {
96  static const char *const gpe_sts_bits[] = {
97  [1] = "HOTPLUG",
98  [2] = "SWGPE",
99  [6] = "TCO_SCI",
100  [7] = "SMB_WAK",
101  [9] = "PCI_EXP",
102  [10] = "BATLOW",
103  [11] = "PME",
104  [12] = "ME",
105  [13] = "PME_B0",
106  [14] = "eSPI",
107  [15] = "GPIO Tier-2",
108  [16] = "LAN_WAKE",
109  [18] = "WADT"
110  };
111 
112  *gpe_arr = ARRAY_SIZE(gpe_sts_bits);
113  return gpe_sts_bits;
114 }
115 
116 void pmc_set_disb(void)
117 {
118  /* Set the DISB after DRAM init */
119  u32 disb_val;
120  const pci_devfn_t dev = PCH_DEV_PMC;
121 
122  disb_val = pci_read_config32(dev, GEN_PMCON_A);
123  disb_val |= DISB;
124 
125  /* Don't clear bits that are write-1-to-clear */
126  disb_val &= ~(GBL_RST_STS | MS4V);
127  pci_write_config32(dev, GEN_PMCON_A, disb_val);
128 }
129 
131 {
132  uint32_t reg32;
133 
135 
136  /* 4KiB alignment. */
137  reg32 &= ~0xfff;
138 
139  return (void *)(uintptr_t) reg32;
140 }
141 
143 {
144  return (uintptr_t) (pmc_mmio_regs());
145 }
146 
148 {
149  /*
150  * The pointer returned must not be cached, because the address depends on the
151  * MMCONF base address and the assigned PCI bus number, which both may change
152  * during the boot process!
153  */
155 }
156 
158 {
160 
161  config = config_of_soc();
162 
163  /* Assign to out variable */
164  *dw0 = config->gpe0_dw0;
165  *dw1 = config->gpe0_dw1;
166  *dw2 = config->gpe0_dw2;
167 }
168 
169 int rtc_failure(void)
170 {
171  u8 reg8;
172  int rtc_failed;
173  /* PMC Controller Device 0x1F, Func 02 */
174  const pci_devfn_t dev = PCH_DEV_PMC;
175 
176  reg8 = pci_read_config8(dev, GEN_PMCON_B);
177  rtc_failed = reg8 & RTC_BATTERY_DEAD;
178  if (rtc_failed) {
179  reg8 &= ~RTC_BATTERY_DEAD;
180  pci_write_config8(dev, GEN_PMCON_B, reg8);
181  printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed);
182  }
183 
184  return !!rtc_failed;
185 }
186 
188 {
189  return rtc_failure();
190 }
191 
192 /* Return 0, 3, or 5 to indicate the previous sleep state. */
194 {
195  /*
196  * Check for any power failure to determine if this a wake from
197  * S5 because the PCH does not set the WAK_STS bit when waking
198  * from a true G3 state.
199  */
200  if (!(ps->pm1_sts & WAK_STS) &&
201  (ps->gen_pmcon_b & (PWR_FLR | SUS_PWR_FLR)))
203 
204  /*
205  * If waking from S3 determine if deep S3 is enabled. If not,
206  * need to check both deep sleep well and normal suspend well.
207  * Otherwise just check deep sleep well.
208  */
209  if (prev_sleep_state == ACPI_S3) {
210  /* PWR_FLR represents deep sleep power well loss. */
212 
213  /* If deep s3 isn't enabled check the suspend well too. */
214  if (!deep_s3_enabled())
215  mask |= SUS_PWR_FLR;
216 
217  if (ps->gen_pmcon_b & mask)
219  }
220  return prev_sleep_state;
221 }
222 
224 {
225  uint8_t *pmc;
226 
229 
230  printk(BIOS_DEBUG, "TCO_STS: %04x %04x\n", ps->tco1_sts, ps->tco2_sts);
231 
234 
235  pmc = pmc_mmio_regs();
236  ps->gblrst_cause[0] = read32(pmc + GBLRST_CAUSE0);
237  ps->gblrst_cause[1] = read32(pmc + GBLRST_CAUSE1);
238 
239  printk(BIOS_DEBUG, "GEN_PMCON: %08x %08x\n",
240  ps->gen_pmcon_a, ps->gen_pmcon_b);
241 
242  printk(BIOS_DEBUG, "GBLRST_CAUSE: %08x %08x\n",
243  ps->gblrst_cause[0], ps->gblrst_cause[1]);
244 }
245 
246 /* STM Support */
248 {
249  return ACPI_BASE_ADDRESS;
250 }
251 
252 /*
253  * Set which power state system will be after reapplying
254  * the power (from G3 State)
255  */
256 void pmc_soc_set_afterg3_en(const bool on)
257 {
258  uint8_t reg8;
259  const pci_devfn_t dev = PCH_DEV_PMC;
260 
261  reg8 = pci_read_config8(dev, GEN_PMCON_B);
262  if (on)
263  reg8 &= ~SLEEP_AFTER_POWER_FAIL;
264  else
265  reg8 |= SLEEP_AFTER_POWER_FAIL;
266  pci_write_config8(dev, GEN_PMCON_B, reg8);
267 }
#define GPE0_STS_BIT
Definition: pm.h:67
#define MCSMI_STS_BIT
Definition: pm.h:65
#define SMI_ON_SLP_EN_STS_BIT
Definition: pm.h:71
#define MONITOR_STS_BIT
Definition: pm.h:58
#define SPI_SMI_STS_BIT
Definition: pm.h:56
#define TCO_STS_BIT
Definition: pm.h:63
#define PERIODIC_STS_BIT
Definition: pm.h:62
#define GPIO_STS_BIT
Definition: pm.h:66
#define LEGACY_USB_STS_BIT
Definition: pm.h:72
#define ESPI_SMI_STS_BIT
Definition: pm.h:54
#define PM1_STS_BIT
Definition: pm.h:68
#define BIOS_STS_BIT
Definition: pm.h:73
#define PCI_EXP_SMI_STS_BIT
Definition: pm.h:59
#define GPIO_UNLOCK_SMI_STS_BIT
Definition: pm.h:55
#define APM_STS_BIT
Definition: pm.h:70
#define SMBUS_SMI_STS_BIT
Definition: pm.h:60
#define SERIRQ_SMI_STS_BIT
Definition: pm.h:61
#define DEVMON_STS_BIT
Definition: pm.h:64
#define SWSMI_TMR_STS_BIT
Definition: pm.h:69
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define WAK_STS
Definition: southbridge.h:27
#define printk(level,...)
Definition: stdlib.h:16
@ ACPI_S5
Definition: acpi.h:1385
@ ACPI_S3
Definition: acpi.h:1383
#define config_of_soc()
Definition: device.h:394
static __always_inline void pci_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define ACPI_BASE_ADDRESS
Definition: iomap.h:99
#define GBLRST_CAUSE0
Definition: pmc.h:131
#define MS4V
Definition: pmc.h:26
#define PWR_FLR
Definition: pmc.h:30
#define SLEEP_AFTER_POWER_FAIL
Definition: pmc.h:51
#define GBL_RST_STS
Definition: pmc.h:20
#define GEN_PMCON_B
Definition: pmc.h:53
#define ETR
Definition: pmc.h:63
#define DISB
Definition: pmc.h:21
#define GEN_PMCON_A
Definition: pmc.h:14
#define GBLRST_CAUSE1
Definition: pmc.h:133
#define RTC_BATTERY_DEAD
Definition: pmc.h:61
#define PWRMBASE
Definition: pmc.h:10
#define SUS_PWR_FLR
Definition: pmc.h:28
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
enum board_config config
Definition: memory.c:448
static __always_inline uint32_t * pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg)
Definition: pci_mmio_cfg.h:117
u32 pci_devfn_t
Definition: pci_type.h:8
static int prev_sleep_state(const struct chipset_power_state *ps)
Definition: power_state.c:36
#define PCH_DEVFN_PMC
Definition: pci_devs.h:217
#define PCH_DEV_PMC
Definition: pci_devs.h:236
void pmc_set_disb(void)
Definition: pmutil.c:121
const char *const * soc_std_gpe_sts_array(size_t *a)
Definition: pmutil.c:99
uint16_t get_pmbase(void)
Definition: pmutil.c:254
void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
Definition: pmutil.c:157
const char *const * soc_smi_sts_array(size_t *a)
Definition: pmutil.c:40
static int rtc_failed(uint32_t gen_pmcon_b)
Definition: pmutil.c:169
static int deep_s3_enabled(void)
Definition: pmutil.c:189
void pmc_soc_set_afterg3_en(const bool on)
Definition: pmutil.c:263
uint8_t * pmc_mmio_regs(void)
Definition: pmutil.c:142
int vbnv_cmos_failed(void)
Definition: pmutil.c:184
uint32_t * soc_pmc_etr_addr(void)
Definition: pmutil.c:152
int soc_prev_sleep_state(const struct chipset_power_state *ps, int prev_sleep_state)
Definition: pmutil.c:198
const char *const * soc_tco_sts_array(size_t *a)
Definition: pmutil.c:72
void soc_fill_power_state(struct chipset_power_state *ps)
Definition: pmutil.c:228
uintptr_t soc_read_pmc_base(void)
Definition: pmutil.c:147
#define TCO2_STS
Definition: smbus.h:9
#define TCO1_STS
Definition: smbus.h:7
int rtc_failure(void)
Definition: pmutil.c:330
static const int mask[4]
Definition: gpio.c:308
uint16_t tco_read_reg(uint16_t tco_reg)
Definition: tco.c:32
static struct tegra_pmc_regs * pmc
Definition: clock.c:19
#define DEVTREE_CONST
Definition: stddef.h:30
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
uint16_t tco2_sts
Definition: pm.h:146
uint16_t tco1_sts
Definition: pm.h:145
uint32_t gen_pmcon_b
Definition: pm.h:150
uint16_t pm1_sts
Definition: pm.h:142
uint32_t gblrst_cause[2]
Definition: pm.h:151
uint32_t gen_pmcon_a
Definition: pm.h:149