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