RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / watchdog / ar7_wdt.c
blobe09eee39dc9d814f2a8446d8fead5c254de0815b
1 /*
2 * drivers/watchdog/ar7_wdt.c
4 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
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.
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.
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
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/platform_device.h>
32 #include <linux/watchdog.h>
33 #include <linux/fs.h>
34 #include <linux/ioport.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
37 #include <linux/clk.h>
39 #include <asm/addrspace.h>
40 #include <asm/mach-ar7/ar7.h>
42 #define DRVNAME "ar7_wdt"
43 #define LONGNAME "TI AR7 Watchdog Timer"
45 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
46 MODULE_DESCRIPTION(LONGNAME);
47 MODULE_LICENSE("GPL");
48 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
50 static int margin = 60;
51 module_param(margin, int, 0);
52 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
54 static int nowayout = WATCHDOG_NOWAYOUT;
55 module_param(nowayout, int, 0);
56 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
58 #define READ_REG(x) readl((void __iomem *)&(x))
59 #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
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;
72 static unsigned long wdt_is_open;
73 static spinlock_t wdt_lock;
74 static unsigned expect_close;
76 #define prescale_value 0xffff
78 /* Resource of the WDT registers */
79 static struct resource *ar7_regs_wdt;
80 /* Pointer to the remapped WDT IO space */
81 static struct ar7_wdt *ar7_wdt;
83 static struct clk *vbus_clk;
85 static void ar7_wdt_kick(u32 value)
87 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
88 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
89 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
90 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
91 WRITE_REG(ar7_wdt->kick, value);
92 return;
95 printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
98 static void ar7_wdt_prescale(u32 value)
100 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
101 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
102 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
103 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
104 WRITE_REG(ar7_wdt->prescale, value);
105 return;
108 printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
111 static void ar7_wdt_change(u32 value)
113 WRITE_REG(ar7_wdt->change_lock, 0x6666);
114 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
115 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
116 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
117 WRITE_REG(ar7_wdt->change, value);
118 return;
121 printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
124 static void ar7_wdt_disable(u32 value)
126 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
127 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
128 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
129 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
130 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
131 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
132 WRITE_REG(ar7_wdt->disable, value);
133 return;
137 printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
140 static void ar7_wdt_update_margin(int new_margin)
142 u32 change;
143 u32 vbus_rate;
145 vbus_rate = clk_get_rate(vbus_clk);
146 change = new_margin * (vbus_rate / prescale_value);
147 if (change < 1)
148 change = 1;
149 if (change > 0xffff)
150 change = 0xffff;
151 ar7_wdt_change(change);
152 margin = change * prescale_value / vbus_rate;
153 printk(KERN_INFO DRVNAME
154 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
155 margin, prescale_value, change, vbus_rate);
158 static void ar7_wdt_enable_wdt(void)
160 printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
161 ar7_wdt_disable(1);
162 ar7_wdt_kick(1);
165 static void ar7_wdt_disable_wdt(void)
167 printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
168 ar7_wdt_disable(0);
171 static int ar7_wdt_open(struct inode *inode, struct file *file)
173 /* only allow one at a time */
174 if (test_and_set_bit(0, &wdt_is_open))
175 return -EBUSY;
176 ar7_wdt_enable_wdt();
177 expect_close = 0;
179 return nonseekable_open(inode, file);
182 static int ar7_wdt_release(struct inode *inode, struct file *file)
184 if (!expect_close)
185 printk(KERN_WARNING DRVNAME
186 ": watchdog device closed unexpectedly,"
187 "will not disable the watchdog timer\n");
188 else if (!nowayout)
189 ar7_wdt_disable_wdt();
190 clear_bit(0, &wdt_is_open);
191 return 0;
194 static ssize_t ar7_wdt_write(struct file *file, const char *data,
195 size_t len, loff_t *ppos)
197 /* check for a magic close character */
198 if (len) {
199 size_t i;
201 spin_lock(&wdt_lock);
202 ar7_wdt_kick(1);
203 spin_unlock(&wdt_lock);
205 expect_close = 0;
206 for (i = 0; i < len; ++i) {
207 char c;
208 if (get_user(c, data + i))
209 return -EFAULT;
210 if (c == 'V')
211 expect_close = 1;
215 return len;
218 static long ar7_wdt_ioctl(struct file *file,
219 unsigned int cmd, unsigned long arg)
221 static const struct watchdog_info ident = {
222 .identity = LONGNAME,
223 .firmware_version = 1,
224 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
225 WDIOF_MAGICCLOSE),
227 int new_margin;
229 switch (cmd) {
230 case WDIOC_GETSUPPORT:
231 if (copy_to_user((struct watchdog_info *)arg, &ident,
232 sizeof(ident)))
233 return -EFAULT;
234 return 0;
235 case WDIOC_GETSTATUS:
236 case WDIOC_GETBOOTSTATUS:
237 if (put_user(0, (int *)arg))
238 return -EFAULT;
239 return 0;
240 case WDIOC_KEEPALIVE:
241 ar7_wdt_kick(1);
242 return 0;
243 case WDIOC_SETTIMEOUT:
244 if (get_user(new_margin, (int *)arg))
245 return -EFAULT;
246 if (new_margin < 1)
247 return -EINVAL;
249 spin_lock(&wdt_lock);
250 ar7_wdt_update_margin(new_margin);
251 ar7_wdt_kick(1);
252 spin_unlock(&wdt_lock);
254 case WDIOC_GETTIMEOUT:
255 if (put_user(margin, (int *)arg))
256 return -EFAULT;
257 return 0;
258 default:
259 return -ENOTTY;
263 static const struct file_operations ar7_wdt_fops = {
264 .owner = THIS_MODULE,
265 .write = ar7_wdt_write,
266 .unlocked_ioctl = ar7_wdt_ioctl,
267 .open = ar7_wdt_open,
268 .release = ar7_wdt_release,
271 static struct miscdevice ar7_wdt_miscdev = {
272 .minor = WATCHDOG_MINOR,
273 .name = "watchdog",
274 .fops = &ar7_wdt_fops,
277 static int __devinit ar7_wdt_probe(struct platform_device *pdev)
279 int rc;
281 spin_lock_init(&wdt_lock);
283 ar7_regs_wdt =
284 platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
285 if (!ar7_regs_wdt) {
286 printk(KERN_ERR DRVNAME ": could not get registers resource\n");
287 rc = -ENODEV;
288 goto out;
291 if (!request_mem_region(ar7_regs_wdt->start,
292 resource_size(ar7_regs_wdt), LONGNAME)) {
293 printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
294 rc = -EBUSY;
295 goto out;
298 ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
299 if (!ar7_wdt) {
300 printk(KERN_ERR DRVNAME ": could not ioremap registers\n");
301 rc = -ENXIO;
302 goto out_mem_region;
305 vbus_clk = clk_get(NULL, "vbus");
306 if (IS_ERR(vbus_clk)) {
307 printk(KERN_ERR DRVNAME ": could not get vbus clock\n");
308 rc = PTR_ERR(vbus_clk);
309 goto out_mem_region;
312 ar7_wdt_disable_wdt();
313 ar7_wdt_prescale(prescale_value);
314 ar7_wdt_update_margin(margin);
316 rc = misc_register(&ar7_wdt_miscdev);
317 if (rc) {
318 printk(KERN_ERR DRVNAME ": unable to register misc device\n");
319 goto out_alloc;
321 goto out;
323 out_alloc:
324 iounmap(ar7_wdt);
325 out_mem_region:
326 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
327 out:
328 return rc;
331 static int __devexit ar7_wdt_remove(struct platform_device *pdev)
333 misc_deregister(&ar7_wdt_miscdev);
334 iounmap(ar7_wdt);
335 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
337 return 0;
340 static void ar7_wdt_shutdown(struct platform_device *pdev)
342 if (!nowayout)
343 ar7_wdt_disable_wdt();
346 static struct platform_driver ar7_wdt_driver = {
347 .probe = ar7_wdt_probe,
348 .remove = __devexit_p(ar7_wdt_remove),
349 .shutdown = ar7_wdt_shutdown,
350 .driver = {
351 .owner = THIS_MODULE,
352 .name = "ar7_wdt",
356 static int __init ar7_wdt_init(void)
358 return platform_driver_register(&ar7_wdt_driver);
361 static void __exit ar7_wdt_cleanup(void)
363 platform_driver_unregister(&ar7_wdt_driver);
366 module_init(ar7_wdt_init);
367 module_exit(ar7_wdt_cleanup);