1
2
3
4
5
6
7
8#include <linux/linkage.h>
9#include <linux/init.h>
10#include <linux/rtc/ds1286.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/sched/signal.h>
14#include <linux/notifier.h>
15#include <linux/panic_notifier.h>
16#include <linux/pm.h>
17#include <linux/timer.h>
18
19#include <asm/io.h>
20#include <asm/irq.h>
21#include <asm/reboot.h>
22#include <asm/sgialib.h>
23#include <asm/sgi/ioc.h>
24#include <asm/sgi/hpc3.h>
25#include <asm/sgi/mc.h>
26#include <asm/sgi/ip22.h>
27
28
29
30
31
32
33#define POWERDOWN_TIMEOUT 120
34
35
36
37
38#define POWERDOWN_FREQ (HZ / 4)
39#define PANIC_FREQ (HZ / 8)
40
41static struct timer_list power_timer, blink_timer, debounce_timer;
42static unsigned long blink_timer_timeout;
43
44#define MACHINE_PANICED 1
45#define MACHINE_SHUTTING_DOWN 2
46
47static int machine_state;
48
49static void __noreturn sgi_machine_power_off(void)
50{
51 unsigned int tmp;
52
53 local_irq_disable();
54
55
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
64
65
66
67 tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM];
68 }
69}
70
71static void __noreturn 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 __noreturn sgi_machine_halt(void)
80{
81 if (machine_state & MACHINE_SHUTTING_DOWN)
82 sgi_machine_power_off();
83 ArcEnterInteractiveMode();
84}
85
86static void power_timeout(struct timer_list *unused)
87{
88 sgi_machine_power_off();
89}
90
91static void blink_timeout(struct timer_list *unused)
92{
93
94 sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF);
95 sgioc->reset = sgi_ioc_reset;
96
97 mod_timer(&blink_timer, jiffies + blink_timer_timeout);
98}
99
100static void debounce(struct timer_list *unused)
101{
102 del_timer(&debounce_timer);
103 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
104
105 debounce_timer.expires = jiffies + (HZ / 20);
106 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) ||
127 kill_cad_pid(SIGINT, 1)) {
128
129 sgi_machine_power_off();
130 }
131
132 machine_state |= MACHINE_SHUTTING_DOWN;
133 blink_timer_timeout = POWERDOWN_FREQ;
134 blink_timeout(&blink_timer);
135
136 timer_setup(&power_timer, power_timeout, 0);
137 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
138 add_timer(&power_timer);
139}
140
141static irqreturn_t panel_int(int irq, void *dev_id)
142{
143 unsigned int buttons;
144
145 buttons = sgioc->panel;
146 sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR;
147
148 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
149
150 disable_irq_nosync(SGI_PANEL_IRQ);
151 timer_setup(&debounce_timer, debounce, 0);
152 debounce_timer.expires = jiffies + 5;
153 add_timer(&debounce_timer);
154 }
155
156
157
158
159
160
161 if (!(buttons & SGIOC_PANEL_POWERINTR))
162 power_button();
163
164 return IRQ_HANDLED;
165}
166
167static int panic_event(struct notifier_block *this, unsigned long event,
168 void *ptr)
169{
170 if (machine_state & MACHINE_PANICED)
171 return NOTIFY_DONE;
172 machine_state |= MACHINE_PANICED;
173
174 blink_timer_timeout = PANIC_FREQ;
175 blink_timeout(&blink_timer);
176
177 return NOTIFY_DONE;
178}
179
180static struct notifier_block panic_block = {
181 .notifier_call = panic_event,
182};
183
184static int __init reboot_setup(void)
185{
186 int res;
187
188 _machine_restart = sgi_machine_restart;
189 _machine_halt = sgi_machine_halt;
190 pm_power_off = sgi_machine_power_off;
191
192 res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
193 if (res) {
194 printk(KERN_ERR "Allocation of front panel IRQ failed\n");
195 return res;
196 }
197
198 timer_setup(&blink_timer, blink_timeout, 0);
199 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
200
201 return 0;
202}
203
204subsys_initcall(reboot_setup);
205