w83977f_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * W83977F Watchdog Timer Driver for Winbond W83977F I/O Chip
  4. *
  5. * (c) Copyright 2005 Jose Goncalves <jose.goncalves@inov.pt>
  6. *
  7. * Based on w83877f_wdt.c by Scott Jennings,
  8. * and wdt977.c by Woody Suwalski
  9. *
  10. * -----------------------
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/init.h>
  20. #include <linux/ioport.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/notifier.h>
  23. #include <linux/reboot.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. #define WATCHDOG_VERSION "1.00"
  27. #define WATCHDOG_NAME "W83977F WDT"
  28. #define IO_INDEX_PORT 0x3F0
  29. #define IO_DATA_PORT (IO_INDEX_PORT+1)
  30. #define UNLOCK_DATA 0x87
  31. #define LOCK_DATA 0xAA
  32. #define DEVICE_REGISTER 0x07
  33. #define DEFAULT_TIMEOUT 45 /* default timeout in seconds */
  34. static int timeout = DEFAULT_TIMEOUT;
  35. static int timeoutW; /* timeout in watchdog counter units */
  36. static unsigned long timer_alive;
  37. static int testmode;
  38. static char expect_close;
  39. static DEFINE_SPINLOCK(spinlock);
  40. module_param(timeout, int, 0);
  41. MODULE_PARM_DESC(timeout,
  42. "Watchdog timeout in seconds (15..7635), default="
  43. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  44. module_param(testmode, int, 0);
  45. MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
  46. static bool nowayout = WATCHDOG_NOWAYOUT;
  47. module_param(nowayout, bool, 0);
  48. MODULE_PARM_DESC(nowayout,
  49. "Watchdog cannot be stopped once started (default="
  50. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  51. /*
  52. * Start the watchdog
  53. */
  54. static int wdt_start(void)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&spinlock, flags);
  58. /* Unlock the SuperIO chip */
  59. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  60. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  61. /*
  62. * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
  63. * F2 has the timeout in watchdog counter units.
  64. * F3 is set to enable watchdog LED blink at timeout.
  65. * F4 is used to just clear the TIMEOUT'ed state (bit 0).
  66. */
  67. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  68. outb_p(0x08, IO_DATA_PORT);
  69. outb_p(0xF2, IO_INDEX_PORT);
  70. outb_p(timeoutW, IO_DATA_PORT);
  71. outb_p(0xF3, IO_INDEX_PORT);
  72. outb_p(0x08, IO_DATA_PORT);
  73. outb_p(0xF4, IO_INDEX_PORT);
  74. outb_p(0x00, IO_DATA_PORT);
  75. /* Set device Aux2 active */
  76. outb_p(0x30, IO_INDEX_PORT);
  77. outb_p(0x01, IO_DATA_PORT);
  78. /*
  79. * Select device Aux1 (dev=7) to set GP16 as the watchdog output
  80. * (in reg E6) and GP13 as the watchdog LED output (in reg E3).
  81. * Map GP16 at pin 119.
  82. * In test mode watch the bit 0 on F4 to indicate "triggered" or
  83. * check watchdog LED on SBC.
  84. */
  85. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  86. outb_p(0x07, IO_DATA_PORT);
  87. if (!testmode) {
  88. unsigned pin_map;
  89. outb_p(0xE6, IO_INDEX_PORT);
  90. outb_p(0x0A, IO_DATA_PORT);
  91. outb_p(0x2C, IO_INDEX_PORT);
  92. pin_map = inb_p(IO_DATA_PORT);
  93. pin_map |= 0x10;
  94. pin_map &= ~(0x20);
  95. outb_p(0x2C, IO_INDEX_PORT);
  96. outb_p(pin_map, IO_DATA_PORT);
  97. }
  98. outb_p(0xE3, IO_INDEX_PORT);
  99. outb_p(0x08, IO_DATA_PORT);
  100. /* Set device Aux1 active */
  101. outb_p(0x30, IO_INDEX_PORT);
  102. outb_p(0x01, IO_DATA_PORT);
  103. /* Lock the SuperIO chip */
  104. outb_p(LOCK_DATA, IO_INDEX_PORT);
  105. spin_unlock_irqrestore(&spinlock, flags);
  106. pr_info("activated\n");
  107. return 0;
  108. }
  109. /*
  110. * Stop the watchdog
  111. */
  112. static int wdt_stop(void)
  113. {
  114. unsigned long flags;
  115. spin_lock_irqsave(&spinlock, flags);
  116. /* Unlock the SuperIO chip */
  117. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  118. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  119. /*
  120. * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
  121. * F2 is reset to its default value (watchdog timer disabled).
  122. * F3 is reset to its default state.
  123. * F4 clears the TIMEOUT'ed state (bit 0) - back to default.
  124. */
  125. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  126. outb_p(0x08, IO_DATA_PORT);
  127. outb_p(0xF2, IO_INDEX_PORT);
  128. outb_p(0xFF, IO_DATA_PORT);
  129. outb_p(0xF3, IO_INDEX_PORT);
  130. outb_p(0x00, IO_DATA_PORT);
  131. outb_p(0xF4, IO_INDEX_PORT);
  132. outb_p(0x00, IO_DATA_PORT);
  133. outb_p(0xF2, IO_INDEX_PORT);
  134. outb_p(0x00, IO_DATA_PORT);
  135. /*
  136. * Select device Aux1 (dev=7) to set GP16 (in reg E6) and
  137. * Gp13 (in reg E3) as inputs.
  138. */
  139. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  140. outb_p(0x07, IO_DATA_PORT);
  141. if (!testmode) {
  142. outb_p(0xE6, IO_INDEX_PORT);
  143. outb_p(0x01, IO_DATA_PORT);
  144. }
  145. outb_p(0xE3, IO_INDEX_PORT);
  146. outb_p(0x01, IO_DATA_PORT);
  147. /* Lock the SuperIO chip */
  148. outb_p(LOCK_DATA, IO_INDEX_PORT);
  149. spin_unlock_irqrestore(&spinlock, flags);
  150. pr_info("shutdown\n");
  151. return 0;
  152. }
  153. /*
  154. * Send a keepalive ping to the watchdog
  155. * This is done by simply re-writing the timeout to reg. 0xF2
  156. */
  157. static int wdt_keepalive(void)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&spinlock, flags);
  161. /* Unlock the SuperIO chip */
  162. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  163. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  164. /* Select device Aux2 (device=8) to kick watchdog reg F2 */
  165. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  166. outb_p(0x08, IO_DATA_PORT);
  167. outb_p(0xF2, IO_INDEX_PORT);
  168. outb_p(timeoutW, IO_DATA_PORT);
  169. /* Lock the SuperIO chip */
  170. outb_p(LOCK_DATA, IO_INDEX_PORT);
  171. spin_unlock_irqrestore(&spinlock, flags);
  172. return 0;
  173. }
  174. /*
  175. * Set the watchdog timeout value
  176. */
  177. static int wdt_set_timeout(int t)
  178. {
  179. unsigned int tmrval;
  180. /*
  181. * Convert seconds to watchdog counter time units, rounding up.
  182. * On PCM-5335 watchdog units are 30 seconds/step with 15 sec startup
  183. * value. This information is supplied in the PCM-5335 manual and was
  184. * checked by me on a real board. This is a bit strange because W83977f
  185. * datasheet says counter unit is in minutes!
  186. */
  187. if (t < 15)
  188. return -EINVAL;
  189. tmrval = ((t + 15) + 29) / 30;
  190. if (tmrval > 255)
  191. return -EINVAL;
  192. /*
  193. * timeout is the timeout in seconds,
  194. * timeoutW is the timeout in watchdog counter units.
  195. */
  196. timeoutW = tmrval;
  197. timeout = (timeoutW * 30) - 15;
  198. return 0;
  199. }
  200. /*
  201. * Get the watchdog status
  202. */
  203. static int wdt_get_status(int *status)
  204. {
  205. int new_status;
  206. unsigned long flags;
  207. spin_lock_irqsave(&spinlock, flags);
  208. /* Unlock the SuperIO chip */
  209. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  210. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  211. /* Select device Aux2 (device=8) to read watchdog reg F4 */
  212. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  213. outb_p(0x08, IO_DATA_PORT);
  214. outb_p(0xF4, IO_INDEX_PORT);
  215. new_status = inb_p(IO_DATA_PORT);
  216. /* Lock the SuperIO chip */
  217. outb_p(LOCK_DATA, IO_INDEX_PORT);
  218. spin_unlock_irqrestore(&spinlock, flags);
  219. *status = 0;
  220. if (new_status & 1)
  221. *status |= WDIOF_CARDRESET;
  222. return 0;
  223. }
  224. /*
  225. * /dev/watchdog handling
  226. */
  227. static int wdt_open(struct inode *inode, struct file *file)
  228. {
  229. /* If the watchdog is alive we don't need to start it again */
  230. if (test_and_set_bit(0, &timer_alive))
  231. return -EBUSY;
  232. if (nowayout)
  233. __module_get(THIS_MODULE);
  234. wdt_start();
  235. return stream_open(inode, file);
  236. }
  237. static int wdt_release(struct inode *inode, struct file *file)
  238. {
  239. /*
  240. * Shut off the timer.
  241. * Lock it in if it's a module and we set nowayout
  242. */
  243. if (expect_close == 42) {
  244. wdt_stop();
  245. clear_bit(0, &timer_alive);
  246. } else {
  247. wdt_keepalive();
  248. pr_crit("unexpected close, not stopping watchdog!\n");
  249. }
  250. expect_close = 0;
  251. return 0;
  252. }
  253. /*
  254. * wdt_write:
  255. * @file: file handle to the watchdog
  256. * @buf: buffer to write (unused as data does not matter here
  257. * @count: count of bytes
  258. * @ppos: pointer to the position to write. No seeks allowed
  259. *
  260. * A write to a watchdog device is defined as a keepalive signal. Any
  261. * write of data will do, as we we don't define content meaning.
  262. */
  263. static ssize_t wdt_write(struct file *file, const char __user *buf,
  264. size_t count, loff_t *ppos)
  265. {
  266. /* See if we got the magic character 'V' and reload the timer */
  267. if (count) {
  268. if (!nowayout) {
  269. size_t ofs;
  270. /* note: just in case someone wrote the
  271. magic character long ago */
  272. expect_close = 0;
  273. /* scan to see whether or not we got the
  274. magic character */
  275. for (ofs = 0; ofs != count; ofs++) {
  276. char c;
  277. if (get_user(c, buf + ofs))
  278. return -EFAULT;
  279. if (c == 'V')
  280. expect_close = 42;
  281. }
  282. }
  283. /* someone wrote to us, we should restart timer */
  284. wdt_keepalive();
  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.
  297. */
  298. static const struct watchdog_info ident = {
  299. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  300. .firmware_version = 1,
  301. .identity = WATCHDOG_NAME,
  302. };
  303. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  304. {
  305. int status;
  306. int new_options, retval = -EINVAL;
  307. int new_timeout;
  308. union {
  309. struct watchdog_info __user *ident;
  310. int __user *i;
  311. } uarg;
  312. uarg.i = (int __user *)arg;
  313. switch (cmd) {
  314. case WDIOC_GETSUPPORT:
  315. return copy_to_user(uarg.ident, &ident,
  316. sizeof(ident)) ? -EFAULT : 0;
  317. case WDIOC_GETSTATUS:
  318. wdt_get_status(&status);
  319. return put_user(status, uarg.i);
  320. case WDIOC_GETBOOTSTATUS:
  321. return put_user(0, uarg.i);
  322. case WDIOC_SETOPTIONS:
  323. if (get_user(new_options, uarg.i))
  324. return -EFAULT;
  325. if (new_options & WDIOS_DISABLECARD) {
  326. wdt_stop();
  327. retval = 0;
  328. }
  329. if (new_options & WDIOS_ENABLECARD) {
  330. wdt_start();
  331. retval = 0;
  332. }
  333. return retval;
  334. case WDIOC_KEEPALIVE:
  335. wdt_keepalive();
  336. return 0;
  337. case WDIOC_SETTIMEOUT:
  338. if (get_user(new_timeout, uarg.i))
  339. return -EFAULT;
  340. if (wdt_set_timeout(new_timeout))
  341. return -EINVAL;
  342. wdt_keepalive();
  343. /* Fall through */
  344. case WDIOC_GETTIMEOUT:
  345. return put_user(timeout, uarg.i);
  346. default:
  347. return -ENOTTY;
  348. }
  349. }
  350. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  351. void *unused)
  352. {
  353. if (code == SYS_DOWN || code == SYS_HALT)
  354. wdt_stop();
  355. return NOTIFY_DONE;
  356. }
  357. static const struct file_operations wdt_fops = {
  358. .owner = THIS_MODULE,
  359. .llseek = no_llseek,
  360. .write = wdt_write,
  361. .unlocked_ioctl = wdt_ioctl,
  362. .open = wdt_open,
  363. .release = wdt_release,
  364. };
  365. static struct miscdevice wdt_miscdev = {
  366. .minor = WATCHDOG_MINOR,
  367. .name = "watchdog",
  368. .fops = &wdt_fops,
  369. };
  370. static struct notifier_block wdt_notifier = {
  371. .notifier_call = wdt_notify_sys,
  372. };
  373. static int __init w83977f_wdt_init(void)
  374. {
  375. int rc;
  376. pr_info("driver v%s\n", WATCHDOG_VERSION);
  377. /*
  378. * Check that the timeout value is within it's range;
  379. * if not reset to the default
  380. */
  381. if (wdt_set_timeout(timeout)) {
  382. wdt_set_timeout(DEFAULT_TIMEOUT);
  383. pr_info("timeout value must be 15 <= timeout <= 7635, using %d\n",
  384. DEFAULT_TIMEOUT);
  385. }
  386. if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
  387. pr_err("I/O address 0x%04x already in use\n", IO_INDEX_PORT);
  388. rc = -EIO;
  389. goto err_out;
  390. }
  391. rc = register_reboot_notifier(&wdt_notifier);
  392. if (rc) {
  393. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  394. goto err_out_region;
  395. }
  396. rc = misc_register(&wdt_miscdev);
  397. if (rc) {
  398. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  399. wdt_miscdev.minor, rc);
  400. goto err_out_reboot;
  401. }
  402. pr_info("initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
  403. timeout, nowayout, testmode);
  404. return 0;
  405. err_out_reboot:
  406. unregister_reboot_notifier(&wdt_notifier);
  407. err_out_region:
  408. release_region(IO_INDEX_PORT, 2);
  409. err_out:
  410. return rc;
  411. }
  412. static void __exit w83977f_wdt_exit(void)
  413. {
  414. wdt_stop();
  415. misc_deregister(&wdt_miscdev);
  416. unregister_reboot_notifier(&wdt_notifier);
  417. release_region(IO_INDEX_PORT, 2);
  418. }
  419. module_init(w83977f_wdt_init);
  420. module_exit(w83977f_wdt_exit);
  421. MODULE_AUTHOR("Jose Goncalves <jose.goncalves@inov.pt>");
  422. MODULE_DESCRIPTION("Driver for watchdog timer in W83977F I/O chip");
  423. MODULE_LICENSE("GPL");