mv64x60_wdt.c 6.4 KB
Newer Older
J
James Chapman 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 * 
 * 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 25
#include <linux/platform_device.h>

26
#include <linux/mv643xx.h>
J
James Chapman 已提交
27 28 29
#include <asm/uaccess.h>
#include <asm/io.h>

30 31
#define MV64x60_WDT_WDC_OFFSET	0

J
James Chapman 已提交
32 33 34 35 36 37 38 39 40 41 42 43
/* MV64x60 WDC (config) register access definitions */
#define MV64x60_WDC_CTL1_MASK	(3 << 24)
#define MV64x60_WDC_CTL1(val)	((val & 3) << 24)
#define MV64x60_WDC_CTL2_MASK	(3 << 26)
#define MV64x60_WDC_CTL2(val)	((val & 3) << 26)

/* Flags bits */
#define MV64x60_WDOG_FLAG_OPENED	0
#define MV64x60_WDOG_FLAG_ENABLED	1

static unsigned long wdt_flags;
static int wdt_status;
44
static void __iomem *mv64x60_wdt_regs;
J
James Chapman 已提交
45
static int mv64x60_wdt_timeout;
46
static unsigned int bus_clk;
J
James Chapman 已提交
47 48 49 50 51 52

static void mv64x60_wdt_reg_write(u32 val)
{
	/* Allow write only to CTL1 / CTL2 fields, retaining values in
	 * other fields.
	 */
53
	u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
J
James Chapman 已提交
54 55
	data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
	data |= val;
56
	writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
J
James Chapman 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

static void mv64x60_wdt_service(void)
{
	/* Write 01 followed by 10 to CTL2 */
	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
}

static void mv64x60_wdt_handler_disable(void)
{
	if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
		/* Write 01 followed by 10 to CTL1 */
		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
	}
}

static void mv64x60_wdt_handler_enable(void)
{
	if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
		/* Write 01 followed by 10 to CTL1 */
		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
	}
}

86 87 88 89 90 91 92 93 94 95 96 97
static void mv64x60_wdt_set_timeout(int timeout)
{
	/* maximum bus cycle count is 0xFFFFFFFF */
	if (timeout > 0xFFFFFFFF / bus_clk)
		timeout = 0xFFFFFFFF / bus_clk;

	mv64x60_wdt_timeout = timeout;
	writel((timeout * bus_clk) >> 8,
	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
	mv64x60_wdt_service();
}

J
James Chapman 已提交
98 99 100 101 102 103 104 105
static int mv64x60_wdt_open(struct inode *inode, struct file *file)
{
	if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
		return -EBUSY;

	mv64x60_wdt_service();
	mv64x60_wdt_handler_enable();

106
	return nonseekable_open(inode, file);
J
James Chapman 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

static int mv64x60_wdt_release(struct inode *inode, struct file *file)
{
	mv64x60_wdt_service();

#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
	mv64x60_wdt_handler_disable();
#endif

	clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);

	return 0;
}

122
static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
J
James Chapman 已提交
123 124 125 126 127 128 129 130 131 132 133
				 size_t len, loff_t * ppos)
{
	if (len)
		mv64x60_wdt_service();

	return len;
}

static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
			     unsigned int cmd, unsigned long arg)
{
134
	int timeout;
135
	void __user *argp = (void __user *)arg;
J
James Chapman 已提交
136
	static struct watchdog_info info = {
137 138
		.options =	WDIOF_SETTIMEOUT	|
				WDIOF_KEEPALIVEPING,
J
James Chapman 已提交
139 140 141 142 143 144
		.firmware_version = 0,
		.identity = "MV64x60 watchdog",
	};

	switch (cmd) {
	case WDIOC_GETSUPPORT:
145
		if (copy_to_user(argp, &info, sizeof(info)))
J
James Chapman 已提交
146 147 148 149 150
			return -EFAULT;
		break;

	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
151
		if (put_user(wdt_status, (int __user *)argp))
J
James Chapman 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			return -EFAULT;
		wdt_status &= ~WDIOF_KEEPALIVEPING;
		break;

	case WDIOC_GETTEMP:
		return -EOPNOTSUPP;

	case WDIOC_SETOPTIONS:
		return -EOPNOTSUPP;

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

	case WDIOC_SETTIMEOUT:
168 169 170 171
		if (get_user(timeout, (int __user *)argp))
			return -EFAULT;
		mv64x60_wdt_set_timeout(timeout);
		/* Fall through */
J
James Chapman 已提交
172 173

	case WDIOC_GETTIMEOUT:
174
		if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
J
James Chapman 已提交
175 176 177 178
			return -EFAULT;
		break;

	default:
179
		return -ENOTTY;
J
James Chapman 已提交
180 181 182 183 184
	}

	return 0;
}

185
static const struct file_operations mv64x60_wdt_fops = {
J
James Chapman 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.write = mv64x60_wdt_write,
	.ioctl = mv64x60_wdt_ioctl,
	.open = mv64x60_wdt_open,
	.release = mv64x60_wdt_release,
};

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

200
static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
J
James Chapman 已提交
201
{
202
	struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
203
	struct resource *r;
204
	int timeout = 10;
J
James Chapman 已提交
205

206
	bus_clk = 133;			/* in MHz */
J
James Chapman 已提交
207
	if (pdata) {
208
		timeout = pdata->timeout;
J
James Chapman 已提交
209 210 211
		bus_clk = pdata->bus_clk;
	}

212 213 214 215 216 217 218
	/* 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 */

219 220 221 222 223 224 225
	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 已提交
226

227
	mv64x60_wdt_set_timeout(timeout);
J
James Chapman 已提交
228 229 230 231

	return misc_register(&mv64x60_wdt_miscdev);
}

232
static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
J
James Chapman 已提交
233 234 235 236 237 238
{
	misc_deregister(&mv64x60_wdt_miscdev);

	mv64x60_wdt_service();
	mv64x60_wdt_handler_disable();

239 240
	iounmap(mv64x60_wdt_regs);

J
James Chapman 已提交
241 242 243
	return 0;
}

244
static struct platform_driver mv64x60_wdt_driver = {
J
James Chapman 已提交
245 246
	.probe = mv64x60_wdt_probe,
	.remove = __devexit_p(mv64x60_wdt_remove),
247 248 249 250
	.driver = {
		.owner = THIS_MODULE,
		.name = MV64x60_WDT_NAME,
	},
J
James Chapman 已提交
251 252 253 254 255 256
};

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

257
	return platform_driver_register(&mv64x60_wdt_driver);
J
James Chapman 已提交
258 259 260 261
}

static void __exit mv64x60_wdt_exit(void)
{
262
	platform_driver_unregister(&mv64x60_wdt_driver);
J
James Chapman 已提交
263 264 265 266 267 268 269 270 271
}

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);