pm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <linux/delay.h>
  17. #include <asm/cplb.h>
  18. #include <asm/gpio.h>
  19. #include <asm/dma.h>
  20. #include <asm/dpmc.h>
  21. #include <asm/pm.h>
  22. #ifdef CONFIG_BF60x
  23. struct bfin_cpu_pm_fns *bfin_cpu_pm;
  24. #endif
  25. void bfin_pm_suspend_standby_enter(void)
  26. {
  27. #if !BFIN_GPIO_PINT
  28. bfin_pm_standby_setup();
  29. #endif
  30. #ifdef CONFIG_BF60x
  31. bfin_cpu_pm->enter(PM_SUSPEND_STANDBY);
  32. #else
  33. # ifdef CONFIG_PM_BFIN_SLEEP_DEEPER
  34. sleep_deeper(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  35. # else
  36. sleep_mode(bfin_sic_iwr[0], bfin_sic_iwr[1], bfin_sic_iwr[2]);
  37. # endif
  38. #endif
  39. #if !BFIN_GPIO_PINT
  40. bfin_pm_standby_restore();
  41. #endif
  42. #ifndef CONFIG_BF60x
  43. #ifdef SIC_IWR0
  44. bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
  45. # ifdef SIC_IWR1
  46. /* BF52x system reset does not properly reset SIC_IWR1 which
  47. * will screw up the bootrom as it relies on MDMA0/1 waking it
  48. * up from IDLE instructions. See this report for more info:
  49. * http://blackfin.uclinux.org/gf/tracker/4323
  50. */
  51. if (ANOMALY_05000435)
  52. bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
  53. else
  54. bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
  55. # endif
  56. # ifdef SIC_IWR2
  57. bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
  58. # endif
  59. #else
  60. bfin_write_SIC_IWR(IWR_DISABLE_ALL);
  61. #endif
  62. #endif
  63. }
  64. int bf53x_suspend_l1_mem(unsigned char *memptr)
  65. {
  66. dma_memcpy_nocache(memptr, (const void *) L1_CODE_START,
  67. L1_CODE_LENGTH);
  68. dma_memcpy_nocache(memptr + L1_CODE_LENGTH,
  69. (const void *) L1_DATA_A_START, L1_DATA_A_LENGTH);
  70. dma_memcpy_nocache(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH,
  71. (const void *) L1_DATA_B_START, L1_DATA_B_LENGTH);
  72. memcpy(memptr + L1_CODE_LENGTH + L1_DATA_A_LENGTH +
  73. L1_DATA_B_LENGTH, (const void *) L1_SCRATCH_START,
  74. L1_SCRATCH_LENGTH);
  75. return 0;
  76. }
  77. int bf53x_resume_l1_mem(unsigned char *memptr)
  78. {
  79. dma_memcpy_nocache((void *) L1_CODE_START, memptr, L1_CODE_LENGTH);
  80. dma_memcpy_nocache((void *) L1_DATA_A_START, memptr + L1_CODE_LENGTH,
  81. L1_DATA_A_LENGTH);
  82. dma_memcpy_nocache((void *) L1_DATA_B_START, memptr + L1_CODE_LENGTH +
  83. L1_DATA_A_LENGTH, L1_DATA_B_LENGTH);
  84. memcpy((void *) L1_SCRATCH_START, memptr + L1_CODE_LENGTH +
  85. L1_DATA_A_LENGTH + L1_DATA_B_LENGTH, L1_SCRATCH_LENGTH);
  86. return 0;
  87. }
  88. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  89. # ifdef CONFIG_BF60x
  90. __attribute__((l1_text))
  91. # endif
  92. static void flushinv_all_dcache(void)
  93. {
  94. register u32 way, bank, subbank, set;
  95. register u32 status, addr;
  96. u32 dmem_ctl = bfin_read_DMEM_CONTROL();
  97. for (bank = 0; bank < 2; ++bank) {
  98. if (!(dmem_ctl & (1 << (DMC1_P - bank))))
  99. continue;
  100. for (way = 0; way < 2; ++way)
  101. for (subbank = 0; subbank < 4; ++subbank)
  102. for (set = 0; set < 64; ++set) {
  103. bfin_write_DTEST_COMMAND(
  104. way << 26 |
  105. bank << 23 |
  106. subbank << 16 |
  107. set << 5
  108. );
  109. CSYNC();
  110. status = bfin_read_DTEST_DATA0();
  111. /* only worry about valid/dirty entries */
  112. if ((status & 0x3) != 0x3)
  113. continue;
  114. /* construct the address using the tag */
  115. addr = (status & 0xFFFFC800) | (subbank << 12) | (set << 5);
  116. /* flush it */
  117. __asm__ __volatile__("FLUSHINV[%0];" : : "a"(addr));
  118. }
  119. }
  120. }
  121. #endif
  122. int bfin_pm_suspend_mem_enter(void)
  123. {
  124. int ret;
  125. #ifndef CONFIG_BF60x
  126. int wakeup;
  127. #endif
  128. unsigned char *memptr = kmalloc(L1_CODE_LENGTH + L1_DATA_A_LENGTH
  129. + L1_DATA_B_LENGTH + L1_SCRATCH_LENGTH,
  130. GFP_ATOMIC);
  131. if (memptr == NULL) {
  132. panic("bf53x_suspend_l1_mem malloc failed");
  133. return -ENOMEM;
  134. }
  135. #ifndef CONFIG_BF60x
  136. wakeup = bfin_read_VR_CTL() & ~FREQ;
  137. wakeup |= SCKELOW;
  138. #ifdef CONFIG_PM_BFIN_WAKE_PH6
  139. wakeup |= PHYWE;
  140. #endif
  141. #ifdef CONFIG_PM_BFIN_WAKE_GP
  142. wakeup |= GPWE;
  143. #endif
  144. #endif
  145. ret = blackfin_dma_suspend();
  146. if (ret) {
  147. kfree(memptr);
  148. return ret;
  149. }
  150. #ifdef CONFIG_GPIO_ADI
  151. bfin_gpio_pm_hibernate_suspend();
  152. #endif
  153. #if defined(CONFIG_BFIN_EXTMEM_WRITEBACK) || defined(CONFIG_BFIN_L2_WRITEBACK)
  154. flushinv_all_dcache();
  155. udelay(1);
  156. #endif
  157. _disable_dcplb();
  158. _disable_icplb();
  159. bf53x_suspend_l1_mem(memptr);
  160. #ifndef CONFIG_BF60x
  161. do_hibernate(wakeup | vr_wakeup); /* See you later! */
  162. #else
  163. bfin_cpu_pm->enter(PM_SUSPEND_MEM);
  164. #endif
  165. bf53x_resume_l1_mem(memptr);
  166. _enable_icplb();
  167. _enable_dcplb();
  168. #ifdef CONFIG_GPIO_ADI
  169. bfin_gpio_pm_hibernate_restore();
  170. #endif
  171. blackfin_dma_resume();
  172. kfree(memptr);
  173. return 0;
  174. }
  175. /*
  176. * bfin_pm_valid - Tell the PM core that we only support the standby sleep
  177. * state
  178. * @state: suspend state we're checking.
  179. *
  180. */
  181. static int bfin_pm_valid(suspend_state_t state)
  182. {
  183. return (state == PM_SUSPEND_STANDBY
  184. #if !(defined(BF533_FAMILY) || defined(CONFIG_BF561))
  185. /*
  186. * On BF533/2/1:
  187. * If we enter Hibernate the SCKE Pin is driven Low,
  188. * so that the SDRAM enters Self Refresh Mode.
  189. * However when the reset sequence that follows hibernate
  190. * state is executed, SCKE is driven High, taking the
  191. * SDRAM out of Self Refresh.
  192. *
  193. * If you reconfigure and access the SDRAM "very quickly",
  194. * you are likely to avoid errors, otherwise the SDRAM
  195. * start losing its contents.
  196. * An external HW workaround is possible using logic gates.
  197. */
  198. || state == PM_SUSPEND_MEM
  199. #endif
  200. );
  201. }
  202. /*
  203. * bfin_pm_enter - Actually enter a sleep state.
  204. * @state: State we're entering.
  205. *
  206. */
  207. static int bfin_pm_enter(suspend_state_t state)
  208. {
  209. switch (state) {
  210. case PM_SUSPEND_STANDBY:
  211. bfin_pm_suspend_standby_enter();
  212. break;
  213. case PM_SUSPEND_MEM:
  214. bfin_pm_suspend_mem_enter();
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. #ifdef CONFIG_BFIN_PM_WAKEUP_TIME_BENCH
  222. void bfin_pm_end(void)
  223. {
  224. u32 cycle, cycle2;
  225. u64 usec64;
  226. u32 usec;
  227. __asm__ __volatile__ (
  228. "1: %0 = CYCLES2\n"
  229. "%1 = CYCLES\n"
  230. "%2 = CYCLES2\n"
  231. "CC = %2 == %0\n"
  232. "if ! CC jump 1b\n"
  233. : "=d,a" (cycle2), "=d,a" (cycle), "=d,a" (usec) : : "CC"
  234. );
  235. usec64 = ((u64)cycle2 << 32) + cycle;
  236. do_div(usec64, get_cclk() / USEC_PER_SEC);
  237. usec = usec64;
  238. if (usec == 0)
  239. usec = 1;
  240. pr_info("PM: resume of kernel completes after %ld msec %03ld usec\n",
  241. usec / USEC_PER_MSEC, usec % USEC_PER_MSEC);
  242. }
  243. #endif
  244. static const struct platform_suspend_ops bfin_pm_ops = {
  245. .enter = bfin_pm_enter,
  246. .valid = bfin_pm_valid,
  247. #ifdef CONFIG_BFIN_PM_WAKEUP_TIME_BENCH
  248. .end = bfin_pm_end,
  249. #endif
  250. };
  251. static int __init bfin_pm_init(void)
  252. {
  253. suspend_set_ops(&bfin_pm_ops);
  254. return 0;
  255. }
  256. __initcall(bfin_pm_init);