e92a8984312539b49cdb72724288dfbd400b2ad2
[openwrt/staging/wigyori.git] / target / linux / adm5120 / files-2.6.26 / drivers / watchdog / adm5120_wdt.c
1 /*
2 * ADM5120_WDT 0.01: Infineon ADM5120 SoC watchdog driver
3 * Copyright (c) Ondrej Zajicek <santiago@crfreenet.org>, 2007
4 *
5 * based on
6 *
7 * RC32434_WDT 0.01: IDT Interprise 79RC32434 watchdog driver
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 */
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/fs.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/irq.h>
21
22 #include <asm/bootinfo.h>
23
24 #include <asm/mach-adm5120/adm5120_info.h>
25 #include <asm/mach-adm5120/adm5120_defs.h>
26 #include <asm/mach-adm5120/adm5120_switch.h>
27
28 #define DEFAULT_TIMEOUT 15 /* (secs) Default is 15 seconds */
29 #define MAX_TIMEOUT 327
30 /* Max is 327 seconds, counter is 15-bit integer, step is 10 ms */
31
32 #define NAME "adm5120_wdt"
33 #define VERSION "0.1"
34
35 static int expect_close = 0;
36 static int access = 0;
37 static unsigned int timeout = DEFAULT_TIMEOUT;
38
39 static int nowayout = WATCHDOG_NOWAYOUT;
40 module_param(nowayout, int, 0);
41 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42 MODULE_LICENSE("GPL");
43
44
45 static inline void wdt_set_timeout(void)
46 {
47 u32 val = (1 << 31) | (((timeout * 100) & 0x7FFF) << 16);
48 SW_WRITE_REG(SWITCH_REG_WDOG0, val);
49 }
50
51 /*
52 It looks like WDOG0-register-write don't modify counter,
53 but WDOG0-register-read resets counter.
54 */
55
56 static inline void wdt_reset_counter(void)
57 {
58 SW_READ_REG(SWITCH_REG_WDOG0);
59 }
60
61 static inline void wdt_disable(void)
62 {
63 SW_WRITE_REG(SWITCH_REG_WDOG0, 0x7FFF0000);
64 }
65
66
67
68 static int wdt_open(struct inode *inode, struct file *file)
69 {
70 /* Allow only one person to hold it open */
71 if (access)
72 return -EBUSY;
73
74 if (nowayout) {
75 __module_get(THIS_MODULE);
76 }
77
78 /* Activate timer */
79 wdt_reset_counter();
80 wdt_set_timeout();
81 printk(KERN_INFO NAME ": enabling watchdog timer\n");
82 access = 1;
83 return 0;
84 }
85
86 static int wdt_release(struct inode *inode, struct file *file)
87 {
88 /*
89 * Shut off the timer.
90 * Lock it in if it's a module and we set nowayout
91 */
92 if (expect_close && (nowayout == 0)) {
93 wdt_disable();
94 printk(KERN_INFO NAME ": disabling watchdog timer\n");
95 module_put(THIS_MODULE);
96 } else {
97 printk(KERN_CRIT NAME ": device closed unexpectedly. WDT will not stop!\n");
98 }
99 access = 0;
100 return 0;
101 }
102
103 static ssize_t wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
104 {
105 /* Refresh the timer. */
106 if (len) {
107 if (!nowayout) {
108 size_t i;
109
110 /* In case it was set long ago */
111 expect_close = 0;
112
113 for (i = 0; i != len; i++) {
114 char c;
115 if (get_user(c, data + i))
116 return -EFAULT;
117 if (c == 'V')
118 expect_close = 1;
119 }
120 }
121 wdt_reset_counter();
122 return len;
123 }
124 return 0;
125 }
126
127 static int wdt_ioctl(struct inode *inode, struct file *file,
128 unsigned int cmd, unsigned long arg)
129 {
130 int new_timeout;
131 static struct watchdog_info ident = {
132 .options = WDIOF_SETTIMEOUT |
133 WDIOF_KEEPALIVEPING |
134 WDIOF_MAGICCLOSE,
135 .firmware_version = 0,
136 .identity = "ADM5120_WDT Watchdog",
137 };
138 switch (cmd) {
139 default:
140 return -ENOTTY;
141 case WDIOC_GETSUPPORT:
142 if(copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
143 return -EFAULT;
144 return 0;
145 case WDIOC_GETSTATUS:
146 case WDIOC_GETBOOTSTATUS:
147 return put_user(0,(int *)arg);
148 case WDIOC_KEEPALIVE:
149 wdt_reset_counter();
150 return 0;
151 case WDIOC_SETTIMEOUT:
152 if (get_user(new_timeout, (int *)arg))
153 return -EFAULT;
154 if (new_timeout < 1)
155 return -EINVAL;
156 if (new_timeout > MAX_TIMEOUT)
157 return -EINVAL;
158 timeout = new_timeout;
159 wdt_set_timeout();
160 /* Fall */
161 case WDIOC_GETTIMEOUT:
162 return put_user(timeout, (int *)arg);
163 }
164 }
165
166 static struct file_operations wdt_fops = {
167 owner: THIS_MODULE,
168 llseek: no_llseek,
169 write: wdt_write,
170 ioctl: wdt_ioctl,
171 open: wdt_open,
172 release: wdt_release,
173 };
174
175 static struct miscdevice wdt_miscdev = {
176 minor: WATCHDOG_MINOR,
177 name: "watchdog",
178 fops: &wdt_fops,
179 };
180
181 static char banner[] __initdata = KERN_INFO NAME ": Watchdog Timer version " VERSION "\n";
182
183 static int __init watchdog_init(void)
184 {
185 int ret;
186
187 ret = misc_register(&wdt_miscdev);
188
189 if (ret)
190 return ret;
191
192 wdt_disable();
193 printk(banner);
194
195 return 0;
196 }
197
198 static void __exit watchdog_exit(void)
199 {
200 misc_deregister(&wdt_miscdev);
201 }
202
203 module_init(watchdog_init);
204 module_exit(watchdog_exit);