pm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* linux/arch/arm/plat-s3c/pm.c
  2. *
  3. * Copyright 2008 Openmoko, Inc.
  4. * Copyright 2004,2006,2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * S3C common power management (suspend to ram) support.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/suspend.h>
  16. #include <linux/errno.h>
  17. #include <linux/delay.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/io.h>
  20. #include <asm/cacheflush.h>
  21. #include <mach/hardware.h>
  22. #include <plat/regs-serial.h>
  23. #include <mach/regs-clock.h>
  24. #include <mach/regs-gpio.h>
  25. #include <mach/regs-mem.h>
  26. #include <mach/regs-irq.h>
  27. #include <plat/pm.h>
  28. #include <plat/pm-core.h>
  29. /* for external use */
  30. unsigned long s3c_pm_flags;
  31. /* Debug code:
  32. *
  33. * This code supports debug output to the low level UARTs for use on
  34. * resume before the console layer is available.
  35. */
  36. #ifdef CONFIG_S3C2410_PM_DEBUG
  37. extern void printascii(const char *);
  38. void s3c_pm_dbg(const char *fmt, ...)
  39. {
  40. va_list va;
  41. char buff[256];
  42. va_start(va, fmt);
  43. vsprintf(buff, fmt, va);
  44. va_end(va);
  45. printascii(buff);
  46. }
  47. static inline void s3c_pm_debug_init(void)
  48. {
  49. /* restart uart clocks so we can use them to output */
  50. s3c_pm_debug_init_uart();
  51. }
  52. #else
  53. #define s3c_pm_debug_init() do { } while(0)
  54. #endif /* CONFIG_S3C2410_PM_DEBUG */
  55. /* Save the UART configurations if we are configured for debug. */
  56. #ifdef CONFIG_S3C2410_PM_DEBUG
  57. #define SAVE_UART(va) \
  58. SAVE_ITEM((va) + S3C2410_ULCON), \
  59. SAVE_ITEM((va) + S3C2410_UCON), \
  60. SAVE_ITEM((va) + S3C2410_UFCON), \
  61. SAVE_ITEM((va) + S3C2410_UMCON), \
  62. SAVE_ITEM((va) + S3C2410_UBRDIV)
  63. static struct sleep_save uart_save[] = {
  64. SAVE_UART(S3C_VA_UART0),
  65. SAVE_UART(S3C_VA_UART1),
  66. #ifndef CONFIG_CPU_S3C2400
  67. SAVE_UART(S3C_VA_UART2),
  68. #endif
  69. };
  70. static void s3c_pm_save_uart(void)
  71. {
  72. s3c_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
  73. }
  74. static void s3c_pm_restore_uart(void)
  75. {
  76. s3c_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
  77. }
  78. #else
  79. static void s3c_pm_save_uart(void) { }
  80. static void s3c_pm_restore_uart(void) { }
  81. #endif
  82. /* helper functions to save and restore register state */
  83. /**
  84. * s3c_pm_do_save() - save a set of registers for restoration on resume.
  85. * @ptr: Pointer to an array of registers.
  86. * @count: Size of the ptr array.
  87. *
  88. * Run through the list of registers given, saving their contents in the
  89. * array for later restoration when we wakeup.
  90. */
  91. void s3c_pm_do_save(struct sleep_save *ptr, int count)
  92. {
  93. for (; count > 0; count--, ptr++) {
  94. ptr->val = __raw_readl(ptr->reg);
  95. S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
  96. }
  97. }
  98. /**
  99. * s3c_pm_do_restore() - restore register values from the save list.
  100. * @ptr: Pointer to an array of registers.
  101. * @count: Size of the ptr array.
  102. *
  103. * Restore the register values saved from s3c_pm_do_save().
  104. *
  105. * Note, we do not use S3C_PMDBG() in here, as the system may not have
  106. * restore the UARTs state yet
  107. */
  108. void s3c_pm_do_restore(struct sleep_save *ptr, int count)
  109. {
  110. for (; count > 0; count--, ptr++) {
  111. printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
  112. ptr->reg, ptr->val, __raw_readl(ptr->reg));
  113. __raw_writel(ptr->val, ptr->reg);
  114. }
  115. }
  116. /**
  117. * s3c_pm_do_restore_core() - early restore register values from save list.
  118. *
  119. * This is similar to s3c_pm_do_restore() except we try and minimise the
  120. * side effects of the function in case registers that hardware might need
  121. * to work has been restored.
  122. *
  123. * WARNING: Do not put any debug in here that may effect memory or use
  124. * peripherals, as things may be changing!
  125. */
  126. void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
  127. {
  128. for (; count > 0; count--, ptr++)
  129. __raw_writel(ptr->val, ptr->reg);
  130. }
  131. /* s3c2410_pm_show_resume_irqs
  132. *
  133. * print any IRQs asserted at resume time (ie, we woke from)
  134. */
  135. static void s3c_pm_show_resume_irqs(int start, unsigned long which,
  136. unsigned long mask)
  137. {
  138. int i;
  139. which &= ~mask;
  140. for (i = 0; i <= 31; i++) {
  141. if (which & (1L<<i)) {
  142. S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
  143. }
  144. }
  145. }
  146. void (*pm_cpu_prep)(void);
  147. void (*pm_cpu_sleep)(void);
  148. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  149. /* s3c_pm_enter
  150. *
  151. * central control for sleep/resume process
  152. */
  153. static int s3c_pm_enter(suspend_state_t state)
  154. {
  155. unsigned long regs_save[16];
  156. /* ensure the debug is initialised (if enabled) */
  157. s3c_pm_debug_init();
  158. S3C_PMDBG("%s(%d)\n", __func__, state);
  159. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  160. printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
  161. return -EINVAL;
  162. }
  163. /* check if we have anything to wake-up with... bad things seem
  164. * to happen if you suspend with no wakeup (system will often
  165. * require a full power-cycle)
  166. */
  167. if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  168. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  169. printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
  170. printk(KERN_ERR "%s: Aborting sleep\n", __func__);
  171. return -EINVAL;
  172. }
  173. /* prepare check area if configured */
  174. s3c_pm_check_prepare();
  175. /* store the physical address of the register recovery block */
  176. s3c_sleep_save_phys = virt_to_phys(regs_save);
  177. S3C_PMDBG("s3c_sleep_save_phys=0x%08lx\n", s3c_sleep_save_phys);
  178. /* save all necessary core registers not covered by the drivers */
  179. s3c_pm_save_gpios();
  180. s3c_pm_save_uart();
  181. s3c_pm_save_core();
  182. /* set the irq configuration for wake */
  183. s3c_pm_configure_extint();
  184. S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  185. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  186. s3c_pm_arch_prepare_irqs();
  187. /* call cpu specific preparation */
  188. pm_cpu_prep();
  189. /* flush cache back to ram */
  190. flush_cache_all();
  191. s3c_pm_check_store();
  192. /* send the cpu to sleep... */
  193. s3c_pm_arch_stop_clocks();
  194. /* s3c2410_cpu_save will also act as our return point from when
  195. * we resume as it saves its own register state, so use the return
  196. * code to differentiate return from save and return from sleep */
  197. if (s3c2410_cpu_save(regs_save) == 0) {
  198. flush_cache_all();
  199. pm_cpu_sleep();
  200. }
  201. /* restore the cpu state using the kernel's cpu init code. */
  202. cpu_init();
  203. /* restore the system state */
  204. s3c_pm_restore_core();
  205. s3c_pm_restore_uart();
  206. s3c_pm_restore_gpios();
  207. s3c_pm_debug_init();
  208. /* check what irq (if any) restored the system */
  209. s3c_pm_arch_show_resume_irqs();
  210. S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
  211. s3c_pm_check_restore();
  212. /* ok, let's return from sleep */
  213. S3C_PMDBG("S3C PM Resume (post-restore)\n");
  214. return 0;
  215. }
  216. static struct platform_suspend_ops s3c_pm_ops = {
  217. .enter = s3c_pm_enter,
  218. .valid = suspend_valid_only_mem,
  219. };
  220. /* s3c2410_pm_init
  221. *
  222. * Attach the power management functions. This should be called
  223. * from the board specific initialisation if the board supports
  224. * it.
  225. */
  226. int __init s3c2410_pm_init(void)
  227. {
  228. printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
  229. suspend_set_ops(&s3c_pm_ops);
  230. return 0;
  231. }