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