blob: ebaf6867b4570549411cac12709fbe9c0cc86670 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/drivers/parisc/power.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * HP PARISC soft power switch support driver
4 *
Helge Deller6e16d942007-01-07 16:07:48 +01005 * Copyright (c) 2001-2007 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/notifier.h>
41#include <linux/reboot.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010042#include <linux/sched/signal.h>
Helge Deller6e16d942007-01-07 16:07:48 +010043#include <linux/kthread.h>
Kyle McMartin5c04ec72007-05-25 19:19:35 -040044#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/pdc.h>
47#include <asm/io.h>
48#include <asm/led.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Helge Deller6e16d942007-01-07 16:07:48 +010050#define DRIVER_NAME "powersw"
51#define KTHREAD_NAME "kpowerswd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Helge Deller6e16d942007-01-07 16:07:48 +010053/* how often should the power button be polled ? */
54#define POWERSWITCH_POLL_PER_SEC 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Helge Deller6e16d942007-01-07 16:07:48 +010056/* how long does the power button needs to be down until we react ? */
57#define POWERSWITCH_DOWN_SEC 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Helge Deller6e16d942007-01-07 16:07:48 +010059/* assembly code to access special registers */
60/* taken from PCXL ERS page 82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define DIAG_CODE(code) (0x14000000 + ((code)<<5))
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define MFCPU_X(rDiagReg, t_ch, t_th, code) \
64 (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
65
66#define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
67#define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
68#define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
69
70#define __getDIAG(dr) ( { \
71 register unsigned long __res asm("r28");\
72 __asm__ __volatile__ ( \
Helge Deller6e16d942007-01-07 16:07:48 +010073 ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ); \
75 __res; \
76} )
77
Helge Deller6e16d942007-01-07 16:07:48 +010078/* local shutdown counter */
Helge Deller8039de12006-01-10 20:35:03 -050079static int shutdown_timer __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* check, give feedback and start shutdown after one second */
82static void process_shutdown(void)
83{
84 if (shutdown_timer == 0)
Helge Deller6e16d942007-01-07 16:07:48 +010085 printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 shutdown_timer++;
88
89 /* wait until the button was pressed for 1 second */
Helge Deller6e16d942007-01-07 16:07:48 +010090 if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
91 static const char msg[] = "Shutting down...";
92 printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 lcd_print(msg);
Helge Deller6e16d942007-01-07 16:07:48 +010094
95 /* send kill signal */
96 if (kill_cad_pid(SIGINT, 1)) {
97 /* just in case killing init process failed */
Helge Dellerc14b3022018-03-24 21:00:14 +010098 machine_power_off();
Helge Deller6e16d942007-01-07 16:07:48 +010099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101}
102
103
Helge Deller6e16d942007-01-07 16:07:48 +0100104/* main power switch task struct */
105static struct task_struct *power_task;
106
107/* filename in /proc which can be used to enable/disable the power switch */
108#define SYSCTL_FILENAME "sys/kernel/power"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* soft power switch enabled/disabled */
Helge Deller8039de12006-01-10 20:35:03 -0500111int pwrsw_enabled __read_mostly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Helge Deller6e16d942007-01-07 16:07:48 +0100113/* main kernel thread worker. It polls the button state */
114static int kpowerswd(void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Helge Deller6e16d942007-01-07 16:07:48 +0100116 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Helge Deller6e16d942007-01-07 16:07:48 +0100118 do {
119 int button_not_pressed;
120 unsigned long soft_power_reg = (unsigned long) param;
121
122 schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
Helge Deller6e16d942007-01-07 16:07:48 +0100123
124 if (unlikely(!pwrsw_enabled))
125 continue;
126
127 if (soft_power_reg) {
128 /*
129 * Non-Gecko-style machines:
130 * Check the power switch status which is read from the
131 * real I/O location at soft_power_reg.
132 * Bit 31 ("the lowest bit) is the status of the power switch.
133 * This bit is "1" if the button is NOT pressed.
134 */
135 button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
136 } else {
137 /*
138 * On gecko style machines (e.g. 712/xx and 715/xx)
139 * the power switch status is stored in Bit 0 ("the highest bit")
140 * of CPU diagnose register 25.
141 * Warning: Some machines never reset the DIAG flag, even if
142 * the button has been released again.
143 */
144 button_not_pressed = (__getDIAG(25) & 0x80000000);
145 }
146
147 if (likely(button_not_pressed)) {
148 if (unlikely(shutdown_timer && /* avoid writing if not necessary */
149 shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
150 shutdown_timer = 0;
151 printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
152 }
153 } else
154 process_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156
Helge Deller6e16d942007-01-07 16:07:48 +0100157 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Helge Deller6e16d942007-01-07 16:07:48 +0100159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162
163/*
164 * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
165 */
166#if 0
David Howells7d12e782006-10-05 14:55:46 +0100167static void powerfail_interrupt(int code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
170 poweroff();
171}
172#endif
173
174
175
176
177/* parisc_panic_event() is called by the panic handler.
178 * As soon as a panic occurs, our tasklets above will not be
179 * executed any longer. This function then re-enables the
180 * soft-power switch and allows the user to switch off the system
181 */
182static int parisc_panic_event(struct notifier_block *this,
183 unsigned long event, void *ptr)
184{
185 /* re-enable the soft-power switch */
186 pdc_soft_power_button(0);
187 return NOTIFY_DONE;
188}
189
190static struct notifier_block parisc_panic_block = {
191 .notifier_call = parisc_panic_event,
192 .priority = INT_MAX,
193};
194
195
196static int __init power_init(void)
197{
198 unsigned long ret;
Helge Deller6e16d942007-01-07 16:07:48 +0100199 unsigned long soft_power_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201#if 0
202 request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
203 0, "powerfail", NULL);
204#endif
205
206 /* enable the soft power switch if possible */
207 ret = pdc_soft_power_info(&soft_power_reg);
208 if (ret == PDC_OK)
209 ret = pdc_soft_power_button(1);
210 if (ret != PDC_OK)
211 soft_power_reg = -1UL;
212
213 switch (soft_power_reg) {
Helge Deller6e16d942007-01-07 16:07:48 +0100214 case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
Helge Deller6e16d942007-01-07 16:07:48 +0100217 case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -ENODEV;
219
Helge Deller6e16d942007-01-07 16:07:48 +0100220 default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 soft_power_reg);
Helge Deller6e16d942007-01-07 16:07:48 +0100222 }
223
224 power_task = kthread_run(kpowerswd, (void*)soft_power_reg, KTHREAD_NAME);
225 if (IS_ERR(power_task)) {
226 printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n");
227 pdc_soft_power_button(0);
228 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
231 /* Register a call for panic conditions. */
Alan Sterne041c682006-03-27 01:16:30 -0800232 atomic_notifier_chain_register(&panic_notifier_list,
233 &parisc_panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
238static void __exit power_exit(void)
239{
Helge Deller6e16d942007-01-07 16:07:48 +0100240 kthread_stop(power_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Alan Sterne041c682006-03-27 01:16:30 -0800242 atomic_notifier_chain_unregister(&panic_notifier_list,
243 &parisc_panic_block);
Helge Deller6e16d942007-01-07 16:07:48 +0100244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 pdc_soft_power_button(0);
246}
247
Helge Deller6e16d942007-01-07 16:07:48 +0100248arch_initcall(power_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249module_exit(power_exit);
250
251
Helge Deller6e16d942007-01-07 16:07:48 +0100252MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253MODULE_DESCRIPTION("Soft power switch driver");
254MODULE_LICENSE("Dual BSD/GPL");