blob: be61e47558918261ff9c780a886776e550df7bfd [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>
45
46#include <asm/io.h>
47#include <asm/uaccess.h>
48#include <asm/system.h>
49
50static unsigned long ibwdt_is_open;
51static spinlock_t ibwdt_lock;
52static char expect_close;
53
54#define PFX "ib700wdt: "
55
56/*
57 *
58 * Watchdog Timer Configuration
59 *
60 * The function of the watchdog timer is to reset the system
61 * automatically and is defined at I/O port 0443H. To enable the
62 * watchdog timer and allow the system to reset, write I/O port 0443H.
63 * To disable the timer, write I/O port 0441H for the system to stop the
64 * watchdog function. The timer has a tolerance of 20% for its
65 * intervals.
66 *
67 * The following describes how the timer should be programmed.
68 *
69 * Enabling Watchdog:
70 * MOV AX,000FH (Choose the values from 0 to F)
71 * MOV DX,0443H
72 * OUT DX,AX
73 *
74 * Disabling Watchdog:
75 * MOV AX,000FH (Any value is fine.)
76 * MOV DX,0441H
77 * OUT DX,AX
78 *
79 * Watchdog timer control table:
80 * Level Value Time/sec | Level Value Time/sec
81 * 1 F 0 | 9 7 16
82 * 2 E 2 | 10 6 18
83 * 3 D 4 | 11 5 20
84 * 4 C 6 | 12 4 22
85 * 5 B 8 | 13 3 24
86 * 6 A 10 | 14 2 26
87 * 7 9 12 | 15 1 28
88 * 8 8 14 | 16 0 30
89 *
90 */
91
92static int wd_times[] = {
93 30, /* 0x0 */
94 28, /* 0x1 */
95 26, /* 0x2 */
96 24, /* 0x3 */
97 22, /* 0x4 */
98 20, /* 0x5 */
99 18, /* 0x6 */
100 16, /* 0x7 */
101 14, /* 0x8 */
102 12, /* 0x9 */
103 10, /* 0xA */
104 8, /* 0xB */
105 6, /* 0xC */
106 4, /* 0xD */
107 2, /* 0xE */
108 0, /* 0xF */
109};
110
111#define WDT_STOP 0x441
112#define WDT_START 0x443
113
114/* Default timeout */
115#define WD_TIMO 0 /* 30 seconds +/- 20%, from table */
116
117static int wd_margin = WD_TIMO;
118
Andrey Panin4bfdf372005-07-27 11:43:58 -0700119static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(nowayout, int, 0);
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100121MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123
124/*
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100125 * Watchdog Operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127
128static void
129ibwdt_ping(void)
130{
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100131 spin_lock(&ibwdt_lock);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* Write a watchdog value */
134 outb_p(wd_margin, WDT_START);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100135
136 spin_unlock(&ibwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100139static void
140ibwdt_disable(void)
141{
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100142 spin_lock(&ibwdt_lock);
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100143 outb_p(0, WDT_STOP);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100144 spin_unlock(&ibwdt_lock);
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100145}
146
147static int
148ibwdt_set_heartbeat(int t)
149{
150 int i;
151
152 if ((t < 0) || (t > 30))
153 return -EINVAL;
154
155 for (i = 0x0F; i > -1; i--)
156 if (wd_times[i] > t)
157 break;
158 wd_margin = i;
159 return 0;
160}
161
162/*
163 * /dev/watchdog handling
164 */
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static ssize_t
167ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
168{
169 if (count) {
170 if (!nowayout) {
171 size_t i;
172
173 /* In case it was set long ago */
174 expect_close = 0;
175
176 for (i = 0; i != count; i++) {
177 char c;
178 if (get_user(c, buf + i))
179 return -EFAULT;
180 if (c == 'V')
181 expect_close = 42;
182 }
183 }
184 ibwdt_ping();
185 }
186 return count;
187}
188
189static int
190ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
191 unsigned long arg)
192{
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100193 int new_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 void __user *argp = (void __user *)arg;
195 int __user *p = argp;
196
197 static struct watchdog_info ident = {
198 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
199 .firmware_version = 1,
200 .identity = "IB700 WDT",
201 };
202
203 switch (cmd) {
204 case WDIOC_GETSUPPORT:
205 if (copy_to_user(argp, &ident, sizeof(ident)))
206 return -EFAULT;
207 break;
208
209 case WDIOC_GETSTATUS:
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100210 case WDIOC_GETBOOTSTATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return put_user(0, p);
212
213 case WDIOC_KEEPALIVE:
214 ibwdt_ping();
215 break;
216
217 case WDIOC_SETTIMEOUT:
218 if (get_user(new_margin, p))
219 return -EFAULT;
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100220 if (ibwdt_set_heartbeat(new_margin))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ibwdt_ping();
223 /* Fall */
224
225 case WDIOC_GETTIMEOUT:
226 return put_user(wd_times[wd_margin], p);
Wim Van Sebroecke42162a2007-01-27 22:12:54 +0100227
228 case WDIOC_SETOPTIONS:
229 {
230 int options, retval = -EINVAL;
231
232 if (get_user(options, p))
233 return -EFAULT;
234
235 if (options & WDIOS_DISABLECARD) {
236 ibwdt_disable();
237 retval = 0;
238 }
239
240 if (options & WDIOS_ENABLECARD) {
241 ibwdt_ping();
242 retval = 0;
243 }
244
245 return retval;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200249 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 return 0;
252}
253
254static int
255ibwdt_open(struct inode *inode, struct file *file)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (test_and_set_bit(0, &ibwdt_is_open)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EBUSY;
259 }
260 if (nowayout)
261 __module_get(THIS_MODULE);
262
263 /* Activate */
264 ibwdt_ping();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return nonseekable_open(inode, file);
266}
267
268static int
269ibwdt_close(struct inode *inode, struct file *file)
270{
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100271 if (expect_close == 42) {
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100272 ibwdt_disable();
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100273 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
Wim Van Sebroeckc9d77102007-01-27 22:07:03 +0100275 ibwdt_ping();
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 clear_bit(0, &ibwdt_is_open);
278 expect_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
281
282/*
283 * Notifier for system down
284 */
285
286static int
287ibwdt_notify_sys(struct notifier_block *this, unsigned long code,
288 void *unused)
289{
290 if (code == SYS_DOWN || code == SYS_HALT) {
291 /* Turn the WDT off */
Wim Van Sebroeck35d55c92007-01-27 21:50:53 +0100292 ibwdt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 return NOTIFY_DONE;
295}
296
297/*
298 * Kernel Interfaces
299 */
300
Arjan van de Ven62322d22006-07-03 00:24:21 -0700301static const struct file_operations ibwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .owner = THIS_MODULE,
303 .llseek = no_llseek,
304 .write = ibwdt_write,
305 .ioctl = ibwdt_ioctl,
306 .open = ibwdt_open,
307 .release = ibwdt_close,
308};
309
310static struct miscdevice ibwdt_miscdev = {
311 .minor = WATCHDOG_MINOR,
312 .name = "watchdog",
313 .fops = &ibwdt_fops,
314};
315
316/*
317 * The WDT needs to learn about soft shutdowns in order to
318 * turn the timebomb registers off.
319 */
320
321static struct notifier_block ibwdt_notifier = {
322 .notifier_call = ibwdt_notify_sys,
323};
324
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100325/*
326 * Init & exit routines
327 */
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int __init ibwdt_init(void)
330{
331 int res;
332
333 printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n");
334
335 spin_lock_init(&ibwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337#if WDT_START != WDT_STOP
338 if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
339 printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP);
340 res = -EIO;
341 goto out_nostopreg;
342 }
343#endif
344
345 if (!request_region(WDT_START, 1, "IB700 WDT")) {
346 printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START);
347 res = -EIO;
348 goto out_nostartreg;
349 }
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 res = register_reboot_notifier(&ibwdt_notifier);
352 if (res) {
353 printk (KERN_ERR PFX "Failed to register reboot notifier.\n");
354 goto out_noreboot;
355 }
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100356
357 res = misc_register(&ibwdt_miscdev);
358 if (res) {
359 printk (KERN_ERR PFX "failed to register misc device\n");
360 goto out_nomisc;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100364out_nomisc:
365 unregister_reboot_notifier(&ibwdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366out_noreboot:
367 release_region(WDT_START, 1);
368out_nostartreg:
369#if WDT_START != WDT_STOP
370 release_region(WDT_STOP, 1);
371#endif
372out_nostopreg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return res;
374}
375
376static void __exit
377ibwdt_exit(void)
378{
379 misc_deregister(&ibwdt_miscdev);
380 unregister_reboot_notifier(&ibwdt_notifier);
Wim Van Sebroeckf6e48032007-01-27 21:58:08 +0100381 release_region(WDT_START,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#if WDT_START != WDT_STOP
383 release_region(WDT_STOP,1);
384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387module_init(ibwdt_init);
388module_exit(ibwdt_exit);
389
390MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
391MODULE_DESCRIPTION("IB700 SBC watchdog driver");
392MODULE_LICENSE("GPL");
393MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
394
395/* end of ib700wdt.c */