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
6 * Copyright (C) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle
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>
17 #include <linux/timer.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>
29 * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds.
30 * I'm not sure if this feature is a good idea, for now it's here just to
31 * make the power button make behave just like under IRIX.
33 #define POWERDOWN_TIMEOUT 120
36 * Blink frequency during reboot grace period and when panicked.
38 #define POWERDOWN_FREQ (HZ / 4)
39 #define PANIC_FREQ (HZ / 8)
41 static struct timer_list power_timer, blink_timer, debounce_timer;
42 static unsigned long blink_timer_timeout;
44 #define MACHINE_PANICED 1
45 #define MACHINE_SHUTTING_DOWN 2
47 static int machine_state;
49 static void __noreturn sgi_machine_power_off(void)
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;
62 sgioc->panel = ~SGIOC_PANEL_POWERON;
63 /* Good bye cruel world ... */
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];
71 static void __noreturn sgi_machine_restart(char *command)
73 if (machine_state & MACHINE_SHUTTING_DOWN)
74 sgi_machine_power_off();
75 sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
79 static void __noreturn sgi_machine_halt(void)
81 if (machine_state & MACHINE_SHUTTING_DOWN)
82 sgi_machine_power_off();
83 ArcEnterInteractiveMode();
86 static void power_timeout(struct timer_list *unused)
88 sgi_machine_power_off();
91 static void blink_timeout(struct timer_list *unused)
93 /* XXX fix this for fullhouse */
94 sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF);
95 sgioc->reset = sgi_ioc_reset;
97 mod_timer(&blink_timer, jiffies + blink_timer_timeout);
100 static void debounce(struct timer_list *unused)
102 del_timer(&debounce_timer);
103 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
104 /* Interrupt still being sent. */
105 debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s */
106 add_timer(&debounce_timer);
108 sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR |
109 SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD |
110 SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD;
115 if (machine_state & MACHINE_PANICED)
116 sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
118 enable_irq(SGI_PANEL_IRQ);
121 static inline void power_button(void)
123 if (machine_state & MACHINE_PANICED)
126 if ((machine_state & MACHINE_SHUTTING_DOWN) ||
127 kill_cad_pid(SIGINT, 1)) {
128 /* No init process or button pressed twice. */
129 sgi_machine_power_off();
132 machine_state |= MACHINE_SHUTTING_DOWN;
133 blink_timer_timeout = POWERDOWN_FREQ;
134 blink_timeout(&blink_timer);
136 timer_setup(&power_timer, power_timeout, 0);
137 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
138 add_timer(&power_timer);
141 static irqreturn_t panel_int(int irq, void *dev_id)
143 unsigned int buttons;
145 buttons = sgioc->panel;
146 sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR;
148 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
149 /* Wait until interrupt goes away */
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);
156 /* Power button was pressed
157 * ioc.ps page 22: "The Panel Register is called Power Control by Full
158 * House. Only lowest 2 bits are used. Guiness uses upper four bits
159 * for volume control". This is not true, all bits are pulled high
161 if (!(buttons & SGIOC_PANEL_POWERINTR))
167 static int panic_event(struct notifier_block *this, unsigned long event,
170 if (machine_state & MACHINE_PANICED)
172 machine_state |= MACHINE_PANICED;
174 blink_timer_timeout = PANIC_FREQ;
175 blink_timeout(&blink_timer);
180 static struct notifier_block panic_block = {
181 .notifier_call = panic_event,
184 static int __init reboot_setup(void)
188 _machine_restart = sgi_machine_restart;
189 _machine_halt = sgi_machine_halt;
190 pm_power_off = sgi_machine_power_off;
192 res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
194 printk(KERN_ERR "Allocation of front panel IRQ failed\n");
198 timer_setup(&blink_timer, blink_timeout, 0);
199 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
204 subsys_initcall(reboot_setup);