xref: /OK3568_Linux_fs/kernel/drivers/watchdog/sbc7240_wdt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	NANO7240 SBC Watchdog device driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	Based on w83877f.c by Scott Jennings,
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *	(c) Copyright 2007  Gilles GIGAN <gilles.gigan@jcu.edu.au>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/ioport.h>
15*4882a593Smuzhiyun #include <linux/jiffies.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/moduleparam.h>
18*4882a593Smuzhiyun #include <linux/miscdevice.h>
19*4882a593Smuzhiyun #include <linux/notifier.h>
20*4882a593Smuzhiyun #include <linux/reboot.h>
21*4882a593Smuzhiyun #include <linux/types.h>
22*4882a593Smuzhiyun #include <linux/watchdog.h>
23*4882a593Smuzhiyun #include <linux/io.h>
24*4882a593Smuzhiyun #include <linux/uaccess.h>
25*4882a593Smuzhiyun #include <linux/atomic.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define SBC7240_ENABLE_PORT		0x443
28*4882a593Smuzhiyun #define SBC7240_DISABLE_PORT		0x043
29*4882a593Smuzhiyun #define SBC7240_SET_TIMEOUT_PORT	SBC7240_ENABLE_PORT
30*4882a593Smuzhiyun #define SBC7240_MAGIC_CHAR		'V'
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define SBC7240_TIMEOUT		30
33*4882a593Smuzhiyun #define SBC7240_MAX_TIMEOUT		255
34*4882a593Smuzhiyun static int timeout = SBC7240_TIMEOUT;	/* in seconds */
35*4882a593Smuzhiyun module_param(timeout, int, 0);
36*4882a593Smuzhiyun MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
37*4882a593Smuzhiyun 		 __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
38*4882a593Smuzhiyun 		 __MODULE_STRING(SBC7240_TIMEOUT) ")");
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
41*4882a593Smuzhiyun module_param(nowayout, bool, 0);
42*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define SBC7240_OPEN_STATUS_BIT		0
45*4882a593Smuzhiyun #define SBC7240_ENABLED_STATUS_BIT	1
46*4882a593Smuzhiyun #define SBC7240_EXPECT_CLOSE_STATUS_BIT	2
47*4882a593Smuzhiyun static unsigned long wdt_status;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * Utility routines
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
wdt_disable(void)53*4882a593Smuzhiyun static void wdt_disable(void)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	/* disable the watchdog */
56*4882a593Smuzhiyun 	if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
57*4882a593Smuzhiyun 		inb_p(SBC7240_DISABLE_PORT);
58*4882a593Smuzhiyun 		pr_info("Watchdog timer is now disabled\n");
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
wdt_enable(void)62*4882a593Smuzhiyun static void wdt_enable(void)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	/* enable the watchdog */
65*4882a593Smuzhiyun 	if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
66*4882a593Smuzhiyun 		inb_p(SBC7240_ENABLE_PORT);
67*4882a593Smuzhiyun 		pr_info("Watchdog timer is now enabled\n");
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
wdt_set_timeout(int t)71*4882a593Smuzhiyun static int wdt_set_timeout(int t)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
74*4882a593Smuzhiyun 		pr_err("timeout value must be 1<=x<=%d\n", SBC7240_MAX_TIMEOUT);
75*4882a593Smuzhiyun 		return -1;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	/* set the timeout */
78*4882a593Smuzhiyun 	outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
79*4882a593Smuzhiyun 	timeout = t;
80*4882a593Smuzhiyun 	pr_info("timeout set to %d seconds\n", t);
81*4882a593Smuzhiyun 	return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* Whack the dog */
wdt_keepalive(void)85*4882a593Smuzhiyun static inline void wdt_keepalive(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
88*4882a593Smuzhiyun 		inb_p(SBC7240_ENABLE_PORT);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun  * /dev/watchdog handling
93*4882a593Smuzhiyun  */
fop_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)94*4882a593Smuzhiyun static ssize_t fop_write(struct file *file, const char __user *buf,
95*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	size_t i;
98*4882a593Smuzhiyun 	char c;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (count) {
101*4882a593Smuzhiyun 		if (!nowayout) {
102*4882a593Smuzhiyun 			clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
103*4882a593Smuzhiyun 				&wdt_status);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 			/* is there a magic char ? */
106*4882a593Smuzhiyun 			for (i = 0; i != count; i++) {
107*4882a593Smuzhiyun 				if (get_user(c, buf + i))
108*4882a593Smuzhiyun 					return -EFAULT;
109*4882a593Smuzhiyun 				if (c == SBC7240_MAGIC_CHAR) {
110*4882a593Smuzhiyun 					set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
111*4882a593Smuzhiyun 						&wdt_status);
112*4882a593Smuzhiyun 					break;
113*4882a593Smuzhiyun 				}
114*4882a593Smuzhiyun 			}
115*4882a593Smuzhiyun 		}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 		wdt_keepalive();
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return count;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
fop_open(struct inode * inode,struct file * file)123*4882a593Smuzhiyun static int fop_open(struct inode *inode, struct file *file)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
126*4882a593Smuzhiyun 		return -EBUSY;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	wdt_enable();
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	return stream_open(inode, file);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
fop_close(struct inode * inode,struct file * file)133*4882a593Smuzhiyun static int fop_close(struct inode *inode, struct file *file)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
136*4882a593Smuzhiyun 	    || !nowayout) {
137*4882a593Smuzhiyun 		wdt_disable();
138*4882a593Smuzhiyun 	} else {
139*4882a593Smuzhiyun 		pr_crit("Unexpected close, not stopping watchdog!\n");
140*4882a593Smuzhiyun 		wdt_keepalive();
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun static const struct watchdog_info ident = {
148*4882a593Smuzhiyun 	.options = WDIOF_KEEPALIVEPING|
149*4882a593Smuzhiyun 		   WDIOF_SETTIMEOUT|
150*4882a593Smuzhiyun 		   WDIOF_MAGICCLOSE,
151*4882a593Smuzhiyun 	.firmware_version = 1,
152*4882a593Smuzhiyun 	.identity = "SBC7240",
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 
fop_ioctl(struct file * file,unsigned int cmd,unsigned long arg)156*4882a593Smuzhiyun static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	switch (cmd) {
159*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
160*4882a593Smuzhiyun 		return copy_to_user((void __user *)arg, &ident, sizeof(ident))
161*4882a593Smuzhiyun 						 ? -EFAULT : 0;
162*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
163*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
164*4882a593Smuzhiyun 		return put_user(0, (int __user *)arg);
165*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
166*4882a593Smuzhiyun 	{
167*4882a593Smuzhiyun 		int options;
168*4882a593Smuzhiyun 		int retval = -EINVAL;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		if (get_user(options, (int __user *)arg))
171*4882a593Smuzhiyun 			return -EFAULT;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		if (options & WDIOS_DISABLECARD) {
174*4882a593Smuzhiyun 			wdt_disable();
175*4882a593Smuzhiyun 			retval = 0;
176*4882a593Smuzhiyun 		}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		if (options & WDIOS_ENABLECARD) {
179*4882a593Smuzhiyun 			wdt_enable();
180*4882a593Smuzhiyun 			retval = 0;
181*4882a593Smuzhiyun 		}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		return retval;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
186*4882a593Smuzhiyun 		wdt_keepalive();
187*4882a593Smuzhiyun 		return 0;
188*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
189*4882a593Smuzhiyun 	{
190*4882a593Smuzhiyun 		int new_timeout;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 		if (get_user(new_timeout, (int __user *)arg))
193*4882a593Smuzhiyun 			return -EFAULT;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 		if (wdt_set_timeout(new_timeout))
196*4882a593Smuzhiyun 			return -EINVAL;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 		fallthrough;
199*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
200*4882a593Smuzhiyun 		return put_user(timeout, (int __user *)arg);
201*4882a593Smuzhiyun 	default:
202*4882a593Smuzhiyun 		return -ENOTTY;
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun static const struct file_operations wdt_fops = {
207*4882a593Smuzhiyun 	.owner = THIS_MODULE,
208*4882a593Smuzhiyun 	.llseek = no_llseek,
209*4882a593Smuzhiyun 	.write = fop_write,
210*4882a593Smuzhiyun 	.open = fop_open,
211*4882a593Smuzhiyun 	.release = fop_close,
212*4882a593Smuzhiyun 	.unlocked_ioctl = fop_ioctl,
213*4882a593Smuzhiyun 	.compat_ioctl = compat_ptr_ioctl,
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun static struct miscdevice wdt_miscdev = {
217*4882a593Smuzhiyun 	.minor = WATCHDOG_MINOR,
218*4882a593Smuzhiyun 	.name = "watchdog",
219*4882a593Smuzhiyun 	.fops = &wdt_fops,
220*4882a593Smuzhiyun };
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun  *	Notifier for system down
224*4882a593Smuzhiyun  */
225*4882a593Smuzhiyun 
wdt_notify_sys(struct notifier_block * this,unsigned long code,void * unused)226*4882a593Smuzhiyun static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
227*4882a593Smuzhiyun 			  void *unused)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
230*4882a593Smuzhiyun 		wdt_disable();
231*4882a593Smuzhiyun 	return NOTIFY_DONE;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun static struct notifier_block wdt_notifier = {
235*4882a593Smuzhiyun 	.notifier_call = wdt_notify_sys,
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun 
sbc7240_wdt_unload(void)238*4882a593Smuzhiyun static void __exit sbc7240_wdt_unload(void)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	pr_info("Removing watchdog\n");
241*4882a593Smuzhiyun 	misc_deregister(&wdt_miscdev);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt_notifier);
244*4882a593Smuzhiyun 	release_region(SBC7240_ENABLE_PORT, 1);
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
sbc7240_wdt_init(void)247*4882a593Smuzhiyun static int __init sbc7240_wdt_init(void)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	int rc = -EBUSY;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
252*4882a593Smuzhiyun 		pr_err("I/O address 0x%04x already in use\n",
253*4882a593Smuzhiyun 		       SBC7240_ENABLE_PORT);
254*4882a593Smuzhiyun 		rc = -EIO;
255*4882a593Smuzhiyun 		goto err_out;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* The IO port 0x043 used to disable the watchdog
259*4882a593Smuzhiyun 	 * is already claimed by the system timer, so we
260*4882a593Smuzhiyun 	 * can't request_region() it ...*/
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
263*4882a593Smuzhiyun 		timeout = SBC7240_TIMEOUT;
264*4882a593Smuzhiyun 		pr_info("timeout value must be 1<=x<=%d, using %d\n",
265*4882a593Smuzhiyun 			SBC7240_MAX_TIMEOUT, timeout);
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 	wdt_set_timeout(timeout);
268*4882a593Smuzhiyun 	wdt_disable();
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	rc = register_reboot_notifier(&wdt_notifier);
271*4882a593Smuzhiyun 	if (rc) {
272*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", rc);
273*4882a593Smuzhiyun 		goto err_out_region;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	rc = misc_register(&wdt_miscdev);
277*4882a593Smuzhiyun 	if (rc) {
278*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
279*4882a593Smuzhiyun 		       wdt_miscdev.minor, rc);
280*4882a593Smuzhiyun 		goto err_out_reboot_notifier;
281*4882a593Smuzhiyun 	}
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
284*4882a593Smuzhiyun 		nowayout);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	return 0;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun err_out_reboot_notifier:
289*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt_notifier);
290*4882a593Smuzhiyun err_out_region:
291*4882a593Smuzhiyun 	release_region(SBC7240_ENABLE_PORT, 1);
292*4882a593Smuzhiyun err_out:
293*4882a593Smuzhiyun 	return rc;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun module_init(sbc7240_wdt_init);
297*4882a593Smuzhiyun module_exit(sbc7240_wdt_unload);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun MODULE_AUTHOR("Gilles Gigan");
300*4882a593Smuzhiyun MODULE_DESCRIPTION("Watchdog device driver for single board"
301*4882a593Smuzhiyun 		   " computers EPIC Nano 7240 from iEi");
302*4882a593Smuzhiyun MODULE_LICENSE("GPL");
303