wdt.c 15 KB

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