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