blob: 20d8637340be40b4b96aa09f9c5f4b6d61436273 [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 <[email protected]>
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>
Arnd Bergmannfc699102017-03-08 08:29:31 +010016#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/notifier.h>
18#include <linux/delay.h>
Joshua Kinard15beb692015-04-16 12:49:09 -070019#include <linux/rtc/ds1685.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
Ralf Baechlefcdb27a2006-01-18 17:37:07 +000021#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/addrspace.h>
24#include <asm/irq.h>
25#include <asm/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/wbflush.h>
27#include <asm/ip32/mace.h>
28#include <asm/ip32/crime.h>
29#include <asm/ip32/ip32_ints.h>
30
31#define POWERDOWN_TIMEOUT 120
32/*
Lee Revellf18190b2006-06-26 18:30:00 +020033 * Blink frequency during reboot grace period and when panicked.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35#define POWERDOWN_FREQ (HZ / 4)
36#define PANIC_FREQ (HZ / 8)
37
Joshua Kinard15beb692015-04-16 12:49:09 -070038extern struct platform_device ip32_rtc_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Joshua Kinard15beb692015-04-16 12:49:09 -070040static struct timer_list power_timer, blink_timer;
Kees Cooka66b8992017-10-09 21:42:26 -070041static unsigned long blink_timer_timeout;
Joshua Kinard15beb692015-04-16 12:49:09 -070042static int has_panicked, shutting_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Joshua Kinard15beb692015-04-16 12:49:09 -070044static __noreturn void ip32_poweroff(void *data)
45{
46 void (*poweroff_func)(struct platform_device *) =
47 symbol_get(ds1685_rtc_poweroff);
48
49#ifdef CONFIG_MODULES
50 /* If the first __symbol_get failed, our module wasn't loaded. */
51 if (!poweroff_func) {
52 request_module("rtc-ds1685");
53 poweroff_func = symbol_get(ds1685_rtc_poweroff);
54 }
55#endif
56
57 if (!poweroff_func)
58 pr_emerg("RTC not available for power-off. Spinning forever ...\n");
59 else {
60 (*poweroff_func)((struct platform_device *)data);
61 symbol_put(ds1685_rtc_poweroff);
62 }
63
64 unreachable();
65}
66
67static void ip32_machine_restart(char *cmd) __noreturn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void ip32_machine_restart(char *cmd)
69{
Joshua Kinard15beb692015-04-16 12:49:09 -070070 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 crime->control = CRIME_CONTROL_HARD_RESET;
Joshua Kinard15beb692015-04-16 12:49:09 -070072 unreachable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Kees Cooka66b8992017-10-09 21:42:26 -070075static void blink_timeout(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
78 mace->perif.ctrl.misc = led;
Kees Cooka66b8992017-10-09 21:42:26 -070079 mod_timer(&blink_timer, jiffies + blink_timer_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Joshua Kinard15beb692015-04-16 12:49:09 -070082static void ip32_machine_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Joshua Kinard15beb692015-04-16 12:49:09 -070084 ip32_poweroff(&ip32_rtc_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Kees Cooka66b8992017-10-09 21:42:26 -070087static void power_timeout(struct timer_list *unused)
Joshua Kinard15beb692015-04-16 12:49:09 -070088{
89 ip32_poweroff(&ip32_rtc_device);
90}
91
92void ip32_prepare_poweroff(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Lee Revellf18190b2006-06-26 18:30:00 +020094 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return;
96
Joshua Kinard15beb692015-04-16 12:49:09 -070097 if (shutting_down || kill_cad_pid(SIGINT, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* No init process or button pressed twice. */
Joshua Kinard15beb692015-04-16 12:49:09 -070099 ip32_poweroff(&ip32_rtc_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
Joshua Kinard15beb692015-04-16 12:49:09 -0700102 shutting_down = 1;
Kees Cooka66b8992017-10-09 21:42:26 -0700103 blink_timer_timeout = POWERDOWN_FREQ;
104 blink_timeout(&blink_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Kees Cooka66b8992017-10-09 21:42:26 -0700106 timer_setup(&power_timer, power_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
108 add_timer(&power_timer);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int panic_event(struct notifier_block *this, unsigned long event,
112 void *ptr)
113{
114 unsigned long led;
115
Lee Revellf18190b2006-06-26 18:30:00 +0200116 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +0200118 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* turn off the green LED */
121 led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
122 mace->perif.ctrl.misc = led;
123
Kees Cooka66b8992017-10-09 21:42:26 -0700124 blink_timer_timeout = PANIC_FREQ;
125 blink_timeout(&blink_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return NOTIFY_DONE;
128}
129
130static struct notifier_block panic_block = {
131 .notifier_call = panic_event,
132};
133
134static __init int ip32_reboot_setup(void)
135{
136 /* turn on the green led only */
137 unsigned long led = mace->perif.ctrl.misc;
138 led |= MACEISA_LED_RED;
139 led &= ~MACEISA_LED_GREEN;
140 mace->perif.ctrl.misc = led;
141
142 _machine_restart = ip32_machine_restart;
143 _machine_halt = ip32_machine_halt;
Joshua Kinard15beb692015-04-16 12:49:09 -0700144 pm_power_off = ip32_machine_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kees Cooka66b8992017-10-09 21:42:26 -0700146 timer_setup(&blink_timer, blink_timeout, 0);
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);