mpc8xx_wdt.c 3.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * mpc8xx_wdt.c - MPC8xx watchdog userspace interface
 *
 * Author: Florian Schirmer <jolt@tuxbox.org>
 *
 * 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>
#include <asm/8xx_immap.h>
19 20
#include <linux/uaccess.h>
#include <linux/io.h>
L
Linus Torvalds 已提交
21 22 23 24
#include <syslib/m8xx_wdt.h>

static unsigned long wdt_opened;
static int wdt_status;
25
static spinlock_t wdt_lock;
L
Linus Torvalds 已提交
26 27 28

static void mpc8xx_wdt_handler_disable(void)
{
29
	volatile uint __iomem *piscr;
30
	piscr = (uint *)&((immap_t *)IMAP_ADDR)->im_sit.sit_piscr;
L
Linus Torvalds 已提交
31

32 33 34
	if (!m8xx_has_internal_rtc)
		m8xx_wdt_stop_timer();
	else
35
		out_be32(piscr, in_be32(piscr) & ~(PISCR_PIE | PISCR_PTE));
L
Linus Torvalds 已提交
36 37 38 39 40
	printk(KERN_NOTICE "mpc8xx_wdt: keep-alive handler deactivated\n");
}

static void mpc8xx_wdt_handler_enable(void)
{
41
	volatile uint __iomem *piscr;
42
	piscr = (uint *)&((immap_t *)IMAP_ADDR)->im_sit.sit_piscr;
L
Linus Torvalds 已提交
43

44 45 46
	if (!m8xx_has_internal_rtc)
		m8xx_wdt_install_timer();
	else
47
		out_be32(piscr, in_be32(piscr) | PISCR_PIE | PISCR_PTE);
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56
	printk(KERN_NOTICE "mpc8xx_wdt: keep-alive handler activated\n");
}

static int mpc8xx_wdt_open(struct inode *inode, struct file *file)
{
	if (test_and_set_bit(0, &wdt_opened))
		return -EBUSY;
	m8xx_wdt_reset();
	mpc8xx_wdt_handler_disable();
W
Wim Van Sebroeck 已提交
57
	return nonseekable_open(inode, file);
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69
}

static int mpc8xx_wdt_release(struct inode *inode, struct file *file)
{
	m8xx_wdt_reset();
#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
	mpc8xx_wdt_handler_enable();
#endif
	clear_bit(0, &wdt_opened);
	return 0;
}

70 71
static ssize_t mpc8xx_wdt_write(struct file *file, const char *data,
						size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
72
{
73 74
	if (len) {
		spin_lock(&wdt_lock);
L
Linus Torvalds 已提交
75
		m8xx_wdt_reset();
76 77
		spin_unlock(&wdt_lock);
	}
L
Linus Torvalds 已提交
78 79 80
	return len;
}

81 82
static long mpc8xx_wdt_ioctl(struct file *file,
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
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
{
	int timeout;
	static struct watchdog_info info = {
		.options = WDIOF_KEEPALIVEPING,
		.firmware_version = 0,
		.identity = "MPC8xx watchdog",
	};

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		if (copy_to_user((void *)arg, &info, sizeof(info)))
			return -EFAULT;
		break;

	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		if (put_user(wdt_status, (int *)arg))
			return -EFAULT;
		wdt_status &= ~WDIOF_KEEPALIVEPING;
		break;

	case WDIOC_GETTEMP:
		return -EOPNOTSUPP;

	case WDIOC_SETOPTIONS:
		return -EOPNOTSUPP;

	case WDIOC_KEEPALIVE:
111
		spin_lock(&wdt_lock);
L
Linus Torvalds 已提交
112 113
		m8xx_wdt_reset();
		wdt_status |= WDIOF_KEEPALIVEPING;
114
		spin_unlock(&wdt_lock);
L
Linus Torvalds 已提交
115 116 117 118 119 120
		break;

	case WDIOC_SETTIMEOUT:
		return -EOPNOTSUPP;

	case WDIOC_GETTIMEOUT:
121
		spin_lock(&wdt_lock);
L
Linus Torvalds 已提交
122
		timeout = m8xx_wdt_get_timeout();
123
		spin_unlock(&wdt_lock);
L
Linus Torvalds 已提交
124 125 126 127 128
		if (put_user(timeout, (int *)arg))
			return -EFAULT;
		break;

	default:
129
		return -ENOTTY;
L
Linus Torvalds 已提交
130 131 132 133 134
	}

	return 0;
}

135
static const struct file_operations mpc8xx_wdt_fops = {
L
Linus Torvalds 已提交
136 137 138
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.write = mpc8xx_wdt_write,
139
	.unlocked_ioctl = mpc8xx_wdt_ioctl,
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151
	.open = mpc8xx_wdt_open,
	.release = mpc8xx_wdt_release,
};

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

static int __init mpc8xx_wdt_init(void)
{
152
	spin_lock_init(&wdt_lock);
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	return misc_register(&mpc8xx_wdt_miscdev);
}

static void __exit mpc8xx_wdt_exit(void)
{
	misc_deregister(&mpc8xx_wdt_miscdev);

	m8xx_wdt_reset();
	mpc8xx_wdt_handler_enable();
}

module_init(mpc8xx_wdt_init);
module_exit(mpc8xx_wdt_exit);

MODULE_AUTHOR("Florian Schirmer <jolt@tuxbox.org>");
MODULE_DESCRIPTION("MPC8xx watchdog driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);