18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/linkage.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/rtc/ds1286.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
148c2ecf20Sopenharmony_ci#include <linux/notifier.h>
158c2ecf20Sopenharmony_ci#include <linux/pm.h>
168c2ecf20Sopenharmony_ci#include <linux/timer.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/io.h>
198c2ecf20Sopenharmony_ci#include <asm/irq.h>
208c2ecf20Sopenharmony_ci#include <asm/reboot.h>
218c2ecf20Sopenharmony_ci#include <asm/sgialib.h>
228c2ecf20Sopenharmony_ci#include <asm/sgi/ioc.h>
238c2ecf20Sopenharmony_ci#include <asm/sgi/hpc3.h>
248c2ecf20Sopenharmony_ci#include <asm/sgi/mc.h>
258c2ecf20Sopenharmony_ci#include <asm/sgi/ip22.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds.
298c2ecf20Sopenharmony_ci * I'm not sure if this feature is a good idea, for now it's here just to
308c2ecf20Sopenharmony_ci * make the power button make behave just like under IRIX.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#define POWERDOWN_TIMEOUT	120
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Blink frequency during reboot grace period and when panicked.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#define POWERDOWN_FREQ		(HZ / 4)
388c2ecf20Sopenharmony_ci#define PANIC_FREQ		(HZ / 8)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct timer_list power_timer, blink_timer, debounce_timer;
418c2ecf20Sopenharmony_cistatic unsigned long blink_timer_timeout;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define MACHINE_PANICED		1
448c2ecf20Sopenharmony_ci#define MACHINE_SHUTTING_DOWN	2
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int machine_state;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void __noreturn sgi_machine_power_off(void)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	unsigned int tmp;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	local_irq_disable();
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* Disable watchdog */
558c2ecf20Sopenharmony_ci	tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff;
568c2ecf20Sopenharmony_ci	hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM;
578c2ecf20Sopenharmony_ci	hpc3c0->rtcregs[RTC_WSEC] = 0;
588c2ecf20Sopenharmony_ci	hpc3c0->rtcregs[RTC_WHSEC] = 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	while (1) {
618c2ecf20Sopenharmony_ci		sgioc->panel = ~SGIOC_PANEL_POWERON;
628c2ecf20Sopenharmony_ci		/* Good bye cruel world ...  */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		/* If we're still running, we probably got sent an alarm
658c2ecf20Sopenharmony_ci		   interrupt.  Read the flag to clear it.  */
668c2ecf20Sopenharmony_ci		tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM];
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void __noreturn sgi_machine_restart(char *command)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	if (machine_state & MACHINE_SHUTTING_DOWN)
738c2ecf20Sopenharmony_ci		sgi_machine_power_off();
748c2ecf20Sopenharmony_ci	sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
758c2ecf20Sopenharmony_ci	while (1);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void __noreturn sgi_machine_halt(void)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (machine_state & MACHINE_SHUTTING_DOWN)
818c2ecf20Sopenharmony_ci		sgi_machine_power_off();
828c2ecf20Sopenharmony_ci	ArcEnterInteractiveMode();
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void power_timeout(struct timer_list *unused)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	sgi_machine_power_off();
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void blink_timeout(struct timer_list *unused)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	/* XXX fix this for fullhouse  */
938c2ecf20Sopenharmony_ci	sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF);
948c2ecf20Sopenharmony_ci	sgioc->reset = sgi_ioc_reset;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	mod_timer(&blink_timer, jiffies + blink_timer_timeout);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic void debounce(struct timer_list *unused)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	del_timer(&debounce_timer);
1028c2ecf20Sopenharmony_ci	if (sgint->istat1 & SGINT_ISTAT1_PWR) {
1038c2ecf20Sopenharmony_ci		/* Interrupt still being sent. */
1048c2ecf20Sopenharmony_ci		debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s	*/
1058c2ecf20Sopenharmony_ci		add_timer(&debounce_timer);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR |
1088c2ecf20Sopenharmony_ci			       SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD |
1098c2ecf20Sopenharmony_ci			       SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		return;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (machine_state & MACHINE_PANICED)
1158c2ecf20Sopenharmony_ci		sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	enable_irq(SGI_PANEL_IRQ);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline void power_button(void)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	if (machine_state & MACHINE_PANICED)
1238c2ecf20Sopenharmony_ci		return;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if ((machine_state & MACHINE_SHUTTING_DOWN) ||
1268c2ecf20Sopenharmony_ci			kill_cad_pid(SIGINT, 1)) {
1278c2ecf20Sopenharmony_ci		/* No init process or button pressed twice.  */
1288c2ecf20Sopenharmony_ci		sgi_machine_power_off();
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	machine_state |= MACHINE_SHUTTING_DOWN;
1328c2ecf20Sopenharmony_ci	blink_timer_timeout = POWERDOWN_FREQ;
1338c2ecf20Sopenharmony_ci	blink_timeout(&blink_timer);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	timer_setup(&power_timer, power_timeout, 0);
1368c2ecf20Sopenharmony_ci	power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
1378c2ecf20Sopenharmony_ci	add_timer(&power_timer);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic irqreturn_t panel_int(int irq, void *dev_id)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	unsigned int buttons;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	buttons = sgioc->panel;
1458c2ecf20Sopenharmony_ci	sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (sgint->istat1 & SGINT_ISTAT1_PWR) {
1488c2ecf20Sopenharmony_ci		/* Wait until interrupt goes away */
1498c2ecf20Sopenharmony_ci		disable_irq_nosync(SGI_PANEL_IRQ);
1508c2ecf20Sopenharmony_ci		timer_setup(&debounce_timer, debounce, 0);
1518c2ecf20Sopenharmony_ci		debounce_timer.expires = jiffies + 5;
1528c2ecf20Sopenharmony_ci		add_timer(&debounce_timer);
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Power button was pressed
1568c2ecf20Sopenharmony_ci	 * ioc.ps page 22: "The Panel Register is called Power Control by Full
1578c2ecf20Sopenharmony_ci	 * House. Only lowest 2 bits are used. Guiness uses upper four bits
1588c2ecf20Sopenharmony_ci	 * for volume control". This is not true, all bits are pulled high
1598c2ecf20Sopenharmony_ci	 * on fullhouse */
1608c2ecf20Sopenharmony_ci	if (!(buttons & SGIOC_PANEL_POWERINTR))
1618c2ecf20Sopenharmony_ci		power_button();
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int panic_event(struct notifier_block *this, unsigned long event,
1678c2ecf20Sopenharmony_ci		      void *ptr)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	if (machine_state & MACHINE_PANICED)
1708c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
1718c2ecf20Sopenharmony_ci	machine_state |= MACHINE_PANICED;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	blink_timer_timeout = PANIC_FREQ;
1748c2ecf20Sopenharmony_ci	blink_timeout(&blink_timer);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic struct notifier_block panic_block = {
1808c2ecf20Sopenharmony_ci	.notifier_call	= panic_event,
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int __init reboot_setup(void)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	int res;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	_machine_restart = sgi_machine_restart;
1888c2ecf20Sopenharmony_ci	_machine_halt = sgi_machine_halt;
1898c2ecf20Sopenharmony_ci	pm_power_off = sgi_machine_power_off;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
1928c2ecf20Sopenharmony_ci	if (res) {
1938c2ecf20Sopenharmony_ci		printk(KERN_ERR "Allocation of front panel IRQ failed\n");
1948c2ecf20Sopenharmony_ci		return res;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	timer_setup(&blink_timer, blink_timeout, 0);
1988c2ecf20Sopenharmony_ci	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cisubsys_initcall(reboot_setup);
204