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