1 // SPDX-License-Identifier: GPL-2.0
3 * rmobile power management support
5 * Copyright (C) 2012 Renesas Solutions Corp.
6 * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
20 #include <linux/pm_clock.h>
21 #include <linux/pm_domain.h>
22 #include <linux/slab.h>
27 #define SPDCR 0x08 /* SYS Power Down Control Register */
28 #define SWUCR 0x14 /* SYS Wakeup Control Register */
29 #define PSTR 0x80 /* Power Status Register */
31 #define PSTR_RETRIES 100
32 #define PSTR_DELAY_US 10
34 struct rmobile_pm_domain
{
35 struct generic_pm_domain genpd
;
36 struct dev_power_governor
*gov
;
39 unsigned int bit_shift
;
43 struct rmobile_pm_domain
*to_rmobile_pd(struct generic_pm_domain
*d
)
45 return container_of(d
, struct rmobile_pm_domain
, genpd
);
48 static int rmobile_pd_power_down(struct generic_pm_domain
*genpd
)
50 struct rmobile_pm_domain
*rmobile_pd
= to_rmobile_pd(genpd
);
51 unsigned int mask
= BIT(rmobile_pd
->bit_shift
);
53 if (rmobile_pd
->suspend
) {
54 int ret
= rmobile_pd
->suspend();
60 if (readl(rmobile_pd
->base
+ PSTR
) & mask
) {
61 unsigned int retry_count
;
62 writel(mask
, rmobile_pd
->base
+ SPDCR
);
64 for (retry_count
= PSTR_RETRIES
; retry_count
; retry_count
--) {
65 if (!(readl(rmobile_pd
->base
+ SPDCR
) & mask
))
71 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n", genpd
->name
, mask
,
72 readl(rmobile_pd
->base
+ PSTR
));
77 static int __rmobile_pd_power_up(struct rmobile_pm_domain
*rmobile_pd
)
79 unsigned int mask
= BIT(rmobile_pd
->bit_shift
);
80 unsigned int retry_count
;
83 if (readl(rmobile_pd
->base
+ PSTR
) & mask
)
86 writel(mask
, rmobile_pd
->base
+ SWUCR
);
88 for (retry_count
= 2 * PSTR_RETRIES
; retry_count
; retry_count
--) {
89 if (!(readl(rmobile_pd
->base
+ SWUCR
) & mask
))
91 if (retry_count
> PSTR_RETRIES
)
92 udelay(PSTR_DELAY_US
);
99 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
100 rmobile_pd
->genpd
.name
, mask
,
101 readl(rmobile_pd
->base
+ PSTR
));
106 static int rmobile_pd_power_up(struct generic_pm_domain
*genpd
)
108 return __rmobile_pd_power_up(to_rmobile_pd(genpd
));
111 static void rmobile_init_pm_domain(struct rmobile_pm_domain
*rmobile_pd
)
113 struct generic_pm_domain
*genpd
= &rmobile_pd
->genpd
;
114 struct dev_power_governor
*gov
= rmobile_pd
->gov
;
116 genpd
->flags
|= GENPD_FLAG_PM_CLK
| GENPD_FLAG_ACTIVE_WAKEUP
;
117 genpd
->attach_dev
= cpg_mstp_attach_dev
;
118 genpd
->detach_dev
= cpg_mstp_detach_dev
;
120 if (!(genpd
->flags
& GENPD_FLAG_ALWAYS_ON
)) {
121 genpd
->power_off
= rmobile_pd_power_down
;
122 genpd
->power_on
= rmobile_pd_power_up
;
123 __rmobile_pd_power_up(rmobile_pd
);
126 pm_genpd_init(genpd
, gov
? : &simple_qos_governor
, false);
129 static int rmobile_pd_suspend_console(void)
132 * Serial consoles make use of SCIF hardware located in this domain,
133 * hence keep the power domain on if "no_console_suspend" is set.
135 return console_suspend_enabled
? 0 : -EBUSY
;
146 #define MAX_NUM_SPECIAL_PDS 16
148 static struct special_pd
{
149 struct device_node
*pd
;
151 } special_pds
[MAX_NUM_SPECIAL_PDS
] __initdata
;
153 static unsigned int num_special_pds __initdata
;
155 static const struct of_device_id special_ids
[] __initconst
= {
156 { .compatible
= "arm,coresight-etm3x", .data
= (void *)PD_DEBUG
},
157 { .compatible
= "renesas,dbsc-r8a73a4", .data
= (void *)PD_MEMCTL
, },
158 { .compatible
= "renesas,dbsc3-r8a7740", .data
= (void *)PD_MEMCTL
, },
159 { .compatible
= "renesas,sbsc-sh73a0", .data
= (void *)PD_MEMCTL
, },
163 static void __init
add_special_pd(struct device_node
*np
, enum pd_types type
)
166 struct device_node
*pd
;
168 pd
= of_parse_phandle(np
, "power-domains", 0);
172 for (i
= 0; i
< num_special_pds
; i
++)
173 if (pd
== special_pds
[i
].pd
&& type
== special_pds
[i
].type
) {
178 if (num_special_pds
== ARRAY_SIZE(special_pds
)) {
179 pr_warn("Too many special PM domains\n");
184 pr_debug("Special PM domain %pOFn type %d for %pOF\n", pd
, type
, np
);
186 special_pds
[num_special_pds
].pd
= pd
;
187 special_pds
[num_special_pds
].type
= type
;
191 static void __init
get_special_pds(void)
193 struct device_node
*np
;
194 const struct of_device_id
*id
;
196 /* PM domains containing CPUs */
197 for_each_of_cpu_node(np
)
198 add_special_pd(np
, PD_CPU
);
200 /* PM domain containing console */
202 add_special_pd(of_stdout
, PD_CONSOLE
);
204 /* PM domains containing other special devices */
205 for_each_matching_node_and_match(np
, special_ids
, &id
)
206 add_special_pd(np
, (enum pd_types
)id
->data
);
209 static void __init
put_special_pds(void)
213 for (i
= 0; i
< num_special_pds
; i
++)
214 of_node_put(special_pds
[i
].pd
);
217 static enum pd_types __init
pd_type(const struct device_node
*pd
)
221 for (i
= 0; i
< num_special_pds
; i
++)
222 if (pd
== special_pds
[i
].pd
)
223 return special_pds
[i
].type
;
228 static void __init
rmobile_setup_pm_domain(struct device_node
*np
,
229 struct rmobile_pm_domain
*pd
)
231 const char *name
= pd
->genpd
.name
;
233 switch (pd_type(np
)) {
236 * This domain contains the CPU core and therefore it should
237 * only be turned off if the CPU is not in use.
239 pr_debug("PM domain %s contains CPU\n", name
);
240 pd
->genpd
.flags
|= GENPD_FLAG_ALWAYS_ON
;
244 pr_debug("PM domain %s contains serial console\n", name
);
245 pd
->gov
= &pm_domain_always_on_gov
;
246 pd
->suspend
= rmobile_pd_suspend_console
;
251 * This domain contains the Coresight-ETM hardware block and
252 * therefore it should only be turned off if the debug module
255 pr_debug("PM domain %s contains Coresight-ETM\n", name
);
256 pd
->genpd
.flags
|= GENPD_FLAG_ALWAYS_ON
;
261 * This domain contains a memory-controller and therefore it
262 * should only be turned off if memory is not in use.
264 pr_debug("PM domain %s contains MEMCTL\n", name
);
265 pd
->genpd
.flags
|= GENPD_FLAG_ALWAYS_ON
;
269 if (pd
->bit_shift
== ~0) {
270 /* Top-level always-on domain */
271 pr_debug("PM domain %s is always-on domain\n", name
);
272 pd
->genpd
.flags
|= GENPD_FLAG_ALWAYS_ON
;
277 rmobile_init_pm_domain(pd
);
280 static int __init
rmobile_add_pm_domains(void __iomem
*base
,
281 struct device_node
*parent
,
282 struct generic_pm_domain
*genpd_parent
)
284 struct device_node
*np
;
286 for_each_child_of_node(parent
, np
) {
287 struct rmobile_pm_domain
*pd
;
290 if (of_property_read_u32(np
, "reg", &idx
)) {
291 /* always-on domain */
294 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
);
300 pd
->genpd
.name
= np
->name
;
304 rmobile_setup_pm_domain(np
, pd
);
306 pm_genpd_add_subdomain(genpd_parent
, &pd
->genpd
);
307 of_genpd_add_provider_simple(np
, &pd
->genpd
);
309 rmobile_add_pm_domains(base
, np
, &pd
->genpd
);
314 static int __init
rmobile_init_pm_domains(void)
316 struct device_node
*np
, *pmd
;
317 bool scanned
= false;
321 for_each_compatible_node(np
, NULL
, "renesas,sysc-rmobile") {
322 base
= of_iomap(np
, 0);
324 pr_warn("%pOF cannot map reg 0\n", np
);
328 pmd
= of_get_child_by_name(np
, "pm-domains");
331 pr_warn("%pOF lacks pm-domains node\n", np
);
336 /* Find PM domains containing special blocks */
341 ret
= rmobile_add_pm_domains(base
, pmd
, NULL
);
354 core_initcall(rmobile_init_pm_domains
);