2 * sh7372 Power management support
4 * Copyright (C) 2011 Magnus Damm
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
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>
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>
32 #define DBGREG1 0xe6100020
33 #define DBGREG9 0xe6100040
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
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
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
67 #define SMFRAM 0xe6a70000
70 #define APARMBAREA 0xe6f10020
74 #define PM_DOMAIN_ON_OFF_LATENCY_NS 250000
76 static int sh7372_a4r_pd_suspend(void)
78 sh7372_intcs_suspend();
79 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
83 static bool a4s_suspend_ready;
85 static int sh7372_a4s_pd_suspend(void)
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.
94 a4s_suspend_ready = true;
98 static void sh7372_a4s_pd_resume(void)
100 a4s_suspend_ready = false;
103 static int sh7372_a3sp_pd_suspend(void)
106 * Serial consoles make use of SCIF hardware located in A3SP,
107 * keep such power domain on if "no_console_suspend" is set.
109 return console_suspend_enabled ? 0 : -EBUSY;
112 static struct rmobile_pm_domain sh7372_pm_domains[] = {
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,
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,
127 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
128 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
133 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
134 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
136 .suspend = sh7372_a4r_pd_suspend,
137 .resume = sh7372_intcs_resume,
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,
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,
153 .genpd.power_on_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
154 .genpd.power_off_latency_ns = PM_DOMAIN_ON_OFF_LATENCY_NS,
156 .gov = &pm_domain_always_on_gov,
158 .suspend = sh7372_a4s_pd_suspend,
159 .resume = sh7372_a4s_pd_resume,
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,
166 .gov = &pm_domain_always_on_gov,
168 .suspend = sh7372_a3sp_pd_suspend,
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,
178 void __init sh7372_init_pm_domains(void)
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");
187 #endif /* CONFIG_PM */
189 #if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
190 static void sh7372_set_reset_vector(unsigned long address)
192 /* set reset vector, translate 4k */
193 __raw_writel(address, SBAR);
194 __raw_writel(0, APARMBAREA);
197 static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
200 __raw_writel(0, PLLC01STPCR);
202 __raw_writel(1 << 28, PLLC01STPCR);
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 */
208 /* disable reset vector translation */
209 __raw_writel(0, SBAR);
212 static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
214 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
215 unsigned long msk, msk2;
217 /* check active clocks to determine potential wakeup sources */
219 mstpsr0 = __raw_readl(MSTPSR0);
220 if ((mstpsr0 & 0x00000003) != 0x00000003) {
221 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
225 mstpsr1 = __raw_readl(MSTPSR1);
226 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
227 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
231 mstpsr2 = __raw_readl(MSTPSR2);
232 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
233 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
237 mstpsr3 = __raw_readl(MSTPSR3);
238 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
239 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
243 mstpsr4 = __raw_readl(MSTPSR4);
244 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
245 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
252 /* make bitmaps of limited number of wakeup sources */
254 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
257 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
260 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
263 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
266 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
269 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
272 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
281 static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
283 u16 tmp, irqcr1, irqcr2;
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);
300 static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
302 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
305 /* read IRQ0A -> IRQ15A mask */
306 tmp = bitrev8(__raw_readb(INTMSK00A));
307 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
309 /* setup WUPSMSK from clocks and external IRQ mask */
310 msk = (~msk & 0xc030000f) | (tmp << 4);
311 __raw_writel(msk, WUPSMSK);
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);
319 /* read IRQ16A -> IRQ31A mask */
320 tmp = bitrev8(__raw_readb(INTMSK20A));
321 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
323 /* setup WUPSMSK2 from clocks and external IRQ mask */
324 msk2 = (~msk2 & 0x00030000) | tmp;
325 __raw_writel(msk2, WUPSMSK2);
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);
334 static void sh7372_enter_a3sm_common(int pllc0_on)
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);
341 #endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
343 #ifdef CONFIG_CPU_IDLE
344 static int sh7372_do_idle_core_standby(unsigned long unused)
346 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
350 static void sh7372_enter_core_standby(void)
352 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
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);
359 /* disable reset vector translation */
360 __raw_writel(0, SBAR);
363 static void sh7372_enter_a3sm_pll_on(void)
365 sh7372_enter_a3sm_common(1);
368 static void sh7372_enter_a3sm_pll_off(void)
370 sh7372_enter_a3sm_common(0);
373 static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
375 struct cpuidle_state *state = &drv->states[drv->state_count];
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;
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;
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;
404 static void sh7372_cpuidle_init(void)
406 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
409 static void sh7372_cpuidle_init(void) {}
412 #ifdef CONFIG_SUSPEND
413 static void sh7372_enter_a4s_common(int pllc0_on)
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();
422 static int sh7372_enter_suspend(suspend_state_t suspend_state)
424 unsigned long msk, msk2;
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);
432 /* enter A4S sleep with PLLC0 off */
433 pr_debug("entering A4S\n");
434 sh7372_enter_a4s_common(0);
439 /* default to enter A3SM sleep with PLLC0 off */
440 pr_debug("entering A3SM\n");
441 sh7372_enter_a3sm_common(0);
446 * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
448 * @pm_event: Event being handled.
451 static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
452 unsigned long pm_event, void *unused)
455 case PM_SUSPEND_PREPARE:
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.
462 pm_genpd_name_poweron("A4R");
464 case PM_POST_SUSPEND:
465 pm_genpd_poweroff_unused();
472 static void sh7372_suspend_init(void)
474 shmobile_suspend_ops.enter = sh7372_enter_suspend;
475 pm_notifier(sh7372_pm_notifier_fn, 0);
478 static void sh7372_suspend_init(void) {}
481 void __init sh7372_pm_init(void)
483 /* enable DBG hardware block to kick SYSC */
484 __raw_writel(0x0000a500, DBGREG9);
485 __raw_writel(0x0000a501, DBGREG9);
486 __raw_writel(0x00000000, DBGREG1);
488 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
489 __raw_writel(0, PDNSEL);
491 sh7372_suspend_init();
492 sh7372_cpuidle_init();