rtd119x_wdt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/watchdog.h>
  15. #define RTD119X_TCWCR 0x0
  16. #define RTD119X_TCWTR 0x4
  17. #define RTD119X_TCWOV 0xc
  18. #define RTD119X_TCWCR_WDEN_DISABLED 0xa5
  19. #define RTD119X_TCWCR_WDEN_ENABLED 0xff
  20. #define RTD119X_TCWCR_WDEN_MASK 0xff
  21. #define RTD119X_TCWTR_WDCLR BIT(0)
  22. struct rtd119x_watchdog_device {
  23. struct watchdog_device wdt_dev;
  24. void __iomem *base;
  25. struct clk *clk;
  26. };
  27. static int rtd119x_wdt_start(struct watchdog_device *wdev)
  28. {
  29. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  30. u32 val;
  31. val = readl_relaxed(data->base + RTD119X_TCWCR);
  32. val &= ~RTD119X_TCWCR_WDEN_MASK;
  33. val |= RTD119X_TCWCR_WDEN_ENABLED;
  34. writel(val, data->base + RTD119X_TCWCR);
  35. return 0;
  36. }
  37. static int rtd119x_wdt_stop(struct watchdog_device *wdev)
  38. {
  39. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  40. u32 val;
  41. val = readl_relaxed(data->base + RTD119X_TCWCR);
  42. val &= ~RTD119X_TCWCR_WDEN_MASK;
  43. val |= RTD119X_TCWCR_WDEN_DISABLED;
  44. writel(val, data->base + RTD119X_TCWCR);
  45. return 0;
  46. }
  47. static int rtd119x_wdt_ping(struct watchdog_device *wdev)
  48. {
  49. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  50. writel_relaxed(RTD119X_TCWTR_WDCLR, data->base + RTD119X_TCWTR);
  51. return rtd119x_wdt_start(wdev);
  52. }
  53. static int rtd119x_wdt_set_timeout(struct watchdog_device *wdev, unsigned int val)
  54. {
  55. struct rtd119x_watchdog_device *data = watchdog_get_drvdata(wdev);
  56. writel(val * clk_get_rate(data->clk), data->base + RTD119X_TCWOV);
  57. data->wdt_dev.timeout = val;
  58. return 0;
  59. }
  60. static const struct watchdog_ops rtd119x_wdt_ops = {
  61. .owner = THIS_MODULE,
  62. .start = rtd119x_wdt_start,
  63. .stop = rtd119x_wdt_stop,
  64. .ping = rtd119x_wdt_ping,
  65. .set_timeout = rtd119x_wdt_set_timeout,
  66. };
  67. static const struct watchdog_info rtd119x_wdt_info = {
  68. .identity = "rtd119x-wdt",
  69. .options = 0,
  70. };
  71. static const struct of_device_id rtd119x_wdt_dt_ids[] = {
  72. { .compatible = "realtek,rtd1295-watchdog" },
  73. { }
  74. };
  75. static void rtd119x_clk_disable_unprepare(void *data)
  76. {
  77. clk_disable_unprepare(data);
  78. }
  79. static int rtd119x_wdt_probe(struct platform_device *pdev)
  80. {
  81. struct device *dev = &pdev->dev;
  82. struct rtd119x_watchdog_device *data;
  83. int ret;
  84. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  85. if (!data)
  86. return -ENOMEM;
  87. data->base = devm_platform_ioremap_resource(pdev, 0);
  88. if (IS_ERR(data->base))
  89. return PTR_ERR(data->base);
  90. data->clk = devm_clk_get(dev, NULL);
  91. if (IS_ERR(data->clk))
  92. return PTR_ERR(data->clk);
  93. ret = clk_prepare_enable(data->clk);
  94. if (ret)
  95. return ret;
  96. ret = devm_add_action_or_reset(dev, rtd119x_clk_disable_unprepare,
  97. data->clk);
  98. if (ret)
  99. return ret;
  100. data->wdt_dev.info = &rtd119x_wdt_info;
  101. data->wdt_dev.ops = &rtd119x_wdt_ops;
  102. data->wdt_dev.timeout = 120;
  103. data->wdt_dev.max_timeout = 0xffffffff / clk_get_rate(data->clk);
  104. data->wdt_dev.min_timeout = 1;
  105. data->wdt_dev.parent = dev;
  106. watchdog_stop_on_reboot(&data->wdt_dev);
  107. watchdog_set_drvdata(&data->wdt_dev, data);
  108. platform_set_drvdata(pdev, data);
  109. writel_relaxed(RTD119X_TCWTR_WDCLR, data->base + RTD119X_TCWTR);
  110. rtd119x_wdt_set_timeout(&data->wdt_dev, data->wdt_dev.timeout);
  111. rtd119x_wdt_stop(&data->wdt_dev);
  112. return devm_watchdog_register_device(dev, &data->wdt_dev);
  113. }
  114. static struct platform_driver rtd119x_wdt_driver = {
  115. .probe = rtd119x_wdt_probe,
  116. .driver = {
  117. .name = "rtd1295-watchdog",
  118. .of_match_table = rtd119x_wdt_dt_ids,
  119. },
  120. };
  121. builtin_platform_driver(rtd119x_wdt_driver);