of_xilinx_wdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
  4. *
  5. * (C) Copyright 2013 - 2014 Xilinx, Inc.
  6. * (C) Copyright 2011 (Alejandro Cabrera <[email protected]>)
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/ioport.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_address.h>
  20. /* Register offsets for the Wdt device */
  21. #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
  22. #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
  23. #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
  24. /* Control/Status Register Masks */
  25. #define XWT_CSR0_WRS_MASK BIT(3) /* Reset status */
  26. #define XWT_CSR0_WDS_MASK BIT(2) /* Timer state */
  27. #define XWT_CSR0_EWDT1_MASK BIT(1) /* Enable bit 1 */
  28. /* Control/Status Register 0/1 bits */
  29. #define XWT_CSRX_EWDT2_MASK BIT(0) /* Enable bit 2 */
  30. /* SelfTest constants */
  31. #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
  32. #define XWT_TIMER_FAILED 0xFFFFFFFF
  33. #define WATCHDOG_NAME "Xilinx Watchdog"
  34. struct xwdt_device {
  35. void __iomem *base;
  36. u32 wdt_interval;
  37. spinlock_t spinlock; /* spinlock for register handling */
  38. struct watchdog_device xilinx_wdt_wdd;
  39. struct clk *clk;
  40. };
  41. static int xilinx_wdt_start(struct watchdog_device *wdd)
  42. {
  43. int ret;
  44. u32 control_status_reg;
  45. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  46. ret = clk_enable(xdev->clk);
  47. if (ret) {
  48. dev_err(wdd->parent, "Failed to enable clock\n");
  49. return ret;
  50. }
  51. spin_lock(&xdev->spinlock);
  52. /* Clean previous status and enable the watchdog timer */
  53. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  54. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  55. iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
  56. xdev->base + XWT_TWCSR0_OFFSET);
  57. iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
  58. spin_unlock(&xdev->spinlock);
  59. dev_dbg(wdd->parent, "Watchdog Started!\n");
  60. return 0;
  61. }
  62. static int xilinx_wdt_stop(struct watchdog_device *wdd)
  63. {
  64. u32 control_status_reg;
  65. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  66. spin_lock(&xdev->spinlock);
  67. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  68. iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
  69. xdev->base + XWT_TWCSR0_OFFSET);
  70. iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
  71. spin_unlock(&xdev->spinlock);
  72. clk_disable(xdev->clk);
  73. dev_dbg(wdd->parent, "Watchdog Stopped!\n");
  74. return 0;
  75. }
  76. static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
  77. {
  78. u32 control_status_reg;
  79. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  80. spin_lock(&xdev->spinlock);
  81. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  82. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  83. iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
  84. spin_unlock(&xdev->spinlock);
  85. return 0;
  86. }
  87. static const struct watchdog_info xilinx_wdt_ident = {
  88. .options = WDIOF_MAGICCLOSE |
  89. WDIOF_KEEPALIVEPING,
  90. .firmware_version = 1,
  91. .identity = WATCHDOG_NAME,
  92. };
  93. static const struct watchdog_ops xilinx_wdt_ops = {
  94. .owner = THIS_MODULE,
  95. .start = xilinx_wdt_start,
  96. .stop = xilinx_wdt_stop,
  97. .ping = xilinx_wdt_keepalive,
  98. };
  99. static u32 xwdt_selftest(struct xwdt_device *xdev)
  100. {
  101. int i;
  102. u32 timer_value1;
  103. u32 timer_value2;
  104. spin_lock(&xdev->spinlock);
  105. timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
  106. timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
  107. for (i = 0;
  108. ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
  109. (timer_value2 == timer_value1)); i++) {
  110. timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
  111. }
  112. spin_unlock(&xdev->spinlock);
  113. if (timer_value2 != timer_value1)
  114. return ~XWT_TIMER_FAILED;
  115. else
  116. return XWT_TIMER_FAILED;
  117. }
  118. static void xwdt_clk_disable_unprepare(void *data)
  119. {
  120. clk_disable_unprepare(data);
  121. }
  122. static int xwdt_probe(struct platform_device *pdev)
  123. {
  124. struct device *dev = &pdev->dev;
  125. int rc;
  126. u32 pfreq = 0, enable_once = 0;
  127. struct xwdt_device *xdev;
  128. struct watchdog_device *xilinx_wdt_wdd;
  129. xdev = devm_kzalloc(dev, sizeof(*xdev), GFP_KERNEL);
  130. if (!xdev)
  131. return -ENOMEM;
  132. xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
  133. xilinx_wdt_wdd->info = &xilinx_wdt_ident;
  134. xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
  135. xilinx_wdt_wdd->parent = dev;
  136. xdev->base = devm_platform_ioremap_resource(pdev, 0);
  137. if (IS_ERR(xdev->base))
  138. return PTR_ERR(xdev->base);
  139. rc = of_property_read_u32(dev->of_node, "xlnx,wdt-interval",
  140. &xdev->wdt_interval);
  141. if (rc)
  142. dev_warn(dev, "Parameter \"xlnx,wdt-interval\" not found\n");
  143. rc = of_property_read_u32(dev->of_node, "xlnx,wdt-enable-once",
  144. &enable_once);
  145. if (rc)
  146. dev_warn(dev,
  147. "Parameter \"xlnx,wdt-enable-once\" not found\n");
  148. watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
  149. xdev->clk = devm_clk_get(dev, NULL);
  150. if (IS_ERR(xdev->clk)) {
  151. if (PTR_ERR(xdev->clk) != -ENOENT)
  152. return PTR_ERR(xdev->clk);
  153. /*
  154. * Clock framework support is optional, continue on
  155. * anyways if we don't find a matching clock.
  156. */
  157. xdev->clk = NULL;
  158. rc = of_property_read_u32(dev->of_node, "clock-frequency",
  159. &pfreq);
  160. if (rc)
  161. dev_warn(dev,
  162. "The watchdog clock freq cannot be obtained\n");
  163. } else {
  164. pfreq = clk_get_rate(xdev->clk);
  165. rc = clk_prepare_enable(xdev->clk);
  166. if (rc) {
  167. dev_err(dev, "unable to enable clock\n");
  168. return rc;
  169. }
  170. rc = devm_add_action_or_reset(dev, xwdt_clk_disable_unprepare,
  171. xdev->clk);
  172. if (rc)
  173. return rc;
  174. }
  175. /*
  176. * Twice of the 2^wdt_interval / freq because the first wdt overflow is
  177. * ignored (interrupt), reset is only generated at second wdt overflow
  178. */
  179. if (pfreq && xdev->wdt_interval)
  180. xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
  181. pfreq);
  182. spin_lock_init(&xdev->spinlock);
  183. watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
  184. rc = xwdt_selftest(xdev);
  185. if (rc == XWT_TIMER_FAILED) {
  186. dev_err(dev, "SelfTest routine error\n");
  187. return rc;
  188. }
  189. rc = devm_watchdog_register_device(dev, xilinx_wdt_wdd);
  190. if (rc)
  191. return rc;
  192. clk_disable(xdev->clk);
  193. dev_info(dev, "Xilinx Watchdog Timer with timeout %ds\n",
  194. xilinx_wdt_wdd->timeout);
  195. platform_set_drvdata(pdev, xdev);
  196. return 0;
  197. }
  198. /**
  199. * xwdt_suspend - Suspend the device.
  200. *
  201. * @dev: handle to the device structure.
  202. * Return: 0 always.
  203. */
  204. static int __maybe_unused xwdt_suspend(struct device *dev)
  205. {
  206. struct xwdt_device *xdev = dev_get_drvdata(dev);
  207. if (watchdog_active(&xdev->xilinx_wdt_wdd))
  208. xilinx_wdt_stop(&xdev->xilinx_wdt_wdd);
  209. return 0;
  210. }
  211. /**
  212. * xwdt_resume - Resume the device.
  213. *
  214. * @dev: handle to the device structure.
  215. * Return: 0 on success, errno otherwise.
  216. */
  217. static int __maybe_unused xwdt_resume(struct device *dev)
  218. {
  219. struct xwdt_device *xdev = dev_get_drvdata(dev);
  220. int ret = 0;
  221. if (watchdog_active(&xdev->xilinx_wdt_wdd))
  222. ret = xilinx_wdt_start(&xdev->xilinx_wdt_wdd);
  223. return ret;
  224. }
  225. static SIMPLE_DEV_PM_OPS(xwdt_pm_ops, xwdt_suspend, xwdt_resume);
  226. /* Match table for of_platform binding */
  227. static const struct of_device_id xwdt_of_match[] = {
  228. { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
  229. { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
  230. {},
  231. };
  232. MODULE_DEVICE_TABLE(of, xwdt_of_match);
  233. static struct platform_driver xwdt_driver = {
  234. .probe = xwdt_probe,
  235. .driver = {
  236. .name = WATCHDOG_NAME,
  237. .of_match_table = xwdt_of_match,
  238. .pm = &xwdt_pm_ops,
  239. },
  240. };
  241. module_platform_driver(xwdt_driver);
  242. MODULE_AUTHOR("Alejandro Cabrera <[email protected]>");
  243. MODULE_DESCRIPTION("Xilinx Watchdog driver");
  244. MODULE_LICENSE("GPL");