blob: 3b131c7a79d01da0a495465aabfc7a80f1dcfcce [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 */
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
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +010085 if (sh7372_pd->suspend) {
86 int ret = sh7372_pd->suspend();
Magnus Damm382414b2011-10-19 23:52:50 +020087
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +010088 if (ret)
89 return ret;
90 }
Magnus Damm382414b2011-10-19 23:52:50 +020091
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020092 if (__raw_readl(PSTR) & mask) {
93 unsigned int retry_count;
94
95 __raw_writel(mask, SPDCR);
96
97 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
98 if (!(__raw_readl(SPDCR) & mask))
99 break;
100 cpu_relax();
101 }
102 }
103
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200104 if (!sh7372_pd->no_debug)
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100105 pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
106 genpd->name, mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200107
108 return 0;
109}
110
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100111static int __pd_power_up(struct sh7372_pm_domain *sh7372_pd, bool do_resume)
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200112{
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200113 unsigned int mask = 1 << sh7372_pd->bit_shift;
114 unsigned int retry_count;
115 int ret = 0;
116
117 if (__raw_readl(PSTR) & mask)
118 goto out;
119
120 __raw_writel(mask, SWUCR);
121
122 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
123 if (!(__raw_readl(SWUCR) & mask))
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100124 break;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200125 if (retry_count > PSTR_RETRIES)
126 udelay(PSTR_DELAY_US);
127 else
128 cpu_relax();
129 }
Guennadi Liakhovetskibe2658f2011-11-10 00:43:52 +0100130 if (!retry_count)
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200131 ret = -EIO;
132
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200133 if (!sh7372_pd->no_debug)
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100134 pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
135 sh7372_pd->genpd.name, mask, __raw_readl(PSTR));
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200136
Magnus Damm382414b2011-10-19 23:52:50 +0200137 out:
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100138 if (ret == 0 && sh7372_pd->resume && do_resume)
Magnus Damm382414b2011-10-19 23:52:50 +0200139 sh7372_pd->resume();
140
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200141 return ret;
142}
143
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100144static int pd_power_up(struct generic_pm_domain *genpd)
145{
146 return __pd_power_up(to_sh7372_pd(genpd), true);
147}
148
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100149static int sh7372_a4r_suspend(void)
Magnus Damm382414b2011-10-19 23:52:50 +0200150{
151 sh7372_intcs_suspend();
152 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100153 return 0;
Magnus Damm382414b2011-10-19 23:52:50 +0200154}
155
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200156static bool pd_active_wakeup(struct device *dev)
157{
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100158 bool (*active_wakeup)(struct device *dev);
159
160 active_wakeup = dev_gpd_data(dev)->ops.active_wakeup;
161 return active_wakeup ? active_wakeup(dev) : true;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200162}
163
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100164static int sh7372_stop_dev(struct device *dev)
165{
166 int (*stop)(struct device *dev);
167
168 stop = dev_gpd_data(dev)->ops.stop;
169 if (stop) {
170 int ret = stop(dev);
171 if (ret)
172 return ret;
173 }
174 return pm_clk_suspend(dev);
175}
176
177static int sh7372_start_dev(struct device *dev)
178{
179 int (*start)(struct device *dev);
180 int ret;
181
182 ret = pm_clk_resume(dev);
183 if (ret)
184 return ret;
185
186 start = dev_gpd_data(dev)->ops.start;
187 if (start)
188 ret = start(dev);
189
190 return ret;
191}
192
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200193void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
194{
195 struct generic_pm_domain *genpd = &sh7372_pd->genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100196 struct dev_power_governor *gov = sh7372_pd->gov;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200197
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100198 pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100199 genpd->dev_ops.stop = sh7372_stop_dev;
200 genpd->dev_ops.start = sh7372_start_dev;
201 genpd->dev_ops.active_wakeup = pd_active_wakeup;
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200202 genpd->dev_irq_safe = true;
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200203 genpd->power_off = pd_power_down;
204 genpd->power_on = pd_power_up;
Rafael J. Wysockibc9f5442011-11-11 13:10:08 +0100205 __pd_power_up(sh7372_pd, false);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200206}
207
208void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
209 struct platform_device *pdev)
210{
211 struct device *dev = &pdev->dev;
212
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200213 pm_genpd_add_device(&sh7372_pd->genpd, dev);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200214 if (pm_clk_no_clocks(dev))
215 pm_clk_add(dev, NULL);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200216}
217
Rafael J. Wysocki111058c2011-08-14 13:35:39 +0200218void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd,
219 struct sh7372_pm_domain *sh7372_sd)
220{
221 pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd);
222}
223
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200224struct sh7372_pm_domain sh7372_a4lc = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100225 .genpd.name = "A4LC",
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200226 .bit_shift = 1,
227};
228
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200229struct sh7372_pm_domain sh7372_a4mp = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100230 .genpd.name = "A4MP",
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200231 .bit_shift = 2,
232};
233
Magnus Dammd24771d2011-07-10 10:38:22 +0200234struct sh7372_pm_domain sh7372_d4 = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100235 .genpd.name = "D4",
Magnus Dammd24771d2011-07-10 10:38:22 +0200236 .bit_shift = 3,
237};
238
Magnus Damm382414b2011-10-19 23:52:50 +0200239struct sh7372_pm_domain sh7372_a4r = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100240 .genpd.name = "A4R",
Magnus Damm382414b2011-10-19 23:52:50 +0200241 .bit_shift = 5,
Magnus Damm382414b2011-10-19 23:52:50 +0200242 .suspend = sh7372_a4r_suspend,
243 .resume = sh7372_intcs_resume,
Magnus Damm382414b2011-10-19 23:52:50 +0200244};
245
Magnus Damm33afebf2011-07-01 22:14:45 +0200246struct sh7372_pm_domain sh7372_a3rv = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100247 .genpd.name = "A3RV",
Magnus Damm33afebf2011-07-01 22:14:45 +0200248 .bit_shift = 6,
249};
250
Magnus Damm082517a2011-07-01 22:14:53 +0200251struct sh7372_pm_domain sh7372_a3ri = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100252 .genpd.name = "A3RI",
Magnus Damm082517a2011-07-01 22:14:53 +0200253 .bit_shift = 8,
254};
255
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100256static int sh7372_a4s_suspend(void)
257{
258 /*
259 * The A4S domain contains the CPU core and therefore it should
260 * only be turned off if the CPU is in use.
261 */
262 return -EBUSY;
263}
264
Magnus Dammf7dadb32011-12-23 01:23:07 +0100265struct sh7372_pm_domain sh7372_a4s = {
266 .genpd.name = "A4S",
267 .bit_shift = 10,
268 .gov = &pm_domain_always_on_gov,
269 .no_debug = true,
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100270 .suspend = sh7372_a4s_suspend,
Magnus Dammf7dadb32011-12-23 01:23:07 +0100271};
272
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100273static int sh7372_a3sp_suspend(void)
274{
275 /*
276 * Serial consoles make use of SCIF hardware located in A3SP,
277 * keep such power domain on if "no_console_suspend" is set.
278 */
Guennadi Liakhovetskia029db42012-01-14 00:40:14 +0100279 return console_suspend_enabled ? 0 : -EBUSY;
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100280}
281
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200282struct sh7372_pm_domain sh7372_a3sp = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100283 .genpd.name = "A3SP",
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200284 .bit_shift = 11,
Mark Browna87dc8f2011-12-08 23:27:40 +0100285 .gov = &pm_domain_always_on_gov,
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200286 .no_debug = true,
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100287 .suspend = sh7372_a3sp_suspend,
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200288};
289
Magnus Dammc47586b2011-07-01 22:15:01 +0200290struct sh7372_pm_domain sh7372_a3sg = {
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100291 .genpd.name = "A3SG",
Magnus Dammc47586b2011-07-01 22:15:01 +0200292 .bit_shift = 13,
293};
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700294
295struct rmobile_pm_domain sh7372_pd_a4lc = {
296 .genpd.name = "A4LC",
297 .bit_shift = 1,
298};
299
300struct rmobile_pm_domain sh7372_pd_a4mp = {
301 .genpd.name = "A4MP",
302 .bit_shift = 2,
303};
304
305struct rmobile_pm_domain sh7372_pd_d4 = {
306 .genpd.name = "D4",
307 .bit_shift = 3,
308};
309
310static int sh7372_a4r_pd_suspend(void)
311{
312 sh7372_intcs_suspend();
313 __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
314 return 0;
315}
316
317struct rmobile_pm_domain sh7372_pd_a4r = {
318 .genpd.name = "A4R",
319 .bit_shift = 5,
320 .suspend = sh7372_a4r_pd_suspend,
321 .resume = sh7372_intcs_resume,
322};
323
324struct rmobile_pm_domain sh7372_pd_a3rv = {
325 .genpd.name = "A3RV",
326 .bit_shift = 6,
327};
328
329struct rmobile_pm_domain sh7372_pd_a3ri = {
330 .genpd.name = "A3RI",
331 .bit_shift = 8,
332};
333
334static int sh7372_pd_a4s_suspend(void)
335{
336 /*
337 * The A4S domain contains the CPU core and therefore it should
338 * only be turned off if the CPU is in use.
339 */
340 return -EBUSY;
341}
342
343struct rmobile_pm_domain sh7372_pd_a4s = {
344 .genpd.name = "A4S",
345 .bit_shift = 10,
346 .gov = &pm_domain_always_on_gov,
347 .no_debug = true,
348 .suspend = sh7372_pd_a4s_suspend,
349};
350
351static int sh7372_a3sp_pd_suspend(void)
352{
353 /*
354 * Serial consoles make use of SCIF hardware located in A3SP,
355 * keep such power domain on if "no_console_suspend" is set.
356 */
357 return console_suspend_enabled ? 0 : -EBUSY;
358}
359
360struct rmobile_pm_domain sh7372_pd_a3sp = {
361 .genpd.name = "A3SP",
362 .bit_shift = 11,
363 .gov = &pm_domain_always_on_gov,
364 .no_debug = true,
365 .suspend = sh7372_a3sp_pd_suspend,
366};
367
368struct rmobile_pm_domain sh7372_pd_a3sg = {
369 .genpd.name = "A3SG",
370 .bit_shift = 13,
371};
372
Kuninori Morimoto1645b762012-06-24 22:00:13 +0200373#endif /* CONFIG_PM */
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200374
Magnus Damma0089bd2011-09-25 23:21:02 +0200375#if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
Magnus Dammf7dadb32011-12-23 01:23:07 +0100376static void sh7372_set_reset_vector(unsigned long address)
Magnus Damm97991652011-04-29 02:28:08 +0900377{
Magnus Damm06b84162011-09-25 23:18:42 +0200378 /* set reset vector, translate 4k */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100379 __raw_writel(address, SBAR);
Magnus Damm06b84162011-09-25 23:18:42 +0200380 __raw_writel(0, APARMBAREA);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100381}
382
Magnus Dammf7dadb32011-12-23 01:23:07 +0100383static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
Magnus Dammcf338352011-09-25 23:20:49 +0200384{
Magnus Dammcf338352011-09-25 23:20:49 +0200385 if (pllc0_on)
386 __raw_writel(0, PLLC01STPCR);
387 else
388 __raw_writel(1 << 28, PLLC01STPCR);
389
Magnus Dammcf338352011-09-25 23:20:49 +0200390 __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100391 cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
Magnus Dammcf338352011-09-25 23:20:49 +0200392 __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
393
394 /* disable reset vector translation */
395 __raw_writel(0, SBAR);
396}
397
Magnus Dammf7dadb32011-12-23 01:23:07 +0100398static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
Magnus Dammcf338352011-09-25 23:20:49 +0200399{
400 unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
401 unsigned long msk, msk2;
402
403 /* check active clocks to determine potential wakeup sources */
404
405 mstpsr0 = __raw_readl(MSTPSR0);
406 if ((mstpsr0 & 0x00000003) != 0x00000003) {
407 pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
408 return 0;
409 }
410
411 mstpsr1 = __raw_readl(MSTPSR1);
412 if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
413 pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
414 return 0;
415 }
416
417 mstpsr2 = __raw_readl(MSTPSR2);
418 if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
419 pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
420 return 0;
421 }
422
423 mstpsr3 = __raw_readl(MSTPSR3);
424 if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
425 pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
426 return 0;
427 }
428
429 mstpsr4 = __raw_readl(MSTPSR4);
430 if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
431 pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
432 return 0;
433 }
434
435 msk = 0;
436 msk2 = 0;
437
438 /* make bitmaps of limited number of wakeup sources */
439
440 if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
441 msk |= 1 << 31;
442
443 if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
444 msk |= 1 << 21;
445
446 if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
447 msk |= 1 << 2;
448
449 if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
450 msk |= 1 << 1;
451
452 if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
453 msk |= 1 << 1;
454
455 if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
456 msk |= 1 << 1;
457
458 if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
459 msk2 |= 1 << 17;
460
461 *mskp = msk;
462 *msk2p = msk2;
463
464 return 1;
465}
466
467static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
468{
469 u16 tmp, irqcr1, irqcr2;
470 int k;
471
472 irqcr1 = 0;
473 irqcr2 = 0;
474
475 /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
476 for (k = 0; k <= 7; k++) {
477 tmp = (icr >> ((7 - k) * 4)) & 0xf;
478 irqcr1 |= (tmp & 0x03) << (k * 2);
479 irqcr2 |= (tmp >> 2) << (k * 2);
480 }
481
482 *irqcr1p = irqcr1;
483 *irqcr2p = irqcr2;
484}
485
Magnus Dammf7dadb32011-12-23 01:23:07 +0100486static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
Magnus Dammcf338352011-09-25 23:20:49 +0200487{
488 u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
489 unsigned long tmp;
490
491 /* read IRQ0A -> IRQ15A mask */
492 tmp = bitrev8(__raw_readb(INTMSK00A));
493 tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
494
495 /* setup WUPSMSK from clocks and external IRQ mask */
496 msk = (~msk & 0xc030000f) | (tmp << 4);
497 __raw_writel(msk, WUPSMSK);
498
499 /* propage level/edge trigger for external IRQ 0->15 */
500 sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
501 sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
502 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
503 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
504
505 /* read IRQ16A -> IRQ31A mask */
506 tmp = bitrev8(__raw_readb(INTMSK20A));
507 tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
508
509 /* setup WUPSMSK2 from clocks and external IRQ mask */
510 msk2 = (~msk2 & 0x00030000) | tmp;
511 __raw_writel(msk2, WUPSMSK2);
512
513 /* propage level/edge trigger for external IRQ 16->31 */
514 sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
515 sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
516 __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
517 __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
518}
Magnus Dammf7dadb32011-12-23 01:23:07 +0100519
520static void sh7372_enter_a3sm_common(int pllc0_on)
521{
Magnus Damm591e2ac2012-07-05 14:46:07 +0900522 /* use INTCA together with SYSC for wakeup */
523 sh7372_setup_sysc(1 << 0, 0);
Magnus Dammf7dadb32011-12-23 01:23:07 +0100524 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
525 sh7372_enter_sysc(pllc0_on, 1 << 12);
526}
527
Magnus Damm082a8ca2011-04-29 02:39:32 +0900528#ifdef CONFIG_CPU_IDLE
Magnus Damm591e2ac2012-07-05 14:46:07 +0900529static int sh7372_do_idle_core_standby(unsigned long unused)
530{
531 cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
532 return 0;
533}
534
535static void sh7372_enter_core_standby(void)
536{
537 sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
538
539 /* enter sleep mode with SYSTBCR to 0x10 */
540 __raw_writel(0x10, SYSTBCR);
541 cpu_suspend(0, sh7372_do_idle_core_standby);
542 __raw_writel(0, SYSTBCR);
543
544 /* disable reset vector translation */
545 __raw_writel(0, SBAR);
546}
Magnus Dammcf338352011-09-25 23:20:49 +0200547
Magnus Damm3abd69d2012-07-05 14:46:47 +0900548static void sh7372_enter_a3sm_pll_on(void)
549{
550 sh7372_enter_a3sm_common(1);
551}
552
553static void sh7372_enter_a3sm_pll_off(void)
554{
555 sh7372_enter_a3sm_common(0);
556}
557
Magnus Dammb73b5c42011-11-11 14:01:30 +0900558static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
Magnus Damm082a8ca2011-04-29 02:39:32 +0900559{
Magnus Dammb73b5c42011-11-11 14:01:30 +0900560 struct cpuidle_state *state = &drv->states[drv->state_count];
Magnus Damm082a8ca2011-04-29 02:39:32 +0900561
Magnus Damm082a8ca2011-04-29 02:39:32 +0900562 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
563 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
564 state->exit_latency = 10;
565 state->target_residency = 20 + 10;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900566 state->flags = CPUIDLE_FLAG_TIME_VALID;
567 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
Magnus Damm3abd69d2012-07-05 14:46:47 +0900568 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900569
Magnus Damm3abd69d2012-07-05 14:46:47 +0900570 state = &drv->states[drv->state_count];
571 snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
572 strncpy(state->desc, "A3SM PLL ON", CPUIDLE_DESC_LEN);
573 state->exit_latency = 20;
574 state->target_residency = 30 + 20;
575 state->flags = CPUIDLE_FLAG_TIME_VALID;
576 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_on;
577 drv->state_count++;
578
579 state = &drv->states[drv->state_count];
580 snprintf(state->name, CPUIDLE_NAME_LEN, "C4");
581 strncpy(state->desc, "A3SM PLL OFF", CPUIDLE_DESC_LEN);
582 state->exit_latency = 120;
583 state->target_residency = 30 + 120;
584 state->flags = CPUIDLE_FLAG_TIME_VALID;
585 shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_off;
Magnus Dammb73b5c42011-11-11 14:01:30 +0900586 drv->state_count++;
Magnus Damm082a8ca2011-04-29 02:39:32 +0900587}
588
589static void sh7372_cpuidle_init(void)
590{
591 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
592}
593#else
594static void sh7372_cpuidle_init(void) {}
595#endif
596
597#ifdef CONFIG_SUSPEND
Magnus Damm3abd69d2012-07-05 14:46:47 +0900598static void sh7372_enter_a4s_common(int pllc0_on)
599{
600 sh7372_intca_suspend();
601 memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
602 sh7372_set_reset_vector(SMFRAM);
603 sh7372_enter_sysc(pllc0_on, 1 << 10);
604 sh7372_intca_resume();
605}
Magnus Dammcf338352011-09-25 23:20:49 +0200606
Magnus Damm97991652011-04-29 02:28:08 +0900607static int sh7372_enter_suspend(suspend_state_t suspend_state)
608{
Magnus Dammcf338352011-09-25 23:20:49 +0200609 unsigned long msk, msk2;
610
611 /* check active clocks to determine potential wakeup sources */
Magnus Dammf7dadb32011-12-23 01:23:07 +0100612 if (sh7372_sysc_valid(&msk, &msk2)) {
Rafael J. Wysocki767c0f32011-12-23 01:23:39 +0100613 if (!console_suspend_enabled &&
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700614 sh7372_pd_a4s.genpd.status == GPD_STATE_POWER_OFF) {
Magnus Damm591e2ac2012-07-05 14:46:07 +0900615 /* convert INTC mask/sense to SYSC mask/sense */
616 sh7372_setup_sysc(msk, msk2);
617
Magnus Dammf7dadb32011-12-23 01:23:07 +0100618 /* enter A4S sleep with PLLC0 off */
619 pr_debug("entering A4S\n");
620 sh7372_enter_a4s_common(0);
Magnus Damm591e2ac2012-07-05 14:46:07 +0900621 return 0;
Magnus Dammf7dadb32011-12-23 01:23:07 +0100622 }
Magnus Dammcf338352011-09-25 23:20:49 +0200623 }
Magnus Damm591e2ac2012-07-05 14:46:07 +0900624
625 /* default to enter A3SM sleep with PLLC0 off */
626 pr_debug("entering A3SM\n");
627 sh7372_enter_a3sm_common(0);
Magnus Damm97991652011-04-29 02:28:08 +0900628 return 0;
629}
630
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100631/**
632 * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
633 * @notifier: Unused.
634 * @pm_event: Event being handled.
635 * @unused: Unused.
636 */
637static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
638 unsigned long pm_event, void *unused)
639{
640 switch (pm_event) {
641 case PM_SUSPEND_PREPARE:
642 /*
643 * This is necessary, because the A4R domain has to be "on"
644 * when suspend_device_irqs() and resume_device_irqs() are
645 * executed during system suspend and resume, respectively, so
646 * that those functions don't crash while accessing the INTCS.
647 */
Kuninori Morimotob9299a72012-07-05 01:28:32 -0700648 pm_genpd_poweron(&sh7372_pd_a4r.genpd);
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100649 break;
650 case PM_POST_SUSPEND:
651 pm_genpd_poweroff_unused();
652 break;
653 }
654
655 return NOTIFY_DONE;
656}
657
Magnus Damm97991652011-04-29 02:28:08 +0900658static void sh7372_suspend_init(void)
659{
660 shmobile_suspend_ops.enter = sh7372_enter_suspend;
Rafael J. Wysockia8cf27b2011-12-23 01:24:34 +0100661 pm_notifier(sh7372_pm_notifier_fn, 0);
Magnus Damm97991652011-04-29 02:28:08 +0900662}
663#else
664static void sh7372_suspend_init(void) {}
665#endif
Magnus Damm3abd69d2012-07-05 14:46:47 +0900666#endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
Magnus Damm97991652011-04-29 02:28:08 +0900667
Magnus Damm97991652011-04-29 02:28:08 +0900668void __init sh7372_pm_init(void)
669{
670 /* enable DBG hardware block to kick SYSC */
671 __raw_writel(0x0000a500, DBGREG9);
672 __raw_writel(0x0000a501, DBGREG9);
673 __raw_writel(0x00000000, DBGREG1);
674
Magnus Dammd93f5cd2011-10-19 23:52:41 +0200675 /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
676 __raw_writel(0, PDNSEL);
677
Magnus Damm97991652011-04-29 02:28:08 +0900678 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900679 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900680}