blob: 8bd415c8729f974a8e1e376d5a2b3e08c84e6053 [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 *
6 * Copyright (C) 2001 Keith M Wesolowski
7 * Copyright (C) 2001 Paul Mundt
8 * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
9 */
10
Ralf Baechleefc46d12015-01-02 15:56:28 +010011#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/kernel.h>
Joshua Kinard15beb692015-04-16 12:49:09 -070014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/notifier.h>
17#include <linux/delay.h>
Joshua Kinard15beb692015-04-16 12:49:09 -070018#include <linux/rtc/ds1685.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/interrupt.h>
Ralf Baechlefcdb27a2006-01-18 17:37:07 +000020#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/addrspace.h>
23#include <asm/irq.h>
24#include <asm/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/wbflush.h>
26#include <asm/ip32/mace.h>
27#include <asm/ip32/crime.h>
28#include <asm/ip32/ip32_ints.h>
29
30#define POWERDOWN_TIMEOUT 120
31/*
Lee Revellf18190b2006-06-26 18:30:00 +020032 * Blink frequency during reboot grace period and when panicked.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34#define POWERDOWN_FREQ (HZ / 4)
35#define PANIC_FREQ (HZ / 8)
36
Joshua Kinard15beb692015-04-16 12:49:09 -070037extern struct platform_device ip32_rtc_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Joshua Kinard15beb692015-04-16 12:49:09 -070039static struct timer_list power_timer, blink_timer;
40static int has_panicked, shutting_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joshua Kinard15beb692015-04-16 12:49:09 -070042static __noreturn void ip32_poweroff(void *data)
43{
44 void (*poweroff_func)(struct platform_device *) =
45 symbol_get(ds1685_rtc_poweroff);
46
47#ifdef CONFIG_MODULES
48 /* If the first __symbol_get failed, our module wasn't loaded. */
49 if (!poweroff_func) {
50 request_module("rtc-ds1685");
51 poweroff_func = symbol_get(ds1685_rtc_poweroff);
52 }
53#endif
54
55 if (!poweroff_func)
56 pr_emerg("RTC not available for power-off. Spinning forever ...\n");
57 else {
58 (*poweroff_func)((struct platform_device *)data);
59 symbol_put(ds1685_rtc_poweroff);
60 }
61
62 unreachable();
63}
64
65static void ip32_machine_restart(char *cmd) __noreturn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ip32_machine_restart(char *cmd)
67{
Joshua Kinard15beb692015-04-16 12:49:09 -070068 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 crime->control = CRIME_CONTROL_HARD_RESET;
Joshua Kinard15beb692015-04-16 12:49:09 -070070 unreachable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void blink_timeout(unsigned long data)
74{
75 unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
76 mace->perif.ctrl.misc = led;
77 mod_timer(&blink_timer, jiffies + data);
78}
79
Joshua Kinard15beb692015-04-16 12:49:09 -070080static void ip32_machine_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Joshua Kinard15beb692015-04-16 12:49:09 -070082 ip32_poweroff(&ip32_rtc_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Joshua Kinard15beb692015-04-16 12:49:09 -070085static void power_timeout(unsigned long data)
86{
87 ip32_poweroff(&ip32_rtc_device);
88}
89
90void ip32_prepare_poweroff(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Lee Revellf18190b2006-06-26 18:30:00 +020092 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return;
94
Joshua Kinard15beb692015-04-16 12:49:09 -070095 if (shutting_down || kill_cad_pid(SIGINT, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* No init process or button pressed twice. */
Joshua Kinard15beb692015-04-16 12:49:09 -070097 ip32_poweroff(&ip32_rtc_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Joshua Kinard15beb692015-04-16 12:49:09 -0700100 shutting_down = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 blink_timer.data = POWERDOWN_FREQ;
102 blink_timeout(POWERDOWN_FREQ);
103
104 init_timer(&power_timer);
105 power_timer.function = power_timeout;
106 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
107 add_timer(&power_timer);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int panic_event(struct notifier_block *this, unsigned long event,
111 void *ptr)
112{
113 unsigned long led;
114
Lee Revellf18190b2006-06-26 18:30:00 +0200115 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +0200117 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* turn off the green LED */
120 led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
121 mace->perif.ctrl.misc = led;
122
123 blink_timer.data = PANIC_FREQ;
124 blink_timeout(PANIC_FREQ);
125
126 return NOTIFY_DONE;
127}
128
129static struct notifier_block panic_block = {
130 .notifier_call = panic_event,
131};
132
133static __init int ip32_reboot_setup(void)
134{
135 /* turn on the green led only */
136 unsigned long led = mace->perif.ctrl.misc;
137 led |= MACEISA_LED_RED;
138 led &= ~MACEISA_LED_GREEN;
139 mace->perif.ctrl.misc = led;
140
141 _machine_restart = ip32_machine_restart;
142 _machine_halt = ip32_machine_halt;
Joshua Kinard15beb692015-04-16 12:49:09 -0700143 pm_power_off = ip32_machine_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 init_timer(&blink_timer);
146 blink_timer.function = blink_timeout;
Alan Sterne041c682006-03-27 01:16:30 -0800147 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150}
151
152subsys_initcall(ip32_reboot_setup);