at91rm9200_wdt.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Watchdog driver for Atmel AT91RM9200 (Thunder)
 *
 *  Copyright (C) 2003 SAN People (Pty) Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Jiri Slaby's avatar
Jiri Slaby committed
14
#include <linux/bitops.h>
15 16 17
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
18
#include <linux/io.h>
19 20 21 22
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
23
#include <linux/platform_device.h>
24 25
#include <linux/types.h>
#include <linux/watchdog.h>
26
#include <linux/uaccess.h>
27 28
#include <linux/of.h>
#include <linux/of_device.h>
29
#include <mach/at91_st.h>
30

31 32
#define WDT_DEFAULT_TIME	5	/* seconds */
#define WDT_MAX_TIME		256	/* seconds */
33 34

static int wdt_time = WDT_DEFAULT_TIME;
35
static bool nowayout = WATCHDOG_NOWAYOUT;
36 37

module_param(wdt_time, int, 0);
38 39
MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
				__MODULE_STRING(WDT_DEFAULT_TIME) ")");
40

41
#ifdef CONFIG_WATCHDOG_NOWAYOUT
42
module_param(nowayout, bool, 0);
43 44 45
MODULE_PARM_DESC(nowayout,
		"Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
46
#endif
47 48 49 50 51 52 53 54 55


static unsigned long at91wdt_busy;

/* ......................................................................... */

/*
 * Disable the watchdog.
 */
56
static inline void at91_wdt_stop(void)
57
{
58
	at91_st_write(AT91_ST_WDMR, AT91_ST_EXTEN);
59 60 61 62 63
}

/*
 * Enable and reset the watchdog.
 */
64
static inline void at91_wdt_start(void)
65
{
66
	at91_st_write(AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN |
67
				(((65536 * wdt_time) >> 8) & AT91_ST_WDV));
68
	at91_st_write(AT91_ST_CR, AT91_ST_WDRST);
69 70 71 72 73
}

/*
 * Reload the watchdog timer.  (ie, pat the watchdog)
 */
74
static inline void at91_wdt_reload(void)
75
{
76
	at91_st_write(AT91_ST_CR, AT91_ST_WDRST);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

/* ......................................................................... */

/*
 * Watchdog device is opened, and watchdog starts running.
 */
static int at91_wdt_open(struct inode *inode, struct file *file)
{
	if (test_and_set_bit(0, &at91wdt_busy))
		return -EBUSY;

	at91_wdt_start();
	return nonseekable_open(inode, file);
}

/*
 * Close the watchdog device.
 * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
 *  disabled.
 */
static int at91_wdt_close(struct inode *inode, struct file *file)
{
100
	/* Disable the watchdog when file is closed */
101
	if (!nowayout)
102
		at91_wdt_stop();
103 104 105 106 107 108 109 110 111 112 113

	clear_bit(0, &at91wdt_busy);
	return 0;
}

/*
 * Change the watchdog time interval.
 */
static int at91_wdt_settimeout(int new_time)
{
	/*
114
	 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
115 116
	 *
	 * Since WDV is a 16-bit counter, the maximum period is
117
	 * 65536 / 256 = 256 seconds.
118 119 120 121
	 */
	if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
		return -EINVAL;

122 123
	/* Set new watchdog time. It will be used when
	   at91_wdt_start() is called. */
124 125 126 127
	wdt_time = new_time;
	return 0;
}

128
static const struct watchdog_info at91_wdt_info = {
129 130 131 132 133 134 135
	.identity	= "at91 watchdog",
	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
};

/*
 * Handle commands from user-space.
 */
136
static long at91_wdt_ioctl(struct file *file,
137
					unsigned int cmd, unsigned long arg)
138 139 140 141 142
{
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
	int new_value;

143 144 145 146 147 148 149 150 151 152 153 154 155
	switch (cmd) {
	case WDIOC_GETSUPPORT:
		return copy_to_user(argp, &at91_wdt_info,
				sizeof(at91_wdt_info)) ? -EFAULT : 0;
	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, p);
	case WDIOC_SETOPTIONS:
		if (get_user(new_value, p))
			return -EFAULT;
		if (new_value & WDIOS_DISABLECARD)
			at91_wdt_stop();
		if (new_value & WDIOS_ENABLECARD)
156
			at91_wdt_start();
157
		return 0;
158 159 160 161 162 163 164 165 166 167 168 169 170 171
	case WDIOC_KEEPALIVE:
		at91_wdt_reload();	/* pat the watchdog */
		return 0;
	case WDIOC_SETTIMEOUT:
		if (get_user(new_value, p))
			return -EFAULT;
		if (at91_wdt_settimeout(new_value))
			return -EINVAL;
		/* Enable new time value */
		at91_wdt_start();
		/* Return current value */
		return put_user(wdt_time, p);
	case WDIOC_GETTIMEOUT:
		return put_user(wdt_time, p);
172 173
	default:
		return -ENOTTY;
174 175 176 177 178 179
	}
}

/*
 * Pat the watchdog whenever device is written to.
 */
180 181
static ssize_t at91_wdt_write(struct file *file, const char *data,
						size_t len, loff_t *ppos)
182 183 184 185 186 187 188
{
	at91_wdt_reload();		/* pat the watchdog */
	return len;
}

/* ......................................................................... */

189
static const struct file_operations at91wdt_fops = {
190 191
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
192
	.unlocked_ioctl	= at91_wdt_ioctl,
193 194 195 196 197 198 199 200 201 202 203
	.open		= at91_wdt_open,
	.release	= at91_wdt_close,
	.write		= at91_wdt_write,
};

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

204
static int at91wdt_probe(struct platform_device *pdev)
205 206 207
{
	int res;

208
	if (at91wdt_miscdev.parent)
209
		return -EBUSY;
210
	at91wdt_miscdev.parent = &pdev->dev;
211 212 213 214 215

	res = misc_register(&at91wdt_miscdev);
	if (res)
		return res;

216 217
	pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
		wdt_time, nowayout ? ", nowayout" : "");
218 219 220
	return 0;
}

221
static int at91wdt_remove(struct platform_device *pdev)
222 223 224 225 226
{
	int res;

	res = misc_deregister(&at91wdt_miscdev);
	if (!res)
227
		at91wdt_miscdev.parent = NULL;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

	return res;
}

static void at91wdt_shutdown(struct platform_device *pdev)
{
	at91_wdt_stop();
}

#ifdef CONFIG_PM

static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
{
	at91_wdt_stop();
	return 0;
}

static int at91wdt_resume(struct platform_device *pdev)
{
	if (at91wdt_busy)
		at91_wdt_start();
249
	return 0;
250 251 252 253 254 255 256
}

#else
#define at91wdt_suspend NULL
#define at91wdt_resume	NULL
#endif

257 258 259 260 261 262
static const struct of_device_id at91_wdt_dt_ids[] = {
	{ .compatible = "atmel,at91rm9200-wdt" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);

263 264
static struct platform_driver at91wdt_driver = {
	.probe		= at91wdt_probe,
265
	.remove		= at91wdt_remove,
266 267 268 269 270 271
	.shutdown	= at91wdt_shutdown,
	.suspend	= at91wdt_suspend,
	.resume		= at91wdt_resume,
	.driver		= {
		.name	= "at91_wdt",
		.owner	= THIS_MODULE,
272
		.of_match_table = at91_wdt_dt_ids,
273 274 275 276 277
	},
};

static int __init at91_wdt_init(void)
{
278 279
	/* Check that the heartbeat value is within range;
	   if not reset to the default */
280 281
	if (at91_wdt_settimeout(wdt_time)) {
		at91_wdt_settimeout(WDT_DEFAULT_TIME);
282 283
		pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
			wdt_time);
284 285 286 287 288
	}

	return platform_driver_register(&at91wdt_driver);
}

289 290
static void __exit at91_wdt_exit(void)
{
291
	platform_driver_unregister(&at91wdt_driver);
292 293 294 295 296 297 298 299
}

module_init(at91_wdt_init);
module_exit(at91_wdt_exit);

MODULE_AUTHOR("Andrew Victor");
MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
MODULE_LICENSE("GPL");
300
MODULE_ALIAS("platform:at91_wdt");