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>
16 #include <mach/pm-rcar.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 #if defined(CONFIG_PM) || defined(CONFIG_SMP)
36 static void __iomem *rcar_sysc_base;
37 static DEFINE_SPINLOCK(rcar_sysc_lock); /* SMP CPUs + I/O devices */
39 static int rcar_sysc_pwr_on_off(struct rcar_sysc_ch *sysc_ch,
40 int sr_bit, int reg_offs)
44 for (k = 0; k < SYSCSR_RETRIES; k++) {
45 if (ioread32(rcar_sysc_base + SYSCSR) & (1 << sr_bit))
47 udelay(SYSCSR_DELAY_US);
50 if (k == SYSCSR_RETRIES)
53 iowrite32(1 << sysc_ch->chan_bit,
54 rcar_sysc_base + sysc_ch->chan_offs + reg_offs);
59 static int rcar_sysc_pwr_off(struct rcar_sysc_ch *sysc_ch)
61 return rcar_sysc_pwr_on_off(sysc_ch, 0, PWROFFCR_OFFS);
64 static int rcar_sysc_pwr_on(struct rcar_sysc_ch *sysc_ch)
66 return rcar_sysc_pwr_on_off(sysc_ch, 1, PWRONCR_OFFS);
69 static int rcar_sysc_update(struct rcar_sysc_ch *sysc_ch,
70 int (*on_off_fn)(struct rcar_sysc_ch *))
72 unsigned int isr_mask = 1 << sysc_ch->isr_bit;
73 unsigned int chan_mask = 1 << sysc_ch->chan_bit;
79 spin_lock_irqsave(&rcar_sysc_lock, flags);
81 iowrite32(isr_mask, rcar_sysc_base + SYSCISCR);
84 ret = on_off_fn(sysc_ch);
88 status = ioread32(rcar_sysc_base +
89 sysc_ch->chan_offs + PWRER_OFFS);
90 } while (status & chan_mask);
92 for (k = 0; k < SYSCISR_RETRIES; k++) {
93 if (ioread32(rcar_sysc_base + SYSCISR) & isr_mask)
95 udelay(SYSCISR_DELAY_US);
98 if (k == SYSCISR_RETRIES)
101 iowrite32(isr_mask, rcar_sysc_base + SYSCISCR);
104 spin_unlock_irqrestore(&rcar_sysc_lock, flags);
106 pr_debug("sysc power domain %d: %08x -> %d\n",
107 sysc_ch->isr_bit, ioread32(rcar_sysc_base + SYSCISR), ret);
111 int rcar_sysc_power_down(struct rcar_sysc_ch *sysc_ch)
113 return rcar_sysc_update(sysc_ch, rcar_sysc_pwr_off);
116 int rcar_sysc_power_up(struct rcar_sysc_ch *sysc_ch)
118 return rcar_sysc_update(sysc_ch, rcar_sysc_pwr_on);
121 bool rcar_sysc_power_is_off(struct rcar_sysc_ch *sysc_ch)
125 st = ioread32(rcar_sysc_base + sysc_ch->chan_offs + PWRSR_OFFS);
126 if (st & (1 << sysc_ch->chan_bit))
132 void __iomem *rcar_sysc_init(phys_addr_t base)
134 rcar_sysc_base = ioremap_nocache(base, PAGE_SIZE);
136 panic("unable to ioremap R-Car SYSC hardware block\n");
138 return rcar_sysc_base;
141 #endif /* CONFIG_PM || CONFIG_SMP */