blob: e76e7e3093015595b4c1caaa57b7f887847c0a49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * W83877F Computer Watchdog Timer driver
3 *
4 * Based on acquirewdt.c by Alan Cox,
5 * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * The authors do NOT admit liability nor provide warranty for
13 * any of this software. This material is provided "AS-IS" in
14 * the hope that it may be useful for others.
15 *
16 * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
17 *
18 * 4/19 - 2001 [Initial revision]
19 * 9/27 - 2001 Added spinlocking
20 * 4/12 - 2002 [rob@osinvestor.com] Eliminate extra comments
21 * Eliminate fop_read
22 * Eliminate extra spin_unlock
23 * Added KERN_* tags to printks
24 * add CONFIG_WATCHDOG_NOWAYOUT support
25 * fix possible wdt_is_open race
Alan Coxc1cfd1a2008-05-19 14:09:29 +010026 * changed watchdog_info to correctly reflect what
27 * the driver offers
28 * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
29 * WDIOC_SETTIMEOUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
31 * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
32 * added extra printk's for startup problems
33 * use module_param
Alan Coxc1cfd1a2008-05-19 14:09:29 +010034 * made timeout (the emulated heartbeat) a
35 * module_param
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * made the keepalive ping an internal subroutine
37 *
38 * This WDT driver is different from most other Linux WDT
39 * drivers in that the driver will ping the watchdog by itself,
40 * because this particular WDT has a very short timeout (1.6
41 * seconds) and it would be insane to count on any userspace
42 * daemon always getting scheduled within that time frame.
43 */
44
Joe Perches27c766a2012-02-15 15:06:19 -080045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/moduleparam.h>
49#include <linux/types.h>
50#include <linux/timer.h>
51#include <linux/jiffies.h>
52#include <linux/miscdevice.h>
53#include <linux/watchdog.h>
54#include <linux/fs.h>
55#include <linux/ioport.h>
56#include <linux/notifier.h>
57#include <linux/reboot.h>
58#include <linux/init.h>
Alan Coxc1cfd1a2008-05-19 14:09:29 +010059#include <linux/io.h>
60#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/system.h>
62
63#define OUR_NAME "w83877f_wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define ENABLE_W83877F_PORT 0x3F0
66#define ENABLE_W83877F 0x87
67#define DISABLE_W83877F 0xAA
68#define WDT_PING 0x443
69#define WDT_REGISTER 0x14
70#define WDT_ENABLE 0x9C
71#define WDT_DISABLE 0x8C
72
73/*
74 * The W83877F seems to be fixed at 1.6s timeout (at least on the
75 * EMACS PC-104 board I'm using). If we reset the watchdog every
76 * ~250ms we should be safe. */
77
78#define WDT_INTERVAL (HZ/4+1)
79
80/*
81 * We must not require too good response from the userspace daemon.
82 * Here we require the userspace daemon to send us a heartbeat
83 * char to /dev/watchdog every 30 seconds.
84 */
85
86#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
Alan Coxc1cfd1a2008-05-19 14:09:29 +010087/* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
88static int timeout = WATCHDOG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089module_param(timeout, int, 0);
Alan Coxc1cfd1a2008-05-19 14:09:29 +010090MODULE_PARM_DESC(timeout,
91 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
92 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010095static bool nowayout = WATCHDOG_NOWAYOUT;
96module_param(nowayout, bool, 0);
Alan Coxc1cfd1a2008-05-19 14:09:29 +010097MODULE_PARM_DESC(nowayout,
98 "Watchdog cannot be stopped once started (default="
99 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static void wdt_timer_ping(unsigned long);
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100102static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static unsigned long next_heartbeat;
104static unsigned long wdt_is_open;
105static char wdt_expect_close;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -0700106static DEFINE_SPINLOCK(wdt_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Whack the dog
110 */
111
112static void wdt_timer_ping(unsigned long data)
113{
114 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
115 * we agree to ping the WDT
116 */
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100117 if (time_before(jiffies, next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* Ping the WDT */
119 spin_lock(&wdt_spinlock);
120
121 /* Ping the WDT by reading from WDT_PING */
122 inb_p(WDT_PING);
123
124 /* Re-set the timer interval */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100125 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 spin_unlock(&wdt_spinlock);
128
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100129 } else
Joe Perches27c766a2012-02-15 15:06:19 -0800130 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/*
134 * Utility routines
135 */
136
137static void wdt_change(int writeval)
138{
139 unsigned long flags;
140 spin_lock_irqsave(&wdt_spinlock, flags);
141
142 /* buy some time */
143 inb_p(WDT_PING);
144
145 /* make W83877F available */
146 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
147 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
148
149 /* enable watchdog */
150 outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
151 outb_p(writeval, ENABLE_W83877F_PORT+1);
152
153 /* lock the W8387FF away */
154 outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
155
156 spin_unlock_irqrestore(&wdt_spinlock, flags);
157}
158
159static void wdt_startup(void)
160{
161 next_heartbeat = jiffies + (timeout * HZ);
162
163 /* Start the timer */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100164 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 wdt_change(WDT_ENABLE);
167
Joe Perches27c766a2012-02-15 15:06:19 -0800168 pr_info("Watchdog timer is now enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static void wdt_turnoff(void)
172{
173 /* Stop the timer */
174 del_timer(&timer);
175
176 wdt_change(WDT_DISABLE);
177
Joe Perches27c766a2012-02-15 15:06:19 -0800178 pr_info("Watchdog timer is now disabled...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static void wdt_keepalive(void)
182{
183 /* user land ping */
184 next_heartbeat = jiffies + (timeout * HZ);
185}
186
187/*
188 * /dev/watchdog handling
189 */
190
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100191static ssize_t fop_write(struct file *file, const char __user *buf,
192 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 /* See if we got the magic character 'V' and reload the timer */
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100195 if (count) {
196 if (!nowayout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 size_t ofs;
198
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100199 /* note: just in case someone wrote the magic
200 character five months ago... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 wdt_expect_close = 0;
202
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100203 /* scan to see whether or not we got the
204 magic character */
205 for (ofs = 0; ofs != count; ofs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 char c;
207 if (get_user(c, buf + ofs))
208 return -EFAULT;
209 if (c == 'V')
210 wdt_expect_close = 42;
211 }
212 }
213
214 /* someone wrote to us, we should restart timer */
215 wdt_keepalive();
216 }
217 return count;
218}
219
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100220static int fop_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 /* Just in case we're already talking to someone... */
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100223 if (test_and_set_bit(0, &wdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EBUSY;
225
226 /* Good, fire up the show */
227 wdt_startup();
228 return nonseekable_open(inode, file);
229}
230
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100231static int fop_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100233 if (wdt_expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 wdt_turnoff();
235 else {
236 del_timer(&timer);
Joe Perches27c766a2012-02-15 15:06:19 -0800237 pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 clear_bit(0, &wdt_is_open);
240 wdt_expect_close = 0;
241 return 0;
242}
243
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100244static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 void __user *argp = (void __user *)arg;
247 int __user *p = argp;
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100248 static const struct watchdog_info ident = {
249 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
250 | WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .firmware_version = 1,
252 .identity = "W83877F",
253 };
254
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100255 switch (cmd) {
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100256 case WDIOC_GETSUPPORT:
257 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
258 case WDIOC_GETSTATUS:
259 case WDIOC_GETBOOTSTATUS:
260 return put_user(0, p);
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100261 case WDIOC_SETOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 {
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100263 int new_options, retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100265 if (get_user(new_options, p))
266 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100268 if (new_options & WDIOS_DISABLECARD) {
269 wdt_turnoff();
270 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100273 if (new_options & WDIOS_ENABLECARD) {
274 wdt_startup();
275 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100277
278 return retval;
279 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000280 case WDIOC_KEEPALIVE:
281 wdt_keepalive();
282 return 0;
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100283 case WDIOC_SETTIMEOUT:
284 {
285 int new_timeout;
286
287 if (get_user(new_timeout, p))
288 return -EFAULT;
289
290 /* arbitrary upper limit */
291 if (new_timeout < 1 || new_timeout > 3600)
292 return -EINVAL;
293
294 timeout = new_timeout;
295 wdt_keepalive();
296 /* Fall through */
297 }
298 case WDIOC_GETTIMEOUT:
299 return put_user(timeout, p);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000300 default:
301 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
Arjan van de Ven62322d22006-07-03 00:24:21 -0700305static const struct file_operations wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
308 .write = fop_write,
309 .open = fop_open,
310 .release = fop_close,
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100311 .unlocked_ioctl = fop_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
314static struct miscdevice wdt_miscdev = {
315 .minor = WATCHDOG_MINOR,
316 .name = "watchdog",
317 .fops = &wdt_fops,
318};
319
320/*
321 * Notifier for system down
322 */
323
324static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
325 void *unused)
326{
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100327 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 wdt_turnoff();
329 return NOTIFY_DONE;
330}
331
332/*
333 * The WDT needs to learn about soft shutdowns in order to
334 * turn the timebomb registers off.
335 */
336
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100337static struct notifier_block wdt_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .notifier_call = wdt_notify_sys,
339};
340
341static void __exit w83877f_wdt_unload(void)
342{
343 wdt_turnoff();
344
345 /* Deregister */
346 misc_deregister(&wdt_miscdev);
347
348 unregister_reboot_notifier(&wdt_notifier);
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100349 release_region(WDT_PING, 1);
350 release_region(ENABLE_W83877F_PORT, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static int __init w83877f_wdt_init(void)
354{
355 int rc = -EBUSY;
356
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100357 if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 timeout = WATCHDOG_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800359 pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
360 timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100363 if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800364 pr_err("I/O address 0x%04x already in use\n",
365 ENABLE_W83877F_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 rc = -EIO;
367 goto err_out;
368 }
369
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100370 if (!request_region(WDT_PING, 1, "W8387FF WDT")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800371 pr_err("I/O address 0x%04x already in use\n", WDT_PING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 rc = -EIO;
373 goto err_out_region1;
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 rc = register_reboot_notifier(&wdt_notifier);
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100377 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800378 pr_err("cannot register reboot notifier (err=%d)\n", rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000379 goto err_out_region2;
380 }
381
382 rc = misc_register(&wdt_miscdev);
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100383 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800384 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
385 wdt_miscdev.minor, rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000386 goto err_out_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Joe Perches27c766a2012-02-15 15:06:19 -0800389 pr_info("WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 timeout, nowayout);
391
392 return 0;
393
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000394err_out_reboot:
395 unregister_reboot_notifier(&wdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396err_out_region2:
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100397 release_region(WDT_PING, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398err_out_region1:
Alan Coxc1cfd1a2008-05-19 14:09:29 +0100399 release_region(ENABLE_W83877F_PORT, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400err_out:
401 return rc;
402}
403
404module_init(w83877f_wdt_init);
405module_exit(w83877f_wdt_unload);
406
407MODULE_AUTHOR("Scott and Bill Jennings");
408MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
409MODULE_LICENSE("GPL");
410MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);