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