Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2017 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 10 | #include <power-domain-uclass.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/arch/power-domain.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/device.h> |
Peng Fan | 1b9729a | 2020-05-05 20:28:40 +0800 | [diff] [blame] | 16 | #include <dm/uclass-internal.h> |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 17 | #include <asm/arch/sci/sci.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | struct imx8_power_domain_priv { |
| 22 | bool state_on; |
| 23 | }; |
| 24 | |
Peng Fan | 1b9729a | 2020-05-05 20:28:40 +0800 | [diff] [blame] | 25 | static 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 | |
| 39 | void 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 Fan | b48acc5 | 2020-05-04 21:16:50 +0800 | [diff] [blame] | 59 | int 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 Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 87 | static 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 | |
| 94 | static 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 | |
| 101 | static int imx8_power_domain_on(struct power_domain *power_domain) |
| 102 | { |
| 103 | struct udevice *dev = power_domain->dev; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 104 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 105 | 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 120 | pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 121 | 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 130 | if (pdata->resource_id != SC_R_NONE) { |
Ye Li | 479a9631 | 2020-05-04 21:16:53 +0800 | [diff] [blame] | 131 | 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 Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 134 | 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 | |
| 149 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 155 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 156 | sc_err_t ret; |
| 157 | |
| 158 | ppriv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 159 | pdata = dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 160 | |
| 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 179 | if (pdata->resource_id != SC_R_NONE) { |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 180 | ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id, |
| 181 | SC_PM_PW_MODE_OFF); |
| 182 | if (ret) { |
Peng Fan | 6c3a5b3 | 2020-05-04 21:16:52 +0800 | [diff] [blame] | 183 | 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 Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 187 | 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 | |
| 199 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 206 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 207 | sc_err_t ret; |
| 208 | struct power_domain parent_pd; |
| 209 | |
| 210 | if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) { |
| 211 | pdata = |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 212 | (struct imx8_power_domain_plat *)dev_get_plat(parent); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 213 | 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 242 | if (pdata->resource_id != SC_R_NONE) { |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 243 | 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 | |
| 263 | static 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 | |
| 288 | static 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 | |
| 298 | static 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 | |
| 329 | static 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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 344 | static int imx8_power_domain_of_to_plat(struct udevice *dev) |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 345 | { |
| 346 | int reg; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 347 | struct imx8_power_domain_plat *pdata = dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 348 | |
| 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 | |
| 361 | static const struct udevice_id imx8_power_domain_ids[] = { |
| 362 | { .compatible = "nxp,imx8-pd" }, |
| 363 | { } |
| 364 | }; |
| 365 | |
| 366 | struct power_domain_ops imx8_power_domain_ops = { |
| 367 | .request = imx8_power_domain_request, |
Simon Glass | 92ed3a3 | 2020-02-03 07:35:51 -0700 | [diff] [blame] | 368 | .rfree = imx8_power_domain_free, |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 369 | .on = imx8_power_domain_on, |
| 370 | .off = imx8_power_domain_off, |
| 371 | .of_xlate = imx8_power_domain_of_xlate, |
| 372 | }; |
| 373 | |
| 374 | U_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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 380 | .of_to_plat = imx8_power_domain_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 381 | .plat_auto = sizeof(struct imx8_power_domain_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 382 | .priv_auto = sizeof(struct imx8_power_domain_priv), |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 383 | .ops = &imx8_power_domain_ops, |
Ye Li | 625347a | 2020-05-04 21:16:51 +0800 | [diff] [blame] | 384 | .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 385 | }; |