Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 6 | #include <asm/io.h> |
| 7 | #include <dm.h> |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 8 | #include <dm/device-internal.h> |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 9 | #include <linux/err.h> |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <power-domain-uclass.h> |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 12 | #include <reset-uclass.h> |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 13 | #include <regmap.h> |
| 14 | #include <syscon.h> |
| 15 | |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 16 | #define APPLE_PMGR_RESET BIT(31) |
| 17 | #define APPLE_PMGR_DEV_DISABLE BIT(10) |
| 18 | #define APPLE_PMGR_WAS_CLKGATED BIT(9) |
| 19 | #define APPLE_PMGR_WAS_PWRGATED BIT(8) |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 20 | #define APPLE_PMGR_PS_ACTUAL GENMASK(7, 4) |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 21 | #define APPLE_PMGR_PS_TARGET GENMASK(3, 0) |
| 22 | |
| 23 | #define APPLE_PMGR_FLAGS (APPLE_PMGR_WAS_CLKGATED | APPLE_PMGR_WAS_PWRGATED) |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 24 | |
| 25 | #define APPLE_PMGR_PS_ACTIVE 0xf |
| 26 | #define APPLE_PMGR_PS_PWRGATE 0x0 |
| 27 | |
| 28 | #define APPLE_PMGR_PS_SET_TIMEOUT_US 100 |
| 29 | |
| 30 | struct apple_pmgr_priv { |
| 31 | struct regmap *regmap; |
| 32 | u32 offset; /* offset within regmap for this domain */ |
| 33 | }; |
| 34 | |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 35 | static int apple_reset_of_xlate(struct reset_ctl *reset_ctl, |
| 36 | struct ofnode_phandle_args *args) |
| 37 | { |
| 38 | if (args->args_count != 0) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 44 | static int apple_reset_assert(struct reset_ctl *reset_ctl) |
| 45 | { |
| 46 | struct apple_pmgr_priv *priv = dev_get_priv(reset_ctl->dev->parent); |
| 47 | |
| 48 | regmap_update_bits(priv->regmap, priv->offset, |
| 49 | APPLE_PMGR_FLAGS | APPLE_PMGR_DEV_DISABLE, |
| 50 | APPLE_PMGR_DEV_DISABLE); |
| 51 | regmap_update_bits(priv->regmap, priv->offset, |
| 52 | APPLE_PMGR_FLAGS | APPLE_PMGR_RESET, |
| 53 | APPLE_PMGR_RESET); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int apple_reset_deassert(struct reset_ctl *reset_ctl) |
| 59 | { |
| 60 | struct apple_pmgr_priv *priv = dev_get_priv(reset_ctl->dev->parent); |
| 61 | |
| 62 | regmap_update_bits(priv->regmap, priv->offset, |
| 63 | APPLE_PMGR_FLAGS | APPLE_PMGR_RESET, 0); |
| 64 | regmap_update_bits(priv->regmap, priv->offset, |
| 65 | APPLE_PMGR_FLAGS | APPLE_PMGR_DEV_DISABLE, 0); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | struct reset_ops apple_reset_ops = { |
| 71 | .of_xlate = apple_reset_of_xlate, |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 72 | .rst_assert = apple_reset_assert, |
| 73 | .rst_deassert = apple_reset_deassert, |
| 74 | }; |
| 75 | |
| 76 | static struct driver apple_reset_driver = { |
| 77 | .name = "apple_reset", |
| 78 | .id = UCLASS_RESET, |
| 79 | .ops = &apple_reset_ops, |
| 80 | }; |
| 81 | |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 82 | static int apple_pmgr_ps_set(struct power_domain *power_domain, u32 pstate) |
| 83 | { |
| 84 | struct apple_pmgr_priv *priv = dev_get_priv(power_domain->dev); |
| 85 | uint reg; |
| 86 | |
| 87 | regmap_update_bits(priv->regmap, priv->offset, APPLE_PMGR_PS_TARGET, |
| 88 | FIELD_PREP(APPLE_PMGR_PS_TARGET, pstate)); |
| 89 | |
| 90 | return regmap_read_poll_timeout( |
| 91 | priv->regmap, priv->offset, reg, |
| 92 | (FIELD_GET(APPLE_PMGR_PS_ACTUAL, reg) == pstate), 1, |
| 93 | APPLE_PMGR_PS_SET_TIMEOUT_US); |
| 94 | } |
| 95 | |
| 96 | static int apple_pmgr_on(struct power_domain *power_domain) |
| 97 | { |
| 98 | return apple_pmgr_ps_set(power_domain, APPLE_PMGR_PS_ACTIVE); |
| 99 | } |
| 100 | |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 101 | static int apple_pmgr_of_xlate(struct power_domain *power_domain, |
| 102 | struct ofnode_phandle_args *args) |
| 103 | { |
| 104 | if (args->args_count != 0) { |
| 105 | debug("Invalid args_count: %d\n", args->args_count); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static const struct udevice_id apple_pmgr_ids[] = { |
| 113 | { .compatible = "apple,pmgr-pwrstate" }, |
| 114 | { /* sentinel */ } |
| 115 | }; |
| 116 | |
| 117 | static int apple_pmgr_probe(struct udevice *dev) |
| 118 | { |
| 119 | struct apple_pmgr_priv *priv = dev_get_priv(dev); |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 120 | struct udevice *child; |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 121 | int ret; |
| 122 | |
| 123 | ret = dev_power_domain_on(dev); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | priv->regmap = syscon_get_regmap(dev->parent); |
| 128 | if (IS_ERR(priv->regmap)) |
| 129 | return PTR_ERR(priv->regmap); |
| 130 | |
| 131 | ret = dev_read_u32(dev, "reg", &priv->offset); |
| 132 | if (ret < 0) |
| 133 | return ret; |
| 134 | |
Mark Kettenis | 9e8c483 | 2022-01-22 20:38:17 +0100 | [diff] [blame] | 135 | device_bind(dev, &apple_reset_driver, "apple_reset", NULL, |
| 136 | dev_ofnode(dev), &child); |
| 137 | |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | struct power_domain_ops apple_pmgr_ops = { |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 142 | .on = apple_pmgr_on, |
Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 143 | .of_xlate = apple_pmgr_of_xlate, |
| 144 | }; |
| 145 | |
| 146 | U_BOOT_DRIVER(apple_pmgr) = { |
| 147 | .name = "apple_pmgr", |
| 148 | .id = UCLASS_POWER_DOMAIN, |
| 149 | .of_match = apple_pmgr_ids, |
| 150 | .ops = &apple_pmgr_ops, |
| 151 | .probe = apple_pmgr_probe, |
| 152 | .priv_auto = sizeof(struct apple_pmgr_priv), |
| 153 | }; |