blob: 610a3cdf39986fcf1515425b11dcd7785b717842 [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>
Steven Miao93f89512012-05-16 18:26:10 +080022#include <asm/pm.h>
Bryan Wu1394f032007-05-06 14:50:22 -070023
Steven Miao93f89512012-05-16 18:26:10 +080024#ifdef CONFIG_BF60x
25struct bfin_cpu_pm_fns *bfin_cpu_pm;
26#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +080027
Bryan Wu1394f032007-05-06 14:50:22 -070028void bfin_pm_suspend_standby_enter(void)
29{
Steven Miao93f89512012-05-16 18:26:10 +080030#ifndef CONFIG_BF60x
Michael Hennerich1efc80b2008-07-19 16:57:32 +080031 bfin_pm_standby_setup();
Sonic Zhangfb5f0042007-12-23 23:02:13 +080032#endif
Bryan Wu1394f032007-05-06 14:50:22 -070033
Steven Miao93f89512012-05-16 18:26:10 +080034#ifdef CONFIG_BF60x
35 bfin_cpu_pm->enter(PM_SUSPEND_STANDBY);
36#else
37# ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
38 sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
39# else
40 sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
41# endif
42#endif
Bryan Wu1394f032007-05-06 14:50:22 -070043
Steven Miao93f89512012-05-16 18:26:10 +080044#ifndef CONFIG_BF60x
45 bfin_pm_standby_restore();
46#endif
47
48#ifndef CONFIG_BF60x
Mike Frysingerbe1d8542009-02-04 16:49:45 +080049#ifdef SIC_IWR0
Michael Hennerich56f5f592008-08-06 17:55:32 +080050 bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080051# ifdef SIC_IWR1
Michael Hennerich55546ac2008-08-13 17:41:13 +080052 /* BF52x system reset does not properly reset SIC_IWR1 which
53 * will screw up the bootrom as it relies on MDMA0/1 waking it
54 * up from IDLE instructions. See this report for more info:
55 * http://blackfin.uclinux.org/gf/tracker/4323
56 */
Mike Frysingerb7e11292008-11-18 17:48:22 +080057 if (ANOMALY_05000435)
58 bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
59 else
60 bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
Mike Frysingerbe1d8542009-02-04 16:49:45 +080061# endif
62# ifdef SIC_IWR2
Michael Hennerich56f5f592008-08-06 17:55:32 +080063 bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
Sonic Zhangfb5f0042007-12-23 23:02:13 +080064# endif
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080065#else
Michael Hennerich56f5f592008-08-06 17:55:32 +080066 bfin_write_SIC_IWR(IWR_DISABLE_ALL);
Michael Hennerichcfefe3c2008-02-09 04:12:37 +080067#endif
Steven Miao93f89512012-05-16 18:26:10 +080068
69#endif
Bryan Wu1394f032007-05-06 14:50:22 -070070}
71
Michael Hennerich1efc80b2008-07-19 16:57:32 +080072int bf53x_suspend_l1_mem(unsigned char *memptr)
73{
Michael Hennerichd1401e12010-06-16 09:12:10 +000074 dma_memcpy_nocache(memptr, (const void *) L1_CODE_START,
75 L1_CODE_LENGTH);
76 dma_memcpy_nocache(memptr + L1_CODE_LENGTH,
77 (const void *) L1_DATA_A_START, L1_DATA_A_LENGTH);
78 dma_memcpy_nocache(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
Michael Hennerich1efc80b2008-07-19 16:57:32 +080079 (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
80 memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
81 L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
82 L1_SCRATCH_LENGTH);
83
84 return 0;
85}
86
87int bf53x_resume_l1_mem(unsigned char *memptr)
88{
Michael Hennerichd1401e12010-06-16 09:12:10 +000089 dma_memcpy_nocache((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
90 dma_memcpy_nocache((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
Michael Hennerich1efc80b2008-07-19 16:57:32 +080091 L1_DATA_A_LENGTH);
Michael Hennerichd1401e12010-06-16 09:12:10 +000092 dma_memcpy_nocache((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
Michael Hennerich1efc80b2008-07-19 16:57:32 +080093 L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
94 memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
95 L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
96
97 return 0;
98}
99
Jie Zhang41ba6532009-06-16 09:48:33 +0000100#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
Steven Miao93f89512012-05-16 18:26:10 +0800101# ifdef CONFIG_BF60x
102__attribute__((l1_text))
103# endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800104static void flushinv_all_dcache(void)
105{
Steven Miao93f89512012-05-16 18:26:10 +0800106 register u32 way, bank, subbank, set;
107 register u32 status, addr;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800108 u32 dmem_ctl = bfin_read_DMEM_CONTROL();
109
110 for (bank = 0; bank < 2; ++bank) {
111 if (!(dmem_ctl & (1 << (DMC1_P - bank))))
112 continue;
113
114 for (way = 0; way < 2; ++way)
115 for (subbank = 0; subbank < 4; ++subbank)
116 for (set = 0; set < 64; ++set) {
117
118 bfin_write_DTEST_COMMAND(
119 way << 26 |
120 bank << 23 |
121 subbank << 16 |
122 set << 5
123 );
124 CSYNC();
125 status = bfin_read_DTEST_DATA0();
126
127 /* only worry about valid/dirty entries */
128 if ((status & 0x3) != 0x3)
129 continue;
130
131 /* construct the address using the tag */
132 addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
133
134 /* flush it */
135 __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
136 }
137 }
138}
139#endif
140
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800141int bfin_pm_suspend_mem_enter(void)
142{
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800143 int wakeup, ret;
144
145 unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
146 + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
147 GFP_KERNEL);
148
149 if (memptr == NULL) {
150 panic("bf53x_suspend_l1_mem malloc failed");
151 return -ENOMEM;
152 }
153
Steven Miao93f89512012-05-16 18:26:10 +0800154#ifndef CONFIG_BF60x
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800155 wakeup = bfin_read_VR_CTL() & ~FREQ;
156 wakeup |= SCKELOW;
157
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800158#ifdef CONFIG_PM_BFIN_WAKE_PH6
159 wakeup |= PHYWE;
160#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800161#ifdef CONFIG_PM_BFIN_WAKE_GP
162 wakeup |= GPWE;
163#endif
Steven Miao0fbd88c2012-05-17 17:29:54 +0800164#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800165
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800166 ret = blackfin_dma_suspend();
167
168 if (ret) {
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800169 kfree(memptr);
170 return ret;
171 }
172
173 bfin_gpio_pm_hibernate_suspend();
174
Yi Lieb7bd9c2009-08-07 01:20:58 +0000175#if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
176 flushinv_all_dcache();
177#endif
178 _disable_dcplb();
179 _disable_icplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800180 bf53x_suspend_l1_mem(memptr);
181
Steven Miao93f89512012-05-16 18:26:10 +0800182#ifndef CONFIG_BF60x
Michael Hennerichd1401e12010-06-16 09:12:10 +0000183 do_hibernate(wakeup | vr_wakeup); /* See you later! */
Steven Miao93f89512012-05-16 18:26:10 +0800184#else
185 bfin_cpu_pm->enter(PM_SUSPEND_MEM);
186#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800187
188 bf53x_resume_l1_mem(memptr);
189
Yi Lieb7bd9c2009-08-07 01:20:58 +0000190 _enable_icplb();
191 _enable_dcplb();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800192
193 bfin_gpio_pm_hibernate_restore();
194 blackfin_dma_resume();
195
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800196 kfree(memptr);
197
198 return 0;
199}
200
Bryan Wu1394f032007-05-06 14:50:22 -0700201/*
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700202 * bfin_pm_valid - Tell the PM core that we only support the standby sleep
203 * state
204 * @state: suspend state we're checking.
Bryan Wu1394f032007-05-06 14:50:22 -0700205 *
206 */
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700207static int bfin_pm_valid(suspend_state_t state)
Bryan Wu1394f032007-05-06 14:50:22 -0700208{
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800209 return (state == PM_SUSPEND_STANDBY
Michael Hennerichb89df502009-03-28 23:14:41 +0800210#if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800211 /*
212 * On BF533/2/1:
213 * If we enter Hibernate the SCKE Pin is driven Low,
214 * so that the SDRAM enters Self Refresh Mode.
215 * However when the reset sequence that follows hibernate
216 * state is executed, SCKE is driven High, taking the
217 * SDRAM out of Self Refresh.
218 *
219 * If you reconfigure and access the SDRAM "very quickly",
220 * you are likely to avoid errors, otherwise the SDRAM
221 * start losing its contents.
222 * An external HW workaround is possible using logic gates.
223 */
224 || state == PM_SUSPEND_MEM
225#endif
226 );
Bryan Wu1394f032007-05-06 14:50:22 -0700227}
228
229/*
230 * bfin_pm_enter - Actually enter a sleep state.
231 * @state: State we're entering.
232 *
233 */
234static int bfin_pm_enter(suspend_state_t state)
235{
236 switch (state) {
237 case PM_SUSPEND_STANDBY:
238 bfin_pm_suspend_standby_enter();
239 break;
Bryan Wu9d7b6672007-05-21 18:09:37 +0800240 case PM_SUSPEND_MEM:
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800241 bfin_pm_suspend_mem_enter();
242 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700243 default:
244 return -EINVAL;
245 }
246
247 return 0;
248}
249
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100250static const struct platform_suspend_ops bfin_pm_ops = {
Bryan Wu1394f032007-05-06 14:50:22 -0700251 .enter = bfin_pm_enter,
Michael Hennerich4bbd10f2007-08-27 17:29:10 +0800252 .valid = bfin_pm_valid,
Bryan Wu1394f032007-05-06 14:50:22 -0700253};
254
255static int __init bfin_pm_init(void)
256{
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700257 suspend_set_ops(&bfin_pm_ops);
Bryan Wu1394f032007-05-06 14:50:22 -0700258 return 0;
259}
260
261__initcall(bfin_pm_init);