71400eae2b686b1f4c00c26f63729caf3fe645f4
[pandora-kernel.git] / arch / arm / mach-shmobile / pm-sh7372.c
1 /*
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>
13 #include <linux/cpuidle.h>
14 #include <linux/module.h>
15 #include <linux/list.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <asm/system.h>
22 #include <asm/io.h>
23 #include <asm/tlbflush.h>
24 #include <mach/common.h>
25 #include <mach/sh7372.h>
26
27 #define SMFRAM 0xe6a70000
28 #define SYSTBCR 0xe6150024
29 #define SBAR 0xe6180020
30 #define APARMBAREA 0xe6f10020
31
32 #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
41 static 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
64 static 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
94 static 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
104 static 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);
110         queue_work(pm_wq, &sh7372_a4lc.genpd.power_off_work);
111
112         return ret;
113 }
114
115 static 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
124 static bool pd_active_wakeup(struct device *dev)
125 {
126         return true;
127 }
128
129 static void sh7372_late_pm_domain_off(void)
130 {
131         /* request power down of unused pm domains */
132         queue_work(pm_wq, &sh7372_a4lc.genpd.power_off_work);
133         queue_work(pm_wq, &sh7372_a4mp.genpd.power_off_work);
134         queue_work(pm_wq, &sh7372_d4.genpd.power_off_work);
135         queue_work(pm_wq, &sh7372_a3rv.genpd.power_off_work);
136         queue_work(pm_wq, &sh7372_a3ri.genpd.power_off_work);
137         queue_work(pm_wq, &sh7372_a3sg.genpd.power_off_work);
138 }
139
140 void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
141 {
142         struct generic_pm_domain *genpd = &sh7372_pd->genpd;
143
144         pm_genpd_init(genpd, NULL, false);
145         genpd->stop_device = pm_clk_suspend;
146         genpd->start_device = pm_clk_resume;
147         genpd->active_wakeup = pd_active_wakeup;
148
149         if (sh7372_pd == &sh7372_a4lc) {
150                 genpd->power_off = pd_power_down_a4lc;
151                 genpd->power_on = pd_power_up;
152         } else if (sh7372_pd == &sh7372_a3rv) {
153                 genpd->power_off = pd_power_down_a3rv;
154                 genpd->power_on = pd_power_up_a3rv;
155         } else {
156                 genpd->power_off = pd_power_down;
157                 genpd->power_on = pd_power_up;
158         }
159         genpd->power_on(&sh7372_pd->genpd);
160
161         shmobile_runtime_pm_late_init = sh7372_late_pm_domain_off;
162 }
163
164 void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
165                                  struct platform_device *pdev)
166 {
167         struct device *dev = &pdev->dev;
168
169         if (!dev->power.subsys_data) {
170                 pm_clk_init(dev);
171                 pm_clk_add(dev, NULL);
172         }
173         pm_genpd_add_device(&sh7372_pd->genpd, dev);
174 }
175
176 struct sh7372_pm_domain sh7372_a4lc = {
177         .bit_shift = 1,
178 };
179
180 struct sh7372_pm_domain sh7372_a4mp = {
181         .bit_shift = 2,
182 };
183
184 struct sh7372_pm_domain sh7372_d4 = {
185         .bit_shift = 3,
186 };
187
188 struct sh7372_pm_domain sh7372_a3rv = {
189         .bit_shift = 6,
190 };
191
192 struct sh7372_pm_domain sh7372_a3ri = {
193         .bit_shift = 8,
194 };
195
196 struct sh7372_pm_domain sh7372_a3sg = {
197         .bit_shift = 13,
198 };
199
200 #endif /* CONFIG_PM */
201
202 static void sh7372_enter_core_standby(void)
203 {
204         void __iomem *smfram = (void __iomem *)SMFRAM;
205
206         __raw_writel(0, APARMBAREA); /* translate 4k */
207         __raw_writel(__pa(sh7372_cpu_resume), SBAR); /* set reset vector */
208         __raw_writel(0x10, SYSTBCR); /* enable core standby */
209
210         __raw_writel(0, smfram + 0x3c); /* clear page table address */
211
212         sh7372_cpu_suspend();
213         cpu_init();
214
215         /* if page table address is non-NULL then we have been powered down */
216         if (__raw_readl(smfram + 0x3c)) {
217                 __raw_writel(__raw_readl(smfram + 0x40),
218                              __va(__raw_readl(smfram + 0x3c)));
219
220                 flush_tlb_all();
221                 set_cr(__raw_readl(smfram + 0x38));
222         }
223
224         __raw_writel(0, SYSTBCR); /* disable core standby */
225         __raw_writel(0, SBAR); /* disable reset vector translation */
226 }
227
228 #ifdef CONFIG_CPU_IDLE
229 static void sh7372_cpuidle_setup(struct cpuidle_device *dev)
230 {
231         struct cpuidle_state *state;
232         int i = dev->state_count;
233
234         state = &dev->states[i];
235         snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
236         strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
237         state->exit_latency = 10;
238         state->target_residency = 20 + 10;
239         state->power_usage = 1; /* perhaps not */
240         state->flags = 0;
241         state->flags |= CPUIDLE_FLAG_TIME_VALID;
242         shmobile_cpuidle_modes[i] = sh7372_enter_core_standby;
243
244         dev->state_count = i + 1;
245 }
246
247 static void sh7372_cpuidle_init(void)
248 {
249         shmobile_cpuidle_setup = sh7372_cpuidle_setup;
250 }
251 #else
252 static void sh7372_cpuidle_init(void) {}
253 #endif
254
255 #ifdef CONFIG_SUSPEND
256 static int sh7372_enter_suspend(suspend_state_t suspend_state)
257 {
258         sh7372_enter_core_standby();
259         return 0;
260 }
261
262 static void sh7372_suspend_init(void)
263 {
264         shmobile_suspend_ops.enter = sh7372_enter_suspend;
265 }
266 #else
267 static void sh7372_suspend_init(void) {}
268 #endif
269
270 #define DBGREG1 0xe6100020
271 #define DBGREG9 0xe6100040
272
273 void __init sh7372_pm_init(void)
274 {
275         /* enable DBG hardware block to kick SYSC */
276         __raw_writel(0x0000a500, DBGREG9);
277         __raw_writel(0x0000a501, DBGREG9);
278         __raw_writel(0x00000000, DBGREG1);
279
280         sh7372_suspend_init();
281         sh7372_cpuidle_init();
282 }