tangox_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
  4. * SMP86xx/SMP87xx Watchdog driver
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/watchdog.h>
  16. #define DEFAULT_TIMEOUT 30
  17. static bool nowayout = WATCHDOG_NOWAYOUT;
  18. module_param(nowayout, bool, 0);
  19. MODULE_PARM_DESC(nowayout,
  20. "Watchdog cannot be stopped once started (default="
  21. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  22. static unsigned int timeout;
  23. module_param(timeout, int, 0);
  24. MODULE_PARM_DESC(timeout, "Watchdog timeout");
  25. /*
  26. * Counter counts down from programmed value. Reset asserts when
  27. * the counter reaches 1.
  28. */
  29. #define WD_COUNTER 0
  30. #define WD_CONFIG 4
  31. #define WD_CONFIG_XTAL_IN BIT(0)
  32. #define WD_CONFIG_DISABLE BIT(31)
  33. struct tangox_wdt_device {
  34. struct watchdog_device wdt;
  35. void __iomem *base;
  36. unsigned long clk_rate;
  37. struct clk *clk;
  38. };
  39. static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
  40. unsigned int new_timeout)
  41. {
  42. wdt->timeout = new_timeout;
  43. return 0;
  44. }
  45. static int tangox_wdt_start(struct watchdog_device *wdt)
  46. {
  47. struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
  48. u32 ticks;
  49. ticks = 1 + wdt->timeout * dev->clk_rate;
  50. writel(ticks, dev->base + WD_COUNTER);
  51. return 0;
  52. }
  53. static int tangox_wdt_stop(struct watchdog_device *wdt)
  54. {
  55. struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
  56. writel(0, dev->base + WD_COUNTER);
  57. return 0;
  58. }
  59. static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
  60. {
  61. struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
  62. u32 count;
  63. count = readl(dev->base + WD_COUNTER);
  64. if (!count)
  65. return 0;
  66. return (count - 1) / dev->clk_rate;
  67. }
  68. static const struct watchdog_info tangox_wdt_info = {
  69. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  70. .identity = "tangox watchdog",
  71. };
  72. static int tangox_wdt_restart(struct watchdog_device *wdt,
  73. unsigned long action, void *data)
  74. {
  75. struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
  76. writel(1, dev->base + WD_COUNTER);
  77. return 0;
  78. }
  79. static const struct watchdog_ops tangox_wdt_ops = {
  80. .start = tangox_wdt_start,
  81. .stop = tangox_wdt_stop,
  82. .set_timeout = tangox_wdt_set_timeout,
  83. .get_timeleft = tangox_wdt_get_timeleft,
  84. .restart = tangox_wdt_restart,
  85. };
  86. static void tangox_clk_disable_unprepare(void *data)
  87. {
  88. clk_disable_unprepare(data);
  89. }
  90. static int tangox_wdt_probe(struct platform_device *pdev)
  91. {
  92. struct tangox_wdt_device *dev;
  93. u32 config;
  94. int err;
  95. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  96. if (!dev)
  97. return -ENOMEM;
  98. dev->base = devm_platform_ioremap_resource(pdev, 0);
  99. if (IS_ERR(dev->base))
  100. return PTR_ERR(dev->base);
  101. dev->clk = devm_clk_get(&pdev->dev, NULL);
  102. if (IS_ERR(dev->clk))
  103. return PTR_ERR(dev->clk);
  104. err = clk_prepare_enable(dev->clk);
  105. if (err)
  106. return err;
  107. err = devm_add_action_or_reset(&pdev->dev,
  108. tangox_clk_disable_unprepare, dev->clk);
  109. if (err)
  110. return err;
  111. dev->clk_rate = clk_get_rate(dev->clk);
  112. if (!dev->clk_rate)
  113. return -EINVAL;
  114. dev->wdt.parent = &pdev->dev;
  115. dev->wdt.info = &tangox_wdt_info;
  116. dev->wdt.ops = &tangox_wdt_ops;
  117. dev->wdt.timeout = DEFAULT_TIMEOUT;
  118. dev->wdt.min_timeout = 1;
  119. dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
  120. watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
  121. watchdog_set_nowayout(&dev->wdt, nowayout);
  122. watchdog_set_drvdata(&dev->wdt, dev);
  123. /*
  124. * Deactivate counter if disable bit is set to avoid
  125. * accidental reset.
  126. */
  127. config = readl(dev->base + WD_CONFIG);
  128. if (config & WD_CONFIG_DISABLE)
  129. writel(0, dev->base + WD_COUNTER);
  130. writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
  131. /*
  132. * Mark as active and restart with configured timeout if
  133. * already running.
  134. */
  135. if (readl(dev->base + WD_COUNTER)) {
  136. set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
  137. tangox_wdt_start(&dev->wdt);
  138. }
  139. watchdog_set_restart_priority(&dev->wdt, 128);
  140. watchdog_stop_on_unregister(&dev->wdt);
  141. err = devm_watchdog_register_device(&pdev->dev, &dev->wdt);
  142. if (err)
  143. return err;
  144. platform_set_drvdata(pdev, dev);
  145. dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
  146. return 0;
  147. }
  148. static const struct of_device_id tangox_wdt_dt_ids[] = {
  149. { .compatible = "sigma,smp8642-wdt" },
  150. { .compatible = "sigma,smp8759-wdt" },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
  154. static struct platform_driver tangox_wdt_driver = {
  155. .probe = tangox_wdt_probe,
  156. .driver = {
  157. .name = "tangox-wdt",
  158. .of_match_table = tangox_wdt_dt_ids,
  159. },
  160. };
  161. module_platform_driver(tangox_wdt_driver);
  162. MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
  163. MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
  164. MODULE_LICENSE("GPL");