smsc37b787_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x
  4. *
  5. * Based on acquirewdt.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
  6. * and some other existing drivers
  7. *
  8. * The authors do NOT admit liability nor provide warranty for
  9. * any of this software. This material is provided "AS-IS" in
  10. * the hope that it may be useful for others.
  11. *
  12. * (C) Copyright 2003-2006 Sven Anders <anders@anduras.de>
  13. *
  14. * History:
  15. * 2003 - Created version 1.0 for Linux 2.4.x.
  16. * 2006 - Ported to Linux 2.6, added nowayout and MAGICCLOSE
  17. * features. Released version 1.1
  18. *
  19. * Theory of operation:
  20. *
  21. * A Watchdog Timer (WDT) is a hardware circuit that can
  22. * reset the computer system in case of a software fault.
  23. * You probably knew that already.
  24. *
  25. * Usually a userspace daemon will notify the kernel WDT driver
  26. * via the /dev/watchdog special device file that userspace is
  27. * still alive, at regular intervals. When such a notification
  28. * occurs, the driver will usually tell the hardware watchdog
  29. * that everything is in order, and that the watchdog should wait
  30. * for yet another little while to reset the system.
  31. * If userspace fails (RAM error, kernel bug, whatever), the
  32. * notifications cease to occur, and the hardware watchdog will
  33. * reset the system (causing a reboot) after the timeout occurs.
  34. *
  35. * Create device with:
  36. * mknod /dev/watchdog c 10 130
  37. *
  38. * For an example userspace keep-alive daemon, see:
  39. * Documentation/watchdog/wdt.rst
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/types.h>
  45. #include <linux/miscdevice.h>
  46. #include <linux/watchdog.h>
  47. #include <linux/delay.h>
  48. #include <linux/fs.h>
  49. #include <linux/ioport.h>
  50. #include <linux/notifier.h>
  51. #include <linux/reboot.h>
  52. #include <linux/init.h>
  53. #include <linux/spinlock.h>
  54. #include <linux/io.h>
  55. #include <linux/uaccess.h>
  56. /* enable support for minutes as units? */
  57. /* (does not always work correctly, so disabled by default!) */
  58. #define SMSC_SUPPORT_MINUTES
  59. #undef SMSC_SUPPORT_MINUTES
  60. #define MAX_TIMEOUT 255
  61. #define UNIT_SECOND 0
  62. #define UNIT_MINUTE 1
  63. #define VERSION "1.1"
  64. #define IOPORT 0x3F0
  65. #define IOPORT_SIZE 2
  66. #define IODEV_NO 8
  67. static int unit = UNIT_SECOND; /* timer's unit */
  68. static int timeout = 60; /* timeout value: default is 60 "units" */
  69. static unsigned long timer_enabled; /* is the timer enabled? */
  70. static char expect_close; /* is the close expected? */
  71. static DEFINE_SPINLOCK(io_lock);/* to guard the watchdog from io races */
  72. static bool nowayout = WATCHDOG_NOWAYOUT;
  73. /* -- Low level function ----------------------------------------*/
  74. /* unlock the IO chip */
  75. static inline void open_io_config(void)
  76. {
  77. outb(0x55, IOPORT);
  78. mdelay(1);
  79. outb(0x55, IOPORT);
  80. }
  81. /* lock the IO chip */
  82. static inline void close_io_config(void)
  83. {
  84. outb(0xAA, IOPORT);
  85. }
  86. /* select the IO device */
  87. static inline void select_io_device(unsigned char devno)
  88. {
  89. outb(0x07, IOPORT);
  90. outb(devno, IOPORT+1);
  91. }
  92. /* write to the control register */
  93. static inline void write_io_cr(unsigned char reg, unsigned char data)
  94. {
  95. outb(reg, IOPORT);
  96. outb(data, IOPORT+1);
  97. }
  98. /* read from the control register */
  99. static inline char read_io_cr(unsigned char reg)
  100. {
  101. outb(reg, IOPORT);
  102. return inb(IOPORT+1);
  103. }
  104. /* -- Medium level functions ------------------------------------*/
  105. static inline void gpio_bit12(unsigned char reg)
  106. {
  107. /* -- General Purpose I/O Bit 1.2 --
  108. * Bit 0, In/Out: 0 = Output, 1 = Input
  109. * Bit 1, Polarity: 0 = No Invert, 1 = Invert
  110. * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
  111. * Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17,
  112. * 11 = Either Edge Triggered Intr. 2
  113. * Bit 5/6 (Reserved)
  114. * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
  115. */
  116. write_io_cr(0xE2, reg);
  117. }
  118. static inline void gpio_bit13(unsigned char reg)
  119. {
  120. /* -- General Purpose I/O Bit 1.3 --
  121. * Bit 0, In/Out: 0 = Output, 1 = Input
  122. * Bit 1, Polarity: 0 = No Invert, 1 = Invert
  123. * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
  124. * Bit 3, Function select: 0 = GPI/O, 1 = LED
  125. * Bit 4-6 (Reserved)
  126. * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
  127. */
  128. write_io_cr(0xE3, reg);
  129. }
  130. static inline void wdt_timer_units(unsigned char new_units)
  131. {
  132. /* -- Watchdog timer units --
  133. * Bit 0-6 (Reserved)
  134. * Bit 7, WDT Time-out Value Units Select
  135. * (0 = Minutes, 1 = Seconds)
  136. */
  137. write_io_cr(0xF1, new_units);
  138. }
  139. static inline void wdt_timeout_value(unsigned char new_timeout)
  140. {
  141. /* -- Watchdog Timer Time-out Value --
  142. * Bit 0-7 Binary coded units (0=Disabled, 1..255)
  143. */
  144. write_io_cr(0xF2, new_timeout);
  145. }
  146. static inline void wdt_timer_conf(unsigned char conf)
  147. {
  148. /* -- Watchdog timer configuration --
  149. * Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon
  150. * Gameport I/O
  151. * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
  152. * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
  153. * Bit 3 Reset the timer
  154. * (Wrong in SMsC documentation? Given as: PowerLED Timout
  155. * Enabled)
  156. * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
  157. * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
  158. */
  159. write_io_cr(0xF3, conf);
  160. }
  161. static inline void wdt_timer_ctrl(unsigned char reg)
  162. {
  163. /* -- Watchdog timer control --
  164. * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
  165. * Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz
  166. * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
  167. * Bit 3 P20 Force Timeout enabled:
  168. * 0 = P20 activity does not generate the WD timeout event
  169. * 1 = P20 Allows rising edge of P20, from the keyboard
  170. * controller, to force the WD timeout event.
  171. * Bit 4 (Reserved)
  172. * -- Soft power management --
  173. * Bit 5 Stop Counter: 1 = Stop software power down counter
  174. * set via register 0xB8, (self-cleaning)
  175. * (Upon read: 0 = Counter running, 1 = Counter stopped)
  176. * Bit 6 Restart Counter: 1 = Restart software power down counter
  177. * set via register 0xB8, (self-cleaning)
  178. * Bit 7 SPOFF: 1 = Force software power down (self-cleaning)
  179. */
  180. write_io_cr(0xF4, reg);
  181. }
  182. /* -- Higher level functions ------------------------------------*/
  183. /* initialize watchdog */
  184. static void wb_smsc_wdt_initialize(void)
  185. {
  186. unsigned char old;
  187. spin_lock(&io_lock);
  188. open_io_config();
  189. select_io_device(IODEV_NO);
  190. /* enable the watchdog */
  191. gpio_bit13(0x08); /* Select pin 80 = LED not GPIO */
  192. gpio_bit12(0x0A); /* Set pin 79 = WDT not
  193. GPIO/Output/Polarity=Invert */
  194. /* disable the timeout */
  195. wdt_timeout_value(0);
  196. /* reset control register */
  197. wdt_timer_ctrl(0x00);
  198. /* reset configuration register */
  199. wdt_timer_conf(0x00);
  200. /* read old (timer units) register */
  201. old = read_io_cr(0xF1) & 0x7F;
  202. if (unit == UNIT_SECOND)
  203. old |= 0x80; /* set to seconds */
  204. /* set the watchdog timer units */
  205. wdt_timer_units(old);
  206. close_io_config();
  207. spin_unlock(&io_lock);
  208. }
  209. /* shutdown the watchdog */
  210. static void wb_smsc_wdt_shutdown(void)
  211. {
  212. spin_lock(&io_lock);
  213. open_io_config();
  214. select_io_device(IODEV_NO);
  215. /* disable the watchdog */
  216. gpio_bit13(0x09);
  217. gpio_bit12(0x09);
  218. /* reset watchdog config register */
  219. wdt_timer_conf(0x00);
  220. /* reset watchdog control register */
  221. wdt_timer_ctrl(0x00);
  222. /* disable timeout */
  223. wdt_timeout_value(0x00);
  224. close_io_config();
  225. spin_unlock(&io_lock);
  226. }
  227. /* set timeout => enable watchdog */
  228. static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
  229. {
  230. spin_lock(&io_lock);
  231. open_io_config();
  232. select_io_device(IODEV_NO);
  233. /* set Power LED to blink, if we enable the timeout */
  234. wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
  235. /* set timeout value */
  236. wdt_timeout_value(new_timeout);
  237. close_io_config();
  238. spin_unlock(&io_lock);
  239. }
  240. /* get timeout */
  241. static unsigned char wb_smsc_wdt_get_timeout(void)
  242. {
  243. unsigned char set_timeout;
  244. spin_lock(&io_lock);
  245. open_io_config();
  246. select_io_device(IODEV_NO);
  247. set_timeout = read_io_cr(0xF2);
  248. close_io_config();
  249. spin_unlock(&io_lock);
  250. return set_timeout;
  251. }
  252. /* disable watchdog */
  253. static void wb_smsc_wdt_disable(void)
  254. {
  255. /* set the timeout to 0 to disable the watchdog */
  256. wb_smsc_wdt_set_timeout(0);
  257. }
  258. /* enable watchdog by setting the current timeout */
  259. static void wb_smsc_wdt_enable(void)
  260. {
  261. /* set the current timeout... */
  262. wb_smsc_wdt_set_timeout(timeout);
  263. }
  264. /* reset the timer */
  265. static void wb_smsc_wdt_reset_timer(void)
  266. {
  267. spin_lock(&io_lock);
  268. open_io_config();
  269. select_io_device(IODEV_NO);
  270. /* reset the timer */
  271. wdt_timeout_value(timeout);
  272. wdt_timer_conf(0x08);
  273. close_io_config();
  274. spin_unlock(&io_lock);
  275. }
  276. /* return, if the watchdog is enabled (timeout is set...) */
  277. static int wb_smsc_wdt_status(void)
  278. {
  279. return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
  280. }
  281. /* -- File operations -------------------------------------------*/
  282. /* open => enable watchdog and set initial timeout */
  283. static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
  284. {
  285. /* /dev/watchdog can only be opened once */
  286. if (test_and_set_bit(0, &timer_enabled))
  287. return -EBUSY;
  288. if (nowayout)
  289. __module_get(THIS_MODULE);
  290. /* Reload and activate timer */
  291. wb_smsc_wdt_enable();
  292. pr_info("Watchdog enabled. Timeout set to %d %s\n",
  293. timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
  294. return stream_open(inode, file);
  295. }
  296. /* close => shut off the timer */
  297. static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
  298. {
  299. /* Shut off the timer. */
  300. if (expect_close == 42) {
  301. wb_smsc_wdt_disable();
  302. pr_info("Watchdog disabled, sleeping again...\n");
  303. } else {
  304. pr_crit("Unexpected close, not stopping watchdog!\n");
  305. wb_smsc_wdt_reset_timer();
  306. }
  307. clear_bit(0, &timer_enabled);
  308. expect_close = 0;
  309. return 0;
  310. }
  311. /* write => update the timer to keep the machine alive */
  312. static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
  313. size_t len, loff_t *ppos)
  314. {
  315. /* See if we got the magic character 'V' and reload the timer */
  316. if (len) {
  317. if (!nowayout) {
  318. size_t i;
  319. /* reset expect flag */
  320. expect_close = 0;
  321. /* scan to see whether or not we got the
  322. magic character */
  323. for (i = 0; i != len; i++) {
  324. char c;
  325. if (get_user(c, data + i))
  326. return -EFAULT;
  327. if (c == 'V')
  328. expect_close = 42;
  329. }
  330. }
  331. /* someone wrote to us, we should reload the timer */
  332. wb_smsc_wdt_reset_timer();
  333. }
  334. return len;
  335. }
  336. /* ioctl => control interface */
  337. static long wb_smsc_wdt_ioctl(struct file *file,
  338. unsigned int cmd, unsigned long arg)
  339. {
  340. int new_timeout;
  341. union {
  342. struct watchdog_info __user *ident;
  343. int __user *i;
  344. } uarg;
  345. static const struct watchdog_info ident = {
  346. .options = WDIOF_KEEPALIVEPING |
  347. WDIOF_SETTIMEOUT |
  348. WDIOF_MAGICCLOSE,
  349. .firmware_version = 0,
  350. .identity = "SMsC 37B787 Watchdog",
  351. };
  352. uarg.i = (int __user *)arg;
  353. switch (cmd) {
  354. case WDIOC_GETSUPPORT:
  355. return copy_to_user(uarg.ident, &ident, sizeof(ident))
  356. ? -EFAULT : 0;
  357. case WDIOC_GETSTATUS:
  358. return put_user(wb_smsc_wdt_status(), uarg.i);
  359. case WDIOC_GETBOOTSTATUS:
  360. return put_user(0, uarg.i);
  361. case WDIOC_SETOPTIONS:
  362. {
  363. int options, retval = -EINVAL;
  364. if (get_user(options, uarg.i))
  365. return -EFAULT;
  366. if (options & WDIOS_DISABLECARD) {
  367. wb_smsc_wdt_disable();
  368. retval = 0;
  369. }
  370. if (options & WDIOS_ENABLECARD) {
  371. wb_smsc_wdt_enable();
  372. retval = 0;
  373. }
  374. return retval;
  375. }
  376. case WDIOC_KEEPALIVE:
  377. wb_smsc_wdt_reset_timer();
  378. return 0;
  379. case WDIOC_SETTIMEOUT:
  380. if (get_user(new_timeout, uarg.i))
  381. return -EFAULT;
  382. /* the API states this is given in secs */
  383. if (unit == UNIT_MINUTE)
  384. new_timeout /= 60;
  385. if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
  386. return -EINVAL;
  387. timeout = new_timeout;
  388. wb_smsc_wdt_set_timeout(timeout);
  389. /* fall through - and return the new timeout... */
  390. case WDIOC_GETTIMEOUT:
  391. new_timeout = timeout;
  392. if (unit == UNIT_MINUTE)
  393. new_timeout *= 60;
  394. return put_user(new_timeout, uarg.i);
  395. default:
  396. return -ENOTTY;
  397. }
  398. }
  399. /* -- Notifier funtions -----------------------------------------*/
  400. static int wb_smsc_wdt_notify_sys(struct notifier_block *this,
  401. unsigned long code, void *unused)
  402. {
  403. if (code == SYS_DOWN || code == SYS_HALT) {
  404. /* set timeout to 0, to avoid possible race-condition */
  405. timeout = 0;
  406. wb_smsc_wdt_disable();
  407. }
  408. return NOTIFY_DONE;
  409. }
  410. /* -- Module's structures ---------------------------------------*/
  411. static const struct file_operations wb_smsc_wdt_fops = {
  412. .owner = THIS_MODULE,
  413. .llseek = no_llseek,
  414. .write = wb_smsc_wdt_write,
  415. .unlocked_ioctl = wb_smsc_wdt_ioctl,
  416. .open = wb_smsc_wdt_open,
  417. .release = wb_smsc_wdt_release,
  418. };
  419. static struct notifier_block wb_smsc_wdt_notifier = {
  420. .notifier_call = wb_smsc_wdt_notify_sys,
  421. };
  422. static struct miscdevice wb_smsc_wdt_miscdev = {
  423. .minor = WATCHDOG_MINOR,
  424. .name = "watchdog",
  425. .fops = &wb_smsc_wdt_fops,
  426. };
  427. /* -- Module init functions -------------------------------------*/
  428. /* module's "constructor" */
  429. static int __init wb_smsc_wdt_init(void)
  430. {
  431. int ret;
  432. pr_info("SMsC 37B787 watchdog component driver "
  433. VERSION " initialising...\n");
  434. if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
  435. pr_err("Unable to register IO port %#x\n", IOPORT);
  436. ret = -EBUSY;
  437. goto out_pnp;
  438. }
  439. /* set new maximum, if it's too big */
  440. if (timeout > MAX_TIMEOUT)
  441. timeout = MAX_TIMEOUT;
  442. /* init the watchdog timer */
  443. wb_smsc_wdt_initialize();
  444. ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
  445. if (ret) {
  446. pr_err("Unable to register reboot notifier err = %d\n", ret);
  447. goto out_io;
  448. }
  449. ret = misc_register(&wb_smsc_wdt_miscdev);
  450. if (ret) {
  451. pr_err("Unable to register miscdev on minor %d\n",
  452. WATCHDOG_MINOR);
  453. goto out_rbt;
  454. }
  455. /* output info */
  456. pr_info("Timeout set to %d %s\n",
  457. timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
  458. pr_info("Watchdog initialized and sleeping (nowayout=%d)...\n",
  459. nowayout);
  460. out_clean:
  461. return ret;
  462. out_rbt:
  463. unregister_reboot_notifier(&wb_smsc_wdt_notifier);
  464. out_io:
  465. release_region(IOPORT, IOPORT_SIZE);
  466. out_pnp:
  467. goto out_clean;
  468. }
  469. /* module's "destructor" */
  470. static void __exit wb_smsc_wdt_exit(void)
  471. {
  472. /* Stop the timer before we leave */
  473. if (!nowayout) {
  474. wb_smsc_wdt_shutdown();
  475. pr_info("Watchdog disabled\n");
  476. }
  477. misc_deregister(&wb_smsc_wdt_miscdev);
  478. unregister_reboot_notifier(&wb_smsc_wdt_notifier);
  479. release_region(IOPORT, IOPORT_SIZE);
  480. pr_info("SMsC 37B787 watchdog component driver removed\n");
  481. }
  482. module_init(wb_smsc_wdt_init);
  483. module_exit(wb_smsc_wdt_exit);
  484. MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
  485. MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
  486. VERSION ")");
  487. MODULE_LICENSE("GPL");
  488. #ifdef SMSC_SUPPORT_MINUTES
  489. module_param(unit, int, 0);
  490. MODULE_PARM_DESC(unit,
  491. "set unit to use, 0=seconds or 1=minutes, default is 0");
  492. #endif
  493. module_param(timeout, int, 0);
  494. MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
  495. module_param(nowayout, bool, 0);
  496. MODULE_PARM_DESC(nowayout,
  497. "Watchdog cannot be stopped once started (default="
  498. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");