2 * IDT Interprise 79RC32434 watchdog driver
4 * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
8 * SoftDog 0.05: A Software Watchdog Device
10 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/reboot.h>
27 #include <linux/smp_lock.h>
28 #include <linux/init.h>
29 #include <linux/platform_device.h>
30 #include <linux/uaccess.h>
32 #include <asm/bootinfo.h>
34 #include <asm/mach-rc32434/integ.h>
36 #define MAX_TIMEOUT 20
37 #define RC32434_WDT_INTERVAL (15 * HZ)
42 struct completion stop;
44 struct timer_list timer;
50 static struct integ __iomem *wdt_reg;
51 static int ticks = 100 * HZ;
53 static int expect_close;
56 static int nowayout = WATCHDOG_NOWAYOUT;
57 module_param(nowayout, int, 0);
58 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
59 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
62 static void rc32434_wdt_start(void)
66 if (!rc32434_wdt_device.inuse) {
67 writel(0, &wdt_reg->wtcount);
69 val = RC32434_ERR_WRE;
70 writel(readl(&wdt_reg->errcs) | val, &wdt_reg->errcs);
73 writel(readl(&wdt_reg->wtc) | val, &wdt_reg->wtc);
75 rc32434_wdt_device.running++;
78 static void rc32434_wdt_stop(void)
82 if (rc32434_wdt_device.running) {
84 val = ~RC32434_WTC_EN;
85 writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
87 val = ~RC32434_ERR_WRE;
88 writel(readl(&wdt_reg->errcs) & val, &wdt_reg->errcs);
90 rc32434_wdt_device.running = 0;
94 static void rc32434_wdt_set(int new_timeout)
96 u32 cmp = new_timeout * HZ;
99 timeout = new_timeout;
101 * store and disable WTC
103 state = (u32)(readl(&wdt_reg->wtc) & RC32434_WTC_EN);
104 val = ~RC32434_WTC_EN;
105 writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
107 writel(0, &wdt_reg->wtcount);
108 writel(cmp, &wdt_reg->wtcompare);
114 writel(readl(&wdt_reg->wtc) | state, &wdt_reg);
117 static void rc32434_wdt_reset(void)
119 ticks = rc32434_wdt_device.default_ticks;
122 static void rc32434_wdt_update(unsigned long unused)
124 if (rc32434_wdt_device.running)
127 writel(0, &wdt_reg->wtcount);
129 if (rc32434_wdt_device.queue && ticks)
130 mod_timer(&rc32434_wdt_device.timer,
131 jiffies + RC32434_WDT_INTERVAL);
133 complete(&rc32434_wdt_device.stop);
136 static int rc32434_wdt_open(struct inode *inode, struct file *file)
138 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
142 __module_get(THIS_MODULE);
144 return nonseekable_open(inode, file);
147 static int rc32434_wdt_release(struct inode *inode, struct file *file)
149 if (expect_close && nowayout == 0) {
151 printk(KERN_INFO KBUILD_MODNAME ": disabling watchdog timer\n");
152 module_put(THIS_MODULE);
154 printk(KERN_CRIT KBUILD_MODNAME
155 ": device closed unexpectedly. WDT will not stop !\n");
157 clear_bit(0, &rc32434_wdt_device.inuse);
161 static ssize_t rc32434_wdt_write(struct file *file, const char *data,
162 size_t len, loff_t *ppos)
168 /* In case it was set long ago */
171 for (i = 0; i != len; i++) {
173 if (get_user(c, data + i))
179 rc32434_wdt_update(0);
185 static int rc32434_wdt_ioctl(struct inode *inode, struct file *file,
186 unsigned int cmd, unsigned long arg)
188 void __user *argp = (void __user *)arg;
191 static struct watchdog_info ident = {
192 .options = WDIOF_SETTIMEOUT |
193 WDIOF_KEEPALIVEPING |
195 .identity = "RC32434_WDT Watchdog",
198 case WDIOC_KEEPALIVE:
201 case WDIOC_GETSTATUS:
202 case WDIOC_GETBOOTSTATUS:
203 value = readl(&wdt_reg->wtcount);
204 if (copy_to_user(argp, &value, sizeof(int)))
207 case WDIOC_GETSUPPORT:
208 if (copy_to_user(argp, &ident, sizeof(ident)))
211 case WDIOC_SETOPTIONS:
212 if (copy_from_user(&value, argp, sizeof(int)))
215 case WDIOS_ENABLECARD:
218 case WDIOS_DISABLECARD:
224 case WDIOC_SETTIMEOUT:
225 if (copy_from_user(&new_timeout, argp, sizeof(int)))
229 if (new_timeout > MAX_TIMEOUT)
231 rc32434_wdt_set(new_timeout);
232 case WDIOC_GETTIMEOUT:
233 return copy_to_user(argp, &timeout, sizeof(int));
241 static struct file_operations rc32434_wdt_fops = {
242 .owner = THIS_MODULE,
244 .write = rc32434_wdt_write,
245 .ioctl = rc32434_wdt_ioctl,
246 .open = rc32434_wdt_open,
247 .release = rc32434_wdt_release,
250 static struct miscdevice rc32434_wdt_miscdev = {
251 .minor = WATCHDOG_MINOR,
253 .fops = &rc32434_wdt_fops,
256 static char banner[] = KERN_INFO KBUILD_MODNAME
257 ": Watchdog Timer version " VERSION ", timer margin: %d sec\n";
259 static int rc32434_wdt_probe(struct platform_device *pdev)
264 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb500_wdt_res");
266 printk(KERN_ERR KBUILD_MODNAME
267 "failed to retrieve resources\n");
271 wdt_reg = ioremap_nocache(r->start, r->end - r->start);
273 printk(KERN_ERR KBUILD_MODNAME
274 "failed to remap I/O resources\n");
278 ret = misc_register(&rc32434_wdt_miscdev);
281 printk(KERN_ERR KBUILD_MODNAME
282 "failed to register watchdog device\n");
286 init_completion(&rc32434_wdt_device.stop);
287 rc32434_wdt_device.queue = 0;
289 clear_bit(0, &rc32434_wdt_device.inuse);
291 setup_timer(&rc32434_wdt_device.timer, rc32434_wdt_update, 0L);
293 rc32434_wdt_device.default_ticks = ticks;
297 printk(banner, timeout);
306 static int rc32434_wdt_remove(struct platform_device *pdev)
308 if (rc32434_wdt_device.queue) {
309 rc32434_wdt_device.queue = 0;
310 wait_for_completion(&rc32434_wdt_device.stop);
312 misc_deregister(&rc32434_wdt_miscdev);
319 static struct platform_driver rc32434_wdt = {
320 .probe = rc32434_wdt_probe,
321 .remove = rc32434_wdt_remove,
323 .name = "rc32434_wdt",
327 static int __init rc32434_wdt_init(void)
329 return platform_driver_register(&rc32434_wdt);
332 static void __exit rc32434_wdt_exit(void)
334 platform_driver_unregister(&rc32434_wdt);
337 module_init(rc32434_wdt_init);
338 module_exit(rc32434_wdt_exit);
340 MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
341 "Florian Fainelli <florian@openwrt.org>");
342 MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
343 MODULE_LICENSE("GPL");
344 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);