• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
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 <dt-bindings/power/meson-s4-power.h>
15 #include <linux/arm-smccc.h>
16 #include <linux/firmware/meson/meson_sm.h>
17 #include <linux/module.h>
18 
19 #define PWRC_ON		1
20 #define PWRC_OFF	0
21 
22 struct meson_secure_pwrc_domain {
23 	struct generic_pm_domain base;
24 	unsigned int index;
25 	struct meson_secure_pwrc *pwrc;
26 };
27 
28 struct meson_secure_pwrc {
29 	struct meson_secure_pwrc_domain *domains;
30 	struct genpd_onecell_data xlate;
31 	struct meson_sm_firmware *fw;
32 };
33 
34 struct meson_secure_pwrc_domain_desc {
35 	unsigned int index;
36 	unsigned int flags;
37 	char *name;
38 	bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
39 };
40 
41 struct meson_secure_pwrc_domain_data {
42 	unsigned int count;
43 	struct meson_secure_pwrc_domain_desc *domains;
44 };
45 
pwrc_secure_is_off(struct meson_secure_pwrc_domain * pwrc_domain)46 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
47 {
48 	int is_off = 1;
49 
50 	if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
51 			  pwrc_domain->index, 0, 0, 0, 0) < 0)
52 		pr_err("failed to get power domain status\n");
53 
54 	return is_off;
55 }
56 
meson_secure_pwrc_off(struct generic_pm_domain * domain)57 static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
58 {
59 	int ret = 0;
60 	struct meson_secure_pwrc_domain *pwrc_domain =
61 		container_of(domain, struct meson_secure_pwrc_domain, base);
62 
63 	if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
64 			  pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
65 		pr_err("failed to set power domain off\n");
66 		ret = -EINVAL;
67 	}
68 
69 	return ret;
70 }
71 
meson_secure_pwrc_on(struct generic_pm_domain * domain)72 static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
73 {
74 	int ret = 0;
75 	struct meson_secure_pwrc_domain *pwrc_domain =
76 		container_of(domain, struct meson_secure_pwrc_domain, base);
77 
78 	if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
79 			  pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
80 		pr_err("failed to set power domain on\n");
81 		ret = -EINVAL;
82 	}
83 
84 	return ret;
85 }
86 
87 #define SEC_PD(__name, __flag)			\
88 [PWRC_##__name##_ID] =				\
89 {						\
90 	.name = #__name,			\
91 	.index = PWRC_##__name##_ID,		\
92 	.is_off = pwrc_secure_is_off,	\
93 	.flags = __flag,			\
94 }
95 
96 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
97 	SEC_PD(DSPA,	0),
98 	SEC_PD(DSPB,	0),
99 	/* UART should keep working in ATF after suspend and before resume */
100 	SEC_PD(UART,	GENPD_FLAG_ALWAYS_ON),
101 	/* DMC is for DDR PHY ana/dig and DMC, and should be always on */
102 	SEC_PD(DMC,	GENPD_FLAG_ALWAYS_ON),
103 	SEC_PD(I2C,	0),
104 	SEC_PD(PSRAM,	0),
105 	SEC_PD(ACODEC,	0),
106 	SEC_PD(AUDIO,	0),
107 	SEC_PD(OTP,	0),
108 	SEC_PD(DMA,	GENPD_FLAG_ALWAYS_ON | GENPD_FLAG_IRQ_SAFE),
109 	SEC_PD(SD_EMMC,	0),
110 	SEC_PD(RAMA,	0),
111 	/* SRAMB is used as ATF runtime memory, and should be always on */
112 	SEC_PD(RAMB,	GENPD_FLAG_ALWAYS_ON),
113 	SEC_PD(IR,	0),
114 	SEC_PD(SPICC,	0),
115 	SEC_PD(SPIFC,	0),
116 	SEC_PD(USB,	0),
117 	/* NIC is for the Arm NIC-400 interconnect, and should be always on */
118 	SEC_PD(NIC,	GENPD_FLAG_ALWAYS_ON),
119 	SEC_PD(PDMIN,	0),
120 	SEC_PD(RSA,	0),
121 };
122 
123 static struct meson_secure_pwrc_domain_desc s4_pwrc_domains[] = {
124 	SEC_PD(S4_DOS_HEVC,	0),
125 	SEC_PD(S4_DOS_VDEC,	0),
126 	SEC_PD(S4_VPU_HDMI,	0),
127 	SEC_PD(S4_USB_COMB,	0),
128 	SEC_PD(S4_GE2D,		0),
129 	/* ETH is for ethernet online wakeup, and should be always on */
130 	SEC_PD(S4_ETH,		GENPD_FLAG_ALWAYS_ON),
131 	SEC_PD(S4_DEMOD,	0),
132 	SEC_PD(S4_AUDIO,	0),
133 };
134 
meson_secure_pwrc_probe(struct platform_device * pdev)135 static int meson_secure_pwrc_probe(struct platform_device *pdev)
136 {
137 	int i;
138 	struct device_node *sm_np;
139 	struct meson_secure_pwrc *pwrc;
140 	const struct meson_secure_pwrc_domain_data *match;
141 
142 	match = of_device_get_match_data(&pdev->dev);
143 	if (!match) {
144 		dev_err(&pdev->dev, "failed to get match data\n");
145 		return -ENODEV;
146 	}
147 
148 	sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
149 	if (!sm_np) {
150 		dev_err(&pdev->dev, "no secure-monitor node\n");
151 		return -ENODEV;
152 	}
153 
154 	pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
155 	if (!pwrc) {
156 		of_node_put(sm_np);
157 		return -ENOMEM;
158 	}
159 
160 	pwrc->fw = meson_sm_get(sm_np);
161 	of_node_put(sm_np);
162 	if (!pwrc->fw)
163 		return -EPROBE_DEFER;
164 
165 	pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
166 					   sizeof(*pwrc->xlate.domains),
167 					   GFP_KERNEL);
168 	if (!pwrc->xlate.domains)
169 		return -ENOMEM;
170 
171 	pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
172 				     sizeof(*pwrc->domains), GFP_KERNEL);
173 	if (!pwrc->domains)
174 		return -ENOMEM;
175 
176 	pwrc->xlate.num_domains = match->count;
177 	platform_set_drvdata(pdev, pwrc);
178 
179 	for (i = 0 ; i < match->count ; ++i) {
180 		struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
181 
182 		if (!match->domains[i].index)
183 			continue;
184 
185 		dom->pwrc = pwrc;
186 		dom->index = match->domains[i].index;
187 		dom->base.name = match->domains[i].name;
188 		dom->base.flags = match->domains[i].flags;
189 		dom->base.power_on = meson_secure_pwrc_on;
190 		dom->base.power_off = meson_secure_pwrc_off;
191 
192 		pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
193 
194 		pwrc->xlate.domains[i] = &dom->base;
195 	}
196 
197 	return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
198 }
199 
200 static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
201 	.domains = a1_pwrc_domains,
202 	.count = ARRAY_SIZE(a1_pwrc_domains),
203 };
204 
205 static struct meson_secure_pwrc_domain_data meson_secure_s4_pwrc_data = {
206 	.domains = s4_pwrc_domains,
207 	.count = ARRAY_SIZE(s4_pwrc_domains),
208 };
209 
210 static const struct of_device_id meson_secure_pwrc_match_table[] = {
211 	{
212 		.compatible = "amlogic,meson-a1-pwrc",
213 		.data = &meson_secure_a1_pwrc_data,
214 	},
215 	{
216 		.compatible = "amlogic,meson-s4-pwrc",
217 		.data = &meson_secure_s4_pwrc_data,
218 	},
219 	{ /* sentinel */ }
220 };
221 MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
222 
223 static struct platform_driver meson_secure_pwrc_driver = {
224 	.probe = meson_secure_pwrc_probe,
225 	.driver = {
226 		.name		= "meson_secure_pwrc",
227 		.of_match_table	= meson_secure_pwrc_match_table,
228 	},
229 };
230 module_platform_driver(meson_secure_pwrc_driver);
231 MODULE_LICENSE("Dual MIT/GPL");
232