blob: 269ada2f1fc373c253683eae4ec41d156d27d525 [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 *
6 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
7 * http://www.redhat.com
8 *
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 *
18 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
19 *
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/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alan Cox94da1e22008-05-19 14:04:46 +010069#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
70#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010071
72/* Module information */
73#define DRV_NAME "acquirewdt"
74#define PFX DRV_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define WATCHDOG_NAME "Acquire WDT"
Alan Cox94da1e22008-05-19 14:04:46 +010076/* There is no way to see what the correct time-out period is */
77#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010079/* internal variables */
Alan Cox94da1e22008-05-19 14:04:46 +010080/* the watchdog platform device */
81static struct platform_device *acq_platform_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static unsigned long acq_is_open;
83static char expect_close;
84
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +010085/* module parameters */
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_stop = 0x43;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088module_param(wdt_stop, int, 0);
89MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
90
Alan Cox94da1e22008-05-19 14:04:46 +010091/* You must set this - there is no sane way to probe for this board. */
92static int wdt_start = 0x443;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(wdt_start, int, 0);
94MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
95
Andrey Panin4bfdf372005-07-27 11:43:58 -070096static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(nowayout, int, 0);
Alan Cox94da1e22008-05-19 14:04:46 +010098MODULE_PARM_DESC(nowayout,
99 "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100103 * Watchdog Operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static void acq_keepalive(void)
107{
108 /* Write a watchdog value */
109 inb_p(wdt_start);
110}
111
112static void acq_stop(void)
113{
114 /* Turn the card off */
115 inb_p(wdt_stop);
116}
117
118/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100119 * /dev/watchdog handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121
Alan Cox94da1e22008-05-19 14:04:46 +0100122static ssize_t acq_write(struct file *file, const char __user *buf,
123 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 /* See if we got the magic character 'V' and reload the timer */
Alan Cox94da1e22008-05-19 14:04:46 +0100126 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!nowayout) {
128 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* note: just in case someone wrote the magic character
130 * five months ago... */
131 expect_close = 0;
Alan Cox94da1e22008-05-19 14:04:46 +0100132 /* scan to see whether or not we got the
133 magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 for (i = 0; i != count; i++) {
135 char c;
136 if (get_user(c, buf + i))
137 return -EFAULT;
138 if (c == 'V')
139 expect_close = 42;
140 }
141 }
Alan Cox94da1e22008-05-19 14:04:46 +0100142 /* Well, anyhow someone wrote to us, we should
143 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 acq_keepalive();
145 }
146 return count;
147}
148
Alan Cox94da1e22008-05-19 14:04:46 +0100149static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 int options, retval = -EINVAL;
152 void __user *argp = (void __user *)arg;
153 int __user *p = argp;
Alan Cox94da1e22008-05-19 14:04:46 +0100154 static struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
156 .firmware_version = 1,
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100157 .identity = WATCHDOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 };
159
Alan Cox94da1e22008-05-19 14:04:46 +0100160 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case WDIOC_GETSUPPORT:
Alan Cox94da1e22008-05-19 14:04:46 +0100162 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 case WDIOC_GETSTATUS:
165 case WDIOC_GETBOOTSTATUS:
Alan Cox94da1e22008-05-19 14:04:46 +0100166 return put_user(0, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 case WDIOC_KEEPALIVE:
Alan Cox94da1e22008-05-19 14:04:46 +0100169 acq_keepalive();
170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 case WDIOC_GETTIMEOUT:
173 return put_user(WATCHDOG_HEARTBEAT, p);
174
175 case WDIOC_SETOPTIONS:
176 {
Alan Cox94da1e22008-05-19 14:04:46 +0100177 if (get_user(options, p))
178 return -EFAULT;
179 if (options & WDIOS_DISABLECARD) {
180 acq_stop();
181 retval = 0;
182 }
183 if (options & WDIOS_ENABLECARD) {
184 acq_keepalive();
185 retval = 0;
186 }
187 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 default:
Alan Cox94da1e22008-05-19 14:04:46 +0100190 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192}
193
194static int acq_open(struct inode *inode, struct file *file)
195{
196 if (test_and_set_bit(0, &acq_is_open))
197 return -EBUSY;
198
199 if (nowayout)
200 __module_get(THIS_MODULE);
201
202 /* Activate */
203 acq_keepalive();
204 return nonseekable_open(inode, file);
205}
206
207static int acq_close(struct inode *inode, struct file *file)
208{
209 if (expect_close == 42) {
210 acq_stop();
211 } else {
Alan Cox94da1e22008-05-19 14:04:46 +0100212 printk(KERN_CRIT PFX
213 "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 acq_keepalive();
215 }
216 clear_bit(0, &acq_is_open);
217 expect_close = 0;
218 return 0;
219}
220
221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * Kernel Interfaces
223 */
224
Arjan van de Ven62322d22006-07-03 00:24:21 -0700225static const struct file_operations acq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .owner = THIS_MODULE,
227 .llseek = no_llseek,
228 .write = acq_write,
Alan Cox94da1e22008-05-19 14:04:46 +0100229 .unlocked_ioctl = acq_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .open = acq_open,
231 .release = acq_close,
232};
233
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100234static struct miscdevice acq_miscdev = {
235 .minor = WATCHDOG_MINOR,
236 .name = "watchdog",
237 .fops = &acq_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
240/*
Wim Van Sebroeck76c11f02007-01-10 23:23:44 +0100241 * Init & exit routines
242 */
243
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100244static int __devinit acq_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int ret;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (wdt_stop != wdt_start) {
249 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
Alan Cox94da1e22008-05-19 14:04:46 +0100250 printk(KERN_ERR PFX
251 "I/O address 0x%04x already in use\n", wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ret = -EIO;
253 goto out;
254 }
255 }
256
257 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
Alan Cox94da1e22008-05-19 14:04:46 +0100258 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 wdt_start);
260 ret = -EIO;
261 goto unreg_stop;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ret = misc_register(&acq_miscdev);
264 if (ret != 0) {
Alan Cox94da1e22008-05-19 14:04:46 +0100265 printk(KERN_ERR PFX
266 "cannot register miscdev on minor=%d (err=%d)\n",
267 WATCHDOG_MINOR, ret);
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100268 goto unreg_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Alan Cox94da1e22008-05-19 14:04:46 +0100270 printk(KERN_INFO PFX "initialized. (nowayout=%d)\n", nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273unreg_regions:
274 release_region(wdt_start, 1);
275unreg_stop:
276 if (wdt_stop != wdt_start)
277 release_region(wdt_stop, 1);
278out:
279 return ret;
280}
281
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100282static int __devexit acq_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 misc_deregister(&acq_miscdev);
Alan Cox94da1e22008-05-19 14:04:46 +0100285 release_region(wdt_start, 1);
286 if (wdt_stop != wdt_start)
287 release_region(wdt_stop, 1);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100288
289 return 0;
290}
291
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100292static void acq_shutdown(struct platform_device *dev)
293{
294 /* Turn the WDT off if we have a soft shutdown */
295 acq_stop();
296}
297
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100298static struct platform_driver acquirewdt_driver = {
299 .probe = acq_probe,
300 .remove = __devexit_p(acq_remove),
Wim Van Sebroeck98c08e92007-01-10 23:38:56 +0100301 .shutdown = acq_shutdown,
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100302 .driver = {
303 .owner = THIS_MODULE,
304 .name = DRV_NAME,
305 },
306};
307
308static int __init acq_init(void)
309{
310 int err;
311
Alan Cox94da1e22008-05-19 14:04:46 +0100312 printk(KERN_INFO
313 "WDT driver for Acquire single board computer initialising.\n");
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100314
315 err = platform_driver_register(&acquirewdt_driver);
316 if (err)
317 return err;
318
Alan Cox94da1e22008-05-19 14:04:46 +0100319 acq_platform_device = platform_device_register_simple(DRV_NAME,
320 -1, NULL, 0);
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100321 if (IS_ERR(acq_platform_device)) {
322 err = PTR_ERR(acq_platform_device);
323 goto unreg_platform_driver;
324 }
Wim Van Sebroeckad5fe322007-01-10 23:36:13 +0100325 return 0;
326
327unreg_platform_driver:
328 platform_driver_unregister(&acquirewdt_driver);
329 return err;
330}
331
332static void __exit acq_exit(void)
333{
334 platform_device_unregister(acq_platform_device);
335 platform_driver_unregister(&acquirewdt_driver);
336 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339module_init(acq_init);
340module_exit(acq_exit);
341
342MODULE_AUTHOR("David Woodhouse");
343MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
344MODULE_LICENSE("GPL");
345MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);