2018-10-18 12:28:29 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright 2017 NXP
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2018-10-18 12:28:29 +00:00
|
|
|
#include <power-domain-uclass.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2018-10-18 12:28:29 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/arch/power-domain.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/device.h>
|
2020-05-05 12:28:40 +00:00
|
|
|
#include <dm/uclass-internal.h>
|
2018-10-18 12:28:29 +00:00
|
|
|
#include <asm/arch/sci/sci.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
struct imx8_power_domain_priv {
|
|
|
|
bool state_on;
|
|
|
|
};
|
|
|
|
|
2020-05-05 12:28:40 +00:00
|
|
|
static bool check_device_power_off(struct udevice *dev,
|
|
|
|
const char *permanent_on_devices[],
|
|
|
|
int size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (!strcmp(dev->name, permanent_on_devices[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void imx8_power_off_pd_devices(const char *permanent_on_devices[], int size)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct power_domain pd;
|
|
|
|
|
|
|
|
for (uclass_find_first_device(UCLASS_POWER_DOMAIN, &dev); dev;
|
|
|
|
uclass_find_next_device(&dev)) {
|
|
|
|
if (!device_active(dev))
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Power off active pd devices except the permanent
|
|
|
|
* power on devices
|
|
|
|
*/
|
|
|
|
if (check_device_power_off(dev, permanent_on_devices, size)) {
|
|
|
|
pd.dev = dev;
|
|
|
|
power_domain_off(&pd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:16:50 +00:00
|
|
|
int imx8_power_domain_lookup_name(const char *name,
|
|
|
|
struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct power_domain_ops *ops;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug("%s(power_domain=%p name=%s)\n", __func__, power_domain, name);
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_name(UCLASS_POWER_DOMAIN, name, &dev);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s fail: %s, ret = %d\n", __func__, name, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops = (struct power_domain_ops *)dev->driver->ops;
|
|
|
|
power_domain->dev = dev;
|
|
|
|
ret = ops->request(power_domain);
|
|
|
|
if (ret) {
|
|
|
|
debug("ops->request() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s ok: %s\n", __func__, dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:28:29 +00:00
|
|
|
static int imx8_power_domain_on(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct udevice *dev = power_domain->dev;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct imx8_power_domain_plat *pdata;
|
2018-10-18 12:28:29 +00:00
|
|
|
struct imx8_power_domain_priv *ppriv;
|
|
|
|
sc_err_t ret;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
struct power_domain parent_domain;
|
|
|
|
struct udevice *parent = dev_get_parent(dev);
|
|
|
|
|
|
|
|
/* Need to power on parent node first */
|
|
|
|
if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
|
|
|
|
parent_domain.dev = parent;
|
|
|
|
err = imx8_power_domain_on(&parent_domain);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
pdata = (struct imx8_power_domain_plat *)dev_get_plat(dev);
|
2018-10-18 12:28:29 +00:00
|
|
|
ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
|
|
|
|
|
|
|
|
debug("%s(power_domain=%s) resource_id %d\n", __func__, dev->name,
|
|
|
|
pdata->resource_id);
|
|
|
|
|
|
|
|
/* Already powered on */
|
|
|
|
if (ppriv->state_on)
|
|
|
|
return 0;
|
|
|
|
|
2020-05-04 13:16:54 +00:00
|
|
|
if (pdata->resource_id != SC_R_NONE) {
|
2020-05-04 13:16:53 +00:00
|
|
|
if (!sc_rm_is_resource_owned(-1, pdata->resource_id))
|
|
|
|
printf("%s [%d] not owned by curr partition\n", dev->name, pdata->resource_id);
|
|
|
|
|
2018-10-18 12:28:29 +00:00
|
|
|
ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
|
|
|
|
SC_PM_PW_MODE_ON);
|
|
|
|
if (ret) {
|
|
|
|
printf("Error: %s Power up failed! (error = %d)\n",
|
|
|
|
dev->name, ret);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ppriv->state_on = true;
|
|
|
|
debug("%s is powered on\n", dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_off_node(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct udevice *dev = power_domain->dev;
|
|
|
|
struct udevice *child;
|
|
|
|
struct imx8_power_domain_priv *ppriv;
|
|
|
|
struct imx8_power_domain_priv *child_ppriv;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct imx8_power_domain_plat *pdata;
|
2018-10-18 12:28:29 +00:00
|
|
|
sc_err_t ret;
|
|
|
|
|
|
|
|
ppriv = dev_get_priv(dev);
|
2020-12-03 23:55:20 +00:00
|
|
|
pdata = dev_get_plat(dev);
|
2018-10-18 12:28:29 +00:00
|
|
|
|
|
|
|
debug("%s, %s, state_on %d\n", __func__, dev->name, ppriv->state_on);
|
|
|
|
|
|
|
|
/* Already powered off */
|
|
|
|
if (!ppriv->state_on)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check if all subnodes are off */
|
|
|
|
for (device_find_first_child(dev, &child);
|
|
|
|
child;
|
|
|
|
device_find_next_child(&child)) {
|
|
|
|
if (device_active(child)) {
|
|
|
|
child_ppriv =
|
|
|
|
(struct imx8_power_domain_priv *)dev_get_priv(child);
|
|
|
|
if (child_ppriv->state_on)
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:16:54 +00:00
|
|
|
if (pdata->resource_id != SC_R_NONE) {
|
2018-10-18 12:28:29 +00:00
|
|
|
ret = sc_pm_set_resource_power_mode(-1, pdata->resource_id,
|
|
|
|
SC_PM_PW_MODE_OFF);
|
|
|
|
if (ret) {
|
2020-05-04 13:16:52 +00:00
|
|
|
if (!sc_rm_is_resource_owned(-1, pdata->resource_id)) {
|
|
|
|
printf("%s not owned by curr partition %d\n", dev->name, pdata->resource_id);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-18 12:28:29 +00:00
|
|
|
printf("Error: %s Power off failed! (error = %d)\n",
|
|
|
|
dev->name, ret);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ppriv->state_on = false;
|
|
|
|
debug("%s is powered off\n", dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_off_parentnodes(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct udevice *dev = power_domain->dev;
|
|
|
|
struct udevice *parent = dev_get_parent(dev);
|
|
|
|
struct udevice *child;
|
|
|
|
struct imx8_power_domain_priv *ppriv;
|
|
|
|
struct imx8_power_domain_priv *child_ppriv;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct imx8_power_domain_plat *pdata;
|
2018-10-18 12:28:29 +00:00
|
|
|
sc_err_t ret;
|
|
|
|
struct power_domain parent_pd;
|
|
|
|
|
|
|
|
if (device_get_uclass_id(parent) == UCLASS_POWER_DOMAIN) {
|
|
|
|
pdata =
|
2020-12-03 23:55:23 +00:00
|
|
|
(struct imx8_power_domain_plat *)dev_get_plat(parent);
|
2018-10-18 12:28:29 +00:00
|
|
|
ppriv = (struct imx8_power_domain_priv *)dev_get_priv(parent);
|
|
|
|
|
|
|
|
debug("%s, %s, state_on %d\n", __func__, parent->name,
|
|
|
|
ppriv->state_on);
|
|
|
|
|
|
|
|
/* Already powered off */
|
|
|
|
if (!ppriv->state_on)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if all sibling nodes are off. If yes,
|
|
|
|
* power off parent
|
|
|
|
*/
|
|
|
|
for (device_find_first_child(parent, &child); child;
|
|
|
|
device_find_next_child(&child)) {
|
|
|
|
if (device_active(child)) {
|
|
|
|
child_ppriv = (struct imx8_power_domain_priv *)
|
|
|
|
dev_get_priv(child);
|
|
|
|
/* Find a power on sibling */
|
|
|
|
if (child_ppriv->state_on) {
|
|
|
|
debug("sibling %s, state_on %d\n",
|
|
|
|
child->name,
|
|
|
|
child_ppriv->state_on);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* power off parent */
|
2020-05-04 13:16:54 +00:00
|
|
|
if (pdata->resource_id != SC_R_NONE) {
|
2018-10-18 12:28:29 +00:00
|
|
|
ret = sc_pm_set_resource_power_mode(-1,
|
|
|
|
pdata->resource_id,
|
|
|
|
SC_PM_PW_MODE_OFF);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s Power off failed! (error = %d)\n",
|
|
|
|
parent->name, ret);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ppriv->state_on = false;
|
|
|
|
debug("%s is powered off\n", parent->name);
|
|
|
|
|
|
|
|
parent_pd.dev = parent;
|
|
|
|
imx8_power_domain_off_parentnodes(&parent_pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_off(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
|
|
|
/* Turn off the node */
|
|
|
|
ret = imx8_power_domain_off_node(power_domain);
|
|
|
|
if (ret) {
|
|
|
|
debug("Can't power off the node of dev %s, ret = %d\n",
|
|
|
|
power_domain->dev->name, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn off parent nodes, if sibling nodes are all off */
|
|
|
|
ret = imx8_power_domain_off_parentnodes(power_domain);
|
|
|
|
if (ret) {
|
|
|
|
printf("Failed to power off parent nodes of dev %s, ret = %d\n",
|
|
|
|
power_domain->dev->name, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_of_xlate(struct power_domain *power_domain,
|
|
|
|
struct ofnode_phandle_args *args)
|
|
|
|
{
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
|
|
|
/* Do nothing to the xlate, since we don't have args used */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
const char *name;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
debug("%s(dev=%p)\n", __func__, dev);
|
|
|
|
|
|
|
|
offset = dev_of_offset(dev);
|
|
|
|
for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
|
|
|
|
offset = fdt_next_subnode(gd->fdt_blob, offset)) {
|
|
|
|
/* Bind the subnode to this driver */
|
|
|
|
name = fdt_get_name(gd->fdt_blob, offset, NULL);
|
|
|
|
|
|
|
|
ret = device_bind_with_driver_data(dev, dev->driver, name,
|
|
|
|
dev->driver_data,
|
|
|
|
offset_to_ofnode(offset),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (ret == -ENODEV)
|
|
|
|
printf("Driver '%s' refuses to bind\n",
|
|
|
|
dev->driver->name);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
printf("Error binding driver '%s': %d\n",
|
|
|
|
dev->driver->name, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8_power_domain_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct imx8_power_domain_priv *ppriv;
|
|
|
|
|
|
|
|
debug("%s(dev=%s)\n", __func__, dev->name);
|
|
|
|
|
|
|
|
ppriv = (struct imx8_power_domain_priv *)dev_get_priv(dev);
|
|
|
|
|
|
|
|
/* Set default to power off */
|
|
|
|
if (ppriv)
|
|
|
|
ppriv->state_on = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int imx8_power_domain_of_to_plat(struct udevice *dev)
|
2018-10-18 12:28:29 +00:00
|
|
|
{
|
|
|
|
int reg;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct imx8_power_domain_plat *pdata = dev_get_plat(dev);
|
2018-10-18 12:28:29 +00:00
|
|
|
|
|
|
|
reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
|
|
|
|
if (reg == -1) {
|
|
|
|
debug("%s: Invalid resource id %d\n", __func__, reg);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
pdata->resource_id = (sc_rsrc_t)reg;
|
|
|
|
|
|
|
|
debug("%s resource_id %d\n", __func__, pdata->resource_id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id imx8_power_domain_ids[] = {
|
|
|
|
{ .compatible = "nxp,imx8-pd" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct power_domain_ops imx8_power_domain_ops = {
|
|
|
|
.on = imx8_power_domain_on,
|
|
|
|
.off = imx8_power_domain_off,
|
|
|
|
.of_xlate = imx8_power_domain_of_xlate,
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(imx8_power_domain) = {
|
|
|
|
.name = "imx8_power_domain",
|
|
|
|
.id = UCLASS_POWER_DOMAIN,
|
|
|
|
.of_match = imx8_power_domain_ids,
|
|
|
|
.bind = imx8_power_domain_bind,
|
|
|
|
.probe = imx8_power_domain_probe,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = imx8_power_domain_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct imx8_power_domain_plat),
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct imx8_power_domain_priv),
|
2018-10-18 12:28:29 +00:00
|
|
|
.ops = &imx8_power_domain_ops,
|
2020-05-04 13:16:51 +00:00
|
|
|
.flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
|
2018-10-18 12:28:29 +00:00
|
|
|
};
|