Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[pandora-kernel.git] / drivers / char / watchdog / wdt977.c
1 /*
2  *      Wdt977  0.04:   A Watchdog Device for Netwinder W83977AF chip
3  *
4  *      (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
5  *
6  *                      -----------------------
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  *
13  *                      -----------------------
14  *      14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
15  *           Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
16  *      19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
17  *      06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
18  *                                  from minutes to seconds.
19  *      07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
20  *                                    nwwatchdog_init.
21  *      25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
22  *                                  remove limitiation to be used on Netwinders only
23  */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/config.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/fs.h>
31 #include <linux/miscdevice.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/watchdog.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
37
38 #include <asm/io.h>
39 #include <asm/system.h>
40 #include <asm/mach-types.h>
41 #include <asm/uaccess.h>
42
43 #define WATCHDOG_VERSION  "0.04"
44 #define WATCHDOG_NAME     "Wdt977"
45 #define PFX WATCHDOG_NAME ": "
46 #define DRIVER_VERSION    WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
47
48 #define IO_INDEX_PORT   0x370           /* on some systems it can be 0x3F0 */
49 #define IO_DATA_PORT    (IO_INDEX_PORT+1)
50
51 #define UNLOCK_DATA     0x87
52 #define LOCK_DATA       0xAA
53 #define DEVICE_REGISTER 0x07
54
55
56 #define DEFAULT_TIMEOUT 60                      /* default timeout in seconds */
57
58 static  int timeout = DEFAULT_TIMEOUT;
59 static  int timeoutM;                           /* timeout in minutes */
60 static  unsigned long timer_alive;
61 static  int testmode;
62 static  char expect_close;
63 static  spinlock_t spinlock;
64
65 module_param(timeout, int, 0);
66 MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
67 module_param(testmode, int, 0);
68 MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
69
70 static int nowayout = WATCHDOG_NOWAYOUT;
71 module_param(nowayout, int, 0);
72 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
73
74 /*
75  * Start the watchdog
76  */
77
78 static int wdt977_start(void)
79 {
80         unsigned long flags;
81
82         spin_lock_irqsave(&spinlock, flags);
83
84         /* unlock the SuperIO chip */
85         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
86         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
87
88         /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
89          * F2 has the timeout in minutes
90          * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
91          *   at timeout, and to reset timer on kbd/mouse activity (not impl.)
92          * F4 is used to just clear the TIMEOUT'ed state (bit 0)
93          */
94         outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
95         outb_p(0x08, IO_DATA_PORT);
96         outb_p(0xF2, IO_INDEX_PORT);
97         outb_p(timeoutM, IO_DATA_PORT);
98         outb_p(0xF3, IO_INDEX_PORT);
99         outb_p(0x00, IO_DATA_PORT);     /* another setting is 0E for kbd/mouse/LED */
100         outb_p(0xF4, IO_INDEX_PORT);
101         outb_p(0x00, IO_DATA_PORT);
102
103         /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
104         /* in test mode watch the bit 1 on F4 to indicate "triggered" */
105         if (!testmode)
106         {
107                 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
108                 outb_p(0x07, IO_DATA_PORT);
109                 outb_p(0xE6, IO_INDEX_PORT);
110                 outb_p(0x08, IO_DATA_PORT);
111         }
112
113         /* lock the SuperIO chip */
114         outb_p(LOCK_DATA, IO_INDEX_PORT);
115
116         spin_unlock_irqrestore(&spinlock, flags);
117         printk(KERN_INFO PFX "activated.\n");
118
119         return 0;
120 }
121
122 /*
123  * Stop the watchdog
124  */
125
126 static int wdt977_stop(void)
127 {
128         unsigned long flags;
129         spin_lock_irqsave(&spinlock, flags);
130
131         /* unlock the SuperIO chip */
132         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
133         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
134
135         /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
136         * F3 is reset to its default state
137         * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
138         * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
139         */
140         outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
141         outb_p(0x08, IO_DATA_PORT);
142         outb_p(0xF2, IO_INDEX_PORT);
143         outb_p(0xFF, IO_DATA_PORT);
144         outb_p(0xF3, IO_INDEX_PORT);
145         outb_p(0x00, IO_DATA_PORT);
146         outb_p(0xF4, IO_INDEX_PORT);
147         outb_p(0x00, IO_DATA_PORT);
148         outb_p(0xF2, IO_INDEX_PORT);
149         outb_p(0x00, IO_DATA_PORT);
150
151         /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
152         outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
153         outb_p(0x07, IO_DATA_PORT);
154         outb_p(0xE6, IO_INDEX_PORT);
155         outb_p(0x08, IO_DATA_PORT);
156
157         /* lock the SuperIO chip */
158         outb_p(LOCK_DATA, IO_INDEX_PORT);
159
160         spin_unlock_irqrestore(&spinlock, flags);
161         printk(KERN_INFO PFX "shutdown.\n");
162
163         return 0;
164 }
165
166 /*
167  * Send a keepalive ping to the watchdog
168  * This is done by simply re-writing the timeout to reg. 0xF2
169  */
170
171 static int wdt977_keepalive(void)
172 {
173         unsigned long flags;
174         spin_lock_irqsave(&spinlock, flags);
175
176         /* unlock the SuperIO chip */
177         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
178         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
179
180         /* select device Aux2 (device=8) and kicks watchdog reg F2 */
181         /* F2 has the timeout in minutes */
182         outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
183         outb_p(0x08, IO_DATA_PORT);
184         outb_p(0xF2, IO_INDEX_PORT);
185         outb_p(timeoutM, IO_DATA_PORT);
186
187         /* lock the SuperIO chip */
188         outb_p(LOCK_DATA, IO_INDEX_PORT);
189         spin_unlock_irqrestore(&spinlock, flags);
190
191         return 0;
192 }
193
194 /*
195  * Set the watchdog timeout value
196  */
197
198 static int wdt977_set_timeout(int t)
199 {
200         int tmrval;
201
202         /* convert seconds to minutes, rounding up */
203         tmrval = (t + 59) / 60;
204
205         if (machine_is_netwinder()) {
206                 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
207                  *  this limits the max timeout to half of device max of 255 minutes...
208                  */
209                 tmrval += tmrval;
210         }
211
212         if ((tmrval < 1) || (tmrval > 255))
213                 return -EINVAL;
214
215         /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
216         timeout = t;
217         timeoutM = tmrval;
218         return 0;
219 }
220
221 /*
222  * Get the watchdog status
223  */
224
225 static int wdt977_get_status(int *status)
226 {
227         int new_status;
228         unsigned long flags;
229
230         spin_lock_irqsave(&spinlock, flags);
231
232         /* unlock the SuperIO chip */
233         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
234         outb_p(UNLOCK_DATA, IO_INDEX_PORT);
235
236         /* select device Aux2 (device=8) and read watchdog reg F4 */
237         outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
238         outb_p(0x08, IO_DATA_PORT);
239         outb_p(0xF4, IO_INDEX_PORT);
240         new_status = inb_p(IO_DATA_PORT);
241
242         /* lock the SuperIO chip */
243         outb_p(LOCK_DATA, IO_INDEX_PORT);
244
245         spin_unlock_irqrestore(&spinlock, flags);
246
247         *status=0;
248         if (new_status & 1)
249                 *status |= WDIOF_CARDRESET;
250
251         return 0;
252 }
253
254
255 /*
256  *      /dev/watchdog handling
257  */
258
259 static int wdt977_open(struct inode *inode, struct file *file)
260 {
261         /* If the watchdog is alive we don't need to start it again */
262         if( test_and_set_bit(0,&timer_alive) )
263                 return -EBUSY;
264
265         if (nowayout)
266                 __module_get(THIS_MODULE);
267
268         wdt977_start();
269         return nonseekable_open(inode, file);
270 }
271
272 static int wdt977_release(struct inode *inode, struct file *file)
273 {
274         /*
275          *      Shut off the timer.
276          *      Lock it in if it's a module and we set nowayout
277          */
278         if (expect_close == 42)
279         {
280                 wdt977_stop();
281                 clear_bit(0,&timer_alive);
282         } else {
283                 wdt977_keepalive();
284                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
285         }
286         expect_close = 0;
287         return 0;
288 }
289
290
291 /*
292  *      wdt977_write:
293  *      @file: file handle to the watchdog
294  *      @buf: buffer to write (unused as data does not matter here
295  *      @count: count of bytes
296  *      @ppos: pointer to the position to write. No seeks allowed
297  *
298  *      A write to a watchdog device is defined as a keepalive signal. Any
299  *      write of data will do, as we we don't define content meaning.
300  */
301
302 static ssize_t wdt977_write(struct file *file, const char __user *buf,
303                             size_t count, loff_t *ppos)
304 {
305         if (count)
306         {
307                 if (!nowayout)
308                 {
309                         size_t i;
310
311                         /* In case it was set long ago */
312                         expect_close = 0;
313
314                         for (i = 0; i != count; i++)
315                         {
316                                 char c;
317                                 if (get_user(c, buf + i))
318                                         return -EFAULT;
319                                 if (c == 'V')
320                                         expect_close = 42;
321                         }
322                 }
323
324                 /* someone wrote to us, we should restart timer */
325                 wdt977_keepalive();
326         }
327         return count;
328 }
329
330 /*
331  *      wdt977_ioctl:
332  *      @inode: inode of the device
333  *      @file: file handle to the device
334  *      @cmd: watchdog command
335  *      @arg: argument pointer
336  *
337  *      The watchdog API defines a common set of functions for all watchdogs
338  *      according to their available features.
339  */
340
341 static struct watchdog_info ident = {
342         .options =              WDIOF_SETTIMEOUT |
343                                 WDIOF_MAGICCLOSE |
344                                 WDIOF_KEEPALIVEPING,
345         .firmware_version =     1,
346         .identity =             WATCHDOG_NAME,
347 };
348
349 static int wdt977_ioctl(struct inode *inode, struct file *file,
350         unsigned int cmd, unsigned long arg)
351 {
352         int status;
353         int new_options, retval = -EINVAL;
354         int new_timeout;
355         union {
356                 struct watchdog_info __user *ident;
357                 int __user *i;
358         } uarg;
359
360         uarg.i = (int __user *)arg;
361
362         switch(cmd)
363         {
364         default:
365                 return -ENOIOCTLCMD;
366
367         case WDIOC_GETSUPPORT:
368                 return copy_to_user(uarg.ident, &ident,
369                         sizeof(ident)) ? -EFAULT : 0;
370
371         case WDIOC_GETSTATUS:
372                 wdt977_get_status(&status);
373                 return put_user(status, uarg.i);
374
375         case WDIOC_GETBOOTSTATUS:
376                 return put_user(0, uarg.i);
377
378         case WDIOC_KEEPALIVE:
379                 wdt977_keepalive();
380                 return 0;
381
382         case WDIOC_SETOPTIONS:
383                 if (get_user (new_options, uarg.i))
384                         return -EFAULT;
385
386                 if (new_options & WDIOS_DISABLECARD) {
387                         wdt977_stop();
388                         retval = 0;
389                 }
390
391                 if (new_options & WDIOS_ENABLECARD) {
392                         wdt977_start();
393                         retval = 0;
394                 }
395
396                 return retval;
397
398         case WDIOC_SETTIMEOUT:
399                 if (get_user(new_timeout, uarg.i))
400                         return -EFAULT;
401
402                 if (wdt977_set_timeout(new_timeout))
403                     return -EINVAL;
404
405                 wdt977_keepalive();
406                 /* Fall */
407
408         case WDIOC_GETTIMEOUT:
409                 return put_user(timeout, uarg.i);
410
411         }
412 }
413
414 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
415         void *unused)
416 {
417         if(code==SYS_DOWN || code==SYS_HALT)
418                 wdt977_stop();
419         return NOTIFY_DONE;
420 }
421
422 static struct file_operations wdt977_fops=
423 {
424         .owner          = THIS_MODULE,
425         .llseek         = no_llseek,
426         .write          = wdt977_write,
427         .ioctl          = wdt977_ioctl,
428         .open           = wdt977_open,
429         .release        = wdt977_release,
430 };
431
432 static struct miscdevice wdt977_miscdev=
433 {
434         .minor          = WATCHDOG_MINOR,
435         .name           = "watchdog",
436         .fops           = &wdt977_fops,
437 };
438
439 static struct notifier_block wdt977_notifier = {
440         .notifier_call = wdt977_notify_sys,
441 };
442
443 static int __init wd977_init(void)
444 {
445         int rc;
446
447         //if (!machine_is_netwinder())
448         //      return -ENODEV;
449
450         printk(KERN_INFO PFX DRIVER_VERSION);
451
452         spin_lock_init(&spinlock);
453
454         /* Check that the timeout value is within it's range ; if not reset to the default */
455         if (wdt977_set_timeout(timeout))
456         {
457                 wdt977_set_timeout(DEFAULT_TIMEOUT);
458                 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
459                         DEFAULT_TIMEOUT);
460         }
461
462         /* on Netwinder the IOports are already reserved by
463          * arch/arm/mach-footbridge/netwinder-hw.c
464          */
465         if (!machine_is_netwinder())
466         {
467                 if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME))
468                 {
469                         printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
470                                 IO_INDEX_PORT);
471                         rc = -EIO;
472                         goto err_out;
473                 }
474         }
475
476         rc = misc_register(&wdt977_miscdev);
477         if (rc)
478         {
479                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
480                         wdt977_miscdev.minor, rc);
481                 goto err_out_region;
482         }
483
484         rc = register_reboot_notifier(&wdt977_notifier);
485         if (rc)
486         {
487                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
488                         rc);
489                 goto err_out_miscdev;
490         }
491
492         printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
493                 timeout, nowayout, testmode);
494
495         return 0;
496
497 err_out_miscdev:
498         misc_deregister(&wdt977_miscdev);
499 err_out_region:
500         if (!machine_is_netwinder())
501                 release_region(IO_INDEX_PORT,2);
502 err_out:
503         return rc;
504 }
505
506 static void __exit wd977_exit(void)
507 {
508         wdt977_stop();
509         misc_deregister(&wdt977_miscdev);
510         unregister_reboot_notifier(&wdt977_notifier);
511         release_region(IO_INDEX_PORT,2);
512 }
513
514 module_init(wd977_init);
515 module_exit(wd977_exit);
516
517 MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
518 MODULE_DESCRIPTION("W83977AF Watchdog driver");
519 MODULE_LICENSE("GPL");
520 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);