power.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * HP PARISC soft power switch driver
  4. *
  5. * Copyright (c) 2001-2023 Helge Deller <deller@gmx.de>
  6. *
  7. * HINT:
  8. * Support of the soft power switch button may be enabled or disabled at
  9. * runtime through the "/proc/sys/kernel/power" procfs entry.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/panic_notifier.h>
  15. #include <linux/reboot.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/kthread.h>
  18. #include <linux/pm.h>
  19. #include <asm/pdc.h>
  20. #include <asm/io.h>
  21. #include <asm/led.h>
  22. #define DRIVER_NAME "powersw"
  23. #define KTHREAD_NAME "kpowerswd"
  24. /* how often should the power button be polled ? */
  25. #define POWERSWITCH_POLL_PER_SEC 2
  26. /* how long does the power button needs to be down until we react ? */
  27. #define POWERSWITCH_DOWN_SEC 2
  28. /* assembly code to access special registers */
  29. /* taken from PCXL ERS page 82 */
  30. #define DIAG_CODE(code) (0x14000000 + ((code)<<5))
  31. #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
  32. (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
  33. #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
  34. #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
  35. #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
  36. #define __getDIAG(dr) ( { \
  37. register unsigned long __res asm("r28");\
  38. __asm__ __volatile__ ( \
  39. ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
  40. ); \
  41. __res; \
  42. } )
  43. /* local shutdown counter */
  44. static int shutdown_timer __read_mostly;
  45. /* check, give feedback and start shutdown after one second */
  46. static void process_shutdown(void)
  47. {
  48. if (shutdown_timer == 0)
  49. printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
  50. shutdown_timer++;
  51. /* wait until the button was pressed for 1 second */
  52. if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
  53. static const char msg[] = "Shutting down...";
  54. printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
  55. lcd_print(msg);
  56. /* send kill signal */
  57. if (kill_cad_pid(SIGINT, 1)) {
  58. /* just in case killing init process failed */
  59. machine_power_off();
  60. }
  61. }
  62. }
  63. /* main power switch task struct */
  64. static struct task_struct *power_task;
  65. /* filename in /proc which can be used to enable/disable the power switch */
  66. #define SYSCTL_FILENAME "sys/kernel/power"
  67. /* soft power switch enabled/disabled */
  68. int pwrsw_enabled __read_mostly = 1;
  69. /* main kernel thread worker. It polls the button state */
  70. static int kpowerswd(void *param)
  71. {
  72. __set_current_state(TASK_RUNNING);
  73. do {
  74. int button_not_pressed;
  75. unsigned long soft_power_reg = (unsigned long) param;
  76. schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
  77. if (unlikely(!pwrsw_enabled))
  78. continue;
  79. if (soft_power_reg) {
  80. /*
  81. * Non-Gecko-style machines:
  82. * Check the power switch status which is read from the
  83. * real I/O location at soft_power_reg.
  84. * Bit 31 ("the lowest bit) is the status of the power switch.
  85. * This bit is "1" if the button is NOT pressed.
  86. */
  87. button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
  88. } else {
  89. /*
  90. * On gecko style machines (e.g. 712/xx and 715/xx)
  91. * the power switch status is stored in Bit 0 ("the highest bit")
  92. * of CPU diagnose register 25.
  93. * Warning: Some machines never reset the DIAG flag, even if
  94. * the button has been released again.
  95. */
  96. button_not_pressed = (__getDIAG(25) & 0x80000000);
  97. }
  98. if (likely(button_not_pressed)) {
  99. if (unlikely(shutdown_timer && /* avoid writing if not necessary */
  100. shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
  101. shutdown_timer = 0;
  102. printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
  103. }
  104. } else
  105. process_shutdown();
  106. } while (!kthread_should_stop());
  107. return 0;
  108. }
  109. /*
  110. * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
  111. */
  112. #if 0
  113. static void powerfail_interrupt(int code, void *x)
  114. {
  115. printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
  116. poweroff();
  117. }
  118. #endif
  119. /*
  120. * parisc_panic_event() is called by the panic handler.
  121. *
  122. * As soon as a panic occurs, our tasklets above will not
  123. * be executed any longer. This function then re-enables
  124. * the soft-power switch and allows the user to switch off
  125. * the system. We rely in pdc_soft_power_button_panic()
  126. * since this version spin_trylocks (instead of regular
  127. * spinlock), preventing deadlocks on panic path.
  128. */
  129. static int parisc_panic_event(struct notifier_block *this,
  130. unsigned long event, void *ptr)
  131. {
  132. /* re-enable the soft-power switch */
  133. pdc_soft_power_button_panic(0);
  134. return NOTIFY_DONE;
  135. }
  136. static struct notifier_block parisc_panic_block = {
  137. .notifier_call = parisc_panic_event,
  138. .priority = INT_MAX,
  139. };
  140. /* qemu soft power-off function */
  141. static int qemu_power_off(struct sys_off_data *data)
  142. {
  143. /* this turns the system off via SeaBIOS */
  144. gsc_writel(0, (unsigned long) data->cb_data);
  145. pdc_soft_power_button(1);
  146. return NOTIFY_DONE;
  147. }
  148. static int __init power_init(void)
  149. {
  150. unsigned long ret;
  151. unsigned long soft_power_reg;
  152. #if 0
  153. request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
  154. 0, "powerfail", NULL);
  155. #endif
  156. /* enable the soft power switch if possible */
  157. ret = pdc_soft_power_info(&soft_power_reg);
  158. if (ret == PDC_OK)
  159. ret = pdc_soft_power_button(1);
  160. if (ret != PDC_OK)
  161. soft_power_reg = -1UL;
  162. switch (soft_power_reg) {
  163. case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
  164. break;
  165. case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
  166. return -ENODEV;
  167. default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
  168. soft_power_reg);
  169. }
  170. power_task = NULL;
  171. if (running_on_qemu && soft_power_reg)
  172. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
  173. qemu_power_off, (void *)soft_power_reg);
  174. if (!running_on_qemu || soft_power_reg)
  175. power_task = kthread_run(kpowerswd, (void*)soft_power_reg,
  176. KTHREAD_NAME);
  177. if (IS_ERR(power_task)) {
  178. printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n");
  179. pdc_soft_power_button(0);
  180. return -EIO;
  181. }
  182. /* Register a call for panic conditions. */
  183. atomic_notifier_chain_register(&panic_notifier_list,
  184. &parisc_panic_block);
  185. return 0;
  186. }
  187. static void __exit power_exit(void)
  188. {
  189. kthread_stop(power_task);
  190. atomic_notifier_chain_unregister(&panic_notifier_list,
  191. &parisc_panic_block);
  192. pdc_soft_power_button(0);
  193. }
  194. arch_initcall(power_init);
  195. module_exit(power_exit);
  196. MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
  197. MODULE_DESCRIPTION("Soft power switch driver");
  198. MODULE_LICENSE("Dual BSD/GPL");