blob: ea7f95f6bb4c92b4cd779a3c158bce85c9694361 [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
Yi Li6a01f232009-01-07 23:14:39 +080028 local_irq_save_hw(flags);
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
Yi Li6a01f232009-01-07 23:14:39 +080059 local_irq_restore_hw(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{
64 dma_memcpy(memptr, (const void *) L1_CODE_START, L1_CODE_LENGTH);
65 dma_memcpy(memptr + L1_CODE_LENGTH, (const void *) L1_DATA_A_START,
66 L1_DATA_A_LENGTH);
67 dma_memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
68 (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
69 memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
70 L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
71 L1_SCRATCH_LENGTH);
72
73 return 0;
74}
75
76int bf53x_resume_l1_mem(unsigned char *memptr)
77{
78 dma_memcpy((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
79 dma_memcpy((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
80 L1_DATA_A_LENGTH);
81 dma_memcpy((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
82 L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
83 memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
84 L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
85
86 return 0;
87}
88
Jie Zhang41ba6532009-06-16 09:48:33 +000089#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
Michael Hennerich1efc80b2008-07-19 16:57:32 +080090static void flushinv_all_dcache(void)
91{
92 u32 way, bank, subbank, set;
93 u32 status, addr;
94 u32 dmem_ctl = bfin_read_DMEM_CONTROL();
95
96 for (bank = 0; bank < 2; ++bank) {
97 if (!(dmem_ctl & (1 << (DMC1_P - bank))))
98 continue;
99
100 for (way = 0; way < 2; ++way)
101 for (subbank = 0; subbank < 4; ++subbank)
102 for (set = 0; set < 64; ++set) {
103
104 bfin_write_DTEST_COMMAND(
105 way << 26 |
106 bank << 23 |
107 subbank << 16 |
108 set << 5
109 );
110 CSYNC();
111 status = bfin_read_DTEST_DATA0();
112
113 /* only worry about valid/dirty entries */
114 if ((status & 0x3) != 0x3)
115 continue;
116
117 /* construct the address using the tag */
118 addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
119
120 /* flush it */
121 __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
122 }
123 }
124}
125#endif
126
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800127int bfin_pm_suspend_mem_enter(void)
128{
129 unsigned long flags;
130 int wakeup, ret;
131
132 unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
133 + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
134 GFP_KERNEL);
135
136 if (memptr == NULL) {
137 panic("bf53x_suspend_l1_mem malloc failed");
138 return -ENOMEM;
139 }
140
141 wakeup = bfin_read_VR_CTL() & ~FREQ;
142 wakeup |= SCKELOW;
143
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800144#ifdef CONFIG_PM_BFIN_WAKE_PH6
145 wakeup |= PHYWE;
146#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800147#ifdef CONFIG_PM_BFIN_WAKE_GP
148 wakeup |= GPWE;
149#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800150
Yi Li6a01f232009-01-07 23:14:39 +0800151 local_irq_save_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800152
153 ret = blackfin_dma_suspend();
154
155 if (ret) {
Yi Li6a01f232009-01-07 23:14:39 +0800156 local_irq_restore_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800157 kfree(memptr);
158 return ret;
159 }
160
161 bfin_gpio_pm_hibernate_suspend();
162
Yi Lieb7bd9c2009-08-07 01:20:58 +0000163#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
164 flushinv_all_dcache();
165#endif
166 _disable_dcplb();
167 _disable_icplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800168 bf53x_suspend_l1_mem(memptr);
169
Michael Hennerich4a88d0c2008-08-05 17:38:41 +0800170 do_hibernate(wakeup | vr_wakeup); /* Goodbye */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800171
172 bf53x_resume_l1_mem(memptr);
173
Yi Lieb7bd9c2009-08-07 01:20:58 +0000174 _enable_icplb();
175 _enable_dcplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800176
177 bfin_gpio_pm_hibernate_restore();
178 blackfin_dma_resume();
179
Yi Li6a01f232009-01-07 23:14:39 +0800180 local_irq_restore_hw(flags);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800181 kfree(memptr);
182
183 return 0;
184}
185
Bryan Wu1394f032007-05-06 14:50:22 -0700186/*
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700187 * bfin_pm_valid - Tell the PM core that we only support the standby sleep
188 * state
189 * @state: suspend state we're checking.
Bryan Wu1394f032007-05-06 14:50:22 -0700190 *
191 */
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700192static int bfin_pm_valid(suspend_state_t state)
Bryan Wu1394f032007-05-06 14:50:22 -0700193{
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800194 return (state == PM_SUSPEND_STANDBY
Michael Hennerichb89df502009-03-28 23:14:41 +0800195#if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800196 /*
197 * On BF533/2/1:
198 * If we enter Hibernate the SCKE Pin is driven Low,
199 * so that the SDRAM enters Self Refresh Mode.
200 * However when the reset sequence that follows hibernate
201 * state is executed, SCKE is driven High, taking the
202 * SDRAM out of Self Refresh.
203 *
204 * If you reconfigure and access the SDRAM "very quickly",
205 * you are likely to avoid errors, otherwise the SDRAM
206 * start losing its contents.
207 * An external HW workaround is possible using logic gates.
208 */
209 || state == PM_SUSPEND_MEM
210#endif
211 );
Bryan Wu1394f032007-05-06 14:50:22 -0700212}
213
214/*
215 * bfin_pm_enter - Actually enter a sleep state.
216 * @state: State we're entering.
217 *
218 */
219static int bfin_pm_enter(suspend_state_t state)
220{
221 switch (state) {
222 case PM_SUSPEND_STANDBY:
223 bfin_pm_suspend_standby_enter();
224 break;
Bryan Wu9d7b6672007-05-21 18:09:37 +0800225 case PM_SUSPEND_MEM:
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800226 bfin_pm_suspend_mem_enter();
227 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700228 default:
229 return -EINVAL;
230 }
231
232 return 0;
233}
234
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700235struct platform_suspend_ops bfin_pm_ops = {
Bryan Wu1394f032007-05-06 14:50:22 -0700236 .enter = bfin_pm_enter,
Michael Hennerich4bbd10f2007-08-27 17:29:10 +0800237 .valid = bfin_pm_valid,
Bryan Wu1394f032007-05-06 14:50:22 -0700238};
239
240static int __init bfin_pm_init(void)
241{
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700242 suspend_set_ops(&bfin_pm_ops);
Bryan Wu1394f032007-05-06 14:50:22 -0700243 return 0;
244}
245
246__initcall(bfin_pm_init);