1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
10 #include <power-domain-uclass.h>
15 #include <dt-bindings/power/meson-g12a-power.h>
16 #include <dt-bindings/power/meson-sm1-power.h>
17 #include <linux/err.h>
21 #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
22 #define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
26 #define HHI_MEM_PD_REG0 (0x40 << 2)
27 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
28 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
29 #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
30 #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
31 #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
32 #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
33 #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
34 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
37 struct meson_ee_pwrc_domain;
39 struct meson_ee_pwrc_mem_domain {
44 struct meson_ee_pwrc_top_domain {
45 unsigned int sleep_reg;
46 unsigned int sleep_mask;
48 unsigned int iso_mask;
51 struct meson_ee_pwrc_domain_desc {
53 unsigned int reset_names_count;
54 unsigned int clk_names_count;
55 struct meson_ee_pwrc_top_domain *top_pd;
56 unsigned int mem_pd_count;
57 struct meson_ee_pwrc_mem_domain *mem_pd;
58 bool (*get_power)(struct power_domain *power_domain);
61 struct meson_ee_pwrc_domain_data {
63 struct meson_ee_pwrc_domain_desc *domains;
66 /* TOP Power Domains */
68 static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu = {
69 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
71 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
75 #define SM1_EE_PD(__bit) \
77 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
78 .sleep_mask = BIT(__bit), \
79 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
80 .iso_mask = BIT(__bit), \
83 static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
84 static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
85 static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
86 static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
87 static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
89 /* Memory PD Domains */
91 #define VPU_MEMPD(__reg) \
92 { __reg, GENMASK(1, 0) }, \
93 { __reg, GENMASK(3, 2) }, \
94 { __reg, GENMASK(5, 4) }, \
95 { __reg, GENMASK(7, 6) }, \
96 { __reg, GENMASK(9, 8) }, \
97 { __reg, GENMASK(11, 10) }, \
98 { __reg, GENMASK(13, 12) }, \
99 { __reg, GENMASK(15, 14) }, \
100 { __reg, GENMASK(17, 16) }, \
101 { __reg, GENMASK(19, 18) }, \
102 { __reg, GENMASK(21, 20) }, \
103 { __reg, GENMASK(23, 22) }, \
104 { __reg, GENMASK(25, 24) }, \
105 { __reg, GENMASK(27, 26) }, \
106 { __reg, GENMASK(29, 28) }, \
107 { __reg, GENMASK(31, 30) }
109 #define VPU_HHI_MEMPD(__reg) \
112 { __reg, BIT(10) }, \
113 { __reg, BIT(11) }, \
114 { __reg, BIT(12) }, \
115 { __reg, BIT(13) }, \
116 { __reg, BIT(14) }, \
119 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
120 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
121 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
122 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
123 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
126 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
127 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
130 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
131 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
132 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
133 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
134 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
135 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
136 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
137 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
138 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
139 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
142 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
143 { HHI_NANOQ_MEM_PD_REG0, 0xff },
144 { HHI_NANOQ_MEM_PD_REG1, 0xff },
147 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
148 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
151 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
152 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
155 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
156 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
159 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
160 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
161 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
162 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
163 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
164 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
165 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
166 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
167 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
168 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
169 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
170 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
171 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
172 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
175 #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
178 .reset_names_count = __resets, \
179 .clk_names_count = __clks, \
180 .top_pd = __top_pd, \
181 .mem_pd_count = ARRAY_SIZE(__mem), \
183 .get_power = __get_power, \
186 #define TOP_PD(__name, __top_pd, __mem, __get_power) \
189 .top_pd = __top_pd, \
190 .mem_pd_count = ARRAY_SIZE(__mem), \
192 .get_power = __get_power, \
195 #define MEM_PD(__name, __mem) \
196 TOP_PD(__name, NULL, __mem, NULL)
198 static bool pwrc_ee_get_power(struct power_domain *power_domain);
200 static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
201 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &g12a_pwrc_vpu, g12a_pwrc_mem_vpu,
202 pwrc_ee_get_power, 11, 2),
203 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
206 static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
207 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
208 pwrc_ee_get_power, 11, 2),
209 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
211 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
213 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
215 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
217 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
218 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
221 struct meson_ee_pwrc_priv {
222 struct regmap *regmap_ao;
223 struct regmap *regmap_hhi;
224 struct reset_ctl_bulk resets;
225 struct clk_bulk clks;
226 const struct meson_ee_pwrc_domain_data *data;
229 static bool pwrc_ee_get_power(struct power_domain *power_domain)
231 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
232 struct meson_ee_pwrc_domain_desc *pwrc_domain;
235 pwrc_domain = &priv->data->domains[power_domain->id];
237 regmap_read(priv->regmap_ao,
238 pwrc_domain->top_pd->sleep_reg, ®);
240 return (reg & pwrc_domain->top_pd->sleep_mask);
243 static int meson_ee_pwrc_request(struct power_domain *power_domain)
248 static int meson_ee_pwrc_free(struct power_domain *power_domain)
253 static int meson_ee_pwrc_off(struct power_domain *power_domain)
255 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
256 struct meson_ee_pwrc_domain_desc *pwrc_domain;
259 pwrc_domain = &priv->data->domains[power_domain->id];
261 if (pwrc_domain->top_pd)
262 regmap_update_bits(priv->regmap_ao,
263 pwrc_domain->top_pd->sleep_reg,
264 pwrc_domain->top_pd->sleep_mask,
265 pwrc_domain->top_pd->sleep_mask);
268 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
269 regmap_update_bits(priv->regmap_hhi,
270 pwrc_domain->mem_pd[i].reg,
271 pwrc_domain->mem_pd[i].mask,
272 pwrc_domain->mem_pd[i].mask);
276 if (pwrc_domain->top_pd)
277 regmap_update_bits(priv->regmap_ao,
278 pwrc_domain->top_pd->iso_reg,
279 pwrc_domain->top_pd->iso_mask,
280 pwrc_domain->top_pd->iso_mask);
282 if (pwrc_domain->clk_names_count) {
284 clk_disable_bulk(&priv->clks);
290 static int meson_ee_pwrc_on(struct power_domain *power_domain)
292 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
293 struct meson_ee_pwrc_domain_desc *pwrc_domain;
296 pwrc_domain = &priv->data->domains[power_domain->id];
298 if (pwrc_domain->top_pd)
299 regmap_update_bits(priv->regmap_ao,
300 pwrc_domain->top_pd->sleep_reg,
301 pwrc_domain->top_pd->sleep_mask, 0);
304 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
305 regmap_update_bits(priv->regmap_hhi,
306 pwrc_domain->mem_pd[i].reg,
307 pwrc_domain->mem_pd[i].mask, 0);
311 if (pwrc_domain->reset_names_count) {
312 ret = reset_assert_bulk(&priv->resets);
317 if (pwrc_domain->top_pd)
318 regmap_update_bits(priv->regmap_ao,
319 pwrc_domain->top_pd->iso_reg,
320 pwrc_domain->top_pd->iso_mask, 0);
322 if (pwrc_domain->reset_names_count) {
323 ret = reset_deassert_bulk(&priv->resets);
328 if (pwrc_domain->clk_names_count)
329 return clk_enable_bulk(&priv->clks);
334 static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
335 struct ofnode_phandle_args *args)
337 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
339 /* #power-domain-cells is 1 */
341 if (args->args_count < 1) {
342 debug("Invalid args_count: %d\n", args->args_count);
346 power_domain->id = args->args[0];
348 if (power_domain->id >= priv->data->count) {
349 debug("Invalid domain ID: %lu\n", power_domain->id);
356 struct power_domain_ops meson_ee_pwrc_ops = {
357 .rfree = meson_ee_pwrc_free,
358 .off = meson_ee_pwrc_off,
359 .on = meson_ee_pwrc_on,
360 .request = meson_ee_pwrc_request,
361 .of_xlate = meson_ee_pwrc_of_xlate,
364 static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
365 .count = ARRAY_SIZE(g12a_pwrc_domains),
366 .domains = g12a_pwrc_domains,
369 static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
370 .count = ARRAY_SIZE(sm1_pwrc_domains),
371 .domains = sm1_pwrc_domains,
374 static const struct udevice_id meson_ee_pwrc_ids[] = {
376 .compatible = "amlogic,meson-g12a-pwrc",
377 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
380 .compatible = "amlogic,meson-sm1-pwrc",
381 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
386 static int meson_ee_pwrc_probe(struct udevice *dev)
388 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
393 priv->data = (void *)dev_get_driver_data(dev);
397 priv->regmap_hhi = syscon_node_to_regmap(dev_get_parent(dev)->node);
398 if (IS_ERR(priv->regmap_hhi))
399 return PTR_ERR(priv->regmap_hhi);
401 ret = ofnode_read_u32(dev->node, "amlogic,ao-sysctrl",
406 ao_node = ofnode_get_by_phandle(ao_phandle);
407 if (!ofnode_valid(ao_node))
410 priv->regmap_ao = syscon_node_to_regmap(ao_node);
411 if (IS_ERR(priv->regmap_ao))
412 return PTR_ERR(priv->regmap_ao);
414 ret = reset_get_bulk(dev, &priv->resets);
418 ret = clk_get_bulk(dev, &priv->clks);
425 U_BOOT_DRIVER(meson_ee_pwrc) = {
426 .name = "meson_ee_pwrc",
427 .id = UCLASS_POWER_DOMAIN,
428 .of_match = meson_ee_pwrc_ids,
429 .probe = meson_ee_pwrc_probe,
430 .ops = &meson_ee_pwrc_ops,
431 .priv_auto_alloc_size = sizeof(struct meson_ee_pwrc_priv),