pm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* linux/arch/arm/plat-s3c/pm.c
  2. *
  3. * Copyright 2008 Openmoko, Inc.
  4. * Copyright 2004-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/of.h>
  19. #include <linux/serial_core.h>
  20. #include <linux/io.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/suspend.h>
  23. #include <mach/hardware.h>
  24. #include <mach/map.h>
  25. #include <plat/regs-serial.h>
  26. #include <mach/regs-clock.h>
  27. #include <mach/regs-irq.h>
  28. #include <mach/irqs.h>
  29. #include <asm/irq.h>
  30. #include <plat/pm.h>
  31. #include <mach/pm-core.h>
  32. /* for external use */
  33. unsigned long s3c_pm_flags;
  34. /* Debug code:
  35. *
  36. * This code supports debug output to the low level UARTs for use on
  37. * resume before the console layer is available.
  38. */
  39. #ifdef CONFIG_SAMSUNG_PM_DEBUG
  40. extern void printascii(const char *);
  41. void s3c_pm_dbg(const char *fmt, ...)
  42. {
  43. va_list va;
  44. char buff[256];
  45. va_start(va, fmt);
  46. vsnprintf(buff, sizeof(buff), fmt, va);
  47. va_end(va);
  48. printascii(buff);
  49. }
  50. static inline void s3c_pm_debug_init(void)
  51. {
  52. /* restart uart clocks so we can use them to output */
  53. s3c_pm_debug_init_uart();
  54. }
  55. #else
  56. #define s3c_pm_debug_init() do { } while(0)
  57. #endif /* CONFIG_SAMSUNG_PM_DEBUG */
  58. /* Save the UART configurations if we are configured for debug. */
  59. unsigned char pm_uart_udivslot;
  60. #ifdef CONFIG_SAMSUNG_PM_DEBUG
  61. static struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS];
  62. static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save)
  63. {
  64. void __iomem *regs = S3C_VA_UARTx(uart);
  65. save->ulcon = __raw_readl(regs + S3C2410_ULCON);
  66. save->ucon = __raw_readl(regs + S3C2410_UCON);
  67. save->ufcon = __raw_readl(regs + S3C2410_UFCON);
  68. save->umcon = __raw_readl(regs + S3C2410_UMCON);
  69. save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV);
  70. if (pm_uart_udivslot)
  71. save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT);
  72. S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
  73. uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv);
  74. }
  75. static void s3c_pm_save_uarts(void)
  76. {
  77. struct pm_uart_save *save = uart_save;
  78. unsigned int uart;
  79. for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
  80. s3c_pm_save_uart(uart, save);
  81. }
  82. static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save)
  83. {
  84. void __iomem *regs = S3C_VA_UARTx(uart);
  85. s3c_pm_arch_update_uart(regs, save);
  86. __raw_writel(save->ulcon, regs + S3C2410_ULCON);
  87. __raw_writel(save->ucon, regs + S3C2410_UCON);
  88. __raw_writel(save->ufcon, regs + S3C2410_UFCON);
  89. __raw_writel(save->umcon, regs + S3C2410_UMCON);
  90. __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV);
  91. if (pm_uart_udivslot)
  92. __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT);
  93. }
  94. static void s3c_pm_restore_uarts(void)
  95. {
  96. struct pm_uart_save *save = uart_save;
  97. unsigned int uart;
  98. for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
  99. s3c_pm_restore_uart(uart, save);
  100. }
  101. #else
  102. static void s3c_pm_save_uarts(void) { }
  103. static void s3c_pm_restore_uarts(void) { }
  104. #endif
  105. /* The IRQ ext-int code goes here, it is too small to currently bother
  106. * with its own file. */
  107. unsigned long s3c_irqwake_intmask = 0xffffffffL;
  108. unsigned long s3c_irqwake_eintmask = 0xffffffffL;
  109. int s3c_irqext_wake(struct irq_data *data, unsigned int state)
  110. {
  111. unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
  112. if (!(s3c_irqwake_eintallow & bit))
  113. return -ENOENT;
  114. printk(KERN_INFO "wake %s for irq %d\n",
  115. state ? "enabled" : "disabled", data->irq);
  116. if (!state)
  117. s3c_irqwake_eintmask |= bit;
  118. else
  119. s3c_irqwake_eintmask &= ~bit;
  120. return 0;
  121. }
  122. /* helper functions to save and restore register state */
  123. /**
  124. * s3c_pm_do_save() - save a set of registers for restoration on resume.
  125. * @ptr: Pointer to an array of registers.
  126. * @count: Size of the ptr array.
  127. *
  128. * Run through the list of registers given, saving their contents in the
  129. * array for later restoration when we wakeup.
  130. */
  131. void s3c_pm_do_save(struct sleep_save *ptr, int count)
  132. {
  133. for (; count > 0; count--, ptr++) {
  134. ptr->val = __raw_readl(ptr->reg);
  135. S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
  136. }
  137. }
  138. /**
  139. * s3c_pm_do_restore() - restore register values from the save list.
  140. * @ptr: Pointer to an array of registers.
  141. * @count: Size of the ptr array.
  142. *
  143. * Restore the register values saved from s3c_pm_do_save().
  144. *
  145. * Note, we do not use S3C_PMDBG() in here, as the system may not have
  146. * restore the UARTs state yet
  147. */
  148. void s3c_pm_do_restore(struct sleep_save *ptr, int count)
  149. {
  150. for (; count > 0; count--, ptr++) {
  151. printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
  152. ptr->reg, ptr->val, __raw_readl(ptr->reg));
  153. __raw_writel(ptr->val, ptr->reg);
  154. }
  155. }
  156. /**
  157. * s3c_pm_do_restore_core() - early restore register values from save list.
  158. *
  159. * This is similar to s3c_pm_do_restore() except we try and minimise the
  160. * side effects of the function in case registers that hardware might need
  161. * to work has been restored.
  162. *
  163. * WARNING: Do not put any debug in here that may effect memory or use
  164. * peripherals, as things may be changing!
  165. */
  166. void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
  167. {
  168. for (; count > 0; count--, ptr++)
  169. __raw_writel(ptr->val, ptr->reg);
  170. }
  171. /* s3c2410_pm_show_resume_irqs
  172. *
  173. * print any IRQs asserted at resume time (ie, we woke from)
  174. */
  175. static void __maybe_unused s3c_pm_show_resume_irqs(int start,
  176. unsigned long which,
  177. unsigned long mask)
  178. {
  179. int i;
  180. which &= ~mask;
  181. for (i = 0; i <= 31; i++) {
  182. if (which & (1L<<i)) {
  183. S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
  184. }
  185. }
  186. }
  187. void (*pm_cpu_prep)(void);
  188. int (*pm_cpu_sleep)(unsigned long);
  189. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  190. /* s3c_pm_enter
  191. *
  192. * central control for sleep/resume process
  193. */
  194. static int s3c_pm_enter(suspend_state_t state)
  195. {
  196. int ret;
  197. /* ensure the debug is initialised (if enabled) */
  198. s3c_pm_debug_init();
  199. S3C_PMDBG("%s(%d)\n", __func__, state);
  200. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  201. printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
  202. return -EINVAL;
  203. }
  204. /* check if we have anything to wake-up with... bad things seem
  205. * to happen if you suspend with no wakeup (system will often
  206. * require a full power-cycle)
  207. */
  208. if (!of_have_populated_dt() &&
  209. !any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  210. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  211. printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
  212. printk(KERN_ERR "%s: Aborting sleep\n", __func__);
  213. return -EINVAL;
  214. }
  215. /* save all necessary core registers not covered by the drivers */
  216. if (!of_have_populated_dt()) {
  217. samsung_pm_save_gpios();
  218. samsung_pm_saved_gpios();
  219. }
  220. s3c_pm_save_uarts();
  221. s3c_pm_save_core();
  222. /* set the irq configuration for wake */
  223. s3c_pm_configure_extint();
  224. S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  225. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  226. s3c_pm_arch_prepare_irqs();
  227. /* call cpu specific preparation */
  228. pm_cpu_prep();
  229. /* flush cache back to ram */
  230. flush_cache_all();
  231. s3c_pm_check_store();
  232. /* send the cpu to sleep... */
  233. s3c_pm_arch_stop_clocks();
  234. /* this will also act as our return point from when
  235. * we resume as it saves its own register state and restores it
  236. * during the resume. */
  237. ret = cpu_suspend(0, pm_cpu_sleep);
  238. if (ret)
  239. return ret;
  240. /* restore the system state */
  241. s3c_pm_restore_core();
  242. s3c_pm_restore_uarts();
  243. if (!of_have_populated_dt()) {
  244. samsung_pm_restore_gpios();
  245. s3c_pm_restored_gpios();
  246. }
  247. s3c_pm_debug_init();
  248. /* check what irq (if any) restored the system */
  249. s3c_pm_arch_show_resume_irqs();
  250. S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
  251. /* LEDs should now be 1110 */
  252. s3c_pm_debug_smdkled(1 << 1, 0);
  253. s3c_pm_check_restore();
  254. /* ok, let's return from sleep */
  255. S3C_PMDBG("S3C PM Resume (post-restore)\n");
  256. return 0;
  257. }
  258. static int s3c_pm_prepare(void)
  259. {
  260. /* prepare check area if configured */
  261. s3c_pm_check_prepare();
  262. return 0;
  263. }
  264. static void s3c_pm_finish(void)
  265. {
  266. s3c_pm_check_cleanup();
  267. }
  268. static const struct platform_suspend_ops s3c_pm_ops = {
  269. .enter = s3c_pm_enter,
  270. .prepare = s3c_pm_prepare,
  271. .finish = s3c_pm_finish,
  272. .valid = suspend_valid_only_mem,
  273. };
  274. /* s3c_pm_init
  275. *
  276. * Attach the power management functions. This should be called
  277. * from the board specific initialisation if the board supports
  278. * it.
  279. */
  280. int __init s3c_pm_init(void)
  281. {
  282. printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
  283. suspend_set_ops(&s3c_pm_ops);
  284. return 0;
  285. }