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