Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * R-Car SYSC Power management support |
| 3 | * |
| 4 | * Copyright (C) 2014 Magnus Damm |
Geert Uytterhoeven | afa6f53 | 2017-03-31 11:01:55 +0200 | [diff] [blame] | 5 | * Copyright (C) 2015-2017 Glider bvba |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
| 11 | |
Geert Uytterhoeven | 1c8c77f | 2016-04-20 14:02:40 +0200 | [diff] [blame] | 12 | #include <linux/clk/renesas.h> |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 13 | #include <linux/delay.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/mm.h> |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 16 | #include <linux/of_address.h> |
| 17 | #include <linux/pm_domain.h> |
| 18 | #include <linux/slab.h> |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Dan Williams | 2584cf8 | 2015-08-10 23:07:05 -0400 | [diff] [blame] | 20 | #include <linux/io.h> |
Geert Uytterhoeven | be32bcb | 2016-04-20 14:02:36 +0200 | [diff] [blame] | 21 | #include <linux/soc/renesas/rcar-sysc.h> |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 22 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 23 | #include "rcar-sysc.h" |
| 24 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 25 | /* SYSC Common */ |
| 26 | #define SYSCSR 0x00 /* SYSC Status Register */ |
| 27 | #define SYSCISR 0x04 /* Interrupt Status Register */ |
| 28 | #define SYSCISCR 0x08 /* Interrupt Status Clear Register */ |
| 29 | #define SYSCIER 0x0c /* Interrupt Enable Register */ |
| 30 | #define SYSCIMR 0x10 /* Interrupt Mask Register */ |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 31 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 32 | /* SYSC Status Register */ |
| 33 | #define SYSCSR_PONENB 1 /* Ready for power resume requests */ |
| 34 | #define SYSCSR_POFFENB 0 /* Ready for power shutoff requests */ |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 35 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 36 | /* |
| 37 | * Power Control Register Offsets inside the register block for each domain |
| 38 | * Note: The "CR" registers for ARM cores exist on H1 only |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 39 | * Use WFI to power off, CPG/APMU to resume ARM cores on R-Car Gen2 |
| 40 | * Use PSCI on R-Car Gen3 |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 41 | */ |
| 42 | #define PWRSR_OFFS 0x00 /* Power Status Register */ |
| 43 | #define PWROFFCR_OFFS 0x04 /* Power Shutoff Control Register */ |
| 44 | #define PWROFFSR_OFFS 0x08 /* Power Shutoff Status Register */ |
| 45 | #define PWRONCR_OFFS 0x0c /* Power Resume Control Register */ |
| 46 | #define PWRONSR_OFFS 0x10 /* Power Resume Status Register */ |
| 47 | #define PWRER_OFFS 0x14 /* Power Shutoff/Resume Error */ |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 48 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 49 | |
| 50 | #define SYSCSR_RETRIES 100 |
| 51 | #define SYSCSR_DELAY_US 1 |
| 52 | |
Geert Uytterhoeven | 2f575fc | 2015-06-04 20:22:29 +0200 | [diff] [blame] | 53 | #define PWRER_RETRIES 100 |
| 54 | #define PWRER_DELAY_US 1 |
| 55 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 56 | #define SYSCISR_RETRIES 1000 |
| 57 | #define SYSCISR_DELAY_US 1 |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 58 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 59 | #define RCAR_PD_ALWAYS_ON 32 /* Always-on power area */ |
| 60 | |
Magnus Damm | c4ca5d8 | 2014-02-24 14:52:12 +0900 | [diff] [blame] | 61 | static void __iomem *rcar_sysc_base; |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 62 | static DEFINE_SPINLOCK(rcar_sysc_lock); /* SMP CPUs + I/O devices */ |
| 63 | |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 64 | static int rcar_sysc_pwr_on_off(const struct rcar_sysc_ch *sysc_ch, bool on) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 65 | { |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 66 | unsigned int sr_bit, reg_offs; |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 67 | int k; |
| 68 | |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 69 | if (on) { |
| 70 | sr_bit = SYSCSR_PONENB; |
| 71 | reg_offs = PWRONCR_OFFS; |
| 72 | } else { |
| 73 | sr_bit = SYSCSR_POFFENB; |
| 74 | reg_offs = PWROFFCR_OFFS; |
| 75 | } |
| 76 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 77 | /* Wait until SYSC is ready to accept a power request */ |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 78 | for (k = 0; k < SYSCSR_RETRIES; k++) { |
Geert Uytterhoeven | 21437c5 | 2015-06-04 20:22:31 +0200 | [diff] [blame] | 79 | if (ioread32(rcar_sysc_base + SYSCSR) & BIT(sr_bit)) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 80 | break; |
| 81 | udelay(SYSCSR_DELAY_US); |
| 82 | } |
| 83 | |
| 84 | if (k == SYSCSR_RETRIES) |
| 85 | return -EAGAIN; |
| 86 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 87 | /* Submit power shutoff or power resume request */ |
Geert Uytterhoeven | 21437c5 | 2015-06-04 20:22:31 +0200 | [diff] [blame] | 88 | iowrite32(BIT(sysc_ch->chan_bit), |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 89 | rcar_sysc_base + sysc_ch->chan_offs + reg_offs); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 94 | static int rcar_sysc_power(const struct rcar_sysc_ch *sysc_ch, bool on) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 95 | { |
Geert Uytterhoeven | 21437c5 | 2015-06-04 20:22:31 +0200 | [diff] [blame] | 96 | unsigned int isr_mask = BIT(sysc_ch->isr_bit); |
| 97 | unsigned int chan_mask = BIT(sysc_ch->chan_bit); |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 98 | unsigned int status; |
| 99 | unsigned long flags; |
| 100 | int ret = 0; |
| 101 | int k; |
| 102 | |
| 103 | spin_lock_irqsave(&rcar_sysc_lock, flags); |
| 104 | |
| 105 | iowrite32(isr_mask, rcar_sysc_base + SYSCISCR); |
| 106 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 107 | /* Submit power shutoff or resume request until it was accepted */ |
Geert Uytterhoeven | 2f575fc | 2015-06-04 20:22:29 +0200 | [diff] [blame] | 108 | for (k = 0; k < PWRER_RETRIES; k++) { |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 109 | ret = rcar_sysc_pwr_on_off(sysc_ch, on); |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 110 | if (ret) |
| 111 | goto out; |
| 112 | |
| 113 | status = ioread32(rcar_sysc_base + |
| 114 | sysc_ch->chan_offs + PWRER_OFFS); |
Geert Uytterhoeven | 2f575fc | 2015-06-04 20:22:29 +0200 | [diff] [blame] | 115 | if (!(status & chan_mask)) |
| 116 | break; |
| 117 | |
| 118 | udelay(PWRER_DELAY_US); |
| 119 | } |
| 120 | |
| 121 | if (k == PWRER_RETRIES) { |
| 122 | ret = -EIO; |
| 123 | goto out; |
| 124 | } |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 125 | |
Geert Uytterhoeven | 577d104 | 2015-06-04 20:22:27 +0200 | [diff] [blame] | 126 | /* Wait until the power shutoff or resume request has completed * */ |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 127 | for (k = 0; k < SYSCISR_RETRIES; k++) { |
| 128 | if (ioread32(rcar_sysc_base + SYSCISR) & isr_mask) |
| 129 | break; |
| 130 | udelay(SYSCISR_DELAY_US); |
| 131 | } |
| 132 | |
| 133 | if (k == SYSCISR_RETRIES) |
| 134 | ret = -EIO; |
| 135 | |
| 136 | iowrite32(isr_mask, rcar_sysc_base + SYSCISCR); |
| 137 | |
| 138 | out: |
| 139 | spin_unlock_irqrestore(&rcar_sysc_lock, flags); |
| 140 | |
Geert Uytterhoeven | 68667ce | 2016-04-20 14:02:37 +0200 | [diff] [blame] | 141 | pr_debug("sysc power %s domain %d: %08x -> %d\n", on ? "on" : "off", |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 142 | sysc_ch->isr_bit, ioread32(rcar_sysc_base + SYSCISR), ret); |
| 143 | return ret; |
| 144 | } |
| 145 | |
Geert Uytterhoeven | 624deb3 | 2015-06-04 20:22:30 +0200 | [diff] [blame] | 146 | int rcar_sysc_power_down(const struct rcar_sysc_ch *sysc_ch) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 147 | { |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 148 | return rcar_sysc_power(sysc_ch, false); |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 149 | } |
| 150 | |
Geert Uytterhoeven | 624deb3 | 2015-06-04 20:22:30 +0200 | [diff] [blame] | 151 | int rcar_sysc_power_up(const struct rcar_sysc_ch *sysc_ch) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 152 | { |
Geert Uytterhoeven | bcb8243 | 2015-06-04 20:22:32 +0200 | [diff] [blame] | 153 | return rcar_sysc_power(sysc_ch, true); |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Geert Uytterhoeven | 2f024ce | 2016-04-20 14:02:39 +0200 | [diff] [blame] | 156 | static bool rcar_sysc_power_is_off(const struct rcar_sysc_ch *sysc_ch) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 157 | { |
| 158 | unsigned int st; |
| 159 | |
| 160 | st = ioread32(rcar_sysc_base + sysc_ch->chan_offs + PWRSR_OFFS); |
Geert Uytterhoeven | 21437c5 | 2015-06-04 20:22:31 +0200 | [diff] [blame] | 161 | if (st & BIT(sysc_ch->chan_bit)) |
Magnus Damm | a6557eb | 2014-01-15 16:43:08 +0900 | [diff] [blame] | 162 | return true; |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 167 | struct rcar_sysc_pd { |
| 168 | struct generic_pm_domain genpd; |
| 169 | struct rcar_sysc_ch ch; |
| 170 | unsigned int flags; |
| 171 | char name[0]; |
| 172 | }; |
| 173 | |
| 174 | static inline struct rcar_sysc_pd *to_rcar_pd(struct generic_pm_domain *d) |
| 175 | { |
| 176 | return container_of(d, struct rcar_sysc_pd, genpd); |
| 177 | } |
| 178 | |
| 179 | static int rcar_sysc_pd_power_off(struct generic_pm_domain *genpd) |
| 180 | { |
| 181 | struct rcar_sysc_pd *pd = to_rcar_pd(genpd); |
| 182 | |
| 183 | pr_debug("%s: %s\n", __func__, genpd->name); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 184 | return rcar_sysc_power_down(&pd->ch); |
| 185 | } |
| 186 | |
| 187 | static int rcar_sysc_pd_power_on(struct generic_pm_domain *genpd) |
| 188 | { |
| 189 | struct rcar_sysc_pd *pd = to_rcar_pd(genpd); |
| 190 | |
| 191 | pr_debug("%s: %s\n", __func__, genpd->name); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 192 | return rcar_sysc_power_up(&pd->ch); |
| 193 | } |
| 194 | |
Geert Uytterhoeven | 1c8c77f | 2016-04-20 14:02:40 +0200 | [diff] [blame] | 195 | static bool has_cpg_mstp; |
| 196 | |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 197 | static int __init rcar_sysc_pd_setup(struct rcar_sysc_pd *pd) |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 198 | { |
| 199 | struct generic_pm_domain *genpd = &pd->genpd; |
| 200 | const char *name = pd->genpd.name; |
| 201 | struct dev_power_governor *gov = &simple_qos_governor; |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 202 | int error; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 203 | |
| 204 | if (pd->flags & PD_CPU) { |
| 205 | /* |
| 206 | * This domain contains a CPU core and therefore it should |
| 207 | * only be turned off if the CPU is not in use. |
| 208 | */ |
| 209 | pr_debug("PM domain %s contains %s\n", name, "CPU"); |
Geert Uytterhoeven | 980532a | 2017-06-12 11:23:45 +0200 | [diff] [blame] | 210 | genpd->flags |= GENPD_FLAG_ALWAYS_ON; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 211 | } else if (pd->flags & PD_SCU) { |
| 212 | /* |
| 213 | * This domain contains an SCU and cache-controller, and |
| 214 | * therefore it should only be turned off if the CPU cores are |
| 215 | * not in use. |
| 216 | */ |
| 217 | pr_debug("PM domain %s contains %s\n", name, "SCU"); |
Geert Uytterhoeven | 980532a | 2017-06-12 11:23:45 +0200 | [diff] [blame] | 218 | genpd->flags |= GENPD_FLAG_ALWAYS_ON; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 219 | } else if (pd->flags & PD_NO_CR) { |
| 220 | /* |
| 221 | * This domain cannot be turned off. |
| 222 | */ |
Geert Uytterhoeven | 980532a | 2017-06-12 11:23:45 +0200 | [diff] [blame] | 223 | genpd->flags |= GENPD_FLAG_ALWAYS_ON; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Geert Uytterhoeven | 1c8c77f | 2016-04-20 14:02:40 +0200 | [diff] [blame] | 226 | if (!(pd->flags & (PD_CPU | PD_SCU))) { |
| 227 | /* Enable Clock Domain for I/O devices */ |
Geert Uytterhoeven | 91c719f | 2017-11-09 14:27:02 +0100 | [diff] [blame] | 228 | genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP; |
Geert Uytterhoeven | 1c8c77f | 2016-04-20 14:02:40 +0200 | [diff] [blame] | 229 | if (has_cpg_mstp) { |
| 230 | genpd->attach_dev = cpg_mstp_attach_dev; |
| 231 | genpd->detach_dev = cpg_mstp_detach_dev; |
| 232 | } else { |
| 233 | genpd->attach_dev = cpg_mssr_attach_dev; |
| 234 | genpd->detach_dev = cpg_mssr_detach_dev; |
| 235 | } |
| 236 | } |
| 237 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 238 | genpd->power_off = rcar_sysc_pd_power_off; |
| 239 | genpd->power_on = rcar_sysc_pd_power_on; |
| 240 | |
| 241 | if (pd->flags & (PD_CPU | PD_NO_CR)) { |
| 242 | /* Skip CPUs (handled by SMP code) and areas without control */ |
| 243 | pr_debug("%s: Not touching %s\n", __func__, genpd->name); |
| 244 | goto finalize; |
| 245 | } |
| 246 | |
| 247 | if (!rcar_sysc_power_is_off(&pd->ch)) { |
| 248 | pr_debug("%s: %s is already powered\n", __func__, genpd->name); |
| 249 | goto finalize; |
| 250 | } |
| 251 | |
| 252 | rcar_sysc_power_up(&pd->ch); |
| 253 | |
| 254 | finalize: |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 255 | error = pm_genpd_init(genpd, gov, false); |
| 256 | if (error) |
| 257 | pr_err("Failed to init PM domain %s: %d\n", name, error); |
| 258 | |
| 259 | return error; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Geert Uytterhoeven | 707aa45 | 2017-12-19 16:54:44 +0100 | [diff] [blame] | 262 | static const struct of_device_id rcar_sysc_matches[] __initconst = { |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 263 | #ifdef CONFIG_SYSC_R8A7743 |
Sergei Shtylyov | 603311b | 2016-10-05 14:35:01 -0700 | [diff] [blame] | 264 | { .compatible = "renesas,r8a7743-sysc", .data = &r8a7743_sysc_info }, |
| 265 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 266 | #ifdef CONFIG_SYSC_R8A7745 |
Sergei Shtylyov | 141723e | 2016-11-05 00:46:13 +0300 | [diff] [blame] | 267 | { .compatible = "renesas,r8a7745-sysc", .data = &r8a7745_sysc_info }, |
| 268 | #endif |
Biju Das | 964f7c0 | 2018-03-28 20:26:09 +0100 | [diff] [blame] | 269 | #ifdef CONFIG_SYSC_R8A77470 |
| 270 | { .compatible = "renesas,r8a77470-sysc", .data = &r8a77470_sysc_info }, |
| 271 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 272 | #ifdef CONFIG_SYSC_R8A7779 |
Geert Uytterhoeven | 9b83ea1 | 2016-04-20 14:02:41 +0200 | [diff] [blame] | 273 | { .compatible = "renesas,r8a7779-sysc", .data = &r8a7779_sysc_info }, |
| 274 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 275 | #ifdef CONFIG_SYSC_R8A7790 |
Geert Uytterhoeven | ad7c9db | 2016-04-20 14:02:42 +0200 | [diff] [blame] | 276 | { .compatible = "renesas,r8a7790-sysc", .data = &r8a7790_sysc_info }, |
| 277 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 278 | #ifdef CONFIG_SYSC_R8A7791 |
Geert Uytterhoeven | c5fbb3c | 2016-04-20 14:02:43 +0200 | [diff] [blame] | 279 | { .compatible = "renesas,r8a7791-sysc", .data = &r8a7791_sysc_info }, |
Geert Uytterhoeven | a247eb9 | 2016-04-20 14:02:44 +0200 | [diff] [blame] | 280 | /* R-Car M2-N is identical to R-Car M2-W w.r.t. power domains. */ |
| 281 | { .compatible = "renesas,r8a7793-sysc", .data = &r8a7791_sysc_info }, |
| 282 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 283 | #ifdef CONFIG_SYSC_R8A7792 |
| 284 | { .compatible = "renesas,r8a7792-sysc", .data = &r8a7792_sysc_info }, |
| 285 | #endif |
| 286 | #ifdef CONFIG_SYSC_R8A7794 |
Geert Uytterhoeven | 9af1dbc | 2016-04-20 14:02:45 +0200 | [diff] [blame] | 287 | { .compatible = "renesas,r8a7794-sysc", .data = &r8a7794_sysc_info }, |
| 288 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 289 | #ifdef CONFIG_SYSC_R8A7795 |
Geert Uytterhoeven | 23f1e2e | 2016-04-20 14:02:46 +0200 | [diff] [blame] | 290 | { .compatible = "renesas,r8a7795-sysc", .data = &r8a7795_sysc_info }, |
| 291 | #endif |
Geert Uytterhoeven | 8be381a | 2017-05-19 10:35:10 +0200 | [diff] [blame] | 292 | #ifdef CONFIG_SYSC_R8A7796 |
Geert Uytterhoeven | e0c98b9 | 2016-05-30 19:05:11 +0200 | [diff] [blame] | 293 | { .compatible = "renesas,r8a7796-sysc", .data = &r8a7796_sysc_info }, |
| 294 | #endif |
Jacopo Mondi | a527709 | 2018-02-20 16:12:06 +0100 | [diff] [blame] | 295 | #ifdef CONFIG_SYSC_R8A77965 |
| 296 | { .compatible = "renesas,r8a77965-sysc", .data = &r8a77965_sysc_info }, |
| 297 | #endif |
Sergei Shtylyov | bab9b2a | 2017-09-12 23:37:20 +0300 | [diff] [blame] | 298 | #ifdef CONFIG_SYSC_R8A77970 |
| 299 | { .compatible = "renesas,r8a77970-sysc", .data = &r8a77970_sysc_info }, |
| 300 | #endif |
Sergei Shtylyov | 41d6d8b | 2018-02-16 21:28:02 +0300 | [diff] [blame] | 301 | #ifdef CONFIG_SYSC_R8A77980 |
| 302 | { .compatible = "renesas,r8a77980-sysc", .data = &r8a77980_sysc_info }, |
| 303 | #endif |
Takeshi Kihara | 44b12d4 | 2018-05-15 21:07:38 +0900 | [diff] [blame] | 304 | #ifdef CONFIG_SYSC_R8A77990 |
| 305 | { .compatible = "renesas,r8a77990-sysc", .data = &r8a77990_sysc_info }, |
| 306 | #endif |
Geert Uytterhoeven | eed17c4 | 2017-07-20 14:34:53 +0200 | [diff] [blame] | 307 | #ifdef CONFIG_SYSC_R8A77995 |
| 308 | { .compatible = "renesas,r8a77995-sysc", .data = &r8a77995_sysc_info }, |
| 309 | #endif |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 310 | { /* sentinel */ } |
| 311 | }; |
| 312 | |
| 313 | struct rcar_pm_domains { |
| 314 | struct genpd_onecell_data onecell_data; |
| 315 | struct generic_pm_domain *domains[RCAR_PD_ALWAYS_ON + 1]; |
| 316 | }; |
| 317 | |
| 318 | static int __init rcar_sysc_pd_init(void) |
| 319 | { |
| 320 | const struct rcar_sysc_info *info; |
| 321 | const struct of_device_id *match; |
| 322 | struct rcar_pm_domains *domains; |
| 323 | struct device_node *np; |
| 324 | u32 syscier, syscimr; |
| 325 | void __iomem *base; |
| 326 | unsigned int i; |
| 327 | int error; |
| 328 | |
Geert Uytterhoeven | b1e5228 | 2016-06-28 16:10:32 +0200 | [diff] [blame] | 329 | if (rcar_sysc_base) |
| 330 | return 0; |
| 331 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 332 | np = of_find_matching_node_and_match(NULL, rcar_sysc_matches, &match); |
| 333 | if (!np) |
| 334 | return -ENODEV; |
| 335 | |
| 336 | info = match->data; |
| 337 | |
Geert Uytterhoeven | afa6f53 | 2017-03-31 11:01:55 +0200 | [diff] [blame] | 338 | if (info->init) { |
| 339 | error = info->init(); |
| 340 | if (error) |
| 341 | return error; |
| 342 | } |
| 343 | |
Geert Uytterhoeven | 1c8c77f | 2016-04-20 14:02:40 +0200 | [diff] [blame] | 344 | has_cpg_mstp = of_find_compatible_node(NULL, NULL, |
| 345 | "renesas,cpg-mstp-clocks"); |
| 346 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 347 | base = of_iomap(np, 0); |
| 348 | if (!base) { |
Rob Herring | 37c342c | 2017-07-18 16:43:29 -0500 | [diff] [blame] | 349 | pr_warn("%pOF: Cannot map regs\n", np); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 350 | error = -ENOMEM; |
| 351 | goto out_put; |
| 352 | } |
| 353 | |
| 354 | rcar_sysc_base = base; |
| 355 | |
| 356 | domains = kzalloc(sizeof(*domains), GFP_KERNEL); |
| 357 | if (!domains) { |
| 358 | error = -ENOMEM; |
| 359 | goto out_put; |
| 360 | } |
| 361 | |
| 362 | domains->onecell_data.domains = domains->domains; |
| 363 | domains->onecell_data.num_domains = ARRAY_SIZE(domains->domains); |
| 364 | |
| 365 | for (i = 0, syscier = 0; i < info->num_areas; i++) |
| 366 | syscier |= BIT(info->areas[i].isr_bit); |
| 367 | |
| 368 | /* |
| 369 | * Mask all interrupt sources to prevent the CPU from receiving them. |
| 370 | * Make sure not to clear reserved bits that were set before. |
| 371 | */ |
| 372 | syscimr = ioread32(base + SYSCIMR); |
| 373 | syscimr |= syscier; |
Rob Herring | 37c342c | 2017-07-18 16:43:29 -0500 | [diff] [blame] | 374 | pr_debug("%pOF: syscimr = 0x%08x\n", np, syscimr); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 375 | iowrite32(syscimr, base + SYSCIMR); |
| 376 | |
| 377 | /* |
| 378 | * SYSC needs all interrupt sources enabled to control power. |
| 379 | */ |
Rob Herring | 37c342c | 2017-07-18 16:43:29 -0500 | [diff] [blame] | 380 | pr_debug("%pOF: syscier = 0x%08x\n", np, syscier); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 381 | iowrite32(syscier, base + SYSCIER); |
| 382 | |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 383 | /* |
| 384 | * First, create all PM domains |
| 385 | */ |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 386 | for (i = 0; i < info->num_areas; i++) { |
| 387 | const struct rcar_sysc_area *area = &info->areas[i]; |
| 388 | struct rcar_sysc_pd *pd; |
| 389 | |
Geert Uytterhoeven | afa6f53 | 2017-03-31 11:01:55 +0200 | [diff] [blame] | 390 | if (!area->name) { |
| 391 | /* Skip NULLified area */ |
| 392 | continue; |
| 393 | } |
| 394 | |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 395 | pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL); |
| 396 | if (!pd) { |
| 397 | error = -ENOMEM; |
| 398 | goto out_put; |
| 399 | } |
| 400 | |
| 401 | strcpy(pd->name, area->name); |
| 402 | pd->genpd.name = pd->name; |
| 403 | pd->ch.chan_offs = area->chan_offs; |
| 404 | pd->ch.chan_bit = area->chan_bit; |
| 405 | pd->ch.isr_bit = area->isr_bit; |
| 406 | pd->flags = area->flags; |
| 407 | |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 408 | error = rcar_sysc_pd_setup(pd); |
| 409 | if (error) |
| 410 | goto out_put; |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 411 | |
| 412 | domains->domains[area->isr_bit] = &pd->genpd; |
| 413 | } |
| 414 | |
Geert Uytterhoeven | 977d5ba | 2018-06-05 17:05:15 +0200 | [diff] [blame] | 415 | /* |
| 416 | * Second, link all PM domains to their parents |
| 417 | */ |
| 418 | for (i = 0; i < info->num_areas; i++) { |
| 419 | const struct rcar_sysc_area *area = &info->areas[i]; |
| 420 | |
| 421 | if (!area->name || area->parent < 0) |
| 422 | continue; |
| 423 | |
| 424 | error = pm_genpd_add_subdomain(domains->domains[area->parent], |
| 425 | domains->domains[area->isr_bit]); |
| 426 | if (error) |
| 427 | pr_warn("Failed to add PM subdomain %s to parent %u\n", |
| 428 | area->name, area->parent); |
| 429 | } |
| 430 | |
Geert Uytterhoeven | 1023578 | 2016-06-28 16:10:31 +0200 | [diff] [blame] | 431 | error = of_genpd_add_provider_onecell(np, &domains->onecell_data); |
Geert Uytterhoeven | dcc09fd | 2016-04-20 14:02:38 +0200 | [diff] [blame] | 432 | |
| 433 | out_put: |
| 434 | of_node_put(np); |
| 435 | return error; |
| 436 | } |
| 437 | early_initcall(rcar_sysc_pd_init); |
Geert Uytterhoeven | b1e5228 | 2016-06-28 16:10:32 +0200 | [diff] [blame] | 438 | |
Geert Uytterhoeven | afa6f53 | 2017-03-31 11:01:55 +0200 | [diff] [blame] | 439 | void __init rcar_sysc_nullify(struct rcar_sysc_area *areas, |
| 440 | unsigned int num_areas, u8 id) |
| 441 | { |
| 442 | unsigned int i; |
| 443 | |
| 444 | for (i = 0; i < num_areas; i++) |
| 445 | if (areas[i].isr_bit == id) { |
| 446 | areas[i].name = NULL; |
| 447 | return; |
| 448 | } |
| 449 | } |
| 450 | |
Geert Uytterhoeven | 0532399 | 2016-06-28 16:10:33 +0200 | [diff] [blame] | 451 | void __init rcar_sysc_init(phys_addr_t base, u32 syscier) |
Geert Uytterhoeven | b1e5228 | 2016-06-28 16:10:32 +0200 | [diff] [blame] | 452 | { |
Geert Uytterhoeven | ced4273 | 2016-06-28 16:10:34 +0200 | [diff] [blame] | 453 | u32 syscimr; |
| 454 | |
Geert Uytterhoeven | 0532399 | 2016-06-28 16:10:33 +0200 | [diff] [blame] | 455 | if (!rcar_sysc_pd_init()) |
| 456 | return; |
Geert Uytterhoeven | b1e5228 | 2016-06-28 16:10:32 +0200 | [diff] [blame] | 457 | |
Geert Uytterhoeven | 0532399 | 2016-06-28 16:10:33 +0200 | [diff] [blame] | 458 | rcar_sysc_base = ioremap_nocache(base, PAGE_SIZE); |
| 459 | |
Geert Uytterhoeven | ced4273 | 2016-06-28 16:10:34 +0200 | [diff] [blame] | 460 | /* |
| 461 | * Mask all interrupt sources to prevent the CPU from receiving them. |
| 462 | * Make sure not to clear reserved bits that were set before. |
| 463 | */ |
| 464 | syscimr = ioread32(rcar_sysc_base + SYSCIMR); |
| 465 | syscimr |= syscier; |
| 466 | pr_debug("%s: syscimr = 0x%08x\n", __func__, syscimr); |
| 467 | iowrite32(syscimr, rcar_sysc_base + SYSCIMR); |
| 468 | |
| 469 | /* |
| 470 | * SYSC needs all interrupt sources enabled to control power. |
| 471 | */ |
| 472 | pr_debug("%s: syscier = 0x%08x\n", __func__, syscier); |
Geert Uytterhoeven | 0532399 | 2016-06-28 16:10:33 +0200 | [diff] [blame] | 473 | iowrite32(syscier, rcar_sysc_base + SYSCIER); |
Geert Uytterhoeven | b1e5228 | 2016-06-28 16:10:32 +0200 | [diff] [blame] | 474 | } |