USB: updates to usb_reset_composite_device()
[linux-2.6/mini2440.git] / drivers / watchdog / smsc37b787_wdt.c
blob5d2b5ba61414664c452729361d159f6a7d639400
1 /*
2 * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x
4 * Based on acquirewdt.c by Alan Cox <alan@redhat.com>
5 * and some other existing drivers
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.
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.
16 * (C) Copyright 2003-2006 Sven Anders <anders@anduras.de>
18 * History:
19 * 2003 - Created version 1.0 for Linux 2.4.x.
20 * 2006 - Ported to Linux 2.6, added nowayout and MAGICCLOSE
21 * features. Released version 1.1
23 * Theory of operation:
25 * A Watchdog Timer (WDT) is a hardware circuit that can
26 * reset the computer system in case of a software fault.
27 * You probably knew that already.
29 * Usually a userspace daemon will notify the kernel WDT driver
30 * via the /dev/watchdog special device file that userspace is
31 * still alive, at regular intervals. When such a notification
32 * occurs, the driver will usually tell the hardware watchdog
33 * that everything is in order, and that the watchdog should wait
34 * for yet another little while to reset the system.
35 * If userspace fails (RAM error, kernel bug, whatever), the
36 * notifications cease to occur, and the hardware watchdog will
37 * reset the system (causing a reboot) after the timeout occurs.
39 * Create device with:
40 * mknod /dev/watchdog c 10 130
42 * For an example userspace keep-alive daemon, see:
43 * Documentation/watchdog/watchdog.txt
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/miscdevice.h>
50 #include <linux/watchdog.h>
51 #include <linux/delay.h>
52 #include <linux/fs.h>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/reboot.h>
56 #include <linux/init.h>
57 #include <linux/spinlock.h>
59 #include <asm/io.h>
60 #include <asm/uaccess.h>
61 #include <asm/system.h>
63 /* enable support for minutes as units? */
64 /* (does not always work correctly, so disabled by default!) */
65 #define SMSC_SUPPORT_MINUTES
66 #undef SMSC_SUPPORT_MINUTES
68 #define MAX_TIMEOUT 255
70 #define UNIT_SECOND 0
71 #define UNIT_MINUTE 1
73 #define MODNAME "smsc37b787_wdt: "
74 #define VERSION "1.1"
76 #define IOPORT 0x3F0
77 #define IOPORT_SIZE 2
78 #define IODEV_NO 8
80 static int unit = UNIT_SECOND; /* timer's unit */
81 static int timeout = 60; /* timeout value: default is 60 "units" */
82 static unsigned long timer_enabled = 0; /* is the timer enabled? */
84 static char expect_close; /* is the close expected? */
86 static DEFINE_SPINLOCK(io_lock);/* to guard the watchdog from io races */
88 static int nowayout = WATCHDOG_NOWAYOUT;
90 /* -- Low level function ----------------------------------------*/
92 /* unlock the IO chip */
94 static inline void open_io_config(void)
96 outb(0x55, IOPORT);
97 mdelay(1);
98 outb(0x55, IOPORT);
101 /* lock the IO chip */
102 static inline void close_io_config(void)
104 outb(0xAA, IOPORT);
107 /* select the IO device */
108 static inline void select_io_device(unsigned char devno)
110 outb(0x07, IOPORT);
111 outb(devno, IOPORT+1);
114 /* write to the control register */
115 static inline void write_io_cr(unsigned char reg, unsigned char data)
117 outb(reg, IOPORT);
118 outb(data, IOPORT+1);
121 /* read from the control register */
122 static inline char read_io_cr(unsigned char reg)
124 outb(reg, IOPORT);
125 return inb(IOPORT+1);
128 /* -- Medium level functions ------------------------------------*/
130 static inline void gpio_bit12(unsigned char reg)
132 // -- General Purpose I/O Bit 1.2 --
133 // Bit 0, In/Out: 0 = Output, 1 = Input
134 // Bit 1, Polarity: 0 = No Invert, 1 = Invert
135 // Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
136 // Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17,
137 // 11 = Either Edge Triggered Intr. 2
138 // Bit 5/6 (Reserved)
139 // Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
140 write_io_cr(0xE2, reg);
143 static inline void gpio_bit13(unsigned char reg)
145 // -- General Purpose I/O Bit 1.3 --
146 // Bit 0, In/Out: 0 = Output, 1 = Input
147 // Bit 1, Polarity: 0 = No Invert, 1 = Invert
148 // Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
149 // Bit 3, Function select: 0 = GPI/O, 1 = LED
150 // Bit 4-6 (Reserved)
151 // Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
152 write_io_cr(0xE3, reg);
155 static inline void wdt_timer_units(unsigned char new_units)
157 // -- Watchdog timer units --
158 // Bit 0-6 (Reserved)
159 // Bit 7, WDT Time-out Value Units Select
160 // (0 = Minutes, 1 = Seconds)
161 write_io_cr(0xF1, new_units);
164 static inline void wdt_timeout_value(unsigned char new_timeout)
166 // -- Watchdog Timer Time-out Value --
167 // Bit 0-7 Binary coded units (0=Disabled, 1..255)
168 write_io_cr(0xF2, new_timeout);
171 static inline void wdt_timer_conf(unsigned char conf)
173 // -- Watchdog timer configuration --
174 // Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon Gameport I/O
175 // Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
176 // Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr.
177 // Bit 3 Reset the timer
178 // (Wrong in SMsC documentation? Given as: PowerLED Timout Enabled)
179 // Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
180 // 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
181 write_io_cr(0xF3, conf);
184 static inline void wdt_timer_ctrl(unsigned char reg)
186 // -- Watchdog timer control --
187 // Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occured
188 // Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz
189 // Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
190 // Bit 3 P20 Force Timeout enabled:
191 // 0 = P20 activity does not generate the WD timeout event
192 // 1 = P20 Allows rising edge of P20, from the keyboard
193 // controller, to force the WD timeout event.
194 // Bit 4 (Reserved)
195 // -- Soft power management --
196 // Bit 5 Stop Counter: 1 = Stop software power down counter
197 // set via register 0xB8, (self-cleaning)
198 // (Upon read: 0 = Counter running, 1 = Counter stopped)
199 // Bit 6 Restart Counter: 1 = Restart software power down counter
200 // set via register 0xB8, (self-cleaning)
201 // Bit 7 SPOFF: 1 = Force software power down (self-cleaning)
203 write_io_cr(0xF4, reg);
206 /* -- Higher level functions ------------------------------------*/
208 /* initialize watchdog */
210 static void wb_smsc_wdt_initialize(void)
212 unsigned char old;
214 spin_lock(&io_lock);
215 open_io_config();
216 select_io_device(IODEV_NO);
218 // enable the watchdog
219 gpio_bit13(0x08); // Select pin 80 = LED not GPIO
220 gpio_bit12(0x0A); // Set pin 79 = WDT not GPIO/Output/Polarity=Invert
222 // disable the timeout
223 wdt_timeout_value(0);
225 // reset control register
226 wdt_timer_ctrl(0x00);
228 // reset configuration register
229 wdt_timer_conf(0x00);
231 // read old (timer units) register
232 old = read_io_cr(0xF1) & 0x7F;
233 if (unit == UNIT_SECOND) old |= 0x80; // set to seconds
235 // set the watchdog timer units
236 wdt_timer_units(old);
238 close_io_config();
239 spin_unlock(&io_lock);
242 /* shutdown the watchdog */
244 static void wb_smsc_wdt_shutdown(void)
246 spin_lock(&io_lock);
247 open_io_config();
248 select_io_device(IODEV_NO);
250 // disable the watchdog
251 gpio_bit13(0x09);
252 gpio_bit12(0x09);
254 // reset watchdog config register
255 wdt_timer_conf(0x00);
257 // reset watchdog control register
258 wdt_timer_ctrl(0x00);
260 // disable timeout
261 wdt_timeout_value(0x00);
263 close_io_config();
264 spin_unlock(&io_lock);
267 /* set timeout => enable watchdog */
269 static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
271 spin_lock(&io_lock);
272 open_io_config();
273 select_io_device(IODEV_NO);
275 // set Power LED to blink, if we enable the timeout
276 wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
278 // set timeout value
279 wdt_timeout_value(new_timeout);
281 close_io_config();
282 spin_unlock(&io_lock);
285 /* get timeout */
287 static unsigned char wb_smsc_wdt_get_timeout(void)
289 unsigned char set_timeout;
291 spin_lock(&io_lock);
292 open_io_config();
293 select_io_device(IODEV_NO);
294 set_timeout = read_io_cr(0xF2);
295 close_io_config();
296 spin_unlock(&io_lock);
298 return set_timeout;
301 /* disable watchdog */
303 static void wb_smsc_wdt_disable(void)
305 // set the timeout to 0 to disable the watchdog
306 wb_smsc_wdt_set_timeout(0);
309 /* enable watchdog by setting the current timeout */
311 static void wb_smsc_wdt_enable(void)
313 // set the current timeout...
314 wb_smsc_wdt_set_timeout(timeout);
317 /* reset the timer */
319 static void wb_smsc_wdt_reset_timer(void)
321 spin_lock(&io_lock);
322 open_io_config();
323 select_io_device(IODEV_NO);
325 // reset the timer
326 wdt_timeout_value(timeout);
327 wdt_timer_conf(0x08);
329 close_io_config();
330 spin_unlock(&io_lock);
333 /* return, if the watchdog is enabled (timeout is set...) */
335 static int wb_smsc_wdt_status(void)
337 return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
341 /* -- File operations -------------------------------------------*/
343 /* open => enable watchdog and set initial timeout */
345 static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
347 /* /dev/watchdog can only be opened once */
349 if (test_and_set_bit(0, &timer_enabled))
350 return -EBUSY;
352 if (nowayout)
353 __module_get(THIS_MODULE);
355 /* Reload and activate timer */
356 wb_smsc_wdt_enable();
358 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
360 return nonseekable_open(inode, file);
363 /* close => shut off the timer */
365 static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
367 /* Shut off the timer. */
369 if (expect_close == 42) {
370 wb_smsc_wdt_disable();
371 printk(KERN_INFO MODNAME "Watchdog disabled, sleeping again...\n");
372 } else {
373 printk(KERN_CRIT MODNAME "Unexpected close, not stopping watchdog!\n");
374 wb_smsc_wdt_reset_timer();
377 clear_bit(0, &timer_enabled);
378 expect_close = 0;
379 return 0;
382 /* write => update the timer to keep the machine alive */
384 static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
385 size_t len, loff_t *ppos)
387 /* See if we got the magic character 'V' and reload the timer */
388 if (len) {
389 if (!nowayout) {
390 size_t i;
392 /* reset expect flag */
393 expect_close = 0;
395 /* scan to see whether or not we got the magic character */
396 for (i = 0; i != len; i++) {
397 char c;
398 if (get_user(c, data+i))
399 return -EFAULT;
400 if (c == 'V')
401 expect_close = 42;
405 /* someone wrote to us, we should reload the timer */
406 wb_smsc_wdt_reset_timer();
408 return len;
411 /* ioctl => control interface */
413 static int wb_smsc_wdt_ioctl(struct inode *inode, struct file *file,
414 unsigned int cmd, unsigned long arg)
416 int new_timeout;
418 union {
419 struct watchdog_info __user *ident;
420 int __user *i;
421 } uarg;
423 static struct watchdog_info ident = {
424 .options = WDIOF_KEEPALIVEPING |
425 WDIOF_SETTIMEOUT |
426 WDIOF_MAGICCLOSE,
427 .firmware_version = 0,
428 .identity = "SMsC 37B787 Watchdog"
431 uarg.i = (int __user *)arg;
433 switch (cmd) {
434 default:
435 return -ENOTTY;
437 case WDIOC_GETSUPPORT:
438 return copy_to_user(uarg.ident, &ident,
439 sizeof(ident)) ? -EFAULT : 0;
441 case WDIOC_GETSTATUS:
442 return put_user(wb_smsc_wdt_status(), uarg.i);
444 case WDIOC_GETBOOTSTATUS:
445 return put_user(0, uarg.i);
447 case WDIOC_KEEPALIVE:
448 wb_smsc_wdt_reset_timer();
449 return 0;
451 case WDIOC_SETTIMEOUT:
452 if (get_user(new_timeout, uarg.i))
453 return -EFAULT;
455 // the API states this is given in secs
456 if (unit == UNIT_MINUTE)
457 new_timeout /= 60;
459 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
460 return -EINVAL;
462 timeout = new_timeout;
463 wb_smsc_wdt_set_timeout(timeout);
465 // fall through and return the new timeout...
467 case WDIOC_GETTIMEOUT:
469 new_timeout = timeout;
471 if (unit == UNIT_MINUTE)
472 new_timeout *= 60;
474 return put_user(new_timeout, uarg.i);
476 case WDIOC_SETOPTIONS:
478 int options, retval = -EINVAL;
480 if (get_user(options, uarg.i))
481 return -EFAULT;
483 if (options & WDIOS_DISABLECARD) {
484 wb_smsc_wdt_disable();
485 retval = 0;
488 if (options & WDIOS_ENABLECARD) {
489 wb_smsc_wdt_enable();
490 retval = 0;
493 return retval;
498 /* -- Notifier funtions -----------------------------------------*/
500 static int wb_smsc_wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
502 if (code == SYS_DOWN || code == SYS_HALT)
504 // set timeout to 0, to avoid possible race-condition
505 timeout = 0;
506 wb_smsc_wdt_disable();
508 return NOTIFY_DONE;
511 /* -- Module's structures ---------------------------------------*/
513 static const struct file_operations wb_smsc_wdt_fops =
515 .owner = THIS_MODULE,
516 .llseek = no_llseek,
517 .write = wb_smsc_wdt_write,
518 .ioctl = wb_smsc_wdt_ioctl,
519 .open = wb_smsc_wdt_open,
520 .release = wb_smsc_wdt_release,
523 static struct notifier_block wb_smsc_wdt_notifier =
525 .notifier_call = wb_smsc_wdt_notify_sys,
528 static struct miscdevice wb_smsc_wdt_miscdev =
530 .minor = WATCHDOG_MINOR,
531 .name = "watchdog",
532 .fops = &wb_smsc_wdt_fops,
535 /* -- Module init functions -------------------------------------*/
537 /* module's "constructor" */
539 static int __init wb_smsc_wdt_init(void)
541 int ret;
543 printk("SMsC 37B787 watchdog component driver " VERSION " initialising...\n");
545 if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
546 printk(KERN_ERR MODNAME "Unable to register IO port %#x\n", IOPORT);
547 ret = -EBUSY;
548 goto out_pnp;
551 // set new maximum, if it's too big
552 if (timeout > MAX_TIMEOUT)
553 timeout = MAX_TIMEOUT;
555 // init the watchdog timer
556 wb_smsc_wdt_initialize();
558 ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
559 if (ret) {
560 printk(KERN_ERR MODNAME "Unable to register reboot notifier err = %d\n", ret);
561 goto out_io;
564 ret = misc_register(&wb_smsc_wdt_miscdev);
565 if (ret) {
566 printk(KERN_ERR MODNAME "Unable to register miscdev on minor %d\n", WATCHDOG_MINOR);
567 goto out_rbt;
570 // output info
571 printk(KERN_INFO MODNAME "Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
572 printk(KERN_INFO MODNAME "Watchdog initialized and sleeping (nowayout=%d)...\n", nowayout);
574 // ret = 0
576 out_clean:
577 return ret;
579 out_rbt:
580 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
582 out_io:
583 release_region(IOPORT, IOPORT_SIZE);
585 out_pnp:
586 goto out_clean;
589 /* module's "destructor" */
591 static void __exit wb_smsc_wdt_exit(void)
593 /* Stop the timer before we leave */
594 if (!nowayout)
596 wb_smsc_wdt_shutdown();
597 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
600 misc_deregister(&wb_smsc_wdt_miscdev);
601 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
602 release_region(IOPORT, IOPORT_SIZE);
604 printk("SMsC 37B787 watchdog component driver removed.\n");
607 module_init(wb_smsc_wdt_init);
608 module_exit(wb_smsc_wdt_exit);
610 MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
611 MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version " VERSION ")");
612 MODULE_LICENSE("GPL");
614 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
616 #ifdef SMSC_SUPPORT_MINUTES
617 module_param(unit, int, 0);
618 MODULE_PARM_DESC(unit, "set unit to use, 0=seconds or 1=minutes, default is 0");
619 #endif
621 module_param(timeout, int, 0);
622 MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
624 module_param(nowayout, int, 0);
625 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");