qcom-wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 0x0
  23. #define WDT_EN 0x8
  24. #define WDT_BITE_TIME 0x24
  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. int ret;
  103. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  104. if (!wdt)
  105. return -ENOMEM;
  106. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  107. wdt->base = devm_ioremap_resource(&pdev->dev, res);
  108. if (IS_ERR(wdt->base))
  109. return PTR_ERR(wdt->base);
  110. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  111. if (IS_ERR(wdt->clk)) {
  112. dev_err(&pdev->dev, "failed to get input clock\n");
  113. return PTR_ERR(wdt->clk);
  114. }
  115. ret = clk_prepare_enable(wdt->clk);
  116. if (ret) {
  117. dev_err(&pdev->dev, "failed to setup clock\n");
  118. return ret;
  119. }
  120. /*
  121. * We use the clock rate to calculate the max timeout, so ensure it's
  122. * not zero to avoid a divide-by-zero exception.
  123. *
  124. * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
  125. * that it would bite before a second elapses it's usefulness is
  126. * limited. Bail if this is the case.
  127. */
  128. wdt->rate = clk_get_rate(wdt->clk);
  129. if (wdt->rate == 0 ||
  130. wdt->rate > 0x10000000U) {
  131. dev_err(&pdev->dev, "invalid clock rate\n");
  132. ret = -EINVAL;
  133. goto err_clk_unprepare;
  134. }
  135. wdt->wdd.dev = &pdev->dev;
  136. wdt->wdd.info = &qcom_wdt_info;
  137. wdt->wdd.ops = &qcom_wdt_ops;
  138. wdt->wdd.min_timeout = 1;
  139. wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
  140. /*
  141. * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  142. * default, unless the max timeout is less than 30 seconds, then use
  143. * the max instead.
  144. */
  145. wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
  146. watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
  147. ret = watchdog_register_device(&wdt->wdd);
  148. if (ret) {
  149. dev_err(&pdev->dev, "failed to register watchdog\n");
  150. goto err_clk_unprepare;
  151. }
  152. /*
  153. * WDT restart notifier has priority 0 (use as a last resort)
  154. */
  155. wdt->restart_nb.notifier_call = qcom_wdt_restart;
  156. ret = register_restart_handler(&wdt->restart_nb);
  157. if (ret)
  158. dev_err(&pdev->dev, "failed to setup restart handler\n");
  159. platform_set_drvdata(pdev, wdt);
  160. return 0;
  161. err_clk_unprepare:
  162. clk_disable_unprepare(wdt->clk);
  163. return ret;
  164. }
  165. static int qcom_wdt_remove(struct platform_device *pdev)
  166. {
  167. struct qcom_wdt *wdt = platform_get_drvdata(pdev);
  168. unregister_restart_handler(&wdt->restart_nb);
  169. watchdog_unregister_device(&wdt->wdd);
  170. clk_disable_unprepare(wdt->clk);
  171. return 0;
  172. }
  173. static const struct of_device_id qcom_wdt_of_table[] = {
  174. { .compatible = "qcom,kpss-wdt-msm8960", },
  175. { .compatible = "qcom,kpss-wdt-apq8064", },
  176. { .compatible = "qcom,kpss-wdt-ipq8064", },
  177. { },
  178. };
  179. MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
  180. static struct platform_driver qcom_watchdog_driver = {
  181. .probe = qcom_wdt_probe,
  182. .remove = qcom_wdt_remove,
  183. .driver = {
  184. .name = KBUILD_MODNAME,
  185. .of_match_table = qcom_wdt_of_table,
  186. },
  187. };
  188. module_platform_driver(qcom_watchdog_driver);
  189. MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
  190. MODULE_LICENSE("GPL v2");