Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / mips / sgi-ip32 / ip32-reset.c
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) 2001 Keith M Wesolowski
7  * Copyright (C) 2001 Paul Mundt
8  * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
9  */
10
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/notifier.h>
15 #include <linux/delay.h>
16 #include <linux/ds17287rtc.h>
17 #include <linux/interrupt.h>
18
19 #include <asm/addrspace.h>
20 #include <asm/irq.h>
21 #include <asm/reboot.h>
22 #include <asm/system.h>
23 #include <asm/wbflush.h>
24 #include <asm/ip32/mace.h>
25 #include <asm/ip32/crime.h>
26 #include <asm/ip32/ip32_ints.h>
27
28 #define POWERDOWN_TIMEOUT       120
29 /*
30  * Blink frequency during reboot grace period and when paniced.
31  */
32 #define POWERDOWN_FREQ          (HZ / 4)
33 #define PANIC_FREQ              (HZ / 8)
34
35 static struct timer_list power_timer, blink_timer, debounce_timer;
36 static int has_paniced, shuting_down;
37
38 static void ip32_machine_restart(char *command) __attribute__((noreturn));
39 static void ip32_machine_halt(void) __attribute__((noreturn));
40 static void ip32_machine_power_off(void) __attribute__((noreturn));
41
42 static void ip32_machine_restart(char *cmd)
43 {
44         crime->control = CRIME_CONTROL_HARD_RESET;
45         while (1);
46 }
47
48 static inline void ip32_machine_halt(void)
49 {
50         ip32_machine_power_off();
51 }
52
53 static void ip32_machine_power_off(void)
54 {
55         volatile unsigned char reg_a, xctrl_a, xctrl_b;
56
57         disable_irq(MACEISA_RTC_IRQ);
58         reg_a = CMOS_READ(RTC_REG_A);
59
60         /* setup for kickstart & wake-up (DS12287 Ref. Man. p. 19) */
61         reg_a &= ~DS_REGA_DV2;
62         reg_a |= DS_REGA_DV1;
63
64         CMOS_WRITE(reg_a | DS_REGA_DV0, RTC_REG_A);
65         wbflush();
66         xctrl_b = CMOS_READ(DS_B1_XCTRL4B)
67                    | DS_XCTRL4B_ABE | DS_XCTRL4B_KFE;
68         CMOS_WRITE(xctrl_b, DS_B1_XCTRL4B);
69         xctrl_a = CMOS_READ(DS_B1_XCTRL4A) & ~DS_XCTRL4A_IFS;
70         CMOS_WRITE(xctrl_a, DS_B1_XCTRL4A);
71         wbflush();
72         /* adios amigos... */
73         CMOS_WRITE(xctrl_a | DS_XCTRL4A_PAB, DS_B1_XCTRL4A);
74         CMOS_WRITE(reg_a, RTC_REG_A);
75         wbflush();
76         while (1);
77 }
78
79 static void power_timeout(unsigned long data)
80 {
81         ip32_machine_power_off();
82 }
83
84 static void blink_timeout(unsigned long data)
85 {
86         unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
87         mace->perif.ctrl.misc = led;
88         mod_timer(&blink_timer, jiffies + data);
89 }
90
91 static void debounce(unsigned long data)
92 {
93         volatile unsigned char reg_a, reg_c, xctrl_a;
94
95         reg_c = CMOS_READ(RTC_INTR_FLAGS);
96         CMOS_WRITE(reg_a | DS_REGA_DV0, RTC_REG_A);
97         wbflush();
98         xctrl_a = CMOS_READ(DS_B1_XCTRL4A);
99         if ((xctrl_a & DS_XCTRL4A_IFS) || (reg_c & RTC_IRQF )) {
100                 /* Interrupt still being sent. */
101                 debounce_timer.expires = jiffies + 50;
102                 add_timer(&debounce_timer);
103
104                 /* clear interrupt source */
105                 CMOS_WRITE(xctrl_a & ~DS_XCTRL4A_IFS, DS_B1_XCTRL4A);
106                 CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
107                 return;
108         }
109         CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
110
111         if (has_paniced)
112                 ip32_machine_restart(NULL);
113
114         enable_irq(MACEISA_RTC_IRQ);
115 }
116
117 static inline void ip32_power_button(void)
118 {
119         if (has_paniced)
120                 return;
121
122         if (shuting_down || kill_proc(1, SIGINT, 1)) {
123                 /* No init process or button pressed twice.  */
124                 ip32_machine_power_off();
125         }
126
127         shuting_down = 1;
128         blink_timer.data = POWERDOWN_FREQ;
129         blink_timeout(POWERDOWN_FREQ);
130
131         init_timer(&power_timer);
132         power_timer.function = power_timeout;
133         power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
134         add_timer(&power_timer);
135 }
136
137 static irqreturn_t ip32_rtc_int(int irq, void *dev_id, struct pt_regs *regs)
138 {
139         volatile unsigned char reg_c;
140
141         reg_c = CMOS_READ(RTC_INTR_FLAGS);
142         if (!(reg_c & RTC_IRQF)) {
143                 printk(KERN_WARNING 
144                         "%s: RTC IRQ without RTC_IRQF\n", __FUNCTION__);
145         }
146         /* Wait until interrupt goes away */
147         disable_irq(MACEISA_RTC_IRQ);
148         init_timer(&debounce_timer);
149         debounce_timer.function = debounce;
150         debounce_timer.expires = jiffies + 50;
151         add_timer(&debounce_timer);
152
153         printk(KERN_DEBUG "Power button pressed\n");
154         ip32_power_button();
155         return IRQ_HANDLED;
156 }
157
158 static int panic_event(struct notifier_block *this, unsigned long event,
159                        void *ptr)
160 {
161         unsigned long led;
162
163         if (has_paniced)
164                 return NOTIFY_DONE;
165         has_paniced = 1;
166
167         /* turn off the green LED */
168         led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
169         mace->perif.ctrl.misc = led;
170
171         blink_timer.data = PANIC_FREQ;
172         blink_timeout(PANIC_FREQ);
173
174         return NOTIFY_DONE;
175 }
176
177 static struct notifier_block panic_block = {
178         .notifier_call = panic_event,
179 };
180
181 static __init int ip32_reboot_setup(void)
182 {
183         /* turn on the green led only */
184         unsigned long led = mace->perif.ctrl.misc;
185         led |= MACEISA_LED_RED;
186         led &= ~MACEISA_LED_GREEN;
187         mace->perif.ctrl.misc = led;
188
189         _machine_restart = ip32_machine_restart;
190         _machine_halt = ip32_machine_halt;
191         _machine_power_off = ip32_machine_power_off;
192
193         init_timer(&blink_timer);
194         blink_timer.function = blink_timeout;
195         notifier_chain_register(&panic_notifier_list, &panic_block);
196
197         request_irq(MACEISA_RTC_IRQ, ip32_rtc_int, 0, "rtc", NULL);
198
199         return 0;
200 }
201
202 subsys_initcall(ip32_reboot_setup);