Commit | Line | Data |
---|---|---|
8f45b112 KM |
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> | |
2173fc7c | 6 | * Copyright (C) 2014 Glider bvba |
8f45b112 KM |
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> | |
2173fc7c GU |
17 | #include <linux/of.h> |
18 | #include <linux/of_address.h> | |
19 | #include <linux/of_platform.h> | |
8f45b112 KM |
20 | #include <linux/platform_device.h> |
21 | #include <linux/pm.h> | |
22 | #include <linux/pm_clock.h> | |
2173fc7c GU |
23 | #include <linux/slab.h> |
24 | ||
8f45b112 | 25 | #include <asm/io.h> |
2173fc7c | 26 | |
6b8b0cb4 | 27 | #include "pm-rmobile.h" |
8f45b112 KM |
28 | |
29 | /* SYSC */ | |
25717b85 GU |
30 | #define SPDCR 0x08 /* SYS Power Down Control Register */ |
31 | #define SWUCR 0x14 /* SYS Wakeup Control Register */ | |
32 | #define PSTR 0x80 /* Power Status Register */ | |
8f45b112 KM |
33 | |
34 | #define PSTR_RETRIES 100 | |
35 | #define PSTR_DELAY_US 10 | |
36 | ||
23758258 GU |
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 | ||
8f45b112 KM |
43 | static int rmobile_pd_power_down(struct generic_pm_domain *genpd) |
44 | { | |
45 | struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd); | |
2173fc7c GU |
46 | unsigned int mask; |
47 | ||
48 | if (rmobile_pd->bit_shift == ~0) | |
49 | return -EBUSY; | |
8f45b112 | 50 | |
23e95fc2 | 51 | mask = BIT(rmobile_pd->bit_shift); |
8f45b112 KM |
52 | if (rmobile_pd->suspend) { |
53 | int ret = rmobile_pd->suspend(); | |
54 | ||
55 | if (ret) | |
56 | return ret; | |
57 | } | |
58 | ||
25717b85 | 59 | if (__raw_readl(rmobile_pd->base + PSTR) & mask) { |
8f45b112 | 60 | unsigned int retry_count; |
25717b85 | 61 | __raw_writel(mask, rmobile_pd->base + SPDCR); |
8f45b112 KM |
62 | |
63 | for (retry_count = PSTR_RETRIES; retry_count; retry_count--) { | |
25717b85 | 64 | if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask)) |
8f45b112 KM |
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", | |
25717b85 GU |
72 | genpd->name, mask, |
73 | __raw_readl(rmobile_pd->base + PSTR)); | |
8f45b112 KM |
74 | |
75 | return 0; | |
76 | } | |
77 | ||
78 | static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd, | |
79 | bool do_resume) | |
80 | { | |
2173fc7c | 81 | unsigned int mask; |
8f45b112 KM |
82 | unsigned int retry_count; |
83 | int ret = 0; | |
84 | ||
2173fc7c GU |
85 | if (rmobile_pd->bit_shift == ~0) |
86 | return 0; | |
87 | ||
23e95fc2 | 88 | mask = BIT(rmobile_pd->bit_shift); |
25717b85 | 89 | if (__raw_readl(rmobile_pd->base + PSTR) & mask) |
8f45b112 KM |
90 | goto out; |
91 | ||
25717b85 | 92 | __raw_writel(mask, rmobile_pd->base + SWUCR); |
8f45b112 KM |
93 | |
94 | for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) { | |
25717b85 | 95 | if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask)) |
8f45b112 KM |
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", | |
25717b85 GU |
107 | rmobile_pd->genpd.name, mask, |
108 | __raw_readl(rmobile_pd->base + PSTR)); | |
8f45b112 KM |
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 | { | |
ab496b9d | 124 | return true; |
8f45b112 KM |
125 | } |
126 | ||
4b9d62e0 GU |
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 | ||
a62595d3 | 157 | static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd) |
8f45b112 KM |
158 | { |
159 | struct generic_pm_domain *genpd = &rmobile_pd->genpd; | |
160 | struct dev_power_governor *gov = rmobile_pd->gov; | |
161 | ||
cffa9138 | 162 | genpd->flags = GENPD_FLAG_PM_CLK; |
8f45b112 | 163 | pm_genpd_init(genpd, gov ? : &simple_qos_governor, false); |
8f45b112 | 164 | genpd->dev_ops.active_wakeup = rmobile_pd_active_wakeup; |
8f45b112 KM |
165 | genpd->power_off = rmobile_pd_power_down; |
166 | genpd->power_on = rmobile_pd_power_up; | |
4b9d62e0 GU |
167 | genpd->attach_dev = rmobile_pd_attach_dev; |
168 | genpd->detach_dev = rmobile_pd_detach_dev; | |
8f45b112 KM |
169 | __rmobile_pd_power_up(rmobile_pd, false); |
170 | } | |
171 | ||
e43ee86e | 172 | static int rmobile_pd_suspend_busy(void) |
2173fc7c GU |
173 | { |
174 | /* | |
e43ee86e | 175 | * This domain should not be turned off. |
2173fc7c GU |
176 | */ |
177 | return -EBUSY; | |
178 | } | |
179 | ||
180 | static int rmobile_pd_suspend_console(void) | |
181 | { | |
182 | /* | |
183 | * Serial consoles make use of SCIF hardware located in this domain, | |
184 | * hence keep the power domain on if "no_console_suspend" is set. | |
185 | */ | |
186 | return console_suspend_enabled ? 0 : -EBUSY; | |
187 | } | |
188 | ||
60e26435 GU |
189 | enum pd_types { |
190 | PD_NORMAL, | |
191 | PD_CPU, | |
192 | PD_CONSOLE, | |
193 | PD_DEBUG, | |
1632ff16 | 194 | PD_MEMCTL, |
60e26435 | 195 | }; |
2173fc7c | 196 | |
60e26435 | 197 | #define MAX_NUM_SPECIAL_PDS 16 |
2173fc7c | 198 | |
60e26435 GU |
199 | static struct special_pd { |
200 | struct device_node *pd; | |
201 | enum pd_types type; | |
202 | } special_pds[MAX_NUM_SPECIAL_PDS] __initdata; | |
2173fc7c | 203 | |
60e26435 | 204 | static unsigned int num_special_pds __initdata; |
2173fc7c | 205 | |
1632ff16 GU |
206 | static const struct of_device_id special_ids[] __initconst = { |
207 | { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG }, | |
208 | { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, }, | |
209 | { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, }, | |
210 | { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, }, | |
211 | { /* sentinel */ }, | |
212 | }; | |
213 | ||
60e26435 GU |
214 | static void __init add_special_pd(struct device_node *np, enum pd_types type) |
215 | { | |
216 | unsigned int i; | |
217 | struct device_node *pd; | |
2173fc7c | 218 | |
60e26435 GU |
219 | pd = of_parse_phandle(np, "power-domains", 0); |
220 | if (!pd) | |
221 | return; | |
2173fc7c | 222 | |
60e26435 GU |
223 | for (i = 0; i < num_special_pds; i++) |
224 | if (pd == special_pds[i].pd && type == special_pds[i].type) { | |
2173fc7c | 225 | of_node_put(pd); |
60e26435 | 226 | return; |
2173fc7c GU |
227 | } |
228 | ||
60e26435 GU |
229 | if (num_special_pds == ARRAY_SIZE(special_pds)) { |
230 | pr_warn("Too many special PM domains\n"); | |
231 | of_node_put(pd); | |
232 | return; | |
2173fc7c GU |
233 | } |
234 | ||
60e26435 GU |
235 | pr_debug("Special PM domain %s type %d for %s\n", pd->name, type, |
236 | np->full_name); | |
237 | ||
238 | special_pds[num_special_pds].pd = pd; | |
239 | special_pds[num_special_pds].type = type; | |
240 | num_special_pds++; | |
241 | } | |
242 | ||
243 | static void __init get_special_pds(void) | |
244 | { | |
245 | struct device_node *np; | |
1632ff16 | 246 | const struct of_device_id *id; |
60e26435 GU |
247 | |
248 | /* PM domains containing CPUs */ | |
249 | for_each_node_by_type(np, "cpu") | |
250 | add_special_pd(np, PD_CPU); | |
251 | ||
2173fc7c GU |
252 | /* PM domain containing console */ |
253 | if (of_stdout) | |
60e26435 | 254 | add_special_pd(of_stdout, PD_CONSOLE); |
2173fc7c | 255 | |
1632ff16 GU |
256 | /* PM domains containing other special devices */ |
257 | for_each_matching_node_and_match(np, special_ids, &id) | |
258 | add_special_pd(np, (enum pd_types)id->data); | |
2173fc7c GU |
259 | } |
260 | ||
261 | static void __init put_special_pds(void) | |
262 | { | |
263 | unsigned int i; | |
264 | ||
60e26435 GU |
265 | for (i = 0; i < num_special_pds; i++) |
266 | of_node_put(special_pds[i].pd); | |
2173fc7c GU |
267 | } |
268 | ||
60e26435 | 269 | static enum pd_types __init pd_type(const struct device_node *pd) |
2173fc7c GU |
270 | { |
271 | unsigned int i; | |
272 | ||
60e26435 GU |
273 | for (i = 0; i < num_special_pds; i++) |
274 | if (pd == special_pds[i].pd) | |
275 | return special_pds[i].type; | |
2173fc7c | 276 | |
60e26435 | 277 | return PD_NORMAL; |
2173fc7c GU |
278 | } |
279 | ||
280 | static void __init rmobile_setup_pm_domain(struct device_node *np, | |
281 | struct rmobile_pm_domain *pd) | |
282 | { | |
283 | const char *name = pd->genpd.name; | |
284 | ||
60e26435 GU |
285 | switch (pd_type(np)) { |
286 | case PD_CPU: | |
e43ee86e GU |
287 | /* |
288 | * This domain contains the CPU core and therefore it should | |
289 | * only be turned off if the CPU is not in use. | |
290 | */ | |
2173fc7c GU |
291 | pr_debug("PM domain %s contains CPU\n", name); |
292 | pd->gov = &pm_domain_always_on_gov; | |
e43ee86e | 293 | pd->suspend = rmobile_pd_suspend_busy; |
60e26435 GU |
294 | break; |
295 | ||
296 | case PD_CONSOLE: | |
2173fc7c GU |
297 | pr_debug("PM domain %s contains serial console\n", name); |
298 | pd->gov = &pm_domain_always_on_gov; | |
299 | pd->suspend = rmobile_pd_suspend_console; | |
60e26435 GU |
300 | break; |
301 | ||
302 | case PD_DEBUG: | |
e43ee86e GU |
303 | /* |
304 | * This domain contains the Coresight-ETM hardware block and | |
305 | * therefore it should only be turned off if the debug module | |
306 | * is not in use. | |
307 | */ | |
2173fc7c GU |
308 | pr_debug("PM domain %s contains Coresight-ETM\n", name); |
309 | pd->gov = &pm_domain_always_on_gov; | |
e43ee86e | 310 | pd->suspend = rmobile_pd_suspend_busy; |
60e26435 GU |
311 | break; |
312 | ||
1632ff16 GU |
313 | case PD_MEMCTL: |
314 | /* | |
315 | * This domain contains a memory-controller and therefore it | |
316 | * should only be turned off if memory is not in use. | |
317 | */ | |
318 | pr_debug("PM domain %s contains MEMCTL\n", name); | |
319 | pd->gov = &pm_domain_always_on_gov; | |
320 | pd->suspend = rmobile_pd_suspend_busy; | |
321 | break; | |
322 | ||
60e26435 GU |
323 | case PD_NORMAL: |
324 | break; | |
2173fc7c GU |
325 | } |
326 | ||
327 | rmobile_init_pm_domain(pd); | |
328 | } | |
329 | ||
330 | static int __init rmobile_add_pm_domains(void __iomem *base, | |
331 | struct device_node *parent, | |
332 | struct generic_pm_domain *genpd_parent) | |
333 | { | |
334 | struct device_node *np; | |
335 | ||
336 | for_each_child_of_node(parent, np) { | |
337 | struct rmobile_pm_domain *pd; | |
338 | u32 idx = ~0; | |
339 | ||
340 | if (of_property_read_u32(np, "reg", &idx)) { | |
341 | /* always-on domain */ | |
342 | } | |
343 | ||
344 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); | |
345 | if (!pd) | |
346 | return -ENOMEM; | |
347 | ||
348 | pd->genpd.name = np->name; | |
349 | pd->base = base; | |
350 | pd->bit_shift = idx; | |
351 | ||
352 | rmobile_setup_pm_domain(np, pd); | |
353 | if (genpd_parent) | |
354 | pm_genpd_add_subdomain(genpd_parent, &pd->genpd); | |
355 | of_genpd_add_provider_simple(np, &pd->genpd); | |
356 | ||
357 | rmobile_add_pm_domains(base, np, &pd->genpd); | |
358 | } | |
359 | return 0; | |
360 | } | |
361 | ||
362 | static int __init rmobile_init_pm_domains(void) | |
363 | { | |
364 | struct device_node *np, *pmd; | |
365 | bool scanned = false; | |
366 | void __iomem *base; | |
367 | int ret = 0; | |
368 | ||
369 | for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") { | |
370 | base = of_iomap(np, 0); | |
371 | if (!base) { | |
372 | pr_warn("%s cannot map reg 0\n", np->full_name); | |
373 | continue; | |
374 | } | |
375 | ||
376 | pmd = of_get_child_by_name(np, "pm-domains"); | |
377 | if (!pmd) { | |
378 | pr_warn("%s lacks pm-domains node\n", np->full_name); | |
379 | continue; | |
380 | } | |
381 | ||
382 | if (!scanned) { | |
383 | /* Find PM domains containing special blocks */ | |
384 | get_special_pds(); | |
385 | scanned = true; | |
386 | } | |
387 | ||
388 | ret = rmobile_add_pm_domains(base, pmd, NULL); | |
389 | of_node_put(pmd); | |
390 | if (ret) { | |
391 | of_node_put(np); | |
392 | break; | |
393 | } | |
394 | } | |
395 | ||
396 | put_special_pds(); | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
401 | core_initcall(rmobile_init_pm_domains); |