blob: 78c98a847433562c01d82c47721820813e8b0df4 [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/system.h>
25#include <asm/io.h>
26#include <asm/tlbflush.h>
Magnus Damm06b84162011-09-25 23:18:42 +020027#include <asm/suspend.h>
Magnus Damm97991652011-04-29 02:28:08 +090028#include <mach/common.h>
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020029#include <mach/sh7372.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 */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020045#define SPDCR 0xe6180008
46#define SWUCR 0xe6180014
Magnus Dammcf338352011-09-25 23:20:49 +020047#define SBAR 0xe6180020
Magnus Damm382414b2011-10-19 23:52:50 +020048#define WUPRMSK 0xe6180028
Magnus Dammcf338352011-09-25 23:20:49 +020049#define WUPSMSK 0xe618002c
50#define WUPSMSK2 0xe6180048
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020051#define PSTR 0xe6180080
Magnus Dammcf338352011-09-25 23:20:49 +020052#define WUPSFAC 0xe6180098
53#define IRQCR 0xe618022c
54#define IRQCR2 0xe6180238
55#define IRQCR3 0xe6180244
56#define IRQCR4 0xe6180248
57#define PDNSEL 0xe6180254
58
59/* INTC */
60#define ICR1A 0xe6900000
61#define ICR2A 0xe6900004
62#define ICR3A 0xe6900008
63#define ICR4A 0xe690000c
64#define INTMSK00A 0xe6900040
65#define INTMSK10A 0xe6900044
66#define INTMSK20A 0xe6900048
67#define INTMSK30A 0xe690004c
68
69/* MFIS */
70#define SMFRAM 0xe6a70000
71
72/* AP-System Core */
73#define APARMBAREA 0xe6f10020
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020074
75#define PSTR_RETRIES 100
76#define PSTR_DELAY_US 10
77
78#ifdef CONFIG_PM
79
80static int pd_power_down(struct generic_pm_domain *genpd)
81{
82 struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
83 unsigned int mask = 1 << sh7372_pd->bit_shift;
84
Magnus Damm382414b2011-10-19 23:52:50 +020085 if (sh7372_pd->suspend)
86 sh7372_pd->suspend();
87
88 if (sh7372_pd->stay_on)
89 return 0;
90
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020091 if (__raw_readl(PSTR) & mask) {
92 unsigned int retry_count;
93
94 __raw_writel(mask, SPDCR);
95
96 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
97 if (!(__raw_readl(SPDCR) & mask))
98 break;
99 cpu_relax();
100 }
101 }
102
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200103 if (!sh7372_pd->no_debug)
104 pr_debug("sh7372 power domain down 0x%08x -> PSTR = 0x%08x\n",
105 mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200106
107 return 0;
108}
109
110static int pd_power_up(struct generic_pm_domain *genpd)
111{
112 struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
113 unsigned int mask = 1 << sh7372_pd->bit_shift;
114 unsigned int retry_count;
115 int ret = 0;
116
Magnus Damm382414b2011-10-19 23:52:50 +0200117 if (sh7372_pd->stay_on)
118 goto out;
119
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200120 if (__raw_readl(PSTR) & mask)
121 goto out;
122
123 __raw_writel(mask, SWUCR);
124
125 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
126 if (!(__raw_readl(SWUCR) & mask))
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100127 break;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200128 if (retry_count > PSTR_RETRIES)
129 udelay(PSTR_DELAY_US);
130 else
131 cpu_relax();
132 }
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100133 if (!retry_count)
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200134 ret = -EIO;
135
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200136 if (!sh7372_pd->no_debug)
137 pr_debug("sh7372 power domain up 0x%08x -> PSTR = 0x%08x\n",
138 mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200139
Magnus Damm382414b2011-10-19 23:52:50 +0200140 out:
141 if (ret == 0 && sh7372_pd->resume)
142 sh7372_pd->resume();
143
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200144 return ret;
145}
146
Magnus Damm382414b2011-10-19 23:52:50 +0200147static void sh7372_a4r_suspend(void)
148{
149 sh7372_intcs_suspend();
150 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
151}
152
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200153static bool pd_active_wakeup(struct device *dev)
154{
155 return true;
156}
157
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200158static bool sh7372_power_down_forbidden(struct dev_pm_domain *domain)
159{
160 return false;
161}
162
163struct dev_power_governor sh7372_always_on_gov = {
164 .power_down_ok = sh7372_power_down_forbidden,
165};
166
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200167void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
168{
169 struct generic_pm_domain *genpd = &sh7372_pd->genpd;
170
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200171 pm_genpd_init(genpd, sh7372_pd->gov, false);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200172 genpd->stop_device = pm_clk_suspend;
173 genpd->start_device = pm_clk_resume;
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200174 genpd->dev_irq_safe = true;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200175 genpd->active_wakeup = pd_active_wakeup;
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200176 genpd->power_off = pd_power_down;
177 genpd->power_on = pd_power_up;
Magnus Damm775b8ae2011-07-10 10:39:32 +0200178 genpd->power_on(&sh7372_pd->genpd);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200179}
180
181void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
182 struct platform_device *pdev)
183{
184 struct device *dev = &pdev->dev;
185
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200186 pm_genpd_add_device(&sh7372_pd->genpd, dev);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200187 if (pm_clk_no_clocks(dev))
188 pm_clk_add(dev, NULL);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200189}
190
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200191void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd,
192 struct sh7372_pm_domain *sh7372_sd)
193{
194 pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd);
195}
196
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200197struct sh7372_pm_domain sh7372_a4lc = {
198 .bit_shift = 1,
199};
200
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200201struct sh7372_pm_domain sh7372_a4mp = {
202 .bit_shift = 2,
203};
204
Magnus Dammd24771d2011-07-10 10:38:22 +0200205struct sh7372_pm_domain sh7372_d4 = {
206 .bit_shift = 3,
207};
208
Magnus Damm382414b2011-10-19 23:52:50 +0200209struct sh7372_pm_domain sh7372_a4r = {
210 .bit_shift = 5,
211 .gov = &sh7372_always_on_gov,
212 .suspend = sh7372_a4r_suspend,
213 .resume = sh7372_intcs_resume,
214 .stay_on = true,
215};
216
Magnus Damm33afebf2011-07-01 22:14:45 +0200217struct sh7372_pm_domain sh7372_a3rv = {
218 .bit_shift = 6,
219};
220
Magnus Damm082517a2011-07-01 22:14:53 +0200221struct sh7372_pm_domain sh7372_a3ri = {
222 .bit_shift = 8,
223};
224
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200225struct sh7372_pm_domain sh7372_a3sp = {
226 .bit_shift = 11,
227 .gov = &sh7372_always_on_gov,
228 .no_debug = true,
229};
230
Magnus Dammc47586b2011-07-01 22:15:01 +0200231struct sh7372_pm_domain sh7372_a3sg = {
232 .bit_shift = 13,
233};
234
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200235#endif /* CONFIG_PM */
236
Magnus Damma0089bd2011-09-25 23:21:02 +0200237#if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
Magnus Damm06b84162011-09-25 23:18:42 +0200238static int sh7372_do_idle_core_standby(unsigned long unused)
239{
240 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
241 return 0;
242}
243
Paul Mundt66ad1292011-05-25 11:22:58 +0900244static void sh7372_enter_core_standby(void)
Magnus Damm97991652011-04-29 02:28:08 +0900245{
Magnus Damm06b84162011-09-25 23:18:42 +0200246 /* set reset vector, translate 4k */
Magnus Dammcf338352011-09-25 23:20:49 +0200247 __raw_writel(__pa(sh7372_resume_core_standby_a3sm), SBAR);
Magnus Damm06b84162011-09-25 23:18:42 +0200248 __raw_writel(0, APARMBAREA);
Magnus Damm97991652011-04-29 02:28:08 +0900249
Magnus Damm06b84162011-09-25 23:18:42 +0200250 /* enter sleep mode with SYSTBCR to 0x10 */
251 __raw_writel(0x10, SYSTBCR);
252 cpu_suspend(0, sh7372_do_idle_core_standby);
253 __raw_writel(0, SYSTBCR);
Magnus Damm97991652011-04-29 02:28:08 +0900254
Magnus Damm06b84162011-09-25 23:18:42 +0200255 /* disable reset vector translation */
256 __raw_writel(0, SBAR);
Magnus Damm97991652011-04-29 02:28:08 +0900257}
Magnus Damma0089bd2011-09-25 23:21:02 +0200258#endif
Magnus Damm97991652011-04-29 02:28:08 +0900259
Magnus Damma0089bd2011-09-25 23:21:02 +0200260#ifdef CONFIG_SUSPEND
Magnus Dammcf338352011-09-25 23:20:49 +0200261static void sh7372_enter_a3sm_common(int pllc0_on)
262{
263 /* set reset vector, translate 4k */
264 __raw_writel(__pa(sh7372_resume_core_standby_a3sm), SBAR);
265 __raw_writel(0, APARMBAREA);
266
267 if (pllc0_on)
268 __raw_writel(0, PLLC01STPCR);
269 else
270 __raw_writel(1 << 28, PLLC01STPCR);
271
272 __raw_writel(0, PDNSEL); /* power-down A3SM only, not A4S */
273 __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
274 cpu_suspend(0, sh7372_do_idle_a3sm);
275 __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
276
277 /* disable reset vector translation */
278 __raw_writel(0, SBAR);
279}
280
281static int sh7372_a3sm_valid(unsigned long *mskp, unsigned long *msk2p)
282{
283 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
284 unsigned long msk, msk2;
285
286 /* check active clocks to determine potential wakeup sources */
287
288 mstpsr0 = __raw_readl(MSTPSR0);
289 if ((mstpsr0 & 0x00000003) != 0x00000003) {
290 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
291 return 0;
292 }
293
294 mstpsr1 = __raw_readl(MSTPSR1);
295 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
296 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
297 return 0;
298 }
299
300 mstpsr2 = __raw_readl(MSTPSR2);
301 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
302 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
303 return 0;
304 }
305
306 mstpsr3 = __raw_readl(MSTPSR3);
307 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
308 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
309 return 0;
310 }
311
312 mstpsr4 = __raw_readl(MSTPSR4);
313 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
314 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
315 return 0;
316 }
317
318 msk = 0;
319 msk2 = 0;
320
321 /* make bitmaps of limited number of wakeup sources */
322
323 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
324 msk |= 1 << 31;
325
326 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
327 msk |= 1 << 21;
328
329 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
330 msk |= 1 << 2;
331
332 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
333 msk |= 1 << 1;
334
335 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
336 msk |= 1 << 1;
337
338 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
339 msk |= 1 << 1;
340
341 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
342 msk2 |= 1 << 17;
343
344 *mskp = msk;
345 *msk2p = msk2;
346
347 return 1;
348}
349
350static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
351{
352 u16 tmp, irqcr1, irqcr2;
353 int k;
354
355 irqcr1 = 0;
356 irqcr2 = 0;
357
358 /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
359 for (k = 0; k <= 7; k++) {
360 tmp = (icr >> ((7 - k) * 4)) & 0xf;
361 irqcr1 |= (tmp & 0x03) << (k * 2);
362 irqcr2 |= (tmp >> 2) << (k * 2);
363 }
364
365 *irqcr1p = irqcr1;
366 *irqcr2p = irqcr2;
367}
368
369static void sh7372_setup_a3sm(unsigned long msk, unsigned long msk2)
370{
371 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
372 unsigned long tmp;
373
374 /* read IRQ0A -> IRQ15A mask */
375 tmp = bitrev8(__raw_readb(INTMSK00A));
376 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
377
378 /* setup WUPSMSK from clocks and external IRQ mask */
379 msk = (~msk & 0xc030000f) | (tmp << 4);
380 __raw_writel(msk, WUPSMSK);
381
382 /* propage level/edge trigger for external IRQ 0->15 */
383 sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
384 sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
385 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
386 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
387
388 /* read IRQ16A -> IRQ31A mask */
389 tmp = bitrev8(__raw_readb(INTMSK20A));
390 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
391
392 /* setup WUPSMSK2 from clocks and external IRQ mask */
393 msk2 = (~msk2 & 0x00030000) | tmp;
394 __raw_writel(msk2, WUPSMSK2);
395
396 /* propage level/edge trigger for external IRQ 16->31 */
397 sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
398 sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
399 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
400 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
401}
Magnus Damma0089bd2011-09-25 23:21:02 +0200402#endif
Magnus Dammcf338352011-09-25 23:20:49 +0200403
Magnus Damm082a8ca2011-04-29 02:39:32 +0900404#ifdef CONFIG_CPU_IDLE
Magnus Dammcf338352011-09-25 23:20:49 +0200405
Magnus Damm082a8ca2011-04-29 02:39:32 +0900406static void sh7372_cpuidle_setup(struct cpuidle_device *dev)
407{
408 struct cpuidle_state *state;
409 int i = dev->state_count;
410
411 state = &dev->states[i];
412 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
413 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
414 state->exit_latency = 10;
415 state->target_residency = 20 + 10;
416 state->power_usage = 1; /* perhaps not */
417 state->flags = 0;
418 state->flags |= CPUIDLE_FLAG_TIME_VALID;
419 shmobile_cpuidle_modes[i] = sh7372_enter_core_standby;
420
421 dev->state_count = i + 1;
422}
423
424static void sh7372_cpuidle_init(void)
425{
426 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
427}
428#else
429static void sh7372_cpuidle_init(void) {}
430#endif
431
432#ifdef CONFIG_SUSPEND
Magnus Dammcf338352011-09-25 23:20:49 +0200433
Magnus Damm97991652011-04-29 02:28:08 +0900434static int sh7372_enter_suspend(suspend_state_t suspend_state)
435{
Magnus Dammcf338352011-09-25 23:20:49 +0200436 unsigned long msk, msk2;
437
438 /* check active clocks to determine potential wakeup sources */
439 if (sh7372_a3sm_valid(&msk, &msk2)) {
440
441 /* convert INTC mask and sense to SYSC mask and sense */
442 sh7372_setup_a3sm(msk, msk2);
443
444 /* enter A3SM sleep with PLLC0 off */
445 pr_debug("entering A3SM\n");
446 sh7372_enter_a3sm_common(0);
447 } else {
448 /* default to Core Standby that supports all wakeup sources */
449 pr_debug("entering Core Standby\n");
450 sh7372_enter_core_standby();
451 }
Magnus Damm97991652011-04-29 02:28:08 +0900452 return 0;
453}
454
455static void sh7372_suspend_init(void)
456{
457 shmobile_suspend_ops.enter = sh7372_enter_suspend;
458}
459#else
460static void sh7372_suspend_init(void) {}
461#endif
462
Magnus Damm97991652011-04-29 02:28:08 +0900463void __init sh7372_pm_init(void)
464{
465 /* enable DBG hardware block to kick SYSC */
466 __raw_writel(0x0000a500, DBGREG9);
467 __raw_writel(0x0000a501, DBGREG9);
468 __raw_writel(0x00000000, DBGREG1);
469
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200470 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
471 __raw_writel(0, PDNSEL);
472
Magnus Damm056879d2011-11-10 00:44:01 +0100473 /* serial consoles make use of SCIF hardware located in A3SP,
474 * keep such power domain on if "no_console_suspend" is set.
475 */
476 sh7372_a3sp.stay_on = !console_suspend_enabled;
477
Magnus Damm97991652011-04-29 02:28:08 +0900478 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900479 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900480}