pm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <asm/dpmc.h>
  15. #include <asm/pm.h>
  16. #include <mach/pm.h>
  17. #include <asm/blackfin.h>
  18. /***********************************************************/
  19. /* */
  20. /* Wakeup Actions for DPM_RESTORE */
  21. /* */
  22. /***********************************************************/
  23. #define BITP_ROM_WUA_CHKHDR 24
  24. #define BITP_ROM_WUA_DDRLOCK 7
  25. #define BITP_ROM_WUA_DDRDLLEN 6
  26. #define BITP_ROM_WUA_DDR 5
  27. #define BITP_ROM_WUA_CGU 4
  28. #define BITP_ROM_WUA_MEMBOOT 2
  29. #define BITP_ROM_WUA_EN 1
  30. #define BITM_ROM_WUA_CHKHDR (0xFF000000)
  31. #define ENUM_ROM_WUA_CHKHDR_AD 0xAD000000
  32. #define BITM_ROM_WUA_DDRLOCK (0x00000080)
  33. #define BITM_ROM_WUA_DDRDLLEN (0x00000040)
  34. #define BITM_ROM_WUA_DDR (0x00000020)
  35. #define BITM_ROM_WUA_CGU (0x00000010)
  36. #define BITM_ROM_WUA_MEMBOOT (0x00000002)
  37. #define BITM_ROM_WUA_EN (0x00000001)
  38. /***********************************************************/
  39. /* */
  40. /* Syscontrol */
  41. /* */
  42. /***********************************************************/
  43. #define BITP_ROM_SYSCTRL_CGU_LOCKINGEN 28 /* unlocks CGU_CTL register */
  44. #define BITP_ROM_SYSCTRL_WUA_OVERRIDE 24
  45. #define BITP_ROM_SYSCTRL_WUA_DDRDLLEN 20 /* Saves the DDR DLL and PADS registers to the DPM registers */
  46. #define BITP_ROM_SYSCTRL_WUA_DDR 19 /* Saves the DDR registers to the DPM registers */
  47. #define BITP_ROM_SYSCTRL_WUA_CGU 18 /* Saves the CGU registers into DPM registers */
  48. #define BITP_ROM_SYSCTRL_WUA_DPMWRITE 17 /* Saves the Syscontrol structure structure contents into DPM registers */
  49. #define BITP_ROM_SYSCTRL_WUA_EN 16 /* reads current PLL and DDR configuration into structure */
  50. #define BITP_ROM_SYSCTRL_DDR_WRITE 13 /* writes the DDR registers from Syscontrol structure for wakeup initialization of DDR */
  51. #define BITP_ROM_SYSCTRL_DDR_READ 12 /* Read the DDR registers into the Syscontrol structure for storing prior to hibernate */
  52. #define BITP_ROM_SYSCTRL_CGU_AUTODIS 11 /* Disables auto handling of UPDT and ALGN fields */
  53. #define BITP_ROM_SYSCTRL_CGU_CLKOUTSEL 7 /* access CGU_CLKOUTSEL register */
  54. #define BITP_ROM_SYSCTRL_CGU_DIV 6 /* access CGU_DIV register */
  55. #define BITP_ROM_SYSCTRL_CGU_STAT 5 /* access CGU_STAT register */
  56. #define BITP_ROM_SYSCTRL_CGU_CTL 4 /* access CGU_CTL register */
  57. #define BITP_ROM_SYSCTRL_CGU_RTNSTAT 2 /* Update structure STAT field upon error */
  58. #define BITP_ROM_SYSCTRL_WRITE 1 /* write registers */
  59. #define BITP_ROM_SYSCTRL_READ 0 /* read registers */
  60. #define BITM_ROM_SYSCTRL_CGU_READ (0x00000001) /* Read CGU registers */
  61. #define BITM_ROM_SYSCTRL_CGU_WRITE (0x00000002) /* Write registers */
  62. #define BITM_ROM_SYSCTRL_CGU_RTNSTAT (0x00000004) /* Update structure STAT field upon error or after a write operation */
  63. #define BITM_ROM_SYSCTRL_CGU_CTL (0x00000010) /* Access CGU_CTL register */
  64. #define BITM_ROM_SYSCTRL_CGU_STAT (0x00000020) /* Access CGU_STAT register */
  65. #define BITM_ROM_SYSCTRL_CGU_DIV (0x00000040) /* Access CGU_DIV register */
  66. #define BITM_ROM_SYSCTRL_CGU_CLKOUTSEL (0x00000080) /* Access CGU_CLKOUTSEL register */
  67. #define BITM_ROM_SYSCTRL_CGU_AUTODIS (0x00000800) /* Disables auto handling of UPDT and ALGN fields */
  68. #define BITM_ROM_SYSCTRL_DDR_READ (0x00001000) /* Reads the contents of the DDR registers and stores them into the structure */
  69. #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 */
  70. #define BITM_ROM_SYSCTRL_WUA_EN (0x00010000) /* Wakeup entry or exit opertation enable */
  71. #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 */
  72. #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 */
  73. #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 */
  74. #define BITM_ROM_SYSCTRL_WUA_DDRDLLEN (0x00100000) /* Enables saving/restoring of the DDR DLLCTL register */
  75. #define BITM_ROM_SYSCTRL_WUA_OVERRIDE (0x01000000)
  76. #define BITM_ROM_SYSCTRL_CGU_LOCKINGEN (0x10000000) /* Unlocks the CGU_CTL register */
  77. /* Structures for the syscontrol() function */
  78. struct STRUCT_ROM_SYSCTRL {
  79. uint32_t ulCGU_CTL;
  80. uint32_t ulCGU_STAT;
  81. uint32_t ulCGU_DIV;
  82. uint32_t ulCGU_CLKOUTSEL;
  83. uint32_t ulWUA_Flags;
  84. uint32_t ulWUA_BootAddr;
  85. uint32_t ulWUA_User;
  86. uint32_t ulDDR_CTL;
  87. uint32_t ulDDR_CFG;
  88. uint32_t ulDDR_TR0;
  89. uint32_t ulDDR_TR1;
  90. uint32_t ulDDR_TR2;
  91. uint32_t ulDDR_MR;
  92. uint32_t ulDDR_EMR1;
  93. uint32_t ulDDR_EMR2;
  94. uint32_t ulDDR_PADCTL;
  95. uint32_t ulDDR_DLLCTL;
  96. uint32_t ulReserved;
  97. };
  98. struct bfin_pm_data {
  99. uint32_t magic;
  100. uint32_t resume_addr;
  101. uint32_t sp;
  102. };
  103. struct bfin_pm_data bf609_pm_data;
  104. struct STRUCT_ROM_SYSCTRL configvalues;
  105. uint32_t dactionflags;
  106. #define FUNC_ROM_SYSCONTROL 0xC8000080
  107. __attribute__((l1_data))
  108. static uint32_t (* const bfrom_SysControl)(uint32_t action_flags, struct STRUCT_ROM_SYSCTRL *settings, void *reserved) = (void *)FUNC_ROM_SYSCONTROL;
  109. __attribute__((l1_text))
  110. void bfin_cpu_suspend(void)
  111. {
  112. __asm__ __volatile__( \
  113. ".align 8;" \
  114. "idle;" \
  115. : : \
  116. );
  117. }
  118. __attribute__((l1_text))
  119. void bfin_deepsleep(unsigned long mask)
  120. {
  121. uint32_t dpm0_ctl;
  122. bfin_write32(DPM0_WAKE_EN, 0x10);
  123. bfin_write32(DPM0_WAKE_POL, 0x10);
  124. dpm0_ctl = bfin_read32(DPM0_CTL);
  125. dpm0_ctl = 0x00000008;
  126. bfin_write32(DPM0_CTL, dpm0_ctl);
  127. SSYNC();
  128. __asm__ __volatile__( \
  129. ".align 8;" \
  130. "idle;" \
  131. : : \
  132. );
  133. }
  134. __attribute__((l1_text))
  135. void bf609_ddr_sr(void)
  136. {
  137. uint32_t reg;
  138. reg = bfin_read_DDR0_CTL();
  139. reg |= 0x8;
  140. bfin_write_DDR0_CTL(reg);
  141. while (!(bfin_read_DDR0_STAT() & 0x8))
  142. continue;
  143. }
  144. __attribute__((l1_text))
  145. void bf609_ddr_sr_exit(void)
  146. {
  147. uint32_t reg;
  148. while (!(bfin_read_DDR0_STAT() & 0x1))
  149. continue;
  150. reg = bfin_read_DDR0_CTL();
  151. reg &= ~0x8;
  152. bfin_write_DDR0_CTL(reg);
  153. while ((bfin_read_DDR0_STAT() & 0x8))
  154. continue;
  155. }
  156. __attribute__((l1_text))
  157. void bfin_hibernate_syscontrol(void)
  158. {
  159. configvalues.ulWUA_Flags = (0xAD000000 | BITM_ROM_WUA_EN
  160. | BITM_ROM_WUA_CGU | BITM_ROM_WUA_DDR | BITM_ROM_WUA_DDRDLLEN);
  161. dactionflags = (BITM_ROM_SYSCTRL_WUA_EN
  162. | BITM_ROM_SYSCTRL_WUA_DPMWRITE | BITM_ROM_SYSCTRL_WUA_CGU
  163. | BITM_ROM_SYSCTRL_WUA_DDR | BITM_ROM_SYSCTRL_WUA_DDRDLLEN);
  164. bfrom_SysControl(dactionflags, &configvalues, NULL);
  165. bfin_write32(DPM0_RESTORE5, bfin_read32(DPM0_RESTORE5) | 4);
  166. }
  167. #ifndef CONFIG_BF60x
  168. # define SIC_SYSIRQ(irq) (irq - (IRQ_CORETMR + 1))
  169. #else
  170. # define SIC_SYSIRQ(irq) ((irq) - IVG15)
  171. #endif
  172. void bfin_hibernate(unsigned long mask)
  173. {
  174. bfin_write32(DPM0_WAKE_EN, 0x10);
  175. bfin_write32(DPM0_WAKE_POL, 0x10);
  176. bfin_write32(DPM0_PGCNTR, 0x0000FFFF);
  177. bfin_write32(DPM0_HIB_DIS, 0xFFFF);
  178. printk(KERN_DEBUG "hibernate: restore %x pgcnt %x\n", bfin_read32(DPM0_RESTORE0), bfin_read32(DPM0_PGCNTR));
  179. bf609_hibernate();
  180. }
  181. void bf609_cpu_pm_enter(suspend_state_t state)
  182. {
  183. int error;
  184. error = irq_set_irq_wake(255, 1);
  185. if(error < 0)
  186. printk(KERN_DEBUG "Unable to get irq wake\n");
  187. error = irq_set_irq_wake(231, 1);
  188. if (error < 0)
  189. printk(KERN_DEBUG "Unable to get irq wake\n");
  190. if (state == PM_SUSPEND_STANDBY)
  191. bfin_deepsleep(0xffff);
  192. else {
  193. bfin_hibernate(0xffff);
  194. }
  195. }
  196. int bf609_cpu_pm_prepare(void)
  197. {
  198. return 0;
  199. }
  200. void bf609_cpu_pm_finish(void)
  201. {
  202. }
  203. static struct bfin_cpu_pm_fns bf609_cpu_pm = {
  204. .enter = bf609_cpu_pm_enter,
  205. .prepare = bf609_cpu_pm_prepare,
  206. .finish = bf609_cpu_pm_finish,
  207. };
  208. static irqreturn_t test_isr(int irq, void *dev_id)
  209. {
  210. printk(KERN_DEBUG "gpio irq %d\n", irq);
  211. return IRQ_HANDLED;
  212. }
  213. static irqreturn_t dpm0_isr(int irq, void *dev_id)
  214. {
  215. uint32_t wake_stat;
  216. wake_stat = bfin_read32(DPM0_WAKE_STAT);
  217. printk(KERN_DEBUG "enter %s wake stat %08x\n", __func__, wake_stat);
  218. bfin_write32(DPM0_WAKE_STAT, wake_stat);
  219. return IRQ_HANDLED;
  220. }
  221. static int __init bf609_init_pm(void)
  222. {
  223. int irq;
  224. int error;
  225. error = gpio_request(GPIO_PG4, "gpiopg4");
  226. if (error < 0) {
  227. printk(KERN_DEBUG "failed to request GPIO %d, error %d\n",
  228. GPIO_PG4, error);
  229. }
  230. irq = gpio_to_irq(GPIO_PG4);
  231. if (irq < 0) {
  232. error = irq;
  233. printk(KERN_DEBUG "Unable to get irq number for GPIO %d, error %d\n",
  234. GPIO_PG4, error);
  235. }
  236. printk(KERN_DEBUG "%s gpio %d irq %d\n", __func__, GPIO_PG4, irq);
  237. error = request_irq(irq, test_isr, IRQF_TRIGGER_FALLING | IRQF_NO_SUSPEND, "gpiopg4", NULL);
  238. if(error < 0)
  239. printk(KERN_DEBUG "Unable to get irq\n");
  240. #if 1
  241. irq = gpio_to_irq(GPIO_PE12);
  242. if (irq < 0) {
  243. error = irq;
  244. printk(KERN_DEBUG "Unable to get irq number for GPIO %d, error %d\n",
  245. GPIO_PE12, error);
  246. }
  247. error = request_irq(irq, test_isr, IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND, "gpiope12", NULL);
  248. if(error < 0)
  249. printk(KERN_DEBUG "Unable to get irq\n");
  250. #endif
  251. error = request_irq(IRQ_CGU_EVT, dpm0_isr, IRQF_NO_SUSPEND, "cgu0 event", NULL);
  252. if(error < 0)
  253. printk(KERN_DEBUG "Unable to get irq\n");
  254. error = request_irq(IRQ_DPM, dpm0_isr, IRQF_NO_SUSPEND, "dpm0 event", NULL);
  255. if (error < 0)
  256. printk(KERN_DEBUG "Unable to get irq\n");
  257. bfin_cpu_pm = &bf609_cpu_pm;
  258. return 0;
  259. }
  260. late_initcall(bf609_init_pm);