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