dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / soc / amlogic / meson-secure-pwrc.c
blob5fb29a4758796dbf6c25bcd991fd05169c74d6ff
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3 * Copyright (c) 2019 Amlogic, Inc.
4 * Author: Jianxin Pan <jianxin.pan@amlogic.com>
5 */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/io.h>
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>
17 #define PWRC_ON 1
18 #define PWRC_OFF 0
20 struct meson_secure_pwrc_domain {
21 struct generic_pm_domain base;
22 unsigned int index;
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 {
33 unsigned int index;
34 unsigned int flags;
35 char *name;
36 bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
39 struct meson_secure_pwrc_domain_data {
40 unsigned int count;
41 struct meson_secure_pwrc_domain_desc *domains;
44 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
46 int is_off = 1;
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");
52 return is_off;
55 static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
57 int ret = 0;
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");
64 ret = -EINVAL;
67 return ret;
70 static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
72 int ret = 0;
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");
79 ret = -EINVAL;
82 return ret;
85 #define SEC_PD(__name, __flag) \
86 [PWRC_##__name##_ID] = \
87 { \
88 .name = #__name, \
89 .index = PWRC_##__name##_ID, \
90 .is_off = pwrc_secure_is_off, \
91 .flags = __flag, \
94 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
95 SEC_PD(DSPA, 0),
96 SEC_PD(DSPB, 0),
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),
101 SEC_PD(I2C, 0),
102 SEC_PD(PSRAM, 0),
103 SEC_PD(ACODEC, 0),
104 SEC_PD(AUDIO, 0),
105 SEC_PD(OTP, 0),
106 SEC_PD(DMA, 0),
107 SEC_PD(SD_EMMC, 0),
108 SEC_PD(RAMA, 0),
109 /* SRAMB is used as ATF runtime memory, and should be always on */
110 SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
111 SEC_PD(IR, 0),
112 SEC_PD(SPICC, 0),
113 SEC_PD(SPIFC, 0),
114 SEC_PD(USB, 0),
115 /* NIC is for the Arm NIC-400 interconnect, and should be always on */
116 SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
117 SEC_PD(PDMIN, 0),
118 SEC_PD(RSA, 0),
121 static int meson_secure_pwrc_probe(struct platform_device *pdev)
123 int i;
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);
129 if (!match) {
130 dev_err(&pdev->dev, "failed to get match data\n");
131 return -ENODEV;
134 sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
135 if (!sm_np) {
136 dev_err(&pdev->dev, "no secure-monitor node\n");
137 return -ENODEV;
140 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
141 if (!pwrc)
142 return -ENOMEM;
144 pwrc->fw = meson_sm_get(sm_np);
145 of_node_put(sm_np);
146 if (!pwrc->fw)
147 return -EPROBE_DEFER;
149 pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
150 sizeof(*pwrc->xlate.domains),
151 GFP_KERNEL);
152 if (!pwrc->xlate.domains)
153 return -ENOMEM;
155 pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
156 sizeof(*pwrc->domains), GFP_KERNEL);
157 if (!pwrc->domains)
158 return -ENOMEM;
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)
167 continue;
169 dom->pwrc = pwrc;
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,
194 { /* sentinel */ }
197 static struct platform_driver meson_secure_pwrc_driver = {
198 .probe = meson_secure_pwrc_probe,
199 .driver = {
200 .name = "meson_secure_pwrc",
201 .of_match_table = meson_secure_pwrc_match_table,
204 builtin_platform_driver(meson_secure_pwrc_driver);