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