f71808e_wdt.c 22 KB

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