ARM: shmobile: R-Mobile: Use BIT() macro instead of open coding
[firefly-linux-kernel-4.4.55.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/console.h>
16 #include <linux/delay.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm.h>
22 #include <linux/pm_clock.h>
23 #include <linux/slab.h>
24
25 #include <asm/io.h>
26
27 #include "pm-rmobile.h"
28
29 /* SYSC */
30 #define SPDCR           0x08    /* SYS Power Down Control Register */
31 #define SWUCR           0x14    /* SYS Wakeup Control Register */
32 #define PSTR            0x80    /* Power Status Register */
33
34 #define PSTR_RETRIES    100
35 #define PSTR_DELAY_US   10
36
37 static inline
38 struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
39 {
40         return container_of(d, struct rmobile_pm_domain, genpd);
41 }
42
43 static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
44 {
45         struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
46         unsigned int mask;
47
48         if (rmobile_pd->bit_shift == ~0)
49                 return -EBUSY;
50
51         mask = BIT(rmobile_pd->bit_shift);
52         if (rmobile_pd->suspend) {
53                 int ret = rmobile_pd->suspend();
54
55                 if (ret)
56                         return ret;
57         }
58
59         if (__raw_readl(rmobile_pd->base + PSTR) & mask) {
60                 unsigned int retry_count;
61                 __raw_writel(mask, rmobile_pd->base + SPDCR);
62
63                 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
64                         if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask))
65                                 break;
66                         cpu_relax();
67                 }
68         }
69
70         if (!rmobile_pd->no_debug)
71                 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
72                          genpd->name, mask,
73                          __raw_readl(rmobile_pd->base + PSTR));
74
75         return 0;
76 }
77
78 static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd,
79                                  bool do_resume)
80 {
81         unsigned int mask;
82         unsigned int retry_count;
83         int ret = 0;
84
85         if (rmobile_pd->bit_shift == ~0)
86                 return 0;
87
88         mask = BIT(rmobile_pd->bit_shift);
89         if (__raw_readl(rmobile_pd->base + PSTR) & mask)
90                 goto out;
91
92         __raw_writel(mask, rmobile_pd->base + SWUCR);
93
94         for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
95                 if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask))
96                         break;
97                 if (retry_count > PSTR_RETRIES)
98                         udelay(PSTR_DELAY_US);
99                 else
100                         cpu_relax();
101         }
102         if (!retry_count)
103                 ret = -EIO;
104
105         if (!rmobile_pd->no_debug)
106                 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
107                          rmobile_pd->genpd.name, mask,
108                          __raw_readl(rmobile_pd->base + PSTR));
109
110 out:
111         if (ret == 0 && rmobile_pd->resume && do_resume)
112                 rmobile_pd->resume();
113
114         return ret;
115 }
116
117 static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
118 {
119         return __rmobile_pd_power_up(to_rmobile_pd(genpd), true);
120 }
121
122 static bool rmobile_pd_active_wakeup(struct device *dev)
123 {
124         return true;
125 }
126
127 static int rmobile_pd_attach_dev(struct generic_pm_domain *domain,
128                                  struct device *dev)
129 {
130         int error;
131
132         error = pm_clk_create(dev);
133         if (error) {
134                 dev_err(dev, "pm_clk_create failed %d\n", error);
135                 return error;
136         }
137
138         error = pm_clk_add(dev, NULL);
139         if (error) {
140                 dev_err(dev, "pm_clk_add failed %d\n", error);
141                 goto fail;
142         }
143
144         return 0;
145
146 fail:
147         pm_clk_destroy(dev);
148         return error;
149 }
150
151 static void rmobile_pd_detach_dev(struct generic_pm_domain *domain,
152                                   struct device *dev)
153 {
154         pm_clk_destroy(dev);
155 }
156
157 static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
158 {
159         struct generic_pm_domain *genpd = &rmobile_pd->genpd;
160         struct dev_power_governor *gov = rmobile_pd->gov;
161
162         genpd->flags = GENPD_FLAG_PM_CLK;
163         pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
164         genpd->dev_ops.active_wakeup    = rmobile_pd_active_wakeup;
165         genpd->power_off                = rmobile_pd_power_down;
166         genpd->power_on                 = rmobile_pd_power_up;
167         genpd->attach_dev               = rmobile_pd_attach_dev;
168         genpd->detach_dev               = rmobile_pd_detach_dev;
169         __rmobile_pd_power_up(rmobile_pd, false);
170 }
171
172 #ifdef CONFIG_ARCH_SHMOBILE_LEGACY
173
174 void rmobile_init_domains(struct rmobile_pm_domain domains[], int num)
175 {
176         int j;
177
178         for (j = 0; j < num; j++)
179                 rmobile_init_pm_domain(&domains[j]);
180 }
181
182 void rmobile_add_device_to_domain_td(const char *domain_name,
183                                      struct platform_device *pdev,
184                                      struct gpd_timing_data *td)
185 {
186         struct device *dev = &pdev->dev;
187
188         __pm_genpd_name_add_device(domain_name, dev, td);
189 }
190
191 void rmobile_add_devices_to_domains(struct pm_domain_device data[],
192                                     int size)
193 {
194         struct gpd_timing_data latencies = {
195                 .stop_latency_ns = DEFAULT_DEV_LATENCY_NS,
196                 .start_latency_ns = DEFAULT_DEV_LATENCY_NS,
197                 .save_state_latency_ns = DEFAULT_DEV_LATENCY_NS,
198                 .restore_state_latency_ns = DEFAULT_DEV_LATENCY_NS,
199         };
200         int j;
201
202         for (j = 0; j < size; j++)
203                 rmobile_add_device_to_domain_td(data[j].domain_name,
204                                                 data[j].pdev, &latencies);
205 }
206
207 #else /* !CONFIG_ARCH_SHMOBILE_LEGACY */
208
209 static int rmobile_pd_suspend_busy(void)
210 {
211         /*
212          * This domain should not be turned off.
213          */
214         return -EBUSY;
215 }
216
217 static int rmobile_pd_suspend_console(void)
218 {
219         /*
220          * Serial consoles make use of SCIF hardware located in this domain,
221          * hence keep the power domain on if "no_console_suspend" is set.
222          */
223         return console_suspend_enabled ? 0 : -EBUSY;
224 }
225
226 enum pd_types {
227         PD_NORMAL,
228         PD_CPU,
229         PD_CONSOLE,
230         PD_DEBUG,
231         PD_MEMCTL,
232 };
233
234 #define MAX_NUM_SPECIAL_PDS     16
235
236 static struct special_pd {
237         struct device_node *pd;
238         enum pd_types type;
239 } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
240
241 static unsigned int num_special_pds __initdata;
242
243 static const struct of_device_id special_ids[] __initconst = {
244         { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
245         { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
246         { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
247         { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
248         { /* sentinel */ },
249 };
250
251 static void __init add_special_pd(struct device_node *np, enum pd_types type)
252 {
253         unsigned int i;
254         struct device_node *pd;
255
256         pd = of_parse_phandle(np, "power-domains", 0);
257         if (!pd)
258                 return;
259
260         for (i = 0; i < num_special_pds; i++)
261                 if (pd == special_pds[i].pd && type == special_pds[i].type) {
262                         of_node_put(pd);
263                         return;
264                 }
265
266         if (num_special_pds == ARRAY_SIZE(special_pds)) {
267                 pr_warn("Too many special PM domains\n");
268                 of_node_put(pd);
269                 return;
270         }
271
272         pr_debug("Special PM domain %s type %d for %s\n", pd->name, type,
273                  np->full_name);
274
275         special_pds[num_special_pds].pd = pd;
276         special_pds[num_special_pds].type = type;
277         num_special_pds++;
278 }
279
280 static void __init get_special_pds(void)
281 {
282         struct device_node *np;
283         const struct of_device_id *id;
284
285         /* PM domains containing CPUs */
286         for_each_node_by_type(np, "cpu")
287                 add_special_pd(np, PD_CPU);
288
289         /* PM domain containing console */
290         if (of_stdout)
291                 add_special_pd(of_stdout, PD_CONSOLE);
292
293         /* PM domains containing other special devices */
294         for_each_matching_node_and_match(np, special_ids, &id)
295                 add_special_pd(np, (enum pd_types)id->data);
296 }
297
298 static void __init put_special_pds(void)
299 {
300         unsigned int i;
301
302         for (i = 0; i < num_special_pds; i++)
303                 of_node_put(special_pds[i].pd);
304 }
305
306 static enum pd_types __init pd_type(const struct device_node *pd)
307 {
308         unsigned int i;
309
310         for (i = 0; i < num_special_pds; i++)
311                 if (pd == special_pds[i].pd)
312                         return special_pds[i].type;
313
314         return PD_NORMAL;
315 }
316
317 static void __init rmobile_setup_pm_domain(struct device_node *np,
318                                            struct rmobile_pm_domain *pd)
319 {
320         const char *name = pd->genpd.name;
321
322         switch (pd_type(np)) {
323         case PD_CPU:
324                 /*
325                  * This domain contains the CPU core and therefore it should
326                  * only be turned off if the CPU is not in use.
327                  */
328                 pr_debug("PM domain %s contains CPU\n", name);
329                 pd->gov = &pm_domain_always_on_gov;
330                 pd->suspend = rmobile_pd_suspend_busy;
331                 break;
332
333         case PD_CONSOLE:
334                 pr_debug("PM domain %s contains serial console\n", name);
335                 pd->gov = &pm_domain_always_on_gov;
336                 pd->suspend = rmobile_pd_suspend_console;
337                 break;
338
339         case PD_DEBUG:
340                 /*
341                  * This domain contains the Coresight-ETM hardware block and
342                  * therefore it should only be turned off if the debug module
343                  * is not in use.
344                  */
345                 pr_debug("PM domain %s contains Coresight-ETM\n", name);
346                 pd->gov = &pm_domain_always_on_gov;
347                 pd->suspend = rmobile_pd_suspend_busy;
348                 break;
349
350         case PD_MEMCTL:
351                 /*
352                  * This domain contains a memory-controller and therefore it
353                  * should only be turned off if memory is not in use.
354                  */
355                 pr_debug("PM domain %s contains MEMCTL\n", name);
356                 pd->gov = &pm_domain_always_on_gov;
357                 pd->suspend = rmobile_pd_suspend_busy;
358                 break;
359
360         case PD_NORMAL:
361                 break;
362         }
363
364         rmobile_init_pm_domain(pd);
365 }
366
367 static int __init rmobile_add_pm_domains(void __iomem *base,
368                                          struct device_node *parent,
369                                          struct generic_pm_domain *genpd_parent)
370 {
371         struct device_node *np;
372
373         for_each_child_of_node(parent, np) {
374                 struct rmobile_pm_domain *pd;
375                 u32 idx = ~0;
376
377                 if (of_property_read_u32(np, "reg", &idx)) {
378                         /* always-on domain */
379                 }
380
381                 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
382                 if (!pd)
383                         return -ENOMEM;
384
385                 pd->genpd.name = np->name;
386                 pd->base = base;
387                 pd->bit_shift = idx;
388
389                 rmobile_setup_pm_domain(np, pd);
390                 if (genpd_parent)
391                         pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
392                 of_genpd_add_provider_simple(np, &pd->genpd);
393
394                 rmobile_add_pm_domains(base, np, &pd->genpd);
395         }
396         return 0;
397 }
398
399 static int __init rmobile_init_pm_domains(void)
400 {
401         struct device_node *np, *pmd;
402         bool scanned = false;
403         void __iomem *base;
404         int ret = 0;
405
406         for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
407                 base = of_iomap(np, 0);
408                 if (!base) {
409                         pr_warn("%s cannot map reg 0\n", np->full_name);
410                         continue;
411                 }
412
413                 pmd = of_get_child_by_name(np, "pm-domains");
414                 if (!pmd) {
415                         pr_warn("%s lacks pm-domains node\n", np->full_name);
416                         continue;
417                 }
418
419                 if (!scanned) {
420                         /* Find PM domains containing special blocks */
421                         get_special_pds();
422                         scanned = true;
423                 }
424
425                 ret = rmobile_add_pm_domains(base, pmd, NULL);
426                 of_node_put(pmd);
427                 if (ret) {
428                         of_node_put(np);
429                         break;
430                 }
431         }
432
433         put_special_pds();
434
435         return ret;
436 }
437
438 core_initcall(rmobile_init_pm_domains);
439
440 #endif /* !CONFIG_ARCH_SHMOBILE_LEGACY */