Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / watchdog / rc32434_wdt.c
blob57027f4653ce7c56b5baf783b9c90816f50e084f
1 /*
2 * IDT Interprise 79RC32434 watchdog driver
4 * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
7 * based on
8 * SoftDog 0.05: A Software Watchdog Device
10 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/fs.h>
24 #include <linux/mm.h>
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
27 #include <linux/reboot.h>
28 #include <linux/smp_lock.h>
29 #include <linux/init.h>
30 #include <linux/platform_device.h>
31 #include <linux/uaccess.h>
33 #include <asm/bootinfo.h>
34 #include <asm/time.h>
35 #include <asm/mach-rc32434/integ.h>
37 #define MAX_TIMEOUT 20
38 #define RC32434_WDT_INTERVAL (15 * HZ)
40 #define VERSION "0.2"
42 static struct {
43 struct completion stop;
44 int running;
45 struct timer_list timer;
46 int queue;
47 int default_ticks;
48 unsigned long inuse;
49 } rc32434_wdt_device;
51 static struct integ __iomem *wdt_reg;
52 static int ticks = 100 * HZ;
54 static int expect_close;
55 static int timeout;
57 static int nowayout = WATCHDOG_NOWAYOUT;
58 module_param(nowayout, int, 0);
59 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
60 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
63 static void rc32434_wdt_start(void)
65 u32 val;
67 if (!rc32434_wdt_device.inuse) {
68 writel(0, &wdt_reg->wtcount);
70 val = RC32434_ERR_WRE;
71 writel(readl(&wdt_reg->errcs) | val, &wdt_reg->errcs);
73 val = RC32434_WTC_EN;
74 writel(readl(&wdt_reg->wtc) | val, &wdt_reg->wtc);
76 rc32434_wdt_device.running++;
79 static void rc32434_wdt_stop(void)
81 u32 val;
83 if (rc32434_wdt_device.running) {
85 val = ~RC32434_WTC_EN;
86 writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
88 val = ~RC32434_ERR_WRE;
89 writel(readl(&wdt_reg->errcs) & val, &wdt_reg->errcs);
91 rc32434_wdt_device.running = 0;
95 static void rc32434_wdt_set(int new_timeout)
97 u32 cmp = new_timeout * HZ;
98 u32 state, val;
100 timeout = new_timeout;
102 * store and disable WTC
104 state = (u32)(readl(&wdt_reg->wtc) & RC32434_WTC_EN);
105 val = ~RC32434_WTC_EN;
106 writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
108 writel(0, &wdt_reg->wtcount);
109 writel(cmp, &wdt_reg->wtcompare);
112 * restore WTC
115 writel(readl(&wdt_reg->wtc) | state, &wdt_reg);
118 static void rc32434_wdt_reset(void)
120 ticks = rc32434_wdt_device.default_ticks;
123 static void rc32434_wdt_update(unsigned long unused)
125 if (rc32434_wdt_device.running)
126 ticks--;
128 writel(0, &wdt_reg->wtcount);
130 if (rc32434_wdt_device.queue && ticks)
131 mod_timer(&rc32434_wdt_device.timer,
132 jiffies + RC32434_WDT_INTERVAL);
133 else
134 complete(&rc32434_wdt_device.stop);
137 static int rc32434_wdt_open(struct inode *inode, struct file *file)
139 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
140 return -EBUSY;
142 if (nowayout)
143 __module_get(THIS_MODULE);
145 return nonseekable_open(inode, file);
148 static int rc32434_wdt_release(struct inode *inode, struct file *file)
150 if (expect_close && nowayout == 0) {
151 rc32434_wdt_stop();
152 printk(KERN_INFO KBUILD_MODNAME ": disabling watchdog timer\n");
153 module_put(THIS_MODULE);
154 } else
155 printk(KERN_CRIT KBUILD_MODNAME
156 ": device closed unexpectedly. WDT will not stop !\n");
158 clear_bit(0, &rc32434_wdt_device.inuse);
159 return 0;
162 static ssize_t rc32434_wdt_write(struct file *file, const char *data,
163 size_t len, loff_t *ppos)
165 if (len) {
166 if (!nowayout) {
167 size_t i;
169 /* In case it was set long ago */
170 expect_close = 0;
172 for (i = 0; i != len; i++) {
173 char c;
174 if (get_user(c, data + i))
175 return -EFAULT;
176 if (c == 'V')
177 expect_close = 1;
180 rc32434_wdt_update(0);
181 return len;
183 return 0;
186 static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
187 unsigned long arg)
189 void __user *argp = (void __user *)arg;
190 int new_timeout;
191 unsigned int value;
192 static struct watchdog_info ident = {
193 .options = WDIOF_SETTIMEOUT |
194 WDIOF_KEEPALIVEPING |
195 WDIOF_MAGICCLOSE,
196 .identity = "RC32434_WDT Watchdog",
198 switch (cmd) {
199 case WDIOC_KEEPALIVE:
200 rc32434_wdt_reset();
201 break;
202 case WDIOC_GETSTATUS:
203 case WDIOC_GETBOOTSTATUS:
204 value = readl(&wdt_reg->wtcount);
205 if (copy_to_user(argp, &value, sizeof(int)))
206 return -EFAULT;
207 break;
208 case WDIOC_GETSUPPORT:
209 if (copy_to_user(argp, &ident, sizeof(ident)))
210 return -EFAULT;
211 break;
212 case WDIOC_SETOPTIONS:
213 if (copy_from_user(&value, argp, sizeof(int)))
214 return -EFAULT;
215 switch (value) {
216 case WDIOS_ENABLECARD:
217 rc32434_wdt_start();
218 break;
219 case WDIOS_DISABLECARD:
220 rc32434_wdt_stop();
221 default:
222 return -EINVAL;
224 break;
225 case WDIOC_SETTIMEOUT:
226 if (copy_from_user(&new_timeout, argp, sizeof(int)))
227 return -EFAULT;
228 if (new_timeout < 1)
229 return -EINVAL;
230 if (new_timeout > MAX_TIMEOUT)
231 return -EINVAL;
232 rc32434_wdt_set(new_timeout);
233 case WDIOC_GETTIMEOUT:
234 return copy_to_user(argp, &timeout, sizeof(int));
235 default:
236 return -ENOTTY;
239 return 0;
242 static struct file_operations rc32434_wdt_fops = {
243 .owner = THIS_MODULE,
244 .llseek = no_llseek,
245 .write = rc32434_wdt_write,
246 .unlocked_ioctl = rc32434_wdt_ioctl,
247 .open = rc32434_wdt_open,
248 .release = rc32434_wdt_release,
251 static struct miscdevice rc32434_wdt_miscdev = {
252 .minor = WATCHDOG_MINOR,
253 .name = "watchdog",
254 .fops = &rc32434_wdt_fops,
257 static char banner[] = KERN_INFO KBUILD_MODNAME
258 ": Watchdog Timer version " VERSION ", timer margin: %d sec\n";
260 static int rc32434_wdt_probe(struct platform_device *pdev)
262 int ret;
263 struct resource *r;
265 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb500_wdt_res");
266 if (!r) {
267 printk(KERN_ERR KBUILD_MODNAME
268 "failed to retrieve resources\n");
269 return -ENODEV;
272 wdt_reg = ioremap_nocache(r->start, r->end - r->start);
273 if (!wdt_reg) {
274 printk(KERN_ERR KBUILD_MODNAME
275 "failed to remap I/O resources\n");
276 return -ENXIO;
279 ret = misc_register(&rc32434_wdt_miscdev);
281 if (ret < 0) {
282 printk(KERN_ERR KBUILD_MODNAME
283 "failed to register watchdog device\n");
284 goto unmap;
287 init_completion(&rc32434_wdt_device.stop);
288 rc32434_wdt_device.queue = 0;
290 clear_bit(0, &rc32434_wdt_device.inuse);
292 setup_timer(&rc32434_wdt_device.timer, rc32434_wdt_update, 0L);
294 rc32434_wdt_device.default_ticks = ticks;
296 rc32434_wdt_start();
298 printk(banner, timeout);
300 return 0;
302 unmap:
303 iounmap(wdt_reg);
304 return ret;
307 static int rc32434_wdt_remove(struct platform_device *pdev)
309 if (rc32434_wdt_device.queue) {
310 rc32434_wdt_device.queue = 0;
311 wait_for_completion(&rc32434_wdt_device.stop);
313 misc_deregister(&rc32434_wdt_miscdev);
315 iounmap(wdt_reg);
317 return 0;
320 static struct platform_driver rc32434_wdt = {
321 .probe = rc32434_wdt_probe,
322 .remove = rc32434_wdt_remove,
323 .driver = {
324 .name = "rc32434_wdt",
328 static int __init rc32434_wdt_init(void)
330 return platform_driver_register(&rc32434_wdt);
333 static void __exit rc32434_wdt_exit(void)
335 platform_driver_unregister(&rc32434_wdt);
338 module_init(rc32434_wdt_init);
339 module_exit(rc32434_wdt_exit);
341 MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
342 "Florian Fainelli <florian@openwrt.org>");
343 MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
344 MODULE_LICENSE("GPL");
345 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);