w83697hf_wdt.c 9.9 KB

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