blob: 92a3b3c15ed3a5ddf57760b44910c6b4cbf86a52 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Ralf Baechlefcdb27a2006-01-18 17:37:07 +00006 * Copyright (C) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Ralf Baechle71baa1a2006-01-15 18:10:39 +00008#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/ds1286.h>
11#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
Ralf Baechlefcdb27a2006-01-18 17:37:07 +000016#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/timer.h>
18
19#include <asm/io.h>
20#include <asm/irq.h>
21#include <asm/system.h>
22#include <asm/reboot.h>
23#include <asm/sgialib.h>
24#include <asm/sgi/ioc.h>
25#include <asm/sgi/hpc3.h>
26#include <asm/sgi/mc.h>
27#include <asm/sgi/ip22.h>
28
29/*
30 * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds.
31 * I'm not sure if this feature is a good idea, for now it's here just to
32 * make the power button make behave just like under IRIX.
33 */
34#define POWERDOWN_TIMEOUT 120
35
36/*
37 * Blink frequency during reboot grace period and when paniced.
38 */
39#define POWERDOWN_FREQ (HZ / 4)
40#define PANIC_FREQ (HZ / 8)
41
42static struct timer_list power_timer, blink_timer, debounce_timer, volume_timer;
43
44#define MACHINE_PANICED 1
45#define MACHINE_SHUTTING_DOWN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ralf Baechle71baa1a2006-01-15 18:10:39 +000047static int machine_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Ralf Baechle71baa1a2006-01-15 18:10:39 +000049static void ATTRIB_NORET sgi_machine_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 unsigned int tmp;
52
53 local_irq_disable();
54
55 /* Disable watchdog */
56 tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff;
57 hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM;
58 hpc3c0->rtcregs[RTC_WSEC] = 0;
59 hpc3c0->rtcregs[RTC_WHSEC] = 0;
60
61 while (1) {
62 sgioc->panel = ~SGIOC_PANEL_POWERON;
63 /* Good bye cruel world ... */
64
65 /* If we're still running, we probably got sent an alarm
66 interrupt. Read the flag to clear it. */
67 tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM];
68 }
69}
70
Ralf Baechle71baa1a2006-01-15 18:10:39 +000071static void ATTRIB_NORET sgi_machine_restart(char *command)
72{
73 if (machine_state & MACHINE_SHUTTING_DOWN)
74 sgi_machine_power_off();
75 sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
76 while (1);
77}
78
79static void ATTRIB_NORET sgi_machine_halt(void)
80{
81 if (machine_state & MACHINE_SHUTTING_DOWN)
82 sgi_machine_power_off();
83 ArcEnterInteractiveMode();
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void power_timeout(unsigned long data)
87{
88 sgi_machine_power_off();
89}
90
91static void blink_timeout(unsigned long data)
92{
93 /* XXX fix this for fullhouse */
94 sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF);
95 sgioc->reset = sgi_ioc_reset;
96
Ralf Baechle71baa1a2006-01-15 18:10:39 +000097 mod_timer(&blink_timer, jiffies + data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void debounce(unsigned long data)
101{
102 del_timer(&debounce_timer);
103 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
104 /* Interrupt still being sent. */
Ralf Baechle71baa1a2006-01-15 18:10:39 +0000105 debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 add_timer(&debounce_timer);
107
108 sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR |
109 SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD |
110 SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD;
111
112 return;
113 }
114
115 if (machine_state & MACHINE_PANICED)
116 sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
117
118 enable_irq(SGI_PANEL_IRQ);
119}
120
121static inline void power_button(void)
122{
123 if (machine_state & MACHINE_PANICED)
124 return;
125
126 if ((machine_state & MACHINE_SHUTTING_DOWN) || kill_proc(1,SIGINT,1)) {
127 /* No init process or button pressed twice. */
128 sgi_machine_power_off();
129 }
130
131 machine_state |= MACHINE_SHUTTING_DOWN;
132 blink_timer.data = POWERDOWN_FREQ;
133 blink_timeout(POWERDOWN_FREQ);
134
135 init_timer(&power_timer);
136 power_timer.function = power_timeout;
137 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
138 add_timer(&power_timer);
139}
140
141void (*indy_volume_button)(int) = NULL;
142
143EXPORT_SYMBOL(indy_volume_button);
144
145static inline void volume_up_button(unsigned long data)
146{
147 del_timer(&volume_timer);
148
149 if (indy_volume_button)
150 indy_volume_button(1);
151
152 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
Ralf Baechle71baa1a2006-01-15 18:10:39 +0000153 volume_timer.expires = jiffies + (HZ / 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 add_timer(&volume_timer);
155 }
156}
157
158static inline void volume_down_button(unsigned long data)
159{
160 del_timer(&volume_timer);
161
162 if (indy_volume_button)
163 indy_volume_button(-1);
164
165 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
Ralf Baechle71baa1a2006-01-15 18:10:39 +0000166 volume_timer.expires = jiffies + (HZ / 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 add_timer(&volume_timer);
168 }
169}
170
171static irqreturn_t panel_int(int irq, void *dev_id, struct pt_regs *regs)
172{
173 unsigned int buttons;
174
175 buttons = sgioc->panel;
176 sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR;
177
178 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
179 /* Wait until interrupt goes away */
180 disable_irq(SGI_PANEL_IRQ);
181 init_timer(&debounce_timer);
182 debounce_timer.function = debounce;
183 debounce_timer.expires = jiffies + 5;
184 add_timer(&debounce_timer);
185 }
186
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700187 /* Power button was pressed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * ioc.ps page 22: "The Panel Register is called Power Control by Full
189 * House. Only lowest 2 bits are used. Guiness uses upper four bits
190 * for volume control". This is not true, all bits are pulled high
191 * on fullhouse */
192 if (ip22_is_fullhouse() || !(buttons & SGIOC_PANEL_POWERINTR)) {
193 power_button();
194 return IRQ_HANDLED;
195 }
196 /* TODO: mute/unmute */
197 /* Volume up button was pressed */
198 if (!(buttons & SGIOC_PANEL_VOLUPINTR)) {
199 init_timer(&volume_timer);
200 volume_timer.function = volume_up_button;
Ralf Baechle71baa1a2006-01-15 18:10:39 +0000201 volume_timer.expires = jiffies + (HZ / 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 add_timer(&volume_timer);
203 }
204 /* Volume down button was pressed */
205 if (!(buttons & SGIOC_PANEL_VOLDNINTR)) {
206 init_timer(&volume_timer);
207 volume_timer.function = volume_down_button;
Ralf Baechle71baa1a2006-01-15 18:10:39 +0000208 volume_timer.expires = jiffies + (HZ / 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 add_timer(&volume_timer);
210 }
211
212 return IRQ_HANDLED;
213}
214
215static int panic_event(struct notifier_block *this, unsigned long event,
216 void *ptr)
217{
218 if (machine_state & MACHINE_PANICED)
219 return NOTIFY_DONE;
220 machine_state |= MACHINE_PANICED;
221
222 blink_timer.data = PANIC_FREQ;
223 blink_timeout(PANIC_FREQ);
224
225 return NOTIFY_DONE;
226}
227
228static struct notifier_block panic_block = {
229 .notifier_call = panic_event,
230};
231
232static int __init reboot_setup(void)
233{
234 _machine_restart = sgi_machine_restart;
235 _machine_halt = sgi_machine_halt;
Ralf Baechlefcdb27a2006-01-18 17:37:07 +0000236 pm_power_off = sgi_machine_power_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
239 init_timer(&blink_timer);
240 blink_timer.function = blink_timeout;
241 notifier_chain_register(&panic_notifier_list, &panic_block);
242
243 return 0;
244}
245
246subsys_initcall(reboot_setup);