1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Acquire Single Board Computer Watchdog Timer driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Based on wdt.c. Original copyright messages:
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
8*4882a593Smuzhiyun * All Rights Reserved.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
11*4882a593Smuzhiyun * warranty for any of this software. This material is provided
12*4882a593Smuzhiyun * "AS-IS" and at no charge.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
17*4882a593Smuzhiyun * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
18*4882a593Smuzhiyun * Can't add timeout - driver doesn't allow changing value
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * Theory of Operation:
23*4882a593Smuzhiyun * The Watch-Dog Timer is provided to ensure that standalone
24*4882a593Smuzhiyun * Systems can always recover from catastrophic conditions that
25*4882a593Smuzhiyun * caused the CPU to crash. This condition may have occurred by
26*4882a593Smuzhiyun * external EMI or a software bug. When the CPU stops working
27*4882a593Smuzhiyun * correctly, hardware on the board will either perform a hardware
28*4882a593Smuzhiyun * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
29*4882a593Smuzhiyun * system back to a known state.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * The Watch-Dog Timer is controlled by two I/O Ports.
32*4882a593Smuzhiyun * 443 hex - Read - Enable or refresh the Watch-Dog Timer
33*4882a593Smuzhiyun * 043 hex - Read - Disable the Watch-Dog Timer
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * To enable the Watch-Dog Timer, a read from I/O port 443h must
36*4882a593Smuzhiyun * be performed. This will enable and activate the countdown timer
37*4882a593Smuzhiyun * which will eventually time out and either reset the CPU or cause
38*4882a593Smuzhiyun * an NMI depending on the setting of a jumper. To ensure that this
39*4882a593Smuzhiyun * reset condition does not occur, the Watch-Dog Timer must be
40*4882a593Smuzhiyun * periodically refreshed by reading the same I/O port 443h.
41*4882a593Smuzhiyun * The Watch-Dog Timer is disabled by reading I/O port 043h.
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * The Watch-Dog Timer Time-Out Period is set via jumpers.
44*4882a593Smuzhiyun * It can be 1, 2, 10, 20, 110 or 220 seconds.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Includes, defines, variables, module parameters, ...
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* Includes */
54*4882a593Smuzhiyun #include <linux/module.h> /* For module specific items */
55*4882a593Smuzhiyun #include <linux/moduleparam.h> /* For new moduleparam's */
56*4882a593Smuzhiyun #include <linux/types.h> /* For standard types (like size_t) */
57*4882a593Smuzhiyun #include <linux/errno.h> /* For the -ENODEV/... values */
58*4882a593Smuzhiyun #include <linux/kernel.h> /* For printk/panic/... */
59*4882a593Smuzhiyun #include <linux/miscdevice.h> /* For struct miscdevice */
60*4882a593Smuzhiyun #include <linux/watchdog.h> /* For the watchdog specific items */
61*4882a593Smuzhiyun #include <linux/fs.h> /* For file operations */
62*4882a593Smuzhiyun #include <linux/ioport.h> /* For io-port access */
63*4882a593Smuzhiyun #include <linux/platform_device.h> /* For platform_driver framework */
64*4882a593Smuzhiyun #include <linux/init.h> /* For __init/__exit/... */
65*4882a593Smuzhiyun #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
66*4882a593Smuzhiyun #include <linux/io.h> /* For inb/outb/... */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* Module information */
69*4882a593Smuzhiyun #define DRV_NAME "acquirewdt"
70*4882a593Smuzhiyun #define WATCHDOG_NAME "Acquire WDT"
71*4882a593Smuzhiyun /* There is no way to see what the correct time-out period is */
72*4882a593Smuzhiyun #define WATCHDOG_HEARTBEAT 0
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* internal variables */
75*4882a593Smuzhiyun /* the watchdog platform device */
76*4882a593Smuzhiyun static struct platform_device *acq_platform_device;
77*4882a593Smuzhiyun static unsigned long acq_is_open;
78*4882a593Smuzhiyun static char expect_close;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* module parameters */
81*4882a593Smuzhiyun /* You must set this - there is no sane way to probe for this board. */
82*4882a593Smuzhiyun static int wdt_stop = 0x43;
83*4882a593Smuzhiyun module_param(wdt_stop, int, 0);
84*4882a593Smuzhiyun MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* You must set this - there is no sane way to probe for this board. */
87*4882a593Smuzhiyun static int wdt_start = 0x443;
88*4882a593Smuzhiyun module_param(wdt_start, int, 0);
89*4882a593Smuzhiyun MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
92*4882a593Smuzhiyun module_param(nowayout, bool, 0);
93*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
94*4882a593Smuzhiyun "Watchdog cannot be stopped once started (default="
95*4882a593Smuzhiyun __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * Watchdog Operations
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun
acq_keepalive(void)101*4882a593Smuzhiyun static void acq_keepalive(void)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun /* Write a watchdog value */
104*4882a593Smuzhiyun inb_p(wdt_start);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
acq_stop(void)107*4882a593Smuzhiyun static void acq_stop(void)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun /* Turn the card off */
110*4882a593Smuzhiyun inb_p(wdt_stop);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * /dev/watchdog handling
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun
acq_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)117*4882a593Smuzhiyun static ssize_t acq_write(struct file *file, const char __user *buf,
118*4882a593Smuzhiyun size_t count, loff_t *ppos)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun /* See if we got the magic character 'V' and reload the timer */
121*4882a593Smuzhiyun if (count) {
122*4882a593Smuzhiyun if (!nowayout) {
123*4882a593Smuzhiyun size_t i;
124*4882a593Smuzhiyun /* note: just in case someone wrote the magic character
125*4882a593Smuzhiyun five months ago... */
126*4882a593Smuzhiyun expect_close = 0;
127*4882a593Smuzhiyun /* scan to see whether or not we got the
128*4882a593Smuzhiyun magic character */
129*4882a593Smuzhiyun for (i = 0; i != count; i++) {
130*4882a593Smuzhiyun char c;
131*4882a593Smuzhiyun if (get_user(c, buf + i))
132*4882a593Smuzhiyun return -EFAULT;
133*4882a593Smuzhiyun if (c == 'V')
134*4882a593Smuzhiyun expect_close = 42;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun /* Well, anyhow someone wrote to us, we should
138*4882a593Smuzhiyun return that favour */
139*4882a593Smuzhiyun acq_keepalive();
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun return count;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
acq_ioctl(struct file * file,unsigned int cmd,unsigned long arg)144*4882a593Smuzhiyun static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun int options, retval = -EINVAL;
147*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
148*4882a593Smuzhiyun int __user *p = argp;
149*4882a593Smuzhiyun static const struct watchdog_info ident = {
150*4882a593Smuzhiyun .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
151*4882a593Smuzhiyun .firmware_version = 1,
152*4882a593Smuzhiyun .identity = WATCHDOG_NAME,
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun switch (cmd) {
156*4882a593Smuzhiyun case WDIOC_GETSUPPORT:
157*4882a593Smuzhiyun return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun case WDIOC_GETSTATUS:
160*4882a593Smuzhiyun case WDIOC_GETBOOTSTATUS:
161*4882a593Smuzhiyun return put_user(0, p);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun case WDIOC_SETOPTIONS:
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun if (get_user(options, p))
166*4882a593Smuzhiyun return -EFAULT;
167*4882a593Smuzhiyun if (options & WDIOS_DISABLECARD) {
168*4882a593Smuzhiyun acq_stop();
169*4882a593Smuzhiyun retval = 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun if (options & WDIOS_ENABLECARD) {
172*4882a593Smuzhiyun acq_keepalive();
173*4882a593Smuzhiyun retval = 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun return retval;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun case WDIOC_KEEPALIVE:
178*4882a593Smuzhiyun acq_keepalive();
179*4882a593Smuzhiyun return 0;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun case WDIOC_GETTIMEOUT:
182*4882a593Smuzhiyun return put_user(WATCHDOG_HEARTBEAT, p);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun default:
185*4882a593Smuzhiyun return -ENOTTY;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
acq_open(struct inode * inode,struct file * file)189*4882a593Smuzhiyun static int acq_open(struct inode *inode, struct file *file)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun if (test_and_set_bit(0, &acq_is_open))
192*4882a593Smuzhiyun return -EBUSY;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (nowayout)
195*4882a593Smuzhiyun __module_get(THIS_MODULE);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /* Activate */
198*4882a593Smuzhiyun acq_keepalive();
199*4882a593Smuzhiyun return stream_open(inode, file);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
acq_close(struct inode * inode,struct file * file)202*4882a593Smuzhiyun static int acq_close(struct inode *inode, struct file *file)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun if (expect_close == 42) {
205*4882a593Smuzhiyun acq_stop();
206*4882a593Smuzhiyun } else {
207*4882a593Smuzhiyun pr_crit("Unexpected close, not stopping watchdog!\n");
208*4882a593Smuzhiyun acq_keepalive();
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun clear_bit(0, &acq_is_open);
211*4882a593Smuzhiyun expect_close = 0;
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * Kernel Interfaces
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun static const struct file_operations acq_fops = {
220*4882a593Smuzhiyun .owner = THIS_MODULE,
221*4882a593Smuzhiyun .llseek = no_llseek,
222*4882a593Smuzhiyun .write = acq_write,
223*4882a593Smuzhiyun .unlocked_ioctl = acq_ioctl,
224*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
225*4882a593Smuzhiyun .open = acq_open,
226*4882a593Smuzhiyun .release = acq_close,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun static struct miscdevice acq_miscdev = {
230*4882a593Smuzhiyun .minor = WATCHDOG_MINOR,
231*4882a593Smuzhiyun .name = "watchdog",
232*4882a593Smuzhiyun .fops = &acq_fops,
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * Init & exit routines
237*4882a593Smuzhiyun */
238*4882a593Smuzhiyun
acq_probe(struct platform_device * dev)239*4882a593Smuzhiyun static int __init acq_probe(struct platform_device *dev)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun int ret;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (wdt_stop != wdt_start) {
244*4882a593Smuzhiyun if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
245*4882a593Smuzhiyun pr_err("I/O address 0x%04x already in use\n", wdt_stop);
246*4882a593Smuzhiyun ret = -EIO;
247*4882a593Smuzhiyun goto out;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
252*4882a593Smuzhiyun pr_err("I/O address 0x%04x already in use\n", wdt_start);
253*4882a593Smuzhiyun ret = -EIO;
254*4882a593Smuzhiyun goto unreg_stop;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun ret = misc_register(&acq_miscdev);
257*4882a593Smuzhiyun if (ret != 0) {
258*4882a593Smuzhiyun pr_err("cannot register miscdev on minor=%d (err=%d)\n",
259*4882a593Smuzhiyun WATCHDOG_MINOR, ret);
260*4882a593Smuzhiyun goto unreg_regions;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun pr_info("initialized. (nowayout=%d)\n", nowayout);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun return 0;
265*4882a593Smuzhiyun unreg_regions:
266*4882a593Smuzhiyun release_region(wdt_start, 1);
267*4882a593Smuzhiyun unreg_stop:
268*4882a593Smuzhiyun if (wdt_stop != wdt_start)
269*4882a593Smuzhiyun release_region(wdt_stop, 1);
270*4882a593Smuzhiyun out:
271*4882a593Smuzhiyun return ret;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
acq_remove(struct platform_device * dev)274*4882a593Smuzhiyun static int acq_remove(struct platform_device *dev)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun misc_deregister(&acq_miscdev);
277*4882a593Smuzhiyun release_region(wdt_start, 1);
278*4882a593Smuzhiyun if (wdt_stop != wdt_start)
279*4882a593Smuzhiyun release_region(wdt_stop, 1);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return 0;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
acq_shutdown(struct platform_device * dev)284*4882a593Smuzhiyun static void acq_shutdown(struct platform_device *dev)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun /* Turn the WDT off if we have a soft shutdown */
287*4882a593Smuzhiyun acq_stop();
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun static struct platform_driver acquirewdt_driver = {
291*4882a593Smuzhiyun .remove = acq_remove,
292*4882a593Smuzhiyun .shutdown = acq_shutdown,
293*4882a593Smuzhiyun .driver = {
294*4882a593Smuzhiyun .name = DRV_NAME,
295*4882a593Smuzhiyun },
296*4882a593Smuzhiyun };
297*4882a593Smuzhiyun
acq_init(void)298*4882a593Smuzhiyun static int __init acq_init(void)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun int err;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun pr_info("WDT driver for Acquire single board computer initialising\n");
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun acq_platform_device = platform_device_register_simple(DRV_NAME,
305*4882a593Smuzhiyun -1, NULL, 0);
306*4882a593Smuzhiyun if (IS_ERR(acq_platform_device))
307*4882a593Smuzhiyun return PTR_ERR(acq_platform_device);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun err = platform_driver_probe(&acquirewdt_driver, acq_probe);
310*4882a593Smuzhiyun if (err)
311*4882a593Smuzhiyun goto unreg_platform_device;
312*4882a593Smuzhiyun return 0;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun unreg_platform_device:
315*4882a593Smuzhiyun platform_device_unregister(acq_platform_device);
316*4882a593Smuzhiyun return err;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
acq_exit(void)319*4882a593Smuzhiyun static void __exit acq_exit(void)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun platform_device_unregister(acq_platform_device);
322*4882a593Smuzhiyun platform_driver_unregister(&acquirewdt_driver);
323*4882a593Smuzhiyun pr_info("Watchdog Module Unloaded\n");
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun module_init(acq_init);
327*4882a593Smuzhiyun module_exit(acq_exit);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun MODULE_AUTHOR("David Woodhouse");
330*4882a593Smuzhiyun MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
331*4882a593Smuzhiyun MODULE_LICENSE("GPL");
332