1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_domain.h>
10 #include <linux/bitfield.h>
11 #include <linux/regmap.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/reset.h>
16 #include <linux/clk.h>
17 #include <dt-bindings/power/meson8-power.h>
18 #include <dt-bindings/power/meson-g12a-power.h>
19 #include <dt-bindings/power/meson-gxbb-power.h>
20 #include <dt-bindings/power/meson-sm1-power.h>
24 #define GX_AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
25 #define GX_AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
28 * Meson8/Meson8b/Meson8m2 only expose the power management registers of the
29 * AO-bus as syscon. 0x3a from GX translates to 0x02, 0x3b translates to 0x03
32 #define MESON8_AO_RTI_GEN_PWR_SLEEP0 (0x02 << 2)
33 #define MESON8_AO_RTI_GEN_PWR_ISO0 (0x03 << 2)
37 #define HHI_MEM_PD_REG0 (0x40 << 2)
38 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
39 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
40 #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
41 #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
42 #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
43 #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
44 #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
45 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
48 struct meson_ee_pwrc_domain;
50 struct meson_ee_pwrc_mem_domain {
55 struct meson_ee_pwrc_top_domain {
56 unsigned int sleep_reg;
57 unsigned int sleep_mask;
59 unsigned int iso_mask;
62 struct meson_ee_pwrc_domain_desc {
64 unsigned int reset_names_count;
65 unsigned int clk_names_count;
66 struct meson_ee_pwrc_top_domain *top_pd;
67 unsigned int mem_pd_count;
68 struct meson_ee_pwrc_mem_domain *mem_pd;
69 bool (*get_power)(struct meson_ee_pwrc_domain *pwrc_domain);
72 struct meson_ee_pwrc_domain_data {
74 struct meson_ee_pwrc_domain_desc *domains;
77 /* TOP Power Domains */
79 static struct meson_ee_pwrc_top_domain gx_pwrc_vpu = {
80 .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
82 .iso_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
86 static struct meson_ee_pwrc_top_domain meson8_pwrc_vpu = {
87 .sleep_reg = MESON8_AO_RTI_GEN_PWR_SLEEP0,
89 .iso_reg = MESON8_AO_RTI_GEN_PWR_SLEEP0,
93 #define SM1_EE_PD(__bit) \
95 .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0, \
96 .sleep_mask = BIT(__bit), \
97 .iso_reg = GX_AO_RTI_GEN_PWR_ISO0, \
98 .iso_mask = BIT(__bit), \
101 static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
102 static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
103 static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
104 static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
105 static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
107 /* Memory PD Domains */
109 #define VPU_MEMPD(__reg) \
110 { __reg, GENMASK(1, 0) }, \
111 { __reg, GENMASK(3, 2) }, \
112 { __reg, GENMASK(5, 4) }, \
113 { __reg, GENMASK(7, 6) }, \
114 { __reg, GENMASK(9, 8) }, \
115 { __reg, GENMASK(11, 10) }, \
116 { __reg, GENMASK(13, 12) }, \
117 { __reg, GENMASK(15, 14) }, \
118 { __reg, GENMASK(17, 16) }, \
119 { __reg, GENMASK(19, 18) }, \
120 { __reg, GENMASK(21, 20) }, \
121 { __reg, GENMASK(23, 22) }, \
122 { __reg, GENMASK(25, 24) }, \
123 { __reg, GENMASK(27, 26) }, \
124 { __reg, GENMASK(29, 28) }, \
125 { __reg, GENMASK(31, 30) }
127 #define VPU_HHI_MEMPD(__reg) \
130 { __reg, BIT(10) }, \
131 { __reg, BIT(11) }, \
132 { __reg, BIT(12) }, \
133 { __reg, BIT(13) }, \
134 { __reg, BIT(14) }, \
137 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
138 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
139 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
140 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
141 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
144 static struct meson_ee_pwrc_mem_domain gxbb_pwrc_mem_vpu[] = {
145 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
146 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
147 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
150 static struct meson_ee_pwrc_mem_domain meson_pwrc_mem_eth[] = {
151 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
154 static struct meson_ee_pwrc_mem_domain meson8_pwrc_audio_dsp_mem[] = {
155 { HHI_MEM_PD_REG0, GENMASK(1, 0) },
158 static struct meson_ee_pwrc_mem_domain meson8_pwrc_mem_vpu[] = {
159 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
160 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
161 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
164 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
165 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
166 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
167 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
168 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
169 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
170 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
171 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
172 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
173 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
176 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
177 { HHI_NANOQ_MEM_PD_REG0, 0xff },
178 { HHI_NANOQ_MEM_PD_REG1, 0xff },
181 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
182 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
185 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
186 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
189 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
190 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
193 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
194 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
195 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
196 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
197 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
198 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
199 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
200 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
201 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
202 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
203 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
204 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
205 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
206 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
209 #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
212 .reset_names_count = __resets, \
213 .clk_names_count = __clks, \
214 .top_pd = __top_pd, \
215 .mem_pd_count = ARRAY_SIZE(__mem), \
217 .get_power = __get_power, \
220 #define TOP_PD(__name, __top_pd, __mem, __get_power) \
223 .top_pd = __top_pd, \
224 .mem_pd_count = ARRAY_SIZE(__mem), \
226 .get_power = __get_power, \
229 #define MEM_PD(__name, __mem) \
230 TOP_PD(__name, NULL, __mem, NULL)
232 static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain *pwrc_domain);
234 static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
235 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, g12a_pwrc_mem_vpu,
236 pwrc_ee_get_power, 11, 2),
237 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
240 static struct meson_ee_pwrc_domain_desc gxbb_pwrc_domains[] = {
241 [PWRC_GXBB_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, gxbb_pwrc_mem_vpu,
242 pwrc_ee_get_power, 12, 2),
243 [PWRC_GXBB_ETHERNET_MEM_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
246 static struct meson_ee_pwrc_domain_desc meson8_pwrc_domains[] = {
247 [PWRC_MESON8_VPU_ID] = VPU_PD("VPU", &meson8_pwrc_vpu,
248 meson8_pwrc_mem_vpu, pwrc_ee_get_power,
250 [PWRC_MESON8_ETHERNET_MEM_ID] = MEM_PD("ETHERNET_MEM",
252 [PWRC_MESON8_AUDIO_DSP_MEM_ID] = MEM_PD("AUDIO_DSP_MEM",
253 meson8_pwrc_audio_dsp_mem),
256 static struct meson_ee_pwrc_domain_desc meson8b_pwrc_domains[] = {
257 [PWRC_MESON8_VPU_ID] = VPU_PD("VPU", &meson8_pwrc_vpu,
258 meson8_pwrc_mem_vpu, pwrc_ee_get_power,
260 [PWRC_MESON8_ETHERNET_MEM_ID] = MEM_PD("ETHERNET_MEM",
262 [PWRC_MESON8_AUDIO_DSP_MEM_ID] = MEM_PD("AUDIO_DSP_MEM",
263 meson8_pwrc_audio_dsp_mem),
266 static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
267 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
268 pwrc_ee_get_power, 11, 2),
269 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
271 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
273 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
275 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
277 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
278 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
281 struct meson_ee_pwrc_domain {
282 struct generic_pm_domain base;
284 struct meson_ee_pwrc *pwrc;
285 struct meson_ee_pwrc_domain_desc desc;
286 struct clk_bulk_data *clks;
288 struct reset_control *rstc;
292 struct meson_ee_pwrc {
293 struct regmap *regmap_ao;
294 struct regmap *regmap_hhi;
295 struct meson_ee_pwrc_domain *domains;
296 struct genpd_onecell_data xlate;
299 static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain *pwrc_domain)
303 regmap_read(pwrc_domain->pwrc->regmap_ao,
304 pwrc_domain->desc.top_pd->sleep_reg, ®);
306 return (reg & pwrc_domain->desc.top_pd->sleep_mask);
309 static int meson_ee_pwrc_off(struct generic_pm_domain *domain)
311 struct meson_ee_pwrc_domain *pwrc_domain =
312 container_of(domain, struct meson_ee_pwrc_domain, base);
315 if (pwrc_domain->desc.top_pd)
316 regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
317 pwrc_domain->desc.top_pd->sleep_reg,
318 pwrc_domain->desc.top_pd->sleep_mask,
319 pwrc_domain->desc.top_pd->sleep_mask);
322 for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
323 regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
324 pwrc_domain->desc.mem_pd[i].reg,
325 pwrc_domain->desc.mem_pd[i].mask,
326 pwrc_domain->desc.mem_pd[i].mask);
330 if (pwrc_domain->desc.top_pd)
331 regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
332 pwrc_domain->desc.top_pd->iso_reg,
333 pwrc_domain->desc.top_pd->iso_mask,
334 pwrc_domain->desc.top_pd->iso_mask);
336 if (pwrc_domain->num_clks) {
338 clk_bulk_disable_unprepare(pwrc_domain->num_clks,
345 static int meson_ee_pwrc_on(struct generic_pm_domain *domain)
347 struct meson_ee_pwrc_domain *pwrc_domain =
348 container_of(domain, struct meson_ee_pwrc_domain, base);
351 if (pwrc_domain->desc.top_pd)
352 regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
353 pwrc_domain->desc.top_pd->sleep_reg,
354 pwrc_domain->desc.top_pd->sleep_mask, 0);
357 for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
358 regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
359 pwrc_domain->desc.mem_pd[i].reg,
360 pwrc_domain->desc.mem_pd[i].mask, 0);
364 ret = reset_control_assert(pwrc_domain->rstc);
368 if (pwrc_domain->desc.top_pd)
369 regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
370 pwrc_domain->desc.top_pd->iso_reg,
371 pwrc_domain->desc.top_pd->iso_mask, 0);
373 ret = reset_control_deassert(pwrc_domain->rstc);
377 return clk_bulk_prepare_enable(pwrc_domain->num_clks,
381 static int meson_ee_pwrc_init_domain(struct platform_device *pdev,
382 struct meson_ee_pwrc *pwrc,
383 struct meson_ee_pwrc_domain *dom)
388 dom->num_rstc = dom->desc.reset_names_count;
389 dom->num_clks = dom->desc.clk_names_count;
392 int count = reset_control_get_count(&pdev->dev);
394 if (count != dom->num_rstc)
395 dev_warn(&pdev->dev, "Invalid resets count %d for domain %s\n",
396 count, dom->desc.name);
398 dom->rstc = devm_reset_control_array_get(&pdev->dev, false,
400 if (IS_ERR(dom->rstc))
401 return PTR_ERR(dom->rstc);
405 int ret = devm_clk_bulk_get_all(&pdev->dev, &dom->clks);
409 if (dom->num_clks != ret) {
410 dev_warn(&pdev->dev, "Invalid clocks count %d for domain %s\n",
411 ret, dom->desc.name);
416 dom->base.name = dom->desc.name;
417 dom->base.power_on = meson_ee_pwrc_on;
418 dom->base.power_off = meson_ee_pwrc_off;
421 * TOFIX: This is a special case for the VPU power domain, which can
422 * be enabled previously by the bootloader. In this case the VPU
423 * pipeline may be functional but no driver maybe never attach
424 * to this power domain, and if the domain is disabled it could
425 * cause system errors. This is why the pm_domain_always_on_gov
427 * For the same reason, the clocks should be enabled in case
428 * we need to power the domain off, otherwise the internal clocks
429 * prepare/enable counters won't be in sync.
431 if (dom->num_clks && dom->desc.get_power && !dom->desc.get_power(dom)) {
432 ret = clk_bulk_prepare_enable(dom->num_clks, dom->clks);
436 ret = pm_genpd_init(&dom->base, &pm_domain_always_on_gov,
441 ret = pm_genpd_init(&dom->base, NULL,
442 (dom->desc.get_power ?
443 dom->desc.get_power(dom) : true));
451 static int meson_ee_pwrc_probe(struct platform_device *pdev)
453 const struct meson_ee_pwrc_domain_data *match;
454 struct regmap *regmap_ao, *regmap_hhi;
455 struct meson_ee_pwrc *pwrc;
458 match = of_device_get_match_data(&pdev->dev);
460 dev_err(&pdev->dev, "failed to get match data\n");
464 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
468 pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
469 sizeof(*pwrc->xlate.domains),
471 if (!pwrc->xlate.domains)
474 pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
475 sizeof(*pwrc->domains), GFP_KERNEL);
479 pwrc->xlate.num_domains = match->count;
481 regmap_hhi = syscon_node_to_regmap(of_get_parent(pdev->dev.of_node));
482 if (IS_ERR(regmap_hhi)) {
483 dev_err(&pdev->dev, "failed to get HHI regmap\n");
484 return PTR_ERR(regmap_hhi);
487 regmap_ao = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
488 "amlogic,ao-sysctrl");
489 if (IS_ERR(regmap_ao)) {
490 dev_err(&pdev->dev, "failed to get AO regmap\n");
491 return PTR_ERR(regmap_ao);
494 pwrc->regmap_ao = regmap_ao;
495 pwrc->regmap_hhi = regmap_hhi;
497 platform_set_drvdata(pdev, pwrc);
499 for (i = 0 ; i < match->count ; ++i) {
500 struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
502 memcpy(&dom->desc, &match->domains[i], sizeof(dom->desc));
504 ret = meson_ee_pwrc_init_domain(pdev, pwrc, dom);
508 pwrc->xlate.domains[i] = &dom->base;
511 return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
514 static void meson_ee_pwrc_shutdown(struct platform_device *pdev)
516 struct meson_ee_pwrc *pwrc = platform_get_drvdata(pdev);
519 for (i = 0 ; i < pwrc->xlate.num_domains ; ++i) {
520 struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
522 if (dom->desc.get_power && !dom->desc.get_power(dom))
523 meson_ee_pwrc_off(&dom->base);
527 static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
528 .count = ARRAY_SIZE(g12a_pwrc_domains),
529 .domains = g12a_pwrc_domains,
532 static struct meson_ee_pwrc_domain_data meson_ee_gxbb_pwrc_data = {
533 .count = ARRAY_SIZE(gxbb_pwrc_domains),
534 .domains = gxbb_pwrc_domains,
537 static struct meson_ee_pwrc_domain_data meson_ee_m8_pwrc_data = {
538 .count = ARRAY_SIZE(meson8_pwrc_domains),
539 .domains = meson8_pwrc_domains,
542 static struct meson_ee_pwrc_domain_data meson_ee_m8b_pwrc_data = {
543 .count = ARRAY_SIZE(meson8b_pwrc_domains),
544 .domains = meson8b_pwrc_domains,
547 static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
548 .count = ARRAY_SIZE(sm1_pwrc_domains),
549 .domains = sm1_pwrc_domains,
552 static const struct of_device_id meson_ee_pwrc_match_table[] = {
554 .compatible = "amlogic,meson8-pwrc",
555 .data = &meson_ee_m8_pwrc_data,
558 .compatible = "amlogic,meson8b-pwrc",
559 .data = &meson_ee_m8b_pwrc_data,
562 .compatible = "amlogic,meson8m2-pwrc",
563 .data = &meson_ee_m8b_pwrc_data,
566 .compatible = "amlogic,meson-gxbb-pwrc",
567 .data = &meson_ee_gxbb_pwrc_data,
570 .compatible = "amlogic,meson-g12a-pwrc",
571 .data = &meson_ee_g12a_pwrc_data,
574 .compatible = "amlogic,meson-sm1-pwrc",
575 .data = &meson_ee_sm1_pwrc_data,
580 static struct platform_driver meson_ee_pwrc_driver = {
581 .probe = meson_ee_pwrc_probe,
582 .shutdown = meson_ee_pwrc_shutdown,
584 .name = "meson_ee_pwrc",
585 .of_match_table = meson_ee_pwrc_match_table,
588 builtin_platform_driver(meson_ee_pwrc_driver);