power.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/drivers/parisc/power.c
  3. * HP PARISC soft power switch support driver
  4. *
  5. * Copyright (c) 2001-2007 Helge Deller <deller@gmx.de>
  6. * All rights reserved.
  7. *
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL").
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  25. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. *
  31. *
  32. * HINT:
  33. * Support of the soft power switch button may be enabled or disabled at
  34. * runtime through the "/proc/sys/kernel/power" procfs entry.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/kernel.h>
  39. #include <linux/notifier.h>
  40. #include <linux/reboot.h>
  41. #include <linux/sched.h>
  42. #include <linux/kthread.h>
  43. #include <linux/pm.h>
  44. #include <asm/pdc.h>
  45. #include <asm/io.h>
  46. #include <asm/led.h>
  47. #define DRIVER_NAME "powersw"
  48. #define KTHREAD_NAME "kpowerswd"
  49. /* how often should the power button be polled ? */
  50. #define POWERSWITCH_POLL_PER_SEC 2
  51. /* how long does the power button needs to be down until we react ? */
  52. #define POWERSWITCH_DOWN_SEC 2
  53. /* assembly code to access special registers */
  54. /* taken from PCXL ERS page 82 */
  55. #define DIAG_CODE(code) (0x14000000 + ((code)<<5))
  56. #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
  57. (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
  58. #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
  59. #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
  60. #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
  61. #define __getDIAG(dr) ( { \
  62. register unsigned long __res asm("r28");\
  63. __asm__ __volatile__ ( \
  64. ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
  65. ); \
  66. __res; \
  67. } )
  68. /* local shutdown counter */
  69. static int shutdown_timer __read_mostly;
  70. /* check, give feedback and start shutdown after one second */
  71. static void process_shutdown(void)
  72. {
  73. if (shutdown_timer == 0)
  74. printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
  75. shutdown_timer++;
  76. /* wait until the button was pressed for 1 second */
  77. if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
  78. static const char msg[] = "Shutting down...";
  79. printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
  80. lcd_print(msg);
  81. /* send kill signal */
  82. if (kill_cad_pid(SIGINT, 1)) {
  83. /* just in case killing init process failed */
  84. if (pm_power_off)
  85. pm_power_off();
  86. }
  87. }
  88. }
  89. /* main power switch task struct */
  90. static struct task_struct *power_task;
  91. /* filename in /proc which can be used to enable/disable the power switch */
  92. #define SYSCTL_FILENAME "sys/kernel/power"
  93. /* soft power switch enabled/disabled */
  94. int pwrsw_enabled __read_mostly = 1;
  95. /* main kernel thread worker. It polls the button state */
  96. static int kpowerswd(void *param)
  97. {
  98. __set_current_state(TASK_RUNNING);
  99. do {
  100. int button_not_pressed;
  101. unsigned long soft_power_reg = (unsigned long) param;
  102. schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
  103. __set_current_state(TASK_RUNNING);
  104. if (unlikely(!pwrsw_enabled))
  105. continue;
  106. if (soft_power_reg) {
  107. /*
  108. * Non-Gecko-style machines:
  109. * Check the power switch status which is read from the
  110. * real I/O location at soft_power_reg.
  111. * Bit 31 ("the lowest bit) is the status of the power switch.
  112. * This bit is "1" if the button is NOT pressed.
  113. */
  114. button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
  115. } else {
  116. /*
  117. * On gecko style machines (e.g. 712/xx and 715/xx)
  118. * the power switch status is stored in Bit 0 ("the highest bit")
  119. * of CPU diagnose register 25.
  120. * Warning: Some machines never reset the DIAG flag, even if
  121. * the button has been released again.
  122. */
  123. button_not_pressed = (__getDIAG(25) & 0x80000000);
  124. }
  125. if (likely(button_not_pressed)) {
  126. if (unlikely(shutdown_timer && /* avoid writing if not necessary */
  127. shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
  128. shutdown_timer = 0;
  129. printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
  130. }
  131. } else
  132. process_shutdown();
  133. } while (!kthread_should_stop());
  134. return 0;
  135. }
  136. /*
  137. * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
  138. */
  139. #if 0
  140. static void powerfail_interrupt(int code, void *x)
  141. {
  142. printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
  143. poweroff();
  144. }
  145. #endif
  146. /* parisc_panic_event() is called by the panic handler.
  147. * As soon as a panic occurs, our tasklets above will not be
  148. * executed any longer. This function then re-enables the
  149. * soft-power switch and allows the user to switch off the system
  150. */
  151. static int parisc_panic_event(struct notifier_block *this,
  152. unsigned long event, void *ptr)
  153. {
  154. /* re-enable the soft-power switch */
  155. pdc_soft_power_button(0);
  156. return NOTIFY_DONE;
  157. }
  158. static struct notifier_block parisc_panic_block = {
  159. .notifier_call = parisc_panic_event,
  160. .priority = INT_MAX,
  161. };
  162. static int __init power_init(void)
  163. {
  164. unsigned long ret;
  165. unsigned long soft_power_reg;
  166. #if 0
  167. request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
  168. 0, "powerfail", NULL);
  169. #endif
  170. /* enable the soft power switch if possible */
  171. ret = pdc_soft_power_info(&soft_power_reg);
  172. if (ret == PDC_OK)
  173. ret = pdc_soft_power_button(1);
  174. if (ret != PDC_OK)
  175. soft_power_reg = -1UL;
  176. switch (soft_power_reg) {
  177. case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
  178. break;
  179. case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
  180. return -ENODEV;
  181. default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
  182. soft_power_reg);
  183. }
  184. power_task = kthread_run(kpowerswd, (void*)soft_power_reg, KTHREAD_NAME);
  185. if (IS_ERR(power_task)) {
  186. printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n");
  187. pdc_soft_power_button(0);
  188. return -EIO;
  189. }
  190. /* Register a call for panic conditions. */
  191. atomic_notifier_chain_register(&panic_notifier_list,
  192. &parisc_panic_block);
  193. return 0;
  194. }
  195. static void __exit power_exit(void)
  196. {
  197. kthread_stop(power_task);
  198. atomic_notifier_chain_unregister(&panic_notifier_list,
  199. &parisc_panic_block);
  200. pdc_soft_power_button(0);
  201. }
  202. arch_initcall(power_init);
  203. module_exit(power_exit);
  204. MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
  205. MODULE_DESCRIPTION("Soft power switch driver");
  206. MODULE_LICENSE("Dual BSD/GPL");