1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #define __SIMPLE_DEVICE__
9 #include <console/console.h>
10 #include <device/device.h>
11 #include <device/mmio.h>
12 #include <device/pci.h>
13 #include <device/pci_ops.h>
15 #include <soc/iomap.h>
17 #include <soc/pci_devs.h>
20 #include <security/vboot/vbnv.h>
22 uint16_t get_pmbase(void)
24 return pci_read_config16(PCI_DEV(0, PCU_DEV
, 0), ABASE
) & 0xfff8;
27 static void print_num_status_bits(int num_bits
, uint32_t status
, const char *const bit_names
[])
34 for (i
= num_bits
- 1; i
>= 0; i
--) {
35 if (status
& (1 << i
)) {
37 printk(BIOS_DEBUG
, "%s ", bit_names
[i
]);
39 printk(BIOS_DEBUG
, "BIT%d ", i
);
44 static uint32_t print_smi_status(uint32_t smi_sts
)
46 static const char *const smi_sts_bits
[] = {
69 printk(BIOS_DEBUG
, "SMI_STS: ");
70 print_num_status_bits(30, smi_sts
, smi_sts_bits
);
71 printk(BIOS_DEBUG
, "\n");
76 static uint32_t reset_smi_status(void)
78 uint16_t pmbase
= get_pmbase();
79 uint32_t smi_sts
= inl(pmbase
+ SMI_STS
);
80 outl(smi_sts
, pmbase
+ SMI_STS
);
84 uint32_t clear_smi_status(void)
86 return print_smi_status(reset_smi_status());
89 void enable_smi(uint32_t mask
)
91 uint16_t pmbase
= get_pmbase();
92 uint32_t smi_en
= inl(pmbase
+ SMI_EN
);
94 outl(smi_en
, pmbase
+ SMI_EN
);
97 void disable_smi(uint32_t mask
)
99 uint16_t pmbase
= get_pmbase();
100 uint32_t smi_en
= inl(pmbase
+ SMI_EN
);
102 outl(smi_en
, pmbase
+ SMI_EN
);
105 void enable_pm1_control(uint32_t mask
)
107 uint16_t pmbase
= get_pmbase();
108 uint32_t pm1_cnt
= inl(pmbase
+ PM1_CNT
);
110 outl(pm1_cnt
, pmbase
+ PM1_CNT
);
113 void disable_pm1_control(uint32_t mask
)
115 uint16_t pmbase
= get_pmbase();
116 uint32_t pm1_cnt
= inl(pmbase
+ PM1_CNT
);
118 outl(pm1_cnt
, pmbase
+ PM1_CNT
);
121 static uint16_t reset_pm1_status(void)
123 uint16_t pmbase
= get_pmbase();
124 uint16_t pm1_sts
= inw(pmbase
+ PM1_STS
);
125 outw(pm1_sts
, pmbase
+ PM1_STS
);
129 static uint16_t print_pm1_status(uint16_t pm1_sts
)
131 static const char *const pm1_sts_bits
[] = {
145 printk(BIOS_SPEW
, "PM1_STS: ");
146 print_num_status_bits(16, pm1_sts
, pm1_sts_bits
);
147 printk(BIOS_SPEW
, "\n");
152 uint16_t clear_pm1_status(void)
154 return print_pm1_status(reset_pm1_status());
157 void enable_pm1(uint16_t events
)
159 outw(events
, get_pmbase() + PM1_EN
);
162 static uint32_t print_tco_status(uint32_t tco_sts
)
164 static const char *const tco_sts_bits
[] = {
172 printk(BIOS_DEBUG
, "TCO_STS: ");
173 print_num_status_bits(18, tco_sts
, tco_sts_bits
);
174 printk(BIOS_DEBUG
, "\n");
179 static uint32_t reset_tco_status(void)
181 uint16_t pmbase
= get_pmbase();
182 uint32_t tco_sts
= inl(pmbase
+ TCO_STS
);
183 uint32_t tco_en
= inl(pmbase
+ TCO1_CNT
);
185 outl(tco_sts
, pmbase
+ TCO_STS
);
186 return tco_sts
& tco_en
;
189 uint32_t clear_tco_status(void)
191 return print_tco_status(reset_tco_status());
194 void enable_gpe(uint32_t mask
)
196 uint16_t pmbase
= get_pmbase();
197 uint32_t gpe0_en
= inl(pmbase
+ GPE0_EN
);
199 outl(gpe0_en
, pmbase
+ GPE0_EN
);
202 void disable_gpe(uint32_t mask
)
204 uint16_t pmbase
= get_pmbase();
205 uint32_t gpe0_en
= inl(pmbase
+ GPE0_EN
);
207 outl(gpe0_en
, pmbase
+ GPE0_EN
);
210 void disable_all_gpe(void)
215 static uint32_t reset_gpe_status(void)
217 uint16_t pmbase
= get_pmbase();
218 uint32_t gpe_sts
= inl(pmbase
+ GPE0_STS
);
219 outl(gpe_sts
, pmbase
+ GPE0_STS
);
223 static uint32_t print_gpe_sts(uint32_t gpe_sts
)
225 static const char *const gpe_sts_bits
[] = {
245 [24] = "CORE_GPIO_0",
246 [25] = "CORE_GPIO_1",
247 [26] = "CORE_GPIO_2",
248 [27] = "CORE_GPIO_3",
249 [28] = "CORE_GPIO_4",
250 [29] = "CORE_GPIO_5",
251 [30] = "CORE_GPIO_6",
252 [31] = "CORE_GPIO_7",
258 printk(BIOS_DEBUG
, "GPE0a_STS: ");
259 print_num_status_bits(32, gpe_sts
, gpe_sts_bits
);
260 printk(BIOS_DEBUG
, "\n");
265 uint32_t clear_gpe_status(void)
267 return print_gpe_sts(reset_gpe_status());
270 static uint32_t reset_alt_status(void)
272 uint16_t pmbase
= get_pmbase();
273 uint32_t alt_gpio_smi
= inl(pmbase
+ ALT_GPIO_SMI
);
274 outl(alt_gpio_smi
, pmbase
+ ALT_GPIO_SMI
);
278 static uint32_t print_alt_sts(uint32_t alt_gpio_smi
)
280 uint32_t alt_gpio_sts
;
281 static const char *const alt_gpio_smi_sts_bits
[] = {
292 [10] = "CORE_GPIO_2",
293 [11] = "CORE_GPIO_3",
294 [12] = "CORE_GPIO_4",
295 [13] = "CORE_GPIO_5",
296 [14] = "CORE_GPIO_6",
297 [15] = "CORE_GPIO_7",
300 /* Status bits are in the upper 16 bits. */
301 alt_gpio_sts
= alt_gpio_smi
>> 16;
305 printk(BIOS_DEBUG
, "ALT_GPIO_SMI: ");
306 print_num_status_bits(16, alt_gpio_sts
, alt_gpio_smi_sts_bits
);
307 printk(BIOS_DEBUG
, "\n");
312 uint32_t clear_alt_status(void)
314 return print_alt_sts(reset_alt_status());
317 void clear_pmc_status(void)
322 prsts
= read32p(PMC_BASE_ADDRESS
+ PRSTS
);
323 gen_pmcon1
= read32p(PMC_BASE_ADDRESS
+ GEN_PMCON1
);
325 /* Clear the status bits. The RPS field is cleared on a 0 write. */
326 write32p(PMC_BASE_ADDRESS
+ GEN_PMCON1
, gen_pmcon1
& ~RPS
);
327 write32p(PMC_BASE_ADDRESS
+ PRSTS
, prsts
);
330 int rtc_failure(void)
335 /* not usable in ramstage as GEN_PMCON1 gets reset */
339 gen_pmcon1
= read32((u32
*)(PMC_BASE_ADDRESS
+ GEN_PMCON1
));
341 rtc_fail
= !!(gen_pmcon1
& RPS
);
343 printk(BIOS_DEBUG
, "RTC failure.\n");
348 int vbnv_cmos_failed(void)
350 return rtc_failure();
353 int platform_is_resuming(void)
355 if (!(inw(ACPI_BASE_ADDRESS
+ PM1_STS
) & WAK_STS
))
358 return acpi_sleep_from_pm1(inl(ACPI_BASE_ADDRESS
+ PM1_CNT
)) == ACPI_S3
;
366 pm1_cnt
= inl(ACPI_BASE_ADDRESS
+ PM1_CNT
);
367 pm1_cnt
|= (0xf << 10);
368 outl(pm1_cnt
, ACPI_BASE_ADDRESS
+ PM1_CNT
);