of_xilinx_wdt.c 6.4 KB

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