commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / char / watchdog / wdt_pci.c
1 /*
2  *      Industrial Computer Source WDT500/501 driver for Linux 2.1.x
3  *
4  *      (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5  *                              http://www.redhat.com
6  *
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.
11  *      
12  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide 
13  *      warranty for any of this software. This material is provided 
14  *      "AS-IS" and at no charge.       
15  *
16  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
17  *
18  *      Release 0.09.
19  *
20  *      Fixes
21  *              Dave Gregorich  :       Modularisation and minor bugs
22  *              Alan Cox        :       Added the watchdog ioctl() stuff
23  *              Alan Cox        :       Fixed the reboot problem (as noted by
24  *                                      Matt Crocker).
25  *              Alan Cox        :       Added wdt= boot option
26  *              Alan Cox        :       Cleaned up copy/user stuff
27  *              Tim Hockin      :       Added insmod parameters, comment cleanup
28  *                                      Parameterized timeout
29  *              JP Nollmann     :       Added support for PCI wdt501p
30  *              Alan Cox        :       Split ISA and PCI cards into two drivers
31  *              Jeff Garzik     :       PCI cleanups
32  *              Tigran Aivazian :       Restructured wdtpci_init_one() to handle failures
33  *              Joel Becker     :       Added WDIOC_GET/SETTIMEOUT
34  *              Zwane Mwaikambo :       Magic char closing, locking changes, cleanups
35  *              Matt Domsch     :       nowayout module option
36  */
37
38 #include <linux/config.h>
39 #include <linux/interrupt.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/types.h>
43 #include <linux/miscdevice.h>
44 #include <linux/watchdog.h>
45 #include <linux/ioport.h>
46 #include <linux/notifier.h>
47 #include <linux/reboot.h>
48 #include <linux/init.h>
49 #include <linux/pci.h>
50
51 #include <asm/io.h>
52 #include <asm/uaccess.h>
53 #include <asm/system.h>
54
55 #define WDT_IS_PCI
56 #include "wd501p.h"
57
58 #define PFX "wdt_pci: "
59
60 /*
61  * Until Access I/O gets their application for a PCI vendor ID approved,
62  * I don't think that it's appropriate to move these constants into the
63  * regular pci_ids.h file. -- JPN 2000/01/18
64  */
65
66 #ifndef PCI_VENDOR_ID_ACCESSIO
67 #define PCI_VENDOR_ID_ACCESSIO 0x494f
68 #endif
69 #ifndef PCI_DEVICE_ID_WDG_CSM
70 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
71 #endif
72
73 static struct semaphore open_sem;
74 static spinlock_t wdtpci_lock;
75 static int expect_close = 0;
76
77 static int io;
78 static int irq;
79
80 /* Default timeout */
81 #define WD_TIMO (100*60)                /* 1 minute */
82 #define WD_TIMO_MAX (WD_TIMO*60)        /* 1 hour(?) */
83
84 static int wd_margin = WD_TIMO;
85
86 #ifdef CONFIG_WATCHDOG_NOWAYOUT
87 static int nowayout = 1;
88 #else
89 static int nowayout = 0;
90 #endif
91
92 module_param(nowayout, int, 0);
93 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
94
95 /*
96  *      Programming support
97  */
98  
99 static void wdtpci_ctr_mode(int ctr, int mode)
100 {
101         ctr<<=6;
102         ctr|=0x30;
103         ctr|=(mode<<1);
104         outb_p(ctr, WDT_CR);
105 }
106
107 static void wdtpci_ctr_load(int ctr, int val)
108 {
109         outb_p(val&0xFF, WDT_COUNT0+ctr);
110         outb_p(val>>8, WDT_COUNT0+ctr);
111 }
112
113 /*
114  *      Kernel methods.
115  */
116  
117  
118 /**
119  *      wdtpci_status:
120  *      
121  *      Extract the status information from a WDT watchdog device. There are
122  *      several board variants so we have to know which bits are valid. Some
123  *      bits default to one and some to zero in order to be maximally painful.
124  *
125  *      we then map the bits onto the status ioctl flags.
126  */
127  
128 static int wdtpci_status(void)
129 {
130         /*
131          *      Status register to bit flags
132          */
133          
134         int flag=0;
135         unsigned char status=inb_p(WDT_SR);
136         status|=FEATUREMAP1;
137         status&=~FEATUREMAP2;   
138         
139         if(!(status&WDC_SR_TGOOD))
140                 flag|=WDIOF_OVERHEAT;
141         if(!(status&WDC_SR_PSUOVER))
142                 flag|=WDIOF_POWEROVER;
143         if(!(status&WDC_SR_PSUUNDR))
144                 flag|=WDIOF_POWERUNDER;
145         if(!(status&WDC_SR_FANGOOD))
146                 flag|=WDIOF_FANFAULT;
147         if(status&WDC_SR_ISOI0)
148                 flag|=WDIOF_EXTERN1;
149         if(status&WDC_SR_ISII1)
150                 flag|=WDIOF_EXTERN2;
151         return flag;
152 }
153
154 /**
155  *      wdtpci_interrupt:
156  *      @irq:           Interrupt number
157  *      @dev_id:        Unused as we don't allow multiple devices.
158  *      @regs:          Unused.
159  *
160  *      Handle an interrupt from the board. These are raised when the status
161  *      map changes in what the board considers an interesting way. That means
162  *      a failure condition occurring.
163  */
164  
165 static irqreturn_t wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
166 {
167         /*
168          *      Read the status register see what is up and
169          *      then printk it. 
170          */
171          
172         unsigned char status=inb_p(WDT_SR);
173         
174         status|=FEATUREMAP1;
175         status&=~FEATUREMAP2;   
176         
177         printk(KERN_CRIT "WDT status %d\n", status);
178         
179         if(!(status&WDC_SR_TGOOD))
180                 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
181         if(!(status&WDC_SR_PSUOVER))
182                 printk(KERN_CRIT "PSU over voltage.\n");
183         if(!(status&WDC_SR_PSUUNDR))
184                 printk(KERN_CRIT "PSU under voltage.\n");
185         if(!(status&WDC_SR_FANGOOD))
186                 printk(KERN_CRIT "Possible fan fault.\n");
187         if(!(status&WDC_SR_WCCR))
188 #ifdef SOFTWARE_REBOOT
189 #ifdef ONLY_TESTING
190                 printk(KERN_CRIT "Would Reboot.\n");
191 #else           
192                 printk(KERN_CRIT "Initiating system reboot.\n");
193                 machine_restart(NULL);
194 #endif          
195 #else
196                 printk(KERN_CRIT "Reset in 5ms.\n");
197 #endif
198         return IRQ_HANDLED;
199 }
200
201
202 /**
203  *      wdtpci_ping:
204  *
205  *      Reload counter one with the watchdog timeout. We don't bother reloading
206  *      the cascade counter. 
207  */
208  
209 static void wdtpci_ping(void)
210 {
211         unsigned long flags;
212
213         /* Write a watchdog value */
214         spin_lock_irqsave(&wdtpci_lock, flags);
215         inb_p(WDT_DC);
216         wdtpci_ctr_mode(1,2);
217         wdtpci_ctr_load(1,wd_margin);           /* Timeout */
218         outb_p(0, WDT_DC);
219         spin_unlock_irqrestore(&wdtpci_lock, flags);
220 }
221
222 /**
223  *      wdtpci_write:
224  *      @file: file handle to the watchdog
225  *      @buf: buffer to write (unused as data does not matter here 
226  *      @count: count of bytes
227  *      @ppos: pointer to the position to write. No seeks allowed
228  *
229  *      A write to a watchdog device is defined as a keepalive signal. Any
230  *      write of data will do, as we we don't define content meaning.
231  */
232  
233 static ssize_t wdtpci_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
234 {
235         /*  Can't seek (pwrite) on this device  */
236         if (ppos != &file->f_pos)
237                 return -ESPIPE;
238
239         if (count) {
240                 if (!nowayout) {
241                         size_t i;
242
243                         expect_close = 0;
244
245                         for (i = 0; i != count; i++) {
246                                 char c;
247                                 if(get_user(c, buf+i))
248                                         return -EFAULT;
249                                 if (c == 'V')
250                                         expect_close = 1;
251                         }
252                 }
253                 wdtpci_ping();
254         }
255
256         return count;
257 }
258
259 /**
260  *      wdtpci_read:
261  *      @file: file handle to the watchdog board
262  *      @buf: buffer to write 1 byte into
263  *      @count: length of buffer
264  *      @ptr: offset (no seek allowed)
265  *
266  *      Read reports the temperature in degrees Fahrenheit. The API is in
267  *      fahrenheit. It was designed by an imperial measurement luddite.
268  */
269  
270 static ssize_t wdtpci_read(struct file *file, char *buf, size_t count, loff_t *ptr)
271 {
272         unsigned short c=inb_p(WDT_RT);
273         unsigned char cp;
274         
275         /*  Can't seek (pread) on this device  */
276         if (ptr != &file->f_pos)
277                 return -ESPIPE;
278
279         switch(minor(file->f_dentry->d_inode->i_rdev))
280         {
281                 case TEMP_MINOR:
282                         c*=11;
283                         c/=15;
284                         cp=c+7;
285                         if(copy_to_user(buf,&cp,1))
286                                 return -EFAULT;
287                         return 1;
288                 default:
289                         return -EINVAL;
290         }
291 }
292
293 /**
294  *      wdtpci_ioctl:
295  *      @inode: inode of the device
296  *      @file: file handle to the device
297  *      @cmd: watchdog command
298  *      @arg: argument pointer
299  *
300  *      The watchdog API defines a common set of functions for all watchdogs
301  *      according to their available features. We only actually usefully support
302  *      querying capabilities and current status. 
303  */
304  
305 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
306         unsigned long arg)
307 {
308         int new_margin;
309         static struct watchdog_info ident = {
310                 .options        = WDIOF_OVERHEAT  | WDIOF_POWERUNDER |
311                                   WDIOF_POWEROVER | WDIOF_EXTERN1 |
312                                   WDIOF_EXTERN2   | WDIOF_FANFAULT |
313                                   WDIOF_SETTIMEOUT|WDIOF_MAGICCLOSE,
314                 .firmware_version = 1,
315                 .identity         = "WDT500/501PCI",
316         };
317         
318         ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
319         switch(cmd)
320         {
321                 default:
322                         return -ENOTTY;
323                 case WDIOC_GETSUPPORT:
324                         return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
325
326                 case WDIOC_GETSTATUS:
327                         return put_user(wdtpci_status(),(int *)arg);
328                 case WDIOC_GETBOOTSTATUS:
329                         return put_user(0, (int *)arg);
330                 case WDIOC_KEEPALIVE:
331                         wdtpci_ping();
332                         return 0;
333                 case WDIOC_SETTIMEOUT:
334                         if (get_user(new_margin, (int *)arg))
335                                 return -EFAULT;
336                         /* Arbitrary, can't find the card's limits */
337                         new_margin *= 100;
338                         if ((new_margin < 0) || (new_margin > WD_TIMO_MAX))
339                                 return -EINVAL;
340                         wd_margin = new_margin;
341                         wdtpci_ping();
342                         /* Fall */
343                 case WDIOC_GETTIMEOUT:
344                         return put_user(wd_margin / 100, (int *)arg);
345         }
346 }
347
348 /**
349  *      wdtpci_open:
350  *      @inode: inode of device
351  *      @file: file handle to device
352  *
353  *      One of our two misc devices has been opened. The watchdog device is
354  *      single open and on opening we load the counters. Counter zero is a 
355  *      100Hz cascade, into counter 1 which downcounts to reboot. When the
356  *      counter triggers counter 2 downcounts the length of the reset pulse
357  *      which set set to be as long as possible. 
358  */
359  
360 static int wdtpci_open(struct inode *inode, struct file *file)
361 {
362         unsigned long flags;
363
364         switch(minor(inode->i_rdev))
365         {
366                 case WATCHDOG_MINOR:
367                         if (down_trylock(&open_sem))
368                                 return -EBUSY;
369
370                         if (nowayout) {
371                                 __module_get(THIS_MODULE);
372                         }
373                         /*
374                          *      Activate 
375                          */
376                         spin_lock_irqsave(&wdtpci_lock, flags);
377                         
378                         inb_p(WDT_DC);          /* Disable */
379
380                         /*
381                          * "pet" the watchdog, as Access says.
382                          * This resets the clock outputs.
383                          */
384                                 
385                         wdtpci_ctr_mode(2,0);
386                         outb_p(0, WDT_DC);
387
388                         inb_p(WDT_DC);
389
390                         outb_p(0, WDT_CLOCK);   /* 2.0833MHz clock */
391                         inb_p(WDT_BUZZER);      /* disable */
392                         inb_p(WDT_OPTONOTRST);  /* disable */
393                         inb_p(WDT_OPTORST);     /* disable */
394                         inb_p(WDT_PROGOUT);     /* disable */
395                         wdtpci_ctr_mode(0,3);
396                         wdtpci_ctr_mode(1,2);
397                         wdtpci_ctr_mode(2,1);
398                         wdtpci_ctr_load(0,20833);       /* count at 100Hz */
399                         wdtpci_ctr_load(1,wd_margin);/* Timeout 60 seconds */
400                         /* DO NOT LOAD CTR2 on PCI card! -- JPN */
401                         outb_p(0, WDT_DC);      /* Enable */
402                         spin_unlock_irqrestore(&wdtpci_lock, flags);
403                         return 0;
404                 case TEMP_MINOR:
405                         return 0;
406                 default:
407                         return -ENODEV;
408         }
409 }
410
411 /**
412  *      wdtpci_close:
413  *      @inode: inode to board
414  *      @file: file handle to board
415  *
416  *      The watchdog has a configurable API. There is a religious dispute 
417  *      between people who want their watchdog to be able to shut down and 
418  *      those who want to be sure if the watchdog manager dies the machine
419  *      reboots. In the former case we disable the counters, in the latter
420  *      case you have to open it again very soon.
421  */
422  
423 static int wdtpci_release(struct inode *inode, struct file *file)
424 {
425
426         if (minor(inode->i_rdev)==WATCHDOG_MINOR) {
427                 unsigned long flags;
428                 if (expect_close) {
429                         spin_lock_irqsave(&wdtpci_lock, flags);
430                         inb_p(WDT_DC);          /* Disable counters */
431                         wdtpci_ctr_load(2,0);   /* 0 length reset pulses now */
432                         spin_unlock_irqrestore(&wdtpci_lock, flags);
433                 } else {
434                         printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
435                         wdtpci_ping();
436                 }
437                 up(&open_sem);
438         }
439         return 0;
440 }
441
442 /**
443  *      notify_sys:
444  *      @this: our notifier block
445  *      @code: the event being reported
446  *      @unused: unused
447  *
448  *      Our notifier is called on system shutdowns. We want to turn the card
449  *      off at reboot otherwise the machine will reboot again during memory
450  *      test or worse yet during the following fsck. This would suck, in fact
451  *      trust me - if it happens it does suck.
452  */
453
454 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
455         void *unused)
456 {
457         unsigned long flags;
458
459         if (code==SYS_DOWN || code==SYS_HALT) {
460                 /* Turn the card off */
461                 spin_lock_irqsave(&wdtpci_lock, flags);
462                 inb_p(WDT_DC);
463                 wdtpci_ctr_load(2,0);
464                 spin_unlock_irqrestore(&wdtpci_lock, flags);
465         }
466         return NOTIFY_DONE;
467 }
468  
469 /*
470  *      Kernel Interfaces
471  */
472  
473  
474 static struct file_operations wdtpci_fops = {
475         .owner          = THIS_MODULE,
476         .llseek         = no_llseek,
477         .read           = wdtpci_read,
478         .write          = wdtpci_write,
479         .ioctl          = wdtpci_ioctl,
480         .open           = wdtpci_open,
481         .release        = wdtpci_release,
482 };
483
484 static struct miscdevice wdtpci_miscdev = {
485         .minor  = WATCHDOG_MINOR,
486         .name   = "watchdog",
487         .fops   = &wdtpci_fops,
488 };
489
490 #ifdef CONFIG_WDT_501_PCI
491 static struct miscdevice temp_miscdev = {
492         .minor  = TEMP_MINOR,
493         .name   = "temperature",
494         .fops   = &wdtpci_fops,
495 };
496 #endif
497
498 /*
499  *      The WDT card needs to learn about soft shutdowns in order to
500  *      turn the timebomb registers off. 
501  */
502  
503 static struct notifier_block wdtpci_notifier = {
504         .notifier_call = wdtpci_notify_sys,
505 };
506
507
508 static int __init wdtpci_init_one (struct pci_dev *dev,
509                                    const struct pci_device_id *ent)
510 {
511         static int dev_count = 0;
512         int ret = -EIO;
513
514         dev_count++;
515         if (dev_count > 1) {
516                 printk (KERN_ERR PFX
517                         "this driver only supports 1 device\n");
518                 return -ENODEV;
519         }
520
521         if (pci_enable_device (dev))
522                 goto out;
523
524         sema_init(&open_sem, 1);
525         spin_lock_init(&wdtpci_lock);
526
527         irq = dev->irq;
528         io = pci_resource_start (dev, 2);
529         printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
530                 "(Interrupt %d)\n", io, irq);
531
532         if (request_region (io, 16, "wdt-pci") == NULL) {
533                 printk (KERN_ERR PFX "I/O %d is not free.\n", io);
534                 goto out;
535         }
536
537         if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
538                          "wdt-pci", &wdtpci_miscdev)) {
539                 printk (KERN_ERR PFX "IRQ %d is not free.\n", irq);
540                 goto out_reg;
541         }
542
543         ret = misc_register (&wdtpci_miscdev);
544         if (ret) {
545                 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
546                 goto out_irq;
547         }
548
549         ret = register_reboot_notifier (&wdtpci_notifier);
550         if (ret) {
551                 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
552                 goto out_misc;
553         }
554 #ifdef CONFIG_WDT_501_PCI
555         ret = misc_register (&temp_miscdev);
556         if (ret) {
557                 printk (KERN_ERR PFX "can't misc_register (temp) on minor=%d\n", TEMP_MINOR);
558                 goto out_rbt;
559         }
560 #endif
561
562         ret = 0;
563 out:
564         return ret;
565
566 #ifdef CONFIG_WDT_501_PCI
567 out_rbt:
568         unregister_reboot_notifier(&wdtpci_notifier);
569 #endif
570 out_misc:
571         misc_deregister(&wdtpci_miscdev);
572 out_irq:
573         free_irq(irq, &wdtpci_miscdev);
574 out_reg:
575         release_region (io, 16);
576         goto out;
577 }
578
579
580 static void __devexit wdtpci_remove_one (struct pci_dev *pdev)
581 {
582         /* here we assume only one device will ever have
583          * been picked up and registered by probe function */
584         unregister_reboot_notifier(&wdtpci_notifier);
585 #ifdef CONFIG_WDT_501_PCI
586         misc_deregister(&temp_miscdev);
587 #endif  
588         misc_deregister(&wdtpci_miscdev);
589         free_irq(irq, &wdtpci_miscdev);
590         release_region(io, 16);
591 }
592
593
594 static struct pci_device_id wdtpci_pci_tbl[] = {
595         {
596                 .vendor    = PCI_VENDOR_ID_ACCESSIO,
597                 .device    = PCI_DEVICE_ID_WDG_CSM,
598                 .subvendor = PCI_ANY_ID,
599                 .subdevice = PCI_ANY_ID,
600         },
601         { 0, }, /* terminate list */
602 };
603 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
604
605
606 static struct pci_driver wdtpci_driver = {
607         .name           = "wdt-pci",
608         .id_table       = wdtpci_pci_tbl,
609         .probe          = wdtpci_init_one,
610         .remove         = __devexit_p(wdtpci_remove_one),
611 };
612
613
614 /**
615  *      wdtpci_cleanup:
616  *
617  *      Unload the watchdog. You cannot do this with any file handles open.
618  *      If your watchdog is set to continue ticking on close and you unload
619  *      it, well it keeps ticking. We won't get the interrupt but the board
620  *      will not touch PC memory so all is fine. You just have to load a new
621  *      module in 60 seconds or reboot.
622  */
623  
624 static void __exit wdtpci_cleanup(void)
625 {
626         pci_unregister_driver (&wdtpci_driver);
627 }
628
629
630 /**
631  *      wdtpci_init:
632  *
633  *      Set up the WDT watchdog board. All we have to do is grab the
634  *      resources we require and bitch if anyone beat us to them.
635  *      The open() function will actually kick the board off.
636  */
637  
638 static int __init wdtpci_init(void)
639 {
640         int rc = pci_register_driver (&wdtpci_driver);
641         
642         if (rc < 1)
643                 return -ENODEV;
644         
645         return 0;
646 }
647
648
649 module_init(wdtpci_init);
650 module_exit(wdtpci_cleanup);
651
652 MODULE_AUTHOR("JP Nollmann, Alan Cox");
653 MODULE_DESCRIPTION("Driver for the ICS PCI watchdog cards");
654 MODULE_LICENSE("GPL");