shwdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * drivers/char/watchdog/shwdt.c
  3. *
  4. * Watchdog driver for integrated watchdog in the SuperH processors.
  5. *
  6. * Copyright (C) 2001, 2002, 2003 Paul Mundt <lethal@linux-sh.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  14. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  15. *
  16. * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
  17. * Added expect close support, made emulated timeout runtime changeable
  18. * general cleanups, add some ioctls
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/reboot.h>
  27. #include <linux/notifier.h>
  28. #include <linux/ioport.h>
  29. #include <linux/fs.h>
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/watchdog.h>
  33. #define PFX "shwdt: "
  34. /*
  35. * Default clock division ratio is 5.25 msecs. For an additional table of
  36. * values, consult the asm-sh/watchdog.h. Overload this at module load
  37. * time.
  38. *
  39. * In order for this to work reliably we need to have HZ set to 1000 or
  40. * something quite higher than 100 (or we need a proper high-res timer
  41. * implementation that will deal with this properly), otherwise the 10ms
  42. * resolution of a jiffy is enough to trigger the overflow. For things like
  43. * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
  44. * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
  45. * necssary.
  46. *
  47. * As a result of this timing problem, the only modes that are particularly
  48. * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms
  49. * overflow periods respectively.
  50. *
  51. * Also, since we can't really expect userspace to be responsive enough
  52. * before the overflow happens, we maintain two seperate timers .. One in
  53. * the kernel for clearing out WOVF every 2ms or so (again, this depends on
  54. * HZ == 1000), and another for monitoring userspace writes to the WDT device.
  55. *
  56. * As such, we currently use a configurable heartbeat interval which defaults
  57. * to 30s. In this case, the userspace daemon is only responsible for periodic
  58. * writes to the device before the next heartbeat is scheduled. If the daemon
  59. * misses its deadline, the kernel timer will allow the WDT to overflow.
  60. */
  61. static int clock_division_ratio = WTCSR_CKS_4096;
  62. #define next_ping_period(cks) msecs_to_jiffies(cks - 4)
  63. static unsigned long shwdt_is_open;
  64. static struct watchdog_info sh_wdt_info;
  65. static char shwdt_expect_close;
  66. static struct timer_list timer;
  67. static unsigned long next_heartbeat;
  68. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
  69. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  70. static int nowayout = WATCHDOG_NOWAYOUT;
  71. /**
  72. * sh_wdt_start - Start the Watchdog
  73. *
  74. * Starts the watchdog.
  75. */
  76. static void sh_wdt_start(void)
  77. {
  78. __u8 csr;
  79. next_heartbeat = jiffies + (heartbeat * HZ);
  80. mod_timer(&timer, next_ping_period(clock_division_ratio));
  81. csr = sh_wdt_read_csr();
  82. csr |= WTCSR_WT | clock_division_ratio;
  83. sh_wdt_write_csr(csr);
  84. sh_wdt_write_cnt(0);
  85. /*
  86. * These processors have a bit of an inconsistent initialization
  87. * process.. starting with SH-3, RSTS was moved to WTCSR, and the
  88. * RSTCSR register was removed.
  89. *
  90. * On the SH-2 however, in addition with bits being in different
  91. * locations, we must deal with RSTCSR outright..
  92. */
  93. csr = sh_wdt_read_csr();
  94. csr |= WTCSR_TME;
  95. csr &= ~WTCSR_RSTS;
  96. sh_wdt_write_csr(csr);
  97. #ifdef CONFIG_CPU_SH2
  98. /*
  99. * Whoever came up with the RSTCSR semantics must've been smoking
  100. * some of the good stuff, since in addition to the WTCSR/WTCNT write
  101. * brain-damage, it's managed to fuck things up one step further..
  102. *
  103. * If we need to clear the WOVF bit, the upper byte has to be 0xa5..
  104. * but if we want to touch RSTE or RSTS, the upper byte has to be
  105. * 0x5a..
  106. */
  107. csr = sh_wdt_read_rstcsr();
  108. csr &= ~RSTCSR_RSTS;
  109. sh_wdt_write_rstcsr(csr);
  110. #endif
  111. }
  112. /**
  113. * sh_wdt_stop - Stop the Watchdog
  114. *
  115. * Stops the watchdog.
  116. */
  117. static void sh_wdt_stop(void)
  118. {
  119. __u8 csr;
  120. del_timer(&timer);
  121. csr = sh_wdt_read_csr();
  122. csr &= ~WTCSR_TME;
  123. sh_wdt_write_csr(csr);
  124. }
  125. /**
  126. * sh_wdt_keepalive - Keep the Userspace Watchdog Alive
  127. *
  128. * The Userspace watchdog got a KeepAlive: schedule the next heartbeat.
  129. */
  130. static void sh_wdt_keepalive(void)
  131. {
  132. next_heartbeat = jiffies + (heartbeat * HZ);
  133. }
  134. /**
  135. * sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat
  136. *
  137. * Set the Userspace Watchdog heartbeat
  138. */
  139. static int sh_wdt_set_heartbeat(int t)
  140. {
  141. if ((t < 1) || (t > 3600)) /* arbitrary upper limit */
  142. return -EINVAL;
  143. heartbeat = t;
  144. return 0;
  145. }
  146. /**
  147. * sh_wdt_ping - Ping the Watchdog
  148. *
  149. * @data: Unused
  150. *
  151. * Clears overflow bit, resets timer counter.
  152. */
  153. static void sh_wdt_ping(unsigned long data)
  154. {
  155. if (time_before(jiffies, next_heartbeat)) {
  156. __u8 csr;
  157. csr = sh_wdt_read_csr();
  158. csr &= ~WTCSR_IOVF;
  159. sh_wdt_write_csr(csr);
  160. sh_wdt_write_cnt(0);
  161. mod_timer(&timer, next_ping_period(clock_division_ratio));
  162. } else {
  163. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  164. }
  165. }
  166. /**
  167. * sh_wdt_open - Open the Device
  168. *
  169. * @inode: inode of device
  170. * @file: file handle of device
  171. *
  172. * Watchdog device is opened and started.
  173. */
  174. static int sh_wdt_open(struct inode *inode, struct file *file)
  175. {
  176. if (test_and_set_bit(0, &shwdt_is_open))
  177. return -EBUSY;
  178. if (nowayout)
  179. __module_get(THIS_MODULE);
  180. sh_wdt_start();
  181. return nonseekable_open(inode, file);
  182. }
  183. /**
  184. * sh_wdt_close - Close the Device
  185. *
  186. * @inode: inode of device
  187. * @file: file handle of device
  188. *
  189. * Watchdog device is closed and stopped.
  190. */
  191. static int sh_wdt_close(struct inode *inode, struct file *file)
  192. {
  193. if (shwdt_expect_close == 42) {
  194. sh_wdt_stop();
  195. } else {
  196. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  197. sh_wdt_keepalive();
  198. }
  199. clear_bit(0, &shwdt_is_open);
  200. shwdt_expect_close = 0;
  201. return 0;
  202. }
  203. /**
  204. * sh_wdt_write - Write to Device
  205. *
  206. * @file: file handle of device
  207. * @buf: buffer to write
  208. * @count: length of buffer
  209. * @ppos: offset
  210. *
  211. * Pings the watchdog on write.
  212. */
  213. static ssize_t sh_wdt_write(struct file *file, const char *buf,
  214. size_t count, loff_t *ppos)
  215. {
  216. if (count) {
  217. if (!nowayout) {
  218. size_t i;
  219. shwdt_expect_close = 0;
  220. for (i = 0; i != count; i++) {
  221. char c;
  222. if (get_user(c, buf + i))
  223. return -EFAULT;
  224. if (c == 'V')
  225. shwdt_expect_close = 42;
  226. }
  227. }
  228. sh_wdt_keepalive();
  229. }
  230. return count;
  231. }
  232. /**
  233. * sh_wdt_ioctl - Query Device
  234. *
  235. * @inode: inode of device
  236. * @file: file handle of device
  237. * @cmd: watchdog command
  238. * @arg: argument
  239. *
  240. * Query basic information from the device or ping it, as outlined by the
  241. * watchdog API.
  242. */
  243. static int sh_wdt_ioctl(struct inode *inode, struct file *file,
  244. unsigned int cmd, unsigned long arg)
  245. {
  246. int new_heartbeat;
  247. int options, retval = -EINVAL;
  248. switch (cmd) {
  249. case WDIOC_GETSUPPORT:
  250. return copy_to_user((struct watchdog_info *)arg,
  251. &sh_wdt_info,
  252. sizeof(sh_wdt_info)) ? -EFAULT : 0;
  253. case WDIOC_GETSTATUS:
  254. case WDIOC_GETBOOTSTATUS:
  255. return put_user(0, (int *)arg);
  256. case WDIOC_KEEPALIVE:
  257. sh_wdt_keepalive();
  258. return 0;
  259. case WDIOC_SETTIMEOUT:
  260. if (get_user(new_heartbeat, (int *)arg))
  261. return -EFAULT;
  262. if (sh_wdt_set_heartbeat(new_heartbeat))
  263. return -EINVAL;
  264. sh_wdt_keepalive();
  265. /* Fall */
  266. case WDIOC_GETTIMEOUT:
  267. return put_user(heartbeat, (int *)arg);
  268. case WDIOC_SETOPTIONS:
  269. if (get_user(options, (int *)arg))
  270. return -EFAULT;
  271. if (options & WDIOS_DISABLECARD) {
  272. sh_wdt_stop();
  273. retval = 0;
  274. }
  275. if (options & WDIOS_ENABLECARD) {
  276. sh_wdt_start();
  277. retval = 0;
  278. }
  279. return retval;
  280. default:
  281. return -ENOIOCTLCMD;
  282. }
  283. return 0;
  284. }
  285. /**
  286. * sh_wdt_notify_sys - Notifier Handler
  287. *
  288. * @this: notifier block
  289. * @code: notifier event
  290. * @unused: unused
  291. *
  292. * Handles specific events, such as turning off the watchdog during a
  293. * shutdown event.
  294. */
  295. static int sh_wdt_notify_sys(struct notifier_block *this,
  296. unsigned long code, void *unused)
  297. {
  298. if (code == SYS_DOWN || code == SYS_HALT) {
  299. sh_wdt_stop();
  300. }
  301. return NOTIFY_DONE;
  302. }
  303. static const struct file_operations sh_wdt_fops = {
  304. .owner = THIS_MODULE,
  305. .llseek = no_llseek,
  306. .write = sh_wdt_write,
  307. .ioctl = sh_wdt_ioctl,
  308. .open = sh_wdt_open,
  309. .release = sh_wdt_close,
  310. };
  311. static struct watchdog_info sh_wdt_info = {
  312. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  313. .firmware_version = 1,
  314. .identity = "SH WDT",
  315. };
  316. static struct notifier_block sh_wdt_notifier = {
  317. .notifier_call = sh_wdt_notify_sys,
  318. };
  319. static struct miscdevice sh_wdt_miscdev = {
  320. .minor = WATCHDOG_MINOR,
  321. .name = "watchdog",
  322. .fops = &sh_wdt_fops,
  323. };
  324. /**
  325. * sh_wdt_init - Initialize module
  326. *
  327. * Registers the device and notifier handler. Actual device
  328. * initialization is handled by sh_wdt_open().
  329. */
  330. static int __init sh_wdt_init(void)
  331. {
  332. int rc;
  333. if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) {
  334. clock_division_ratio = WTCSR_CKS_4096;
  335. printk(KERN_INFO PFX "clock_division_ratio value must be 0x5<=x<=0x7, using %d\n",
  336. clock_division_ratio);
  337. }
  338. if (sh_wdt_set_heartbeat(heartbeat))
  339. {
  340. heartbeat = WATCHDOG_HEARTBEAT;
  341. printk(KERN_INFO PFX "heartbeat value must be 1<=x<=3600, using %d\n",
  342. heartbeat);
  343. }
  344. init_timer(&timer);
  345. timer.function = sh_wdt_ping;
  346. timer.data = 0;
  347. rc = register_reboot_notifier(&sh_wdt_notifier);
  348. if (rc) {
  349. printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n", rc);
  350. return rc;
  351. }
  352. rc = misc_register(&sh_wdt_miscdev);
  353. if (rc) {
  354. printk(KERN_ERR PFX "Can't register miscdev on minor=%d (err=%d)\n",
  355. sh_wdt_miscdev.minor, rc);
  356. unregister_reboot_notifier(&sh_wdt_notifier);
  357. return rc;
  358. }
  359. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  360. heartbeat, nowayout);
  361. return 0;
  362. }
  363. /**
  364. * sh_wdt_exit - Deinitialize module
  365. *
  366. * Unregisters the device and notifier handler. Actual device
  367. * deinitialization is handled by sh_wdt_close().
  368. */
  369. static void __exit sh_wdt_exit(void)
  370. {
  371. misc_deregister(&sh_wdt_miscdev);
  372. unregister_reboot_notifier(&sh_wdt_notifier);
  373. }
  374. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  375. MODULE_DESCRIPTION("SuperH watchdog driver");
  376. MODULE_LICENSE("GPL");
  377. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  378. module_param(clock_division_ratio, int, 0);
  379. MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). Defaults to 0x7.");
  380. module_param(heartbeat, int, 0);
  381. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  382. module_param(nowayout, int, 0);
  383. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  384. module_init(sh_wdt_init);
  385. module_exit(sh_wdt_exit);