Blame view

drivers/watchdog/qcom-wdt.c 7.79 KB
97fb5e8d9   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
2
  /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
3
   */
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
4
  #include <linux/bits.h>
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
5
  #include <linux/clk.h>
05e487d90   Josh Cartwright   watchdog: qcom: r...
6
  #include <linux/delay.h>
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
7
  #include <linux/interrupt.h>
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
8
9
10
11
12
13
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
  #include <linux/watchdog.h>
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
14
  #include <linux/of_device.h>
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
15

f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
16
17
18
19
  enum wdt_reg {
  	WDT_RST,
  	WDT_EN,
  	WDT_STS,
10073a205   Matthew McClintock   watchdog: qcom: c...
20
  	WDT_BARK_TIME,
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
21
22
  	WDT_BITE_TIME,
  };
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
23
24
  #define QCOM_WDT_ENABLE		BIT(0)
  #define QCOM_WDT_ENABLE_IRQ	BIT(1)
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
25
26
27
28
  static const u32 reg_offset_data_apcs_tmr[] = {
  	[WDT_RST] = 0x38,
  	[WDT_EN] = 0x40,
  	[WDT_STS] = 0x44,
10073a205   Matthew McClintock   watchdog: qcom: c...
29
  	[WDT_BARK_TIME] = 0x4C,
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
30
31
32
33
34
35
36
  	[WDT_BITE_TIME] = 0x5C,
  };
  
  static const u32 reg_offset_data_kpss[] = {
  	[WDT_RST] = 0x4,
  	[WDT_EN] = 0x8,
  	[WDT_STS] = 0xC,
10073a205   Matthew McClintock   watchdog: qcom: c...
37
  	[WDT_BARK_TIME] = 0x10,
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
38
39
  	[WDT_BITE_TIME] = 0x14,
  };
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
40

000de5417   Ansuel Smith   watchdog: qcom-wd...
41
42
43
44
  struct qcom_wdt_match_data {
  	const u32 *offset;
  	bool pretimeout;
  };
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
45
46
  struct qcom_wdt {
  	struct watchdog_device	wdd;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
47
48
  	unsigned long		rate;
  	void __iomem		*base;
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
49
  	const u32		*layout;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
50
  };
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
51
52
53
54
  static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
  {
  	return wdt->base + wdt->layout[reg];
  }
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
55
56
57
58
59
  static inline
  struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
  {
  	return container_of(wdd, struct qcom_wdt, wdd);
  }
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  static inline int qcom_get_enable(struct watchdog_device *wdd)
  {
  	int enable = QCOM_WDT_ENABLE;
  
  	if (wdd->pretimeout)
  		enable |= QCOM_WDT_ENABLE_IRQ;
  
  	return enable;
  }
  
  static irqreturn_t qcom_wdt_isr(int irq, void *arg)
  {
  	struct watchdog_device *wdd = arg;
  
  	watchdog_notify_pretimeout(wdd);
  
  	return IRQ_HANDLED;
  }
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
78
79
80
  static int qcom_wdt_start(struct watchdog_device *wdd)
  {
  	struct qcom_wdt *wdt = to_qcom_wdt(wdd);
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
81
  	unsigned int bark = wdd->timeout - wdd->pretimeout;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
82

f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
83
84
  	writel(0, wdt_addr(wdt, WDT_EN));
  	writel(1, wdt_addr(wdt, WDT_RST));
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
85
  	writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
86
  	writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
87
  	writel(qcom_get_enable(wdd), wdt_addr(wdt, WDT_EN));
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
88
89
90
91
92
93
  	return 0;
  }
  
  static int qcom_wdt_stop(struct watchdog_device *wdd)
  {
  	struct qcom_wdt *wdt = to_qcom_wdt(wdd);
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
94
  	writel(0, wdt_addr(wdt, WDT_EN));
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
95
96
97
98
99
100
  	return 0;
  }
  
  static int qcom_wdt_ping(struct watchdog_device *wdd)
  {
  	struct qcom_wdt *wdt = to_qcom_wdt(wdd);
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
101
  	writel(1, wdt_addr(wdt, WDT_RST));
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
102
103
104
105
106
107
108
109
110
  	return 0;
  }
  
  static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
  				unsigned int timeout)
  {
  	wdd->timeout = timeout;
  	return qcom_wdt_start(wdd);
  }
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
111
112
113
114
115
116
  static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd,
  				   unsigned int timeout)
  {
  	wdd->pretimeout = timeout;
  	return qcom_wdt_start(wdd);
  }
4d8b229d5   Guenter Roeck   watchdog: Add 'ac...
117
118
  static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  			    void *data)
05e487d90   Josh Cartwright   watchdog: qcom: r...
119
  {
80969a68f   Damien Riegel   watchdog: qcom-wd...
120
  	struct qcom_wdt *wdt = to_qcom_wdt(wdd);
05e487d90   Josh Cartwright   watchdog: qcom: r...
121
122
123
124
125
126
127
  	u32 timeout;
  
  	/*
  	 * Trigger watchdog bite:
  	 *    Setup BITE_TIME to be 128ms, and enable WDT.
  	 */
  	timeout = 128 * wdt->rate / 1000;
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
128
129
  	writel(0, wdt_addr(wdt, WDT_EN));
  	writel(1, wdt_addr(wdt, WDT_RST));
10073a205   Matthew McClintock   watchdog: qcom: c...
130
  	writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
131
  	writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
132
  	writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN));
05e487d90   Josh Cartwright   watchdog: qcom: r...
133
134
135
136
137
138
139
  
  	/*
  	 * Actually make sure the above sequence hits hardware before sleeping.
  	 */
  	wmb();
  
  	msleep(150);
80969a68f   Damien Riegel   watchdog: qcom-wd...
140
  	return 0;
05e487d90   Josh Cartwright   watchdog: qcom: r...
141
  }
80969a68f   Damien Riegel   watchdog: qcom-wd...
142
143
144
145
146
  static const struct watchdog_ops qcom_wdt_ops = {
  	.start		= qcom_wdt_start,
  	.stop		= qcom_wdt_stop,
  	.ping		= qcom_wdt_ping,
  	.set_timeout	= qcom_wdt_set_timeout,
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
147
  	.set_pretimeout	= qcom_wdt_set_pretimeout,
80969a68f   Damien Riegel   watchdog: qcom-wd...
148
149
150
151
152
153
154
  	.restart        = qcom_wdt_restart,
  	.owner		= THIS_MODULE,
  };
  
  static const struct watchdog_info qcom_wdt_info = {
  	.options	= WDIOF_KEEPALIVEPING
  			| WDIOF_MAGICCLOSE
b6ef36d2c   Guenter Roeck   watchdog: qcom: R...
155
156
  			| WDIOF_SETTIMEOUT
  			| WDIOF_CARDRESET,
80969a68f   Damien Riegel   watchdog: qcom-wd...
157
158
  	.identity	= KBUILD_MODNAME,
  };
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
159
160
161
162
163
164
165
166
  static const struct watchdog_info qcom_wdt_pt_info = {
  	.options	= WDIOF_KEEPALIVEPING
  			| WDIOF_MAGICCLOSE
  			| WDIOF_SETTIMEOUT
  			| WDIOF_PRETIMEOUT
  			| WDIOF_CARDRESET,
  	.identity	= KBUILD_MODNAME,
  };
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
167
168
169
170
  static void qcom_clk_disable_unprepare(void *data)
  {
  	clk_disable_unprepare(data);
  }
000de5417   Ansuel Smith   watchdog: qcom-wd...
171
172
173
174
175
176
177
178
179
  static const struct qcom_wdt_match_data match_data_apcs_tmr = {
  	.offset = reg_offset_data_apcs_tmr,
  	.pretimeout = false,
  };
  
  static const struct qcom_wdt_match_data match_data_kpss = {
  	.offset = reg_offset_data_kpss,
  	.pretimeout = true,
  };
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
180
181
  static int qcom_wdt_probe(struct platform_device *pdev)
  {
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
182
  	struct device *dev = &pdev->dev;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
183
184
  	struct qcom_wdt *wdt;
  	struct resource *res;
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
185
  	struct device_node *np = dev->of_node;
000de5417   Ansuel Smith   watchdog: qcom-wd...
186
  	const struct qcom_wdt_match_data *data;
0dfd582e0   Mathieu Olivari   watchdog: qcom: u...
187
  	u32 percpu_offset;
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
188
  	int irq, ret;
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
189
  	struct clk *clk;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
190

000de5417   Ansuel Smith   watchdog: qcom-wd...
191
192
  	data = of_device_get_match_data(dev);
  	if (!data) {
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
193
194
  		dev_err(dev, "Unsupported QCOM WDT module
  ");
f0d9d0f4b   Matthew McClintock   watchdog: qcom: a...
195
196
  		return -ENODEV;
  	}
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
197
  	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
198
199
200
201
  	if (!wdt)
  		return -ENOMEM;
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
15210ad18   Fabio Estevam   watchdog: qcom: C...
202
203
  	if (!res)
  		return -ENOMEM;
0dfd582e0   Mathieu Olivari   watchdog: qcom: u...
204
205
206
207
208
209
210
  
  	/* We use CPU0's DGT for the watchdog */
  	if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  		percpu_offset = 0;
  
  	res->start += percpu_offset;
  	res->end += percpu_offset;
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
211
  	wdt->base = devm_ioremap_resource(dev, res);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
212
213
  	if (IS_ERR(wdt->base))
  		return PTR_ERR(wdt->base);
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
214
215
  	clk = devm_clk_get(dev, NULL);
  	if (IS_ERR(clk)) {
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
216
217
  		dev_err(dev, "failed to get input clock
  ");
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
218
  		return PTR_ERR(clk);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
219
  	}
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
220
  	ret = clk_prepare_enable(clk);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
221
  	if (ret) {
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
222
223
  		dev_err(dev, "failed to setup clock
  ");
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
224
225
  		return ret;
  	}
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
226
  	ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk);
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
227
228
  	if (ret)
  		return ret;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
229
230
231
232
233
234
235
236
237
  
  	/*
  	 * We use the clock rate to calculate the max timeout, so ensure it's
  	 * not zero to avoid a divide-by-zero exception.
  	 *
  	 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
  	 * that it would bite before a second elapses it's usefulness is
  	 * limited.  Bail if this is the case.
  	 */
52a142140   Jorge Ramirez-Ortiz   watchdog: qcom: r...
238
  	wdt->rate = clk_get_rate(clk);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
239
240
  	if (wdt->rate == 0 ||
  	    wdt->rate > 0x10000000U) {
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
241
242
243
  		dev_err(dev, "invalid clock rate
  ");
  		return -EINVAL;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
244
  	}
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
245
  	/* check if there is pretimeout support */
e0b4f4e0c   Sai Prakash Ranjan   watchdog: qcom: U...
246
  	irq = platform_get_irq_optional(pdev, 0);
000de5417   Ansuel Smith   watchdog: qcom-wd...
247
  	if (data->pretimeout && irq > 0) {
cc9cc794c   Stephen Boyd   watchdog: qcom: U...
248
  		ret = devm_request_irq(dev, irq, qcom_wdt_isr, 0,
36375491a   Jorge Ramirez-Ortiz   watchdog: qcom: s...
249
250
251
252
253
254
255
256
257
258
259
260
  				       "wdt_bark", &wdt->wdd);
  		if (ret)
  			return ret;
  
  		wdt->wdd.info = &qcom_wdt_pt_info;
  		wdt->wdd.pretimeout = 1;
  	} else {
  		if (irq == -EPROBE_DEFER)
  			return -EPROBE_DEFER;
  
  		wdt->wdd.info = &qcom_wdt_info;
  	}
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
261
262
263
  	wdt->wdd.ops = &qcom_wdt_ops;
  	wdt->wdd.min_timeout = 1;
  	wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
264
  	wdt->wdd.parent = dev;
000de5417   Ansuel Smith   watchdog: qcom-wd...
265
  	wdt->layout = data->offset;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
266

f06f35c66   Christian Lamparter   watchdog: qcom: f...
267
  	if (readl(wdt_addr(wdt, WDT_STS)) & 1)
b6ef36d2c   Guenter Roeck   watchdog: qcom: R...
268
  		wdt->wdd.bootstatus = WDIOF_CARDRESET;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
269
270
271
272
273
274
  	/*
  	 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  	 * default, unless the max timeout is less than 30 seconds, then use
  	 * the max instead.
  	 */
  	wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
275
  	watchdog_init_timeout(&wdt->wdd, 0, dev);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
276

bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
277
  	ret = devm_watchdog_register_device(dev, &wdt->wdd);
ccbf872a3   Wolfram Sang   watchdog: qcom-wd...
278
  	if (ret)
bba07e6ed   Guenter Roeck   watchdog: qcom-wd...
279
  		return ret;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
280
281
282
  
  	platform_set_drvdata(pdev, wdt);
  	return 0;
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
283
  }
671cdde36   Sai Prakash Ranjan   watchdog: qcom: A...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  static int __maybe_unused qcom_wdt_suspend(struct device *dev)
  {
  	struct qcom_wdt *wdt = dev_get_drvdata(dev);
  
  	if (watchdog_active(&wdt->wdd))
  		qcom_wdt_stop(&wdt->wdd);
  
  	return 0;
  }
  
  static int __maybe_unused qcom_wdt_resume(struct device *dev)
  {
  	struct qcom_wdt *wdt = dev_get_drvdata(dev);
  
  	if (watchdog_active(&wdt->wdd))
  		qcom_wdt_start(&wdt->wdd);
  
  	return 0;
  }
  
  static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume);
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
305
  static const struct of_device_id qcom_wdt_of_table[] = {
000de5417   Ansuel Smith   watchdog: qcom-wd...
306
307
308
  	{ .compatible = "qcom,kpss-timer", .data = &match_data_apcs_tmr },
  	{ .compatible = "qcom,scss-timer", .data = &match_data_apcs_tmr },
  	{ .compatible = "qcom,kpss-wdt", .data = &match_data_kpss },
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
309
310
311
312
313
314
  	{ },
  };
  MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
  
  static struct platform_driver qcom_watchdog_driver = {
  	.probe	= qcom_wdt_probe,
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
315
316
317
  	.driver	= {
  		.name		= KBUILD_MODNAME,
  		.of_match_table	= qcom_wdt_of_table,
671cdde36   Sai Prakash Ranjan   watchdog: qcom: A...
318
  		.pm		= &qcom_wdt_pm_ops,
1094ebe9d   Josh Cartwright   watchdog: qcom: a...
319
320
321
322
323
324
  	},
  };
  module_platform_driver(qcom_watchdog_driver);
  
  MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
  MODULE_LICENSE("GPL v2");