rtd119x_wdt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Realtek RTD129x watchdog
  3. *
  4. * Copyright (c) 2017 Andreas Färber
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/watchdog.h>
  16. #define RTD119X_TCWCR 0x0
  17. #define RTD119X_TCWTR 0x4
  18. #define RTD119X_TCWOV 0xc
  19. #define RTD119X_TCWCR_WDEN_DISABLED 0xa5
  20. #define RTD119X_TCWCR_WDEN_ENABLED 0xff
  21. #define RTD119X_TCWCR_WDEN_MASK 0xff
  22. #define RTD119X_TCWTR_WDCLR BIT(0)
  23. struct rtd119x_watchdog_device {
  24. struct watchdog_device wdt_dev;
  25. void __iomem *base;
  26. struct clk *clk;
  27. };
  28. static int rtd119x_wdt_start(struct watchdog_device *wdev)
  29. {
  30. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  31. u32 val;
  32. val = readl_relaxed(data->base + RTD119X_TCWCR);
  33. val &= ~RTD119X_TCWCR_WDEN_MASK;
  34. val |= RTD119X_TCWCR_WDEN_ENABLED;
  35. writel(val, data->base + RTD119X_TCWCR);
  36. return 0;
  37. }
  38. static int rtd119x_wdt_stop(struct watchdog_device *wdev)
  39. {
  40. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  41. u32 val;
  42. val = readl_relaxed(data->base + RTD119X_TCWCR);
  43. val &= ~RTD119X_TCWCR_WDEN_MASK;
  44. val |= RTD119X_TCWCR_WDEN_DISABLED;
  45. writel(val, data->base + RTD119X_TCWCR);
  46. return 0;
  47. }
  48. static int rtd119x_wdt_ping(struct watchdog_device *wdev)
  49. {
  50. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  51. writel_relaxed(RTD119X_TCWTR_WDCLR, data->base + RTD119X_TCWTR);
  52. return rtd119x_wdt_start(wdev);
  53. }
  54. static int rtd119x_wdt_set_timeout(struct watchdog_device *wdev, unsigned int val)
  55. {
  56. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  57. writel(val * clk_get_rate(data->clk), data->base + RTD119X_TCWOV);
  58. data->wdt_dev.timeout = val;
  59. return 0;
  60. }
  61. static const struct watchdog_ops rtd119x_wdt_ops = {
  62. .owner = THIS_MODULE,
  63. .start = rtd119x_wdt_start,
  64. .stop = rtd119x_wdt_stop,
  65. .ping = rtd119x_wdt_ping,
  66. .set_timeout = rtd119x_wdt_set_timeout,
  67. };
  68. static const struct watchdog_info rtd119x_wdt_info = {
  69. .identity = "rtd119x-wdt",
  70. .options = 0,
  71. };
  72. static const struct of_device_id rtd119x_wdt_dt_ids[] = {
  73. { .compatible = "realtek,rtd1295-watchdog" },
  74. { }
  75. };
  76. static int rtd119x_wdt_probe(struct platform_device *pdev)
  77. {
  78. struct rtd119x_watchdog_device *data;
  79. struct resource *res;
  80. int ret;
  81. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  82. if (!data)
  83. return -ENOMEM;
  84. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  85. data->base = devm_ioremap_resource(&pdev->dev, res);
  86. if (IS_ERR(data->base))
  87. return PTR_ERR(data->base);
  88. data->clk = of_clk_get(pdev->dev.of_node, 0);
  89. if (IS_ERR(data->clk))
  90. return PTR_ERR(data->clk);
  91. ret = clk_prepare_enable(data->clk);
  92. if (ret) {
  93. clk_put(data->clk);
  94. return ret;
  95. }
  96. data->wdt_dev.info = &rtd119x_wdt_info;
  97. data->wdt_dev.ops = &rtd119x_wdt_ops;
  98. data->wdt_dev.timeout = 120;
  99. data->wdt_dev.max_timeout = 0xffffffff / clk_get_rate(data->clk);
  100. data->wdt_dev.min_timeout = 1;
  101. data->wdt_dev.parent = &pdev->dev;
  102. watchdog_stop_on_reboot(&data->wdt_dev);
  103. watchdog_set_drvdata(&data->wdt_dev, data);
  104. platform_set_drvdata(pdev, data);
  105. writel_relaxed(RTD119X_TCWTR_WDCLR, data->base + RTD119X_TCWTR);
  106. rtd119x_wdt_set_timeout(&data->wdt_dev, data->wdt_dev.timeout);
  107. rtd119x_wdt_stop(&data->wdt_dev);
  108. ret = watchdog_register_device(&data->wdt_dev);
  109. if (ret) {
  110. clk_disable_unprepare(data->clk);
  111. clk_put(data->clk);
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. static int rtd119x_wdt_remove(struct platform_device *pdev)
  117. {
  118. struct rtd119x_watchdog_device *data = platform_get_drvdata(pdev);
  119. watchdog_unregister_device(&data->wdt_dev);
  120. clk_disable_unprepare(data->clk);
  121. clk_put(data->clk);
  122. return 0;
  123. }
  124. static struct platform_driver rtd119x_wdt_driver = {
  125. .probe = rtd119x_wdt_probe,
  126. .remove = rtd119x_wdt_remove,
  127. .driver = {
  128. .name = "rtd1295-watchdog",
  129. .of_match_table = rtd119x_wdt_dt_ids,
  130. },
  131. };
  132. builtin_platform_driver(rtd119x_wdt_driver);