blob: 3e57aa4d643a33035fb7f6f37a494ac9148211de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Acquire Single Board Computer Watchdog Timer driver
3 *
4 * Based on wdt.c. Original copyright messages:
5 *
Alan Cox29fa0582008-10-27 15:17:56 +00006 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
7 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
15 * warranty for any of this software. This material is provided
16 * "AS-IS" and at no charge.
17 *
Alan Cox29fa0582008-10-27 15:17:56 +000018 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
21 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
22 * Can't add timeout - driver doesn't allow changing value
23 */
24
25/*
26 * Theory of Operation:
27 * The Watch-Dog Timer is provided to ensure that standalone
28 * Systems can always recover from catastrophic conditions that
29 * caused the CPU to crash. This condition may have occured by
30 * external EMI or a software bug. When the CPU stops working
31 * correctly, hardware on the board will either perform a hardware
32 * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
33 * system back to a known state.
34 *
35 * The Watch-Dog Timer is controlled by two I/O Ports.
36 * 443 hex - Read - Enable or refresh the Watch-Dog Timer
37 * 043 hex - Read - Disable the Watch-Dog Timer
38 *
39 * To enable the Watch-Dog Timer, a read from I/O port 443h must
40 * be performed. This will enable and activate the countdown timer
41 * which will eventually time out and either reset the CPU or cause
42 * an NMI depending on the setting of a jumper. To ensure that this
43 * reset condition does not occur, the Watch-Dog Timer must be
44 * periodically refreshed by reading the same I/O port 443h.
45 * The Watch-Dog Timer is disabled by reading I/O port 043h.
46 *
47 * The Watch-Dog Timer Time-Out Period is set via jumpers.
48 * It can be 1, 2, 10, 20, 110 or 220 seconds.
49 */
50
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010051/*
52 * Includes, defines, variables, module parameters, ...
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010055/* Includes */
56#include <linux/module.h> /* For module specific items */
57#include <linux/moduleparam.h> /* For new moduleparam's */
58#include <linux/types.h> /* For standard types (like size_t) */
59#include <linux/errno.h> /* For the -ENODEV/... values */
60#include <linux/kernel.h> /* For printk/panic/... */
Alan Cox94da1e22008-05-19 14:04:46 +010061#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
62 (WATCHDOG_MINOR) */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010063#include <linux/watchdog.h> /* For the watchdog specific items */
64#include <linux/fs.h> /* For file operations */
65#include <linux/ioport.h> /* For io-port access */
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +010066#include <linux/platform_device.h> /* For platform_driver framework */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010067#include <linux/init.h> /* For __init/__exit/... */
Alan Cox94da1e22008-05-19 14:04:46 +010068#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
69#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010070
71/* Module information */
72#define DRV_NAME "acquirewdt"
73#define PFX DRV_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define WATCHDOG_NAME "Acquire WDT"
Alan Cox94da1e22008-05-19 14:04:46 +010075/* There is no way to see what the correct time-out period is */
76#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010078/* internal variables */
Alan Cox94da1e22008-05-19 14:04:46 +010079/* the watchdog platform device */
80static struct platform_device *acq_platform_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static unsigned long acq_is_open;
82static char expect_close;
83
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010084/* module parameters */
Alan Cox94da1e22008-05-19 14:04:46 +010085/* You must set this - there is no sane way to probe for this board. */
86static int wdt_stop = 0x43;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(wdt_stop, int, 0);
88MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
89
Alan Cox94da1e22008-05-19 14:04:46 +010090/* You must set this - there is no sane way to probe for this board. */
91static int wdt_start = 0x443;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param(wdt_start, int, 0);
93MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
94
Andrey Panin4bfdf372005-07-27 11:43:58 -070095static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(nowayout, int, 0);
Alan Cox94da1e22008-05-19 14:04:46 +010097MODULE_PARM_DESC(nowayout,
98 "Watchdog cannot be stopped once started (default="
99 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100102 * Watchdog Operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104
105static void acq_keepalive(void)
106{
107 /* Write a watchdog value */
108 inb_p(wdt_start);
109}
110
111static void acq_stop(void)
112{
113 /* Turn the card off */
114 inb_p(wdt_stop);
115}
116
117/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100118 * /dev/watchdog handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
120
Alan Cox94da1e22008-05-19 14:04:46 +0100121static ssize_t acq_write(struct file *file, const char __user *buf,
122 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 /* See if we got the magic character 'V' and reload the timer */
Alan Cox94da1e22008-05-19 14:04:46 +0100125 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!nowayout) {
127 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* note: just in case someone wrote the magic character
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000129 five months ago... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 expect_close = 0;
Alan Cox94da1e22008-05-19 14:04:46 +0100131 /* scan to see whether or not we got the
132 magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 for (i = 0; i != count; i++) {
134 char c;
135 if (get_user(c, buf + i))
136 return -EFAULT;
137 if (c == 'V')
138 expect_close = 42;
139 }
140 }
Alan Cox94da1e22008-05-19 14:04:46 +0100141 /* Well, anyhow someone wrote to us, we should
142 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 acq_keepalive();
144 }
145 return count;
146}
147
Alan Cox94da1e22008-05-19 14:04:46 +0100148static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int options, retval = -EINVAL;
151 void __user *argp = (void __user *)arg;
152 int __user *p = argp;
Alan Cox94da1e22008-05-19 14:04:46 +0100153 static struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
155 .firmware_version = 1,
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100156 .identity = WATCHDOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 };
158
Alan Cox94da1e22008-05-19 14:04:46 +0100159 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 case WDIOC_GETSUPPORT:
Alan Cox94da1e22008-05-19 14:04:46 +0100161 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 case WDIOC_GETSTATUS:
164 case WDIOC_GETBOOTSTATUS:
Alan Cox94da1e22008-05-19 14:04:46 +0100165 return put_user(0, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 case WDIOC_SETOPTIONS:
168 {
Alan Cox94da1e22008-05-19 14:04:46 +0100169 if (get_user(options, p))
170 return -EFAULT;
171 if (options & WDIOS_DISABLECARD) {
172 acq_stop();
173 retval = 0;
174 }
175 if (options & WDIOS_ENABLECARD) {
176 acq_keepalive();
177 retval = 0;
178 }
179 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000181 case WDIOC_KEEPALIVE:
182 acq_keepalive();
183 return 0;
184
185 case WDIOC_GETTIMEOUT:
186 return put_user(WATCHDOG_HEARTBEAT, p);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 default:
Alan Cox94da1e22008-05-19 14:04:46 +0100189 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191}
192
193static int acq_open(struct inode *inode, struct file *file)
194{
195 if (test_and_set_bit(0, &acq_is_open))
196 return -EBUSY;
197
198 if (nowayout)
199 __module_get(THIS_MODULE);
200
201 /* Activate */
202 acq_keepalive();
203 return nonseekable_open(inode, file);
204}
205
206static int acq_close(struct inode *inode, struct file *file)
207{
208 if (expect_close == 42) {
209 acq_stop();
210 } else {
Alan Cox94da1e22008-05-19 14:04:46 +0100211 printk(KERN_CRIT PFX
212 "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 acq_keepalive();
214 }
215 clear_bit(0, &acq_is_open);
216 expect_close = 0;
217 return 0;
218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Kernel Interfaces
222 */
223
Arjan van de Ven62322d22006-07-03 00:24:21 -0700224static const struct file_operations acq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .owner = THIS_MODULE,
226 .llseek = no_llseek,
227 .write = acq_write,
Alan Cox94da1e22008-05-19 14:04:46 +0100228 .unlocked_ioctl = acq_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .open = acq_open,
230 .release = acq_close,
231};
232
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100233static struct miscdevice acq_miscdev = {
234 .minor = WATCHDOG_MINOR,
235 .name = "watchdog",
236 .fops = &acq_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
239/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100240 * Init & exit routines
241 */
242
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100243static int __devinit acq_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int ret;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (wdt_stop != wdt_start) {
248 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
Alan Cox94da1e22008-05-19 14:04:46 +0100249 printk(KERN_ERR PFX
250 "I/O address 0x%04x already in use\n", wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ret = -EIO;
252 goto out;
253 }
254 }
255
256 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
Alan Cox94da1e22008-05-19 14:04:46 +0100257 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 wdt_start);
259 ret = -EIO;
260 goto unreg_stop;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ret = misc_register(&acq_miscdev);
263 if (ret != 0) {
Alan Cox94da1e22008-05-19 14:04:46 +0100264 printk(KERN_ERR PFX
265 "cannot register miscdev on minor=%d (err=%d)\n",
266 WATCHDOG_MINOR, ret);
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100267 goto unreg_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Alan Cox94da1e22008-05-19 14:04:46 +0100269 printk(KERN_INFO PFX "initialized. (nowayout=%d)\n", nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272unreg_regions:
273 release_region(wdt_start, 1);
274unreg_stop:
275 if (wdt_stop != wdt_start)
276 release_region(wdt_stop, 1);
277out:
278 return ret;
279}
280
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100281static int __devexit acq_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 misc_deregister(&acq_miscdev);
Alan Cox94da1e22008-05-19 14:04:46 +0100284 release_region(wdt_start, 1);
285 if (wdt_stop != wdt_start)
286 release_region(wdt_stop, 1);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100287
288 return 0;
289}
290
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100291static void acq_shutdown(struct platform_device *dev)
292{
293 /* Turn the WDT off if we have a soft shutdown */
294 acq_stop();
295}
296
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100297static struct platform_driver acquirewdt_driver = {
298 .probe = acq_probe,
299 .remove = __devexit_p(acq_remove),
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100300 .shutdown = acq_shutdown,
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100301 .driver = {
302 .owner = THIS_MODULE,
303 .name = DRV_NAME,
304 },
305};
306
307static int __init acq_init(void)
308{
309 int err;
310
Alan Cox94da1e22008-05-19 14:04:46 +0100311 printk(KERN_INFO
312 "WDT driver for Acquire single board computer initialising.\n");
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100313
314 err = platform_driver_register(&acquirewdt_driver);
315 if (err)
316 return err;
317
Alan Cox94da1e22008-05-19 14:04:46 +0100318 acq_platform_device = platform_device_register_simple(DRV_NAME,
319 -1, NULL, 0);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100320 if (IS_ERR(acq_platform_device)) {
321 err = PTR_ERR(acq_platform_device);
322 goto unreg_platform_driver;
323 }
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100324 return 0;
325
326unreg_platform_driver:
327 platform_driver_unregister(&acquirewdt_driver);
328 return err;
329}
330
331static void __exit acq_exit(void)
332{
333 platform_device_unregister(acq_platform_device);
334 platform_driver_unregister(&acquirewdt_driver);
335 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338module_init(acq_init);
339module_exit(acq_exit);
340
341MODULE_AUTHOR("David Woodhouse");
342MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
343MODULE_LICENSE("GPL");
344MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);