pm.c 7.8 KB

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