wdt.c 15 KB

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