blob: e348bcfe389da51b96cbc2c0bddbbb93a9e7cf5c [file] [log] [blame]
Kuninori Morimoto8f45b112012-07-05 01:24:46 -07001/*
2 * rmobile power management support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +01006 * Copyright (C) 2014 Glider bvba
Kuninori Morimoto8f45b112012-07-05 01:24:46 -07007 *
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 */
Simon Horman09c32422016-03-08 09:42:07 +090015#include <linux/clk/renesas.h>
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070016#include <linux/console.h>
17#include <linux/delay.h>
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010018#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_platform.h>
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070021#include <linux/platform_device.h>
22#include <linux/pm.h>
23#include <linux/pm_clock.h>
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010024#include <linux/slab.h>
25
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070026#include <asm/io.h>
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010027
Magnus Damm6b8b0cb2014-06-17 16:47:45 +090028#include "pm-rmobile.h"
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070029
30/* SYSC */
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010031#define SPDCR 0x08 /* SYS Power Down Control Register */
32#define SWUCR 0x14 /* SYS Wakeup Control Register */
33#define PSTR 0x80 /* Power Status Register */
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070034
35#define PSTR_RETRIES 100
36#define PSTR_DELAY_US 10
37
Geert Uytterhoeven23758252015-06-04 20:22:25 +020038static inline
39struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
40{
41 return container_of(d, struct rmobile_pm_domain, genpd);
42}
43
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070044static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
45{
46 struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010047 unsigned int mask;
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070048
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010049 if (rmobile_pd->bit_shift == ~0)
50 return -EBUSY;
51
Geert Uytterhoeven23e95fc2015-06-04 20:22:26 +020052 mask = BIT(rmobile_pd->bit_shift);
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070053 if (rmobile_pd->suspend) {
54 int ret = rmobile_pd->suspend();
55
56 if (ret)
57 return ret;
58 }
59
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010060 if (__raw_readl(rmobile_pd->base + PSTR) & mask) {
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070061 unsigned int retry_count;
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010062 __raw_writel(mask, rmobile_pd->base + SPDCR);
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070063
64 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010065 if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask))
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070066 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",
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010073 genpd->name, mask,
74 __raw_readl(rmobile_pd->base + PSTR));
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070075
76 return 0;
77}
78
79static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd,
80 bool do_resume)
81{
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010082 unsigned int mask;
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070083 unsigned int retry_count;
84 int ret = 0;
85
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +010086 if (rmobile_pd->bit_shift == ~0)
87 return 0;
88
Geert Uytterhoeven23e95fc2015-06-04 20:22:26 +020089 mask = BIT(rmobile_pd->bit_shift);
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010090 if (__raw_readl(rmobile_pd->base + PSTR) & mask)
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070091 goto out;
92
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010093 __raw_writel(mask, rmobile_pd->base + SWUCR);
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070094
95 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
Geert Uytterhoeven25717b82014-12-03 14:41:44 +010096 if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask))
Kuninori Morimoto8f45b112012-07-05 01:24:46 -070097 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",
Geert Uytterhoeven25717b82014-12-03 14:41:44 +0100108 rmobile_pd->genpd.name, mask,
109 __raw_readl(rmobile_pd->base + PSTR));
Kuninori Morimoto8f45b112012-07-05 01:24:46 -0700110
111out:
112 if (ret == 0 && rmobile_pd->resume && do_resume)
113 rmobile_pd->resume();
114
115 return ret;
116}
117
118static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
119{
120 return __rmobile_pd_power_up(to_rmobile_pd(genpd), true);
121}
122
Rafael J. Wysockia62595d2012-08-07 01:15:02 +0200123static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
Kuninori Morimoto8f45b112012-07-05 01:24:46 -0700124{
125 struct generic_pm_domain *genpd = &rmobile_pd->genpd;
126 struct dev_power_governor *gov = rmobile_pd->gov;
127
Geert Uytterhoeveneb0ddf92017-11-07 13:48:12 +0100128 genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
Kuninori Morimoto8f45b112012-07-05 01:24:46 -0700129 genpd->power_off = rmobile_pd_power_down;
130 genpd->power_on = rmobile_pd_power_up;
Geert Uytterhoeven8b98d742015-08-04 14:28:17 +0200131 genpd->attach_dev = cpg_mstp_attach_dev;
132 genpd->detach_dev = cpg_mstp_detach_dev;
Kuninori Morimoto8f45b112012-07-05 01:24:46 -0700133 __rmobile_pd_power_up(rmobile_pd, false);
Geert Uytterhoeven85d9bf12016-05-04 10:27:46 +0200134 pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
Kuninori Morimoto8f45b112012-07-05 01:24:46 -0700135}
136
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100137static int rmobile_pd_suspend_console(void)
138{
139 /*
140 * Serial consoles make use of SCIF hardware located in this domain,
141 * hence keep the power domain on if "no_console_suspend" is set.
142 */
143 return console_suspend_enabled ? 0 : -EBUSY;
144}
145
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100146enum pd_types {
147 PD_NORMAL,
148 PD_CPU,
149 PD_CONSOLE,
150 PD_DEBUG,
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100151 PD_MEMCTL,
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100152};
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100153
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100154#define MAX_NUM_SPECIAL_PDS 16
155
156static struct special_pd {
157 struct device_node *pd;
158 enum pd_types type;
159} special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
160
161static unsigned int num_special_pds __initdata;
162
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100163static const struct of_device_id special_ids[] __initconst = {
164 { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
165 { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
166 { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
167 { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
168 { /* sentinel */ },
169};
170
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100171static void __init add_special_pd(struct device_node *np, enum pd_types type)
172{
173 unsigned int i;
174 struct device_node *pd;
175
176 pd = of_parse_phandle(np, "power-domains", 0);
177 if (!pd)
178 return;
179
180 for (i = 0; i < num_special_pds; i++)
181 if (pd == special_pds[i].pd && type == special_pds[i].type) {
182 of_node_put(pd);
183 return;
184 }
185
186 if (num_special_pds == ARRAY_SIZE(special_pds)) {
187 pr_warn("Too many special PM domains\n");
188 of_node_put(pd);
189 return;
190 }
191
Rob Herringa8e65e02017-07-21 14:28:32 -0500192 pr_debug("Special PM domain %s type %d for %pOF\n", pd->name, type, np);
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100193
194 special_pds[num_special_pds].pd = pd;
195 special_pds[num_special_pds].type = type;
196 num_special_pds++;
197}
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100198
199static void __init get_special_pds(void)
200{
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100201 struct device_node *np;
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100202 const struct of_device_id *id;
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100203
204 /* PM domains containing CPUs */
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100205 for_each_node_by_type(np, "cpu")
206 add_special_pd(np, PD_CPU);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100207
208 /* PM domain containing console */
209 if (of_stdout)
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100210 add_special_pd(of_stdout, PD_CONSOLE);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100211
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100212 /* PM domains containing other special devices */
213 for_each_matching_node_and_match(np, special_ids, &id)
214 add_special_pd(np, (enum pd_types)id->data);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100215}
216
217static void __init put_special_pds(void)
218{
219 unsigned int i;
220
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100221 for (i = 0; i < num_special_pds; i++)
222 of_node_put(special_pds[i].pd);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100223}
224
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100225static enum pd_types __init pd_type(const struct device_node *pd)
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100226{
227 unsigned int i;
228
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100229 for (i = 0; i < num_special_pds; i++)
230 if (pd == special_pds[i].pd)
231 return special_pds[i].type;
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100232
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100233 return PD_NORMAL;
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100234}
235
236static void __init rmobile_setup_pm_domain(struct device_node *np,
237 struct rmobile_pm_domain *pd)
238{
239 const char *name = pd->genpd.name;
240
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100241 switch (pd_type(np)) {
242 case PD_CPU:
Geert Uytterhoevene43ee862015-01-14 13:11:20 +0100243 /*
244 * This domain contains the CPU core and therefore it should
245 * only be turned off if the CPU is not in use.
246 */
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100247 pr_debug("PM domain %s contains CPU\n", name);
Geert Uytterhoevenfd8cf822017-06-12 11:23:46 +0200248 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100249 break;
250
251 case PD_CONSOLE:
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100252 pr_debug("PM domain %s contains serial console\n", name);
253 pd->gov = &pm_domain_always_on_gov;
254 pd->suspend = rmobile_pd_suspend_console;
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100255 break;
256
257 case PD_DEBUG:
Geert Uytterhoevene43ee862015-01-14 13:11:20 +0100258 /*
259 * This domain contains the Coresight-ETM hardware block and
260 * therefore it should only be turned off if the debug module
261 * is not in use.
262 */
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100263 pr_debug("PM domain %s contains Coresight-ETM\n", name);
Geert Uytterhoevenfd8cf822017-06-12 11:23:46 +0200264 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100265 break;
266
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100267 case PD_MEMCTL:
268 /*
269 * This domain contains a memory-controller and therefore it
270 * should only be turned off if memory is not in use.
271 */
272 pr_debug("PM domain %s contains MEMCTL\n", name);
Geert Uytterhoevenfd8cf822017-06-12 11:23:46 +0200273 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
Geert Uytterhoeven1632ff12015-01-14 13:11:22 +0100274 break;
275
Geert Uytterhoeven60e26432015-01-14 13:11:21 +0100276 case PD_NORMAL:
277 break;
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100278 }
279
280 rmobile_init_pm_domain(pd);
281}
282
283static int __init rmobile_add_pm_domains(void __iomem *base,
284 struct device_node *parent,
285 struct generic_pm_domain *genpd_parent)
286{
287 struct device_node *np;
288
289 for_each_child_of_node(parent, np) {
290 struct rmobile_pm_domain *pd;
291 u32 idx = ~0;
292
293 if (of_property_read_u32(np, "reg", &idx)) {
294 /* always-on domain */
295 }
296
297 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Julia Lawall1fd377b2015-10-10 14:30:52 +0200298 if (!pd) {
299 of_node_put(np);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100300 return -ENOMEM;
Julia Lawall1fd377b2015-10-10 14:30:52 +0200301 }
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100302
303 pd->genpd.name = np->name;
304 pd->base = base;
305 pd->bit_shift = idx;
306
307 rmobile_setup_pm_domain(np, pd);
308 if (genpd_parent)
309 pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
310 of_genpd_add_provider_simple(np, &pd->genpd);
311
312 rmobile_add_pm_domains(base, np, &pd->genpd);
313 }
314 return 0;
315}
316
317static int __init rmobile_init_pm_domains(void)
318{
319 struct device_node *np, *pmd;
320 bool scanned = false;
321 void __iomem *base;
322 int ret = 0;
323
324 for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
325 base = of_iomap(np, 0);
326 if (!base) {
Rob Herringa8e65e02017-07-21 14:28:32 -0500327 pr_warn("%pOF cannot map reg 0\n", np);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100328 continue;
329 }
330
331 pmd = of_get_child_by_name(np, "pm-domains");
332 if (!pmd) {
Rob Herringa8e65e02017-07-21 14:28:32 -0500333 pr_warn("%pOF lacks pm-domains node\n", np);
Geert Uytterhoeven2173fc72014-12-03 14:41:45 +0100334 continue;
335 }
336
337 if (!scanned) {
338 /* Find PM domains containing special blocks */
339 get_special_pds();
340 scanned = true;
341 }
342
343 ret = rmobile_add_pm_domains(base, pmd, NULL);
344 of_node_put(pmd);
345 if (ret) {
346 of_node_put(np);
347 break;
348 }
349 }
350
351 put_special_pds();
352
353 return ret;
354}
355
356core_initcall(rmobile_init_pm_domains);