smsc37b787_wdt.c 16 KB

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