w83697hf_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * w83697hf/hg WDT driver
  3. *
  4. * (c) Copyright 2006 Samuel Tardieu <sam@rfc1149.net>
  5. * (c) Copyright 2006 Marcus Junker <junker@anduras.de>
  6. *
  7. * Based on w83627hf_wdt.c which is based on advantechwdt.c
  8. * which is based on wdt.c.
  9. * Original copyright messages:
  10. *
  11. * (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
  12. *
  13. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  14. *
  15. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  16. * All Rights Reserved.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. * Neither Marcus Junker nor ANDURAS AG admit liability nor provide
  24. * warranty for any of this software. This material is provided
  25. * "AS-IS" and at no charge.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/types.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/watchdog.h>
  33. #include <linux/fs.h>
  34. #include <linux/ioport.h>
  35. #include <linux/notifier.h>
  36. #include <linux/reboot.h>
  37. #include <linux/init.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/io.h>
  40. #include <linux/uaccess.h>
  41. #define WATCHDOG_NAME "w83697hf/hg WDT"
  42. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  43. #define WATCHDOG_EARLY_DISABLE 1 /* Disable until userland kicks in */
  44. static unsigned long wdt_is_open;
  45. static char expect_close;
  46. static DEFINE_SPINLOCK(io_lock);
  47. /* You must set this - there is no sane way to probe for this board. */
  48. static int wdt_io = 0x2e;
  49. module_param(wdt_io, int, 0);
  50. MODULE_PARM_DESC(wdt_io,
  51. "w83697hf/hg WDT io port (default 0x2e, 0 = autodetect)");
  52. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  53. module_param(timeout, int, 0);
  54. MODULE_PARM_DESC(timeout,
  55. "Watchdog timeout in seconds. 1<= timeout <=255 (default="
  56. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  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. static int early_disable = WATCHDOG_EARLY_DISABLE;
  63. module_param(early_disable, int, 0);
  64. MODULE_PARM_DESC(early_disable,
  65. "Watchdog gets disabled at boot time (default="
  66. __MODULE_STRING(WATCHDOG_EARLY_DISABLE) ")");
  67. /*
  68. * Kernel methods.
  69. */
  70. #define W83697HF_EFER (wdt_io + 0) /* Extended Function Enable Register */
  71. #define W83697HF_EFIR (wdt_io + 0) /* Extended Function Index Register
  72. (same as EFER) */
  73. #define W83697HF_EFDR (wdt_io + 1) /* Extended Function Data Register */
  74. static inline void w83697hf_unlock(void)
  75. {
  76. outb_p(0x87, W83697HF_EFER); /* Enter extended function mode */
  77. outb_p(0x87, W83697HF_EFER); /* Again according to manual */
  78. }
  79. static inline void w83697hf_lock(void)
  80. {
  81. outb_p(0xAA, W83697HF_EFER); /* Leave extended function mode */
  82. }
  83. /*
  84. * The three functions w83697hf_get_reg(), w83697hf_set_reg() and
  85. * w83697hf_write_timeout() must be called with the device unlocked.
  86. */
  87. static unsigned char w83697hf_get_reg(unsigned char reg)
  88. {
  89. outb_p(reg, W83697HF_EFIR);
  90. return inb_p(W83697HF_EFDR);
  91. }
  92. static void w83697hf_set_reg(unsigned char reg, unsigned char data)
  93. {
  94. outb_p(reg, W83697HF_EFIR);
  95. outb_p(data, W83697HF_EFDR);
  96. }
  97. static void w83697hf_write_timeout(int timeout)
  98. {
  99. /* Write Timeout counter to CRF4 */
  100. w83697hf_set_reg(0xF4, timeout);
  101. }
  102. static void w83697hf_select_wdt(void)
  103. {
  104. w83697hf_unlock();
  105. w83697hf_set_reg(0x07, 0x08); /* Switch to logic device 8 (GPIO2) */
  106. }
  107. static inline void w83697hf_deselect_wdt(void)
  108. {
  109. w83697hf_lock();
  110. }
  111. static void w83697hf_init(void)
  112. {
  113. unsigned char bbuf;
  114. w83697hf_select_wdt();
  115. bbuf = w83697hf_get_reg(0x29);
  116. bbuf &= ~0x60;
  117. bbuf |= 0x20;
  118. /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  119. w83697hf_set_reg(0x29, bbuf);
  120. bbuf = w83697hf_get_reg(0xF3);
  121. bbuf &= ~0x04;
  122. w83697hf_set_reg(0xF3, bbuf); /* Count mode is seconds */
  123. w83697hf_deselect_wdt();
  124. }
  125. static void wdt_ping(void)
  126. {
  127. spin_lock(&io_lock);
  128. w83697hf_select_wdt();
  129. w83697hf_write_timeout(timeout);
  130. w83697hf_deselect_wdt();
  131. spin_unlock(&io_lock);
  132. }
  133. static void wdt_enable(void)
  134. {
  135. spin_lock(&io_lock);
  136. w83697hf_select_wdt();
  137. w83697hf_write_timeout(timeout);
  138. w83697hf_set_reg(0x30, 1); /* Enable timer */
  139. w83697hf_deselect_wdt();
  140. spin_unlock(&io_lock);
  141. }
  142. static void wdt_disable(void)
  143. {
  144. spin_lock(&io_lock);
  145. w83697hf_select_wdt();
  146. w83697hf_set_reg(0x30, 0); /* Disable timer */
  147. w83697hf_write_timeout(0);
  148. w83697hf_deselect_wdt();
  149. spin_unlock(&io_lock);
  150. }
  151. static unsigned char wdt_running(void)
  152. {
  153. unsigned char t;
  154. spin_lock(&io_lock);
  155. w83697hf_select_wdt();
  156. t = w83697hf_get_reg(0xF4); /* Read timer */
  157. w83697hf_deselect_wdt();
  158. spin_unlock(&io_lock);
  159. return t;
  160. }
  161. static int wdt_set_heartbeat(int t)
  162. {
  163. if (t < 1 || t > 255)
  164. return -EINVAL;
  165. timeout = t;
  166. return 0;
  167. }
  168. static ssize_t wdt_write(struct file *file, const char __user *buf,
  169. size_t count, loff_t *ppos)
  170. {
  171. if (count) {
  172. if (!nowayout) {
  173. size_t i;
  174. expect_close = 0;
  175. for (i = 0; i != count; i++) {
  176. char c;
  177. if (get_user(c, buf + i))
  178. return -EFAULT;
  179. if (c == 'V')
  180. expect_close = 42;
  181. }
  182. }
  183. wdt_ping();
  184. }
  185. return count;
  186. }
  187. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  188. {
  189. void __user *argp = (void __user *)arg;
  190. int __user *p = argp;
  191. int new_timeout;
  192. static const struct watchdog_info ident = {
  193. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  194. | WDIOF_MAGICCLOSE,
  195. .firmware_version = 1,
  196. .identity = "W83697HF WDT",
  197. };
  198. switch (cmd) {
  199. case WDIOC_GETSUPPORT:
  200. if (copy_to_user(argp, &ident, sizeof(ident)))
  201. return -EFAULT;
  202. break;
  203. case WDIOC_GETSTATUS:
  204. case WDIOC_GETBOOTSTATUS:
  205. return put_user(0, p);
  206. case WDIOC_SETOPTIONS:
  207. {
  208. int options, retval = -EINVAL;
  209. if (get_user(options, p))
  210. return -EFAULT;
  211. if (options & WDIOS_DISABLECARD) {
  212. wdt_disable();
  213. retval = 0;
  214. }
  215. if (options & WDIOS_ENABLECARD) {
  216. wdt_enable();
  217. retval = 0;
  218. }
  219. return retval;
  220. }
  221. case WDIOC_KEEPALIVE:
  222. wdt_ping();
  223. break;
  224. case WDIOC_SETTIMEOUT:
  225. if (get_user(new_timeout, p))
  226. return -EFAULT;
  227. if (wdt_set_heartbeat(new_timeout))
  228. return -EINVAL;
  229. wdt_ping();
  230. /* Fall */
  231. case WDIOC_GETTIMEOUT:
  232. return put_user(timeout, p);
  233. default:
  234. return -ENOTTY;
  235. }
  236. return 0;
  237. }
  238. static int wdt_open(struct inode *inode, struct file *file)
  239. {
  240. if (test_and_set_bit(0, &wdt_is_open))
  241. return -EBUSY;
  242. /*
  243. * Activate
  244. */
  245. wdt_enable();
  246. return nonseekable_open(inode, file);
  247. }
  248. static int wdt_close(struct inode *inode, struct file *file)
  249. {
  250. if (expect_close == 42)
  251. wdt_disable();
  252. else {
  253. pr_crit("Unexpected close, not stopping watchdog!\n");
  254. wdt_ping();
  255. }
  256. expect_close = 0;
  257. clear_bit(0, &wdt_is_open);
  258. return 0;
  259. }
  260. /*
  261. * Notifier for system down
  262. */
  263. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  264. void *unused)
  265. {
  266. if (code == SYS_DOWN || code == SYS_HALT)
  267. wdt_disable(); /* Turn the WDT off */
  268. return NOTIFY_DONE;
  269. }
  270. /*
  271. * Kernel Interfaces
  272. */
  273. static const struct file_operations wdt_fops = {
  274. .owner = THIS_MODULE,
  275. .llseek = no_llseek,
  276. .write = wdt_write,
  277. .unlocked_ioctl = wdt_ioctl,
  278. .open = wdt_open,
  279. .release = wdt_close,
  280. };
  281. static struct miscdevice wdt_miscdev = {
  282. .minor = WATCHDOG_MINOR,
  283. .name = "watchdog",
  284. .fops = &wdt_fops,
  285. };
  286. /*
  287. * The WDT needs to learn about soft shutdowns in order to
  288. * turn the timebomb registers off.
  289. */
  290. static struct notifier_block wdt_notifier = {
  291. .notifier_call = wdt_notify_sys,
  292. };
  293. static int w83697hf_check_wdt(void)
  294. {
  295. if (!request_region(wdt_io, 2, WATCHDOG_NAME)) {
  296. pr_err("I/O address 0x%x already in use\n", wdt_io);
  297. return -EIO;
  298. }
  299. pr_debug("Looking for watchdog at address 0x%x\n", wdt_io);
  300. w83697hf_unlock();
  301. if (w83697hf_get_reg(0x20) == 0x60) {
  302. pr_info("watchdog found at address 0x%x\n", wdt_io);
  303. w83697hf_lock();
  304. return 0;
  305. }
  306. /* Reprotect in case it was a compatible device */
  307. w83697hf_lock();
  308. pr_info("watchdog not found at address 0x%x\n", wdt_io);
  309. release_region(wdt_io, 2);
  310. return -EIO;
  311. }
  312. static int w83697hf_ioports[] = { 0x2e, 0x4e, 0x00 };
  313. static int __init wdt_init(void)
  314. {
  315. int ret, i, found = 0;
  316. pr_info("WDT driver for W83697HF/HG initializing\n");
  317. if (wdt_io == 0) {
  318. /* we will autodetect the W83697HF/HG watchdog */
  319. for (i = 0; ((!found) && (w83697hf_ioports[i] != 0)); i++) {
  320. wdt_io = w83697hf_ioports[i];
  321. if (!w83697hf_check_wdt())
  322. found++;
  323. }
  324. } else {
  325. if (!w83697hf_check_wdt())
  326. found++;
  327. }
  328. if (!found) {
  329. pr_err("No W83697HF/HG could be found\n");
  330. ret = -EIO;
  331. goto out;
  332. }
  333. w83697hf_init();
  334. if (early_disable) {
  335. if (wdt_running())
  336. pr_warn("Stopping previously enabled watchdog until userland kicks in\n");
  337. wdt_disable();
  338. }
  339. if (wdt_set_heartbeat(timeout)) {
  340. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  341. pr_info("timeout value must be 1 <= timeout <= 255, using %d\n",
  342. WATCHDOG_TIMEOUT);
  343. }
  344. ret = register_reboot_notifier(&wdt_notifier);
  345. if (ret != 0) {
  346. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  347. goto unreg_regions;
  348. }
  349. ret = misc_register(&wdt_miscdev);
  350. if (ret != 0) {
  351. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  352. WATCHDOG_MINOR, ret);
  353. goto unreg_reboot;
  354. }
  355. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  356. timeout, nowayout);
  357. out:
  358. return ret;
  359. unreg_reboot:
  360. unregister_reboot_notifier(&wdt_notifier);
  361. unreg_regions:
  362. release_region(wdt_io, 2);
  363. goto out;
  364. }
  365. static void __exit wdt_exit(void)
  366. {
  367. misc_deregister(&wdt_miscdev);
  368. unregister_reboot_notifier(&wdt_notifier);
  369. release_region(wdt_io, 2);
  370. }
  371. module_init(wdt_init);
  372. module_exit(wdt_exit);
  373. MODULE_LICENSE("GPL");
  374. MODULE_AUTHOR("Marcus Junker <junker@anduras.de>, "
  375. "Samuel Tardieu <sam@rfc1149.net>");
  376. MODULE_DESCRIPTION("w83697hf/hg WDT driver");
  377. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);