smsc37b787_wdt.c 15 KB

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