blob: 9ad3b610f0fedfc0425c87193ca4a70f958c47c9 [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
94static bool pd_active_wakeup(struct device *dev)
95{
96 return true;
97}
98
Magnus Dammb9416f02011-07-10 10:38:53 +020099static void sh7372_late_pm_domain_off(void)
100{
101 /* request power down of unused pm domains */
102 queue_work(pm_wq, &sh7372_a4lc.genpd.power_off_work);
103 queue_work(pm_wq, &sh7372_a4mp.genpd.power_off_work);
104 queue_work(pm_wq, &sh7372_d4.genpd.power_off_work);
105 queue_work(pm_wq, &sh7372_a3rv.genpd.power_off_work);
106 queue_work(pm_wq, &sh7372_a3ri.genpd.power_off_work);
107 queue_work(pm_wq, &sh7372_a3sg.genpd.power_off_work);
108}
109
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200110void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
111{
112 struct generic_pm_domain *genpd = &sh7372_pd->genpd;
113
114 pm_genpd_init(genpd, NULL, false);
115 genpd->stop_device = pm_clk_suspend;
116 genpd->start_device = pm_clk_resume;
117 genpd->active_wakeup = pd_active_wakeup;
118 genpd->power_off = pd_power_down;
119 genpd->power_on = pd_power_up;
120 pd_power_up(&sh7372_pd->genpd);
Magnus Dammb9416f02011-07-10 10:38:53 +0200121
122 shmobile_runtime_pm_late_init = sh7372_late_pm_domain_off;
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200123}
124
125void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
126 struct platform_device *pdev)
127{
128 struct device *dev = &pdev->dev;
129
130 if (!dev->power.subsys_data) {
131 pm_clk_init(dev);
132 pm_clk_add(dev, NULL);
133 }
134 pm_genpd_add_device(&sh7372_pd->genpd, dev);
135}
136
137struct sh7372_pm_domain sh7372_a4lc = {
138 .bit_shift = 1,
139};
140
Kuninori Morimotoc1ba5bb2011-07-10 10:12:08 +0200141struct sh7372_pm_domain sh7372_a4mp = {
142 .bit_shift = 2,
143};
144
Magnus Dammd24771d2011-07-10 10:38:22 +0200145struct sh7372_pm_domain sh7372_d4 = {
146 .bit_shift = 3,
147};
148
Magnus Damm33afebf2011-07-01 22:14:45 +0200149struct sh7372_pm_domain sh7372_a3rv = {
150 .bit_shift = 6,
151};
152
Magnus Damm082517a2011-07-01 22:14:53 +0200153struct sh7372_pm_domain sh7372_a3ri = {
154 .bit_shift = 8,
155};
156
Magnus Dammc47586b2011-07-01 22:15:01 +0200157struct sh7372_pm_domain sh7372_a3sg = {
158 .bit_shift = 13,
159};
160
Rafael J. Wysockie3e01092011-07-01 22:13:56 +0200161#endif /* CONFIG_PM */
162
Paul Mundt66ad1292011-05-25 11:22:58 +0900163static void sh7372_enter_core_standby(void)
Magnus Damm97991652011-04-29 02:28:08 +0900164{
165 void __iomem *smfram = (void __iomem *)SMFRAM;
166
167 __raw_writel(0, APARMBAREA); /* translate 4k */
168 __raw_writel(__pa(sh7372_cpu_resume), SBAR); /* set reset vector */
169 __raw_writel(0x10, SYSTBCR); /* enable core standby */
170
171 __raw_writel(0, smfram + 0x3c); /* clear page table address */
172
173 sh7372_cpu_suspend();
174 cpu_init();
175
176 /* if page table address is non-NULL then we have been powered down */
177 if (__raw_readl(smfram + 0x3c)) {
178 __raw_writel(__raw_readl(smfram + 0x40),
179 __va(__raw_readl(smfram + 0x3c)));
180
181 flush_tlb_all();
182 set_cr(__raw_readl(smfram + 0x38));
183 }
184
185 __raw_writel(0, SYSTBCR); /* disable core standby */
186 __raw_writel(0, SBAR); /* disable reset vector translation */
187}
188
Magnus Damm082a8ca2011-04-29 02:39:32 +0900189#ifdef CONFIG_CPU_IDLE
190static void sh7372_cpuidle_setup(struct cpuidle_device *dev)
191{
192 struct cpuidle_state *state;
193 int i = dev->state_count;
194
195 state = &dev->states[i];
196 snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
197 strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
198 state->exit_latency = 10;
199 state->target_residency = 20 + 10;
200 state->power_usage = 1; /* perhaps not */
201 state->flags = 0;
202 state->flags |= CPUIDLE_FLAG_TIME_VALID;
203 shmobile_cpuidle_modes[i] = sh7372_enter_core_standby;
204
205 dev->state_count = i + 1;
206}
207
208static void sh7372_cpuidle_init(void)
209{
210 shmobile_cpuidle_setup = sh7372_cpuidle_setup;
211}
212#else
213static void sh7372_cpuidle_init(void) {}
214#endif
215
216#ifdef CONFIG_SUSPEND
Magnus Damm97991652011-04-29 02:28:08 +0900217static int sh7372_enter_suspend(suspend_state_t suspend_state)
218{
219 sh7372_enter_core_standby();
220 return 0;
221}
222
223static void sh7372_suspend_init(void)
224{
225 shmobile_suspend_ops.enter = sh7372_enter_suspend;
226}
227#else
228static void sh7372_suspend_init(void) {}
229#endif
230
231#define DBGREG1 0xe6100020
232#define DBGREG9 0xe6100040
233
234void __init sh7372_pm_init(void)
235{
236 /* enable DBG hardware block to kick SYSC */
237 __raw_writel(0x0000a500, DBGREG9);
238 __raw_writel(0x0000a501, DBGREG9);
239 __raw_writel(0x00000000, DBGREG1);
240
241 sh7372_suspend_init();
Magnus Damm082a8ca2011-04-29 02:39:32 +0900242 sh7372_cpuidle_init();
Magnus Damm97991652011-04-29 02:28:08 +0900243}