tangox_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 int tangox_wdt_probe(struct platform_device *pdev)
  87. {
  88. struct tangox_wdt_device *dev;
  89. struct resource *res;
  90. u32 config;
  91. int err;
  92. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  93. if (!dev)
  94. return -ENOMEM;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. dev->base = devm_ioremap_resource(&pdev->dev, res);
  97. if (IS_ERR(dev->base))
  98. return PTR_ERR(dev->base);
  99. dev->clk = devm_clk_get(&pdev->dev, NULL);
  100. if (IS_ERR(dev->clk))
  101. return PTR_ERR(dev->clk);
  102. err = clk_prepare_enable(dev->clk);
  103. if (err)
  104. return err;
  105. dev->clk_rate = clk_get_rate(dev->clk);
  106. if (!dev->clk_rate) {
  107. err = -EINVAL;
  108. goto err;
  109. }
  110. dev->wdt.parent = &pdev->dev;
  111. dev->wdt.info = &tangox_wdt_info;
  112. dev->wdt.ops = &tangox_wdt_ops;
  113. dev->wdt.timeout = DEFAULT_TIMEOUT;
  114. dev->wdt.min_timeout = 1;
  115. dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
  116. watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
  117. watchdog_set_nowayout(&dev->wdt, nowayout);
  118. watchdog_set_drvdata(&dev->wdt, dev);
  119. /*
  120. * Deactivate counter if disable bit is set to avoid
  121. * accidental reset.
  122. */
  123. config = readl(dev->base + WD_CONFIG);
  124. if (config & WD_CONFIG_DISABLE)
  125. writel(0, dev->base + WD_COUNTER);
  126. writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
  127. /*
  128. * Mark as active and restart with configured timeout if
  129. * already running.
  130. */
  131. if (readl(dev->base + WD_COUNTER)) {
  132. set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
  133. tangox_wdt_start(&dev->wdt);
  134. }
  135. watchdog_set_restart_priority(&dev->wdt, 128);
  136. err = watchdog_register_device(&dev->wdt);
  137. if (err)
  138. goto err;
  139. platform_set_drvdata(pdev, dev);
  140. dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
  141. return 0;
  142. err:
  143. clk_disable_unprepare(dev->clk);
  144. return err;
  145. }
  146. static int tangox_wdt_remove(struct platform_device *pdev)
  147. {
  148. struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
  149. tangox_wdt_stop(&dev->wdt);
  150. clk_disable_unprepare(dev->clk);
  151. watchdog_unregister_device(&dev->wdt);
  152. return 0;
  153. }
  154. static const struct of_device_id tangox_wdt_dt_ids[] = {
  155. { .compatible = "sigma,smp8642-wdt" },
  156. { .compatible = "sigma,smp8759-wdt" },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
  160. static struct platform_driver tangox_wdt_driver = {
  161. .probe = tangox_wdt_probe,
  162. .remove = tangox_wdt_remove,
  163. .driver = {
  164. .name = "tangox-wdt",
  165. .of_match_table = tangox_wdt_dt_ids,
  166. },
  167. };
  168. module_platform_driver(tangox_wdt_driver);
  169. MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
  170. MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
  171. MODULE_LICENSE("GPL");