pm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * File: arch/blackfin/mach-common/pm.c
  3. * Based on: arm/mach-omap/pm.c
  4. * Author: Cliff Brake <cbrake@accelent.com> Copyright (c) 2001
  5. *
  6. * Created: 2001
  7. * Description: Blackfin power management
  8. *
  9. * Modified: Nicolas Pitre - PXA250 support
  10. * Copyright (c) 2002 Monta Vista Software, Inc.
  11. * David Singleton - OMAP1510
  12. * Copyright (c) 2002 Monta Vista Software, Inc.
  13. * Dirk Behme <dirk.behme@de.bosch.com> - OMAP1510/1610
  14. * Copyright 2004
  15. * Copyright 2004-2008 Analog Devices Inc.
  16. *
  17. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, see the file COPYING, or write
  31. * to the Free Software Foundation, Inc.,
  32. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  33. */
  34. #include <linux/suspend.h>
  35. #include <linux/sched.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/io.h>
  38. #include <linux/irq.h>
  39. #include <asm/cplb.h>
  40. #include <asm/gpio.h>
  41. #include <asm/dma.h>
  42. #include <asm/dpmc.h>
  43. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_H
  44. #define WAKEUP_TYPE PM_WAKE_HIGH
  45. #endif
  46. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_L
  47. #define WAKEUP_TYPE PM_WAKE_LOW
  48. #endif
  49. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_F
  50. #define WAKEUP_TYPE PM_WAKE_FALLING
  51. #endif
  52. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_R
  53. #define WAKEUP_TYPE PM_WAKE_RISING
  54. #endif
  55. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_B
  56. #define WAKEUP_TYPE PM_WAKE_BOTH_EDGES
  57. #endif
  58. void bfin_pm_suspend_standby_enter(void)
  59. {
  60. unsigned long flags;
  61. #ifdef CONFIG_PM_WAKEUP_BY_GPIO
  62. gpio_pm_wakeup_request(CONFIG_PM_WAKEUP_GPIO_NUMBER, WAKEUP_TYPE);
  63. #endif
  64. local_irq_save_hw(flags);
  65. bfin_pm_standby_setup();
  66. #ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
  67. sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  68. #else
  69. sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  70. #endif
  71. bfin_pm_standby_restore();
  72. #ifdef SIC_IWR0
  73. bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
  74. # ifdef SIC_IWR1
  75. /* BF52x system reset does not properly reset SIC_IWR1 which
  76. * will screw up the bootrom as it relies on MDMA0/1 waking it
  77. * up from IDLE instructions. See this report for more info:
  78. * http://blackfin.uclinux.org/gf/tracker/4323
  79. */
  80. if (ANOMALY_05000435)
  81. bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
  82. else
  83. bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
  84. # endif
  85. # ifdef SIC_IWR2
  86. bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
  87. # endif
  88. #else
  89. bfin_write_SIC_IWR(IWR_DISABLE_ALL);
  90. #endif
  91. local_irq_restore_hw(flags);
  92. }
  93. int bf53x_suspend_l1_mem(unsigned char *memptr)
  94. {
  95. dma_memcpy(memptr, (const void *) L1_CODE_START, L1_CODE_LENGTH);
  96. dma_memcpy(memptr + L1_CODE_LENGTH, (const void *) L1_DATA_A_START,
  97. L1_DATA_A_LENGTH);
  98. dma_memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
  99. (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
  100. memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
  101. L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
  102. L1_SCRATCH_LENGTH);
  103. return 0;
  104. }
  105. int bf53x_resume_l1_mem(unsigned char *memptr)
  106. {
  107. dma_memcpy((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
  108. dma_memcpy((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
  109. L1_DATA_A_LENGTH);
  110. dma_memcpy((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
  111. L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
  112. memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
  113. L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
  114. return 0;
  115. }
  116. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  117. static void flushinv_all_dcache(void)
  118. {
  119. u32 way, bank, subbank, set;
  120. u32 status, addr;
  121. u32 dmem_ctl = bfin_read_DMEM_CONTROL();
  122. for (bank = 0; bank < 2; ++bank) {
  123. if (!(dmem_ctl & (1 << (DMC1_P - bank))))
  124. continue;
  125. for (way = 0; way < 2; ++way)
  126. for (subbank = 0; subbank < 4; ++subbank)
  127. for (set = 0; set < 64; ++set) {
  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. /* only worry about valid/dirty entries */
  137. if ((status & 0x3) != 0x3)
  138. continue;
  139. /* construct the address using the tag */
  140. addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
  141. /* flush it */
  142. __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
  143. }
  144. }
  145. }
  146. #endif
  147. int bfin_pm_suspend_mem_enter(void)
  148. {
  149. unsigned long flags;
  150. int wakeup, ret;
  151. unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
  152. + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
  153. GFP_KERNEL);
  154. if (memptr == NULL) {
  155. panic("bf53x_suspend_l1_mem malloc failed");
  156. return -ENOMEM;
  157. }
  158. wakeup = bfin_read_VR_CTL() & ~FREQ;
  159. wakeup |= SCKELOW;
  160. #ifdef CONFIG_PM_BFIN_WAKE_PH6
  161. wakeup |= PHYWE;
  162. #endif
  163. #ifdef CONFIG_PM_BFIN_WAKE_GP
  164. wakeup |= GPWE;
  165. #endif
  166. local_irq_save_hw(flags);
  167. ret = blackfin_dma_suspend();
  168. if (ret) {
  169. local_irq_restore_hw(flags);
  170. kfree(memptr);
  171. return ret;
  172. }
  173. bfin_gpio_pm_hibernate_suspend();
  174. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  175. flushinv_all_dcache();
  176. #endif
  177. _disable_dcplb();
  178. _disable_icplb();
  179. bf53x_suspend_l1_mem(memptr);
  180. do_hibernate(wakeup | vr_wakeup); /* Goodbye */
  181. bf53x_resume_l1_mem(memptr);
  182. _enable_icplb();
  183. _enable_dcplb();
  184. bfin_gpio_pm_hibernate_restore();
  185. blackfin_dma_resume();
  186. local_irq_restore_hw(flags);
  187. kfree(memptr);
  188. return 0;
  189. }
  190. /*
  191. * bfin_pm_valid - Tell the PM core that we only support the standby sleep
  192. * state
  193. * @state: suspend state we're checking.
  194. *
  195. */
  196. static int bfin_pm_valid(suspend_state_t state)
  197. {
  198. return (state == PM_SUSPEND_STANDBY
  199. #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
  200. /*
  201. * On BF533/2/1:
  202. * If we enter Hibernate the SCKE Pin is driven Low,
  203. * so that the SDRAM enters Self Refresh Mode.
  204. * However when the reset sequence that follows hibernate
  205. * state is executed, SCKE is driven High, taking the
  206. * SDRAM out of Self Refresh.
  207. *
  208. * If you reconfigure and access the SDRAM "very quickly",
  209. * you are likely to avoid errors, otherwise the SDRAM
  210. * start losing its contents.
  211. * An external HW workaround is possible using logic gates.
  212. */
  213. || state == PM_SUSPEND_MEM
  214. #endif
  215. );
  216. }
  217. /*
  218. * bfin_pm_enter - Actually enter a sleep state.
  219. * @state: State we're entering.
  220. *
  221. */
  222. static int bfin_pm_enter(suspend_state_t state)
  223. {
  224. switch (state) {
  225. case PM_SUSPEND_STANDBY:
  226. bfin_pm_suspend_standby_enter();
  227. break;
  228. case PM_SUSPEND_MEM:
  229. bfin_pm_suspend_mem_enter();
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. return 0;
  235. }
  236. struct platform_suspend_ops bfin_pm_ops = {
  237. .enter = bfin_pm_enter,
  238. .valid = bfin_pm_valid,
  239. };
  240. static int __init bfin_pm_init(void)
  241. {
  242. suspend_set_ops(&bfin_pm_ops);
  243. return 0;
  244. }
  245. __initcall(bfin_pm_init);