wdt977.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip
  4. *
  5. * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
  6. *
  7. * -----------------------
  8. *
  9. * -----------------------
  10. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  11. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  12. * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
  13. * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
  14. * from minutes to seconds.
  15. * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
  16. * nwwatchdog_init.
  17. * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
  18. * remove limitiation to be used on
  19. * Netwinders only
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/init.h>
  29. #include <linux/ioport.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/notifier.h>
  32. #include <linux/reboot.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/mach-types.h>
  36. #define WATCHDOG_VERSION "0.04"
  37. #define WATCHDOG_NAME "Wdt977"
  38. #define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
  39. #define IO_DATA_PORT (IO_INDEX_PORT + 1)
  40. #define UNLOCK_DATA 0x87
  41. #define LOCK_DATA 0xAA
  42. #define DEVICE_REGISTER 0x07
  43. #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
  44. static int timeout = DEFAULT_TIMEOUT;
  45. static int timeoutM; /* timeout in minutes */
  46. static unsigned long timer_alive;
  47. static int testmode;
  48. static char expect_close;
  49. static DEFINE_SPINLOCK(spinlock);
  50. module_param(timeout, int, 0);
  51. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default="
  52. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  53. module_param(testmode, int, 0);
  54. MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
  55. static bool nowayout = WATCHDOG_NOWAYOUT;
  56. module_param(nowayout, bool, 0);
  57. MODULE_PARM_DESC(nowayout,
  58. "Watchdog cannot be stopped once started (default="
  59. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  60. /*
  61. * Start the watchdog
  62. */
  63. static int wdt977_start(void)
  64. {
  65. unsigned long flags;
  66. spin_lock_irqsave(&spinlock, flags);
  67. /* unlock the SuperIO chip */
  68. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  69. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  70. /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
  71. * F2 has the timeout in minutes
  72. * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
  73. * at timeout, and to reset timer on kbd/mouse activity (not impl.)
  74. * F4 is used to just clear the TIMEOUT'ed state (bit 0)
  75. */
  76. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  77. outb_p(0x08, IO_DATA_PORT);
  78. outb_p(0xF2, IO_INDEX_PORT);
  79. outb_p(timeoutM, IO_DATA_PORT);
  80. outb_p(0xF3, IO_INDEX_PORT);
  81. outb_p(0x00, IO_DATA_PORT); /* another setting is 0E for
  82. kbd/mouse/LED */
  83. outb_p(0xF4, IO_INDEX_PORT);
  84. outb_p(0x00, IO_DATA_PORT);
  85. /* At last select device Aux1 (dev=7) and set GP16 as a
  86. * watchdog output. In test mode watch the bit 1 on F4 to
  87. * indicate "triggered"
  88. */
  89. if (!testmode) {
  90. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  91. outb_p(0x07, IO_DATA_PORT);
  92. outb_p(0xE6, IO_INDEX_PORT);
  93. outb_p(0x08, IO_DATA_PORT);
  94. }
  95. /* lock the SuperIO chip */
  96. outb_p(LOCK_DATA, IO_INDEX_PORT);
  97. spin_unlock_irqrestore(&spinlock, flags);
  98. pr_info("activated\n");
  99. return 0;
  100. }
  101. /*
  102. * Stop the watchdog
  103. */
  104. static int wdt977_stop(void)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&spinlock, flags);
  108. /* unlock the SuperIO chip */
  109. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  110. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  111. /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
  112. * F3 is reset to its default state
  113. * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
  114. * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
  115. */
  116. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  117. outb_p(0x08, IO_DATA_PORT);
  118. outb_p(0xF2, IO_INDEX_PORT);
  119. outb_p(0xFF, IO_DATA_PORT);
  120. outb_p(0xF3, IO_INDEX_PORT);
  121. outb_p(0x00, IO_DATA_PORT);
  122. outb_p(0xF4, IO_INDEX_PORT);
  123. outb_p(0x00, IO_DATA_PORT);
  124. outb_p(0xF2, IO_INDEX_PORT);
  125. outb_p(0x00, IO_DATA_PORT);
  126. /* at last select device Aux1 (dev=7) and set
  127. GP16 as a watchdog output */
  128. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  129. outb_p(0x07, IO_DATA_PORT);
  130. outb_p(0xE6, IO_INDEX_PORT);
  131. outb_p(0x08, IO_DATA_PORT);
  132. /* lock the SuperIO chip */
  133. outb_p(LOCK_DATA, IO_INDEX_PORT);
  134. spin_unlock_irqrestore(&spinlock, flags);
  135. pr_info("shutdown\n");
  136. return 0;
  137. }
  138. /*
  139. * Send a keepalive ping to the watchdog
  140. * This is done by simply re-writing the timeout to reg. 0xF2
  141. */
  142. static int wdt977_keepalive(void)
  143. {
  144. unsigned long flags;
  145. spin_lock_irqsave(&spinlock, flags);
  146. /* unlock the SuperIO chip */
  147. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  148. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  149. /* select device Aux2 (device=8) and kicks watchdog reg F2 */
  150. /* F2 has the timeout in minutes */
  151. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  152. outb_p(0x08, IO_DATA_PORT);
  153. outb_p(0xF2, IO_INDEX_PORT);
  154. outb_p(timeoutM, IO_DATA_PORT);
  155. /* lock the SuperIO chip */
  156. outb_p(LOCK_DATA, IO_INDEX_PORT);
  157. spin_unlock_irqrestore(&spinlock, flags);
  158. return 0;
  159. }
  160. /*
  161. * Set the watchdog timeout value
  162. */
  163. static int wdt977_set_timeout(int t)
  164. {
  165. int tmrval;
  166. /* convert seconds to minutes, rounding up */
  167. tmrval = (t + 59) / 60;
  168. if (machine_is_netwinder()) {
  169. /* we have a hw bug somewhere, so each 977 minute is actually
  170. * only 30sec. This limits the max timeout to half of device
  171. * max of 255 minutes...
  172. */
  173. tmrval += tmrval;
  174. }
  175. if (tmrval < 1 || tmrval > 255)
  176. return -EINVAL;
  177. /* timeout is the timeout in seconds, timeoutM is
  178. the timeout in minutes) */
  179. timeout = t;
  180. timeoutM = tmrval;
  181. return 0;
  182. }
  183. /*
  184. * Get the watchdog status
  185. */
  186. static int wdt977_get_status(int *status)
  187. {
  188. int new_status;
  189. unsigned long flags;
  190. spin_lock_irqsave(&spinlock, flags);
  191. /* unlock the SuperIO chip */
  192. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  193. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  194. /* select device Aux2 (device=8) and read watchdog reg F4 */
  195. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  196. outb_p(0x08, IO_DATA_PORT);
  197. outb_p(0xF4, IO_INDEX_PORT);
  198. new_status = inb_p(IO_DATA_PORT);
  199. /* lock the SuperIO chip */
  200. outb_p(LOCK_DATA, IO_INDEX_PORT);
  201. spin_unlock_irqrestore(&spinlock, flags);
  202. *status = 0;
  203. if (new_status & 1)
  204. *status |= WDIOF_CARDRESET;
  205. return 0;
  206. }
  207. /*
  208. * /dev/watchdog handling
  209. */
  210. static int wdt977_open(struct inode *inode, struct file *file)
  211. {
  212. /* If the watchdog is alive we don't need to start it again */
  213. if (test_and_set_bit(0, &timer_alive))
  214. return -EBUSY;
  215. if (nowayout)
  216. __module_get(THIS_MODULE);
  217. wdt977_start();
  218. return stream_open(inode, file);
  219. }
  220. static int wdt977_release(struct inode *inode, struct file *file)
  221. {
  222. /*
  223. * Shut off the timer.
  224. * Lock it in if it's a module and we set nowayout
  225. */
  226. if (expect_close == 42) {
  227. wdt977_stop();
  228. clear_bit(0, &timer_alive);
  229. } else {
  230. wdt977_keepalive();
  231. pr_crit("Unexpected close, not stopping watchdog!\n");
  232. }
  233. expect_close = 0;
  234. return 0;
  235. }
  236. /*
  237. * wdt977_write:
  238. * @file: file handle to the watchdog
  239. * @buf: buffer to write (unused as data does not matter here
  240. * @count: count of bytes
  241. * @ppos: pointer to the position to write. No seeks allowed
  242. *
  243. * A write to a watchdog device is defined as a keepalive signal. Any
  244. * write of data will do, as we we don't define content meaning.
  245. */
  246. static ssize_t wdt977_write(struct file *file, const char __user *buf,
  247. size_t count, loff_t *ppos)
  248. {
  249. if (count) {
  250. if (!nowayout) {
  251. size_t i;
  252. /* In case it was set long ago */
  253. expect_close = 0;
  254. for (i = 0; i != count; i++) {
  255. char c;
  256. if (get_user(c, buf + i))
  257. return -EFAULT;
  258. if (c == 'V')
  259. expect_close = 42;
  260. }
  261. }
  262. /* someone wrote to us, we should restart timer */
  263. wdt977_keepalive();
  264. }
  265. return count;
  266. }
  267. static const struct watchdog_info ident = {
  268. .options = WDIOF_SETTIMEOUT |
  269. WDIOF_MAGICCLOSE |
  270. WDIOF_KEEPALIVEPING,
  271. .firmware_version = 1,
  272. .identity = WATCHDOG_NAME,
  273. };
  274. /*
  275. * wdt977_ioctl:
  276. * @inode: inode of the device
  277. * @file: file handle to the device
  278. * @cmd: watchdog command
  279. * @arg: argument pointer
  280. *
  281. * The watchdog API defines a common set of functions for all watchdogs
  282. * according to their available features.
  283. */
  284. static long wdt977_ioctl(struct file *file, unsigned int cmd,
  285. unsigned long arg)
  286. {
  287. int status;
  288. int new_options, retval = -EINVAL;
  289. int new_timeout;
  290. union {
  291. struct watchdog_info __user *ident;
  292. int __user *i;
  293. } uarg;
  294. uarg.i = (int __user *)arg;
  295. switch (cmd) {
  296. case WDIOC_GETSUPPORT:
  297. return copy_to_user(uarg.ident, &ident,
  298. sizeof(ident)) ? -EFAULT : 0;
  299. case WDIOC_GETSTATUS:
  300. wdt977_get_status(&status);
  301. return put_user(status, uarg.i);
  302. case WDIOC_GETBOOTSTATUS:
  303. return put_user(0, uarg.i);
  304. case WDIOC_SETOPTIONS:
  305. if (get_user(new_options, uarg.i))
  306. return -EFAULT;
  307. if (new_options & WDIOS_DISABLECARD) {
  308. wdt977_stop();
  309. retval = 0;
  310. }
  311. if (new_options & WDIOS_ENABLECARD) {
  312. wdt977_start();
  313. retval = 0;
  314. }
  315. return retval;
  316. case WDIOC_KEEPALIVE:
  317. wdt977_keepalive();
  318. return 0;
  319. case WDIOC_SETTIMEOUT:
  320. if (get_user(new_timeout, uarg.i))
  321. return -EFAULT;
  322. if (wdt977_set_timeout(new_timeout))
  323. return -EINVAL;
  324. wdt977_keepalive();
  325. fallthrough;
  326. case WDIOC_GETTIMEOUT:
  327. return put_user(timeout, uarg.i);
  328. default:
  329. return -ENOTTY;
  330. }
  331. }
  332. static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
  333. void *unused)
  334. {
  335. if (code == SYS_DOWN || code == SYS_HALT)
  336. wdt977_stop();
  337. return NOTIFY_DONE;
  338. }
  339. static const struct file_operations wdt977_fops = {
  340. .owner = THIS_MODULE,
  341. .llseek = no_llseek,
  342. .write = wdt977_write,
  343. .unlocked_ioctl = wdt977_ioctl,
  344. .compat_ioctl = compat_ptr_ioctl,
  345. .open = wdt977_open,
  346. .release = wdt977_release,
  347. };
  348. static struct miscdevice wdt977_miscdev = {
  349. .minor = WATCHDOG_MINOR,
  350. .name = "watchdog",
  351. .fops = &wdt977_fops,
  352. };
  353. static struct notifier_block wdt977_notifier = {
  354. .notifier_call = wdt977_notify_sys,
  355. };
  356. static int __init wd977_init(void)
  357. {
  358. int rc;
  359. pr_info("driver v%s\n", WATCHDOG_VERSION);
  360. /* Check that the timeout value is within its range;
  361. if not reset to the default */
  362. if (wdt977_set_timeout(timeout)) {
  363. wdt977_set_timeout(DEFAULT_TIMEOUT);
  364. pr_info("timeout value must be 60 < timeout < 15300, using %d\n",
  365. DEFAULT_TIMEOUT);
  366. }
  367. /* on Netwinder the IOports are already reserved by
  368. * arch/arm/mach-footbridge/netwinder-hw.c
  369. */
  370. if (!machine_is_netwinder()) {
  371. if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
  372. pr_err("I/O address 0x%04x already in use\n",
  373. IO_INDEX_PORT);
  374. rc = -EIO;
  375. goto err_out;
  376. }
  377. }
  378. rc = register_reboot_notifier(&wdt977_notifier);
  379. if (rc) {
  380. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  381. goto err_out_region;
  382. }
  383. rc = misc_register(&wdt977_miscdev);
  384. if (rc) {
  385. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  386. wdt977_miscdev.minor, rc);
  387. goto err_out_reboot;
  388. }
  389. pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
  390. timeout, nowayout, testmode);
  391. return 0;
  392. err_out_reboot:
  393. unregister_reboot_notifier(&wdt977_notifier);
  394. err_out_region:
  395. if (!machine_is_netwinder())
  396. release_region(IO_INDEX_PORT, 2);
  397. err_out:
  398. return rc;
  399. }
  400. static void __exit wd977_exit(void)
  401. {
  402. wdt977_stop();
  403. misc_deregister(&wdt977_miscdev);
  404. unregister_reboot_notifier(&wdt977_notifier);
  405. release_region(IO_INDEX_PORT, 2);
  406. }
  407. module_init(wd977_init);
  408. module_exit(wd977_exit);
  409. MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
  410. MODULE_DESCRIPTION("W83977AF Watchdog driver");
  411. MODULE_LICENSE("GPL");