Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY UCLASS_ACPI_PMC |
| 7 | |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <log.h> |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Simon Glass | 3cabcf9 | 2020-04-08 16:57:35 -0600 | [diff] [blame] | 11 | #include <acpi/acpi_s3.h> |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 12 | #ifdef CONFIG_X86 |
| 13 | #include <asm/intel_pinctrl.h> |
| 14 | #endif |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <power/acpi_pmc.h> |
| 17 | |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 18 | struct tco_regs { |
| 19 | u32 tco_rld; |
| 20 | u32 tco_sts; |
| 21 | u32 tco1_cnt; |
| 22 | u32 tco_tmr; |
| 23 | }; |
| 24 | |
| 25 | enum { |
| 26 | TCO_STS_TIMEOUT = 1 << 3, |
| 27 | TCO_STS_SECOND_TO_STS = 1 << 17, |
| 28 | TCO1_CNT_HLT = 1 << 11, |
| 29 | }; |
| 30 | |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 31 | #ifdef CONFIG_X86 |
| 32 | static int gpe0_shift(struct acpi_pmc_upriv *upriv, int regnum) |
| 33 | { |
| 34 | return upriv->gpe0_dwx_shift_base + regnum * 4; |
| 35 | } |
| 36 | |
| 37 | int pmc_gpe_init(struct udevice *dev) |
| 38 | { |
| 39 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 40 | struct udevice *itss; |
| 41 | u32 *dw; |
| 42 | u32 gpio_cfg_mask; |
| 43 | u32 gpio_cfg; |
| 44 | int ret, i; |
| 45 | u32 mask; |
| 46 | |
| 47 | if (device_get_uclass_id(dev) != UCLASS_ACPI_PMC) |
| 48 | return log_msg_ret("uclass", -EPROTONOSUPPORT); |
| 49 | dw = upriv->gpe0_dw; |
| 50 | mask = upriv->gpe0_dwx_mask; |
| 51 | gpio_cfg_mask = 0; |
| 52 | for (i = 0; i < upriv->gpe0_count; i++) { |
| 53 | gpio_cfg_mask |= mask << gpe0_shift(upriv, i); |
| 54 | if (dw[i] & ~mask) |
| 55 | return log_msg_ret("Base GPE0 value", -EINVAL); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Route the GPIOs to the GPE0 block. Determine that all values |
| 60 | * are different and if they aren't, use the reset values. |
| 61 | */ |
| 62 | if (dw[0] == dw[1] || dw[1] == dw[2]) { |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 63 | if (spl_phase() > PHASE_TPL) |
| 64 | log_info("PMC: Using default GPE route"); |
Simon Glass | 28eefef | 2019-12-06 21:42:57 -0700 | [diff] [blame] | 65 | gpio_cfg = readl(upriv->gpe_cfg); |
| 66 | for (i = 0; i < upriv->gpe0_count; i++) |
| 67 | dw[i] = gpio_cfg >> gpe0_shift(upriv, i); |
| 68 | } else { |
| 69 | gpio_cfg = 0; |
| 70 | for (i = 0; i < upriv->gpe0_count; i++) |
| 71 | gpio_cfg |= dw[i] << gpe0_shift(upriv, i); |
| 72 | clrsetbits_le32(upriv->gpe_cfg, gpio_cfg_mask, gpio_cfg); |
| 73 | } |
| 74 | |
| 75 | /* Set the routes in the GPIO communities as well */ |
| 76 | ret = uclass_first_device_err(UCLASS_IRQ, &itss); |
| 77 | if (ret) |
| 78 | return log_msg_ret("Cannot find itss", ret); |
| 79 | pinctrl_route_gpe(itss, dw[0], dw[1], dw[2]); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | #endif /* CONFIG_X86 */ |
| 84 | |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 85 | static void pmc_fill_pm_reg_info(struct udevice *dev) |
| 86 | { |
| 87 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 88 | int i; |
| 89 | |
| 90 | upriv->pm1_sts = inw(upriv->acpi_base + PM1_STS); |
| 91 | upriv->pm1_en = inw(upriv->acpi_base + PM1_EN); |
| 92 | upriv->pm1_cnt = inw(upriv->acpi_base + PM1_CNT); |
| 93 | |
| 94 | log_debug("pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n", |
| 95 | upriv->pm1_sts, upriv->pm1_en, upriv->pm1_cnt); |
| 96 | |
| 97 | for (i = 0; i < GPE0_REG_MAX; i++) { |
| 98 | upriv->gpe0_sts[i] = inl(upriv->acpi_base + GPE0_STS + i * 4); |
| 99 | upriv->gpe0_en[i] = inl(upriv->acpi_base + GPE0_EN + i * 4); |
| 100 | log_debug("gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n", i, |
| 101 | upriv->gpe0_sts[i], i, upriv->gpe0_en[i]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | int pmc_disable_tco_base(ulong tco_base) |
| 106 | { |
| 107 | struct tco_regs *regs = (struct tco_regs *)tco_base; |
| 108 | |
| 109 | debug("tco_base %lx = %x\n", (ulong)®s->tco1_cnt, TCO1_CNT_HLT); |
| 110 | setio_32(®s->tco1_cnt, TCO1_CNT_HLT); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int pmc_init(struct udevice *dev) |
| 116 | { |
| 117 | const struct acpi_pmc_ops *ops = acpi_pmc_get_ops(dev); |
| 118 | int ret; |
| 119 | |
| 120 | pmc_fill_pm_reg_info(dev); |
| 121 | if (!ops->init) |
| 122 | return -ENOSYS; |
| 123 | |
| 124 | ret = ops->init(dev); |
| 125 | if (ret) |
| 126 | return log_msg_ret("Failed to init pmc", ret); |
| 127 | |
| 128 | #ifdef DEBUG |
| 129 | pmc_dump_info(dev); |
| 130 | #endif |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int pmc_prev_sleep_state(struct udevice *dev) |
| 136 | { |
| 137 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 138 | const struct acpi_pmc_ops *ops = acpi_pmc_get_ops(dev); |
| 139 | int prev_sleep_state = ACPI_S0; /* Default to S0 */ |
| 140 | |
| 141 | if (upriv->pm1_sts & WAK_STS) { |
| 142 | switch (acpi_sleep_from_pm1(upriv->pm1_cnt)) { |
| 143 | case ACPI_S3: |
| 144 | if (IS_ENABLED(HAVE_ACPI_RESUME)) |
| 145 | prev_sleep_state = ACPI_S3; |
| 146 | break; |
| 147 | case ACPI_S5: |
| 148 | prev_sleep_state = ACPI_S5; |
| 149 | break; |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | /* Clear SLP_TYP */ |
| 155 | outl(upriv->pm1_cnt & ~SLP_TYP, upriv->acpi_base + PM1_CNT); |
| 156 | } |
| 157 | |
| 158 | if (!ops->prev_sleep_state) |
| 159 | return prev_sleep_state; |
| 160 | |
| 161 | return ops->prev_sleep_state(dev, prev_sleep_state); |
| 162 | } |
| 163 | |
| 164 | int pmc_disable_tco(struct udevice *dev) |
| 165 | { |
| 166 | const struct acpi_pmc_ops *ops = acpi_pmc_get_ops(dev); |
| 167 | |
| 168 | pmc_fill_pm_reg_info(dev); |
| 169 | if (!ops->disable_tco) |
| 170 | return -ENOSYS; |
| 171 | |
| 172 | return ops->disable_tco(dev); |
| 173 | } |
| 174 | |
| 175 | int pmc_global_reset_set_enable(struct udevice *dev, bool enable) |
| 176 | { |
| 177 | const struct acpi_pmc_ops *ops = acpi_pmc_get_ops(dev); |
| 178 | |
| 179 | if (!ops->global_reset_set_enable) |
| 180 | return -ENOSYS; |
| 181 | |
| 182 | return ops->global_reset_set_enable(dev, enable); |
| 183 | } |
| 184 | |
| 185 | void pmc_dump_info(struct udevice *dev) |
| 186 | { |
| 187 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 188 | int i; |
| 189 | |
| 190 | printf("Device: %s\n", dev->name); |
| 191 | printf("ACPI base %x, pmc_bar0 %p, pmc_bar2 %p, gpe_cfg %p\n", |
| 192 | upriv->acpi_base, upriv->pmc_bar0, upriv->pmc_bar2, |
| 193 | upriv->gpe_cfg); |
| 194 | printf("pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n", |
| 195 | upriv->pm1_sts, upriv->pm1_en, upriv->pm1_cnt); |
| 196 | |
| 197 | for (i = 0; i < GPE0_REG_MAX; i++) { |
| 198 | printf("gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n", i, |
| 199 | upriv->gpe0_sts[i], i, upriv->gpe0_en[i]); |
| 200 | } |
| 201 | |
| 202 | printf("prsts: %08x\n", upriv->prsts); |
| 203 | printf("tco_sts: %04x %04x\n", upriv->tco1_sts, upriv->tco2_sts); |
| 204 | printf("gen_pmcon1: %08x gen_pmcon2: %08x gen_pmcon3: %08x\n", |
| 205 | upriv->gen_pmcon1, upriv->gen_pmcon2, upriv->gen_pmcon3); |
| 206 | } |
| 207 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 208 | int pmc_ofdata_to_uc_plat(struct udevice *dev) |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 209 | { |
| 210 | struct acpi_pmc_upriv *upriv = dev_get_uclass_priv(dev); |
| 211 | int ret; |
| 212 | |
| 213 | ret = dev_read_u32(dev, "gpe0-dwx-mask", &upriv->gpe0_dwx_mask); |
| 214 | if (ret) |
| 215 | return log_msg_ret("no gpe0-dwx-mask", ret); |
| 216 | ret = dev_read_u32(dev, "gpe0-dwx-shift-base", |
| 217 | &upriv->gpe0_dwx_shift_base); |
| 218 | if (ret) |
| 219 | return log_msg_ret("no gpe0-dwx-shift-base", ret); |
| 220 | ret = dev_read_u32(dev, "gpe0-sts", &upriv->gpe0_sts_reg); |
| 221 | if (ret) |
| 222 | return log_msg_ret("no gpe0-sts", ret); |
| 223 | upriv->gpe0_sts_reg += upriv->acpi_base; |
| 224 | ret = dev_read_u32(dev, "gpe0-en", &upriv->gpe0_en_reg); |
| 225 | if (ret) |
| 226 | return log_msg_ret("no gpe0-en", ret); |
| 227 | upriv->gpe0_en_reg += upriv->acpi_base; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | UCLASS_DRIVER(acpi_pmc) = { |
| 233 | .id = UCLASS_ACPI_PMC, |
| 234 | .name = "power-mgr", |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 235 | .per_device_auto = sizeof(struct acpi_pmc_upriv), |
Simon Glass | 6c6d88e | 2019-12-06 21:41:53 -0700 | [diff] [blame] | 236 | }; |