blob: 37b0f95abac73a435591e63b752577f2abd17639 [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 Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Peng Fanbc6244b2018-10-18 14:28:29 +020010#include <power-domain-uclass.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Peng Fanbc6244b2018-10-18 14:28:29 +020012#include <asm/io.h>
13#include <asm/arch/power-domain.h>
14#include <dm/device-internal.h>
15#include <dm/device.h>
Peng Fan1b9729a2020-05-05 20:28:40 +080016#include <dm/uclass-internal.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080017#include <firmware/imx/sci/sci.h>
Peng Fanbc6244b2018-10-18 14:28:29 +020018
19DECLARE_GLOBAL_DATA_PTR;
20
21struct imx8_power_domain_priv {
22 bool state_on;
23};
24
Peng Fan1b9729a2020-05-05 20:28:40 +080025static bool check_device_power_off(struct udevice *dev,
26 const char *permanent_on_devices[],
27 int size)
28{
29 int i;
30
31 for (i = 0; i < size; i++) {
32 if (!strcmp(dev->name, permanent_on_devices[i]))
33 return false;
34 }
35
36 return true;
37}
38
39void imx8_power_off_pd_devices(const char *permanent_on_devices[], int size)
40{
41 struct udevice *dev;
42 struct power_domain pd;
43
44 for (uclass_find_first_device(UCLASS_POWER_DOMAIN, &dev); dev;
45 uclass_find_next_device(&dev)) {
46 if (!device_active(dev))
47 continue;
48 /*
49 * Power off active pd devices except the permanent
50 * power on devices
51 */
52 if (check_device_power_off(dev, permanent_on_devices, size)) {
53 pd.dev = dev;
54 power_domain_off(&pd);
55 }
56 }
57}
58
Peng Fanb48acc52020-05-04 21:16:50 +080059int imx8_power_domain_lookup_name(const char *name,
60 struct power_domain *power_domain)
61{
62 struct udevice *dev;
63 struct power_domain_ops *ops;
64 int ret;
65
66 debug("%s(power_domain=%p name=%s)\n", __func__, power_domain, name);
67
68 ret = uclass_get_device_by_name(UCLASS_POWER_DOMAIN, name, &dev);
69 if (ret) {
70 printf("%s fail: %s, ret = %d\n", __func__, name, ret);
71 return ret;
72 }
73
74 ops = (struct power_domain_ops *)dev->driver->ops;
75 power_domain->dev = dev;
76 ret = ops->request(power_domain);
77 if (ret) {
78 debug("ops->request() failed: %d\n", ret);
79 return ret;
80 }
81
82 debug("%s ok: %s\n", __func__, dev->name);
83
84 return 0;
85}
86
Peng Fanbc6244b2018-10-18 14:28:29 +020087static int imx8_power_domain_on(struct power_domain *power_domain)
88{
89 struct udevice *dev = power_domain->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -070090 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +020091 struct imx8_power_domain_priv *ppriv;
92 sc_err_t ret;
93 int err;
94
95 struct power_domain parent_domain;
96 struct udevice *parent = dev_get_parent(dev);
97
98 /* Need to power on parent node first */
99 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
100 parent_domain.dev = parent;
101 err = imx8_power_domain_on(&parent_domain);
102 if (err)
103 return err;
104 }
105
Simon Glassb75b15b2020-12-03 16:55:23 -0700106 pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200107 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
108
109 debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
110 pdata->resource_id);
111
112 /* Already powered on */
113 if (ppriv->state_on)
114 return 0;
115
Leonard Crestez797be1b2020-05-04 21:16:54 +0800116 if (pdata->resource_id != SC_R_NONE) {
Ye Li479a96312020-05-04 21:16:53 +0800117 if (!sc_rm_is_resource_owned(-1, pdata->resource_id))
118 printf("%s [%d] not owned by curr partition\n", dev->name, pdata->resource_id);
119
Peng Fanbc6244b2018-10-18 14:28:29 +0200120 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
121 SC_PM_PW_MODE_ON);
122 if (ret) {
123 printf("Error: %s Power up failed! (error = %d)\n",
124 dev->name, ret);
125 return -EIO;
126 }
127 }
128
129 ppriv->state_on = true;
130 debug("%s is powered on\n", dev->name);
131
132 return 0;
133}
134
135static int imx8_power_domain_off_node(struct power_domain *power_domain)
136{
137 struct udevice *dev = power_domain->dev;
138 struct udevice *child;
139 struct imx8_power_domain_priv *ppriv;
140 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700141 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200142 sc_err_t ret;
143
144 ppriv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700145 pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200146
147 debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
148
149 /* Already powered off */
150 if (!ppriv->state_on)
151 return 0;
152
153 /* Check if all subnodes are off */
154 for (device_find_first_child(dev, &child);
155 child;
156 device_find_next_child(&child)) {
157 if (device_active(child)) {
158 child_ppriv =
159 (struct imx8_power_domain_priv *)dev_get_priv(child);
160 if (child_ppriv->state_on)
161 return -EPERM;
162 }
163 }
164
Leonard Crestez797be1b2020-05-04 21:16:54 +0800165 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200166 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
167 SC_PM_PW_MODE_OFF);
168 if (ret) {
Peng Fan6c3a5b32020-05-04 21:16:52 +0800169 if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
170 printf("%s not owned by curr partition %d\n", dev->name, pdata->resource_id);
171 return 0;
172 }
Peng Fanbc6244b2018-10-18 14:28:29 +0200173 printf("Error: %s Power off failed! (error = %d)\n",
174 dev->name, ret);
175 return -EIO;
176 }
177 }
178
179 ppriv->state_on = false;
180 debug("%s is powered off\n", dev->name);
181
182 return 0;
183}
184
185static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
186{
187 struct udevice *dev = power_domain->dev;
188 struct udevice *parent = dev_get_parent(dev);
189 struct udevice *child;
190 struct imx8_power_domain_priv *ppriv;
191 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700192 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200193 sc_err_t ret;
194 struct power_domain parent_pd;
195
196 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
197 pdata =
Simon Glassb75b15b2020-12-03 16:55:23 -0700198 (struct imx8_power_domain_plat *)dev_get_plat(parent);
Peng Fanbc6244b2018-10-18 14:28:29 +0200199 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
200
201 debug("%s, %s, state_on %d\n", __func__, parent->name,
202 ppriv->state_on);
203
204 /* Already powered off */
205 if (!ppriv->state_on)
206 return 0;
207
208 /*
209 * Check if all sibling nodes are off. If yes,
210 * power off parent
211 */
212 for (device_find_first_child(parent, &child); child;
213 device_find_next_child(&child)) {
214 if (device_active(child)) {
215 child_ppriv = (struct imx8_power_domain_priv *)
216 dev_get_priv(child);
217 /* Find a power on sibling */
218 if (child_ppriv->state_on) {
219 debug("sibling %s, state_on %d\n",
220 child->name,
221 child_ppriv->state_on);
222 return 0;
223 }
224 }
225 }
226
227 /* power off parent */
Leonard Crestez797be1b2020-05-04 21:16:54 +0800228 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200229 ret = sc_pm_set_resource_power_mode(-1,
230 pdata->resource_id,
231 SC_PM_PW_MODE_OFF);
232 if (ret) {
233 printf("%s Power off failed! (error = %d)\n",
234 parent->name, ret);
235 return -EIO;
236 }
237 }
238
239 ppriv->state_on = false;
240 debug("%s is powered off\n", parent->name);
241
242 parent_pd.dev = parent;
243 imx8_power_domain_off_parentnodes(&parent_pd);
244 }
245
246 return 0;
247}
248
249static int imx8_power_domain_off(struct power_domain *power_domain)
250{
251 int ret;
252
253 debug("%s(power_domain=%p)\n", __func__, power_domain);
254
255 /* Turn off the node */
256 ret = imx8_power_domain_off_node(power_domain);
257 if (ret) {
258 debug("Can't power off the node of dev %s, ret = %d\n",
259 power_domain->dev->name, ret);
260 return ret;
261 }
262
263 /* Turn off parent nodes, if sibling nodes are all off */
264 ret = imx8_power_domain_off_parentnodes(power_domain);
265 if (ret) {
266 printf("Failed to power off parent nodes of dev %s, ret = %d\n",
267 power_domain->dev->name, ret);
268 return ret;
269 }
270
271 return 0;
272}
273
274static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
275 struct ofnode_phandle_args *args)
276{
277 debug("%s(power_domain=%p)\n", __func__, power_domain);
278
279 /* Do nothing to the xlate, since we don't have args used */
280
281 return 0;
282}
283
284static int imx8_power_domain_bind(struct udevice *dev)
285{
286 int offset;
287 const char *name;
288 int ret = 0;
289
290 debug("%s(dev=%p)\n", __func__, dev);
291
292 offset = dev_of_offset(dev);
293 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
294 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
295 /* Bind the subnode to this driver */
296 name = fdt_get_name(gd->fdt_blob, offset, NULL);
297
298 ret = device_bind_with_driver_data(dev, dev->driver, name,
299 dev->driver_data,
300 offset_to_ofnode(offset),
301 NULL);
302
303 if (ret == -ENODEV)
304 printf("Driver '%s' refuses to bind\n",
305 dev->driver->name);
306
307 if (ret)
308 printf("Error binding driver '%s': %d\n",
309 dev->driver->name, ret);
310 }
311
312 return 0;
313}
314
315static int imx8_power_domain_probe(struct udevice *dev)
316{
317 struct imx8_power_domain_priv *ppriv;
318
319 debug("%s(dev=%s)\n", __func__, dev->name);
320
321 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
322
323 /* Set default to power off */
324 if (ppriv)
325 ppriv->state_on = false;
326
327 return 0;
328}
329
Simon Glassaad29ae2020-12-03 16:55:21 -0700330static int imx8_power_domain_of_to_plat(struct udevice *dev)
Peng Fanbc6244b2018-10-18 14:28:29 +0200331{
332 int reg;
Simon Glassb75b15b2020-12-03 16:55:23 -0700333 struct imx8_power_domain_plat *pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200334
335 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
336 if (reg == -1) {
337 debug("%s: Invalid resource id %d\n", __func__, reg);
338 return -EINVAL;
339 }
340 pdata->resource_id = (sc_rsrc_t)reg;
341
342 debug("%s resource_id %d\n", __func__, pdata->resource_id);
343
344 return 0;
345}
346
347static const struct udevice_id imx8_power_domain_ids[] = {
348 { .compatible = "nxp,imx8-pd" },
349 { }
350};
351
352struct power_domain_ops imx8_power_domain_ops = {
Peng Fanbc6244b2018-10-18 14:28:29 +0200353 .on = imx8_power_domain_on,
354 .off = imx8_power_domain_off,
355 .of_xlate = imx8_power_domain_of_xlate,
356};
357
358U_BOOT_DRIVER(imx8_power_domain) = {
359 .name = "imx8_power_domain",
360 .id = UCLASS_POWER_DOMAIN,
361 .of_match = imx8_power_domain_ids,
362 .bind = imx8_power_domain_bind,
363 .probe = imx8_power_domain_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700364 .of_to_plat = imx8_power_domain_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700365 .plat_auto = sizeof(struct imx8_power_domain_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700366 .priv_auto = sizeof(struct imx8_power_domain_priv),
Peng Fanbc6244b2018-10-18 14:28:29 +0200367 .ops = &imx8_power_domain_ops,
Ye Li625347a2020-05-04 21:16:51 +0800368 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Peng Fanbc6244b2018-10-18 14:28:29 +0200369};