Merge tag 'jfs-5.2' of git://github.com/kleikamp/linux-shaggy
[linux-2.6-block.git] / drivers / soc / renesas / rmobile-sysc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * rmobile power management support
4  *
5  * Copyright (C) 2012  Renesas Solutions Corp.
6  * Copyright (C) 2012  Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  * Copyright (C) 2014  Glider bvba
8  *
9  * based on pm-sh7372.c
10  *  Copyright (C) 2011 Magnus Damm
11  */
12 #include <linux/clk/renesas.h>
13 #include <linux/console.h>
14 #include <linux/delay.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/pm_clock.h>
21 #include <linux/pm_domain.h>
22 #include <linux/slab.h>
23
24 #include <asm/io.h>
25
26 /* SYSC */
27 #define SPDCR           0x08    /* SYS Power Down Control Register */
28 #define SWUCR           0x14    /* SYS Wakeup Control Register */
29 #define PSTR            0x80    /* Power Status Register */
30
31 #define PSTR_RETRIES    100
32 #define PSTR_DELAY_US   10
33
34 struct rmobile_pm_domain {
35         struct generic_pm_domain genpd;
36         struct dev_power_governor *gov;
37         int (*suspend)(void);
38         void __iomem *base;
39         unsigned int bit_shift;
40 };
41
42 static inline
43 struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
44 {
45         return container_of(d, struct rmobile_pm_domain, genpd);
46 }
47
48 static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
49 {
50         struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
51         unsigned int mask;
52
53         if (rmobile_pd->bit_shift == ~0)
54                 return -EBUSY;
55
56         mask = BIT(rmobile_pd->bit_shift);
57         if (rmobile_pd->suspend) {
58                 int ret = rmobile_pd->suspend();
59
60                 if (ret)
61                         return ret;
62         }
63
64         if (__raw_readl(rmobile_pd->base + PSTR) & mask) {
65                 unsigned int retry_count;
66                 __raw_writel(mask, rmobile_pd->base + SPDCR);
67
68                 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
69                         if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask))
70                                 break;
71                         cpu_relax();
72                 }
73         }
74
75         pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n", genpd->name, mask,
76                  __raw_readl(rmobile_pd->base + PSTR));
77
78         return 0;
79 }
80
81 static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd)
82 {
83         unsigned int mask;
84         unsigned int retry_count;
85         int ret = 0;
86
87         if (rmobile_pd->bit_shift == ~0)
88                 return 0;
89
90         mask = BIT(rmobile_pd->bit_shift);
91         if (__raw_readl(rmobile_pd->base + PSTR) & mask)
92                 return ret;
93
94         __raw_writel(mask, rmobile_pd->base + SWUCR);
95
96         for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
97                 if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask))
98                         break;
99                 if (retry_count > PSTR_RETRIES)
100                         udelay(PSTR_DELAY_US);
101                 else
102                         cpu_relax();
103         }
104         if (!retry_count)
105                 ret = -EIO;
106
107         pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
108                  rmobile_pd->genpd.name, mask,
109                  __raw_readl(rmobile_pd->base + PSTR));
110
111         return ret;
112 }
113
114 static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
115 {
116         return __rmobile_pd_power_up(to_rmobile_pd(genpd));
117 }
118
119 static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
120 {
121         struct generic_pm_domain *genpd = &rmobile_pd->genpd;
122         struct dev_power_governor *gov = rmobile_pd->gov;
123
124         genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
125         genpd->power_off                = rmobile_pd_power_down;
126         genpd->power_on                 = rmobile_pd_power_up;
127         genpd->attach_dev               = cpg_mstp_attach_dev;
128         genpd->detach_dev               = cpg_mstp_detach_dev;
129         __rmobile_pd_power_up(rmobile_pd);
130         pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
131 }
132
133 static int rmobile_pd_suspend_console(void)
134 {
135         /*
136          * Serial consoles make use of SCIF hardware located in this domain,
137          * hence keep the power domain on if "no_console_suspend" is set.
138          */
139         return console_suspend_enabled ? 0 : -EBUSY;
140 }
141
142 enum pd_types {
143         PD_NORMAL,
144         PD_CPU,
145         PD_CONSOLE,
146         PD_DEBUG,
147         PD_MEMCTL,
148 };
149
150 #define MAX_NUM_SPECIAL_PDS     16
151
152 static struct special_pd {
153         struct device_node *pd;
154         enum pd_types type;
155 } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
156
157 static unsigned int num_special_pds __initdata;
158
159 static const struct of_device_id special_ids[] __initconst = {
160         { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
161         { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
162         { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
163         { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
164         { /* sentinel */ },
165 };
166
167 static void __init add_special_pd(struct device_node *np, enum pd_types type)
168 {
169         unsigned int i;
170         struct device_node *pd;
171
172         pd = of_parse_phandle(np, "power-domains", 0);
173         if (!pd)
174                 return;
175
176         for (i = 0; i < num_special_pds; i++)
177                 if (pd == special_pds[i].pd && type == special_pds[i].type) {
178                         of_node_put(pd);
179                         return;
180                 }
181
182         if (num_special_pds == ARRAY_SIZE(special_pds)) {
183                 pr_warn("Too many special PM domains\n");
184                 of_node_put(pd);
185                 return;
186         }
187
188         pr_debug("Special PM domain %pOFn type %d for %pOF\n", pd, type, np);
189
190         special_pds[num_special_pds].pd = pd;
191         special_pds[num_special_pds].type = type;
192         num_special_pds++;
193 }
194
195 static void __init get_special_pds(void)
196 {
197         struct device_node *np;
198         const struct of_device_id *id;
199
200         /* PM domains containing CPUs */
201         for_each_of_cpu_node(np)
202                 add_special_pd(np, PD_CPU);
203
204         /* PM domain containing console */
205         if (of_stdout)
206                 add_special_pd(of_stdout, PD_CONSOLE);
207
208         /* PM domains containing other special devices */
209         for_each_matching_node_and_match(np, special_ids, &id)
210                 add_special_pd(np, (enum pd_types)id->data);
211 }
212
213 static void __init put_special_pds(void)
214 {
215         unsigned int i;
216
217         for (i = 0; i < num_special_pds; i++)
218                 of_node_put(special_pds[i].pd);
219 }
220
221 static enum pd_types __init pd_type(const struct device_node *pd)
222 {
223         unsigned int i;
224
225         for (i = 0; i < num_special_pds; i++)
226                 if (pd == special_pds[i].pd)
227                         return special_pds[i].type;
228
229         return PD_NORMAL;
230 }
231
232 static void __init rmobile_setup_pm_domain(struct device_node *np,
233                                            struct rmobile_pm_domain *pd)
234 {
235         const char *name = pd->genpd.name;
236
237         switch (pd_type(np)) {
238         case PD_CPU:
239                 /*
240                  * This domain contains the CPU core and therefore it should
241                  * only be turned off if the CPU is not in use.
242                  */
243                 pr_debug("PM domain %s contains CPU\n", name);
244                 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
245                 break;
246
247         case PD_CONSOLE:
248                 pr_debug("PM domain %s contains serial console\n", name);
249                 pd->gov = &pm_domain_always_on_gov;
250                 pd->suspend = rmobile_pd_suspend_console;
251                 break;
252
253         case PD_DEBUG:
254                 /*
255                  * This domain contains the Coresight-ETM hardware block and
256                  * therefore it should only be turned off if the debug module
257                  * is not in use.
258                  */
259                 pr_debug("PM domain %s contains Coresight-ETM\n", name);
260                 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
261                 break;
262
263         case PD_MEMCTL:
264                 /*
265                  * This domain contains a memory-controller and therefore it
266                  * should only be turned off if memory is not in use.
267                  */
268                 pr_debug("PM domain %s contains MEMCTL\n", name);
269                 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
270                 break;
271
272         case PD_NORMAL:
273                 break;
274         }
275
276         rmobile_init_pm_domain(pd);
277 }
278
279 static int __init rmobile_add_pm_domains(void __iomem *base,
280                                          struct device_node *parent,
281                                          struct generic_pm_domain *genpd_parent)
282 {
283         struct device_node *np;
284
285         for_each_child_of_node(parent, np) {
286                 struct rmobile_pm_domain *pd;
287                 u32 idx = ~0;
288
289                 if (of_property_read_u32(np, "reg", &idx)) {
290                         /* always-on domain */
291                 }
292
293                 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
294                 if (!pd) {
295                         of_node_put(np);
296                         return -ENOMEM;
297                 }
298
299                 pd->genpd.name = np->name;
300                 pd->base = base;
301                 pd->bit_shift = idx;
302
303                 rmobile_setup_pm_domain(np, pd);
304                 if (genpd_parent)
305                         pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
306                 of_genpd_add_provider_simple(np, &pd->genpd);
307
308                 rmobile_add_pm_domains(base, np, &pd->genpd);
309         }
310         return 0;
311 }
312
313 static int __init rmobile_init_pm_domains(void)
314 {
315         struct device_node *np, *pmd;
316         bool scanned = false;
317         void __iomem *base;
318         int ret = 0;
319
320         for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
321                 base = of_iomap(np, 0);
322                 if (!base) {
323                         pr_warn("%pOF cannot map reg 0\n", np);
324                         continue;
325                 }
326
327                 pmd = of_get_child_by_name(np, "pm-domains");
328                 if (!pmd) {
329                         pr_warn("%pOF lacks pm-domains node\n", np);
330                         continue;
331                 }
332
333                 if (!scanned) {
334                         /* Find PM domains containing special blocks */
335                         get_special_pds();
336                         scanned = true;
337                 }
338
339                 ret = rmobile_add_pm_domains(base, pmd, NULL);
340                 of_node_put(pmd);
341                 if (ret) {
342                         of_node_put(np);
343                         break;
344                 }
345         }
346
347         put_special_pds();
348
349         return ret;
350 }
351
352 core_initcall(rmobile_init_pm_domains);