Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 1 | // 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 Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 12 | #include <dt-structs.h> |
| 13 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 15 | #include <spl.h> |
Simon Glass | 3cabcf9 | 2020-04-08 16:57:35 -0600 | [diff] [blame] | 16 | #include <acpi/acpi_s3.h> |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/pci.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 20 | #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 | |
| 50 | enum { |
| 51 | ETR = 0x1048, |
| 52 | CF9_LOCK = 1UL << 31, |
| 53 | CF9_GLB_RST = 1 << 20, |
| 54 | }; |
| 55 | |
| 56 | struct 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 | |
| 63 | static 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 | |
| 78 | static 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 | |
| 90 | static 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 | |
| 99 | static 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 | |
| 111 | int 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 | |
Simon Glass | ca60199 | 2020-09-22 12:45:23 -0600 | [diff] [blame] | 121 | ret = dev_read_u32_array(dev, "early-regs", base, |
| 122 | ARRAY_SIZE(base)); |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 123 | if (ret) |
| 124 | return log_msg_ret("Missing/short early-regs", ret); |
Simon Glass | 3d9acea | 2019-12-29 21:19:13 -0700 | [diff] [blame] | 125 | 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 Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 135 | upriv->acpi_base = base[4]; |
| 136 | |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 137 | /* 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 | |
| 169 | static int enable_pmcbar(struct udevice *dev) |
| 170 | { |
| 171 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 172 | struct apl_pmc_platdata *priv = dev_get_platdata(dev); |
| 173 | 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 | |
| 194 | static int apl_pmc_probe(struct udevice *dev) |
| 195 | { |
Simon Glass | 3d9acea | 2019-12-29 21:19:13 -0700 | [diff] [blame] | 196 | if (spl_phase() == PHASE_TPL) { |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 197 | return enable_pmcbar(dev); |
Simon Glass | 3d9acea | 2019-12-29 21:19:13 -0700 | [diff] [blame] | 198 | } 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 Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static 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 | |
| 215 | static const struct udevice_id apl_pmc_ids[] = { |
| 216 | { .compatible = "intel,apl-pmc" }, |
| 217 | { } |
| 218 | }; |
| 219 | |
Simon Glass | 9d20db0 | 2020-10-05 05:27:01 -0600 | [diff] [blame] | 220 | U_BOOT_DRIVER(intel_apl_pmc) = { |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 221 | .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, |
| 227 | .platdata_auto_alloc_size = sizeof(struct apl_pmc_platdata), |
| 228 | }; |