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