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> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_H |
| 24 | #define WAKEUP_TYPE PM_WAKE_HIGH |
| 25 | #endif |
| 26 | |
| 27 | #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_L |
| 28 | #define WAKEUP_TYPE PM_WAKE_LOW |
| 29 | #endif |
| 30 | |
| 31 | #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_F |
| 32 | #define WAKEUP_TYPE PM_WAKE_FALLING |
| 33 | #endif |
| 34 | |
| 35 | #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_R |
| 36 | #define WAKEUP_TYPE PM_WAKE_RISING |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_B |
| 40 | #define WAKEUP_TYPE PM_WAKE_BOTH_EDGES |
| 41 | #endif |
| 42 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 43 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 44 | void bfin_pm_suspend_standby_enter(void) |
| 45 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 46 | unsigned long flags; |
| 47 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_PM_WAKEUP_BY_GPIO |
| 49 | gpio_pm_wakeup_request(CONFIG_PM_WAKEUP_GPIO_NUMBER, WAKEUP_TYPE); |
| 50 | #endif |
| 51 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 52 | local_irq_save_hw(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 53 | bfin_pm_standby_setup(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 54 | |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 55 | #ifdef CONFIG_PM_BFIN_SLEEP_DEEPER |
| 56 | sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 57 | #else |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 58 | sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 59 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 60 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 61 | bfin_pm_standby_restore(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 62 | |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 63 | #ifdef SIC_IWR0 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 64 | bfin_write_SIC_IWR0(IWR_DISABLE_ALL); |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 65 | # ifdef SIC_IWR1 |
Michael Hennerich | 55546ac | 2008-08-13 17:41:13 +0800 | [diff] [blame] | 66 | /* BF52x system reset does not properly reset SIC_IWR1 which |
| 67 | * will screw up the bootrom as it relies on MDMA0/1 waking it |
| 68 | * up from IDLE instructions. See this report for more info: |
| 69 | * http://blackfin.uclinux.org/gf/tracker/4323 |
| 70 | */ |
Mike Frysinger | b7e1129 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 71 | if (ANOMALY_05000435) |
| 72 | bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11)); |
| 73 | else |
| 74 | bfin_write_SIC_IWR1(IWR_DISABLE_ALL); |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 75 | # endif |
| 76 | # ifdef SIC_IWR2 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 77 | bfin_write_SIC_IWR2(IWR_DISABLE_ALL); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 78 | # endif |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 79 | #else |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 80 | bfin_write_SIC_IWR(IWR_DISABLE_ALL); |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 81 | #endif |
| 82 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 83 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 86 | int bf53x_suspend_l1_mem(unsigned char *memptr) |
| 87 | { |
| 88 | dma_memcpy(memptr, (const void *) L1_CODE_START, L1_CODE_LENGTH); |
| 89 | dma_memcpy(memptr + L1_CODE_LENGTH, (const void *) L1_DATA_A_START, |
| 90 | L1_DATA_A_LENGTH); |
| 91 | dma_memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH, |
| 92 | (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH); |
| 93 | memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH + |
| 94 | L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START, |
| 95 | L1_SCRATCH_LENGTH); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int bf53x_resume_l1_mem(unsigned char *memptr) |
| 101 | { |
| 102 | dma_memcpy((void *) L1_CODE_START, memptr, L1_CODE_LENGTH); |
| 103 | dma_memcpy((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH, |
| 104 | L1_DATA_A_LENGTH); |
| 105 | dma_memcpy((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH + |
| 106 | L1_DATA_A_LENGTH, L1_DATA_B_LENGTH); |
| 107 | memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH + |
| 108 | L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Jie Zhang | 41ba653 | 2009-06-16 09:48:33 +0000 | [diff] [blame] | 113 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 114 | static void flushinv_all_dcache(void) |
| 115 | { |
| 116 | u32 way, bank, subbank, set; |
| 117 | u32 status, addr; |
| 118 | u32 dmem_ctl = bfin_read_DMEM_CONTROL(); |
| 119 | |
| 120 | for (bank = 0; bank < 2; ++bank) { |
| 121 | if (!(dmem_ctl & (1 << (DMC1_P - bank)))) |
| 122 | continue; |
| 123 | |
| 124 | for (way = 0; way < 2; ++way) |
| 125 | for (subbank = 0; subbank < 4; ++subbank) |
| 126 | for (set = 0; set < 64; ++set) { |
| 127 | |
| 128 | bfin_write_DTEST_COMMAND( |
| 129 | way << 26 | |
| 130 | bank << 23 | |
| 131 | subbank << 16 | |
| 132 | set << 5 |
| 133 | ); |
| 134 | CSYNC(); |
| 135 | status = bfin_read_DTEST_DATA0(); |
| 136 | |
| 137 | /* only worry about valid/dirty entries */ |
| 138 | if ((status & 0x3) != 0x3) |
| 139 | continue; |
| 140 | |
| 141 | /* construct the address using the tag */ |
| 142 | addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5); |
| 143 | |
| 144 | /* flush it */ |
| 145 | __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr)); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 151 | int bfin_pm_suspend_mem_enter(void) |
| 152 | { |
| 153 | unsigned long flags; |
| 154 | int wakeup, ret; |
| 155 | |
| 156 | unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH |
| 157 | + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH, |
| 158 | GFP_KERNEL); |
| 159 | |
| 160 | if (memptr == NULL) { |
| 161 | panic("bf53x_suspend_l1_mem malloc failed"); |
| 162 | return -ENOMEM; |
| 163 | } |
| 164 | |
| 165 | wakeup = bfin_read_VR_CTL() & ~FREQ; |
| 166 | wakeup |= SCKELOW; |
| 167 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 168 | #ifdef CONFIG_PM_BFIN_WAKE_PH6 |
| 169 | wakeup |= PHYWE; |
| 170 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 171 | #ifdef CONFIG_PM_BFIN_WAKE_GP |
| 172 | wakeup |= GPWE; |
| 173 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 174 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 175 | local_irq_save_hw(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 176 | |
| 177 | ret = blackfin_dma_suspend(); |
| 178 | |
| 179 | if (ret) { |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 180 | local_irq_restore_hw(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 181 | kfree(memptr); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | bfin_gpio_pm_hibernate_suspend(); |
| 186 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 187 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
| 188 | flushinv_all_dcache(); |
| 189 | #endif |
| 190 | _disable_dcplb(); |
| 191 | _disable_icplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 192 | bf53x_suspend_l1_mem(memptr); |
| 193 | |
Michael Hennerich | 4a88d0c | 2008-08-05 17:38:41 +0800 | [diff] [blame] | 194 | do_hibernate(wakeup | vr_wakeup); /* Goodbye */ |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 195 | |
| 196 | bf53x_resume_l1_mem(memptr); |
| 197 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 198 | _enable_icplb(); |
| 199 | _enable_dcplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 200 | |
| 201 | bfin_gpio_pm_hibernate_restore(); |
| 202 | blackfin_dma_resume(); |
| 203 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 204 | local_irq_restore_hw(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 205 | kfree(memptr); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 210 | /* |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 211 | * bfin_pm_valid - Tell the PM core that we only support the standby sleep |
| 212 | * state |
| 213 | * @state: suspend state we're checking. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 214 | * |
| 215 | */ |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 216 | static int bfin_pm_valid(suspend_state_t state) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 217 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 218 | return (state == PM_SUSPEND_STANDBY |
Michael Hennerich | b89df50 | 2009-03-28 23:14:41 +0800 | [diff] [blame] | 219 | #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561)) |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 220 | /* |
| 221 | * On BF533/2/1: |
| 222 | * If we enter Hibernate the SCKE Pin is driven Low, |
| 223 | * so that the SDRAM enters Self Refresh Mode. |
| 224 | * However when the reset sequence that follows hibernate |
| 225 | * state is executed, SCKE is driven High, taking the |
| 226 | * SDRAM out of Self Refresh. |
| 227 | * |
| 228 | * If you reconfigure and access the SDRAM "very quickly", |
| 229 | * you are likely to avoid errors, otherwise the SDRAM |
| 230 | * start losing its contents. |
| 231 | * An external HW workaround is possible using logic gates. |
| 232 | */ |
| 233 | || state == PM_SUSPEND_MEM |
| 234 | #endif |
| 235 | ); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * bfin_pm_enter - Actually enter a sleep state. |
| 240 | * @state: State we're entering. |
| 241 | * |
| 242 | */ |
| 243 | static int bfin_pm_enter(suspend_state_t state) |
| 244 | { |
| 245 | switch (state) { |
| 246 | case PM_SUSPEND_STANDBY: |
| 247 | bfin_pm_suspend_standby_enter(); |
| 248 | break; |
Bryan Wu | 9d7b667 | 2007-05-21 18:09:37 +0800 | [diff] [blame] | 249 | case PM_SUSPEND_MEM: |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 250 | bfin_pm_suspend_mem_enter(); |
| 251 | break; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 252 | default: |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 259 | struct platform_suspend_ops bfin_pm_ops = { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 260 | .enter = bfin_pm_enter, |
Michael Hennerich | 4bbd10f | 2007-08-27 17:29:10 +0800 | [diff] [blame] | 261 | .valid = bfin_pm_valid, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | static int __init bfin_pm_init(void) |
| 265 | { |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 266 | suspend_set_ops(&bfin_pm_ops); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | __initcall(bfin_pm_init); |