1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (c) 2019 Amlogic, Inc.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/platform_device.h>
12 #include <linux/pm_domain.h>
13 #include <dt-bindings/power/meson-a1-power.h>
14 #include <dt-bindings/power/amlogic,c3-pwrc.h>
15 #include <dt-bindings/power/meson-s4-power.h>
16 #include <linux/arm-smccc.h>
17 #include <linux/firmware/meson/meson_sm.h>
18 #include <linux/module.h>
23 struct meson_secure_pwrc_domain {
24 struct generic_pm_domain base;
26 struct meson_secure_pwrc *pwrc;
29 struct meson_secure_pwrc {
30 struct meson_secure_pwrc_domain *domains;
31 struct genpd_onecell_data xlate;
32 struct meson_sm_firmware *fw;
35 struct meson_secure_pwrc_domain_desc {
39 bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
42 struct meson_secure_pwrc_domain_data {
44 struct meson_secure_pwrc_domain_desc *domains;
47 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
51 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
52 pwrc_domain->index, 0, 0, 0, 0) < 0)
53 pr_err("failed to get power domain status\n");
58 static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
61 struct meson_secure_pwrc_domain *pwrc_domain =
62 container_of(domain, struct meson_secure_pwrc_domain, base);
64 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
65 pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
66 pr_err("failed to set power domain off\n");
73 static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
76 struct meson_secure_pwrc_domain *pwrc_domain =
77 container_of(domain, struct meson_secure_pwrc_domain, base);
79 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
80 pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
81 pr_err("failed to set power domain on\n");
88 #define SEC_PD(__name, __flag) \
89 [PWRC_##__name##_ID] = \
92 .index = PWRC_##__name##_ID, \
93 .is_off = pwrc_secure_is_off, \
97 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
100 /* UART should keep working in ATF after suspend and before resume */
101 SEC_PD(UART, GENPD_FLAG_ALWAYS_ON),
102 /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
103 SEC_PD(DMC, GENPD_FLAG_ALWAYS_ON),
109 SEC_PD(DMA, GENPD_FLAG_ALWAYS_ON | GENPD_FLAG_IRQ_SAFE),
112 /* SRAMB is used as ATF runtime memory, and should be always on */
113 SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
118 /* NIC is for the Arm NIC-400 interconnect, and should be always on */
119 SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
124 static struct meson_secure_pwrc_domain_desc c3_pwrc_domains[] = {
126 SEC_PD(C3_AUDIO, GENPD_FLAG_ALWAYS_ON),
127 SEC_PD(C3_SDIOA, GENPD_FLAG_ALWAYS_ON),
128 SEC_PD(C3_EMMC, GENPD_FLAG_ALWAYS_ON),
129 SEC_PD(C3_USB_COMB, GENPD_FLAG_ALWAYS_ON),
130 SEC_PD(C3_SDCARD, GENPD_FLAG_ALWAYS_ON),
131 SEC_PD(C3_ETH, GENPD_FLAG_ALWAYS_ON),
132 SEC_PD(C3_GE2D, GENPD_FLAG_ALWAYS_ON),
133 SEC_PD(C3_CVE, GENPD_FLAG_ALWAYS_ON),
134 SEC_PD(C3_GDC_WRAP, GENPD_FLAG_ALWAYS_ON),
135 SEC_PD(C3_ISP_TOP, GENPD_FLAG_ALWAYS_ON),
136 SEC_PD(C3_MIPI_ISP_WRAP, GENPD_FLAG_ALWAYS_ON),
137 SEC_PD(C3_VCODEC, 0),
140 static struct meson_secure_pwrc_domain_desc s4_pwrc_domains[] = {
141 SEC_PD(S4_DOS_HEVC, 0),
142 SEC_PD(S4_DOS_VDEC, 0),
143 SEC_PD(S4_VPU_HDMI, 0),
144 SEC_PD(S4_USB_COMB, 0),
146 /* ETH is for ethernet online wakeup, and should be always on */
147 SEC_PD(S4_ETH, GENPD_FLAG_ALWAYS_ON),
152 static int meson_secure_pwrc_probe(struct platform_device *pdev)
155 struct device_node *sm_np;
156 struct meson_secure_pwrc *pwrc;
157 const struct meson_secure_pwrc_domain_data *match;
159 match = of_device_get_match_data(&pdev->dev);
161 dev_err(&pdev->dev, "failed to get match data\n");
165 sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
167 dev_err(&pdev->dev, "no secure-monitor node\n");
171 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
177 pwrc->fw = meson_sm_get(sm_np);
180 return -EPROBE_DEFER;
182 pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
183 sizeof(*pwrc->xlate.domains),
185 if (!pwrc->xlate.domains)
188 pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
189 sizeof(*pwrc->domains), GFP_KERNEL);
193 pwrc->xlate.num_domains = match->count;
194 platform_set_drvdata(pdev, pwrc);
196 for (i = 0 ; i < match->count ; ++i) {
197 struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
199 if (!match->domains[i].name)
203 dom->index = match->domains[i].index;
204 dom->base.name = match->domains[i].name;
205 dom->base.flags = match->domains[i].flags;
206 dom->base.power_on = meson_secure_pwrc_on;
207 dom->base.power_off = meson_secure_pwrc_off;
209 pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
211 pwrc->xlate.domains[i] = &dom->base;
214 return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
217 static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
218 .domains = a1_pwrc_domains,
219 .count = ARRAY_SIZE(a1_pwrc_domains),
222 static struct meson_secure_pwrc_domain_data amlogic_secure_c3_pwrc_data = {
223 .domains = c3_pwrc_domains,
224 .count = ARRAY_SIZE(c3_pwrc_domains),
227 static struct meson_secure_pwrc_domain_data meson_secure_s4_pwrc_data = {
228 .domains = s4_pwrc_domains,
229 .count = ARRAY_SIZE(s4_pwrc_domains),
232 static const struct of_device_id meson_secure_pwrc_match_table[] = {
234 .compatible = "amlogic,meson-a1-pwrc",
235 .data = &meson_secure_a1_pwrc_data,
238 .compatible = "amlogic,c3-pwrc",
239 .data = &amlogic_secure_c3_pwrc_data,
242 .compatible = "amlogic,meson-s4-pwrc",
243 .data = &meson_secure_s4_pwrc_data,
247 MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
249 static struct platform_driver meson_secure_pwrc_driver = {
250 .probe = meson_secure_pwrc_probe,
252 .name = "meson_secure_pwrc",
253 .of_match_table = meson_secure_pwrc_match_table,
256 module_platform_driver(meson_secure_pwrc_driver);
257 MODULE_LICENSE("Dual MIT/GPL");