blob: f186c504b3fba6970d519b7ed320a07811b5b274 [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);
Simon Glassfa20e932020-12-03 16:55:20 -0700114 struct apl_pmc_platdata *plat = dev_get_plat(dev);
Simon Glasse63ca972019-12-06 21:42:57 -0700115
116#if !CONFIG_IS_ENABLED(OF_PLATDATA)
117 u32 base[6];
118 int size;
119 int ret;
120
Simon Glassf77b9e22020-09-22 12:45:23 -0600121 ret = dev_read_u32_array(dev, "early-regs", base,
122 ARRAY_SIZE(base));
Simon Glasse63ca972019-12-06 21:42:57 -0700123 if (ret)
124 return log_msg_ret("Missing/short early-regs", ret);
Simon Glassbaf7bd42019-12-29 21:19:13 -0700125 if (spl_phase() == PHASE_TPL) {
126 upriv->pmc_bar0 = (void *)base[0];
127 upriv->pmc_bar2 = (void *)base[2];
128
129 /* Since PCI is not enabled, we must get the BDF manually */
130 plat->bdf = pci_get_devfn(dev);
131 if (plat->bdf < 0)
132 return log_msg_ret("Cannot get PMC PCI address",
133 plat->bdf);
134 }
Simon Glasse63ca972019-12-06 21:42:57 -0700135 upriv->acpi_base = base[4];
136
Simon Glasse63ca972019-12-06 21:42:57 -0700137 /* Get the dwX values for pmc gpe settings */
138 size = dev_read_size(dev, "gpe0-dw");
139 if (size < 0)
140 return log_msg_ret("Cannot read gpe0-dm", size);
141 upriv->gpe0_count = size / sizeof(u32);
142 ret = dev_read_u32_array(dev, "gpe0-dw", upriv->gpe0_dw,
143 upriv->gpe0_count);
144 if (ret)
145 return log_msg_ret("Bad gpe0-dw", ret);
146
147 return pmc_ofdata_to_uc_platdata(dev);
148#else
149 struct dtd_intel_apl_pmc *dtplat = &plat->dtplat;
150
151 plat->bdf = pci_ofplat_get_devfn(dtplat->reg[0]);
152 upriv->pmc_bar0 = (void *)dtplat->early_regs[0];
153 upriv->pmc_bar2 = (void *)dtplat->early_regs[2];
154 upriv->acpi_base = dtplat->early_regs[4];
155 upriv->gpe0_dwx_mask = dtplat->gpe0_dwx_mask;
156 upriv->gpe0_dwx_shift_base = dtplat->gpe0_dwx_shift_base;
157 upriv->gpe0_sts_reg = dtplat->gpe0_sts;
158 upriv->gpe0_sts_reg += upriv->acpi_base;
159 upriv->gpe0_en_reg = dtplat->gpe0_en;
160 upriv->gpe0_en_reg += upriv->acpi_base;
161 upriv->gpe0_count = min((int)ARRAY_SIZE(dtplat->gpe0_dw), GPE0_REG_MAX);
162 memcpy(upriv->gpe0_dw, dtplat->gpe0_dw, sizeof(dtplat->gpe0_dw));
163#endif
164 upriv->gpe_cfg = (u32 *)(upriv->pmc_bar0 + GPIO_GPE_CFG);
165
166 return 0;
167}
168
169static int enable_pmcbar(struct udevice *dev)
170{
171 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700172 struct apl_pmc_platdata *priv = dev_get_plat(dev);
Simon Glasse63ca972019-12-06 21:42:57 -0700173 pci_dev_t pmc = priv->bdf;
174
175 /*
176 * Set PMC base addresses and enable decoding. BARs 1 and 3 are 64-bit
177 * BARs.
178 */
179 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_0, (ulong)upriv->pmc_bar0,
180 PCI_SIZE_32);
181 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32);
182 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_2, (ulong)upriv->pmc_bar2,
183 PCI_SIZE_32);
184 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_3, 0, PCI_SIZE_32);
185 pci_x86_write_config(pmc, PCI_BASE_ADDRESS_4, upriv->acpi_base,
186 PCI_SIZE_16);
187 pci_x86_write_config(pmc, PCI_COMMAND, PCI_COMMAND_IO |
188 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
189 PCI_SIZE_16);
190
191 return 0;
192}
193
194static int apl_pmc_probe(struct udevice *dev)
195{
Simon Glassbaf7bd42019-12-29 21:19:13 -0700196 if (spl_phase() == PHASE_TPL) {
Simon Glasse63ca972019-12-06 21:42:57 -0700197 return enable_pmcbar(dev);
Simon Glassbaf7bd42019-12-29 21:19:13 -0700198 } else {
199 struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev);
200
201 upriv->pmc_bar0 = (void *)dm_pci_read_bar32(dev, 0);
202 upriv->pmc_bar2 = (void *)dm_pci_read_bar32(dev, 2);
203 }
Simon Glasse63ca972019-12-06 21:42:57 -0700204
205 return 0;
206}
207
208static struct acpi_pmc_ops apl_pmc_ops = {
209 .init = apl_pmc_fill_power_state,
210 .prev_sleep_state = apl_prev_sleep_state,
211 .disable_tco = apl_disable_tco,
212 .global_reset_set_enable = apl_global_reset_set_enable,
213};
214
215static const struct udevice_id apl_pmc_ids[] = {
216 { .compatible = "intel,apl-pmc" },
217 { }
218};
219
Simon Glassa055da82020-10-05 05:27:01 -0600220U_BOOT_DRIVER(intel_apl_pmc) = {
Simon Glasse63ca972019-12-06 21:42:57 -0700221 .name = "intel_apl_pmc",
222 .id = UCLASS_ACPI_PMC,
223 .of_match = apl_pmc_ids,
224 .ofdata_to_platdata = apl_pmc_ofdata_to_uc_platdata,
225 .probe = apl_pmc_probe,
226 .ops = &apl_pmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700227 .plat_auto = sizeof(struct apl_pmc_platdata),
Simon Glasse63ca972019-12-06 21:42:57 -0700228};