qcom-wdt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/reboot.h>
  21. #include <linux/watchdog.h>
  22. #define WDT_RST 0x38
  23. #define WDT_EN 0x40
  24. #define WDT_BITE_TIME 0x5C
  25. struct qcom_wdt {
  26. struct watchdog_device wdd;
  27. struct clk *clk;
  28. unsigned long rate;
  29. struct notifier_block restart_nb;
  30. void __iomem *base;
  31. };
  32. static inline
  33. struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
  34. {
  35. return container_of(wdd, struct qcom_wdt, wdd);
  36. }
  37. static int qcom_wdt_start(struct watchdog_device *wdd)
  38. {
  39. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  40. writel(0, wdt->base + WDT_EN);
  41. writel(1, wdt->base + WDT_RST);
  42. writel(wdd->timeout * wdt->rate, wdt->base + WDT_BITE_TIME);
  43. writel(1, wdt->base + WDT_EN);
  44. return 0;
  45. }
  46. static int qcom_wdt_stop(struct watchdog_device *wdd)
  47. {
  48. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  49. writel(0, wdt->base + WDT_EN);
  50. return 0;
  51. }
  52. static int qcom_wdt_ping(struct watchdog_device *wdd)
  53. {
  54. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  55. writel(1, wdt->base + WDT_RST);
  56. return 0;
  57. }
  58. static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
  59. unsigned int timeout)
  60. {
  61. wdd->timeout = timeout;
  62. return qcom_wdt_start(wdd);
  63. }
  64. static const struct watchdog_ops qcom_wdt_ops = {
  65. .start = qcom_wdt_start,
  66. .stop = qcom_wdt_stop,
  67. .ping = qcom_wdt_ping,
  68. .set_timeout = qcom_wdt_set_timeout,
  69. .owner = THIS_MODULE,
  70. };
  71. static const struct watchdog_info qcom_wdt_info = {
  72. .options = WDIOF_KEEPALIVEPING
  73. | WDIOF_MAGICCLOSE
  74. | WDIOF_SETTIMEOUT,
  75. .identity = KBUILD_MODNAME,
  76. };
  77. static int qcom_wdt_restart(struct notifier_block *nb, unsigned long action,
  78. void *data)
  79. {
  80. struct qcom_wdt *wdt = container_of(nb, struct qcom_wdt, restart_nb);
  81. u32 timeout;
  82. /*
  83. * Trigger watchdog bite:
  84. * Setup BITE_TIME to be 128ms, and enable WDT.
  85. */
  86. timeout = 128 * wdt->rate / 1000;
  87. writel(0, wdt->base + WDT_EN);
  88. writel(1, wdt->base + WDT_RST);
  89. writel(timeout, wdt->base + WDT_BITE_TIME);
  90. writel(1, wdt->base + WDT_EN);
  91. /*
  92. * Actually make sure the above sequence hits hardware before sleeping.
  93. */
  94. wmb();
  95. msleep(150);
  96. return NOTIFY_DONE;
  97. }
  98. static int qcom_wdt_probe(struct platform_device *pdev)
  99. {
  100. struct qcom_wdt *wdt;
  101. struct resource *res;
  102. struct device_node *np = pdev->dev.of_node;
  103. u32 percpu_offset;
  104. int ret;
  105. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  106. if (!wdt)
  107. return -ENOMEM;
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. /* We use CPU0's DGT for the watchdog */
  110. if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  111. percpu_offset = 0;
  112. res->start += percpu_offset;
  113. res->end += percpu_offset;
  114. wdt->base = devm_ioremap_resource(&pdev->dev, res);
  115. if (IS_ERR(wdt->base))
  116. return PTR_ERR(wdt->base);
  117. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  118. if (IS_ERR(wdt->clk)) {
  119. dev_err(&pdev->dev, "failed to get input clock\n");
  120. return PTR_ERR(wdt->clk);
  121. }
  122. ret = clk_prepare_enable(wdt->clk);
  123. if (ret) {
  124. dev_err(&pdev->dev, "failed to setup clock\n");
  125. return ret;
  126. }
  127. /*
  128. * We use the clock rate to calculate the max timeout, so ensure it's
  129. * not zero to avoid a divide-by-zero exception.
  130. *
  131. * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
  132. * that it would bite before a second elapses it's usefulness is
  133. * limited. Bail if this is the case.
  134. */
  135. wdt->rate = clk_get_rate(wdt->clk);
  136. if (wdt->rate == 0 ||
  137. wdt->rate > 0x10000000U) {
  138. dev_err(&pdev->dev, "invalid clock rate\n");
  139. ret = -EINVAL;
  140. goto err_clk_unprepare;
  141. }
  142. wdt->wdd.dev = &pdev->dev;
  143. wdt->wdd.info = &qcom_wdt_info;
  144. wdt->wdd.ops = &qcom_wdt_ops;
  145. wdt->wdd.min_timeout = 1;
  146. wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
  147. wdt->wdd.parent = &pdev->dev;
  148. /*
  149. * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  150. * default, unless the max timeout is less than 30 seconds, then use
  151. * the max instead.
  152. */
  153. wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
  154. watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
  155. ret = watchdog_register_device(&wdt->wdd);
  156. if (ret) {
  157. dev_err(&pdev->dev, "failed to register watchdog\n");
  158. goto err_clk_unprepare;
  159. }
  160. /*
  161. * WDT restart notifier has priority 0 (use as a last resort)
  162. */
  163. wdt->restart_nb.notifier_call = qcom_wdt_restart;
  164. ret = register_restart_handler(&wdt->restart_nb);
  165. if (ret)
  166. dev_err(&pdev->dev, "failed to setup restart handler\n");
  167. platform_set_drvdata(pdev, wdt);
  168. return 0;
  169. err_clk_unprepare:
  170. clk_disable_unprepare(wdt->clk);
  171. return ret;
  172. }
  173. static int qcom_wdt_remove(struct platform_device *pdev)
  174. {
  175. struct qcom_wdt *wdt = platform_get_drvdata(pdev);
  176. unregister_restart_handler(&wdt->restart_nb);
  177. watchdog_unregister_device(&wdt->wdd);
  178. clk_disable_unprepare(wdt->clk);
  179. return 0;
  180. }
  181. static const struct of_device_id qcom_wdt_of_table[] = {
  182. { .compatible = "qcom,kpss-timer" },
  183. { .compatible = "qcom,scss-timer" },
  184. { },
  185. };
  186. MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
  187. static struct platform_driver qcom_watchdog_driver = {
  188. .probe = qcom_wdt_probe,
  189. .remove = qcom_wdt_remove,
  190. .driver = {
  191. .name = KBUILD_MODNAME,
  192. .of_match_table = qcom_wdt_of_table,
  193. },
  194. };
  195. module_platform_driver(qcom_watchdog_driver);
  196. MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
  197. MODULE_LICENSE("GPL v2");