it87_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Watchdog Timer Driver
  4. * for ITE IT87xx Environment Control - Low Pin Count Input / Output
  5. *
  6. * (c) Copyright 2007 Oliver Schuster <[email protected]>
  7. *
  8. * Based on softdog.c by Alan Cox,
  9. * 83977f_wdt.c by Jose Goncalves,
  10. * it87.c by Chris Gauthron, Jean Delvare
  11. *
  12. * Data-sheets: Publicly available at the ITE website
  13. * http://www.ite.com.tw/
  14. *
  15. * Support of the watchdog timers, which are available on
  16. * IT8607, IT8620, IT8622, IT8625, IT8628, IT8655, IT8665, IT8686,
  17. * IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726, IT8728,
  18. * IT8772, IT8783 and IT8784.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/types.h>
  27. #include <linux/watchdog.h>
  28. #define WATCHDOG_NAME "IT87 WDT"
  29. /* Defaults for Module Parameter */
  30. #define DEFAULT_TIMEOUT 60
  31. #define DEFAULT_TESTMODE 0
  32. #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
  33. /* IO Ports */
  34. #define REG 0x2e
  35. #define VAL 0x2f
  36. /* Logical device Numbers LDN */
  37. #define GPIO 0x07
  38. /* Configuration Registers and Functions */
  39. #define LDNREG 0x07
  40. #define CHIPID 0x20
  41. #define CHIPREV 0x22
  42. /* Chip Id numbers */
  43. #define NO_DEV_ID 0xffff
  44. #define IT8607_ID 0x8607
  45. #define IT8620_ID 0x8620
  46. #define IT8622_ID 0x8622
  47. #define IT8625_ID 0x8625
  48. #define IT8628_ID 0x8628
  49. #define IT8655_ID 0x8655
  50. #define IT8665_ID 0x8665
  51. #define IT8686_ID 0x8686
  52. #define IT8702_ID 0x8702
  53. #define IT8705_ID 0x8705
  54. #define IT8712_ID 0x8712
  55. #define IT8716_ID 0x8716
  56. #define IT8718_ID 0x8718
  57. #define IT8720_ID 0x8720
  58. #define IT8721_ID 0x8721
  59. #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
  60. #define IT8728_ID 0x8728
  61. #define IT8772_ID 0x8772
  62. #define IT8783_ID 0x8783
  63. #define IT8784_ID 0x8784
  64. #define IT8786_ID 0x8786
  65. /* GPIO Configuration Registers LDN=0x07 */
  66. #define WDTCTRL 0x71
  67. #define WDTCFG 0x72
  68. #define WDTVALLSB 0x73
  69. #define WDTVALMSB 0x74
  70. /* GPIO Bits WDTCFG */
  71. #define WDT_TOV1 0x80
  72. #define WDT_KRST 0x40
  73. #define WDT_TOVE 0x20
  74. #define WDT_PWROK 0x10 /* not in it8721 */
  75. #define WDT_INT_MASK 0x0f
  76. static unsigned int max_units, chip_type;
  77. static unsigned int timeout = DEFAULT_TIMEOUT;
  78. static int testmode = DEFAULT_TESTMODE;
  79. static bool nowayout = DEFAULT_NOWAYOUT;
  80. module_param(timeout, int, 0);
  81. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
  82. __MODULE_STRING(DEFAULT_TIMEOUT));
  83. module_param(testmode, int, 0);
  84. MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
  85. __MODULE_STRING(DEFAULT_TESTMODE));
  86. module_param(nowayout, bool, 0);
  87. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
  88. __MODULE_STRING(WATCHDOG_NOWAYOUT));
  89. /* Superio Chip */
  90. static inline int superio_enter(void)
  91. {
  92. /*
  93. * Try to reserve REG and REG + 1 for exclusive access.
  94. */
  95. if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
  96. return -EBUSY;
  97. outb(0x87, REG);
  98. outb(0x01, REG);
  99. outb(0x55, REG);
  100. outb(0x55, REG);
  101. return 0;
  102. }
  103. static inline void superio_exit(void)
  104. {
  105. outb(0x02, REG);
  106. outb(0x02, VAL);
  107. release_region(REG, 2);
  108. }
  109. static inline void superio_select(int ldn)
  110. {
  111. outb(LDNREG, REG);
  112. outb(ldn, VAL);
  113. }
  114. static inline int superio_inb(int reg)
  115. {
  116. outb(reg, REG);
  117. return inb(VAL);
  118. }
  119. static inline void superio_outb(int val, int reg)
  120. {
  121. outb(reg, REG);
  122. outb(val, VAL);
  123. }
  124. static inline int superio_inw(int reg)
  125. {
  126. int val;
  127. outb(reg++, REG);
  128. val = inb(VAL) << 8;
  129. outb(reg, REG);
  130. val |= inb(VAL);
  131. return val;
  132. }
  133. /* Internal function, should be called after superio_select(GPIO) */
  134. static void _wdt_update_timeout(unsigned int t)
  135. {
  136. unsigned char cfg = WDT_KRST;
  137. if (testmode)
  138. cfg = 0;
  139. if (t <= max_units)
  140. cfg |= WDT_TOV1;
  141. else
  142. t /= 60;
  143. if (chip_type != IT8721_ID)
  144. cfg |= WDT_PWROK;
  145. superio_outb(cfg, WDTCFG);
  146. superio_outb(t, WDTVALLSB);
  147. if (max_units > 255)
  148. superio_outb(t >> 8, WDTVALMSB);
  149. }
  150. static int wdt_update_timeout(unsigned int t)
  151. {
  152. int ret;
  153. ret = superio_enter();
  154. if (ret)
  155. return ret;
  156. superio_select(GPIO);
  157. _wdt_update_timeout(t);
  158. superio_exit();
  159. return 0;
  160. }
  161. static int wdt_round_time(int t)
  162. {
  163. t += 59;
  164. t -= t % 60;
  165. return t;
  166. }
  167. /* watchdog timer handling */
  168. static int wdt_start(struct watchdog_device *wdd)
  169. {
  170. return wdt_update_timeout(wdd->timeout);
  171. }
  172. static int wdt_stop(struct watchdog_device *wdd)
  173. {
  174. return wdt_update_timeout(0);
  175. }
  176. /**
  177. * wdt_set_timeout - set a new timeout value with watchdog ioctl
  178. * @t: timeout value in seconds
  179. *
  180. * The hardware device has a 8 or 16 bit watchdog timer (depends on
  181. * chip version) that can be configured to count seconds or minutes.
  182. *
  183. * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
  184. */
  185. static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  186. {
  187. int ret = 0;
  188. if (t > max_units)
  189. t = wdt_round_time(t);
  190. wdd->timeout = t;
  191. if (watchdog_hw_running(wdd))
  192. ret = wdt_update_timeout(t);
  193. return ret;
  194. }
  195. static const struct watchdog_info ident = {
  196. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  197. .firmware_version = 1,
  198. .identity = WATCHDOG_NAME,
  199. };
  200. static const struct watchdog_ops wdt_ops = {
  201. .owner = THIS_MODULE,
  202. .start = wdt_start,
  203. .stop = wdt_stop,
  204. .set_timeout = wdt_set_timeout,
  205. };
  206. static struct watchdog_device wdt_dev = {
  207. .info = &ident,
  208. .ops = &wdt_ops,
  209. .min_timeout = 1,
  210. };
  211. static int __init it87_wdt_init(void)
  212. {
  213. u8 chip_rev;
  214. int rc;
  215. rc = superio_enter();
  216. if (rc)
  217. return rc;
  218. chip_type = superio_inw(CHIPID);
  219. chip_rev = superio_inb(CHIPREV) & 0x0f;
  220. superio_exit();
  221. switch (chip_type) {
  222. case IT8702_ID:
  223. max_units = 255;
  224. break;
  225. case IT8712_ID:
  226. max_units = (chip_rev < 8) ? 255 : 65535;
  227. break;
  228. case IT8716_ID:
  229. case IT8726_ID:
  230. max_units = 65535;
  231. break;
  232. case IT8607_ID:
  233. case IT8620_ID:
  234. case IT8622_ID:
  235. case IT8625_ID:
  236. case IT8628_ID:
  237. case IT8655_ID:
  238. case IT8665_ID:
  239. case IT8686_ID:
  240. case IT8718_ID:
  241. case IT8720_ID:
  242. case IT8721_ID:
  243. case IT8728_ID:
  244. case IT8772_ID:
  245. case IT8783_ID:
  246. case IT8784_ID:
  247. case IT8786_ID:
  248. max_units = 65535;
  249. break;
  250. case IT8705_ID:
  251. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  252. chip_type, chip_rev);
  253. return -ENODEV;
  254. case NO_DEV_ID:
  255. pr_err("no device\n");
  256. return -ENODEV;
  257. default:
  258. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  259. chip_type, chip_rev);
  260. return -ENODEV;
  261. }
  262. rc = superio_enter();
  263. if (rc)
  264. return rc;
  265. superio_select(GPIO);
  266. superio_outb(WDT_TOV1, WDTCFG);
  267. superio_outb(0x00, WDTCTRL);
  268. superio_exit();
  269. if (timeout < 1 || timeout > max_units * 60) {
  270. timeout = DEFAULT_TIMEOUT;
  271. pr_warn("Timeout value out of range, use default %d sec\n",
  272. DEFAULT_TIMEOUT);
  273. }
  274. if (timeout > max_units)
  275. timeout = wdt_round_time(timeout);
  276. wdt_dev.timeout = timeout;
  277. wdt_dev.max_timeout = max_units * 60;
  278. watchdog_stop_on_reboot(&wdt_dev);
  279. rc = watchdog_register_device(&wdt_dev);
  280. if (rc) {
  281. pr_err("Cannot register watchdog device (err=%d)\n", rc);
  282. return rc;
  283. }
  284. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
  285. chip_type, chip_rev, timeout, nowayout, testmode);
  286. return 0;
  287. }
  288. static void __exit it87_wdt_exit(void)
  289. {
  290. watchdog_unregister_device(&wdt_dev);
  291. }
  292. module_init(it87_wdt_init);
  293. module_exit(it87_wdt_exit);
  294. MODULE_AUTHOR("Oliver Schuster");
  295. MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
  296. MODULE_LICENSE("GPL");