machzwd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * MachZ ZF-Logic Watchdog Timer driver for Linux
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * The author does NOT admit liability nor provide warranty for
  11. * any of this software. This material is provided "AS-IS" in
  12. * the hope that it may be useful for others.
  13. *
  14. * Author: Fernando Fuganti <fuganti@conectiva.com.br>
  15. *
  16. * Based on sbc60xxwdt.c by Jakob Oestergaard
  17. *
  18. *
  19. * We have two timers (wd#1, wd#2) driven by a 32 KHz clock with the
  20. * following periods:
  21. * wd#1 - 2 seconds;
  22. * wd#2 - 7.2 ms;
  23. * After the expiration of wd#1, it can generate a NMI, SCI, SMI, or
  24. * a system RESET and it starts wd#2 that unconditionally will RESET
  25. * the system when the counter reaches zero.
  26. *
  27. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  28. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/types.h>
  34. #include <linux/timer.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/watchdog.h>
  38. #include <linux/fs.h>
  39. #include <linux/ioport.h>
  40. #include <linux/notifier.h>
  41. #include <linux/reboot.h>
  42. #include <linux/init.h>
  43. #include <linux/io.h>
  44. #include <linux/uaccess.h>
  45. /* ports */
  46. #define ZF_IOBASE 0x218
  47. #define INDEX 0x218
  48. #define DATA_B 0x219
  49. #define DATA_W 0x21A
  50. #define DATA_D 0x21A
  51. /* indexes */ /* size */
  52. #define ZFL_VERSION 0x02 /* 16 */
  53. #define CONTROL 0x10 /* 16 */
  54. #define STATUS 0x12 /* 8 */
  55. #define COUNTER_1 0x0C /* 16 */
  56. #define COUNTER_2 0x0E /* 8 */
  57. #define PULSE_LEN 0x0F /* 8 */
  58. /* controls */
  59. #define ENABLE_WD1 0x0001
  60. #define ENABLE_WD2 0x0002
  61. #define RESET_WD1 0x0010
  62. #define RESET_WD2 0x0020
  63. #define GEN_SCI 0x0100
  64. #define GEN_NMI 0x0200
  65. #define GEN_SMI 0x0400
  66. #define GEN_RESET 0x0800
  67. /* utilities */
  68. #define WD1 0
  69. #define WD2 1
  70. #define zf_writew(port, data) { outb(port, INDEX); outw(data, DATA_W); }
  71. #define zf_writeb(port, data) { outb(port, INDEX); outb(data, DATA_B); }
  72. #define zf_get_ZFL_version() zf_readw(ZFL_VERSION)
  73. static unsigned short zf_readw(unsigned char port)
  74. {
  75. outb(port, INDEX);
  76. return inw(DATA_W);
  77. }
  78. MODULE_AUTHOR("Fernando Fuganti <fuganti@conectiva.com.br>");
  79. MODULE_DESCRIPTION("MachZ ZF-Logic Watchdog driver");
  80. MODULE_LICENSE("GPL");
  81. static bool nowayout = WATCHDOG_NOWAYOUT;
  82. module_param(nowayout, bool, 0);
  83. MODULE_PARM_DESC(nowayout,
  84. "Watchdog cannot be stopped once started (default="
  85. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  86. #define PFX "machzwd"
  87. static const struct watchdog_info zf_info = {
  88. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  89. .firmware_version = 1,
  90. .identity = "ZF-Logic watchdog",
  91. };
  92. /*
  93. * action refers to action taken when watchdog resets
  94. * 0 = GEN_RESET
  95. * 1 = GEN_SMI
  96. * 2 = GEN_NMI
  97. * 3 = GEN_SCI
  98. * defaults to GEN_RESET (0)
  99. */
  100. static int action;
  101. module_param(action, int, 0);
  102. MODULE_PARM_DESC(action, "after watchdog resets, generate: "
  103. "0 = RESET(*) 1 = SMI 2 = NMI 3 = SCI");
  104. static void zf_ping(unsigned long data);
  105. static int zf_action = GEN_RESET;
  106. static unsigned long zf_is_open;
  107. static char zf_expect_close;
  108. static DEFINE_SPINLOCK(zf_port_lock);
  109. static DEFINE_TIMER(zf_timer, zf_ping, 0, 0);
  110. static unsigned long next_heartbeat;
  111. /* timeout for user land heart beat (10 seconds) */
  112. #define ZF_USER_TIMEO (HZ*10)
  113. /* timeout for hardware watchdog (~500ms) */
  114. #define ZF_HW_TIMEO (HZ/2)
  115. /* number of ticks on WD#1 (driven by a 32KHz clock, 2s) */
  116. #define ZF_CTIMEOUT 0xffff
  117. #ifndef ZF_DEBUG
  118. #define dprintk(format, args...)
  119. #else
  120. #define dprintk(format, args...) \
  121. pr_debug(":%s:%d: " format, __func__, __LINE__ , ## args)
  122. #endif
  123. static inline void zf_set_status(unsigned char new)
  124. {
  125. zf_writeb(STATUS, new);
  126. }
  127. /* CONTROL register functions */
  128. static inline unsigned short zf_get_control(void)
  129. {
  130. return zf_readw(CONTROL);
  131. }
  132. static inline void zf_set_control(unsigned short new)
  133. {
  134. zf_writew(CONTROL, new);
  135. }
  136. /* WD#? counter functions */
  137. /*
  138. * Just set counter value
  139. */
  140. static inline void zf_set_timer(unsigned short new, unsigned char n)
  141. {
  142. switch (n) {
  143. case WD1:
  144. zf_writew(COUNTER_1, new);
  145. case WD2:
  146. zf_writeb(COUNTER_2, new > 0xff ? 0xff : new);
  147. default:
  148. return;
  149. }
  150. }
  151. /*
  152. * stop hardware timer
  153. */
  154. static void zf_timer_off(void)
  155. {
  156. unsigned int ctrl_reg = 0;
  157. unsigned long flags;
  158. /* stop internal ping */
  159. del_timer_sync(&zf_timer);
  160. spin_lock_irqsave(&zf_port_lock, flags);
  161. /* stop watchdog timer */
  162. ctrl_reg = zf_get_control();
  163. ctrl_reg |= (ENABLE_WD1|ENABLE_WD2); /* disable wd1 and wd2 */
  164. ctrl_reg &= ~(ENABLE_WD1|ENABLE_WD2);
  165. zf_set_control(ctrl_reg);
  166. spin_unlock_irqrestore(&zf_port_lock, flags);
  167. pr_info("Watchdog timer is now disabled\n");
  168. }
  169. /*
  170. * start hardware timer
  171. */
  172. static void zf_timer_on(void)
  173. {
  174. unsigned int ctrl_reg = 0;
  175. unsigned long flags;
  176. spin_lock_irqsave(&zf_port_lock, flags);
  177. zf_writeb(PULSE_LEN, 0xff);
  178. zf_set_timer(ZF_CTIMEOUT, WD1);
  179. /* user land ping */
  180. next_heartbeat = jiffies + ZF_USER_TIMEO;
  181. /* start the timer for internal ping */
  182. mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO);
  183. /* start watchdog timer */
  184. ctrl_reg = zf_get_control();
  185. ctrl_reg |= (ENABLE_WD1|zf_action);
  186. zf_set_control(ctrl_reg);
  187. spin_unlock_irqrestore(&zf_port_lock, flags);
  188. pr_info("Watchdog timer is now enabled\n");
  189. }
  190. static void zf_ping(unsigned long data)
  191. {
  192. unsigned int ctrl_reg = 0;
  193. unsigned long flags;
  194. zf_writeb(COUNTER_2, 0xff);
  195. if (time_before(jiffies, next_heartbeat)) {
  196. dprintk("time_before: %ld\n", next_heartbeat - jiffies);
  197. /*
  198. * reset event is activated by transition from 0 to 1 on
  199. * RESET_WD1 bit and we assume that it is already zero...
  200. */
  201. spin_lock_irqsave(&zf_port_lock, flags);
  202. ctrl_reg = zf_get_control();
  203. ctrl_reg |= RESET_WD1;
  204. zf_set_control(ctrl_reg);
  205. /* ...and nothing changes until here */
  206. ctrl_reg &= ~(RESET_WD1);
  207. zf_set_control(ctrl_reg);
  208. spin_unlock_irqrestore(&zf_port_lock, flags);
  209. mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO);
  210. } else
  211. pr_crit("I will reset your machine\n");
  212. }
  213. static ssize_t zf_write(struct file *file, const char __user *buf, size_t count,
  214. loff_t *ppos)
  215. {
  216. /* See if we got the magic character */
  217. if (count) {
  218. /*
  219. * no need to check for close confirmation
  220. * no way to disable watchdog ;)
  221. */
  222. if (!nowayout) {
  223. size_t ofs;
  224. /*
  225. * note: just in case someone wrote the magic character
  226. * five months ago...
  227. */
  228. zf_expect_close = 0;
  229. /* now scan */
  230. for (ofs = 0; ofs != count; ofs++) {
  231. char c;
  232. if (get_user(c, buf + ofs))
  233. return -EFAULT;
  234. if (c == 'V') {
  235. zf_expect_close = 42;
  236. dprintk("zf_expect_close = 42\n");
  237. }
  238. }
  239. }
  240. /*
  241. * Well, anyhow someone wrote to us,
  242. * we should return that favour
  243. */
  244. next_heartbeat = jiffies + ZF_USER_TIMEO;
  245. dprintk("user ping at %ld\n", jiffies);
  246. }
  247. return count;
  248. }
  249. static long zf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  250. {
  251. void __user *argp = (void __user *)arg;
  252. int __user *p = argp;
  253. switch (cmd) {
  254. case WDIOC_GETSUPPORT:
  255. if (copy_to_user(argp, &zf_info, sizeof(zf_info)))
  256. return -EFAULT;
  257. break;
  258. case WDIOC_GETSTATUS:
  259. case WDIOC_GETBOOTSTATUS:
  260. return put_user(0, p);
  261. case WDIOC_KEEPALIVE:
  262. zf_ping(0);
  263. break;
  264. default:
  265. return -ENOTTY;
  266. }
  267. return 0;
  268. }
  269. static int zf_open(struct inode *inode, struct file *file)
  270. {
  271. if (test_and_set_bit(0, &zf_is_open))
  272. return -EBUSY;
  273. if (nowayout)
  274. __module_get(THIS_MODULE);
  275. zf_timer_on();
  276. return nonseekable_open(inode, file);
  277. }
  278. static int zf_close(struct inode *inode, struct file *file)
  279. {
  280. if (zf_expect_close == 42)
  281. zf_timer_off();
  282. else {
  283. del_timer(&zf_timer);
  284. pr_err("device file closed unexpectedly. Will not stop the WDT!\n");
  285. }
  286. clear_bit(0, &zf_is_open);
  287. zf_expect_close = 0;
  288. return 0;
  289. }
  290. /*
  291. * Notifier for system down
  292. */
  293. static int zf_notify_sys(struct notifier_block *this, unsigned long code,
  294. void *unused)
  295. {
  296. if (code == SYS_DOWN || code == SYS_HALT)
  297. zf_timer_off();
  298. return NOTIFY_DONE;
  299. }
  300. static const struct file_operations zf_fops = {
  301. .owner = THIS_MODULE,
  302. .llseek = no_llseek,
  303. .write = zf_write,
  304. .unlocked_ioctl = zf_ioctl,
  305. .open = zf_open,
  306. .release = zf_close,
  307. };
  308. static struct miscdevice zf_miscdev = {
  309. .minor = WATCHDOG_MINOR,
  310. .name = "watchdog",
  311. .fops = &zf_fops,
  312. };
  313. /*
  314. * The device needs to learn about soft shutdowns in order to
  315. * turn the timebomb registers off.
  316. */
  317. static struct notifier_block zf_notifier = {
  318. .notifier_call = zf_notify_sys,
  319. };
  320. static void __init zf_show_action(int act)
  321. {
  322. static const char * const str[] = { "RESET", "SMI", "NMI", "SCI" };
  323. pr_info("Watchdog using action = %s\n", str[act]);
  324. }
  325. static int __init zf_init(void)
  326. {
  327. int ret;
  328. pr_info("MachZ ZF-Logic Watchdog driver initializing\n");
  329. ret = zf_get_ZFL_version();
  330. if (!ret || ret == 0xffff) {
  331. pr_warn("no ZF-Logic found\n");
  332. return -ENODEV;
  333. }
  334. if (action <= 3 && action >= 0)
  335. zf_action = zf_action >> action;
  336. else
  337. action = 0;
  338. zf_show_action(action);
  339. if (!request_region(ZF_IOBASE, 3, "MachZ ZFL WDT")) {
  340. pr_err("cannot reserve I/O ports at %d\n", ZF_IOBASE);
  341. ret = -EBUSY;
  342. goto no_region;
  343. }
  344. ret = register_reboot_notifier(&zf_notifier);
  345. if (ret) {
  346. pr_err("can't register reboot notifier (err=%d)\n", ret);
  347. goto no_reboot;
  348. }
  349. ret = misc_register(&zf_miscdev);
  350. if (ret) {
  351. pr_err("can't misc_register on minor=%d\n", WATCHDOG_MINOR);
  352. goto no_misc;
  353. }
  354. zf_set_status(0);
  355. zf_set_control(0);
  356. return 0;
  357. no_misc:
  358. unregister_reboot_notifier(&zf_notifier);
  359. no_reboot:
  360. release_region(ZF_IOBASE, 3);
  361. no_region:
  362. return ret;
  363. }
  364. static void __exit zf_exit(void)
  365. {
  366. zf_timer_off();
  367. misc_deregister(&zf_miscdev);
  368. unregister_reboot_notifier(&zf_notifier);
  369. release_region(ZF_IOBASE, 3);
  370. }
  371. module_init(zf_init);
  372. module_exit(zf_exit);