pm.c 7.8 KB

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