blob: c32dbcc31ae44a63bdccf2fba493f7c3a4971a1e [file] [log] [blame]
Patrick Wildtd08a1942019-10-03 15:51:50 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2017 NXP
4 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <malloc.h>
Patrick Wildtd08a1942019-10-03 15:51:50 +02009#include <power-domain-uclass.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Patrick Wildtd08a1942019-10-03 15:51:50 +020011#include <asm/io.h>
Patrick Wildtd08a1942019-10-03 15:51:50 +020012#include <asm/mach-imx/sys_proto.h>
13#include <dm/device-internal.h>
14#include <dm/device.h>
15#include <imx_sip.h>
Peng Fan4b8f22d2020-05-11 15:16:37 +080016#include <linux/arm-smccc.h>
Patrick Wildtd08a1942019-10-03 15:51:50 +020017
18DECLARE_GLOBAL_DATA_PTR;
19
Marek Vasut19842b62022-04-13 00:42:50 +020020struct imx8m_power_domain_plat {
21 int resource_id;
22 int has_pd;
23 struct power_domain pd;
24};
25
Patrick Wildtd08a1942019-10-03 15:51:50 +020026static int imx8m_power_domain_on(struct power_domain *power_domain)
27{
28 struct udevice *dev = power_domain->dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -070029 struct imx8m_power_domain_plat *pdata;
Peng Fan4b8f22d2020-05-11 15:16:37 +080030
Simon Glassc69cda22020-12-03 16:55:20 -070031 pdata = dev_get_plat(dev);
Patrick Wildtd08a1942019-10-03 15:51:50 +020032
33 if (pdata->resource_id < 0)
34 return -EINVAL;
35
36 if (pdata->has_pd)
37 power_domain_on(&pdata->pd);
38
Peng Fan4b8f22d2020-05-11 15:16:37 +080039 arm_smccc_smc(IMX_SIP_GPC, IMX_SIP_GPC_PM_DOMAIN,
40 pdata->resource_id, 1, 0, 0, 0, 0, NULL);
Patrick Wildtd08a1942019-10-03 15:51:50 +020041
42 return 0;
43}
44
45static int imx8m_power_domain_off(struct power_domain *power_domain)
46{
47 struct udevice *dev = power_domain->dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -070048 struct imx8m_power_domain_plat *pdata;
Simon Glassc69cda22020-12-03 16:55:20 -070049 pdata = dev_get_plat(dev);
Patrick Wildtd08a1942019-10-03 15:51:50 +020050
51 if (pdata->resource_id < 0)
52 return -EINVAL;
53
Peng Fan4b8f22d2020-05-11 15:16:37 +080054 arm_smccc_smc(IMX_SIP_GPC, IMX_SIP_GPC_PM_DOMAIN,
55 pdata->resource_id, 0, 0, 0, 0, 0, NULL);
Patrick Wildtd08a1942019-10-03 15:51:50 +020056
57 if (pdata->has_pd)
58 power_domain_off(&pdata->pd);
59
60 return 0;
61}
62
63static int imx8m_power_domain_of_xlate(struct power_domain *power_domain,
64 struct ofnode_phandle_args *args)
65{
66 return 0;
67}
68
69static int imx8m_power_domain_bind(struct udevice *dev)
70{
71 int offset;
72 const char *name;
73 int ret = 0;
74
75 offset = dev_of_offset(dev);
76 for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0;
77 offset = fdt_next_subnode(gd->fdt_blob, offset)) {
78 /* Bind the subnode to this driver */
79 name = fdt_get_name(gd->fdt_blob, offset, NULL);
80
Marek Vasut8741d922022-04-13 00:42:49 +020081 /* Descend into 'pgc' subnode */
82 if (!strstr(name, "power-domain")) {
83 offset = fdt_first_subnode(gd->fdt_blob, offset);
84 name = fdt_get_name(gd->fdt_blob, offset, NULL);
85 }
86
Patrick Wildtd08a1942019-10-03 15:51:50 +020087 ret = device_bind_with_driver_data(dev, dev->driver, name,
88 dev->driver_data,
89 offset_to_ofnode(offset),
90 NULL);
91
92 if (ret == -ENODEV)
93 printf("Driver '%s' refuses to bind\n",
94 dev->driver->name);
95
96 if (ret)
97 printf("Error binding driver '%s': %d\n",
98 dev->driver->name, ret);
99 }
100
101 return 0;
102}
103
Simon Glassd1998a92020-12-03 16:55:21 -0700104static int imx8m_power_domain_of_to_plat(struct udevice *dev)
Patrick Wildtd08a1942019-10-03 15:51:50 +0200105{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700106 struct imx8m_power_domain_plat *pdata = dev_get_plat(dev);
Patrick Wildtd08a1942019-10-03 15:51:50 +0200107
108 pdata->resource_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
109 "reg", -1);
110
111 if (!power_domain_get(dev, &pdata->pd))
112 pdata->has_pd = 1;
113
114 return 0;
115}
116
117static const struct udevice_id imx8m_power_domain_ids[] = {
118 { .compatible = "fsl,imx8mq-gpc" },
Marek Vasutda160342021-04-02 13:32:40 +0200119 { .compatible = "fsl,imx8mm-gpc" },
Marek Vasutf174a0d2021-04-10 00:19:27 +0200120 { .compatible = "fsl,imx8mn-gpc" },
Patrick Wildtd08a1942019-10-03 15:51:50 +0200121 { }
122};
123
124struct power_domain_ops imx8m_power_domain_ops = {
Patrick Wildtd08a1942019-10-03 15:51:50 +0200125 .on = imx8m_power_domain_on,
126 .off = imx8m_power_domain_off,
127 .of_xlate = imx8m_power_domain_of_xlate,
128};
129
130U_BOOT_DRIVER(imx8m_power_domain) = {
131 .name = "imx8m_power_domain",
132 .id = UCLASS_POWER_DOMAIN,
133 .of_match = imx8m_power_domain_ids,
134 .bind = imx8m_power_domain_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700135 .of_to_plat = imx8m_power_domain_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700136 .plat_auto = sizeof(struct imx8m_power_domain_plat),
Patrick Wildtd08a1942019-10-03 15:51:50 +0200137 .ops = &imx8m_power_domain_ops,
138};