power.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * linux/arch/parisc/kernel/power.c
  3. * HP PARISC soft power switch support driver
  4. *
  5. * Copyright (c) 2001-2005 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. *
  33. * HINT:
  34. * Support of the soft power switch button may be enabled or disabled at
  35. * runtime through the "/proc/sys/kernel/power" procfs entry.
  36. */
  37. #include <linux/config.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/kernel.h>
  41. #include <linux/string.h>
  42. #include <linux/notifier.h>
  43. #include <linux/reboot.h>
  44. #include <linux/sched.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/workqueue.h>
  47. #include <asm/pdc.h>
  48. #include <asm/io.h>
  49. #include <asm/led.h>
  50. #include <asm/uaccess.h>
  51. #ifdef DEBUG
  52. # define DPRINTK(x...) printk(x)
  53. #else
  54. # define DPRINTK(x...)
  55. #endif
  56. /* filename in /proc which can be used to enable/disable the power switch */
  57. #define SYSCTL_FILENAME "sys/kernel/power"
  58. #define DIAG_CODE(code) (0x14000000 + ((code)<<5))
  59. /* this will go to processor.h or any other place... */
  60. /* taken from PCXL ERS page 82 */
  61. #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
  62. (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
  63. #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
  64. #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
  65. #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
  66. #define __getDIAG(dr) ( { \
  67. register unsigned long __res asm("r28");\
  68. __asm__ __volatile__ ( \
  69. ".word %1\n nop\n" : "=&r" (__res) : "i" (MFCPU_T(dr,28)) \
  70. ); \
  71. __res; \
  72. } )
  73. static void deferred_poweroff(void *dummy)
  74. {
  75. extern int cad_pid; /* from kernel/sys.c */
  76. if (kill_proc(cad_pid, SIGINT, 1)) {
  77. /* just in case killing init process failed */
  78. machine_power_off();
  79. }
  80. }
  81. /*
  82. * This function gets called from interrupt context.
  83. * As it's called within an interrupt, it wouldn't sync if we don't
  84. * use schedule_work().
  85. */
  86. static DECLARE_WORK(poweroff_work, deferred_poweroff, NULL);
  87. static void poweroff(void)
  88. {
  89. static int powering_off __read_mostly;
  90. if (powering_off)
  91. return;
  92. powering_off++;
  93. schedule_work(&poweroff_work);
  94. }
  95. /* local time-counter for shutdown */
  96. static int shutdown_timer __read_mostly;
  97. /* check, give feedback and start shutdown after one second */
  98. static void process_shutdown(void)
  99. {
  100. if (shutdown_timer == 0)
  101. DPRINTK(KERN_INFO "Shutdown requested...\n");
  102. shutdown_timer++;
  103. /* wait until the button was pressed for 1 second */
  104. if (shutdown_timer == HZ) {
  105. #if defined (DEBUG) || defined(CONFIG_CHASSIS_LCD_LED)
  106. static char msg[] = "Shutting down...";
  107. #endif
  108. DPRINTK(KERN_INFO "%s\n", msg);
  109. lcd_print(msg);
  110. poweroff();
  111. }
  112. }
  113. /* main power switch tasklet struct (scheduled from time.c) */
  114. DECLARE_TASKLET_DISABLED(power_tasklet, NULL, 0);
  115. /* soft power switch enabled/disabled */
  116. int pwrsw_enabled __read_mostly = 1;
  117. /*
  118. * On gecko style machines (e.g. 712/xx and 715/xx)
  119. * the power switch status is stored in Bit 0 ("the highest bit")
  120. * of CPU diagnose register 25.
  121. *
  122. */
  123. static void gecko_tasklet_func(unsigned long unused)
  124. {
  125. if (unlikely(!pwrsw_enabled))
  126. return;
  127. if (__getDIAG(25) & 0x80000000) {
  128. /* power switch button not pressed or released again */
  129. /* Warning: Some machines do never reset this DIAG flag! */
  130. shutdown_timer = 0;
  131. } else {
  132. process_shutdown();
  133. }
  134. }
  135. /*
  136. * Check the power switch status which is read from the
  137. * real I/O location at soft_power_reg.
  138. * Bit 31 ("the lowest bit) is the status of the power switch.
  139. */
  140. static void polling_tasklet_func(unsigned long soft_power_reg)
  141. {
  142. unsigned long current_status;
  143. if (unlikely(!pwrsw_enabled))
  144. return;
  145. current_status = gsc_readl(soft_power_reg);
  146. if (current_status & 0x1) {
  147. /* power switch button not pressed */
  148. shutdown_timer = 0;
  149. } else {
  150. process_shutdown();
  151. }
  152. }
  153. /*
  154. * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
  155. */
  156. #if 0
  157. static void powerfail_interrupt(int code, void *x, struct pt_regs *regs)
  158. {
  159. printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
  160. poweroff();
  161. }
  162. #endif
  163. /* parisc_panic_event() is called by the panic handler.
  164. * As soon as a panic occurs, our tasklets above will not be
  165. * executed any longer. This function then re-enables the
  166. * soft-power switch and allows the user to switch off the system
  167. */
  168. static int parisc_panic_event(struct notifier_block *this,
  169. unsigned long event, void *ptr)
  170. {
  171. /* re-enable the soft-power switch */
  172. pdc_soft_power_button(0);
  173. return NOTIFY_DONE;
  174. }
  175. static struct notifier_block parisc_panic_block = {
  176. .notifier_call = parisc_panic_event,
  177. .priority = INT_MAX,
  178. };
  179. static int __init power_init(void)
  180. {
  181. unsigned long ret;
  182. unsigned long soft_power_reg = 0;
  183. #if 0
  184. request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
  185. 0, "powerfail", NULL);
  186. #endif
  187. /* enable the soft power switch if possible */
  188. ret = pdc_soft_power_info(&soft_power_reg);
  189. if (ret == PDC_OK)
  190. ret = pdc_soft_power_button(1);
  191. if (ret != PDC_OK)
  192. soft_power_reg = -1UL;
  193. switch (soft_power_reg) {
  194. case 0: printk(KERN_INFO "Gecko-style soft power switch enabled.\n");
  195. power_tasklet.func = gecko_tasklet_func;
  196. break;
  197. case -1UL: printk(KERN_INFO "Soft power switch support not available.\n");
  198. return -ENODEV;
  199. default: printk(KERN_INFO "Soft power switch enabled, polling @ 0x%08lx.\n",
  200. soft_power_reg);
  201. power_tasklet.data = soft_power_reg;
  202. power_tasklet.func = polling_tasklet_func;
  203. }
  204. /* Register a call for panic conditions. */
  205. atomic_notifier_chain_register(&panic_notifier_list,
  206. &parisc_panic_block);
  207. tasklet_enable(&power_tasklet);
  208. return 0;
  209. }
  210. static void __exit power_exit(void)
  211. {
  212. if (!power_tasklet.func)
  213. return;
  214. tasklet_disable(&power_tasklet);
  215. atomic_notifier_chain_unregister(&panic_notifier_list,
  216. &parisc_panic_block);
  217. power_tasklet.func = NULL;
  218. pdc_soft_power_button(0);
  219. }
  220. module_init(power_init);
  221. module_exit(power_exit);
  222. MODULE_AUTHOR("Helge Deller");
  223. MODULE_DESCRIPTION("Soft power switch driver");
  224. MODULE_LICENSE("Dual BSD/GPL");