[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / char / watchdog / wafer5823wdt.c
blobabb0bea45c02f813d706ff81fd294bcec0d6cbb7
1 /*
2 * ICP Wafer 5823 Single Board Computer WDT driver
3 * http://www.icpamerica.com/wafer_5823.php
4 * May also work on other similar models
6 * (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
8 * Release 0.02
10 * Based on advantechwdt.c which is based on wdt.c.
11 * Original copyright messages:
13 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
14 * http://www.redhat.com
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
22 * warranty for any of this software. This material is provided
23 * "AS-IS" and at no charge.
25 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/miscdevice.h>
32 #include <linux/watchdog.h>
33 #include <linux/fs.h>
34 #include <linux/ioport.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
37 #include <linux/init.h>
38 #include <linux/spinlock.h>
39 #include <asm/io.h>
40 #include <asm/uaccess.h>
42 #define WATCHDOG_NAME "Wafer 5823 WDT"
43 #define PFX WATCHDOG_NAME ": "
44 #define WD_TIMO 60 /* 60 sec default timeout */
46 static unsigned long wafwdt_is_open;
47 static char expect_close;
48 static spinlock_t wafwdt_lock;
51 * You must set these - there is no sane way to probe for this board.
53 * To enable, write the timeout value in seconds (1 to 255) to I/O
54 * port WDT_START, then read the port to start the watchdog. To pat
55 * the dog, read port WDT_STOP to stop the timer, then read WDT_START
56 * to restart it again.
59 static int wdt_stop = 0x843;
60 static int wdt_start = 0x443;
62 static int timeout = WD_TIMO; /* in seconds */
63 module_param(timeout, int, 0);
64 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WD_TIMO) ".");
66 #ifdef CONFIG_WATCHDOG_NOWAYOUT
67 static int nowayout = 1;
68 #else
69 static int nowayout = 0;
70 #endif
72 module_param(nowayout, int, 0);
73 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
75 static void wafwdt_ping(void)
77 /* pat watchdog */
78 spin_lock(&wafwdt_lock);
79 inb_p(wdt_stop);
80 inb_p(wdt_start);
81 spin_unlock(&wafwdt_lock);
84 static void wafwdt_start(void)
86 /* start up watchdog */
87 outb_p(timeout, wdt_start);
88 inb_p(wdt_start);
91 static void
92 wafwdt_stop(void)
94 /* stop watchdog */
95 inb_p(wdt_stop);
98 static ssize_t wafwdt_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
100 /* See if we got the magic character 'V' and reload the timer */
101 if (count) {
102 if (!nowayout) {
103 size_t i;
105 /* In case it was set long ago */
106 expect_close = 0;
108 /* scan to see whether or not we got the magic character */
109 for (i = 0; i != count; i++) {
110 char c;
111 if (get_user(c, buf + i))
112 return -EFAULT;
113 if (c == 'V')
114 expect_close = 42;
117 /* Well, anyhow someone wrote to us, we should return that favour */
118 wafwdt_ping();
120 return count;
123 static int wafwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
124 unsigned long arg)
126 int new_timeout;
127 void __user *argp = (void __user *)arg;
128 int __user *p = argp;
129 static struct watchdog_info ident = {
130 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
131 .firmware_version = 1,
132 .identity = "Wafer 5823 WDT",
135 switch (cmd) {
136 case WDIOC_GETSUPPORT:
137 if (copy_to_user(argp, &ident, sizeof (ident)))
138 return -EFAULT;
139 break;
141 case WDIOC_GETSTATUS:
142 case WDIOC_GETBOOTSTATUS:
143 return put_user(0, p);
145 case WDIOC_KEEPALIVE:
146 wafwdt_ping();
147 break;
149 case WDIOC_SETTIMEOUT:
150 if (get_user(new_timeout, p))
151 return -EFAULT;
152 if ((new_timeout < 1) || (new_timeout > 255))
153 return -EINVAL;
154 timeout = new_timeout;
155 wafwdt_stop();
156 wafwdt_start();
157 /* Fall */
158 case WDIOC_GETTIMEOUT:
159 return put_user(timeout, p);
161 case WDIOC_SETOPTIONS:
163 int options, retval = -EINVAL;
165 if (get_user(options, p))
166 return -EFAULT;
168 if (options & WDIOS_DISABLECARD) {
169 wafwdt_start();
170 retval = 0;
173 if (options & WDIOS_ENABLECARD) {
174 wafwdt_stop();
175 retval = 0;
178 return retval;
181 default:
182 return -ENOIOCTLCMD;
184 return 0;
187 static int wafwdt_open(struct inode *inode, struct file *file)
189 if (test_and_set_bit(0, &wafwdt_is_open))
190 return -EBUSY;
193 * Activate
195 wafwdt_start();
196 return nonseekable_open(inode, file);
199 static int
200 wafwdt_close(struct inode *inode, struct file *file)
202 if (expect_close == 42) {
203 wafwdt_stop();
204 } else {
205 printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
206 wafwdt_ping();
208 clear_bit(0, &wafwdt_is_open);
209 expect_close = 0;
210 return 0;
214 * Notifier for system down
217 static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
219 if (code == SYS_DOWN || code == SYS_HALT) {
220 /* Turn the WDT off */
221 wafwdt_stop();
223 return NOTIFY_DONE;
227 * Kernel Interfaces
230 static struct file_operations wafwdt_fops = {
231 .owner = THIS_MODULE,
232 .llseek = no_llseek,
233 .write = wafwdt_write,
234 .ioctl = wafwdt_ioctl,
235 .open = wafwdt_open,
236 .release = wafwdt_close,
239 static struct miscdevice wafwdt_miscdev = {
240 .minor = WATCHDOG_MINOR,
241 .name = "watchdog",
242 .fops = &wafwdt_fops,
246 * The WDT needs to learn about soft shutdowns in order to
247 * turn the timebomb registers off.
250 static struct notifier_block wafwdt_notifier = {
251 .notifier_call = wafwdt_notify_sys,
254 static int __init wafwdt_init(void)
256 int ret;
258 printk(KERN_INFO "WDT driver for Wafer 5823 single board computer initialising.\n");
260 spin_lock_init(&wafwdt_lock);
262 if (timeout < 1 || timeout > 255) {
263 timeout = WD_TIMO;
264 printk (KERN_INFO PFX "timeout value must be 1<=x<=255, using %d\n",
265 timeout);
268 if (wdt_stop != wdt_start) {
269 if(!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
270 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
271 wdt_stop);
272 ret = -EIO;
273 goto error;
277 if(!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
278 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
279 wdt_start);
280 ret = -EIO;
281 goto error2;
284 ret = register_reboot_notifier(&wafwdt_notifier);
285 if (ret != 0) {
286 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
287 ret);
288 goto error3;
291 ret = misc_register(&wafwdt_miscdev);
292 if (ret != 0) {
293 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
294 WATCHDOG_MINOR, ret);
295 goto error4;
298 printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
299 timeout, nowayout);
301 return ret;
302 error4:
303 unregister_reboot_notifier(&wafwdt_notifier);
304 error3:
305 release_region(wdt_start, 1);
306 error2:
307 if (wdt_stop != wdt_start)
308 release_region(wdt_stop, 1);
309 error:
310 return ret;
313 static void __exit wafwdt_exit(void)
315 misc_deregister(&wafwdt_miscdev);
316 unregister_reboot_notifier(&wafwdt_notifier);
317 if(wdt_stop != wdt_start)
318 release_region(wdt_stop, 1);
319 release_region(wdt_start, 1);
322 module_init(wafwdt_init);
323 module_exit(wafwdt_exit);
325 MODULE_AUTHOR("Justin Cormack");
326 MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
327 MODULE_LICENSE("GPL");
328 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
330 /* end of wafer5823wdt.c */