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>
14 #include <linux/of_device.h>
24 #define QCOM_WDT_ENABLE BIT(0)
26 static const u32 reg_offset_data_apcs_tmr[] = {
30 [WDT_BARK_TIME] = 0x4C,
31 [WDT_BITE_TIME] = 0x5C,
34 static const u32 reg_offset_data_kpss[] = {
38 [WDT_BARK_TIME] = 0x10,
39 [WDT_BITE_TIME] = 0x14,
42 struct qcom_wdt_match_data {
48 struct watchdog_device wdd;
54 static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
56 return wdt->base + wdt->layout[reg];
60 struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
62 return container_of(wdd, struct qcom_wdt, wdd);
65 static irqreturn_t qcom_wdt_isr(int irq, void *arg)
67 struct watchdog_device *wdd = arg;
69 watchdog_notify_pretimeout(wdd);
74 static int qcom_wdt_start(struct watchdog_device *wdd)
76 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
77 unsigned int bark = wdd->timeout - wdd->pretimeout;
79 writel(0, wdt_addr(wdt, WDT_EN));
80 writel(1, wdt_addr(wdt, WDT_RST));
81 writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
82 writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
83 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
87 static int qcom_wdt_stop(struct watchdog_device *wdd)
89 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
91 writel(0, wdt_addr(wdt, WDT_EN));
95 static int qcom_wdt_ping(struct watchdog_device *wdd)
97 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
99 writel(1, wdt_addr(wdt, WDT_RST));
103 static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
104 unsigned int timeout)
106 wdd->timeout = timeout;
107 return qcom_wdt_start(wdd);
110 static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd,
111 unsigned int timeout)
113 wdd->pretimeout = timeout;
114 return qcom_wdt_start(wdd);
117 static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
120 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
124 * Trigger watchdog bite:
125 * Setup BITE_TIME to be 128ms, and enable WDT.
127 timeout = 128 * wdt->rate / 1000;
129 writel(0, wdt_addr(wdt, WDT_EN));
130 writel(1, wdt_addr(wdt, WDT_RST));
131 writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
132 writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
133 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
136 * Actually make sure the above sequence hits hardware before sleeping.
144 static int qcom_wdt_is_running(struct watchdog_device *wdd)
146 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
148 return (readl(wdt_addr(wdt, WDT_EN)) & QCOM_WDT_ENABLE);
151 static const struct watchdog_ops qcom_wdt_ops = {
152 .start = qcom_wdt_start,
153 .stop = qcom_wdt_stop,
154 .ping = qcom_wdt_ping,
155 .set_timeout = qcom_wdt_set_timeout,
156 .set_pretimeout = qcom_wdt_set_pretimeout,
157 .restart = qcom_wdt_restart,
158 .owner = THIS_MODULE,
161 static const struct watchdog_info qcom_wdt_info = {
162 .options = WDIOF_KEEPALIVEPING
166 .identity = KBUILD_MODNAME,
169 static const struct watchdog_info qcom_wdt_pt_info = {
170 .options = WDIOF_KEEPALIVEPING
175 .identity = KBUILD_MODNAME,
178 static void qcom_clk_disable_unprepare(void *data)
180 clk_disable_unprepare(data);
183 static const struct qcom_wdt_match_data match_data_apcs_tmr = {
184 .offset = reg_offset_data_apcs_tmr,
188 static const struct qcom_wdt_match_data match_data_kpss = {
189 .offset = reg_offset_data_kpss,
193 static int qcom_wdt_probe(struct platform_device *pdev)
195 struct device *dev = &pdev->dev;
196 struct qcom_wdt *wdt;
197 struct resource *res;
198 struct device_node *np = dev->of_node;
199 const struct qcom_wdt_match_data *data;
204 data = of_device_get_match_data(dev);
206 dev_err(dev, "Unsupported QCOM WDT module\n");
210 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
214 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 /* We use CPU0's DGT for the watchdog */
219 if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
222 res->start += percpu_offset;
223 res->end += percpu_offset;
225 wdt->base = devm_ioremap_resource(dev, res);
226 if (IS_ERR(wdt->base))
227 return PTR_ERR(wdt->base);
229 clk = devm_clk_get(dev, NULL);
231 dev_err(dev, "failed to get input clock\n");
235 ret = clk_prepare_enable(clk);
237 dev_err(dev, "failed to setup clock\n");
240 ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk);
245 * We use the clock rate to calculate the max timeout, so ensure it's
246 * not zero to avoid a divide-by-zero exception.
248 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
249 * that it would bite before a second elapses it's usefulness is
250 * limited. Bail if this is the case.
252 wdt->rate = clk_get_rate(clk);
253 if (wdt->rate == 0 ||
254 wdt->rate > 0x10000000U) {
255 dev_err(dev, "invalid clock rate\n");
259 /* check if there is pretimeout support */
260 irq = platform_get_irq_optional(pdev, 0);
261 if (data->pretimeout && irq > 0) {
262 ret = devm_request_irq(dev, irq, qcom_wdt_isr, 0,
263 "wdt_bark", &wdt->wdd);
267 wdt->wdd.info = &qcom_wdt_pt_info;
268 wdt->wdd.pretimeout = 1;
270 if (irq == -EPROBE_DEFER)
271 return -EPROBE_DEFER;
273 wdt->wdd.info = &qcom_wdt_info;
276 wdt->wdd.ops = &qcom_wdt_ops;
277 wdt->wdd.min_timeout = 1;
278 wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
279 wdt->wdd.parent = dev;
280 wdt->layout = data->offset;
282 if (readl(wdt_addr(wdt, WDT_STS)) & 1)
283 wdt->wdd.bootstatus = WDIOF_CARDRESET;
286 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
287 * default, unless the max timeout is less than 30 seconds, then use
290 wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
291 watchdog_init_timeout(&wdt->wdd, 0, dev);
294 * If WDT is already running, call WDT start which
295 * will stop the WDT, set timeouts as bootloader
296 * might use different ones and set running bit
297 * to inform the WDT subsystem to ping the WDT
299 if (qcom_wdt_is_running(&wdt->wdd)) {
300 qcom_wdt_start(&wdt->wdd);
301 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
304 ret = devm_watchdog_register_device(dev, &wdt->wdd);
308 platform_set_drvdata(pdev, wdt);
312 static int __maybe_unused qcom_wdt_suspend(struct device *dev)
314 struct qcom_wdt *wdt = dev_get_drvdata(dev);
316 if (watchdog_active(&wdt->wdd))
317 qcom_wdt_stop(&wdt->wdd);
322 static int __maybe_unused qcom_wdt_resume(struct device *dev)
324 struct qcom_wdt *wdt = dev_get_drvdata(dev);
326 if (watchdog_active(&wdt->wdd))
327 qcom_wdt_start(&wdt->wdd);
332 static const struct dev_pm_ops qcom_wdt_pm_ops = {
333 SET_LATE_SYSTEM_SLEEP_PM_OPS(qcom_wdt_suspend, qcom_wdt_resume)
336 static const struct of_device_id qcom_wdt_of_table[] = {
337 { .compatible = "qcom,kpss-timer", .data = &match_data_apcs_tmr },
338 { .compatible = "qcom,scss-timer", .data = &match_data_apcs_tmr },
339 { .compatible = "qcom,kpss-wdt", .data = &match_data_kpss },
342 MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
344 static struct platform_driver qcom_watchdog_driver = {
345 .probe = qcom_wdt_probe,
347 .name = KBUILD_MODNAME,
348 .of_match_table = qcom_wdt_of_table,
349 .pm = &qcom_wdt_pm_ops,
352 module_platform_driver(qcom_watchdog_driver);
354 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
355 MODULE_LICENSE("GPL v2");