blob: bf9940621ee7f39392e45058165840cd4f6e7b5c [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
Mark Kettenis2fc94052022-01-10 20:58:44 +01006#include <asm/io.h>
7#include <dm.h>
Mark Kettenis9e8c4832022-01-22 20:38:17 +01008#include <dm/device-internal.h>
Mark Kettenis2fc94052022-01-10 20:58:44 +01009#include <linux/err.h>
10#include <linux/bitfield.h>
11#include <power-domain-uclass.h>
Mark Kettenis9e8c4832022-01-22 20:38:17 +010012#include <reset-uclass.h>
Mark Kettenis2fc94052022-01-10 20:58:44 +010013#include <regmap.h>
14#include <syscon.h>
15
Mark Kettenis9e8c4832022-01-22 20:38:17 +010016#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 Kettenis2fc94052022-01-10 20:58:44 +010020#define APPLE_PMGR_PS_ACTUAL GENMASK(7, 4)
Mark Kettenis9e8c4832022-01-22 20:38:17 +010021#define APPLE_PMGR_PS_TARGET GENMASK(3, 0)
22
23#define APPLE_PMGR_FLAGS (APPLE_PMGR_WAS_CLKGATED | APPLE_PMGR_WAS_PWRGATED)
Mark Kettenis2fc94052022-01-10 20:58:44 +010024
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
30struct apple_pmgr_priv {
31 struct regmap *regmap;
32 u32 offset; /* offset within regmap for this domain */
33};
34
Mark Kettenis9e8c4832022-01-22 20:38:17 +010035static 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 Kettenis9e8c4832022-01-22 20:38:17 +010044static 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
58static 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
70struct reset_ops apple_reset_ops = {
71 .of_xlate = apple_reset_of_xlate,
Mark Kettenis9e8c4832022-01-22 20:38:17 +010072 .rst_assert = apple_reset_assert,
73 .rst_deassert = apple_reset_deassert,
74};
75
76static struct driver apple_reset_driver = {
77 .name = "apple_reset",
78 .id = UCLASS_RESET,
79 .ops = &apple_reset_ops,
80};
81
Mark Kettenis2fc94052022-01-10 20:58:44 +010082static 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
96static 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 Kettenis2fc94052022-01-10 20:58:44 +0100101static 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
112static const struct udevice_id apple_pmgr_ids[] = {
113 { .compatible = "apple,pmgr-pwrstate" },
114 { /* sentinel */ }
115};
116
117static int apple_pmgr_probe(struct udevice *dev)
118{
119 struct apple_pmgr_priv *priv = dev_get_priv(dev);
Mark Kettenis9e8c4832022-01-22 20:38:17 +0100120 struct udevice *child;
Mark Kettenis2fc94052022-01-10 20:58:44 +0100121 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 Kettenis9e8c4832022-01-22 20:38:17 +0100135 device_bind(dev, &apple_reset_driver, "apple_reset", NULL,
136 dev_ofnode(dev), &child);
137
Mark Kettenis2fc94052022-01-10 20:58:44 +0100138 return 0;
139}
140
141struct power_domain_ops apple_pmgr_ops = {
Mark Kettenis2fc94052022-01-10 20:58:44 +0100142 .on = apple_pmgr_on,
Mark Kettenis2fc94052022-01-10 20:58:44 +0100143 .of_xlate = apple_pmgr_of_xlate,
144};
145
146U_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};