shwdt.c 11 KB

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