1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
11 #include <power-domain-uclass.h>
16 #include <dt-bindings/power/meson-g12a-power.h>
17 #include <dt-bindings/power/meson-sm1-power.h>
18 #include <linux/err.h>
22 #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
23 #define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
27 #define HHI_MEM_PD_REG0 (0x40 << 2)
28 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
29 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
30 #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
31 #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
32 #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
33 #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
34 #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
35 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
38 struct meson_ee_pwrc_domain;
40 struct meson_ee_pwrc_mem_domain {
45 struct meson_ee_pwrc_top_domain {
46 unsigned int sleep_reg;
47 unsigned int sleep_mask;
49 unsigned int iso_mask;
52 struct meson_ee_pwrc_domain_desc {
54 unsigned int reset_names_count;
55 unsigned int clk_names_count;
56 struct meson_ee_pwrc_top_domain *top_pd;
57 unsigned int mem_pd_count;
58 struct meson_ee_pwrc_mem_domain *mem_pd;
59 bool (*get_power)(struct power_domain *power_domain);
62 struct meson_ee_pwrc_domain_data {
64 struct meson_ee_pwrc_domain_desc *domains;
67 /* TOP Power Domains */
69 static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu = {
70 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
72 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
76 #define SM1_EE_PD(__bit) \
78 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
79 .sleep_mask = BIT(__bit), \
80 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
81 .iso_mask = BIT(__bit), \
84 static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
85 static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
86 static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
87 static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
88 static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
90 /* Memory PD Domains */
92 #define VPU_MEMPD(__reg) \
93 { __reg, GENMASK(1, 0) }, \
94 { __reg, GENMASK(3, 2) }, \
95 { __reg, GENMASK(5, 4) }, \
96 { __reg, GENMASK(7, 6) }, \
97 { __reg, GENMASK(9, 8) }, \
98 { __reg, GENMASK(11, 10) }, \
99 { __reg, GENMASK(13, 12) }, \
100 { __reg, GENMASK(15, 14) }, \
101 { __reg, GENMASK(17, 16) }, \
102 { __reg, GENMASK(19, 18) }, \
103 { __reg, GENMASK(21, 20) }, \
104 { __reg, GENMASK(23, 22) }, \
105 { __reg, GENMASK(25, 24) }, \
106 { __reg, GENMASK(27, 26) }, \
107 { __reg, GENMASK(29, 28) }, \
108 { __reg, GENMASK(31, 30) }
110 #define VPU_HHI_MEMPD(__reg) \
113 { __reg, BIT(10) }, \
114 { __reg, BIT(11) }, \
115 { __reg, BIT(12) }, \
116 { __reg, BIT(13) }, \
117 { __reg, BIT(14) }, \
120 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
121 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
122 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
123 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
124 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
127 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
128 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
131 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
132 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
133 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
134 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
135 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
136 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
137 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
138 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
139 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
140 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
143 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
144 { HHI_NANOQ_MEM_PD_REG0, 0xff },
145 { HHI_NANOQ_MEM_PD_REG1, 0xff },
148 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
149 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
152 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
153 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
156 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
157 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
160 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
161 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
162 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
163 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
164 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
165 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
166 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
167 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
168 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
169 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
170 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
171 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
172 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
173 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
176 #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
179 .reset_names_count = __resets, \
180 .clk_names_count = __clks, \
181 .top_pd = __top_pd, \
182 .mem_pd_count = ARRAY_SIZE(__mem), \
184 .get_power = __get_power, \
187 #define TOP_PD(__name, __top_pd, __mem, __get_power) \
190 .top_pd = __top_pd, \
191 .mem_pd_count = ARRAY_SIZE(__mem), \
193 .get_power = __get_power, \
196 #define MEM_PD(__name, __mem) \
197 TOP_PD(__name, NULL, __mem, NULL)
199 static bool pwrc_ee_get_power(struct power_domain *power_domain);
201 static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
202 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &g12a_pwrc_vpu, g12a_pwrc_mem_vpu,
203 pwrc_ee_get_power, 11, 2),
204 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
207 static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
208 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
209 pwrc_ee_get_power, 11, 2),
210 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
212 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
214 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
216 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
218 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
219 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
222 struct meson_ee_pwrc_priv {
223 struct regmap *regmap_ao;
224 struct regmap *regmap_hhi;
225 struct reset_ctl_bulk resets;
226 struct clk_bulk clks;
227 const struct meson_ee_pwrc_domain_data *data;
230 static bool pwrc_ee_get_power(struct power_domain *power_domain)
232 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
233 struct meson_ee_pwrc_domain_desc *pwrc_domain;
236 pwrc_domain = &priv->data->domains[power_domain->id];
238 regmap_read(priv->regmap_ao,
239 pwrc_domain->top_pd->sleep_reg, ®);
241 return (reg & pwrc_domain->top_pd->sleep_mask);
244 static int meson_ee_pwrc_request(struct power_domain *power_domain)
249 static int meson_ee_pwrc_free(struct power_domain *power_domain)
254 static int meson_ee_pwrc_off(struct power_domain *power_domain)
256 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
257 struct meson_ee_pwrc_domain_desc *pwrc_domain;
260 pwrc_domain = &priv->data->domains[power_domain->id];
262 if (pwrc_domain->top_pd)
263 regmap_update_bits(priv->regmap_ao,
264 pwrc_domain->top_pd->sleep_reg,
265 pwrc_domain->top_pd->sleep_mask,
266 pwrc_domain->top_pd->sleep_mask);
269 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
270 regmap_update_bits(priv->regmap_hhi,
271 pwrc_domain->mem_pd[i].reg,
272 pwrc_domain->mem_pd[i].mask,
273 pwrc_domain->mem_pd[i].mask);
277 if (pwrc_domain->top_pd)
278 regmap_update_bits(priv->regmap_ao,
279 pwrc_domain->top_pd->iso_reg,
280 pwrc_domain->top_pd->iso_mask,
281 pwrc_domain->top_pd->iso_mask);
283 if (pwrc_domain->clk_names_count) {
285 clk_disable_bulk(&priv->clks);
291 static int meson_ee_pwrc_on(struct power_domain *power_domain)
293 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
294 struct meson_ee_pwrc_domain_desc *pwrc_domain;
297 pwrc_domain = &priv->data->domains[power_domain->id];
299 if (pwrc_domain->top_pd)
300 regmap_update_bits(priv->regmap_ao,
301 pwrc_domain->top_pd->sleep_reg,
302 pwrc_domain->top_pd->sleep_mask, 0);
305 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
306 regmap_update_bits(priv->regmap_hhi,
307 pwrc_domain->mem_pd[i].reg,
308 pwrc_domain->mem_pd[i].mask, 0);
312 if (pwrc_domain->reset_names_count) {
313 ret = reset_assert_bulk(&priv->resets);
318 if (pwrc_domain->top_pd)
319 regmap_update_bits(priv->regmap_ao,
320 pwrc_domain->top_pd->iso_reg,
321 pwrc_domain->top_pd->iso_mask, 0);
323 if (pwrc_domain->reset_names_count) {
324 ret = reset_deassert_bulk(&priv->resets);
329 if (pwrc_domain->clk_names_count)
330 return clk_enable_bulk(&priv->clks);
335 static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
336 struct ofnode_phandle_args *args)
338 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
340 /* #power-domain-cells is 1 */
342 if (args->args_count < 1) {
343 debug("Invalid args_count: %d\n", args->args_count);
347 power_domain->id = args->args[0];
349 if (power_domain->id >= priv->data->count) {
350 debug("Invalid domain ID: %lu\n", power_domain->id);
357 struct power_domain_ops meson_ee_pwrc_ops = {
358 .rfree = meson_ee_pwrc_free,
359 .off = meson_ee_pwrc_off,
360 .on = meson_ee_pwrc_on,
361 .request = meson_ee_pwrc_request,
362 .of_xlate = meson_ee_pwrc_of_xlate,
365 static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
366 .count = ARRAY_SIZE(g12a_pwrc_domains),
367 .domains = g12a_pwrc_domains,
370 static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
371 .count = ARRAY_SIZE(sm1_pwrc_domains),
372 .domains = sm1_pwrc_domains,
375 static const struct udevice_id meson_ee_pwrc_ids[] = {
377 .compatible = "amlogic,meson-g12a-pwrc",
378 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
381 .compatible = "amlogic,meson-sm1-pwrc",
382 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
387 static int meson_ee_pwrc_probe(struct udevice *dev)
389 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
394 priv->data = (void *)dev_get_driver_data(dev);
398 priv->regmap_hhi = syscon_node_to_regmap(dev_get_parent(dev)->node);
399 if (IS_ERR(priv->regmap_hhi))
400 return PTR_ERR(priv->regmap_hhi);
402 ret = ofnode_read_u32(dev->node, "amlogic,ao-sysctrl",
407 ao_node = ofnode_get_by_phandle(ao_phandle);
408 if (!ofnode_valid(ao_node))
411 priv->regmap_ao = syscon_node_to_regmap(ao_node);
412 if (IS_ERR(priv->regmap_ao))
413 return PTR_ERR(priv->regmap_ao);
415 ret = reset_get_bulk(dev, &priv->resets);
419 ret = clk_get_bulk(dev, &priv->clks);
426 U_BOOT_DRIVER(meson_ee_pwrc) = {
427 .name = "meson_ee_pwrc",
428 .id = UCLASS_POWER_DOMAIN,
429 .of_match = meson_ee_pwrc_ids,
430 .probe = meson_ee_pwrc_probe,
431 .ops = &meson_ee_pwrc_ops,
432 .priv_auto_alloc_size = sizeof(struct meson_ee_pwrc_priv),