f71808e_wdt.c 22 KB

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