add second package source for packages built from the /packages repository fix up...
[openwrt/staging/dedeckeh.git] / target / linux / au1000-2.6 / files / drivers / char / watchdog / mtx-1_watchdog.c
1 /*
2 * Driver for the MTX-1 Watchdog.
3 *
4 * (c) Copyright 2005 4G Systems <info@4g-systems.biz>, All Rights Reserved.
5 * http://www.4g-systems.biz
6 *
7 * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
8 *
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.
13 *
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.
17 *
18 * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
19 *
20 * Release 0.01.
21 *
22 * Author: Michael Stickel michael.stickel@4g-systems.biz
23 *
24 * Release 0.02.
25 * Author: Florian Fainelli florian@openwrt.org
26 * use the Linux watchdog API
27 *
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.
31 *
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.
35 */
36
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>
42 #include <linux/fs.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>
49 #include <asm/io.h>
50 #include <asm/uaccess.h>
51
52 #include <asm/mach-au1x00/au1000.h>
53
54 #define MTX1_WDT_INTERVAL (5 * HZ)
55
56 static int ticks = 100 * HZ;
57
58 static struct {
59 struct completion stop;
60 volatile int running;
61 struct timer_list timer;
62 volatile int queue;
63 int default_ticks;
64 unsigned long inuse;
65 } mtx1_wdt_device;
66
67 static void mtx1_wdt_trigger(unsigned long unused)
68 {
69 u32 tmp;
70
71 if (mtx1_wdt_device.running)
72 ticks--;
73 /*
74 * toggle GPIO2_15
75 */
76 tmp = au_readl(GPIO2_DIR);
77 tmp = (tmp & ~(1<<15)) | ((~tmp) & (1<<15));
78 au_writel (tmp, GPIO2_DIR);
79
80 if (mtx1_wdt_device.queue && ticks)
81 mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
82 else {
83 complete(&mtx1_wdt_device.stop);
84 }
85 }
86
87 static void mtx1_wdt_reset(void)
88 {
89 ticks = mtx1_wdt_device.default_ticks;
90 }
91
92
93 static void mtx1_wdt_start(void)
94 {
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);
99 }
100 mtx1_wdt_device.running++;
101 }
102
103 static int mtx1_wdt_stop(void)
104 {
105 if (mtx1_wdt_device.queue) {
106 mtx1_wdt_device.queue = 0;
107 au_writel (au_readl(GPIO2_DIR) & ~((u32)(1<<15)), GPIO2_DIR);
108 }
109
110 ticks = mtx1_wdt_device.default_ticks;
111
112 return 0;
113 }
114
115 /* Filesystem functions */
116
117 static char restart_after_close;
118
119 static int mtx1_wdt_open(struct inode *inode, struct file *file)
120 {
121 if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
122 return -EBUSY;
123
124 return nonseekable_open(inode, file);
125 }
126
127
128 static int mtx1_wdt_release(struct inode *inode, struct file *file)
129 {
130 clear_bit(0, &mtx1_wdt_device.inuse);
131 return 0;
132 }
133
134 static int mtx1_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
135 {
136 void __user *argp = (void __user *)arg;
137 unsigned int value;
138 static struct watchdog_info ident =
139 {
140 .options = WDIOF_CARDRESET,
141 .identity = "MTX-1 WDT",
142 };
143
144 switch(cmd) {
145 case WDIOC_KEEPALIVE:
146 mtx1_wdt_reset();
147 break;
148 case WDIOC_GETSTATUS:
149 if ( copy_to_user(argp, &value, sizeof(int)) )
150 return -EFAULT;
151 break;
152 case WDIOC_GETSUPPORT:
153 if ( copy_to_user(argp, &ident, sizeof(ident)) )
154 return -EFAULT;
155 break;
156 case WDIOC_SETOPTIONS:
157 if ( copy_from_user(&value, argp, sizeof(int)) )
158 return -EFAULT;
159 switch(value) {
160 case WDIOS_ENABLECARD:
161 mtx1_wdt_start();
162 break;
163 case WDIOS_DISABLECARD:
164 return mtx1_wdt_stop();
165 default:
166 return -EINVAL;
167 }
168 break;
169 default:
170 return -ENOTTY;
171 }
172 return 0;
173 }
174
175
176 static ssize_t mtx1_wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
177 {
178 if (!count)
179 return -EIO;
180
181 mtx1_wdt_reset();
182
183 if (count > 0) {
184 char buffer[10];
185 int n = (count>9)?9:count;
186
187 if (copy_from_user (&buffer, buf, n))
188 return -EFAULT;
189 buffer[n]=0;
190
191 if (count >= 4 && strncmp("auto", buffer, 4)==0)
192 restart_after_close = 1;
193
194 else if (count >= 6 && strncmp("manual", buffer, 6)==0)
195 restart_after_close = 0;
196
197 return n;
198 }
199 return count;
200 }
201
202 #if 0
203 static ssize_t mtx1_wdt_read (struct file *file, char *buf, size_t count, loff_t *ppos)
204 {
205 char * state = restart_after_close ? "auto\n" : "manual\n";
206 int n = strlen(state)+1;
207
208 if (file->f_pos >= n)
209 return 0;
210
211 if (count < n)
212 return -EINVAL;
213
214 if(copy_to_user(buf, state, n))
215 return -EFAULT;
216
217 file->f_pos += n;
218
219 return n;
220 }
221 #endif
222
223 static struct file_operations mtx1_wdt_fops = {
224 .owner = THIS_MODULE,
225 .llseek = no_llseek,
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
231 };
232
233
234 static struct miscdevice mtx1_wdt_misc = {
235 .minor = WATCHDOG_MINOR,
236 .name = "watchdog",
237 .fops = &mtx1_wdt_fops
238 };
239
240
241 static int __init mtx1_wdt_init(void)
242 {
243 int ret;
244
245 if ((ret = misc_register(&mtx1_wdt_misc)) < 0) {
246 printk(KERN_ERR " mtx-1_wdt : failed to register\n");
247 return ret;
248 }
249
250 init_completion(&mtx1_wdt_device.stop);
251 mtx1_wdt_device.queue = 0;
252
253 clear_bit(0, &mtx1_wdt_device.inuse);
254
255 setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L);
256
257 mtx1_wdt_device.default_ticks = ticks;
258
259 mtx1_wdt_start();
260
261 printk(KERN_INFO "MTX-1 Watchdog driver\n");
262
263 return 0;
264 }
265
266 static void __exit mtx1_wdt_exit(void)
267 {
268 if (mtx1_wdt_device.queue) {
269 mtx1_wdt_device.queue = 0;
270 wait_for_completion(&mtx1_wdt_device.stop);
271 }
272 misc_deregister(&mtx1_wdt_misc);
273 }
274
275 module_init(mtx1_wdt_init);
276 module_exit(mtx1_wdt_exit);
277
278 MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
279 MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
280 MODULE_LICENSE("GPL");