pm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright 2008 Openmoko, Inc.
  4. // Copyright 2004-2008 Simtec Electronics
  5. // Ben Dooks <[email protected]>
  6. // http://armlinux.simtec.co.uk/
  7. //
  8. // S3C common power management (suspend to ram) support.
  9. #include <linux/init.h>
  10. #include <linux/suspend.h>
  11. #include <linux/errno.h>
  12. #include <linux/delay.h>
  13. #include <linux/of.h>
  14. #include <linux/serial_s3c.h>
  15. #include <linux/io.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/suspend.h>
  18. #include "map.h"
  19. #include "regs-clock.h"
  20. #include "regs-irq.h"
  21. #include "irqs.h"
  22. #include <asm/irq.h>
  23. #include "cpu.h"
  24. #include "pm.h"
  25. #include "pm-core.h"
  26. /* for external use */
  27. unsigned long s3c_pm_flags;
  28. /* The IRQ ext-int code goes here, it is too small to currently bother
  29. * with its own file. */
  30. unsigned long s3c_irqwake_intmask = 0xffffffffL;
  31. unsigned long s3c_irqwake_eintmask = 0xffffffffL;
  32. int s3c_irqext_wake(struct irq_data *data, unsigned int state)
  33. {
  34. unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
  35. if (!(s3c_irqwake_eintallow & bit))
  36. return -ENOENT;
  37. printk(KERN_INFO "wake %s for irq %d\n",
  38. state ? "enabled" : "disabled", data->irq);
  39. if (!state)
  40. s3c_irqwake_eintmask |= bit;
  41. else
  42. s3c_irqwake_eintmask &= ~bit;
  43. return 0;
  44. }
  45. void (*pm_cpu_prep)(void);
  46. int (*pm_cpu_sleep)(unsigned long);
  47. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  48. /* s3c_pm_enter
  49. *
  50. * central control for sleep/resume process
  51. */
  52. static int s3c_pm_enter(suspend_state_t state)
  53. {
  54. int ret;
  55. /* ensure the debug is initialised (if enabled) */
  56. s3c_pm_debug_init_uart();
  57. S3C_PMDBG("%s(%d)\n", __func__, state);
  58. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  59. printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
  60. return -EINVAL;
  61. }
  62. /* check if we have anything to wake-up with... bad things seem
  63. * to happen if you suspend with no wakeup (system will often
  64. * require a full power-cycle)
  65. */
  66. if (!of_have_populated_dt() &&
  67. !any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  68. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  69. printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
  70. printk(KERN_ERR "%s: Aborting sleep\n", __func__);
  71. return -EINVAL;
  72. }
  73. /* save all necessary core registers not covered by the drivers */
  74. if (!of_have_populated_dt()) {
  75. samsung_pm_save_gpios();
  76. samsung_pm_saved_gpios();
  77. }
  78. s3c_pm_save_uarts(soc_is_s3c2410());
  79. s3c_pm_save_core();
  80. /* set the irq configuration for wake */
  81. s3c_pm_configure_extint();
  82. S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  83. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  84. s3c_pm_arch_prepare_irqs();
  85. /* call cpu specific preparation */
  86. pm_cpu_prep();
  87. /* flush cache back to ram */
  88. flush_cache_all();
  89. s3c_pm_check_store();
  90. /* send the cpu to sleep... */
  91. s3c_pm_arch_stop_clocks();
  92. /* this will also act as our return point from when
  93. * we resume as it saves its own register state and restores it
  94. * during the resume. */
  95. ret = cpu_suspend(0, pm_cpu_sleep);
  96. if (ret)
  97. return ret;
  98. /* restore the system state */
  99. s3c_pm_restore_core();
  100. s3c_pm_restore_uarts(soc_is_s3c2410());
  101. if (!of_have_populated_dt()) {
  102. samsung_pm_restore_gpios();
  103. s3c_pm_restored_gpios();
  104. }
  105. s3c_pm_debug_init_uart();
  106. /* check what irq (if any) restored the system */
  107. s3c_pm_arch_show_resume_irqs();
  108. S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
  109. /* LEDs should now be 1110 */
  110. s3c_pm_debug_smdkled(1 << 1, 0);
  111. s3c_pm_check_restore();
  112. /* ok, let's return from sleep */
  113. S3C_PMDBG("S3C PM Resume (post-restore)\n");
  114. return 0;
  115. }
  116. static int s3c_pm_prepare(void)
  117. {
  118. /* prepare check area if configured */
  119. s3c_pm_check_prepare();
  120. return 0;
  121. }
  122. static void s3c_pm_finish(void)
  123. {
  124. s3c_pm_check_cleanup();
  125. }
  126. static const struct platform_suspend_ops s3c_pm_ops = {
  127. .enter = s3c_pm_enter,
  128. .prepare = s3c_pm_prepare,
  129. .finish = s3c_pm_finish,
  130. .valid = suspend_valid_only_mem,
  131. };
  132. /* s3c_pm_init
  133. *
  134. * Attach the power management functions. This should be called
  135. * from the board specific initialisation if the board supports
  136. * it.
  137. */
  138. int __init s3c_pm_init(void)
  139. {
  140. printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
  141. suspend_set_ops(&s3c_pm_ops);
  142. return 0;
  143. }