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@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>
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>
35 #include <asm/mach-rc32434/integ.h>
37 #define MAX_TIMEOUT 20
38 #define RC32434_WDT_INTERVAL (15 * HZ)
43 struct completion stop;
45 struct timer_list timer;
51 static struct integ __iomem *wdt_reg;
52 static int ticks = 100 * HZ;
54 static int expect_close;
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)
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);
74 writel(readl(&wdt_reg->wtc) | val, &wdt_reg->wtc);
76 rc32434_wdt_device.running++;
79 static void rc32434_wdt_stop(void)
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;
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);
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)
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);
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))
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) {
152 printk(KERN_INFO KBUILD_MODNAME ": disabling watchdog timer\n");
153 module_put(THIS_MODULE);
155 printk(KERN_CRIT KBUILD_MODNAME
156 ": device closed unexpectedly. WDT will not stop !\n");
158 clear_bit(0, &rc32434_wdt_device.inuse);
162 static ssize_t rc32434_wdt_write(struct file *file, const char *data,
163 size_t len, loff_t *ppos)
169 /* In case it was set long ago */
172 for (i = 0; i != len; i++) {
174 if (get_user(c, data + i))
180 rc32434_wdt_update(0);
186 static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
189 void __user *argp = (void __user *)arg;
192 static struct watchdog_info ident = {
193 .options = WDIOF_SETTIMEOUT |
194 WDIOF_KEEPALIVEPING |
196 .identity = "RC32434_WDT Watchdog",
199 case WDIOC_KEEPALIVE:
202 case WDIOC_GETSTATUS:
203 case WDIOC_GETBOOTSTATUS:
204 value = readl(&wdt_reg->wtcount);
205 if (copy_to_user(argp, &value, sizeof(int)))
208 case WDIOC_GETSUPPORT:
209 if (copy_to_user(argp, &ident, sizeof(ident)))
212 case WDIOC_SETOPTIONS:
213 if (copy_from_user(&value, argp, sizeof(int)))
216 case WDIOS_ENABLECARD:
219 case WDIOS_DISABLECARD:
225 case WDIOC_SETTIMEOUT:
226 if (copy_from_user(&new_timeout, argp, sizeof(int)))
230 if (new_timeout > MAX_TIMEOUT)
232 rc32434_wdt_set(new_timeout);
233 case WDIOC_GETTIMEOUT:
234 return copy_to_user(argp, &timeout, sizeof(int));
242 static struct file_operations rc32434_wdt_fops = {
243 .owner = THIS_MODULE,
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,
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)
265 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb500_wdt_res");
267 printk(KERN_ERR KBUILD_MODNAME
268 "failed to retrieve resources\n");
272 wdt_reg = ioremap_nocache(r->start, r->end - r->start);
274 printk(KERN_ERR KBUILD_MODNAME
275 "failed to remap I/O resources\n");
279 ret = misc_register(&rc32434_wdt_miscdev);
282 printk(KERN_ERR KBUILD_MODNAME
283 "failed to register watchdog device\n");
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;
298 printk(banner, timeout);
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);
320 static struct platform_driver rc32434_wdt = {
321 .probe = rc32434_wdt_probe,
322 .remove = rc32434_wdt_remove,
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);