blob: 7289f4af93d0e6c90707ceac5f3b0bae1bb10336 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Acquire Single Board Computer Watchdog Timer driver
3 *
4 * Based on wdt.c. Original copyright messages:
5 *
6 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
7 * http://www.redhat.com
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
15 * warranty for any of this software. This material is provided
16 * "AS-IS" and at no charge.
17 *
18 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
19 *
20 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
21 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
22 * Can't add timeout - driver doesn't allow changing value
23 */
24
25/*
26 * Theory of Operation:
27 * The Watch-Dog Timer is provided to ensure that standalone
28 * Systems can always recover from catastrophic conditions that
29 * caused the CPU to crash. This condition may have occured by
30 * external EMI or a software bug. When the CPU stops working
31 * correctly, hardware on the board will either perform a hardware
32 * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
33 * system back to a known state.
34 *
35 * The Watch-Dog Timer is controlled by two I/O Ports.
36 * 443 hex - Read - Enable or refresh the Watch-Dog Timer
37 * 043 hex - Read - Disable the Watch-Dog Timer
38 *
39 * To enable the Watch-Dog Timer, a read from I/O port 443h must
40 * be performed. This will enable and activate the countdown timer
41 * which will eventually time out and either reset the CPU or cause
42 * an NMI depending on the setting of a jumper. To ensure that this
43 * reset condition does not occur, the Watch-Dog Timer must be
44 * periodically refreshed by reading the same I/O port 443h.
45 * The Watch-Dog Timer is disabled by reading I/O port 043h.
46 *
47 * The Watch-Dog Timer Time-Out Period is set via jumpers.
48 * It can be 1, 2, 10, 20, 110 or 220 seconds.
49 */
50
51#include <linux/module.h>
52#include <linux/moduleparam.h>
53#include <linux/types.h>
54#include <linux/miscdevice.h>
55#include <linux/watchdog.h>
56#include <linux/fs.h>
57#include <linux/ioport.h>
58#include <linux/notifier.h>
59#include <linux/reboot.h>
60#include <linux/init.h>
61
62#include <asm/io.h>
63#include <asm/uaccess.h>
64#include <asm/system.h>
65
66#define WATCHDOG_NAME "Acquire WDT"
67#define PFX WATCHDOG_NAME ": "
68#define WATCHDOG_HEARTBEAT 0 /* There is no way to see what the correct time-out period is */
69
70static unsigned long acq_is_open;
71static char expect_close;
72
73/*
74 * You must set these - there is no sane way to probe for this board.
75 */
76
77static int wdt_stop = 0x43;
78module_param(wdt_stop, int, 0);
79MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
80
81static int wdt_start = 0x443;
82module_param(wdt_start, int, 0);
83MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
84
Andrey Panin4bfdf372005-07-27 11:43:58 -070085static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param(nowayout, int, 0);
87MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
88
89/*
90 * Kernel methods.
91 */
92
93static void acq_keepalive(void)
94{
95 /* Write a watchdog value */
96 inb_p(wdt_start);
97}
98
99static void acq_stop(void)
100{
101 /* Turn the card off */
102 inb_p(wdt_stop);
103}
104
105/*
106 * /dev/watchdog handling.
107 */
108
109static ssize_t acq_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
110{
111 /* See if we got the magic character 'V' and reload the timer */
112 if(count) {
113 if (!nowayout) {
114 size_t i;
115
116 /* note: just in case someone wrote the magic character
117 * five months ago... */
118 expect_close = 0;
119
120 /* scan to see whether or not we got the magic character */
121 for (i = 0; i != count; i++) {
122 char c;
123 if (get_user(c, buf + i))
124 return -EFAULT;
125 if (c == 'V')
126 expect_close = 42;
127 }
128 }
129
130 /* Well, anyhow someone wrote to us, we should return that favour */
131 acq_keepalive();
132 }
133 return count;
134}
135
136static int acq_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
137 unsigned long arg)
138{
139 int options, retval = -EINVAL;
140 void __user *argp = (void __user *)arg;
141 int __user *p = argp;
142 static struct watchdog_info ident =
143 {
144 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
145 .firmware_version = 1,
146 .identity = "Acquire WDT",
147 };
148
149 switch(cmd)
150 {
151 case WDIOC_GETSUPPORT:
152 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
153
154 case WDIOC_GETSTATUS:
155 case WDIOC_GETBOOTSTATUS:
156 return put_user(0, p);
157
158 case WDIOC_KEEPALIVE:
159 acq_keepalive();
160 return 0;
161
162 case WDIOC_GETTIMEOUT:
163 return put_user(WATCHDOG_HEARTBEAT, p);
164
165 case WDIOC_SETOPTIONS:
166 {
167 if (get_user(options, p))
168 return -EFAULT;
169
170 if (options & WDIOS_DISABLECARD)
171 {
172 acq_stop();
173 retval = 0;
174 }
175
176 if (options & WDIOS_ENABLECARD)
177 {
178 acq_keepalive();
179 retval = 0;
180 }
181
182 return retval;
183 }
184
185 default:
186 return -ENOIOCTLCMD;
187 }
188}
189
190static int acq_open(struct inode *inode, struct file *file)
191{
192 if (test_and_set_bit(0, &acq_is_open))
193 return -EBUSY;
194
195 if (nowayout)
196 __module_get(THIS_MODULE);
197
198 /* Activate */
199 acq_keepalive();
200 return nonseekable_open(inode, file);
201}
202
203static int acq_close(struct inode *inode, struct file *file)
204{
205 if (expect_close == 42) {
206 acq_stop();
207 } else {
208 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
209 acq_keepalive();
210 }
211 clear_bit(0, &acq_is_open);
212 expect_close = 0;
213 return 0;
214}
215
216/*
217 * Notifier for system down
218 */
219
220static int acq_notify_sys(struct notifier_block *this, unsigned long code,
221 void *unused)
222{
223 if(code==SYS_DOWN || code==SYS_HALT) {
224 /* Turn the WDT off */
225 acq_stop();
226 }
227 return NOTIFY_DONE;
228}
229
230/*
231 * Kernel Interfaces
232 */
233
234static struct file_operations acq_fops = {
235 .owner = THIS_MODULE,
236 .llseek = no_llseek,
237 .write = acq_write,
238 .ioctl = acq_ioctl,
239 .open = acq_open,
240 .release = acq_close,
241};
242
243static struct miscdevice acq_miscdev=
244{
245 .minor = WATCHDOG_MINOR,
246 .name = "watchdog",
247 .fops = &acq_fops,
248};
249
250/*
251 * The WDT card needs to learn about soft shutdowns in order to
252 * turn the timebomb registers off.
253 */
254
255static struct notifier_block acq_notifier =
256{
257 .notifier_call = acq_notify_sys,
258};
259
260static int __init acq_init(void)
261{
262 int ret;
263
264 printk(KERN_INFO "WDT driver for Acquire single board computer initialising.\n");
265
266 if (wdt_stop != wdt_start) {
267 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
268 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
269 wdt_stop);
270 ret = -EIO;
271 goto out;
272 }
273 }
274
275 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
276 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
277 wdt_start);
278 ret = -EIO;
279 goto unreg_stop;
280 }
281
282 ret = register_reboot_notifier(&acq_notifier);
283 if (ret != 0) {
284 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
285 ret);
286 goto unreg_regions;
287 }
288
289 ret = misc_register(&acq_miscdev);
290 if (ret != 0) {
291 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
292 WATCHDOG_MINOR, ret);
293 goto unreg_reboot;
294 }
295
296 printk (KERN_INFO PFX "initialized. (nowayout=%d)\n",
297 nowayout);
298
299 return 0;
300
301unreg_reboot:
302 unregister_reboot_notifier(&acq_notifier);
303unreg_regions:
304 release_region(wdt_start, 1);
305unreg_stop:
306 if (wdt_stop != wdt_start)
307 release_region(wdt_stop, 1);
308out:
309 return ret;
310}
311
312static void __exit acq_exit(void)
313{
314 misc_deregister(&acq_miscdev);
315 unregister_reboot_notifier(&acq_notifier);
316 if(wdt_stop != wdt_start)
317 release_region(wdt_stop,1);
318 release_region(wdt_start,1);
319}
320
321module_init(acq_init);
322module_exit(acq_exit);
323
324MODULE_AUTHOR("David Woodhouse");
325MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
326MODULE_LICENSE("GPL");
327MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);