f71808e_wdt.c 22 KB

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