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