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