wdt.c 15 KB

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