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>
16 #include <linux/module.h>
21 struct meson_secure_pwrc_domain {
22 struct generic_pm_domain base;
24 struct meson_secure_pwrc *pwrc;
27 struct meson_secure_pwrc {
28 struct meson_secure_pwrc_domain *domains;
29 struct genpd_onecell_data xlate;
30 struct meson_sm_firmware *fw;
33 struct meson_secure_pwrc_domain_desc {
37 bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
40 struct meson_secure_pwrc_domain_data {
42 struct meson_secure_pwrc_domain_desc *domains;
45 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
49 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
50 pwrc_domain->index, 0, 0, 0, 0) < 0)
51 pr_err("failed to get power domain status\n");
56 static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
59 struct meson_secure_pwrc_domain *pwrc_domain =
60 container_of(domain, struct meson_secure_pwrc_domain, base);
62 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
63 pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
64 pr_err("failed to set power domain off\n");
71 static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
74 struct meson_secure_pwrc_domain *pwrc_domain =
75 container_of(domain, struct meson_secure_pwrc_domain, base);
77 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
78 pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
79 pr_err("failed to set power domain on\n");
86 #define SEC_PD(__name, __flag) \
87 [PWRC_##__name##_ID] = \
90 .index = PWRC_##__name##_ID, \
91 .is_off = pwrc_secure_is_off, \
95 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
98 /* UART should keep working in ATF after suspend and before resume */
99 SEC_PD(UART, GENPD_FLAG_ALWAYS_ON),
100 /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
101 SEC_PD(DMC, GENPD_FLAG_ALWAYS_ON),
110 /* SRAMB is used as ATF runtime memory, and should be always on */
111 SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
116 /* NIC is for the Arm NIC-400 interconnect, and should be always on */
117 SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
122 static int meson_secure_pwrc_probe(struct platform_device *pdev)
125 struct device_node *sm_np;
126 struct meson_secure_pwrc *pwrc;
127 const struct meson_secure_pwrc_domain_data *match;
129 match = of_device_get_match_data(&pdev->dev);
131 dev_err(&pdev->dev, "failed to get match data\n");
135 sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
137 dev_err(&pdev->dev, "no secure-monitor node\n");
141 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
145 pwrc->fw = meson_sm_get(sm_np);
148 return -EPROBE_DEFER;
150 pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
151 sizeof(*pwrc->xlate.domains),
153 if (!pwrc->xlate.domains)
156 pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
157 sizeof(*pwrc->domains), GFP_KERNEL);
161 pwrc->xlate.num_domains = match->count;
162 platform_set_drvdata(pdev, pwrc);
164 for (i = 0 ; i < match->count ; ++i) {
165 struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
167 if (!match->domains[i].index)
171 dom->index = match->domains[i].index;
172 dom->base.name = match->domains[i].name;
173 dom->base.flags = match->domains[i].flags;
174 dom->base.power_on = meson_secure_pwrc_on;
175 dom->base.power_off = meson_secure_pwrc_off;
177 pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
179 pwrc->xlate.domains[i] = &dom->base;
182 return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
185 static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
186 .domains = a1_pwrc_domains,
187 .count = ARRAY_SIZE(a1_pwrc_domains),
190 static const struct of_device_id meson_secure_pwrc_match_table[] = {
192 .compatible = "amlogic,meson-a1-pwrc",
193 .data = &meson_secure_a1_pwrc_data,
197 MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
199 static struct platform_driver meson_secure_pwrc_driver = {
200 .probe = meson_secure_pwrc_probe,
202 .name = "meson_secure_pwrc",
203 .of_match_table = meson_secure_pwrc_match_table,
206 module_platform_driver(meson_secure_pwrc_driver);
207 MODULE_LICENSE("Dual MIT/GPL");