blob: 192dec7109aa3ece3cf26e9a6b825d9611927db0 [file] [log] [blame]
Simon Glasse63ca972019-12-06 21:42:57 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017 Intel Corporation.
4 * Copyright 2019 Google LLC
5 *
6 * Modified from coreboot pmclib.c, pmc.c and pmutil.c
7 */
8
9#define LOG_CATEGORY UCLASS_ACPI_PMC
10
11#include <common.h>
Simon Glasse63ca972019-12-06 21:42:57 -070012#include <dt-structs.h>
13#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glasse63ca972019-12-06 21:42:57 -070015#include <spl.h>
Simon Glass50461092020-04-08 16:57:35 -060016#include <acpi/acpi_s3.h>
Simon Glasse63ca972019-12-06 21:42:57 -070017#include <asm/io.h>
18#include <asm/pci.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glasse63ca972019-12-06 21:42:57 -070020#include <power/acpi_pmc.h>
21
22#define GPIO_GPE_CFG 0x1050
23
24/* Memory mapped IO registers behind PMC_BASE_ADDRESS */
25#define PRSTS 0x1000
26#define GEN_PMCON1 0x1020
27#define COLD_BOOT_STS BIT(27)
28#define COLD_RESET_STS BIT(26)
29#define WARM_RESET_STS BIT(25)
30#define GLOBAL_RESET_STS BIT(24)
31#define SRS BIT(20)
32#define MS4V BIT(18)
33#define RPS BIT(2)
34#define GEN_PMCON1_CLR1_BITS (COLD_BOOT_STS | COLD_RESET_STS | \
35 WARM_RESET_STS | GLOBAL_RESET_STS | \
36 SRS | MS4V)
37#define GEN_PMCON2 0x1024
38#define GEN_PMCON3 0x1028
39
40/* Offset of TCO registers from ACPI base I/O address */
41#define TCO_REG_OFFSET 0x60
42#define TCO1_STS 0x64
43#define DMISCI_STS BIT(9)
44#define BOOT_STS BIT(18)
45#define TCO2_STS 0x66
46#define TCO1_CNT 0x68
47#define TCO_LOCK BIT(12)
48#define TCO2_CNT 0x6a
49
50enum {
51 ETR = 0x1048,
52 CF9_LOCK = 1UL << 31,
53 CF9_GLB_RST = 1 << 20,
54};
55
56struct apl_pmc_platdata {
57#if CONFIG_IS_ENABLED(OF_PLATDATA)
58 struct dtd_intel_apl_pmc dtplat;
59#endif
60 pci_dev_t bdf;
61};
62
63static int apl_pmc_fill_power_state(struct udevice *dev)
64{
65 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
66
67 upriv->tco1_sts = inw(upriv->acpi_base + TCO1_STS);
68 upriv->tco2_sts = inw(upriv->acpi_base + TCO2_STS);
69
70 upriv->prsts = readl(upriv->pmc_bar0 + PRSTS);
71 upriv->gen_pmcon1 = readl(upriv->pmc_bar0 + GEN_PMCON1);
72 upriv->gen_pmcon2 = readl(upriv->pmc_bar0 + GEN_PMCON2);
73 upriv->gen_pmcon3 = readl(upriv->pmc_bar0 + GEN_PMCON3);
74
75 return 0;
76}
77
78static int apl_prev_sleep_state(struct udevice *dev, int prev_sleep_state)
79{
80 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
81
82 /* WAK_STS bit will not be set when waking from G3 state */
83 if (!(upriv->pm1_sts & WAK_STS) &&
84 (upriv->gen_pmcon1 & COLD_BOOT_STS))
85 prev_sleep_state = ACPI_S5;
86
87 return prev_sleep_state;
88}
89
90static int apl_disable_tco(struct udevice *dev)
91{
92 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
93
94 pmc_disable_tco_base(upriv->acpi_base + TCO_REG_OFFSET);
95
96 return 0;
97}
98
99static int apl_global_reset_set_enable(struct udevice *dev, bool enable)
100{
101 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
102
103 if (enable)
104 setbits_le32(upriv->pmc_bar0 + ETR, CF9_GLB_RST);
105 else
106 clrbits_le32(upriv->pmc_bar0 + ETR, CF9_GLB_RST);
107
108 return 0;
109}
110
111int apl_pmc_ofdata_to_uc_platdata(struct udevice *dev)
112{
113 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
114 struct apl_pmc_platdata *plat = dev_get_platdata(dev);
115
116#if !CONFIG_IS_ENABLED(OF_PLATDATA)
117 u32 base[6];
118 int size;
119 int ret;
120
121 ret = dev_read_u32_array(dev, "early-regs", base, ARRAY_SIZE(base));
122 if (ret)
123 return log_msg_ret("Missing/short early-regs", ret);
Simon Glassbaf7bd42019-12-29 21:19:13 -0700124 if (spl_phase() == PHASE_TPL) {
125 upriv->pmc_bar0 = (void *)base[0];
126 upriv->pmc_bar2 = (void *)base[2];
127
128 /* Since PCI is not enabled, we must get the BDF manually */
129 plat->bdf = pci_get_devfn(dev);
130 if (plat->bdf < 0)
131 return log_msg_ret("Cannot get PMC PCI address",
132 plat->bdf);
133 }
Simon Glasse63ca972019-12-06 21:42:57 -0700134 upriv->acpi_base = base[4];
135
136 /* Since PCI is not enabled, we must get the BDF manually */
137 plat->bdf = pci_get_devfn(dev);
138 if (plat->bdf < 0)
139 return log_msg_ret("Cannot get PMC PCI address", plat->bdf);
140
141 /* Get the dwX values for pmc gpe settings */
142 size = dev_read_size(dev, "gpe0-dw");
143 if (size < 0)
144 return log_msg_ret("Cannot read gpe0-dm", size);
145 upriv->gpe0_count = size / sizeof(u32);
146 ret = dev_read_u32_array(dev, "gpe0-dw", upriv->gpe0_dw,
147 upriv->gpe0_count);
148 if (ret)
149 return log_msg_ret("Bad gpe0-dw", ret);
150
151 return pmc_ofdata_to_uc_platdata(dev);
152#else
153 struct dtd_intel_apl_pmc *dtplat = &plat->dtplat;
154
155 plat->bdf = pci_ofplat_get_devfn(dtplat->reg[0]);
156 upriv->pmc_bar0 = (void *)dtplat->early_regs[0];
157 upriv->pmc_bar2 = (void *)dtplat->early_regs[2];
158 upriv->acpi_base = dtplat->early_regs[4];
159 upriv->gpe0_dwx_mask = dtplat->gpe0_dwx_mask;
160 upriv->gpe0_dwx_shift_base = dtplat->gpe0_dwx_shift_base;
161 upriv->gpe0_sts_reg = dtplat->gpe0_sts;
162 upriv->gpe0_sts_reg += upriv->acpi_base;
163 upriv->gpe0_en_reg = dtplat->gpe0_en;
164 upriv->gpe0_en_reg += upriv->acpi_base;
165 upriv->gpe0_count = min((int)ARRAY_SIZE(dtplat->gpe0_dw), GPE0_REG_MAX);
166 memcpy(upriv->gpe0_dw, dtplat->gpe0_dw, sizeof(dtplat->gpe0_dw));
167#endif
168 upriv->gpe_cfg = (u32 *)(upriv->pmc_bar0 + GPIO_GPE_CFG);
169
170 return 0;
171}
172
173static int enable_pmcbar(struct udevice *dev)
174{
175 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
176 struct apl_pmc_platdata *priv = dev_get_platdata(dev);
177 pci_dev_t pmc = priv->bdf;
178
179 /*
180 * Set PMC base addresses and enable decoding. BARs 1 and 3 are 64-bit
181 * BARs.
182 */
183 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_0, (ulong)upriv->pmc_bar0,
184 PCI_SIZE_32);
185 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32);
186 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_2, (ulong)upriv->pmc_bar2,
187 PCI_SIZE_32);
188 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_3, 0, PCI_SIZE_32);
189 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_4, upriv->acpi_base,
190 PCI_SIZE_16);
191 pci_x86_write_config(pmc, PCI_COMMAND, PCI_COMMAND_IO |
192 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
193 PCI_SIZE_16);
194
195 return 0;
196}
197
198static int apl_pmc_probe(struct udevice *dev)
199{
Simon Glassbaf7bd42019-12-29 21:19:13 -0700200 if (spl_phase() == PHASE_TPL) {
Simon Glasse63ca972019-12-06 21:42:57 -0700201 return enable_pmcbar(dev);
Simon Glassbaf7bd42019-12-29 21:19:13 -0700202 } else {
203 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
204
205 upriv->pmc_bar0 = (void *)dm_pci_read_bar32(dev, 0);
206 upriv->pmc_bar2 = (void *)dm_pci_read_bar32(dev, 2);
207 }
Simon Glasse63ca972019-12-06 21:42:57 -0700208
209 return 0;
210}
211
212static struct acpi_pmc_ops apl_pmc_ops = {
213 .init = apl_pmc_fill_power_state,
214 .prev_sleep_state = apl_prev_sleep_state,
215 .disable_tco = apl_disable_tco,
216 .global_reset_set_enable = apl_global_reset_set_enable,
217};
218
219static const struct udevice_id apl_pmc_ids[] = {
220 { .compatible = "intel,apl-pmc" },
221 { }
222};
223
224U_BOOT_DRIVER(apl_pmc) = {
225 .name = "intel_apl_pmc",
226 .id = UCLASS_ACPI_PMC,
227 .of_match = apl_pmc_ids,
228 .ofdata_to_platdata = apl_pmc_ofdata_to_uc_platdata,
229 .probe = apl_pmc_probe,
230 .ops = &apl_pmc_ops,
231 .platdata_auto_alloc_size = sizeof(struct apl_pmc_platdata),
232};