summaryrefslogtreecommitdiff
path: root/target/linux/gemini/patches-3.10/110-watchdog-add-gemini_wdt-driver.patch
blob: 31a68964233752077c57700d929adff10597bea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
--- /dev/null
+++ b/drivers/watchdog/gemini_wdt.c
@@ -0,0 +1,378 @@
+/*
+ *  Watchdog driver for Cortina Systems Gemini SoC
+ *
+ *  Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/fs.h>
+#include <linux/uaccess.h>
+#include <linux/miscdevice.h>
+#include <linux/platform_device.h>
+#include <linux/watchdog.h>
+#include <linux/slab.h>
+
+#define GEMINI_WDCOUNTER	0x0
+#define GEMINI_WDLOAD		0x4
+#define GEMINI_WDRESTART	0x8
+
+#define WDRESTART_MAGIC		0x5AB9
+
+#define GEMINI_WDCR		0xC
+
+#define WDCR_CLOCK_5MHZ		(1 << 4)
+#define WDCR_SYS_RST		(1 << 1)
+#define WDCR_ENABLE		(1 << 0)
+
+#define WDT_CLOCK		5000000		/* 5 MHz */
+#define WDT_DEFAULT_TIMEOUT	13
+#define WDT_MAX_TIMEOUT		(0xFFFFFFFF / WDT_CLOCK)
+
+/* status bits */
+#define WDT_ACTIVE		0
+#define WDT_OK_TO_CLOSE		1
+
+static unsigned int timeout = WDT_DEFAULT_TIMEOUT;
+static int nowayout = WATCHDOG_NOWAYOUT;
+
+static DEFINE_SPINLOCK(gemini_wdt_lock);
+
+static struct platform_device *gemini_wdt_dev;
+
+struct gemini_wdt_struct {
+	struct resource		*res;
+	struct device		*dev;
+	void __iomem		*base;
+	unsigned long		status;
+};
+
+static struct watchdog_info gemini_wdt_info = {
+	.identity	= "Gemini watchdog",
+	.options	= WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
+			  WDIOF_SETTIMEOUT,
+};
+
+/* Disable the watchdog. */
+static void gemini_wdt_stop(struct gemini_wdt_struct *gemini_wdt)
+{
+	spin_lock(&gemini_wdt_lock);
+
+	__raw_writel(0, gemini_wdt->base + GEMINI_WDCR);
+
+	clear_bit(WDT_ACTIVE, &gemini_wdt->status);
+
+	spin_unlock(&gemini_wdt_lock);
+}
+
+/* Service the watchdog */
+static void gemini_wdt_service(struct gemini_wdt_struct *gemini_wdt)
+{
+	__raw_writel(WDRESTART_MAGIC, gemini_wdt->base + GEMINI_WDRESTART);
+}
+
+/* Enable and reset the watchdog. */
+static void gemini_wdt_start(struct gemini_wdt_struct *gemini_wdt)
+{
+	spin_lock(&gemini_wdt_lock);
+
+	__raw_writel(timeout * WDT_CLOCK, gemini_wdt->base + GEMINI_WDLOAD);
+
+	gemini_wdt_service(gemini_wdt);
+
+	/* set clock before enabling */
+	__raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST,
+			gemini_wdt->base + GEMINI_WDCR);
+
+	__raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST | WDCR_ENABLE,
+			gemini_wdt->base + GEMINI_WDCR);
+
+	set_bit(WDT_ACTIVE, &gemini_wdt->status);
+
+	spin_unlock(&gemini_wdt_lock);
+}
+
+/* Watchdog device is opened, and watchdog starts running. */
+static int gemini_wdt_open(struct inode *inode, struct file *file)
+{
+	struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(gemini_wdt_dev);
+
+	if (test_bit(WDT_ACTIVE, &gemini_wdt->status))
+		return -EBUSY;
+
+	file->private_data = gemini_wdt;
+
+	gemini_wdt_start(gemini_wdt);
+
+	return nonseekable_open(inode, file);
+}
+
+/* Close the watchdog device. */
+static int gemini_wdt_close(struct inode *inode, struct file *file)
+{
+	struct gemini_wdt_struct *gemini_wdt = file->private_data;
+
+	/* Disable the watchdog if possible */
+	if (test_bit(WDT_OK_TO_CLOSE, &gemini_wdt->status))
+		gemini_wdt_stop(gemini_wdt);
+	else
+		dev_warn(gemini_wdt->dev, "Device closed unexpectedly - timer will not stop\n");
+
+	return 0;
+}
+
+/* Handle commands from user-space. */
+static long gemini_wdt_ioctl(struct file *file, unsigned int cmd,
+			     unsigned long arg)
+{
+	struct gemini_wdt_struct *gemini_wdt = file->private_data;
+
+	int value;
+
+	switch (cmd) {
+	case WDIOC_KEEPALIVE:
+		gemini_wdt_service(gemini_wdt);
+		return 0;
+
+	case WDIOC_GETSUPPORT:
+		return copy_to_user((struct watchdog_info *)arg, &gemini_wdt_info,
+			sizeof(gemini_wdt_info)) ? -EFAULT : 0;
+
+	case WDIOC_SETTIMEOUT:
+		if (get_user(value, (int *)arg))
+			return -EFAULT;
+
+		if ((value < 1) || (value > WDT_MAX_TIMEOUT))
+			return -EINVAL;
+
+		timeout = value;
+
+		/* restart wdt to use new timeout */
+		gemini_wdt_stop(gemini_wdt);
+		gemini_wdt_start(gemini_wdt);
+
+		/* Fall through */
+	case WDIOC_GETTIMEOUT:
+		return put_user(timeout, (int *)arg);
+
+	case WDIOC_GETTIMELEFT:
+		value = __raw_readl(gemini_wdt->base + GEMINI_WDCOUNTER);
+		return put_user(value / WDT_CLOCK, (int *)arg);
+
+	default:
+		return -ENOTTY;
+	}
+}
+
+/* Refresh the watchdog whenever device is written to. */
+static ssize_t gemini_wdt_write(struct file *file, const char *data,
+						size_t len, loff_t *ppos)
+{
+	struct gemini_wdt_struct *gemini_wdt = file->private_data;
+
+	if (len) {
+		if (!nowayout) {
+			size_t i;
+
+			clear_bit(WDT_OK_TO_CLOSE, &gemini_wdt->status);
+			for (i = 0; i != len; i++) {
+				char c;
+
+				if (get_user(c, data + i))
+					return -EFAULT;
+				if (c == 'V')
+					set_bit(WDT_OK_TO_CLOSE,
+						&gemini_wdt->status);
+			}
+		}
+		gemini_wdt_service(gemini_wdt);
+	}
+
+	return len;
+}
+
+static const struct file_operations gemini_wdt_fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.unlocked_ioctl = gemini_wdt_ioctl,
+	.open		= gemini_wdt_open,
+	.release	= gemini_wdt_close,
+	.write		= gemini_wdt_write,
+};
+
+static struct miscdevice gemini_wdt_miscdev = {
+	.minor		= WATCHDOG_MINOR,
+	.name		= "watchdog",
+	.fops		= &gemini_wdt_fops,
+};
+
+static void gemini_wdt_shutdown(struct platform_device *pdev)
+{
+	struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
+
+	gemini_wdt_stop(gemini_wdt);
+}
+
+static int gemini_wdt_probe(struct platform_device *pdev)
+{
+	int ret;
+	int res_size;
+	struct resource *res;
+	void __iomem *base;
+	struct gemini_wdt_struct *gemini_wdt;
+	unsigned int reg;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		dev_err(&pdev->dev, "can't get device resources\n");
+		return -ENODEV;
+	}
+
+	res_size = resource_size(res);
+	if (!request_mem_region(res->start, res_size, res->name)) {
+		dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
+			res_size, res->start);
+		return -ENOMEM;
+	}
+
+	base = ioremap(res->start, res_size);
+	if (!base) {
+		dev_err(&pdev->dev, "ioremap failed\n");
+		ret = -EIO;
+		goto fail0;
+	}
+
+	gemini_wdt = kzalloc(sizeof(struct gemini_wdt_struct), GFP_KERNEL);
+	if (!gemini_wdt) {
+		dev_err(&pdev->dev, "can't allocate interface\n");
+		ret = -ENOMEM;
+		goto fail1;
+	}
+
+	/* Setup gemini_wdt driver structure */
+	gemini_wdt->base = base;
+	gemini_wdt->res = res;
+
+	/* Set up platform driver data */
+	platform_set_drvdata(pdev, gemini_wdt);
+	gemini_wdt_dev = pdev;
+
+	if (gemini_wdt_miscdev.parent) {
+		ret = -EBUSY;
+		goto fail2;
+	}
+
+	gemini_wdt_miscdev.parent = &pdev->dev;
+
+	reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
+	if (reg & WDCR_ENABLE) {
+		/* Watchdog was enabled by the bootloader, disable it. */
+		reg &= ~(WDCR_ENABLE);
+		__raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
+	}
+
+	ret = misc_register(&gemini_wdt_miscdev);
+	if (ret)
+		goto fail2;
+
+	return 0;
+
+fail2:
+	platform_set_drvdata(pdev, NULL);
+	kfree(gemini_wdt);
+fail1:
+	iounmap(base);
+fail0:
+	release_mem_region(res->start, res_size);
+
+	return ret;
+}
+
+static int gemini_wdt_remove(struct platform_device *pdev)
+{
+	struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
+
+	platform_set_drvdata(pdev, NULL);
+	misc_deregister(&gemini_wdt_miscdev);
+	gemini_wdt_dev = NULL;
+	iounmap(gemini_wdt->base);
+	release_mem_region(gemini_wdt->res->start, resource_size(gemini_wdt->res));
+
+	kfree(gemini_wdt);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int gemini_wdt_suspend(struct platform_device *pdev, pm_message_t message)
+{
+	struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
+	unsigned int reg;
+
+	reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
+	reg &= ~(WDCR_WDENABLE);
+	__raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
+
+	return 0;
+}
+
+static int gemini_wdt_resume(struct platform_device *pdev)
+{
+	struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
+	unsigned int reg;
+
+	if (gemini_wdt->status) {
+		reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
+		reg |= WDCR_WDENABLE;
+		__raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
+	}
+
+	return 0;
+}
+#else
+#define gemini_wdt_suspend	NULL
+#define gemini_wdt_resume	NULL
+#endif
+
+static struct platform_driver gemini_wdt_driver = {
+	.probe		= gemini_wdt_probe,
+	.remove		= gemini_wdt_remove,
+	.shutdown	= gemini_wdt_shutdown,
+	.suspend	= gemini_wdt_suspend,
+	.resume		= gemini_wdt_resume,
+	.driver		= {
+		.name	= "gemini-wdt",
+		.owner	= THIS_MODULE,
+	},
+};
+
+static int __init gemini_wdt_init(void)
+{
+	return platform_driver_probe(&gemini_wdt_driver, gemini_wdt_probe);
+}
+
+static void __exit gemini_wdt_exit(void)
+{
+	platform_driver_unregister(&gemini_wdt_driver);
+}
+
+module_init(gemini_wdt_init);
+module_exit(gemini_wdt_exit);
+
+module_param(timeout, uint, 0);
+MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
+
+module_param(nowayout, int, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
+
+MODULE_AUTHOR("Paulius Zaleckas");
+MODULE_DESCRIPTION("Watchdog driver for Gemini");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
+MODULE_ALIAS("platform:gemini-wdt");
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -154,6 +154,16 @@ config 977_WATCHDOG
 
 	  Not sure? It's safe to say N.
 
+config GEMINI_WATCHDOG
+	tristate "Gemini watchdog"
+	depends on ARCH_GEMINI
+	help
+	  Say Y here if to include support for the watchdog timer
+	  embedded in the Cortina Systems Gemini family of devices.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called gemini_wdt.
+
 config IXP4XX_WATCHDOG
 	tristate "IXP4xx Watchdog"
 	depends on ARCH_IXP4XX
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -36,6 +36,7 @@ obj-$(CONFIG_OMAP_WATCHDOG) += omap_wdt.
 obj-$(CONFIG_TWL4030_WATCHDOG) += twl4030_wdt.o
 obj-$(CONFIG_21285_WATCHDOG) += wdt285.o
 obj-$(CONFIG_977_WATCHDOG) += wdt977.o
+obj-$(CONFIG_GEMINI_WATCHDOG) += gemini_wdt.o
 obj-$(CONFIG_IXP4XX_WATCHDOG) += ixp4xx_wdt.o
 obj-$(CONFIG_KS8695_WATCHDOG) += ks8695_wdt.o
 obj-$(CONFIG_S3C2410_WATCHDOG) += s3c2410_wdt.o