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/delay.h>
19 #include <linux/err.h>
23 #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
24 #define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
28 #define HHI_MEM_PD_REG0 (0x40 << 2)
29 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
30 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
31 #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
32 #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
33 #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
34 #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
35 #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
36 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
39 struct meson_ee_pwrc_domain;
41 struct meson_ee_pwrc_mem_domain {
46 struct meson_ee_pwrc_top_domain {
47 unsigned int sleep_reg;
48 unsigned int sleep_mask;
50 unsigned int iso_mask;
53 struct meson_ee_pwrc_domain_desc {
55 unsigned int reset_names_count;
56 unsigned int clk_names_count;
57 struct meson_ee_pwrc_top_domain *top_pd;
58 unsigned int mem_pd_count;
59 struct meson_ee_pwrc_mem_domain *mem_pd;
60 bool (*get_power)(struct power_domain *power_domain);
63 struct meson_ee_pwrc_domain_data {
65 struct meson_ee_pwrc_domain_desc *domains;
68 /* TOP Power Domains */
70 static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu = {
71 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
73 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
77 #define SM1_EE_PD(__bit) \
79 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
80 .sleep_mask = BIT(__bit), \
81 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
82 .iso_mask = BIT(__bit), \
85 static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
86 static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
87 static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
88 static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
89 static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
91 /* Memory PD Domains */
93 #define VPU_MEMPD(__reg) \
94 { __reg, GENMASK(1, 0) }, \
95 { __reg, GENMASK(3, 2) }, \
96 { __reg, GENMASK(5, 4) }, \
97 { __reg, GENMASK(7, 6) }, \
98 { __reg, GENMASK(9, 8) }, \
99 { __reg, GENMASK(11, 10) }, \
100 { __reg, GENMASK(13, 12) }, \
101 { __reg, GENMASK(15, 14) }, \
102 { __reg, GENMASK(17, 16) }, \
103 { __reg, GENMASK(19, 18) }, \
104 { __reg, GENMASK(21, 20) }, \
105 { __reg, GENMASK(23, 22) }, \
106 { __reg, GENMASK(25, 24) }, \
107 { __reg, GENMASK(27, 26) }, \
108 { __reg, GENMASK(29, 28) }, \
109 { __reg, GENMASK(31, 30) }
111 #define VPU_HHI_MEMPD(__reg) \
114 { __reg, BIT(10) }, \
115 { __reg, BIT(11) }, \
116 { __reg, BIT(12) }, \
117 { __reg, BIT(13) }, \
118 { __reg, BIT(14) }, \
121 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
122 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
123 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
124 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
125 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
128 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
129 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
132 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
133 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
134 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
135 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
136 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
137 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
138 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
139 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
140 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
141 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
144 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
145 { HHI_NANOQ_MEM_PD_REG0, 0xff },
146 { HHI_NANOQ_MEM_PD_REG1, 0xff },
149 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
150 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
153 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
154 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
157 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
158 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
161 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
162 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
163 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
164 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
165 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
166 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
167 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
168 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
169 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
170 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
171 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
172 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
173 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
174 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
177 #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
180 .reset_names_count = __resets, \
181 .clk_names_count = __clks, \
182 .top_pd = __top_pd, \
183 .mem_pd_count = ARRAY_SIZE(__mem), \
185 .get_power = __get_power, \
188 #define TOP_PD(__name, __top_pd, __mem, __get_power) \
191 .top_pd = __top_pd, \
192 .mem_pd_count = ARRAY_SIZE(__mem), \
194 .get_power = __get_power, \
197 #define MEM_PD(__name, __mem) \
198 TOP_PD(__name, NULL, __mem, NULL)
200 static bool pwrc_ee_get_power(struct power_domain *power_domain);
202 static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
203 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &g12a_pwrc_vpu, g12a_pwrc_mem_vpu,
204 pwrc_ee_get_power, 11, 2),
205 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
208 static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
209 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
210 pwrc_ee_get_power, 11, 2),
211 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
213 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
215 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
217 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
219 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
220 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
223 struct meson_ee_pwrc_priv {
224 struct regmap *regmap_ao;
225 struct regmap *regmap_hhi;
226 struct reset_ctl_bulk resets;
227 struct clk_bulk clks;
228 const struct meson_ee_pwrc_domain_data *data;
231 static bool pwrc_ee_get_power(struct power_domain *power_domain)
233 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
234 struct meson_ee_pwrc_domain_desc *pwrc_domain;
237 pwrc_domain = &priv->data->domains[power_domain->id];
239 regmap_read(priv->regmap_ao,
240 pwrc_domain->top_pd->sleep_reg, ®);
242 return (reg & pwrc_domain->top_pd->sleep_mask);
245 static int meson_ee_pwrc_request(struct power_domain *power_domain)
250 static int meson_ee_pwrc_free(struct power_domain *power_domain)
255 static int meson_ee_pwrc_off(struct power_domain *power_domain)
257 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
258 struct meson_ee_pwrc_domain_desc *pwrc_domain;
261 pwrc_domain = &priv->data->domains[power_domain->id];
263 if (pwrc_domain->top_pd)
264 regmap_update_bits(priv->regmap_ao,
265 pwrc_domain->top_pd->sleep_reg,
266 pwrc_domain->top_pd->sleep_mask,
267 pwrc_domain->top_pd->sleep_mask);
270 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
271 regmap_update_bits(priv->regmap_hhi,
272 pwrc_domain->mem_pd[i].reg,
273 pwrc_domain->mem_pd[i].mask,
274 pwrc_domain->mem_pd[i].mask);
278 if (pwrc_domain->top_pd)
279 regmap_update_bits(priv->regmap_ao,
280 pwrc_domain->top_pd->iso_reg,
281 pwrc_domain->top_pd->iso_mask,
282 pwrc_domain->top_pd->iso_mask);
284 if (pwrc_domain->clk_names_count) {
286 clk_disable_bulk(&priv->clks);
292 static int meson_ee_pwrc_on(struct power_domain *power_domain)
294 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
295 struct meson_ee_pwrc_domain_desc *pwrc_domain;
298 pwrc_domain = &priv->data->domains[power_domain->id];
300 if (pwrc_domain->top_pd)
301 regmap_update_bits(priv->regmap_ao,
302 pwrc_domain->top_pd->sleep_reg,
303 pwrc_domain->top_pd->sleep_mask, 0);
306 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
307 regmap_update_bits(priv->regmap_hhi,
308 pwrc_domain->mem_pd[i].reg,
309 pwrc_domain->mem_pd[i].mask, 0);
313 if (pwrc_domain->reset_names_count) {
314 ret = reset_assert_bulk(&priv->resets);
319 if (pwrc_domain->top_pd)
320 regmap_update_bits(priv->regmap_ao,
321 pwrc_domain->top_pd->iso_reg,
322 pwrc_domain->top_pd->iso_mask, 0);
324 if (pwrc_domain->reset_names_count) {
325 ret = reset_deassert_bulk(&priv->resets);
330 if (pwrc_domain->clk_names_count)
331 return clk_enable_bulk(&priv->clks);
336 static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
337 struct ofnode_phandle_args *args)
339 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
341 /* #power-domain-cells is 1 */
343 if (args->args_count < 1) {
344 debug("Invalid args_count: %d\n", args->args_count);
348 power_domain->id = args->args[0];
350 if (power_domain->id >= priv->data->count) {
351 debug("Invalid domain ID: %lu\n", power_domain->id);
358 struct power_domain_ops meson_ee_pwrc_ops = {
359 .rfree = meson_ee_pwrc_free,
360 .off = meson_ee_pwrc_off,
361 .on = meson_ee_pwrc_on,
362 .request = meson_ee_pwrc_request,
363 .of_xlate = meson_ee_pwrc_of_xlate,
366 static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
367 .count = ARRAY_SIZE(g12a_pwrc_domains),
368 .domains = g12a_pwrc_domains,
371 static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
372 .count = ARRAY_SIZE(sm1_pwrc_domains),
373 .domains = sm1_pwrc_domains,
376 static const struct udevice_id meson_ee_pwrc_ids[] = {
378 .compatible = "amlogic,meson-g12a-pwrc",
379 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
382 .compatible = "amlogic,meson-sm1-pwrc",
383 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
388 static int meson_ee_pwrc_probe(struct udevice *dev)
390 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
395 priv->data = (void *)dev_get_driver_data(dev);
399 priv->regmap_hhi = syscon_node_to_regmap(dev_get_parent(dev)->node);
400 if (IS_ERR(priv->regmap_hhi))
401 return PTR_ERR(priv->regmap_hhi);
403 ret = ofnode_read_u32(dev->node, "amlogic,ao-sysctrl",
408 ao_node = ofnode_get_by_phandle(ao_phandle);
409 if (!ofnode_valid(ao_node))
412 priv->regmap_ao = syscon_node_to_regmap(ao_node);
413 if (IS_ERR(priv->regmap_ao))
414 return PTR_ERR(priv->regmap_ao);
416 ret = reset_get_bulk(dev, &priv->resets);
420 ret = clk_get_bulk(dev, &priv->clks);
427 U_BOOT_DRIVER(meson_ee_pwrc) = {
428 .name = "meson_ee_pwrc",
429 .id = UCLASS_POWER_DOMAIN,
430 .of_match = meson_ee_pwrc_ids,
431 .probe = meson_ee_pwrc_probe,
432 .ops = &meson_ee_pwrc_ops,
433 .priv_auto_alloc_size = sizeof(struct meson_ee_pwrc_priv),