wdt.c 16 KB

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