it87_wdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * Watchdog Timer Driver
  3. * for ITE IT87xx Environment Control - Low Pin Count Input / Output
  4. *
  5. * (c) Copyright 2007 Oliver Schuster <olivers137@aol.com>
  6. *
  7. * Based on softdog.c by Alan Cox,
  8. * 83977f_wdt.c by Jose Goncalves,
  9. * it87.c by Chris Gauthron, Jean Delvare
  10. *
  11. * Data-sheets: Publicly available at the ITE website
  12. * http://www.ite.com.tw/
  13. *
  14. * Support of the watchdog timers, which are available on
  15. * IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726
  16. * and IT8728.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/types.h>
  36. #include <linux/kernel.h>
  37. #include <linux/fs.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/init.h>
  40. #include <linux/ioport.h>
  41. #include <linux/watchdog.h>
  42. #include <linux/notifier.h>
  43. #include <linux/reboot.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/io.h>
  46. #define WATCHDOG_VERSION "1.14"
  47. #define WATCHDOG_NAME "IT87 WDT"
  48. #define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
  49. #define WD_MAGIC 'V'
  50. /* Defaults for Module Parameter */
  51. #define DEFAULT_NOGAMEPORT 0
  52. #define DEFAULT_NOCIR 0
  53. #define DEFAULT_EXCLUSIVE 1
  54. #define DEFAULT_TIMEOUT 60
  55. #define DEFAULT_TESTMODE 0
  56. #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
  57. /* IO Ports */
  58. #define REG 0x2e
  59. #define VAL 0x2f
  60. /* Logical device Numbers LDN */
  61. #define GPIO 0x07
  62. #define GAMEPORT 0x09
  63. #define CIR 0x0a
  64. /* Configuration Registers and Functions */
  65. #define LDNREG 0x07
  66. #define CHIPID 0x20
  67. #define CHIPREV 0x22
  68. #define ACTREG 0x30
  69. #define BASEREG 0x60
  70. /* Chip Id numbers */
  71. #define NO_DEV_ID 0xffff
  72. #define IT8702_ID 0x8702
  73. #define IT8705_ID 0x8705
  74. #define IT8712_ID 0x8712
  75. #define IT8716_ID 0x8716
  76. #define IT8718_ID 0x8718
  77. #define IT8720_ID 0x8720
  78. #define IT8721_ID 0x8721
  79. #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
  80. #define IT8728_ID 0x8728
  81. /* GPIO Configuration Registers LDN=0x07 */
  82. #define WDTCTRL 0x71
  83. #define WDTCFG 0x72
  84. #define WDTVALLSB 0x73
  85. #define WDTVALMSB 0x74
  86. /* GPIO Bits WDTCTRL */
  87. #define WDT_CIRINT 0x80
  88. #define WDT_MOUSEINT 0x40
  89. #define WDT_KYBINT 0x20
  90. #define WDT_GAMEPORT 0x10 /* not in it8718, it8720, it8721, it8728 */
  91. #define WDT_FORCE 0x02
  92. #define WDT_ZERO 0x01
  93. /* GPIO Bits WDTCFG */
  94. #define WDT_TOV1 0x80
  95. #define WDT_KRST 0x40
  96. #define WDT_TOVE 0x20
  97. #define WDT_PWROK 0x10 /* not in it8721 */
  98. #define WDT_INT_MASK 0x0f
  99. /* CIR Configuration Register LDN=0x0a */
  100. #define CIR_ILS 0x70
  101. /* The default Base address is not always available, we use this */
  102. #define CIR_BASE 0x0208
  103. /* CIR Controller */
  104. #define CIR_DR(b) (b)
  105. #define CIR_IER(b) (b + 1)
  106. #define CIR_RCR(b) (b + 2)
  107. #define CIR_TCR1(b) (b + 3)
  108. #define CIR_TCR2(b) (b + 4)
  109. #define CIR_TSR(b) (b + 5)
  110. #define CIR_RSR(b) (b + 6)
  111. #define CIR_BDLR(b) (b + 5)
  112. #define CIR_BDHR(b) (b + 6)
  113. #define CIR_IIR(b) (b + 7)
  114. /* Default Base address of Game port */
  115. #define GP_BASE_DEFAULT 0x0201
  116. /* wdt_status */
  117. #define WDTS_TIMER_RUN 0
  118. #define WDTS_DEV_OPEN 1
  119. #define WDTS_KEEPALIVE 2
  120. #define WDTS_LOCKED 3
  121. #define WDTS_USE_GP 4
  122. #define WDTS_EXPECTED 5
  123. #define WDTS_USE_CIR 6
  124. static unsigned int base, gpact, ciract, max_units, chip_type;
  125. static unsigned long wdt_status;
  126. static int nogameport = DEFAULT_NOGAMEPORT;
  127. static int nocir = DEFAULT_NOCIR;
  128. static int exclusive = DEFAULT_EXCLUSIVE;
  129. static int timeout = DEFAULT_TIMEOUT;
  130. static int testmode = DEFAULT_TESTMODE;
  131. static bool nowayout = DEFAULT_NOWAYOUT;
  132. module_param(nogameport, int, 0);
  133. MODULE_PARM_DESC(nogameport, "Forbid the activation of game port, default="
  134. __MODULE_STRING(DEFAULT_NOGAMEPORT));
  135. module_param(nocir, int, 0);
  136. MODULE_PARM_DESC(nocir, "Forbid the use of Consumer IR interrupts to reset timer, default="
  137. __MODULE_STRING(DEFAULT_NOCIR));
  138. module_param(exclusive, int, 0);
  139. MODULE_PARM_DESC(exclusive, "Watchdog exclusive device open, default="
  140. __MODULE_STRING(DEFAULT_EXCLUSIVE));
  141. module_param(timeout, int, 0);
  142. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
  143. __MODULE_STRING(DEFAULT_TIMEOUT));
  144. module_param(testmode, int, 0);
  145. MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
  146. __MODULE_STRING(DEFAULT_TESTMODE));
  147. module_param(nowayout, bool, 0);
  148. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
  149. __MODULE_STRING(WATCHDOG_NOWAYOUT));
  150. /* Superio Chip */
  151. static inline int superio_enter(void)
  152. {
  153. /*
  154. * Try to reserve REG and REG + 1 for exclusive access.
  155. */
  156. if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
  157. return -EBUSY;
  158. outb(0x87, REG);
  159. outb(0x01, REG);
  160. outb(0x55, REG);
  161. outb(0x55, REG);
  162. return 0;
  163. }
  164. static inline void superio_exit(void)
  165. {
  166. outb(0x02, REG);
  167. outb(0x02, VAL);
  168. release_region(REG, 2);
  169. }
  170. static inline void superio_select(int ldn)
  171. {
  172. outb(LDNREG, REG);
  173. outb(ldn, VAL);
  174. }
  175. static inline int superio_inb(int reg)
  176. {
  177. outb(reg, REG);
  178. return inb(VAL);
  179. }
  180. static inline void superio_outb(int val, int reg)
  181. {
  182. outb(reg, REG);
  183. outb(val, VAL);
  184. }
  185. static inline int superio_inw(int reg)
  186. {
  187. int val;
  188. outb(reg++, REG);
  189. val = inb(VAL) << 8;
  190. outb(reg, REG);
  191. val |= inb(VAL);
  192. return val;
  193. }
  194. static inline void superio_outw(int val, int reg)
  195. {
  196. outb(reg++, REG);
  197. outb(val >> 8, VAL);
  198. outb(reg, REG);
  199. outb(val, VAL);
  200. }
  201. /* Internal function, should be called after superio_select(GPIO) */
  202. static void wdt_update_timeout(void)
  203. {
  204. unsigned char cfg = WDT_KRST;
  205. int tm = timeout;
  206. if (testmode)
  207. cfg = 0;
  208. if (tm <= max_units)
  209. cfg |= WDT_TOV1;
  210. else
  211. tm /= 60;
  212. if (chip_type != IT8721_ID)
  213. cfg |= WDT_PWROK;
  214. superio_outb(cfg, WDTCFG);
  215. superio_outb(tm, WDTVALLSB);
  216. if (max_units > 255)
  217. superio_outb(tm>>8, WDTVALMSB);
  218. }
  219. static int wdt_round_time(int t)
  220. {
  221. t += 59;
  222. t -= t % 60;
  223. return t;
  224. }
  225. /* watchdog timer handling */
  226. static void wdt_keepalive(void)
  227. {
  228. if (test_bit(WDTS_USE_GP, &wdt_status))
  229. inb(base);
  230. else if (test_bit(WDTS_USE_CIR, &wdt_status))
  231. /* The timer reloads with around 5 msec delay */
  232. outb(0x55, CIR_DR(base));
  233. else {
  234. if (superio_enter())
  235. return;
  236. superio_select(GPIO);
  237. wdt_update_timeout();
  238. superio_exit();
  239. }
  240. set_bit(WDTS_KEEPALIVE, &wdt_status);
  241. }
  242. static int wdt_start(void)
  243. {
  244. int ret = superio_enter();
  245. if (ret)
  246. return ret;
  247. superio_select(GPIO);
  248. if (test_bit(WDTS_USE_GP, &wdt_status))
  249. superio_outb(WDT_GAMEPORT, WDTCTRL);
  250. else if (test_bit(WDTS_USE_CIR, &wdt_status))
  251. superio_outb(WDT_CIRINT, WDTCTRL);
  252. wdt_update_timeout();
  253. superio_exit();
  254. return 0;
  255. }
  256. static int wdt_stop(void)
  257. {
  258. int ret = superio_enter();
  259. if (ret)
  260. return ret;
  261. superio_select(GPIO);
  262. superio_outb(0x00, WDTCTRL);
  263. superio_outb(WDT_TOV1, WDTCFG);
  264. superio_outb(0x00, WDTVALLSB);
  265. if (max_units > 255)
  266. superio_outb(0x00, WDTVALMSB);
  267. superio_exit();
  268. return 0;
  269. }
  270. /**
  271. * wdt_set_timeout - set a new timeout value with watchdog ioctl
  272. * @t: timeout value in seconds
  273. *
  274. * The hardware device has a 8 or 16 bit watchdog timer (depends on
  275. * chip version) that can be configured to count seconds or minutes.
  276. *
  277. * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
  278. */
  279. static int wdt_set_timeout(int t)
  280. {
  281. if (t < 1 || t > max_units * 60)
  282. return -EINVAL;
  283. if (t > max_units)
  284. timeout = wdt_round_time(t);
  285. else
  286. timeout = t;
  287. if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
  288. int ret = superio_enter();
  289. if (ret)
  290. return ret;
  291. superio_select(GPIO);
  292. wdt_update_timeout();
  293. superio_exit();
  294. }
  295. return 0;
  296. }
  297. /**
  298. * wdt_get_status - determines the status supported by watchdog ioctl
  299. * @status: status returned to user space
  300. *
  301. * The status bit of the device does not allow to distinguish
  302. * between a regular system reset and a watchdog forced reset.
  303. * But, in test mode it is useful, so it is supported through
  304. * WDIOC_GETSTATUS watchdog ioctl. Additionally the driver
  305. * reports the keepalive signal and the acception of the magic.
  306. *
  307. * Used within WDIOC_GETSTATUS watchdog device ioctl.
  308. */
  309. static int wdt_get_status(int *status)
  310. {
  311. *status = 0;
  312. if (testmode) {
  313. int ret = superio_enter();
  314. if (ret)
  315. return ret;
  316. superio_select(GPIO);
  317. if (superio_inb(WDTCTRL) & WDT_ZERO) {
  318. superio_outb(0x00, WDTCTRL);
  319. clear_bit(WDTS_TIMER_RUN, &wdt_status);
  320. *status |= WDIOF_CARDRESET;
  321. }
  322. superio_exit();
  323. }
  324. if (test_and_clear_bit(WDTS_KEEPALIVE, &wdt_status))
  325. *status |= WDIOF_KEEPALIVEPING;
  326. if (test_bit(WDTS_EXPECTED, &wdt_status))
  327. *status |= WDIOF_MAGICCLOSE;
  328. return 0;
  329. }
  330. /* /dev/watchdog handling */
  331. /**
  332. * wdt_open - watchdog file_operations .open
  333. * @inode: inode of the device
  334. * @file: file handle to the device
  335. *
  336. * The watchdog timer starts by opening the device.
  337. *
  338. * Used within the file operation of the watchdog device.
  339. */
  340. static int wdt_open(struct inode *inode, struct file *file)
  341. {
  342. if (exclusive && test_and_set_bit(WDTS_DEV_OPEN, &wdt_status))
  343. return -EBUSY;
  344. if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
  345. int ret;
  346. if (nowayout && !test_and_set_bit(WDTS_LOCKED, &wdt_status))
  347. __module_get(THIS_MODULE);
  348. ret = wdt_start();
  349. if (ret) {
  350. clear_bit(WDTS_LOCKED, &wdt_status);
  351. clear_bit(WDTS_TIMER_RUN, &wdt_status);
  352. clear_bit(WDTS_DEV_OPEN, &wdt_status);
  353. return ret;
  354. }
  355. }
  356. return nonseekable_open(inode, file);
  357. }
  358. /**
  359. * wdt_release - watchdog file_operations .release
  360. * @inode: inode of the device
  361. * @file: file handle to the device
  362. *
  363. * Closing the watchdog device either stops the watchdog timer
  364. * or in the case, that nowayout is set or the magic character
  365. * wasn't written, a critical warning about an running watchdog
  366. * timer is given.
  367. *
  368. * Used within the file operation of the watchdog device.
  369. */
  370. static int wdt_release(struct inode *inode, struct file *file)
  371. {
  372. if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
  373. if (test_and_clear_bit(WDTS_EXPECTED, &wdt_status)) {
  374. int ret = wdt_stop();
  375. if (ret) {
  376. /*
  377. * Stop failed. Just keep the watchdog alive
  378. * and hope nothing bad happens.
  379. */
  380. set_bit(WDTS_EXPECTED, &wdt_status);
  381. wdt_keepalive();
  382. return ret;
  383. }
  384. clear_bit(WDTS_TIMER_RUN, &wdt_status);
  385. } else {
  386. wdt_keepalive();
  387. pr_crit("unexpected close, not stopping watchdog!\n");
  388. }
  389. }
  390. clear_bit(WDTS_DEV_OPEN, &wdt_status);
  391. return 0;
  392. }
  393. /**
  394. * wdt_write - watchdog file_operations .write
  395. * @file: file handle to the watchdog
  396. * @buf: buffer to write
  397. * @count: count of bytes
  398. * @ppos: pointer to the position to write. No seeks allowed
  399. *
  400. * A write to a watchdog device is defined as a keepalive signal. Any
  401. * write of data will do, as we don't define content meaning.
  402. *
  403. * Used within the file operation of the watchdog device.
  404. */
  405. static ssize_t wdt_write(struct file *file, const char __user *buf,
  406. size_t count, loff_t *ppos)
  407. {
  408. if (count) {
  409. clear_bit(WDTS_EXPECTED, &wdt_status);
  410. wdt_keepalive();
  411. }
  412. if (!nowayout) {
  413. size_t ofs;
  414. /* note: just in case someone wrote the magic character long ago */
  415. for (ofs = 0; ofs != count; ofs++) {
  416. char c;
  417. if (get_user(c, buf + ofs))
  418. return -EFAULT;
  419. if (c == WD_MAGIC)
  420. set_bit(WDTS_EXPECTED, &wdt_status);
  421. }
  422. }
  423. return count;
  424. }
  425. static const struct watchdog_info ident = {
  426. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  427. .firmware_version = 1,
  428. .identity = WATCHDOG_NAME,
  429. };
  430. /**
  431. * wdt_ioctl - watchdog file_operations .unlocked_ioctl
  432. * @file: file handle to the device
  433. * @cmd: watchdog command
  434. * @arg: argument pointer
  435. *
  436. * The watchdog API defines a common set of functions for all watchdogs
  437. * according to their available features.
  438. *
  439. * Used within the file operation of the watchdog device.
  440. */
  441. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  442. {
  443. int rc = 0, status, new_options, new_timeout;
  444. union {
  445. struct watchdog_info __user *ident;
  446. int __user *i;
  447. } uarg;
  448. uarg.i = (int __user *)arg;
  449. switch (cmd) {
  450. case WDIOC_GETSUPPORT:
  451. return copy_to_user(uarg.ident,
  452. &ident, sizeof(ident)) ? -EFAULT : 0;
  453. case WDIOC_GETSTATUS:
  454. rc = wdt_get_status(&status);
  455. if (rc)
  456. return rc;
  457. return put_user(status, uarg.i);
  458. case WDIOC_GETBOOTSTATUS:
  459. return put_user(0, uarg.i);
  460. case WDIOC_KEEPALIVE:
  461. wdt_keepalive();
  462. return 0;
  463. case WDIOC_SETOPTIONS:
  464. if (get_user(new_options, uarg.i))
  465. return -EFAULT;
  466. switch (new_options) {
  467. case WDIOS_DISABLECARD:
  468. if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
  469. rc = wdt_stop();
  470. if (rc)
  471. return rc;
  472. }
  473. clear_bit(WDTS_TIMER_RUN, &wdt_status);
  474. return 0;
  475. case WDIOS_ENABLECARD:
  476. if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
  477. rc = wdt_start();
  478. if (rc) {
  479. clear_bit(WDTS_TIMER_RUN, &wdt_status);
  480. return rc;
  481. }
  482. }
  483. return 0;
  484. default:
  485. return -EFAULT;
  486. }
  487. case WDIOC_SETTIMEOUT:
  488. if (get_user(new_timeout, uarg.i))
  489. return -EFAULT;
  490. rc = wdt_set_timeout(new_timeout);
  491. case WDIOC_GETTIMEOUT:
  492. if (put_user(timeout, uarg.i))
  493. return -EFAULT;
  494. return rc;
  495. default:
  496. return -ENOTTY;
  497. }
  498. }
  499. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  500. void *unused)
  501. {
  502. if (code == SYS_DOWN || code == SYS_HALT)
  503. wdt_stop();
  504. return NOTIFY_DONE;
  505. }
  506. static const struct file_operations wdt_fops = {
  507. .owner = THIS_MODULE,
  508. .llseek = no_llseek,
  509. .write = wdt_write,
  510. .unlocked_ioctl = wdt_ioctl,
  511. .open = wdt_open,
  512. .release = wdt_release,
  513. };
  514. static struct miscdevice wdt_miscdev = {
  515. .minor = WATCHDOG_MINOR,
  516. .name = "watchdog",
  517. .fops = &wdt_fops,
  518. };
  519. static struct notifier_block wdt_notifier = {
  520. .notifier_call = wdt_notify_sys,
  521. };
  522. static int __init it87_wdt_init(void)
  523. {
  524. int rc = 0;
  525. int try_gameport = !nogameport;
  526. u8 chip_rev;
  527. int gp_rreq_fail = 0;
  528. wdt_status = 0;
  529. rc = superio_enter();
  530. if (rc)
  531. return rc;
  532. chip_type = superio_inw(CHIPID);
  533. chip_rev = superio_inb(CHIPREV) & 0x0f;
  534. superio_exit();
  535. switch (chip_type) {
  536. case IT8702_ID:
  537. max_units = 255;
  538. break;
  539. case IT8712_ID:
  540. max_units = (chip_rev < 8) ? 255 : 65535;
  541. break;
  542. case IT8716_ID:
  543. case IT8726_ID:
  544. max_units = 65535;
  545. break;
  546. case IT8718_ID:
  547. case IT8720_ID:
  548. case IT8721_ID:
  549. case IT8728_ID:
  550. max_units = 65535;
  551. try_gameport = 0;
  552. break;
  553. case IT8705_ID:
  554. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  555. chip_type, chip_rev);
  556. return -ENODEV;
  557. case NO_DEV_ID:
  558. pr_err("no device\n");
  559. return -ENODEV;
  560. default:
  561. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  562. chip_type, chip_rev);
  563. return -ENODEV;
  564. }
  565. rc = superio_enter();
  566. if (rc)
  567. return rc;
  568. superio_select(GPIO);
  569. superio_outb(WDT_TOV1, WDTCFG);
  570. superio_outb(0x00, WDTCTRL);
  571. /* First try to get Gameport support */
  572. if (try_gameport) {
  573. superio_select(GAMEPORT);
  574. base = superio_inw(BASEREG);
  575. if (!base) {
  576. base = GP_BASE_DEFAULT;
  577. superio_outw(base, BASEREG);
  578. }
  579. gpact = superio_inb(ACTREG);
  580. superio_outb(0x01, ACTREG);
  581. if (request_region(base, 1, WATCHDOG_NAME))
  582. set_bit(WDTS_USE_GP, &wdt_status);
  583. else
  584. gp_rreq_fail = 1;
  585. }
  586. /* If we haven't Gameport support, try to get CIR support */
  587. if (!nocir && !test_bit(WDTS_USE_GP, &wdt_status)) {
  588. if (!request_region(CIR_BASE, 8, WATCHDOG_NAME)) {
  589. if (gp_rreq_fail)
  590. pr_err("I/O Address 0x%04x and 0x%04x already in use\n",
  591. base, CIR_BASE);
  592. else
  593. pr_err("I/O Address 0x%04x already in use\n",
  594. CIR_BASE);
  595. rc = -EIO;
  596. goto err_out;
  597. }
  598. base = CIR_BASE;
  599. superio_select(CIR);
  600. superio_outw(base, BASEREG);
  601. superio_outb(0x00, CIR_ILS);
  602. ciract = superio_inb(ACTREG);
  603. superio_outb(0x01, ACTREG);
  604. if (gp_rreq_fail) {
  605. superio_select(GAMEPORT);
  606. superio_outb(gpact, ACTREG);
  607. }
  608. set_bit(WDTS_USE_CIR, &wdt_status);
  609. }
  610. if (timeout < 1 || timeout > max_units * 60) {
  611. timeout = DEFAULT_TIMEOUT;
  612. pr_warn("Timeout value out of range, use default %d sec\n",
  613. DEFAULT_TIMEOUT);
  614. }
  615. if (timeout > max_units)
  616. timeout = wdt_round_time(timeout);
  617. rc = register_reboot_notifier(&wdt_notifier);
  618. if (rc) {
  619. pr_err("Cannot register reboot notifier (err=%d)\n", rc);
  620. goto err_out_region;
  621. }
  622. rc = misc_register(&wdt_miscdev);
  623. if (rc) {
  624. pr_err("Cannot register miscdev on minor=%d (err=%d)\n",
  625. wdt_miscdev.minor, rc);
  626. goto err_out_reboot;
  627. }
  628. /* Initialize CIR to use it as keepalive source */
  629. if (test_bit(WDTS_USE_CIR, &wdt_status)) {
  630. outb(0x00, CIR_RCR(base));
  631. outb(0xc0, CIR_TCR1(base));
  632. outb(0x5c, CIR_TCR2(base));
  633. outb(0x10, CIR_IER(base));
  634. outb(0x00, CIR_BDHR(base));
  635. outb(0x01, CIR_BDLR(base));
  636. outb(0x09, CIR_IER(base));
  637. }
  638. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d exclusive=%d nogameport=%d nocir=%d)\n",
  639. chip_type, chip_rev, timeout,
  640. nowayout, testmode, exclusive, nogameport, nocir);
  641. superio_exit();
  642. return 0;
  643. err_out_reboot:
  644. unregister_reboot_notifier(&wdt_notifier);
  645. err_out_region:
  646. if (test_bit(WDTS_USE_GP, &wdt_status))
  647. release_region(base, 1);
  648. else if (test_bit(WDTS_USE_CIR, &wdt_status)) {
  649. release_region(base, 8);
  650. superio_select(CIR);
  651. superio_outb(ciract, ACTREG);
  652. }
  653. err_out:
  654. if (try_gameport) {
  655. superio_select(GAMEPORT);
  656. superio_outb(gpact, ACTREG);
  657. }
  658. superio_exit();
  659. return rc;
  660. }
  661. static void __exit it87_wdt_exit(void)
  662. {
  663. if (superio_enter() == 0) {
  664. superio_select(GPIO);
  665. superio_outb(0x00, WDTCTRL);
  666. superio_outb(0x00, WDTCFG);
  667. superio_outb(0x00, WDTVALLSB);
  668. if (max_units > 255)
  669. superio_outb(0x00, WDTVALMSB);
  670. if (test_bit(WDTS_USE_GP, &wdt_status)) {
  671. superio_select(GAMEPORT);
  672. superio_outb(gpact, ACTREG);
  673. } else if (test_bit(WDTS_USE_CIR, &wdt_status)) {
  674. superio_select(CIR);
  675. superio_outb(ciract, ACTREG);
  676. }
  677. superio_exit();
  678. }
  679. misc_deregister(&wdt_miscdev);
  680. unregister_reboot_notifier(&wdt_notifier);
  681. if (test_bit(WDTS_USE_GP, &wdt_status))
  682. release_region(base, 1);
  683. else if (test_bit(WDTS_USE_CIR, &wdt_status))
  684. release_region(base, 8);
  685. }
  686. module_init(it87_wdt_init);
  687. module_exit(it87_wdt_exit);
  688. MODULE_AUTHOR("Oliver Schuster");
  689. MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
  690. MODULE_LICENSE("GPL");