f71808e_wdt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /***************************************************************************
  2. * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
  3. * Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com> *
  4. * Copyright (C) 2010 Giel van Schijndel <me@mortis.eu> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <linux/ioport.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <linux/notifier.h>
  30. #include <linux/reboot.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/watchdog.h>
  33. #define DRVNAME "f71808e_wdt"
  34. #define SIO_F71808FG_LD_WDT 0x07 /* Watchdog timer logical device */
  35. #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
  36. #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
  37. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  38. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  39. #define SIO_REG_DEVREV 0x22 /* Device revision */
  40. #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
  41. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  42. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  43. #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
  44. #define SIO_F71808_ID 0x0901 /* Chipset ID */
  45. #define SIO_F71858_ID 0x0507 /* Chipset ID */
  46. #define SIO_F71862_ID 0x0601 /* Chipset ID */
  47. #define SIO_F71882_ID 0x0541 /* Chipset ID */
  48. #define SIO_F71889_ID 0x0723 /* Chipset ID */
  49. #define F71882FG_REG_START 0x01
  50. #define F71808FG_REG_WDO_CONF 0xf0
  51. #define F71808FG_REG_WDT_CONF 0xf5
  52. #define F71808FG_REG_WD_TIME 0xf6
  53. #define F71808FG_FLAG_WDOUT_EN 7
  54. #define F71808FG_FLAG_WDTMOUT_STS 5
  55. #define F71808FG_FLAG_WD_EN 5
  56. #define F71808FG_FLAG_WD_PULSE 4
  57. #define F71808FG_FLAG_WD_UNIT 3
  58. /* Default values */
  59. #define WATCHDOG_TIMEOUT 60 /* 1 minute default timeout */
  60. #define WATCHDOG_MAX_TIMEOUT (60 * 255)
  61. #define WATCHDOG_PULSE_WIDTH 125 /* 125 ms, default pulse width for
  62. watchdog signal */
  63. static unsigned short force_id;
  64. module_param(force_id, ushort, 0);
  65. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  66. static const int max_timeout = WATCHDOG_MAX_TIMEOUT;
  67. static int timeout = 60; /* default timeout in seconds */
  68. module_param(timeout, int, 0);
  69. MODULE_PARM_DESC(timeout,
  70. "Watchdog timeout in seconds. 1<= timeout <="
  71. __MODULE_STRING(WATCHDOG_MAX_TIMEOUT) " (default="
  72. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  73. static unsigned int pulse_width = WATCHDOG_PULSE_WIDTH;
  74. module_param(pulse_width, uint, 0);
  75. MODULE_PARM_DESC(pulse_width,
  76. "Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms"
  77. " (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH) ")");
  78. static int nowayout = WATCHDOG_NOWAYOUT;
  79. module_param(nowayout, bool, 0444);
  80. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  81. static unsigned int start_withtimeout;
  82. module_param(start_withtimeout, uint, 0);
  83. MODULE_PARM_DESC(start_withtimeout, "Start watchdog timer on module load with"
  84. " given initial timeout. Zero (default) disables this feature.");
  85. enum chips { f71808fg, f71858fg, f71862fg, f71882fg, f71889fg };
  86. static const char *f71808e_names[] = {
  87. "f71808fg",
  88. "f71858fg",
  89. "f71862fg",
  90. "f71882fg",
  91. "f71889fg",
  92. };
  93. /* Super-I/O Function prototypes */
  94. static inline int superio_inb(int base, int reg);
  95. static inline int superio_inw(int base, int reg);
  96. static inline void superio_outb(int base, int reg, u8 val);
  97. static inline void superio_set_bit(int base, int reg, int bit);
  98. static inline void superio_clear_bit(int base, int reg, int bit);
  99. static inline int superio_enter(int base);
  100. static inline void superio_select(int base, int ld);
  101. static inline void superio_exit(int base);
  102. struct watchdog_data {
  103. unsigned short sioaddr;
  104. enum chips type;
  105. unsigned long opened;
  106. struct mutex lock;
  107. char expect_close;
  108. struct watchdog_info ident;
  109. unsigned short timeout;
  110. u8 timer_val; /* content for the wd_time register */
  111. char minutes_mode;
  112. u8 pulse_val; /* pulse width flag */
  113. char pulse_mode; /* enable pulse output mode? */
  114. char caused_reboot; /* last reboot was by the watchdog */
  115. };
  116. static struct watchdog_data watchdog = {
  117. .lock = __MUTEX_INITIALIZER(watchdog.lock),
  118. };
  119. /* Super I/O functions */
  120. static inline int superio_inb(int base, int reg)
  121. {
  122. outb(reg, base);
  123. return inb(base + 1);
  124. }
  125. static int superio_inw(int base, int reg)
  126. {
  127. int val;
  128. val = superio_inb(base, reg) << 8;
  129. val |= superio_inb(base, reg + 1);
  130. return val;
  131. }
  132. static inline void superio_outb(int base, int reg, u8 val)
  133. {
  134. outb(reg, base);
  135. outb(val, base + 1);
  136. }
  137. static inline void superio_set_bit(int base, int reg, int bit)
  138. {
  139. unsigned long val = superio_inb(base, reg);
  140. __set_bit(bit, &val);
  141. superio_outb(base, reg, val);
  142. }
  143. static inline void superio_clear_bit(int base, int reg, int bit)
  144. {
  145. unsigned long val = superio_inb(base, reg);
  146. __clear_bit(bit, &val);
  147. superio_outb(base, reg, val);
  148. }
  149. static inline int superio_enter(int base)
  150. {
  151. /* Don't step on other drivers' I/O space by accident */
  152. if (!request_muxed_region(base, 2, DRVNAME)) {
  153. printk(KERN_ERR DRVNAME ": I/O address 0x%04x already in use\n",
  154. (int)base);
  155. return -EBUSY;
  156. }
  157. /* according to the datasheet the key must be send twice! */
  158. outb(SIO_UNLOCK_KEY, base);
  159. outb(SIO_UNLOCK_KEY, base);
  160. return 0;
  161. }
  162. static inline void superio_select(int base, int ld)
  163. {
  164. outb(SIO_REG_LDSEL, base);
  165. outb(ld, base + 1);
  166. }
  167. static inline void superio_exit(int base)
  168. {
  169. outb(SIO_LOCK_KEY, base);
  170. release_region(base, 2);
  171. }
  172. static int watchdog_set_timeout(int timeout)
  173. {
  174. if (timeout <= 0
  175. || timeout > max_timeout) {
  176. printk(KERN_ERR DRVNAME ": watchdog timeout out of range\n");
  177. return -EINVAL;
  178. }
  179. mutex_lock(&watchdog.lock);
  180. watchdog.timeout = timeout;
  181. if (timeout > 0xff) {
  182. watchdog.timer_val = DIV_ROUND_UP(timeout, 60);
  183. watchdog.minutes_mode = true;
  184. } else {
  185. watchdog.timer_val = timeout;
  186. watchdog.minutes_mode = false;
  187. }
  188. mutex_unlock(&watchdog.lock);
  189. return 0;
  190. }
  191. static int watchdog_set_pulse_width(unsigned int pw)
  192. {
  193. int err = 0;
  194. mutex_lock(&watchdog.lock);
  195. if (pw <= 1) {
  196. watchdog.pulse_val = 0;
  197. } else if (pw <= 25) {
  198. watchdog.pulse_val = 1;
  199. } else if (pw <= 125) {
  200. watchdog.pulse_val = 2;
  201. } else if (pw <= 5000) {
  202. watchdog.pulse_val = 3;
  203. } else {
  204. printk(KERN_ERR DRVNAME ": pulse width out of range\n");
  205. err = -EINVAL;
  206. goto exit_unlock;
  207. }
  208. watchdog.pulse_mode = pw;
  209. exit_unlock:
  210. mutex_unlock(&watchdog.lock);
  211. return err;
  212. }
  213. static int watchdog_keepalive(void)
  214. {
  215. int err = 0;
  216. mutex_lock(&watchdog.lock);
  217. err = superio_enter(watchdog.sioaddr);
  218. if (err)
  219. goto exit_unlock;
  220. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  221. if (watchdog.minutes_mode)
  222. /* select minutes for timer units */
  223. superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  224. F71808FG_FLAG_WD_UNIT);
  225. else
  226. /* select seconds for timer units */
  227. superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  228. F71808FG_FLAG_WD_UNIT);
  229. /* Set timer value */
  230. superio_outb(watchdog.sioaddr, F71808FG_REG_WD_TIME,
  231. watchdog.timer_val);
  232. superio_exit(watchdog.sioaddr);
  233. exit_unlock:
  234. mutex_unlock(&watchdog.lock);
  235. return err;
  236. }
  237. static int watchdog_start(void)
  238. {
  239. /* Make sure we don't die as soon as the watchdog is enabled below */
  240. int err = watchdog_keepalive();
  241. if (err)
  242. return err;
  243. mutex_lock(&watchdog.lock);
  244. err = superio_enter(watchdog.sioaddr);
  245. if (err)
  246. goto exit_unlock;
  247. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  248. /* Watchdog pin configuration */
  249. switch (watchdog.type) {
  250. case f71808fg:
  251. /* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */
  252. superio_clear_bit(watchdog.sioaddr, 0x2a, 3);
  253. superio_clear_bit(watchdog.sioaddr, 0x2b, 3);
  254. break;
  255. case f71882fg:
  256. /* Set pin 56 to WDTRST# */
  257. superio_set_bit(watchdog.sioaddr, 0x29, 1);
  258. break;
  259. default:
  260. /*
  261. * 'default' label to shut up the compiler and catch
  262. * programmer errors
  263. */
  264. err = -ENODEV;
  265. goto exit_superio;
  266. }
  267. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  268. superio_set_bit(watchdog.sioaddr, SIO_REG_ENABLE, 0);
  269. superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDO_CONF,
  270. F71808FG_FLAG_WDOUT_EN);
  271. superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  272. F71808FG_FLAG_WD_EN);
  273. if (watchdog.pulse_mode) {
  274. /* Select "pulse" output mode with given duration */
  275. u8 wdt_conf = superio_inb(watchdog.sioaddr,
  276. F71808FG_REG_WDT_CONF);
  277. /* Set WD_PSWIDTH bits (1:0) */
  278. wdt_conf = (wdt_conf & 0xfc) | (watchdog.pulse_val & 0x03);
  279. /* Set WD_PULSE to "pulse" mode */
  280. wdt_conf |= BIT(F71808FG_FLAG_WD_PULSE);
  281. superio_outb(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  282. wdt_conf);
  283. } else {
  284. /* Select "level" output mode */
  285. superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  286. F71808FG_FLAG_WD_PULSE);
  287. }
  288. exit_superio:
  289. superio_exit(watchdog.sioaddr);
  290. exit_unlock:
  291. mutex_unlock(&watchdog.lock);
  292. return err;
  293. }
  294. static int watchdog_stop(void)
  295. {
  296. int err = 0;
  297. mutex_lock(&watchdog.lock);
  298. err = superio_enter(watchdog.sioaddr);
  299. if (err)
  300. goto exit_unlock;
  301. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  302. superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  303. F71808FG_FLAG_WD_EN);
  304. superio_exit(watchdog.sioaddr);
  305. exit_unlock:
  306. mutex_unlock(&watchdog.lock);
  307. return err;
  308. }
  309. static int watchdog_get_status(void)
  310. {
  311. int status = 0;
  312. mutex_lock(&watchdog.lock);
  313. status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0;
  314. mutex_unlock(&watchdog.lock);
  315. return status;
  316. }
  317. static bool watchdog_is_running(void)
  318. {
  319. /*
  320. * if we fail to determine the watchdog's status assume it to be
  321. * running to be on the safe side
  322. */
  323. bool is_running = true;
  324. mutex_lock(&watchdog.lock);
  325. if (superio_enter(watchdog.sioaddr))
  326. goto exit_unlock;
  327. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  328. is_running = (superio_inb(watchdog.sioaddr, SIO_REG_ENABLE) & BIT(0))
  329. && (superio_inb(watchdog.sioaddr, F71808FG_REG_WDT_CONF)
  330. & F71808FG_FLAG_WD_EN);
  331. superio_exit(watchdog.sioaddr);
  332. exit_unlock:
  333. mutex_unlock(&watchdog.lock);
  334. return is_running;
  335. }
  336. /* /dev/watchdog api */
  337. static int watchdog_open(struct inode *inode, struct file *file)
  338. {
  339. int err;
  340. /* If the watchdog is alive we don't need to start it again */
  341. if (test_and_set_bit(0, &watchdog.opened))
  342. return -EBUSY;
  343. err = watchdog_start();
  344. if (err) {
  345. clear_bit(0, &watchdog.opened);
  346. return err;
  347. }
  348. if (nowayout)
  349. __module_get(THIS_MODULE);
  350. watchdog.expect_close = 0;
  351. return nonseekable_open(inode, file);
  352. }
  353. static int watchdog_release(struct inode *inode, struct file *file)
  354. {
  355. clear_bit(0, &watchdog.opened);
  356. if (!watchdog.expect_close) {
  357. watchdog_keepalive();
  358. printk(KERN_CRIT DRVNAME
  359. ": Unexpected close, not stopping watchdog!\n");
  360. } else if (!nowayout) {
  361. watchdog_stop();
  362. }
  363. return 0;
  364. }
  365. /*
  366. * watchdog_write:
  367. * @file: file handle to the watchdog
  368. * @buf: buffer to write
  369. * @count: count of bytes
  370. * @ppos: pointer to the position to write. No seeks allowed
  371. *
  372. * A write to a watchdog device is defined as a keepalive signal. Any
  373. * write of data will do, as we we don't define content meaning.
  374. */
  375. static ssize_t watchdog_write(struct file *file, const char __user *buf,
  376. size_t count, loff_t *ppos)
  377. {
  378. if (count) {
  379. if (!nowayout) {
  380. size_t i;
  381. /* In case it was set long ago */
  382. bool expect_close = false;
  383. for (i = 0; i != count; i++) {
  384. char c;
  385. if (get_user(c, buf + i))
  386. return -EFAULT;
  387. expect_close = (c == 'V');
  388. }
  389. /* Properly order writes across fork()ed processes */
  390. mutex_lock(&watchdog.lock);
  391. watchdog.expect_close = expect_close;
  392. mutex_unlock(&watchdog.lock);
  393. }
  394. /* someone wrote to us, we should restart timer */
  395. watchdog_keepalive();
  396. }
  397. return count;
  398. }
  399. /*
  400. * watchdog_ioctl:
  401. * @inode: inode of the device
  402. * @file: file handle to the device
  403. * @cmd: watchdog command
  404. * @arg: argument pointer
  405. *
  406. * The watchdog API defines a common set of functions for all watchdogs
  407. * according to their available features.
  408. */
  409. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  410. unsigned long arg)
  411. {
  412. int status;
  413. int new_options;
  414. int new_timeout;
  415. union {
  416. struct watchdog_info __user *ident;
  417. int __user *i;
  418. } uarg;
  419. uarg.i = (int __user *)arg;
  420. switch (cmd) {
  421. case WDIOC_GETSUPPORT:
  422. return copy_to_user(uarg.ident, &watchdog.ident,
  423. sizeof(watchdog.ident)) ? -EFAULT : 0;
  424. case WDIOC_GETSTATUS:
  425. status = watchdog_get_status();
  426. if (status < 0)
  427. return status;
  428. return put_user(status, uarg.i);
  429. case WDIOC_GETBOOTSTATUS:
  430. return put_user(0, uarg.i);
  431. case WDIOC_SETOPTIONS:
  432. if (get_user(new_options, uarg.i))
  433. return -EFAULT;
  434. if (new_options & WDIOS_DISABLECARD)
  435. watchdog_stop();
  436. if (new_options & WDIOS_ENABLECARD)
  437. return watchdog_start();
  438. case WDIOC_KEEPALIVE:
  439. watchdog_keepalive();
  440. return 0;
  441. case WDIOC_SETTIMEOUT:
  442. if (get_user(new_timeout, uarg.i))
  443. return -EFAULT;
  444. if (watchdog_set_timeout(new_timeout))
  445. return -EINVAL;
  446. watchdog_keepalive();
  447. /* Fall */
  448. case WDIOC_GETTIMEOUT:
  449. return put_user(watchdog.timeout, uarg.i);
  450. default:
  451. return -ENOTTY;
  452. }
  453. }
  454. static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  455. void *unused)
  456. {
  457. if (code == SYS_DOWN || code == SYS_HALT)
  458. watchdog_stop();
  459. return NOTIFY_DONE;
  460. }
  461. static const struct file_operations watchdog_fops = {
  462. .owner = THIS_MODULE,
  463. .llseek = no_llseek,
  464. .open = watchdog_open,
  465. .release = watchdog_release,
  466. .write = watchdog_write,
  467. .unlocked_ioctl = watchdog_ioctl,
  468. };
  469. static struct miscdevice watchdog_miscdev = {
  470. .minor = WATCHDOG_MINOR,
  471. .name = "watchdog",
  472. .fops = &watchdog_fops,
  473. };
  474. static struct notifier_block watchdog_notifier = {
  475. .notifier_call = watchdog_notify_sys,
  476. };
  477. static int __init watchdog_init(int sioaddr)
  478. {
  479. int wdt_conf, err = 0;
  480. /* No need to lock watchdog.lock here because no entry points
  481. * into the module have been registered yet.
  482. */
  483. watchdog.sioaddr = sioaddr;
  484. watchdog.ident.options = WDIOC_SETTIMEOUT
  485. | WDIOF_MAGICCLOSE
  486. | WDIOF_KEEPALIVEPING;
  487. snprintf(watchdog.ident.identity,
  488. sizeof(watchdog.ident.identity), "%s watchdog",
  489. f71808e_names[watchdog.type]);
  490. err = superio_enter(sioaddr);
  491. if (err)
  492. return err;
  493. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  494. wdt_conf = superio_inb(sioaddr, F71808FG_REG_WDT_CONF);
  495. watchdog.caused_reboot = wdt_conf & F71808FG_FLAG_WDTMOUT_STS;
  496. superio_exit(sioaddr);
  497. err = watchdog_set_timeout(timeout);
  498. if (err)
  499. return err;
  500. err = watchdog_set_pulse_width(pulse_width);
  501. if (err)
  502. return err;
  503. err = register_reboot_notifier(&watchdog_notifier);
  504. if (err)
  505. return err;
  506. err = misc_register(&watchdog_miscdev);
  507. if (err) {
  508. printk(KERN_ERR DRVNAME
  509. ": cannot register miscdev on minor=%d\n",
  510. watchdog_miscdev.minor);
  511. goto exit_reboot;
  512. }
  513. if (start_withtimeout) {
  514. if (start_withtimeout <= 0
  515. || start_withtimeout > max_timeout) {
  516. printk(KERN_ERR DRVNAME
  517. ": starting timeout out of range\n");
  518. err = -EINVAL;
  519. goto exit_miscdev;
  520. }
  521. err = watchdog_start();
  522. if (err) {
  523. printk(KERN_ERR DRVNAME
  524. ": cannot start watchdog timer\n");
  525. goto exit_miscdev;
  526. }
  527. mutex_lock(&watchdog.lock);
  528. err = superio_enter(sioaddr);
  529. if (err)
  530. goto exit_unlock;
  531. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  532. if (start_withtimeout > 0xff) {
  533. /* select minutes for timer units */
  534. superio_set_bit(sioaddr, F71808FG_REG_WDT_CONF,
  535. F71808FG_FLAG_WD_UNIT);
  536. superio_outb(sioaddr, F71808FG_REG_WD_TIME,
  537. DIV_ROUND_UP(start_withtimeout, 60));
  538. } else {
  539. /* select seconds for timer units */
  540. superio_clear_bit(sioaddr, F71808FG_REG_WDT_CONF,
  541. F71808FG_FLAG_WD_UNIT);
  542. superio_outb(sioaddr, F71808FG_REG_WD_TIME,
  543. start_withtimeout);
  544. }
  545. superio_exit(sioaddr);
  546. mutex_unlock(&watchdog.lock);
  547. if (nowayout)
  548. __module_get(THIS_MODULE);
  549. printk(KERN_INFO DRVNAME
  550. ": watchdog started with initial timeout of %u sec\n",
  551. start_withtimeout);
  552. }
  553. return 0;
  554. exit_unlock:
  555. mutex_unlock(&watchdog.lock);
  556. exit_miscdev:
  557. misc_deregister(&watchdog_miscdev);
  558. exit_reboot:
  559. unregister_reboot_notifier(&watchdog_notifier);
  560. return err;
  561. }
  562. static int __init f71808e_find(int sioaddr)
  563. {
  564. u16 devid;
  565. int err = superio_enter(sioaddr);
  566. if (err)
  567. return err;
  568. devid = superio_inw(sioaddr, SIO_REG_MANID);
  569. if (devid != SIO_FINTEK_ID) {
  570. pr_debug(DRVNAME ": Not a Fintek device\n");
  571. err = -ENODEV;
  572. goto exit;
  573. }
  574. devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
  575. switch (devid) {
  576. case SIO_F71808_ID:
  577. watchdog.type = f71808fg;
  578. break;
  579. case SIO_F71882_ID:
  580. watchdog.type = f71882fg;
  581. break;
  582. case SIO_F71862_ID:
  583. case SIO_F71889_ID:
  584. /* These have a watchdog, though it isn't implemented (yet). */
  585. err = -ENOSYS;
  586. goto exit;
  587. case SIO_F71858_ID:
  588. /* Confirmed (by datasheet) not to have a watchdog. */
  589. err = -ENODEV;
  590. goto exit;
  591. default:
  592. printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n",
  593. (unsigned int)devid);
  594. err = -ENODEV;
  595. goto exit;
  596. }
  597. printk(KERN_INFO DRVNAME ": Found %s watchdog chip, revision %d\n",
  598. f71808e_names[watchdog.type],
  599. (int)superio_inb(sioaddr, SIO_REG_DEVREV));
  600. exit:
  601. superio_exit(sioaddr);
  602. return err;
  603. }
  604. static int __init f71808e_init(void)
  605. {
  606. static const unsigned short addrs[] = { 0x2e, 0x4e };
  607. int err = -ENODEV;
  608. int i;
  609. for (i = 0; i < ARRAY_SIZE(addrs); i++) {
  610. err = f71808e_find(addrs[i]);
  611. if (err == 0)
  612. break;
  613. }
  614. if (i == ARRAY_SIZE(addrs))
  615. return err;
  616. return watchdog_init(addrs[i]);
  617. }
  618. static void __exit f71808e_exit(void)
  619. {
  620. if (watchdog_is_running()) {
  621. printk(KERN_WARNING DRVNAME
  622. ": Watchdog timer still running, stopping it\n");
  623. watchdog_stop();
  624. }
  625. misc_deregister(&watchdog_miscdev);
  626. unregister_reboot_notifier(&watchdog_notifier);
  627. }
  628. MODULE_DESCRIPTION("F71808E Watchdog Driver");
  629. MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
  630. MODULE_LICENSE("GPL");
  631. module_init(f71808e_init);
  632. module_exit(f71808e_exit);