shwdt.c 11 KB

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