mv64x60_wdt.c 7.8 KB
Newer Older
J
James Chapman 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
 *
 * Author: James Chapman <jchapman@katalix.com>
 *
 * Platform-specific setup code should configure the dog to generate
 * interrupt or reset as required.  This code only enables/disables
 * and services the watchdog.
 *
 * Derived from mpc8xx_wdt.c, with the following copyright.
11
 *
J
James Chapman 已提交
12 13 14 15 16 17 18 19 20 21 22 23
 * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under
 * the terms of the GNU General Public License version 2. This program
 * is licensed "as is" without any warranty of any kind, whether express
 * or implied.
 */

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/watchdog.h>
24
#include <linux/platform_device.h>
25
#include <linux/mv643xx.h>
26 27
#include <linux/uaccess.h>
#include <linux/io.h>
J
James Chapman 已提交
28

29 30
#define MV64x60_WDT_WDC_OFFSET	0

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * The watchdog configuration register contains a pair of 2-bit fields,
 *   1.  a reload field, bits 27-26, which triggers a reload of
 *       the countdown register, and
 *   2.  an enable field, bits 25-24, which toggles between
 *       enabling and disabling the watchdog timer.
 * Bit 31 is a read-only field which indicates whether the
 * watchdog timer is currently enabled.
 *
 * The low 24 bits contain the timer reload value.
 */
#define MV64x60_WDC_ENABLE_SHIFT	24
#define MV64x60_WDC_SERVICE_SHIFT	26
#define MV64x60_WDC_ENABLED_SHIFT	31

#define MV64x60_WDC_ENABLED_TRUE	1
#define MV64x60_WDC_ENABLED_FALSE	0
J
James Chapman 已提交
48 49 50 51 52 53

/* Flags bits */
#define MV64x60_WDOG_FLAG_OPENED	0

static unsigned long wdt_flags;
static int wdt_status;
54
static void __iomem *mv64x60_wdt_regs;
J
James Chapman 已提交
55
static int mv64x60_wdt_timeout;
56
static int mv64x60_wdt_count;
57
static unsigned int bus_clk;
58
static char expect_close;
59
static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
J
James Chapman 已提交
60

61 62
static int nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, int, 0);
63 64 65
MODULE_PARM_DESC(nowayout,
		"Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
66

67
static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
J
James Chapman 已提交
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	u32 data;
	u32 enabled;
	int ret = 0;

	spin_lock(&mv64x60_wdt_spinlock);
	data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
	enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;

	/* only toggle the requested field if enabled state matches predicate */
	if ((enabled ^ enabled_predicate) == 0) {
		/* We write a 1, then a 2 -- to the appropriate field */
		data = (1 << field_shift) | mv64x60_wdt_count;
		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);

		data = (2 << field_shift) | mv64x60_wdt_count;
		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
		ret = 1;
	}
	spin_unlock(&mv64x60_wdt_spinlock);

	return ret;
J
James Chapman 已提交
90 91 92 93
}

static void mv64x60_wdt_service(void)
{
94 95
	mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
			       MV64x60_WDC_SERVICE_SHIFT);
J
James Chapman 已提交
96 97
}

98
static void mv64x60_wdt_handler_enable(void)
J
James Chapman 已提交
99
{
100 101 102 103
	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
				   MV64x60_WDC_ENABLE_SHIFT)) {
		mv64x60_wdt_service();
		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
J
James Chapman 已提交
104 105 106
	}
}

107
static void mv64x60_wdt_handler_disable(void)
J
James Chapman 已提交
108
{
109 110 111
	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
				   MV64x60_WDC_ENABLE_SHIFT))
		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
J
James Chapman 已提交
112 113
}

114
static void mv64x60_wdt_set_timeout(unsigned int timeout)
115 116 117 118 119
{
	/* maximum bus cycle count is 0xFFFFFFFF */
	if (timeout > 0xFFFFFFFF / bus_clk)
		timeout = 0xFFFFFFFF / bus_clk;

120
	mv64x60_wdt_count = timeout * bus_clk >> 8;
121 122 123
	mv64x60_wdt_timeout = timeout;
}

J
James Chapman 已提交
124 125 126 127 128
static int mv64x60_wdt_open(struct inode *inode, struct file *file)
{
	if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
		return -EBUSY;

129 130 131
	if (nowayout)
		__module_get(THIS_MODULE);

J
James Chapman 已提交
132 133
	mv64x60_wdt_handler_enable();

134
	return nonseekable_open(inode, file);
J
James Chapman 已提交
135 136 137 138
}

static int mv64x60_wdt_release(struct inode *inode, struct file *file)
{
139
	if (expect_close == 42)
140
		mv64x60_wdt_handler_disable();
141 142 143 144 145 146
	else {
		printk(KERN_CRIT
		       "mv64x60_wdt: unexpected close, not stopping timer!\n");
		mv64x60_wdt_service();
	}
	expect_close = 0;
J
James Chapman 已提交
147 148 149 150 151 152

	clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);

	return 0;
}

153
static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
154
				 size_t len, loff_t *ppos)
J
James Chapman 已提交
155
{
156 157 158 159 160 161 162 163
	if (len) {
		if (!nowayout) {
			size_t i;

			expect_close = 0;

			for (i = 0; i != len; i++) {
				char c;
164
				if (get_user(c, data + i))
165 166 167 168 169
					return -EFAULT;
				if (c == 'V')
					expect_close = 42;
			}
		}
J
James Chapman 已提交
170
		mv64x60_wdt_service();
171
	}
J
James Chapman 已提交
172 173 174 175

	return len;
}

176 177
static long mv64x60_wdt_ioctl(struct file *file,
					unsigned int cmd, unsigned long arg)
J
James Chapman 已提交
178
{
179
	int timeout;
180
	int options;
181
	void __user *argp = (void __user *)arg;
J
James Chapman 已提交
182
	static struct watchdog_info info = {
183
		.options =	WDIOF_SETTIMEOUT	|
184
				WDIOF_MAGICCLOSE	|
185
				WDIOF_KEEPALIVEPING,
J
James Chapman 已提交
186 187 188 189 190 191
		.firmware_version = 0,
		.identity = "MV64x60 watchdog",
	};

	switch (cmd) {
	case WDIOC_GETSUPPORT:
192
		if (copy_to_user(argp, &info, sizeof(info)))
J
James Chapman 已提交
193 194 195 196 197
			return -EFAULT;
		break;

	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
198
		if (put_user(wdt_status, (int __user *)argp))
J
James Chapman 已提交
199 200 201 202 203 204 205 206
			return -EFAULT;
		wdt_status &= ~WDIOF_KEEPALIVEPING;
		break;

	case WDIOC_GETTEMP:
		return -EOPNOTSUPP;

	case WDIOC_SETOPTIONS:
207 208 209 210 211 212 213 214 215
		if (get_user(options, (int __user *)argp))
			return -EFAULT;

		if (options & WDIOS_DISABLECARD)
			mv64x60_wdt_handler_disable();

		if (options & WDIOS_ENABLECARD)
			mv64x60_wdt_handler_enable();
		break;
J
James Chapman 已提交
216 217 218 219 220 221 222

	case WDIOC_KEEPALIVE:
		mv64x60_wdt_service();
		wdt_status |= WDIOF_KEEPALIVEPING;
		break;

	case WDIOC_SETTIMEOUT:
223 224 225 226
		if (get_user(timeout, (int __user *)argp))
			return -EFAULT;
		mv64x60_wdt_set_timeout(timeout);
		/* Fall through */
J
James Chapman 已提交
227 228

	case WDIOC_GETTIMEOUT:
229
		if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
J
James Chapman 已提交
230 231 232 233
			return -EFAULT;
		break;

	default:
234
		return -ENOTTY;
J
James Chapman 已提交
235 236 237 238 239
	}

	return 0;
}

240
static const struct file_operations mv64x60_wdt_fops = {
J
James Chapman 已提交
241 242 243
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.write = mv64x60_wdt_write,
244
	.unlocked_ioctl = mv64x60_wdt_ioctl,
J
James Chapman 已提交
245 246 247 248 249 250 251 252 253 254
	.open = mv64x60_wdt_open,
	.release = mv64x60_wdt_release,
};

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

255
static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
J
James Chapman 已提交
256
{
257
	struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
258
	struct resource *r;
259
	int timeout = 10;
J
James Chapman 已提交
260

261
	bus_clk = 133;			/* in MHz */
J
James Chapman 已提交
262
	if (pdata) {
263
		timeout = pdata->timeout;
J
James Chapman 已提交
264 265 266
		bus_clk = pdata->bus_clk;
	}

267 268 269 270 271 272 273
	/* Since bus_clk is truncated MHz, actual frequency could be
	 * up to 1MHz higher.  Round up, since it's better to time out
	 * too late than too soon.
	 */
	bus_clk++;
	bus_clk *= 1000000;		/* convert to Hz */

274 275 276 277 278 279 280
	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
	if (!r)
		return -ENODEV;

	mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
	if (mv64x60_wdt_regs == NULL)
		return -ENOMEM;
J
James Chapman 已提交
281

282
	mv64x60_wdt_set_timeout(timeout);
J
James Chapman 已提交
283

284 285
	mv64x60_wdt_handler_disable();	/* in case timer was already running */

J
James Chapman 已提交
286 287 288
	return misc_register(&mv64x60_wdt_miscdev);
}

289
static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
J
James Chapman 已提交
290 291 292 293 294
{
	misc_deregister(&mv64x60_wdt_miscdev);

	mv64x60_wdt_handler_disable();

295 296
	iounmap(mv64x60_wdt_regs);

J
James Chapman 已提交
297 298 299
	return 0;
}

300
static struct platform_driver mv64x60_wdt_driver = {
J
James Chapman 已提交
301 302
	.probe = mv64x60_wdt_probe,
	.remove = __devexit_p(mv64x60_wdt_remove),
303 304 305 306
	.driver = {
		.owner = THIS_MODULE,
		.name = MV64x60_WDT_NAME,
	},
J
James Chapman 已提交
307 308 309 310 311 312
};

static int __init mv64x60_wdt_init(void)
{
	printk(KERN_INFO "MV64x60 watchdog driver\n");

313
	return platform_driver_register(&mv64x60_wdt_driver);
J
James Chapman 已提交
314 315 316 317
}

static void __exit mv64x60_wdt_exit(void)
{
318
	platform_driver_unregister(&mv64x60_wdt_driver);
J
James Chapman 已提交
319 320 321 322 323 324 325 326 327
}

module_init(mv64x60_wdt_init);
module_exit(mv64x60_wdt_exit);

MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
MODULE_DESCRIPTION("MV64x60 watchdog driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
328
MODULE_ALIAS("platform:" MV64x60_WDT_NAME);