shwdt.c 13 KB

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