xilinx_wwdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Window watchdog device driver for Xilinx Versal WWDT
  4. *
  5. * Copyright (C) 2022 - 2024, Advanced Micro Devices, Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/ioport.h>
  11. #include <linux/math64.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/watchdog.h>
  16. /* Max timeout is calculated at 100MHz source clock */
  17. #define XWWDT_DEFAULT_TIMEOUT 42
  18. #define XWWDT_MIN_TIMEOUT 1
  19. /* Register offsets for the WWDT device */
  20. #define XWWDT_MWR_OFFSET 0x00
  21. #define XWWDT_ESR_OFFSET 0x04
  22. #define XWWDT_FCR_OFFSET 0x08
  23. #define XWWDT_FWR_OFFSET 0x0c
  24. #define XWWDT_SWR_OFFSET 0x10
  25. /* Master Write Control Register Masks */
  26. #define XWWDT_MWR_MASK BIT(0)
  27. /* Enable and Status Register Masks */
  28. #define XWWDT_ESR_WINT_MASK BIT(16)
  29. #define XWWDT_ESR_WSW_MASK BIT(8)
  30. #define XWWDT_ESR_WEN_MASK BIT(0)
  31. #define XWWDT_CLOSE_WINDOW_PERCENT 50
  32. /* Maximum count value of each 32 bit window */
  33. #define XWWDT_MAX_COUNT_WINDOW GENMASK(31, 0)
  34. /* Maximum count value of closed and open window combined */
  35. #define XWWDT_MAX_COUNT_WINDOW_COMBINED GENMASK_ULL(32, 1)
  36. static int wwdt_timeout;
  37. static int closed_window_percent;
  38. module_param(wwdt_timeout, int, 0);
  39. MODULE_PARM_DESC(wwdt_timeout,
  40. "Watchdog time in seconds. (default="
  41. __MODULE_STRING(XWWDT_DEFAULT_TIMEOUT) ")");
  42. module_param(closed_window_percent, int, 0);
  43. MODULE_PARM_DESC(closed_window_percent,
  44. "Watchdog closed window percentage. (default="
  45. __MODULE_STRING(XWWDT_CLOSE_WINDOW_PERCENT) ")");
  46. /**
  47. * struct xwwdt_device - Watchdog device structure
  48. * @base: base io address of WDT device
  49. * @spinlock: spinlock for IO register access
  50. * @xilinx_wwdt_wdd: watchdog device structure
  51. * @freq: source clock frequency of WWDT
  52. * @close_percent: Closed window percent
  53. * @closed_timeout: Closed window timeout in ticks
  54. * @open_timeout: Open window timeout in ticks
  55. */
  56. struct xwwdt_device {
  57. void __iomem *base;
  58. spinlock_t spinlock; /* spinlock for register handling */
  59. struct watchdog_device xilinx_wwdt_wdd;
  60. unsigned long freq;
  61. u32 close_percent;
  62. u64 closed_timeout;
  63. u64 open_timeout;
  64. };
  65. static int xilinx_wwdt_start(struct watchdog_device *wdd)
  66. {
  67. struct xwwdt_device *xdev = watchdog_get_drvdata(wdd);
  68. struct watchdog_device *xilinx_wwdt_wdd = &xdev->xilinx_wwdt_wdd;
  69. u32 control_status_reg;
  70. spin_lock(&xdev->spinlock);
  71. iowrite32(XWWDT_MWR_MASK, xdev->base + XWWDT_MWR_OFFSET);
  72. iowrite32(~(u32)XWWDT_ESR_WEN_MASK, xdev->base + XWWDT_ESR_OFFSET);
  73. iowrite32((u32)xdev->closed_timeout, xdev->base + XWWDT_FWR_OFFSET);
  74. iowrite32((u32)xdev->open_timeout, xdev->base + XWWDT_SWR_OFFSET);
  75. /* Enable the window watchdog timer */
  76. control_status_reg = ioread32(xdev->base + XWWDT_ESR_OFFSET);
  77. control_status_reg |= XWWDT_ESR_WEN_MASK;
  78. iowrite32(control_status_reg, xdev->base + XWWDT_ESR_OFFSET);
  79. spin_unlock(&xdev->spinlock);
  80. dev_dbg(xilinx_wwdt_wdd->parent, "Watchdog Started!\n");
  81. return 0;
  82. }
  83. static int xilinx_wwdt_keepalive(struct watchdog_device *wdd)
  84. {
  85. struct xwwdt_device *xdev = watchdog_get_drvdata(wdd);
  86. u32 control_status_reg;
  87. spin_lock(&xdev->spinlock);
  88. /* Enable write access control bit for the window watchdog */
  89. iowrite32(XWWDT_MWR_MASK, xdev->base + XWWDT_MWR_OFFSET);
  90. /* Trigger restart kick to watchdog */
  91. control_status_reg = ioread32(xdev->base + XWWDT_ESR_OFFSET);
  92. control_status_reg |= XWWDT_ESR_WSW_MASK;
  93. iowrite32(control_status_reg, xdev->base + XWWDT_ESR_OFFSET);
  94. spin_unlock(&xdev->spinlock);
  95. return 0;
  96. }
  97. static const struct watchdog_info xilinx_wwdt_ident = {
  98. .options = WDIOF_KEEPALIVEPING |
  99. WDIOF_SETTIMEOUT,
  100. .firmware_version = 1,
  101. .identity = "xlnx_window watchdog",
  102. };
  103. static const struct watchdog_ops xilinx_wwdt_ops = {
  104. .owner = THIS_MODULE,
  105. .start = xilinx_wwdt_start,
  106. .ping = xilinx_wwdt_keepalive,
  107. };
  108. static int xwwdt_probe(struct platform_device *pdev)
  109. {
  110. struct watchdog_device *xilinx_wwdt_wdd;
  111. struct device *dev = &pdev->dev;
  112. struct xwwdt_device *xdev;
  113. u64 max_per_window_ms;
  114. u64 min_per_window_ms;
  115. u64 timeout_count;
  116. struct clk *clk;
  117. u32 timeout_ms;
  118. u64 ms_count;
  119. int ret;
  120. xdev = devm_kzalloc(dev, sizeof(*xdev), GFP_KERNEL);
  121. if (!xdev)
  122. return -ENOMEM;
  123. xilinx_wwdt_wdd = &xdev->xilinx_wwdt_wdd;
  124. xilinx_wwdt_wdd->info = &xilinx_wwdt_ident;
  125. xilinx_wwdt_wdd->ops = &xilinx_wwdt_ops;
  126. xilinx_wwdt_wdd->parent = dev;
  127. xdev->base = devm_platform_ioremap_resource(pdev, 0);
  128. if (IS_ERR(xdev->base))
  129. return PTR_ERR(xdev->base);
  130. clk = devm_clk_get_enabled(dev, NULL);
  131. if (IS_ERR(clk))
  132. return PTR_ERR(clk);
  133. xdev->freq = clk_get_rate(clk);
  134. if (xdev->freq < 1000000)
  135. return -EINVAL;
  136. xilinx_wwdt_wdd->min_timeout = XWWDT_MIN_TIMEOUT;
  137. xilinx_wwdt_wdd->timeout = XWWDT_DEFAULT_TIMEOUT;
  138. xilinx_wwdt_wdd->max_hw_heartbeat_ms =
  139. div64_u64(XWWDT_MAX_COUNT_WINDOW_COMBINED, xdev->freq) * 1000;
  140. if (closed_window_percent == 0 || closed_window_percent >= 100)
  141. xdev->close_percent = XWWDT_CLOSE_WINDOW_PERCENT;
  142. else
  143. xdev->close_percent = closed_window_percent;
  144. watchdog_init_timeout(xilinx_wwdt_wdd, wwdt_timeout, &pdev->dev);
  145. /* Calculate ticks for 1 milli-second */
  146. ms_count = div_u64(xdev->freq, 1000);
  147. timeout_ms = xilinx_wwdt_wdd->timeout * 1000;
  148. timeout_count = timeout_ms * ms_count;
  149. if (timeout_ms > xilinx_wwdt_wdd->max_hw_heartbeat_ms) {
  150. /*
  151. * To avoid ping restrictions until the minimum hardware heartbeat,
  152. * we will solely rely on the open window and
  153. * adjust the minimum hardware heartbeat to 0.
  154. */
  155. xdev->closed_timeout = 0;
  156. xdev->open_timeout = XWWDT_MAX_COUNT_WINDOW;
  157. xilinx_wwdt_wdd->min_hw_heartbeat_ms = 0;
  158. xilinx_wwdt_wdd->max_hw_heartbeat_ms = xilinx_wwdt_wdd->max_hw_heartbeat_ms / 2;
  159. } else {
  160. xdev->closed_timeout = div64_u64(timeout_count * xdev->close_percent, 100);
  161. xilinx_wwdt_wdd->min_hw_heartbeat_ms =
  162. div64_u64(timeout_ms * xdev->close_percent, 100);
  163. if (timeout_ms > xilinx_wwdt_wdd->max_hw_heartbeat_ms / 2) {
  164. max_per_window_ms = xilinx_wwdt_wdd->max_hw_heartbeat_ms / 2;
  165. min_per_window_ms = timeout_ms - max_per_window_ms;
  166. if (xilinx_wwdt_wdd->min_hw_heartbeat_ms > max_per_window_ms) {
  167. dev_info(xilinx_wwdt_wdd->parent,
  168. "Closed window cannot be set to %d%%. Using maximum supported value.\n",
  169. xdev->close_percent);
  170. xdev->closed_timeout = max_per_window_ms * ms_count;
  171. xilinx_wwdt_wdd->min_hw_heartbeat_ms = max_per_window_ms;
  172. } else if (xilinx_wwdt_wdd->min_hw_heartbeat_ms < min_per_window_ms) {
  173. dev_info(xilinx_wwdt_wdd->parent,
  174. "Closed window cannot be set to %d%%. Using minimum supported value.\n",
  175. xdev->close_percent);
  176. xdev->closed_timeout = min_per_window_ms * ms_count;
  177. xilinx_wwdt_wdd->min_hw_heartbeat_ms = min_per_window_ms;
  178. }
  179. }
  180. xdev->open_timeout = timeout_count - xdev->closed_timeout;
  181. }
  182. spin_lock_init(&xdev->spinlock);
  183. watchdog_set_drvdata(xilinx_wwdt_wdd, xdev);
  184. watchdog_set_nowayout(xilinx_wwdt_wdd, 1);
  185. ret = devm_watchdog_register_device(dev, xilinx_wwdt_wdd);
  186. if (ret)
  187. return ret;
  188. dev_info(dev, "Xilinx window watchdog Timer with timeout %ds\n",
  189. xilinx_wwdt_wdd->timeout);
  190. return 0;
  191. }
  192. static const struct of_device_id xwwdt_of_match[] = {
  193. { .compatible = "xlnx,versal-wwdt", },
  194. {},
  195. };
  196. MODULE_DEVICE_TABLE(of, xwwdt_of_match);
  197. static struct platform_driver xwwdt_driver = {
  198. .probe = xwwdt_probe,
  199. .driver = {
  200. .name = "Xilinx window watchdog",
  201. .of_match_table = xwwdt_of_match,
  202. },
  203. };
  204. module_platform_driver(xwwdt_driver);
  205. MODULE_AUTHOR("Neeli Srinivas <srinivas.neeli@amd.com>");
  206. MODULE_DESCRIPTION("Xilinx window watchdog driver");
  207. MODULE_LICENSE("GPL");