smsc37b787_wdt.c 15 KB

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