2 * linux/arch/parisc/kernel/power.c
3 * HP PARISC soft power switch support driver
5 * Copyright (c) 2001-2005 Helge Deller <deller@gmx.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL").
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
34 * Support of the soft power switch button may be enabled or disabled at
35 * runtime through the "/proc/sys/kernel/power" procfs entry.
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>
51 #include <asm/uaccess.h>
55 # define DPRINTK(x...) printk(x)
57 # define DPRINTK(x...)
61 /* filename in /proc which can be used to enable/disable the power switch */
62 #define SYSCTL_FILENAME "sys/kernel/power"
65 #define DIAG_CODE(code) (0x14000000 + ((code)<<5))
67 /* this will go to processor.h or any other place... */
68 /* taken from PCXL ERS page 82 */
69 #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
70 (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
72 #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
73 #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
74 #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
76 #define __getDIAG(dr) ( { \
77 register unsigned long __res asm("r28");\
78 __asm__ __volatile__ ( \
79 ".word %1\n nop\n" : "=&r" (__res) : "i" (MFCPU_T(dr,28)) \
85 static void deferred_poweroff(void *dummy
)
87 extern int cad_pid
; /* from kernel/sys.c */
88 if (kill_proc(cad_pid
, SIGINT
, 1)) {
89 /* just in case killing init process failed */
95 * This function gets called from interrupt context.
96 * As it's called within an interrupt, it wouldn't sync if we don't
97 * use schedule_work().
100 static DECLARE_WORK(poweroff_work
, deferred_poweroff
, NULL
);
102 static void poweroff(void)
104 static int powering_off __read_mostly
;
110 schedule_work(&poweroff_work
);
114 /* local time-counter for shutdown */
115 static int shutdown_timer __read_mostly
;
117 /* check, give feedback and start shutdown after one second */
118 static void process_shutdown(void)
120 if (shutdown_timer
== 0)
121 DPRINTK(KERN_INFO
"Shutdown requested...\n");
125 /* wait until the button was pressed for 1 second */
126 if (shutdown_timer
== HZ
) {
127 #if defined (DEBUG) || defined(CONFIG_CHASSIS_LCD_LED)
128 static char msg
[] = "Shutting down...";
130 DPRINTK(KERN_INFO
"%s\n", msg
);
137 /* main power switch tasklet struct (scheduled from time.c) */
138 DECLARE_TASKLET_DISABLED(power_tasklet
, NULL
, 0);
140 /* soft power switch enabled/disabled */
141 int pwrsw_enabled __read_mostly
= 1;
144 * On gecko style machines (e.g. 712/xx and 715/xx)
145 * the power switch status is stored in Bit 0 ("the highest bit")
146 * of CPU diagnose register 25.
149 static void gecko_tasklet_func(unsigned long unused
)
151 if (unlikely(!pwrsw_enabled
))
154 if (__getDIAG(25) & 0x80000000) {
155 /* power switch button not pressed or released again */
156 /* Warning: Some machines do never reset this DIAG flag! */
166 * Check the power switch status which is read from the
167 * real I/O location at soft_power_reg.
168 * Bit 31 ("the lowest bit) is the status of the power switch.
171 static void polling_tasklet_func(unsigned long soft_power_reg
)
173 unsigned long current_status
;
175 if (unlikely(!pwrsw_enabled
))
178 current_status
= gsc_readl(soft_power_reg
);
179 if (current_status
& 0x1) {
180 /* power switch button not pressed */
189 * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
192 static void powerfail_interrupt(int code
, void *x
, struct pt_regs
*regs
)
194 printk(KERN_CRIT
"POWERFAIL INTERRUPTION !\n");
202 /* parisc_panic_event() is called by the panic handler.
203 * As soon as a panic occurs, our tasklets above will not be
204 * executed any longer. This function then re-enables the
205 * soft-power switch and allows the user to switch off the system
207 static int parisc_panic_event(struct notifier_block
*this,
208 unsigned long event
, void *ptr
)
210 /* re-enable the soft-power switch */
211 pdc_soft_power_button(0);
215 static struct notifier_block parisc_panic_block
= {
216 .notifier_call
= parisc_panic_event
,
221 static int __init
power_init(void)
224 unsigned long soft_power_reg
= 0;
227 request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION
)+2, &powerfail_interrupt
,
228 0, "powerfail", NULL
);
231 /* enable the soft power switch if possible */
232 ret
= pdc_soft_power_info(&soft_power_reg
);
234 ret
= pdc_soft_power_button(1);
236 soft_power_reg
= -1UL;
238 switch (soft_power_reg
) {
239 case 0: printk(KERN_INFO
"Gecko-style soft power switch enabled.\n");
240 power_tasklet
.func
= gecko_tasklet_func
;
243 case -1UL: printk(KERN_INFO
"Soft power switch support not available.\n");
246 default: printk(KERN_INFO
"Soft power switch enabled, polling @ 0x%08lx.\n",
248 power_tasklet
.data
= soft_power_reg
;
249 power_tasklet
.func
= polling_tasklet_func
;
252 /* Register a call for panic conditions. */
253 atomic_notifier_chain_register(&panic_notifier_list
,
254 &parisc_panic_block
);
256 tasklet_enable(&power_tasklet
);
261 static void __exit
power_exit(void)
263 if (!power_tasklet
.func
)
266 tasklet_disable(&power_tasklet
);
267 atomic_notifier_chain_unregister(&panic_notifier_list
,
268 &parisc_panic_block
);
269 power_tasklet
.func
= NULL
;
270 pdc_soft_power_button(0);
273 module_init(power_init
);
274 module_exit(power_exit
);
277 MODULE_AUTHOR("Helge Deller");
278 MODULE_DESCRIPTION("Soft power switch driver");
279 MODULE_LICENSE("Dual BSD/GPL");