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>
23 #define PWRSR_OFFS 0x00
24 #define PWROFFCR_OFFS 0x04
25 #define PWRONCR_OFFS 0x0c
26 #define PWRER_OFFS 0x14
28 #define SYSCSR_RETRIES 100
29 #define SYSCSR_DELAY_US 1
31 #define SYSCISR_RETRIES 1000
32 #define SYSCISR_DELAY_US 1
34 static void __iomem
*rcar_sysc_base
;
35 static DEFINE_SPINLOCK(rcar_sysc_lock
); /* SMP CPUs + I/O devices */
37 static int rcar_sysc_pwr_on_off(struct rcar_sysc_ch
*sysc_ch
,
38 int sr_bit
, int reg_offs
)
42 for (k
= 0; k
< SYSCSR_RETRIES
; k
++) {
43 if (ioread32(rcar_sysc_base
+ SYSCSR
) & (1 << sr_bit
))
45 udelay(SYSCSR_DELAY_US
);
48 if (k
== SYSCSR_RETRIES
)
51 iowrite32(1 << sysc_ch
->chan_bit
,
52 rcar_sysc_base
+ sysc_ch
->chan_offs
+ reg_offs
);
57 static int rcar_sysc_pwr_off(struct rcar_sysc_ch
*sysc_ch
)
59 return rcar_sysc_pwr_on_off(sysc_ch
, 0, PWROFFCR_OFFS
);
62 static int rcar_sysc_pwr_on(struct rcar_sysc_ch
*sysc_ch
)
64 return rcar_sysc_pwr_on_off(sysc_ch
, 1, PWRONCR_OFFS
);
67 static int rcar_sysc_update(struct rcar_sysc_ch
*sysc_ch
,
68 int (*on_off_fn
)(struct rcar_sysc_ch
*))
70 unsigned int isr_mask
= 1 << sysc_ch
->isr_bit
;
71 unsigned int chan_mask
= 1 << sysc_ch
->chan_bit
;
77 spin_lock_irqsave(&rcar_sysc_lock
, flags
);
79 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
82 ret
= on_off_fn(sysc_ch
);
86 status
= ioread32(rcar_sysc_base
+
87 sysc_ch
->chan_offs
+ PWRER_OFFS
);
88 } while (status
& chan_mask
);
90 for (k
= 0; k
< SYSCISR_RETRIES
; k
++) {
91 if (ioread32(rcar_sysc_base
+ SYSCISR
) & isr_mask
)
93 udelay(SYSCISR_DELAY_US
);
96 if (k
== SYSCISR_RETRIES
)
99 iowrite32(isr_mask
, rcar_sysc_base
+ SYSCISCR
);
102 spin_unlock_irqrestore(&rcar_sysc_lock
, flags
);
104 pr_debug("sysc power domain %d: %08x -> %d\n",
105 sysc_ch
->isr_bit
, ioread32(rcar_sysc_base
+ SYSCISR
), ret
);
109 int rcar_sysc_power_down(struct rcar_sysc_ch
*sysc_ch
)
111 return rcar_sysc_update(sysc_ch
, rcar_sysc_pwr_off
);
114 int rcar_sysc_power_up(struct rcar_sysc_ch
*sysc_ch
)
116 return rcar_sysc_update(sysc_ch
, rcar_sysc_pwr_on
);
119 bool rcar_sysc_power_is_off(struct rcar_sysc_ch
*sysc_ch
)
123 st
= ioread32(rcar_sysc_base
+ sysc_ch
->chan_offs
+ PWRSR_OFFS
);
124 if (st
& (1 << sysc_ch
->chan_bit
))
130 void __iomem
*rcar_sysc_init(phys_addr_t base
)
132 rcar_sysc_base
= ioremap_nocache(base
, PAGE_SIZE
);
134 panic("unable to ioremap R-Car SYSC hardware block\n");
136 return rcar_sysc_base
;