of_xilinx_wdt.c 7.3 KB

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