omap_wdt.c 11.8 KB
Newer Older
1
/*
2
 * omap_wdt.c
3
 *
4
 * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog
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
 *
 * Author: MontaVista Software, Inc.
 *	 <gdavis@mvista.com> or <source@mvista.com>
 *
 * 2003 (c) MontaVista Software, Inc. 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.
 *
 * History:
 *
 * 20030527: George G. Davis <gdavis@mvista.com>
 *	Initially based on linux-2.4.19-rmk7-pxa1/drivers/char/sa1100_wdt.c
 *	(c) Copyright 2000 Oleg Drokin <green@crimea.edu>
 *	Based on SoftDog driver by Alan Cox <alan@redhat.com>
 *
 * Copyright (c) 2004 Texas Instruments.
 *	1. Modified to support OMAP1610 32-KHz watchdog timer
 *	2. Ported to 2.6 kernel
 *
 * Copyright (c) 2005 David Brownell
 *	Use the driver model and standard identifiers; handle bigger timeouts.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/reboot.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/moduleparam.h>
#include <linux/clk.h>
J
Jiri Slaby 已提交
42
#include <linux/bitops.h>
W
Wim Van Sebroeck 已提交
43
#include <linux/io.h>
44
#include <linux/uaccess.h>
45 46
#include <mach/hardware.h>
#include <mach/prcm.h>
47 48 49

#include "omap_wdt.h"

50 51
static struct platform_device *omap_wdt_dev;

52 53 54 55 56
static unsigned timer_margin;
module_param(timer_margin, uint, 0);
MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)");

static unsigned int wdt_trgr_pattern = 0x1234;
57
static spinlock_t wdt_lock;
58

59 60 61 62 63 64 65 66 67 68 69 70
struct omap_wdt_dev {
	void __iomem    *base;          /* physical */
	struct device   *dev;
	int             omap_wdt_users;
	struct clk      *armwdt_ck;
	struct clk      *mpu_wdt_ick;
	struct clk      *mpu_wdt_fck;
	struct resource *mem;
	struct miscdevice omap_wdt_miscdev;
};

static void omap_wdt_ping(struct omap_wdt_dev *wdev)
71
{
72
	void __iomem    *base = wdev->base;
73
	/* wait for posted write to complete */
74
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
75 76
		cpu_relax();
	wdt_trgr_pattern = ~wdt_trgr_pattern;
77
	__raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
78
	/* wait for posted write to complete */
79
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
80 81 82 83
		cpu_relax();
	/* reloaded WCRR from WLDR */
}

84
static void omap_wdt_enable(struct omap_wdt_dev *wdev)
85
{
86 87
	void __iomem *base;
	base = wdev->base;
88
	/* Sequence to enable the watchdog */
89 90
	__raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
91
		cpu_relax();
92 93
	__raw_writel(0x4444, base + OMAP_WATCHDOG_SPR);
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
94 95 96
		cpu_relax();
}

97
static void omap_wdt_disable(struct omap_wdt_dev *wdev)
98
{
99 100
	void __iomem *base;
	base = wdev->base;
101
	/* sequence required to disable watchdog */
102 103
	__raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR);	/* TIMER_MODE */
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
104
		cpu_relax();
105 106
	__raw_writel(0x5555, base + OMAP_WATCHDOG_SPR);	/* TIMER_MODE */
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
107 108 109 110 111 112 113 114 115 116 117 118
		cpu_relax();
}

static void omap_wdt_adjust_timeout(unsigned new_timeout)
{
	if (new_timeout < TIMER_MARGIN_MIN)
		new_timeout = TIMER_MARGIN_DEFAULT;
	if (new_timeout > TIMER_MARGIN_MAX)
		new_timeout = TIMER_MARGIN_MAX;
	timer_margin = new_timeout;
}

119
static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev)
120 121
{
	u32 pre_margin = GET_WLDR_VAL(timer_margin);
122 123
	void __iomem *base;
	base = wdev->base;
124 125

	/* just count up at 32 KHz */
126
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
127
		cpu_relax();
128 129
	__raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
130 131 132 133 134 135 136 137 138
		cpu_relax();
}

/*
 *	Allow only one task to hold it open
 */

static int omap_wdt_open(struct inode *inode, struct file *file)
{
139 140 141 142 143
	struct omap_wdt_dev *wdev;
	void __iomem *base;
	wdev = platform_get_drvdata(omap_wdt_dev);
	base = wdev->base;
	if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users)))
144 145 146
		return -EBUSY;

	if (cpu_is_omap16xx())
147
		clk_enable(wdev->armwdt_ck);	/* Enable the clock */
148

149 150 151
	if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
		clk_enable(wdev->mpu_wdt_ick);    /* Enable the interface clock */
		clk_enable(wdev->mpu_wdt_fck);    /* Enable the functional clock */
152 153 154
	}

	/* initialize prescaler */
155
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
156
		cpu_relax();
157 158
	__raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
159 160
		cpu_relax();

161 162 163 164
	file->private_data = (void *) wdev;

	omap_wdt_set_timeout(wdev);
	omap_wdt_enable(wdev);
W
Wim Van Sebroeck 已提交
165
	return nonseekable_open(inode, file);
166 167 168 169
}

static int omap_wdt_release(struct inode *inode, struct file *file)
{
170 171
	struct omap_wdt_dev *wdev;
	wdev = file->private_data;
172 173 174 175 176
	/*
	 *      Shut off the timer unless NOWAYOUT is defined.
	 */
#ifndef CONFIG_WATCHDOG_NOWAYOUT

177
	omap_wdt_disable(wdev);
178

179 180 181 182 183 184
	if (cpu_is_omap16xx())
		clk_disable(wdev->armwdt_ck);	/* Disable the clock */

	if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
		clk_disable(wdev->mpu_wdt_ick);	/* Disable the clock */
		clk_disable(wdev->mpu_wdt_fck);	/* Disable the clock */
185 186 187 188
	}
#else
	printk(KERN_CRIT "omap_wdt: Unexpected close, not stopping!\n");
#endif
189
	wdev->omap_wdt_users = 0;
190 191 192
	return 0;
}

193
static ssize_t omap_wdt_write(struct file *file, const char __user *data,
194 195
		size_t len, loff_t *ppos)
{
196 197
	struct omap_wdt_dev *wdev;
	wdev = file->private_data;
198
	/* Refresh LOAD_TIME. */
199 200
	if (len) {
		spin_lock(&wdt_lock);
201
		omap_wdt_ping(wdev);
202 203
		spin_unlock(&wdt_lock);
	}
204 205 206
	return len;
}

207 208
static long omap_wdt_ioctl(struct file *file, unsigned int cmd,
						unsigned long arg)
209
{
210
	struct omap_wdt_dev *wdev;
211
	int new_margin;
212
	static const struct watchdog_info ident = {
213 214 215 216
		.identity = "OMAP Watchdog",
		.options = WDIOF_SETTIMEOUT,
		.firmware_version = 0,
	};
217
	wdev = file->private_data;
218 219 220 221 222 223 224 225 226

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		return copy_to_user((struct watchdog_info __user *)arg, &ident,
				sizeof(ident));
	case WDIOC_GETSTATUS:
		return put_user(0, (int __user *)arg);
	case WDIOC_GETBOOTSTATUS:
		if (cpu_is_omap16xx())
227
			return put_user(__raw_readw(ARM_SYSST),
228 229 230 231 232
					(int __user *)arg);
		if (cpu_is_omap24xx())
			return put_user(omap_prcm_get_reset_sources(),
					(int __user *)arg);
	case WDIOC_KEEPALIVE:
233
		spin_lock(&wdt_lock);
234
		omap_wdt_ping(wdev);
235
		spin_unlock(&wdt_lock);
236 237 238 239 240 241
		return 0;
	case WDIOC_SETTIMEOUT:
		if (get_user(new_margin, (int __user *)arg))
			return -EFAULT;
		omap_wdt_adjust_timeout(new_margin);

242
		spin_lock(&wdt_lock);
243 244 245
		omap_wdt_disable(wdev);
		omap_wdt_set_timeout(wdev);
		omap_wdt_enable(wdev);
246

247
		omap_wdt_ping(wdev);
248
		spin_unlock(&wdt_lock);
249 250 251
		/* Fall */
	case WDIOC_GETTIMEOUT:
		return put_user(timer_margin, (int __user *)arg);
252 253
	default:
		return -ENOTTY;
254 255 256
	}
}

257
static const struct file_operations omap_wdt_fops = {
258 259
	.owner = THIS_MODULE,
	.write = omap_wdt_write,
260
	.unlocked_ioctl = omap_wdt_ioctl,
261 262 263 264 265 266 267 268 269
	.open = omap_wdt_open,
	.release = omap_wdt_release,
};


static int __init omap_wdt_probe(struct platform_device *pdev)
{
	struct resource *res, *mem;
	int ret;
270
	struct omap_wdt_dev *wdev;
271 272 273 274 275 276

	/* reserve static register mappings */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENOENT;

277 278 279
	if (omap_wdt_dev)
		return -EBUSY;

280 281 282 283 284
	mem = request_mem_region(res->start, res->end - res->start + 1,
				 pdev->name);
	if (mem == NULL)
		return -EBUSY;

285 286 287 288 289 290 291
	wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL);
	if (!wdev) {
		ret = -ENOMEM;
		goto fail;
	}
	wdev->omap_wdt_users = 0;
	wdev->mem = mem;
292 293

	if (cpu_is_omap16xx()) {
294 295 296 297
		wdev->armwdt_ck = clk_get(&pdev->dev, "armwdt_ck");
		if (IS_ERR(wdev->armwdt_ck)) {
			ret = PTR_ERR(wdev->armwdt_ck);
			wdev->armwdt_ck = NULL;
298 299 300 301 302
			goto fail;
		}
	}

	if (cpu_is_omap24xx()) {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		wdev->mpu_wdt_ick = clk_get(&pdev->dev, "mpu_wdt_ick");
		if (IS_ERR(wdev->mpu_wdt_ick)) {
			ret = PTR_ERR(wdev->mpu_wdt_ick);
			wdev->mpu_wdt_ick = NULL;
			goto fail;
		}
		wdev->mpu_wdt_fck = clk_get(&pdev->dev, "mpu_wdt_fck");
		if (IS_ERR(wdev->mpu_wdt_fck)) {
			ret = PTR_ERR(wdev->mpu_wdt_fck);
			wdev->mpu_wdt_fck = NULL;
			goto fail;
		}
	}

	if (cpu_is_omap34xx()) {
		wdev->mpu_wdt_ick = clk_get(&pdev->dev, "wdt2_ick");
		if (IS_ERR(wdev->mpu_wdt_ick)) {
			ret = PTR_ERR(wdev->mpu_wdt_ick);
			wdev->mpu_wdt_ick = NULL;
322 323
			goto fail;
		}
324 325 326 327
		wdev->mpu_wdt_fck = clk_get(&pdev->dev, "wdt2_fck");
		if (IS_ERR(wdev->mpu_wdt_fck)) {
			ret = PTR_ERR(wdev->mpu_wdt_fck);
			wdev->mpu_wdt_fck = NULL;
328 329 330
			goto fail;
		}
	}
331 332 333 334 335 336
	wdev->base = ioremap(res->start, res->end - res->start + 1);
	if (!wdev->base) {
		ret = -ENOMEM;
		goto fail;
	}

337
	platform_set_drvdata(pdev, wdev);
338

339
	omap_wdt_disable(wdev);
340 341
	omap_wdt_adjust_timeout(timer_margin);

342 343 344 345 346 347
	wdev->omap_wdt_miscdev.parent = &pdev->dev;
	wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR;
	wdev->omap_wdt_miscdev.name = "watchdog";
	wdev->omap_wdt_miscdev.fops = &omap_wdt_fops;

	ret = misc_register(&(wdev->omap_wdt_miscdev));
348 349 350
	if (ret)
		goto fail;

351
	pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
352
		__raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
353
		timer_margin);
354 355

	/* autogate OCP interface clock */
356
	__raw_writel(0x01, wdev->base + OMAP_WATCHDOG_SYS_CONFIG);
357 358 359

	omap_wdt_dev = pdev;

360 361 362
	return 0;

fail:
363 364 365 366 367 368 369 370
	if (wdev) {
		platform_set_drvdata(pdev, NULL);
		if (wdev->armwdt_ck)
			clk_put(wdev->armwdt_ck);
		if (wdev->mpu_wdt_ick)
			clk_put(wdev->mpu_wdt_ick);
		if (wdev->mpu_wdt_fck)
			clk_put(wdev->mpu_wdt_fck);
371
		iounmap(wdev->base);
372 373 374 375 376
		kfree(wdev);
	}
	if (mem) {
		release_mem_region(res->start, res->end - res->start + 1);
	}
377 378 379 380 381
	return ret;
}

static void omap_wdt_shutdown(struct platform_device *pdev)
{
382 383 384 385 386
	struct omap_wdt_dev *wdev;
	wdev = platform_get_drvdata(pdev);

	if (wdev->omap_wdt_users)
		omap_wdt_disable(wdev);
387 388 389 390
}

static int omap_wdt_remove(struct platform_device *pdev)
{
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	struct omap_wdt_dev *wdev;
	wdev = platform_get_drvdata(pdev);
	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	if (!res)
		return -ENOENT;

	misc_deregister(&(wdev->omap_wdt_miscdev));
	release_mem_region(res->start, res->end - res->start + 1);
	platform_set_drvdata(pdev, NULL);
	if (wdev->armwdt_ck) {
		clk_put(wdev->armwdt_ck);
		wdev->armwdt_ck = NULL;
	}
	if (wdev->mpu_wdt_ick) {
		clk_put(wdev->mpu_wdt_ick);
		wdev->mpu_wdt_ick = NULL;
	}
	if (wdev->mpu_wdt_fck) {
		clk_put(wdev->mpu_wdt_fck);
		wdev->mpu_wdt_fck = NULL;
	}
413 414
	iounmap(wdev->base);

415 416
	kfree(wdev);
	omap_wdt_dev = NULL;
417 418 419 420 421 422 423 424 425 426 427 428 429
	return 0;
}

#ifdef	CONFIG_PM

/* REVISIT ... not clear this is the best way to handle system suspend; and
 * it's very inappropriate for selective device suspend (e.g. suspending this
 * through sysfs rather than by stopping the watchdog daemon).  Also, this
 * may not play well enough with NOWAYOUT...
 */

static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
{
430 431 432 433
	struct omap_wdt_dev *wdev;
	wdev = platform_get_drvdata(pdev);
	if (wdev->omap_wdt_users)
		omap_wdt_disable(wdev);
434 435 436 437 438
	return 0;
}

static int omap_wdt_resume(struct platform_device *pdev)
{
439 440 441 442 443
	struct omap_wdt_dev *wdev;
	wdev = platform_get_drvdata(pdev);
	if (wdev->omap_wdt_users) {
		omap_wdt_enable(wdev);
		omap_wdt_ping(wdev);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	}
	return 0;
}

#else
#define	omap_wdt_suspend	NULL
#define	omap_wdt_resume		NULL
#endif

static struct platform_driver omap_wdt_driver = {
	.probe		= omap_wdt_probe,
	.remove		= omap_wdt_remove,
	.shutdown	= omap_wdt_shutdown,
	.suspend	= omap_wdt_suspend,
	.resume		= omap_wdt_resume,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "omap_wdt",
	},
};

static int __init omap_wdt_init(void)
{
467
	spin_lock_init(&wdt_lock);
468 469 470 471 472 473 474 475 476 477 478 479 480 481
	return platform_driver_register(&omap_wdt_driver);
}

static void __exit omap_wdt_exit(void)
{
	platform_driver_unregister(&omap_wdt_driver);
}

module_init(omap_wdt_init);
module_exit(omap_wdt_exit);

MODULE_AUTHOR("George G. Davis");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
482
MODULE_ALIAS("platform:omap_wdt");