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)
25 #define QCOM_WDT_ENABLE_IRQ BIT(1)
27 static const u32 reg_offset_data_apcs_tmr
[] = {
31 [WDT_BARK_TIME
] = 0x4C,
32 [WDT_BITE_TIME
] = 0x5C,
35 static const u32 reg_offset_data_kpss
[] = {
39 [WDT_BARK_TIME
] = 0x10,
40 [WDT_BITE_TIME
] = 0x14,
43 struct qcom_wdt_match_data
{
49 struct watchdog_device wdd
;
55 static void __iomem
*wdt_addr(struct qcom_wdt
*wdt
, enum wdt_reg reg
)
57 return wdt
->base
+ wdt
->layout
[reg
];
61 struct qcom_wdt
*to_qcom_wdt(struct watchdog_device
*wdd
)
63 return container_of(wdd
, struct qcom_wdt
, wdd
);
66 static inline int qcom_get_enable(struct watchdog_device
*wdd
)
68 int enable
= QCOM_WDT_ENABLE
;
71 enable
|= QCOM_WDT_ENABLE_IRQ
;
76 static irqreturn_t
qcom_wdt_isr(int irq
, void *arg
)
78 struct watchdog_device
*wdd
= arg
;
80 watchdog_notify_pretimeout(wdd
);
85 static int qcom_wdt_start(struct watchdog_device
*wdd
)
87 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
88 unsigned int bark
= wdd
->timeout
- wdd
->pretimeout
;
90 writel(0, wdt_addr(wdt
, WDT_EN
));
91 writel(1, wdt_addr(wdt
, WDT_RST
));
92 writel(bark
* wdt
->rate
, wdt_addr(wdt
, WDT_BARK_TIME
));
93 writel(wdd
->timeout
* wdt
->rate
, wdt_addr(wdt
, WDT_BITE_TIME
));
94 writel(qcom_get_enable(wdd
), wdt_addr(wdt
, WDT_EN
));
98 static int qcom_wdt_stop(struct watchdog_device
*wdd
)
100 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
102 writel(0, wdt_addr(wdt
, WDT_EN
));
106 static int qcom_wdt_ping(struct watchdog_device
*wdd
)
108 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
110 writel(1, wdt_addr(wdt
, WDT_RST
));
114 static int qcom_wdt_set_timeout(struct watchdog_device
*wdd
,
115 unsigned int timeout
)
117 wdd
->timeout
= timeout
;
118 return qcom_wdt_start(wdd
);
121 static int qcom_wdt_set_pretimeout(struct watchdog_device
*wdd
,
122 unsigned int timeout
)
124 wdd
->pretimeout
= timeout
;
125 return qcom_wdt_start(wdd
);
128 static int qcom_wdt_restart(struct watchdog_device
*wdd
, unsigned long action
,
131 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
135 * Trigger watchdog bite:
136 * Setup BITE_TIME to be 128ms, and enable WDT.
138 timeout
= 128 * wdt
->rate
/ 1000;
140 writel(0, wdt_addr(wdt
, WDT_EN
));
141 writel(1, wdt_addr(wdt
, WDT_RST
));
142 writel(timeout
, wdt_addr(wdt
, WDT_BARK_TIME
));
143 writel(timeout
, wdt_addr(wdt
, WDT_BITE_TIME
));
144 writel(QCOM_WDT_ENABLE
, wdt_addr(wdt
, WDT_EN
));
147 * Actually make sure the above sequence hits hardware before sleeping.
155 static const struct watchdog_ops qcom_wdt_ops
= {
156 .start
= qcom_wdt_start
,
157 .stop
= qcom_wdt_stop
,
158 .ping
= qcom_wdt_ping
,
159 .set_timeout
= qcom_wdt_set_timeout
,
160 .set_pretimeout
= qcom_wdt_set_pretimeout
,
161 .restart
= qcom_wdt_restart
,
162 .owner
= THIS_MODULE
,
165 static const struct watchdog_info qcom_wdt_info
= {
166 .options
= WDIOF_KEEPALIVEPING
170 .identity
= KBUILD_MODNAME
,
173 static const struct watchdog_info qcom_wdt_pt_info
= {
174 .options
= WDIOF_KEEPALIVEPING
179 .identity
= KBUILD_MODNAME
,
182 static void qcom_clk_disable_unprepare(void *data
)
184 clk_disable_unprepare(data
);
187 static const struct qcom_wdt_match_data match_data_apcs_tmr
= {
188 .offset
= reg_offset_data_apcs_tmr
,
192 static const struct qcom_wdt_match_data match_data_kpss
= {
193 .offset
= reg_offset_data_kpss
,
197 static int qcom_wdt_probe(struct platform_device
*pdev
)
199 struct device
*dev
= &pdev
->dev
;
200 struct qcom_wdt
*wdt
;
201 struct resource
*res
;
202 struct device_node
*np
= dev
->of_node
;
203 const struct qcom_wdt_match_data
*data
;
208 data
= of_device_get_match_data(dev
);
210 dev_err(dev
, "Unsupported QCOM WDT module\n");
214 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
218 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
222 /* We use CPU0's DGT for the watchdog */
223 if (of_property_read_u32(np
, "cpu-offset", &percpu_offset
))
226 res
->start
+= percpu_offset
;
227 res
->end
+= percpu_offset
;
229 wdt
->base
= devm_ioremap_resource(dev
, res
);
230 if (IS_ERR(wdt
->base
))
231 return PTR_ERR(wdt
->base
);
233 clk
= devm_clk_get(dev
, NULL
);
235 dev_err(dev
, "failed to get input clock\n");
239 ret
= clk_prepare_enable(clk
);
241 dev_err(dev
, "failed to setup clock\n");
244 ret
= devm_add_action_or_reset(dev
, qcom_clk_disable_unprepare
, clk
);
249 * We use the clock rate to calculate the max timeout, so ensure it's
250 * not zero to avoid a divide-by-zero exception.
252 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
253 * that it would bite before a second elapses it's usefulness is
254 * limited. Bail if this is the case.
256 wdt
->rate
= clk_get_rate(clk
);
257 if (wdt
->rate
== 0 ||
258 wdt
->rate
> 0x10000000U
) {
259 dev_err(dev
, "invalid clock rate\n");
263 /* check if there is pretimeout support */
264 irq
= platform_get_irq_optional(pdev
, 0);
265 if (data
->pretimeout
&& irq
> 0) {
266 ret
= devm_request_irq(dev
, irq
, qcom_wdt_isr
, 0,
267 "wdt_bark", &wdt
->wdd
);
271 wdt
->wdd
.info
= &qcom_wdt_pt_info
;
272 wdt
->wdd
.pretimeout
= 1;
274 if (irq
== -EPROBE_DEFER
)
275 return -EPROBE_DEFER
;
277 wdt
->wdd
.info
= &qcom_wdt_info
;
280 wdt
->wdd
.ops
= &qcom_wdt_ops
;
281 wdt
->wdd
.min_timeout
= 1;
282 wdt
->wdd
.max_timeout
= 0x10000000U
/ wdt
->rate
;
283 wdt
->wdd
.parent
= dev
;
284 wdt
->layout
= data
->offset
;
286 if (readl(wdt_addr(wdt
, WDT_STS
)) & 1)
287 wdt
->wdd
.bootstatus
= WDIOF_CARDRESET
;
290 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
291 * default, unless the max timeout is less than 30 seconds, then use
294 wdt
->wdd
.timeout
= min(wdt
->wdd
.max_timeout
, 30U);
295 watchdog_init_timeout(&wdt
->wdd
, 0, dev
);
297 ret
= devm_watchdog_register_device(dev
, &wdt
->wdd
);
301 platform_set_drvdata(pdev
, wdt
);
305 static int __maybe_unused
qcom_wdt_suspend(struct device
*dev
)
307 struct qcom_wdt
*wdt
= dev_get_drvdata(dev
);
309 if (watchdog_active(&wdt
->wdd
))
310 qcom_wdt_stop(&wdt
->wdd
);
315 static int __maybe_unused
qcom_wdt_resume(struct device
*dev
)
317 struct qcom_wdt
*wdt
= dev_get_drvdata(dev
);
319 if (watchdog_active(&wdt
->wdd
))
320 qcom_wdt_start(&wdt
->wdd
);
325 static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops
, qcom_wdt_suspend
, qcom_wdt_resume
);
327 static const struct of_device_id qcom_wdt_of_table
[] = {
328 { .compatible
= "qcom,kpss-timer", .data
= &match_data_apcs_tmr
},
329 { .compatible
= "qcom,scss-timer", .data
= &match_data_apcs_tmr
},
330 { .compatible
= "qcom,kpss-wdt", .data
= &match_data_kpss
},
333 MODULE_DEVICE_TABLE(of
, qcom_wdt_of_table
);
335 static struct platform_driver qcom_watchdog_driver
= {
336 .probe
= qcom_wdt_probe
,
338 .name
= KBUILD_MODNAME
,
339 .of_match_table
= qcom_wdt_of_table
,
340 .pm
= &qcom_wdt_pm_ops
,
343 module_platform_driver(qcom_watchdog_driver
);
345 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
346 MODULE_LICENSE("GPL v2");