1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (c) 2019 Amlogic, Inc.
4 * Author: Jianxin Pan <jianxin.pan@amlogic.com>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_domain.h>
13 #include <dt-bindings/power/meson-a1-power.h>
14 #include <linux/arm-smccc.h>
15 #include <linux/firmware/meson/meson_sm.h>
20 struct meson_secure_pwrc_domain
{
21 struct generic_pm_domain base
;
23 struct meson_secure_pwrc
*pwrc
;
26 struct meson_secure_pwrc
{
27 struct meson_secure_pwrc_domain
*domains
;
28 struct genpd_onecell_data xlate
;
29 struct meson_sm_firmware
*fw
;
32 struct meson_secure_pwrc_domain_desc
{
36 bool (*is_off
)(struct meson_secure_pwrc_domain
*pwrc_domain
);
39 struct meson_secure_pwrc_domain_data
{
41 struct meson_secure_pwrc_domain_desc
*domains
;
44 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain
*pwrc_domain
)
48 if (meson_sm_call(pwrc_domain
->pwrc
->fw
, SM_A1_PWRC_GET
, &is_off
,
49 pwrc_domain
->index
, 0, 0, 0, 0) < 0)
50 pr_err("failed to get power domain status\n");
55 static int meson_secure_pwrc_off(struct generic_pm_domain
*domain
)
58 struct meson_secure_pwrc_domain
*pwrc_domain
=
59 container_of(domain
, struct meson_secure_pwrc_domain
, base
);
61 if (meson_sm_call(pwrc_domain
->pwrc
->fw
, SM_A1_PWRC_SET
, NULL
,
62 pwrc_domain
->index
, PWRC_OFF
, 0, 0, 0) < 0) {
63 pr_err("failed to set power domain off\n");
70 static int meson_secure_pwrc_on(struct generic_pm_domain
*domain
)
73 struct meson_secure_pwrc_domain
*pwrc_domain
=
74 container_of(domain
, struct meson_secure_pwrc_domain
, base
);
76 if (meson_sm_call(pwrc_domain
->pwrc
->fw
, SM_A1_PWRC_SET
, NULL
,
77 pwrc_domain
->index
, PWRC_ON
, 0, 0, 0) < 0) {
78 pr_err("failed to set power domain on\n");
85 #define SEC_PD(__name, __flag) \
86 [PWRC_##__name##_ID] = \
89 .index = PWRC_##__name##_ID, \
90 .is_off = pwrc_secure_is_off, \
94 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains
[] = {
97 /* UART should keep working in ATF after suspend and before resume */
98 SEC_PD(UART
, GENPD_FLAG_ALWAYS_ON
),
99 /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
100 SEC_PD(DMC
, GENPD_FLAG_ALWAYS_ON
),
109 /* SRAMB is used as ATF runtime memory, and should be always on */
110 SEC_PD(RAMB
, GENPD_FLAG_ALWAYS_ON
),
115 /* NIC is for the Arm NIC-400 interconnect, and should be always on */
116 SEC_PD(NIC
, GENPD_FLAG_ALWAYS_ON
),
121 static int meson_secure_pwrc_probe(struct platform_device
*pdev
)
124 struct device_node
*sm_np
;
125 struct meson_secure_pwrc
*pwrc
;
126 const struct meson_secure_pwrc_domain_data
*match
;
128 match
= of_device_get_match_data(&pdev
->dev
);
130 dev_err(&pdev
->dev
, "failed to get match data\n");
134 sm_np
= of_find_compatible_node(NULL
, NULL
, "amlogic,meson-gxbb-sm");
136 dev_err(&pdev
->dev
, "no secure-monitor node\n");
140 pwrc
= devm_kzalloc(&pdev
->dev
, sizeof(*pwrc
), GFP_KERNEL
);
144 pwrc
->fw
= meson_sm_get(sm_np
);
147 return -EPROBE_DEFER
;
149 pwrc
->xlate
.domains
= devm_kcalloc(&pdev
->dev
, match
->count
,
150 sizeof(*pwrc
->xlate
.domains
),
152 if (!pwrc
->xlate
.domains
)
155 pwrc
->domains
= devm_kcalloc(&pdev
->dev
, match
->count
,
156 sizeof(*pwrc
->domains
), GFP_KERNEL
);
160 pwrc
->xlate
.num_domains
= match
->count
;
161 platform_set_drvdata(pdev
, pwrc
);
163 for (i
= 0 ; i
< match
->count
; ++i
) {
164 struct meson_secure_pwrc_domain
*dom
= &pwrc
->domains
[i
];
166 if (!match
->domains
[i
].index
)
170 dom
->index
= match
->domains
[i
].index
;
171 dom
->base
.name
= match
->domains
[i
].name
;
172 dom
->base
.flags
= match
->domains
[i
].flags
;
173 dom
->base
.power_on
= meson_secure_pwrc_on
;
174 dom
->base
.power_off
= meson_secure_pwrc_off
;
176 pm_genpd_init(&dom
->base
, NULL
, match
->domains
[i
].is_off(dom
));
178 pwrc
->xlate
.domains
[i
] = &dom
->base
;
181 return of_genpd_add_provider_onecell(pdev
->dev
.of_node
, &pwrc
->xlate
);
184 static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data
= {
185 .domains
= a1_pwrc_domains
,
186 .count
= ARRAY_SIZE(a1_pwrc_domains
),
189 static const struct of_device_id meson_secure_pwrc_match_table
[] = {
191 .compatible
= "amlogic,meson-a1-pwrc",
192 .data
= &meson_secure_a1_pwrc_data
,
197 static struct platform_driver meson_secure_pwrc_driver
= {
198 .probe
= meson_secure_pwrc_probe
,
200 .name
= "meson_secure_pwrc",
201 .of_match_table
= meson_secure_pwrc_match_table
,
204 builtin_platform_driver(meson_secure_pwrc_driver
);