rm9k_wdt.c 10.2 KB
Newer Older
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
/*
 *  Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
 *  chips.
 *
 *  Copyright (C) 2004 by Basler Vision Technologies AG
 *  Author: Thomas Koeller <thomas.koeller@baslerweb.com>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/reboot.h>
29
#include <linux/notifier.h>
30 31
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
A
Alan Cox 已提交
32 33
#include <linux/io.h>
#include <linux/uaccess.h>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <asm/atomic.h>
#include <asm/processor.h>
#include <asm/system.h>
#include <asm/rm9k-ocd.h>

#include <rm9k_wdt.h>


#define CLOCK                  125000000
#define MAX_TIMEOUT_SECONDS    32
#define CPCCR                  0x0080
#define CPGIG1SR               0x0044
#define CPGIG1ER               0x0054


/* Function prototypes */
50
static irqreturn_t wdt_gpi_irqhdl(int, void *);
51 52
static void wdt_gpi_start(void);
static void wdt_gpi_stop(void);
53 54 55
static void wdt_gpi_set_timeout(unsigned int);
static int wdt_gpi_open(struct inode *, struct file *);
static int wdt_gpi_release(struct inode *, struct file *);
A
Alan Cox 已提交
56 57
static ssize_t wdt_gpi_write(struct file *, const char __user *, size_t,
								loff_t *);
58 59
static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
A
Alan Cox 已提交
60 61
static const struct resource *wdt_gpi_get_resource(struct platform_device *,
						const char *, unsigned int);
62 63
static int __init wdt_gpi_probe(struct device *);
static int __exit wdt_gpi_remove(struct device *);
64 65 66 67 68


static const char wdt_gpi_name[] = "wdt_gpi";
static atomic_t opencnt;
static int expect_close;
69
static int locked;
70 71 72


/* These are set from device resources */
A
Alan Cox 已提交
73
static void __iomem *wd_regs;
74 75 76 77 78 79
static unsigned int wd_irq, wd_ctr;


/* Module arguments */
static int timeout = MAX_TIMEOUT_SECONDS;
module_param(timeout, int, 0444);
80 81
MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");

82 83
static unsigned long resetaddr = 0xbffdc200;
module_param(resetaddr, ulong, 0444);
84 85
MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");

86 87
static unsigned long flagaddr = 0xbffdc104;
module_param(flagaddr, ulong, 0444);
88 89
MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");

90
static int powercycle;
91
module_param(powercycle, bool, 0444);
92
MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
93 94 95

static int nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0444);
96
MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
97 98


99
/* Kernel interfaces */
100
static const struct file_operations fops = {
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	.owner		= THIS_MODULE,
	.open		= wdt_gpi_open,
	.release	= wdt_gpi_release,
	.write		= wdt_gpi_write,
	.unlocked_ioctl	= wdt_gpi_ioctl,
};

static struct miscdevice miscdev = {
	.minor		= WATCHDOG_MINOR,
	.name		= wdt_gpi_name,
	.fops		= &fops,
};

static struct notifier_block wdt_gpi_shutdown = {
	.notifier_call	= wdt_gpi_notify,
};


119
/* Interrupt handler */
120
static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt)
121
{
122 123 124
	if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
		return IRQ_NONE;
	__raw_writel(0x1, wd_regs + 0x0008);
125 126


127
	printk(KERN_CRIT "%s: watchdog expired - resetting system\n",
128
		wdt_gpi_name);
129

130 131 132 133 134
	*(volatile char *) flagaddr |= 0x01;
	*(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
	iob();
	while (1)
		cpu_relax();
135 136 137
}


138
/* Watchdog functions */
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
static void wdt_gpi_start(void)
{
	u32 reg;

	lock_titan_regs();
	reg = titan_readl(CPGIG1ER);
	titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
	iob();
	unlock_titan_regs();
}

static void wdt_gpi_stop(void)
{
	u32 reg;

	lock_titan_regs();
	reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
	titan_writel(reg, CPCCR);
	reg = titan_readl(CPGIG1ER);
	titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
	iob();
	unlock_titan_regs();
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static void wdt_gpi_set_timeout(unsigned int to)
{
	u32 reg;
	const u32 wdval = (to * CLOCK) & ~0x0000000f;

	lock_titan_regs();
	reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
	titan_writel(reg, CPCCR);
	wmb();
	__raw_writel(wdval, wd_regs + 0x0000);
	wmb();
	titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
	wmb();
	titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
	iob();
	unlock_titan_regs();
}


182
/* /dev/watchdog operations */
183
static int wdt_gpi_open(struct inode *inode, struct file *file)
184 185 186
{
	int res;

187
	if (unlikely(atomic_dec_if_positive(&opencnt) < 0))
188 189 190 191 192 193 194 195 196
		return -EBUSY;

	expect_close = 0;
	if (locked) {
		module_put(THIS_MODULE);
		free_irq(wd_irq, &miscdev);
		locked = 0;
	}

197
	res = request_irq(wd_irq, wdt_gpi_irqhdl, IRQF_SHARED | IRQF_DISABLED,
198 199 200 201 202
			  wdt_gpi_name, &miscdev);
	if (unlikely(res))
		return res;

	wdt_gpi_set_timeout(timeout);
203
	wdt_gpi_start();
204 205 206

	printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
		wdt_gpi_name, timeout);
207
	return nonseekable_open(inode, file);
208 209
}

210
static int wdt_gpi_release(struct inode *inode, struct file *file)
211 212
{
	if (nowayout) {
213
		printk(KERN_INFO "%s: no way out - watchdog left running\n",
214 215 216 217 218
			wdt_gpi_name);
		__module_get(THIS_MODULE);
		locked = 1;
	} else {
		if (expect_close) {
219
			wdt_gpi_stop();
220
			free_irq(wd_irq, &miscdev);
A
Alan Cox 已提交
221 222
			printk(KERN_INFO "%s: watchdog stopped\n",
							wdt_gpi_name);
223
		} else {
224
			printk(KERN_CRIT "%s: unexpected close() -"
225 226 227 228 229 230 231 232 233 234 235 236
				" watchdog left running\n",
				wdt_gpi_name);
			wdt_gpi_set_timeout(timeout);
			__module_get(THIS_MODULE);
			locked = 1;
		}
	}

	atomic_inc(&opencnt);
	return 0;
}

237 238
static ssize_t wdt_gpi_write(struct file *f, const char __user *d, size_t s,
								loff_t *o)
239 240 241 242 243 244 245 246
{
	char val;

	wdt_gpi_set_timeout(timeout);
	expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
	return s ? 1 : 0;
}

A
Alan Cox 已提交
247
static long wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
248 249 250 251
{
	long res = -ENOTTY;
	const long size = _IOC_SIZE(cmd);
	int stat;
252
	void __user *argp = (void __user *)arg;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	static struct watchdog_info wdinfo = {
		.identity               = "RM9xxx/GPI watchdog",
		.firmware_version       = 0,
		.options                = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
	};

	if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
		return -ENOTTY;

	if ((_IOC_DIR(cmd) & _IOC_READ)
	    && !access_ok(VERIFY_WRITE, arg, size))
		return -EFAULT;

	if ((_IOC_DIR(cmd) & _IOC_WRITE)
	    && !access_ok(VERIFY_READ, arg, size))
		return -EFAULT;

	expect_close = 0;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		wdinfo.options = nowayout ?
			WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
A
Alan Cox 已提交
276 277
			WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
			WDIOF_MAGICCLOSE;
278
		res = __copy_to_user(argp, &wdinfo, size) ?  -EFAULT : size;
279 280 281 282 283 284 285 286
		break;

	case WDIOC_GETSTATUS:
		break;

	case WDIOC_GETBOOTSTATUS:
		stat = (*(volatile char *) flagaddr & 0x01)
			? WDIOF_CARDRESET : 0;
287
		res = __copy_to_user(argp, &stat, size) ?
288 289 290 291 292 293 294 295 296 297 298 299 300 301
			-EFAULT : size;
		break;

	case WDIOC_SETOPTIONS:
		break;

	case WDIOC_KEEPALIVE:
		wdt_gpi_set_timeout(timeout);
		res = size;
		break;

	case WDIOC_SETTIMEOUT:
		{
			int val;
302
			if (unlikely(__copy_from_user(&val, argp, size))) {
303 304 305 306
				res = -EFAULT;
				break;
			}

307 308
			if (val > MAX_TIMEOUT_SECONDS)
				val = MAX_TIMEOUT_SECONDS;
309 310 311
			timeout = val;
			wdt_gpi_set_timeout(val);
			res = size;
312
			printk(KERN_INFO "%s: timeout set to %u seconds\n",
313 314 315 316 317
				wdt_gpi_name, timeout);
		}
		break;

	case WDIOC_GETTIMEOUT:
318
		res = __copy_to_user(argp, &timeout, size) ?
319 320 321 322 323 324 325 326
			-EFAULT : size;
		break;
	}

	return res;
}


327
/* Shutdown notifier */
328 329
static int wdt_gpi_notify(struct notifier_block *this, unsigned long code,
			  void *unused)
330
{
331 332 333
	if (code == SYS_DOWN || code == SYS_HALT)
		wdt_gpi_stop();

334 335 336 337
	return NOTIFY_DONE;
}


338
/* Init & exit procedures */
339 340
static const struct resource *wdt_gpi_get_resource(struct platform_device *pdv,
					const char *name, unsigned int type)
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
{
	char buf[80];
	if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
		return NULL;
	return platform_get_resource_byname(pdv, type, buf);
}

/* No hotplugging on the platform bus - use __init */
static int __init wdt_gpi_probe(struct device *dev)
{
	int res;
	struct platform_device * const pdv = to_platform_device(dev);
	const struct resource
		* const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
						  IORESOURCE_MEM),
		* const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
						  IORESOURCE_IRQ),
		* const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
						  0);

	if (unlikely(!rr || !ri || !rc))
		return -ENXIO;

	wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
	if (unlikely(!wd_regs))
		return -ENOMEM;
	wd_irq = ri->start;
	wd_ctr = rc->start;
	res = misc_register(&miscdev);
	if (res)
		iounmap(wd_regs);
	else
		register_reboot_notifier(&wdt_gpi_shutdown);
	return res;
}

static int __exit wdt_gpi_remove(struct device *dev)
{
	int res;

	unregister_reboot_notifier(&wdt_gpi_shutdown);
	res = misc_deregister(&miscdev);
	iounmap(wd_regs);
	wd_regs = NULL;
	return res;
}


/* Device driver init & exit */
static struct device_driver wdt_gpi_driver = {
	.name		= (char *) wdt_gpi_name,
	.bus		= &platform_bus_type,
	.owner		= THIS_MODULE,
	.probe		= wdt_gpi_probe,
	.remove		= __exit_p(wdt_gpi_remove),
	.shutdown	= NULL,
	.suspend	= NULL,
	.resume		= NULL,
};
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

static int __init wdt_gpi_init_module(void)
{
	atomic_set(&opencnt, 1);
	if (timeout > MAX_TIMEOUT_SECONDS)
		timeout = MAX_TIMEOUT_SECONDS;
	return driver_register(&wdt_gpi_driver);
}

static void __exit wdt_gpi_cleanup_module(void)
{
	driver_unregister(&wdt_gpi_driver);
}

module_init(wdt_gpi_init_module);
module_exit(wdt_gpi_cleanup_module);

MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
422