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