pm.c 4.1 KB

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