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