2 * Driver for the MTX-1 Watchdog.
4 * (c) Copyright 2005 4G Systems <info@4g-systems.biz>, All Rights Reserved.
5 * http://www.4g-systems.biz
7 * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Neither Michael Stickel nor 4G Systems admit liability nor provide
15 * warranty for any of this software. This material is provided
16 * "AS-IS" and at no charge.
18 * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
22 * Author: Michael Stickel michael.stickel@4g-systems.biz
25 * Author: Florian Fainelli florian@openwrt.org
26 * use the Linux watchdog API
28 * The Watchdog is configured to reset the MTX-1
29 * if it is not triggered for 100 seconds.
30 * It should not be triggered more often than 1.6 seconds.
32 * A timer triggers the watchdog every 5 seconds, until
33 * it is opened for the first time. After the first open
34 * it MUST be triggered every 2..95 seconds.
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/types.h>
40 #include <linux/errno.h>
41 #include <linux/miscdevice.h>
43 #include <linux/init.h>
44 #include <linux/ioport.h>
45 #include <linux/timer.h>
46 #include <linux/completion.h>
47 #include <linux/jiffies.h>
48 #include <linux/watchdog.h>
50 #include <asm/uaccess.h>
52 #include <asm/mach-au1x00/au1000.h>
54 #define MTX1_WDT_INTERVAL (5 * HZ)
56 static int ticks
= 100 * HZ
;
59 struct completion stop
;
61 struct timer_list timer
;
67 static void mtx1_wdt_trigger(unsigned long unused
)
71 if (mtx1_wdt_device
.running
)
76 tmp
= au_readl(GPIO2_DIR
);
77 tmp
= (tmp
& ~(1<<15)) | ((~tmp
) & (1<<15));
78 au_writel (tmp
, GPIO2_DIR
);
80 if (mtx1_wdt_device
.queue
&& ticks
)
81 mod_timer(&mtx1_wdt_device
.timer
, jiffies
+ MTX1_WDT_INTERVAL
);
83 complete(&mtx1_wdt_device
.stop
);
87 static void mtx1_wdt_reset(void)
89 ticks
= mtx1_wdt_device
.default_ticks
;
93 static void mtx1_wdt_start(void)
95 if (!mtx1_wdt_device
.queue
) {
96 mtx1_wdt_device
.queue
= 1;
97 au_writel (au_readl(GPIO2_DIR
) | (u32
)(1<<15), GPIO2_DIR
);
98 mod_timer(&mtx1_wdt_device
.timer
, jiffies
+ MTX1_WDT_INTERVAL
);
100 mtx1_wdt_device
.running
++;
103 static int mtx1_wdt_stop(void)
105 if (mtx1_wdt_device
.queue
) {
106 mtx1_wdt_device
.queue
= 0;
107 au_writel (au_readl(GPIO2_DIR
) & ~((u32
)(1<<15)), GPIO2_DIR
);
110 ticks
= mtx1_wdt_device
.default_ticks
;
115 /* Filesystem functions */
117 static char restart_after_close
;
119 static int mtx1_wdt_open(struct inode
*inode
, struct file
*file
)
121 if (test_and_set_bit(0, &mtx1_wdt_device
.inuse
))
124 return nonseekable_open(inode
, file
);
128 static int mtx1_wdt_release(struct inode
*inode
, struct file
*file
)
130 clear_bit(0, &mtx1_wdt_device
.inuse
);
134 static int mtx1_wdt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
136 void __user
*argp
= (void __user
*)arg
;
138 static struct watchdog_info ident
=
140 .options
= WDIOF_CARDRESET
,
141 .identity
= "MTX-1 WDT",
145 case WDIOC_KEEPALIVE
:
148 case WDIOC_GETSTATUS
:
149 if ( copy_to_user(argp
, &value
, sizeof(int)) )
152 case WDIOC_GETSUPPORT
:
153 if ( copy_to_user(argp
, &ident
, sizeof(ident
)) )
156 case WDIOC_SETOPTIONS
:
157 if ( copy_from_user(&value
, argp
, sizeof(int)) )
160 case WDIOS_ENABLECARD
:
163 case WDIOS_DISABLECARD
:
164 return mtx1_wdt_stop();
176 static ssize_t
mtx1_wdt_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
185 int n
= (count
>9)?9:count
;
187 if (copy_from_user (&buffer
, buf
, n
))
191 if (count
>= 4 && strncmp("auto", buffer
, 4)==0)
192 restart_after_close
= 1;
194 else if (count
>= 6 && strncmp("manual", buffer
, 6)==0)
195 restart_after_close
= 0;
203 static ssize_t
mtx1_wdt_read (struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
205 char * state
= restart_after_close
? "auto\n" : "manual\n";
206 int n
= strlen(state
)+1;
208 if (file
->f_pos
>= n
)
214 if(copy_to_user(buf
, state
, n
))
223 static struct file_operations mtx1_wdt_fops
= {
224 .owner
= THIS_MODULE
,
226 .ioctl
= mtx1_wdt_ioctl
,
227 .open
= mtx1_wdt_open
,
228 .write
= mtx1_wdt_write
,
229 //.read = mtx1_wdt_read,
230 .release
= mtx1_wdt_release
234 static struct miscdevice mtx1_wdt_misc
= {
235 .minor
= WATCHDOG_MINOR
,
237 .fops
= &mtx1_wdt_fops
241 static int __init
mtx1_wdt_init(void)
245 if ((ret
= misc_register(&mtx1_wdt_misc
)) < 0) {
246 printk(KERN_ERR
" mtx-1_wdt : failed to register\n");
250 init_completion(&mtx1_wdt_device
.stop
);
251 mtx1_wdt_device
.queue
= 0;
253 clear_bit(0, &mtx1_wdt_device
.inuse
);
255 setup_timer(&mtx1_wdt_device
.timer
, mtx1_wdt_trigger
, 0L);
257 mtx1_wdt_device
.default_ticks
= ticks
;
261 printk(KERN_INFO
"MTX-1 Watchdog driver\n");
266 static void __exit
mtx1_wdt_exit(void)
268 if (mtx1_wdt_device
.queue
) {
269 mtx1_wdt_device
.queue
= 0;
270 wait_for_completion(&mtx1_wdt_device
.stop
);
272 misc_deregister(&mtx1_wdt_misc
);
275 module_init(mtx1_wdt_init
);
276 module_exit(mtx1_wdt_exit
);
278 MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
279 MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
280 MODULE_LICENSE("GPL");