shwdt.c 13 KB

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