sp805_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * drivers/char/watchdog/sp805-wdt.c
  3. *
  4. * Watchdog driver for ARM SP805 watchdog module
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Viresh Kumar <vireshk@kernel.org>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2 or later. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/resource.h>
  15. #include <linux/amba/bus.h>
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/kernel.h>
  21. #include <linux/math64.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/pm.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/types.h>
  28. #include <linux/watchdog.h>
  29. /* default timeout in seconds */
  30. #define DEFAULT_TIMEOUT 60
  31. #define MODULE_NAME "sp805-wdt"
  32. /* watchdog register offsets and masks */
  33. #define WDTLOAD 0x000
  34. #define LOAD_MIN 0x00000001
  35. #define LOAD_MAX 0xFFFFFFFF
  36. #define WDTVALUE 0x004
  37. #define WDTCONTROL 0x008
  38. /* control register masks */
  39. #define INT_ENABLE (1 << 0)
  40. #define RESET_ENABLE (1 << 1)
  41. #define WDTINTCLR 0x00C
  42. #define WDTRIS 0x010
  43. #define WDTMIS 0x014
  44. #define INT_MASK (1 << 0)
  45. #define WDTLOCK 0xC00
  46. #define UNLOCK 0x1ACCE551
  47. #define LOCK 0x00000001
  48. /**
  49. * struct sp805_wdt: sp805 wdt device structure
  50. * @wdd: instance of struct watchdog_device
  51. * @lock: spin lock protecting dev structure and io access
  52. * @base: base address of wdt
  53. * @clk: clock structure of wdt
  54. * @adev: amba device structure of wdt
  55. * @status: current status of wdt
  56. * @load_val: load value to be set for current timeout
  57. */
  58. struct sp805_wdt {
  59. struct watchdog_device wdd;
  60. spinlock_t lock;
  61. void __iomem *base;
  62. struct clk *clk;
  63. struct amba_device *adev;
  64. unsigned int load_val;
  65. };
  66. static bool nowayout = WATCHDOG_NOWAYOUT;
  67. module_param(nowayout, bool, 0);
  68. MODULE_PARM_DESC(nowayout,
  69. "Set to 1 to keep watchdog running after device release");
  70. /* This routine finds load value that will reset system in required timout */
  71. static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout)
  72. {
  73. struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
  74. u64 load, rate;
  75. rate = clk_get_rate(wdt->clk);
  76. /*
  77. * sp805 runs counter with given value twice, after the end of first
  78. * counter it gives an interrupt and then starts counter again. If
  79. * interrupt already occurred then it resets the system. This is why
  80. * load is half of what should be required.
  81. */
  82. load = div_u64(rate, 2) * timeout - 1;
  83. load = (load > LOAD_MAX) ? LOAD_MAX : load;
  84. load = (load < LOAD_MIN) ? LOAD_MIN : load;
  85. spin_lock(&wdt->lock);
  86. wdt->load_val = load;
  87. /* roundup timeout to closest positive integer value */
  88. wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
  89. spin_unlock(&wdt->lock);
  90. return 0;
  91. }
  92. /* returns number of seconds left for reset to occur */
  93. static unsigned int wdt_timeleft(struct watchdog_device *wdd)
  94. {
  95. struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
  96. u64 load, rate;
  97. rate = clk_get_rate(wdt->clk);
  98. spin_lock(&wdt->lock);
  99. load = readl_relaxed(wdt->base + WDTVALUE);
  100. /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
  101. if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK))
  102. load += wdt->load_val + 1;
  103. spin_unlock(&wdt->lock);
  104. return div_u64(load, rate);
  105. }
  106. static int wdt_config(struct watchdog_device *wdd, bool ping)
  107. {
  108. struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
  109. int ret;
  110. if (!ping) {
  111. ret = clk_prepare_enable(wdt->clk);
  112. if (ret) {
  113. dev_err(&wdt->adev->dev, "clock enable fail");
  114. return ret;
  115. }
  116. }
  117. spin_lock(&wdt->lock);
  118. writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
  119. writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
  120. writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
  121. if (!ping)
  122. writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
  123. WDTCONTROL);
  124. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  125. /* Flush posted writes. */
  126. readl_relaxed(wdt->base + WDTLOCK);
  127. spin_unlock(&wdt->lock);
  128. return 0;
  129. }
  130. static int wdt_ping(struct watchdog_device *wdd)
  131. {
  132. return wdt_config(wdd, true);
  133. }
  134. /* enables watchdog timers reset */
  135. static int wdt_enable(struct watchdog_device *wdd)
  136. {
  137. return wdt_config(wdd, false);
  138. }
  139. /* disables watchdog timers reset */
  140. static int wdt_disable(struct watchdog_device *wdd)
  141. {
  142. struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
  143. spin_lock(&wdt->lock);
  144. writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
  145. writel_relaxed(0, wdt->base + WDTCONTROL);
  146. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  147. /* Flush posted writes. */
  148. readl_relaxed(wdt->base + WDTLOCK);
  149. spin_unlock(&wdt->lock);
  150. clk_disable_unprepare(wdt->clk);
  151. return 0;
  152. }
  153. static const struct watchdog_info wdt_info = {
  154. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  155. .identity = MODULE_NAME,
  156. };
  157. static const struct watchdog_ops wdt_ops = {
  158. .owner = THIS_MODULE,
  159. .start = wdt_enable,
  160. .stop = wdt_disable,
  161. .ping = wdt_ping,
  162. .set_timeout = wdt_setload,
  163. .get_timeleft = wdt_timeleft,
  164. };
  165. static int
  166. sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
  167. {
  168. struct sp805_wdt *wdt;
  169. int ret = 0;
  170. wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
  171. if (!wdt) {
  172. ret = -ENOMEM;
  173. goto err;
  174. }
  175. wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
  176. if (IS_ERR(wdt->base))
  177. return PTR_ERR(wdt->base);
  178. wdt->clk = devm_clk_get(&adev->dev, NULL);
  179. if (IS_ERR(wdt->clk)) {
  180. dev_warn(&adev->dev, "Clock not found\n");
  181. ret = PTR_ERR(wdt->clk);
  182. goto err;
  183. }
  184. wdt->adev = adev;
  185. wdt->wdd.info = &wdt_info;
  186. wdt->wdd.ops = &wdt_ops;
  187. wdt->wdd.parent = &adev->dev;
  188. spin_lock_init(&wdt->lock);
  189. watchdog_set_nowayout(&wdt->wdd, nowayout);
  190. watchdog_set_drvdata(&wdt->wdd, wdt);
  191. wdt_setload(&wdt->wdd, DEFAULT_TIMEOUT);
  192. ret = watchdog_register_device(&wdt->wdd);
  193. if (ret) {
  194. dev_err(&adev->dev, "watchdog_register_device() failed: %d\n",
  195. ret);
  196. goto err;
  197. }
  198. amba_set_drvdata(adev, wdt);
  199. dev_info(&adev->dev, "registration successful\n");
  200. return 0;
  201. err:
  202. dev_err(&adev->dev, "Probe Failed!!!\n");
  203. return ret;
  204. }
  205. static int sp805_wdt_remove(struct amba_device *adev)
  206. {
  207. struct sp805_wdt *wdt = amba_get_drvdata(adev);
  208. watchdog_unregister_device(&wdt->wdd);
  209. watchdog_set_drvdata(&wdt->wdd, NULL);
  210. return 0;
  211. }
  212. static int __maybe_unused sp805_wdt_suspend(struct device *dev)
  213. {
  214. struct sp805_wdt *wdt = dev_get_drvdata(dev);
  215. if (watchdog_active(&wdt->wdd))
  216. return wdt_disable(&wdt->wdd);
  217. return 0;
  218. }
  219. static int __maybe_unused sp805_wdt_resume(struct device *dev)
  220. {
  221. struct sp805_wdt *wdt = dev_get_drvdata(dev);
  222. if (watchdog_active(&wdt->wdd))
  223. return wdt_enable(&wdt->wdd);
  224. return 0;
  225. }
  226. static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
  227. sp805_wdt_resume);
  228. static struct amba_id sp805_wdt_ids[] = {
  229. {
  230. .id = 0x00141805,
  231. .mask = 0x00ffffff,
  232. },
  233. { 0, 0 },
  234. };
  235. MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
  236. static struct amba_driver sp805_wdt_driver = {
  237. .drv = {
  238. .name = MODULE_NAME,
  239. .pm = &sp805_wdt_dev_pm_ops,
  240. },
  241. .id_table = sp805_wdt_ids,
  242. .probe = sp805_wdt_probe,
  243. .remove = sp805_wdt_remove,
  244. };
  245. module_amba_driver(sp805_wdt_driver);
  246. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  247. MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
  248. MODULE_LICENSE("GPL");