qcom-wdt.c 6.4 KB

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