blob: 0b07138908b7d76207a0c66549bc1eedf6825bc6 [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. Wysockie3e01092011-07-01 22:13:56 +020018#include <linux/pm_runtime.h>
19#include <linux/platform_device.h>
20#include <linux/delay.h>
Magnus Damm97991652011-04-29 02:28:08 +090021#include <asm/system.h>
22#include <asm/io.h>
23#include <asm/tlbflush.h>
24#include <mach/common.h>
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020025#include <mach/sh7372.h>
Magnus Damm97991652011-04-29 02:28:08 +090026
27#define SMFRAM 0xe6a70000
28#define SYSTBCR 0xe6150024
29#define SBAR 0xe6180020
30#define APARMBAREA 0xe6f10020
31
Rafael J. Wysockie3e01092011-07-01 22:13:56 +020032#define SPDCR 0xe6180008
33#define SWUCR 0xe6180014
34#define PSTR 0xe6180080
35
36#define PSTR_RETRIES 100
37#define PSTR_DELAY_US 10
38
39#ifdef CONFIG_PM
40
41static int pd_power_down(struct generic_pm_domain *genpd)
42{
43 struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
44 unsigned int mask = 1 << sh7372_pd->bit_shift;
45
46 if (__raw_readl(PSTR) & mask) {
47 unsigned int retry_count;
48
49 __raw_writel(mask, SPDCR);
50
51 for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
52 if (!(__raw_readl(SPDCR) & mask))
53 break;
54 cpu_relax();
55 }
56 }
57
58 pr_debug("sh7372 power domain down 0x%08x -> PSTR = 0x%08x\n",
59 mask, __raw_readl(PSTR));
60
61 return 0;
62}
63
64static int pd_power_up(struct generic_pm_domain *genpd)
65{
66 struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
67 unsigned int mask = 1 << sh7372_pd->bit_shift;
68 unsigned int retry_count;
69 int ret = 0;
70
71 if (__raw_readl(PSTR) & mask)
72 goto out;
73
74 __raw_writel(mask, SWUCR);
75
76 for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
77 if (!(__raw_readl(SWUCR) & mask))
78 goto out;
79 if (retry_count > PSTR_RETRIES)
80 udelay(PSTR_DELAY_US);
81 else
82 cpu_relax();
83 }
84 if (__raw_readl(SWUCR) & mask)
85 ret = -EIO;
86
87 out:
88 pr_debug("sh7372 power domain up 0x%08x -> PSTR = 0x%08x\n",
89 mask, __raw_readl(PSTR));
90
91 return ret;
92}
93
Magnus Damm775b8ae2011-07-10 10:39:32 +020094static int pd_power_up_a3rv(struct generic_pm_domain *genpd)
95{
96 int ret = pd_power_up(genpd);
97
98 /* force A4LC on after A3RV has been requested on */
99 pm_genpd_poweron(&sh7372_a4lc.genpd);
100
101 return ret;
102}
103
104static int pd_power_down_a3rv(struct generic_pm_domain *genpd)
105{
106 int ret = pd_power_down(genpd);
107
108 /* try to power down A4LC after A3RV is requested off */
109 pm_genpd_poweron(&sh7372_a4lc.genpd);
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200110 genpd_queue_power_off_work(&sh7372_a4lc.genpd);
Magnus Damm775b8ae2011-07-10 10:39:32 +0200111
112 return ret;
113}
114
115static int pd_power_down_a4lc(struct generic_pm_domain *genpd)
116{
117 /* only power down A4LC if A3RV is off */
118 if (!(__raw_readl(PSTR) & (1 << sh7372_a3rv.bit_shift)))
119 return pd_power_down(genpd);
120
121 return 0;
122}
123
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200124static bool pd_active_wakeup(struct device *dev)
125{
126 return true;
127}
128
129void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
130{
131 struct generic_pm_domain *genpd = &sh7372_pd->genpd;
132
133 pm_genpd_init(genpd, NULL, false);
134 genpd->stop_device = pm_clk_suspend;
135 genpd->start_device = pm_clk_resume;
136 genpd->active_wakeup = pd_active_wakeup;
Magnus Damm775b8ae2011-07-10 10:39:32 +0200137
138 if (sh7372_pd == &sh7372_a4lc) {
139 genpd->power_off = pd_power_down_a4lc;
140 genpd->power_on = pd_power_up;
141 } else if (sh7372_pd == &sh7372_a3rv) {
142 genpd->power_off = pd_power_down_a3rv;
143 genpd->power_on = pd_power_up_a3rv;
144 } else {
145 genpd->power_off = pd_power_down;
146 genpd->power_on = pd_power_up;
147 }
148 genpd->power_on(&sh7372_pd->genpd);
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200149}
150
151void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
152 struct platform_device *pdev)
153{
154 struct device *dev = &pdev->dev;
155
156 if (!dev->power.subsys_data) {
157 pm_clk_init(dev);
158 pm_clk_add(dev, NULL);
159 }
160 pm_genpd_add_device(&sh7372_pd->genpd, dev);
161}
162
163struct sh7372_pm_domain sh7372_a4lc = {
164 .bit_shift = 1,
165};
166
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200167struct sh7372_pm_domain sh7372_a4mp = {
168 .bit_shift = 2,
169};
170
Magnus Dammd24771d2011-07-10 10:38:22 +0200171struct sh7372_pm_domain sh7372_d4 = {
172 .bit_shift = 3,
173};
174
Magnus Damm33afebf2011-07-01 22:14:45 +0200175struct sh7372_pm_domain sh7372_a3rv = {
176 .bit_shift = 6,
177};
178
Magnus Damm082517a2011-07-01 22:14:53 +0200179struct sh7372_pm_domain sh7372_a3ri = {
180 .bit_shift = 8,
181};
182
Magnus Dammc47586b2011-07-01 22:15:01 +0200183struct sh7372_pm_domain sh7372_a3sg = {
184 .bit_shift = 13,
185};
186
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200187#endif /* CONFIG_PM */
188
Paul Mundt66ad1292011-05-25 11:22:58 +0900189static void sh7372_enter_core_standby(void)
Magnus Damm97991652011-04-29 02:28:08 +0900190{
191 void __iomem *smfram = (void __iomem *)SMFRAM;
192
193 __raw_writel(0, APARMBAREA); /* translate 4k */
194 __raw_writel(__pa(sh7372_cpu_resume), SBAR); /* set reset vector */
195 __raw_writel(0x10, SYSTBCR); /* enable core standby */
196
197 __raw_writel(0, smfram + 0x3c); /* clear page table address */
198
199 sh7372_cpu_suspend();
200 cpu_init();
201
202 /* if page table address is non-NULL then we have been powered down */
203 if (__raw_readl(smfram + 0x3c)) {
204 __raw_writel(__raw_readl(smfram + 0x40),
205 __va(__raw_readl(smfram + 0x3c)));
206
207 flush_tlb_all();
208 set_cr(__raw_readl(smfram + 0x38));
209 }
210
211 __raw_writel(0, SYSTBCR); /* disable core standby */
212 __raw_writel(0, SBAR); /* disable reset vector translation */
213}
214
Magnus Damm082a8ca2011-04-29 02:39:32 +0900215#ifdef CONFIG_CPU_IDLE
216static void sh7372_cpuidle_setup(struct cpuidle_device *dev)
217{
218 struct cpuidle_state *state;
219 int i = dev->state_count;
220
221 state = &dev->states[i];
222 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
223 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
224 state->exit_latency = 10;
225 state->target_residency = 20 + 10;
226 state->power_usage = 1; /* perhaps not */
227 state->flags = 0;
228 state->flags |= CPUIDLE_FLAG_TIME_VALID;
229 shmobile_cpuidle_modes[i] = sh7372_enter_core_standby;
230
231 dev->state_count = i + 1;
232}
233
234static void sh7372_cpuidle_init(void)
235{
236 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
237}
238#else
239static void sh7372_cpuidle_init(void) {}
240#endif
241
242#ifdef CONFIG_SUSPEND
Magnus Damm97991652011-04-29 02:28:08 +0900243static int sh7372_enter_suspend(suspend_state_t suspend_state)
244{
245 sh7372_enter_core_standby();
246 return 0;
247}
248
249static void sh7372_suspend_init(void)
250{
251 shmobile_suspend_ops.enter = sh7372_enter_suspend;
252}
253#else
254static void sh7372_suspend_init(void) {}
255#endif
256
257#define DBGREG1 0xe6100020
258#define DBGREG9 0xe6100040
259
260void __init sh7372_pm_init(void)
261{
262 /* enable DBG hardware block to kick SYSC */
263 __raw_writel(0x0000a500, DBGREG9);
264 __raw_writel(0x0000a501, DBGREG9);
265 __raw_writel(0x00000000, DBGREG1);
266
267 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900268 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900269}