This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / char / watchdog / pcwd_pci.c
1 /*
2  *      Berkshire PCI-PC Watchdog Card Driver
3  *
4  *      (c) Copyright 2003 Wim Van Sebroeck <wim@iguana.be>.
5  *
6  *      Based on source code of the following authors:
7  *        Ken Hollis <kenji@bitgate.com>,
8  *        Lindsay Harris <lindsay@bluegum.com>,
9  *        Alan Cox <alan@redhat.com>,
10  *        Matt Domsch <Matt_Domsch@dell.com>,
11  *        Rob Radez <rob@osinvestor.com>
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License
15  *      as published by the Free Software Foundation; either version
16  *      2 of the License, or (at your option) any later version.
17  *
18  *      Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19  *      provide warranty for any of this software. This material is
20  *      provided "AS-IS" and at no charge.
21  */
22
23 /*
24  *      A bells and whistles driver is available from http://www.pcwd.de/
25  *      More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
26  */
27
28 /*
29  *      Includes, defines, variables, module parameters, ...
30  */
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/miscdevice.h>
38 #include <linux/watchdog.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/init.h>
42 #include <linux/fs.h>
43 #include <linux/pci.h>
44 #include <linux/ioport.h>
45 #include <linux/spinlock.h>
46
47 #include <asm/uaccess.h>
48 #include <asm/io.h>
49
50 /* Module and version information */
51 #define WATCHDOG_VERSION "1.00"
52 #define WATCHDOG_DATE "12 Jun 2004"
53 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54 #define WATCHDOG_NAME "pcwd_pci"
55 #define PFX WATCHDOG_NAME ": "
56 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
57
58 /* Stuff for the PCI ID's  */
59 #ifndef PCI_VENDOR_ID_QUICKLOGIC
60 #define PCI_VENDOR_ID_QUICKLOGIC    0x11e3
61 #endif
62
63 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
64 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
65 #endif
66
67 /*
68  * These are the defines that describe the control status bits for the
69  * PCI-PC Watchdog card.
70  */
71 #define WD_PCI_WTRP             0x01    /* Watchdog Trip status */
72 #define WD_PCI_HRBT             0x02    /* Watchdog Heartbeat */
73 #define WD_PCI_TTRP             0x04    /* Temperature Trip status */
74
75 /* according to documentation max. time to process a command for the pci
76  * watchdog card is 100 ms, so we give it 150 ms to do it's job */
77 #define PCI_COMMAND_TIMEOUT     150
78
79 /* Watchdog's internal commands */
80 #define CMD_GET_STATUS                  0x04
81 #define CMD_GET_FIRMWARE_VERSION        0x08
82 #define CMD_READ_WATCHDOG_TIMEOUT       0x18
83 #define CMD_WRITE_WATCHDOG_TIMEOUT      0x19
84
85 /* We can only use 1 card due to the /dev/watchdog restriction */
86 static int cards_found;
87
88 /* internal variables */
89 static int temp_panic;
90 static unsigned long is_active;
91 static char expect_release;
92 static struct {
93         int supports_temp;      /* Wether or not the card has a temperature device */
94         int boot_status;        /* The card's boot status */
95         unsigned long io_addr;  /* The cards I/O address */
96         spinlock_t io_lock;
97         struct pci_dev *pdev;
98 } pcipcwd_private;
99
100 /* module parameters */
101 #define WATCHDOG_HEARTBEAT 2    /* 2 sec default heartbeat */
102 static int heartbeat = WATCHDOG_HEARTBEAT;
103 module_param(heartbeat, int, 0);
104 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
105
106 #ifdef CONFIG_WATCHDOG_NOWAYOUT
107 static int nowayout = 1;
108 #else
109 static int nowayout = 0;
110 #endif
111
112 module_param(nowayout, int, 0);
113 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
114
115 /*
116  *      Internal functions
117  */
118
119 static int send_command(int cmd, int *msb, int *lsb)
120 {
121         int got_response, count;
122
123         spin_lock(&pcipcwd_private.io_lock);
124         /* If a command requires data it should be written first.
125          * Data for commands with 8 bits of data should be written to port 4.
126          * Commands with 16 bits of data, should be written as LSB to port 4
127          * and MSB to port 5.
128          * After the required data has been written then write the command to
129          * port 6. */
130         outb_p(*lsb, pcipcwd_private.io_addr + 4);
131         outb_p(*msb, pcipcwd_private.io_addr + 5);
132         outb_p(cmd, pcipcwd_private.io_addr + 6);
133
134         /* wait till the pci card processed the command, signaled by
135          * the WRSP bit in port 2 and give it a max. timeout of
136          * PCI_COMMAND_TIMEOUT to process */
137         got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
138         for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
139                 mdelay(1);
140                 got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
141         }
142
143         if (got_response) {
144                 /* read back response */
145                 *lsb = inb_p(pcipcwd_private.io_addr + 4);
146                 *msb = inb_p(pcipcwd_private.io_addr + 5);
147
148                 /* clear WRSP bit */
149                 inb_p(pcipcwd_private.io_addr + 6);
150         }
151         spin_unlock(&pcipcwd_private.io_lock);
152
153         return got_response;
154 }
155
156 static int pcipcwd_start(void)
157 {
158         int stat_reg;
159
160         spin_lock(&pcipcwd_private.io_lock);
161         outb_p(0x00, pcipcwd_private.io_addr + 3);
162         udelay(1000);
163
164         stat_reg = inb_p(pcipcwd_private.io_addr + 2);
165         spin_unlock(&pcipcwd_private.io_lock);
166
167         if (stat_reg & 0x10) {
168                 printk(KERN_ERR PFX "Card timer not enabled\n");
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 static int pcipcwd_stop(void)
176 {
177         int stat_reg;
178
179         spin_lock(&pcipcwd_private.io_lock);
180         outb_p(0xA5, pcipcwd_private.io_addr + 3);
181         udelay(1000);
182
183         outb_p(0xA5, pcipcwd_private.io_addr + 3);
184         udelay(1000);
185
186         stat_reg = inb_p(pcipcwd_private.io_addr + 2);
187         spin_unlock(&pcipcwd_private.io_lock);
188
189         if (!(stat_reg & 0x10)) {
190                 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
191                 return -1;
192         }
193
194         return 0;
195 }
196
197 static int pcipcwd_keepalive(void)
198 {
199         /* Re-trigger watchdog by writing to port 0 */
200         outb_p(0x42, pcipcwd_private.io_addr);
201         return 0;
202 }
203
204 static int pcipcwd_set_heartbeat(int t)
205 {
206         int t_msb = t / 256;
207         int t_lsb = t % 256;
208
209         if ((t < 0x0001) || (t > 0xFFFF))
210                 return -EINVAL;
211
212         /* Write new heartbeat to watchdog */
213         send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
214
215         heartbeat = t;
216         return 0;
217 }
218
219 static int pcipcwd_get_status(int *status)
220 {
221         int new_status;
222
223         *status=0;
224         new_status = inb_p(pcipcwd_private.io_addr + 1);
225         if (new_status & WD_PCI_WTRP)
226                 *status |= WDIOF_CARDRESET;
227         if (new_status & WD_PCI_TTRP) {
228                 *status |= WDIOF_OVERHEAT;
229                 if (temp_panic)
230                         panic(PFX "Temperature overheat trip!\n");
231         }
232
233         return 0;
234 }
235
236 static int pcipcwd_clear_status(void)
237 {
238         outb_p(0x01, pcipcwd_private.io_addr + 1);
239         return 0;
240 }
241
242 static int pcipcwd_get_temperature(int *temperature)
243 {
244         *temperature = 0;
245         if (!pcipcwd_private.supports_temp)
246                 return -ENODEV;
247
248         /*
249          * Convert celsius to fahrenheit, since this was
250          * the decided 'standard' for this return value.
251          */
252         *temperature = ((inb_p(pcipcwd_private.io_addr)) * 9 / 5) + 32;
253
254         return 0;
255 }
256
257 /*
258  *      /dev/watchdog handling
259  */
260
261 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
262                               size_t len, loff_t *ppos)
263 {
264         /* Can't seek (pwrite) on this device  */
265         if (ppos != &file->f_pos)
266                 return -ESPIPE;
267
268         /* See if we got the magic character 'V' and reload the timer */
269         if (len) {
270                 if (!nowayout) {
271                         size_t i;
272
273                         /* note: just in case someone wrote the magic character
274                          * five months ago... */
275                         expect_release = 0;
276
277                         /* scan to see whether or not we got the magic character */
278                         for (i = 0; i != len; i++) {
279                                 char c;
280                                 if(get_user(c, data+i))
281                                         return -EFAULT;
282                                 if (c == 'V')
283                                         expect_release = 42;
284                         }
285                 }
286
287                 /* someone wrote to us, we should reload the timer */
288                 pcipcwd_keepalive();
289         }
290         return len;
291 }
292
293 static int pcipcwd_ioctl(struct inode *inode, struct file *file,
294                           unsigned int cmd, unsigned long arg)
295 {
296         void __user *argp = (void __user *)arg;
297         int __user *p = argp;
298         static struct watchdog_info ident = {
299                 .options =              WDIOF_OVERHEAT |
300                                         WDIOF_CARDRESET |
301                                         WDIOF_KEEPALIVEPING |
302                                         WDIOF_SETTIMEOUT |
303                                         WDIOF_MAGICCLOSE,
304                 .firmware_version =     1,
305                 .identity =             WATCHDOG_DRIVER_NAME,
306         };
307
308         switch (cmd) {
309                 case WDIOC_GETSUPPORT:
310                         return copy_to_user(argp, &ident,
311                                 sizeof (ident)) ? -EFAULT : 0;
312
313                 case WDIOC_GETSTATUS:
314                 {
315                         int status;
316
317                         pcipcwd_get_status(&status);
318
319                         return put_user(status, p);
320                 }
321
322                 case WDIOC_GETBOOTSTATUS:
323                         return put_user(pcipcwd_private.boot_status, p);
324
325                 case WDIOC_GETTEMP:
326                 {
327                         int temperature;
328
329                         if (pcipcwd_get_temperature(&temperature))
330                                 return -EFAULT;
331
332                         return put_user(temperature, p);
333                 }
334
335                 case WDIOC_KEEPALIVE:
336                         pcipcwd_keepalive();
337                         return 0;
338
339                 case WDIOC_SETOPTIONS:
340                 {
341                         int new_options, retval = -EINVAL;
342
343                         if (get_user (new_options, p))
344                                 return -EFAULT;
345
346                         if (new_options & WDIOS_DISABLECARD) {
347                                 pcipcwd_stop();
348                                 retval = 0;
349                         }
350
351                         if (new_options & WDIOS_ENABLECARD) {
352                                 pcipcwd_start();
353                                 retval = 0;
354                         }
355
356                         if (new_options & WDIOS_TEMPPANIC) {
357                                 temp_panic = 1;
358                                 retval = 0;
359                         }
360
361                         return retval;
362                 }
363
364                 case WDIOC_SETTIMEOUT:
365                 {
366                         int new_heartbeat;
367
368                         if (get_user(new_heartbeat, p))
369                                 return -EFAULT;
370
371                         if (pcipcwd_set_heartbeat(new_heartbeat))
372                             return -EINVAL;
373
374                         pcipcwd_keepalive();
375                         /* Fall */
376                 }
377
378                 case WDIOC_GETTIMEOUT:
379                         return put_user(heartbeat, p);
380
381                 default:
382                         return -ENOIOCTLCMD;
383         }
384 }
385
386 static int pcipcwd_open(struct inode *inode, struct file *file)
387 {
388         /* /dev/watchdog can only be opened once */
389         if (test_and_set_bit(0, &is_active))
390                 return -EBUSY;
391
392         /* Activate */
393         pcipcwd_start();
394         pcipcwd_keepalive();
395         return 0;
396 }
397
398 static int pcipcwd_release(struct inode *inode, struct file *file)
399 {
400         /*
401          *      Shut off the timer.
402          */
403         if (expect_release == 42) {
404                 pcipcwd_stop();
405         } else {
406                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
407                 pcipcwd_keepalive();
408         }
409         expect_release = 0;
410         clear_bit(0, &is_active);
411         return 0;
412 }
413
414 /*
415  *      /dev/temperature handling
416  */
417
418 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
419                                 size_t len, loff_t *ppos)
420 {
421         int temperature;
422
423         /* Can't seek (pwrite) on this device  */
424         if (ppos != &file->f_pos)
425                 return -ESPIPE;
426
427         if (pcipcwd_get_temperature(&temperature))
428                 return -EFAULT;
429
430         if (copy_to_user (data, &temperature, 1))
431                 return -EFAULT;
432
433         return 1;
434 }
435
436 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
437 {
438         if (!pcipcwd_private.supports_temp)
439                 return -ENODEV;
440
441         return 0;
442 }
443
444 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
445 {
446         return 0;
447 }
448
449 /*
450  *      Notify system
451  */
452
453 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
454 {
455         if (code==SYS_DOWN || code==SYS_HALT) {
456                 /* Turn the WDT off */
457                 pcipcwd_stop();
458         }
459
460         return NOTIFY_DONE;
461 }
462
463 /*
464  *      Kernel Interfaces
465  */
466
467 static struct file_operations pcipcwd_fops = {
468         .owner =        THIS_MODULE,
469         .llseek =       no_llseek,
470         .write =        pcipcwd_write,
471         .ioctl =        pcipcwd_ioctl,
472         .open =         pcipcwd_open,
473         .release =      pcipcwd_release,
474 };
475
476 static struct miscdevice pcipcwd_miscdev = {
477         .minor =        WATCHDOG_MINOR,
478         .name =         "watchdog",
479         .fops =         &pcipcwd_fops,
480 };
481
482 static struct file_operations pcipcwd_temp_fops = {
483         .owner =        THIS_MODULE,
484         .llseek =       no_llseek,
485         .read =         pcipcwd_temp_read,
486         .open =         pcipcwd_temp_open,
487         .release =      pcipcwd_temp_release,
488 };
489
490 static struct miscdevice pcipcwd_temp_miscdev = {
491         .minor =        TEMP_MINOR,
492         .name =         "temperature",
493         .fops =         &pcipcwd_temp_fops,
494 };
495
496 static struct notifier_block pcipcwd_notifier = {
497         .notifier_call =        pcipcwd_notify_sys,
498 };
499
500 /*
501  *      Init & exit routines
502  */
503
504 static inline void check_temperature_support(void)
505 {
506         if (inb_p(pcipcwd_private.io_addr) != 0xF0)
507                 pcipcwd_private.supports_temp = 1;
508 }
509
510 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
511                 const struct pci_device_id *ent)
512 {
513         int ret = -EIO;
514         int got_fw_rev, fw_rev_major, fw_rev_minor;
515         char fw_ver_str[20];
516         char option_switches;
517
518         cards_found++;
519         if (cards_found == 1)
520                 printk(KERN_INFO PFX DRIVER_VERSION);
521
522         if (cards_found > 1) {
523                 printk(KERN_ERR PFX "This driver only supports 1 device\n");
524                 return -ENODEV;
525         }
526
527         if (pci_enable_device(pdev)) {
528                 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
529                 return -ENODEV;
530         }
531
532         if (pci_resource_start(pdev, 0) == 0x0000) {
533                 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
534                 ret = -ENODEV;
535                 goto err_out_disable_device;
536         }
537
538         pcipcwd_private.pdev = pdev;
539         pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
540
541         if (pci_request_regions(pdev, WATCHDOG_NAME)) {
542                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
543                         (int) pcipcwd_private.io_addr);
544                 ret = -EIO;
545                 goto err_out_disable_device;
546         }
547
548         /* get the boot_status */
549         pcipcwd_get_status(&pcipcwd_private.boot_status);
550
551         /* clear the "card caused reboot" flag */
552         pcipcwd_clear_status();
553
554         /* disable card */
555         pcipcwd_stop();
556
557         /* Check whether or not the card supports the temperature device */
558         check_temperature_support();
559
560         /* Get the Firmware Version */
561         got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
562         if (got_fw_rev) {
563                 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
564         } else {
565                 sprintf(fw_ver_str, "<card no answer>");
566         }
567
568         /* Get switch settings */
569         option_switches = inb_p(pcipcwd_private.io_addr + 3);
570
571         printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
572                 (int) pcipcwd_private.io_addr, fw_ver_str,
573                 (pcipcwd_private.supports_temp ? "with" : "without"));
574
575         printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
576                 option_switches,
577                 ((option_switches & 0x10) ? "ON" : "OFF"),
578                 ((option_switches & 0x08) ? "ON" : "OFF"));
579
580         if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
581                 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
582
583         if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
584                 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
585
586         if (pcipcwd_private.boot_status == 0)
587                 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
588
589         /* Check that the heartbeat value is within it's range ; if not reset to the default */
590         if (pcipcwd_set_heartbeat(heartbeat)) {
591                 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
592                 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
593                         WATCHDOG_HEARTBEAT);
594         }
595
596         ret = register_reboot_notifier(&pcipcwd_notifier);
597         if (ret != 0) {
598                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
599                         ret);
600                 goto err_out_release_region;
601         }
602
603         if (pcipcwd_private.supports_temp) {
604                 ret = misc_register(&pcipcwd_temp_miscdev);
605                 if (ret != 0) {
606                         printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
607                                 TEMP_MINOR, ret);
608                         goto err_out_unregister_reboot;
609                 }
610         }
611
612         ret = misc_register(&pcipcwd_miscdev);
613         if (ret != 0) {
614                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
615                         WATCHDOG_MINOR, ret);
616                 goto err_out_misc_deregister;
617         }
618
619         printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
620                 heartbeat, nowayout);
621
622         return 0;
623
624 err_out_misc_deregister:
625         if (pcipcwd_private.supports_temp)
626                 misc_deregister(&pcipcwd_temp_miscdev);
627 err_out_unregister_reboot:
628         unregister_reboot_notifier(&pcipcwd_notifier);
629 err_out_release_region:
630         pci_release_regions(pdev);
631 err_out_disable_device:
632         pci_disable_device(pdev);
633         return ret;
634 }
635
636 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
637 {
638         /* Stop the timer before we leave */
639         if (!nowayout)
640                 pcipcwd_stop();
641
642         /* Deregister */
643         misc_deregister(&pcipcwd_miscdev);
644         if (pcipcwd_private.supports_temp)
645                 misc_deregister(&pcipcwd_temp_miscdev);
646         unregister_reboot_notifier(&pcipcwd_notifier);
647         pci_release_regions(pdev);
648         pci_disable_device(pdev);
649         cards_found--;
650 }
651
652 static struct pci_device_id pcipcwd_pci_tbl[] = {
653         { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
654                 PCI_ANY_ID, PCI_ANY_ID, },
655         { 0 },                  /* End of list */
656 };
657 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
658
659 static struct pci_driver pcipcwd_driver = {
660         .name           = WATCHDOG_NAME,
661         .id_table       = pcipcwd_pci_tbl,
662         .probe          = pcipcwd_card_init,
663         .remove         = __devexit_p(pcipcwd_card_exit),
664 };
665
666 static int __init pcipcwd_init_module(void)
667 {
668         spin_lock_init (&pcipcwd_private.io_lock);
669
670         return pci_module_init(&pcipcwd_driver);
671 }
672
673 static void __exit pcipcwd_cleanup_module(void)
674 {
675         pci_unregister_driver(&pcipcwd_driver);
676 }
677
678 module_init(pcipcwd_init_module);
679 module_exit(pcipcwd_cleanup_module);
680
681 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
682 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
683 MODULE_LICENSE("GPL");
684 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
685 MODULE_ALIAS_MISCDEV(TEMP_MINOR);