wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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=" __MODULE_STRING(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. }
  236. return IRQ_HANDLED;
  237. }
  238. /**
  239. * wdt_write:
  240. * @file: file handle to the watchdog
  241. * @buf: buffer to write (unused as data does not matter here
  242. * @count: count of bytes
  243. * @ppos: pointer to the position to write. No seeks allowed
  244. *
  245. * A write to a watchdog device is defined as a keepalive signal. Any
  246. * write of data will do, as we we don't define content meaning.
  247. */
  248. static ssize_t wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  249. {
  250. if(count) {
  251. if (!nowayout) {
  252. size_t i;
  253. /* In case it was set long ago */
  254. expect_close = 0;
  255. for (i = 0; i != count; i++) {
  256. char c;
  257. if (get_user(c, buf + i))
  258. return -EFAULT;
  259. if (c == 'V')
  260. expect_close = 42;
  261. }
  262. }
  263. wdt_ping();
  264. }
  265. return count;
  266. }
  267. /**
  268. * wdt_ioctl:
  269. * @inode: inode of the device
  270. * @file: file handle to the device
  271. * @cmd: watchdog command
  272. * @arg: argument pointer
  273. *
  274. * The watchdog API defines a common set of functions for all watchdogs
  275. * according to their available features. We only actually usefully support
  276. * querying capabilities and current status.
  277. */
  278. static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  279. unsigned long arg)
  280. {
  281. void __user *argp = (void __user *)arg;
  282. int __user *p = argp;
  283. int new_heartbeat;
  284. int status;
  285. static struct watchdog_info ident = {
  286. .options = WDIOF_SETTIMEOUT|
  287. WDIOF_MAGICCLOSE|
  288. WDIOF_KEEPALIVEPING,
  289. .firmware_version = 1,
  290. .identity = "WDT500/501",
  291. };
  292. /* Add options according to the card we have */
  293. ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
  294. #ifdef CONFIG_WDT_501
  295. ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
  296. if (tachometer)
  297. ident.options |= WDIOF_FANFAULT;
  298. #endif /* CONFIG_WDT_501 */
  299. switch(cmd)
  300. {
  301. default:
  302. return -ENOTTY;
  303. case WDIOC_GETSUPPORT:
  304. return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
  305. case WDIOC_GETSTATUS:
  306. wdt_get_status(&status);
  307. return put_user(status, p);
  308. case WDIOC_GETBOOTSTATUS:
  309. return put_user(0, p);
  310. case WDIOC_KEEPALIVE:
  311. wdt_ping();
  312. return 0;
  313. case WDIOC_SETTIMEOUT:
  314. if (get_user(new_heartbeat, p))
  315. return -EFAULT;
  316. if (wdt_set_heartbeat(new_heartbeat))
  317. return -EINVAL;
  318. wdt_ping();
  319. /* Fall */
  320. case WDIOC_GETTIMEOUT:
  321. return put_user(heartbeat, p);
  322. }
  323. }
  324. /**
  325. * wdt_open:
  326. * @inode: inode of device
  327. * @file: file handle to device
  328. *
  329. * The watchdog device has been opened. The watchdog device is single
  330. * open and on opening we load the counters. Counter zero is a 100Hz
  331. * cascade, into counter 1 which downcounts to reboot. When the counter
  332. * triggers counter 2 downcounts the length of the reset pulse which
  333. * set set to be as long as possible.
  334. */
  335. static int wdt_open(struct inode *inode, struct file *file)
  336. {
  337. if(test_and_set_bit(0, &wdt_is_open))
  338. return -EBUSY;
  339. /*
  340. * Activate
  341. */
  342. wdt_start();
  343. return nonseekable_open(inode, file);
  344. }
  345. /**
  346. * wdt_release:
  347. * @inode: inode to board
  348. * @file: file handle to board
  349. *
  350. * The watchdog has a configurable API. There is a religious dispute
  351. * between people who want their watchdog to be able to shut down and
  352. * those who want to be sure if the watchdog manager dies the machine
  353. * reboots. In the former case we disable the counters, in the latter
  354. * case you have to open it again very soon.
  355. */
  356. static int wdt_release(struct inode *inode, struct file *file)
  357. {
  358. if (expect_close == 42) {
  359. wdt_stop();
  360. clear_bit(0, &wdt_is_open);
  361. } else {
  362. printk(KERN_CRIT "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  363. wdt_ping();
  364. }
  365. expect_close = 0;
  366. return 0;
  367. }
  368. #ifdef CONFIG_WDT_501
  369. /**
  370. * wdt_temp_read:
  371. * @file: file handle to the watchdog board
  372. * @buf: buffer to write 1 byte into
  373. * @count: length of buffer
  374. * @ptr: offset (no seek allowed)
  375. *
  376. * Temp_read reports the temperature in degrees Fahrenheit. The API is in
  377. * farenheit. It was designed by an imperial measurement luddite.
  378. */
  379. static ssize_t wdt_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  380. {
  381. int temperature;
  382. if (wdt_get_temperature(&temperature))
  383. return -EFAULT;
  384. if (copy_to_user (buf, &temperature, 1))
  385. return -EFAULT;
  386. return 1;
  387. }
  388. /**
  389. * wdt_temp_open:
  390. * @inode: inode of device
  391. * @file: file handle to device
  392. *
  393. * The temperature device has been opened.
  394. */
  395. static int wdt_temp_open(struct inode *inode, struct file *file)
  396. {
  397. return nonseekable_open(inode, file);
  398. }
  399. /**
  400. * wdt_temp_release:
  401. * @inode: inode to board
  402. * @file: file handle to board
  403. *
  404. * The temperature device has been closed.
  405. */
  406. static int wdt_temp_release(struct inode *inode, struct file *file)
  407. {
  408. return 0;
  409. }
  410. #endif /* CONFIG_WDT_501 */
  411. /**
  412. * notify_sys:
  413. * @this: our notifier block
  414. * @code: the event being reported
  415. * @unused: unused
  416. *
  417. * Our notifier is called on system shutdowns. We want to turn the card
  418. * off at reboot otherwise the machine will reboot again during memory
  419. * test or worse yet during the following fsck. This would suck, in fact
  420. * trust me - if it happens it does suck.
  421. */
  422. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  423. void *unused)
  424. {
  425. if(code==SYS_DOWN || code==SYS_HALT) {
  426. /* Turn the card off */
  427. wdt_stop();
  428. }
  429. return NOTIFY_DONE;
  430. }
  431. /*
  432. * Kernel Interfaces
  433. */
  434. static const struct file_operations wdt_fops = {
  435. .owner = THIS_MODULE,
  436. .llseek = no_llseek,
  437. .write = wdt_write,
  438. .ioctl = wdt_ioctl,
  439. .open = wdt_open,
  440. .release = wdt_release,
  441. };
  442. static struct miscdevice wdt_miscdev = {
  443. .minor = WATCHDOG_MINOR,
  444. .name = "watchdog",
  445. .fops = &wdt_fops,
  446. };
  447. #ifdef CONFIG_WDT_501
  448. static const struct file_operations wdt_temp_fops = {
  449. .owner = THIS_MODULE,
  450. .llseek = no_llseek,
  451. .read = wdt_temp_read,
  452. .open = wdt_temp_open,
  453. .release = wdt_temp_release,
  454. };
  455. static struct miscdevice temp_miscdev = {
  456. .minor = TEMP_MINOR,
  457. .name = "temperature",
  458. .fops = &wdt_temp_fops,
  459. };
  460. #endif /* CONFIG_WDT_501 */
  461. /*
  462. * The WDT card needs to learn about soft shutdowns in order to
  463. * turn the timebomb registers off.
  464. */
  465. static struct notifier_block wdt_notifier = {
  466. .notifier_call = wdt_notify_sys,
  467. };
  468. /**
  469. * cleanup_module:
  470. *
  471. * Unload the watchdog. You cannot do this with any file handles open.
  472. * If your watchdog is set to continue ticking on close and you unload
  473. * it, well it keeps ticking. We won't get the interrupt but the board
  474. * will not touch PC memory so all is fine. You just have to load a new
  475. * module in 60 seconds or reboot.
  476. */
  477. static void __exit wdt_exit(void)
  478. {
  479. misc_deregister(&wdt_miscdev);
  480. #ifdef CONFIG_WDT_501
  481. misc_deregister(&temp_miscdev);
  482. #endif /* CONFIG_WDT_501 */
  483. unregister_reboot_notifier(&wdt_notifier);
  484. free_irq(irq, NULL);
  485. release_region(io,8);
  486. }
  487. /**
  488. * wdt_init:
  489. *
  490. * Set up the WDT watchdog board. All we have to do is grab the
  491. * resources we require and bitch if anyone beat us to them.
  492. * The open() function will actually kick the board off.
  493. */
  494. static int __init wdt_init(void)
  495. {
  496. int ret;
  497. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  498. if (wdt_set_heartbeat(heartbeat)) {
  499. wdt_set_heartbeat(WD_TIMO);
  500. printk(KERN_INFO "wdt: heartbeat value must be 0<heartbeat<65536, using %d\n",
  501. WD_TIMO);
  502. }
  503. if (!request_region(io, 8, "wdt501p")) {
  504. printk(KERN_ERR "wdt: I/O address 0x%04x already in use\n", io);
  505. ret = -EBUSY;
  506. goto out;
  507. }
  508. ret = request_irq(irq, wdt_interrupt, IRQF_DISABLED, "wdt501p", NULL);
  509. if(ret) {
  510. printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
  511. goto outreg;
  512. }
  513. ret = register_reboot_notifier(&wdt_notifier);
  514. if(ret) {
  515. printk(KERN_ERR "wdt: cannot register reboot notifier (err=%d)\n", ret);
  516. goto outirq;
  517. }
  518. #ifdef CONFIG_WDT_501
  519. ret = misc_register(&temp_miscdev);
  520. if (ret) {
  521. printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  522. TEMP_MINOR, ret);
  523. goto outrbt;
  524. }
  525. #endif /* CONFIG_WDT_501 */
  526. ret = misc_register(&wdt_miscdev);
  527. if (ret) {
  528. printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  529. WATCHDOG_MINOR, ret);
  530. goto outmisc;
  531. }
  532. ret = 0;
  533. printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
  534. io, irq, heartbeat, nowayout);
  535. #ifdef CONFIG_WDT_501
  536. printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
  537. #endif /* CONFIG_WDT_501 */
  538. out:
  539. return ret;
  540. outmisc:
  541. #ifdef CONFIG_WDT_501
  542. misc_deregister(&temp_miscdev);
  543. outrbt:
  544. #endif /* CONFIG_WDT_501 */
  545. unregister_reboot_notifier(&wdt_notifier);
  546. outirq:
  547. free_irq(irq, NULL);
  548. outreg:
  549. release_region(io,8);
  550. goto out;
  551. }
  552. module_init(wdt_init);
  553. module_exit(wdt_exit);
  554. MODULE_AUTHOR("Alan Cox");
  555. MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
  556. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  557. MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  558. MODULE_LICENSE("GPL");