[WATCHDOG] w83697hf WDT driver
[linux-2.6/libata-dev.git] / drivers / char / watchdog / w83697hf_wdt.c
blobef6612e1b91fc398e142f3752e04d790c377a75e
1 /*
2 * w83697hf WDT driver
4 * (c) Copyright 2006 Marcus Junker <junker@anduras.de>
6 * Based on w83627hf_wdt.c advantechwdt.c which is based on wdt.c.
7 * Original copyright messages:
9 * (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
11 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
13 * (c) Copyright 1996 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 Marcus Junker nor ANDURAS AG admit liability nor provide
22 * warranty for any of this software. This material is provided
23 * "AS-IS" and at no charge.
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/types.h>
29 #include <linux/miscdevice.h>
30 #include <linux/watchdog.h>
31 #include <linux/fs.h>
32 #include <linux/ioport.h>
33 #include <linux/notifier.h>
34 #include <linux/reboot.h>
35 #include <linux/init.h>
37 #include <asm/io.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #define WATCHDOG_NAME "w83697hf WDT"
42 #define PFX WATCHDOG_NAME ": "
43 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
45 static unsigned long wdt_is_open;
46 static char expect_close;
48 /* You must set this - there is no sane way to probe for this board. */
49 static int wdt_io = 0x2E;
50 module_param(wdt_io, int, 0);
51 MODULE_PARM_DESC(wdt_io, "w83697hf WDT io port (default 0x2E)");
53 static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
54 module_param(timeout, int, 0);
55 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=63, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
57 static int nowayout = WATCHDOG_NOWAYOUT;
58 module_param(nowayout, int, 0);
59 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
62 * Kernel methods.
65 #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
66 #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register (same as EFER) */
67 #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
69 static void
70 w83697hf_select_wd_register(void)
72 outb_p(0x87, WDT_EFER); /* Enter extended function mode */
73 outb_p(0x87, WDT_EFER); /* Again according to manual */
75 outb_p(0x29, WDT_EFER); /* select CR29 */
76 outb_p(0x20, WDT_EFDR); /* select WDTO */
78 outb_p(0x07, WDT_EFER); /* point to logical device number reg */
79 outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
80 outb_p(0x30, WDT_EFER); /* select CR30 */
81 outb_p(0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
84 static void
85 w83697hf_unselect_wd_register(void)
87 outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
90 /* tyan motherboards seem to set F5 to 0x4C ?
91 * So explicitly init to appropriate value. */
92 static void
93 w83697hf_init(void)
95 unsigned char t;
97 w83697hf_select_wd_register();
99 outb_p(0xF3, WDT_EFER); /* Select CRF3 */
101 t=inb_p(WDT_EFDR); /* read CRF6 */
102 if (t != 0) {
103 printk (KERN_INFO PFX "Watchdog already running. Resetting timeout to %d sec\n", timeout);
104 outb_p(timeout, WDT_EFDR); /* Write back to CRF6 */
106 outb_p(0xF4, WDT_EFER); /* Select CRF4 */
107 t=inb_p(WDT_EFDR); /* read CRF4 */
108 t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
109 outb_p(t, WDT_EFDR); /* Write back to CRF5 */
111 w83697hf_unselect_wd_register();
114 static void
115 wdt_ctrl(int timeout)
117 w83697hf_select_wd_register();
119 outb_p(0xF4, WDT_EFER); /* Select CRF4 */
120 outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF4 */
122 w83697hf_unselect_wd_register();
125 static int
126 wdt_ping(void)
128 wdt_ctrl(timeout);
129 return 0;
132 static int
133 wdt_disable(void)
135 wdt_ctrl(0);
136 return 0;
139 static int
140 wdt_set_heartbeat(int t)
142 if ((t < 1) || (t > 63))
143 return -EINVAL;
145 timeout = t;
146 return 0;
149 static ssize_t
150 wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
152 if (count) {
153 if (!nowayout) {
154 size_t i;
156 expect_close = 0;
158 for (i = 0; i != count; i++) {
159 char c;
160 if (get_user(c, buf+i))
161 return -EFAULT;
162 if (c == 'V')
163 expect_close = 42;
166 wdt_ping();
168 return count;
171 static int
172 wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
173 unsigned long arg)
175 void __user *argp = (void __user *)arg;
176 int __user *p = argp;
177 int new_timeout;
178 static struct watchdog_info ident = {
179 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
180 .firmware_version = 1,
181 .identity = "W83697HF WDT",
184 switch (cmd) {
185 case WDIOC_GETSUPPORT:
186 if (copy_to_user(argp, &ident, sizeof(ident)))
187 return -EFAULT;
188 break;
190 case WDIOC_GETSTATUS:
191 case WDIOC_GETBOOTSTATUS:
192 return put_user(0, p);
194 case WDIOC_KEEPALIVE:
195 wdt_ping();
196 break;
198 case WDIOC_SETTIMEOUT:
199 if (get_user(new_timeout, p))
200 return -EFAULT;
201 if (wdt_set_heartbeat(new_timeout))
202 return -EINVAL;
203 wdt_ping();
204 /* Fall */
206 case WDIOC_GETTIMEOUT:
207 return put_user(timeout, p);
209 case WDIOC_SETOPTIONS:
211 int options, retval = -EINVAL;
213 if (get_user(options, p))
214 return -EFAULT;
216 if (options & WDIOS_DISABLECARD) {
217 wdt_disable();
218 retval = 0;
221 if (options & WDIOS_ENABLECARD) {
222 wdt_ping();
223 retval = 0;
226 return retval;
229 default:
230 return -ENOIOCTLCMD;
232 return 0;
235 static int
236 wdt_open(struct inode *inode, struct file *file)
238 if (test_and_set_bit(0, &wdt_is_open))
239 return -EBUSY;
241 * Activate
244 wdt_ping();
245 return nonseekable_open(inode, file);
248 static int
249 wdt_close(struct inode *inode, struct file *file)
251 if (expect_close == 42) {
252 wdt_disable();
253 } else {
254 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
255 wdt_ping();
257 expect_close = 0;
258 clear_bit(0, &wdt_is_open);
259 return 0;
263 * Notifier for system down
266 static int
267 wdt_notify_sys(struct notifier_block *this, unsigned long code,
268 void *unused)
270 if (code == SYS_DOWN || code == SYS_HALT) {
271 /* Turn the WDT off */
272 wdt_disable();
274 return NOTIFY_DONE;
278 * Kernel Interfaces
281 static struct file_operations wdt_fops = {
282 .owner = THIS_MODULE,
283 .llseek = no_llseek,
284 .write = wdt_write,
285 .ioctl = wdt_ioctl,
286 .open = wdt_open,
287 .release = wdt_close,
290 static struct miscdevice wdt_miscdev = {
291 .minor = WATCHDOG_MINOR,
292 .name = "watchdog",
293 .fops = &wdt_fops,
297 * The WDT needs to learn about soft shutdowns in order to
298 * turn the timebomb registers off.
301 static struct notifier_block wdt_notifier = {
302 .notifier_call = wdt_notify_sys,
305 static int __init
306 wdt_init(void)
308 int ret;
310 printk(KERN_INFO "WDT driver for the Winbond(TM) W83697HF Super I/O chip initialising.\n");
312 if (wdt_set_heartbeat(timeout)) {
313 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
314 printk (KERN_INFO PFX "timeout value must be 1<=timeout<=63, using %d\n",
315 WATCHDOG_TIMEOUT);
318 if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
319 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
320 wdt_io);
321 ret = -EIO;
322 goto out;
325 w83697hf_init();
327 ret = register_reboot_notifier(&wdt_notifier);
328 if (ret != 0) {
329 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
330 ret);
331 goto unreg_regions;
334 ret = misc_register(&wdt_miscdev);
335 if (ret != 0) {
336 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
337 WATCHDOG_MINOR, ret);
338 goto unreg_reboot;
341 printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
342 timeout, nowayout);
344 out:
345 return ret;
346 unreg_reboot:
347 unregister_reboot_notifier(&wdt_notifier);
348 unreg_regions:
349 release_region(wdt_io, 1);
350 goto out;
353 static void __exit
354 wdt_exit(void)
356 misc_deregister(&wdt_miscdev);
357 unregister_reboot_notifier(&wdt_notifier);
358 release_region(wdt_io,1);
361 module_init(wdt_init);
362 module_exit(wdt_exit);
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
366 MODULE_DESCRIPTION("w83697hf WDT driver");
367 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);