pm.c 8.0 KB

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