blob: 063c2dd31e72856fdf83391ddff0721240df6f1b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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 * 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.
32 */
33#define POWERDOWN_TIMEOUT 120
34
35/*
Lee Revellf18190b2006-06-26 18:30:00 +020036 * Blink frequency during reboot grace period and when panicked.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38#define POWERDOWN_FREQ (HZ / 4)
39#define PANIC_FREQ (HZ / 8)
40
Thomas Bogendoerferb03d7b12008-07-11 23:03:03 +020041static struct timer_list power_timer, blink_timer, debounce_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define MACHINE_PANICED 1
44#define MACHINE_SHUTTING_DOWN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ralf Baechle71baa1a2006-01-15 18:10:39 +000046static int machine_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Robert P. J. Dayb3f6df92007-05-25 14:32:28 -040048static void __noreturn sgi_machine_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 unsigned int tmp;
51
52 local_irq_disable();
53
54 /* Disable watchdog */
55 tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff;
56 hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM;
57 hpc3c0->rtcregs[RTC_WSEC] = 0;
58 hpc3c0->rtcregs[RTC_WHSEC] = 0;
59
60 while (1) {
61 sgioc->panel = ~SGIOC_PANEL_POWERON;
62 /* Good bye cruel world ... */
63
64 /* If we're still running, we probably got sent an alarm
65 interrupt. Read the flag to clear it. */
66 tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM];
67 }
68}
69
Robert P. J. Dayb3f6df92007-05-25 14:32:28 -040070static void __noreturn sgi_machine_restart(char *command)
Ralf Baechle71baa1a2006-01-15 18:10:39 +000071{
72 if (machine_state & MACHINE_SHUTTING_DOWN)
73 sgi_machine_power_off();
74 sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
75 while (1);
76}
77
Robert P. J. Dayb3f6df92007-05-25 14:32:28 -040078static void __noreturn sgi_machine_halt(void)
Ralf Baechle71baa1a2006-01-15 18:10:39 +000079{
80 if (machine_state & MACHINE_SHUTTING_DOWN)
81 sgi_machine_power_off();
82 ArcEnterInteractiveMode();
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void power_timeout(unsigned long data)
86{
87 sgi_machine_power_off();
88}
89
90static 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
Ralf Baechle71baa1a2006-01-15 18:10:39 +000096 mod_timer(&blink_timer, jiffies + data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static void debounce(unsigned long data)
100{
101 del_timer(&debounce_timer);
102 if (sgint->istat1 & SGINT_ISTAT1_PWR) {
103 /* Interrupt still being sent. */
Ralf Baechle70342282013-01-22 12:59:30 +0100104 debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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
120static inline void power_button(void)
121{
122 if (machine_state & MACHINE_PANICED)
123 return;
124
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700125 if ((machine_state & MACHINE_SHUTTING_DOWN) ||
126 kill_cad_pid(SIGINT, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* 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
David Howells7d12e782006-10-05 14:55:46 +0100141static irqreturn_t panel_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
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 /* Wait until interrupt goes away */
Ralf Baechle7e9e05c2009-05-16 12:23:45 +0100150 disable_irq_nosync(SGI_PANEL_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 init_timer(&debounce_timer);
152 debounce_timer.function = debounce;
153 debounce_timer.expires = jiffies + 5;
154 add_timer(&debounce_timer);
155 }
156
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700157 /* Power button was pressed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * ioc.ps page 22: "The Panel Register is called Power Control by Full
159 * House. Only lowest 2 bits are used. Guiness uses upper four bits
160 * for volume control". This is not true, all bits are pulled high
161 * on fullhouse */
Thomas Bogendoerferb03d7b12008-07-11 23:03:03 +0200162 if (!(buttons & SGIOC_PANEL_POWERINTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 power_button();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return IRQ_HANDLED;
166}
167
168static int panic_event(struct notifier_block *this, unsigned long event,
Ralf Baechle70342282013-01-22 12:59:30 +0100169 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (machine_state & MACHINE_PANICED)
172 return NOTIFY_DONE;
173 machine_state |= MACHINE_PANICED;
174
175 blink_timer.data = PANIC_FREQ;
176 blink_timeout(PANIC_FREQ);
177
178 return NOTIFY_DONE;
179}
180
181static struct notifier_block panic_block = {
182 .notifier_call = panic_event,
183};
184
185static int __init reboot_setup(void)
186{
Ralf Baechle754d0f22007-10-14 23:04:27 +0100187 int res;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 _machine_restart = sgi_machine_restart;
190 _machine_halt = sgi_machine_halt;
Ralf Baechlefcdb27a2006-01-18 17:37:07 +0000191 pm_power_off = sgi_machine_power_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Ralf Baechle754d0f22007-10-14 23:04:27 +0100193 res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
194 if (res) {
195 printk(KERN_ERR "Allocation of front panel IRQ failed\n");
196 return res;
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 init_timer(&blink_timer);
200 blink_timer.function = blink_timeout;
Alan Sterne041c682006-03-27 01:16:30 -0800201 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return 0;
204}
205
206subsys_initcall(reboot_setup);