shwdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * drivers/watchdog/shwdt.c
  3. *
  4. * Watchdog driver for integrated watchdog in the SuperH processors.
  5. *
  6. * Copyright (C) 2001 - 2012 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/ioport.h>
  29. #include <linux/fs.h>
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/watchdog.h>
  35. #define DRV_NAME "sh-wdt"
  36. /*
  37. * Default clock division ratio is 5.25 msecs. For an additional table of
  38. * values, consult the asm-sh/watchdog.h. Overload this at module load
  39. * time.
  40. *
  41. * In order for this to work reliably we need to have HZ set to 1000 or
  42. * something quite higher than 100 (or we need a proper high-res timer
  43. * implementation that will deal with this properly), otherwise the 10ms
  44. * resolution of a jiffy is enough to trigger the overflow. For things like
  45. * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
  46. * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
  47. * necssary.
  48. *
  49. * As a result of this timing problem, the only modes that are particularly
  50. * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
  51. * overflow periods respectively.
  52. *
  53. * Also, since we can't really expect userspace to be responsive enough
  54. * before the overflow happens, we maintain two separate timers .. One in
  55. * the kernel for clearing out WOVF every 2ms or so (again, this depends on
  56. * HZ == 1000), and another for monitoring userspace writes to the WDT device.
  57. *
  58. * As such, we currently use a configurable heartbeat interval which defaults
  59. * to 30s. In this case, the userspace daemon is only responsible for periodic
  60. * writes to the device before the next heartbeat is scheduled. If the daemon
  61. * misses its deadline, the kernel timer will allow the WDT to overflow.
  62. */
  63. static int clock_division_ratio = WTCSR_CKS_4096;
  64. #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
  65. static const struct watchdog_info sh_wdt_info;
  66. static struct platform_device *sh_wdt_dev;
  67. static DEFINE_SPINLOCK(shwdt_lock);
  68. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
  69. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  70. static bool nowayout = WATCHDOG_NOWAYOUT;
  71. static unsigned long next_heartbeat;
  72. struct sh_wdt {
  73. void __iomem *base;
  74. struct device *dev;
  75. struct timer_list timer;
  76. unsigned long enabled;
  77. char expect_close;
  78. };
  79. static void sh_wdt_start(struct sh_wdt *wdt)
  80. {
  81. unsigned long flags;
  82. u8 csr;
  83. spin_lock_irqsave(&shwdt_lock, flags);
  84. next_heartbeat = jiffies + (heartbeat * HZ);
  85. mod_timer(&wdt->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. csr = sh_wdt_read_rstcsr();
  104. csr &= ~RSTCSR_RSTS;
  105. sh_wdt_write_rstcsr(csr);
  106. #endif
  107. spin_unlock_irqrestore(&shwdt_lock, flags);
  108. }
  109. static void sh_wdt_stop(struct sh_wdt *wdt)
  110. {
  111. unsigned long flags;
  112. u8 csr;
  113. spin_lock_irqsave(&shwdt_lock, flags);
  114. del_timer(&wdt->timer);
  115. csr = sh_wdt_read_csr();
  116. csr &= ~WTCSR_TME;
  117. sh_wdt_write_csr(csr);
  118. spin_unlock_irqrestore(&shwdt_lock, flags);
  119. }
  120. static inline void sh_wdt_keepalive(struct sh_wdt *wdt)
  121. {
  122. unsigned long flags;
  123. spin_lock_irqsave(&shwdt_lock, flags);
  124. next_heartbeat = jiffies + (heartbeat * HZ);
  125. spin_unlock_irqrestore(&shwdt_lock, flags);
  126. }
  127. static int sh_wdt_set_heartbeat(int t)
  128. {
  129. unsigned long flags;
  130. if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
  131. return -EINVAL;
  132. spin_lock_irqsave(&shwdt_lock, flags);
  133. heartbeat = t;
  134. spin_unlock_irqrestore(&shwdt_lock, flags);
  135. return 0;
  136. }
  137. static void sh_wdt_ping(unsigned long data)
  138. {
  139. struct sh_wdt *wdt = (struct sh_wdt *)data;
  140. unsigned long flags;
  141. spin_lock_irqsave(&shwdt_lock, flags);
  142. if (time_before(jiffies, next_heartbeat)) {
  143. u8 csr;
  144. csr = sh_wdt_read_csr();
  145. csr &= ~WTCSR_IOVF;
  146. sh_wdt_write_csr(csr);
  147. sh_wdt_write_cnt(0);
  148. mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
  149. } else
  150. dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
  151. "the watchdog\n");
  152. spin_unlock_irqrestore(&shwdt_lock, flags);
  153. }
  154. static int sh_wdt_open(struct inode *inode, struct file *file)
  155. {
  156. struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
  157. if (test_and_set_bit(0, &wdt->enabled))
  158. return -EBUSY;
  159. if (nowayout)
  160. __module_get(THIS_MODULE);
  161. file->private_data = wdt;
  162. sh_wdt_start(wdt);
  163. return nonseekable_open(inode, file);
  164. }
  165. static int sh_wdt_close(struct inode *inode, struct file *file)
  166. {
  167. struct sh_wdt *wdt = file->private_data;
  168. if (wdt->expect_close == 42) {
  169. sh_wdt_stop(wdt);
  170. } else {
  171. dev_crit(wdt->dev, "Unexpected close, not "
  172. "stopping watchdog!\n");
  173. sh_wdt_keepalive(wdt);
  174. }
  175. clear_bit(0, &wdt->enabled);
  176. wdt->expect_close = 0;
  177. return 0;
  178. }
  179. static ssize_t sh_wdt_write(struct file *file, const char *buf,
  180. size_t count, loff_t *ppos)
  181. {
  182. struct sh_wdt *wdt = file->private_data;
  183. if (count) {
  184. if (!nowayout) {
  185. size_t i;
  186. wdt->expect_close = 0;
  187. for (i = 0; i != count; i++) {
  188. char c;
  189. if (get_user(c, buf + i))
  190. return -EFAULT;
  191. if (c == 'V')
  192. wdt->expect_close = 42;
  193. }
  194. }
  195. sh_wdt_keepalive(wdt);
  196. }
  197. return count;
  198. }
  199. static long sh_wdt_ioctl(struct file *file, unsigned int cmd,
  200. unsigned long arg)
  201. {
  202. struct sh_wdt *wdt = file->private_data;
  203. int new_heartbeat;
  204. int options, retval = -EINVAL;
  205. switch (cmd) {
  206. case WDIOC_GETSUPPORT:
  207. return copy_to_user((struct watchdog_info *)arg,
  208. &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0;
  209. case WDIOC_GETSTATUS:
  210. case WDIOC_GETBOOTSTATUS:
  211. return put_user(0, (int *)arg);
  212. case WDIOC_SETOPTIONS:
  213. if (get_user(options, (int *)arg))
  214. return -EFAULT;
  215. if (options & WDIOS_DISABLECARD) {
  216. sh_wdt_stop(wdt);
  217. retval = 0;
  218. }
  219. if (options & WDIOS_ENABLECARD) {
  220. sh_wdt_start(wdt);
  221. retval = 0;
  222. }
  223. return retval;
  224. case WDIOC_KEEPALIVE:
  225. sh_wdt_keepalive(wdt);
  226. return 0;
  227. case WDIOC_SETTIMEOUT:
  228. if (get_user(new_heartbeat, (int *)arg))
  229. return -EFAULT;
  230. if (sh_wdt_set_heartbeat(new_heartbeat))
  231. return -EINVAL;
  232. sh_wdt_keepalive(wdt);
  233. /* Fall */
  234. case WDIOC_GETTIMEOUT:
  235. return put_user(heartbeat, (int *)arg);
  236. default:
  237. return -ENOTTY;
  238. }
  239. return 0;
  240. }
  241. static const struct file_operations sh_wdt_fops = {
  242. .owner = THIS_MODULE,
  243. .llseek = no_llseek,
  244. .write = sh_wdt_write,
  245. .unlocked_ioctl = sh_wdt_ioctl,
  246. .open = sh_wdt_open,
  247. .release = sh_wdt_close,
  248. };
  249. static const struct watchdog_info sh_wdt_info = {
  250. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  251. WDIOF_MAGICCLOSE,
  252. .firmware_version = 1,
  253. .identity = "SH WDT",
  254. };
  255. static struct miscdevice sh_wdt_miscdev = {
  256. .minor = WATCHDOG_MINOR,
  257. .name = "watchdog",
  258. .fops = &sh_wdt_fops,
  259. };
  260. static int __devinit sh_wdt_probe(struct platform_device *pdev)
  261. {
  262. struct sh_wdt *wdt;
  263. struct resource *res;
  264. int rc;
  265. /*
  266. * As this driver only covers the global watchdog case, reject
  267. * any attempts to register per-CPU watchdogs.
  268. */
  269. if (pdev->id != -1)
  270. return -EINVAL;
  271. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  272. if (unlikely(!res))
  273. return -EINVAL;
  274. if (!devm_request_mem_region(&pdev->dev, res->start,
  275. resource_size(res), DRV_NAME))
  276. return -EBUSY;
  277. wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
  278. if (unlikely(!wdt)) {
  279. rc = -ENOMEM;
  280. goto out_release;
  281. }
  282. wdt->dev = &pdev->dev;
  283. wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  284. if (unlikely(!wdt->base)) {
  285. rc = -ENXIO;
  286. goto out_err;
  287. }
  288. sh_wdt_miscdev.parent = wdt->dev;
  289. rc = misc_register(&sh_wdt_miscdev);
  290. if (unlikely(rc)) {
  291. dev_err(&pdev->dev,
  292. "Can't register miscdev on minor=%d (err=%d)\n",
  293. sh_wdt_miscdev.minor, rc);
  294. goto out_unmap;
  295. }
  296. init_timer(&wdt->timer);
  297. wdt->timer.function = sh_wdt_ping;
  298. wdt->timer.data = (unsigned long)wdt;
  299. wdt->timer.expires = next_ping_period(clock_division_ratio);
  300. platform_set_drvdata(pdev, wdt);
  301. sh_wdt_dev = pdev;
  302. dev_info(&pdev->dev, "initialized.\n");
  303. return 0;
  304. out_unmap:
  305. devm_iounmap(&pdev->dev, wdt->base);
  306. out_err:
  307. devm_kfree(&pdev->dev, wdt);
  308. out_release:
  309. devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
  310. return rc;
  311. }
  312. static int __devexit sh_wdt_remove(struct platform_device *pdev)
  313. {
  314. struct sh_wdt *wdt = platform_get_drvdata(pdev);
  315. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  316. platform_set_drvdata(pdev, NULL);
  317. misc_deregister(&sh_wdt_miscdev);
  318. sh_wdt_dev = NULL;
  319. devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
  320. devm_iounmap(&pdev->dev, wdt->base);
  321. devm_kfree(&pdev->dev, wdt);
  322. return 0;
  323. }
  324. static void sh_wdt_shutdown(struct platform_device *pdev)
  325. {
  326. struct sh_wdt *wdt = platform_get_drvdata(pdev);
  327. sh_wdt_stop(wdt);
  328. }
  329. static struct platform_driver sh_wdt_driver = {
  330. .driver = {
  331. .name = DRV_NAME,
  332. .owner = THIS_MODULE,
  333. },
  334. .probe = sh_wdt_probe,
  335. .remove = __devexit_p(sh_wdt_remove),
  336. .shutdown = sh_wdt_shutdown,
  337. };
  338. static int __init sh_wdt_init(void)
  339. {
  340. int rc;
  341. if (unlikely(clock_division_ratio < 0x5 ||
  342. clock_division_ratio > 0x7)) {
  343. clock_division_ratio = WTCSR_CKS_4096;
  344. pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
  345. clock_division_ratio);
  346. }
  347. rc = sh_wdt_set_heartbeat(heartbeat);
  348. if (unlikely(rc)) {
  349. heartbeat = WATCHDOG_HEARTBEAT;
  350. pr_info("heartbeat value must be 1<=x<=3600, using %d\n",
  351. heartbeat);
  352. }
  353. pr_info("configured with heartbeat=%d sec (nowayout=%d)\n",
  354. heartbeat, nowayout);
  355. return platform_driver_register(&sh_wdt_driver);
  356. }
  357. static void __exit sh_wdt_exit(void)
  358. {
  359. platform_driver_unregister(&sh_wdt_driver);
  360. }
  361. module_init(sh_wdt_init);
  362. module_exit(sh_wdt_exit);
  363. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  364. MODULE_DESCRIPTION("SuperH watchdog driver");
  365. MODULE_LICENSE("GPL");
  366. MODULE_ALIAS("platform:" DRV_NAME);
  367. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  368. module_param(clock_division_ratio, int, 0);
  369. MODULE_PARM_DESC(clock_division_ratio,
  370. "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
  371. "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
  372. module_param(heartbeat, int, 0);
  373. MODULE_PARM_DESC(heartbeat,
  374. "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
  375. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  376. module_param(nowayout, bool, 0);
  377. MODULE_PARM_DESC(nowayout,
  378. "Watchdog cannot be stopped once started (default="
  379. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");