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