blob: b74e15159c02341f680dff5a90084ed59df5a12b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IB700 Single Board Computer WDT driver
3 *
4 * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
5 *
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +01006 * Based on advantechwdt.c which is based on acquirewdt.c which
7 * is based on wdt.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
10 *
11 * Based on acquirewdt.c which is based on wdt.c.
12 * Original copyright messages:
13 *
14 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
15 * http://www.redhat.com
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 *
22 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
23 * warranty for any of this software. This material is provided
24 * "AS-IS" and at no charge.
25 *
26 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
27 *
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +010028 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
29 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
30 * Added timeout module option to override default
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/types.h>
36#include <linux/miscdevice.h>
37#include <linux/watchdog.h>
38#include <linux/ioport.h>
39#include <linux/notifier.h>
40#include <linux/fs.h>
41#include <linux/reboot.h>
42#include <linux/init.h>
43#include <linux/spinlock.h>
44#include <linux/moduleparam.h>
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +010045#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/io.h>
48#include <asm/uaccess.h>
49#include <asm/system.h>
50
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +010051static struct platform_device *ibwdt_platform_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static unsigned long ibwdt_is_open;
53static spinlock_t ibwdt_lock;
54static char expect_close;
55
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +010056/* Module information */
57#define DRV_NAME "ib700wdt"
58#define PFX DRV_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 *
62 * Watchdog Timer Configuration
63 *
64 * The function of the watchdog timer is to reset the system
65 * automatically and is defined at I/O port 0443H. To enable the
66 * watchdog timer and allow the system to reset, write I/O port 0443H.
67 * To disable the timer, write I/O port 0441H for the system to stop the
68 * watchdog function. The timer has a tolerance of 20% for its
69 * intervals.
70 *
71 * The following describes how the timer should be programmed.
72 *
73 * Enabling Watchdog:
74 * MOV AX,000FH (Choose the values from 0 to F)
75 * MOV DX,0443H
76 * OUT DX,AX
77 *
78 * Disabling Watchdog:
79 * MOV AX,000FH (Any value is fine.)
80 * MOV DX,0441H
81 * OUT DX,AX
82 *
83 * Watchdog timer control table:
84 * Level Value Time/sec | Level Value Time/sec
85 * 1 F 0 | 9 7 16
86 * 2 E 2 | 10 6 18
87 * 3 D 4 | 11 5 20
88 * 4 C 6 | 12 4 22
89 * 5 B 8 | 13 3 24
90 * 6 A 10 | 14 2 26
91 * 7 9 12 | 15 1 28
92 * 8 8 14 | 16 0 30
93 *
94 */
95
96static int wd_times[] = {
97 30, /* 0x0 */
98 28, /* 0x1 */
99 26, /* 0x2 */
100 24, /* 0x3 */
101 22, /* 0x4 */
102 20, /* 0x5 */
103 18, /* 0x6 */
104 16, /* 0x7 */
105 14, /* 0x8 */
106 12, /* 0x9 */
107 10, /* 0xA */
108 8, /* 0xB */
109 6, /* 0xC */
110 4, /* 0xD */
111 2, /* 0xE */
112 0, /* 0xF */
113};
114
115#define WDT_STOP 0x441
116#define WDT_START 0x443
117
118/* Default timeout */
119#define WD_TIMO 0 /* 30 seconds +/- 20%, from table */
120
121static int wd_margin = WD_TIMO;
122
Andrey Panin4bfdf372005-07-27 11:43:58 -0700123static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124module_param(nowayout, int, 0);
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100125MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127
128/*
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100129 * Watchdog Operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
132static void
133ibwdt_ping(void)
134{
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100135 spin_lock(&ibwdt_lock);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* Write a watchdog value */
138 outb_p(wd_margin, WDT_START);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100139
140 spin_unlock(&ibwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100143static void
144ibwdt_disable(void)
145{
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100146 spin_lock(&ibwdt_lock);
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100147 outb_p(0, WDT_STOP);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100148 spin_unlock(&ibwdt_lock);
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100149}
150
151static int
152ibwdt_set_heartbeat(int t)
153{
154 int i;
155
156 if ((t < 0) || (t > 30))
157 return -EINVAL;
158
159 for (i = 0x0F; i > -1; i--)
160 if (wd_times[i] > t)
161 break;
162 wd_margin = i;
163 return 0;
164}
165
166/*
167 * /dev/watchdog handling
168 */
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static ssize_t
171ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
172{
173 if (count) {
174 if (!nowayout) {
175 size_t i;
176
177 /* In case it was set long ago */
178 expect_close = 0;
179
180 for (i = 0; i != count; i++) {
181 char c;
182 if (get_user(c, buf + i))
183 return -EFAULT;
184 if (c == 'V')
185 expect_close = 42;
186 }
187 }
188 ibwdt_ping();
189 }
190 return count;
191}
192
193static int
194ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
195 unsigned long arg)
196{
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100197 int new_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 void __user *argp = (void __user *)arg;
199 int __user *p = argp;
200
201 static struct watchdog_info ident = {
202 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
203 .firmware_version = 1,
204 .identity = "IB700 WDT",
205 };
206
207 switch (cmd) {
208 case WDIOC_GETSUPPORT:
209 if (copy_to_user(argp, &ident, sizeof(ident)))
210 return -EFAULT;
211 break;
212
213 case WDIOC_GETSTATUS:
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100214 case WDIOC_GETBOOTSTATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return put_user(0, p);
216
217 case WDIOC_KEEPALIVE:
218 ibwdt_ping();
219 break;
220
221 case WDIOC_SETTIMEOUT:
222 if (get_user(new_margin, p))
223 return -EFAULT;
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100224 if (ibwdt_set_heartbeat(new_margin))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ibwdt_ping();
227 /* Fall */
228
229 case WDIOC_GETTIMEOUT:
230 return put_user(wd_times[wd_margin], p);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100231
232 case WDIOC_SETOPTIONS:
233 {
234 int options, retval = -EINVAL;
235
236 if (get_user(options, p))
237 return -EFAULT;
238
239 if (options & WDIOS_DISABLECARD) {
240 ibwdt_disable();
241 retval = 0;
242 }
243
244 if (options & WDIOS_ENABLECARD) {
245 ibwdt_ping();
246 retval = 0;
247 }
248
249 return retval;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200253 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 return 0;
256}
257
258static int
259ibwdt_open(struct inode *inode, struct file *file)
260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (test_and_set_bit(0, &ibwdt_is_open)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EBUSY;
263 }
264 if (nowayout)
265 __module_get(THIS_MODULE);
266
267 /* Activate */
268 ibwdt_ping();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return nonseekable_open(inode, file);
270}
271
272static int
273ibwdt_close(struct inode *inode, struct file *file)
274{
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100275 if (expect_close == 42) {
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100276 ibwdt_disable();
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100277 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100279 ibwdt_ping();
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 clear_bit(0, &ibwdt_is_open);
282 expect_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286/*
287 * Notifier for system down
288 */
289
290static int
291ibwdt_notify_sys(struct notifier_block *this, unsigned long code,
292 void *unused)
293{
294 if (code == SYS_DOWN || code == SYS_HALT) {
295 /* Turn the WDT off */
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100296 ibwdt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 return NOTIFY_DONE;
299}
300
301/*
302 * Kernel Interfaces
303 */
304
Arjan van de Ven62322d22006-07-03 00:24:21 -0700305static const struct file_operations ibwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
308 .write = ibwdt_write,
309 .ioctl = ibwdt_ioctl,
310 .open = ibwdt_open,
311 .release = ibwdt_close,
312};
313
314static struct miscdevice ibwdt_miscdev = {
315 .minor = WATCHDOG_MINOR,
316 .name = "watchdog",
317 .fops = &ibwdt_fops,
318};
319
320/*
321 * The WDT needs to learn about soft shutdowns in order to
322 * turn the timebomb registers off.
323 */
324
325static struct notifier_block ibwdt_notifier = {
326 .notifier_call = ibwdt_notify_sys,
327};
328
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100329/*
330 * Init & exit routines
331 */
332
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +0100333static int __devinit ibwdt_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int res;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_lock_init(&ibwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339#if WDT_START != WDT_STOP
340 if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
341 printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP);
342 res = -EIO;
343 goto out_nostopreg;
344 }
345#endif
346
347 if (!request_region(WDT_START, 1, "IB700 WDT")) {
348 printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START);
349 res = -EIO;
350 goto out_nostartreg;
351 }
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 res = register_reboot_notifier(&ibwdt_notifier);
354 if (res) {
355 printk (KERN_ERR PFX "Failed to register reboot notifier.\n");
356 goto out_noreboot;
357 }
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100358
359 res = misc_register(&ibwdt_miscdev);
360 if (res) {
361 printk (KERN_ERR PFX "failed to register misc device\n");
362 goto out_nomisc;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100366out_nomisc:
367 unregister_reboot_notifier(&ibwdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368out_noreboot:
369 release_region(WDT_START, 1);
370out_nostartreg:
371#if WDT_START != WDT_STOP
372 release_region(WDT_STOP, 1);
373#endif
374out_nostopreg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return res;
376}
377
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +0100378static int __devexit ibwdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 misc_deregister(&ibwdt_miscdev);
381 unregister_reboot_notifier(&ibwdt_notifier);
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100382 release_region(WDT_START,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#if WDT_START != WDT_STOP
384 release_region(WDT_STOP,1);
385#endif
Wim Van Sebroeck745ac1ea2007-01-27 22:39:46 +0100386 return 0;
387}
388
389static struct platform_driver ibwdt_driver = {
390 .probe = ibwdt_probe,
391 .remove = __devexit_p(ibwdt_remove),
392 .driver = {
393 .owner = THIS_MODULE,
394 .name = DRV_NAME,
395 },
396};
397
398static int __init ibwdt_init(void)
399{
400 int err;
401
402 printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n");
403
404 err = platform_driver_register(&ibwdt_driver);
405 if (err)
406 return err;
407
408 ibwdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
409 if (IS_ERR(ibwdt_platform_device)) {
410 err = PTR_ERR(ibwdt_platform_device);
411 goto unreg_platform_driver;
412 }
413
414 return 0;
415
416unreg_platform_driver:
417 platform_driver_unregister(&ibwdt_driver);
418 return err;
419}
420
421static void __exit ibwdt_exit(void)
422{
423 platform_device_unregister(ibwdt_platform_device);
424 platform_driver_unregister(&ibwdt_driver);
425 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428module_init(ibwdt_init);
429module_exit(ibwdt_exit);
430
431MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
432MODULE_DESCRIPTION("IB700 SBC watchdog driver");
433MODULE_LICENSE("GPL");
434MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
435
436/* end of ib700wdt.c */