qcom-wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/bits.h>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/of_device.h>
  15. enum wdt_reg {
  16. WDT_RST,
  17. WDT_EN,
  18. WDT_STS,
  19. WDT_BARK_TIME,
  20. WDT_BITE_TIME,
  21. };
  22. #define QCOM_WDT_ENABLE BIT(0)
  23. static const u32 reg_offset_data_apcs_tmr[] = {
  24. [WDT_RST] = 0x38,
  25. [WDT_EN] = 0x40,
  26. [WDT_STS] = 0x44,
  27. [WDT_BARK_TIME] = 0x4C,
  28. [WDT_BITE_TIME] = 0x5C,
  29. };
  30. static const u32 reg_offset_data_kpss[] = {
  31. [WDT_RST] = 0x4,
  32. [WDT_EN] = 0x8,
  33. [WDT_STS] = 0xC,
  34. [WDT_BARK_TIME] = 0x10,
  35. [WDT_BITE_TIME] = 0x14,
  36. };
  37. struct qcom_wdt {
  38. struct watchdog_device wdd;
  39. unsigned long rate;
  40. void __iomem *base;
  41. const u32 *layout;
  42. };
  43. static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
  44. {
  45. return wdt->base + wdt->layout[reg];
  46. }
  47. static inline
  48. struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
  49. {
  50. return container_of(wdd, struct qcom_wdt, wdd);
  51. }
  52. static irqreturn_t qcom_wdt_isr(int irq, void *arg)
  53. {
  54. struct watchdog_device *wdd = arg;
  55. watchdog_notify_pretimeout(wdd);
  56. return IRQ_HANDLED;
  57. }
  58. static int qcom_wdt_start(struct watchdog_device *wdd)
  59. {
  60. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  61. unsigned int bark = wdd->timeout - wdd->pretimeout;
  62. writel(0, wdt_addr(wdt, WDT_EN));
  63. writel(1, wdt_addr(wdt, WDT_RST));
  64. writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
  65. writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
  66. writel(QCOM_WDT_ENABLE, 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_set_pretimeout(struct watchdog_device *wdd,
  88. unsigned int timeout)
  89. {
  90. wdd->pretimeout = timeout;
  91. return qcom_wdt_start(wdd);
  92. }
  93. static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  94. void *data)
  95. {
  96. struct qcom_wdt *wdt = to_qcom_wdt(wdd);
  97. u32 timeout;
  98. /*
  99. * Trigger watchdog bite:
  100. * Setup BITE_TIME to be 128ms, and enable WDT.
  101. */
  102. timeout = 128 * wdt->rate / 1000;
  103. writel(0, wdt_addr(wdt, WDT_EN));
  104. writel(1, wdt_addr(wdt, WDT_RST));
  105. writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
  106. writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
  107. writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
  108. /*
  109. * Actually make sure the above sequence hits hardware before sleeping.
  110. */
  111. wmb();
  112. mdelay(150);
  113. return 0;
  114. }
  115. static const struct watchdog_ops qcom_wdt_ops = {
  116. .start = qcom_wdt_start,
  117. .stop = qcom_wdt_stop,
  118. .ping = qcom_wdt_ping,
  119. .set_timeout = qcom_wdt_set_timeout,
  120. .set_pretimeout = qcom_wdt_set_pretimeout,
  121. .restart = qcom_wdt_restart,
  122. .owner = THIS_MODULE,
  123. };
  124. static const struct watchdog_info qcom_wdt_info = {
  125. .options = WDIOF_KEEPALIVEPING
  126. | WDIOF_MAGICCLOSE
  127. | WDIOF_SETTIMEOUT
  128. | WDIOF_CARDRESET,
  129. .identity = KBUILD_MODNAME,
  130. };
  131. static const struct watchdog_info qcom_wdt_pt_info = {
  132. .options = WDIOF_KEEPALIVEPING
  133. | WDIOF_MAGICCLOSE
  134. | WDIOF_SETTIMEOUT
  135. | WDIOF_PRETIMEOUT
  136. | WDIOF_CARDRESET,
  137. .identity = KBUILD_MODNAME,
  138. };
  139. static void qcom_clk_disable_unprepare(void *data)
  140. {
  141. clk_disable_unprepare(data);
  142. }
  143. static int qcom_wdt_probe(struct platform_device *pdev)
  144. {
  145. struct device *dev = &pdev->dev;
  146. struct qcom_wdt *wdt;
  147. struct resource *res;
  148. struct device_node *np = dev->of_node;
  149. const u32 *regs;
  150. u32 percpu_offset;
  151. int irq, ret;
  152. struct clk *clk;
  153. regs = of_device_get_match_data(dev);
  154. if (!regs) {
  155. dev_err(dev, "Unsupported QCOM WDT module\n");
  156. return -ENODEV;
  157. }
  158. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  159. if (!wdt)
  160. return -ENOMEM;
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. if (!res)
  163. return -ENOMEM;
  164. /* We use CPU0's DGT for the watchdog */
  165. if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  166. percpu_offset = 0;
  167. res->start += percpu_offset;
  168. res->end += percpu_offset;
  169. wdt->base = devm_ioremap_resource(dev, res);
  170. if (IS_ERR(wdt->base))
  171. return PTR_ERR(wdt->base);
  172. clk = devm_clk_get(dev, NULL);
  173. if (IS_ERR(clk)) {
  174. dev_err(dev, "failed to get input clock\n");
  175. return PTR_ERR(clk);
  176. }
  177. ret = clk_prepare_enable(clk);
  178. if (ret) {
  179. dev_err(dev, "failed to setup clock\n");
  180. return ret;
  181. }
  182. ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk);
  183. if (ret)
  184. return ret;
  185. /*
  186. * We use the clock rate to calculate the max timeout, so ensure it's
  187. * not zero to avoid a divide-by-zero exception.
  188. *
  189. * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
  190. * that it would bite before a second elapses it's usefulness is
  191. * limited. Bail if this is the case.
  192. */
  193. wdt->rate = clk_get_rate(clk);
  194. if (wdt->rate == 0 ||
  195. wdt->rate > 0x10000000U) {
  196. dev_err(dev, "invalid clock rate\n");
  197. return -EINVAL;
  198. }
  199. /* check if there is pretimeout support */
  200. irq = platform_get_irq_optional(pdev, 0);
  201. if (irq > 0) {
  202. ret = devm_request_irq(dev, irq, qcom_wdt_isr,
  203. IRQF_TRIGGER_RISING,
  204. "wdt_bark", &wdt->wdd);
  205. if (ret)
  206. return ret;
  207. wdt->wdd.info = &qcom_wdt_pt_info;
  208. wdt->wdd.pretimeout = 1;
  209. } else {
  210. if (irq == -EPROBE_DEFER)
  211. return -EPROBE_DEFER;
  212. wdt->wdd.info = &qcom_wdt_info;
  213. }
  214. wdt->wdd.ops = &qcom_wdt_ops;
  215. wdt->wdd.min_timeout = 1;
  216. wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
  217. wdt->wdd.parent = dev;
  218. wdt->layout = regs;
  219. if (readl(wdt_addr(wdt, WDT_STS)) & 1)
  220. wdt->wdd.bootstatus = WDIOF_CARDRESET;
  221. /*
  222. * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  223. * default, unless the max timeout is less than 30 seconds, then use
  224. * the max instead.
  225. */
  226. wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
  227. watchdog_init_timeout(&wdt->wdd, 0, dev);
  228. ret = devm_watchdog_register_device(dev, &wdt->wdd);
  229. if (ret)
  230. return ret;
  231. platform_set_drvdata(pdev, wdt);
  232. return 0;
  233. }
  234. static int __maybe_unused qcom_wdt_suspend(struct device *dev)
  235. {
  236. struct qcom_wdt *wdt = dev_get_drvdata(dev);
  237. if (watchdog_active(&wdt->wdd))
  238. qcom_wdt_stop(&wdt->wdd);
  239. return 0;
  240. }
  241. static int __maybe_unused qcom_wdt_resume(struct device *dev)
  242. {
  243. struct qcom_wdt *wdt = dev_get_drvdata(dev);
  244. if (watchdog_active(&wdt->wdd))
  245. qcom_wdt_start(&wdt->wdd);
  246. return 0;
  247. }
  248. static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume);
  249. static const struct of_device_id qcom_wdt_of_table[] = {
  250. { .compatible = "qcom,kpss-timer", .data = reg_offset_data_apcs_tmr },
  251. { .compatible = "qcom,scss-timer", .data = reg_offset_data_apcs_tmr },
  252. { .compatible = "qcom,kpss-wdt", .data = reg_offset_data_kpss },
  253. { },
  254. };
  255. MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
  256. static struct platform_driver qcom_watchdog_driver = {
  257. .probe = qcom_wdt_probe,
  258. .driver = {
  259. .name = KBUILD_MODNAME,
  260. .of_match_table = qcom_wdt_of_table,
  261. .pm = &qcom_wdt_pm_ops,
  262. },
  263. };
  264. module_platform_driver(qcom_watchdog_driver);
  265. MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
  266. MODULE_LICENSE("GPL v2");