blob: 094f096aee0f2ce5500ae5198b5e2464f65a81f1 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 */
David Brazdil0f672f62019-12-10 10:32:29 +00004#include <linux/bits.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005#include <linux/clk.h>
6#include <linux/delay.h>
David Brazdil0f672f62019-12-10 10:32:29 +00007#include <linux/interrupt.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#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
16enum wdt_reg {
17 WDT_RST,
18 WDT_EN,
19 WDT_STS,
20 WDT_BARK_TIME,
21 WDT_BITE_TIME,
22};
23
David Brazdil0f672f62019-12-10 10:32:29 +000024#define QCOM_WDT_ENABLE BIT(0)
David Brazdil0f672f62019-12-10 10:32:29 +000025
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026static const u32 reg_offset_data_apcs_tmr[] = {
27 [WDT_RST] = 0x38,
28 [WDT_EN] = 0x40,
29 [WDT_STS] = 0x44,
30 [WDT_BARK_TIME] = 0x4C,
31 [WDT_BITE_TIME] = 0x5C,
32};
33
34static const u32 reg_offset_data_kpss[] = {
35 [WDT_RST] = 0x4,
36 [WDT_EN] = 0x8,
37 [WDT_STS] = 0xC,
38 [WDT_BARK_TIME] = 0x10,
39 [WDT_BITE_TIME] = 0x14,
40};
41
42struct qcom_wdt {
43 struct watchdog_device wdd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 unsigned long rate;
45 void __iomem *base;
46 const u32 *layout;
47};
48
49static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
50{
51 return wdt->base + wdt->layout[reg];
52}
53
54static inline
55struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
56{
57 return container_of(wdd, struct qcom_wdt, wdd);
58}
59
David Brazdil0f672f62019-12-10 10:32:29 +000060static irqreturn_t qcom_wdt_isr(int irq, void *arg)
61{
62 struct watchdog_device *wdd = arg;
63
64 watchdog_notify_pretimeout(wdd);
65
66 return IRQ_HANDLED;
67}
68
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069static int qcom_wdt_start(struct watchdog_device *wdd)
70{
71 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
David Brazdil0f672f62019-12-10 10:32:29 +000072 unsigned int bark = wdd->timeout - wdd->pretimeout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073
74 writel(0, wdt_addr(wdt, WDT_EN));
75 writel(1, wdt_addr(wdt, WDT_RST));
David Brazdil0f672f62019-12-10 10:32:29 +000076 writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
Olivier Deprez0e641232021-09-23 10:07:05 +020078 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 return 0;
80}
81
82static int qcom_wdt_stop(struct watchdog_device *wdd)
83{
84 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
85
86 writel(0, wdt_addr(wdt, WDT_EN));
87 return 0;
88}
89
90static int qcom_wdt_ping(struct watchdog_device *wdd)
91{
92 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
93
94 writel(1, wdt_addr(wdt, WDT_RST));
95 return 0;
96}
97
98static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
99 unsigned int timeout)
100{
101 wdd->timeout = timeout;
102 return qcom_wdt_start(wdd);
103}
104
David Brazdil0f672f62019-12-10 10:32:29 +0000105static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd,
106 unsigned int timeout)
107{
108 wdd->pretimeout = timeout;
109 return qcom_wdt_start(wdd);
110}
111
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
113 void *data)
114{
115 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
116 u32 timeout;
117
118 /*
119 * Trigger watchdog bite:
120 * Setup BITE_TIME to be 128ms, and enable WDT.
121 */
122 timeout = 128 * wdt->rate / 1000;
123
124 writel(0, wdt_addr(wdt, WDT_EN));
125 writel(1, wdt_addr(wdt, WDT_RST));
126 writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
127 writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
David Brazdil0f672f62019-12-10 10:32:29 +0000128 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129
130 /*
131 * Actually make sure the above sequence hits hardware before sleeping.
132 */
133 wmb();
134
Olivier Deprez0e641232021-09-23 10:07:05 +0200135 mdelay(150);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 return 0;
137}
138
139static const struct watchdog_ops qcom_wdt_ops = {
140 .start = qcom_wdt_start,
141 .stop = qcom_wdt_stop,
142 .ping = qcom_wdt_ping,
143 .set_timeout = qcom_wdt_set_timeout,
David Brazdil0f672f62019-12-10 10:32:29 +0000144 .set_pretimeout = qcom_wdt_set_pretimeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145 .restart = qcom_wdt_restart,
146 .owner = THIS_MODULE,
147};
148
149static const struct watchdog_info qcom_wdt_info = {
150 .options = WDIOF_KEEPALIVEPING
151 | WDIOF_MAGICCLOSE
152 | WDIOF_SETTIMEOUT
153 | WDIOF_CARDRESET,
154 .identity = KBUILD_MODNAME,
155};
156
David Brazdil0f672f62019-12-10 10:32:29 +0000157static const struct watchdog_info qcom_wdt_pt_info = {
158 .options = WDIOF_KEEPALIVEPING
159 | WDIOF_MAGICCLOSE
160 | WDIOF_SETTIMEOUT
161 | WDIOF_PRETIMEOUT
162 | WDIOF_CARDRESET,
163 .identity = KBUILD_MODNAME,
164};
165
166static void qcom_clk_disable_unprepare(void *data)
167{
168 clk_disable_unprepare(data);
169}
170
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171static int qcom_wdt_probe(struct platform_device *pdev)
172{
David Brazdil0f672f62019-12-10 10:32:29 +0000173 struct device *dev = &pdev->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 struct qcom_wdt *wdt;
175 struct resource *res;
David Brazdil0f672f62019-12-10 10:32:29 +0000176 struct device_node *np = dev->of_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177 const u32 *regs;
178 u32 percpu_offset;
David Brazdil0f672f62019-12-10 10:32:29 +0000179 int irq, ret;
180 struct clk *clk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181
David Brazdil0f672f62019-12-10 10:32:29 +0000182 regs = of_device_get_match_data(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183 if (!regs) {
David Brazdil0f672f62019-12-10 10:32:29 +0000184 dev_err(dev, "Unsupported QCOM WDT module\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000185 return -ENODEV;
186 }
187
David Brazdil0f672f62019-12-10 10:32:29 +0000188 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189 if (!wdt)
190 return -ENOMEM;
191
192 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 if (!res)
194 return -ENOMEM;
195
196 /* We use CPU0's DGT for the watchdog */
197 if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
198 percpu_offset = 0;
199
200 res->start += percpu_offset;
201 res->end += percpu_offset;
202
David Brazdil0f672f62019-12-10 10:32:29 +0000203 wdt->base = devm_ioremap_resource(dev, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204 if (IS_ERR(wdt->base))
205 return PTR_ERR(wdt->base);
206
David Brazdil0f672f62019-12-10 10:32:29 +0000207 clk = devm_clk_get(dev, NULL);
208 if (IS_ERR(clk)) {
209 dev_err(dev, "failed to get input clock\n");
210 return PTR_ERR(clk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000211 }
212
David Brazdil0f672f62019-12-10 10:32:29 +0000213 ret = clk_prepare_enable(clk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +0000215 dev_err(dev, "failed to setup clock\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 return ret;
217 }
David Brazdil0f672f62019-12-10 10:32:29 +0000218 ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk);
219 if (ret)
220 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221
222 /*
223 * We use the clock rate to calculate the max timeout, so ensure it's
224 * not zero to avoid a divide-by-zero exception.
225 *
226 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
227 * that it would bite before a second elapses it's usefulness is
228 * limited. Bail if this is the case.
229 */
David Brazdil0f672f62019-12-10 10:32:29 +0000230 wdt->rate = clk_get_rate(clk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231 if (wdt->rate == 0 ||
232 wdt->rate > 0x10000000U) {
David Brazdil0f672f62019-12-10 10:32:29 +0000233 dev_err(dev, "invalid clock rate\n");
234 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235 }
236
David Brazdil0f672f62019-12-10 10:32:29 +0000237 /* check if there is pretimeout support */
Olivier Deprez0e641232021-09-23 10:07:05 +0200238 irq = platform_get_irq_optional(pdev, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000239 if (irq > 0) {
240 ret = devm_request_irq(dev, irq, qcom_wdt_isr,
241 IRQF_TRIGGER_RISING,
242 "wdt_bark", &wdt->wdd);
243 if (ret)
244 return ret;
245
246 wdt->wdd.info = &qcom_wdt_pt_info;
247 wdt->wdd.pretimeout = 1;
248 } else {
249 if (irq == -EPROBE_DEFER)
250 return -EPROBE_DEFER;
251
252 wdt->wdd.info = &qcom_wdt_info;
253 }
254
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 wdt->wdd.ops = &qcom_wdt_ops;
256 wdt->wdd.min_timeout = 1;
257 wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
David Brazdil0f672f62019-12-10 10:32:29 +0000258 wdt->wdd.parent = dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 wdt->layout = regs;
260
261 if (readl(wdt_addr(wdt, WDT_STS)) & 1)
262 wdt->wdd.bootstatus = WDIOF_CARDRESET;
263
264 /*
265 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
266 * default, unless the max timeout is less than 30 seconds, then use
267 * the max instead.
268 */
269 wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
David Brazdil0f672f62019-12-10 10:32:29 +0000270 watchdog_init_timeout(&wdt->wdd, 0, dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271
David Brazdil0f672f62019-12-10 10:32:29 +0000272 ret = devm_watchdog_register_device(dev, &wdt->wdd);
273 if (ret)
274 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275
276 platform_set_drvdata(pdev, wdt);
277 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278}
279
David Brazdil0f672f62019-12-10 10:32:29 +0000280static int __maybe_unused qcom_wdt_suspend(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281{
David Brazdil0f672f62019-12-10 10:32:29 +0000282 struct qcom_wdt *wdt = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283
David Brazdil0f672f62019-12-10 10:32:29 +0000284 if (watchdog_active(&wdt->wdd))
285 qcom_wdt_stop(&wdt->wdd);
286
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 return 0;
288}
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290static int __maybe_unused qcom_wdt_resume(struct device *dev)
291{
292 struct qcom_wdt *wdt = dev_get_drvdata(dev);
293
294 if (watchdog_active(&wdt->wdd))
295 qcom_wdt_start(&wdt->wdd);
296
297 return 0;
298}
299
300static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume);
301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302static const struct of_device_id qcom_wdt_of_table[] = {
303 { .compatible = "qcom,kpss-timer", .data = reg_offset_data_apcs_tmr },
304 { .compatible = "qcom,scss-timer", .data = reg_offset_data_apcs_tmr },
305 { .compatible = "qcom,kpss-wdt", .data = reg_offset_data_kpss },
306 { },
307};
308MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
309
310static struct platform_driver qcom_watchdog_driver = {
311 .probe = qcom_wdt_probe,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312 .driver = {
313 .name = KBUILD_MODNAME,
314 .of_match_table = qcom_wdt_of_table,
David Brazdil0f672f62019-12-10 10:32:29 +0000315 .pm = &qcom_wdt_pm_ops,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 },
317};
318module_platform_driver(qcom_watchdog_driver);
319
320MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
321MODULE_LICENSE("GPL v2");