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