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 | |
2173fc7c | 51 | mask = 1 << 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 | ||
88 | mask = 1 << 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 | ||
2173fc7c GU |
172 | #ifdef CONFIG_ARCH_SHMOBILE_LEGACY |
173 | ||
0d09f450 RW |
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 | ||
455ae3a5 RW |
182 | void rmobile_add_device_to_domain_td(const char *domain_name, |
183 | struct platform_device *pdev, | |
184 | struct gpd_timing_data *td) | |
8f45b112 KM |
185 | { |
186 | struct device *dev = &pdev->dev; | |
187 | ||
455ae3a5 | 188 | __pm_genpd_name_add_device(domain_name, dev, td); |
8f45b112 KM |
189 | } |
190 | ||
ac18e02d RW |
191 | void rmobile_add_devices_to_domains(struct pm_domain_device data[], |
192 | int size) | |
8f45b112 | 193 | { |
ac18e02d RW |
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); | |
8f45b112 | 205 | } |
2173fc7c GU |
206 | |
207 | #else /* !CONFIG_ARCH_SHMOBILE_LEGACY */ | |
208 | ||
e43ee86e | 209 | static int rmobile_pd_suspend_busy(void) |
2173fc7c GU |
210 | { |
211 | /* | |
e43ee86e | 212 | * This domain should not be turned off. |
2173fc7c GU |
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 | ||
60e26435 GU |
226 | enum pd_types { |
227 | PD_NORMAL, | |
228 | PD_CPU, | |
229 | PD_CONSOLE, | |
230 | PD_DEBUG, | |
1632ff16 | 231 | PD_MEMCTL, |
60e26435 | 232 | }; |
2173fc7c | 233 | |
60e26435 | 234 | #define MAX_NUM_SPECIAL_PDS 16 |
2173fc7c | 235 | |
60e26435 GU |
236 | static struct special_pd { |
237 | struct device_node *pd; | |
238 | enum pd_types type; | |
239 | } special_pds[MAX_NUM_SPECIAL_PDS] __initdata; | |
2173fc7c | 240 | |
60e26435 | 241 | static unsigned int num_special_pds __initdata; |
2173fc7c | 242 | |
1632ff16 GU |
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 | ||
60e26435 GU |
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; | |
2173fc7c | 255 | |
60e26435 GU |
256 | pd = of_parse_phandle(np, "power-domains", 0); |
257 | if (!pd) | |
258 | return; | |
2173fc7c | 259 | |
60e26435 GU |
260 | for (i = 0; i < num_special_pds; i++) |
261 | if (pd == special_pds[i].pd && type == special_pds[i].type) { | |
2173fc7c | 262 | of_node_put(pd); |
60e26435 | 263 | return; |
2173fc7c GU |
264 | } |
265 | ||
60e26435 GU |
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; | |
2173fc7c GU |
270 | } |
271 | ||
60e26435 GU |
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; | |
1632ff16 | 283 | const struct of_device_id *id; |
60e26435 GU |
284 | |
285 | /* PM domains containing CPUs */ | |
286 | for_each_node_by_type(np, "cpu") | |
287 | add_special_pd(np, PD_CPU); | |
288 | ||
2173fc7c GU |
289 | /* PM domain containing console */ |
290 | if (of_stdout) | |
60e26435 | 291 | add_special_pd(of_stdout, PD_CONSOLE); |
2173fc7c | 292 | |
1632ff16 GU |
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); | |
2173fc7c GU |
296 | } |
297 | ||
298 | static void __init put_special_pds(void) | |
299 | { | |
300 | unsigned int i; | |
301 | ||
60e26435 GU |
302 | for (i = 0; i < num_special_pds; i++) |
303 | of_node_put(special_pds[i].pd); | |
2173fc7c GU |
304 | } |
305 | ||
60e26435 | 306 | static enum pd_types __init pd_type(const struct device_node *pd) |
2173fc7c GU |
307 | { |
308 | unsigned int i; | |
309 | ||
60e26435 GU |
310 | for (i = 0; i < num_special_pds; i++) |
311 | if (pd == special_pds[i].pd) | |
312 | return special_pds[i].type; | |
2173fc7c | 313 | |
60e26435 | 314 | return PD_NORMAL; |
2173fc7c GU |
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 | ||
60e26435 GU |
322 | switch (pd_type(np)) { |
323 | case PD_CPU: | |
e43ee86e GU |
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 | */ | |
2173fc7c GU |
328 | pr_debug("PM domain %s contains CPU\n", name); |
329 | pd->gov = &pm_domain_always_on_gov; | |
e43ee86e | 330 | pd->suspend = rmobile_pd_suspend_busy; |
60e26435 GU |
331 | break; |
332 | ||
333 | case PD_CONSOLE: | |
2173fc7c GU |
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; | |
60e26435 GU |
337 | break; |
338 | ||
339 | case PD_DEBUG: | |
e43ee86e GU |
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 | */ | |
2173fc7c GU |
345 | pr_debug("PM domain %s contains Coresight-ETM\n", name); |
346 | pd->gov = &pm_domain_always_on_gov; | |
e43ee86e | 347 | pd->suspend = rmobile_pd_suspend_busy; |
60e26435 GU |
348 | break; |
349 | ||
1632ff16 GU |
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 | ||
60e26435 GU |
360 | case PD_NORMAL: |
361 | break; | |
2173fc7c GU |
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 */ |