blob: 6f01a60b3467a0765d2efc59317d289554a5030d [file] [log] [blame]
Peng Fanbc6244b2018-10-18 14:28:29 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2017 NXP
4 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Peng Fanbc6244b2018-10-18 14:28:29 +02009#include <power-domain-uclass.h>
10#include <asm/io.h>
11#include <asm/arch/power-domain.h>
12#include <dm/device-internal.h>
13#include <dm/device.h>
14#include <asm/arch/sci/sci.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18struct imx8_power_domain_priv {
19 bool state_on;
20};
21
22static int imx8_power_domain_request(struct power_domain *power_domain)
23{
24 debug("%s(power_domain=%p)\n", __func__, power_domain);
25
26 return 0;
27}
28
29static int imx8_power_domain_free(struct power_domain *power_domain)
30{
31 debug("%s(power_domain=%p)\n", __func__, power_domain);
32
33 return 0;
34}
35
36static int imx8_power_domain_on(struct power_domain *power_domain)
37{
38 struct udevice *dev = power_domain->dev;
39 struct imx8_power_domain_platdata *pdata;
40 struct imx8_power_domain_priv *ppriv;
41 sc_err_t ret;
42 int err;
43
44 struct power_domain parent_domain;
45 struct udevice *parent = dev_get_parent(dev);
46
47 /* Need to power on parent node first */
48 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
49 parent_domain.dev = parent;
50 err = imx8_power_domain_on(&parent_domain);
51 if (err)
52 return err;
53 }
54
55 pdata = (struct imx8_power_domain_platdata *)dev_get_platdata(dev);
56 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
57
58 debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
59 pdata->resource_id);
60
61 /* Already powered on */
62 if (ppriv->state_on)
63 return 0;
64
65 if (pdata->resource_id != SC_R_LAST) {
66 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
67 SC_PM_PW_MODE_ON);
68 if (ret) {
69 printf("Error: %s Power up failed! (error = %d)\n",
70 dev->name, ret);
71 return -EIO;
72 }
73 }
74
75 ppriv->state_on = true;
76 debug("%s is powered on\n", dev->name);
77
78 return 0;
79}
80
81static int imx8_power_domain_off_node(struct power_domain *power_domain)
82{
83 struct udevice *dev = power_domain->dev;
84 struct udevice *child;
85 struct imx8_power_domain_priv *ppriv;
86 struct imx8_power_domain_priv *child_ppriv;
87 struct imx8_power_domain_platdata *pdata;
88 sc_err_t ret;
89
90 ppriv = dev_get_priv(dev);
91 pdata = dev_get_platdata(dev);
92
93 debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
94
95 /* Already powered off */
96 if (!ppriv->state_on)
97 return 0;
98
99 /* Check if all subnodes are off */
100 for (device_find_first_child(dev, &child);
101 child;
102 device_find_next_child(&child)) {
103 if (device_active(child)) {
104 child_ppriv =
105 (struct imx8_power_domain_priv *)dev_get_priv(child);
106 if (child_ppriv->state_on)
107 return -EPERM;
108 }
109 }
110
111 if (pdata->resource_id != SC_R_LAST) {
112 if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
113 printf("%s not owned by curr partition\n", dev->name);
114 return 0;
115 }
116 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
117 SC_PM_PW_MODE_OFF);
118 if (ret) {
119 printf("Error: %s Power off failed! (error = %d)\n",
120 dev->name, ret);
121 return -EIO;
122 }
123 }
124
125 ppriv->state_on = false;
126 debug("%s is powered off\n", dev->name);
127
128 return 0;
129}
130
131static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
132{
133 struct udevice *dev = power_domain->dev;
134 struct udevice *parent = dev_get_parent(dev);
135 struct udevice *child;
136 struct imx8_power_domain_priv *ppriv;
137 struct imx8_power_domain_priv *child_ppriv;
138 struct imx8_power_domain_platdata *pdata;
139 sc_err_t ret;
140 struct power_domain parent_pd;
141
142 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
143 pdata =
144 (struct imx8_power_domain_platdata *)dev_get_platdata(parent);
145 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
146
147 debug("%s, %s, state_on %d\n", __func__, parent->name,
148 ppriv->state_on);
149
150 /* Already powered off */
151 if (!ppriv->state_on)
152 return 0;
153
154 /*
155 * Check if all sibling nodes are off. If yes,
156 * power off parent
157 */
158 for (device_find_first_child(parent, &child); child;
159 device_find_next_child(&child)) {
160 if (device_active(child)) {
161 child_ppriv = (struct imx8_power_domain_priv *)
162 dev_get_priv(child);
163 /* Find a power on sibling */
164 if (child_ppriv->state_on) {
165 debug("sibling %s, state_on %d\n",
166 child->name,
167 child_ppriv->state_on);
168 return 0;
169 }
170 }
171 }
172
173 /* power off parent */
174 if (pdata->resource_id != SC_R_LAST) {
175 ret = sc_pm_set_resource_power_mode(-1,
176 pdata->resource_id,
177 SC_PM_PW_MODE_OFF);
178 if (ret) {
179 printf("%s Power off failed! (error = %d)\n",
180 parent->name, ret);
181 return -EIO;
182 }
183 }
184
185 ppriv->state_on = false;
186 debug("%s is powered off\n", parent->name);
187
188 parent_pd.dev = parent;
189 imx8_power_domain_off_parentnodes(&parent_pd);
190 }
191
192 return 0;
193}
194
195static int imx8_power_domain_off(struct power_domain *power_domain)
196{
197 int ret;
198
199 debug("%s(power_domain=%p)\n", __func__, power_domain);
200
201 /* Turn off the node */
202 ret = imx8_power_domain_off_node(power_domain);
203 if (ret) {
204 debug("Can't power off the node of dev %s, ret = %d\n",
205 power_domain->dev->name, ret);
206 return ret;
207 }
208
209 /* Turn off parent nodes, if sibling nodes are all off */
210 ret = imx8_power_domain_off_parentnodes(power_domain);
211 if (ret) {
212 printf("Failed to power off parent nodes of dev %s, ret = %d\n",
213 power_domain->dev->name, ret);
214 return ret;
215 }
216
217 return 0;
218}
219
220static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
221 struct ofnode_phandle_args *args)
222{
223 debug("%s(power_domain=%p)\n", __func__, power_domain);
224
225 /* Do nothing to the xlate, since we don't have args used */
226
227 return 0;
228}
229
230static int imx8_power_domain_bind(struct udevice *dev)
231{
232 int offset;
233 const char *name;
234 int ret = 0;
235
236 debug("%s(dev=%p)\n", __func__, dev);
237
238 offset = dev_of_offset(dev);
239 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
240 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
241 /* Bind the subnode to this driver */
242 name = fdt_get_name(gd->fdt_blob, offset, NULL);
243
244 ret = device_bind_with_driver_data(dev, dev->driver, name,
245 dev->driver_data,
246 offset_to_ofnode(offset),
247 NULL);
248
249 if (ret == -ENODEV)
250 printf("Driver '%s' refuses to bind\n",
251 dev->driver->name);
252
253 if (ret)
254 printf("Error binding driver '%s': %d\n",
255 dev->driver->name, ret);
256 }
257
258 return 0;
259}
260
261static int imx8_power_domain_probe(struct udevice *dev)
262{
263 struct imx8_power_domain_priv *ppriv;
264
265 debug("%s(dev=%s)\n", __func__, dev->name);
266
267 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
268
269 /* Set default to power off */
270 if (ppriv)
271 ppriv->state_on = false;
272
273 return 0;
274}
275
276static int imx8_power_domain_ofdata_to_platdata(struct udevice *dev)
277{
278 int reg;
279 struct imx8_power_domain_platdata *pdata = dev_get_platdata(dev);
280
281 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
282 if (reg == -1) {
283 debug("%s: Invalid resource id %d\n", __func__, reg);
284 return -EINVAL;
285 }
286 pdata->resource_id = (sc_rsrc_t)reg;
287
288 debug("%s resource_id %d\n", __func__, pdata->resource_id);
289
290 return 0;
291}
292
293static const struct udevice_id imx8_power_domain_ids[] = {
294 { .compatible = "nxp,imx8-pd" },
295 { }
296};
297
298struct power_domain_ops imx8_power_domain_ops = {
299 .request = imx8_power_domain_request,
Simon Glass92ed3a32020-02-03 07:35:51 -0700300 .rfree = imx8_power_domain_free,
Peng Fanbc6244b2018-10-18 14:28:29 +0200301 .on = imx8_power_domain_on,
302 .off = imx8_power_domain_off,
303 .of_xlate = imx8_power_domain_of_xlate,
304};
305
306U_BOOT_DRIVER(imx8_power_domain) = {
307 .name = "imx8_power_domain",
308 .id = UCLASS_POWER_DOMAIN,
309 .of_match = imx8_power_domain_ids,
310 .bind = imx8_power_domain_bind,
311 .probe = imx8_power_domain_probe,
312 .ofdata_to_platdata = imx8_power_domain_ofdata_to_platdata,
313 .platdata_auto_alloc_size = sizeof(struct imx8_power_domain_platdata),
314 .priv_auto_alloc_size = sizeof(struct imx8_power_domain_priv),
315 .ops = &imx8_power_domain_ops,
316};