1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/kernel.h>
40#include <linux/notifier.h>
41#include <linux/reboot.h>
42#include <linux/sched/signal.h>
43#include <linux/kthread.h>
44#include <linux/pm.h>
45
46#include <asm/pdc.h>
47#include <asm/io.h>
48#include <asm/led.h>
49
50#define DRIVER_NAME "powersw"
51#define KTHREAD_NAME "kpowerswd"
52
53
54#define POWERSWITCH_POLL_PER_SEC 2
55
56
57#define POWERSWITCH_DOWN_SEC 2
58
59
60
61#define DIAG_CODE(code) (0x14000000 + ((code)<<5))
62
63#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)
67#define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30)
68#define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0)
69
70#define __getDIAG(dr) ( { \
71 register unsigned long __res asm("r28");\
72 __asm__ __volatile__ ( \
73 ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
74 ); \
75 __res; \
76} )
77
78
79static int shutdown_timer __read_mostly;
80
81
82static void process_shutdown(void)
83{
84 if (shutdown_timer == 0)
85 printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
86
87 shutdown_timer++;
88
89
90 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);
93 lcd_print(msg);
94
95
96 if (kill_cad_pid(SIGINT, 1)) {
97
98 machine_power_off();
99 }
100 }
101}
102
103
104
105static struct task_struct *power_task;
106
107
108#define SYSCTL_FILENAME "sys/kernel/power"
109
110
111int pwrsw_enabled __read_mostly = 1;
112
113
114static int kpowerswd(void *param)
115{
116 __set_current_state(TASK_RUNNING);
117
118 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);
123
124 if (unlikely(!pwrsw_enabled))
125 continue;
126
127 if (soft_power_reg) {
128
129
130
131
132
133
134
135 button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
136 } else {
137
138
139
140
141
142
143
144 button_not_pressed = (__getDIAG(25) & 0x80000000);
145 }
146
147 if (likely(button_not_pressed)) {
148 if (unlikely(shutdown_timer &&
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();
155
156
157 } while (!kthread_should_stop());
158
159 return 0;
160}
161
162
163
164
165
166#if 0
167static void powerfail_interrupt(int code, void *x)
168{
169 printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
170 poweroff();
171}
172#endif
173
174
175
176
177
178
179
180
181
182static int parisc_panic_event(struct notifier_block *this,
183 unsigned long event, void *ptr)
184{
185
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;
199 unsigned long soft_power_reg;
200
201#if 0
202 request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
203 0, "powerfail", NULL);
204#endif
205
206
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) {
214 case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
215 break;
216
217 case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
218 return -ENODEV;
219
220 default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
221 soft_power_reg);
222 }
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;
229 }
230
231
232 atomic_notifier_chain_register(&panic_notifier_list,
233 &parisc_panic_block);
234
235 return 0;
236}
237
238static void __exit power_exit(void)
239{
240 kthread_stop(power_task);
241
242 atomic_notifier_chain_unregister(&panic_notifier_list,
243 &parisc_panic_block);
244
245 pdc_soft_power_button(0);
246}
247
248arch_initcall(power_init);
249module_exit(power_exit);
250
251
252MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
253MODULE_DESCRIPTION("Soft power switch driver");
254MODULE_LICENSE("Dual BSD/GPL");
255