wdt.c 15 KB

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