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 | * |
Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 6 | * Copyright (C) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
Ralf Baechle | 71baa1a | 2006-01-15 18:10:39 +0000 | [diff] [blame] | 8 | #include <linux/linkage.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/init.h> |
Alexandre Belloni | d4a5f6d | 2016-06-27 00:03:00 +0200 | [diff] [blame] | 10 | #include <linux/rtc/ds1286.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/kernel.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 13 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/notifier.h> |
Andy Shevchenko | f39650d | 2021-06-30 18:54:59 -0700 | [diff] [blame] | 15 | #include <linux/panic_notifier.h> |
Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 16 | #include <linux/pm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/timer.h> |
| 18 | |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/reboot.h> |
| 22 | #include <asm/sgialib.h> |
| 23 | #include <asm/sgi/ioc.h> |
| 24 | #include <asm/sgi/hpc3.h> |
| 25 | #include <asm/sgi/mc.h> |
| 26 | #include <asm/sgi/ip22.h> |
| 27 | |
| 28 | /* |
| 29 | * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds. |
| 30 | * I'm not sure if this feature is a good idea, for now it's here just to |
| 31 | * make the power button make behave just like under IRIX. |
| 32 | */ |
| 33 | #define POWERDOWN_TIMEOUT 120 |
| 34 | |
| 35 | /* |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 36 | * Blink frequency during reboot grace period and when panicked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | #define POWERDOWN_FREQ (HZ / 4) |
| 39 | #define PANIC_FREQ (HZ / 8) |
| 40 | |
Thomas Bogendoerfer | b03d7b1 | 2008-07-11 23:03:03 +0200 | [diff] [blame] | 41 | static struct timer_list power_timer, blink_timer, debounce_timer; |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 42 | static unsigned long blink_timer_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define MACHINE_PANICED 1 |
| 45 | #define MACHINE_SHUTTING_DOWN 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Ralf Baechle | 71baa1a | 2006-01-15 18:10:39 +0000 | [diff] [blame] | 47 | static int machine_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 49 | static void __noreturn sgi_machine_power_off(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | unsigned int tmp; |
| 52 | |
| 53 | local_irq_disable(); |
| 54 | |
| 55 | /* Disable watchdog */ |
| 56 | tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff; |
| 57 | hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM; |
| 58 | hpc3c0->rtcregs[RTC_WSEC] = 0; |
| 59 | hpc3c0->rtcregs[RTC_WHSEC] = 0; |
| 60 | |
| 61 | while (1) { |
| 62 | sgioc->panel = ~SGIOC_PANEL_POWERON; |
| 63 | /* Good bye cruel world ... */ |
| 64 | |
| 65 | /* If we're still running, we probably got sent an alarm |
| 66 | interrupt. Read the flag to clear it. */ |
| 67 | tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM]; |
| 68 | } |
| 69 | } |
| 70 | |
Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 71 | static void __noreturn sgi_machine_restart(char *command) |
Ralf Baechle | 71baa1a | 2006-01-15 18:10:39 +0000 | [diff] [blame] | 72 | { |
| 73 | if (machine_state & MACHINE_SHUTTING_DOWN) |
| 74 | sgi_machine_power_off(); |
| 75 | sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT; |
| 76 | while (1); |
| 77 | } |
| 78 | |
Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 79 | static void __noreturn sgi_machine_halt(void) |
Ralf Baechle | 71baa1a | 2006-01-15 18:10:39 +0000 | [diff] [blame] | 80 | { |
| 81 | if (machine_state & MACHINE_SHUTTING_DOWN) |
| 82 | sgi_machine_power_off(); |
| 83 | ArcEnterInteractiveMode(); |
| 84 | } |
| 85 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 86 | static void power_timeout(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | sgi_machine_power_off(); |
| 89 | } |
| 90 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 91 | static void blink_timeout(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | /* XXX fix this for fullhouse */ |
| 94 | sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF); |
| 95 | sgioc->reset = sgi_ioc_reset; |
| 96 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 97 | mod_timer(&blink_timer, jiffies + blink_timer_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 100 | static void debounce(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | del_timer(&debounce_timer); |
| 103 | if (sgint->istat1 & SGINT_ISTAT1_PWR) { |
| 104 | /* Interrupt still being sent. */ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 105 | debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | add_timer(&debounce_timer); |
| 107 | |
| 108 | sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR | |
| 109 | SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD | |
| 110 | SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD; |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if (machine_state & MACHINE_PANICED) |
| 116 | sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT; |
| 117 | |
| 118 | enable_irq(SGI_PANEL_IRQ); |
| 119 | } |
| 120 | |
| 121 | static inline void power_button(void) |
| 122 | { |
| 123 | if (machine_state & MACHINE_PANICED) |
| 124 | return; |
| 125 | |
Cedric Le Goater | 9ec5209 | 2006-10-02 02:19:00 -0700 | [diff] [blame] | 126 | if ((machine_state & MACHINE_SHUTTING_DOWN) || |
| 127 | kill_cad_pid(SIGINT, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* No init process or button pressed twice. */ |
| 129 | sgi_machine_power_off(); |
| 130 | } |
| 131 | |
| 132 | machine_state |= MACHINE_SHUTTING_DOWN; |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 133 | blink_timer_timeout = POWERDOWN_FREQ; |
| 134 | blink_timeout(&blink_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 136 | timer_setup(&power_timer, power_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ; |
| 138 | add_timer(&power_timer); |
| 139 | } |
| 140 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 141 | static irqreturn_t panel_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | unsigned int buttons; |
| 144 | |
| 145 | buttons = sgioc->panel; |
| 146 | sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR; |
| 147 | |
| 148 | if (sgint->istat1 & SGINT_ISTAT1_PWR) { |
| 149 | /* Wait until interrupt goes away */ |
Ralf Baechle | 7e9e05c | 2009-05-16 12:23:45 +0100 | [diff] [blame] | 150 | disable_irq_nosync(SGI_PANEL_IRQ); |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 151 | timer_setup(&debounce_timer, debounce, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | debounce_timer.expires = jiffies + 5; |
| 153 | add_timer(&debounce_timer); |
| 154 | } |
| 155 | |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 156 | /* Power button was pressed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * ioc.ps page 22: "The Panel Register is called Power Control by Full |
| 158 | * House. Only lowest 2 bits are used. Guiness uses upper four bits |
| 159 | * for volume control". This is not true, all bits are pulled high |
| 160 | * on fullhouse */ |
Thomas Bogendoerfer | b03d7b1 | 2008-07-11 23:03:03 +0200 | [diff] [blame] | 161 | if (!(buttons & SGIOC_PANEL_POWERINTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | power_button(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | return IRQ_HANDLED; |
| 165 | } |
| 166 | |
| 167 | static int panic_event(struct notifier_block *this, unsigned long event, |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 168 | void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | if (machine_state & MACHINE_PANICED) |
| 171 | return NOTIFY_DONE; |
| 172 | machine_state |= MACHINE_PANICED; |
| 173 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 174 | blink_timer_timeout = PANIC_FREQ; |
| 175 | blink_timeout(&blink_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | return NOTIFY_DONE; |
| 178 | } |
| 179 | |
| 180 | static struct notifier_block panic_block = { |
| 181 | .notifier_call = panic_event, |
| 182 | }; |
| 183 | |
| 184 | static int __init reboot_setup(void) |
| 185 | { |
Ralf Baechle | 754d0f2 | 2007-10-14 23:04:27 +0100 | [diff] [blame] | 186 | int res; |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | _machine_restart = sgi_machine_restart; |
| 189 | _machine_halt = sgi_machine_halt; |
Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 190 | pm_power_off = sgi_machine_power_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Ralf Baechle | 754d0f2 | 2007-10-14 23:04:27 +0100 | [diff] [blame] | 192 | res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL); |
| 193 | if (res) { |
| 194 | printk(KERN_ERR "Allocation of front panel IRQ failed\n"); |
| 195 | return res; |
| 196 | } |
| 197 | |
Kees Cook | a66b899 | 2017-10-09 21:42:26 -0700 | [diff] [blame] | 198 | timer_setup(&blink_timer, blink_timeout, 0); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 199 | atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | subsys_initcall(reboot_setup); |