2 * rmobile power management support
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 * Copyright (C) 2014 Glider bvba
9 * Copyright (C) 2011 Magnus Damm
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file "COPYING" in the main directory of this archive
15 #include <linux/clk/renesas.h>
16 #include <linux/console.h>
17 #include <linux/delay.h>
19 #include <linux/of_address.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
23 #include <linux/pm_clock.h>
24 #include <linux/slab.h>
28 #include "pm-rmobile.h"
31 #define SPDCR 0x08 /* SYS Power Down Control Register */
32 #define SWUCR 0x14 /* SYS Wakeup Control Register */
33 #define PSTR 0x80 /* Power Status Register */
35 #define PSTR_RETRIES 100
36 #define PSTR_DELAY_US 10
39 struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
41 return container_of(d, struct rmobile_pm_domain, genpd);
44 static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
46 struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
49 if (rmobile_pd->bit_shift == ~0)
52 mask = BIT(rmobile_pd->bit_shift);
53 if (rmobile_pd->suspend) {
54 int ret = rmobile_pd->suspend();
60 if (__raw_readl(rmobile_pd->base + PSTR) & mask) {
61 unsigned int retry_count;
62 __raw_writel(mask, rmobile_pd->base + SPDCR);
64 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
65 if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask))
71 if (!rmobile_pd->no_debug)
72 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
74 __raw_readl(rmobile_pd->base + PSTR));
79 static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd,
83 unsigned int retry_count;
86 if (rmobile_pd->bit_shift == ~0)
89 mask = BIT(rmobile_pd->bit_shift);
90 if (__raw_readl(rmobile_pd->base + PSTR) & mask)
93 __raw_writel(mask, rmobile_pd->base + SWUCR);
95 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
96 if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask))
98 if (retry_count > PSTR_RETRIES)
99 udelay(PSTR_DELAY_US);
106 if (!rmobile_pd->no_debug)
107 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
108 rmobile_pd->genpd.name, mask,
109 __raw_readl(rmobile_pd->base + PSTR));
112 if (ret == 0 && rmobile_pd->resume && do_resume)
113 rmobile_pd->resume();
118 static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
120 return __rmobile_pd_power_up(to_rmobile_pd(genpd), true);
123 static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
125 struct generic_pm_domain *genpd = &rmobile_pd->genpd;
126 struct dev_power_governor *gov = rmobile_pd->gov;
128 genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
129 genpd->power_off = rmobile_pd_power_down;
130 genpd->power_on = rmobile_pd_power_up;
131 genpd->attach_dev = cpg_mstp_attach_dev;
132 genpd->detach_dev = cpg_mstp_detach_dev;
133 __rmobile_pd_power_up(rmobile_pd, false);
134 pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
137 static int rmobile_pd_suspend_console(void)
140 * Serial consoles make use of SCIF hardware located in this domain,
141 * hence keep the power domain on if "no_console_suspend" is set.
143 return console_suspend_enabled ? 0 : -EBUSY;
154 #define MAX_NUM_SPECIAL_PDS 16
156 static struct special_pd {
157 struct device_node *pd;
159 } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
161 static unsigned int num_special_pds __initdata;
163 static const struct of_device_id special_ids[] __initconst = {
164 { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
165 { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
166 { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
167 { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
171 static void __init add_special_pd(struct device_node *np, enum pd_types type)
174 struct device_node *pd;
176 pd = of_parse_phandle(np, "power-domains", 0);
180 for (i = 0; i < num_special_pds; i++)
181 if (pd == special_pds[i].pd && type == special_pds[i].type) {
186 if (num_special_pds == ARRAY_SIZE(special_pds)) {
187 pr_warn("Too many special PM domains\n");
192 pr_debug("Special PM domain %s type %d for %pOF\n", pd->name, type, np);
194 special_pds[num_special_pds].pd = pd;
195 special_pds[num_special_pds].type = type;
199 static void __init get_special_pds(void)
201 struct device_node *np;
202 const struct of_device_id *id;
204 /* PM domains containing CPUs */
205 for_each_node_by_type(np, "cpu")
206 add_special_pd(np, PD_CPU);
208 /* PM domain containing console */
210 add_special_pd(of_stdout, PD_CONSOLE);
212 /* PM domains containing other special devices */
213 for_each_matching_node_and_match(np, special_ids, &id)
214 add_special_pd(np, (enum pd_types)id->data);
217 static void __init put_special_pds(void)
221 for (i = 0; i < num_special_pds; i++)
222 of_node_put(special_pds[i].pd);
225 static enum pd_types __init pd_type(const struct device_node *pd)
229 for (i = 0; i < num_special_pds; i++)
230 if (pd == special_pds[i].pd)
231 return special_pds[i].type;
236 static void __init rmobile_setup_pm_domain(struct device_node *np,
237 struct rmobile_pm_domain *pd)
239 const char *name = pd->genpd.name;
241 switch (pd_type(np)) {
244 * This domain contains the CPU core and therefore it should
245 * only be turned off if the CPU is not in use.
247 pr_debug("PM domain %s contains CPU\n", name);
248 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
252 pr_debug("PM domain %s contains serial console\n", name);
253 pd->gov = &pm_domain_always_on_gov;
254 pd->suspend = rmobile_pd_suspend_console;
259 * This domain contains the Coresight-ETM hardware block and
260 * therefore it should only be turned off if the debug module
263 pr_debug("PM domain %s contains Coresight-ETM\n", name);
264 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
269 * This domain contains a memory-controller and therefore it
270 * should only be turned off if memory is not in use.
272 pr_debug("PM domain %s contains MEMCTL\n", name);
273 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
280 rmobile_init_pm_domain(pd);
283 static int __init rmobile_add_pm_domains(void __iomem *base,
284 struct device_node *parent,
285 struct generic_pm_domain *genpd_parent)
287 struct device_node *np;
289 for_each_child_of_node(parent, np) {
290 struct rmobile_pm_domain *pd;
293 if (of_property_read_u32(np, "reg", &idx)) {
294 /* always-on domain */
297 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
303 pd->genpd.name = np->name;
307 rmobile_setup_pm_domain(np, pd);
309 pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
310 of_genpd_add_provider_simple(np, &pd->genpd);
312 rmobile_add_pm_domains(base, np, &pd->genpd);
317 static int __init rmobile_init_pm_domains(void)
319 struct device_node *np, *pmd;
320 bool scanned = false;
324 for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
325 base = of_iomap(np, 0);
327 pr_warn("%pOF cannot map reg 0\n", np);
331 pmd = of_get_child_by_name(np, "pm-domains");
334 pr_warn("%pOF lacks pm-domains node\n", np);
339 /* Find PM domains containing special blocks */
344 ret = rmobile_add_pm_domains(base, pmd, NULL);
357 core_initcall(rmobile_init_pm_domains);