pm.c 11 KB

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