wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Industrial Computer Source WDT500/501 driver
  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.10.
  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. * Tigran Aivazian : Restructured wdt_init() to handle failures
  30. * Joel Becker : Added WDIOC_GET/SETTIMEOUT
  31. * Matt Domsch : Added nowayout module option
  32. */
  33. #include <linux/interrupt.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/types.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/watchdog.h>
  39. #include <linux/fs.h>
  40. #include <linux/ioport.h>
  41. #include <linux/notifier.h>
  42. #include <linux/reboot.h>
  43. #include <linux/init.h>
  44. #include <asm/io.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/system.h>
  47. #include "wd501p.h"
  48. static unsigned long wdt_is_open;
  49. static char expect_close;
  50. /*
  51. * Module parameters
  52. */
  53. #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
  54. static int heartbeat = WD_TIMO;
  55. static int wd_heartbeat;
  56. module_param(heartbeat, int, 0);
  57. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WD_TIMO) ")");
  58. static int nowayout = WATCHDOG_NOWAYOUT;
  59. module_param(nowayout, int, 0);
  60. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  61. /* You must set these - there is no sane way to probe for this board. */
  62. static int io=0x240;
  63. static int irq=11;
  64. module_param(io, int, 0);
  65. MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
  66. module_param(irq, int, 0);
  67. MODULE_PARM_DESC(irq, "WDT irq (default=11)");
  68. #ifdef CONFIG_WDT_501
  69. /* Support for the Fan Tachometer on the WDT501-P */
  70. static int tachometer;
  71. module_param(tachometer, int, 0);
  72. MODULE_PARM_DESC(tachometer, "WDT501-P Fan Tachometer support (0=disable, default=0)");
  73. #endif /* CONFIG_WDT_501 */
  74. /*
  75. * Programming support
  76. */
  77. static void wdt_ctr_mode(int ctr, int mode)
  78. {
  79. ctr<<=6;
  80. ctr|=0x30;
  81. ctr|=(mode<<1);
  82. outb_p(ctr, WDT_CR);
  83. }
  84. static void wdt_ctr_load(int ctr, int val)
  85. {
  86. outb_p(val&0xFF, WDT_COUNT0+ctr);
  87. outb_p(val>>8, WDT_COUNT0+ctr);
  88. }
  89. /**
  90. * wdt_start:
  91. *
  92. * Start the watchdog driver.
  93. */
  94. static int wdt_start(void)
  95. {
  96. inb_p(WDT_DC); /* Disable watchdog */
  97. wdt_ctr_mode(0,3); /* Program CTR0 for Mode 3: Square Wave Generator */
  98. wdt_ctr_mode(1,2); /* Program CTR1 for Mode 2: Rate Generator */
  99. wdt_ctr_mode(2,0); /* Program CTR2 for Mode 0: Pulse on Terminal Count */
  100. wdt_ctr_load(0, 8948); /* Count at 100Hz */
  101. wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
  102. wdt_ctr_load(2,65535); /* Length of reset pulse */
  103. outb_p(0, WDT_DC); /* Enable watchdog */
  104. return 0;
  105. }
  106. /**
  107. * wdt_stop:
  108. *
  109. * Stop the watchdog driver.
  110. */
  111. static int wdt_stop (void)
  112. {
  113. /* Turn the card off */
  114. inb_p(WDT_DC); /* Disable watchdog */
  115. wdt_ctr_load(2,0); /* 0 length reset pulses now */
  116. return 0;
  117. }
  118. /**
  119. * wdt_ping:
  120. *
  121. * Reload counter one with the watchdog heartbeat. We don't bother reloading
  122. * the cascade counter.
  123. */
  124. static int wdt_ping(void)
  125. {
  126. /* Write a watchdog value */
  127. inb_p(WDT_DC); /* Disable watchdog */
  128. wdt_ctr_mode(1,2); /* Re-Program CTR1 for Mode 2: Rate Generator */
  129. wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
  130. outb_p(0, WDT_DC); /* Enable watchdog */
  131. return 0;
  132. }
  133. /**
  134. * wdt_set_heartbeat:
  135. * @t: the new heartbeat value that needs to be set.
  136. *
  137. * Set a new heartbeat value for the watchdog device. If the heartbeat value is
  138. * incorrect we keep the old value and return -EINVAL. If successfull we
  139. * return 0.
  140. */
  141. static int wdt_set_heartbeat(int t)
  142. {
  143. if ((t < 1) || (t > 65535))
  144. return -EINVAL;
  145. heartbeat = t;
  146. wd_heartbeat = t * 100;
  147. return 0;
  148. }
  149. /**
  150. * wdt_get_status:
  151. * @status: the new status.
  152. *
  153. * Extract the status information from a WDT watchdog device. There are
  154. * several board variants so we have to know which bits are valid. Some
  155. * bits default to one and some to zero in order to be maximally painful.
  156. *
  157. * we then map the bits onto the status ioctl flags.
  158. */
  159. static int wdt_get_status(int *status)
  160. {
  161. unsigned char new_status=inb_p(WDT_SR);
  162. *status=0;
  163. if (new_status & WDC_SR_ISOI0)
  164. *status |= WDIOF_EXTERN1;
  165. if (new_status & WDC_SR_ISII1)
  166. *status |= WDIOF_EXTERN2;
  167. #ifdef CONFIG_WDT_501
  168. if (!(new_status & WDC_SR_TGOOD))
  169. *status |= WDIOF_OVERHEAT;
  170. if (!(new_status & WDC_SR_PSUOVER))
  171. *status |= WDIOF_POWEROVER;
  172. if (!(new_status & WDC_SR_PSUUNDR))
  173. *status |= WDIOF_POWERUNDER;
  174. if (tachometer) {
  175. if (!(new_status & WDC_SR_FANGOOD))
  176. *status |= WDIOF_FANFAULT;
  177. }
  178. #endif /* CONFIG_WDT_501 */
  179. return 0;
  180. }
  181. #ifdef CONFIG_WDT_501
  182. /**
  183. * wdt_get_temperature:
  184. *
  185. * Reports the temperature in degrees Fahrenheit. The API is in
  186. * farenheit. It was designed by an imperial measurement luddite.
  187. */
  188. static int wdt_get_temperature(int *temperature)
  189. {
  190. unsigned short c=inb_p(WDT_RT);
  191. *temperature = (c * 11 / 15) + 7;
  192. return 0;
  193. }
  194. #endif /* CONFIG_WDT_501 */
  195. /**
  196. * wdt_interrupt:
  197. * @irq: Interrupt number
  198. * @dev_id: Unused as we don't allow multiple devices.
  199. *
  200. * Handle an interrupt from the board. These are raised when the status
  201. * map changes in what the board considers an interesting way. That means
  202. * a failure condition occurring.
  203. */
  204. static irqreturn_t wdt_interrupt(int irq, void *dev_id)
  205. {
  206. /*
  207. * Read the status register see what is up and
  208. * then printk it.
  209. */
  210. unsigned char status=inb_p(WDT_SR);
  211. printk(KERN_CRIT "WDT status %d\n", status);
  212. #ifdef CONFIG_WDT_501
  213. if (!(status & WDC_SR_TGOOD))
  214. printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
  215. if (!(status & WDC_SR_PSUOVER))
  216. printk(KERN_CRIT "PSU over voltage.\n");
  217. if (!(status & WDC_SR_PSUUNDR))
  218. printk(KERN_CRIT "PSU under voltage.\n");
  219. if (tachometer) {
  220. if (!(status & WDC_SR_FANGOOD))
  221. printk(KERN_CRIT "Possible fan fault.\n");
  222. }
  223. #endif /* CONFIG_WDT_501 */
  224. if (!(status & WDC_SR_WCCR))
  225. #ifdef SOFTWARE_REBOOT
  226. #ifdef ONLY_TESTING
  227. printk(KERN_CRIT "Would Reboot.\n");
  228. #else
  229. printk(KERN_CRIT "Initiating system reboot.\n");
  230. emergency_restart();
  231. #endif
  232. #else
  233. printk(KERN_CRIT "Reset in 5ms.\n");
  234. #endif
  235. return IRQ_HANDLED;
  236. }
  237. /**
  238. * wdt_write:
  239. * @file: file handle to the watchdog
  240. * @buf: buffer to write (unused as data does not matter here
  241. * @count: count of bytes
  242. * @ppos: pointer to the position to write. No seeks allowed
  243. *
  244. * A write to a watchdog device is defined as a keepalive signal. Any
  245. * write of data will do, as we we don't define content meaning.
  246. */
  247. static ssize_t wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  248. {
  249. if(count) {
  250. if (!nowayout) {
  251. size_t i;
  252. /* In case it was set long ago */
  253. expect_close = 0;
  254. for (i = 0; i != count; i++) {
  255. char c;
  256. if (get_user(c, buf + i))
  257. return -EFAULT;
  258. if (c == 'V')
  259. expect_close = 42;
  260. }
  261. }
  262. wdt_ping();
  263. }
  264. return count;
  265. }
  266. /**
  267. * wdt_ioctl:
  268. * @inode: inode of the device
  269. * @file: file handle to the device
  270. * @cmd: watchdog command
  271. * @arg: argument pointer
  272. *
  273. * The watchdog API defines a common set of functions for all watchdogs
  274. * according to their available features. We only actually usefully support
  275. * querying capabilities and current status.
  276. */
  277. static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  278. unsigned long arg)
  279. {
  280. void __user *argp = (void __user *)arg;
  281. int __user *p = argp;
  282. int new_heartbeat;
  283. int status;
  284. static struct watchdog_info ident = {
  285. .options = WDIOF_SETTIMEOUT|
  286. WDIOF_MAGICCLOSE|
  287. WDIOF_KEEPALIVEPING,
  288. .firmware_version = 1,
  289. .identity = "WDT500/501",
  290. };
  291. /* Add options according to the card we have */
  292. ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
  293. #ifdef CONFIG_WDT_501
  294. ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
  295. if (tachometer)
  296. ident.options |= WDIOF_FANFAULT;
  297. #endif /* CONFIG_WDT_501 */
  298. switch(cmd)
  299. {
  300. default:
  301. return -ENOTTY;
  302. case WDIOC_GETSUPPORT:
  303. return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
  304. case WDIOC_GETSTATUS:
  305. wdt_get_status(&status);
  306. return put_user(status, p);
  307. case WDIOC_GETBOOTSTATUS:
  308. return put_user(0, p);
  309. case WDIOC_KEEPALIVE:
  310. wdt_ping();
  311. return 0;
  312. case WDIOC_SETTIMEOUT:
  313. if (get_user(new_heartbeat, p))
  314. return -EFAULT;
  315. if (wdt_set_heartbeat(new_heartbeat))
  316. return -EINVAL;
  317. wdt_ping();
  318. /* Fall */
  319. case WDIOC_GETTIMEOUT:
  320. return put_user(heartbeat, p);
  321. }
  322. }
  323. /**
  324. * wdt_open:
  325. * @inode: inode of device
  326. * @file: file handle to device
  327. *
  328. * The watchdog device has been opened. The watchdog device is single
  329. * open and on opening we load the counters. Counter zero is a 100Hz
  330. * cascade, into counter 1 which downcounts to reboot. When the counter
  331. * triggers counter 2 downcounts the length of the reset pulse which
  332. * set set to be as long as possible.
  333. */
  334. static int wdt_open(struct inode *inode, struct file *file)
  335. {
  336. if(test_and_set_bit(0, &wdt_is_open))
  337. return -EBUSY;
  338. /*
  339. * Activate
  340. */
  341. wdt_start();
  342. return nonseekable_open(inode, file);
  343. }
  344. /**
  345. * wdt_release:
  346. * @inode: inode to board
  347. * @file: file handle to board
  348. *
  349. * The watchdog has a configurable API. There is a religious dispute
  350. * between people who want their watchdog to be able to shut down and
  351. * those who want to be sure if the watchdog manager dies the machine
  352. * reboots. In the former case we disable the counters, in the latter
  353. * case you have to open it again very soon.
  354. */
  355. static int wdt_release(struct inode *inode, struct file *file)
  356. {
  357. if (expect_close == 42) {
  358. wdt_stop();
  359. clear_bit(0, &wdt_is_open);
  360. } else {
  361. printk(KERN_CRIT "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  362. wdt_ping();
  363. }
  364. expect_close = 0;
  365. return 0;
  366. }
  367. #ifdef CONFIG_WDT_501
  368. /**
  369. * wdt_temp_read:
  370. * @file: file handle to the watchdog board
  371. * @buf: buffer to write 1 byte into
  372. * @count: length of buffer
  373. * @ptr: offset (no seek allowed)
  374. *
  375. * Temp_read reports the temperature in degrees Fahrenheit. The API is in
  376. * farenheit. It was designed by an imperial measurement luddite.
  377. */
  378. static ssize_t wdt_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  379. {
  380. int temperature;
  381. if (wdt_get_temperature(&temperature))
  382. return -EFAULT;
  383. if (copy_to_user (buf, &temperature, 1))
  384. return -EFAULT;
  385. return 1;
  386. }
  387. /**
  388. * wdt_temp_open:
  389. * @inode: inode of device
  390. * @file: file handle to device
  391. *
  392. * The temperature device has been opened.
  393. */
  394. static int wdt_temp_open(struct inode *inode, struct file *file)
  395. {
  396. return nonseekable_open(inode, file);
  397. }
  398. /**
  399. * wdt_temp_release:
  400. * @inode: inode to board
  401. * @file: file handle to board
  402. *
  403. * The temperature device has been closed.
  404. */
  405. static int wdt_temp_release(struct inode *inode, struct file *file)
  406. {
  407. return 0;
  408. }
  409. #endif /* CONFIG_WDT_501 */
  410. /**
  411. * notify_sys:
  412. * @this: our notifier block
  413. * @code: the event being reported
  414. * @unused: unused
  415. *
  416. * Our notifier is called on system shutdowns. We want to turn the card
  417. * off at reboot otherwise the machine will reboot again during memory
  418. * test or worse yet during the following fsck. This would suck, in fact
  419. * trust me - if it happens it does suck.
  420. */
  421. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  422. void *unused)
  423. {
  424. if(code==SYS_DOWN || code==SYS_HALT) {
  425. /* Turn the card off */
  426. wdt_stop();
  427. }
  428. return NOTIFY_DONE;
  429. }
  430. /*
  431. * Kernel Interfaces
  432. */
  433. static const struct file_operations wdt_fops = {
  434. .owner = THIS_MODULE,
  435. .llseek = no_llseek,
  436. .write = wdt_write,
  437. .ioctl = wdt_ioctl,
  438. .open = wdt_open,
  439. .release = wdt_release,
  440. };
  441. static struct miscdevice wdt_miscdev = {
  442. .minor = WATCHDOG_MINOR,
  443. .name = "watchdog",
  444. .fops = &wdt_fops,
  445. };
  446. #ifdef CONFIG_WDT_501
  447. static const struct file_operations wdt_temp_fops = {
  448. .owner = THIS_MODULE,
  449. .llseek = no_llseek,
  450. .read = wdt_temp_read,
  451. .open = wdt_temp_open,
  452. .release = wdt_temp_release,
  453. };
  454. static struct miscdevice temp_miscdev = {
  455. .minor = TEMP_MINOR,
  456. .name = "temperature",
  457. .fops = &wdt_temp_fops,
  458. };
  459. #endif /* CONFIG_WDT_501 */
  460. /*
  461. * The WDT card needs to learn about soft shutdowns in order to
  462. * turn the timebomb registers off.
  463. */
  464. static struct notifier_block wdt_notifier = {
  465. .notifier_call = wdt_notify_sys,
  466. };
  467. /**
  468. * cleanup_module:
  469. *
  470. * Unload the watchdog. You cannot do this with any file handles open.
  471. * If your watchdog is set to continue ticking on close and you unload
  472. * it, well it keeps ticking. We won't get the interrupt but the board
  473. * will not touch PC memory so all is fine. You just have to load a new
  474. * module in 60 seconds or reboot.
  475. */
  476. static void __exit wdt_exit(void)
  477. {
  478. misc_deregister(&wdt_miscdev);
  479. #ifdef CONFIG_WDT_501
  480. misc_deregister(&temp_miscdev);
  481. #endif /* CONFIG_WDT_501 */
  482. unregister_reboot_notifier(&wdt_notifier);
  483. free_irq(irq, NULL);
  484. release_region(io,8);
  485. }
  486. /**
  487. * wdt_init:
  488. *
  489. * Set up the WDT watchdog board. All we have to do is grab the
  490. * resources we require and bitch if anyone beat us to them.
  491. * The open() function will actually kick the board off.
  492. */
  493. static int __init wdt_init(void)
  494. {
  495. int ret;
  496. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  497. if (wdt_set_heartbeat(heartbeat)) {
  498. wdt_set_heartbeat(WD_TIMO);
  499. printk(KERN_INFO "wdt: heartbeat value must be 0<heartbeat<65536, using %d\n",
  500. WD_TIMO);
  501. }
  502. if (!request_region(io, 8, "wdt501p")) {
  503. printk(KERN_ERR "wdt: I/O address 0x%04x already in use\n", io);
  504. ret = -EBUSY;
  505. goto out;
  506. }
  507. ret = request_irq(irq, wdt_interrupt, IRQF_DISABLED, "wdt501p", NULL);
  508. if(ret) {
  509. printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
  510. goto outreg;
  511. }
  512. ret = register_reboot_notifier(&wdt_notifier);
  513. if(ret) {
  514. printk(KERN_ERR "wdt: cannot register reboot notifier (err=%d)\n", ret);
  515. goto outirq;
  516. }
  517. #ifdef CONFIG_WDT_501
  518. ret = misc_register(&temp_miscdev);
  519. if (ret) {
  520. printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  521. TEMP_MINOR, ret);
  522. goto outrbt;
  523. }
  524. #endif /* CONFIG_WDT_501 */
  525. ret = misc_register(&wdt_miscdev);
  526. if (ret) {
  527. printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  528. WATCHDOG_MINOR, ret);
  529. goto outmisc;
  530. }
  531. ret = 0;
  532. printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
  533. io, irq, heartbeat, nowayout);
  534. #ifdef CONFIG_WDT_501
  535. printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
  536. #endif /* CONFIG_WDT_501 */
  537. out:
  538. return ret;
  539. outmisc:
  540. #ifdef CONFIG_WDT_501
  541. misc_deregister(&temp_miscdev);
  542. outrbt:
  543. #endif /* CONFIG_WDT_501 */
  544. unregister_reboot_notifier(&wdt_notifier);
  545. outirq:
  546. free_irq(irq, NULL);
  547. outreg:
  548. release_region(io,8);
  549. goto out;
  550. }
  551. module_init(wdt_init);
  552. module_exit(wdt_exit);
  553. MODULE_AUTHOR("Alan Cox");
  554. MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
  555. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  556. MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  557. MODULE_LICENSE("GPL");