meson_gxbb_wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2016 BayLibre, SAS.
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. *
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/watchdog.h>
  16. #define DEFAULT_TIMEOUT 30 /* seconds */
  17. #define GXBB_WDT_CTRL_REG 0x0
  18. #define GXBB_WDT_TCNT_REG 0x8
  19. #define GXBB_WDT_RSET_REG 0xc
  20. #define GXBB_WDT_CTRL_CLKDIV_EN BIT(25)
  21. #define GXBB_WDT_CTRL_CLK_EN BIT(24)
  22. #define GXBB_WDT_CTRL_EE_RESET BIT(21)
  23. #define GXBB_WDT_CTRL_EN BIT(18)
  24. #define GXBB_WDT_CTRL_DIV_MASK (BIT(18) - 1)
  25. #define GXBB_WDT_TCNT_SETUP_MASK (BIT(16) - 1)
  26. #define GXBB_WDT_TCNT_CNT_SHIFT 16
  27. struct meson_gxbb_wdt {
  28. void __iomem *reg_base;
  29. struct watchdog_device wdt_dev;
  30. struct clk *clk;
  31. };
  32. static int meson_gxbb_wdt_start(struct watchdog_device *wdt_dev)
  33. {
  34. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  35. writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) | GXBB_WDT_CTRL_EN,
  36. data->reg_base + GXBB_WDT_CTRL_REG);
  37. return 0;
  38. }
  39. static int meson_gxbb_wdt_stop(struct watchdog_device *wdt_dev)
  40. {
  41. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  42. writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) & ~GXBB_WDT_CTRL_EN,
  43. data->reg_base + GXBB_WDT_CTRL_REG);
  44. return 0;
  45. }
  46. static int meson_gxbb_wdt_ping(struct watchdog_device *wdt_dev)
  47. {
  48. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  49. writel(0, data->reg_base + GXBB_WDT_RSET_REG);
  50. return 0;
  51. }
  52. static int meson_gxbb_wdt_set_timeout(struct watchdog_device *wdt_dev,
  53. unsigned int timeout)
  54. {
  55. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  56. unsigned long tcnt = timeout * 1000;
  57. if (tcnt > GXBB_WDT_TCNT_SETUP_MASK)
  58. tcnt = GXBB_WDT_TCNT_SETUP_MASK;
  59. wdt_dev->timeout = timeout;
  60. meson_gxbb_wdt_ping(wdt_dev);
  61. writel(tcnt, data->reg_base + GXBB_WDT_TCNT_REG);
  62. return 0;
  63. }
  64. static unsigned int meson_gxbb_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  65. {
  66. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  67. unsigned long reg;
  68. reg = readl(data->reg_base + GXBB_WDT_TCNT_REG);
  69. return ((reg & GXBB_WDT_TCNT_SETUP_MASK) -
  70. (reg >> GXBB_WDT_TCNT_CNT_SHIFT)) / 1000;
  71. }
  72. static const struct watchdog_ops meson_gxbb_wdt_ops = {
  73. .start = meson_gxbb_wdt_start,
  74. .stop = meson_gxbb_wdt_stop,
  75. .ping = meson_gxbb_wdt_ping,
  76. .set_timeout = meson_gxbb_wdt_set_timeout,
  77. .get_timeleft = meson_gxbb_wdt_get_timeleft,
  78. };
  79. static const struct watchdog_info meson_gxbb_wdt_info = {
  80. .identity = "Meson GXBB Watchdog",
  81. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  82. };
  83. static int __maybe_unused meson_gxbb_wdt_resume(struct device *dev)
  84. {
  85. struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
  86. if (watchdog_active(&data->wdt_dev))
  87. meson_gxbb_wdt_start(&data->wdt_dev);
  88. return 0;
  89. }
  90. static int __maybe_unused meson_gxbb_wdt_suspend(struct device *dev)
  91. {
  92. struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
  93. if (watchdog_active(&data->wdt_dev))
  94. meson_gxbb_wdt_stop(&data->wdt_dev);
  95. return 0;
  96. }
  97. static const struct dev_pm_ops meson_gxbb_wdt_pm_ops = {
  98. SET_SYSTEM_SLEEP_PM_OPS(meson_gxbb_wdt_suspend, meson_gxbb_wdt_resume)
  99. };
  100. static const struct of_device_id meson_gxbb_wdt_dt_ids[] = {
  101. { .compatible = "amlogic,meson-gxbb-wdt", },
  102. { /* sentinel */ },
  103. };
  104. MODULE_DEVICE_TABLE(of, meson_gxbb_wdt_dt_ids);
  105. static void meson_clk_disable_unprepare(void *data)
  106. {
  107. clk_disable_unprepare(data);
  108. }
  109. static int meson_gxbb_wdt_probe(struct platform_device *pdev)
  110. {
  111. struct device *dev = &pdev->dev;
  112. struct meson_gxbb_wdt *data;
  113. int ret;
  114. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  115. if (!data)
  116. return -ENOMEM;
  117. data->reg_base = devm_platform_ioremap_resource(pdev, 0);
  118. if (IS_ERR(data->reg_base))
  119. return PTR_ERR(data->reg_base);
  120. data->clk = devm_clk_get(dev, NULL);
  121. if (IS_ERR(data->clk))
  122. return PTR_ERR(data->clk);
  123. ret = clk_prepare_enable(data->clk);
  124. if (ret)
  125. return ret;
  126. ret = devm_add_action_or_reset(dev, meson_clk_disable_unprepare,
  127. data->clk);
  128. if (ret)
  129. return ret;
  130. platform_set_drvdata(pdev, data);
  131. data->wdt_dev.parent = dev;
  132. data->wdt_dev.info = &meson_gxbb_wdt_info;
  133. data->wdt_dev.ops = &meson_gxbb_wdt_ops;
  134. data->wdt_dev.max_hw_heartbeat_ms = GXBB_WDT_TCNT_SETUP_MASK;
  135. data->wdt_dev.min_timeout = 1;
  136. data->wdt_dev.timeout = DEFAULT_TIMEOUT;
  137. watchdog_set_drvdata(&data->wdt_dev, data);
  138. /* Setup with 1ms timebase */
  139. writel(((clk_get_rate(data->clk) / 1000) & GXBB_WDT_CTRL_DIV_MASK) |
  140. GXBB_WDT_CTRL_EE_RESET |
  141. GXBB_WDT_CTRL_CLK_EN |
  142. GXBB_WDT_CTRL_CLKDIV_EN,
  143. data->reg_base + GXBB_WDT_CTRL_REG);
  144. meson_gxbb_wdt_set_timeout(&data->wdt_dev, data->wdt_dev.timeout);
  145. watchdog_stop_on_reboot(&data->wdt_dev);
  146. return devm_watchdog_register_device(dev, &data->wdt_dev);
  147. }
  148. static struct platform_driver meson_gxbb_wdt_driver = {
  149. .probe = meson_gxbb_wdt_probe,
  150. .driver = {
  151. .name = "meson-gxbb-wdt",
  152. .pm = &meson_gxbb_wdt_pm_ops,
  153. .of_match_table = meson_gxbb_wdt_dt_ids,
  154. },
  155. };
  156. module_platform_driver(meson_gxbb_wdt_driver);
  157. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  158. MODULE_DESCRIPTION("Amlogic Meson GXBB Watchdog timer driver");
  159. MODULE_LICENSE("Dual BSD/GPL");