of_xilinx_wdt.c 7.7 KB

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