summaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/adx_wdt.c
blob: 9d7d155364f8950f09d581135a94d7794f006558 (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
/*
 * Copyright (C) 2008-2009 Avionic Design GmbH
 *
 * 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/fs.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/watchdog.h>

#define WATCHDOG_NAME "adx-wdt"

/* register offsets */
#define	ADX_WDT_CONTROL		0x00
#define	ADX_WDT_CONTROL_ENABLE	(1 << 0)
#define	ADX_WDT_CONTROL_nRESET	(1 << 1)
#define	ADX_WDT_TIMEOUT		0x08

static struct platform_device *adx_wdt_dev;
static unsigned long driver_open;

#define	WDT_STATE_STOP	0
#define	WDT_STATE_START	1

struct adx_wdt {
	void __iomem *base;
	unsigned long timeout;
	unsigned int state;
	unsigned int wake;
	spinlock_t lock;
};

static struct watchdog_info adx_wdt_info = {
	.identity = "Avionic Design Xanthos Watchdog",
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
};

static void adx_wdt_start_locked(struct adx_wdt *wdt)
{
	u32 ctrl;

	ctrl = readl(wdt->base + ADX_WDT_CONTROL);
	ctrl |= ADX_WDT_CONTROL_ENABLE;
	writel(ctrl, wdt->base + ADX_WDT_CONTROL);
	wdt->state = WDT_STATE_START;
}

static void adx_wdt_start(struct adx_wdt *wdt)
{
	unsigned long flags;

	spin_lock_irqsave(&wdt->lock, flags);
	adx_wdt_start_locked(wdt);
	spin_unlock_irqrestore(&wdt->lock, flags);
}

static void adx_wdt_stop_locked(struct adx_wdt *wdt)
{
	u32 ctrl;

	ctrl = readl(wdt->base + ADX_WDT_CONTROL);
	ctrl &= ~ADX_WDT_CONTROL_ENABLE;
	writel(ctrl, wdt->base + ADX_WDT_CONTROL);
	wdt->state = WDT_STATE_STOP;
}

static void adx_wdt_stop(struct adx_wdt *wdt)
{
	unsigned long flags;

	spin_lock_irqsave(&wdt->lock, flags);
	adx_wdt_stop_locked(wdt);
	spin_unlock_irqrestore(&wdt->lock, flags);
}

static void adx_wdt_set_timeout(struct adx_wdt *wdt, unsigned long seconds)
{
	unsigned long timeout = seconds * 1000;
	unsigned long flags;
	unsigned int state;

	spin_lock_irqsave(&wdt->lock, flags);
	state = wdt->state;
	adx_wdt_stop_locked(wdt);
	writel(timeout, wdt->base + ADX_WDT_TIMEOUT);

	if (state == WDT_STATE_START)
		adx_wdt_start_locked(wdt);

	wdt->timeout = timeout;
	spin_unlock_irqrestore(&wdt->lock, flags);
}

static void adx_wdt_get_timeout(struct adx_wdt *wdt, unsigned long *seconds)
{
	*seconds = wdt->timeout / 1000;
}

static void adx_wdt_keepalive(struct adx_wdt *wdt)
{
	unsigned long flags;

	spin_lock_irqsave(&wdt->lock, flags);
	writel(wdt->timeout, wdt->base + ADX_WDT_TIMEOUT);
	spin_unlock_irqrestore(&wdt->lock, flags);
}

static int adx_wdt_open(struct inode *inode, struct file *file)
{
	struct adx_wdt *wdt = platform_get_drvdata(adx_wdt_dev);

	if (test_and_set_bit(0, &driver_open))
		return -EBUSY;

	file->private_data = wdt;
	adx_wdt_set_timeout(wdt, 30);
	adx_wdt_start(wdt);

	return nonseekable_open(inode, file);
}

static int adx_wdt_release(struct inode *inode, struct file *file)
{
	struct adx_wdt *wdt = file->private_data;

	adx_wdt_stop(wdt);
	clear_bit(0, &driver_open);

	return 0;
}

static long adx_wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct adx_wdt *wdt = file->private_data;
	void __user *argp = (void __user *)arg;
	unsigned long __user *p = argp;
	unsigned long seconds = 0;
	unsigned int options;
	long ret = -EINVAL;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		if (copy_to_user(argp, &adx_wdt_info, sizeof(adx_wdt_info)))
			return -EFAULT;
		else
			return 0;

	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, p);

	case WDIOC_KEEPALIVE:
		adx_wdt_keepalive(wdt);
		return 0;

	case WDIOC_SETTIMEOUT:
		if (get_user(seconds, p))
			return -EFAULT;

		adx_wdt_set_timeout(wdt, seconds);

		/* fallthrough */
	case WDIOC_GETTIMEOUT:
		adx_wdt_get_timeout(wdt, &seconds);
		return put_user(seconds, p);

	case WDIOC_SETOPTIONS:
		if (copy_from_user(&options, argp, sizeof(options)))
			return -EFAULT;

		if (options & WDIOS_DISABLECARD) {
			adx_wdt_stop(wdt);
			ret = 0;
		}

		if (options & WDIOS_ENABLECARD) {
			adx_wdt_start(wdt);
			ret = 0;
		}

		return ret;

	default:
		break;
	}

	return -ENOTTY;
}

static ssize_t adx_wdt_write(struct file *file, const char __user *data,
		size_t len, loff_t *ppos)
{
	struct adx_wdt *wdt = file->private_data;

	if (len)
		adx_wdt_keepalive(wdt);

	return len;
}

static const struct file_operations adx_wdt_fops = {
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.open = adx_wdt_open,
	.release = adx_wdt_release,
	.unlocked_ioctl = adx_wdt_ioctl,
	.write = adx_wdt_write,
};

static struct miscdevice adx_wdt_miscdev = {
	.minor = WATCHDOG_MINOR,
	.name = "watchdog",
	.fops = &adx_wdt_fops,
};

static int __devinit adx_wdt_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct adx_wdt *wdt;
	int ret = 0;
	u32 ctrl;

	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
	if (!wdt) {
		dev_err(&pdev->dev, "cannot allocate WDT structure\n");
		return -ENOMEM;
	}

	spin_lock_init(&wdt->lock);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "cannot obtain I/O memory region\n");
		return -ENXIO;
	}

	res = devm_request_mem_region(&pdev->dev, res->start,
			resource_size(res), res->name);
	if (!res) {
		dev_err(&pdev->dev, "cannot request I/O memory region\n");
		return -ENXIO;
	}

	wdt->base = devm_ioremap_nocache(&pdev->dev, res->start,
			resource_size(res));
	if (!wdt->base) {
		dev_err(&pdev->dev, "cannot remap I/O memory region\n");
		return -ENXIO;
	}

	/* disable watchdog and reboot on timeout */
	ctrl = readl(wdt->base + ADX_WDT_CONTROL);
	ctrl &= ~ADX_WDT_CONTROL_ENABLE;
	ctrl &= ~ADX_WDT_CONTROL_nRESET;
	writel(ctrl, wdt->base + ADX_WDT_CONTROL);

	platform_set_drvdata(pdev, wdt);
	adx_wdt_dev = pdev;

	ret = misc_register(&adx_wdt_miscdev);
	if (ret) {
		dev_err(&pdev->dev, "cannot register miscdev on minor %d "
				"(err=%d)\n", WATCHDOG_MINOR, ret);
		return ret;
	}

	return 0;
}

static int __devexit adx_wdt_remove(struct platform_device *pdev)
{
	struct adx_wdt *wdt = platform_get_drvdata(pdev);

	misc_deregister(&adx_wdt_miscdev);
	adx_wdt_stop(wdt);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

static void adx_wdt_shutdown(struct platform_device *pdev)
{
	struct adx_wdt *wdt = platform_get_drvdata(pdev);
	adx_wdt_stop(wdt);
}

#ifdef CONFIG_PM
static int adx_wdt_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct adx_wdt *wdt = platform_get_drvdata(pdev);

	wdt->wake = (wdt->state == WDT_STATE_START) ? 1 : 0;
	adx_wdt_stop(wdt);

	return 0;
}

static int adx_wdt_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct adx_wdt *wdt = platform_get_drvdata(pdev);

	if (wdt->wake)
		adx_wdt_start(wdt);

	return 0;
}

static const struct dev_pm_ops adx_wdt_pm_ops = {
	.suspend = adx_wdt_suspend,
	.resume = adx_wdt_resume,
};

#  define ADX_WDT_PM_OPS	(&adx_wdt_pm_ops)
#else
#  define ADX_WDT_PM_OPS	NULL
#endif

static struct platform_driver adx_wdt_driver = {
	.probe = adx_wdt_probe,
	.remove = __devexit_p(adx_wdt_remove),
	.shutdown = adx_wdt_shutdown,
	.driver = {
		.name = WATCHDOG_NAME,
		.owner = THIS_MODULE,
		.pm = ADX_WDT_PM_OPS,
	},
};

static int __init adx_wdt_init(void)
{
	return platform_driver_register(&adx_wdt_driver);
}

static void __exit adx_wdt_exit(void)
{
	platform_driver_unregister(&adx_wdt_driver);
}

module_init(adx_wdt_init);
module_exit(adx_wdt_exit);

MODULE_DESCRIPTION("Avionic Design Xanthos Watchdog Driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
OpenPOWER on IntegriCloud