Add ar7-2.6 port (marked as broken for now).
[librecmc/librecmc.git] / target / linux / ar7-2.6 / 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
37 #include <asm/addrspace.h>
38 #include <asm/io.h>
39 #include <asm/uaccess.h>
40
41 #include <asm/ar7/ar7.h>
42
43 #define DRVNAME "ar7_wdt"
44 #define LONGNAME "TI AR7 Watchdog Timer"
45
46 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
47 MODULE_DESCRIPTION(LONGNAME);
48 MODULE_LICENSE("GPL");
49 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
50
51 static int margin = 60;
52 module_param(margin, int, 0);
53 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
54
55 static int nowayout = WATCHDOG_NOWAYOUT;
56 module_param(nowayout, int, 0);
57 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
58
59 typedef struct {
60         u32 kick_lock;
61         u32 kick;
62         u32 change_lock;
63         u32 change ;
64         u32 disable_lock;
65         u32 disable;
66         u32 prescale_lock;
67         u32 prescale;
68 } ar7_wdt_t;
69
70 static struct semaphore open_semaphore;
71 static unsigned expect_close;
72
73 /* XXX correct? assumed to be sysfreq/2. get this dynamically ... */
74 #define vbus_freq (ar7_bus_freq() / 2)
75
76 /* XXX currently fixed, allows max margin ~68.72 secs */
77 #define prescale_value 0xFFFF
78
79 static void ar7_wdt_kick(u32 value)
80 {
81         volatile ar7_wdt_t *ar7_wdt = (ar7_wdt_t *)ioremap(AR7_REGS_WDT, sizeof(ar7_wdt_t));
82
83         ar7_wdt->kick_lock = 0x5555;
84         if ((ar7_wdt->kick_lock & 3) == 1) {
85                 ar7_wdt->kick_lock = 0xAAAA;
86                 if ((ar7_wdt->kick_lock & 3) == 3) {
87                         ar7_wdt->kick = value;
88                         return;
89                 }
90         }
91         printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
92 }
93
94 static void ar7_wdt_prescale(u32 value)
95 {
96         volatile ar7_wdt_t *ar7_wdt = (ar7_wdt_t *)ioremap(AR7_REGS_WDT, sizeof(ar7_wdt_t));
97
98         ar7_wdt->prescale_lock = 0x5A5A;
99         if ((ar7_wdt->prescale_lock & 3) == 1) {
100                 ar7_wdt->prescale_lock = 0xA5A5;
101                 if ((ar7_wdt->prescale_lock & 3) == 3) {
102                         ar7_wdt->prescale = value;
103                         return;
104                 }
105         }
106         printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
107 }
108
109 static void ar7_wdt_change(u32 value)
110 {
111         volatile ar7_wdt_t *ar7_wdt = (ar7_wdt_t *)ioremap(AR7_REGS_WDT, sizeof(ar7_wdt_t));
112
113         ar7_wdt->change_lock = 0x6666;
114         if ((ar7_wdt->change_lock & 3) == 1) {
115                 ar7_wdt->change_lock = 0xBBBB;
116                 if ((ar7_wdt->change_lock & 3) == 3) {
117                         ar7_wdt->change = value;
118                         return;
119                 }
120         }
121         printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
122 }
123
124 static void ar7_wdt_disable(u32 value)
125 {
126         volatile ar7_wdt_t *ar7_wdt = (ar7_wdt_t *)ioremap(AR7_REGS_WDT, sizeof(ar7_wdt_t));
127
128         ar7_wdt->disable_lock = 0x7777;
129         if ((ar7_wdt->disable_lock & 3) == 1) {
130                 ar7_wdt->disable_lock = 0xCCCC;
131                 if ((ar7_wdt->disable_lock & 3) == 2) {
132                         ar7_wdt->disable_lock = 0xDDDD; 
133                         if ((ar7_wdt->disable_lock & 3) == 3) {
134                                 ar7_wdt->disable = value;
135                                 return;
136                         }
137                 }
138         }
139         printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
140 }
141
142 static void ar7_wdt_update_margin(int new_margin)
143 {
144         u32 change;
145
146         change = new_margin * (vbus_freq / prescale_value);
147         if (change < 1) change = 1;
148         if (change > 0xFFFF) change = 0xFFFF;
149         ar7_wdt_change(change);
150         margin = change * prescale_value / vbus_freq;
151         printk(KERN_INFO DRVNAME
152                ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
153                margin, prescale_value, change, vbus_freq);
154 }
155
156 static void ar7_wdt_enable_wdt(void)
157 {
158         printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
159         ar7_wdt_disable(1);
160         ar7_wdt_kick(1);
161 }
162
163 static void ar7_wdt_disable_wdt(void)
164 {
165         printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
166         ar7_wdt_disable(0);
167 }
168
169 static int ar7_wdt_open(struct inode *inode, struct file *file)
170 {
171         /* only allow one at a time */
172         if (down_trylock(&open_semaphore))
173                 return -EBUSY;
174         ar7_wdt_enable_wdt();
175         expect_close = 0;
176
177         return 0;
178 }
179
180 static int ar7_wdt_release(struct inode *inode, struct file *file)
181 {
182         if (!expect_close) {
183                 printk(KERN_WARNING DRVNAME ": watchdog device closed unexpectedly, will not disable the watchdog timer\n");
184         } else if (!nowayout) {
185                 ar7_wdt_disable_wdt();
186         }
187         up(&open_semaphore);
188
189         return 0;
190 }
191
192 static int ar7_wdt_notify_sys(struct notifier_block *this, 
193                               unsigned long code, void *unused)
194 {
195         if (code == SYS_HALT || code == SYS_POWER_OFF)
196                 if (!nowayout)
197                         ar7_wdt_disable_wdt();
198
199         return NOTIFY_DONE;
200 }
201
202 static struct notifier_block ar7_wdt_notifier =
203 {
204         .notifier_call = ar7_wdt_notify_sys
205 };
206
207 static ssize_t ar7_wdt_write(struct file *file, const char *data, 
208                              size_t len, loff_t *ppos)
209 {
210         if (ppos != &file->f_pos)
211                 return -ESPIPE;
212
213         /* check for a magic close character */
214         if (len) {
215                 size_t i;
216
217                 ar7_wdt_kick(1);
218
219                 expect_close = 0;
220                 for (i = 0; i < len; ++i) {
221                         char c;
222                         if (get_user(c, data+i))
223                                 return -EFAULT;
224                         if (c == 'V')
225                                 expect_close = 1;
226                 }
227
228         }
229         return len;
230 }
231
232 static int ar7_wdt_ioctl(struct inode *inode, struct file *file, 
233                          unsigned int cmd, unsigned long arg)
234 {
235         static struct watchdog_info ident = {
236                 .identity = LONGNAME,
237                 .firmware_version = 1, 
238                 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING),
239         };
240         int new_margin;
241         
242         switch (cmd) {
243         default:
244                 return -ENOTTY;
245         case WDIOC_GETSUPPORT:
246                 if(copy_to_user((struct watchdog_info *)arg, &ident, 
247                                 sizeof(ident)))
248                         return -EFAULT;
249                 return 0;
250         case WDIOC_GETSTATUS:
251         case WDIOC_GETBOOTSTATUS:
252                 if (put_user(0, (int *)arg))
253                         return -EFAULT;
254                 return 0;
255         case WDIOC_KEEPALIVE:
256                 ar7_wdt_kick(1);
257                 return 0;
258         case WDIOC_SETTIMEOUT:
259                 if (get_user(new_margin, (int *)arg))
260                         return -EFAULT;
261                 if (new_margin < 1)
262                         return -EINVAL;
263
264                 ar7_wdt_update_margin(new_margin);
265                 ar7_wdt_kick(1);
266
267         case WDIOC_GETTIMEOUT:
268                 if (put_user(margin, (int *)arg))
269                         return -EFAULT;
270                 return 0;
271         }
272 }
273
274 static struct file_operations ar7_wdt_fops = {
275         .owner   = THIS_MODULE,
276         .write   = ar7_wdt_write,
277         .ioctl   = ar7_wdt_ioctl,
278         .open    = ar7_wdt_open,
279         .release = ar7_wdt_release,
280 };
281
282 static struct miscdevice ar7_wdt_miscdev = {
283         .minor = WATCHDOG_MINOR,
284         .name  = "watchdog",
285         .fops  = &ar7_wdt_fops,
286 };
287
288 static int __init ar7_wdt_init(void)
289 {
290         int rc;
291
292         if (!request_mem_region(AR7_REGS_WDT, sizeof(ar7_wdt_t), LONGNAME)) {
293                 printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
294                 return -EBUSY;
295         }
296
297         ar7_wdt_disable_wdt();
298         ar7_wdt_prescale(prescale_value);
299         ar7_wdt_update_margin(margin);
300
301         sema_init(&open_semaphore, 1);
302
303         rc = misc_register(&ar7_wdt_miscdev);
304         if (rc) {
305                 printk(KERN_ERR DRVNAME ": unable to register misc device\n");
306                 goto out_alloc;
307         }
308
309         rc = register_reboot_notifier(&ar7_wdt_notifier);
310         if (rc) {
311                 printk(KERN_ERR DRVNAME ": unable to register reboot notifier\n");
312                 goto out_register;
313         }
314         goto out;
315
316 out_register:
317         misc_deregister(&ar7_wdt_miscdev);
318 out_alloc:
319         release_mem_region(AR7_REGS_WDT, sizeof(ar7_wdt_t));
320 out:
321         return rc;
322 }
323
324 static void __exit ar7_wdt_cleanup(void)
325 {
326         unregister_reboot_notifier(&ar7_wdt_notifier);
327         misc_deregister(&ar7_wdt_miscdev);
328         release_mem_region(AR7_REGS_WDT, sizeof(ar7_wdt_t));
329 }
330
331 module_init(ar7_wdt_init);
332 module_exit(ar7_wdt_cleanup);