pm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Blackfin power management
  3. *
  4. * Copyright 2006-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2
  7. * based on arm/mach-omap/pm.c
  8. * Copyright 2001, Cliff Brake <cbrake@accelent.com> and others
  9. */
  10. #include <linux/suspend.h>
  11. #include <linux/sched.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <asm/cplb.h>
  17. #include <asm/gpio.h>
  18. #include <asm/dma.h>
  19. #include <asm/dpmc.h>
  20. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_H
  21. #define WAKEUP_TYPE PM_WAKE_HIGH
  22. #endif
  23. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_L
  24. #define WAKEUP_TYPE PM_WAKE_LOW
  25. #endif
  26. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_F
  27. #define WAKEUP_TYPE PM_WAKE_FALLING
  28. #endif
  29. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_R
  30. #define WAKEUP_TYPE PM_WAKE_RISING
  31. #endif
  32. #ifdef CONFIG_PM_WAKEUP_GPIO_POLAR_EDGE_B
  33. #define WAKEUP_TYPE PM_WAKE_BOTH_EDGES
  34. #endif
  35. void bfin_pm_suspend_standby_enter(void)
  36. {
  37. unsigned long flags;
  38. #ifdef CONFIG_PM_WAKEUP_BY_GPIO
  39. gpio_pm_wakeup_request(CONFIG_PM_WAKEUP_GPIO_NUMBER, WAKEUP_TYPE);
  40. #endif
  41. local_irq_save_hw(flags);
  42. bfin_pm_standby_setup();
  43. #ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
  44. sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  45. #else
  46. sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  47. #endif
  48. bfin_pm_standby_restore();
  49. #ifdef SIC_IWR0
  50. bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
  51. # ifdef SIC_IWR1
  52. /* BF52x system reset does not properly reset SIC_IWR1 which
  53. * will screw up the bootrom as it relies on MDMA0/1 waking it
  54. * up from IDLE instructions. See this report for more info:
  55. * http://blackfin.uclinux.org/gf/tracker/4323
  56. */
  57. if (ANOMALY_05000435)
  58. bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
  59. else
  60. bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
  61. # endif
  62. # ifdef SIC_IWR2
  63. bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
  64. # endif
  65. #else
  66. bfin_write_SIC_IWR(IWR_DISABLE_ALL);
  67. #endif
  68. local_irq_restore_hw(flags);
  69. }
  70. int bf53x_suspend_l1_mem(unsigned char *memptr)
  71. {
  72. dma_memcpy(memptr, (const void *) L1_CODE_START, L1_CODE_LENGTH);
  73. dma_memcpy(memptr + L1_CODE_LENGTH, (const void *) L1_DATA_A_START,
  74. L1_DATA_A_LENGTH);
  75. dma_memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
  76. (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
  77. memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
  78. L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
  79. L1_SCRATCH_LENGTH);
  80. return 0;
  81. }
  82. int bf53x_resume_l1_mem(unsigned char *memptr)
  83. {
  84. dma_memcpy((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
  85. dma_memcpy((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
  86. L1_DATA_A_LENGTH);
  87. dma_memcpy((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
  88. L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
  89. memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
  90. L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
  91. return 0;
  92. }
  93. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  94. static void flushinv_all_dcache(void)
  95. {
  96. u32 way, bank, subbank, set;
  97. u32 status, addr;
  98. u32 dmem_ctl = bfin_read_DMEM_CONTROL();
  99. for (bank = 0; bank < 2; ++bank) {
  100. if (!(dmem_ctl & (1 << (DMC1_P - bank))))
  101. continue;
  102. for (way = 0; way < 2; ++way)
  103. for (subbank = 0; subbank < 4; ++subbank)
  104. for (set = 0; set < 64; ++set) {
  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. /* only worry about valid/dirty entries */
  114. if ((status & 0x3) != 0x3)
  115. continue;
  116. /* construct the address using the tag */
  117. addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
  118. /* flush it */
  119. __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
  120. }
  121. }
  122. }
  123. #endif
  124. int bfin_pm_suspend_mem_enter(void)
  125. {
  126. unsigned long flags;
  127. int wakeup, ret;
  128. unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
  129. + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
  130. GFP_KERNEL);
  131. if (memptr == NULL) {
  132. panic("bf53x_suspend_l1_mem malloc failed");
  133. return -ENOMEM;
  134. }
  135. wakeup = bfin_read_VR_CTL() & ~FREQ;
  136. wakeup |= SCKELOW;
  137. #ifdef CONFIG_PM_BFIN_WAKE_PH6
  138. wakeup |= PHYWE;
  139. #endif
  140. #ifdef CONFIG_PM_BFIN_WAKE_GP
  141. wakeup |= GPWE;
  142. #endif
  143. local_irq_save_hw(flags);
  144. ret = blackfin_dma_suspend();
  145. if (ret) {
  146. local_irq_restore_hw(flags);
  147. kfree(memptr);
  148. return ret;
  149. }
  150. bfin_gpio_pm_hibernate_suspend();
  151. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  152. flushinv_all_dcache();
  153. #endif
  154. _disable_dcplb();
  155. _disable_icplb();
  156. bf53x_suspend_l1_mem(memptr);
  157. do_hibernate(wakeup | vr_wakeup); /* Goodbye */
  158. bf53x_resume_l1_mem(memptr);
  159. _enable_icplb();
  160. _enable_dcplb();
  161. bfin_gpio_pm_hibernate_restore();
  162. blackfin_dma_resume();
  163. local_irq_restore_hw(flags);
  164. kfree(memptr);
  165. return 0;
  166. }
  167. /*
  168. * bfin_pm_valid - Tell the PM core that we only support the standby sleep
  169. * state
  170. * @state: suspend state we're checking.
  171. *
  172. */
  173. static int bfin_pm_valid(suspend_state_t state)
  174. {
  175. return (state == PM_SUSPEND_STANDBY
  176. #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
  177. /*
  178. * On BF533/2/1:
  179. * If we enter Hibernate the SCKE Pin is driven Low,
  180. * so that the SDRAM enters Self Refresh Mode.
  181. * However when the reset sequence that follows hibernate
  182. * state is executed, SCKE is driven High, taking the
  183. * SDRAM out of Self Refresh.
  184. *
  185. * If you reconfigure and access the SDRAM "very quickly",
  186. * you are likely to avoid errors, otherwise the SDRAM
  187. * start losing its contents.
  188. * An external HW workaround is possible using logic gates.
  189. */
  190. || state == PM_SUSPEND_MEM
  191. #endif
  192. );
  193. }
  194. /*
  195. * bfin_pm_enter - Actually enter a sleep state.
  196. * @state: State we're entering.
  197. *
  198. */
  199. static int bfin_pm_enter(suspend_state_t state)
  200. {
  201. switch (state) {
  202. case PM_SUSPEND_STANDBY:
  203. bfin_pm_suspend_standby_enter();
  204. break;
  205. case PM_SUSPEND_MEM:
  206. bfin_pm_suspend_mem_enter();
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. struct platform_suspend_ops bfin_pm_ops = {
  214. .enter = bfin_pm_enter,
  215. .valid = bfin_pm_valid,
  216. };
  217. static int __init bfin_pm_init(void)
  218. {
  219. suspend_set_ops(&bfin_pm_ops);
  220. return 0;
  221. }
  222. __initcall(bfin_pm_init);