[brcm63xx] backport an upstream fix: We're not disabling IRQ, so we must call the...
[openwrt.git] / target / linux / brcm63xx / files / drivers / watchdog / bcm63xx_wdt.c
1 /*
2  *  Broadcom BCM63xx SoC watchdog driver
3  *
4  *  Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com>
5  *  Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version
10  *  2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/reboot.h>
21 #include <linux/smp_lock.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/uaccess.h>
25 #include <linux/timer.h>
26
27 #include <bcm63xx_cpu.h>
28 #include <bcm63xx_io.h>
29 #include <bcm63xx_regs.h>
30
31 #define PFX KBUILD_MODNAME
32
33 #define WDT_HZ          50000000 /* Fclk */
34 #define WDT_INTERVAL    (40)    /* in seconds */
35
36 static struct {
37         void __iomem *regs;
38         struct completion stop;
39         int running;
40         struct timer_list timer;
41         int queue;
42         int default_ticks;
43         unsigned long inuse;
44 } bcm63xx_wdt_device;
45
46 static int ticks = 100 * WDT_HZ;
47
48 static int expect_close;
49 static int timeout;
50
51 static int nowayout = WATCHDOG_NOWAYOUT;
52 module_param(nowayout, int, 0);
53 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
54         __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
55
56
57 static void bcm63xx_wdt_toggle(void)
58 {
59         bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
60         bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
61 }
62
63 static void bcm63xx_wdt_start(void)
64 {
65         if (!bcm63xx_wdt_device.inuse) {
66                 bcm63xx_wdt_toggle();
67                 mod_timer(&bcm63xx_wdt_device.timer, jiffies + WDT_INTERVAL);
68         }
69
70         bcm63xx_wdt_device.running++;
71 }
72
73 static void bcm63xx_wdt_stop(void)
74 {
75         if (bcm63xx_wdt_device.running) {
76                 bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
77                 bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
78
79                 bcm63xx_wdt_device.running = 0;
80         }
81 }
82
83 static void bcm63xx_wdt_set(int new_timeout)
84 {
85         new_timeout *= WDT_HZ;
86         bcm_writel(new_timeout, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
87 }
88
89 static void bcm63xx_wdt_reset(void)
90 {
91         ticks = bcm63xx_wdt_device.default_ticks;
92 }
93
94 static void bcm63xx_wdt_update(unsigned long unused)
95 {
96         if (bcm63xx_wdt_device.running)
97                 ticks--;
98
99         bcm63xx_wdt_toggle();
100
101         if (bcm63xx_wdt_device.queue && ticks)
102                 mod_timer(&bcm63xx_wdt_device.timer,
103                         jiffies + WDT_INTERVAL);
104         else
105                 complete(&bcm63xx_wdt_device.stop);
106 }
107
108 static int bcm63xx_wdt_open(struct inode *inode, struct file *file)
109 {
110         if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse))
111                 return -EBUSY;
112
113         if (nowayout)
114                 __module_get(THIS_MODULE);
115
116         return nonseekable_open(inode, file);
117 }
118
119 static int bcm63xx_wdt_release(struct inode *inode, struct file *file)
120 {
121         if (expect_close && nowayout == 0) {
122                 bcm63xx_wdt_stop();
123                 printk(KERN_INFO PFX ": disabling watchdog timer\n");
124                 module_put(THIS_MODULE);
125         } else
126                 printk(KERN_CRIT PFX
127                         ": device closed unexpectedly. WDT will not stop !\n");
128
129         clear_bit(0, &bcm63xx_wdt_device.inuse);
130         return 0;
131 }
132
133 static ssize_t bcm63xx_wdt_write(struct file *file, const char *data,
134                                 size_t len, loff_t *ppos)
135 {
136         if (len) {
137                 if (!nowayout) {
138                         size_t i;
139
140                         /* In case it was set long ago */
141                         expect_close = 0;
142
143                         for (i = 0; i != len; i++) {
144                                 char c;
145                                 if (get_user(c, data + i))
146                                         return -EFAULT;
147                                 if (c == 'V')
148                                         expect_close = 1;
149                         }
150                 }
151                 bcm63xx_wdt_update(0);
152                 return len;
153         }
154         return 0;
155 }
156
157 static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd,
158                                 unsigned long arg)
159 {
160         void __user *argp = (void __user *)arg;
161         int new_timeout;
162         unsigned int value;
163         static struct watchdog_info ident = {
164                 .options =              WDIOF_SETTIMEOUT |
165                                         WDIOF_KEEPALIVEPING |
166                                         WDIOF_MAGICCLOSE,
167                 .identity =             "BCM63xx Watchdog",
168         };
169         switch (cmd) {
170         case WDIOC_KEEPALIVE:
171                 bcm63xx_wdt_reset();
172                 break;
173         case WDIOC_GETSTATUS:
174         case WDIOC_GETBOOTSTATUS:
175                 value = bcm_readl(bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
176                 if (copy_to_user(argp, &value, sizeof(int)))
177                         return -EFAULT;
178                 break;
179         case WDIOC_GETSUPPORT:
180                 if (copy_to_user(argp, &ident, sizeof(ident)))
181                         return -EFAULT;
182                 break;
183         case WDIOC_SETOPTIONS:
184                 if (copy_from_user(&value, argp, sizeof(int)))
185                         return -EFAULT;
186                 switch (value) {
187                 case WDIOS_ENABLECARD:
188                         bcm63xx_wdt_start();
189                         break;
190                 case WDIOS_DISABLECARD:
191                         bcm63xx_wdt_stop();
192                 default:
193                         return -EINVAL;
194                 }
195                 break;
196         case WDIOC_SETTIMEOUT:
197                 if (copy_from_user(&new_timeout, argp, sizeof(int)))
198                         return -EFAULT;
199                 if (new_timeout < 5)
200                         return -EINVAL;
201                 if (new_timeout > 40)
202                         return -EINVAL;
203                 bcm63xx_wdt_set(new_timeout);
204                 bcm63xx_wdt_toggle();
205         case WDIOC_GETTIMEOUT:
206                 return copy_to_user(argp, &timeout, sizeof(int));
207         default:
208                 return -ENOTTY;
209         }
210
211         return 0;
212 }
213
214 static struct file_operations bcm63xx_wdt_fops = {
215         .owner          = THIS_MODULE,
216         .llseek         = no_llseek,
217         .write          = bcm63xx_wdt_write,
218         .unlocked_ioctl = bcm63xx_wdt_ioctl,
219         .open           = bcm63xx_wdt_open,
220         .release        = bcm63xx_wdt_release,
221 };
222
223 static struct miscdevice bcm63xx_wdt_miscdev = {
224         .minor  = WATCHDOG_MINOR,
225         .name   = "watchdog",
226         .fops   = &bcm63xx_wdt_fops,
227 };
228
229 static int bcm63xx_wdt_probe(struct platform_device *pdev)
230 {
231         int ret;
232         struct resource *r;
233
234         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235         if (!r) {
236                 printk(KERN_ERR PFX 
237                         "failed to retrieve resources\n");
238                 return -ENODEV;
239         }
240
241         bcm63xx_wdt_device.regs = ioremap_nocache(r->start, r->end - r->start);
242         if (!bcm63xx_wdt_device.regs) {
243                 printk(KERN_ERR PFX
244                         "failed to remap I/O resources\n");
245                 return -ENXIO;
246         }
247
248         ret = misc_register(&bcm63xx_wdt_miscdev);
249         if (ret < 0) {
250                 printk(KERN_ERR PFX
251                         "failed to register watchdog device\n");
252                 goto unmap;
253         }
254
255         init_completion(&bcm63xx_wdt_device.stop);
256         bcm63xx_wdt_device.queue = 0;
257
258         clear_bit(0, &bcm63xx_wdt_device.inuse);
259
260         setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_wdt_update, 0L);
261
262         bcm63xx_wdt_device.default_ticks = ticks;
263         bcm63xx_wdt_set(ticks);
264         bcm63xx_wdt_start();
265         
266         printk(KERN_INFO PFX " started, timer margin: %d sec\n", WDT_INTERVAL);
267
268         return 0;
269
270 unmap:
271         iounmap(bcm63xx_wdt_device.regs);
272         return ret;
273 }
274
275 static int bcm63xx_wdt_remove(struct platform_device *pdev)
276 {
277         if (bcm63xx_wdt_device.queue) {
278                 bcm63xx_wdt_device.queue = 0;
279                 wait_for_completion(&bcm63xx_wdt_device.stop);
280         }
281
282         misc_deregister(&bcm63xx_wdt_miscdev);
283
284         iounmap(bcm63xx_wdt_device.regs);
285
286         return 0;
287 }
288
289 static struct platform_driver bcm63xx_wdt = {
290         .probe  = bcm63xx_wdt_probe,
291         .remove = bcm63xx_wdt_remove,
292         .driver = {
293                 .name = "bcm63xx-wdt",
294         }
295 };
296
297 static int __init bcm63xx_wdt_init(void)
298 {
299         return platform_driver_register(&bcm63xx_wdt);
300 }
301
302 static void __exit bcm63xx_wdt_exit(void)
303 {
304         platform_driver_unregister(&bcm63xx_wdt);
305 }
306
307 module_init(bcm63xx_wdt_init);
308 module_exit(bcm63xx_wdt_exit);
309
310 MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
311 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
312 MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
313 MODULE_LICENSE("GPL");
314 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
315 MODULE_ALIAS("platform:bcm63xx-wdt");