blob: c8ca2665752fb9aa47765de2a57e32b2baca2d64 [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;
Peng Fanbc6244b2018-10-18 14:28:29 +020092 int err;
93
94 struct power_domain parent_domain;
95 struct udevice *parent = dev_get_parent(dev);
96
97 /* Need to power on parent node first */
98 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
99 parent_domain.dev = parent;
100 err = imx8_power_domain_on(&parent_domain);
101 if (err)
102 return err;
103 }
104
Simon Glassb75b15b2020-12-03 16:55:23 -0700105 pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200106 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
107
108 debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
109 pdata->resource_id);
110
111 /* Already powered on */
112 if (ppriv->state_on)
113 return 0;
114
Leonard Crestez797be1b2020-05-04 21:16:54 +0800115 if (pdata->resource_id != SC_R_NONE) {
Ye Li479a96312020-05-04 21:16:53 +0800116 if (!sc_rm_is_resource_owned(-1, pdata->resource_id))
117 printf("%s [%d] not owned by curr partition\n", dev->name, pdata->resource_id);
118
Peng Fan25b7eb42023-06-15 18:08:58 +0800119 err = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
Peng Fanbc6244b2018-10-18 14:28:29 +0200120 SC_PM_PW_MODE_ON);
Peng Fan25b7eb42023-06-15 18:08:58 +0800121 if (err) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200122 printf("Error: %s Power up failed! (error = %d)\n",
Peng Fan25b7eb42023-06-15 18:08:58 +0800123 dev->name, err);
Peng Fanbc6244b2018-10-18 14:28:29 +0200124 return -EIO;
125 }
126 }
127
128 ppriv->state_on = true;
129 debug("%s is powered on\n", dev->name);
130
131 return 0;
132}
133
134static int imx8_power_domain_off_node(struct power_domain *power_domain)
135{
136 struct udevice *dev = power_domain->dev;
137 struct udevice *child;
138 struct imx8_power_domain_priv *ppriv;
139 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700140 struct imx8_power_domain_plat *pdata;
Peng Fan25b7eb42023-06-15 18:08:58 +0800141 int ret;
Peng Fanbc6244b2018-10-18 14:28:29 +0200142
143 ppriv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700144 pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200145
146 debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
147
148 /* Already powered off */
149 if (!ppriv->state_on)
150 return 0;
151
152 /* Check if all subnodes are off */
153 for (device_find_first_child(dev, &child);
154 child;
155 device_find_next_child(&child)) {
156 if (device_active(child)) {
157 child_ppriv =
158 (struct imx8_power_domain_priv *)dev_get_priv(child);
159 if (child_ppriv->state_on)
160 return -EPERM;
161 }
162 }
163
Leonard Crestez797be1b2020-05-04 21:16:54 +0800164 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200165 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
166 SC_PM_PW_MODE_OFF);
167 if (ret) {
Peng Fan6c3a5b32020-05-04 21:16:52 +0800168 if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
169 printf("%s not owned by curr partition %d\n", dev->name, pdata->resource_id);
170 return 0;
171 }
Peng Fanbc6244b2018-10-18 14:28:29 +0200172 printf("Error: %s Power off failed! (error = %d)\n",
173 dev->name, ret);
174 return -EIO;
175 }
176 }
177
178 ppriv->state_on = false;
179 debug("%s is powered off\n", dev->name);
180
181 return 0;
182}
183
184static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
185{
186 struct udevice *dev = power_domain->dev;
187 struct udevice *parent = dev_get_parent(dev);
188 struct udevice *child;
189 struct imx8_power_domain_priv *ppriv;
190 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700191 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200192 sc_err_t ret;
193 struct power_domain parent_pd;
194
195 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
196 pdata =
Simon Glassb75b15b2020-12-03 16:55:23 -0700197 (struct imx8_power_domain_plat *)dev_get_plat(parent);
Peng Fanbc6244b2018-10-18 14:28:29 +0200198 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
199
200 debug("%s, %s, state_on %d\n", __func__, parent->name,
201 ppriv->state_on);
202
203 /* Already powered off */
204 if (!ppriv->state_on)
205 return 0;
206
207 /*
208 * Check if all sibling nodes are off. If yes,
209 * power off parent
210 */
211 for (device_find_first_child(parent, &child); child;
212 device_find_next_child(&child)) {
213 if (device_active(child)) {
214 child_ppriv = (struct imx8_power_domain_priv *)
215 dev_get_priv(child);
216 /* Find a power on sibling */
217 if (child_ppriv->state_on) {
218 debug("sibling %s, state_on %d\n",
219 child->name,
220 child_ppriv->state_on);
221 return 0;
222 }
223 }
224 }
225
226 /* power off parent */
Leonard Crestez797be1b2020-05-04 21:16:54 +0800227 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200228 ret = sc_pm_set_resource_power_mode(-1,
229 pdata->resource_id,
230 SC_PM_PW_MODE_OFF);
231 if (ret) {
232 printf("%s Power off failed! (error = %d)\n",
233 parent->name, ret);
234 return -EIO;
235 }
236 }
237
238 ppriv->state_on = false;
239 debug("%s is powered off\n", parent->name);
240
241 parent_pd.dev = parent;
242 imx8_power_domain_off_parentnodes(&parent_pd);
243 }
244
245 return 0;
246}
247
248static int imx8_power_domain_off(struct power_domain *power_domain)
249{
250 int ret;
251
252 debug("%s(power_domain=%p)\n", __func__, power_domain);
253
254 /* Turn off the node */
255 ret = imx8_power_domain_off_node(power_domain);
256 if (ret) {
257 debug("Can't power off the node of dev %s, ret = %d\n",
258 power_domain->dev->name, ret);
259 return ret;
260 }
261
262 /* Turn off parent nodes, if sibling nodes are all off */
263 ret = imx8_power_domain_off_parentnodes(power_domain);
264 if (ret) {
265 printf("Failed to power off parent nodes of dev %s, ret = %d\n",
266 power_domain->dev->name, ret);
267 return ret;
268 }
269
270 return 0;
271}
272
273static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
274 struct ofnode_phandle_args *args)
275{
276 debug("%s(power_domain=%p)\n", __func__, power_domain);
277
278 /* Do nothing to the xlate, since we don't have args used */
279
280 return 0;
281}
282
283static int imx8_power_domain_bind(struct udevice *dev)
284{
285 int offset;
286 const char *name;
287 int ret = 0;
288
289 debug("%s(dev=%p)\n", __func__, dev);
290
291 offset = dev_of_offset(dev);
292 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
293 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
294 /* Bind the subnode to this driver */
295 name = fdt_get_name(gd->fdt_blob, offset, NULL);
296
297 ret = device_bind_with_driver_data(dev, dev->driver, name,
298 dev->driver_data,
299 offset_to_ofnode(offset),
300 NULL);
301
302 if (ret == -ENODEV)
303 printf("Driver '%s' refuses to bind\n",
304 dev->driver->name);
305
306 if (ret)
307 printf("Error binding driver '%s': %d\n",
308 dev->driver->name, ret);
309 }
310
311 return 0;
312}
313
314static int imx8_power_domain_probe(struct udevice *dev)
315{
316 struct imx8_power_domain_priv *ppriv;
317
318 debug("%s(dev=%s)\n", __func__, dev->name);
319
320 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
321
322 /* Set default to power off */
323 if (ppriv)
324 ppriv->state_on = false;
325
326 return 0;
327}
328
Simon Glassaad29ae2020-12-03 16:55:21 -0700329static int imx8_power_domain_of_to_plat(struct udevice *dev)
Peng Fanbc6244b2018-10-18 14:28:29 +0200330{
331 int reg;
Simon Glassb75b15b2020-12-03 16:55:23 -0700332 struct imx8_power_domain_plat *pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200333
334 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
335 if (reg == -1) {
336 debug("%s: Invalid resource id %d\n", __func__, reg);
337 return -EINVAL;
338 }
339 pdata->resource_id = (sc_rsrc_t)reg;
340
341 debug("%s resource_id %d\n", __func__, pdata->resource_id);
342
343 return 0;
344}
345
346static const struct udevice_id imx8_power_domain_ids[] = {
347 { .compatible = "nxp,imx8-pd" },
348 { }
349};
350
351struct power_domain_ops imx8_power_domain_ops = {
Peng Fanbc6244b2018-10-18 14:28:29 +0200352 .on = imx8_power_domain_on,
353 .off = imx8_power_domain_off,
354 .of_xlate = imx8_power_domain_of_xlate,
355};
356
357U_BOOT_DRIVER(imx8_power_domain) = {
358 .name = "imx8_power_domain",
359 .id = UCLASS_POWER_DOMAIN,
360 .of_match = imx8_power_domain_ids,
361 .bind = imx8_power_domain_bind,
362 .probe = imx8_power_domain_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700363 .of_to_plat = imx8_power_domain_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700364 .plat_auto = sizeof(struct imx8_power_domain_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700365 .priv_auto = sizeof(struct imx8_power_domain_priv),
Peng Fanbc6244b2018-10-18 14:28:29 +0200366 .ops = &imx8_power_domain_ops,
Ye Li625347a2020-05-04 21:16:51 +0800367 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Peng Fanbc6244b2018-10-18 14:28:29 +0200368};