wdt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. #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 <linux/io.h>
  46. #include <linux/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,
  59. "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  60. __MODULE_STRING(WD_TIMO) ")");
  61. static int nowayout = WATCHDOG_NOWAYOUT;
  62. module_param(nowayout, int, 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. printk(KERN_CRIT "Overheat alarm.(%d)\n", inb_p(WDT_RT));
  224. if (!(status & WDC_SR_PSUOVER))
  225. printk(KERN_CRIT "PSU over voltage.\n");
  226. if (!(status & WDC_SR_PSUUNDR))
  227. printk(KERN_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. printk(KERN_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. printk(KERN_CRIT "Possible fan fault.\n");
  253. }
  254. }
  255. if (!(status & WDC_SR_WCCR)) {
  256. #ifdef SOFTWARE_REBOOT
  257. #ifdef ONLY_TESTING
  258. printk(KERN_CRIT "Would Reboot.\n");
  259. #else
  260. printk(KERN_CRIT "Initiating system reboot.\n");
  261. emergency_restart();
  262. #endif
  263. #else
  264. printk(KERN_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. printk(KERN_CRIT
  394. "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  395. wdt_ping();
  396. }
  397. expect_close = 0;
  398. return 0;
  399. }
  400. /**
  401. * wdt_temp_read:
  402. * @file: file handle to the watchdog board
  403. * @buf: buffer to write 1 byte into
  404. * @count: length of buffer
  405. * @ptr: offset (no seek allowed)
  406. *
  407. * Temp_read reports the temperature in degrees Fahrenheit. The API is in
  408. * farenheit. It was designed by an imperial measurement luddite.
  409. */
  410. static ssize_t wdt_temp_read(struct file *file, char __user *buf,
  411. size_t count, loff_t *ptr)
  412. {
  413. int temperature = wdt_get_temperature();
  414. if (copy_to_user(buf, &temperature, 1))
  415. return -EFAULT;
  416. return 1;
  417. }
  418. /**
  419. * wdt_temp_open:
  420. * @inode: inode of device
  421. * @file: file handle to device
  422. *
  423. * The temperature device has been opened.
  424. */
  425. static int wdt_temp_open(struct inode *inode, struct file *file)
  426. {
  427. return nonseekable_open(inode, file);
  428. }
  429. /**
  430. * wdt_temp_release:
  431. * @inode: inode to board
  432. * @file: file handle to board
  433. *
  434. * The temperature device has been closed.
  435. */
  436. static int wdt_temp_release(struct inode *inode, struct file *file)
  437. {
  438. return 0;
  439. }
  440. /**
  441. * notify_sys:
  442. * @this: our notifier block
  443. * @code: the event being reported
  444. * @unused: unused
  445. *
  446. * Our notifier is called on system shutdowns. We want to turn the card
  447. * off at reboot otherwise the machine will reboot again during memory
  448. * test or worse yet during the following fsck. This would suck, in fact
  449. * trust me - if it happens it does suck.
  450. */
  451. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  452. void *unused)
  453. {
  454. if (code == SYS_DOWN || code == SYS_HALT)
  455. wdt_stop();
  456. return NOTIFY_DONE;
  457. }
  458. /*
  459. * Kernel Interfaces
  460. */
  461. static const struct file_operations wdt_fops = {
  462. .owner = THIS_MODULE,
  463. .llseek = no_llseek,
  464. .write = wdt_write,
  465. .unlocked_ioctl = wdt_ioctl,
  466. .open = wdt_open,
  467. .release = wdt_release,
  468. };
  469. static struct miscdevice wdt_miscdev = {
  470. .minor = WATCHDOG_MINOR,
  471. .name = "watchdog",
  472. .fops = &wdt_fops,
  473. };
  474. static const struct file_operations wdt_temp_fops = {
  475. .owner = THIS_MODULE,
  476. .llseek = no_llseek,
  477. .read = wdt_temp_read,
  478. .open = wdt_temp_open,
  479. .release = wdt_temp_release,
  480. };
  481. static struct miscdevice temp_miscdev = {
  482. .minor = TEMP_MINOR,
  483. .name = "temperature",
  484. .fops = &wdt_temp_fops,
  485. };
  486. /*
  487. * The WDT card needs to learn about soft shutdowns in order to
  488. * turn the timebomb registers off.
  489. */
  490. static struct notifier_block wdt_notifier = {
  491. .notifier_call = wdt_notify_sys,
  492. };
  493. /**
  494. * cleanup_module:
  495. *
  496. * Unload the watchdog. You cannot do this with any file handles open.
  497. * If your watchdog is set to continue ticking on close and you unload
  498. * it, well it keeps ticking. We won't get the interrupt but the board
  499. * will not touch PC memory so all is fine. You just have to load a new
  500. * module in 60 seconds or reboot.
  501. */
  502. static void __exit wdt_exit(void)
  503. {
  504. misc_deregister(&wdt_miscdev);
  505. if (type == 501)
  506. misc_deregister(&temp_miscdev);
  507. unregister_reboot_notifier(&wdt_notifier);
  508. free_irq(irq, NULL);
  509. release_region(io, 8);
  510. }
  511. /**
  512. * wdt_init:
  513. *
  514. * Set up the WDT watchdog board. All we have to do is grab the
  515. * resources we require and bitch if anyone beat us to them.
  516. * The open() function will actually kick the board off.
  517. */
  518. static int __init wdt_init(void)
  519. {
  520. int ret;
  521. if (type != 500 && type != 501) {
  522. printk(KERN_ERR "wdt: unknown card type '%d'.\n", type);
  523. return -ENODEV;
  524. }
  525. /* Check that the heartbeat value is within it's range;
  526. if not reset to the default */
  527. if (wdt_set_heartbeat(heartbeat)) {
  528. wdt_set_heartbeat(WD_TIMO);
  529. printk(KERN_INFO "wdt: heartbeat value must be "
  530. "0 < heartbeat < 65536, using %d\n", WD_TIMO);
  531. }
  532. if (!request_region(io, 8, "wdt501p")) {
  533. printk(KERN_ERR
  534. "wdt: I/O address 0x%04x already in use\n", io);
  535. ret = -EBUSY;
  536. goto out;
  537. }
  538. ret = request_irq(irq, wdt_interrupt, IRQF_DISABLED, "wdt501p", NULL);
  539. if (ret) {
  540. printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
  541. goto outreg;
  542. }
  543. ret = register_reboot_notifier(&wdt_notifier);
  544. if (ret) {
  545. printk(KERN_ERR
  546. "wdt: cannot register reboot notifier (err=%d)\n", ret);
  547. goto outirq;
  548. }
  549. if (type == 501) {
  550. ret = misc_register(&temp_miscdev);
  551. if (ret) {
  552. printk(KERN_ERR "wdt: cannot register miscdev "
  553. "on minor=%d (err=%d)\n", TEMP_MINOR, ret);
  554. goto outrbt;
  555. }
  556. }
  557. ret = misc_register(&wdt_miscdev);
  558. if (ret) {
  559. printk(KERN_ERR
  560. "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  561. WATCHDOG_MINOR, ret);
  562. goto outmisc;
  563. }
  564. printk(KERN_INFO "WDT500/501-P driver 0.10 "
  565. "at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
  566. io, irq, heartbeat, nowayout);
  567. if (type == 501)
  568. printk(KERN_INFO "wdt: Fan Tachometer is %s\n",
  569. (tachometer ? "Enabled" : "Disabled"));
  570. return 0;
  571. outmisc:
  572. if (type == 501)
  573. misc_deregister(&temp_miscdev);
  574. outrbt:
  575. unregister_reboot_notifier(&wdt_notifier);
  576. outirq:
  577. free_irq(irq, NULL);
  578. outreg:
  579. release_region(io, 8);
  580. out:
  581. return ret;
  582. }
  583. module_init(wdt_init);
  584. module_exit(wdt_exit);
  585. MODULE_AUTHOR("Alan Cox");
  586. MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
  587. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  588. MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  589. MODULE_LICENSE("GPL");