shwdt.c 9.1 KB

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