summaryrefslogtreecommitdiffstats
path: root/target/linux/ar71xx/files/drivers/watchdog/ar71xx_wdt.c
blob: abe157cb0dd4185b8bd74bdac2ec5c5b0562dd8a (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
/*
 * Driver for the Atheros AR71xx SoC's built-in hardware watchdog timer.
 *
 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
 *
 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
 *	Author: Deepak Saxena <dsaxena@plexity.net>
 *	Copyright 2004 (c) MontaVista, Software, Inc.
 *
 * which again was based on sa1100 driver,
 *	Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
 *
 * 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/bitops.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/watchdog.h>

#include <asm/mach-ar71xx/ar71xx.h>

#define DRV_NAME	"ar71xx-wdt"
#define DRV_DESC	"Atheros AR71xx hardware watchdog driver"
#define DRV_VERSION	"0.1.0"

#define WDT_TIMEOUT	15	/* seconds */

static int nowayout = WATCHDOG_NOWAYOUT;

#ifdef CONFIG_WATCHDOG_NOWAYOUT
module_param(nowayout, int, 0);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
			   "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
#endif

static unsigned long wdt_flags;

#define WDT_FLAGS_BUSY		0
#define WDT_FLAGS_EXPECT_CLOSE	1

static int wdt_timeout = WDT_TIMEOUT;
static int boot_status;
static int max_timeout;

static inline void ar71xx_wdt_keepalive(void)
{
	ar71xx_reset_wr(AR71XX_RESET_REG_WDOG, ar71xx_ahb_freq * wdt_timeout);
}

static inline void ar71xx_wdt_enable(void)
{
	printk(KERN_DEBUG DRV_NAME ": enabling watchdog timer\n");
	ar71xx_wdt_keepalive();
	ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
}

static inline void ar71xx_wdt_disable(void)
{
	printk(KERN_DEBUG DRV_NAME ": disabling watchdog timer\n");
	ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
}

static int ar71xx_wdt_set_timeout(int val)
{
	if (val < 1 || val > max_timeout)
		return -EINVAL;

	wdt_timeout = val;
	ar71xx_wdt_keepalive();

	printk(KERN_DEBUG DRV_NAME ": timeout=%d secs\n", wdt_timeout);

	return 0;
}

static int ar71xx_wdt_open(struct inode *inode, struct file *file)
{
	if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
		return -EBUSY;

	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);

	ar71xx_wdt_enable();

	return nonseekable_open(inode, file);
}

static int ar71xx_wdt_release(struct inode *inode, struct file *file)
{
	if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags)) {
		ar71xx_wdt_disable();
	} else {
		printk(KERN_CRIT DRV_NAME ": device closed unexpectedly, "
					"watchdog timer will not stop!\n");
	}

	clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);

	return 0;
}

static ssize_t ar71xx_wdt_write(struct file *file, const char *data,
				size_t len, loff_t *ppos)
{
	if (len) {
		if (!nowayout) {
			size_t i;

			clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);

			for (i = 0; i != len; i++) {
				char c;

				if (get_user(c, data + i))
					return -EFAULT;

				if (c == 'V')
					set_bit(WDT_FLAGS_EXPECT_CLOSE,
						&wdt_flags);
			}
		}

		ar71xx_wdt_keepalive();
	}

	return len;
}

static struct watchdog_info ar71xx_wdt_info = {
	.options		= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
				  WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
	.firmware_version	= 0,
	.identity		= "AR71XX watchdog",
};

static long ar71xx_wdt_ioctl(struct file *file,
			    unsigned int cmd, unsigned long arg)
{
	int t;
	int ret;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		ret = copy_to_user((struct watchdog_info *)arg,
				   &ar71xx_wdt_info,
				   sizeof(ar71xx_wdt_info)) ? -EFAULT : 0;
		break;

	case WDIOC_GETSTATUS:
		ret = put_user(0, (int *)arg) ? -EFAULT : 0;
		break;

	case WDIOC_GETBOOTSTATUS:
		ret = put_user(boot_status, (int *)arg) ? -EFAULT : 0;
		break;

	case WDIOC_KEEPALIVE:
		ar71xx_wdt_keepalive();
		ret = 0;
		break;

	case WDIOC_SETTIMEOUT:
		ret = get_user(t, (int *)arg) ? -EFAULT : 0;
		if (ret)
			break;

		ret = ar71xx_wdt_set_timeout(t);
		if (ret)
			break;

		/* fallthrough */
	case WDIOC_GETTIMEOUT:
		ret = put_user(wdt_timeout, (int *)arg) ? -EFAULT : 0;
		break;

	default:
		ret = -ENOTTY;
		break;
	}

	return ret;
}

static const struct file_operations ar71xx_wdt_fops = {
	.owner		= THIS_MODULE,
	.write		= ar71xx_wdt_write,
	.unlocked_ioctl	= ar71xx_wdt_ioctl,
	.open		= ar71xx_wdt_open,
	.release	= ar71xx_wdt_release,
};

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

static int __devinit ar71xx_wdt_probe(struct platform_device *pdev)
{
	int ret;

	max_timeout = (0xfffffffful / ar71xx_ahb_freq);
	wdt_timeout = (max_timeout < WDT_TIMEOUT) ? max_timeout : WDT_TIMEOUT;

	if (ar71xx_reset_rr(AR71XX_RESET_REG_WDOG_CTRL) & WDOG_CTRL_LAST_RESET)
		boot_status = WDIOF_CARDRESET;

	ret = misc_register(&ar71xx_wdt_miscdev);
	if (ret)
		goto err_out;

	printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");

	printk(KERN_DEBUG DRV_NAME ": timeout=%d secs (max=%d)\n",
					wdt_timeout, max_timeout);

	return 0;

err_out:
	return ret;
}

static int __devexit ar71xx_wdt_remove(struct platform_device *pdev)
{
	misc_deregister(&ar71xx_wdt_miscdev);
	return 0;
}

static struct platform_driver ar71xx_wdt_driver = {
	.probe		= ar71xx_wdt_probe,
	.remove		= __devexit_p(ar71xx_wdt_remove),
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init ar71xx_wdt_init(void)
{
	return platform_driver_register(&ar71xx_wdt_driver);
}
module_init(ar71xx_wdt_init);

static void __exit ar71xx_wdt_exit(void)
{
	platform_driver_unregister(&ar71xx_wdt_driver);
}
module_exit(ar71xx_wdt_exit);

MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRV_NAME);
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);