blob: 22f53094a1146f4a03dc3906b52586097782ffc0 [file] [log] [blame]
Hannah Williams01bc8972016-02-04 20:13:34 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 * Copyright (C) 2015-2016 Intel Corp.
6 *
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, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Alexandru Gagniuca6339802016-04-05 12:40:24 -070018#define __SIMPLE_DEVICE__
19
Duncan Laurie2e790092016-09-19 12:05:49 -070020#include <arch/acpi.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080021#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020022#include <device/mmio.h>
Aaron Durbin3118b622017-09-15 11:48:53 -060023#include <cbmem.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080024#include <console/console.h>
Andrey Petrov3b637532016-11-30 17:39:16 -080025#include <cpu/x86/msr.h>
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070026#include <device/device.h>
27#include <device/pci.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080028#include <device/pci_def.h>
Barnali Sarkar66fe0c42017-05-23 18:17:14 +053029#include <intelblocks/msr.h>
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070030#include <intelblocks/pmclib.h>
Aaron Durbin3118b622017-09-15 11:48:53 -060031#include <intelblocks/rtc.h>
Subrata Banik7bc4dc52018-05-17 18:40:32 +053032#include <intelblocks/tco.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080033#include <soc/iomap.h>
Andrey Petrov3b637532016-11-30 17:39:16 -080034#include <soc/cpu.h>
Alexandru Gagniuca6339802016-04-05 12:40:24 -070035#include <soc/pci_devs.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080036#include <soc/pm.h>
Subrata Banik7bc4dc52018-05-17 18:40:32 +053037#include <soc/smbus.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020038#include <security/vboot/vbnv.h>
Elyes HAOUASadd76f92019-03-21 09:55:49 +010039
Duncan Lauriea673d1c2016-09-19 12:02:54 -070040#include "chip.h"
Hannah Williams01bc8972016-02-04 20:13:34 -080041
Alexandru Gagniuca6339802016-04-05 12:40:24 -070042static uintptr_t read_pmc_mmio_bar(void)
43{
Lijian Zhao91086802016-09-06 18:15:29 -070044 return PMC_BAR0;
Alexandru Gagniuca6339802016-04-05 12:40:24 -070045}
Hannah Williams01bc8972016-02-04 20:13:34 -080046
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070047uintptr_t soc_read_pmc_base(void)
Shaunak Saha9a0c9ac2016-06-27 23:00:15 -070048{
49 return read_pmc_mmio_bar();
50}
51
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070052const char *const *soc_smi_sts_array(size_t *a)
Hannah Williams01bc8972016-02-04 20:13:34 -080053{
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070054 static const char *const smi_sts_bits[] = {
Aaron Durbin7929dd02016-06-10 18:01:45 -050055 [BIOS_SMI_STS] = "BIOS",
56 [LEGACY_USB_SMI_STS] = "LEGACY USB",
57 [SLP_SMI_STS] = "SLP_SMI",
58 [APM_SMI_STS] = "APM",
59 [SWSMI_TMR_SMI_STS] = "SWSMI_TMR",
Aaron Durbina554b712016-06-10 18:04:21 -050060 [FAKE_PM1_SMI_STS] = "PM1",
Lee Leahy320b7ca2017-03-09 09:42:48 -080061 [GPIO_SMI_STS] = "GPIO_SMI",
62 [GPIO_UNLOCK_SMI_STS] = "GPIO_UNLOCK_SSMI",
Aaron Durbin7929dd02016-06-10 18:01:45 -050063 [MC_SMI_STS] = "MCSMI",
64 [TCO_SMI_STS] = "TCO",
65 [PERIODIC_SMI_STS] = "PERIODIC",
66 [SERIRQ_SMI_STS] = "SERIRQ",
67 [SMBUS_SMI_STS] = "SMBUS_SMI",
68 [XHCI_SMI_STS] = "XHCI",
69 [HSMBUS_SMI_STS] = "HOST_SMBUS",
70 [SCS_SMI_STS] = "SCS",
71 [PCIE_SMI_STS] = "PCI_EXP_SMI",
72 [SCC2_SMI_STS] = "SCC2",
73 [SPI_SSMI_STS] = "SPI_SSMI",
74 [SPI_SMI_STS] = "SPI",
75 [PMC_OCP_SMI_STS] = "OCP_CSE",
Hannah Williams01bc8972016-02-04 20:13:34 -080076 };
77
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070078 *a = ARRAY_SIZE(smi_sts_bits);
79 return smi_sts_bits;
Hannah Williams01bc8972016-02-04 20:13:34 -080080}
81
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070082/*
83 * For APL/GLK this check for power button status if nothing else
84 * is indicating an SMI and SMIs aren't turned into SCIs.
85 * Apparently, there is no PM1 status bit in the SMI status
86 * register. That makes things difficult for
87 * determining if the power button caused an SMI.
88 */
89uint32_t soc_get_smi_status(uint32_t generic_sts)
Hannah Williams01bc8972016-02-04 20:13:34 -080090{
Furquan Shaikh43810d92017-10-16 22:22:46 -070091 if (generic_sts == 0 && !(pmc_read_pm1_control() & SCI_EN)) {
Barnali Sarkar9e55ff62017-06-05 20:01:14 +053092 uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
Aaron Durbina554b712016-06-10 18:04:21 -050093
94 /* Fake PM1 status bit if power button pressed. */
95 if (pm1_sts & PWRBTN_STS)
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070096 generic_sts |= (1 << FAKE_PM1_SMI_STS);
Aaron Durbina554b712016-06-10 18:04:21 -050097 }
98
Shaunak Saha93cdc8b2017-04-18 15:42:09 -070099 return generic_sts;
Hannah Williams01bc8972016-02-04 20:13:34 -0800100}
101
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700102const char *const *soc_tco_sts_array(size_t *a)
Hannah Williams01bc8972016-02-04 20:13:34 -0800103{
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700104 static const char *const tco_sts_bits[] = {
Hannah Williams01bc8972016-02-04 20:13:34 -0800105 [3] = "TIMEOUT",
106 [17] = "SECOND_TO",
107 };
108
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700109 *a = ARRAY_SIZE(tco_sts_bits);
110 return tco_sts_bits;
Hannah Williams01bc8972016-02-04 20:13:34 -0800111}
112
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700113const char *const *soc_std_gpe_sts_array(size_t *a)
Hannah Williams01bc8972016-02-04 20:13:34 -0800114{
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700115 static const char *const gpe_sts_bits[] = {
Hannah Williams01bc8972016-02-04 20:13:34 -0800116 [0] = "PCIE_SCI",
117 [2] = "SWGPE",
118 [3] = "PCIE_WAKE0",
119 [4] = "PUNIT",
120 [6] = "PCIE_WAKE1",
121 [7] = "PCIE_WAKE2",
122 [8] = "PCIE_WAKE3",
123 [9] = "PCI_EXP",
124 [10] = "BATLOW",
125 [11] = "CSE_PME",
126 [12] = "XDCI_PME",
127 [13] = "XHCI_PME",
128 [14] = "AVS_PME",
129 [15] = "GPIO_TIER1_SCI",
130 [16] = "SMB_WAK",
131 [17] = "SATA_PME",
132 };
133
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700134 *a = ARRAY_SIZE(gpe_sts_bits);
135 return gpe_sts_bits;
Hannah Williams01bc8972016-02-04 20:13:34 -0800136}
137
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700138void soc_clear_pm_registers(uintptr_t pmc_bar)
Duncan Laurie2e790092016-09-19 12:05:49 -0700139{
Hannah Williams01bc8972016-02-04 20:13:34 -0800140 uint32_t gen_pmcon1;
141
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700142 gen_pmcon1 = read32((void *)(pmc_bar + GEN_PMCON1));
Hannah Williams01bc8972016-02-04 20:13:34 -0800143 /* Clear the status bits. The RPS field is cleared on a 0 write. */
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700144 write32((void *)(pmc_bar + GEN_PMCON1), gen_pmcon1 & ~RPS);
Hannah Williams01bc8972016-02-04 20:13:34 -0800145}
146
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700147void soc_get_gpi_gpe_configs(uint8_t *dw0, uint8_t *dw1, uint8_t *dw2)
Hannah Williams01bc8972016-02-04 20:13:34 -0800148{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500149 DEVTREE_CONST struct soc_intel_apollolake_config *config;
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700150
151 /* Look up the device in devicetree */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500152 DEVTREE_CONST struct device *dev = dev_find_slot(0, SA_DEVFN_ROOT);
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700153 if (!dev || !dev->chip_info) {
154 printk(BIOS_ERR, "BUG! Could not find SOC devicetree config\n");
155 return;
156 }
157 config = dev->chip_info;
158
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700159 /* Assign to out variable */
160 *dw0 = config->gpe0_dw1;
161 *dw1 = config->gpe0_dw2;
162 *dw2 = config->gpe0_dw3;
163}
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700164
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700165void soc_fill_power_state(struct chipset_power_state *ps)
166{
167 uintptr_t pmc_bar0 = read_pmc_mmio_bar();
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700168
Subrata Banik7bc4dc52018-05-17 18:40:32 +0530169 ps->tco1_sts = tco_read_reg(TCO1_STS);
170 ps->tco2_sts = tco_read_reg(TCO2_STS);
171
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700172 ps->prsts = read32((void *)(pmc_bar0 + PRSTS));
173 ps->gen_pmcon1 = read32((void *)(pmc_bar0 + GEN_PMCON1));
174 ps->gen_pmcon2 = read32((void *)(pmc_bar0 + GEN_PMCON2));
175 ps->gen_pmcon3 = read32((void *)(pmc_bar0 + GEN_PMCON3));
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700176
Subrata Banik7bc4dc52018-05-17 18:40:32 +0530177 printk(BIOS_DEBUG, "prsts: %08x\n",
178 ps->prsts);
179 printk(BIOS_DEBUG, "tco_sts: %04x %04x\n",
180 ps->tco1_sts, ps->tco2_sts);
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700181 printk(BIOS_DEBUG,
182 "gen_pmcon1: %08x gen_pmcon2: %08x gen_pmcon3: %08x\n",
183 ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3);
Duncan Lauriea673d1c2016-09-19 12:02:54 -0700184}
Andrey Petrov3b637532016-11-30 17:39:16 -0800185
Hannah Williamscdecc0d2018-01-04 11:57:14 -0800186/* Return 0, 3, or 5 to indicate the previous sleep state. */
187int soc_prev_sleep_state(const struct chipset_power_state *ps,
188 int prev_sleep_state)
189{
190 /* WAK_STS bit will not be set when waking from G3 state */
191
192 if (!(ps->pm1_sts & WAK_STS) && (ps->gen_pmcon1 & COLD_BOOT_STS))
193 prev_sleep_state = ACPI_S5;
194 return prev_sleep_state;
195}
196
Andrey Petrov3b637532016-11-30 17:39:16 -0800197void enable_pm_timer_emulation(void)
198{
199 /* ACPI PM timer emulation */
200 msr_t msr;
201 /*
202 * The derived frequency is calculated as follows:
203 * (CTC_FREQ * msr[63:32]) >> 32 = target frequency.
204 * Back solve the multiplier so the 3.579545MHz ACPI timer
205 * frequency is used.
206 */
207 msr.hi = (3579545ULL << 32) / CTC_FREQ;
Shaunak Saha93cdc8b2017-04-18 15:42:09 -0700208 /* Set PM1 timer IO port and enable */
Barnali Sarkar9e55ff62017-06-05 20:01:14 +0530209 msr.lo = EMULATE_PM_TMR_EN | (ACPI_BASE_ADDRESS + R_ACPI_PM1_TMR);
Elyes HAOUASf212cf32018-12-18 10:24:55 +0100210 wrmsr(MSR_EMULATE_PM_TIMER, msr);
Andrey Petrov3b637532016-11-30 17:39:16 -0800211}
Aaron Durbin3118b622017-09-15 11:48:53 -0600212
213static int rtc_failed(uint32_t gen_pmcon1)
214{
215 return !!(gen_pmcon1 & RPS);
216}
217
218int soc_get_rtc_failed(void)
219{
220 const struct chipset_power_state *ps = cbmem_find(CBMEM_ID_POWER_STATE);
221
222 if (!ps) {
223 printk(BIOS_ERR, "Could not find power state in cbmem, RTC init aborted\n");
224 return 1;
225 }
226
227 return rtc_failed(ps->gen_pmcon1);
228}
Aaron Durbin0990fbf2017-09-15 15:23:04 -0600229
230int vbnv_cmos_failed(void)
231{
Furquan Shaikh9d079102018-02-02 15:11:29 -0800232 uintptr_t pmc_bar = read_pmc_mmio_bar();
233 uint32_t gen_pmcon1 = read32((void *)(pmc_bar + GEN_PMCON1));
234 int rtc_failure = rtc_failed(gen_pmcon1);
235
236 if (rtc_failure) {
237 printk(BIOS_INFO, "RTC failed!\n");
238
239 /* We do not want to write 1 to clear-1 bits. Set them to 0. */
240 gen_pmcon1 &= ~GEN_PMCON1_CLR1_BITS;
241
242 /* RPS is write 0 to clear. */
243 gen_pmcon1 &= ~RPS;
244
245 write32((void *)(pmc_bar + GEN_PMCON1), gen_pmcon1);
246 }
247
248 return rtc_failure;
Aaron Durbin0990fbf2017-09-15 15:23:04 -0600249}