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