blob: b94e6ef4c7a7a08c8f44c19dc8d0fd220e32bba2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Wim Van Sebroeck0f112a82007-08-09 19:38:00 +00002 * Eurotech CPU-1220/1410/1420 on board WDT driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (c) Copyright 2001 Ascensit <support@ascensit.com>
5 * (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com>
6 * (c) Copyright 2002 Rob Radez <rob@osinvestor.com>
7 *
8 * Based on wdt.c.
9 * Original copyright messages:
10 *
11 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
12 * http://www.redhat.com
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
20 * warranty for any of this software. This material is provided
21 * "AS-IS" and at no charge.
22 *
23 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>*
24 */
25
26/* Changelog:
27 *
Wim Van Sebroeck0f112a82007-08-09 19:38:00 +000028 * 2001 - Rodolfo Giometti
29 * Initial release
30 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * 2002/04/25 - Rob Radez
32 * clean up #includes
33 * clean up locking
34 * make __setup param unique
35 * proper options in watchdog_info
36 * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
37 * add expect_close support
38 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40 * Added Matt Domsch's nowayout module option.
41 */
42
Wim Van Sebroeck0f112a82007-08-09 19:38:00 +000043/*
44 * The eurotech CPU-1220/1410/1420's watchdog is a part
45 * of the on-board SUPER I/O device SMSC FDC 37B782.
46 */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/interrupt.h>
49#include <linux/module.h>
50#include <linux/moduleparam.h>
51#include <linux/types.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 Cox89ea2422008-05-19 14:05:41 +010059#include <linux/io.h>
60#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/system.h>
63
64static unsigned long eurwdt_is_open;
65static int eurwdt_timeout;
66static char eur_expect_close;
Alan Cox89ea2422008-05-19 14:05:41 +010067static spinlock_t eurwdt_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * You must set these - there is no sane way to probe for this board.
71 * You can use eurwdt=x,y to set these now.
72 */
73
74static int io = 0x3f0;
75static int irq = 10;
76static char *ev = "int";
77
78#define WDT_TIMEOUT 60 /* 1 minute */
79
Andrey Panin4bfdf372005-07-27 11:43:58 -070080static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081module_param(nowayout, int, 0);
Alan Cox89ea2422008-05-19 14:05:41 +010082MODULE_PARM_DESC(nowayout,
83 "Watchdog cannot be stopped once started (default="
84 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Some symbolic names
88 */
89
90#define WDT_CTRL_REG 0x30
91#define WDT_OUTPIN_CFG 0xe2
92#define WDT_EVENT_INT 0x00
93#define WDT_EVENT_REBOOT 0x08
94#define WDT_UNIT_SEL 0xf1
95#define WDT_UNIT_SECS 0x80
96#define WDT_TIMEOUT_VAL 0xf2
97#define WDT_TIMER_CFG 0xf3
98
99
100module_param(io, int, 0);
101MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
102module_param(irq, int, 0);
103MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
104module_param(ev, charp, 0);
105MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
106
107
108/*
109 * Programming support
110 */
111
112static inline void eurwdt_write_reg(u8 index, u8 data)
113{
114 outb(index, io);
115 outb(data, io+1);
116}
117
118static inline void eurwdt_lock_chip(void)
119{
120 outb(0xaa, io);
121}
122
123static inline void eurwdt_unlock_chip(void)
124{
125 outb(0x55, io);
126 eurwdt_write_reg(0x07, 0x08); /* set the logical device */
127}
128
129static inline void eurwdt_set_timeout(int timeout)
130{
131 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
132}
133
134static inline void eurwdt_disable_timer(void)
135{
136 eurwdt_set_timeout(0);
137}
138
139static void eurwdt_activate_timer(void)
140{
141 eurwdt_disable_timer();
142 eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */
Alan Cox89ea2422008-05-19 14:05:41 +0100143 eurwdt_write_reg(WDT_OUTPIN_CFG,
144 !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* Setting interrupt line */
147 if (irq == 2 || irq > 15 || irq < 0) {
148 printk(KERN_ERR ": invalid irq number\n");
149 irq = 0; /* if invalid we disable interrupt */
150 }
151 if (irq == 0)
152 printk(KERN_INFO ": interrupt disabled\n");
153
154 eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
155
156 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
157 eurwdt_set_timeout(0); /* the default timeout */
158}
159
160
161/*
162 * Kernel methods.
163 */
164
David Howells7d12e782006-10-05 14:55:46 +0100165static irqreturn_t eurwdt_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 printk(KERN_CRIT "timeout WDT timeout\n");
168
169#ifdef ONLY_TESTING
170 printk(KERN_CRIT "Would Reboot.\n");
171#else
172 printk(KERN_CRIT "Initiating system reboot.\n");
Andrew Mortoncc1d3a92005-07-26 21:41:37 -0700173 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
175 return IRQ_HANDLED;
176}
177
178
179/**
180 * eurwdt_ping:
181 *
182 * Reload counter one with the watchdog timeout.
183 */
184
185static void eurwdt_ping(void)
186{
187 /* Write the watchdog default value */
188 eurwdt_set_timeout(eurwdt_timeout);
189}
190
191/**
192 * eurwdt_write:
193 * @file: file handle to the watchdog
194 * @buf: buffer to write (unused as data does not matter here
195 * @count: count of bytes
196 * @ppos: pointer to the position to write. No seeks allowed
197 *
198 * A write to a watchdog device is defined as a keepalive signal. Any
199 * write of data will do, as we we don't define content meaning.
200 */
201
202static ssize_t eurwdt_write(struct file *file, const char __user *buf,
203size_t count, loff_t *ppos)
204{
205 if (count) {
206 if (!nowayout) {
207 size_t i;
208
209 eur_expect_close = 0;
210
211 for (i = 0; i != count; i++) {
212 char c;
Alan Cox89ea2422008-05-19 14:05:41 +0100213 if (get_user(c, buf+i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EFAULT;
215 if (c == 'V')
216 eur_expect_close = 42;
217 }
218 }
Alan Cox89ea2422008-05-19 14:05:41 +0100219 spin_lock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 eurwdt_ping(); /* the default timeout */
Alan Cox89ea2422008-05-19 14:05:41 +0100221 spin_unlock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return count;
224}
225
226/**
227 * eurwdt_ioctl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * @file: file handle to the device
229 * @cmd: watchdog command
230 * @arg: argument pointer
231 *
232 * The watchdog API defines a common set of functions for all watchdogs
233 * according to their available features.
234 */
235
Alan Cox89ea2422008-05-19 14:05:41 +0100236static long eurwdt_ioctl(struct file *file,
237 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 void __user *argp = (void __user *)arg;
240 int __user *p = argp;
241 static struct watchdog_info ident = {
Alan Cox89ea2422008-05-19 14:05:41 +0100242 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
243 | WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .firmware_version = 1,
245 .identity = "WDT Eurotech CPU-1220/1410",
246 };
247
248 int time;
249 int options, retval = -EINVAL;
250
Alan Cox89ea2422008-05-19 14:05:41 +0100251 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200253 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 case WDIOC_GETSUPPORT:
256 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
257
258 case WDIOC_GETSTATUS:
259 case WDIOC_GETBOOTSTATUS:
260 return put_user(0, p);
261
262 case WDIOC_KEEPALIVE:
Alan Cox89ea2422008-05-19 14:05:41 +0100263 spin_lock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 eurwdt_ping();
Alan Cox89ea2422008-05-19 14:05:41 +0100265 spin_unlock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267
268 case WDIOC_SETTIMEOUT:
269 if (copy_from_user(&time, p, sizeof(int)))
270 return -EFAULT;
271
272 /* Sanity check */
273 if (time < 0 || time > 255)
274 return -EINVAL;
275
Alan Cox89ea2422008-05-19 14:05:41 +0100276 spin_lock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 eurwdt_timeout = time;
278 eurwdt_set_timeout(time);
Alan Cox89ea2422008-05-19 14:05:41 +0100279 spin_unlock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Fall */
281
282 case WDIOC_GETTIMEOUT:
283 return put_user(eurwdt_timeout, p);
284
285 case WDIOC_SETOPTIONS:
286 if (get_user(options, p))
287 return -EFAULT;
Alan Cox89ea2422008-05-19 14:05:41 +0100288 spin_lock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (options & WDIOS_DISABLECARD) {
290 eurwdt_disable_timer();
291 retval = 0;
292 }
293 if (options & WDIOS_ENABLECARD) {
294 eurwdt_activate_timer();
295 eurwdt_ping();
296 retval = 0;
297 }
Alan Cox89ea2422008-05-19 14:05:41 +0100298 spin_unlock(&eurwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return retval;
300 }
301}
302
303/**
304 * eurwdt_open:
305 * @inode: inode of device
306 * @file: file handle to device
307 *
308 * The misc device has been opened. The watchdog device is single
309 * open and on opening we load the counter.
310 */
311
312static int eurwdt_open(struct inode *inode, struct file *file)
313{
314 if (test_and_set_bit(0, &eurwdt_is_open))
315 return -EBUSY;
316 eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
317 /* Activate the WDT */
318 eurwdt_activate_timer();
319 return nonseekable_open(inode, file);
320}
321
322/**
323 * eurwdt_release:
324 * @inode: inode to board
325 * @file: file handle to board
326 *
327 * The watchdog has a configurable API. There is a religious dispute
328 * between people who want their watchdog to be able to shut down and
329 * those who want to be sure if the watchdog manager dies the machine
330 * reboots. In the former case we disable the counters, in the latter
331 * case you have to open it again very soon.
332 */
333
334static int eurwdt_release(struct inode *inode, struct file *file)
335{
Alan Cox89ea2422008-05-19 14:05:41 +0100336 if (eur_expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 eurwdt_disable_timer();
Alan Cox89ea2422008-05-19 14:05:41 +0100338 else {
339 printk(KERN_CRIT
340 "eurwdt: Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 eurwdt_ping();
342 }
343 clear_bit(0, &eurwdt_is_open);
344 eur_expect_close = 0;
345 return 0;
346}
347
348/**
349 * eurwdt_notify_sys:
350 * @this: our notifier block
351 * @code: the event being reported
352 * @unused: unused
353 *
354 * Our notifier is called on system shutdowns. We want to turn the card
355 * off at reboot otherwise the machine will reboot again during memory
356 * test or worse yet during the following fsck. This would suck, in fact
357 * trust me - if it happens it does suck.
358 */
359
360static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
361 void *unused)
362{
363 if (code == SYS_DOWN || code == SYS_HALT) {
364 /* Turn the card off */
365 eurwdt_disable_timer();
366 }
367
368 return NOTIFY_DONE;
369}
370
371/*
372 * Kernel Interfaces
373 */
374
375
Arjan van de Ven62322d22006-07-03 00:24:21 -0700376static const struct file_operations eurwdt_fops = {
Alan Cox89ea2422008-05-19 14:05:41 +0100377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .write = eurwdt_write,
380 .unlocked_ioctl = eurwdt_ioctl,
381 .open = eurwdt_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 .release = eurwdt_release,
383};
384
385static struct miscdevice eurwdt_miscdev = {
386 .minor = WATCHDOG_MINOR,
387 .name = "watchdog",
388 .fops = &eurwdt_fops,
389};
390
391/*
392 * The WDT card needs to learn about soft shutdowns in order to
393 * turn the timebomb registers off.
394 */
395
396static struct notifier_block eurwdt_notifier = {
397 .notifier_call = eurwdt_notify_sys,
398};
399
400/**
401 * cleanup_module:
402 *
403 * Unload the watchdog. You cannot do this with any file handles open.
404 * If your watchdog is set to continue ticking on close and you unload
405 * it, well it keeps ticking. We won't get the interrupt but the board
406 * will not touch PC memory so all is fine. You just have to load a new
407 * module in 60 seconds or reboot.
408 */
409
410static void __exit eurwdt_exit(void)
411{
412 eurwdt_lock_chip();
413
414 misc_deregister(&eurwdt_miscdev);
415
416 unregister_reboot_notifier(&eurwdt_notifier);
417 release_region(io, 2);
418 free_irq(irq, NULL);
419}
420
421/**
422 * eurwdt_init:
423 *
424 * Set up the WDT watchdog board. After grabbing the resources
425 * we require we need also to unlock the device.
426 * The open() function will actually kick the board off.
427 */
428
429static int __init eurwdt_init(void)
430{
431 int ret;
432
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700433 ret = request_irq(irq, eurwdt_interrupt, IRQF_DISABLED, "eurwdt", NULL);
Alan Cox89ea2422008-05-19 14:05:41 +0100434 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 if (!request_region(io, 2, "eurwdt")) {
440 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
441 ret = -EBUSY;
442 goto outirq;
443 }
444
445 ret = register_reboot_notifier(&eurwdt_notifier);
446 if (ret) {
Alan Cox89ea2422008-05-19 14:05:41 +0100447 printk(KERN_ERR
448 "eurwdt: can't register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto outreg;
450 }
451
Alan Cox89ea2422008-05-19 14:05:41 +0100452 spin_lock_init(&eurwdt_lock);
453
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300454 ret = misc_register(&eurwdt_miscdev);
455 if (ret) {
456 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
457 WATCHDOG_MINOR);
458 goto outreboot;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 eurwdt_unlock_chip();
462
463 ret = 0;
464 printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
465 " - timeout event: %s\n",
466 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
467
468out:
469 return ret;
470
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300471outreboot:
472 unregister_reboot_notifier(&eurwdt_notifier);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474outreg:
475 release_region(io, 2);
476
477outirq:
478 free_irq(irq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
480}
481
482module_init(eurwdt_init);
483module_exit(eurwdt_exit);
484
485MODULE_AUTHOR("Rodolfo Giometti");
486MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
487MODULE_LICENSE("GPL");
488MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);