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) 2001 Keith M Wesolowski
7 * Copyright (C) 2001 Paul Mundt
8 * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
11 #include <linux/compiler.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/panic_notifier.h>
16 #include <linux/sched.h>
17 #include <linux/sched/signal.h>
18 #include <linux/notifier.h>
19 #include <linux/delay.h>
20 #include <linux/rtc/ds1685.h>
21 #include <linux/interrupt.h>
24 #include <asm/addrspace.h>
26 #include <asm/reboot.h>
27 #include <asm/wbflush.h>
28 #include <asm/ip32/mace.h>
29 #include <asm/ip32/crime.h>
30 #include <asm/ip32/ip32_ints.h>
32 #define POWERDOWN_TIMEOUT 120
34 * Blink frequency during reboot grace period and when panicked.
36 #define POWERDOWN_FREQ (HZ / 4)
37 #define PANIC_FREQ (HZ / 8)
39 extern struct platform_device ip32_rtc_device;
41 static struct timer_list power_timer, blink_timer;
42 static unsigned long blink_timer_timeout;
43 static int has_panicked, shutting_down;
45 static __noreturn void ip32_poweroff(void *data)
47 void (*poweroff_func)(struct platform_device *) =
48 symbol_get(ds1685_rtc_poweroff);
51 /* If the first __symbol_get failed, our module wasn't loaded. */
53 request_module("rtc-ds1685");
54 poweroff_func = symbol_get(ds1685_rtc_poweroff);
59 pr_emerg("RTC not available for power-off. Spinning forever ...\n");
61 (*poweroff_func)((struct platform_device *)data);
62 symbol_put(ds1685_rtc_poweroff);
68 static void ip32_machine_restart(char *cmd) __noreturn;
69 static void ip32_machine_restart(char *cmd)
72 crime->control = CRIME_CONTROL_HARD_RESET;
76 static void blink_timeout(struct timer_list *unused)
78 unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
79 mace->perif.ctrl.misc = led;
80 mod_timer(&blink_timer, jiffies + blink_timer_timeout);
83 static void ip32_machine_halt(void)
85 ip32_poweroff(&ip32_rtc_device);
88 static void power_timeout(struct timer_list *unused)
90 ip32_poweroff(&ip32_rtc_device);
93 void ip32_prepare_poweroff(void)
98 if (shutting_down || kill_cad_pid(SIGINT, 1)) {
99 /* No init process or button pressed twice. */
100 ip32_poweroff(&ip32_rtc_device);
104 blink_timer_timeout = POWERDOWN_FREQ;
105 blink_timeout(&blink_timer);
107 timer_setup(&power_timer, power_timeout, 0);
108 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
109 add_timer(&power_timer);
112 static int panic_event(struct notifier_block *this, unsigned long event,
121 /* turn off the green LED */
122 led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
123 mace->perif.ctrl.misc = led;
125 blink_timer_timeout = PANIC_FREQ;
126 blink_timeout(&blink_timer);
131 static struct notifier_block panic_block = {
132 .notifier_call = panic_event,
135 static __init int ip32_reboot_setup(void)
137 /* turn on the green led only */
138 unsigned long led = mace->perif.ctrl.misc;
139 led |= MACEISA_LED_RED;
140 led &= ~MACEISA_LED_GREEN;
141 mace->perif.ctrl.misc = led;
143 _machine_restart = ip32_machine_restart;
144 _machine_halt = ip32_machine_halt;
145 pm_power_off = ip32_machine_halt;
147 timer_setup(&blink_timer, blink_timeout, 0);
148 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
153 subsys_initcall(ip32_reboot_setup);