w83627hf_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * w83627hf/thf WDT driver
  3. *
  4. * (c) Copyright 2013 Guenter Roeck
  5. * converted to watchdog infrastructure
  6. *
  7. * (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
  8. * added support for W83627THF.
  9. *
  10. * (c) Copyright 2003,2007 Pádraig Brady <P@draigBrady.com>
  11. *
  12. * Based on advantechwdt.c which is based on wdt.c.
  13. * Original copyright messages:
  14. *
  15. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  16. *
  17. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  18. * All Rights Reserved.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. *
  25. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  26. * warranty for any of this software. This material is provided
  27. * "AS-IS" and at no charge.
  28. *
  29. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/types.h>
  35. #include <linux/watchdog.h>
  36. #include <linux/ioport.h>
  37. #include <linux/init.h>
  38. #include <linux/io.h>
  39. #define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
  40. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  41. static int wdt_io;
  42. static int cr_wdt_timeout; /* WDT timeout register */
  43. static int cr_wdt_control; /* WDT control register */
  44. static int cr_wdt_csr; /* WDT control & status register */
  45. enum chips { w83627hf, w83627s, w83697hf, w83697ug, w83637hf, w83627thf,
  46. w83687thf, w83627ehf, w83627dhg, w83627uhg, w83667hg, w83627dhg_p,
  47. w83667hg_b, nct6775, nct6776, nct6779, nct6791, nct6792, nct6102 };
  48. static int timeout; /* in seconds */
  49. module_param(timeout, int, 0);
  50. MODULE_PARM_DESC(timeout,
  51. "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
  52. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  53. static bool nowayout = WATCHDOG_NOWAYOUT;
  54. module_param(nowayout, bool, 0);
  55. MODULE_PARM_DESC(nowayout,
  56. "Watchdog cannot be stopped once started (default="
  57. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  58. static int early_disable;
  59. module_param(early_disable, int, 0);
  60. MODULE_PARM_DESC(early_disable, "Disable watchdog at boot time (default=0)");
  61. /*
  62. * Kernel methods.
  63. */
  64. #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
  65. #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
  66. (same as EFER) */
  67. #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
  68. #define W83627HF_LD_WDT 0x08
  69. #define W83627HF_ID 0x52
  70. #define W83627S_ID 0x59
  71. #define W83697HF_ID 0x60
  72. #define W83697UG_ID 0x68
  73. #define W83637HF_ID 0x70
  74. #define W83627THF_ID 0x82
  75. #define W83687THF_ID 0x85
  76. #define W83627EHF_ID 0x88
  77. #define W83627DHG_ID 0xa0
  78. #define W83627UHG_ID 0xa2
  79. #define W83667HG_ID 0xa5
  80. #define W83627DHG_P_ID 0xb0
  81. #define W83667HG_B_ID 0xb3
  82. #define NCT6775_ID 0xb4
  83. #define NCT6776_ID 0xc3
  84. #define NCT6102_ID 0xc4
  85. #define NCT6779_ID 0xc5
  86. #define NCT6791_ID 0xc8
  87. #define NCT6792_ID 0xc9
  88. #define W83627HF_WDT_TIMEOUT 0xf6
  89. #define W83697HF_WDT_TIMEOUT 0xf4
  90. #define NCT6102D_WDT_TIMEOUT 0xf1
  91. #define W83627HF_WDT_CONTROL 0xf5
  92. #define W83697HF_WDT_CONTROL 0xf3
  93. #define NCT6102D_WDT_CONTROL 0xf0
  94. #define W836X7HF_WDT_CSR 0xf7
  95. #define NCT6102D_WDT_CSR 0xf2
  96. static void superio_outb(int reg, int val)
  97. {
  98. outb(reg, WDT_EFER);
  99. outb(val, WDT_EFDR);
  100. }
  101. static inline int superio_inb(int reg)
  102. {
  103. outb(reg, WDT_EFER);
  104. return inb(WDT_EFDR);
  105. }
  106. static int superio_enter(void)
  107. {
  108. if (!request_muxed_region(wdt_io, 2, WATCHDOG_NAME))
  109. return -EBUSY;
  110. outb_p(0x87, WDT_EFER); /* Enter extended function mode */
  111. outb_p(0x87, WDT_EFER); /* Again according to manual */
  112. return 0;
  113. }
  114. static void superio_select(int ld)
  115. {
  116. superio_outb(0x07, ld);
  117. }
  118. static void superio_exit(void)
  119. {
  120. outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
  121. release_region(wdt_io, 2);
  122. }
  123. static int w83627hf_init(struct watchdog_device *wdog, enum chips chip)
  124. {
  125. int ret;
  126. unsigned char t;
  127. ret = superio_enter();
  128. if (ret)
  129. return ret;
  130. superio_select(W83627HF_LD_WDT);
  131. /* set CR30 bit 0 to activate GPIO2 */
  132. t = superio_inb(0x30);
  133. if (!(t & 0x01))
  134. superio_outb(0x30, t | 0x01);
  135. switch (chip) {
  136. case w83627hf:
  137. case w83627s:
  138. t = superio_inb(0x2B) & ~0x10;
  139. superio_outb(0x2B, t); /* set GPIO24 to WDT0 */
  140. break;
  141. case w83697hf:
  142. /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  143. t = superio_inb(0x29) & ~0x60;
  144. t |= 0x20;
  145. superio_outb(0x29, t);
  146. break;
  147. case w83697ug:
  148. /* Set pin 118 to WDTO# mode */
  149. t = superio_inb(0x2b) & ~0x04;
  150. superio_outb(0x2b, t);
  151. break;
  152. case w83627thf:
  153. t = (superio_inb(0x2B) & ~0x08) | 0x04;
  154. superio_outb(0x2B, t); /* set GPIO3 to WDT0 */
  155. break;
  156. case w83627dhg:
  157. case w83627dhg_p:
  158. t = superio_inb(0x2D) & ~0x01; /* PIN77 -> WDT0# */
  159. superio_outb(0x2D, t); /* set GPIO5 to WDT0 */
  160. t = superio_inb(cr_wdt_control);
  161. t |= 0x02; /* enable the WDTO# output low pulse
  162. * to the KBRST# pin */
  163. superio_outb(cr_wdt_control, t);
  164. break;
  165. case w83637hf:
  166. break;
  167. case w83687thf:
  168. t = superio_inb(0x2C) & ~0x80; /* PIN47 -> WDT0# */
  169. superio_outb(0x2C, t);
  170. break;
  171. case w83627ehf:
  172. case w83627uhg:
  173. case w83667hg:
  174. case w83667hg_b:
  175. case nct6775:
  176. case nct6776:
  177. case nct6779:
  178. case nct6791:
  179. case nct6792:
  180. case nct6102:
  181. /*
  182. * These chips have a fixed WDTO# output pin (W83627UHG),
  183. * or support more than one WDTO# output pin.
  184. * Don't touch its configuration, and hope the BIOS
  185. * does the right thing.
  186. */
  187. t = superio_inb(cr_wdt_control);
  188. t |= 0x02; /* enable the WDTO# output low pulse
  189. * to the KBRST# pin */
  190. superio_outb(cr_wdt_control, t);
  191. break;
  192. default:
  193. break;
  194. }
  195. t = superio_inb(cr_wdt_timeout);
  196. if (t != 0) {
  197. if (early_disable) {
  198. pr_warn("Stopping previously enabled watchdog until userland kicks in\n");
  199. superio_outb(cr_wdt_timeout, 0);
  200. } else {
  201. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  202. wdog->timeout);
  203. superio_outb(cr_wdt_timeout, wdog->timeout);
  204. }
  205. }
  206. /* set second mode & disable keyboard turning off watchdog */
  207. t = superio_inb(cr_wdt_control) & ~0x0C;
  208. superio_outb(cr_wdt_control, t);
  209. /* reset trigger, disable keyboard & mouse turning off watchdog */
  210. t = superio_inb(cr_wdt_csr) & ~0xD0;
  211. superio_outb(cr_wdt_csr, t);
  212. superio_exit();
  213. return 0;
  214. }
  215. static int wdt_set_time(unsigned int timeout)
  216. {
  217. int ret;
  218. ret = superio_enter();
  219. if (ret)
  220. return ret;
  221. superio_select(W83627HF_LD_WDT);
  222. superio_outb(cr_wdt_timeout, timeout);
  223. superio_exit();
  224. return 0;
  225. }
  226. static int wdt_start(struct watchdog_device *wdog)
  227. {
  228. return wdt_set_time(wdog->timeout);
  229. }
  230. static int wdt_stop(struct watchdog_device *wdog)
  231. {
  232. return wdt_set_time(0);
  233. }
  234. static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  235. {
  236. wdog->timeout = timeout;
  237. return 0;
  238. }
  239. static unsigned int wdt_get_time(struct watchdog_device *wdog)
  240. {
  241. unsigned int timeleft;
  242. int ret;
  243. ret = superio_enter();
  244. if (ret)
  245. return 0;
  246. superio_select(W83627HF_LD_WDT);
  247. timeleft = superio_inb(cr_wdt_timeout);
  248. superio_exit();
  249. return timeleft;
  250. }
  251. /*
  252. * Kernel Interfaces
  253. */
  254. static struct watchdog_info wdt_info = {
  255. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  256. .identity = "W83627HF Watchdog",
  257. };
  258. static const struct watchdog_ops wdt_ops = {
  259. .owner = THIS_MODULE,
  260. .start = wdt_start,
  261. .stop = wdt_stop,
  262. .set_timeout = wdt_set_timeout,
  263. .get_timeleft = wdt_get_time,
  264. };
  265. static struct watchdog_device wdt_dev = {
  266. .info = &wdt_info,
  267. .ops = &wdt_ops,
  268. .timeout = WATCHDOG_TIMEOUT,
  269. .min_timeout = 1,
  270. .max_timeout = 255,
  271. };
  272. /*
  273. * The WDT needs to learn about soft shutdowns in order to
  274. * turn the timebomb registers off.
  275. */
  276. static int wdt_find(int addr)
  277. {
  278. u8 val;
  279. int ret;
  280. cr_wdt_timeout = W83627HF_WDT_TIMEOUT;
  281. cr_wdt_control = W83627HF_WDT_CONTROL;
  282. cr_wdt_csr = W836X7HF_WDT_CSR;
  283. ret = superio_enter();
  284. if (ret)
  285. return ret;
  286. superio_select(W83627HF_LD_WDT);
  287. val = superio_inb(0x20);
  288. switch (val) {
  289. case W83627HF_ID:
  290. ret = w83627hf;
  291. break;
  292. case W83627S_ID:
  293. ret = w83627s;
  294. break;
  295. case W83697HF_ID:
  296. ret = w83697hf;
  297. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  298. cr_wdt_control = W83697HF_WDT_CONTROL;
  299. break;
  300. case W83697UG_ID:
  301. ret = w83697ug;
  302. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  303. cr_wdt_control = W83697HF_WDT_CONTROL;
  304. break;
  305. case W83637HF_ID:
  306. ret = w83637hf;
  307. break;
  308. case W83627THF_ID:
  309. ret = w83627thf;
  310. break;
  311. case W83687THF_ID:
  312. ret = w83687thf;
  313. break;
  314. case W83627EHF_ID:
  315. ret = w83627ehf;
  316. break;
  317. case W83627DHG_ID:
  318. ret = w83627dhg;
  319. break;
  320. case W83627DHG_P_ID:
  321. ret = w83627dhg_p;
  322. break;
  323. case W83627UHG_ID:
  324. ret = w83627uhg;
  325. break;
  326. case W83667HG_ID:
  327. ret = w83667hg;
  328. break;
  329. case W83667HG_B_ID:
  330. ret = w83667hg_b;
  331. break;
  332. case NCT6775_ID:
  333. ret = nct6775;
  334. break;
  335. case NCT6776_ID:
  336. ret = nct6776;
  337. break;
  338. case NCT6779_ID:
  339. ret = nct6779;
  340. break;
  341. case NCT6791_ID:
  342. ret = nct6791;
  343. break;
  344. case NCT6792_ID:
  345. ret = nct6792;
  346. break;
  347. case NCT6102_ID:
  348. ret = nct6102;
  349. cr_wdt_timeout = NCT6102D_WDT_TIMEOUT;
  350. cr_wdt_control = NCT6102D_WDT_CONTROL;
  351. cr_wdt_csr = NCT6102D_WDT_CSR;
  352. break;
  353. case 0xff:
  354. ret = -ENODEV;
  355. break;
  356. default:
  357. ret = -ENODEV;
  358. pr_err("Unsupported chip ID: 0x%02x\n", val);
  359. break;
  360. }
  361. superio_exit();
  362. return ret;
  363. }
  364. static int __init wdt_init(void)
  365. {
  366. int ret;
  367. int chip;
  368. const char * const chip_name[] = {
  369. "W83627HF",
  370. "W83627S",
  371. "W83697HF",
  372. "W83697UG",
  373. "W83637HF",
  374. "W83627THF",
  375. "W83687THF",
  376. "W83627EHF",
  377. "W83627DHG",
  378. "W83627UHG",
  379. "W83667HG",
  380. "W83667DHG-P",
  381. "W83667HG-B",
  382. "NCT6775",
  383. "NCT6776",
  384. "NCT6779",
  385. "NCT6791",
  386. "NCT6792",
  387. "NCT6102",
  388. };
  389. wdt_io = 0x2e;
  390. chip = wdt_find(0x2e);
  391. if (chip < 0) {
  392. wdt_io = 0x4e;
  393. chip = wdt_find(0x4e);
  394. if (chip < 0)
  395. return chip;
  396. }
  397. pr_info("WDT driver for %s Super I/O chip initialising\n",
  398. chip_name[chip]);
  399. watchdog_init_timeout(&wdt_dev, timeout, NULL);
  400. watchdog_set_nowayout(&wdt_dev, nowayout);
  401. watchdog_stop_on_reboot(&wdt_dev);
  402. ret = w83627hf_init(&wdt_dev, chip);
  403. if (ret) {
  404. pr_err("failed to initialize watchdog (err=%d)\n", ret);
  405. return ret;
  406. }
  407. ret = watchdog_register_device(&wdt_dev);
  408. if (ret)
  409. return ret;
  410. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  411. wdt_dev.timeout, nowayout);
  412. return ret;
  413. }
  414. static void __exit wdt_exit(void)
  415. {
  416. watchdog_unregister_device(&wdt_dev);
  417. }
  418. module_init(wdt_init);
  419. module_exit(wdt_exit);
  420. MODULE_LICENSE("GPL");
  421. MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
  422. MODULE_DESCRIPTION("w83627hf/thf WDT driver");