1 // SPDX-License-Identifier: GPL-2.0
3 * R-Car SYSC Power management support
5 * Copyright (C) 2014 Magnus Damm
6 * Copyright (C) 2015-2017 Glider bvba
9 #include <linux/clk/renesas.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
13 #include <linux/of_address.h>
14 #include <linux/pm_domain.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
18 #include <linux/iopoll.h>
19 #include <linux/soc/renesas/rcar-sysc.h>
21 #include "rcar-sysc.h"
24 #define SYSCSR 0x00 /* SYSC Status Register */
25 #define SYSCISR 0x04 /* Interrupt Status Register */
26 #define SYSCISCR 0x08 /* Interrupt Status Clear Register */
27 #define SYSCIER 0x0c /* Interrupt Enable Register */
28 #define SYSCIMR 0x10 /* Interrupt Mask Register */
30 /* SYSC Status Register */
31 #define SYSCSR_PONENB 1 /* Ready for power resume requests */
32 #define SYSCSR_POFFENB 0 /* Ready for power shutoff requests */
35 * Power Control Register Offsets inside the register block for each domain
36 * Note: The "CR" registers for ARM cores exist on H1 only
37 * Use WFI to power off, CPG/APMU to resume ARM cores on R-Car Gen2
38 * Use PSCI on R-Car Gen3
40 #define PWRSR_OFFS 0x00 /* Power Status Register */
41 #define PWROFFCR_OFFS 0x04 /* Power Shutoff Control Register */
42 #define PWROFFSR_OFFS 0x08 /* Power Shutoff Status Register */
43 #define PWRONCR_OFFS 0x0c /* Power Resume Control Register */
44 #define PWRONSR_OFFS 0x10 /* Power Resume Status Register */
45 #define PWRER_OFFS 0x14 /* Power Shutoff/Resume Error */
48 #define SYSCSR_TIMEOUT 1000
49 #define SYSCSR_DELAY_US 1
51 #define PWRER_RETRIES 1000
52 #define PWRER_DELAY_US 1
54 #define SYSCISR_TIMEOUT 1000
55 #define SYSCISR_DELAY_US 1
57 #define RCAR_PD_ALWAYS_ON 32 /* Always-on power area */
60 struct generic_pm_domain genpd
;
68 static void __iomem
*rcar_sysc_base
;
69 static DEFINE_SPINLOCK(rcar_sysc_lock
); /* SMP CPUs + I/O devices */
70 static u32 rcar_sysc_extmask_offs
, rcar_sysc_extmask_val
;
72 static int rcar_sysc_pwr_on_off(const struct rcar_sysc_pd
*pd
, bool on
)
74 unsigned int sr_bit
, reg_offs
;
79 sr_bit
= SYSCSR_PONENB
;
80 reg_offs
= PWRONCR_OFFS
;
82 sr_bit
= SYSCSR_POFFENB
;
83 reg_offs
= PWROFFCR_OFFS
;
86 /* Wait until SYSC is ready to accept a power request */
87 ret
= readl_poll_timeout_atomic(rcar_sysc_base
+ SYSCSR
, val
,
88 val
& BIT(sr_bit
), SYSCSR_DELAY_US
,
93 /* Power-off delay quirk */
94 if (!on
&& (pd
->flags
& PD_OFF_DELAY
))
97 /* Submit power shutoff or power resume request */
98 iowrite32(BIT(pd
->chan_bit
), rcar_sysc_base
+ pd
->chan_offs
+ reg_offs
);
103 static int rcar_sysc_power(const struct rcar_sysc_pd
*pd
, bool on
)
105 unsigned int isr_mask
= BIT(pd
->isr_bit
);
106 unsigned int chan_mask
= BIT(pd
->chan_bit
);
107 unsigned int status
, k
;
111 spin_lock_irqsave(&rcar_sysc_lock
, flags
);
114 * Mask external power requests for CPU or 3DG domains
116 if (rcar_sysc_extmask_val
) {
117 iowrite32(rcar_sysc_extmask_val
,
118 rcar_sysc_base
+ rcar_sysc_extmask_offs
);
122 * The interrupt source needs to be enabled, but masked, to prevent the
123 * CPU from receiving it.
125 iowrite32(ioread32(rcar_sysc_base
+ SYSCIMR
) | isr_mask
,
126 rcar_sysc_base
+ SYSCIMR
);
127 iowrite32(ioread32(rcar_sysc_base
+ SYSCIER
) | isr_mask
,
128 rcar_sysc_base
+ SYSCIER
);
130 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
132 /* Submit power shutoff or resume request until it was accepted */
133 for (k
= 0; k
< PWRER_RETRIES
; k
++) {
134 ret
= rcar_sysc_pwr_on_off(pd
, on
);
138 status
= ioread32(rcar_sysc_base
+ pd
->chan_offs
+ PWRER_OFFS
);
139 if (!(status
& chan_mask
))
142 udelay(PWRER_DELAY_US
);
145 if (k
== PWRER_RETRIES
) {
150 /* Wait until the power shutoff or resume request has completed * */
151 ret
= readl_poll_timeout_atomic(rcar_sysc_base
+ SYSCISR
, status
,
152 status
& isr_mask
, SYSCISR_DELAY_US
,
157 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
160 if (rcar_sysc_extmask_val
)
161 iowrite32(0, rcar_sysc_base
+ rcar_sysc_extmask_offs
);
163 spin_unlock_irqrestore(&rcar_sysc_lock
, flags
);
165 pr_debug("sysc power %s domain %d: %08x -> %d\n", on
? "on" : "off",
166 pd
->isr_bit
, ioread32(rcar_sysc_base
+ SYSCISR
), ret
);
170 static bool rcar_sysc_power_is_off(const struct rcar_sysc_pd
*pd
)
174 st
= ioread32(rcar_sysc_base
+ pd
->chan_offs
+ PWRSR_OFFS
);
175 if (st
& BIT(pd
->chan_bit
))
181 static inline struct rcar_sysc_pd
*to_rcar_pd(struct generic_pm_domain
*d
)
183 return container_of(d
, struct rcar_sysc_pd
, genpd
);
186 static int rcar_sysc_pd_power_off(struct generic_pm_domain
*genpd
)
188 struct rcar_sysc_pd
*pd
= to_rcar_pd(genpd
);
190 pr_debug("%s: %s\n", __func__
, genpd
->name
);
191 return rcar_sysc_power(pd
, false);
194 static int rcar_sysc_pd_power_on(struct generic_pm_domain
*genpd
)
196 struct rcar_sysc_pd
*pd
= to_rcar_pd(genpd
);
198 pr_debug("%s: %s\n", __func__
, genpd
->name
);
199 return rcar_sysc_power(pd
, true);
202 static bool has_cpg_mstp
;
204 static int __init
rcar_sysc_pd_setup(struct rcar_sysc_pd
*pd
)
206 struct generic_pm_domain
*genpd
= &pd
->genpd
;
207 const char *name
= pd
->genpd
.name
;
210 if (pd
->flags
& PD_CPU
) {
212 * This domain contains a CPU core and therefore it should
213 * only be turned off if the CPU is not in use.
215 pr_debug("PM domain %s contains %s\n", name
, "CPU");
216 genpd
->flags
|= GENPD_FLAG_ALWAYS_ON
;
217 } else if (pd
->flags
& PD_SCU
) {
219 * This domain contains an SCU and cache-controller, and
220 * therefore it should only be turned off if the CPU cores are
223 pr_debug("PM domain %s contains %s\n", name
, "SCU");
224 genpd
->flags
|= GENPD_FLAG_ALWAYS_ON
;
225 } else if (pd
->flags
& PD_NO_CR
) {
227 * This domain cannot be turned off.
229 genpd
->flags
|= GENPD_FLAG_ALWAYS_ON
;
232 if (!(pd
->flags
& (PD_CPU
| PD_SCU
))) {
233 /* Enable Clock Domain for I/O devices */
234 genpd
->flags
|= GENPD_FLAG_PM_CLK
| GENPD_FLAG_ACTIVE_WAKEUP
;
236 genpd
->attach_dev
= cpg_mstp_attach_dev
;
237 genpd
->detach_dev
= cpg_mstp_detach_dev
;
239 genpd
->attach_dev
= cpg_mssr_attach_dev
;
240 genpd
->detach_dev
= cpg_mssr_detach_dev
;
244 genpd
->power_off
= rcar_sysc_pd_power_off
;
245 genpd
->power_on
= rcar_sysc_pd_power_on
;
247 if (pd
->flags
& (PD_CPU
| PD_NO_CR
)) {
248 /* Skip CPUs (handled by SMP code) and areas without control */
249 pr_debug("%s: Not touching %s\n", __func__
, genpd
->name
);
253 if (!rcar_sysc_power_is_off(pd
)) {
254 pr_debug("%s: %s is already powered\n", __func__
, genpd
->name
);
258 rcar_sysc_power(pd
, true);
261 error
= pm_genpd_init(genpd
, &simple_qos_governor
, false);
263 pr_err("Failed to init PM domain %s: %d\n", name
, error
);
268 static const struct of_device_id rcar_sysc_matches
[] __initconst
= {
269 #ifdef CONFIG_SYSC_R8A7742
270 { .compatible
= "renesas,r8a7742-sysc", .data
= &r8a7742_sysc_info
},
272 #ifdef CONFIG_SYSC_R8A7743
273 { .compatible
= "renesas,r8a7743-sysc", .data
= &r8a7743_sysc_info
},
274 /* RZ/G1N is identical to RZ/G2M w.r.t. power domains. */
275 { .compatible
= "renesas,r8a7744-sysc", .data
= &r8a7743_sysc_info
},
277 #ifdef CONFIG_SYSC_R8A7745
278 { .compatible
= "renesas,r8a7745-sysc", .data
= &r8a7745_sysc_info
},
280 #ifdef CONFIG_SYSC_R8A77470
281 { .compatible
= "renesas,r8a77470-sysc", .data
= &r8a77470_sysc_info
},
283 #ifdef CONFIG_SYSC_R8A774A1
284 { .compatible
= "renesas,r8a774a1-sysc", .data
= &r8a774a1_sysc_info
},
286 #ifdef CONFIG_SYSC_R8A774B1
287 { .compatible
= "renesas,r8a774b1-sysc", .data
= &r8a774b1_sysc_info
},
289 #ifdef CONFIG_SYSC_R8A774C0
290 { .compatible
= "renesas,r8a774c0-sysc", .data
= &r8a774c0_sysc_info
},
292 #ifdef CONFIG_SYSC_R8A774E1
293 { .compatible
= "renesas,r8a774e1-sysc", .data
= &r8a774e1_sysc_info
},
295 #ifdef CONFIG_SYSC_R8A7779
296 { .compatible
= "renesas,r8a7779-sysc", .data
= &r8a7779_sysc_info
},
298 #ifdef CONFIG_SYSC_R8A7790
299 { .compatible
= "renesas,r8a7790-sysc", .data
= &r8a7790_sysc_info
},
301 #ifdef CONFIG_SYSC_R8A7791
302 { .compatible
= "renesas,r8a7791-sysc", .data
= &r8a7791_sysc_info
},
303 /* R-Car M2-N is identical to R-Car M2-W w.r.t. power domains. */
304 { .compatible
= "renesas,r8a7793-sysc", .data
= &r8a7791_sysc_info
},
306 #ifdef CONFIG_SYSC_R8A7792
307 { .compatible
= "renesas,r8a7792-sysc", .data
= &r8a7792_sysc_info
},
309 #ifdef CONFIG_SYSC_R8A7794
310 { .compatible
= "renesas,r8a7794-sysc", .data
= &r8a7794_sysc_info
},
312 #ifdef CONFIG_SYSC_R8A7795
313 { .compatible
= "renesas,r8a7795-sysc", .data
= &r8a7795_sysc_info
},
315 #ifdef CONFIG_SYSC_R8A77960
316 { .compatible
= "renesas,r8a7796-sysc", .data
= &r8a77960_sysc_info
},
318 #ifdef CONFIG_SYSC_R8A77961
319 { .compatible
= "renesas,r8a77961-sysc", .data
= &r8a77961_sysc_info
},
321 #ifdef CONFIG_SYSC_R8A77965
322 { .compatible
= "renesas,r8a77965-sysc", .data
= &r8a77965_sysc_info
},
324 #ifdef CONFIG_SYSC_R8A77970
325 { .compatible
= "renesas,r8a77970-sysc", .data
= &r8a77970_sysc_info
},
327 #ifdef CONFIG_SYSC_R8A77980
328 { .compatible
= "renesas,r8a77980-sysc", .data
= &r8a77980_sysc_info
},
330 #ifdef CONFIG_SYSC_R8A77990
331 { .compatible
= "renesas,r8a77990-sysc", .data
= &r8a77990_sysc_info
},
333 #ifdef CONFIG_SYSC_R8A77995
334 { .compatible
= "renesas,r8a77995-sysc", .data
= &r8a77995_sysc_info
},
339 struct rcar_pm_domains
{
340 struct genpd_onecell_data onecell_data
;
341 struct generic_pm_domain
*domains
[RCAR_PD_ALWAYS_ON
+ 1];
344 static struct genpd_onecell_data
*rcar_sysc_onecell_data
;
346 static int __init
rcar_sysc_pd_init(void)
348 const struct rcar_sysc_info
*info
;
349 const struct of_device_id
*match
;
350 struct rcar_pm_domains
*domains
;
351 struct device_node
*np
;
356 np
= of_find_matching_node_and_match(NULL
, rcar_sysc_matches
, &match
);
363 error
= info
->init();
368 has_cpg_mstp
= of_find_compatible_node(NULL
, NULL
,
369 "renesas,cpg-mstp-clocks");
371 base
= of_iomap(np
, 0);
373 pr_warn("%pOF: Cannot map regs\n", np
);
378 rcar_sysc_base
= base
;
380 /* Optional External Request Mask Register */
381 rcar_sysc_extmask_offs
= info
->extmask_offs
;
382 rcar_sysc_extmask_val
= info
->extmask_val
;
384 domains
= kzalloc(sizeof(*domains
), GFP_KERNEL
);
390 domains
->onecell_data
.domains
= domains
->domains
;
391 domains
->onecell_data
.num_domains
= ARRAY_SIZE(domains
->domains
);
392 rcar_sysc_onecell_data
= &domains
->onecell_data
;
394 for (i
= 0; i
< info
->num_areas
; i
++) {
395 const struct rcar_sysc_area
*area
= &info
->areas
[i
];
396 struct rcar_sysc_pd
*pd
;
400 /* Skip NULLified area */
404 n
= strlen(area
->name
) + 1;
405 pd
= kzalloc(sizeof(*pd
) + n
, GFP_KERNEL
);
411 memcpy(pd
->name
, area
->name
, n
);
412 pd
->genpd
.name
= pd
->name
;
413 pd
->chan_offs
= area
->chan_offs
;
414 pd
->chan_bit
= area
->chan_bit
;
415 pd
->isr_bit
= area
->isr_bit
;
416 pd
->flags
= area
->flags
;
418 error
= rcar_sysc_pd_setup(pd
);
422 domains
->domains
[area
->isr_bit
] = &pd
->genpd
;
424 if (area
->parent
< 0)
427 error
= pm_genpd_add_subdomain(domains
->domains
[area
->parent
],
430 pr_warn("Failed to add PM subdomain %s to parent %u\n",
431 area
->name
, area
->parent
);
436 error
= of_genpd_add_provider_onecell(np
, &domains
->onecell_data
);
438 fwnode_dev_initialized(of_fwnode_handle(np
), true);
444 early_initcall(rcar_sysc_pd_init
);
446 #ifdef CONFIG_ARCH_R8A7779
447 static int rcar_sysc_power_cpu(unsigned int idx
, bool on
)
449 struct generic_pm_domain
*genpd
;
450 struct rcar_sysc_pd
*pd
;
453 if (!rcar_sysc_onecell_data
)
456 for (i
= 0; i
< rcar_sysc_onecell_data
->num_domains
; i
++) {
457 genpd
= rcar_sysc_onecell_data
->domains
[i
];
461 pd
= to_rcar_pd(genpd
);
462 if (!(pd
->flags
& PD_CPU
) || pd
->chan_bit
!= idx
)
465 return rcar_sysc_power(pd
, on
);
471 int rcar_sysc_power_down_cpu(unsigned int cpu
)
473 return rcar_sysc_power_cpu(cpu
, false);
476 int rcar_sysc_power_up_cpu(unsigned int cpu
)
478 return rcar_sysc_power_cpu(cpu
, true);
480 #endif /* CONFIG_ARCH_R8A7779 */