it8712f_wdt.c 8.2 KB

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