blob: a3bdb12acde99771357de0fb95c1e207ece71e3f [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>
Magnus Damm97991652011-04-29 02:28:08 +090029
Magnus Dammcf338352011-09-25 23:20:49 +020030/* DBG */
31#define DBGREG1 0xe6100020
32#define DBGREG9 0xe6100040
Magnus Damm97991652011-04-29 02:28:08 +090033
Magnus Dammcf338352011-09-25 23:20:49 +020034/* CPGA */
35#define SYSTBCR 0xe6150024
36#define MSTPSR0 0xe6150030
37#define MSTPSR1 0xe6150038
38#define MSTPSR2 0xe6150040
39#define MSTPSR3 0xe6150048
40#define MSTPSR4 0xe615004c
41#define PLLC01STPCR 0xe61500c8
42
43/* SYSC */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020044#define SPDCR 0xe6180008
45#define SWUCR 0xe6180014
Magnus Dammcf338352011-09-25 23:20:49 +020046#define SBAR 0xe6180020
Magnus Damm382414b2011-10-19 23:52:50 +020047#define WUPRMSK 0xe6180028
Magnus Dammcf338352011-09-25 23:20:49 +020048#define WUPSMSK 0xe618002c
49#define WUPSMSK2 0xe6180048
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020050#define PSTR 0xe6180080
Magnus Dammcf338352011-09-25 23:20:49 +020051#define WUPSFAC 0xe6180098
52#define IRQCR 0xe618022c
53#define IRQCR2 0xe6180238
54#define IRQCR3 0xe6180244
55#define IRQCR4 0xe6180248
56#define PDNSEL 0xe6180254
57
58/* INTC */
59#define ICR1A 0xe6900000
60#define ICR2A 0xe6900004
61#define ICR3A 0xe6900008
62#define ICR4A 0xe690000c
63#define INTMSK00A 0xe6900040
64#define INTMSK10A 0xe6900044
65#define INTMSK20A 0xe6900048
66#define INTMSK30A 0xe690004c
67
68/* MFIS */
69#define SMFRAM 0xe6a70000
70
71/* AP-System Core */
72#define APARMBAREA 0xe6f10020
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020073
74#define PSTR_RETRIES 100
75#define PSTR_DELAY_US 10
76
77#ifdef CONFIG_PM
78
79static int pd_power_down(struct generic_pm_domain *genpd)
80{
81 struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
82 unsigned int mask = 1 << sh7372_pd->bit_shift;
83
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +010084 if (sh7372_pd->suspend) {
85 int ret = sh7372_pd->suspend();
Magnus Damm382414b2011-10-19 23:52:50 +020086
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +010087 if (ret)
88 return ret;
89 }
Magnus Damm382414b2011-10-19 23:52:50 +020090
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)
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100104 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
105 genpd->name, mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200106
107 return 0;
108}
109
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100110static int __pd_power_up(struct sh7372_pm_domain *sh7372_pd, bool do_resume)
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200111{
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200112 unsigned int mask = 1 << sh7372_pd->bit_shift;
113 unsigned int retry_count;
114 int ret = 0;
115
116 if (__raw_readl(PSTR) & mask)
117 goto out;
118
119 __raw_writel(mask, SWUCR);
120
121 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
122 if (!(__raw_readl(SWUCR) & mask))
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100123 break;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200124 if (retry_count > PSTR_RETRIES)
125 udelay(PSTR_DELAY_US);
126 else
127 cpu_relax();
128 }
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100129 if (!retry_count)
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200130 ret = -EIO;
131
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200132 if (!sh7372_pd->no_debug)
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100133 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
134 sh7372_pd->genpd.name, mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200135
Magnus Damm382414b2011-10-19 23:52:50 +0200136 out:
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100137 if (ret == 0 && sh7372_pd->resume && do_resume)
Magnus Damm382414b2011-10-19 23:52:50 +0200138 sh7372_pd->resume();
139
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200140 return ret;
141}
142
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100143static int pd_power_up(struct generic_pm_domain *genpd)
144{
145 return __pd_power_up(to_sh7372_pd(genpd), true);
146}
147
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100148static int sh7372_a4r_suspend(void)
Magnus Damm382414b2011-10-19 23:52:50 +0200149{
150 sh7372_intcs_suspend();
151 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100152 return 0;
Magnus Damm382414b2011-10-19 23:52:50 +0200153}
154
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200155static bool pd_active_wakeup(struct device *dev)
156{
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100157 bool (*active_wakeup)(struct device *dev);
158
159 active_wakeup = dev_gpd_data(dev)->ops.active_wakeup;
160 return active_wakeup ? active_wakeup(dev) : true;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200161}
162
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100163static int sh7372_stop_dev(struct device *dev)
164{
165 int (*stop)(struct device *dev);
166
167 stop = dev_gpd_data(dev)->ops.stop;
168 if (stop) {
169 int ret = stop(dev);
170 if (ret)
171 return ret;
172 }
173 return pm_clk_suspend(dev);
174}
175
176static int sh7372_start_dev(struct device *dev)
177{
178 int (*start)(struct device *dev);
179 int ret;
180
181 ret = pm_clk_resume(dev);
182 if (ret)
183 return ret;
184
185 start = dev_gpd_data(dev)->ops.start;
186 if (start)
187 ret = start(dev);
188
189 return ret;
190}
191
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200192void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
193{
194 struct generic_pm_domain *genpd = &sh7372_pd->genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100195 struct dev_power_governor *gov = sh7372_pd->gov;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200196
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100197 pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100198 genpd->dev_ops.stop = sh7372_stop_dev;
199 genpd->dev_ops.start = sh7372_start_dev;
200 genpd->dev_ops.active_wakeup = pd_active_wakeup;
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200201 genpd->dev_irq_safe = true;
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200202 genpd->power_off = pd_power_down;
203 genpd->power_on = pd_power_up;
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100204 __pd_power_up(sh7372_pd, false);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200205}
206
207void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
208 struct platform_device *pdev)
209{
210 struct device *dev = &pdev->dev;
211
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200212 pm_genpd_add_device(&sh7372_pd->genpd, dev);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200213 if (pm_clk_no_clocks(dev))
214 pm_clk_add(dev, NULL);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200215}
216
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200217void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd,
218 struct sh7372_pm_domain *sh7372_sd)
219{
220 pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd);
221}
222
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200223struct sh7372_pm_domain sh7372_a4lc = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100224 .genpd.name = "A4LC",
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200225 .bit_shift = 1,
226};
227
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200228struct sh7372_pm_domain sh7372_a4mp = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100229 .genpd.name = "A4MP",
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200230 .bit_shift = 2,
231};
232
Magnus Dammd24771d2011-07-10 10:38:22 +0200233struct sh7372_pm_domain sh7372_d4 = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100234 .genpd.name = "D4",
Magnus Dammd24771d2011-07-10 10:38:22 +0200235 .bit_shift = 3,
236};
237
Magnus Damm382414b2011-10-19 23:52:50 +0200238struct sh7372_pm_domain sh7372_a4r = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100239 .genpd.name = "A4R",
Magnus Damm382414b2011-10-19 23:52:50 +0200240 .bit_shift = 5,
Magnus Damm382414b2011-10-19 23:52:50 +0200241 .suspend = sh7372_a4r_suspend,
242 .resume = sh7372_intcs_resume,
Magnus Damm382414b2011-10-19 23:52:50 +0200243};
244
Magnus Damm33afebf2011-07-01 22:14:45 +0200245struct sh7372_pm_domain sh7372_a3rv = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100246 .genpd.name = "A3RV",
Magnus Damm33afebf2011-07-01 22:14:45 +0200247 .bit_shift = 6,
248};
249
Magnus Damm082517a2011-07-01 22:14:53 +0200250struct sh7372_pm_domain sh7372_a3ri = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100251 .genpd.name = "A3RI",
Magnus Damm082517a2011-07-01 22:14:53 +0200252 .bit_shift = 8,
253};
254
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100255static int sh7372_a4s_suspend(void)
256{
257 /*
258 * The A4S domain contains the CPU core and therefore it should
259 * only be turned off if the CPU is in use.
260 */
261 return -EBUSY;
262}
263
Magnus Dammf7dadb32011-12-23 01:23:07 +0100264struct sh7372_pm_domain sh7372_a4s = {
265 .genpd.name = "A4S",
266 .bit_shift = 10,
267 .gov = &pm_domain_always_on_gov,
268 .no_debug = true,
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100269 .suspend = sh7372_a4s_suspend,
Magnus Dammf7dadb32011-12-23 01:23:07 +0100270};
271
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100272static int sh7372_a3sp_suspend(void)
273{
274 /*
275 * Serial consoles make use of SCIF hardware located in A3SP,
276 * keep such power domain on if "no_console_suspend" is set.
277 */
Guennadi Liakhovetskia029db42012-01-14 00:40:14 +0100278 return console_suspend_enabled ? 0 : -EBUSY;
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100279}
280
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200281struct sh7372_pm_domain sh7372_a3sp = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100282 .genpd.name = "A3SP",
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200283 .bit_shift = 11,
Mark Browna87dc8f2011-12-08 23:27:40 +0100284 .gov = &pm_domain_always_on_gov,
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200285 .no_debug = true,
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100286 .suspend = sh7372_a3sp_suspend,
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200287};
288
Magnus Dammc47586b2011-07-01 22:15:01 +0200289struct sh7372_pm_domain sh7372_a3sg = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100290 .genpd.name = "A3SG",
Magnus Dammc47586b2011-07-01 22:15:01 +0200291 .bit_shift = 13,
292};
293
Rafael J. Wysocki88995e82011-11-17 21:39:55 +0100294#else /* !CONFIG_PM */
295
296static inline void sh7372_a3sp_init(void) {}
297
298#endif /* !CONFIG_PM */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200299
Magnus Damma0089bd2011-09-25 23:21:02 +0200300#if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
Magnus Damm06b84162011-09-25 23:18:42 +0200301static int sh7372_do_idle_core_standby(unsigned long unused)
302{
303 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
304 return 0;
305}
306
Magnus Dammf7dadb32011-12-23 01:23:07 +0100307static void sh7372_set_reset_vector(unsigned long address)
Magnus Damm97991652011-04-29 02:28:08 +0900308{
Magnus Damm06b84162011-09-25 23:18:42 +0200309 /* set reset vector, translate 4k */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100310 __raw_writel(address, SBAR);
Magnus Damm06b84162011-09-25 23:18:42 +0200311 __raw_writel(0, APARMBAREA);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100312}
313
314static void sh7372_enter_core_standby(void)
315{
316 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
Magnus Damm97991652011-04-29 02:28:08 +0900317
Magnus Damm06b84162011-09-25 23:18:42 +0200318 /* enter sleep mode with SYSTBCR to 0x10 */
319 __raw_writel(0x10, SYSTBCR);
320 cpu_suspend(0, sh7372_do_idle_core_standby);
321 __raw_writel(0, SYSTBCR);
Magnus Damm97991652011-04-29 02:28:08 +0900322
Magnus Damm06b84162011-09-25 23:18:42 +0200323 /* disable reset vector translation */
324 __raw_writel(0, SBAR);
Magnus Damm97991652011-04-29 02:28:08 +0900325}
Magnus Damma0089bd2011-09-25 23:21:02 +0200326#endif
Magnus Damm97991652011-04-29 02:28:08 +0900327
Magnus Damma0089bd2011-09-25 23:21:02 +0200328#ifdef CONFIG_SUSPEND
Magnus Dammf7dadb32011-12-23 01:23:07 +0100329static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
Magnus Dammcf338352011-09-25 23:20:49 +0200330{
Magnus Dammcf338352011-09-25 23:20:49 +0200331 if (pllc0_on)
332 __raw_writel(0, PLLC01STPCR);
333 else
334 __raw_writel(1 << 28, PLLC01STPCR);
335
Magnus Dammcf338352011-09-25 23:20:49 +0200336 __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100337 cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
Magnus Dammcf338352011-09-25 23:20:49 +0200338 __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
339
340 /* disable reset vector translation */
341 __raw_writel(0, SBAR);
342}
343
Magnus Dammf7dadb32011-12-23 01:23:07 +0100344static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
Magnus Dammcf338352011-09-25 23:20:49 +0200345{
346 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
347 unsigned long msk, msk2;
348
349 /* check active clocks to determine potential wakeup sources */
350
351 mstpsr0 = __raw_readl(MSTPSR0);
352 if ((mstpsr0 & 0x00000003) != 0x00000003) {
353 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
354 return 0;
355 }
356
357 mstpsr1 = __raw_readl(MSTPSR1);
358 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
359 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
360 return 0;
361 }
362
363 mstpsr2 = __raw_readl(MSTPSR2);
364 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
365 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
366 return 0;
367 }
368
369 mstpsr3 = __raw_readl(MSTPSR3);
370 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
371 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
372 return 0;
373 }
374
375 mstpsr4 = __raw_readl(MSTPSR4);
376 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
377 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
378 return 0;
379 }
380
381 msk = 0;
382 msk2 = 0;
383
384 /* make bitmaps of limited number of wakeup sources */
385
386 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
387 msk |= 1 << 31;
388
389 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
390 msk |= 1 << 21;
391
392 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
393 msk |= 1 << 2;
394
395 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
396 msk |= 1 << 1;
397
398 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
399 msk |= 1 << 1;
400
401 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
402 msk |= 1 << 1;
403
404 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
405 msk2 |= 1 << 17;
406
407 *mskp = msk;
408 *msk2p = msk2;
409
410 return 1;
411}
412
413static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
414{
415 u16 tmp, irqcr1, irqcr2;
416 int k;
417
418 irqcr1 = 0;
419 irqcr2 = 0;
420
421 /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
422 for (k = 0; k <= 7; k++) {
423 tmp = (icr >> ((7 - k) * 4)) & 0xf;
424 irqcr1 |= (tmp & 0x03) << (k * 2);
425 irqcr2 |= (tmp >> 2) << (k * 2);
426 }
427
428 *irqcr1p = irqcr1;
429 *irqcr2p = irqcr2;
430}
431
Magnus Dammf7dadb32011-12-23 01:23:07 +0100432static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
Magnus Dammcf338352011-09-25 23:20:49 +0200433{
434 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
435 unsigned long tmp;
436
437 /* read IRQ0A -> IRQ15A mask */
438 tmp = bitrev8(__raw_readb(INTMSK00A));
439 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
440
441 /* setup WUPSMSK from clocks and external IRQ mask */
442 msk = (~msk & 0xc030000f) | (tmp << 4);
443 __raw_writel(msk, WUPSMSK);
444
445 /* propage level/edge trigger for external IRQ 0->15 */
446 sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
447 sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
448 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
449 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
450
451 /* read IRQ16A -> IRQ31A mask */
452 tmp = bitrev8(__raw_readb(INTMSK20A));
453 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
454
455 /* setup WUPSMSK2 from clocks and external IRQ mask */
456 msk2 = (~msk2 & 0x00030000) | tmp;
457 __raw_writel(msk2, WUPSMSK2);
458
459 /* propage level/edge trigger for external IRQ 16->31 */
460 sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
461 sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
462 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
463 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
464}
Magnus Dammf7dadb32011-12-23 01:23:07 +0100465
466static void sh7372_enter_a3sm_common(int pllc0_on)
467{
468 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
469 sh7372_enter_sysc(pllc0_on, 1 << 12);
470}
471
472static void sh7372_enter_a4s_common(int pllc0_on)
473{
474 sh7372_intca_suspend();
475 memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
476 sh7372_set_reset_vector(SMFRAM);
477 sh7372_enter_sysc(pllc0_on, 1 << 10);
478 sh7372_intca_resume();
479}
480
Magnus Damma0089bd2011-09-25 23:21:02 +0200481#endif
Magnus Dammcf338352011-09-25 23:20:49 +0200482
Magnus Damm082a8ca2011-04-29 02:39:32 +0900483#ifdef CONFIG_CPU_IDLE
Magnus Dammcf338352011-09-25 23:20:49 +0200484
Magnus Dammb73b5c42011-11-11 14:01:30 +0900485static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
Magnus Damm082a8ca2011-04-29 02:39:32 +0900486{
Magnus Dammb73b5c42011-11-11 14:01:30 +0900487 struct cpuidle_state *state = &drv->states[drv->state_count];
Magnus Damm082a8ca2011-04-29 02:39:32 +0900488
Magnus Damm082a8ca2011-04-29 02:39:32 +0900489 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
490 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
491 state->exit_latency = 10;
492 state->target_residency = 20 + 10;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900493 state->flags = CPUIDLE_FLAG_TIME_VALID;
494 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900495
Magnus Dammb73b5c42011-11-11 14:01:30 +0900496 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900497}
498
499static void sh7372_cpuidle_init(void)
500{
501 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
502}
503#else
504static void sh7372_cpuidle_init(void) {}
505#endif
506
507#ifdef CONFIG_SUSPEND
Magnus Dammcf338352011-09-25 23:20:49 +0200508
Magnus Damm97991652011-04-29 02:28:08 +0900509static int sh7372_enter_suspend(suspend_state_t suspend_state)
510{
Magnus Dammcf338352011-09-25 23:20:49 +0200511 unsigned long msk, msk2;
512
513 /* check active clocks to determine potential wakeup sources */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100514 if (sh7372_sysc_valid(&msk, &msk2)) {
Magnus Dammcf338352011-09-25 23:20:49 +0200515 /* convert INTC mask and sense to SYSC mask and sense */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100516 sh7372_setup_sysc(msk, msk2);
Magnus Dammcf338352011-09-25 23:20:49 +0200517
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100518 if (!console_suspend_enabled &&
Magnus Dammf7dadb32011-12-23 01:23:07 +0100519 sh7372_a4s.genpd.status == GPD_STATE_POWER_OFF) {
520 /* enter A4S sleep with PLLC0 off */
521 pr_debug("entering A4S\n");
522 sh7372_enter_a4s_common(0);
523 } else {
524 /* enter A3SM sleep with PLLC0 off */
525 pr_debug("entering A3SM\n");
526 sh7372_enter_a3sm_common(0);
527 }
Magnus Dammcf338352011-09-25 23:20:49 +0200528 } else {
529 /* default to Core Standby that supports all wakeup sources */
530 pr_debug("entering Core Standby\n");
531 sh7372_enter_core_standby();
532 }
Magnus Damm97991652011-04-29 02:28:08 +0900533 return 0;
534}
535
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100536/**
537 * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
538 * @notifier: Unused.
539 * @pm_event: Event being handled.
540 * @unused: Unused.
541 */
542static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
543 unsigned long pm_event, void *unused)
544{
545 switch (pm_event) {
546 case PM_SUSPEND_PREPARE:
547 /*
548 * This is necessary, because the A4R domain has to be "on"
549 * when suspend_device_irqs() and resume_device_irqs() are
550 * executed during system suspend and resume, respectively, so
551 * that those functions don't crash while accessing the INTCS.
552 */
553 pm_genpd_poweron(&sh7372_a4r.genpd);
554 break;
555 case PM_POST_SUSPEND:
556 pm_genpd_poweroff_unused();
557 break;
558 }
559
560 return NOTIFY_DONE;
561}
562
Magnus Damm97991652011-04-29 02:28:08 +0900563static void sh7372_suspend_init(void)
564{
565 shmobile_suspend_ops.enter = sh7372_enter_suspend;
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100566 pm_notifier(sh7372_pm_notifier_fn, 0);
Magnus Damm97991652011-04-29 02:28:08 +0900567}
568#else
569static void sh7372_suspend_init(void) {}
570#endif
571
Magnus Damm97991652011-04-29 02:28:08 +0900572void __init sh7372_pm_init(void)
573{
574 /* enable DBG hardware block to kick SYSC */
575 __raw_writel(0x0000a500, DBGREG9);
576 __raw_writel(0x0000a501, DBGREG9);
577 __raw_writel(0x00000000, DBGREG1);
578
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200579 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
580 __raw_writel(0, PDNSEL);
581
Magnus Damm97991652011-04-29 02:28:08 +0900582 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900583 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900584}