ARM: shmobile: Set PM domain on/off latencies directly
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-shmobile / pm-sh7372.c
1 /*
2  * sh7372 Power management support
3  *
4  *  Copyright (C) 2011 Magnus Damm
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10
11 #include <linux/pm.h>
12 #include <linux/suspend.h>
13 #include <linux/cpuidle.h>
14 #include <linux/module.h>
15 #include <linux/list.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/pm_clock.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/irq.h>
22 #include <linux/bitrev.h>
23 #include <linux/console.h>
24 #include <asm/io.h>
25 #include <asm/tlbflush.h>
26 #include <asm/suspend.h>
27 #include <mach/common.h>
28 #include <mach/sh7372.h>
29 #include <mach/pm-rmobile.h>
30
31 /* DBG */
32 #define DBGREG1 0xe6100020
33 #define DBGREG9 0xe6100040
34
35 /* CPGA */
36 #define SYSTBCR 0xe6150024
37 #define MSTPSR0 0xe6150030
38 #define MSTPSR1 0xe6150038
39 #define MSTPSR2 0xe6150040
40 #define MSTPSR3 0xe6150048
41 #define MSTPSR4 0xe615004c
42 #define PLLC01STPCR 0xe61500c8
43
44 /* SYSC */
45 #define SBAR 0xe6180020
46 #define WUPRMSK 0xe6180028
47 #define WUPSMSK 0xe618002c
48 #define WUPSMSK2 0xe6180048
49 #define WUPSFAC 0xe6180098
50 #define IRQCR 0xe618022c
51 #define IRQCR2 0xe6180238
52 #define IRQCR3 0xe6180244
53 #define IRQCR4 0xe6180248
54 #define PDNSEL 0xe6180254
55
56 /* INTC */
57 #define ICR1A 0xe6900000
58 #define ICR2A 0xe6900004
59 #define ICR3A 0xe6900008
60 #define ICR4A 0xe690000c
61 #define INTMSK00A 0xe6900040
62 #define INTMSK10A 0xe6900044
63 #define INTMSK20A 0xe6900048
64 #define INTMSK30A 0xe690004c
65
66 /* MFIS */
67 #define SMFRAM 0xe6a70000
68
69 /* AP-System Core */
70 #define APARMBAREA 0xe6f10020
71
72 #ifdef CONFIG_PM
73
74 #define PM_DOMAIN_ON_OFF_LATENCY_NS     250000
75
76 static int sh7372_a4r_pd_suspend(void)
77 {
78         sh7372_intcs_suspend();
79         __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
80         return 0;
81 }
82
83 static bool a4s_suspend_ready;
84
85 static int sh7372_a4s_pd_suspend(void)
86 {
87         /*
88          * The A4S domain contains the CPU core and therefore it should
89          * only be turned off if the CPU is not in use.  This may happen
90          * during system suspend, when SYSC is going to be used for generating
91          * resume signals and a4s_suspend_ready is set to let
92          * sh7372_enter_suspend() know that it can turn A4S off.
93          */
94         a4s_suspend_ready = true;
95         return -EBUSY;
96 }
97
98 static void sh7372_a4s_pd_resume(void)
99 {
100         a4s_suspend_ready = false;
101 }
102
103 static int sh7372_a3sp_pd_suspend(void)
104 {
105         /*
106          * Serial consoles make use of SCIF hardware located in A3SP,
107          * keep such power domain on if "no_console_suspend" is set.
108          */
109         return console_suspend_enabled ? 0 : -EBUSY;
110 }
111
112 static struct rmobile_pm_domain sh7372_pm_domains[] = {
113         {
114                 .genpd.name = "A4LC",
115                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
116                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
117                 .bit_shift = 1,
118         },
119         {
120                 .genpd.name = "A4MP",
121                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
122                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
123                 .bit_shift = 2,
124         },
125         {
126                 .genpd.name = "D4",
127                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
128                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
129                 .bit_shift = 3,
130         },
131         {
132                 .genpd.name = "A4R",
133                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
134                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
135                 .bit_shift = 5,
136                 .suspend = sh7372_a4r_pd_suspend,
137                 .resume = sh7372_intcs_resume,
138         },
139         {
140                 .genpd.name = "A3RV",
141                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
142                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
143                 .bit_shift = 6,
144         },
145         {
146                 .genpd.name = "A3RI",
147                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
148                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
149                 .bit_shift = 8,
150         },
151         {
152                 .genpd.name = "A4S",
153                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
154                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
155                 .bit_shift = 10,
156                 .gov = &pm_domain_always_on_gov,
157                 .no_debug = true,
158                 .suspend = sh7372_a4s_pd_suspend,
159                 .resume = sh7372_a4s_pd_resume,
160         },
161         {
162                 .genpd.name = "A3SP",
163                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
164                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
165                 .bit_shift = 11,
166                 .gov = &pm_domain_always_on_gov,
167                 .no_debug = true,
168                 .suspend = sh7372_a3sp_pd_suspend,
169         },
170         {
171                 .genpd.name = "A3SG",
172                 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
173                 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
174                 .bit_shift = 13,
175         },
176 };
177
178 void __init sh7372_init_pm_domains(void)
179 {
180         rmobile_init_domains(sh7372_pm_domains, ARRAY_SIZE(sh7372_pm_domains));
181         pm_genpd_add_subdomain_names("A4LC", "A3RV");
182         pm_genpd_add_subdomain_names("A4R", "A4LC");
183         pm_genpd_add_subdomain_names("A4S", "A3SG");
184         pm_genpd_add_subdomain_names("A4S", "A3SP");
185 }
186
187 #endif /* CONFIG_PM */
188
189 #if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
190 static void sh7372_set_reset_vector(unsigned long address)
191 {
192         /* set reset vector, translate 4k */
193         __raw_writel(address, SBAR);
194         __raw_writel(0, APARMBAREA);
195 }
196
197 static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
198 {
199         if (pllc0_on)
200                 __raw_writel(0, PLLC01STPCR);
201         else
202                 __raw_writel(1 << 28, PLLC01STPCR);
203
204         __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
205         cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
206         __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
207
208          /* disable reset vector translation */
209         __raw_writel(0, SBAR);
210 }
211
212 static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
213 {
214         unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
215         unsigned long msk, msk2;
216
217         /* check active clocks to determine potential wakeup sources */
218
219         mstpsr0 = __raw_readl(MSTPSR0);
220         if ((mstpsr0 & 0x00000003) != 0x00000003) {
221                 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
222                 return 0;
223         }
224
225         mstpsr1 = __raw_readl(MSTPSR1);
226         if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
227                 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
228                 return 0;
229         }
230
231         mstpsr2 = __raw_readl(MSTPSR2);
232         if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
233                 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
234                 return 0;
235         }
236
237         mstpsr3 = __raw_readl(MSTPSR3);
238         if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
239                 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
240                 return 0;
241         }
242
243         mstpsr4 = __raw_readl(MSTPSR4);
244         if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
245                 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
246                 return 0;
247         }
248
249         msk = 0;
250         msk2 = 0;
251
252         /* make bitmaps of limited number of wakeup sources */
253
254         if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
255                 msk |= 1 << 31;
256
257         if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
258                 msk |= 1 << 21;
259
260         if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
261                 msk |= 1 << 2;
262
263         if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
264                 msk |= 1 << 1;
265
266         if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
267                 msk |= 1 << 1;
268
269         if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
270                 msk |= 1 << 1;
271
272         if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
273                 msk2 |= 1 << 17;
274
275         *mskp = msk;
276         *msk2p = msk2;
277
278         return 1;
279 }
280
281 static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
282 {
283         u16 tmp, irqcr1, irqcr2;
284         int k;
285
286         irqcr1 = 0;
287         irqcr2 = 0;
288
289         /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
290         for (k = 0; k <= 7; k++) {
291                 tmp = (icr >> ((7 - k) * 4)) & 0xf;
292                 irqcr1 |= (tmp & 0x03) << (k * 2);
293                 irqcr2 |= (tmp >> 2) << (k * 2);
294         }
295
296         *irqcr1p = irqcr1;
297         *irqcr2p = irqcr2;
298 }
299
300 static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
301 {
302         u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
303         unsigned long tmp;
304
305         /* read IRQ0A -> IRQ15A mask */
306         tmp = bitrev8(__raw_readb(INTMSK00A));
307         tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
308
309         /* setup WUPSMSK from clocks and external IRQ mask */
310         msk = (~msk & 0xc030000f) | (tmp << 4);
311         __raw_writel(msk, WUPSMSK);
312
313         /* propage level/edge trigger for external IRQ 0->15 */
314         sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
315         sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
316         __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
317         __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
318
319         /* read IRQ16A -> IRQ31A mask */
320         tmp = bitrev8(__raw_readb(INTMSK20A));
321         tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
322
323         /* setup WUPSMSK2 from clocks and external IRQ mask */
324         msk2 = (~msk2 & 0x00030000) | tmp;
325         __raw_writel(msk2, WUPSMSK2);
326
327         /* propage level/edge trigger for external IRQ 16->31 */
328         sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
329         sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
330         __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
331         __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
332 }
333
334 static void sh7372_enter_a3sm_common(int pllc0_on)
335 {
336         /* use INTCA together with SYSC for wakeup */
337         sh7372_setup_sysc(1 << 0, 0);
338         sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
339         sh7372_enter_sysc(pllc0_on, 1 << 12);
340 }
341 #endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
342
343 #ifdef CONFIG_CPU_IDLE
344 static int sh7372_do_idle_core_standby(unsigned long unused)
345 {
346         cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
347         return 0;
348 }
349
350 static void sh7372_enter_core_standby(void)
351 {
352         sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
353
354         /* enter sleep mode with SYSTBCR to 0x10 */
355         __raw_writel(0x10, SYSTBCR);
356         cpu_suspend(0, sh7372_do_idle_core_standby);
357         __raw_writel(0, SYSTBCR);
358
359          /* disable reset vector translation */
360         __raw_writel(0, SBAR);
361 }
362
363 static void sh7372_enter_a3sm_pll_on(void)
364 {
365         sh7372_enter_a3sm_common(1);
366 }
367
368 static void sh7372_enter_a3sm_pll_off(void)
369 {
370         sh7372_enter_a3sm_common(0);
371 }
372
373 static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
374 {
375         struct cpuidle_state *state = &drv->states[drv->state_count];
376
377         snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
378         strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
379         state->exit_latency = 10;
380         state->target_residency = 20 + 10;
381         state->flags = CPUIDLE_FLAG_TIME_VALID;
382         shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
383         drv->state_count++;
384
385         state = &drv->states[drv->state_count];
386         snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
387         strncpy(state->desc, "A3SM PLL ON", CPUIDLE_DESC_LEN);
388         state->exit_latency = 20;
389         state->target_residency = 30 + 20;
390         state->flags = CPUIDLE_FLAG_TIME_VALID;
391         shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_on;
392         drv->state_count++;
393
394         state = &drv->states[drv->state_count];
395         snprintf(state->name, CPUIDLE_NAME_LEN, "C4");
396         strncpy(state->desc, "A3SM PLL OFF", CPUIDLE_DESC_LEN);
397         state->exit_latency = 120;
398         state->target_residency = 30 + 120;
399         state->flags = CPUIDLE_FLAG_TIME_VALID;
400         shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_off;
401         drv->state_count++;
402 }
403
404 static void sh7372_cpuidle_init(void)
405 {
406         shmobile_cpuidle_setup = sh7372_cpuidle_setup;
407 }
408 #else
409 static void sh7372_cpuidle_init(void) {}
410 #endif
411
412 #ifdef CONFIG_SUSPEND
413 static void sh7372_enter_a4s_common(int pllc0_on)
414 {
415         sh7372_intca_suspend();
416         memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
417         sh7372_set_reset_vector(SMFRAM);
418         sh7372_enter_sysc(pllc0_on, 1 << 10);
419         sh7372_intca_resume();
420 }
421
422 static int sh7372_enter_suspend(suspend_state_t suspend_state)
423 {
424         unsigned long msk, msk2;
425
426         /* check active clocks to determine potential wakeup sources */
427         if (sh7372_sysc_valid(&msk, &msk2)) {
428                 if (!console_suspend_enabled && a4s_suspend_ready) {
429                         /* convert INTC mask/sense to SYSC mask/sense */
430                         sh7372_setup_sysc(msk, msk2);
431
432                         /* enter A4S sleep with PLLC0 off */
433                         pr_debug("entering A4S\n");
434                         sh7372_enter_a4s_common(0);
435                         return 0;
436                 }
437         }
438
439         /* default to enter A3SM sleep with PLLC0 off */
440         pr_debug("entering A3SM\n");
441         sh7372_enter_a3sm_common(0);
442         return 0;
443 }
444
445 /**
446  * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
447  * @notifier: Unused.
448  * @pm_event: Event being handled.
449  * @unused: Unused.
450  */
451 static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
452                                  unsigned long pm_event, void *unused)
453 {
454         switch (pm_event) {
455         case PM_SUSPEND_PREPARE:
456                 /*
457                  * This is necessary, because the A4R domain has to be "on"
458                  * when suspend_device_irqs() and resume_device_irqs() are
459                  * executed during system suspend and resume, respectively, so
460                  * that those functions don't crash while accessing the INTCS.
461                  */
462                 pm_genpd_name_poweron("A4R");
463                 break;
464         case PM_POST_SUSPEND:
465                 pm_genpd_poweroff_unused();
466                 break;
467         }
468
469         return NOTIFY_DONE;
470 }
471
472 static void sh7372_suspend_init(void)
473 {
474         shmobile_suspend_ops.enter = sh7372_enter_suspend;
475         pm_notifier(sh7372_pm_notifier_fn, 0);
476 }
477 #else
478 static void sh7372_suspend_init(void) {}
479 #endif
480
481 void __init sh7372_pm_init(void)
482 {
483         /* enable DBG hardware block to kick SYSC */
484         __raw_writel(0x0000a500, DBGREG9);
485         __raw_writel(0x0000a501, DBGREG9);
486         __raw_writel(0x00000000, DBGREG1);
487
488         /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
489         __raw_writel(0, PDNSEL);
490
491         sh7372_suspend_init();
492         sh7372_cpuidle_init();
493 }