blob: e2fae2dbc8600d7d92bc8f0cb5fb1ee46e28398d [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 Fanbc6244b2018-10-18 14:28:29 +020017#include <asm/arch/sci/sci.h>
18
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_request(struct power_domain *power_domain)
88{
89 debug("%s(power_domain=%p)\n", __func__, power_domain);
90
91 return 0;
92}
93
94static int imx8_power_domain_free(struct power_domain *power_domain)
95{
96 debug("%s(power_domain=%p)\n", __func__, power_domain);
97
98 return 0;
99}
100
101static int imx8_power_domain_on(struct power_domain *power_domain)
102{
103 struct udevice *dev = power_domain->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700104 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200105 struct imx8_power_domain_priv *ppriv;
106 sc_err_t ret;
107 int err;
108
109 struct power_domain parent_domain;
110 struct udevice *parent = dev_get_parent(dev);
111
112 /* Need to power on parent node first */
113 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
114 parent_domain.dev = parent;
115 err = imx8_power_domain_on(&parent_domain);
116 if (err)
117 return err;
118 }
119
Simon Glassb75b15b2020-12-03 16:55:23 -0700120 pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200121 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
122
123 debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
124 pdata->resource_id);
125
126 /* Already powered on */
127 if (ppriv->state_on)
128 return 0;
129
Leonard Crestez797be1b2020-05-04 21:16:54 +0800130 if (pdata->resource_id != SC_R_NONE) {
Ye Li479a96312020-05-04 21:16:53 +0800131 if (!sc_rm_is_resource_owned(-1, pdata->resource_id))
132 printf("%s [%d] not owned by curr partition\n", dev->name, pdata->resource_id);
133
Peng Fanbc6244b2018-10-18 14:28:29 +0200134 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
135 SC_PM_PW_MODE_ON);
136 if (ret) {
137 printf("Error: %s Power up failed! (error = %d)\n",
138 dev->name, ret);
139 return -EIO;
140 }
141 }
142
143 ppriv->state_on = true;
144 debug("%s is powered on\n", dev->name);
145
146 return 0;
147}
148
149static int imx8_power_domain_off_node(struct power_domain *power_domain)
150{
151 struct udevice *dev = power_domain->dev;
152 struct udevice *child;
153 struct imx8_power_domain_priv *ppriv;
154 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700155 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200156 sc_err_t ret;
157
158 ppriv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700159 pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200160
161 debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
162
163 /* Already powered off */
164 if (!ppriv->state_on)
165 return 0;
166
167 /* Check if all subnodes are off */
168 for (device_find_first_child(dev, &child);
169 child;
170 device_find_next_child(&child)) {
171 if (device_active(child)) {
172 child_ppriv =
173 (struct imx8_power_domain_priv *)dev_get_priv(child);
174 if (child_ppriv->state_on)
175 return -EPERM;
176 }
177 }
178
Leonard Crestez797be1b2020-05-04 21:16:54 +0800179 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200180 ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
181 SC_PM_PW_MODE_OFF);
182 if (ret) {
Peng Fan6c3a5b32020-05-04 21:16:52 +0800183 if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
184 printf("%s not owned by curr partition %d\n", dev->name, pdata->resource_id);
185 return 0;
186 }
Peng Fanbc6244b2018-10-18 14:28:29 +0200187 printf("Error: %s Power off failed! (error = %d)\n",
188 dev->name, ret);
189 return -EIO;
190 }
191 }
192
193 ppriv->state_on = false;
194 debug("%s is powered off\n", dev->name);
195
196 return 0;
197}
198
199static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
200{
201 struct udevice *dev = power_domain->dev;
202 struct udevice *parent = dev_get_parent(dev);
203 struct udevice *child;
204 struct imx8_power_domain_priv *ppriv;
205 struct imx8_power_domain_priv *child_ppriv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700206 struct imx8_power_domain_plat *pdata;
Peng Fanbc6244b2018-10-18 14:28:29 +0200207 sc_err_t ret;
208 struct power_domain parent_pd;
209
210 if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
211 pdata =
Simon Glassb75b15b2020-12-03 16:55:23 -0700212 (struct imx8_power_domain_plat *)dev_get_plat(parent);
Peng Fanbc6244b2018-10-18 14:28:29 +0200213 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
214
215 debug("%s, %s, state_on %d\n", __func__, parent->name,
216 ppriv->state_on);
217
218 /* Already powered off */
219 if (!ppriv->state_on)
220 return 0;
221
222 /*
223 * Check if all sibling nodes are off. If yes,
224 * power off parent
225 */
226 for (device_find_first_child(parent, &child); child;
227 device_find_next_child(&child)) {
228 if (device_active(child)) {
229 child_ppriv = (struct imx8_power_domain_priv *)
230 dev_get_priv(child);
231 /* Find a power on sibling */
232 if (child_ppriv->state_on) {
233 debug("sibling %s, state_on %d\n",
234 child->name,
235 child_ppriv->state_on);
236 return 0;
237 }
238 }
239 }
240
241 /* power off parent */
Leonard Crestez797be1b2020-05-04 21:16:54 +0800242 if (pdata->resource_id != SC_R_NONE) {
Peng Fanbc6244b2018-10-18 14:28:29 +0200243 ret = sc_pm_set_resource_power_mode(-1,
244 pdata->resource_id,
245 SC_PM_PW_MODE_OFF);
246 if (ret) {
247 printf("%s Power off failed! (error = %d)\n",
248 parent->name, ret);
249 return -EIO;
250 }
251 }
252
253 ppriv->state_on = false;
254 debug("%s is powered off\n", parent->name);
255
256 parent_pd.dev = parent;
257 imx8_power_domain_off_parentnodes(&parent_pd);
258 }
259
260 return 0;
261}
262
263static int imx8_power_domain_off(struct power_domain *power_domain)
264{
265 int ret;
266
267 debug("%s(power_domain=%p)\n", __func__, power_domain);
268
269 /* Turn off the node */
270 ret = imx8_power_domain_off_node(power_domain);
271 if (ret) {
272 debug("Can't power off the node of dev %s, ret = %d\n",
273 power_domain->dev->name, ret);
274 return ret;
275 }
276
277 /* Turn off parent nodes, if sibling nodes are all off */
278 ret = imx8_power_domain_off_parentnodes(power_domain);
279 if (ret) {
280 printf("Failed to power off parent nodes of dev %s, ret = %d\n",
281 power_domain->dev->name, ret);
282 return ret;
283 }
284
285 return 0;
286}
287
288static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
289 struct ofnode_phandle_args *args)
290{
291 debug("%s(power_domain=%p)\n", __func__, power_domain);
292
293 /* Do nothing to the xlate, since we don't have args used */
294
295 return 0;
296}
297
298static int imx8_power_domain_bind(struct udevice *dev)
299{
300 int offset;
301 const char *name;
302 int ret = 0;
303
304 debug("%s(dev=%p)\n", __func__, dev);
305
306 offset = dev_of_offset(dev);
307 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
308 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
309 /* Bind the subnode to this driver */
310 name = fdt_get_name(gd->fdt_blob, offset, NULL);
311
312 ret = device_bind_with_driver_data(dev, dev->driver, name,
313 dev->driver_data,
314 offset_to_ofnode(offset),
315 NULL);
316
317 if (ret == -ENODEV)
318 printf("Driver '%s' refuses to bind\n",
319 dev->driver->name);
320
321 if (ret)
322 printf("Error binding driver '%s': %d\n",
323 dev->driver->name, ret);
324 }
325
326 return 0;
327}
328
329static int imx8_power_domain_probe(struct udevice *dev)
330{
331 struct imx8_power_domain_priv *ppriv;
332
333 debug("%s(dev=%s)\n", __func__, dev->name);
334
335 ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
336
337 /* Set default to power off */
338 if (ppriv)
339 ppriv->state_on = false;
340
341 return 0;
342}
343
Simon Glassaad29ae2020-12-03 16:55:21 -0700344static int imx8_power_domain_of_to_plat(struct udevice *dev)
Peng Fanbc6244b2018-10-18 14:28:29 +0200345{
346 int reg;
Simon Glassb75b15b2020-12-03 16:55:23 -0700347 struct imx8_power_domain_plat *pdata = dev_get_plat(dev);
Peng Fanbc6244b2018-10-18 14:28:29 +0200348
349 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
350 if (reg == -1) {
351 debug("%s: Invalid resource id %d\n", __func__, reg);
352 return -EINVAL;
353 }
354 pdata->resource_id = (sc_rsrc_t)reg;
355
356 debug("%s resource_id %d\n", __func__, pdata->resource_id);
357
358 return 0;
359}
360
361static const struct udevice_id imx8_power_domain_ids[] = {
362 { .compatible = "nxp,imx8-pd" },
363 { }
364};
365
366struct power_domain_ops imx8_power_domain_ops = {
367 .request = imx8_power_domain_request,
Simon Glass92ed3a32020-02-03 07:35:51 -0700368 .rfree = imx8_power_domain_free,
Peng Fanbc6244b2018-10-18 14:28:29 +0200369 .on = imx8_power_domain_on,
370 .off = imx8_power_domain_off,
371 .of_xlate = imx8_power_domain_of_xlate,
372};
373
374U_BOOT_DRIVER(imx8_power_domain) = {
375 .name = "imx8_power_domain",
376 .id = UCLASS_POWER_DOMAIN,
377 .of_match = imx8_power_domain_ids,
378 .bind = imx8_power_domain_bind,
379 .probe = imx8_power_domain_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700380 .of_to_plat = imx8_power_domain_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700381 .plat_auto = sizeof(struct imx8_power_domain_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700382 .priv_auto = sizeof(struct imx8_power_domain_priv),
Peng Fanbc6244b2018-10-18 14:28:29 +0200383 .ops = &imx8_power_domain_ops,
Ye Li625347a2020-05-04 21:16:51 +0800384 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Peng Fanbc6244b2018-10-18 14:28:29 +0200385};