machzwd.c 9.7 KB

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