Enabled working ar7_wdt, style cleanups
[openwrt/svn-archive/archive.git] / target / linux / ar7 / files / drivers / char / watchdog / ar7_wdt.c
1 /*
2 * linux/drivers/char/ar7_wdt.c
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
6 *
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
34 #include <linux/fs.h>
35 #include <linux/ioport.h>
36 #include <linux/io.h>
37 #include <linux/uaccess.h>
38
39 #include <asm/addrspace.h>
40 #include <asm/ar7/ar7.h>
41
42 #define DRVNAME "ar7_wdt"
43 #define LONGNAME "TI AR7 Watchdog Timer"
44
45 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
46 MODULE_DESCRIPTION(LONGNAME);
47 MODULE_LICENSE("GPL");
48 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
49
50 static int margin = 60;
51 module_param(margin, int, 0);
52 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
53
54 static int nowayout = WATCHDOG_NOWAYOUT;
55 module_param(nowayout, int, 0);
56 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
57
58 #define READ_REG(x) readl((void __iomem *)&(x))
59 #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
60
61 struct ar7_wdt {
62 u32 kick_lock;
63 u32 kick;
64 u32 change_lock;
65 u32 change;
66 u32 disable_lock;
67 u32 disable;
68 u32 prescale_lock;
69 u32 prescale;
70 };
71
72 static struct semaphore open_semaphore;
73 static unsigned expect_close;
74
75 /* XXX currently fixed, allows max margin ~68.72 secs */
76 #define prescale_value 0xffff
77
78 /* Offset of the WDT registers */
79 static unsigned long ar7_regs_wdt;
80 /* Pointer to the remapped WDT IO space */
81 static struct ar7_wdt *ar7_wdt;
82 static void ar7_wdt_get_regs(void)
83 {
84 u16 chip_id = ar7_chip_id();
85 switch (chip_id) {
86 case AR7_CHIP_7100:
87 case AR7_CHIP_7200:
88 ar7_regs_wdt = AR7_REGS_WDT;
89 break;
90 default:
91 ar7_regs_wdt = UR8_REGS_WDT;
92 break;
93 }
94 }
95
96
97 static void ar7_wdt_kick(u32 value)
98 {
99 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
100 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
101 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
102 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
103 WRITE_REG(ar7_wdt->kick, value);
104 return;
105 }
106 }
107 printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
108 }
109
110 static void ar7_wdt_prescale(u32 value)
111 {
112 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
113 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
114 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
115 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
116 WRITE_REG(ar7_wdt->prescale, value);
117 return;
118 }
119 }
120 printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
121 }
122
123 static void ar7_wdt_change(u32 value)
124 {
125 WRITE_REG(ar7_wdt->change_lock, 0x6666);
126 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
127 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
128 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
129 WRITE_REG(ar7_wdt->change, value);
130 return;
131 }
132 }
133 printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
134 }
135
136 static void ar7_wdt_disable(u32 value)
137 {
138 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
139 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
140 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
141 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
142 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
143 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
144 WRITE_REG(ar7_wdt->disable, value);
145 return;
146 }
147 }
148 }
149 printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
150 }
151
152 static void ar7_wdt_update_margin(int new_margin)
153 {
154 u32 change;
155
156 change = new_margin * (ar7_vbus_freq() / prescale_value);
157 if (change < 1) change = 1;
158 if (change > 0xffff) change = 0xffff;
159 ar7_wdt_change(change);
160 margin = change * prescale_value / ar7_vbus_freq();
161 printk(KERN_INFO DRVNAME
162 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
163 margin, prescale_value, change, ar7_vbus_freq());
164 }
165
166 static void ar7_wdt_enable_wdt(void)
167 {
168 printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
169 ar7_wdt_disable(1);
170 ar7_wdt_kick(1);
171 }
172
173 static void ar7_wdt_disable_wdt(void)
174 {
175 printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
176 ar7_wdt_disable(0);
177 }
178
179 static int ar7_wdt_open(struct inode *inode, struct file *file)
180 {
181 /* only allow one at a time */
182 if (down_trylock(&open_semaphore))
183 return -EBUSY;
184 ar7_wdt_enable_wdt();
185 expect_close = 0;
186
187 return 0;
188 }
189
190 static int ar7_wdt_release(struct inode *inode, struct file *file)
191 {
192 if (!expect_close)
193 printk(KERN_WARNING DRVNAME
194 ": watchdog device closed unexpectedly,"
195 "will not disable the watchdog timer\n");
196 else if (!nowayout)
197 ar7_wdt_disable_wdt();
198
199 up(&open_semaphore);
200
201 return 0;
202 }
203
204 static int ar7_wdt_notify_sys(struct notifier_block *this,
205 unsigned long code, void *unused)
206 {
207 if (code == SYS_HALT || code == SYS_POWER_OFF)
208 if (!nowayout)
209 ar7_wdt_disable_wdt();
210
211 return NOTIFY_DONE;
212 }
213
214 static struct notifier_block ar7_wdt_notifier = {
215 .notifier_call = ar7_wdt_notify_sys
216 };
217
218 static ssize_t ar7_wdt_write(struct file *file, const char *data,
219 size_t len, loff_t *ppos)
220 {
221 if (ppos != &file->f_pos)
222 return -ESPIPE;
223
224 /* check for a magic close character */
225 if (len) {
226 size_t i;
227
228 ar7_wdt_kick(1);
229
230 expect_close = 0;
231 for (i = 0; i < len; ++i) {
232 char c;
233 if (get_user(c, data+i))
234 return -EFAULT;
235 if (c == 'V')
236 expect_close = 1;
237 }
238
239 }
240 return len;
241 }
242
243 static int ar7_wdt_ioctl(struct inode *inode, struct file *file,
244 unsigned int cmd, unsigned long arg)
245 {
246 static struct watchdog_info ident = {
247 .identity = LONGNAME,
248 .firmware_version = 1,
249 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING),
250 };
251 int new_margin;
252
253 switch (cmd) {
254 default:
255 return -ENOTTY;
256 case WDIOC_GETSUPPORT:
257 if (copy_to_user((struct watchdog_info *)arg, &ident,
258 sizeof(ident)))
259 return -EFAULT;
260 return 0;
261 case WDIOC_GETSTATUS:
262 case WDIOC_GETBOOTSTATUS:
263 if (put_user(0, (int *)arg))
264 return -EFAULT;
265 return 0;
266 case WDIOC_KEEPALIVE:
267 ar7_wdt_kick(1);
268 return 0;
269 case WDIOC_SETTIMEOUT:
270 if (get_user(new_margin, (int *)arg))
271 return -EFAULT;
272 if (new_margin < 1)
273 return -EINVAL;
274
275 ar7_wdt_update_margin(new_margin);
276 ar7_wdt_kick(1);
277
278 case WDIOC_GETTIMEOUT:
279 if (put_user(margin, (int *)arg))
280 return -EFAULT;
281 return 0;
282 }
283 }
284
285 static struct file_operations ar7_wdt_fops = {
286 .owner = THIS_MODULE,
287 .write = ar7_wdt_write,
288 .ioctl = ar7_wdt_ioctl,
289 .open = ar7_wdt_open,
290 .release = ar7_wdt_release,
291 };
292
293 static struct miscdevice ar7_wdt_miscdev = {
294 .minor = WATCHDOG_MINOR,
295 .name = "watchdog",
296 .fops = &ar7_wdt_fops,
297 };
298
299 static int __init ar7_wdt_init(void)
300 {
301 int rc;
302
303 ar7_wdt_get_regs();
304
305 if (!request_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt),
306 LONGNAME)) {
307 printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
308 return -EBUSY;
309 }
310
311 ar7_wdt = (struct ar7_wdt *)
312 ioremap(ar7_regs_wdt, sizeof(struct ar7_wdt));
313
314 ar7_wdt_disable_wdt();
315 ar7_wdt_prescale(prescale_value);
316 ar7_wdt_update_margin(margin);
317
318 sema_init(&open_semaphore, 1);
319
320 rc = misc_register(&ar7_wdt_miscdev);
321 if (rc) {
322 printk(KERN_ERR DRVNAME ": unable to register misc device\n");
323 goto out_alloc;
324 }
325
326 rc = register_reboot_notifier(&ar7_wdt_notifier);
327 if (rc) {
328 printk(KERN_ERR DRVNAME
329 ": unable to register reboot notifier\n");
330 goto out_register;
331 }
332 goto out;
333
334 out_register:
335 misc_deregister(&ar7_wdt_miscdev);
336 out_alloc:
337 release_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt));
338 out:
339 return rc;
340 }
341
342 static void __exit ar7_wdt_cleanup(void)
343 {
344 unregister_reboot_notifier(&ar7_wdt_notifier);
345 misc_deregister(&ar7_wdt_miscdev);
346 iounmap(ar7_wdt);
347 release_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt));
348 }
349
350 module_init(ar7_wdt_init);
351 module_exit(ar7_wdt_cleanup);