blob: 886cbbcf3eedd0db1d151ac16497c6b3b55869b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
5 *
6 * (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
7 *
8 * Release 0.02
9 *
10 * Based on advantechwdt.c which is based on wdt.c.
11 * Original copyright messages:
12 *
13 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
14 * http://www.redhat.com
15 *
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.
20 *
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.
24 *
25 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
26 *
27 */
28
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>
Alan Cox694b16b2008-05-19 14:09:40 +010039#include <linux/io.h>
40#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define WATCHDOG_NAME "Wafer 5823 WDT"
43#define PFX WATCHDOG_NAME ": "
44#define WD_TIMO 60 /* 60 sec default timeout */
45
46static unsigned long wafwdt_is_open;
47static char expect_close;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070048static DEFINE_SPINLOCK(wafwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * You must set these - there is no sane way to probe for this board.
52 *
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.
57 */
58
59static int wdt_stop = 0x843;
60static int wdt_start = 0x443;
61
62static int timeout = WD_TIMO; /* in seconds */
63module_param(timeout, int, 0);
Alan Cox694b16b2008-05-19 14:09:40 +010064MODULE_PARM_DESC(timeout,
65 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
66 __MODULE_STRING(WD_TIMO) ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andrey Panin4bfdf372005-07-27 11:43:58 -070068static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069module_param(nowayout, int, 0);
Alan Cox694b16b2008-05-19 14:09:40 +010070MODULE_PARM_DESC(nowayout,
71 "Watchdog cannot be stopped once started (default="
72 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static void wafwdt_ping(void)
75{
76 /* pat watchdog */
77 spin_lock(&wafwdt_lock);
78 inb_p(wdt_stop);
79 inb_p(wdt_start);
80 spin_unlock(&wafwdt_lock);
81}
82
83static void wafwdt_start(void)
84{
85 /* start up watchdog */
86 outb_p(timeout, wdt_start);
87 inb_p(wdt_start);
88}
89
90static void
91wafwdt_stop(void)
92{
93 /* stop watchdog */
94 inb_p(wdt_stop);
95}
96
Alan Cox694b16b2008-05-19 14:09:40 +010097static ssize_t wafwdt_write(struct file *file, const char __user *buf,
98 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 /* See if we got the magic character 'V' and reload the timer */
101 if (count) {
102 if (!nowayout) {
103 size_t i;
104
105 /* In case it was set long ago */
106 expect_close = 0;
107
Alan Cox694b16b2008-05-19 14:09:40 +0100108 /* scan to see whether or not we got the magic
109 character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (i = 0; i != count; i++) {
111 char c;
112 if (get_user(c, buf + i))
113 return -EFAULT;
114 if (c == 'V')
115 expect_close = 42;
116 }
117 }
Alan Cox694b16b2008-05-19 14:09:40 +0100118 /* Well, anyhow someone wrote to us, we should
119 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 wafwdt_ping();
121 }
122 return count;
123}
124
Alan Cox694b16b2008-05-19 14:09:40 +0100125static long wafwdt_ioctl(struct file *file, unsigned int cmd,
126 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int new_timeout;
129 void __user *argp = (void __user *)arg;
130 int __user *p = argp;
Alan Cox694b16b2008-05-19 14:09:40 +0100131 static const struct watchdog_info ident = {
132 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
133 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .firmware_version = 1,
135 .identity = "Wafer 5823 WDT",
136 };
137
138 switch (cmd) {
139 case WDIOC_GETSUPPORT:
Alan Cox694b16b2008-05-19 14:09:40 +0100140 if (copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EFAULT;
142 break;
143
144 case WDIOC_GETSTATUS:
145 case WDIOC_GETBOOTSTATUS:
146 return put_user(0, p);
147
148 case WDIOC_KEEPALIVE:
149 wafwdt_ping();
150 break;
151
152 case WDIOC_SETTIMEOUT:
153 if (get_user(new_timeout, p))
154 return -EFAULT;
155 if ((new_timeout < 1) || (new_timeout > 255))
156 return -EINVAL;
157 timeout = new_timeout;
158 wafwdt_stop();
159 wafwdt_start();
160 /* Fall */
161 case WDIOC_GETTIMEOUT:
162 return put_user(timeout, p);
163
164 case WDIOC_SETOPTIONS:
165 {
166 int options, retval = -EINVAL;
167
168 if (get_user(options, p))
169 return -EFAULT;
170
171 if (options & WDIOS_DISABLECARD) {
172 wafwdt_start();
173 retval = 0;
174 }
175
176 if (options & WDIOS_ENABLECARD) {
177 wafwdt_stop();
178 retval = 0;
179 }
180
181 return retval;
182 }
183
184 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200185 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 return 0;
188}
189
190static int wafwdt_open(struct inode *inode, struct file *file)
191{
192 if (test_and_set_bit(0, &wafwdt_is_open))
193 return -EBUSY;
194
195 /*
196 * Activate
197 */
198 wafwdt_start();
199 return nonseekable_open(inode, file);
200}
201
202static int
203wafwdt_close(struct inode *inode, struct file *file)
204{
Alan Cox694b16b2008-05-19 14:09:40 +0100205 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 wafwdt_stop();
Alan Cox694b16b2008-05-19 14:09:40 +0100207 else {
208 printk(KERN_CRIT PFX
209 "WDT device closed unexpectedly. WDT will not stop!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 wafwdt_ping();
211 }
212 clear_bit(0, &wafwdt_is_open);
213 expect_close = 0;
214 return 0;
215}
216
217/*
218 * Notifier for system down
219 */
220
Alan Cox694b16b2008-05-19 14:09:40 +0100221static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code,
222 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Alan Cox694b16b2008-05-19 14:09:40 +0100224 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 wafwdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NOTIFY_DONE;
227}
228
229/*
230 * Kernel Interfaces
231 */
232
Arjan van de Ven62322d22006-07-03 00:24:21 -0700233static const struct file_operations wafwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .owner = THIS_MODULE,
235 .llseek = no_llseek,
236 .write = wafwdt_write,
Alan Cox694b16b2008-05-19 14:09:40 +0100237 .unlocked_ioctl = wafwdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .open = wafwdt_open,
239 .release = wafwdt_close,
240};
241
242static struct miscdevice wafwdt_miscdev = {
243 .minor = WATCHDOG_MINOR,
244 .name = "watchdog",
245 .fops = &wafwdt_fops,
246};
247
248/*
249 * The WDT needs to learn about soft shutdowns in order to
250 * turn the timebomb registers off.
251 */
252
253static struct notifier_block wafwdt_notifier = {
254 .notifier_call = wafwdt_notify_sys,
255};
256
257static int __init wafwdt_init(void)
258{
259 int ret;
260
Alan Cox694b16b2008-05-19 14:09:40 +0100261 printk(KERN_INFO
262 "WDT driver for Wafer 5823 single board computer initialising.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (timeout < 1 || timeout > 255) {
265 timeout = WD_TIMO;
Alan Cox694b16b2008-05-19 14:09:40 +0100266 printk(KERN_INFO PFX
267 "timeout value must be 1 <= x <= 255, using %d\n",
268 timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 if (wdt_stop != wdt_start) {
Alan Cox694b16b2008-05-19 14:09:40 +0100272 if (!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
273 printk(KERN_ERR PFX
274 "I/O address 0x%04x already in use\n",
275 wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ret = -EIO;
277 goto error;
278 }
279 }
280
Alan Cox694b16b2008-05-19 14:09:40 +0100281 if (!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
282 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 wdt_start);
284 ret = -EIO;
285 goto error2;
286 }
287
288 ret = register_reboot_notifier(&wafwdt_notifier);
289 if (ret != 0) {
Alan Cox694b16b2008-05-19 14:09:40 +0100290 printk(KERN_ERR PFX
291 "cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto error3;
293 }
294
295 ret = misc_register(&wafwdt_miscdev);
296 if (ret != 0) {
Alan Cox694b16b2008-05-19 14:09:40 +0100297 printk(KERN_ERR PFX
298 "cannot register miscdev on minor=%d (err=%d)\n",
299 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto error4;
301 }
302
Alan Cox694b16b2008-05-19 14:09:40 +0100303 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 timeout, nowayout);
305
306 return ret;
307error4:
308 unregister_reboot_notifier(&wafwdt_notifier);
309error3:
310 release_region(wdt_start, 1);
311error2:
312 if (wdt_stop != wdt_start)
313 release_region(wdt_stop, 1);
314error:
315 return ret;
316}
317
318static void __exit wafwdt_exit(void)
319{
320 misc_deregister(&wafwdt_miscdev);
321 unregister_reboot_notifier(&wafwdt_notifier);
Alan Cox694b16b2008-05-19 14:09:40 +0100322 if (wdt_stop != wdt_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 release_region(wdt_stop, 1);
324 release_region(wdt_start, 1);
325}
326
327module_init(wafwdt_init);
328module_exit(wafwdt_exit);
329
330MODULE_AUTHOR("Justin Cormack");
331MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
332MODULE_LICENSE("GPL");
333MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
334
335/* end of wafer5823wdt.c */