blob: 0a4804694deee043dffcbf8d4afbce80f5b29fe7 [file] [log] [blame]
Magnus Damm97991652011-04-29 02:28:08 +09001/*
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>
Magnus Damm082a8ca2011-04-29 02:39:32 +090013#include <linux/cpuidle.h>
Magnus Damm97991652011-04-29 02:28:08 +090014#include <linux/module.h>
15#include <linux/list.h>
16#include <linux/err.h>
17#include <linux/slab.h>
Rafael J. Wysockib5e8d262011-08-25 15:34:19 +020018#include <linux/pm_clock.h>
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020019#include <linux/platform_device.h>
20#include <linux/delay.h>
Magnus Dammcf338352011-09-25 23:20:49 +020021#include <linux/irq.h>
22#include <linux/bitrev.h>
Magnus Damm056879d2011-11-10 00:44:01 +010023#include <linux/console.h>
Magnus Damm97991652011-04-29 02:28:08 +090024#include <asm/io.h>
25#include <asm/tlbflush.h>
Magnus Damm06b84162011-09-25 23:18:42 +020026#include <asm/suspend.h>
Magnus Damm97991652011-04-29 02:28:08 +090027#include <mach/common.h>
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020028#include <mach/sh7372.h>
Kuninori Morimotob9299a72012-07-05 01:28:32 -070029#include <mach/pm-rmobile.h>
Magnus Damm97991652011-04-29 02:28:08 +090030
Magnus Dammcf338352011-09-25 23:20:49 +020031/* DBG */
32#define DBGREG1 0xe6100020
33#define DBGREG9 0xe6100040
Magnus Damm97991652011-04-29 02:28:08 +090034
Magnus Dammcf338352011-09-25 23:20:49 +020035/* 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 */
Magnus Dammcf338352011-09-25 23:20:49 +020045#define SBAR 0xe6180020
Magnus Damm382414b2011-10-19 23:52:50 +020046#define WUPRMSK 0xe6180028
Magnus Dammcf338352011-09-25 23:20:49 +020047#define WUPSMSK 0xe618002c
48#define WUPSMSK2 0xe6180048
Magnus Dammcf338352011-09-25 23:20:49 +020049#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
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020071
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020072#ifdef CONFIG_PM
73
Kuninori Morimotob9299a72012-07-05 01:28:32 -070074struct rmobile_pm_domain sh7372_pd_a4lc = {
75 .genpd.name = "A4LC",
76 .bit_shift = 1,
77};
78
79struct rmobile_pm_domain sh7372_pd_a4mp = {
80 .genpd.name = "A4MP",
81 .bit_shift = 2,
82};
83
84struct rmobile_pm_domain sh7372_pd_d4 = {
85 .genpd.name = "D4",
86 .bit_shift = 3,
87};
88
89static int sh7372_a4r_pd_suspend(void)
90{
91 sh7372_intcs_suspend();
92 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
93 return 0;
94}
95
96struct rmobile_pm_domain sh7372_pd_a4r = {
97 .genpd.name = "A4R",
98 .bit_shift = 5,
99 .suspend = sh7372_a4r_pd_suspend,
100 .resume = sh7372_intcs_resume,
101};
102
103struct rmobile_pm_domain sh7372_pd_a3rv = {
104 .genpd.name = "A3RV",
105 .bit_shift = 6,
106};
107
108struct rmobile_pm_domain sh7372_pd_a3ri = {
109 .genpd.name = "A3RI",
110 .bit_shift = 8,
111};
112
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200113static bool a4s_suspend_ready;
114
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700115static int sh7372_pd_a4s_suspend(void)
116{
117 /*
118 * The A4S domain contains the CPU core and therefore it should
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200119 * only be turned off if the CPU is not in use. This may happen
120 * during system suspend, when SYSC is going to be used for generating
121 * resume signals and a4s_suspend_ready is set to let
122 * sh7372_enter_suspend() know that it can turn A4S off.
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700123 */
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200124 a4s_suspend_ready = true;
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700125 return -EBUSY;
126}
127
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200128static void sh7372_pd_a4s_resume(void)
129{
130 a4s_suspend_ready = false;
131}
132
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700133struct rmobile_pm_domain sh7372_pd_a4s = {
134 .genpd.name = "A4S",
135 .bit_shift = 10,
136 .gov = &pm_domain_always_on_gov,
137 .no_debug = true,
138 .suspend = sh7372_pd_a4s_suspend,
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200139 .resume = sh7372_pd_a4s_resume,
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700140};
141
142static int sh7372_a3sp_pd_suspend(void)
143{
144 /*
145 * Serial consoles make use of SCIF hardware located in A3SP,
146 * keep such power domain on if "no_console_suspend" is set.
147 */
148 return console_suspend_enabled ? 0 : -EBUSY;
149}
150
151struct rmobile_pm_domain sh7372_pd_a3sp = {
152 .genpd.name = "A3SP",
153 .bit_shift = 11,
154 .gov = &pm_domain_always_on_gov,
155 .no_debug = true,
156 .suspend = sh7372_a3sp_pd_suspend,
157};
158
159struct rmobile_pm_domain sh7372_pd_a3sg = {
160 .genpd.name = "A3SG",
161 .bit_shift = 13,
162};
163
Kuninori Morimoto1645b762012-06-24 22:00:13 +0200164#endif /* CONFIG_PM */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200165
Magnus Damma0089bd2011-09-25 23:21:02 +0200166#if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
Magnus Dammf7dadb32011-12-23 01:23:07 +0100167static void sh7372_set_reset_vector(unsigned long address)
Magnus Damm97991652011-04-29 02:28:08 +0900168{
Magnus Damm06b84162011-09-25 23:18:42 +0200169 /* set reset vector, translate 4k */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100170 __raw_writel(address, SBAR);
Magnus Damm06b84162011-09-25 23:18:42 +0200171 __raw_writel(0, APARMBAREA);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100172}
173
Magnus Dammf7dadb32011-12-23 01:23:07 +0100174static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
Magnus Dammcf338352011-09-25 23:20:49 +0200175{
Magnus Dammcf338352011-09-25 23:20:49 +0200176 if (pllc0_on)
177 __raw_writel(0, PLLC01STPCR);
178 else
179 __raw_writel(1 << 28, PLLC01STPCR);
180
Magnus Dammcf338352011-09-25 23:20:49 +0200181 __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100182 cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
Magnus Dammcf338352011-09-25 23:20:49 +0200183 __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
184
185 /* disable reset vector translation */
186 __raw_writel(0, SBAR);
187}
188
Magnus Dammf7dadb32011-12-23 01:23:07 +0100189static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
Magnus Dammcf338352011-09-25 23:20:49 +0200190{
191 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
192 unsigned long msk, msk2;
193
194 /* check active clocks to determine potential wakeup sources */
195
196 mstpsr0 = __raw_readl(MSTPSR0);
197 if ((mstpsr0 & 0x00000003) != 0x00000003) {
198 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
199 return 0;
200 }
201
202 mstpsr1 = __raw_readl(MSTPSR1);
203 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
204 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
205 return 0;
206 }
207
208 mstpsr2 = __raw_readl(MSTPSR2);
209 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
210 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
211 return 0;
212 }
213
214 mstpsr3 = __raw_readl(MSTPSR3);
215 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
216 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
217 return 0;
218 }
219
220 mstpsr4 = __raw_readl(MSTPSR4);
221 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
222 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
223 return 0;
224 }
225
226 msk = 0;
227 msk2 = 0;
228
229 /* make bitmaps of limited number of wakeup sources */
230
231 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
232 msk |= 1 << 31;
233
234 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
235 msk |= 1 << 21;
236
237 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
238 msk |= 1 << 2;
239
240 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
241 msk |= 1 << 1;
242
243 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
244 msk |= 1 << 1;
245
246 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
247 msk |= 1 << 1;
248
249 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
250 msk2 |= 1 << 17;
251
252 *mskp = msk;
253 *msk2p = msk2;
254
255 return 1;
256}
257
258static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
259{
260 u16 tmp, irqcr1, irqcr2;
261 int k;
262
263 irqcr1 = 0;
264 irqcr2 = 0;
265
266 /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
267 for (k = 0; k <= 7; k++) {
268 tmp = (icr >> ((7 - k) * 4)) & 0xf;
269 irqcr1 |= (tmp & 0x03) << (k * 2);
270 irqcr2 |= (tmp >> 2) << (k * 2);
271 }
272
273 *irqcr1p = irqcr1;
274 *irqcr2p = irqcr2;
275}
276
Magnus Dammf7dadb32011-12-23 01:23:07 +0100277static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
Magnus Dammcf338352011-09-25 23:20:49 +0200278{
279 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
280 unsigned long tmp;
281
282 /* read IRQ0A -> IRQ15A mask */
283 tmp = bitrev8(__raw_readb(INTMSK00A));
284 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
285
286 /* setup WUPSMSK from clocks and external IRQ mask */
287 msk = (~msk & 0xc030000f) | (tmp << 4);
288 __raw_writel(msk, WUPSMSK);
289
290 /* propage level/edge trigger for external IRQ 0->15 */
291 sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
292 sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
293 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
294 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
295
296 /* read IRQ16A -> IRQ31A mask */
297 tmp = bitrev8(__raw_readb(INTMSK20A));
298 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
299
300 /* setup WUPSMSK2 from clocks and external IRQ mask */
301 msk2 = (~msk2 & 0x00030000) | tmp;
302 __raw_writel(msk2, WUPSMSK2);
303
304 /* propage level/edge trigger for external IRQ 16->31 */
305 sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
306 sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
307 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
308 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
309}
Magnus Dammf7dadb32011-12-23 01:23:07 +0100310
311static void sh7372_enter_a3sm_common(int pllc0_on)
312{
Magnus Damm591e2ac2012-07-05 14:46:07 +0900313 /* use INTCA together with SYSC for wakeup */
314 sh7372_setup_sysc(1 << 0, 0);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100315 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
316 sh7372_enter_sysc(pllc0_on, 1 << 12);
317}
Rafael J. Wysocki911a4722012-07-11 22:55:12 +0200318#endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100319
Magnus Damm082a8ca2011-04-29 02:39:32 +0900320#ifdef CONFIG_CPU_IDLE
Magnus Damm591e2ac2012-07-05 14:46:07 +0900321static int sh7372_do_idle_core_standby(unsigned long unused)
322{
323 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
324 return 0;
325}
326
327static void sh7372_enter_core_standby(void)
328{
329 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
330
331 /* enter sleep mode with SYSTBCR to 0x10 */
332 __raw_writel(0x10, SYSTBCR);
333 cpu_suspend(0, sh7372_do_idle_core_standby);
334 __raw_writel(0, SYSTBCR);
335
336 /* disable reset vector translation */
337 __raw_writel(0, SBAR);
338}
Magnus Dammcf338352011-09-25 23:20:49 +0200339
Magnus Damm3abd69d2012-07-05 14:46:47 +0900340static void sh7372_enter_a3sm_pll_on(void)
341{
342 sh7372_enter_a3sm_common(1);
343}
344
345static void sh7372_enter_a3sm_pll_off(void)
346{
347 sh7372_enter_a3sm_common(0);
348}
349
Magnus Dammb73b5c42011-11-11 14:01:30 +0900350static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
Magnus Damm082a8ca2011-04-29 02:39:32 +0900351{
Magnus Dammb73b5c42011-11-11 14:01:30 +0900352 struct cpuidle_state *state = &drv->states[drv->state_count];
Magnus Damm082a8ca2011-04-29 02:39:32 +0900353
Magnus Damm082a8ca2011-04-29 02:39:32 +0900354 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
355 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
356 state->exit_latency = 10;
357 state->target_residency = 20 + 10;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900358 state->flags = CPUIDLE_FLAG_TIME_VALID;
359 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
Magnus Damm3abd69d2012-07-05 14:46:47 +0900360 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900361
Magnus Damm3abd69d2012-07-05 14:46:47 +0900362 state = &drv->states[drv->state_count];
363 snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
364 strncpy(state->desc, "A3SM PLL ON", CPUIDLE_DESC_LEN);
365 state->exit_latency = 20;
366 state->target_residency = 30 + 20;
367 state->flags = CPUIDLE_FLAG_TIME_VALID;
368 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_on;
369 drv->state_count++;
370
371 state = &drv->states[drv->state_count];
372 snprintf(state->name, CPUIDLE_NAME_LEN, "C4");
373 strncpy(state->desc, "A3SM PLL OFF", CPUIDLE_DESC_LEN);
374 state->exit_latency = 120;
375 state->target_residency = 30 + 120;
376 state->flags = CPUIDLE_FLAG_TIME_VALID;
377 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_off;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900378 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900379}
380
381static void sh7372_cpuidle_init(void)
382{
383 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
384}
385#else
386static void sh7372_cpuidle_init(void) {}
387#endif
388
389#ifdef CONFIG_SUSPEND
Magnus Damm3abd69d2012-07-05 14:46:47 +0900390static void sh7372_enter_a4s_common(int pllc0_on)
391{
392 sh7372_intca_suspend();
393 memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
394 sh7372_set_reset_vector(SMFRAM);
395 sh7372_enter_sysc(pllc0_on, 1 << 10);
396 sh7372_intca_resume();
397}
Magnus Dammcf338352011-09-25 23:20:49 +0200398
Magnus Damm97991652011-04-29 02:28:08 +0900399static int sh7372_enter_suspend(suspend_state_t suspend_state)
400{
Magnus Dammcf338352011-09-25 23:20:49 +0200401 unsigned long msk, msk2;
402
403 /* check active clocks to determine potential wakeup sources */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100404 if (sh7372_sysc_valid(&msk, &msk2)) {
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200405 if (!console_suspend_enabled && a4s_suspend_ready) {
Magnus Damm591e2ac2012-07-05 14:46:07 +0900406 /* convert INTC mask/sense to SYSC mask/sense */
407 sh7372_setup_sysc(msk, msk2);
408
Magnus Dammf7dadb32011-12-23 01:23:07 +0100409 /* enter A4S sleep with PLLC0 off */
410 pr_debug("entering A4S\n");
411 sh7372_enter_a4s_common(0);
Magnus Damm591e2ac2012-07-05 14:46:07 +0900412 return 0;
Magnus Dammf7dadb32011-12-23 01:23:07 +0100413 }
Magnus Dammcf338352011-09-25 23:20:49 +0200414 }
Magnus Damm591e2ac2012-07-05 14:46:07 +0900415
416 /* default to enter A3SM sleep with PLLC0 off */
417 pr_debug("entering A3SM\n");
418 sh7372_enter_a3sm_common(0);
Magnus Damm97991652011-04-29 02:28:08 +0900419 return 0;
420}
421
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100422/**
423 * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
424 * @notifier: Unused.
425 * @pm_event: Event being handled.
426 * @unused: Unused.
427 */
428static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
429 unsigned long pm_event, void *unused)
430{
431 switch (pm_event) {
432 case PM_SUSPEND_PREPARE:
433 /*
434 * This is necessary, because the A4R domain has to be "on"
435 * when suspend_device_irqs() and resume_device_irqs() are
436 * executed during system suspend and resume, respectively, so
437 * that those functions don't crash while accessing the INTCS.
438 */
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700439 pm_genpd_poweron(&sh7372_pd_a4r.genpd);
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100440 break;
441 case PM_POST_SUSPEND:
442 pm_genpd_poweroff_unused();
443 break;
444 }
445
446 return NOTIFY_DONE;
447}
448
Magnus Damm97991652011-04-29 02:28:08 +0900449static void sh7372_suspend_init(void)
450{
451 shmobile_suspend_ops.enter = sh7372_enter_suspend;
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100452 pm_notifier(sh7372_pm_notifier_fn, 0);
Magnus Damm97991652011-04-29 02:28:08 +0900453}
454#else
455static void sh7372_suspend_init(void) {}
456#endif
457
Magnus Damm97991652011-04-29 02:28:08 +0900458void __init sh7372_pm_init(void)
459{
460 /* enable DBG hardware block to kick SYSC */
461 __raw_writel(0x0000a500, DBGREG9);
462 __raw_writel(0x0000a501, DBGREG9);
463 __raw_writel(0x00000000, DBGREG1);
464
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200465 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
466 __raw_writel(0, PDNSEL);
467
Magnus Damm97991652011-04-29 02:28:08 +0900468 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900469 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900470}