it8712f_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * IT8712F "Smart Guardian" Watchdog support
  3. *
  4. * Copyright (c) 2006-2007 Jorge Boncompte - DTI2 <jorge@dti2.net>
  5. *
  6. * Based on info and code taken from:
  7. *
  8. * drivers/char/watchdog/scx200_wdt.c
  9. * drivers/hwmon/it87.c
  10. * IT8712F EC-LPC I/O Preliminary Specification 0.8.2
  11. * IT8712F EC-LPC I/O Preliminary Specification 0.9.3
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * The author(s) of this software shall not be held liable for damages
  19. * of any nature resulting due to the use of this software. This
  20. * software is provided AS-IS with no warranties.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/watchdog.h>
  27. #include <linux/notifier.h>
  28. #include <linux/reboot.h>
  29. #include <linux/fs.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/io.h>
  33. #include <linux/ioport.h>
  34. #define NAME "it8712f_wdt"
  35. MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
  36. MODULE_DESCRIPTION("IT8712F Watchdog Driver");
  37. MODULE_LICENSE("GPL");
  38. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  39. static int max_units = 255;
  40. static int margin = 60; /* in seconds */
  41. module_param(margin, int, 0);
  42. MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
  43. static int nowayout = WATCHDOG_NOWAYOUT;
  44. module_param(nowayout, int, 0);
  45. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  46. static unsigned long wdt_open;
  47. static unsigned expect_close;
  48. static unsigned char revision;
  49. /* Dog Food address - We use the game port address */
  50. static unsigned short address;
  51. #define REG 0x2e /* The register to read/write */
  52. #define VAL 0x2f /* The value to read/write */
  53. #define LDN 0x07 /* Register: Logical device select */
  54. #define DEVID 0x20 /* Register: Device ID */
  55. #define DEVREV 0x22 /* Register: Device Revision */
  56. #define ACT_REG 0x30 /* LDN Register: Activation */
  57. #define BASE_REG 0x60 /* LDN Register: Base address */
  58. #define IT8712F_DEVID 0x8712
  59. #define LDN_GPIO 0x07 /* GPIO and Watch Dog Timer */
  60. #define LDN_GAME 0x09 /* Game Port */
  61. #define WDT_CONTROL 0x71 /* WDT Register: Control */
  62. #define WDT_CONFIG 0x72 /* WDT Register: Configuration */
  63. #define WDT_TIMEOUT 0x73 /* WDT Register: Timeout Value */
  64. #define WDT_RESET_GAME 0x10 /* Reset timer on read or write to game port */
  65. #define WDT_RESET_KBD 0x20 /* Reset timer on keyboard interrupt */
  66. #define WDT_RESET_MOUSE 0x40 /* Reset timer on mouse interrupt */
  67. #define WDT_RESET_CIR 0x80 /* Reset timer on consumer IR interrupt */
  68. #define WDT_UNIT_SEC 0x80 /* If 0 in MINUTES */
  69. #define WDT_OUT_PWROK 0x10 /* Pulse PWROK on timeout */
  70. #define WDT_OUT_KRST 0x40 /* Pulse reset on timeout */
  71. static int wdt_control_reg = WDT_RESET_GAME;
  72. module_param(wdt_control_reg, int, 0);
  73. MODULE_PARM_DESC(wdt_control_reg, "Value to write to watchdog control "
  74. "register. The default WDT_RESET_GAME resets the timer on "
  75. "game port reads that this driver generates. You can also "
  76. "use KBD, MOUSE or CIR if you have some external way to "
  77. "generate those interrupts.");
  78. static int superio_inb(int reg)
  79. {
  80. outb(reg, REG);
  81. return inb(VAL);
  82. }
  83. static void superio_outb(int val, int reg)
  84. {
  85. outb(reg, REG);
  86. outb(val, VAL);
  87. }
  88. static int superio_inw(int reg)
  89. {
  90. int val;
  91. outb(reg++, REG);
  92. val = inb(VAL) << 8;
  93. outb(reg, REG);
  94. val |= inb(VAL);
  95. return val;
  96. }
  97. static inline void superio_select(int ldn)
  98. {
  99. outb(LDN, REG);
  100. outb(ldn, VAL);
  101. }
  102. static inline int superio_enter(void)
  103. {
  104. /*
  105. * Try to reserve REG and REG + 1 for exclusive access.
  106. */
  107. if (!request_muxed_region(REG, 2, NAME))
  108. return -EBUSY;
  109. outb(0x87, REG);
  110. outb(0x01, REG);
  111. outb(0x55, REG);
  112. outb(0x55, REG);
  113. return 0;
  114. }
  115. static inline void superio_exit(void)
  116. {
  117. outb(0x02, REG);
  118. outb(0x02, VAL);
  119. release_region(REG, 2);
  120. }
  121. static inline void it8712f_wdt_ping(void)
  122. {
  123. if (wdt_control_reg & WDT_RESET_GAME)
  124. inb(address);
  125. }
  126. static void it8712f_wdt_update_margin(void)
  127. {
  128. int config = WDT_OUT_KRST | WDT_OUT_PWROK;
  129. int units = margin;
  130. /* Switch to minutes precision if the configured margin
  131. * value does not fit within the register width.
  132. */
  133. if (units <= max_units) {
  134. config |= WDT_UNIT_SEC; /* else UNIT is MINUTES */
  135. printk(KERN_INFO NAME ": timer margin %d seconds\n", units);
  136. } else {
  137. units /= 60;
  138. printk(KERN_INFO NAME ": timer margin %d minutes\n", units);
  139. }
  140. superio_outb(config, WDT_CONFIG);
  141. if (revision >= 0x08)
  142. superio_outb(units >> 8, WDT_TIMEOUT + 1);
  143. superio_outb(units, WDT_TIMEOUT);
  144. }
  145. static int it8712f_wdt_get_status(void)
  146. {
  147. if (superio_inb(WDT_CONTROL) & 0x01)
  148. return WDIOF_CARDRESET;
  149. else
  150. return 0;
  151. }
  152. static int it8712f_wdt_enable(void)
  153. {
  154. int ret = superio_enter();
  155. if (ret)
  156. return ret;
  157. printk(KERN_DEBUG NAME ": enabling watchdog timer\n");
  158. superio_select(LDN_GPIO);
  159. superio_outb(wdt_control_reg, WDT_CONTROL);
  160. it8712f_wdt_update_margin();
  161. superio_exit();
  162. it8712f_wdt_ping();
  163. return 0;
  164. }
  165. static int it8712f_wdt_disable(void)
  166. {
  167. int ret = superio_enter();
  168. if (ret)
  169. return ret;
  170. printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
  171. superio_select(LDN_GPIO);
  172. superio_outb(0, WDT_CONFIG);
  173. superio_outb(0, WDT_CONTROL);
  174. if (revision >= 0x08)
  175. superio_outb(0, WDT_TIMEOUT + 1);
  176. superio_outb(0, WDT_TIMEOUT);
  177. superio_exit();
  178. return 0;
  179. }
  180. static int it8712f_wdt_notify(struct notifier_block *this,
  181. unsigned long code, void *unused)
  182. {
  183. if (code == SYS_HALT || code == SYS_POWER_OFF)
  184. if (!nowayout)
  185. it8712f_wdt_disable();
  186. return NOTIFY_DONE;
  187. }
  188. static struct notifier_block it8712f_wdt_notifier = {
  189. .notifier_call = it8712f_wdt_notify,
  190. };
  191. static ssize_t it8712f_wdt_write(struct file *file, const char __user *data,
  192. size_t len, loff_t *ppos)
  193. {
  194. /* check for a magic close character */
  195. if (len) {
  196. size_t i;
  197. it8712f_wdt_ping();
  198. expect_close = 0;
  199. for (i = 0; i < len; ++i) {
  200. char c;
  201. if (get_user(c, data + i))
  202. return -EFAULT;
  203. if (c == 'V')
  204. expect_close = 42;
  205. }
  206. }
  207. return len;
  208. }
  209. static long it8712f_wdt_ioctl(struct file *file, unsigned int cmd,
  210. unsigned long arg)
  211. {
  212. void __user *argp = (void __user *)arg;
  213. int __user *p = argp;
  214. static const struct watchdog_info ident = {
  215. .identity = "IT8712F Watchdog",
  216. .firmware_version = 1,
  217. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  218. WDIOF_MAGICCLOSE,
  219. };
  220. int value;
  221. int ret;
  222. switch (cmd) {
  223. case WDIOC_GETSUPPORT:
  224. if (copy_to_user(argp, &ident, sizeof(ident)))
  225. return -EFAULT;
  226. return 0;
  227. case WDIOC_GETSTATUS:
  228. ret = superio_enter();
  229. if (ret)
  230. return ret;
  231. superio_select(LDN_GPIO);
  232. value = it8712f_wdt_get_status();
  233. superio_exit();
  234. return put_user(value, p);
  235. case WDIOC_GETBOOTSTATUS:
  236. return put_user(0, p);
  237. case WDIOC_KEEPALIVE:
  238. it8712f_wdt_ping();
  239. return 0;
  240. case WDIOC_SETTIMEOUT:
  241. if (get_user(value, p))
  242. return -EFAULT;
  243. if (value < 1)
  244. return -EINVAL;
  245. if (value > (max_units * 60))
  246. return -EINVAL;
  247. margin = value;
  248. ret = superio_enter();
  249. if (ret)
  250. return ret;
  251. superio_select(LDN_GPIO);
  252. it8712f_wdt_update_margin();
  253. superio_exit();
  254. it8712f_wdt_ping();
  255. /* Fall through */
  256. case WDIOC_GETTIMEOUT:
  257. if (put_user(margin, p))
  258. return -EFAULT;
  259. return 0;
  260. default:
  261. return -ENOTTY;
  262. }
  263. }
  264. static int it8712f_wdt_open(struct inode *inode, struct file *file)
  265. {
  266. int ret;
  267. /* only allow one at a time */
  268. if (test_and_set_bit(0, &wdt_open))
  269. return -EBUSY;
  270. ret = it8712f_wdt_enable();
  271. if (ret)
  272. return ret;
  273. return nonseekable_open(inode, file);
  274. }
  275. static int it8712f_wdt_release(struct inode *inode, struct file *file)
  276. {
  277. if (expect_close != 42) {
  278. printk(KERN_WARNING NAME
  279. ": watchdog device closed unexpectedly, will not"
  280. " disable the watchdog timer\n");
  281. } else if (!nowayout) {
  282. if (it8712f_wdt_disable())
  283. printk(KERN_WARNING NAME "Watchdog disable failed\n");
  284. }
  285. expect_close = 0;
  286. clear_bit(0, &wdt_open);
  287. return 0;
  288. }
  289. static const struct file_operations it8712f_wdt_fops = {
  290. .owner = THIS_MODULE,
  291. .llseek = no_llseek,
  292. .write = it8712f_wdt_write,
  293. .unlocked_ioctl = it8712f_wdt_ioctl,
  294. .open = it8712f_wdt_open,
  295. .release = it8712f_wdt_release,
  296. };
  297. static struct miscdevice it8712f_wdt_miscdev = {
  298. .minor = WATCHDOG_MINOR,
  299. .name = "watchdog",
  300. .fops = &it8712f_wdt_fops,
  301. };
  302. static int __init it8712f_wdt_find(unsigned short *address)
  303. {
  304. int err = -ENODEV;
  305. int chip_type;
  306. int ret = superio_enter();
  307. if (ret)
  308. return ret;
  309. chip_type = superio_inw(DEVID);
  310. if (chip_type != IT8712F_DEVID)
  311. goto exit;
  312. superio_select(LDN_GAME);
  313. superio_outb(1, ACT_REG);
  314. if (!(superio_inb(ACT_REG) & 0x01)) {
  315. printk(KERN_ERR NAME ": Device not activated, skipping\n");
  316. goto exit;
  317. }
  318. *address = superio_inw(BASE_REG);
  319. if (*address == 0) {
  320. printk(KERN_ERR NAME ": Base address not set, skipping\n");
  321. goto exit;
  322. }
  323. err = 0;
  324. revision = superio_inb(DEVREV) & 0x0f;
  325. /* Later revisions have 16-bit values per datasheet 0.9.1 */
  326. if (revision >= 0x08)
  327. max_units = 65535;
  328. if (margin > (max_units * 60))
  329. margin = (max_units * 60);
  330. printk(KERN_INFO NAME ": Found IT%04xF chip revision %d - "
  331. "using DogFood address 0x%x\n",
  332. chip_type, revision, *address);
  333. exit:
  334. superio_exit();
  335. return err;
  336. }
  337. static int __init it8712f_wdt_init(void)
  338. {
  339. int err = 0;
  340. if (it8712f_wdt_find(&address))
  341. return -ENODEV;
  342. if (!request_region(address, 1, "IT8712F Watchdog")) {
  343. printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
  344. return -EBUSY;
  345. }
  346. err = it8712f_wdt_disable();
  347. if (err) {
  348. printk(KERN_ERR NAME ": unable to disable watchdog timer.\n");
  349. goto out;
  350. }
  351. err = register_reboot_notifier(&it8712f_wdt_notifier);
  352. if (err) {
  353. printk(KERN_ERR NAME ": unable to register reboot notifier\n");
  354. goto out;
  355. }
  356. err = misc_register(&it8712f_wdt_miscdev);
  357. if (err) {
  358. printk(KERN_ERR NAME
  359. ": cannot register miscdev on minor=%d (err=%d)\n",
  360. WATCHDOG_MINOR, err);
  361. goto reboot_out;
  362. }
  363. return 0;
  364. reboot_out:
  365. unregister_reboot_notifier(&it8712f_wdt_notifier);
  366. out:
  367. release_region(address, 1);
  368. return err;
  369. }
  370. static void __exit it8712f_wdt_exit(void)
  371. {
  372. misc_deregister(&it8712f_wdt_miscdev);
  373. unregister_reboot_notifier(&it8712f_wdt_notifier);
  374. release_region(address, 1);
  375. }
  376. module_init(it8712f_wdt_init);
  377. module_exit(it8712f_wdt_exit);