1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 #include <linux/bits.h>
6 #include <linux/delay.h>
7 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/watchdog.h>
23 #define QCOM_WDT_ENABLE BIT(0)
25 static const u32 reg_offset_data_apcs_tmr[] = {
29 [WDT_BARK_TIME] = 0x4C,
30 [WDT_BITE_TIME] = 0x5C,
33 static const u32 reg_offset_data_kpss[] = {
37 [WDT_BARK_TIME] = 0x10,
38 [WDT_BITE_TIME] = 0x14,
41 struct qcom_wdt_match_data {
47 struct watchdog_device wdd;
53 static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
55 return wdt->base + wdt->layout[reg];
59 struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
61 return container_of(wdd, struct qcom_wdt, wdd);
64 static irqreturn_t qcom_wdt_isr(int irq, void *arg)
66 struct watchdog_device *wdd = arg;
68 watchdog_notify_pretimeout(wdd);
73 static int qcom_wdt_start(struct watchdog_device *wdd)
75 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
76 unsigned int bark = wdd->timeout - wdd->pretimeout;
78 writel(0, wdt_addr(wdt, WDT_EN));
79 writel(1, wdt_addr(wdt, WDT_RST));
80 writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
81 writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
82 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
86 static int qcom_wdt_stop(struct watchdog_device *wdd)
88 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
90 writel(0, wdt_addr(wdt, WDT_EN));
94 static int qcom_wdt_ping(struct watchdog_device *wdd)
96 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
98 writel(1, wdt_addr(wdt, WDT_RST));
102 static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
103 unsigned int timeout)
105 wdd->timeout = timeout;
106 return qcom_wdt_start(wdd);
109 static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd,
110 unsigned int timeout)
112 wdd->pretimeout = timeout;
113 return qcom_wdt_start(wdd);
116 static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
119 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
123 * Trigger watchdog bite:
124 * Setup BITE_TIME to be 128ms, and enable WDT.
126 timeout = 128 * wdt->rate / 1000;
128 writel(0, wdt_addr(wdt, WDT_EN));
129 writel(1, wdt_addr(wdt, WDT_RST));
130 writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
131 writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
132 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
135 * Actually make sure the above sequence hits hardware before sleeping.
143 static int qcom_wdt_is_running(struct watchdog_device *wdd)
145 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
147 return (readl(wdt_addr(wdt, WDT_EN)) & QCOM_WDT_ENABLE);
150 static const struct watchdog_ops qcom_wdt_ops = {
151 .start = qcom_wdt_start,
152 .stop = qcom_wdt_stop,
153 .ping = qcom_wdt_ping,
154 .set_timeout = qcom_wdt_set_timeout,
155 .set_pretimeout = qcom_wdt_set_pretimeout,
156 .restart = qcom_wdt_restart,
157 .owner = THIS_MODULE,
160 static const struct watchdog_info qcom_wdt_info = {
161 .options = WDIOF_KEEPALIVEPING
165 .identity = KBUILD_MODNAME,
168 static const struct watchdog_info qcom_wdt_pt_info = {
169 .options = WDIOF_KEEPALIVEPING
174 .identity = KBUILD_MODNAME,
177 static const struct qcom_wdt_match_data match_data_apcs_tmr = {
178 .offset = reg_offset_data_apcs_tmr,
182 static const struct qcom_wdt_match_data match_data_kpss = {
183 .offset = reg_offset_data_kpss,
187 static int qcom_wdt_probe(struct platform_device *pdev)
189 struct device *dev = &pdev->dev;
190 struct qcom_wdt *wdt;
191 struct resource *res;
192 struct device_node *np = dev->of_node;
193 const struct qcom_wdt_match_data *data;
198 data = of_device_get_match_data(dev);
200 dev_err(dev, "Unsupported QCOM WDT module\n");
204 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
212 /* We use CPU0's DGT for the watchdog */
213 if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
216 res->start += percpu_offset;
217 res->end += percpu_offset;
219 wdt->base = devm_ioremap_resource(dev, res);
220 if (IS_ERR(wdt->base))
221 return PTR_ERR(wdt->base);
223 clk = devm_clk_get_enabled(dev, NULL);
225 dev_err(dev, "failed to get input clock\n");
230 * We use the clock rate to calculate the max timeout, so ensure it's
231 * not zero to avoid a divide-by-zero exception.
233 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
234 * that it would bite before a second elapses it's usefulness is
235 * limited. Bail if this is the case.
237 wdt->rate = clk_get_rate(clk);
238 if (wdt->rate == 0 ||
239 wdt->rate > 0x10000000U) {
240 dev_err(dev, "invalid clock rate\n");
244 /* check if there is pretimeout support */
245 irq = platform_get_irq_optional(pdev, 0);
246 if (data->pretimeout && irq > 0) {
247 ret = devm_request_irq(dev, irq, qcom_wdt_isr, 0,
248 "wdt_bark", &wdt->wdd);
252 wdt->wdd.info = &qcom_wdt_pt_info;
253 wdt->wdd.pretimeout = 1;
255 if (irq == -EPROBE_DEFER)
256 return -EPROBE_DEFER;
258 wdt->wdd.info = &qcom_wdt_info;
261 wdt->wdd.ops = &qcom_wdt_ops;
262 wdt->wdd.min_timeout = 1;
263 wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
264 wdt->wdd.parent = dev;
265 wdt->layout = data->offset;
267 if (readl(wdt_addr(wdt, WDT_STS)) & 1)
268 wdt->wdd.bootstatus = WDIOF_CARDRESET;
271 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
272 * default, unless the max timeout is less than 30 seconds, then use
275 wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
276 watchdog_init_timeout(&wdt->wdd, 0, dev);
279 * If WDT is already running, call WDT start which
280 * will stop the WDT, set timeouts as bootloader
281 * might use different ones and set running bit
282 * to inform the WDT subsystem to ping the WDT
284 if (qcom_wdt_is_running(&wdt->wdd)) {
285 qcom_wdt_start(&wdt->wdd);
286 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
289 ret = devm_watchdog_register_device(dev, &wdt->wdd);
293 platform_set_drvdata(pdev, wdt);
297 static int __maybe_unused qcom_wdt_suspend(struct device *dev)
299 struct qcom_wdt *wdt = dev_get_drvdata(dev);
301 if (watchdog_active(&wdt->wdd))
302 qcom_wdt_stop(&wdt->wdd);
307 static int __maybe_unused qcom_wdt_resume(struct device *dev)
309 struct qcom_wdt *wdt = dev_get_drvdata(dev);
311 if (watchdog_active(&wdt->wdd))
312 qcom_wdt_start(&wdt->wdd);
317 static const struct dev_pm_ops qcom_wdt_pm_ops = {
318 SET_LATE_SYSTEM_SLEEP_PM_OPS(qcom_wdt_suspend, qcom_wdt_resume)
321 static const struct of_device_id qcom_wdt_of_table[] = {
322 { .compatible = "qcom,kpss-timer", .data = &match_data_apcs_tmr },
323 { .compatible = "qcom,scss-timer", .data = &match_data_apcs_tmr },
324 { .compatible = "qcom,kpss-wdt", .data = &match_data_kpss },
327 MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
329 static struct platform_driver qcom_watchdog_driver = {
330 .probe = qcom_wdt_probe,
332 .name = KBUILD_MODNAME,
333 .of_match_table = qcom_wdt_of_table,
334 .pm = &qcom_wdt_pm_ops,
337 module_platform_driver(qcom_watchdog_driver);
339 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
340 MODULE_LICENSE("GPL v2");