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