Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 2 | * Blackfin power management |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Copyright 2006-2009 Analog Devices Inc. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 5 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 6 | * Licensed under the GPL-2 |
| 7 | * based on arm/mach-omap/pm.c |
| 8 | * Copyright 2001, Cliff Brake <cbrake@accelent.com> and others |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Rafael J. Wysocki | 95d9ffb | 2007-10-18 03:04:39 -0700 | [diff] [blame] | 11 | #include <linux/suspend.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include <linux/proc_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/irq.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 17 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 18 | #include <asm/cplb.h> |
Michael Hennerich | fd92348 | 2007-06-11 16:39:40 +0800 | [diff] [blame] | 19 | #include <asm/gpio.h> |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 20 | #include <asm/dma.h> |
| 21 | #include <asm/dpmc.h> |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 22 | #include <asm/pm.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 23 | |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 24 | #ifdef CONFIG_BF60x |
| 25 | struct bfin_cpu_pm_fns *bfin_cpu_pm; |
| 26 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 27 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 28 | void bfin_pm_suspend_standby_enter(void) |
| 29 | { |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 30 | #ifndef CONFIG_BF60x |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 31 | bfin_pm_standby_setup(); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 32 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 33 | |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 34 | #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 Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 43 | |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 44 | #ifndef CONFIG_BF60x |
| 45 | bfin_pm_standby_restore(); |
| 46 | #endif |
| 47 | |
| 48 | #ifndef CONFIG_BF60x |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 49 | #ifdef SIC_IWR0 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 50 | bfin_write_SIC_IWR0(IWR_DISABLE_ALL); |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 51 | # ifdef SIC_IWR1 |
Michael Hennerich | 55546ac | 2008-08-13 17:41:13 +0800 | [diff] [blame] | 52 | /* 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 Frysinger | b7e1129 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 57 | 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 Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 61 | # endif |
| 62 | # ifdef SIC_IWR2 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 63 | bfin_write_SIC_IWR2(IWR_DISABLE_ALL); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 64 | # endif |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 65 | #else |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 66 | bfin_write_SIC_IWR(IWR_DISABLE_ALL); |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 67 | #endif |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 68 | |
| 69 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 72 | int bf53x_suspend_l1_mem(unsigned char *memptr) |
| 73 | { |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 74 | 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 79 | (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 | |
| 87 | int bf53x_resume_l1_mem(unsigned char *memptr) |
| 88 | { |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 89 | 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 91 | L1_DATA_A_LENGTH); |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 92 | dma_memcpy_nocache((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH + |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 93 | 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 Zhang | 41ba653 | 2009-06-16 09:48:33 +0000 | [diff] [blame] | 100 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 101 | # ifdef CONFIG_BF60x |
| 102 | __attribute__((l1_text)) |
| 103 | # endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 104 | static void flushinv_all_dcache(void) |
| 105 | { |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 106 | register u32 way, bank, subbank, set; |
| 107 | register u32 status, addr; |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 108 | 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 141 | int bfin_pm_suspend_mem_enter(void) |
| 142 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 143 | 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 Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 154 | #ifndef CONFIG_BF60x |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 155 | wakeup = bfin_read_VR_CTL() & ~FREQ; |
| 156 | wakeup |= SCKELOW; |
| 157 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 158 | #ifdef CONFIG_PM_BFIN_WAKE_PH6 |
| 159 | wakeup |= PHYWE; |
| 160 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 161 | #ifdef CONFIG_PM_BFIN_WAKE_GP |
| 162 | wakeup |= GPWE; |
| 163 | #endif |
Steven Miao | 0fbd88c | 2012-05-17 17:29:54 +0800 | [diff] [blame] | 164 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 165 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 166 | ret = blackfin_dma_suspend(); |
| 167 | |
| 168 | if (ret) { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 169 | kfree(memptr); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | bfin_gpio_pm_hibernate_suspend(); |
| 174 | |
Steven Miao | ba4691a | 2012-06-15 11:40:48 +0800 | [diff] [blame] | 175 | #if BFIN_GPIO_PINT |
Steven Miao | d49cdf840 | 2012-06-14 18:04:01 +0800 | [diff] [blame] | 176 | bfin_pint_suspend(); |
Steven Miao | ba4691a | 2012-06-15 11:40:48 +0800 | [diff] [blame] | 177 | #endif |
Steven Miao | d49cdf840 | 2012-06-14 18:04:01 +0800 | [diff] [blame] | 178 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 179 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
| 180 | flushinv_all_dcache(); |
| 181 | #endif |
| 182 | _disable_dcplb(); |
| 183 | _disable_icplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 184 | bf53x_suspend_l1_mem(memptr); |
| 185 | |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 186 | #ifndef CONFIG_BF60x |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 187 | do_hibernate(wakeup | vr_wakeup); /* See you later! */ |
Steven Miao | 93f8951 | 2012-05-16 18:26:10 +0800 | [diff] [blame] | 188 | #else |
| 189 | bfin_cpu_pm->enter(PM_SUSPEND_MEM); |
| 190 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 191 | |
| 192 | bf53x_resume_l1_mem(memptr); |
| 193 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 194 | _enable_icplb(); |
| 195 | _enable_dcplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 196 | |
Steven Miao | ba4691a | 2012-06-15 11:40:48 +0800 | [diff] [blame] | 197 | #if BFIN_GPIO_PINT |
Steven Miao | d49cdf840 | 2012-06-14 18:04:01 +0800 | [diff] [blame] | 198 | bfin_pint_resume(); |
Steven Miao | ba4691a | 2012-06-15 11:40:48 +0800 | [diff] [blame] | 199 | #endif |
Steven Miao | d49cdf840 | 2012-06-14 18:04:01 +0800 | [diff] [blame] | 200 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 201 | bfin_gpio_pm_hibernate_restore(); |
| 202 | blackfin_dma_resume(); |
| 203 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 204 | kfree(memptr); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 209 | /* |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 210 | * bfin_pm_valid - Tell the PM core that we only support the standby sleep |
| 211 | * state |
| 212 | * @state: suspend state we're checking. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 213 | * |
| 214 | */ |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 215 | static int bfin_pm_valid(suspend_state_t state) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 216 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 217 | return (state == PM_SUSPEND_STANDBY |
Michael Hennerich | b89df50 | 2009-03-28 23:14:41 +0800 | [diff] [blame] | 218 | #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561)) |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 219 | /* |
| 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 Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * bfin_pm_enter - Actually enter a sleep state. |
| 239 | * @state: State we're entering. |
| 240 | * |
| 241 | */ |
| 242 | static 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 Wu | 9d7b667 | 2007-05-21 18:09:37 +0800 | [diff] [blame] | 248 | case PM_SUSPEND_MEM: |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 249 | bfin_pm_suspend_mem_enter(); |
| 250 | break; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 251 | default: |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Sonic Zhang | 72b099e | 2012-05-17 17:33:00 +0800 | [diff] [blame] | 258 | #ifdef CONFIG_BFIN_PM_WAKEUP_TIME_BENCH |
| 259 | void bfin_pm_end(void) |
| 260 | { |
| 261 | u32 cycle, cycle2; |
| 262 | u64 usec64; |
| 263 | u32 usec; |
| 264 | |
| 265 | __asm__ __volatile__ ( |
| 266 | "1: %0 = CYCLES2\n" |
| 267 | "%1 = CYCLES\n" |
| 268 | "%2 = CYCLES2\n" |
| 269 | "CC = %2 == %0\n" |
| 270 | "if ! CC jump 1b\n" |
| 271 | : "=d,a" (cycle2), "=d,a" (cycle), "=d,a" (usec) : : "CC" |
| 272 | ); |
| 273 | |
| 274 | usec64 = ((u64)cycle2 << 32) + cycle; |
| 275 | do_div(usec64, get_cclk() / USEC_PER_SEC); |
| 276 | usec = usec64; |
| 277 | if (usec == 0) |
| 278 | usec = 1; |
| 279 | |
| 280 | pr_info("PM: resume of kernel completes after %ld msec %03ld usec\n", |
| 281 | usec / USEC_PER_MSEC, usec % USEC_PER_MSEC); |
| 282 | } |
| 283 | #endif |
| 284 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 285 | static const struct platform_suspend_ops bfin_pm_ops = { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 286 | .enter = bfin_pm_enter, |
Michael Hennerich | 4bbd10f | 2007-08-27 17:29:10 +0800 | [diff] [blame] | 287 | .valid = bfin_pm_valid, |
Sonic Zhang | 72b099e | 2012-05-17 17:33:00 +0800 | [diff] [blame] | 288 | #ifdef CONFIG_BFIN_PM_WAKEUP_TIME_BENCH |
| 289 | .end = bfin_pm_end, |
| 290 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | static int __init bfin_pm_init(void) |
| 294 | { |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 295 | suspend_set_ops(&bfin_pm_ops); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | __initcall(bfin_pm_init); |