eurotechwdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Eurotech CPU-1220/1410 on board WDT driver
  3. *
  4. * (c) Copyright 2001 Ascensit <support@ascensit.com>
  5. * (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com>
  6. * (c) Copyright 2002 Rob Radez <rob@osinvestor.com>
  7. *
  8. * Based on wdt.c.
  9. * Original copyright messages:
  10. *
  11. * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
  12. * http://www.redhat.com
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  20. * warranty for any of this software. This material is provided
  21. * "AS-IS" and at no charge.
  22. *
  23. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>*
  24. */
  25. /* Changelog:
  26. *
  27. * 2002/04/25 - Rob Radez
  28. * clean up #includes
  29. * clean up locking
  30. * make __setup param unique
  31. * proper options in watchdog_info
  32. * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
  33. * add expect_close support
  34. *
  35. * 2001 - Rodolfo Giometti
  36. * Initial release
  37. *
  38. * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
  39. * Added Matt Domsch's nowayout module option.
  40. */
  41. #include <linux/config.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/types.h>
  46. #include <linux/miscdevice.h>
  47. #include <linux/watchdog.h>
  48. #include <linux/fs.h>
  49. #include <linux/ioport.h>
  50. #include <linux/notifier.h>
  51. #include <linux/reboot.h>
  52. #include <linux/init.h>
  53. #include <asm/io.h>
  54. #include <asm/uaccess.h>
  55. #include <asm/system.h>
  56. static unsigned long eurwdt_is_open;
  57. static int eurwdt_timeout;
  58. static char eur_expect_close;
  59. /*
  60. * You must set these - there is no sane way to probe for this board.
  61. * You can use eurwdt=x,y to set these now.
  62. */
  63. static int io = 0x3f0;
  64. static int irq = 10;
  65. static char *ev = "int";
  66. #define WDT_TIMEOUT 60 /* 1 minute */
  67. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  68. static int nowayout = 1;
  69. #else
  70. static int nowayout = 0;
  71. #endif
  72. module_param(nowayout, int, 0);
  73. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  74. /*
  75. * Some symbolic names
  76. */
  77. #define WDT_CTRL_REG 0x30
  78. #define WDT_OUTPIN_CFG 0xe2
  79. #define WDT_EVENT_INT 0x00
  80. #define WDT_EVENT_REBOOT 0x08
  81. #define WDT_UNIT_SEL 0xf1
  82. #define WDT_UNIT_SECS 0x80
  83. #define WDT_TIMEOUT_VAL 0xf2
  84. #define WDT_TIMER_CFG 0xf3
  85. module_param(io, int, 0);
  86. MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
  87. module_param(irq, int, 0);
  88. MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
  89. module_param(ev, charp, 0);
  90. MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
  91. /*
  92. * Programming support
  93. */
  94. static inline void eurwdt_write_reg(u8 index, u8 data)
  95. {
  96. outb(index, io);
  97. outb(data, io+1);
  98. }
  99. static inline void eurwdt_lock_chip(void)
  100. {
  101. outb(0xaa, io);
  102. }
  103. static inline void eurwdt_unlock_chip(void)
  104. {
  105. outb(0x55, io);
  106. eurwdt_write_reg(0x07, 0x08); /* set the logical device */
  107. }
  108. static inline void eurwdt_set_timeout(int timeout)
  109. {
  110. eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
  111. }
  112. static inline void eurwdt_disable_timer(void)
  113. {
  114. eurwdt_set_timeout(0);
  115. }
  116. static void eurwdt_activate_timer(void)
  117. {
  118. eurwdt_disable_timer();
  119. eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */
  120. eurwdt_write_reg(WDT_OUTPIN_CFG, !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
  121. /* Setting interrupt line */
  122. if (irq == 2 || irq > 15 || irq < 0) {
  123. printk(KERN_ERR ": invalid irq number\n");
  124. irq = 0; /* if invalid we disable interrupt */
  125. }
  126. if (irq == 0)
  127. printk(KERN_INFO ": interrupt disabled\n");
  128. eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
  129. eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
  130. eurwdt_set_timeout(0); /* the default timeout */
  131. }
  132. /*
  133. * Kernel methods.
  134. */
  135. static irqreturn_t eurwdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  136. {
  137. printk(KERN_CRIT "timeout WDT timeout\n");
  138. #ifdef ONLY_TESTING
  139. printk(KERN_CRIT "Would Reboot.\n");
  140. #else
  141. printk(KERN_CRIT "Initiating system reboot.\n");
  142. machine_restart(NULL);
  143. #endif
  144. return IRQ_HANDLED;
  145. }
  146. /**
  147. * eurwdt_ping:
  148. *
  149. * Reload counter one with the watchdog timeout.
  150. */
  151. static void eurwdt_ping(void)
  152. {
  153. /* Write the watchdog default value */
  154. eurwdt_set_timeout(eurwdt_timeout);
  155. }
  156. /**
  157. * eurwdt_write:
  158. * @file: file handle to the watchdog
  159. * @buf: buffer to write (unused as data does not matter here
  160. * @count: count of bytes
  161. * @ppos: pointer to the position to write. No seeks allowed
  162. *
  163. * A write to a watchdog device is defined as a keepalive signal. Any
  164. * write of data will do, as we we don't define content meaning.
  165. */
  166. static ssize_t eurwdt_write(struct file *file, const char __user *buf,
  167. size_t count, loff_t *ppos)
  168. {
  169. if (count) {
  170. if (!nowayout) {
  171. size_t i;
  172. eur_expect_close = 0;
  173. for (i = 0; i != count; i++) {
  174. char c;
  175. if(get_user(c, buf+i))
  176. return -EFAULT;
  177. if (c == 'V')
  178. eur_expect_close = 42;
  179. }
  180. }
  181. eurwdt_ping(); /* the default timeout */
  182. }
  183. return count;
  184. }
  185. /**
  186. * eurwdt_ioctl:
  187. * @inode: inode of the device
  188. * @file: file handle to the device
  189. * @cmd: watchdog command
  190. * @arg: argument pointer
  191. *
  192. * The watchdog API defines a common set of functions for all watchdogs
  193. * according to their available features.
  194. */
  195. static int eurwdt_ioctl(struct inode *inode, struct file *file,
  196. unsigned int cmd, unsigned long arg)
  197. {
  198. void __user *argp = (void __user *)arg;
  199. int __user *p = argp;
  200. static struct watchdog_info ident = {
  201. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  202. .firmware_version = 1,
  203. .identity = "WDT Eurotech CPU-1220/1410",
  204. };
  205. int time;
  206. int options, retval = -EINVAL;
  207. switch(cmd) {
  208. default:
  209. return -ENOIOCTLCMD;
  210. case WDIOC_GETSUPPORT:
  211. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  212. case WDIOC_GETSTATUS:
  213. case WDIOC_GETBOOTSTATUS:
  214. return put_user(0, p);
  215. case WDIOC_KEEPALIVE:
  216. eurwdt_ping();
  217. return 0;
  218. case WDIOC_SETTIMEOUT:
  219. if (copy_from_user(&time, p, sizeof(int)))
  220. return -EFAULT;
  221. /* Sanity check */
  222. if (time < 0 || time > 255)
  223. return -EINVAL;
  224. eurwdt_timeout = time;
  225. eurwdt_set_timeout(time);
  226. /* Fall */
  227. case WDIOC_GETTIMEOUT:
  228. return put_user(eurwdt_timeout, p);
  229. case WDIOC_SETOPTIONS:
  230. if (get_user(options, p))
  231. return -EFAULT;
  232. if (options & WDIOS_DISABLECARD) {
  233. eurwdt_disable_timer();
  234. retval = 0;
  235. }
  236. if (options & WDIOS_ENABLECARD) {
  237. eurwdt_activate_timer();
  238. eurwdt_ping();
  239. retval = 0;
  240. }
  241. return retval;
  242. }
  243. }
  244. /**
  245. * eurwdt_open:
  246. * @inode: inode of device
  247. * @file: file handle to device
  248. *
  249. * The misc device has been opened. The watchdog device is single
  250. * open and on opening we load the counter.
  251. */
  252. static int eurwdt_open(struct inode *inode, struct file *file)
  253. {
  254. if (test_and_set_bit(0, &eurwdt_is_open))
  255. return -EBUSY;
  256. eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
  257. /* Activate the WDT */
  258. eurwdt_activate_timer();
  259. return nonseekable_open(inode, file);
  260. }
  261. /**
  262. * eurwdt_release:
  263. * @inode: inode to board
  264. * @file: file handle to board
  265. *
  266. * The watchdog has a configurable API. There is a religious dispute
  267. * between people who want their watchdog to be able to shut down and
  268. * those who want to be sure if the watchdog manager dies the machine
  269. * reboots. In the former case we disable the counters, in the latter
  270. * case you have to open it again very soon.
  271. */
  272. static int eurwdt_release(struct inode *inode, struct file *file)
  273. {
  274. if (eur_expect_close == 42) {
  275. eurwdt_disable_timer();
  276. } else {
  277. printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
  278. eurwdt_ping();
  279. }
  280. clear_bit(0, &eurwdt_is_open);
  281. eur_expect_close = 0;
  282. return 0;
  283. }
  284. /**
  285. * eurwdt_notify_sys:
  286. * @this: our notifier block
  287. * @code: the event being reported
  288. * @unused: unused
  289. *
  290. * Our notifier is called on system shutdowns. We want to turn the card
  291. * off at reboot otherwise the machine will reboot again during memory
  292. * test or worse yet during the following fsck. This would suck, in fact
  293. * trust me - if it happens it does suck.
  294. */
  295. static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
  296. void *unused)
  297. {
  298. if (code == SYS_DOWN || code == SYS_HALT) {
  299. /* Turn the card off */
  300. eurwdt_disable_timer();
  301. }
  302. return NOTIFY_DONE;
  303. }
  304. /*
  305. * Kernel Interfaces
  306. */
  307. static struct file_operations eurwdt_fops = {
  308. .owner = THIS_MODULE,
  309. .llseek = no_llseek,
  310. .write = eurwdt_write,
  311. .ioctl = eurwdt_ioctl,
  312. .open = eurwdt_open,
  313. .release = eurwdt_release,
  314. };
  315. static struct miscdevice eurwdt_miscdev = {
  316. .minor = WATCHDOG_MINOR,
  317. .name = "watchdog",
  318. .fops = &eurwdt_fops,
  319. };
  320. /*
  321. * The WDT card needs to learn about soft shutdowns in order to
  322. * turn the timebomb registers off.
  323. */
  324. static struct notifier_block eurwdt_notifier = {
  325. .notifier_call = eurwdt_notify_sys,
  326. };
  327. /**
  328. * cleanup_module:
  329. *
  330. * Unload the watchdog. You cannot do this with any file handles open.
  331. * If your watchdog is set to continue ticking on close and you unload
  332. * it, well it keeps ticking. We won't get the interrupt but the board
  333. * will not touch PC memory so all is fine. You just have to load a new
  334. * module in 60 seconds or reboot.
  335. */
  336. static void __exit eurwdt_exit(void)
  337. {
  338. eurwdt_lock_chip();
  339. misc_deregister(&eurwdt_miscdev);
  340. unregister_reboot_notifier(&eurwdt_notifier);
  341. release_region(io, 2);
  342. free_irq(irq, NULL);
  343. }
  344. /**
  345. * eurwdt_init:
  346. *
  347. * Set up the WDT watchdog board. After grabbing the resources
  348. * we require we need also to unlock the device.
  349. * The open() function will actually kick the board off.
  350. */
  351. static int __init eurwdt_init(void)
  352. {
  353. int ret;
  354. ret = misc_register(&eurwdt_miscdev);
  355. if (ret) {
  356. printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
  357. WATCHDOG_MINOR);
  358. goto out;
  359. }
  360. ret = request_irq(irq, eurwdt_interrupt, SA_INTERRUPT, "eurwdt", NULL);
  361. if(ret) {
  362. printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
  363. goto outmisc;
  364. }
  365. if (!request_region(io, 2, "eurwdt")) {
  366. printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
  367. ret = -EBUSY;
  368. goto outirq;
  369. }
  370. ret = register_reboot_notifier(&eurwdt_notifier);
  371. if (ret) {
  372. printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
  373. goto outreg;
  374. }
  375. eurwdt_unlock_chip();
  376. ret = 0;
  377. printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
  378. " - timeout event: %s\n",
  379. io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
  380. out:
  381. return ret;
  382. outreg:
  383. release_region(io, 2);
  384. outirq:
  385. free_irq(irq, NULL);
  386. outmisc:
  387. misc_deregister(&eurwdt_miscdev);
  388. goto out;
  389. }
  390. module_init(eurwdt_init);
  391. module_exit(eurwdt_exit);
  392. MODULE_AUTHOR("Rodolfo Giometti");
  393. MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
  394. MODULE_LICENSE("GPL");
  395. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);