w83697hf_wdt.c 9.1 KB

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