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