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 | 2e0644a | 2023-04-28 12:08:09 +0800 | [diff] [blame^] | 17 | #include <firmware/imx/sci/sci.h> |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 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_on(struct power_domain *power_domain) |
| 88 | { |
| 89 | struct udevice *dev = power_domain->dev; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 90 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 91 | 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 106 | pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 107 | 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 116 | if (pdata->resource_id != SC_R_NONE) { |
Ye Li | 479a9631 | 2020-05-04 21:16:53 +0800 | [diff] [blame] | 117 | 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 Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 120 | 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 | |
| 135 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 141 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 142 | sc_err_t ret; |
| 143 | |
| 144 | ppriv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 145 | pdata = dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 146 | |
| 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 165 | if (pdata->resource_id != SC_R_NONE) { |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 166 | ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id, |
| 167 | SC_PM_PW_MODE_OFF); |
| 168 | if (ret) { |
Peng Fan | 6c3a5b3 | 2020-05-04 21:16:52 +0800 | [diff] [blame] | 169 | 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 Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 173 | 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 | |
| 185 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 192 | struct imx8_power_domain_plat *pdata; |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 193 | sc_err_t ret; |
| 194 | struct power_domain parent_pd; |
| 195 | |
| 196 | if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) { |
| 197 | pdata = |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 198 | (struct imx8_power_domain_plat *)dev_get_plat(parent); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 199 | 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 Crestez | 797be1b | 2020-05-04 21:16:54 +0800 | [diff] [blame] | 228 | if (pdata->resource_id != SC_R_NONE) { |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 229 | 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 | |
| 249 | static 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 | |
| 274 | static 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 | |
| 284 | static 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 | |
| 315 | static 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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 330 | static int imx8_power_domain_of_to_plat(struct udevice *dev) |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 331 | { |
| 332 | int reg; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 333 | struct imx8_power_domain_plat *pdata = dev_get_plat(dev); |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 334 | |
| 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 | |
| 347 | static const struct udevice_id imx8_power_domain_ids[] = { |
| 348 | { .compatible = "nxp,imx8-pd" }, |
| 349 | { } |
| 350 | }; |
| 351 | |
| 352 | struct power_domain_ops imx8_power_domain_ops = { |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 353 | .on = imx8_power_domain_on, |
| 354 | .off = imx8_power_domain_off, |
| 355 | .of_xlate = imx8_power_domain_of_xlate, |
| 356 | }; |
| 357 | |
| 358 | U_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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 364 | .of_to_plat = imx8_power_domain_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 365 | .plat_auto = sizeof(struct imx8_power_domain_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 366 | .priv_auto = sizeof(struct imx8_power_domain_priv), |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 367 | .ops = &imx8_power_domain_ops, |
Ye Li | 625347a | 2020-05-04 21:16:51 +0800 | [diff] [blame] | 368 | .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Peng Fan | bc6244b | 2018-10-18 14:28:29 +0200 | [diff] [blame] | 369 | }; |