machzwd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 unconditionaly 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. #include <linux/config.h>
  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 <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/system.h>
  46. /* ports */
  47. #define ZF_IOBASE 0x218
  48. #define INDEX 0x218
  49. #define DATA_B 0x219
  50. #define DATA_W 0x21A
  51. #define DATA_D 0x21A
  52. /* indexes */ /* size */
  53. #define ZFL_VERSION 0x02 /* 16 */
  54. #define CONTROL 0x10 /* 16 */
  55. #define STATUS 0x12 /* 8 */
  56. #define COUNTER_1 0x0C /* 16 */
  57. #define COUNTER_2 0x0E /* 8 */
  58. #define PULSE_LEN 0x0F /* 8 */
  59. /* controls */
  60. #define ENABLE_WD1 0x0001
  61. #define ENABLE_WD2 0x0002
  62. #define RESET_WD1 0x0010
  63. #define RESET_WD2 0x0020
  64. #define GEN_SCI 0x0100
  65. #define GEN_NMI 0x0200
  66. #define GEN_SMI 0x0400
  67. #define GEN_RESET 0x0800
  68. /* utilities */
  69. #define WD1 0
  70. #define WD2 1
  71. #define zf_writew(port, data) { outb(port, INDEX); outw(data, DATA_W); }
  72. #define zf_writeb(port, data) { outb(port, INDEX); outb(data, DATA_B); }
  73. #define zf_get_ZFL_version() zf_readw(ZFL_VERSION)
  74. static unsigned short zf_readw(unsigned char port)
  75. {
  76. outb(port, INDEX);
  77. return inw(DATA_W);
  78. }
  79. MODULE_AUTHOR("Fernando Fuganti <fuganti@conectiva.com.br>");
  80. MODULE_DESCRIPTION("MachZ ZF-Logic Watchdog driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  83. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  84. static int nowayout = 1;
  85. #else
  86. static int nowayout = 0;
  87. #endif
  88. module_param(nowayout, int, 0);
  89. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  90. #define PFX "machzwd"
  91. static struct watchdog_info zf_info = {
  92. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  93. .firmware_version = 1,
  94. .identity = "ZF-Logic watchdog",
  95. };
  96. /*
  97. * action refers to action taken when watchdog resets
  98. * 0 = GEN_RESET
  99. * 1 = GEN_SMI
  100. * 2 = GEN_NMI
  101. * 3 = GEN_SCI
  102. * defaults to GEN_RESET (0)
  103. */
  104. static int action = 0;
  105. module_param(action, int, 0);
  106. MODULE_PARM_DESC(action, "after watchdog resets, generate: 0 = RESET(*) 1 = SMI 2 = NMI 3 = SCI");
  107. static int zf_action = GEN_RESET;
  108. static unsigned long zf_is_open;
  109. static char zf_expect_close;
  110. static spinlock_t zf_lock;
  111. static spinlock_t zf_port_lock;
  112. static struct timer_list zf_timer;
  113. static unsigned long next_heartbeat = 0;
  114. /* timeout for user land heart beat (10 seconds) */
  115. #define ZF_USER_TIMEO (HZ*10)
  116. /* timeout for hardware watchdog (~500ms) */
  117. #define ZF_HW_TIMEO (HZ/2)
  118. /* number of ticks on WD#1 (driven by a 32KHz clock, 2s) */
  119. #define ZF_CTIMEOUT 0xffff
  120. #ifndef ZF_DEBUG
  121. # define dprintk(format, args...)
  122. #else
  123. # define dprintk(format, args...) printk(KERN_DEBUG PFX ":%s:%d: " format, __FUNCTION__, __LINE__ , ## args)
  124. #endif
  125. static inline void zf_set_status(unsigned char new)
  126. {
  127. zf_writeb(STATUS, new);
  128. }
  129. /* CONTROL register functions */
  130. static inline unsigned short zf_get_control(void)
  131. {
  132. return zf_readw(CONTROL);
  133. }
  134. static inline void zf_set_control(unsigned short new)
  135. {
  136. zf_writew(CONTROL, new);
  137. }
  138. /* WD#? counter functions */
  139. /*
  140. * Just set counter value
  141. */
  142. static inline void zf_set_timer(unsigned short new, unsigned char n)
  143. {
  144. switch(n){
  145. case WD1:
  146. zf_writew(COUNTER_1, new);
  147. case WD2:
  148. zf_writeb(COUNTER_2, new > 0xff ? 0xff : new);
  149. default:
  150. return;
  151. }
  152. }
  153. /*
  154. * stop hardware timer
  155. */
  156. static void zf_timer_off(void)
  157. {
  158. unsigned int ctrl_reg = 0;
  159. unsigned long flags;
  160. /* stop internal ping */
  161. del_timer_sync(&zf_timer);
  162. spin_lock_irqsave(&zf_port_lock, flags);
  163. /* stop watchdog timer */
  164. ctrl_reg = zf_get_control();
  165. ctrl_reg |= (ENABLE_WD1|ENABLE_WD2); /* disable wd1 and wd2 */
  166. ctrl_reg &= ~(ENABLE_WD1|ENABLE_WD2);
  167. zf_set_control(ctrl_reg);
  168. spin_unlock_irqrestore(&zf_port_lock, flags);
  169. printk(KERN_INFO PFX ": Watchdog timer is now disabled\n");
  170. }
  171. /*
  172. * start hardware timer
  173. */
  174. static void zf_timer_on(void)
  175. {
  176. unsigned int ctrl_reg = 0;
  177. unsigned long flags;
  178. spin_lock_irqsave(&zf_port_lock, flags);
  179. zf_writeb(PULSE_LEN, 0xff);
  180. zf_set_timer(ZF_CTIMEOUT, WD1);
  181. /* user land ping */
  182. next_heartbeat = jiffies + ZF_USER_TIMEO;
  183. /* start the timer for internal ping */
  184. zf_timer.expires = jiffies + ZF_HW_TIMEO;
  185. add_timer(&zf_timer);
  186. /* start watchdog timer */
  187. ctrl_reg = zf_get_control();
  188. ctrl_reg |= (ENABLE_WD1|zf_action);
  189. zf_set_control(ctrl_reg);
  190. spin_unlock_irqrestore(&zf_port_lock, flags);
  191. printk(KERN_INFO PFX ": Watchdog timer is now enabled\n");
  192. }
  193. static void zf_ping(unsigned long data)
  194. {
  195. unsigned int ctrl_reg = 0;
  196. unsigned long flags;
  197. zf_writeb(COUNTER_2, 0xff);
  198. if(time_before(jiffies, next_heartbeat)){
  199. dprintk("time_before: %ld\n", next_heartbeat - jiffies);
  200. /*
  201. * reset event is activated by transition from 0 to 1 on
  202. * RESET_WD1 bit and we assume that it is already zero...
  203. */
  204. spin_lock_irqsave(&zf_port_lock, flags);
  205. ctrl_reg = zf_get_control();
  206. ctrl_reg |= RESET_WD1;
  207. zf_set_control(ctrl_reg);
  208. /* ...and nothing changes until here */
  209. ctrl_reg &= ~(RESET_WD1);
  210. zf_set_control(ctrl_reg);
  211. spin_unlock_irqrestore(&zf_port_lock, flags);
  212. zf_timer.expires = jiffies + ZF_HW_TIMEO;
  213. add_timer(&zf_timer);
  214. }else{
  215. printk(KERN_CRIT PFX ": I will reset your machine\n");
  216. }
  217. }
  218. static ssize_t zf_write(struct file *file, const char __user *buf, size_t count,
  219. loff_t *ppos)
  220. {
  221. /* See if we got the magic character */
  222. if(count){
  223. /*
  224. * no need to check for close confirmation
  225. * no way to disable watchdog ;)
  226. */
  227. if (!nowayout) {
  228. size_t ofs;
  229. /*
  230. * note: just in case someone wrote the magic character
  231. * five months ago...
  232. */
  233. zf_expect_close = 0;
  234. /* now scan */
  235. for (ofs = 0; ofs != count; ofs++){
  236. char c;
  237. if (get_user(c, buf + ofs))
  238. return -EFAULT;
  239. if (c == 'V'){
  240. zf_expect_close = 42;
  241. dprintk("zf_expect_close = 42\n");
  242. }
  243. }
  244. }
  245. /*
  246. * Well, anyhow someone wrote to us,
  247. * we should return that favour
  248. */
  249. next_heartbeat = jiffies + ZF_USER_TIMEO;
  250. dprintk("user ping at %ld\n", jiffies);
  251. }
  252. return count;
  253. }
  254. static int zf_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  255. unsigned long arg)
  256. {
  257. void __user *argp = (void __user *)arg;
  258. int __user *p = argp;
  259. switch(cmd){
  260. case WDIOC_GETSUPPORT:
  261. if (copy_to_user(argp, &zf_info, sizeof(zf_info)))
  262. return -EFAULT;
  263. break;
  264. case WDIOC_GETSTATUS:
  265. return put_user(0, p);
  266. case WDIOC_KEEPALIVE:
  267. zf_ping(0);
  268. break;
  269. default:
  270. return -ENOIOCTLCMD;
  271. }
  272. return 0;
  273. }
  274. static int zf_open(struct inode *inode, struct file *file)
  275. {
  276. spin_lock(&zf_lock);
  277. if(test_and_set_bit(0, &zf_is_open)) {
  278. spin_unlock(&zf_lock);
  279. return -EBUSY;
  280. }
  281. if (nowayout)
  282. __module_get(THIS_MODULE);
  283. spin_unlock(&zf_lock);
  284. zf_timer_on();
  285. return nonseekable_open(inode, file);
  286. }
  287. static int zf_close(struct inode *inode, struct file *file)
  288. {
  289. if(zf_expect_close == 42){
  290. zf_timer_off();
  291. } else {
  292. del_timer(&zf_timer);
  293. printk(KERN_ERR PFX ": device file closed unexpectedly. Will not stop the WDT!\n");
  294. }
  295. spin_lock(&zf_lock);
  296. clear_bit(0, &zf_is_open);
  297. spin_unlock(&zf_lock);
  298. zf_expect_close = 0;
  299. return 0;
  300. }
  301. /*
  302. * Notifier for system down
  303. */
  304. static int zf_notify_sys(struct notifier_block *this, unsigned long code,
  305. void *unused)
  306. {
  307. if(code == SYS_DOWN || code == SYS_HALT){
  308. zf_timer_off();
  309. }
  310. return NOTIFY_DONE;
  311. }
  312. static struct file_operations zf_fops = {
  313. .owner = THIS_MODULE,
  314. .llseek = no_llseek,
  315. .write = zf_write,
  316. .ioctl = zf_ioctl,
  317. .open = zf_open,
  318. .release = zf_close,
  319. };
  320. static struct miscdevice zf_miscdev = {
  321. .minor = WATCHDOG_MINOR,
  322. .name = "watchdog",
  323. .fops = &zf_fops,
  324. };
  325. /*
  326. * The device needs to learn about soft shutdowns in order to
  327. * turn the timebomb registers off.
  328. */
  329. static struct notifier_block zf_notifier = {
  330. .notifier_call = zf_notify_sys,
  331. };
  332. static void __init zf_show_action(int act)
  333. {
  334. char *str[] = { "RESET", "SMI", "NMI", "SCI" };
  335. printk(KERN_INFO PFX ": Watchdog using action = %s\n", str[act]);
  336. }
  337. static int __init zf_init(void)
  338. {
  339. int ret;
  340. printk(KERN_INFO PFX ": MachZ ZF-Logic Watchdog driver initializing.\n");
  341. ret = zf_get_ZFL_version();
  342. printk("%#x\n", ret);
  343. if((!ret) || (ret != 0xffff)){
  344. printk(KERN_WARNING PFX ": no ZF-Logic found\n");
  345. return -ENODEV;
  346. }
  347. if((action <= 3) && (action >= 0)){
  348. zf_action = zf_action>>action;
  349. } else
  350. action = 0;
  351. zf_show_action(action);
  352. spin_lock_init(&zf_lock);
  353. spin_lock_init(&zf_port_lock);
  354. ret = misc_register(&zf_miscdev);
  355. if (ret){
  356. printk(KERN_ERR "can't misc_register on minor=%d\n",
  357. WATCHDOG_MINOR);
  358. goto out;
  359. }
  360. if(!request_region(ZF_IOBASE, 3, "MachZ ZFL WDT")){
  361. printk(KERN_ERR "cannot reserve I/O ports at %d\n",
  362. ZF_IOBASE);
  363. ret = -EBUSY;
  364. goto no_region;
  365. }
  366. ret = register_reboot_notifier(&zf_notifier);
  367. if(ret){
  368. printk(KERN_ERR "can't register reboot notifier (err=%d)\n",
  369. ret);
  370. goto no_reboot;
  371. }
  372. zf_set_status(0);
  373. zf_set_control(0);
  374. /* this is the timer that will do the hard work */
  375. init_timer(&zf_timer);
  376. zf_timer.function = zf_ping;
  377. zf_timer.data = 0;
  378. return 0;
  379. no_reboot:
  380. release_region(ZF_IOBASE, 3);
  381. no_region:
  382. misc_deregister(&zf_miscdev);
  383. out:
  384. return ret;
  385. }
  386. static void __exit zf_exit(void)
  387. {
  388. zf_timer_off();
  389. misc_deregister(&zf_miscdev);
  390. unregister_reboot_notifier(&zf_notifier);
  391. release_region(ZF_IOBASE, 3);
  392. }
  393. module_init(zf_init);
  394. module_exit(zf_exit);