blob: bc6f333565d3b669d83d842c6884b14d8b7f74ea [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Acquire Single Board Computer Watchdog Timer driver
4 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00005 * Based on wdt.c. Original copyright messages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Alan Cox29fa0582008-10-27 15:17:56 +00007 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
8 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
11 * warranty for any of this software. This material is provided
12 * "AS-IS" and at no charge.
13 *
Alan Cox29fa0582008-10-27 15:17:56 +000014 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000016 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
17 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
18 * Can't add timeout - driver doesn't allow changing value
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
21/*
22 * Theory of Operation:
23 * The Watch-Dog Timer is provided to ensure that standalone
24 * Systems can always recover from catastrophic conditions that
Lucas De Marchi25985ed2011-03-30 22:57:33 -030025 * caused the CPU to crash. This condition may have occurred by
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * external EMI or a software bug. When the CPU stops working
27 * correctly, hardware on the board will either perform a hardware
28 * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
29 * system back to a known state.
30 *
31 * The Watch-Dog Timer is controlled by two I/O Ports.
32 * 443 hex - Read - Enable or refresh the Watch-Dog Timer
33 * 043 hex - Read - Disable the Watch-Dog Timer
34 *
35 * To enable the Watch-Dog Timer, a read from I/O port 443h must
36 * be performed. This will enable and activate the countdown timer
37 * which will eventually time out and either reset the CPU or cause
38 * an NMI depending on the setting of a jumper. To ensure that this
39 * reset condition does not occur, the Watch-Dog Timer must be
40 * periodically refreshed by reading the same I/O port 443h.
41 * The Watch-Dog Timer is disabled by reading I/O port 043h.
42 *
43 * The Watch-Dog Timer Time-Out Period is set via jumpers.
44 * It can be 1, 2, 10, 20, 110 or 220 seconds.
45 */
46
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010047/*
48 * Includes, defines, variables, module parameters, ...
49 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Joe Perches27c766a2012-02-15 15:06:19 -080051#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010053/* Includes */
54#include <linux/module.h> /* For module specific items */
55#include <linux/moduleparam.h> /* For new moduleparam's */
56#include <linux/types.h> /* For standard types (like size_t) */
57#include <linux/errno.h> /* For the -ENODEV/... values */
58#include <linux/kernel.h> /* For printk/panic/... */
Jean Delvare487722c2013-10-21 17:38:49 +020059#include <linux/miscdevice.h> /* For struct miscdevice */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010060#include <linux/watchdog.h> /* For the watchdog specific items */
61#include <linux/fs.h> /* For file operations */
62#include <linux/ioport.h> /* For io-port access */
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +010063#include <linux/platform_device.h> /* For platform_driver framework */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010064#include <linux/init.h> /* For __init/__exit/... */
Alan Cox94da1e22008-05-19 14:04:46 +010065#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
66#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010067
68/* Module information */
69#define DRV_NAME "acquirewdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define WATCHDOG_NAME "Acquire WDT"
Alan Cox94da1e22008-05-19 14:04:46 +010071/* There is no way to see what the correct time-out period is */
72#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010074/* internal variables */
Alan Cox94da1e22008-05-19 14:04:46 +010075/* the watchdog platform device */
76static struct platform_device *acq_platform_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static unsigned long acq_is_open;
78static char expect_close;
79
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010080/* module parameters */
Alan Cox94da1e22008-05-19 14:04:46 +010081/* You must set this - there is no sane way to probe for this board. */
82static int wdt_stop = 0x43;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083module_param(wdt_stop, int, 0);
84MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
85
Alan Cox94da1e22008-05-19 14:04:46 +010086/* You must set this - there is no sane way to probe for this board. */
87static int wdt_start = 0x443;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088module_param(wdt_start, int, 0);
89MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
90
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010091static bool nowayout = WATCHDOG_NOWAYOUT;
92module_param(nowayout, bool, 0);
Alan Cox94da1e22008-05-19 14:04:46 +010093MODULE_PARM_DESC(nowayout,
94 "Watchdog cannot be stopped once started (default="
95 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010098 * Watchdog Operations
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100
101static void acq_keepalive(void)
102{
103 /* Write a watchdog value */
104 inb_p(wdt_start);
105}
106
107static void acq_stop(void)
108{
109 /* Turn the card off */
110 inb_p(wdt_stop);
111}
112
113/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100114 * /dev/watchdog handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
Alan Cox94da1e22008-05-19 14:04:46 +0100117static ssize_t acq_write(struct file *file, const char __user *buf,
118 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 /* See if we got the magic character 'V' and reload the timer */
Alan Cox94da1e22008-05-19 14:04:46 +0100121 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!nowayout) {
123 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* note: just in case someone wrote the magic character
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000125 five months ago... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 expect_close = 0;
Alan Cox94da1e22008-05-19 14:04:46 +0100127 /* scan to see whether or not we got the
128 magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 for (i = 0; i != count; i++) {
130 char c;
131 if (get_user(c, buf + i))
132 return -EFAULT;
133 if (c == 'V')
134 expect_close = 42;
135 }
136 }
Alan Cox94da1e22008-05-19 14:04:46 +0100137 /* Well, anyhow someone wrote to us, we should
138 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 acq_keepalive();
140 }
141 return count;
142}
143
Alan Cox94da1e22008-05-19 14:04:46 +0100144static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int options, retval = -EINVAL;
147 void __user *argp = (void __user *)arg;
148 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000149 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
151 .firmware_version = 1,
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100152 .identity = WATCHDOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 };
154
Alan Cox94da1e22008-05-19 14:04:46 +0100155 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 case WDIOC_GETSUPPORT:
Alan Cox94da1e22008-05-19 14:04:46 +0100157 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 case WDIOC_GETSTATUS:
160 case WDIOC_GETBOOTSTATUS:
Alan Cox94da1e22008-05-19 14:04:46 +0100161 return put_user(0, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case WDIOC_SETOPTIONS:
164 {
Alan Cox94da1e22008-05-19 14:04:46 +0100165 if (get_user(options, p))
166 return -EFAULT;
167 if (options & WDIOS_DISABLECARD) {
168 acq_stop();
169 retval = 0;
170 }
171 if (options & WDIOS_ENABLECARD) {
172 acq_keepalive();
173 retval = 0;
174 }
175 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000177 case WDIOC_KEEPALIVE:
178 acq_keepalive();
179 return 0;
180
181 case WDIOC_GETTIMEOUT:
182 return put_user(WATCHDOG_HEARTBEAT, p);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 default:
Alan Cox94da1e22008-05-19 14:04:46 +0100185 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187}
188
189static int acq_open(struct inode *inode, struct file *file)
190{
191 if (test_and_set_bit(0, &acq_is_open))
192 return -EBUSY;
193
194 if (nowayout)
195 __module_get(THIS_MODULE);
196
197 /* Activate */
198 acq_keepalive();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300199 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static int acq_close(struct inode *inode, struct file *file)
203{
204 if (expect_close == 42) {
205 acq_stop();
206 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800207 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 acq_keepalive();
209 }
210 clear_bit(0, &acq_is_open);
211 expect_close = 0;
212 return 0;
213}
214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Kernel Interfaces
217 */
218
Arjan van de Ven62322d22006-07-03 00:24:21 -0700219static const struct file_operations acq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .owner = THIS_MODULE,
221 .llseek = no_llseek,
222 .write = acq_write,
Alan Cox94da1e22008-05-19 14:04:46 +0100223 .unlocked_ioctl = acq_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200224 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .open = acq_open,
226 .release = acq_close,
227};
228
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100229static struct miscdevice acq_miscdev = {
230 .minor = WATCHDOG_MINOR,
231 .name = "watchdog",
232 .fops = &acq_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
235/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100236 * Init & exit routines
237 */
238
Jean Delvareb0e0b4b2014-03-14 13:04:37 +0100239static int __init acq_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int ret;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (wdt_stop != wdt_start) {
244 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800245 pr_err("I/O address 0x%04x already in use\n", wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ret = -EIO;
247 goto out;
248 }
249 }
250
251 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800252 pr_err("I/O address 0x%04x already in use\n", wdt_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ret = -EIO;
254 goto unreg_stop;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ret = misc_register(&acq_miscdev);
257 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800258 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
259 WATCHDOG_MINOR, ret);
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100260 goto unreg_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Joe Perches27c766a2012-02-15 15:06:19 -0800262 pr_info("initialized. (nowayout=%d)\n", nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265unreg_regions:
266 release_region(wdt_start, 1);
267unreg_stop:
268 if (wdt_stop != wdt_start)
269 release_region(wdt_stop, 1);
270out:
271 return ret;
272}
273
Bill Pemberton4b12b892012-11-19 13:26:24 -0500274static int acq_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 misc_deregister(&acq_miscdev);
Alan Cox94da1e22008-05-19 14:04:46 +0100277 release_region(wdt_start, 1);
278 if (wdt_stop != wdt_start)
279 release_region(wdt_stop, 1);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100280
281 return 0;
282}
283
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100284static void acq_shutdown(struct platform_device *dev)
285{
286 /* Turn the WDT off if we have a soft shutdown */
287 acq_stop();
288}
289
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100290static struct platform_driver acquirewdt_driver = {
Bill Pemberton82268712012-11-19 13:21:12 -0500291 .remove = acq_remove,
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100292 .shutdown = acq_shutdown,
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100293 .driver = {
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100294 .name = DRV_NAME,
295 },
296};
297
298static int __init acq_init(void)
299{
300 int err;
301
Joe Perches27c766a2012-02-15 15:06:19 -0800302 pr_info("WDT driver for Acquire single board computer initialising\n");
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100303
Alan Cox94da1e22008-05-19 14:04:46 +0100304 acq_platform_device = platform_device_register_simple(DRV_NAME,
305 -1, NULL, 0);
Jean Delvareb0e0b4b2014-03-14 13:04:37 +0100306 if (IS_ERR(acq_platform_device))
307 return PTR_ERR(acq_platform_device);
308
309 err = platform_driver_probe(&acquirewdt_driver, acq_probe);
310 if (err)
311 goto unreg_platform_device;
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100312 return 0;
313
Jean Delvareb0e0b4b2014-03-14 13:04:37 +0100314unreg_platform_device:
315 platform_device_unregister(acq_platform_device);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100316 return err;
317}
318
319static void __exit acq_exit(void)
320{
321 platform_device_unregister(acq_platform_device);
322 platform_driver_unregister(&acquirewdt_driver);
Joe Perches27c766a2012-02-15 15:06:19 -0800323 pr_info("Watchdog Module Unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326module_init(acq_init);
327module_exit(acq_exit);
328
329MODULE_AUTHOR("David Woodhouse");
330MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
331MODULE_LICENSE("GPL");