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