blob: ab7465d186fdaa6f849ee193be13f3772658fed4 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Josh Cartwright1094ebe2014-09-25 17:51:02 -05002/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
Josh Cartwright1094ebe2014-09-25 17:51:02 -05003 */
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +02004#include <linux/bits.h>
Josh Cartwright1094ebe2014-09-25 17:51:02 -05005#include <linux/clk.h>
Josh Cartwright05e487d2014-09-25 17:51:04 -05006#include <linux/delay.h>
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +02007#include <linux/interrupt.h>
Josh Cartwright1094ebe2014-09-25 17:51:02 -05008#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>
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070014#include <linux/of_device.h>
Josh Cartwright1094ebe2014-09-25 17:51:02 -050015
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070016enum wdt_reg {
17 WDT_RST,
18 WDT_EN,
19 WDT_STS,
Matthew McClintock10073a22016-06-28 11:35:21 -070020 WDT_BARK_TIME,
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070021 WDT_BITE_TIME,
22};
23
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +020024#define QCOM_WDT_ENABLE BIT(0)
25#define QCOM_WDT_ENABLE_IRQ BIT(1)
26
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070027static const u32 reg_offset_data_apcs_tmr[] = {
28 [WDT_RST] = 0x38,
29 [WDT_EN] = 0x40,
30 [WDT_STS] = 0x44,
Matthew McClintock10073a22016-06-28 11:35:21 -070031 [WDT_BARK_TIME] = 0x4C,
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070032 [WDT_BITE_TIME] = 0x5C,
33};
34
35static const u32 reg_offset_data_kpss[] = {
36 [WDT_RST] = 0x4,
37 [WDT_EN] = 0x8,
38 [WDT_STS] = 0xC,
Matthew McClintock10073a22016-06-28 11:35:21 -070039 [WDT_BARK_TIME] = 0x10,
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070040 [WDT_BITE_TIME] = 0x14,
41};
Josh Cartwright1094ebe2014-09-25 17:51:02 -050042
Ansuel Smith000de542020-02-04 20:56:48 +010043struct qcom_wdt_match_data {
44 const u32 *offset;
45 bool pretimeout;
46};
47
Josh Cartwright1094ebe2014-09-25 17:51:02 -050048struct qcom_wdt {
49 struct watchdog_device wdd;
Josh Cartwright1094ebe2014-09-25 17:51:02 -050050 unsigned long rate;
51 void __iomem *base;
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070052 const u32 *layout;
Josh Cartwright1094ebe2014-09-25 17:51:02 -050053};
54
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070055static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
56{
57 return wdt->base + wdt->layout[reg];
58}
59
Josh Cartwright1094ebe2014-09-25 17:51:02 -050060static inline
61struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
62{
63 return container_of(wdd, struct qcom_wdt, wdd);
64}
65
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +020066static inline int qcom_get_enable(struct watchdog_device *wdd)
67{
68 int enable = QCOM_WDT_ENABLE;
69
70 if (wdd->pretimeout)
71 enable |= QCOM_WDT_ENABLE_IRQ;
72
73 return enable;
74}
75
76static irqreturn_t qcom_wdt_isr(int irq, void *arg)
77{
78 struct watchdog_device *wdd = arg;
79
80 watchdog_notify_pretimeout(wdd);
81
82 return IRQ_HANDLED;
83}
84
Josh Cartwright1094ebe2014-09-25 17:51:02 -050085static int qcom_wdt_start(struct watchdog_device *wdd)
86{
87 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +020088 unsigned int bark = wdd->timeout - wdd->pretimeout;
Josh Cartwright1094ebe2014-09-25 17:51:02 -050089
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070090 writel(0, wdt_addr(wdt, WDT_EN));
91 writel(1, wdt_addr(wdt, WDT_RST));
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +020092 writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -070093 writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +020094 writel(qcom_get_enable(wdd), wdt_addr(wdt, WDT_EN));
Josh Cartwright1094ebe2014-09-25 17:51:02 -050095 return 0;
96}
97
98static int qcom_wdt_stop(struct watchdog_device *wdd)
99{
100 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
101
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -0700102 writel(0, wdt_addr(wdt, WDT_EN));
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500103 return 0;
104}
105
106static int qcom_wdt_ping(struct watchdog_device *wdd)
107{
108 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
109
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -0700110 writel(1, wdt_addr(wdt, WDT_RST));
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500111 return 0;
112}
113
114static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
115 unsigned int timeout)
116{
117 wdd->timeout = timeout;
118 return qcom_wdt_start(wdd);
119}
120
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200121static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd,
122 unsigned int timeout)
123{
124 wdd->pretimeout = timeout;
125 return qcom_wdt_start(wdd);
126}
127
Guenter Roeck4d8b2292016-02-26 17:32:49 -0800128static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
129 void *data)
Josh Cartwright05e487d2014-09-25 17:51:04 -0500130{
Damien Riegel80969a62015-11-16 12:28:09 -0500131 struct qcom_wdt *wdt = to_qcom_wdt(wdd);
Josh Cartwright05e487d2014-09-25 17:51:04 -0500132 u32 timeout;
133
134 /*
135 * Trigger watchdog bite:
136 * Setup BITE_TIME to be 128ms, and enable WDT.
137 */
138 timeout = 128 * wdt->rate / 1000;
139
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -0700140 writel(0, wdt_addr(wdt, WDT_EN));
141 writel(1, wdt_addr(wdt, WDT_RST));
Matthew McClintock10073a22016-06-28 11:35:21 -0700142 writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -0700143 writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200144 writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
Josh Cartwright05e487d2014-09-25 17:51:04 -0500145
146 /*
147 * Actually make sure the above sequence hits hardware before sleeping.
148 */
149 wmb();
150
151 msleep(150);
Damien Riegel80969a62015-11-16 12:28:09 -0500152 return 0;
Josh Cartwright05e487d2014-09-25 17:51:04 -0500153}
154
Damien Riegel80969a62015-11-16 12:28:09 -0500155static 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,
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200160 .set_pretimeout = qcom_wdt_set_pretimeout,
Damien Riegel80969a62015-11-16 12:28:09 -0500161 .restart = qcom_wdt_restart,
162 .owner = THIS_MODULE,
163};
164
165static const struct watchdog_info qcom_wdt_info = {
166 .options = WDIOF_KEEPALIVEPING
167 | WDIOF_MAGICCLOSE
Guenter Roeckb6ef36d2016-04-04 17:37:46 -0700168 | WDIOF_SETTIMEOUT
169 | WDIOF_CARDRESET,
Damien Riegel80969a62015-11-16 12:28:09 -0500170 .identity = KBUILD_MODNAME,
171};
172
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200173static const struct watchdog_info qcom_wdt_pt_info = {
174 .options = WDIOF_KEEPALIVEPING
175 | WDIOF_MAGICCLOSE
176 | WDIOF_SETTIMEOUT
177 | WDIOF_PRETIMEOUT
178 | WDIOF_CARDRESET,
179 .identity = KBUILD_MODNAME,
180};
181
Guenter Roeckbba07e62019-04-09 10:23:50 -0700182static void qcom_clk_disable_unprepare(void *data)
183{
184 clk_disable_unprepare(data);
185}
186
Ansuel Smith000de542020-02-04 20:56:48 +0100187static const struct qcom_wdt_match_data match_data_apcs_tmr = {
188 .offset = reg_offset_data_apcs_tmr,
189 .pretimeout = false,
190};
191
192static const struct qcom_wdt_match_data match_data_kpss = {
193 .offset = reg_offset_data_kpss,
194 .pretimeout = true,
195};
196
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500197static int qcom_wdt_probe(struct platform_device *pdev)
198{
Guenter Roeckbba07e62019-04-09 10:23:50 -0700199 struct device *dev = &pdev->dev;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500200 struct qcom_wdt *wdt;
201 struct resource *res;
Guenter Roeckbba07e62019-04-09 10:23:50 -0700202 struct device_node *np = dev->of_node;
Ansuel Smith000de542020-02-04 20:56:48 +0100203 const struct qcom_wdt_match_data *data;
Mathieu Olivari0dfd5822015-02-20 18:19:34 -0800204 u32 percpu_offset;
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200205 int irq, ret;
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200206 struct clk *clk;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500207
Ansuel Smith000de542020-02-04 20:56:48 +0100208 data = of_device_get_match_data(dev);
209 if (!data) {
Guenter Roeckbba07e62019-04-09 10:23:50 -0700210 dev_err(dev, "Unsupported QCOM WDT module\n");
Matthew McClintockf0d9d0f2016-06-29 10:50:01 -0700211 return -ENODEV;
212 }
213
Guenter Roeckbba07e62019-04-09 10:23:50 -0700214 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500215 if (!wdt)
216 return -ENOMEM;
217
218 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam15210ad2017-07-22 13:04:33 -0300219 if (!res)
220 return -ENOMEM;
Mathieu Olivari0dfd5822015-02-20 18:19:34 -0800221
222 /* We use CPU0's DGT for the watchdog */
223 if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
224 percpu_offset = 0;
225
226 res->start += percpu_offset;
227 res->end += percpu_offset;
228
Guenter Roeckbba07e62019-04-09 10:23:50 -0700229 wdt->base = devm_ioremap_resource(dev, res);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500230 if (IS_ERR(wdt->base))
231 return PTR_ERR(wdt->base);
232
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200233 clk = devm_clk_get(dev, NULL);
234 if (IS_ERR(clk)) {
Guenter Roeckbba07e62019-04-09 10:23:50 -0700235 dev_err(dev, "failed to get input clock\n");
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200236 return PTR_ERR(clk);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500237 }
238
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200239 ret = clk_prepare_enable(clk);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500240 if (ret) {
Guenter Roeckbba07e62019-04-09 10:23:50 -0700241 dev_err(dev, "failed to setup clock\n");
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500242 return ret;
243 }
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200244 ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk);
Guenter Roeckbba07e62019-04-09 10:23:50 -0700245 if (ret)
246 return ret;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500247
248 /*
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.
251 *
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.
255 */
Jorge Ramirez-Ortiz52a14212019-09-06 22:54:11 +0200256 wdt->rate = clk_get_rate(clk);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500257 if (wdt->rate == 0 ||
258 wdt->rate > 0x10000000U) {
Guenter Roeckbba07e62019-04-09 10:23:50 -0700259 dev_err(dev, "invalid clock rate\n");
260 return -EINVAL;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500261 }
262
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200263 /* check if there is pretimeout support */
Sai Prakash Ranjane0b4f4e2019-12-13 12:19:34 +0530264 irq = platform_get_irq_optional(pdev, 0);
Ansuel Smith000de542020-02-04 20:56:48 +0100265 if (data->pretimeout && irq > 0) {
Stephen Boydcc9cc792020-02-19 16:20:47 -0800266 ret = devm_request_irq(dev, irq, qcom_wdt_isr, 0,
Jorge Ramirez-Ortiz36375492019-09-06 22:54:10 +0200267 "wdt_bark", &wdt->wdd);
268 if (ret)
269 return ret;
270
271 wdt->wdd.info = &qcom_wdt_pt_info;
272 wdt->wdd.pretimeout = 1;
273 } else {
274 if (irq == -EPROBE_DEFER)
275 return -EPROBE_DEFER;
276
277 wdt->wdd.info = &qcom_wdt_info;
278 }
279
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500280 wdt->wdd.ops = &qcom_wdt_ops;
281 wdt->wdd.min_timeout = 1;
282 wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
Guenter Roeckbba07e62019-04-09 10:23:50 -0700283 wdt->wdd.parent = dev;
Ansuel Smith000de542020-02-04 20:56:48 +0100284 wdt->layout = data->offset;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500285
Christian Lamparterf06f35c2016-11-14 02:11:16 +0100286 if (readl(wdt_addr(wdt, WDT_STS)) & 1)
Guenter Roeckb6ef36d2016-04-04 17:37:46 -0700287 wdt->wdd.bootstatus = WDIOF_CARDRESET;
288
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500289 /*
290 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
291 * default, unless the max timeout is less than 30 seconds, then use
292 * the max instead.
293 */
294 wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
Guenter Roeckbba07e62019-04-09 10:23:50 -0700295 watchdog_init_timeout(&wdt->wdd, 0, dev);
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500296
Guenter Roeckbba07e62019-04-09 10:23:50 -0700297 ret = devm_watchdog_register_device(dev, &wdt->wdd);
Wolfram Sangccbf872a2019-05-18 23:27:48 +0200298 if (ret)
Guenter Roeckbba07e62019-04-09 10:23:50 -0700299 return ret;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500300
301 platform_set_drvdata(pdev, wdt);
302 return 0;
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500303}
304
Sai Prakash Ranjan671cdde2019-01-17 20:49:42 +0530305static int __maybe_unused qcom_wdt_suspend(struct device *dev)
306{
307 struct qcom_wdt *wdt = dev_get_drvdata(dev);
308
309 if (watchdog_active(&wdt->wdd))
310 qcom_wdt_stop(&wdt->wdd);
311
312 return 0;
313}
314
315static int __maybe_unused qcom_wdt_resume(struct device *dev)
316{
317 struct qcom_wdt *wdt = dev_get_drvdata(dev);
318
319 if (watchdog_active(&wdt->wdd))
320 qcom_wdt_start(&wdt->wdd);
321
322 return 0;
323}
324
325static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume);
326
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500327static const struct of_device_id qcom_wdt_of_table[] = {
Ansuel Smith000de542020-02-04 20:56:48 +0100328 { .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 },
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500331 { },
332};
333MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
334
335static struct platform_driver qcom_watchdog_driver = {
336 .probe = qcom_wdt_probe,
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500337 .driver = {
338 .name = KBUILD_MODNAME,
339 .of_match_table = qcom_wdt_of_table,
Sai Prakash Ranjan671cdde2019-01-17 20:49:42 +0530340 .pm = &qcom_wdt_pm_ops,
Josh Cartwright1094ebe2014-09-25 17:51:02 -0500341 },
342};
343module_platform_driver(qcom_watchdog_driver);
344
345MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
346MODULE_LICENSE("GPL v2");