blob: 8837be4edb4a47ebb90501129d2aa69fc725486e [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Blackfin power management
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Copyright 2006-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07005 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2
7 * based on arm/mach-omap/pm.c
8 * Copyright 2001, Cliff Brake <cbrake@accelent.com> and others
Bryan Wu1394f032007-05-06 14:50:22 -07009 */
10
Rafael J. Wysocki95d9ffb2007-10-18 03:04:39 -070011#include <linux/suspend.h>
Bryan Wu1394f032007-05-06 14:50:22 -070012#include <linux/sched.h>
13#include <linux/proc_fs.h>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080014#include <linux/io.h>
15#include <linux/irq.h>
Bryan Wu1394f032007-05-06 14:50:22 -070016
Yi Lieb7bd9c2009-08-07 01:20:58 +000017#include <asm/cplb.h>
Michael Hennerichfd923482007-06-11 16:39:40 +080018#include <asm/gpio.h>
Michael Hennerich1efc80b2008-07-19 16:57:32 +080019#include <asm/dma.h>
20#include <asm/dpmc.h>
Bryan Wu1394f032007-05-06 14:50:22 -070021
22#ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_H
23#define WAKEUP_TYPE PM_WAKE_HIGH
24#endif
25
26#ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_L
27#define WAKEUP_TYPE PM_WAKE_LOW
28#endif
29
30#ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_F
31#define WAKEUP_TYPE PM_WAKE_FALLING
32#endif
33
34#ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_R
35#define WAKEUP_TYPE PM_WAKE_RISING
36#endif
37
38#ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_B
39#define WAKEUP_TYPE PM_WAKE_BOTH_EDGES
40#endif
41
Michael Hennerich1efc80b2008-07-19 16:57:32 +080042
Bryan Wu1394f032007-05-06 14:50:22 -070043void bfin_pm_suspend_standby_enter(void)
44{
Michael Hennerich1efc80b2008-07-19 16:57:32 +080045 unsigned long flags;
46
Bryan Wu1394f032007-05-06 14:50:22 -070047#ifdef CONFIG_PM_WAKEUP_BY_GPIO
48 gpio_pm_wakeup_request(CONFIG_PM_WAKEUP_GPIO_NUMBER, WAKEUP_TYPE);
49#endif
50
Yi Li6a01f232009-01-07 23:14:39 +080051 local_irq_save_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +080052 bfin_pm_standby_setup();
Bryan Wu1394f032007-05-06 14:50:22 -070053
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080054#ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
55 sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080056#else
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080057 sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080058#endif
Bryan Wu1394f032007-05-06 14:50:22 -070059
Michael Hennerich1efc80b2008-07-19 16:57:32 +080060 bfin_pm_standby_restore();
Bryan Wu1394f032007-05-06 14:50:22 -070061
Mike Frysingerbe1d8542009-02-04 16:49:45 +080062#ifdef SIC_IWR0
Michael Hennerich56f5f592008-08-06 17:55:32 +080063 bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080064# ifdef SIC_IWR1
Michael Hennerich55546ac2008-08-13 17:41:13 +080065 /* BF52x system reset does not properly reset SIC_IWR1 which
66 * will screw up the bootrom as it relies on MDMA0/1 waking it
67 * up from IDLE instructions. See this report for more info:
68 * http://blackfin.uclinux.org/gf/tracker/4323
69 */
Mike Frysingerb7e11292008-11-18 17:48:22 +080070 if (ANOMALY_05000435)
71 bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
72 else
73 bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080074# endif
75# ifdef SIC_IWR2
Michael Hennerich56f5f592008-08-06 17:55:32 +080076 bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080077# endif
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080078#else
Michael Hennerich56f5f592008-08-06 17:55:32 +080079 bfin_write_SIC_IWR(IWR_DISABLE_ALL);
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080080#endif
81
Yi Li6a01f232009-01-07 23:14:39 +080082 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -070083}
84
Michael Hennerich1efc80b2008-07-19 16:57:32 +080085int bf53x_suspend_l1_mem(unsigned char *memptr)
86{
87 dma_memcpy(memptr, (const void *) L1_CODE_START, L1_CODE_LENGTH);
88 dma_memcpy(memptr + L1_CODE_LENGTH, (const void *) L1_DATA_A_START,
89 L1_DATA_A_LENGTH);
90 dma_memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
91 (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
92 memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
93 L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
94 L1_SCRATCH_LENGTH);
95
96 return 0;
97}
98
99int bf53x_resume_l1_mem(unsigned char *memptr)
100{
101 dma_memcpy((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
102 dma_memcpy((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
103 L1_DATA_A_LENGTH);
104 dma_memcpy((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
105 L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
106 memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
107 L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
108
109 return 0;
110}
111
Jie Zhang41ba6532009-06-16 09:48:33 +0000112#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800113static void flushinv_all_dcache(void)
114{
115 u32 way, bank, subbank, set;
116 u32 status, addr;
117 u32 dmem_ctl = bfin_read_DMEM_CONTROL();
118
119 for (bank = 0; bank < 2; ++bank) {
120 if (!(dmem_ctl & (1 << (DMC1_P - bank))))
121 continue;
122
123 for (way = 0; way < 2; ++way)
124 for (subbank = 0; subbank < 4; ++subbank)
125 for (set = 0; set < 64; ++set) {
126
127 bfin_write_DTEST_COMMAND(
128 way << 26 |
129 bank << 23 |
130 subbank << 16 |
131 set << 5
132 );
133 CSYNC();
134 status = bfin_read_DTEST_DATA0();
135
136 /* only worry about valid/dirty entries */
137 if ((status & 0x3) != 0x3)
138 continue;
139
140 /* construct the address using the tag */
141 addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
142
143 /* flush it */
144 __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
145 }
146 }
147}
148#endif
149
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800150int bfin_pm_suspend_mem_enter(void)
151{
152 unsigned long flags;
153 int wakeup, ret;
154
155 unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
156 + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
157 GFP_KERNEL);
158
159 if (memptr == NULL) {
160 panic("bf53x_suspend_l1_mem malloc failed");
161 return -ENOMEM;
162 }
163
164 wakeup = bfin_read_VR_CTL() & ~FREQ;
165 wakeup |= SCKELOW;
166
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800167#ifdef CONFIG_PM_BFIN_WAKE_PH6
168 wakeup |= PHYWE;
169#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800170#ifdef CONFIG_PM_BFIN_WAKE_GP
171 wakeup |= GPWE;
172#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800173
Yi Li6a01f232009-01-07 23:14:39 +0800174 local_irq_save_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800175
176 ret = blackfin_dma_suspend();
177
178 if (ret) {
Yi Li6a01f232009-01-07 23:14:39 +0800179 local_irq_restore_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800180 kfree(memptr);
181 return ret;
182 }
183
184 bfin_gpio_pm_hibernate_suspend();
185
Yi Lieb7bd9c2009-08-07 01:20:58 +0000186#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
187 flushinv_all_dcache();
188#endif
189 _disable_dcplb();
190 _disable_icplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800191 bf53x_suspend_l1_mem(memptr);
192
Michael Hennerich4a88d0c2008-08-05 17:38:41 +0800193 do_hibernate(wakeup | vr_wakeup); /* Goodbye */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800194
195 bf53x_resume_l1_mem(memptr);
196
Yi Lieb7bd9c2009-08-07 01:20:58 +0000197 _enable_icplb();
198 _enable_dcplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800199
200 bfin_gpio_pm_hibernate_restore();
201 blackfin_dma_resume();
202
Yi Li6a01f232009-01-07 23:14:39 +0800203 local_irq_restore_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800204 kfree(memptr);
205
206 return 0;
207}
208
Bryan Wu1394f032007-05-06 14:50:22 -0700209/*
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700210 * bfin_pm_valid - Tell the PM core that we only support the standby sleep
211 * state
212 * @state: suspend state we're checking.
Bryan Wu1394f032007-05-06 14:50:22 -0700213 *
214 */
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700215static int bfin_pm_valid(suspend_state_t state)
Bryan Wu1394f032007-05-06 14:50:22 -0700216{
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800217 return (state == PM_SUSPEND_STANDBY
Michael Hennerichb89df502009-03-28 23:14:41 +0800218#if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800219 /*
220 * On BF533/2/1:
221 * If we enter Hibernate the SCKE Pin is driven Low,
222 * so that the SDRAM enters Self Refresh Mode.
223 * However when the reset sequence that follows hibernate
224 * state is executed, SCKE is driven High, taking the
225 * SDRAM out of Self Refresh.
226 *
227 * If you reconfigure and access the SDRAM "very quickly",
228 * you are likely to avoid errors, otherwise the SDRAM
229 * start losing its contents.
230 * An external HW workaround is possible using logic gates.
231 */
232 || state == PM_SUSPEND_MEM
233#endif
234 );
Bryan Wu1394f032007-05-06 14:50:22 -0700235}
236
237/*
238 * bfin_pm_enter - Actually enter a sleep state.
239 * @state: State we're entering.
240 *
241 */
242static int bfin_pm_enter(suspend_state_t state)
243{
244 switch (state) {
245 case PM_SUSPEND_STANDBY:
246 bfin_pm_suspend_standby_enter();
247 break;
Bryan Wu9d7b6672007-05-21 18:09:37 +0800248 case PM_SUSPEND_MEM:
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800249 bfin_pm_suspend_mem_enter();
250 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700251 default:
252 return -EINVAL;
253 }
254
255 return 0;
256}
257
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700258struct platform_suspend_ops bfin_pm_ops = {
Bryan Wu1394f032007-05-06 14:50:22 -0700259 .enter = bfin_pm_enter,
Michael Hennerich4bbd10f2007-08-27 17:29:10 +0800260 .valid = bfin_pm_valid,
Bryan Wu1394f032007-05-06 14:50:22 -0700261};
262
263static int __init bfin_pm_init(void)
264{
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700265 suspend_set_ops(&bfin_pm_ops);
Bryan Wu1394f032007-05-06 14:50:22 -0700266 return 0;
267}
268
269__initcall(bfin_pm_init);