blob: 745af2d96553a66bef74dcd42af254bbb9a85ede [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080015#include <linux/io.h>
16#include <linux/irq.h>
Bryan Wu1394f032007-05-06 14:50:22 -070017
Yi Lieb7bd9c2009-08-07 01:20:58 +000018#include <asm/cplb.h>
Michael Hennerichfd923482007-06-11 16:39:40 +080019#include <asm/gpio.h>
Michael Hennerich1efc80b2008-07-19 16:57:32 +080020#include <asm/dma.h>
21#include <asm/dpmc.h>
Bryan Wu1394f032007-05-06 14:50:22 -070022
Michael Hennerich1efc80b2008-07-19 16:57:32 +080023
Bryan Wu1394f032007-05-06 14:50:22 -070024void bfin_pm_suspend_standby_enter(void)
25{
Michael Hennerich1efc80b2008-07-19 16:57:32 +080026 unsigned long flags;
27
David Howells3b139cd2010-10-07 14:08:52 +010028 flags = hard_local_irq_save();
Michael Hennerich1efc80b2008-07-19 16:57:32 +080029 bfin_pm_standby_setup();
Bryan Wu1394f032007-05-06 14:50:22 -070030
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080031#ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
32 sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080033#else
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080034 sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080035#endif
Bryan Wu1394f032007-05-06 14:50:22 -070036
Michael Hennerich1efc80b2008-07-19 16:57:32 +080037 bfin_pm_standby_restore();
Bryan Wu1394f032007-05-06 14:50:22 -070038
Mike Frysingerbe1d8542009-02-04 16:49:45 +080039#ifdef SIC_IWR0
Michael Hennerich56f5f592008-08-06 17:55:32 +080040 bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080041# ifdef SIC_IWR1
Michael Hennerich55546ac2008-08-13 17:41:13 +080042 /* BF52x system reset does not properly reset SIC_IWR1 which
43 * will screw up the bootrom as it relies on MDMA0/1 waking it
44 * up from IDLE instructions. See this report for more info:
45 * http://blackfin.uclinux.org/gf/tracker/4323
46 */
Mike Frysingerb7e11292008-11-18 17:48:22 +080047 if (ANOMALY_05000435)
48 bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
49 else
50 bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080051# endif
52# ifdef SIC_IWR2
Michael Hennerich56f5f592008-08-06 17:55:32 +080053 bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080054# endif
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080055#else
Michael Hennerich56f5f592008-08-06 17:55:32 +080056 bfin_write_SIC_IWR(IWR_DISABLE_ALL);
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080057#endif
58
David Howells3b139cd2010-10-07 14:08:52 +010059 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -070060}
61
Michael Hennerich1efc80b2008-07-19 16:57:32 +080062int bf53x_suspend_l1_mem(unsigned char *memptr)
63{
Michael Hennerichd1401e12010-06-16 09:12:10 +000064 dma_memcpy_nocache(memptr, (const void *) L1_CODE_START,
65 L1_CODE_LENGTH);
66 dma_memcpy_nocache(memptr + L1_CODE_LENGTH,
67 (const void *) L1_DATA_A_START, L1_DATA_A_LENGTH);
68 dma_memcpy_nocache(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
Michael Hennerich1efc80b2008-07-19 16:57:32 +080069 (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
70 memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
71 L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
72 L1_SCRATCH_LENGTH);
73
74 return 0;
75}
76
77int bf53x_resume_l1_mem(unsigned char *memptr)
78{
Michael Hennerichd1401e12010-06-16 09:12:10 +000079 dma_memcpy_nocache((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
80 dma_memcpy_nocache((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
Michael Hennerich1efc80b2008-07-19 16:57:32 +080081 L1_DATA_A_LENGTH);
Michael Hennerichd1401e12010-06-16 09:12:10 +000082 dma_memcpy_nocache((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
Michael Hennerich1efc80b2008-07-19 16:57:32 +080083 L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
84 memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
85 L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
86
87 return 0;
88}
89
Jie Zhang41ba6532009-06-16 09:48:33 +000090#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
Michael Hennerich1efc80b2008-07-19 16:57:32 +080091static void flushinv_all_dcache(void)
92{
93 u32 way, bank, subbank, set;
94 u32 status, addr;
95 u32 dmem_ctl = bfin_read_DMEM_CONTROL();
96
97 for (bank = 0; bank < 2; ++bank) {
98 if (!(dmem_ctl & (1 << (DMC1_P - bank))))
99 continue;
100
101 for (way = 0; way < 2; ++way)
102 for (subbank = 0; subbank < 4; ++subbank)
103 for (set = 0; set < 64; ++set) {
104
105 bfin_write_DTEST_COMMAND(
106 way << 26 |
107 bank << 23 |
108 subbank << 16 |
109 set << 5
110 );
111 CSYNC();
112 status = bfin_read_DTEST_DATA0();
113
114 /* only worry about valid/dirty entries */
115 if ((status & 0x3) != 0x3)
116 continue;
117
118 /* construct the address using the tag */
119 addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
120
121 /* flush it */
122 __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
123 }
124 }
125}
126#endif
127
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800128int bfin_pm_suspend_mem_enter(void)
129{
130 unsigned long flags;
131 int wakeup, ret;
132
133 unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
134 + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
135 GFP_KERNEL);
136
137 if (memptr == NULL) {
138 panic("bf53x_suspend_l1_mem malloc failed");
139 return -ENOMEM;
140 }
141
142 wakeup = bfin_read_VR_CTL() & ~FREQ;
143 wakeup |= SCKELOW;
144
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800145#ifdef CONFIG_PM_BFIN_WAKE_PH6
146 wakeup |= PHYWE;
147#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800148#ifdef CONFIG_PM_BFIN_WAKE_GP
149 wakeup |= GPWE;
150#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800151
David Howells3b139cd2010-10-07 14:08:52 +0100152 flags = hard_local_irq_save();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800153
154 ret = blackfin_dma_suspend();
155
156 if (ret) {
David Howells3b139cd2010-10-07 14:08:52 +0100157 hard_local_irq_restore(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800158 kfree(memptr);
159 return ret;
160 }
161
162 bfin_gpio_pm_hibernate_suspend();
163
Yi Lieb7bd9c2009-08-07 01:20:58 +0000164#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
165 flushinv_all_dcache();
166#endif
167 _disable_dcplb();
168 _disable_icplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800169 bf53x_suspend_l1_mem(memptr);
170
Michael Hennerichd1401e12010-06-16 09:12:10 +0000171 do_hibernate(wakeup | vr_wakeup); /* See you later! */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800172
173 bf53x_resume_l1_mem(memptr);
174
Yi Lieb7bd9c2009-08-07 01:20:58 +0000175 _enable_icplb();
176 _enable_dcplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800177
178 bfin_gpio_pm_hibernate_restore();
179 blackfin_dma_resume();
180
David Howells3b139cd2010-10-07 14:08:52 +0100181 hard_local_irq_restore(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800182 kfree(memptr);
183
184 return 0;
185}
186
Bryan Wu1394f032007-05-06 14:50:22 -0700187/*
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700188 * bfin_pm_valid - Tell the PM core that we only support the standby sleep
189 * state
190 * @state: suspend state we're checking.
Bryan Wu1394f032007-05-06 14:50:22 -0700191 *
192 */
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700193static int bfin_pm_valid(suspend_state_t state)
Bryan Wu1394f032007-05-06 14:50:22 -0700194{
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800195 return (state == PM_SUSPEND_STANDBY
Michael Hennerichb89df502009-03-28 23:14:41 +0800196#if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800197 /*
198 * On BF533/2/1:
199 * If we enter Hibernate the SCKE Pin is driven Low,
200 * so that the SDRAM enters Self Refresh Mode.
201 * However when the reset sequence that follows hibernate
202 * state is executed, SCKE is driven High, taking the
203 * SDRAM out of Self Refresh.
204 *
205 * If you reconfigure and access the SDRAM "very quickly",
206 * you are likely to avoid errors, otherwise the SDRAM
207 * start losing its contents.
208 * An external HW workaround is possible using logic gates.
209 */
210 || state == PM_SUSPEND_MEM
211#endif
212 );
Bryan Wu1394f032007-05-06 14:50:22 -0700213}
214
215/*
216 * bfin_pm_enter - Actually enter a sleep state.
217 * @state: State we're entering.
218 *
219 */
220static int bfin_pm_enter(suspend_state_t state)
221{
222 switch (state) {
223 case PM_SUSPEND_STANDBY:
224 bfin_pm_suspend_standby_enter();
225 break;
Bryan Wu9d7b6672007-05-21 18:09:37 +0800226 case PM_SUSPEND_MEM:
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800227 bfin_pm_suspend_mem_enter();
228 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700229 default:
230 return -EINVAL;
231 }
232
233 return 0;
234}
235
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100236static const struct platform_suspend_ops bfin_pm_ops = {
Bryan Wu1394f032007-05-06 14:50:22 -0700237 .enter = bfin_pm_enter,
Michael Hennerich4bbd10f2007-08-27 17:29:10 +0800238 .valid = bfin_pm_valid,
Bryan Wu1394f032007-05-06 14:50:22 -0700239};
240
241static int __init bfin_pm_init(void)
242{
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700243 suspend_set_ops(&bfin_pm_ops);
Bryan Wu1394f032007-05-06 14:50:22 -0700244 return 0;
245}
246
247__initcall(bfin_pm_init);