pm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 = 0x00000008;
  125. bfin_write32(DPM0_CTL, dpm0_ctl);
  126. SSYNC();
  127. __asm__ __volatile__( \
  128. ".align 8;" \
  129. "idle;" \
  130. : : \
  131. );
  132. #ifdef CONFIG_BFIN_PM_WAKEUP_TIME_BENCH
  133. __asm__ __volatile__(
  134. "R0 = 0;"
  135. "CYCLES = R0;"
  136. "CYCLES2 = R0;"
  137. "R0 = SYSCFG;"
  138. "BITSET(R0, 1);"
  139. "SYSCFG = R0;"
  140. : : : "R0"
  141. );
  142. #endif
  143. }
  144. __attribute__((l1_text))
  145. void bf609_ddr_sr(void)
  146. {
  147. uint32_t reg;
  148. reg = bfin_read_DMC0_CTL();
  149. reg |= 0x8;
  150. bfin_write_DMC0_CTL(reg);
  151. while (!(bfin_read_DMC0_STAT() & 0x8))
  152. continue;
  153. }
  154. __attribute__((l1_text))
  155. void bf609_ddr_sr_exit(void)
  156. {
  157. uint32_t reg;
  158. while (!(bfin_read_DMC0_STAT() & 0x1))
  159. continue;
  160. reg = bfin_read_DMC0_CTL();
  161. reg &= ~0x8;
  162. bfin_write_DMC0_CTL(reg);
  163. while ((bfin_read_DMC0_STAT() & 0x8))
  164. continue;
  165. }
  166. __attribute__((l1_text))
  167. void bfin_hibernate_syscontrol(void)
  168. {
  169. configvalues.ulWUA_Flags = (0xAD000000 | BITM_ROM_WUA_EN
  170. | BITM_ROM_WUA_CGU | BITM_ROM_WUA_DDR | BITM_ROM_WUA_DDRDLLEN);
  171. dactionflags = (BITM_ROM_SYSCTRL_WUA_EN
  172. | BITM_ROM_SYSCTRL_WUA_DPMWRITE | BITM_ROM_SYSCTRL_WUA_CGU
  173. | BITM_ROM_SYSCTRL_WUA_DDR | BITM_ROM_SYSCTRL_WUA_DDRDLLEN);
  174. bfrom_SysControl(dactionflags, &configvalues, NULL);
  175. bfin_write32(DPM0_RESTORE5, bfin_read32(DPM0_RESTORE5) | 4);
  176. }
  177. #ifndef CONFIG_BF60x
  178. # define SIC_SYSIRQ(irq) (irq - (IRQ_CORETMR + 1))
  179. #else
  180. # define SIC_SYSIRQ(irq) ((irq) - IVG15)
  181. #endif
  182. void bfin_hibernate(unsigned long mask)
  183. {
  184. bfin_write32(DPM0_WAKE_EN, 0x10);
  185. bfin_write32(DPM0_WAKE_POL, 0x10);
  186. bfin_write32(DPM0_PGCNTR, 0x0000FFFF);
  187. bfin_write32(DPM0_HIB_DIS, 0xFFFF);
  188. printk(KERN_DEBUG "hibernate: restore %x pgcnt %x\n", bfin_read32(DPM0_RESTORE0), bfin_read32(DPM0_PGCNTR));
  189. bf609_hibernate();
  190. }
  191. void bf609_cpu_pm_enter(suspend_state_t state)
  192. {
  193. int error;
  194. unsigned long wakeup = 0;
  195. unsigned long wakeup_pol = 0;
  196. #ifdef CONFIG_PM_BFIN_WAKE_PA15
  197. wakeup |= PA15WE;
  198. # if CONFIG_PM_BFIN_WAKE_PA15_POL
  199. wakeup_pol |= PA15WE;
  200. # endif
  201. #endif
  202. #ifdef CONFIG_PM_BFIN_WAKE_PB15
  203. wakeup |= PB15WE;
  204. # if CONFIG_PM_BFIN_WAKE_PA15_POL
  205. wakeup_pol |= PB15WE;
  206. # endif
  207. #endif
  208. #ifdef CONFIG_PM_BFIN_WAKE_PC15
  209. wakeup |= PC15WE;
  210. # if CONFIG_PM_BFIN_WAKE_PC15_POL
  211. wakeup_pol |= PC15WE;
  212. # endif
  213. #endif
  214. #ifdef CONFIG_PM_BFIN_WAKE_PD06
  215. wakeup |= PD06WE;
  216. # if CONFIG_PM_BFIN_WAKE_PD06_POL
  217. wakeup_pol |= PD06WE;
  218. # endif
  219. #endif
  220. #ifdef CONFIG_PM_BFIN_WAKE_PE12
  221. wakeup |= PE12WE;
  222. # if CONFIG_PM_BFIN_WAKE_PE12_POL
  223. wakeup_pol |= PE12WE;
  224. # endif
  225. #endif
  226. #ifdef CONFIG_PM_BFIN_WAKE_PG04
  227. wakeup |= PG04WE;
  228. # if CONFIG_PM_BFIN_WAKE_PG04_POL
  229. wakeup_pol |= PG04WE;
  230. # endif
  231. #endif
  232. #ifdef CONFIG_PM_BFIN_WAKE_PG13
  233. wakeup |= PG13WE;
  234. # if CONFIG_PM_BFIN_WAKE_PG13_POL
  235. wakeup_pol |= PG13WE;
  236. # endif
  237. #endif
  238. #ifdef CONFIG_PM_BFIN_WAKE_USB
  239. wakeup |= USBWE;
  240. # if CONFIG_PM_BFIN_WAKE_USB_POL
  241. wakeup_pol |= USBWE;
  242. # endif
  243. #endif
  244. error = irq_set_irq_wake(255, 1);
  245. if(error < 0)
  246. printk(KERN_DEBUG "Unable to get irq wake\n");
  247. error = irq_set_irq_wake(231, 1);
  248. if (error < 0)
  249. printk(KERN_DEBUG "Unable to get irq wake\n");
  250. if (state == PM_SUSPEND_STANDBY)
  251. bfin_deepsleep(wakeup);
  252. else {
  253. bfin_hibernate(wakeup);
  254. }
  255. }
  256. int bf609_cpu_pm_prepare(void)
  257. {
  258. return 0;
  259. }
  260. void bf609_cpu_pm_finish(void)
  261. {
  262. }
  263. static struct bfin_cpu_pm_fns bf609_cpu_pm = {
  264. .enter = bf609_cpu_pm_enter,
  265. .prepare = bf609_cpu_pm_prepare,
  266. .finish = bf609_cpu_pm_finish,
  267. };
  268. static irqreturn_t test_isr(int irq, void *dev_id)
  269. {
  270. printk(KERN_DEBUG "gpio irq %d\n", irq);
  271. return IRQ_HANDLED;
  272. }
  273. static irqreturn_t dpm0_isr(int irq, void *dev_id)
  274. {
  275. uint32_t wake_stat;
  276. wake_stat = bfin_read32(DPM0_WAKE_STAT);
  277. printk(KERN_DEBUG "enter %s wake stat %08x\n", __func__, wake_stat);
  278. bfin_write32(DPM0_WAKE_STAT, wake_stat);
  279. return IRQ_HANDLED;
  280. }
  281. static int __init bf609_init_pm(void)
  282. {
  283. int irq;
  284. int error;
  285. #if CONFIG_PM_BFIN_WAKE_PE12
  286. irq = gpio_to_irq(GPIO_PE12);
  287. if (irq < 0) {
  288. error = irq;
  289. printk(KERN_DEBUG "Unable to get irq number for GPIO %d, error %d\n",
  290. GPIO_PE12, error);
  291. }
  292. error = request_irq(irq, test_isr, IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND, "gpiope12", NULL);
  293. if(error < 0)
  294. printk(KERN_DEBUG "Unable to get irq\n");
  295. #endif
  296. error = request_irq(IRQ_CGU_EVT, dpm0_isr, IRQF_NO_SUSPEND, "cgu0 event", NULL);
  297. if(error < 0)
  298. printk(KERN_DEBUG "Unable to get irq\n");
  299. error = request_irq(IRQ_DPM, dpm0_isr, IRQF_NO_SUSPEND, "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);