4d5612146cded76629c7aa9132ee39c9387627b4
[pandora-kernel.git] / drivers / char / watchdog / pc87413_wdt.c
1 /*
2  *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
3  *
4  *      This code is based on wdt.c with original copyright.
5  *
6  *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7  *                     and Marcus Junker, <junker@anduras.de>
8  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  *
14  *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
15  *      admit liability nor provide warranty for any of this software.
16  *      This material is provided "AS-IS" and at no charge.
17  *
18  *      Release 1.1
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/notifier.h>
29 #include <linux/fs.h>
30 #include <linux/reboot.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/moduleparam.h>
34 #include <linux/version.h>
35
36 #include <asm/io.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39
40 /* #define DEBUG 1 */
41
42 #define DEFAULT_TIMEOUT     1            /* 1 minute */
43 #define MAX_TIMEOUT         255
44
45 #define VERSION             "1.1"
46 #define MODNAME             "pc87413 WDT"
47 #define PFX                 MODNAME ": "
48 #define DPFX                MODNAME " - DEBUG: "
49
50 #define WDT_INDEX_IO_PORT   (io+0)       /* I/O port base (index register) */
51 #define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
52 #define SWC_LDN             0x04
53 #define SIOCFG2             0x22         /* Serial IO register */
54 #define WDCTL               0x10         /* Watchdog-Timer-Controll-Register */
55 #define WDTO                0x11         /* Watchdog timeout register */
56 #define WDCFG               0x12         /* Watchdog config register */
57
58 static int io = 0x2E;                    /* Address used on Portwell Boards */
59
60 static int timeout = DEFAULT_TIMEOUT;    /* timeout value */
61 static unsigned long timer_enabled = 0;  /* is the timer enabled? */
62
63 static char expect_close;                /* is the close expected? */
64
65 static spinlock_t io_lock;               /* to guard the watchdog from io races */
66
67 static int nowayout = WATCHDOG_NOWAYOUT;
68
69 /* -- Low level function ----------------------------------------*/
70
71 /* Select pins for Watchdog output */
72
73 static inline void pc87413_select_wdt_out (void)
74 {
75         unsigned int cr_data = 0;
76
77         /* Step 1: Select multiple pin,pin55,as WDT output */
78
79         outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
80
81         cr_data = inb (WDT_DATA_IO_PORT);
82
83         cr_data |= 0x80; /* Set Bit7 to 1*/
84         outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
85
86         outb_p(cr_data, WDT_DATA_IO_PORT);
87
88 #ifdef DEBUG
89         printk(KERN_INFO DPFX "Select multiple pin,pin55,as WDT output:"
90                               " Bit7 to 1: %d\n", cr_data);
91 #endif
92 }
93
94 /* Enable SWC functions */
95
96 static inline void pc87413_enable_swc(void)
97 {
98         unsigned int cr_data=0;
99
100         /* Step 2: Enable SWC functions */
101
102         outb_p(0x07, WDT_INDEX_IO_PORT);        /* Point SWC_LDN (LDN=4) */
103         outb_p(SWC_LDN, WDT_DATA_IO_PORT);
104
105         outb_p(0x30, WDT_INDEX_IO_PORT);        /* Read Index 0x30 First */
106         cr_data = inb(WDT_DATA_IO_PORT);
107         cr_data |= 0x01;                        /* Set Bit0 to 1 */
108         outb_p(0x30, WDT_INDEX_IO_PORT);
109         outb_p(cr_data, WDT_DATA_IO_PORT);      /* Index0x30_bit0P1 */
110
111 #ifdef DEBUG
112         printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
113 #endif
114 }
115
116 /* Read SWC I/O base address */
117
118 static inline unsigned int pc87413_get_swc_base(void)
119 {
120         unsigned int  swc_base_addr = 0;
121         unsigned char addr_l, addr_h = 0;
122
123         /* Step 3: Read SWC I/O Base Address */
124
125         outb_p(0x60, WDT_INDEX_IO_PORT);        /* Read Index 0x60 */
126         addr_h = inb(WDT_DATA_IO_PORT);
127
128         outb_p(0x61, WDT_INDEX_IO_PORT);        /* Read Index 0x61 */
129
130         addr_l = inb(WDT_DATA_IO_PORT);
131
132         swc_base_addr = (addr_h << 8) + addr_l;
133
134 #ifdef DEBUG
135         printk(KERN_INFO DPFX "Read SWC I/O Base Address: low %d, high %d,"
136                               " res %d\n", addr_l, addr_h, swc_base_addr);
137 #endif
138
139         return swc_base_addr;
140 }
141
142 /* Select Bank 3 of SWC */
143
144 static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
145 {
146         /* Step 4: Select Bank3 of SWC */
147
148         outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
149
150 #ifdef DEBUG
151         printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
152 #endif
153 }
154
155 /* Set watchdog timeout to x minutes */
156
157 static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
158                                          char pc87413_time)
159 {
160         /* Step 5: Programm WDTO, Twd. */
161
162         outb_p(pc87413_time, swc_base_addr + WDTO);
163
164 #ifdef DEBUG
165         printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
166 #endif
167 }
168
169 /* Enable WDEN */
170
171 static inline void pc87413_enable_wden(unsigned int swc_base_addr)
172 {
173         /* Step 6: Enable WDEN */
174
175         outb_p(inb (swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
176
177 #ifdef DEBUG
178         printk(KERN_INFO DPFX "Enable WDEN\n");
179 #endif
180 }
181
182 /* Enable SW_WD_TREN */
183 static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
184 {
185         /* Enable SW_WD_TREN */
186
187         outb_p(inb (swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
188
189 #ifdef DEBUG
190         printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
191 #endif
192 }
193
194 /* Disable SW_WD_TREN */
195
196 static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
197 {
198         /* Disable SW_WD_TREN */
199
200         outb_p(inb (swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
201
202 #ifdef DEBUG
203         printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
204 #endif
205 }
206
207 /* Enable SW_WD_TRG */
208
209 static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
210 {
211         /* Enable SW_WD_TRG */
212
213         outb_p(inb (swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
214
215 #ifdef DEBUG
216         printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
217 #endif
218 }
219
220 /* Disable SW_WD_TRG */
221
222 static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
223 {
224         /* Disable SW_WD_TRG */
225
226         outb_p(inb (swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
227
228 #ifdef DEBUG
229         printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
230 #endif
231 }
232
233 /* -- Higher level functions ------------------------------------*/
234
235 /* Enable the watchdog */
236
237 static void pc87413_enable(void)
238 {
239         unsigned int swc_base_addr;
240
241         spin_lock(&io_lock);
242
243         pc87413_select_wdt_out();
244         pc87413_enable_swc();
245         swc_base_addr = pc87413_get_swc_base();
246         pc87413_swc_bank3(swc_base_addr);
247         pc87413_programm_wdto(swc_base_addr, timeout);
248         pc87413_enable_wden(swc_base_addr);
249         pc87413_enable_sw_wd_tren(swc_base_addr);
250         pc87413_enable_sw_wd_trg(swc_base_addr);
251
252         spin_unlock(&io_lock);
253 }
254
255 /* Disable the watchdog */
256
257 static void pc87413_disable(void)
258 {
259         unsigned int swc_base_addr;
260
261         spin_lock(&io_lock);
262
263         pc87413_select_wdt_out();
264         pc87413_enable_swc();
265         swc_base_addr = pc87413_get_swc_base();
266         pc87413_swc_bank3(swc_base_addr);
267         pc87413_disable_sw_wd_tren(swc_base_addr);
268         pc87413_disable_sw_wd_trg(swc_base_addr);
269         pc87413_programm_wdto(swc_base_addr, 0);
270
271         spin_unlock(&io_lock);
272 }
273
274 /* Refresh the watchdog */
275
276 static void pc87413_refresh(void)
277 {
278         unsigned int swc_base_addr;
279
280         spin_lock(&io_lock);
281
282         pc87413_select_wdt_out();
283         pc87413_enable_swc();
284         swc_base_addr = pc87413_get_swc_base();
285         pc87413_swc_bank3(swc_base_addr);
286         pc87413_disable_sw_wd_tren(swc_base_addr);
287         pc87413_disable_sw_wd_trg(swc_base_addr);
288         pc87413_programm_wdto(swc_base_addr, timeout);
289         pc87413_enable_wden(swc_base_addr);
290         pc87413_enable_sw_wd_tren(swc_base_addr);
291         pc87413_enable_sw_wd_trg(swc_base_addr);
292
293         spin_unlock(&io_lock);
294 }
295
296 /* -- File operations -------------------------------------------*/
297
298 /**
299  *      pc87413_open:
300  *      @inode: inode of device
301  *      @file: file handle to device
302  *
303  */
304
305 static int pc87413_open(struct inode *inode, struct file *file)
306 {
307         /* /dev/watchdog can only be opened once */
308
309         if (test_and_set_bit(0, &timer_enabled))
310                 return -EBUSY;
311
312         if (nowayout)
313                 __module_get(THIS_MODULE);
314
315         /* Reload and activate timer */
316         pc87413_refresh();
317
318         printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to"
319                                  " %d minute(s).\n", timeout);
320
321         return nonseekable_open(inode, file);
322 }
323
324 /**
325  *      pc87413_release:
326  *      @inode: inode to board
327  *      @file: file handle to board
328  *
329  *      The watchdog has a configurable API. There is a religious dispute
330  *      between people who want their watchdog to be able to shut down and
331  *      those who want to be sure if the watchdog manager dies the machine
332  *      reboots. In the former case we disable the counters, in the latter
333  *      case you have to open it again very soon.
334  */
335
336 static int pc87413_release(struct inode *inode, struct file *file)
337 {
338         /* Shut off the timer. */
339
340         if (expect_close == 42) {
341                 pc87413_disable();
342                 printk(KERN_INFO MODNAME "Watchdog disabled,"
343                                          " sleeping again...\n");
344         } else {
345                 printk(KERN_CRIT MODNAME "Unexpected close, not stopping"
346                                          " watchdog!\n");
347                 pc87413_refresh();
348         }
349
350         clear_bit(0, &timer_enabled);
351         expect_close = 0;
352
353         return 0;
354 }
355
356 /**
357  *      pc87413_status:
358  *
359  *      return, if the watchdog is enabled (timeout is set...)
360  */
361
362
363 static int pc87413_status(void)
364 {
365           return 1; /* currently not supported */
366 }
367
368 /**
369  *      pc87413_write:
370  *      @file: file handle to the watchdog
371  *      @data: data buffer to write
372  *      @len: length in bytes
373  *      @ppos: pointer to the position to write. No seeks allowed
374  *
375  *      A write to a watchdog device is defined as a keepalive signal. Any
376  *      write of data will do, as we we don't define content meaning.
377  */
378
379 static ssize_t pc87413_write(struct file *file, const char __user *data,
380                              size_t len, loff_t *ppos)
381 {
382         /* See if we got the magic character 'V' and reload the timer */
383         if (len) {
384                 if (!nowayout) {
385                         size_t i;
386
387                         /* reset expect flag */
388                         expect_close = 0;
389
390                         /* scan to see whether or not we got the magic character */
391                         for (i = 0; i != len; i++) {
392                                 char c;
393                                 if (get_user(c, data+i))
394                                         return -EFAULT;
395                                 if (c == 'V')
396                                         expect_close = 42;
397                         }
398                 }
399
400                 /* someone wrote to us, we should reload the timer */
401                 pc87413_refresh();
402         }
403         return len;
404 }
405
406 /**
407  *      pc87413_ioctl:
408  *      @inode: inode of the device
409  *      @file: file handle to the device
410  *      @cmd: watchdog command
411  *      @arg: argument pointer
412  *
413  *      The watchdog API defines a common set of functions for all watchdogs
414  *      according to their available features. We only actually usefully support
415  *      querying capabilities and current status.
416  */
417
418 static int pc87413_ioctl(struct inode *inode, struct file *file,
419                          unsigned int cmd, unsigned long arg)
420 {
421         int new_timeout;
422
423         union {
424                 struct watchdog_info __user *ident;
425                 int __user *i;
426         } uarg;
427
428         static struct watchdog_info ident = {
429                 .options          = WDIOF_KEEPALIVEPING |
430                                     WDIOF_SETTIMEOUT |
431                                     WDIOF_MAGICCLOSE,
432                 .firmware_version = 1,
433                 .identity         = "PC87413(HF/F) watchdog"
434         };
435
436         uarg.i = (int __user *)arg;
437
438         switch(cmd) {
439                 default:
440                         return -ENOTTY;
441
442                 case WDIOC_GETSUPPORT:
443                         return copy_to_user(uarg.ident, &ident,
444                                 sizeof(ident)) ? -EFAULT : 0;
445
446                 case WDIOC_GETSTATUS:
447                         return put_user(pc87413_status(), uarg.i);
448
449                 case WDIOC_GETBOOTSTATUS:
450                         return put_user(0, uarg.i);
451
452                 case WDIOC_KEEPALIVE:
453                         pc87413_refresh();
454 #ifdef DEBUG
455                         printk(KERN_INFO DPFX "keepalive\n");
456 #endif
457                         return 0;
458
459                 case WDIOC_SETTIMEOUT:
460                         if (get_user(new_timeout, uarg.i))
461                                 return -EFAULT;
462
463                         // the API states this is given in secs
464                         new_timeout /= 60;
465
466                         if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
467                                 return -EINVAL;
468
469                         timeout = new_timeout;
470                         pc87413_refresh();
471
472                         // fall through and return the new timeout...
473
474                 case WDIOC_GETTIMEOUT:
475
476                         new_timeout = timeout * 60;
477
478                         return put_user(new_timeout, uarg.i);
479
480                 case WDIOC_SETOPTIONS:
481                 {
482                         int options, retval = -EINVAL;
483
484                         if (get_user(options, uarg.i))
485                                 return -EFAULT;
486
487                         if (options & WDIOS_DISABLECARD) {
488                                 pc87413_disable();
489                                 retval = 0;
490                         }
491
492                         if (options & WDIOS_ENABLECARD) {
493                                 pc87413_enable();
494                                 retval = 0;
495                         }
496
497                         return retval;
498                 }
499         }
500 }
501
502 /* -- Notifier funtions -----------------------------------------*/
503
504 /**
505  *      notify_sys:
506  *      @this: our notifier block
507  *      @code: the event being reported
508  *      @unused: unused
509  *
510  *      Our notifier is called on system shutdowns. We want to turn the card
511  *      off at reboot otherwise the machine will reboot again during memory
512  *      test or worse yet during the following fsck. This would suck, in fact
513  *      trust me - if it happens it does suck.
514  */
515
516 static int pc87413_notify_sys(struct notifier_block *this,
517                               unsigned long code,
518                               void *unused)
519 {
520         if (code == SYS_DOWN || code == SYS_HALT)
521         {
522                 /* Turn the card off */
523                 pc87413_disable();
524         }
525         return NOTIFY_DONE;
526 }
527
528 /* -- Module's structures ---------------------------------------*/
529
530 static struct file_operations pc87413_fops = {
531         .owner          = THIS_MODULE,
532         .llseek         = no_llseek,
533         .write          = pc87413_write,
534         .ioctl          = pc87413_ioctl,
535         .open           = pc87413_open,
536         .release        = pc87413_release,
537 };
538
539 static struct notifier_block pc87413_notifier =
540 {
541         .notifier_call  = pc87413_notify_sys,
542 };
543
544 static struct miscdevice pc87413_miscdev=
545 {
546         .minor          = WATCHDOG_MINOR,
547         .name           = "watchdog",
548         .fops           = &pc87413_fops
549 };
550
551 /* -- Module init functions -------------------------------------*/
552
553 /**
554  *      pc87413_init: module's "constructor"
555  *
556  *      Set up the WDT watchdog board. All we have to do is grab the
557  *      resources we require and bitch if anyone beat us to them.
558  *      The open() function will actually kick the board off.
559  */
560
561 static int __init pc87413_init(void)
562 {
563         int ret;
564
565         spin_lock_init(&io_lock);
566
567         printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n", WDT_INDEX_IO_PORT);
568
569         /* request_region(io, 2, "pc87413"); */
570
571         ret = register_reboot_notifier(&pc87413_notifier);
572         if (ret != 0) {
573                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
574                         ret);
575         }
576
577         ret = misc_register(&pc87413_miscdev);
578
579         if (ret != 0) {
580                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
581                         WATCHDOG_MINOR, ret);
582                 unregister_reboot_notifier(&pc87413_notifier);
583                 return ret;
584         }
585
586         printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
587
588         pc87413_enable();
589
590         return 0;
591 }
592
593 /**
594  *      pc87413_exit: module's "destructor"
595  *
596  *      Unload the watchdog. You cannot do this with any file handles open.
597  *      If your watchdog is set to continue ticking on close and you unload
598  *      it, well it keeps ticking. We won't get the interrupt but the board
599  *      will not touch PC memory so all is fine. You just have to load a new
600  *      module in 60 seconds or reboot.
601  */
602
603 static void __exit pc87413_exit(void)
604 {
605         /* Stop the timer before we leave */
606         if (!nowayout)
607         {
608                 pc87413_disable();
609                 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
610         }
611
612         misc_deregister(&pc87413_miscdev);
613         unregister_reboot_notifier(&pc87413_notifier);
614         /* release_region(io,2); */
615
616         printk(MODNAME " watchdog component driver removed.\n");
617 }
618
619 module_init(pc87413_init);
620 module_exit(pc87413_exit);
621
622 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
623 MODULE_DESCRIPTION("PC87413 WDT driver");
624 MODULE_LICENSE("GPL");
625
626 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
627
628 module_param(io, int, 0);
629 MODULE_PARM_DESC(wdt_io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
630
631 module_param(timeout, int, 0);
632 MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout) ").");
633
634 module_param(nowayout, int, 0);
635 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
636