pm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Blackfin bf609 power management
  3. *
  4. * Copyright 2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2
  7. */
  8. #include <linux/suspend.h>
  9. #include <linux/io.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/gpio.h>
  12. #include <linux/irq.h>
  13. #include <linux/delay.h>
  14. #include <linux/syscore_ops.h>
  15. #include <asm/dpmc.h>
  16. #include <asm/pm.h>
  17. #include <mach/pm.h>
  18. #include <asm/blackfin.h>
  19. #include <asm/mem_init.h>
  20. /***********************************************************/
  21. /* */
  22. /* Wakeup Actions for DPM_RESTORE */
  23. /* */
  24. /***********************************************************/
  25. #define BITP_ROM_WUA_CHKHDR 24
  26. #define BITP_ROM_WUA_DDRLOCK 7
  27. #define BITP_ROM_WUA_DDRDLLEN 6
  28. #define BITP_ROM_WUA_DDR 5
  29. #define BITP_ROM_WUA_CGU 4
  30. #define BITP_ROM_WUA_MEMBOOT 2
  31. #define BITP_ROM_WUA_EN 1
  32. #define BITM_ROM_WUA_CHKHDR (0xFF000000)
  33. #define ENUM_ROM_WUA_CHKHDR_AD 0xAD000000
  34. #define BITM_ROM_WUA_DDRLOCK (0x00000080)
  35. #define BITM_ROM_WUA_DDRDLLEN (0x00000040)
  36. #define BITM_ROM_WUA_DDR (0x00000020)
  37. #define BITM_ROM_WUA_CGU (0x00000010)
  38. #define BITM_ROM_WUA_MEMBOOT (0x00000002)
  39. #define BITM_ROM_WUA_EN (0x00000001)
  40. /***********************************************************/
  41. /* */
  42. /* Syscontrol */
  43. /* */
  44. /***********************************************************/
  45. #define BITP_ROM_SYSCTRL_CGU_LOCKINGEN 28 /* unlocks CGU_CTL register */
  46. #define BITP_ROM_SYSCTRL_WUA_OVERRIDE 24
  47. #define BITP_ROM_SYSCTRL_WUA_DDRDLLEN 20 /* Saves the DDR DLL and PADS registers to the DPM registers */
  48. #define BITP_ROM_SYSCTRL_WUA_DDR 19 /* Saves the DDR registers to the DPM registers */
  49. #define BITP_ROM_SYSCTRL_WUA_CGU 18 /* Saves the CGU registers into DPM registers */
  50. #define BITP_ROM_SYSCTRL_WUA_DPMWRITE 17 /* Saves the Syscontrol structure structure contents into DPM registers */
  51. #define BITP_ROM_SYSCTRL_WUA_EN 16 /* reads current PLL and DDR configuration into structure */
  52. #define BITP_ROM_SYSCTRL_DDR_WRITE 13 /* writes the DDR registers from Syscontrol structure for wakeup initialization of DDR */
  53. #define BITP_ROM_SYSCTRL_DDR_READ 12 /* Read the DDR registers into the Syscontrol structure for storing prior to hibernate */
  54. #define BITP_ROM_SYSCTRL_CGU_AUTODIS 11 /* Disables auto handling of UPDT and ALGN fields */
  55. #define BITP_ROM_SYSCTRL_CGU_CLKOUTSEL 7 /* access CGU_CLKOUTSEL register */
  56. #define BITP_ROM_SYSCTRL_CGU_DIV 6 /* access CGU_DIV register */
  57. #define BITP_ROM_SYSCTRL_CGU_STAT 5 /* access CGU_STAT register */
  58. #define BITP_ROM_SYSCTRL_CGU_CTL 4 /* access CGU_CTL register */
  59. #define BITP_ROM_SYSCTRL_CGU_RTNSTAT 2 /* Update structure STAT field upon error */
  60. #define BITP_ROM_SYSCTRL_WRITE 1 /* write registers */
  61. #define BITP_ROM_SYSCTRL_READ 0 /* read registers */
  62. #define BITM_ROM_SYSCTRL_CGU_READ (0x00000001) /* Read CGU registers */
  63. #define BITM_ROM_SYSCTRL_CGU_WRITE (0x00000002) /* Write registers */
  64. #define BITM_ROM_SYSCTRL_CGU_RTNSTAT (0x00000004) /* Update structure STAT field upon error or after a write operation */
  65. #define BITM_ROM_SYSCTRL_CGU_CTL (0x00000010) /* Access CGU_CTL register */
  66. #define BITM_ROM_SYSCTRL_CGU_STAT (0x00000020) /* Access CGU_STAT register */
  67. #define BITM_ROM_SYSCTRL_CGU_DIV (0x00000040) /* Access CGU_DIV register */
  68. #define BITM_ROM_SYSCTRL_CGU_CLKOUTSEL (0x00000080) /* Access CGU_CLKOUTSEL register */
  69. #define BITM_ROM_SYSCTRL_CGU_AUTODIS (0x00000800) /* Disables auto handling of UPDT and ALGN fields */
  70. #define BITM_ROM_SYSCTRL_DDR_READ (0x00001000) /* Reads the contents of the DDR registers and stores them into the structure */
  71. #define BITM_ROM_SYSCTRL_DDR_WRITE (0x00002000) /* Writes the DDR registers from the structure, only really intented for wakeup functionality and not for full DDR configuration */
  72. #define BITM_ROM_SYSCTRL_WUA_EN (0x00010000) /* Wakeup entry or exit opertation enable */
  73. #define BITM_ROM_SYSCTRL_WUA_DPMWRITE (0x00020000) /* When set indicates a restore of the PLL and DDR is to be performed otherwise a save is required */
  74. #define BITM_ROM_SYSCTRL_WUA_CGU (0x00040000) /* Only applicable for a PLL and DDR save operation to the DPM, saves the current settings if cleared or the contents of the structure if set */
  75. #define BITM_ROM_SYSCTRL_WUA_DDR (0x00080000) /* Only applicable for a PLL and DDR save operation to the DPM, saves the current settings if cleared or the contents of the structure if set */
  76. #define BITM_ROM_SYSCTRL_WUA_DDRDLLEN (0x00100000) /* Enables saving/restoring of the DDR DLLCTL register */
  77. #define BITM_ROM_SYSCTRL_WUA_OVERRIDE (0x01000000)
  78. #define BITM_ROM_SYSCTRL_CGU_LOCKINGEN (0x10000000) /* Unlocks the CGU_CTL register */
  79. /* Structures for the syscontrol() function */
  80. struct STRUCT_ROM_SYSCTRL {
  81. uint32_t ulCGU_CTL;
  82. uint32_t ulCGU_STAT;
  83. uint32_t ulCGU_DIV;
  84. uint32_t ulCGU_CLKOUTSEL;
  85. uint32_t ulWUA_Flags;
  86. uint32_t ulWUA_BootAddr;
  87. uint32_t ulWUA_User;
  88. uint32_t ulDDR_CTL;
  89. uint32_t ulDDR_CFG;
  90. uint32_t ulDDR_TR0;
  91. uint32_t ulDDR_TR1;
  92. uint32_t ulDDR_TR2;
  93. uint32_t ulDDR_MR;
  94. uint32_t ulDDR_EMR1;
  95. uint32_t ulDDR_EMR2;
  96. uint32_t ulDDR_PADCTL;
  97. uint32_t ulDDR_DLLCTL;
  98. uint32_t ulReserved;
  99. };
  100. struct bfin_pm_data {
  101. uint32_t magic;
  102. uint32_t resume_addr;
  103. uint32_t sp;
  104. };
  105. struct bfin_pm_data bf609_pm_data;
  106. struct STRUCT_ROM_SYSCTRL configvalues;
  107. uint32_t dactionflags;
  108. #define FUNC_ROM_SYSCONTROL 0xC8000080
  109. __attribute__((l1_data))
  110. static uint32_t (* const bfrom_SysControl)(uint32_t action_flags, struct STRUCT_ROM_SYSCTRL *settings, void *reserved) = (void *)FUNC_ROM_SYSCONTROL;
  111. __attribute__((l1_text))
  112. void bfin_cpu_suspend(void)
  113. {
  114. __asm__ __volatile__( \
  115. ".align 8;" \
  116. "idle;" \
  117. : : \
  118. );
  119. }
  120. __attribute__((l1_text))
  121. void bf609_ddr_sr(void)
  122. {
  123. dmc_enter_self_refresh();
  124. }
  125. __attribute__((l1_text))
  126. void bf609_ddr_sr_exit(void)
  127. {
  128. dmc_exit_self_refresh();
  129. /* After wake up from deep sleep and exit DDR from self refress mode,
  130. * should wait till CGU PLL is locked.
  131. */
  132. while (bfin_read32(CGU0_STAT) & CLKSALGN)
  133. continue;
  134. }
  135. __attribute__((l1_text))
  136. void bf609_resume_ccbuf(void)
  137. {
  138. bfin_write32(DPM0_CCBF_EN, 3);
  139. bfin_write32(DPM0_CTL, 2);
  140. while ((bfin_read32(DPM0_STAT) & 0xf) != 1);
  141. }
  142. __attribute__((l1_text))
  143. void bfin_hibernate_syscontrol(void)
  144. {
  145. configvalues.ulWUA_Flags = (0xAD000000 | BITM_ROM_WUA_EN
  146. | BITM_ROM_WUA_CGU | BITM_ROM_WUA_DDR | BITM_ROM_WUA_DDRDLLEN);
  147. dactionflags = (BITM_ROM_SYSCTRL_WUA_EN
  148. | BITM_ROM_SYSCTRL_WUA_DPMWRITE | BITM_ROM_SYSCTRL_WUA_CGU
  149. | BITM_ROM_SYSCTRL_WUA_DDR | BITM_ROM_SYSCTRL_WUA_DDRDLLEN);
  150. bfrom_SysControl(dactionflags, &configvalues, NULL);
  151. bfin_write32(DPM0_RESTORE5, bfin_read32(DPM0_RESTORE5) | 4);
  152. }
  153. asmlinkage void enter_deepsleep(void);
  154. __attribute__((l1_text))
  155. void bfin_deepsleep(unsigned long mask, unsigned long pol_mask)
  156. {
  157. bfin_write32(DPM0_WAKE_EN, mask);
  158. bfin_write32(DPM0_WAKE_POL, pol_mask);
  159. SSYNC();
  160. enter_deepsleep();
  161. }
  162. void bfin_hibernate(unsigned long mask, unsigned long pol_mask)
  163. {
  164. bfin_write32(DPM0_WAKE_EN, mask);
  165. bfin_write32(DPM0_WAKE_POL, pol_mask);
  166. bfin_write32(DPM0_PGCNTR, 0x0000FFFF);
  167. bfin_write32(DPM0_HIB_DIS, 0xFFFF);
  168. bf609_hibernate();
  169. }
  170. void bf609_cpu_pm_enter(suspend_state_t state)
  171. {
  172. int error;
  173. unsigned long wakeup = 0;
  174. unsigned long wakeup_pol = 0;
  175. #ifdef CONFIG_PM_BFIN_WAKE_PA15
  176. wakeup |= PA15WE;
  177. # if CONFIG_PM_BFIN_WAKE_PA15_POL
  178. wakeup_pol |= PA15WE;
  179. # endif
  180. #endif
  181. #ifdef CONFIG_PM_BFIN_WAKE_PB15
  182. wakeup |= PB15WE;
  183. # if CONFIG_PM_BFIN_WAKE_PB15_POL
  184. wakeup_pol |= PB15WE;
  185. # endif
  186. #endif
  187. #ifdef CONFIG_PM_BFIN_WAKE_PC15
  188. wakeup |= PC15WE;
  189. # if CONFIG_PM_BFIN_WAKE_PC15_POL
  190. wakeup_pol |= PC15WE;
  191. # endif
  192. #endif
  193. #ifdef CONFIG_PM_BFIN_WAKE_PD06
  194. wakeup |= PD06WE;
  195. # if CONFIG_PM_BFIN_WAKE_PD06_POL
  196. wakeup_pol |= PD06WE;
  197. # endif
  198. #endif
  199. #ifdef CONFIG_PM_BFIN_WAKE_PE12
  200. wakeup |= PE12WE;
  201. # if CONFIG_PM_BFIN_WAKE_PE12_POL
  202. wakeup_pol |= PE12WE;
  203. # endif
  204. #endif
  205. #ifdef CONFIG_PM_BFIN_WAKE_PG04
  206. wakeup |= PG04WE;
  207. # if CONFIG_PM_BFIN_WAKE_PG04_POL
  208. wakeup_pol |= PG04WE;
  209. # endif
  210. #endif
  211. #ifdef CONFIG_PM_BFIN_WAKE_PG13
  212. wakeup |= PG13WE;
  213. # if CONFIG_PM_BFIN_WAKE_PG13_POL
  214. wakeup_pol |= PG13WE;
  215. # endif
  216. #endif
  217. #ifdef CONFIG_PM_BFIN_WAKE_USB
  218. wakeup |= USBWE;
  219. # if CONFIG_PM_BFIN_WAKE_USB_POL
  220. wakeup_pol |= USBWE;
  221. # endif
  222. #endif
  223. error = irq_set_irq_wake(255, 1);
  224. if(error < 0)
  225. printk(KERN_DEBUG "Unable to get irq wake\n");
  226. error = irq_set_irq_wake(231, 1);
  227. if (error < 0)
  228. printk(KERN_DEBUG "Unable to get irq wake\n");
  229. if (state == PM_SUSPEND_STANDBY)
  230. bfin_deepsleep(wakeup, wakeup_pol);
  231. else {
  232. bfin_hibernate(wakeup, wakeup_pol);
  233. }
  234. }
  235. int bf609_cpu_pm_prepare(void)
  236. {
  237. return 0;
  238. }
  239. void bf609_cpu_pm_finish(void)
  240. {
  241. }
  242. static struct bfin_cpu_pm_fns bf609_cpu_pm = {
  243. .enter = bf609_cpu_pm_enter,
  244. .prepare = bf609_cpu_pm_prepare,
  245. .finish = bf609_cpu_pm_finish,
  246. };
  247. #if defined(CONFIG_MTD_PHYSMAP) || defined(CONFIG_MTD_PHYSMAP_MODULE)
  248. static int smc_pm_syscore_suspend(void)
  249. {
  250. bf609_nor_flash_exit(NULL);
  251. return 0;
  252. }
  253. static void smc_pm_syscore_resume(void)
  254. {
  255. bf609_nor_flash_init(NULL);
  256. }
  257. static struct syscore_ops smc_pm_syscore_ops = {
  258. .suspend = smc_pm_syscore_suspend,
  259. .resume = smc_pm_syscore_resume,
  260. };
  261. #endif
  262. static irqreturn_t test_isr(int irq, void *dev_id)
  263. {
  264. printk(KERN_DEBUG "gpio irq %d\n", irq);
  265. if (irq == 231)
  266. bfin_sec_raise_irq(BFIN_SYSIRQ(IRQ_SOFT1));
  267. return IRQ_HANDLED;
  268. }
  269. static irqreturn_t dpm0_isr(int irq, void *dev_id)
  270. {
  271. bfin_write32(DPM0_WAKE_STAT, bfin_read32(DPM0_WAKE_STAT));
  272. bfin_write32(CGU0_STAT, bfin_read32(CGU0_STAT));
  273. return IRQ_HANDLED;
  274. }
  275. static int __init bf609_init_pm(void)
  276. {
  277. int irq;
  278. int error;
  279. #if defined(CONFIG_MTD_PHYSMAP) || defined(CONFIG_MTD_PHYSMAP_MODULE)
  280. register_syscore_ops(&smc_pm_syscore_ops);
  281. #endif
  282. #ifdef CONFIG_PM_BFIN_WAKE_PE12
  283. irq = gpio_to_irq(GPIO_PE12);
  284. if (irq < 0) {
  285. error = irq;
  286. printk(KERN_DEBUG "Unable to get irq number for GPIO %d, error %d\n",
  287. GPIO_PE12, error);
  288. }
  289. error = request_irq(irq, test_isr, IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND
  290. | IRQF_FORCE_RESUME, "gpiope12", NULL);
  291. if(error < 0)
  292. printk(KERN_DEBUG "Unable to get irq\n");
  293. #endif
  294. error = request_irq(IRQ_CGU_EVT, dpm0_isr, IRQF_NO_SUSPEND |
  295. IRQF_FORCE_RESUME, "cgu0 event", NULL);
  296. if(error < 0)
  297. printk(KERN_DEBUG "Unable to get irq\n");
  298. error = request_irq(IRQ_DPM, dpm0_isr, IRQF_NO_SUSPEND |
  299. IRQF_FORCE_RESUME, "dpm0 event", NULL);
  300. if (error < 0)
  301. printk(KERN_DEBUG "Unable to get irq\n");
  302. bfin_cpu_pm = &bf609_cpu_pm;
  303. return 0;
  304. }
  305. late_initcall(bf609_init_pm);