smsc37b787_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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/watchdog.txt
  44. */
  45. #include <linux/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/types.h>
  48. #include <linux/miscdevice.h>
  49. #include <linux/watchdog.h>
  50. #include <linux/delay.h>
  51. #include <linux/fs.h>
  52. #include <linux/ioport.h>
  53. #include <linux/notifier.h>
  54. #include <linux/reboot.h>
  55. #include <linux/init.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/io.h>
  58. #include <linux/uaccess.h>
  59. #include <asm/system.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 MODNAME "smsc37b787_wdt: "
  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. printk(KERN_INFO MODNAME
  298. "Watchdog enabled. Timeout set to %d %s.\n",
  299. timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
  300. return nonseekable_open(inode, file);
  301. }
  302. /* close => shut off the timer */
  303. static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
  304. {
  305. /* Shut off the timer. */
  306. if (expect_close == 42) {
  307. wb_smsc_wdt_disable();
  308. printk(KERN_INFO MODNAME
  309. "Watchdog disabled, sleeping again...\n");
  310. } else {
  311. printk(KERN_CRIT MODNAME
  312. "Unexpected close, not stopping watchdog!\n");
  313. wb_smsc_wdt_reset_timer();
  314. }
  315. clear_bit(0, &timer_enabled);
  316. expect_close = 0;
  317. return 0;
  318. }
  319. /* write => update the timer to keep the machine alive */
  320. static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
  321. size_t len, loff_t *ppos)
  322. {
  323. /* See if we got the magic character 'V' and reload the timer */
  324. if (len) {
  325. if (!nowayout) {
  326. size_t i;
  327. /* reset expect flag */
  328. expect_close = 0;
  329. /* scan to see whether or not we got the
  330. magic character */
  331. for (i = 0; i != len; i++) {
  332. char c;
  333. if (get_user(c, data + i))
  334. return -EFAULT;
  335. if (c == 'V')
  336. expect_close = 42;
  337. }
  338. }
  339. /* someone wrote to us, we should reload the timer */
  340. wb_smsc_wdt_reset_timer();
  341. }
  342. return len;
  343. }
  344. /* ioctl => control interface */
  345. static long wb_smsc_wdt_ioctl(struct file *file,
  346. unsigned int cmd, unsigned long arg)
  347. {
  348. int new_timeout;
  349. union {
  350. struct watchdog_info __user *ident;
  351. int __user *i;
  352. } uarg;
  353. static const struct watchdog_info ident = {
  354. .options = WDIOF_KEEPALIVEPING |
  355. WDIOF_SETTIMEOUT |
  356. WDIOF_MAGICCLOSE,
  357. .firmware_version = 0,
  358. .identity = "SMsC 37B787 Watchdog",
  359. };
  360. uarg.i = (int __user *)arg;
  361. switch (cmd) {
  362. case WDIOC_GETSUPPORT:
  363. return copy_to_user(uarg.ident, &ident, sizeof(ident))
  364. ? -EFAULT : 0;
  365. case WDIOC_GETSTATUS:
  366. return put_user(wb_smsc_wdt_status(), uarg.i);
  367. case WDIOC_GETBOOTSTATUS:
  368. return put_user(0, uarg.i);
  369. case WDIOC_SETOPTIONS:
  370. {
  371. int options, retval = -EINVAL;
  372. if (get_user(options, uarg.i))
  373. return -EFAULT;
  374. if (options & WDIOS_DISABLECARD) {
  375. wb_smsc_wdt_disable();
  376. retval = 0;
  377. }
  378. if (options & WDIOS_ENABLECARD) {
  379. wb_smsc_wdt_enable();
  380. retval = 0;
  381. }
  382. return retval;
  383. }
  384. case WDIOC_KEEPALIVE:
  385. wb_smsc_wdt_reset_timer();
  386. return 0;
  387. case WDIOC_SETTIMEOUT:
  388. if (get_user(new_timeout, uarg.i))
  389. return -EFAULT;
  390. /* the API states this is given in secs */
  391. if (unit == UNIT_MINUTE)
  392. new_timeout /= 60;
  393. if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
  394. return -EINVAL;
  395. timeout = new_timeout;
  396. wb_smsc_wdt_set_timeout(timeout);
  397. /* fall through and return the new timeout... */
  398. case WDIOC_GETTIMEOUT:
  399. new_timeout = timeout;
  400. if (unit == UNIT_MINUTE)
  401. new_timeout *= 60;
  402. return put_user(new_timeout, uarg.i);
  403. default:
  404. return -ENOTTY;
  405. }
  406. }
  407. /* -- Notifier funtions -----------------------------------------*/
  408. static int wb_smsc_wdt_notify_sys(struct notifier_block *this,
  409. unsigned long code, void *unused)
  410. {
  411. if (code == SYS_DOWN || code == SYS_HALT) {
  412. /* set timeout to 0, to avoid possible race-condition */
  413. timeout = 0;
  414. wb_smsc_wdt_disable();
  415. }
  416. return NOTIFY_DONE;
  417. }
  418. /* -- Module's structures ---------------------------------------*/
  419. static const struct file_operations wb_smsc_wdt_fops = {
  420. .owner = THIS_MODULE,
  421. .llseek = no_llseek,
  422. .write = wb_smsc_wdt_write,
  423. .unlocked_ioctl = wb_smsc_wdt_ioctl,
  424. .open = wb_smsc_wdt_open,
  425. .release = wb_smsc_wdt_release,
  426. };
  427. static struct notifier_block wb_smsc_wdt_notifier = {
  428. .notifier_call = wb_smsc_wdt_notify_sys,
  429. };
  430. static struct miscdevice wb_smsc_wdt_miscdev = {
  431. .minor = WATCHDOG_MINOR,
  432. .name = "watchdog",
  433. .fops = &wb_smsc_wdt_fops,
  434. };
  435. /* -- Module init functions -------------------------------------*/
  436. /* module's "constructor" */
  437. static int __init wb_smsc_wdt_init(void)
  438. {
  439. int ret;
  440. printk(KERN_INFO "SMsC 37B787 watchdog component driver "
  441. VERSION " initialising...\n");
  442. if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
  443. printk(KERN_ERR MODNAME "Unable to register IO port %#x\n",
  444. IOPORT);
  445. ret = -EBUSY;
  446. goto out_pnp;
  447. }
  448. /* set new maximum, if it's too big */
  449. if (timeout > MAX_TIMEOUT)
  450. timeout = MAX_TIMEOUT;
  451. /* init the watchdog timer */
  452. wb_smsc_wdt_initialize();
  453. ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
  454. if (ret) {
  455. printk(KERN_ERR MODNAME
  456. "Unable to register reboot notifier err = %d\n", ret);
  457. goto out_io;
  458. }
  459. ret = misc_register(&wb_smsc_wdt_miscdev);
  460. if (ret) {
  461. printk(KERN_ERR MODNAME
  462. "Unable to register miscdev on minor %d\n",
  463. WATCHDOG_MINOR);
  464. goto out_rbt;
  465. }
  466. /* output info */
  467. printk(KERN_INFO MODNAME "Timeout set to %d %s.\n",
  468. timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
  469. printk(KERN_INFO MODNAME
  470. "Watchdog initialized and sleeping (nowayout=%d)...\n",
  471. nowayout);
  472. out_clean:
  473. return ret;
  474. out_rbt:
  475. unregister_reboot_notifier(&wb_smsc_wdt_notifier);
  476. out_io:
  477. release_region(IOPORT, IOPORT_SIZE);
  478. out_pnp:
  479. goto out_clean;
  480. }
  481. /* module's "destructor" */
  482. static void __exit wb_smsc_wdt_exit(void)
  483. {
  484. /* Stop the timer before we leave */
  485. if (!nowayout) {
  486. wb_smsc_wdt_shutdown();
  487. printk(KERN_INFO MODNAME "Watchdog disabled.\n");
  488. }
  489. misc_deregister(&wb_smsc_wdt_miscdev);
  490. unregister_reboot_notifier(&wb_smsc_wdt_notifier);
  491. release_region(IOPORT, IOPORT_SIZE);
  492. printk(KERN_INFO "SMsC 37B787 watchdog component driver removed.\n");
  493. }
  494. module_init(wb_smsc_wdt_init);
  495. module_exit(wb_smsc_wdt_exit);
  496. MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
  497. MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
  498. VERSION ")");
  499. MODULE_LICENSE("GPL");
  500. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  501. #ifdef SMSC_SUPPORT_MINUTES
  502. module_param(unit, int, 0);
  503. MODULE_PARM_DESC(unit,
  504. "set unit to use, 0=seconds or 1=minutes, default is 0");
  505. #endif
  506. module_param(timeout, int, 0);
  507. MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
  508. module_param(nowayout, int, 0);
  509. MODULE_PARM_DESC(nowayout,
  510. "Watchdog cannot be stopped once started (default="
  511. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");