fix ar2315 watchdog restart (patch from #3953)
[openwrt.git] / target / linux / atheros / files / drivers / watchdog / ar2315-wtd.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
17  * Based on EP93xx and ifxmips wdt driver
18  */
19
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/fs.h>
27 #include <linux/ioport.h>
28 #include <linux/notifier.h>
29 #include <linux/reboot.h>
30 #include <linux/init.h>
31 #include <linux/platform_device.h>
32
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/addrspace.h>
37 #include <ar531x.h>
38
39 #define CLOCK_RATE 40000000
40 #define HEARTBEAT(x) (x < 1 || x > 90)?(20):(x)
41
42 static int wdt_timeout = 20;
43 static int started = 0;
44 static int in_use = 0;
45
46 static void
47 ar2315_wdt_enable(void)
48 {
49         sysRegWrite(AR5315_WD, wdt_timeout * CLOCK_RATE);
50         sysRegWrite(AR5315_ISR, 0x80);
51 }
52
53 static ssize_t
54 ar2315_wdt_write(struct file *file, const char __user *data, size_t len, loff_t *ppos)
55 {
56         if(len)
57                 ar2315_wdt_enable();
58         return len;
59 }
60
61 static int
62 ar2315_wdt_open(struct inode *inode, struct file *file)
63 {
64         if(in_use)
65                 return -EBUSY;
66         ar2315_wdt_enable();
67         in_use = started = 1;
68         return nonseekable_open(inode, file);
69 }
70
71 static int
72 ar2315_wdt_release(struct inode *inode, struct file *file)
73 {
74         in_use = 0;
75         return 0;
76 }
77
78 static irqreturn_t
79 ar2315_wdt_interrupt(int irq, void *dev_id)
80 {
81         if(started)
82         {
83                 printk(KERN_CRIT "watchdog expired, rebooting system\n");
84                 emergency_restart();
85         } else {
86                 sysRegWrite(AR5315_WDC, 0);
87                 sysRegWrite(AR5315_WD, 0);
88                 sysRegWrite(AR5315_ISR, 0x80);
89         }
90         return IRQ_HANDLED;
91 }
92
93 static struct watchdog_info ident = {
94         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
95         .identity = "ar2315 Watchdog",
96 };
97
98 static int
99 ar2315_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
100 {
101         int new_wdt_timeout;
102         int ret = -ENOIOCTLCMD;
103
104         switch(cmd)
105         {
106                 case WDIOC_GETSUPPORT:
107                         ret = copy_to_user((struct watchdog_info __user *)arg, &ident, sizeof(ident)) ? -EFAULT : 0;
108                         break;
109
110                 case WDIOC_KEEPALIVE:
111                         ar2315_wdt_enable();
112                         ret = 0;
113                         break;
114
115                 case WDIOC_SETTIMEOUT:
116                         if((ret = get_user(new_wdt_timeout, (int __user *)arg)))
117                                 break;
118                         wdt_timeout = HEARTBEAT(new_wdt_timeout);
119                         ar2315_wdt_enable();
120                         break;
121
122                 case WDIOC_GETTIMEOUT:
123                         ret = put_user(wdt_timeout, (int __user *)arg);
124                         break;
125         }
126         return ret;
127 }
128
129 static struct file_operations ar2315_wdt_fops = {
130         .owner          = THIS_MODULE,
131         .llseek         = no_llseek,
132         .write          = ar2315_wdt_write,
133         .ioctl          = ar2315_wdt_ioctl,
134         .open           = ar2315_wdt_open,
135         .release        = ar2315_wdt_release,
136 };
137
138 static struct miscdevice ar2315_wdt_miscdev = {
139         .minor  = WATCHDOG_MINOR,
140         .name   = "watchdog",
141         .fops   = &ar2315_wdt_fops,
142 };
143
144 static int
145 ar2315_wdt_probe(struct platform_device *dev)
146 {
147         int ret = 0;
148
149         ar2315_wdt_enable();
150         ret = request_irq(AR531X_MISC_IRQ_WATCHDOG, ar2315_wdt_interrupt, IRQF_DISABLED, "ar2315_wdt", NULL);
151         if(ret)
152         {
153                 printk(KERN_ERR "ar2315wdt: failed to register inetrrupt\n");
154                 goto out;
155         }
156
157         ret = misc_register(&ar2315_wdt_miscdev);
158         if(ret)
159                 printk(KERN_ERR "ar2315wdt: failed to register miscdev\n");
160
161 out:
162         return ret;
163 }
164
165 static int
166 ar2315_wdt_remove(struct platform_device *dev)
167 {
168         misc_deregister(&ar2315_wdt_miscdev);
169         free_irq(AR531X_MISC_IRQ_WATCHDOG, NULL);
170         return 0;
171 }
172
173 static struct platform_driver ar2315_wdt_driver = {
174         .probe = ar2315_wdt_probe,
175         .remove = ar2315_wdt_remove,
176         .driver = {
177                 .name = "ar2315_wdt",
178                 .owner = THIS_MODULE,
179         },
180 };
181
182 static int __init
183 init_ar2315_wdt(void)
184 {
185         int ret = platform_driver_register(&ar2315_wdt_driver);
186         if(ret)
187                 printk(KERN_INFO "ar2315_wdt: error registering platfom driver!");
188         return ret;
189 }
190
191 static void __exit
192 exit_ar2315_wdt(void)
193 {
194         platform_driver_unregister(&ar2315_wdt_driver);
195 }
196
197 module_init(init_ar2315_wdt);
198 module_exit(exit_ar2315_wdt);