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. if (!ping) {
  121. writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
  122. writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
  123. WDTCONTROL);
  124. }
  125. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  126. /* Flush posted writes. */
  127. readl_relaxed(wdt->base + WDTLOCK);
  128. spin_unlock(&wdt->lock);
  129. return 0;
  130. }
  131. static int wdt_ping(struct watchdog_device *wdd)
  132. {
  133. return wdt_config(wdd, true);
  134. }
  135. /* enables watchdog timers reset */
  136. static int wdt_enable(struct watchdog_device *wdd)
  137. {
  138. return wdt_config(wdd, false);
  139. }
  140. /* disables watchdog timers reset */
  141. static int wdt_disable(struct watchdog_device *wdd)
  142. {
  143. struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
  144. spin_lock(&wdt->lock);
  145. writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
  146. writel_relaxed(0, wdt->base + WDTCONTROL);
  147. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  148. /* Flush posted writes. */
  149. readl_relaxed(wdt->base + WDTLOCK);
  150. spin_unlock(&wdt->lock);
  151. clk_disable_unprepare(wdt->clk);
  152. return 0;
  153. }
  154. static const struct watchdog_info wdt_info = {
  155. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  156. .identity = MODULE_NAME,
  157. };
  158. static const struct watchdog_ops wdt_ops = {
  159. .owner = THIS_MODULE,
  160. .start = wdt_enable,
  161. .stop = wdt_disable,
  162. .ping = wdt_ping,
  163. .set_timeout = wdt_setload,
  164. .get_timeleft = wdt_timeleft,
  165. };
  166. static int
  167. sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
  168. {
  169. struct sp805_wdt *wdt;
  170. int ret = 0;
  171. wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
  172. if (!wdt) {
  173. ret = -ENOMEM;
  174. goto err;
  175. }
  176. wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
  177. if (IS_ERR(wdt->base))
  178. return PTR_ERR(wdt->base);
  179. wdt->clk = devm_clk_get(&adev->dev, NULL);
  180. if (IS_ERR(wdt->clk)) {
  181. dev_warn(&adev->dev, "Clock not found\n");
  182. ret = PTR_ERR(wdt->clk);
  183. goto err;
  184. }
  185. wdt->adev = adev;
  186. wdt->wdd.info = &wdt_info;
  187. wdt->wdd.ops = &wdt_ops;
  188. wdt->wdd.parent = &adev->dev;
  189. spin_lock_init(&wdt->lock);
  190. watchdog_set_nowayout(&wdt->wdd, nowayout);
  191. watchdog_set_drvdata(&wdt->wdd, wdt);
  192. wdt_setload(&wdt->wdd, DEFAULT_TIMEOUT);
  193. ret = watchdog_register_device(&wdt->wdd);
  194. if (ret) {
  195. dev_err(&adev->dev, "watchdog_register_device() failed: %d\n",
  196. ret);
  197. goto err;
  198. }
  199. amba_set_drvdata(adev, wdt);
  200. dev_info(&adev->dev, "registration successful\n");
  201. return 0;
  202. err:
  203. dev_err(&adev->dev, "Probe Failed!!!\n");
  204. return ret;
  205. }
  206. static int sp805_wdt_remove(struct amba_device *adev)
  207. {
  208. struct sp805_wdt *wdt = amba_get_drvdata(adev);
  209. watchdog_unregister_device(&wdt->wdd);
  210. watchdog_set_drvdata(&wdt->wdd, NULL);
  211. return 0;
  212. }
  213. static int __maybe_unused sp805_wdt_suspend(struct device *dev)
  214. {
  215. struct sp805_wdt *wdt = dev_get_drvdata(dev);
  216. if (watchdog_active(&wdt->wdd))
  217. return wdt_disable(&wdt->wdd);
  218. return 0;
  219. }
  220. static int __maybe_unused sp805_wdt_resume(struct device *dev)
  221. {
  222. struct sp805_wdt *wdt = dev_get_drvdata(dev);
  223. if (watchdog_active(&wdt->wdd))
  224. return wdt_enable(&wdt->wdd);
  225. return 0;
  226. }
  227. static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
  228. sp805_wdt_resume);
  229. static struct amba_id sp805_wdt_ids[] = {
  230. {
  231. .id = 0x00141805,
  232. .mask = 0x00ffffff,
  233. },
  234. { 0, 0 },
  235. };
  236. MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
  237. static struct amba_driver sp805_wdt_driver = {
  238. .drv = {
  239. .name = MODULE_NAME,
  240. .pm = &sp805_wdt_dev_pm_ops,
  241. },
  242. .id_table = sp805_wdt_ids,
  243. .probe = sp805_wdt_probe,
  244. .remove = sp805_wdt_remove,
  245. };
  246. module_amba_driver(sp805_wdt_driver);
  247. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  248. MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
  249. MODULE_LICENSE("GPL");