it87_wdt.c 18 KB

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