1 // SPDX-License-Identifier: GPL-2.0
3 * rmobile power management support
5 * Copyright (C) 2012 Renesas Solutions Corp.
7 * Copyright (C) 2014 Glider bvba
10 * Copyright (C) 2011 Magnus Damm
12 #include <linux/clk/renesas.h>
13 #include <linux/console.h>
14 #include <linux/delay.h>
16 #include <linux/of_address.h>
18 #include <linux/pm_clock.h>
19 #include <linux/pm_domain.h>
20 #include <linux/slab.h>
25 #define SPDCR 0x08 /* SYS Power Down Control Register */
26 #define SWUCR 0x14 /* SYS Wakeup Control Register */
27 #define PSTR 0x80 /* Power Status Register */
29 #define PSTR_RETRIES 100
30 #define PSTR_DELAY_US 10
32 struct rmobile_pm_domain {
33 struct generic_pm_domain genpd;
34 struct dev_power_governor *gov;
37 unsigned int bit_shift;
41 struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
43 return container_of(d, struct rmobile_pm_domain, genpd);
46 static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
48 struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
49 unsigned int mask = BIT(rmobile_pd->bit_shift);
51 if (rmobile_pd->suspend) {
52 int ret = rmobile_pd->suspend();
58 if (readl(rmobile_pd->base + PSTR) & mask) {
59 unsigned int retry_count;
60 writel(mask, rmobile_pd->base + SPDCR);
62 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
63 if (!(readl(rmobile_pd->base + SPDCR) & mask))
69 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n", genpd->name, mask,
70 readl(rmobile_pd->base + PSTR));
75 static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd)
77 unsigned int mask = BIT(rmobile_pd->bit_shift);
78 unsigned int retry_count;
81 if (readl(rmobile_pd->base + PSTR) & mask)
84 writel(mask, rmobile_pd->base + SWUCR);
86 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
87 if (!(readl(rmobile_pd->base + SWUCR) & mask))
89 if (retry_count > PSTR_RETRIES)
90 udelay(PSTR_DELAY_US);
97 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
98 rmobile_pd->genpd.name, mask,
99 readl(rmobile_pd->base + PSTR));
104 static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
106 return __rmobile_pd_power_up(to_rmobile_pd(genpd));
109 static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
111 struct generic_pm_domain *genpd = &rmobile_pd->genpd;
112 struct dev_power_governor *gov = rmobile_pd->gov;
114 genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
115 genpd->attach_dev = cpg_mstp_attach_dev;
116 genpd->detach_dev = cpg_mstp_detach_dev;
118 if (!(genpd->flags & GENPD_FLAG_ALWAYS_ON)) {
119 genpd->power_off = rmobile_pd_power_down;
120 genpd->power_on = rmobile_pd_power_up;
121 __rmobile_pd_power_up(rmobile_pd);
124 pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
127 static int rmobile_pd_suspend_console(void)
130 * Serial consoles make use of SCIF hardware located in this domain,
131 * hence keep the power domain on if "no_console_suspend" is set.
133 return console_suspend_enabled ? 0 : -EBUSY;
144 #define MAX_NUM_SPECIAL_PDS 16
146 static struct special_pd {
147 struct device_node *pd;
149 } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
151 static unsigned int num_special_pds __initdata;
153 static const struct of_device_id special_ids[] __initconst = {
154 { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
155 { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
156 { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
157 { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
161 static void __init add_special_pd(struct device_node *np, enum pd_types type)
164 struct device_node *pd;
166 pd = of_parse_phandle(np, "power-domains", 0);
170 for (i = 0; i < num_special_pds; i++)
171 if (pd == special_pds[i].pd && type == special_pds[i].type) {
176 if (num_special_pds == ARRAY_SIZE(special_pds)) {
177 pr_warn("Too many special PM domains\n");
182 pr_debug("Special PM domain %pOFn type %d for %pOF\n", pd, type, np);
184 special_pds[num_special_pds].pd = pd;
185 special_pds[num_special_pds].type = type;
189 static void __init get_special_pds(void)
191 struct device_node *np;
192 const struct of_device_id *id;
194 /* PM domains containing CPUs */
195 for_each_of_cpu_node(np)
196 add_special_pd(np, PD_CPU);
198 /* PM domain containing console */
200 add_special_pd(of_stdout, PD_CONSOLE);
202 /* PM domains containing other special devices */
203 for_each_matching_node_and_match(np, special_ids, &id)
204 add_special_pd(np, (enum pd_types)id->data);
207 static void __init put_special_pds(void)
211 for (i = 0; i < num_special_pds; i++)
212 of_node_put(special_pds[i].pd);
215 static enum pd_types __init pd_type(const struct device_node *pd)
219 for (i = 0; i < num_special_pds; i++)
220 if (pd == special_pds[i].pd)
221 return special_pds[i].type;
226 static void __init rmobile_setup_pm_domain(struct device_node *np,
227 struct rmobile_pm_domain *pd)
229 const char *name = pd->genpd.name;
231 switch (pd_type(np)) {
234 * This domain contains the CPU core and therefore it should
235 * only be turned off if the CPU is not in use.
237 pr_debug("PM domain %s contains CPU\n", name);
238 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
242 pr_debug("PM domain %s contains serial console\n", name);
243 pd->gov = &pm_domain_always_on_gov;
244 pd->suspend = rmobile_pd_suspend_console;
249 * This domain contains the Coresight-ETM hardware block and
250 * therefore it should only be turned off if the debug module
253 pr_debug("PM domain %s contains Coresight-ETM\n", name);
254 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
259 * This domain contains a memory-controller and therefore it
260 * should only be turned off if memory is not in use.
262 pr_debug("PM domain %s contains MEMCTL\n", name);
263 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
267 if (pd->bit_shift == ~0) {
268 /* Top-level always-on domain */
269 pr_debug("PM domain %s is always-on domain\n", name);
270 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
275 rmobile_init_pm_domain(pd);
278 static int __init rmobile_add_pm_domains(void __iomem *base,
279 struct device_node *parent,
280 struct generic_pm_domain *genpd_parent)
282 struct device_node *np;
284 for_each_child_of_node(parent, np) {
285 struct rmobile_pm_domain *pd;
288 if (of_property_read_u32(np, "reg", &idx)) {
289 /* always-on domain */
292 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
298 pd->genpd.name = np->name;
302 rmobile_setup_pm_domain(np, pd);
304 pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
305 of_genpd_add_provider_simple(np, &pd->genpd);
307 rmobile_add_pm_domains(base, np, &pd->genpd);
312 static int __init rmobile_init_pm_domains(void)
314 struct device_node *np, *pmd;
315 bool scanned = false;
319 for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
320 base = of_iomap(np, 0);
322 pr_warn("%pOF cannot map reg 0\n", np);
326 pmd = of_get_child_by_name(np, "pm-domains");
329 pr_warn("%pOF lacks pm-domains node\n", np);
334 /* Find PM domains containing special blocks */
339 ret = rmobile_add_pm_domains(base, pmd, NULL);
346 fwnode_dev_initialized(&np->fwnode, true);
354 core_initcall(rmobile_init_pm_domains);