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) 2001 Keith M Wesolowski
78c2ecf20Sopenharmony_ci * Copyright (C) 2001 Paul Mundt
88c2ecf20Sopenharmony_ci * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/compiler.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/sched.h>
168c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
178c2ecf20Sopenharmony_ci#include <linux/notifier.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/rtc/ds1685.h>
208c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
218c2ecf20Sopenharmony_ci#include <linux/pm.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
248c2ecf20Sopenharmony_ci#include <asm/irq.h>
258c2ecf20Sopenharmony_ci#include <asm/reboot.h>
268c2ecf20Sopenharmony_ci#include <asm/wbflush.h>
278c2ecf20Sopenharmony_ci#include <asm/ip32/mace.h>
288c2ecf20Sopenharmony_ci#include <asm/ip32/crime.h>
298c2ecf20Sopenharmony_ci#include <asm/ip32/ip32_ints.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define POWERDOWN_TIMEOUT	120
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * Blink frequency during reboot grace period and when panicked.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define POWERDOWN_FREQ		(HZ / 4)
368c2ecf20Sopenharmony_ci#define PANIC_FREQ		(HZ / 8)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciextern struct platform_device ip32_rtc_device;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct timer_list power_timer, blink_timer;
418c2ecf20Sopenharmony_cistatic unsigned long blink_timer_timeout;
428c2ecf20Sopenharmony_cistatic int has_panicked, shutting_down;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic __noreturn void ip32_poweroff(void *data)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	void (*poweroff_func)(struct platform_device *) =
478c2ecf20Sopenharmony_ci		symbol_get(ds1685_rtc_poweroff);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifdef CONFIG_MODULES
508c2ecf20Sopenharmony_ci	/* If the first __symbol_get failed, our module wasn't loaded. */
518c2ecf20Sopenharmony_ci	if (!poweroff_func) {
528c2ecf20Sopenharmony_ci		request_module("rtc-ds1685");
538c2ecf20Sopenharmony_ci		poweroff_func = symbol_get(ds1685_rtc_poweroff);
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (!poweroff_func)
588c2ecf20Sopenharmony_ci		pr_emerg("RTC not available for power-off.  Spinning forever ...\n");
598c2ecf20Sopenharmony_ci	else {
608c2ecf20Sopenharmony_ci		(*poweroff_func)((struct platform_device *)data);
618c2ecf20Sopenharmony_ci		symbol_put(ds1685_rtc_poweroff);
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	unreachable();
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void ip32_machine_restart(char *cmd) __noreturn;
688c2ecf20Sopenharmony_cistatic void ip32_machine_restart(char *cmd)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	msleep(20);
718c2ecf20Sopenharmony_ci	crime->control = CRIME_CONTROL_HARD_RESET;
728c2ecf20Sopenharmony_ci	unreachable();
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void blink_timeout(struct timer_list *unused)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
788c2ecf20Sopenharmony_ci	mace->perif.ctrl.misc = led;
798c2ecf20Sopenharmony_ci	mod_timer(&blink_timer, jiffies + blink_timer_timeout);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void ip32_machine_halt(void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	ip32_poweroff(&ip32_rtc_device);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic void power_timeout(struct timer_list *unused)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	ip32_poweroff(&ip32_rtc_device);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_civoid ip32_prepare_poweroff(void)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	if (has_panicked)
958c2ecf20Sopenharmony_ci		return;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (shutting_down || kill_cad_pid(SIGINT, 1)) {
988c2ecf20Sopenharmony_ci		/* No init process or button pressed twice.  */
998c2ecf20Sopenharmony_ci		ip32_poweroff(&ip32_rtc_device);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	shutting_down = 1;
1038c2ecf20Sopenharmony_ci	blink_timer_timeout = POWERDOWN_FREQ;
1048c2ecf20Sopenharmony_ci	blink_timeout(&blink_timer);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	timer_setup(&power_timer, power_timeout, 0);
1078c2ecf20Sopenharmony_ci	power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
1088c2ecf20Sopenharmony_ci	add_timer(&power_timer);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int panic_event(struct notifier_block *this, unsigned long event,
1128c2ecf20Sopenharmony_ci		       void *ptr)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	unsigned long led;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (has_panicked)
1178c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
1188c2ecf20Sopenharmony_ci	has_panicked = 1;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* turn off the green LED */
1218c2ecf20Sopenharmony_ci	led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
1228c2ecf20Sopenharmony_ci	mace->perif.ctrl.misc = led;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	blink_timer_timeout = PANIC_FREQ;
1258c2ecf20Sopenharmony_ci	blink_timeout(&blink_timer);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic struct notifier_block panic_block = {
1318c2ecf20Sopenharmony_ci	.notifier_call = panic_event,
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic __init int ip32_reboot_setup(void)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	/* turn on the green led only */
1378c2ecf20Sopenharmony_ci	unsigned long led = mace->perif.ctrl.misc;
1388c2ecf20Sopenharmony_ci	led |= MACEISA_LED_RED;
1398c2ecf20Sopenharmony_ci	led &= ~MACEISA_LED_GREEN;
1408c2ecf20Sopenharmony_ci	mace->perif.ctrl.misc = led;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	_machine_restart = ip32_machine_restart;
1438c2ecf20Sopenharmony_ci	_machine_halt = ip32_machine_halt;
1448c2ecf20Sopenharmony_ci	pm_power_off = ip32_machine_halt;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	timer_setup(&blink_timer, blink_timeout, 0);
1478c2ecf20Sopenharmony_ci	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cisubsys_initcall(ip32_reboot_setup);
153