[WATCHDOG] s3c2410 watchdog power management
[powerpc.git] / drivers / char / watchdog / s3c2410_wdt.c
1 /* linux/drivers/char/watchdog/s3c2410_wdt.c
2  *
3  * Copyright (c) 2004 Simtec Electronics
4  *      Ben Dooks <ben@simtec.co.uk>
5  *
6  * S3C2410 Watchdog Timer Support
7  *
8  * Based on, softdog.c by Alan Cox,
9  *     (c) Copyright 1996 Alan Cox <alan@redhat.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  * Changelog:
26  *      05-Oct-2004     BJD     Added semaphore init to stop crashes on open
27  *                              Fixed tmr_count / wdt_count confusion
28  *                              Added configurable debug
29  *
30  *      11-Jan-2005     BJD     Fixed divide-by-2 in timeout code
31  *
32  *      25-Jan-2005     DA      Added suspend/resume support
33  *
34  *      10-Mar-2005     LCVR    Changed S3C2410_VA to S3C24XX_VA
35 */
36
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/config.h>
40 #include <linux/types.h>
41 #include <linux/timer.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
44 #include <linux/fs.h>
45 #include <linux/notifier.h>
46 #include <linux/reboot.h>
47 #include <linux/init.h>
48 #include <linux/device.h>
49 #include <linux/interrupt.h>
50
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
53
54 #include <asm/arch/map.h>
55 #include <asm/hardware/clock.h>
56
57 #undef S3C24XX_VA_WATCHDOG
58 #define S3C24XX_VA_WATCHDOG (0)
59
60 #include <asm/arch/regs-watchdog.h>
61
62 #define PFX "s3c2410-wdt: "
63
64 #define CONFIG_S3C2410_WATCHDOG_ATBOOT          (0)
65 #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME    (15)
66
67 static int nowayout = WATCHDOG_NOWAYOUT;
68 static int tmr_margin   = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
69 static int tmr_atboot   = CONFIG_S3C2410_WATCHDOG_ATBOOT;
70 static int soft_noboot  = 0;
71 static int debug        = 0;
72
73 module_param(tmr_margin,  int, 0);
74 module_param(tmr_atboot,  int, 0);
75 module_param(nowayout,    int, 0);
76 module_param(soft_noboot, int, 0);
77 module_param(debug,       int, 0);
78
79 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
80
81 MODULE_PARM_DESC(tmr_atboot, "Watchdog is started at boot time if set to 1, default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
82
83 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
84
85 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
86
87 MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
88
89
90 typedef enum close_state {
91         CLOSE_STATE_NOT,
92         CLOSE_STATE_ALLOW=0x4021
93 } close_state_t;
94
95 static DECLARE_MUTEX(open_lock);
96
97 static struct resource  *wdt_mem;
98 static struct resource  *wdt_irq;
99 static struct clk       *wdt_clock;
100 static void __iomem     *wdt_base;
101 static unsigned int      wdt_count;
102 static close_state_t     allow_close;
103
104 /* watchdog control routines */
105
106 #define DBG(msg...) do { \
107         if (debug) \
108                 printk(KERN_INFO msg); \
109         } while(0)
110
111 /* functions */
112
113 static int s3c2410wdt_keepalive(void)
114 {
115         writel(wdt_count, wdt_base + S3C2410_WTCNT);
116         return 0;
117 }
118
119 static int s3c2410wdt_stop(void)
120 {
121         unsigned long wtcon;
122
123         wtcon = readl(wdt_base + S3C2410_WTCON);
124         wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
125         writel(wtcon, wdt_base + S3C2410_WTCON);
126
127         return 0;
128 }
129
130 static int s3c2410wdt_start(void)
131 {
132         unsigned long wtcon;
133
134         s3c2410wdt_stop();
135
136         wtcon = readl(wdt_base + S3C2410_WTCON);
137         wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
138
139         if (soft_noboot) {
140                 wtcon |= S3C2410_WTCON_INTEN;
141                 wtcon &= ~S3C2410_WTCON_RSTEN;
142         } else {
143                 wtcon &= ~S3C2410_WTCON_INTEN;
144                 wtcon |= S3C2410_WTCON_RSTEN;
145         }
146
147         DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
148             __FUNCTION__, wdt_count, wtcon);
149
150         writel(wdt_count, wdt_base + S3C2410_WTDAT);
151         writel(wdt_count, wdt_base + S3C2410_WTCNT);
152         writel(wtcon, wdt_base + S3C2410_WTCON);
153
154         return 0;
155 }
156
157 static int s3c2410wdt_set_heartbeat(int timeout)
158 {
159         unsigned int freq = clk_get_rate(wdt_clock);
160         unsigned int count;
161         unsigned int divisor = 1;
162         unsigned long wtcon;
163
164         if (timeout < 1)
165                 return -EINVAL;
166
167         freq /= 128;
168         count = timeout * freq;
169
170         DBG("%s: count=%d, timeout=%d, freq=%d\n",
171             __FUNCTION__, count, timeout, freq);
172
173         /* if the count is bigger than the watchdog register,
174            then work out what we need to do (and if) we can
175            actually make this value
176         */
177
178         if (count >= 0x10000) {
179                 for (divisor = 1; divisor <= 0x100; divisor++) {
180                         if ((count / divisor) < 0x10000)
181                                 break;
182                 }
183
184                 if ((count / divisor) >= 0x10000) {
185                         printk(KERN_ERR PFX "timeout %d too big\n", timeout);
186                         return -EINVAL;
187                 }
188         }
189
190         tmr_margin = timeout;
191
192         DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
193             __FUNCTION__, timeout, divisor, count, count/divisor);
194
195         count /= divisor;
196         wdt_count = count;
197
198         /* update the pre-scaler */
199         wtcon = readl(wdt_base + S3C2410_WTCON);
200         wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
201         wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
202
203         writel(count, wdt_base + S3C2410_WTDAT);
204         writel(wtcon, wdt_base + S3C2410_WTCON);
205
206         return 0;
207 }
208
209 /*
210  *      /dev/watchdog handling
211  */
212
213 static int s3c2410wdt_open(struct inode *inode, struct file *file)
214 {
215         if(down_trylock(&open_lock))
216                 return -EBUSY;
217
218         if (nowayout) {
219                 __module_get(THIS_MODULE);
220         } else {
221                 allow_close = CLOSE_STATE_ALLOW;
222         }
223
224         /* start the timer */
225         s3c2410wdt_start();
226         return nonseekable_open(inode, file);
227 }
228
229 static int s3c2410wdt_release(struct inode *inode, struct file *file)
230 {
231         /*
232          *      Shut off the timer.
233          *      Lock it in if it's a module and we set nowayout
234          */
235         if (allow_close == CLOSE_STATE_ALLOW) {
236                 s3c2410wdt_stop();
237         } else {
238                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
239                 s3c2410wdt_keepalive();
240         }
241
242         allow_close = CLOSE_STATE_NOT;
243         up(&open_lock);
244         return 0;
245 }
246
247 static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
248                                 size_t len, loff_t *ppos)
249 {
250         /*
251          *      Refresh the timer.
252          */
253         if(len) {
254                 if (!nowayout) {
255                         size_t i;
256
257                         /* In case it was set long ago */
258                         allow_close = CLOSE_STATE_NOT;
259
260                         for (i = 0; i != len; i++) {
261                                 char c;
262
263                                 if (get_user(c, data + i))
264                                         return -EFAULT;
265                                 if (c == 'V')
266                                         allow_close = CLOSE_STATE_ALLOW;
267                         }
268                 }
269
270                 s3c2410wdt_keepalive();
271         }
272         return len;
273 }
274
275 #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
276
277 static struct watchdog_info s3c2410_wdt_ident = {
278         .options          =     OPTIONS,
279         .firmware_version =     0,
280         .identity         =     "S3C2410 Watchdog",
281 };
282
283
284 static int s3c2410wdt_ioctl(struct inode *inode, struct file *file,
285         unsigned int cmd, unsigned long arg)
286 {
287         void __user *argp = (void __user *)arg;
288         int __user *p = argp;
289         int new_margin;
290
291         switch (cmd) {
292                 default:
293                         return -ENOIOCTLCMD;
294
295                 case WDIOC_GETSUPPORT:
296                         return copy_to_user(argp, &s3c2410_wdt_ident,
297                                 sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
298
299                 case WDIOC_GETSTATUS:
300                 case WDIOC_GETBOOTSTATUS:
301                         return put_user(0, p);
302
303                 case WDIOC_KEEPALIVE:
304                         s3c2410wdt_keepalive();
305                         return 0;
306
307                 case WDIOC_SETTIMEOUT:
308                         if (get_user(new_margin, p))
309                                 return -EFAULT;
310
311                         if (s3c2410wdt_set_heartbeat(new_margin))
312                                 return -EINVAL;
313
314                         s3c2410wdt_keepalive();
315                         return put_user(tmr_margin, p);
316
317                 case WDIOC_GETTIMEOUT:
318                         return put_user(tmr_margin, p);
319         }
320 }
321
322 /*
323  *      Notifier for system down
324  */
325
326 static int s3c2410wdt_notify_sys(struct notifier_block *this, unsigned long code,
327                               void *unused)
328 {
329         if(code==SYS_DOWN || code==SYS_HALT) {
330                 /* Turn the WDT off */
331                 s3c2410wdt_stop();
332         }
333         return NOTIFY_DONE;
334 }
335
336 /* kernel interface */
337
338 static struct file_operations s3c2410wdt_fops = {
339         .owner          = THIS_MODULE,
340         .llseek         = no_llseek,
341         .write          = s3c2410wdt_write,
342         .ioctl          = s3c2410wdt_ioctl,
343         .open           = s3c2410wdt_open,
344         .release        = s3c2410wdt_release,
345 };
346
347 static struct miscdevice s3c2410wdt_miscdev = {
348         .minor          = WATCHDOG_MINOR,
349         .name           = "watchdog",
350         .fops           = &s3c2410wdt_fops,
351 };
352
353 static struct notifier_block s3c2410wdt_notifier = {
354         .notifier_call  = s3c2410wdt_notify_sys,
355 };
356
357 /* interrupt handler code */
358
359 static irqreturn_t s3c2410wdt_irq(int irqno, void *param,
360                                   struct pt_regs *regs)
361 {
362         printk(KERN_INFO PFX "Watchdog timer expired!\n");
363
364         s3c2410wdt_keepalive();
365         return IRQ_HANDLED;
366 }
367 /* device interface */
368
369 static int s3c2410wdt_probe(struct device *dev)
370 {
371         struct platform_device *pdev = to_platform_device(dev);
372         struct resource *res;
373         int started = 0;
374         int ret;
375         int size;
376
377         DBG("%s: probe=%p, device=%p\n", __FUNCTION__, pdev, dev);
378
379         /* get the memory region for the watchdog timer */
380
381         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382         if (res == NULL) {
383                 printk(KERN_INFO PFX "failed to get memory region resouce\n");
384                 return -ENOENT;
385         }
386
387         size = (res->end-res->start)+1;
388         wdt_mem = request_mem_region(res->start, size, pdev->name);
389         if (wdt_mem == NULL) {
390                 printk(KERN_INFO PFX "failed to get memory region\n");
391                 return -ENOENT;
392         }
393
394         wdt_base = ioremap(res->start, size);
395         if (wdt_base == 0) {
396                 printk(KERN_INFO PFX "failed to ioremap() region\n");
397                 return -EINVAL;
398         }
399
400         DBG("probe: mapped wdt_base=%p\n", wdt_base);
401
402         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
403         if (res == NULL) {
404                 printk(KERN_INFO PFX "failed to get irq resource\n");
405                 return -ENOENT;
406         }
407
408         ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, dev);
409         if (ret != 0) {
410                 printk(KERN_INFO PFX "failed to install irq (%d)\n", ret);
411                 return ret;
412         }
413
414         wdt_clock = clk_get(dev, "watchdog");
415         if (wdt_clock == NULL) {
416                 printk(KERN_INFO PFX "failed to find watchdog clock source\n");
417                 return -ENOENT;
418         }
419
420         clk_use(wdt_clock);
421         clk_enable(wdt_clock);
422
423         /* see if we can actually set the requested timer margin, and if
424          * not, try the default value */
425
426         if (s3c2410wdt_set_heartbeat(tmr_margin)) {
427                 started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
428
429                 if (started == 0) {
430                         printk(KERN_INFO PFX "tmr_margin value out of range, default %d used\n",
431                                CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
432                 } else {
433                         printk(KERN_INFO PFX "default timer value is out of range, cannot start\n");
434                 }
435         }
436
437         ret = register_reboot_notifier(&s3c2410wdt_notifier);
438         if (ret) {
439                 printk (KERN_ERR PFX "cannot register reboot notifier (%d)\n",
440                         ret);
441                 return ret;
442         }
443
444         ret = misc_register(&s3c2410wdt_miscdev);
445         if (ret) {
446                 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (%d)\n",
447                         WATCHDOG_MINOR, ret);
448                 unregister_reboot_notifier(&s3c2410wdt_notifier);
449                 return ret;
450         }
451
452         if (tmr_atboot && started == 0) {
453                 printk(KERN_INFO PFX "Starting Watchdog Timer\n");
454                 s3c2410wdt_start();
455         }
456
457         return 0;
458 }
459
460 static int s3c2410wdt_remove(struct device *dev)
461 {
462         if (wdt_mem != NULL) {
463                 release_resource(wdt_mem);
464                 kfree(wdt_mem);
465                 wdt_mem = NULL;
466         }
467
468         if (wdt_irq != NULL) {
469                 free_irq(wdt_irq->start, dev);
470                 wdt_irq = NULL;
471         }
472
473         if (wdt_clock != NULL) {
474                 clk_disable(wdt_clock);
475                 clk_unuse(wdt_clock);
476                 clk_put(wdt_clock);
477                 wdt_clock = NULL;
478         }
479
480         misc_deregister(&s3c2410wdt_miscdev);
481         return 0;
482 }
483
484 #ifdef CONFIG_PM
485
486 static unsigned long wtcon_save;
487 static unsigned long wtdat_save;
488
489 static int s3c2410wdt_suspend(struct device *dev, u32 state, u32 level)
490 {
491         if (level == SUSPEND_POWER_DOWN) {
492                 /* Save watchdog state, and turn it off. */
493                 wtcon_save = readl(wdt_base + S3C2410_WTCON);
494                 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
495
496                 /* Note that WTCNT doesn't need to be saved. */
497                 s3c2410wdt_stop();
498         }
499
500         return 0;
501 }
502
503 static int s3c2410wdt_resume(struct device *dev, u32 level)
504 {
505         if (level == RESUME_POWER_ON) {
506                 /* Restore watchdog state. */
507
508                 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
509                 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
510                 writel(wtcon_save, wdt_base + S3C2410_WTCON);
511
512                 printk(KERN_INFO PFX "watchdog %sabled\n",
513                        (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
514         }
515
516         return 0;
517 }
518
519 #else
520 #define s3c2410wdt_suspend NULL
521 #define s3c2410wdt_resume  NULL
522 #endif /* CONFIG_PM */
523
524
525 static struct device_driver s3c2410wdt_driver = {
526         .name           = "s3c2410-wdt",
527         .bus            = &platform_bus_type,
528         .probe          = s3c2410wdt_probe,
529         .remove         = s3c2410wdt_remove,
530         .suspend        = s3c2410wdt_suspend,
531         .resume         = s3c2410wdt_resume,
532 };
533
534
535 static char banner[] __initdata = KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
536
537 static int __init watchdog_init(void)
538 {
539         printk(banner);
540         return driver_register(&s3c2410wdt_driver);
541 }
542
543 static void __exit watchdog_exit(void)
544 {
545         driver_unregister(&s3c2410wdt_driver);
546         unregister_reboot_notifier(&s3c2410wdt_notifier);
547 }
548
549 module_init(watchdog_init);
550 module_exit(watchdog_exit);
551
552 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
553               "Dimitry Andric <dimitry.andric@tomtom.com>");
554 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
555 MODULE_LICENSE("GPL");
556 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);