of_xilinx_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
  3. *
  4. * (C) Copyright 2013 - 2014 Xilinx, Inc.
  5. * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/ioport.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_address.h>
  23. /* Register offsets for the Wdt device */
  24. #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
  25. #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
  26. #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
  27. /* Control/Status Register Masks */
  28. #define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
  29. #define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
  30. #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
  31. /* Control/Status Register 0/1 bits */
  32. #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
  33. /* SelfTest constants */
  34. #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
  35. #define XWT_TIMER_FAILED 0xFFFFFFFF
  36. #define WATCHDOG_NAME "Xilinx Watchdog"
  37. struct xwdt_device {
  38. void __iomem *base;
  39. u32 wdt_interval;
  40. spinlock_t spinlock;
  41. struct watchdog_device xilinx_wdt_wdd;
  42. struct clk *clk;
  43. };
  44. static int xilinx_wdt_start(struct watchdog_device *wdd)
  45. {
  46. u32 control_status_reg;
  47. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  48. spin_lock(&xdev->spinlock);
  49. /* Clean previous status and enable the watchdog timer */
  50. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  51. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  52. iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
  53. xdev->base + XWT_TWCSR0_OFFSET);
  54. iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
  55. spin_unlock(&xdev->spinlock);
  56. return 0;
  57. }
  58. static int xilinx_wdt_stop(struct watchdog_device *wdd)
  59. {
  60. u32 control_status_reg;
  61. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  62. spin_lock(&xdev->spinlock);
  63. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  64. iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
  65. xdev->base + XWT_TWCSR0_OFFSET);
  66. iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
  67. spin_unlock(&xdev->spinlock);
  68. pr_info("Stopped!\n");
  69. return 0;
  70. }
  71. static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
  72. {
  73. u32 control_status_reg;
  74. struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
  75. spin_lock(&xdev->spinlock);
  76. control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
  77. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  78. iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
  79. spin_unlock(&xdev->spinlock);
  80. return 0;
  81. }
  82. static const struct watchdog_info xilinx_wdt_ident = {
  83. .options = WDIOF_MAGICCLOSE |
  84. WDIOF_KEEPALIVEPING,
  85. .firmware_version = 1,
  86. .identity = WATCHDOG_NAME,
  87. };
  88. static const struct watchdog_ops xilinx_wdt_ops = {
  89. .owner = THIS_MODULE,
  90. .start = xilinx_wdt_start,
  91. .stop = xilinx_wdt_stop,
  92. .ping = xilinx_wdt_keepalive,
  93. };
  94. static u32 xwdt_selftest(struct xwdt_device *xdev)
  95. {
  96. int i;
  97. u32 timer_value1;
  98. u32 timer_value2;
  99. spin_lock(&xdev->spinlock);
  100. timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
  101. timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
  102. for (i = 0;
  103. ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
  104. (timer_value2 == timer_value1)); i++) {
  105. timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
  106. }
  107. spin_unlock(&xdev->spinlock);
  108. if (timer_value2 != timer_value1)
  109. return ~XWT_TIMER_FAILED;
  110. else
  111. return XWT_TIMER_FAILED;
  112. }
  113. static int xwdt_probe(struct platform_device *pdev)
  114. {
  115. int rc;
  116. u32 pfreq = 0, enable_once = 0;
  117. struct resource *res;
  118. struct xwdt_device *xdev;
  119. struct watchdog_device *xilinx_wdt_wdd;
  120. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  121. if (!xdev)
  122. return -ENOMEM;
  123. xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
  124. xilinx_wdt_wdd->info = &xilinx_wdt_ident;
  125. xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
  126. xilinx_wdt_wdd->parent = &pdev->dev;
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. xdev->base = devm_ioremap_resource(&pdev->dev, res);
  129. if (IS_ERR(xdev->base))
  130. return PTR_ERR(xdev->base);
  131. rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &pfreq);
  132. if (rc)
  133. dev_warn(&pdev->dev,
  134. "The watchdog clock frequency cannot be obtained\n");
  135. rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
  136. &xdev->wdt_interval);
  137. if (rc)
  138. dev_warn(&pdev->dev,
  139. "Parameter \"xlnx,wdt-interval\" not found\n");
  140. rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
  141. &enable_once);
  142. if (rc)
  143. dev_warn(&pdev->dev,
  144. "Parameter \"xlnx,wdt-enable-once\" not found\n");
  145. watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
  146. /*
  147. * Twice of the 2^wdt_interval / freq because the first wdt overflow is
  148. * ignored (interrupt), reset is only generated at second wdt overflow
  149. */
  150. if (pfreq && xdev->wdt_interval)
  151. xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
  152. pfreq);
  153. spin_lock_init(&xdev->spinlock);
  154. watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
  155. xdev->clk = devm_clk_get(&pdev->dev, NULL);
  156. if (IS_ERR(xdev->clk)) {
  157. if (PTR_ERR(xdev->clk) == -ENOENT)
  158. xdev->clk = NULL;
  159. else
  160. return PTR_ERR(xdev->clk);
  161. }
  162. rc = clk_prepare_enable(xdev->clk);
  163. if (rc) {
  164. dev_err(&pdev->dev, "unable to enable clock\n");
  165. return rc;
  166. }
  167. rc = xwdt_selftest(xdev);
  168. if (rc == XWT_TIMER_FAILED) {
  169. dev_err(&pdev->dev, "SelfTest routine error\n");
  170. goto err_clk_disable;
  171. }
  172. rc = watchdog_register_device(xilinx_wdt_wdd);
  173. if (rc) {
  174. dev_err(&pdev->dev, "Cannot register watchdog (err=%d)\n", rc);
  175. goto err_clk_disable;
  176. }
  177. dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
  178. xdev->base, xilinx_wdt_wdd->timeout);
  179. platform_set_drvdata(pdev, xdev);
  180. return 0;
  181. err_clk_disable:
  182. clk_disable_unprepare(xdev->clk);
  183. return rc;
  184. }
  185. static int xwdt_remove(struct platform_device *pdev)
  186. {
  187. struct xwdt_device *xdev = platform_get_drvdata(pdev);
  188. watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
  189. clk_disable_unprepare(xdev->clk);
  190. return 0;
  191. }
  192. /* Match table for of_platform binding */
  193. static const struct of_device_id xwdt_of_match[] = {
  194. { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
  195. { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
  196. {},
  197. };
  198. MODULE_DEVICE_TABLE(of, xwdt_of_match);
  199. static struct platform_driver xwdt_driver = {
  200. .probe = xwdt_probe,
  201. .remove = xwdt_remove,
  202. .driver = {
  203. .name = WATCHDOG_NAME,
  204. .of_match_table = xwdt_of_match,
  205. },
  206. };
  207. module_platform_driver(xwdt_driver);
  208. MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
  209. MODULE_DESCRIPTION("Xilinx Watchdog driver");
  210. MODULE_LICENSE("GPL v2");