w83627hf_wdt.c 11 KB

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