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