]> err.no Git - linux-2.6/blob - drivers/watchdog/s3c2410_wdt.c
Fix CONFIG_AC97_BUS dependency
[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
162 static int s3c2410wdt_set_heartbeat(int timeout)
163 {
164         unsigned int freq = clk_get_rate(wdt_clock);
165         unsigned int count;
166         unsigned int divisor = 1;
167         unsigned long wtcon;
168
169         if (timeout < 1)
170                 return -EINVAL;
171
172         freq /= 128;
173         count = timeout * freq;
174
175         DBG("%s: count=%d, timeout=%d, freq=%d\n",
176             __func__, count, timeout, freq);
177
178         /* if the count is bigger than the watchdog register,
179            then work out what we need to do (and if) we can
180            actually make this value
181         */
182
183         if (count >= 0x10000) {
184                 for (divisor = 1; divisor <= 0x100; divisor++) {
185                         if ((count / divisor) < 0x10000)
186                                 break;
187                 }
188
189                 if ((count / divisor) >= 0x10000) {
190                         dev_err(wdt_dev, "timeout %d too big\n", timeout);
191                         return -EINVAL;
192                 }
193         }
194
195         tmr_margin = timeout;
196
197         DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
198             __func__, timeout, divisor, count, count/divisor);
199
200         count /= divisor;
201         wdt_count = count;
202
203         /* update the pre-scaler */
204         wtcon = readl(wdt_base + S3C2410_WTCON);
205         wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
206         wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
207
208         writel(count, wdt_base + S3C2410_WTDAT);
209         writel(wtcon, wdt_base + S3C2410_WTCON);
210
211         return 0;
212 }
213
214 /*
215  *      /dev/watchdog handling
216  */
217
218 static int s3c2410wdt_open(struct inode *inode, struct file *file)
219 {
220         if (test_and_set_bit(0, &open_lock))
221                 return -EBUSY;
222
223         if (nowayout)
224                 __module_get(THIS_MODULE);
225
226         allow_close = CLOSE_STATE_NOT;
227
228         /* start the timer */
229         s3c2410wdt_start();
230         return nonseekable_open(inode, file);
231 }
232
233 static int s3c2410wdt_release(struct inode *inode, struct file *file)
234 {
235         /*
236          *      Shut off the timer.
237          *      Lock it in if it's a module and we set nowayout
238          */
239
240         if (allow_close == CLOSE_STATE_ALLOW)
241                 s3c2410wdt_stop();
242         else {
243                 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
244                 s3c2410wdt_keepalive();
245         }
246         allow_close = CLOSE_STATE_NOT;
247         clear_bit(0, &open_lock);
248         return 0;
249 }
250
251 static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
252                                 size_t len, loff_t *ppos)
253 {
254         /*
255          *      Refresh the timer.
256          */
257         if (len) {
258                 if (!nowayout) {
259                         size_t i;
260
261                         /* In case it was set long ago */
262                         allow_close = CLOSE_STATE_NOT;
263
264                         for (i = 0; i != len; i++) {
265                                 char c;
266
267                                 if (get_user(c, data + i))
268                                         return -EFAULT;
269                                 if (c == 'V')
270                                         allow_close = CLOSE_STATE_ALLOW;
271                         }
272                 }
273                 s3c2410wdt_keepalive();
274         }
275         return len;
276 }
277
278 #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
279
280 static const struct watchdog_info s3c2410_wdt_ident = {
281         .options          =     OPTIONS,
282         .firmware_version =     0,
283         .identity         =     "S3C2410 Watchdog",
284 };
285
286
287 static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
288                                                         unsigned long arg)
289 {
290         void __user *argp = (void __user *)arg;
291         int __user *p = argp;
292         int new_margin;
293
294         switch (cmd) {
295         case WDIOC_GETSUPPORT:
296                 return copy_to_user(argp, &s3c2410_wdt_ident,
297                         sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
298         case WDIOC_GETSTATUS:
299         case WDIOC_GETBOOTSTATUS:
300                 return put_user(0, p);
301         case WDIOC_KEEPALIVE:
302                 s3c2410wdt_keepalive();
303                 return 0;
304         case WDIOC_SETTIMEOUT:
305                 if (get_user(new_margin, p))
306                         return -EFAULT;
307                 if (s3c2410wdt_set_heartbeat(new_margin))
308                         return -EINVAL;
309                 s3c2410wdt_keepalive();
310                 return put_user(tmr_margin, p);
311         case WDIOC_GETTIMEOUT:
312                 return put_user(tmr_margin, p);
313         default:
314                 return -ENOTTY;
315         }
316 }
317
318 /* kernel interface */
319
320 static const struct file_operations s3c2410wdt_fops = {
321         .owner          = THIS_MODULE,
322         .llseek         = no_llseek,
323         .write          = s3c2410wdt_write,
324         .unlocked_ioctl = s3c2410wdt_ioctl,
325         .open           = s3c2410wdt_open,
326         .release        = s3c2410wdt_release,
327 };
328
329 static struct miscdevice s3c2410wdt_miscdev = {
330         .minor          = WATCHDOG_MINOR,
331         .name           = "watchdog",
332         .fops           = &s3c2410wdt_fops,
333 };
334
335 /* interrupt handler code */
336
337 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
338 {
339         dev_info(wdt_dev, "watchdog timer expired (irq)\n");
340
341         s3c2410wdt_keepalive();
342         return IRQ_HANDLED;
343 }
344 /* device interface */
345
346 static int s3c2410wdt_probe(struct platform_device *pdev)
347 {
348         struct resource *res;
349         struct device *dev;
350         unsigned int wtcon;
351         int started = 0;
352         int ret;
353         int size;
354
355         DBG("%s: probe=%p\n", __func__, pdev);
356
357         dev = &pdev->dev;
358         wdt_dev = &pdev->dev;
359
360         /* get the memory region for the watchdog timer */
361
362         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363         if (res == NULL) {
364                 dev_err(dev, "no memory resource specified\n");
365                 return -ENOENT;
366         }
367
368         size = (res->end-res->start)+1;
369         wdt_mem = request_mem_region(res->start, size, pdev->name);
370         if (wdt_mem == NULL) {
371                 dev_err(dev, "failed to get memory region\n");
372                 ret = -ENOENT;
373                 goto err_req;
374         }
375
376         wdt_base = ioremap(res->start, size);
377         if (wdt_base == 0) {
378                 dev_err(dev, "failed to ioremap() region\n");
379                 ret = -EINVAL;
380                 goto err_req;
381         }
382
383         DBG("probe: mapped wdt_base=%p\n", wdt_base);
384
385         wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
386         if (wdt_irq == NULL) {
387                 dev_err(dev, "no irq resource specified\n");
388                 ret = -ENOENT;
389                 goto err_map;
390         }
391
392         ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
393         if (ret != 0) {
394                 dev_err(dev, "failed to install irq (%d)\n", ret);
395                 goto err_map;
396         }
397
398         wdt_clock = clk_get(&pdev->dev, "watchdog");
399         if (IS_ERR(wdt_clock)) {
400                 dev_err(dev, "failed to find watchdog clock source\n");
401                 ret = PTR_ERR(wdt_clock);
402                 goto err_irq;
403         }
404
405         clk_enable(wdt_clock);
406
407         /* see if we can actually set the requested timer margin, and if
408          * not, try the default value */
409
410         if (s3c2410wdt_set_heartbeat(tmr_margin)) {
411                 started = s3c2410wdt_set_heartbeat(
412                                         CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
413
414                 if (started == 0)
415                         dev_info(dev,
416                            "tmr_margin value out of range, default %d used\n",
417                                CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
418                 else
419                         dev_info(dev, "default timer value is out of range, cannot start\n");
420         }
421
422         ret = misc_register(&s3c2410wdt_miscdev);
423         if (ret) {
424                 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
425                         WATCHDOG_MINOR, ret);
426                 goto err_clk;
427         }
428
429         if (tmr_atboot && started == 0) {
430                 dev_info(dev, "starting watchdog timer\n");
431                 s3c2410wdt_start();
432         } else if (!tmr_atboot) {
433                 /* if we're not enabling the watchdog, then ensure it is
434                  * disabled if it has been left running from the bootloader
435                  * or other source */
436
437                 s3c2410wdt_stop();
438         }
439
440         /* print out a statement of readiness */
441
442         wtcon = readl(wdt_base + S3C2410_WTCON);
443
444         dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
445                  (wtcon & S3C2410_WTCON_ENABLE) ?  "" : "in",
446                  (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
447                  (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
448
449         return 0;
450
451  err_clk:
452         clk_disable(wdt_clock);
453         clk_put(wdt_clock);
454
455  err_irq:
456         free_irq(wdt_irq->start, pdev);
457
458  err_map:
459         iounmap(wdt_base);
460
461  err_req:
462         release_resource(wdt_mem);
463         kfree(wdt_mem);
464
465         return ret;
466 }
467
468 static int s3c2410wdt_remove(struct platform_device *dev)
469 {
470         release_resource(wdt_mem);
471         kfree(wdt_mem);
472         wdt_mem = NULL;
473
474         free_irq(wdt_irq->start, dev);
475         wdt_irq = NULL;
476
477         clk_disable(wdt_clock);
478         clk_put(wdt_clock);
479         wdt_clock = NULL;
480
481         iounmap(wdt_base);
482         misc_deregister(&s3c2410wdt_miscdev);
483         return 0;
484 }
485
486 static void s3c2410wdt_shutdown(struct platform_device *dev)
487 {
488         s3c2410wdt_stop();
489 }
490
491 #ifdef CONFIG_PM
492
493 static unsigned long wtcon_save;
494 static unsigned long wtdat_save;
495
496 static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
497 {
498         /* Save watchdog state, and turn it off. */
499         wtcon_save = readl(wdt_base + S3C2410_WTCON);
500         wtdat_save = readl(wdt_base + S3C2410_WTDAT);
501
502         /* Note that WTCNT doesn't need to be saved. */
503         s3c2410wdt_stop();
504
505         return 0;
506 }
507
508 static int s3c2410wdt_resume(struct platform_device *dev)
509 {
510         /* Restore watchdog state. */
511
512         writel(wtdat_save, wdt_base + S3C2410_WTDAT);
513         writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
514         writel(wtcon_save, wdt_base + S3C2410_WTCON);
515
516         printk(KERN_INFO PFX "watchdog %sabled\n",
517                (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
518
519         return 0;
520 }
521
522 #else
523 #define s3c2410wdt_suspend NULL
524 #define s3c2410wdt_resume  NULL
525 #endif /* CONFIG_PM */
526
527
528 static struct platform_driver s3c2410wdt_driver = {
529         .probe          = s3c2410wdt_probe,
530         .remove         = s3c2410wdt_remove,
531         .shutdown       = s3c2410wdt_shutdown,
532         .suspend        = s3c2410wdt_suspend,
533         .resume         = s3c2410wdt_resume,
534         .driver         = {
535                 .owner  = THIS_MODULE,
536                 .name   = "s3c2410-wdt",
537         },
538 };
539
540
541 static char banner[] __initdata =
542         KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
543
544 static int __init watchdog_init(void)
545 {
546         printk(banner);
547         return platform_driver_register(&s3c2410wdt_driver);
548 }
549
550 static void __exit watchdog_exit(void)
551 {
552         platform_driver_unregister(&s3c2410wdt_driver);
553 }
554
555 module_init(watchdog_init);
556 module_exit(watchdog_exit);
557
558 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
559               "Dimitry Andric <dimitry.andric@tomtom.com>");
560 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
561 MODULE_LICENSE("GPL");
562 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
563 MODULE_ALIAS("platform:s3c2410-wdt");