Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ralf Baechle | efc46d1 | 2015-01-02 15:56:28 +0100 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
Arnd Bergmann | fc69910 | 2017-03-08 08:29:31 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/notifier.h> |
| 18 | #include <linux/delay.h> |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 19 | #include <linux/rtc/ds1685.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 21 | #include <linux/pm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/addrspace.h> |
| 24 | #include <asm/irq.h> |
| 25 | #include <asm/reboot.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #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 Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 33 | * Blink frequency during reboot grace period and when panicked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | #define POWERDOWN_FREQ (HZ / 4) |
| 36 | #define PANIC_FREQ (HZ / 8) |
| 37 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 38 | extern struct platform_device ip32_rtc_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 40 | static struct timer_list power_timer, blink_timer; |
| 41 | static int has_panicked, shutting_down; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 43 | static __noreturn void ip32_poweroff(void *data) |
| 44 | { |
| 45 | void (*poweroff_func)(struct platform_device *) = |
| 46 | symbol_get(ds1685_rtc_poweroff); |
| 47 | |
| 48 | #ifdef CONFIG_MODULES |
| 49 | /* If the first __symbol_get failed, our module wasn't loaded. */ |
| 50 | if (!poweroff_func) { |
| 51 | request_module("rtc-ds1685"); |
| 52 | poweroff_func = symbol_get(ds1685_rtc_poweroff); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | if (!poweroff_func) |
| 57 | pr_emerg("RTC not available for power-off. Spinning forever ...\n"); |
| 58 | else { |
| 59 | (*poweroff_func)((struct platform_device *)data); |
| 60 | symbol_put(ds1685_rtc_poweroff); |
| 61 | } |
| 62 | |
| 63 | unreachable(); |
| 64 | } |
| 65 | |
| 66 | static void ip32_machine_restart(char *cmd) __noreturn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static void ip32_machine_restart(char *cmd) |
| 68 | { |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 69 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | crime->control = CRIME_CONTROL_HARD_RESET; |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 71 | unreachable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void blink_timeout(unsigned long data) |
| 75 | { |
| 76 | unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED; |
| 77 | mace->perif.ctrl.misc = led; |
| 78 | mod_timer(&blink_timer, jiffies + data); |
| 79 | } |
| 80 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 81 | static void ip32_machine_halt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 83 | ip32_poweroff(&ip32_rtc_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 86 | static void power_timeout(unsigned long data) |
| 87 | { |
| 88 | ip32_poweroff(&ip32_rtc_device); |
| 89 | } |
| 90 | |
| 91 | void ip32_prepare_poweroff(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 93 | if (has_panicked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return; |
| 95 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 96 | if (shutting_down || kill_cad_pid(SIGINT, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* No init process or button pressed twice. */ |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 98 | ip32_poweroff(&ip32_rtc_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Joshua Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 101 | shutting_down = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | blink_timer.data = POWERDOWN_FREQ; |
| 103 | blink_timeout(POWERDOWN_FREQ); |
| 104 | |
| 105 | init_timer(&power_timer); |
| 106 | power_timer.function = power_timeout; |
| 107 | power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ; |
| 108 | add_timer(&power_timer); |
| 109 | } |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static int panic_event(struct notifier_block *this, unsigned long event, |
| 112 | void *ptr) |
| 113 | { |
| 114 | unsigned long led; |
| 115 | |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 116 | if (has_panicked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return NOTIFY_DONE; |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 118 | has_panicked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* turn off the green LED */ |
| 121 | led = mace->perif.ctrl.misc | MACEISA_LED_GREEN; |
| 122 | mace->perif.ctrl.misc = led; |
| 123 | |
| 124 | blink_timer.data = PANIC_FREQ; |
| 125 | blink_timeout(PANIC_FREQ); |
| 126 | |
| 127 | return NOTIFY_DONE; |
| 128 | } |
| 129 | |
| 130 | static struct notifier_block panic_block = { |
| 131 | .notifier_call = panic_event, |
| 132 | }; |
| 133 | |
| 134 | static __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 Kinard | 15beb69 | 2015-04-16 12:49:09 -0700 | [diff] [blame] | 144 | pm_power_off = ip32_machine_halt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | init_timer(&blink_timer); |
| 147 | blink_timer.function = blink_timeout; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 148 | atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | subsys_initcall(ip32_reboot_setup); |