pm.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * loongson-specific suspend support
  4. *
  5. * Copyright (C) 2009 Lemote Inc.
  6. * Author: Wu Zhangjin <[email protected]>
  7. */
  8. #include <linux/suspend.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pm.h>
  11. #include <asm/i8259.h>
  12. #include <asm/mipsregs.h>
  13. #include <loongson.h>
  14. static unsigned int __maybe_unused cached_master_mask; /* i8259A */
  15. static unsigned int __maybe_unused cached_slave_mask;
  16. static unsigned int __maybe_unused cached_bonito_irq_mask; /* bonito */
  17. void arch_suspend_disable_irqs(void)
  18. {
  19. /* disable all mips events */
  20. local_irq_disable();
  21. #ifdef CONFIG_I8259
  22. /* disable all events of i8259A */
  23. cached_slave_mask = inb(PIC_SLAVE_IMR);
  24. cached_master_mask = inb(PIC_MASTER_IMR);
  25. outb(0xff, PIC_SLAVE_IMR);
  26. inb(PIC_SLAVE_IMR);
  27. outb(0xff, PIC_MASTER_IMR);
  28. inb(PIC_MASTER_IMR);
  29. #endif
  30. /* disable all events of bonito */
  31. cached_bonito_irq_mask = LOONGSON_INTEN;
  32. LOONGSON_INTENCLR = 0xffff;
  33. (void)LOONGSON_INTENCLR;
  34. }
  35. void arch_suspend_enable_irqs(void)
  36. {
  37. /* enable all mips events */
  38. local_irq_enable();
  39. #ifdef CONFIG_I8259
  40. /* only enable the cached events of i8259A */
  41. outb(cached_slave_mask, PIC_SLAVE_IMR);
  42. outb(cached_master_mask, PIC_MASTER_IMR);
  43. #endif
  44. /* enable all cached events of bonito */
  45. LOONGSON_INTENSET = cached_bonito_irq_mask;
  46. (void)LOONGSON_INTENSET;
  47. }
  48. /*
  49. * Setup the board-specific events for waking up loongson from wait mode
  50. */
  51. void __weak setup_wakeup_events(void)
  52. {
  53. }
  54. /*
  55. * Check wakeup events
  56. */
  57. int __weak wakeup_loongson(void)
  58. {
  59. return 1;
  60. }
  61. /*
  62. * If the events are really what we want to wakeup the CPU, wake it up
  63. * otherwise put the CPU asleep again.
  64. */
  65. static void wait_for_wakeup_events(void)
  66. {
  67. while (!wakeup_loongson())
  68. writel(readl(LOONGSON_CHIPCFG) & ~0x7, LOONGSON_CHIPCFG);
  69. }
  70. /*
  71. * Stop all perf counters
  72. *
  73. * $24 is the control register of Loongson perf counter
  74. */
  75. static inline void stop_perf_counters(void)
  76. {
  77. __write_64bit_c0_register($24, 0, 0);
  78. }
  79. static void loongson_suspend_enter(void)
  80. {
  81. unsigned int cached_cpu_freq;
  82. /* setup wakeup events via enabling the IRQs */
  83. setup_wakeup_events();
  84. stop_perf_counters();
  85. cached_cpu_freq = readl(LOONGSON_CHIPCFG);
  86. /* Put CPU into wait mode */
  87. writel(readl(LOONGSON_CHIPCFG) & ~0x7, LOONGSON_CHIPCFG);
  88. /* wait for the given events to wakeup cpu from wait mode */
  89. wait_for_wakeup_events();
  90. writel(cached_cpu_freq, LOONGSON_CHIPCFG);
  91. mmiowb();
  92. }
  93. void __weak mach_suspend(void)
  94. {
  95. }
  96. void __weak mach_resume(void)
  97. {
  98. }
  99. static int loongson_pm_enter(suspend_state_t state)
  100. {
  101. mach_suspend();
  102. /* processor specific suspend */
  103. loongson_suspend_enter();
  104. mach_resume();
  105. return 0;
  106. }
  107. static int loongson_pm_valid_state(suspend_state_t state)
  108. {
  109. switch (state) {
  110. case PM_SUSPEND_ON:
  111. case PM_SUSPEND_STANDBY:
  112. case PM_SUSPEND_MEM:
  113. return 1;
  114. default:
  115. return 0;
  116. }
  117. }
  118. static const struct platform_suspend_ops loongson_pm_ops = {
  119. .valid = loongson_pm_valid_state,
  120. .enter = loongson_pm_enter,
  121. };
  122. static int __init loongson_pm_init(void)
  123. {
  124. suspend_set_ops(&loongson_pm_ops);
  125. return 0;
  126. }
  127. arch_initcall(loongson_pm_init);