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 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 23 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 24 | void bfin_pm_suspend_standby_enter(void) |
| 25 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 26 | unsigned long flags; |
| 27 | |
David Howells | 3b139cd | 2010-10-07 14:08:52 +0100 | [diff] [blame] | 28 | flags = hard_local_irq_save(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 29 | bfin_pm_standby_setup(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 30 | |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 31 | #ifdef CONFIG_PM_BFIN_SLEEP_DEEPER |
| 32 | 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] | 33 | #else |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 34 | 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] | 35 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 36 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 37 | bfin_pm_standby_restore(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 38 | |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 39 | #ifdef SIC_IWR0 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 40 | bfin_write_SIC_IWR0(IWR_DISABLE_ALL); |
Mike Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 41 | # ifdef SIC_IWR1 |
Michael Hennerich | 55546ac | 2008-08-13 17:41:13 +0800 | [diff] [blame] | 42 | /* 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 Frysinger | b7e1129 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 47 | 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 Frysinger | be1d854 | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 51 | # endif |
| 52 | # ifdef SIC_IWR2 |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 53 | bfin_write_SIC_IWR2(IWR_DISABLE_ALL); |
Sonic Zhang | fb5f004 | 2007-12-23 23:02:13 +0800 | [diff] [blame] | 54 | # endif |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 55 | #else |
Michael Hennerich | 56f5f59 | 2008-08-06 17:55:32 +0800 | [diff] [blame] | 56 | bfin_write_SIC_IWR(IWR_DISABLE_ALL); |
Michael Hennerich | cfefe3c | 2008-02-09 04:12:37 +0800 | [diff] [blame] | 57 | #endif |
| 58 | |
David Howells | 3b139cd | 2010-10-07 14:08:52 +0100 | [diff] [blame] | 59 | hard_local_irq_restore(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 62 | int bf53x_suspend_l1_mem(unsigned char *memptr) |
| 63 | { |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 64 | 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 69 | (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 | |
| 77 | int bf53x_resume_l1_mem(unsigned char *memptr) |
| 78 | { |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 79 | 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 81 | L1_DATA_A_LENGTH); |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 82 | dma_memcpy_nocache((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH + |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 83 | 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 Zhang | 41ba653 | 2009-06-16 09:48:33 +0000 | [diff] [blame] | 90 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 91 | static 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 128 | int 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 Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 145 | #ifdef CONFIG_PM_BFIN_WAKE_PH6 |
| 146 | wakeup |= PHYWE; |
| 147 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 148 | #ifdef CONFIG_PM_BFIN_WAKE_GP |
| 149 | wakeup |= GPWE; |
| 150 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 151 | |
David Howells | 3b139cd | 2010-10-07 14:08:52 +0100 | [diff] [blame] | 152 | flags = hard_local_irq_save(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 153 | |
| 154 | ret = blackfin_dma_suspend(); |
| 155 | |
| 156 | if (ret) { |
David Howells | 3b139cd | 2010-10-07 14:08:52 +0100 | [diff] [blame] | 157 | hard_local_irq_restore(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 158 | kfree(memptr); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | bfin_gpio_pm_hibernate_suspend(); |
| 163 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 164 | #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK) |
| 165 | flushinv_all_dcache(); |
| 166 | #endif |
| 167 | _disable_dcplb(); |
| 168 | _disable_icplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 169 | bf53x_suspend_l1_mem(memptr); |
| 170 | |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 171 | do_hibernate(wakeup | vr_wakeup); /* See you later! */ |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 172 | |
| 173 | bf53x_resume_l1_mem(memptr); |
| 174 | |
Yi Li | eb7bd9c | 2009-08-07 01:20:58 +0000 | [diff] [blame] | 175 | _enable_icplb(); |
| 176 | _enable_dcplb(); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 177 | |
| 178 | bfin_gpio_pm_hibernate_restore(); |
| 179 | blackfin_dma_resume(); |
| 180 | |
David Howells | 3b139cd | 2010-10-07 14:08:52 +0100 | [diff] [blame] | 181 | hard_local_irq_restore(flags); |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 182 | kfree(memptr); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 187 | /* |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 188 | * bfin_pm_valid - Tell the PM core that we only support the standby sleep |
| 189 | * state |
| 190 | * @state: suspend state we're checking. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 191 | * |
| 192 | */ |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 193 | static int bfin_pm_valid(suspend_state_t state) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 194 | { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 195 | return (state == PM_SUSPEND_STANDBY |
Michael Hennerich | b89df50 | 2009-03-28 23:14:41 +0800 | [diff] [blame] | 196 | #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561)) |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 197 | /* |
| 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 Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * bfin_pm_enter - Actually enter a sleep state. |
| 217 | * @state: State we're entering. |
| 218 | * |
| 219 | */ |
| 220 | static 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 Wu | 9d7b667 | 2007-05-21 18:09:37 +0800 | [diff] [blame] | 226 | case PM_SUSPEND_MEM: |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 227 | bfin_pm_suspend_mem_enter(); |
| 228 | break; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 229 | default: |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame^] | 236 | static const struct platform_suspend_ops bfin_pm_ops = { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 237 | .enter = bfin_pm_enter, |
Michael Hennerich | 4bbd10f | 2007-08-27 17:29:10 +0800 | [diff] [blame] | 238 | .valid = bfin_pm_valid, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | static int __init bfin_pm_init(void) |
| 242 | { |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 243 | suspend_set_ops(&bfin_pm_ops); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | __initcall(bfin_pm_init); |