blob: 25c2f257561152094e832aeac08d7ebcd9a8ed82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Eurotech CPU-1220/1410 on board WDT driver
3 *
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 *
28 * 2002/04/25 - Rob Radez
29 * clean up #includes
30 * clean up locking
31 * make __setup param unique
32 * proper options in watchdog_info
33 * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
34 * add expect_close support
35 *
36 * 2001 - Rodolfo Giometti
37 * Initial release
38 *
39 * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40 * Added Matt Domsch's nowayout module option.
41 */
42
43#include <linux/config.h>
44#include <linux/interrupt.h>
45#include <linux/module.h>
46#include <linux/moduleparam.h>
47#include <linux/types.h>
48#include <linux/miscdevice.h>
49#include <linux/watchdog.h>
50#include <linux/fs.h>
51#include <linux/ioport.h>
52#include <linux/notifier.h>
53#include <linux/reboot.h>
54#include <linux/init.h>
55
56#include <asm/io.h>
57#include <asm/uaccess.h>
58#include <asm/system.h>
59
60static unsigned long eurwdt_is_open;
61static int eurwdt_timeout;
62static char eur_expect_close;
63
64/*
65 * You must set these - there is no sane way to probe for this board.
66 * You can use eurwdt=x,y to set these now.
67 */
68
69static int io = 0x3f0;
70static int irq = 10;
71static char *ev = "int";
72
73#define WDT_TIMEOUT 60 /* 1 minute */
74
Andrey Panin4bfdf372005-07-27 11:43:58 -070075static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076module_param(nowayout, int, 0);
77MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
78
79/*
80 * Some symbolic names
81 */
82
83#define WDT_CTRL_REG 0x30
84#define WDT_OUTPIN_CFG 0xe2
85#define WDT_EVENT_INT 0x00
86#define WDT_EVENT_REBOOT 0x08
87#define WDT_UNIT_SEL 0xf1
88#define WDT_UNIT_SECS 0x80
89#define WDT_TIMEOUT_VAL 0xf2
90#define WDT_TIMER_CFG 0xf3
91
92
93module_param(io, int, 0);
94MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
95module_param(irq, int, 0);
96MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
97module_param(ev, charp, 0);
98MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
99
100
101/*
102 * Programming support
103 */
104
105static inline void eurwdt_write_reg(u8 index, u8 data)
106{
107 outb(index, io);
108 outb(data, io+1);
109}
110
111static inline void eurwdt_lock_chip(void)
112{
113 outb(0xaa, io);
114}
115
116static inline void eurwdt_unlock_chip(void)
117{
118 outb(0x55, io);
119 eurwdt_write_reg(0x07, 0x08); /* set the logical device */
120}
121
122static inline void eurwdt_set_timeout(int timeout)
123{
124 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
125}
126
127static inline void eurwdt_disable_timer(void)
128{
129 eurwdt_set_timeout(0);
130}
131
132static void eurwdt_activate_timer(void)
133{
134 eurwdt_disable_timer();
135 eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */
136 eurwdt_write_reg(WDT_OUTPIN_CFG, !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
137
138 /* Setting interrupt line */
139 if (irq == 2 || irq > 15 || irq < 0) {
140 printk(KERN_ERR ": invalid irq number\n");
141 irq = 0; /* if invalid we disable interrupt */
142 }
143 if (irq == 0)
144 printk(KERN_INFO ": interrupt disabled\n");
145
146 eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
147
148 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
149 eurwdt_set_timeout(0); /* the default timeout */
150}
151
152
153/*
154 * Kernel methods.
155 */
156
157static irqreturn_t eurwdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
158{
159 printk(KERN_CRIT "timeout WDT timeout\n");
160
161#ifdef ONLY_TESTING
162 printk(KERN_CRIT "Would Reboot.\n");
163#else
164 printk(KERN_CRIT "Initiating system reboot.\n");
Andrew Mortoncc1d3a92005-07-26 21:41:37 -0700165 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif
167 return IRQ_HANDLED;
168}
169
170
171/**
172 * eurwdt_ping:
173 *
174 * Reload counter one with the watchdog timeout.
175 */
176
177static void eurwdt_ping(void)
178{
179 /* Write the watchdog default value */
180 eurwdt_set_timeout(eurwdt_timeout);
181}
182
183/**
184 * eurwdt_write:
185 * @file: file handle to the watchdog
186 * @buf: buffer to write (unused as data does not matter here
187 * @count: count of bytes
188 * @ppos: pointer to the position to write. No seeks allowed
189 *
190 * A write to a watchdog device is defined as a keepalive signal. Any
191 * write of data will do, as we we don't define content meaning.
192 */
193
194static ssize_t eurwdt_write(struct file *file, const char __user *buf,
195size_t count, loff_t *ppos)
196{
197 if (count) {
198 if (!nowayout) {
199 size_t i;
200
201 eur_expect_close = 0;
202
203 for (i = 0; i != count; i++) {
204 char c;
205 if(get_user(c, buf+i))
206 return -EFAULT;
207 if (c == 'V')
208 eur_expect_close = 42;
209 }
210 }
211 eurwdt_ping(); /* the default timeout */
212 }
213
214 return count;
215}
216
217/**
218 * eurwdt_ioctl:
219 * @inode: inode of the device
220 * @file: file handle to the device
221 * @cmd: watchdog command
222 * @arg: argument pointer
223 *
224 * The watchdog API defines a common set of functions for all watchdogs
225 * according to their available features.
226 */
227
228static int eurwdt_ioctl(struct inode *inode, struct file *file,
229 unsigned int cmd, unsigned long arg)
230{
231 void __user *argp = (void __user *)arg;
232 int __user *p = argp;
233 static struct watchdog_info ident = {
234 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
235 .firmware_version = 1,
236 .identity = "WDT Eurotech CPU-1220/1410",
237 };
238
239 int time;
240 int options, retval = -EINVAL;
241
242 switch(cmd) {
243 default:
244 return -ENOIOCTLCMD;
245
246 case WDIOC_GETSUPPORT:
247 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
248
249 case WDIOC_GETSTATUS:
250 case WDIOC_GETBOOTSTATUS:
251 return put_user(0, p);
252
253 case WDIOC_KEEPALIVE:
254 eurwdt_ping();
255 return 0;
256
257 case WDIOC_SETTIMEOUT:
258 if (copy_from_user(&time, p, sizeof(int)))
259 return -EFAULT;
260
261 /* Sanity check */
262 if (time < 0 || time > 255)
263 return -EINVAL;
264
265 eurwdt_timeout = time;
266 eurwdt_set_timeout(time);
267 /* Fall */
268
269 case WDIOC_GETTIMEOUT:
270 return put_user(eurwdt_timeout, p);
271
272 case WDIOC_SETOPTIONS:
273 if (get_user(options, p))
274 return -EFAULT;
275 if (options & WDIOS_DISABLECARD) {
276 eurwdt_disable_timer();
277 retval = 0;
278 }
279 if (options & WDIOS_ENABLECARD) {
280 eurwdt_activate_timer();
281 eurwdt_ping();
282 retval = 0;
283 }
284 return retval;
285 }
286}
287
288/**
289 * eurwdt_open:
290 * @inode: inode of device
291 * @file: file handle to device
292 *
293 * The misc device has been opened. The watchdog device is single
294 * open and on opening we load the counter.
295 */
296
297static int eurwdt_open(struct inode *inode, struct file *file)
298{
299 if (test_and_set_bit(0, &eurwdt_is_open))
300 return -EBUSY;
301 eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
302 /* Activate the WDT */
303 eurwdt_activate_timer();
304 return nonseekable_open(inode, file);
305}
306
307/**
308 * eurwdt_release:
309 * @inode: inode to board
310 * @file: file handle to board
311 *
312 * The watchdog has a configurable API. There is a religious dispute
313 * between people who want their watchdog to be able to shut down and
314 * those who want to be sure if the watchdog manager dies the machine
315 * reboots. In the former case we disable the counters, in the latter
316 * case you have to open it again very soon.
317 */
318
319static int eurwdt_release(struct inode *inode, struct file *file)
320{
321 if (eur_expect_close == 42) {
322 eurwdt_disable_timer();
323 } else {
324 printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
325 eurwdt_ping();
326 }
327 clear_bit(0, &eurwdt_is_open);
328 eur_expect_close = 0;
329 return 0;
330}
331
332/**
333 * eurwdt_notify_sys:
334 * @this: our notifier block
335 * @code: the event being reported
336 * @unused: unused
337 *
338 * Our notifier is called on system shutdowns. We want to turn the card
339 * off at reboot otherwise the machine will reboot again during memory
340 * test or worse yet during the following fsck. This would suck, in fact
341 * trust me - if it happens it does suck.
342 */
343
344static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
345 void *unused)
346{
347 if (code == SYS_DOWN || code == SYS_HALT) {
348 /* Turn the card off */
349 eurwdt_disable_timer();
350 }
351
352 return NOTIFY_DONE;
353}
354
355/*
356 * Kernel Interfaces
357 */
358
359
360static struct file_operations eurwdt_fops = {
361 .owner = THIS_MODULE,
362 .llseek = no_llseek,
363 .write = eurwdt_write,
364 .ioctl = eurwdt_ioctl,
365 .open = eurwdt_open,
366 .release = eurwdt_release,
367};
368
369static struct miscdevice eurwdt_miscdev = {
370 .minor = WATCHDOG_MINOR,
371 .name = "watchdog",
372 .fops = &eurwdt_fops,
373};
374
375/*
376 * The WDT card needs to learn about soft shutdowns in order to
377 * turn the timebomb registers off.
378 */
379
380static struct notifier_block eurwdt_notifier = {
381 .notifier_call = eurwdt_notify_sys,
382};
383
384/**
385 * cleanup_module:
386 *
387 * Unload the watchdog. You cannot do this with any file handles open.
388 * If your watchdog is set to continue ticking on close and you unload
389 * it, well it keeps ticking. We won't get the interrupt but the board
390 * will not touch PC memory so all is fine. You just have to load a new
391 * module in 60 seconds or reboot.
392 */
393
394static void __exit eurwdt_exit(void)
395{
396 eurwdt_lock_chip();
397
398 misc_deregister(&eurwdt_miscdev);
399
400 unregister_reboot_notifier(&eurwdt_notifier);
401 release_region(io, 2);
402 free_irq(irq, NULL);
403}
404
405/**
406 * eurwdt_init:
407 *
408 * Set up the WDT watchdog board. After grabbing the resources
409 * we require we need also to unlock the device.
410 * The open() function will actually kick the board off.
411 */
412
413static int __init eurwdt_init(void)
414{
415 int ret;
416
417 ret = misc_register(&eurwdt_miscdev);
418 if (ret) {
419 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
420 WATCHDOG_MINOR);
421 goto out;
422 }
423
424 ret = request_irq(irq, eurwdt_interrupt, SA_INTERRUPT, "eurwdt", NULL);
425 if(ret) {
426 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
427 goto outmisc;
428 }
429
430 if (!request_region(io, 2, "eurwdt")) {
431 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
432 ret = -EBUSY;
433 goto outirq;
434 }
435
436 ret = register_reboot_notifier(&eurwdt_notifier);
437 if (ret) {
438 printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
439 goto outreg;
440 }
441
442 eurwdt_unlock_chip();
443
444 ret = 0;
445 printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
446 " - timeout event: %s\n",
447 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
448
449out:
450 return ret;
451
452outreg:
453 release_region(io, 2);
454
455outirq:
456 free_irq(irq, NULL);
457
458outmisc:
459 misc_deregister(&eurwdt_miscdev);
460 goto out;
461}
462
463module_init(eurwdt_init);
464module_exit(eurwdt_exit);
465
466MODULE_AUTHOR("Rodolfo Giometti");
467MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
468MODULE_LICENSE("GPL");
469MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);