xref: /OK3568_Linux_fs/kernel/drivers/watchdog/indydog.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	IndyDog	0.3	A Hardware Watchdog Device for SGI IP22
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	(c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
6*4882a593Smuzhiyun  *						All Rights Reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *	based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/moduleparam.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <linux/mm.h>
19*4882a593Smuzhiyun #include <linux/miscdevice.h>
20*4882a593Smuzhiyun #include <linux/watchdog.h>
21*4882a593Smuzhiyun #include <linux/notifier.h>
22*4882a593Smuzhiyun #include <linux/reboot.h>
23*4882a593Smuzhiyun #include <linux/init.h>
24*4882a593Smuzhiyun #include <linux/uaccess.h>
25*4882a593Smuzhiyun #include <asm/sgi/mc.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static unsigned long indydog_alive;
28*4882a593Smuzhiyun static DEFINE_SPINLOCK(indydog_lock);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define WATCHDOG_TIMEOUT 30		/* 30 sec default timeout */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
33*4882a593Smuzhiyun module_param(nowayout, bool, 0);
34*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
35*4882a593Smuzhiyun 		"Watchdog cannot be stopped once started (default="
36*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
37*4882a593Smuzhiyun 
indydog_start(void)38*4882a593Smuzhiyun static void indydog_start(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	spin_lock(&indydog_lock);
41*4882a593Smuzhiyun 	sgimc->cpuctrl0 |= SGIMC_CCTRL0_WDOG;
42*4882a593Smuzhiyun 	spin_unlock(&indydog_lock);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
indydog_stop(void)45*4882a593Smuzhiyun static void indydog_stop(void)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	spin_lock(&indydog_lock);
48*4882a593Smuzhiyun 	sgimc->cpuctrl0 &= ~SGIMC_CCTRL0_WDOG;
49*4882a593Smuzhiyun 	spin_unlock(&indydog_lock);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	pr_info("Stopped watchdog timer\n");
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
indydog_ping(void)54*4882a593Smuzhiyun static void indydog_ping(void)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	sgimc->watchdogt = 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  *	Allow only one person to hold it open
61*4882a593Smuzhiyun  */
indydog_open(struct inode * inode,struct file * file)62*4882a593Smuzhiyun static int indydog_open(struct inode *inode, struct file *file)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	if (test_and_set_bit(0, &indydog_alive))
65*4882a593Smuzhiyun 		return -EBUSY;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (nowayout)
68*4882a593Smuzhiyun 		__module_get(THIS_MODULE);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* Activate timer */
71*4882a593Smuzhiyun 	indydog_start();
72*4882a593Smuzhiyun 	indydog_ping();
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	pr_info("Started watchdog timer\n");
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return stream_open(inode, file);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
indydog_release(struct inode * inode,struct file * file)79*4882a593Smuzhiyun static int indydog_release(struct inode *inode, struct file *file)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	/* Shut off the timer.
82*4882a593Smuzhiyun 	 * Lock it in if it's a module and we defined ...NOWAYOUT */
83*4882a593Smuzhiyun 	if (!nowayout)
84*4882a593Smuzhiyun 		indydog_stop();		/* Turn the WDT off */
85*4882a593Smuzhiyun 	clear_bit(0, &indydog_alive);
86*4882a593Smuzhiyun 	return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
indydog_write(struct file * file,const char * data,size_t len,loff_t * ppos)89*4882a593Smuzhiyun static ssize_t indydog_write(struct file *file, const char *data,
90*4882a593Smuzhiyun 						size_t len, loff_t *ppos)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	/* Refresh the timer. */
93*4882a593Smuzhiyun 	if (len)
94*4882a593Smuzhiyun 		indydog_ping();
95*4882a593Smuzhiyun 	return len;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
indydog_ioctl(struct file * file,unsigned int cmd,unsigned long arg)98*4882a593Smuzhiyun static long indydog_ioctl(struct file *file, unsigned int cmd,
99*4882a593Smuzhiyun 							unsigned long arg)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	int options, retval = -EINVAL;
102*4882a593Smuzhiyun 	static const struct watchdog_info ident = {
103*4882a593Smuzhiyun 		.options		= WDIOF_KEEPALIVEPING,
104*4882a593Smuzhiyun 		.firmware_version	= 0,
105*4882a593Smuzhiyun 		.identity		= "Hardware Watchdog for SGI IP22",
106*4882a593Smuzhiyun 	};
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	switch (cmd) {
109*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
110*4882a593Smuzhiyun 		if (copy_to_user((struct watchdog_info *)arg,
111*4882a593Smuzhiyun 				 &ident, sizeof(ident)))
112*4882a593Smuzhiyun 			return -EFAULT;
113*4882a593Smuzhiyun 		return 0;
114*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
115*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
116*4882a593Smuzhiyun 		return put_user(0, (int *)arg);
117*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
118*4882a593Smuzhiyun 	{
119*4882a593Smuzhiyun 		if (get_user(options, (int *)arg))
120*4882a593Smuzhiyun 			return -EFAULT;
121*4882a593Smuzhiyun 		if (options & WDIOS_DISABLECARD) {
122*4882a593Smuzhiyun 			indydog_stop();
123*4882a593Smuzhiyun 			retval = 0;
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 		if (options & WDIOS_ENABLECARD) {
126*4882a593Smuzhiyun 			indydog_start();
127*4882a593Smuzhiyun 			retval = 0;
128*4882a593Smuzhiyun 		}
129*4882a593Smuzhiyun 		return retval;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
132*4882a593Smuzhiyun 		indydog_ping();
133*4882a593Smuzhiyun 		return 0;
134*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
135*4882a593Smuzhiyun 		return put_user(WATCHDOG_TIMEOUT, (int *)arg);
136*4882a593Smuzhiyun 	default:
137*4882a593Smuzhiyun 		return -ENOTTY;
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
indydog_notify_sys(struct notifier_block * this,unsigned long code,void * unused)141*4882a593Smuzhiyun static int indydog_notify_sys(struct notifier_block *this,
142*4882a593Smuzhiyun 					unsigned long code, void *unused)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
145*4882a593Smuzhiyun 		indydog_stop();		/* Turn the WDT off */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	return NOTIFY_DONE;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static const struct file_operations indydog_fops = {
151*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
152*4882a593Smuzhiyun 	.llseek		= no_llseek,
153*4882a593Smuzhiyun 	.write		= indydog_write,
154*4882a593Smuzhiyun 	.unlocked_ioctl	= indydog_ioctl,
155*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
156*4882a593Smuzhiyun 	.open		= indydog_open,
157*4882a593Smuzhiyun 	.release	= indydog_release,
158*4882a593Smuzhiyun };
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun static struct miscdevice indydog_miscdev = {
161*4882a593Smuzhiyun 	.minor		= WATCHDOG_MINOR,
162*4882a593Smuzhiyun 	.name		= "watchdog",
163*4882a593Smuzhiyun 	.fops		= &indydog_fops,
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun static struct notifier_block indydog_notifier = {
167*4882a593Smuzhiyun 	.notifier_call = indydog_notify_sys,
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun 
watchdog_init(void)170*4882a593Smuzhiyun static int __init watchdog_init(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	int ret;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	ret = register_reboot_notifier(&indydog_notifier);
175*4882a593Smuzhiyun 	if (ret) {
176*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
177*4882a593Smuzhiyun 		return ret;
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	ret = misc_register(&indydog_miscdev);
181*4882a593Smuzhiyun 	if (ret) {
182*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
183*4882a593Smuzhiyun 		       WATCHDOG_MINOR, ret);
184*4882a593Smuzhiyun 		unregister_reboot_notifier(&indydog_notifier);
185*4882a593Smuzhiyun 		return ret;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n");
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	return 0;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
watchdog_exit(void)193*4882a593Smuzhiyun static void __exit watchdog_exit(void)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	misc_deregister(&indydog_miscdev);
196*4882a593Smuzhiyun 	unregister_reboot_notifier(&indydog_notifier);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun module_init(watchdog_init);
200*4882a593Smuzhiyun module_exit(watchdog_exit);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
203*4882a593Smuzhiyun MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
204*4882a593Smuzhiyun MODULE_LICENSE("GPL");
205