qcom-wdt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/watchdog.h>
  21. #define WDT_RST 0x38
  22. #define WDT_EN 0x40
  23. #define WDT_BITE_TIME 0x5C
  24. struct qcom_wdt {
  25. struct watchdog_device wdd;
  26. struct clk *clk;
  27. unsigned long rate;
  28. void __iomem *base;
  29. };
  30. static inline
  31. struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
  32. {
  33. return container_of(wdd, struct qcom_wdt, wdd);
  34. }
  35. static int qcom_wdt_start(struct watchdog_device *wdd)
  36. {
  37. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  38. writel(0, wdt->base + WDT_EN);
  39. writel(1, wdt->base + WDT_RST);
  40. writel(wdd->timeout * wdt->rate, wdt->base + WDT_BITE_TIME);
  41. writel(1, wdt->base + WDT_EN);
  42. return 0;
  43. }
  44. static int qcom_wdt_stop(struct watchdog_device *wdd)
  45. {
  46. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  47. writel(0, wdt->base + WDT_EN);
  48. return 0;
  49. }
  50. static int qcom_wdt_ping(struct watchdog_device *wdd)
  51. {
  52. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  53. writel(1, wdt->base + WDT_RST);
  54. return 0;
  55. }
  56. static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
  57. unsigned int timeout)
  58. {
  59. wdd->timeout = timeout;
  60. return qcom_wdt_start(wdd);
  61. }
  62. static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  63. void *data)
  64. {
  65. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  66. u32 timeout;
  67. /*
  68. * Trigger watchdog bite:
  69. * Setup BITE_TIME to be 128ms, and enable WDT.
  70. */
  71. timeout = 128 * wdt->rate / 1000;
  72. writel(0, wdt->base + WDT_EN);
  73. writel(1, wdt->base + WDT_RST);
  74. writel(timeout, wdt->base + WDT_BITE_TIME);
  75. writel(1, wdt->base + WDT_EN);
  76. /*
  77. * Actually make sure the above sequence hits hardware before sleeping.
  78. */
  79. wmb();
  80. msleep(150);
  81. return 0;
  82. }
  83. static const struct watchdog_ops qcom_wdt_ops = {
  84. .start = qcom_wdt_start,
  85. .stop = qcom_wdt_stop,
  86. .ping = qcom_wdt_ping,
  87. .set_timeout = qcom_wdt_set_timeout,
  88. .restart = qcom_wdt_restart,
  89. .owner = THIS_MODULE,
  90. };
  91. static const struct watchdog_info qcom_wdt_info = {
  92. .options = WDIOF_KEEPALIVEPING
  93. | WDIOF_MAGICCLOSE
  94. | WDIOF_SETTIMEOUT,
  95. .identity = KBUILD_MODNAME,
  96. };
  97. static int qcom_wdt_probe(struct platform_device *pdev)
  98. {
  99. struct qcom_wdt *wdt;
  100. struct resource *res;
  101. struct device_node *np = pdev->dev.of_node;
  102. u32 percpu_offset;
  103. int ret;
  104. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  105. if (!wdt)
  106. return -ENOMEM;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. /* We use CPU0's DGT for the watchdog */
  109. if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  110. percpu_offset = 0;
  111. res->start += percpu_offset;
  112. res->end += percpu_offset;
  113. wdt->base = devm_ioremap_resource(&pdev->dev, res);
  114. if (IS_ERR(wdt->base))
  115. return PTR_ERR(wdt->base);
  116. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  117. if (IS_ERR(wdt->clk)) {
  118. dev_err(&pdev->dev, "failed to get input clock\n");
  119. return PTR_ERR(wdt->clk);
  120. }
  121. ret = clk_prepare_enable(wdt->clk);
  122. if (ret) {
  123. dev_err(&pdev->dev, "failed to setup clock\n");
  124. return ret;
  125. }
  126. /*
  127. * We use the clock rate to calculate the max timeout, so ensure it's
  128. * not zero to avoid a divide-by-zero exception.
  129. *
  130. * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
  131. * that it would bite before a second elapses it's usefulness is
  132. * limited. Bail if this is the case.
  133. */
  134. wdt->rate = clk_get_rate(wdt->clk);
  135. if (wdt->rate == 0 ||
  136. wdt->rate > 0x10000000U) {
  137. dev_err(&pdev->dev, "invalid clock rate\n");
  138. ret = -EINVAL;
  139. goto err_clk_unprepare;
  140. }
  141. wdt->wdd.info = &qcom_wdt_info;
  142. wdt->wdd.ops = &qcom_wdt_ops;
  143. wdt->wdd.min_timeout = 1;
  144. wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
  145. wdt->wdd.parent = &pdev->dev;
  146. /*
  147. * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  148. * default, unless the max timeout is less than 30 seconds, then use
  149. * the max instead.
  150. */
  151. wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
  152. watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
  153. ret = watchdog_register_device(&wdt->wdd);
  154. if (ret) {
  155. dev_err(&pdev->dev, "failed to register watchdog\n");
  156. goto err_clk_unprepare;
  157. }
  158. platform_set_drvdata(pdev, wdt);
  159. return 0;
  160. err_clk_unprepare:
  161. clk_disable_unprepare(wdt->clk);
  162. return ret;
  163. }
  164. static int qcom_wdt_remove(struct platform_device *pdev)
  165. {
  166. struct qcom_wdt *wdt = platform_get_drvdata(pdev);
  167. watchdog_unregister_device(&wdt->wdd);
  168. clk_disable_unprepare(wdt->clk);
  169. return 0;
  170. }
  171. static const struct of_device_id qcom_wdt_of_table[] = {
  172. { .compatible = "qcom,kpss-timer" },
  173. { .compatible = "qcom,scss-timer" },
  174. { },
  175. };
  176. MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
  177. static struct platform_driver qcom_watchdog_driver = {
  178. .probe = qcom_wdt_probe,
  179. .remove = qcom_wdt_remove,
  180. .driver = {
  181. .name = KBUILD_MODNAME,
  182. .of_match_table = qcom_wdt_of_table,
  183. },
  184. };
  185. module_platform_driver(qcom_watchdog_driver);
  186. MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
  187. MODULE_LICENSE("GPL v2");