it87_wdt.c 18 KB

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