smsc37b787_wdt.c 15 KB

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