shwdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 <linux/mm.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. * 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. * The Userspace watchdog got a KeepAlive: schedule the next heartbeat.
  128. */
  129. static inline void sh_wdt_keepalive(void)
  130. {
  131. next_heartbeat = jiffies + (heartbeat * HZ);
  132. }
  133. /**
  134. * sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat
  135. * Set the Userspace Watchdog heartbeat
  136. */
  137. static int sh_wdt_set_heartbeat(int t)
  138. {
  139. if (unlikely((t < 1) || (t > 3600))) /* arbitrary upper limit */
  140. return -EINVAL;
  141. heartbeat = t;
  142. return 0;
  143. }
  144. /**
  145. * sh_wdt_ping - Ping the Watchdog
  146. * @data: Unused
  147. *
  148. * Clears overflow bit, resets timer counter.
  149. */
  150. static void sh_wdt_ping(unsigned long data)
  151. {
  152. if (time_before(jiffies, next_heartbeat)) {
  153. __u8 csr;
  154. csr = sh_wdt_read_csr();
  155. csr &= ~WTCSR_IOVF;
  156. sh_wdt_write_csr(csr);
  157. sh_wdt_write_cnt(0);
  158. mod_timer(&timer, next_ping_period(clock_division_ratio));
  159. } else
  160. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping "
  161. "the watchdog\n");
  162. }
  163. /**
  164. * sh_wdt_open - Open the Device
  165. * @inode: inode of device
  166. * @file: file handle of device
  167. *
  168. * Watchdog device is opened and started.
  169. */
  170. static int sh_wdt_open(struct inode *inode, struct file *file)
  171. {
  172. if (test_and_set_bit(0, &shwdt_is_open))
  173. return -EBUSY;
  174. if (nowayout)
  175. __module_get(THIS_MODULE);
  176. sh_wdt_start();
  177. return nonseekable_open(inode, file);
  178. }
  179. /**
  180. * sh_wdt_close - Close the Device
  181. * @inode: inode of device
  182. * @file: file handle of device
  183. *
  184. * Watchdog device is closed and stopped.
  185. */
  186. static int sh_wdt_close(struct inode *inode, struct file *file)
  187. {
  188. if (shwdt_expect_close == 42) {
  189. sh_wdt_stop();
  190. } else {
  191. printk(KERN_CRIT PFX "Unexpected close, not "
  192. "stopping watchdog!\n");
  193. sh_wdt_keepalive();
  194. }
  195. clear_bit(0, &shwdt_is_open);
  196. shwdt_expect_close = 0;
  197. return 0;
  198. }
  199. /**
  200. * sh_wdt_write - Write to Device
  201. * @file: file handle of device
  202. * @buf: buffer to write
  203. * @count: length of buffer
  204. * @ppos: offset
  205. *
  206. * Pings the watchdog on write.
  207. */
  208. static ssize_t sh_wdt_write(struct file *file, const char *buf,
  209. size_t count, loff_t *ppos)
  210. {
  211. if (count) {
  212. if (!nowayout) {
  213. size_t i;
  214. shwdt_expect_close = 0;
  215. for (i = 0; i != count; i++) {
  216. char c;
  217. if (get_user(c, buf + i))
  218. return -EFAULT;
  219. if (c == 'V')
  220. shwdt_expect_close = 42;
  221. }
  222. }
  223. sh_wdt_keepalive();
  224. }
  225. return count;
  226. }
  227. /**
  228. * sh_wdt_mmap - map WDT/CPG registers into userspace
  229. * @file: file structure for the device
  230. * @vma: VMA to map the registers into
  231. *
  232. * A simple mmap() implementation for the corner cases where the counter
  233. * needs to be mapped in userspace directly. Due to the relatively small
  234. * size of the area, neighbouring registers not necessarily tied to the
  235. * CPG will also be accessible through the register page, so this remains
  236. * configurable for users that really know what they're doing.
  237. *
  238. * Additionaly, the register page maps in the CPG register base relative
  239. * to the nearest page-aligned boundary, which requires that userspace do
  240. * the appropriate CPU subtype math for calculating the page offset for
  241. * the counter value.
  242. */
  243. static int sh_wdt_mmap(struct file *file, struct vm_area_struct *vma)
  244. {
  245. int ret = -ENOSYS;
  246. #ifdef CONFIG_SH_WDT_MMAP
  247. unsigned long addr;
  248. /* Only support the simple cases where we map in a register page. */
  249. if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
  250. return -EINVAL;
  251. /*
  252. * Pick WTCNT as the start, it's usually the first register after the
  253. * FRQCR, and neither one are generally page-aligned out of the box.
  254. */
  255. addr = WTCNT & ~(PAGE_SIZE - 1);
  256. vma->vm_flags |= VM_IO;
  257. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  258. if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
  259. PAGE_SIZE, vma->vm_page_prot)) {
  260. printk(KERN_ERR PFX "%s: io_remap_pfn_range failed\n",
  261. __FUNCTION__);
  262. return -EAGAIN;
  263. }
  264. ret = 0;
  265. #endif
  266. return ret;
  267. }
  268. /**
  269. * sh_wdt_ioctl - Query Device
  270. * @inode: inode of device
  271. * @file: file handle of device
  272. * @cmd: watchdog command
  273. * @arg: argument
  274. *
  275. * Query basic information from the device or ping it, as outlined by the
  276. * watchdog API.
  277. */
  278. static int sh_wdt_ioctl(struct inode *inode, struct file *file,
  279. unsigned int cmd, unsigned long arg)
  280. {
  281. int new_heartbeat;
  282. int options, retval = -EINVAL;
  283. switch (cmd) {
  284. case WDIOC_GETSUPPORT:
  285. return copy_to_user((struct watchdog_info *)arg,
  286. &sh_wdt_info,
  287. sizeof(sh_wdt_info)) ? -EFAULT : 0;
  288. case WDIOC_GETSTATUS:
  289. case WDIOC_GETBOOTSTATUS:
  290. return put_user(0, (int *)arg);
  291. case WDIOC_KEEPALIVE:
  292. sh_wdt_keepalive();
  293. return 0;
  294. case WDIOC_SETTIMEOUT:
  295. if (get_user(new_heartbeat, (int *)arg))
  296. return -EFAULT;
  297. if (sh_wdt_set_heartbeat(new_heartbeat))
  298. return -EINVAL;
  299. sh_wdt_keepalive();
  300. /* Fall */
  301. case WDIOC_GETTIMEOUT:
  302. return put_user(heartbeat, (int *)arg);
  303. case WDIOC_SETOPTIONS:
  304. if (get_user(options, (int *)arg))
  305. return -EFAULT;
  306. if (options & WDIOS_DISABLECARD) {
  307. sh_wdt_stop();
  308. retval = 0;
  309. }
  310. if (options & WDIOS_ENABLECARD) {
  311. sh_wdt_start();
  312. retval = 0;
  313. }
  314. return retval;
  315. default:
  316. return -ENOTTY;
  317. }
  318. return 0;
  319. }
  320. /**
  321. * sh_wdt_notify_sys - Notifier Handler
  322. * @this: notifier block
  323. * @code: notifier event
  324. * @unused: unused
  325. *
  326. * Handles specific events, such as turning off the watchdog during a
  327. * shutdown event.
  328. */
  329. static int sh_wdt_notify_sys(struct notifier_block *this,
  330. unsigned long code, void *unused)
  331. {
  332. if (code == SYS_DOWN || code == SYS_HALT)
  333. sh_wdt_stop();
  334. return NOTIFY_DONE;
  335. }
  336. static const struct file_operations sh_wdt_fops = {
  337. .owner = THIS_MODULE,
  338. .llseek = no_llseek,
  339. .write = sh_wdt_write,
  340. .ioctl = sh_wdt_ioctl,
  341. .open = sh_wdt_open,
  342. .release = sh_wdt_close,
  343. .mmap = sh_wdt_mmap,
  344. };
  345. static struct watchdog_info sh_wdt_info = {
  346. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  347. WDIOF_MAGICCLOSE,
  348. .firmware_version = 1,
  349. .identity = "SH WDT",
  350. };
  351. static struct notifier_block sh_wdt_notifier = {
  352. .notifier_call = sh_wdt_notify_sys,
  353. };
  354. static struct miscdevice sh_wdt_miscdev = {
  355. .minor = WATCHDOG_MINOR,
  356. .name = "watchdog",
  357. .fops = &sh_wdt_fops,
  358. };
  359. /**
  360. * sh_wdt_init - Initialize module
  361. * Registers the device and notifier handler. Actual device
  362. * initialization is handled by sh_wdt_open().
  363. */
  364. static int __init sh_wdt_init(void)
  365. {
  366. int rc;
  367. if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) {
  368. clock_division_ratio = WTCSR_CKS_4096;
  369. printk(KERN_INFO PFX "clock_division_ratio value must "
  370. "be 0x5<=x<=0x7, using %d\n", clock_division_ratio);
  371. }
  372. rc = sh_wdt_set_heartbeat(heartbeat);
  373. if (unlikely(rc)) {
  374. heartbeat = WATCHDOG_HEARTBEAT;
  375. printk(KERN_INFO PFX "heartbeat value must "
  376. "be 1<=x<=3600, using %d\n", heartbeat);
  377. }
  378. init_timer(&timer);
  379. timer.function = sh_wdt_ping;
  380. timer.data = 0;
  381. rc = register_reboot_notifier(&sh_wdt_notifier);
  382. if (unlikely(rc)) {
  383. printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n",
  384. rc);
  385. return rc;
  386. }
  387. rc = misc_register(&sh_wdt_miscdev);
  388. if (unlikely(rc)) {
  389. printk(KERN_ERR PFX "Can't register miscdev on "
  390. "minor=%d (err=%d)\n", sh_wdt_miscdev.minor, rc);
  391. unregister_reboot_notifier(&sh_wdt_notifier);
  392. return rc;
  393. }
  394. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  395. heartbeat, nowayout);
  396. return 0;
  397. }
  398. /**
  399. * sh_wdt_exit - Deinitialize module
  400. * Unregisters the device and notifier handler. Actual device
  401. * deinitialization is handled by sh_wdt_close().
  402. */
  403. static void __exit sh_wdt_exit(void)
  404. {
  405. misc_deregister(&sh_wdt_miscdev);
  406. unregister_reboot_notifier(&sh_wdt_notifier);
  407. }
  408. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  409. MODULE_DESCRIPTION("SuperH watchdog driver");
  410. MODULE_LICENSE("GPL");
  411. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  412. module_param(clock_division_ratio, int, 0);
  413. MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")");
  414. module_param(heartbeat, int, 0);
  415. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  416. module_param(nowayout, int, 0);
  417. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  418. module_init(sh_wdt_init);
  419. module_exit(sh_wdt_exit);