2 * R-Car SYSC Power management support
4 * Copyright (C) 2014 Magnus Damm
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/delay.h>
12 #include <linux/err.h>
14 #include <linux/spinlock.h>
19 #define SYSCSR 0x00 /* SYSC Status Register */
20 #define SYSCISR 0x04 /* Interrupt Status Register */
21 #define SYSCISCR 0x08 /* Interrupt Status Clear Register */
22 #define SYSCIER 0x0c /* Interrupt Enable Register */
23 #define SYSCIMR 0x10 /* Interrupt Mask Register */
25 /* SYSC Status Register */
26 #define SYSCSR_PONENB 1 /* Ready for power resume requests */
27 #define SYSCSR_POFFENB 0 /* Ready for power shutoff requests */
30 * Power Control Register Offsets inside the register block for each domain
31 * Note: The "CR" registers for ARM cores exist on H1 only
32 * Use WFI to power off, CPG/APMU to resume ARM cores on R-Car Gen2
34 #define PWRSR_OFFS 0x00 /* Power Status Register */
35 #define PWROFFCR_OFFS 0x04 /* Power Shutoff Control Register */
36 #define PWROFFSR_OFFS 0x08 /* Power Shutoff Status Register */
37 #define PWRONCR_OFFS 0x0c /* Power Resume Control Register */
38 #define PWRONSR_OFFS 0x10 /* Power Resume Status Register */
39 #define PWRER_OFFS 0x14 /* Power Shutoff/Resume Error */
42 #define SYSCSR_RETRIES 100
43 #define SYSCSR_DELAY_US 1
45 #define PWRER_RETRIES 100
46 #define PWRER_DELAY_US 1
48 #define SYSCISR_RETRIES 1000
49 #define SYSCISR_DELAY_US 1
51 static void __iomem
*rcar_sysc_base
;
52 static DEFINE_SPINLOCK(rcar_sysc_lock
); /* SMP CPUs + I/O devices */
54 static int rcar_sysc_pwr_on_off(const struct rcar_sysc_ch
*sysc_ch
, bool on
)
56 unsigned int sr_bit
, reg_offs
;
60 sr_bit
= SYSCSR_PONENB
;
61 reg_offs
= PWRONCR_OFFS
;
63 sr_bit
= SYSCSR_POFFENB
;
64 reg_offs
= PWROFFCR_OFFS
;
67 /* Wait until SYSC is ready to accept a power request */
68 for (k
= 0; k
< SYSCSR_RETRIES
; k
++) {
69 if (ioread32(rcar_sysc_base
+ SYSCSR
) & BIT(sr_bit
))
71 udelay(SYSCSR_DELAY_US
);
74 if (k
== SYSCSR_RETRIES
)
77 /* Submit power shutoff or power resume request */
78 iowrite32(BIT(sysc_ch
->chan_bit
),
79 rcar_sysc_base
+ sysc_ch
->chan_offs
+ reg_offs
);
84 static int rcar_sysc_power(const struct rcar_sysc_ch
*sysc_ch
, bool on
)
86 unsigned int isr_mask
= BIT(sysc_ch
->isr_bit
);
87 unsigned int chan_mask
= BIT(sysc_ch
->chan_bit
);
93 spin_lock_irqsave(&rcar_sysc_lock
, flags
);
95 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
97 /* Submit power shutoff or resume request until it was accepted */
98 for (k
= 0; k
< PWRER_RETRIES
; k
++) {
99 ret
= rcar_sysc_pwr_on_off(sysc_ch
, on
);
103 status
= ioread32(rcar_sysc_base
+
104 sysc_ch
->chan_offs
+ PWRER_OFFS
);
105 if (!(status
& chan_mask
))
108 udelay(PWRER_DELAY_US
);
111 if (k
== PWRER_RETRIES
) {
116 /* Wait until the power shutoff or resume request has completed * */
117 for (k
= 0; k
< SYSCISR_RETRIES
; k
++) {
118 if (ioread32(rcar_sysc_base
+ SYSCISR
) & isr_mask
)
120 udelay(SYSCISR_DELAY_US
);
123 if (k
== SYSCISR_RETRIES
)
126 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
129 spin_unlock_irqrestore(&rcar_sysc_lock
, flags
);
131 pr_debug("sysc power domain %d: %08x -> %d\n",
132 sysc_ch
->isr_bit
, ioread32(rcar_sysc_base
+ SYSCISR
), ret
);
136 int rcar_sysc_power_down(const struct rcar_sysc_ch
*sysc_ch
)
138 return rcar_sysc_power(sysc_ch
, false);
141 int rcar_sysc_power_up(const struct rcar_sysc_ch
*sysc_ch
)
143 return rcar_sysc_power(sysc_ch
, true);
146 bool rcar_sysc_power_is_off(const struct rcar_sysc_ch
*sysc_ch
)
150 st
= ioread32(rcar_sysc_base
+ sysc_ch
->chan_offs
+ PWRSR_OFFS
);
151 if (st
& BIT(sysc_ch
->chan_bit
))
157 void __iomem
*rcar_sysc_init(phys_addr_t base
)
159 rcar_sysc_base
= ioremap_nocache(base
, PAGE_SIZE
);
161 panic("unable to ioremap R-Car SYSC hardware block\n");
163 return rcar_sysc_base
;