eurotechwdt.c 10 KB

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