blob: 0d722b5b52195bbb497f3b33f5bd4950488a5876 [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 -070074static int sh7372_a4r_pd_suspend(void)
75{
76 sh7372_intcs_suspend();
77 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
78 return 0;
79}
80
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +020081static bool a4s_suspend_ready;
82
Rafael J. Wysockie7e59a42012-08-07 01:12:56 +020083static int sh7372_a4s_pd_suspend(void)
Kuninori Morimotob9299a72012-07-05 01:28:32 -070084{
85 /*
86 * The A4S domain contains the CPU core and therefore it should
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +020087 * only be turned off if the CPU is not in use. This may happen
88 * during system suspend, when SYSC is going to be used for generating
89 * resume signals and a4s_suspend_ready is set to let
90 * sh7372_enter_suspend() know that it can turn A4S off.
Kuninori Morimotob9299a72012-07-05 01:28:32 -070091 */
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +020092 a4s_suspend_ready = true;
Kuninori Morimotob9299a72012-07-05 01:28:32 -070093 return -EBUSY;
94}
95
Rafael J. Wysockie7e59a42012-08-07 01:12:56 +020096static void sh7372_a4s_pd_resume(void)
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +020097{
98 a4s_suspend_ready = false;
99}
100
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700101static int sh7372_a3sp_pd_suspend(void)
102{
103 /*
104 * Serial consoles make use of SCIF hardware located in A3SP,
105 * keep such power domain on if "no_console_suspend" is set.
106 */
107 return console_suspend_enabled ? 0 : -EBUSY;
108}
109
Rafael J. Wysockie7e59a42012-08-07 01:12:56 +0200110static struct rmobile_pm_domain sh7372_pm_domains[] = {
111 {
112 .genpd.name = "A4LC",
113 .bit_shift = 1,
114 },
115 {
116 .genpd.name = "A4MP",
117 .bit_shift = 2,
118 },
119 {
120 .genpd.name = "D4",
121 .bit_shift = 3,
122 },
123 {
124 .genpd.name = "A4R",
125 .bit_shift = 5,
126 .suspend = sh7372_a4r_pd_suspend,
127 .resume = sh7372_intcs_resume,
128 },
129 {
130 .genpd.name = "A3RV",
131 .bit_shift = 6,
132 },
133 {
134 .genpd.name = "A3RI",
135 .bit_shift = 8,
136 },
137 {
138 .genpd.name = "A4S",
139 .bit_shift = 10,
140 .gov = &pm_domain_always_on_gov,
141 .no_debug = true,
142 .suspend = sh7372_a4s_pd_suspend,
143 .resume = sh7372_a4s_pd_resume,
144 },
145 {
146 .genpd.name = "A3SP",
147 .bit_shift = 11,
148 .gov = &pm_domain_always_on_gov,
149 .no_debug = true,
150 .suspend = sh7372_a3sp_pd_suspend,
151 },
152 {
153 .genpd.name = "A3SG",
154 .bit_shift = 13,
155 },
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700156};
157
Rafael J. Wysockie7e59a42012-08-07 01:12:56 +0200158void __init sh7372_init_pm_domains(void)
159{
160 rmobile_init_domains(sh7372_pm_domains, ARRAY_SIZE(sh7372_pm_domains));
161 pm_genpd_add_subdomain_names("A4LC", "A3RV");
162 pm_genpd_add_subdomain_names("A4R", "A4LC");
163 pm_genpd_add_subdomain_names("A4S", "A3SG");
164 pm_genpd_add_subdomain_names("A4S", "A3SP");
165}
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700166
Kuninori Morimoto1645b762012-06-24 22:00:13 +0200167#endif /* CONFIG_PM */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200168
Magnus Damma0089bd2011-09-25 23:21:02 +0200169#if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
Magnus Dammf7dadb32011-12-23 01:23:07 +0100170static void sh7372_set_reset_vector(unsigned long address)
Magnus Damm97991652011-04-29 02:28:08 +0900171{
Magnus Damm06b84162011-09-25 23:18:42 +0200172 /* set reset vector, translate 4k */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100173 __raw_writel(address, SBAR);
Magnus Damm06b84162011-09-25 23:18:42 +0200174 __raw_writel(0, APARMBAREA);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100175}
176
Magnus Dammf7dadb32011-12-23 01:23:07 +0100177static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
Magnus Dammcf338352011-09-25 23:20:49 +0200178{
Magnus Dammcf338352011-09-25 23:20:49 +0200179 if (pllc0_on)
180 __raw_writel(0, PLLC01STPCR);
181 else
182 __raw_writel(1 << 28, PLLC01STPCR);
183
Magnus Dammcf338352011-09-25 23:20:49 +0200184 __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100185 cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
Magnus Dammcf338352011-09-25 23:20:49 +0200186 __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
187
188 /* disable reset vector translation */
189 __raw_writel(0, SBAR);
190}
191
Magnus Dammf7dadb32011-12-23 01:23:07 +0100192static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
Magnus Dammcf338352011-09-25 23:20:49 +0200193{
194 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
195 unsigned long msk, msk2;
196
197 /* check active clocks to determine potential wakeup sources */
198
199 mstpsr0 = __raw_readl(MSTPSR0);
200 if ((mstpsr0 & 0x00000003) != 0x00000003) {
201 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
202 return 0;
203 }
204
205 mstpsr1 = __raw_readl(MSTPSR1);
206 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
207 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
208 return 0;
209 }
210
211 mstpsr2 = __raw_readl(MSTPSR2);
212 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
213 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
214 return 0;
215 }
216
217 mstpsr3 = __raw_readl(MSTPSR3);
218 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
219 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
220 return 0;
221 }
222
223 mstpsr4 = __raw_readl(MSTPSR4);
224 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
225 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
226 return 0;
227 }
228
229 msk = 0;
230 msk2 = 0;
231
232 /* make bitmaps of limited number of wakeup sources */
233
234 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
235 msk |= 1 << 31;
236
237 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
238 msk |= 1 << 21;
239
240 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
241 msk |= 1 << 2;
242
243 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
244 msk |= 1 << 1;
245
246 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
247 msk |= 1 << 1;
248
249 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
250 msk |= 1 << 1;
251
252 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
253 msk2 |= 1 << 17;
254
255 *mskp = msk;
256 *msk2p = msk2;
257
258 return 1;
259}
260
261static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
262{
263 u16 tmp, irqcr1, irqcr2;
264 int k;
265
266 irqcr1 = 0;
267 irqcr2 = 0;
268
269 /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
270 for (k = 0; k <= 7; k++) {
271 tmp = (icr >> ((7 - k) * 4)) & 0xf;
272 irqcr1 |= (tmp & 0x03) << (k * 2);
273 irqcr2 |= (tmp >> 2) << (k * 2);
274 }
275
276 *irqcr1p = irqcr1;
277 *irqcr2p = irqcr2;
278}
279
Magnus Dammf7dadb32011-12-23 01:23:07 +0100280static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
Magnus Dammcf338352011-09-25 23:20:49 +0200281{
282 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
283 unsigned long tmp;
284
285 /* read IRQ0A -> IRQ15A mask */
286 tmp = bitrev8(__raw_readb(INTMSK00A));
287 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
288
289 /* setup WUPSMSK from clocks and external IRQ mask */
290 msk = (~msk & 0xc030000f) | (tmp << 4);
291 __raw_writel(msk, WUPSMSK);
292
293 /* propage level/edge trigger for external IRQ 0->15 */
294 sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
295 sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
296 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
297 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
298
299 /* read IRQ16A -> IRQ31A mask */
300 tmp = bitrev8(__raw_readb(INTMSK20A));
301 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
302
303 /* setup WUPSMSK2 from clocks and external IRQ mask */
304 msk2 = (~msk2 & 0x00030000) | tmp;
305 __raw_writel(msk2, WUPSMSK2);
306
307 /* propage level/edge trigger for external IRQ 16->31 */
308 sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
309 sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
310 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
311 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
312}
Magnus Dammf7dadb32011-12-23 01:23:07 +0100313
314static void sh7372_enter_a3sm_common(int pllc0_on)
315{
Magnus Damm591e2ac2012-07-05 14:46:07 +0900316 /* use INTCA together with SYSC for wakeup */
317 sh7372_setup_sysc(1 << 0, 0);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100318 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
319 sh7372_enter_sysc(pllc0_on, 1 << 12);
320}
Rafael J. Wysocki911a4722012-07-11 22:55:12 +0200321#endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100322
Magnus Damm082a8ca2011-04-29 02:39:32 +0900323#ifdef CONFIG_CPU_IDLE
Magnus Damm591e2ac2012-07-05 14:46:07 +0900324static int sh7372_do_idle_core_standby(unsigned long unused)
325{
326 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
327 return 0;
328}
329
330static void sh7372_enter_core_standby(void)
331{
332 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
333
334 /* enter sleep mode with SYSTBCR to 0x10 */
335 __raw_writel(0x10, SYSTBCR);
336 cpu_suspend(0, sh7372_do_idle_core_standby);
337 __raw_writel(0, SYSTBCR);
338
339 /* disable reset vector translation */
340 __raw_writel(0, SBAR);
341}
Magnus Dammcf338352011-09-25 23:20:49 +0200342
Magnus Damm3abd69d2012-07-05 14:46:47 +0900343static void sh7372_enter_a3sm_pll_on(void)
344{
345 sh7372_enter_a3sm_common(1);
346}
347
348static void sh7372_enter_a3sm_pll_off(void)
349{
350 sh7372_enter_a3sm_common(0);
351}
352
Magnus Dammb73b5c42011-11-11 14:01:30 +0900353static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
Magnus Damm082a8ca2011-04-29 02:39:32 +0900354{
Magnus Dammb73b5c42011-11-11 14:01:30 +0900355 struct cpuidle_state *state = &drv->states[drv->state_count];
Magnus Damm082a8ca2011-04-29 02:39:32 +0900356
Magnus Damm082a8ca2011-04-29 02:39:32 +0900357 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
358 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
359 state->exit_latency = 10;
360 state->target_residency = 20 + 10;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900361 state->flags = CPUIDLE_FLAG_TIME_VALID;
362 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
Magnus Damm3abd69d2012-07-05 14:46:47 +0900363 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900364
Magnus Damm3abd69d2012-07-05 14:46:47 +0900365 state = &drv->states[drv->state_count];
366 snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
367 strncpy(state->desc, "A3SM PLL ON", CPUIDLE_DESC_LEN);
368 state->exit_latency = 20;
369 state->target_residency = 30 + 20;
370 state->flags = CPUIDLE_FLAG_TIME_VALID;
371 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_on;
372 drv->state_count++;
373
374 state = &drv->states[drv->state_count];
375 snprintf(state->name, CPUIDLE_NAME_LEN, "C4");
376 strncpy(state->desc, "A3SM PLL OFF", CPUIDLE_DESC_LEN);
377 state->exit_latency = 120;
378 state->target_residency = 30 + 120;
379 state->flags = CPUIDLE_FLAG_TIME_VALID;
380 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_off;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900381 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900382}
383
384static void sh7372_cpuidle_init(void)
385{
386 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
387}
388#else
389static void sh7372_cpuidle_init(void) {}
390#endif
391
392#ifdef CONFIG_SUSPEND
Magnus Damm3abd69d2012-07-05 14:46:47 +0900393static void sh7372_enter_a4s_common(int pllc0_on)
394{
395 sh7372_intca_suspend();
396 memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
397 sh7372_set_reset_vector(SMFRAM);
398 sh7372_enter_sysc(pllc0_on, 1 << 10);
399 sh7372_intca_resume();
400}
Magnus Dammcf338352011-09-25 23:20:49 +0200401
Magnus Damm97991652011-04-29 02:28:08 +0900402static int sh7372_enter_suspend(suspend_state_t suspend_state)
403{
Magnus Dammcf338352011-09-25 23:20:49 +0200404 unsigned long msk, msk2;
405
406 /* check active clocks to determine potential wakeup sources */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100407 if (sh7372_sysc_valid(&msk, &msk2)) {
Rafael J. Wysocki70fe7b22012-08-15 20:54:15 +0200408 if (!console_suspend_enabled && a4s_suspend_ready) {
Magnus Damm591e2ac2012-07-05 14:46:07 +0900409 /* convert INTC mask/sense to SYSC mask/sense */
410 sh7372_setup_sysc(msk, msk2);
411
Magnus Dammf7dadb32011-12-23 01:23:07 +0100412 /* enter A4S sleep with PLLC0 off */
413 pr_debug("entering A4S\n");
414 sh7372_enter_a4s_common(0);
Magnus Damm591e2ac2012-07-05 14:46:07 +0900415 return 0;
Magnus Dammf7dadb32011-12-23 01:23:07 +0100416 }
Magnus Dammcf338352011-09-25 23:20:49 +0200417 }
Magnus Damm591e2ac2012-07-05 14:46:07 +0900418
419 /* default to enter A3SM sleep with PLLC0 off */
420 pr_debug("entering A3SM\n");
421 sh7372_enter_a3sm_common(0);
Magnus Damm97991652011-04-29 02:28:08 +0900422 return 0;
423}
424
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100425/**
426 * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
427 * @notifier: Unused.
428 * @pm_event: Event being handled.
429 * @unused: Unused.
430 */
431static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
432 unsigned long pm_event, void *unused)
433{
434 switch (pm_event) {
435 case PM_SUSPEND_PREPARE:
436 /*
437 * This is necessary, because the A4R domain has to be "on"
438 * when suspend_device_irqs() and resume_device_irqs() are
439 * executed during system suspend and resume, respectively, so
440 * that those functions don't crash while accessing the INTCS.
441 */
Rafael J. Wysockie7e59a42012-08-07 01:12:56 +0200442 pm_genpd_name_poweron("A4R");
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100443 break;
444 case PM_POST_SUSPEND:
445 pm_genpd_poweroff_unused();
446 break;
447 }
448
449 return NOTIFY_DONE;
450}
451
Magnus Damm97991652011-04-29 02:28:08 +0900452static void sh7372_suspend_init(void)
453{
454 shmobile_suspend_ops.enter = sh7372_enter_suspend;
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100455 pm_notifier(sh7372_pm_notifier_fn, 0);
Magnus Damm97991652011-04-29 02:28:08 +0900456}
457#else
458static void sh7372_suspend_init(void) {}
459#endif
460
Magnus Damm97991652011-04-29 02:28:08 +0900461void __init sh7372_pm_init(void)
462{
463 /* enable DBG hardware block to kick SYSC */
464 __raw_writel(0x0000a500, DBGREG9);
465 __raw_writel(0x0000a501, DBGREG9);
466 __raw_writel(0x00000000, DBGREG1);
467
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200468 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
469 __raw_writel(0, PDNSEL);
470
Magnus Damm97991652011-04-29 02:28:08 +0900471 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900472 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900473}