f71808e_wdt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. case f71889fg:
  260. /* set pin 40 to WDTRST# */
  261. superio_outb(watchdog.sioaddr, 0x2b,
  262. superio_inb(watchdog.sioaddr, 0x2b) & 0xcf);
  263. break;
  264. default:
  265. /*
  266. * 'default' label to shut up the compiler and catch
  267. * programmer errors
  268. */
  269. err = -ENODEV;
  270. goto exit_superio;
  271. }
  272. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  273. superio_set_bit(watchdog.sioaddr, SIO_REG_ENABLE, 0);
  274. superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDO_CONF,
  275. F71808FG_FLAG_WDOUT_EN);
  276. superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  277. F71808FG_FLAG_WD_EN);
  278. if (watchdog.pulse_mode) {
  279. /* Select "pulse" output mode with given duration */
  280. u8 wdt_conf = superio_inb(watchdog.sioaddr,
  281. F71808FG_REG_WDT_CONF);
  282. /* Set WD_PSWIDTH bits (1:0) */
  283. wdt_conf = (wdt_conf & 0xfc) | (watchdog.pulse_val & 0x03);
  284. /* Set WD_PULSE to "pulse" mode */
  285. wdt_conf |= BIT(F71808FG_FLAG_WD_PULSE);
  286. superio_outb(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  287. wdt_conf);
  288. } else {
  289. /* Select "level" output mode */
  290. superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  291. F71808FG_FLAG_WD_PULSE);
  292. }
  293. exit_superio:
  294. superio_exit(watchdog.sioaddr);
  295. exit_unlock:
  296. mutex_unlock(&watchdog.lock);
  297. return err;
  298. }
  299. static int watchdog_stop(void)
  300. {
  301. int err = 0;
  302. mutex_lock(&watchdog.lock);
  303. err = superio_enter(watchdog.sioaddr);
  304. if (err)
  305. goto exit_unlock;
  306. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  307. superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
  308. F71808FG_FLAG_WD_EN);
  309. superio_exit(watchdog.sioaddr);
  310. exit_unlock:
  311. mutex_unlock(&watchdog.lock);
  312. return err;
  313. }
  314. static int watchdog_get_status(void)
  315. {
  316. int status = 0;
  317. mutex_lock(&watchdog.lock);
  318. status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0;
  319. mutex_unlock(&watchdog.lock);
  320. return status;
  321. }
  322. static bool watchdog_is_running(void)
  323. {
  324. /*
  325. * if we fail to determine the watchdog's status assume it to be
  326. * running to be on the safe side
  327. */
  328. bool is_running = true;
  329. mutex_lock(&watchdog.lock);
  330. if (superio_enter(watchdog.sioaddr))
  331. goto exit_unlock;
  332. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  333. is_running = (superio_inb(watchdog.sioaddr, SIO_REG_ENABLE) & BIT(0))
  334. && (superio_inb(watchdog.sioaddr, F71808FG_REG_WDT_CONF)
  335. & F71808FG_FLAG_WD_EN);
  336. superio_exit(watchdog.sioaddr);
  337. exit_unlock:
  338. mutex_unlock(&watchdog.lock);
  339. return is_running;
  340. }
  341. /* /dev/watchdog api */
  342. static int watchdog_open(struct inode *inode, struct file *file)
  343. {
  344. int err;
  345. /* If the watchdog is alive we don't need to start it again */
  346. if (test_and_set_bit(0, &watchdog.opened))
  347. return -EBUSY;
  348. err = watchdog_start();
  349. if (err) {
  350. clear_bit(0, &watchdog.opened);
  351. return err;
  352. }
  353. if (nowayout)
  354. __module_get(THIS_MODULE);
  355. watchdog.expect_close = 0;
  356. return nonseekable_open(inode, file);
  357. }
  358. static int watchdog_release(struct inode *inode, struct file *file)
  359. {
  360. clear_bit(0, &watchdog.opened);
  361. if (!watchdog.expect_close) {
  362. watchdog_keepalive();
  363. printk(KERN_CRIT DRVNAME
  364. ": Unexpected close, not stopping watchdog!\n");
  365. } else if (!nowayout) {
  366. watchdog_stop();
  367. }
  368. return 0;
  369. }
  370. /*
  371. * watchdog_write:
  372. * @file: file handle to the watchdog
  373. * @buf: buffer to write
  374. * @count: count of bytes
  375. * @ppos: pointer to the position to write. No seeks allowed
  376. *
  377. * A write to a watchdog device is defined as a keepalive signal. Any
  378. * write of data will do, as we we don't define content meaning.
  379. */
  380. static ssize_t watchdog_write(struct file *file, const char __user *buf,
  381. size_t count, loff_t *ppos)
  382. {
  383. if (count) {
  384. if (!nowayout) {
  385. size_t i;
  386. /* In case it was set long ago */
  387. bool expect_close = false;
  388. for (i = 0; i != count; i++) {
  389. char c;
  390. if (get_user(c, buf + i))
  391. return -EFAULT;
  392. expect_close = (c == 'V');
  393. }
  394. /* Properly order writes across fork()ed processes */
  395. mutex_lock(&watchdog.lock);
  396. watchdog.expect_close = expect_close;
  397. mutex_unlock(&watchdog.lock);
  398. }
  399. /* someone wrote to us, we should restart timer */
  400. watchdog_keepalive();
  401. }
  402. return count;
  403. }
  404. /*
  405. * watchdog_ioctl:
  406. * @inode: inode of the device
  407. * @file: file handle to the device
  408. * @cmd: watchdog command
  409. * @arg: argument pointer
  410. *
  411. * The watchdog API defines a common set of functions for all watchdogs
  412. * according to their available features.
  413. */
  414. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  415. unsigned long arg)
  416. {
  417. int status;
  418. int new_options;
  419. int new_timeout;
  420. union {
  421. struct watchdog_info __user *ident;
  422. int __user *i;
  423. } uarg;
  424. uarg.i = (int __user *)arg;
  425. switch (cmd) {
  426. case WDIOC_GETSUPPORT:
  427. return copy_to_user(uarg.ident, &watchdog.ident,
  428. sizeof(watchdog.ident)) ? -EFAULT : 0;
  429. case WDIOC_GETSTATUS:
  430. status = watchdog_get_status();
  431. if (status < 0)
  432. return status;
  433. return put_user(status, uarg.i);
  434. case WDIOC_GETBOOTSTATUS:
  435. return put_user(0, uarg.i);
  436. case WDIOC_SETOPTIONS:
  437. if (get_user(new_options, uarg.i))
  438. return -EFAULT;
  439. if (new_options & WDIOS_DISABLECARD)
  440. watchdog_stop();
  441. if (new_options & WDIOS_ENABLECARD)
  442. return watchdog_start();
  443. case WDIOC_KEEPALIVE:
  444. watchdog_keepalive();
  445. return 0;
  446. case WDIOC_SETTIMEOUT:
  447. if (get_user(new_timeout, uarg.i))
  448. return -EFAULT;
  449. if (watchdog_set_timeout(new_timeout))
  450. return -EINVAL;
  451. watchdog_keepalive();
  452. /* Fall */
  453. case WDIOC_GETTIMEOUT:
  454. return put_user(watchdog.timeout, uarg.i);
  455. default:
  456. return -ENOTTY;
  457. }
  458. }
  459. static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  460. void *unused)
  461. {
  462. if (code == SYS_DOWN || code == SYS_HALT)
  463. watchdog_stop();
  464. return NOTIFY_DONE;
  465. }
  466. static const struct file_operations watchdog_fops = {
  467. .owner = THIS_MODULE,
  468. .llseek = no_llseek,
  469. .open = watchdog_open,
  470. .release = watchdog_release,
  471. .write = watchdog_write,
  472. .unlocked_ioctl = watchdog_ioctl,
  473. };
  474. static struct miscdevice watchdog_miscdev = {
  475. .minor = WATCHDOG_MINOR,
  476. .name = "watchdog",
  477. .fops = &watchdog_fops,
  478. };
  479. static struct notifier_block watchdog_notifier = {
  480. .notifier_call = watchdog_notify_sys,
  481. };
  482. static int __init watchdog_init(int sioaddr)
  483. {
  484. int wdt_conf, err = 0;
  485. /* No need to lock watchdog.lock here because no entry points
  486. * into the module have been registered yet.
  487. */
  488. watchdog.sioaddr = sioaddr;
  489. watchdog.ident.options = WDIOC_SETTIMEOUT
  490. | WDIOF_MAGICCLOSE
  491. | WDIOF_KEEPALIVEPING;
  492. snprintf(watchdog.ident.identity,
  493. sizeof(watchdog.ident.identity), "%s watchdog",
  494. f71808e_names[watchdog.type]);
  495. err = superio_enter(sioaddr);
  496. if (err)
  497. return err;
  498. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  499. wdt_conf = superio_inb(sioaddr, F71808FG_REG_WDT_CONF);
  500. watchdog.caused_reboot = wdt_conf & F71808FG_FLAG_WDTMOUT_STS;
  501. superio_exit(sioaddr);
  502. err = watchdog_set_timeout(timeout);
  503. if (err)
  504. return err;
  505. err = watchdog_set_pulse_width(pulse_width);
  506. if (err)
  507. return err;
  508. err = register_reboot_notifier(&watchdog_notifier);
  509. if (err)
  510. return err;
  511. err = misc_register(&watchdog_miscdev);
  512. if (err) {
  513. printk(KERN_ERR DRVNAME
  514. ": cannot register miscdev on minor=%d\n",
  515. watchdog_miscdev.minor);
  516. goto exit_reboot;
  517. }
  518. if (start_withtimeout) {
  519. if (start_withtimeout <= 0
  520. || start_withtimeout > max_timeout) {
  521. printk(KERN_ERR DRVNAME
  522. ": starting timeout out of range\n");
  523. err = -EINVAL;
  524. goto exit_miscdev;
  525. }
  526. err = watchdog_start();
  527. if (err) {
  528. printk(KERN_ERR DRVNAME
  529. ": cannot start watchdog timer\n");
  530. goto exit_miscdev;
  531. }
  532. mutex_lock(&watchdog.lock);
  533. err = superio_enter(sioaddr);
  534. if (err)
  535. goto exit_unlock;
  536. superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
  537. if (start_withtimeout > 0xff) {
  538. /* select minutes for timer units */
  539. superio_set_bit(sioaddr, F71808FG_REG_WDT_CONF,
  540. F71808FG_FLAG_WD_UNIT);
  541. superio_outb(sioaddr, F71808FG_REG_WD_TIME,
  542. DIV_ROUND_UP(start_withtimeout, 60));
  543. } else {
  544. /* select seconds for timer units */
  545. superio_clear_bit(sioaddr, F71808FG_REG_WDT_CONF,
  546. F71808FG_FLAG_WD_UNIT);
  547. superio_outb(sioaddr, F71808FG_REG_WD_TIME,
  548. start_withtimeout);
  549. }
  550. superio_exit(sioaddr);
  551. mutex_unlock(&watchdog.lock);
  552. if (nowayout)
  553. __module_get(THIS_MODULE);
  554. printk(KERN_INFO DRVNAME
  555. ": watchdog started with initial timeout of %u sec\n",
  556. start_withtimeout);
  557. }
  558. return 0;
  559. exit_unlock:
  560. mutex_unlock(&watchdog.lock);
  561. exit_miscdev:
  562. misc_deregister(&watchdog_miscdev);
  563. exit_reboot:
  564. unregister_reboot_notifier(&watchdog_notifier);
  565. return err;
  566. }
  567. static int __init f71808e_find(int sioaddr)
  568. {
  569. u16 devid;
  570. int err = superio_enter(sioaddr);
  571. if (err)
  572. return err;
  573. devid = superio_inw(sioaddr, SIO_REG_MANID);
  574. if (devid != SIO_FINTEK_ID) {
  575. pr_debug(DRVNAME ": Not a Fintek device\n");
  576. err = -ENODEV;
  577. goto exit;
  578. }
  579. devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
  580. switch (devid) {
  581. case SIO_F71808_ID:
  582. watchdog.type = f71808fg;
  583. break;
  584. case SIO_F71882_ID:
  585. watchdog.type = f71882fg;
  586. break;
  587. case SIO_F71889_ID:
  588. watchdog.type = f71889fg;
  589. break;
  590. case SIO_F71862_ID:
  591. /* These have a watchdog, though it isn't implemented (yet). */
  592. err = -ENOSYS;
  593. goto exit;
  594. case SIO_F71858_ID:
  595. /* Confirmed (by datasheet) not to have a watchdog. */
  596. err = -ENODEV;
  597. goto exit;
  598. default:
  599. printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n",
  600. (unsigned int)devid);
  601. err = -ENODEV;
  602. goto exit;
  603. }
  604. printk(KERN_INFO DRVNAME ": Found %s watchdog chip, revision %d\n",
  605. f71808e_names[watchdog.type],
  606. (int)superio_inb(sioaddr, SIO_REG_DEVREV));
  607. exit:
  608. superio_exit(sioaddr);
  609. return err;
  610. }
  611. static int __init f71808e_init(void)
  612. {
  613. static const unsigned short addrs[] = { 0x2e, 0x4e };
  614. int err = -ENODEV;
  615. int i;
  616. for (i = 0; i < ARRAY_SIZE(addrs); i++) {
  617. err = f71808e_find(addrs[i]);
  618. if (err == 0)
  619. break;
  620. }
  621. if (i == ARRAY_SIZE(addrs))
  622. return err;
  623. return watchdog_init(addrs[i]);
  624. }
  625. static void __exit f71808e_exit(void)
  626. {
  627. if (watchdog_is_running()) {
  628. printk(KERN_WARNING DRVNAME
  629. ": Watchdog timer still running, stopping it\n");
  630. watchdog_stop();
  631. }
  632. misc_deregister(&watchdog_miscdev);
  633. unregister_reboot_notifier(&watchdog_notifier);
  634. }
  635. MODULE_DESCRIPTION("F71808E Watchdog Driver");
  636. MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
  637. MODULE_LICENSE("GPL");
  638. module_init(f71808e_init);
  639. module_exit(f71808e_exit);