xref: /OK3568_Linux_fs/kernel/drivers/watchdog/sbc_epx_c3.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	SBC EPX C3 0.1	A Hardware Watchdog Device for the Winsystems EPX-C3
4*4882a593Smuzhiyun  *	single board computer
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *	(c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights
7*4882a593Smuzhiyun  *	Reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *	based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/moduleparam.h>
16*4882a593Smuzhiyun #include <linux/types.h>
17*4882a593Smuzhiyun #include <linux/kernel.h>
18*4882a593Smuzhiyun #include <linux/fs.h>
19*4882a593Smuzhiyun #include <linux/mm.h>
20*4882a593Smuzhiyun #include <linux/miscdevice.h>
21*4882a593Smuzhiyun #include <linux/watchdog.h>
22*4882a593Smuzhiyun #include <linux/notifier.h>
23*4882a593Smuzhiyun #include <linux/reboot.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/ioport.h>
26*4882a593Smuzhiyun #include <linux/uaccess.h>
27*4882a593Smuzhiyun #include <linux/io.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static int epx_c3_alive;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define WATCHDOG_TIMEOUT 1		/* 1 sec default timeout */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
34*4882a593Smuzhiyun module_param(nowayout, bool, 0);
35*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
36*4882a593Smuzhiyun 					__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */
39*4882a593Smuzhiyun #define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */
40*4882a593Smuzhiyun 
epx_c3_start(void)41*4882a593Smuzhiyun static void epx_c3_start(void)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	outb(1, EPXC3_WATCHDOG_CTL_REG);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
epx_c3_stop(void)46*4882a593Smuzhiyun static void epx_c3_stop(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	outb(0, EPXC3_WATCHDOG_CTL_REG);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	pr_info("Stopped watchdog timer\n");
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
epx_c3_pet(void)54*4882a593Smuzhiyun static void epx_c3_pet(void)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	outb(1, EPXC3_WATCHDOG_PET_REG);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  *	Allow only one person to hold it open
61*4882a593Smuzhiyun  */
epx_c3_open(struct inode * inode,struct file * file)62*4882a593Smuzhiyun static int epx_c3_open(struct inode *inode, struct file *file)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	if (epx_c3_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 	epx_c3_start();
72*4882a593Smuzhiyun 	epx_c3_pet();
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	epx_c3_alive = 1;
75*4882a593Smuzhiyun 	pr_info("Started watchdog timer\n");
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return stream_open(inode, file);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
epx_c3_release(struct inode * inode,struct file * file)80*4882a593Smuzhiyun static int epx_c3_release(struct inode *inode, struct file *file)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	/* Shut off the timer.
83*4882a593Smuzhiyun 	 * Lock it in if it's a module and we defined ...NOWAYOUT */
84*4882a593Smuzhiyun 	if (!nowayout)
85*4882a593Smuzhiyun 		epx_c3_stop();		/* Turn the WDT off */
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	epx_c3_alive = 0;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
epx_c3_write(struct file * file,const char __user * data,size_t len,loff_t * ppos)92*4882a593Smuzhiyun static ssize_t epx_c3_write(struct file *file, const char __user *data,
93*4882a593Smuzhiyun 			size_t len, loff_t *ppos)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	/* Refresh the timer. */
96*4882a593Smuzhiyun 	if (len)
97*4882a593Smuzhiyun 		epx_c3_pet();
98*4882a593Smuzhiyun 	return len;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
epx_c3_ioctl(struct file * file,unsigned int cmd,unsigned long arg)101*4882a593Smuzhiyun static long epx_c3_ioctl(struct file *file, unsigned int cmd,
102*4882a593Smuzhiyun 						unsigned long arg)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	int options, retval = -EINVAL;
105*4882a593Smuzhiyun 	int __user *argp = (void __user *)arg;
106*4882a593Smuzhiyun 	static const struct watchdog_info ident = {
107*4882a593Smuzhiyun 		.options		= WDIOF_KEEPALIVEPING,
108*4882a593Smuzhiyun 		.firmware_version	= 0,
109*4882a593Smuzhiyun 		.identity		= "Winsystems EPX-C3 H/W Watchdog",
110*4882a593Smuzhiyun 	};
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	switch (cmd) {
113*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
114*4882a593Smuzhiyun 		if (copy_to_user(argp, &ident, sizeof(ident)))
115*4882a593Smuzhiyun 			return -EFAULT;
116*4882a593Smuzhiyun 		return 0;
117*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
118*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
119*4882a593Smuzhiyun 		return put_user(0, argp);
120*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
121*4882a593Smuzhiyun 		if (get_user(options, argp))
122*4882a593Smuzhiyun 			return -EFAULT;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		if (options & WDIOS_DISABLECARD) {
125*4882a593Smuzhiyun 			epx_c3_stop();
126*4882a593Smuzhiyun 			retval = 0;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		if (options & WDIOS_ENABLECARD) {
130*4882a593Smuzhiyun 			epx_c3_start();
131*4882a593Smuzhiyun 			retval = 0;
132*4882a593Smuzhiyun 		}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 		return retval;
135*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
136*4882a593Smuzhiyun 		epx_c3_pet();
137*4882a593Smuzhiyun 		return 0;
138*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
139*4882a593Smuzhiyun 		return put_user(WATCHDOG_TIMEOUT, argp);
140*4882a593Smuzhiyun 	default:
141*4882a593Smuzhiyun 		return -ENOTTY;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
epx_c3_notify_sys(struct notifier_block * this,unsigned long code,void * unused)145*4882a593Smuzhiyun static int epx_c3_notify_sys(struct notifier_block *this, unsigned long code,
146*4882a593Smuzhiyun 				void *unused)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
149*4882a593Smuzhiyun 		epx_c3_stop();		/* Turn the WDT off */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	return NOTIFY_DONE;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static const struct file_operations epx_c3_fops = {
155*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
156*4882a593Smuzhiyun 	.llseek		= no_llseek,
157*4882a593Smuzhiyun 	.write		= epx_c3_write,
158*4882a593Smuzhiyun 	.unlocked_ioctl	= epx_c3_ioctl,
159*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
160*4882a593Smuzhiyun 	.open		= epx_c3_open,
161*4882a593Smuzhiyun 	.release	= epx_c3_release,
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun static struct miscdevice epx_c3_miscdev = {
165*4882a593Smuzhiyun 	.minor		= WATCHDOG_MINOR,
166*4882a593Smuzhiyun 	.name		= "watchdog",
167*4882a593Smuzhiyun 	.fops		= &epx_c3_fops,
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun static struct notifier_block epx_c3_notifier = {
171*4882a593Smuzhiyun 	.notifier_call = epx_c3_notify_sys,
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
watchdog_init(void)174*4882a593Smuzhiyun static int __init watchdog_init(void)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	int ret;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (!request_region(EPXC3_WATCHDOG_CTL_REG, 2, "epxc3_watchdog"))
179*4882a593Smuzhiyun 		return -EBUSY;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	ret = register_reboot_notifier(&epx_c3_notifier);
182*4882a593Smuzhiyun 	if (ret) {
183*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
184*4882a593Smuzhiyun 		goto out;
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	ret = misc_register(&epx_c3_miscdev);
188*4882a593Smuzhiyun 	if (ret) {
189*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
190*4882a593Smuzhiyun 		       WATCHDOG_MINOR, ret);
191*4882a593Smuzhiyun 		unregister_reboot_notifier(&epx_c3_notifier);
192*4882a593Smuzhiyun 		goto out;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	pr_info("Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n");
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun out:
200*4882a593Smuzhiyun 	release_region(EPXC3_WATCHDOG_CTL_REG, 2);
201*4882a593Smuzhiyun 	return ret;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
watchdog_exit(void)204*4882a593Smuzhiyun static void __exit watchdog_exit(void)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	misc_deregister(&epx_c3_miscdev);
207*4882a593Smuzhiyun 	unregister_reboot_notifier(&epx_c3_notifier);
208*4882a593Smuzhiyun 	release_region(EPXC3_WATCHDOG_CTL_REG, 2);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun module_init(watchdog_init);
212*4882a593Smuzhiyun module_exit(watchdog_exit);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>");
215*4882a593Smuzhiyun MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC.  "
216*4882a593Smuzhiyun 	"Note that there is no way to probe for this device -- "
217*4882a593Smuzhiyun 	"so only use it if you are *sure* you are running on this specific "
218*4882a593Smuzhiyun 	"SBC system from Winsystems!  It writes to IO ports 0x1ee and 0x1ef!");
219*4882a593Smuzhiyun MODULE_LICENSE("GPL");
220