blob: 713a51d7807d1816b6c7d8687f45af9706ce76af [file] [log] [blame]
Peng Fanbc6244b2018-10-18 14:28:29 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2017 NXP
4 */
5
Peng Fanbc6244b2018-10-18 14:28:29 +02006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.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>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Peng Fanbc6244b2018-10-18 14:28:29 +020011#include <asm/io.h>
12#include <asm/arch/power-domain.h>
13#include <dm/device-internal.h>
14#include <dm/device.h>
Peng Fan1b9729a2020-05-05 20:28:40 +080015#include <dm/uclass-internal.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080016#include <firmware/imx/sci/sci.h>
Peng Fanbc6244b2018-10-18 14:28:29 +020017
18DECLARE_GLOBAL_DATA_PTR;
19
20struct imx8_power_domain_priv {
21 bool state_on;
22};
23
Peng Fan1b9729a2020-05-05 20:28:40 +080024static bool check_device_power_off(struct udevice *dev,
25 const char *permanent_on_devices[],
26 int size)
27{
28 int i;
29
30 for (i = 0; i < size; i++) {
31 if (!strcmp(dev->name, permanent_on_devices[i]))
32 return false;
33 }
34
35 return true;
36}
37
38void imx8_power_off_pd_devices(const char *permanent_on_devices[], int size)
39{
40 struct udevice *dev;
41 struct power_domain pd;
42
43 for (uclass_find_first_device(UCLASS_POWER_DOMAIN, &dev); dev;
44 uclass_find_next_device(&dev)) {
45 if (!device_active(dev))
46 continue;
47 /*
48 * Power off active pd devices except the permanent
49 * power on devices
50 */
51 if (check_device_power_off(dev, permanent_on_devices, size)) {
52 pd.dev = dev;
53 power_domain_off(&pd);
54 }
55 }
56}
57
Peng Fanb48acc52020-05-04 21:16:50 +080058int imx8_power_domain_lookup_name(const char *name,
59 struct power_domain *power_domain)
60{
61 struct udevice *dev;
62 struct power_domain_ops *ops;
63 int ret;
64
65 debug("%s(power_domain=%p name=%s)\n", __func__, power_domain, name);
66
67 ret = uclass_get_device_by_name(UCLASS_POWER_DOMAIN, name, &dev);
68 if (ret) {
69 printf("%s fail: %s, ret = %d\n", __func__, name, ret);
70 return ret;
71 }
72
73 ops = (struct power_domain_ops *)dev->driver->ops;
74 power_domain->dev = dev;
75 ret = ops->request(power_domain);
76 if (ret) {
77 debug("ops->request() failed: %d\n", ret);
78 return ret;
79 }
80
81 debug("%s ok: %s\n", __func__, dev->name);
82
83 return 0;
84}
85
Peng Fanbc6244b2018-10-18 14:28:29 +020086static int imx8_power_domain_on(struct power_domain *power_domain)
87{
88 struct udevice *dev = power_domain->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -070089 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +020090 struct imx8_power_domain_priv *ppriv;
Peng Fanbc6244b2018-10-18 14:28:29 +020091 int err;
92
93 struct power_domain parent_domain;
94 struct udevice *parent = dev_get_parent(dev);
95
96 /* Need to power on parent node first */
97 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
98 parent_domain.dev = parent;
99 err = imx8_power_domain_on(&parent_domain);
100 if (err)
101 return err;
102 }
103
Simon Glassb75b15b2020-12-03 16:55:23 -0700104 pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200105 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
106
107 debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
108 pdata->resource_id);
109
110 /* Already powered on */
111 if (ppriv->state_on)
112 return 0;
113
Leonard Crestez797be1b2020-05-04 21:16:54 +0800114 if (pdata->resource_id != SC_R_NONE) {
Ye Li479a96312020-05-04 21:16:53 +0800115 if (!sc_rm_is_resource_owned(-1, pdata->resource_id))
116 printf("%s [%d] not owned by curr partition\n", dev->name, pdata->resource_id);
117
Peng Fan25b7eb42023-06-15 18:08:58 +0800118 err = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
Peng Fanbc6244b2018-10-18 14:28:29 +0200119 SC_PM_PW_MODE_ON);
Peng Fan25b7eb42023-06-15 18:08:58 +0800120 if (err) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200121 printf("Error: %s Power up failed! (error = %d)\n",
Peng Fan25b7eb42023-06-15 18:08:58 +0800122 dev->name, err);
Peng Fanbc6244b2018-10-18 14:28:29 +0200123 return -EIO;
124 }
125 }
126
127 ppriv->state_on = true;
128 debug("%s is powered on\n", dev->name);
129
130 return 0;
131}
132
133static int imx8_power_domain_off_node(struct power_domain *power_domain)
134{
135 struct udevice *dev = power_domain->dev;
136 struct udevice *child;
137 struct imx8_power_domain_priv *ppriv;
138 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700139 struct imx8_power_domain_plat *pdata;
Peng Fan25b7eb42023-06-15 18:08:58 +0800140 int ret;
Peng Fanbc6244b2018-10-18 14:28:29 +0200141
142 ppriv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700143 pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200144
145 debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
146
147 /* Already powered off */
148 if (!ppriv->state_on)
149 return 0;
150
151 /* Check if all subnodes are off */
152 for (device_find_first_child(dev, &child);
153 child;
154 device_find_next_child(&child)) {
155 if (device_active(child)) {
156 child_ppriv =
157 (struct imx8_power_domain_priv *)dev_get_priv(child);
158 if (child_ppriv->state_on)
159 return -EPERM;
160 }
161 }
162
Leonard Crestez797be1b2020-05-04 21:16:54 +0800163 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200164 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
165 SC_PM_PW_MODE_OFF);
166 if (ret) {
Peng Fan6c3a5b32020-05-04 21:16:52 +0800167 if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
168 printf("%s not owned by curr partition %d\n", dev->name, pdata->resource_id);
169 return 0;
170 }
Peng Fanbc6244b2018-10-18 14:28:29 +0200171 printf("Error: %s Power off failed! (error = %d)\n",
172 dev->name, ret);
173 return -EIO;
174 }
175 }
176
177 ppriv->state_on = false;
178 debug("%s is powered off\n", dev->name);
179
180 return 0;
181}
182
183static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
184{
185 struct udevice *dev = power_domain->dev;
186 struct udevice *parent = dev_get_parent(dev);
187 struct udevice *child;
188 struct imx8_power_domain_priv *ppriv;
189 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700190 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200191 sc_err_t ret;
192 struct power_domain parent_pd;
193
194 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
195 pdata =
Simon Glassb75b15b2020-12-03 16:55:23 -0700196 (struct imx8_power_domain_plat *)dev_get_plat(parent);
Peng Fanbc6244b2018-10-18 14:28:29 +0200197 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
198
199 debug("%s, %s, state_on %d\n", __func__, parent->name,
200 ppriv->state_on);
201
202 /* Already powered off */
203 if (!ppriv->state_on)
204 return 0;
205
206 /*
207 * Check if all sibling nodes are off. If yes,
208 * power off parent
209 */
210 for (device_find_first_child(parent, &child); child;
211 device_find_next_child(&child)) {
212 if (device_active(child)) {
213 child_ppriv = (struct imx8_power_domain_priv *)
214 dev_get_priv(child);
215 /* Find a power on sibling */
216 if (child_ppriv->state_on) {
217 debug("sibling %s, state_on %d\n",
218 child->name,
219 child_ppriv->state_on);
220 return 0;
221 }
222 }
223 }
224
225 /* power off parent */
Leonard Crestez797be1b2020-05-04 21:16:54 +0800226 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200227 ret = sc_pm_set_resource_power_mode(-1,
228 pdata->resource_id,
229 SC_PM_PW_MODE_OFF);
230 if (ret) {
231 printf("%s Power off failed! (error = %d)\n",
232 parent->name, ret);
233 return -EIO;
234 }
235 }
236
237 ppriv->state_on = false;
238 debug("%s is powered off\n", parent->name);
239
240 parent_pd.dev = parent;
241 imx8_power_domain_off_parentnodes(&parent_pd);
242 }
243
244 return 0;
245}
246
247static int imx8_power_domain_off(struct power_domain *power_domain)
248{
249 int ret;
250
251 debug("%s(power_domain=%p)\n", __func__, power_domain);
252
253 /* Turn off the node */
254 ret = imx8_power_domain_off_node(power_domain);
255 if (ret) {
256 debug("Can't power off the node of dev %s, ret = %d\n",
257 power_domain->dev->name, ret);
258 return ret;
259 }
260
261 /* Turn off parent nodes, if sibling nodes are all off */
262 ret = imx8_power_domain_off_parentnodes(power_domain);
263 if (ret) {
264 printf("Failed to power off parent nodes of dev %s, ret = %d\n",
265 power_domain->dev->name, ret);
266 return ret;
267 }
268
269 return 0;
270}
271
272static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
273 struct ofnode_phandle_args *args)
274{
275 debug("%s(power_domain=%p)\n", __func__, power_domain);
276
277 /* Do nothing to the xlate, since we don't have args used */
278
279 return 0;
280}
281
282static int imx8_power_domain_bind(struct udevice *dev)
283{
284 int offset;
285 const char *name;
286 int ret = 0;
287
288 debug("%s(dev=%p)\n", __func__, dev);
289
290 offset = dev_of_offset(dev);
291 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
292 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
293 /* Bind the subnode to this driver */
294 name = fdt_get_name(gd->fdt_blob, offset, NULL);
295
296 ret = device_bind_with_driver_data(dev, dev->driver, name,
297 dev->driver_data,
298 offset_to_ofnode(offset),
299 NULL);
300
301 if (ret == -ENODEV)
302 printf("Driver '%s' refuses to bind\n",
303 dev->driver->name);
304
305 if (ret)
306 printf("Error binding driver '%s': %d\n",
307 dev->driver->name, ret);
308 }
309
310 return 0;
311}
312
313static int imx8_power_domain_probe(struct udevice *dev)
314{
315 struct imx8_power_domain_priv *ppriv;
316
317 debug("%s(dev=%s)\n", __func__, dev->name);
318
319 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
320
321 /* Set default to power off */
322 if (ppriv)
323 ppriv->state_on = false;
324
325 return 0;
326}
327
Simon Glassaad29ae2020-12-03 16:55:21 -0700328static int imx8_power_domain_of_to_plat(struct udevice *dev)
Peng Fanbc6244b2018-10-18 14:28:29 +0200329{
330 int reg;
Simon Glassb75b15b2020-12-03 16:55:23 -0700331 struct imx8_power_domain_plat *pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200332
333 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
334 if (reg == -1) {
335 debug("%s: Invalid resource id %d\n", __func__, reg);
336 return -EINVAL;
337 }
338 pdata->resource_id = (sc_rsrc_t)reg;
339
340 debug("%s resource_id %d\n", __func__, pdata->resource_id);
341
342 return 0;
343}
344
345static const struct udevice_id imx8_power_domain_ids[] = {
346 { .compatible = "nxp,imx8-pd" },
347 { }
348};
349
350struct power_domain_ops imx8_power_domain_ops = {
Peng Fanbc6244b2018-10-18 14:28:29 +0200351 .on = imx8_power_domain_on,
352 .off = imx8_power_domain_off,
353 .of_xlate = imx8_power_domain_of_xlate,
354};
355
356U_BOOT_DRIVER(imx8_power_domain) = {
357 .name = "imx8_power_domain",
358 .id = UCLASS_POWER_DOMAIN,
359 .of_match = imx8_power_domain_ids,
360 .bind = imx8_power_domain_bind,
361 .probe = imx8_power_domain_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700362 .of_to_plat = imx8_power_domain_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700363 .plat_auto = sizeof(struct imx8_power_domain_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700364 .priv_auto = sizeof(struct imx8_power_domain_priv),
Peng Fanbc6244b2018-10-18 14:28:29 +0200365 .ops = &imx8_power_domain_ops,
Ye Li625347a2020-05-04 21:16:51 +0800366 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Peng Fanbc6244b2018-10-18 14:28:29 +0200367};