shwdt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/fs.h>
  29. #include <linux/mm.h>
  30. #include <linux/slab.h>
  31. #include <linux/io.h>
  32. #include <asm/watchdog.h>
  33. #define DRV_NAME "sh-wdt"
  34. /*
  35. * Default clock division ratio is 5.25 msecs. For an additional table of
  36. * values, consult the asm-sh/watchdog.h. Overload this at module load
  37. * time.
  38. *
  39. * In order for this to work reliably we need to have HZ set to 1000 or
  40. * something quite higher than 100 (or we need a proper high-res timer
  41. * implementation that will deal with this properly), otherwise the 10ms
  42. * resolution of a jiffy is enough to trigger the overflow. For things like
  43. * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
  44. * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
  45. * necssary.
  46. *
  47. * As a result of this timing problem, the only modes that are particularly
  48. * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
  49. * overflow periods respectively.
  50. *
  51. * Also, since we can't really expect userspace to be responsive enough
  52. * before the overflow happens, we maintain two separate timers .. One in
  53. * the kernel for clearing out WOVF every 2ms or so (again, this depends on
  54. * HZ == 1000), and another for monitoring userspace writes to the WDT device.
  55. *
  56. * As such, we currently use a configurable heartbeat interval which defaults
  57. * to 30s. In this case, the userspace daemon is only responsible for periodic
  58. * writes to the device before the next heartbeat is scheduled. If the daemon
  59. * misses its deadline, the kernel timer will allow the WDT to overflow.
  60. */
  61. static int clock_division_ratio = WTCSR_CKS_4096;
  62. #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
  63. static DEFINE_SPINLOCK(shwdt_lock);
  64. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
  65. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  66. static bool nowayout = WATCHDOG_NOWAYOUT;
  67. static unsigned long next_heartbeat;
  68. struct sh_wdt {
  69. void __iomem *base;
  70. struct device *dev;
  71. struct timer_list timer;
  72. unsigned long enabled;
  73. char expect_close;
  74. };
  75. static int sh_wdt_start(struct watchdog_device *wdt_dev)
  76. {
  77. struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  78. unsigned long flags;
  79. u8 csr;
  80. spin_lock_irqsave(&shwdt_lock, flags);
  81. next_heartbeat = jiffies + (heartbeat * HZ);
  82. mod_timer(&wdt->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. csr = sh_wdt_read_rstcsr();
  101. csr &= ~RSTCSR_RSTS;
  102. sh_wdt_write_rstcsr(csr);
  103. #endif
  104. spin_unlock_irqrestore(&shwdt_lock, flags);
  105. return 0;
  106. }
  107. static int sh_wdt_stop(struct watchdog_device *wdt_dev)
  108. {
  109. struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  110. unsigned long flags;
  111. u8 csr;
  112. spin_lock_irqsave(&shwdt_lock, flags);
  113. del_timer(&wdt->timer);
  114. csr = sh_wdt_read_csr();
  115. csr &= ~WTCSR_TME;
  116. sh_wdt_write_csr(csr);
  117. spin_unlock_irqrestore(&shwdt_lock, flags);
  118. return 0;
  119. }
  120. static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
  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. return 0;
  127. }
  128. static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
  129. {
  130. unsigned long flags;
  131. if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
  132. return -EINVAL;
  133. spin_lock_irqsave(&shwdt_lock, flags);
  134. heartbeat = t;
  135. wdt_dev->timeout = t;
  136. spin_unlock_irqrestore(&shwdt_lock, flags);
  137. return 0;
  138. }
  139. static void sh_wdt_ping(unsigned long data)
  140. {
  141. struct sh_wdt *wdt = (struct sh_wdt *)data;
  142. unsigned long flags;
  143. spin_lock_irqsave(&shwdt_lock, flags);
  144. if (time_before(jiffies, next_heartbeat)) {
  145. u8 csr;
  146. csr = sh_wdt_read_csr();
  147. csr &= ~WTCSR_IOVF;
  148. sh_wdt_write_csr(csr);
  149. sh_wdt_write_cnt(0);
  150. mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
  151. } else
  152. dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
  153. "the watchdog\n");
  154. spin_unlock_irqrestore(&shwdt_lock, flags);
  155. }
  156. static const struct watchdog_info sh_wdt_info = {
  157. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  158. WDIOF_MAGICCLOSE,
  159. .firmware_version = 1,
  160. .identity = "SH WDT",
  161. };
  162. static const struct watchdog_ops sh_wdt_ops = {
  163. .owner = THIS_MODULE,
  164. .start = sh_wdt_start,
  165. .stop = sh_wdt_stop,
  166. .ping = sh_wdt_keepalive,
  167. .set_timeout = sh_wdt_set_heartbeat,
  168. };
  169. static struct watchdog_device sh_wdt_dev = {
  170. .info = &sh_wdt_info,
  171. .ops = &sh_wdt_ops,
  172. };
  173. static int __devinit sh_wdt_probe(struct platform_device *pdev)
  174. {
  175. struct sh_wdt *wdt;
  176. struct resource *res;
  177. int rc;
  178. /*
  179. * As this driver only covers the global watchdog case, reject
  180. * any attempts to register per-CPU watchdogs.
  181. */
  182. if (pdev->id != -1)
  183. return -EINVAL;
  184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. if (unlikely(!res))
  186. return -EINVAL;
  187. if (!devm_request_mem_region(&pdev->dev, res->start,
  188. resource_size(res), DRV_NAME))
  189. return -EBUSY;
  190. wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
  191. if (unlikely(!wdt)) {
  192. rc = -ENOMEM;
  193. goto out_release;
  194. }
  195. wdt->dev = &pdev->dev;
  196. wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  197. if (unlikely(!wdt->base)) {
  198. rc = -ENXIO;
  199. goto out_err;
  200. }
  201. rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
  202. if (unlikely(rc)) {
  203. /* Default timeout if invalid */
  204. sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
  205. dev_warn(&pdev->dev,
  206. "heartbeat value must be 1<=x<=3600, using %d\n",
  207. sh_wdt_dev.timeout);
  208. }
  209. dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
  210. sh_wdt_dev.timeout, nowayout);
  211. watchdog_set_nowayout(&sh_wdt_dev, nowayout);
  212. watchdog_set_drvdata(&sh_wdt_dev, wdt);
  213. rc = watchdog_register_device(&sh_wdt_dev);
  214. if (unlikely(rc)) {
  215. dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
  216. goto out_unmap;
  217. }
  218. init_timer(&wdt->timer);
  219. wdt->timer.function = sh_wdt_ping;
  220. wdt->timer.data = (unsigned long)wdt;
  221. wdt->timer.expires = next_ping_period(clock_division_ratio);
  222. platform_set_drvdata(pdev, wdt);
  223. dev_info(&pdev->dev, "initialized.\n");
  224. return 0;
  225. out_unmap:
  226. devm_iounmap(&pdev->dev, wdt->base);
  227. out_err:
  228. devm_kfree(&pdev->dev, wdt);
  229. out_release:
  230. devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
  231. return rc;
  232. }
  233. static int __devexit sh_wdt_remove(struct platform_device *pdev)
  234. {
  235. struct sh_wdt *wdt = platform_get_drvdata(pdev);
  236. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. platform_set_drvdata(pdev, NULL);
  238. watchdog_unregister_device(&sh_wdt_dev);
  239. devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
  240. devm_iounmap(&pdev->dev, wdt->base);
  241. devm_kfree(&pdev->dev, wdt);
  242. return 0;
  243. }
  244. static void sh_wdt_shutdown(struct platform_device *pdev)
  245. {
  246. sh_wdt_stop(&sh_wdt_dev);
  247. }
  248. static struct platform_driver sh_wdt_driver = {
  249. .driver = {
  250. .name = DRV_NAME,
  251. .owner = THIS_MODULE,
  252. },
  253. .probe = sh_wdt_probe,
  254. .remove = __devexit_p(sh_wdt_remove),
  255. .shutdown = sh_wdt_shutdown,
  256. };
  257. static int __init sh_wdt_init(void)
  258. {
  259. if (unlikely(clock_division_ratio < 0x5 ||
  260. clock_division_ratio > 0x7)) {
  261. clock_division_ratio = WTCSR_CKS_4096;
  262. pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
  263. clock_division_ratio);
  264. }
  265. return platform_driver_register(&sh_wdt_driver);
  266. }
  267. static void __exit sh_wdt_exit(void)
  268. {
  269. platform_driver_unregister(&sh_wdt_driver);
  270. }
  271. module_init(sh_wdt_init);
  272. module_exit(sh_wdt_exit);
  273. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  274. MODULE_DESCRIPTION("SuperH watchdog driver");
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS("platform:" DRV_NAME);
  277. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  278. module_param(clock_division_ratio, int, 0);
  279. MODULE_PARM_DESC(clock_division_ratio,
  280. "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
  281. "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
  282. module_param(heartbeat, int, 0);
  283. MODULE_PARM_DESC(heartbeat,
  284. "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
  285. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  286. module_param(nowayout, bool, 0);
  287. MODULE_PARM_DESC(nowayout,
  288. "Watchdog cannot be stopped once started (default="
  289. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");