blob: b57ac6b49147400a0230a777b81592202947a439 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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,
Alan Cox29fa0582008-10-27 15:17:56 +00009 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024*/
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
29#include <linux/timer.h>
30#include <linux/miscdevice.h>
31#include <linux/watchdog.h>
32#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000036#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010037#include <linux/uaccess.h>
38#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ben Dooksb4307082007-07-24 13:28:01 +010042#undef S3C_VA_WATCHDOG
43#define S3C_VA_WATCHDOG (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ben Dooks180ee702008-10-30 10:14:32 +000045#include <plat/regs-watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define PFX "s3c2410-wdt: "
48
49#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
50#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
51
Ben Dooks25ff3782006-09-06 12:24:35 +010052static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
54static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010055static int soft_noboot;
56static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58module_param(tmr_margin, int, 0);
59module_param(tmr_atboot, int, 0);
60module_param(nowayout, int, 0);
61module_param(soft_noboot, int, 0);
62module_param(debug, int, 0);
63
Alan Cox41dc8b72008-08-04 17:54:46 +010064MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
65 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
66MODULE_PARM_DESC(tmr_atboot,
67 "Watchdog is started at boot time if set to 1, default="
68 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
69MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000071MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
72 "0 to reboot (default depends on ONLY_TESTING)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
74
Alan Cox41dc8b72008-08-04 17:54:46 +010075static unsigned long open_lock;
Ben Dookse8ef92b2007-06-14 12:08:55 +010076static struct device *wdt_dev; /* platform device attached to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct resource *wdt_mem;
78static struct resource *wdt_irq;
79static struct clk *wdt_clock;
80static void __iomem *wdt_base;
81static unsigned int wdt_count;
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000082static char expect_close;
Alan Cox41dc8b72008-08-04 17:54:46 +010083static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* watchdog control routines */
86
87#define DBG(msg...) do { \
88 if (debug) \
89 printk(KERN_INFO msg); \
Alan Cox41dc8b72008-08-04 17:54:46 +010090 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* functions */
93
Alan Cox41dc8b72008-08-04 17:54:46 +010094static void s3c2410wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Alan Cox41dc8b72008-08-04 17:54:46 +010096 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 writel(wdt_count, wdt_base + S3C2410_WTCNT);
Alan Cox41dc8b72008-08-04 17:54:46 +010098 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Alan Cox41dc8b72008-08-04 17:54:46 +0100101static void __s3c2410wdt_stop(void)
102{
103 unsigned long wtcon;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 wtcon = readl(wdt_base + S3C2410_WTCON);
106 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
107 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Alan Cox41dc8b72008-08-04 17:54:46 +0100110static void s3c2410wdt_stop(void)
111{
112 spin_lock(&wdt_lock);
113 __s3c2410wdt_stop();
114 spin_unlock(&wdt_lock);
115}
116
117static void s3c2410wdt_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 unsigned long wtcon;
120
Alan Cox41dc8b72008-08-04 17:54:46 +0100121 spin_lock(&wdt_lock);
122
123 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 wtcon = readl(wdt_base + S3C2410_WTCON);
126 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
127
128 if (soft_noboot) {
129 wtcon |= S3C2410_WTCON_INTEN;
130 wtcon &= ~S3C2410_WTCON_RSTEN;
131 } else {
132 wtcon &= ~S3C2410_WTCON_INTEN;
133 wtcon |= S3C2410_WTCON_RSTEN;
134 }
135
136 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800137 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 writel(wdt_count, wdt_base + S3C2410_WTDAT);
140 writel(wdt_count, wdt_base + S3C2410_WTCNT);
141 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100142 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static int s3c2410wdt_set_heartbeat(int timeout)
146{
147 unsigned int freq = clk_get_rate(wdt_clock);
148 unsigned int count;
149 unsigned int divisor = 1;
150 unsigned long wtcon;
151
152 if (timeout < 1)
153 return -EINVAL;
154
155 freq /= 128;
156 count = timeout * freq;
157
158 DBG("%s: count=%d, timeout=%d, freq=%d\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800159 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* if the count is bigger than the watchdog register,
162 then work out what we need to do (and if) we can
163 actually make this value
164 */
165
166 if (count >= 0x10000) {
167 for (divisor = 1; divisor <= 0x100; divisor++) {
168 if ((count / divisor) < 0x10000)
169 break;
170 }
171
172 if ((count / divisor) >= 0x10000) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100173 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EINVAL;
175 }
176 }
177
178 tmr_margin = timeout;
179
180 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800181 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 count /= divisor;
184 wdt_count = count;
185
186 /* update the pre-scaler */
187 wtcon = readl(wdt_base + S3C2410_WTCON);
188 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
189 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
190
191 writel(count, wdt_base + S3C2410_WTDAT);
192 writel(wtcon, wdt_base + S3C2410_WTCON);
193
194 return 0;
195}
196
197/*
198 * /dev/watchdog handling
199 */
200
201static int s3c2410wdt_open(struct inode *inode, struct file *file)
202{
Alan Cox41dc8b72008-08-04 17:54:46 +0100203 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EBUSY;
205
Ben Dooks25ff3782006-09-06 12:24:35 +0100206 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 __module_get(THIS_MODULE);
Ben Dooks25ff3782006-09-06 12:24:35 +0100208
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000209 expect_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* start the timer */
212 s3c2410wdt_start();
213 return nonseekable_open(inode, file);
214}
215
216static int s3c2410wdt_release(struct inode *inode, struct file *file)
217{
218 /*
219 * Shut off the timer.
220 * Lock it in if it's a module and we set nowayout
221 */
Ben Dooks25ff3782006-09-06 12:24:35 +0100222
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000223 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 s3c2410wdt_stop();
Alan Cox41dc8b72008-08-04 17:54:46 +0100225 else {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100226 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 s3c2410wdt_keepalive();
228 }
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000229 expect_close = 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100230 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
234static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
235 size_t len, loff_t *ppos)
236{
237 /*
238 * Refresh the timer.
239 */
Alan Cox41dc8b72008-08-04 17:54:46 +0100240 if (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!nowayout) {
242 size_t i;
243
244 /* In case it was set long ago */
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000245 expect_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 for (i = 0; i != len; i++) {
248 char c;
249
250 if (get_user(c, data + i))
251 return -EFAULT;
252 if (c == 'V')
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000253 expect_close = 42;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 s3c2410wdt_keepalive();
257 }
258 return len;
259}
260
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000261#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alan Cox41dc8b72008-08-04 17:54:46 +0100263static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .options = OPTIONS,
265 .firmware_version = 0,
266 .identity = "S3C2410 Watchdog",
267};
268
269
Alan Cox41dc8b72008-08-04 17:54:46 +0100270static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
271 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 void __user *argp = (void __user *)arg;
274 int __user *p = argp;
275 int new_margin;
276
277 switch (cmd) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100278 case WDIOC_GETSUPPORT:
279 return copy_to_user(argp, &s3c2410_wdt_ident,
280 sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
281 case WDIOC_GETSTATUS:
282 case WDIOC_GETBOOTSTATUS:
283 return put_user(0, p);
284 case WDIOC_KEEPALIVE:
285 s3c2410wdt_keepalive();
286 return 0;
287 case WDIOC_SETTIMEOUT:
288 if (get_user(new_margin, p))
289 return -EFAULT;
290 if (s3c2410wdt_set_heartbeat(new_margin))
291 return -EINVAL;
292 s3c2410wdt_keepalive();
293 return put_user(tmr_margin, p);
294 case WDIOC_GETTIMEOUT:
295 return put_user(tmr_margin, p);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000296 default:
297 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* kernel interface */
302
Arjan van de Ven62322d22006-07-03 00:24:21 -0700303static const struct file_operations s3c2410wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .owner = THIS_MODULE,
305 .llseek = no_llseek,
306 .write = s3c2410wdt_write,
Alan Cox41dc8b72008-08-04 17:54:46 +0100307 .unlocked_ioctl = s3c2410wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .open = s3c2410wdt_open,
309 .release = s3c2410wdt_release,
310};
311
312static struct miscdevice s3c2410wdt_miscdev = {
313 .minor = WATCHDOG_MINOR,
314 .name = "watchdog",
315 .fops = &s3c2410wdt_fops,
316};
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* interrupt handler code */
319
David Howells7d12e782006-10-05 14:55:46 +0100320static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100322 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 s3c2410wdt_keepalive();
325 return IRQ_HANDLED;
326}
327/* device interface */
328
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000329static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct resource *res;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100332 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100333 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int started = 0;
335 int ret;
336 int size;
337
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800338 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ben Dookse8ef92b2007-06-14 12:08:55 +0100340 dev = &pdev->dev;
341 wdt_dev = &pdev->dev;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* get the memory region for the watchdog timer */
344
345 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
346 if (res == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100347 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -ENOENT;
349 }
350
Ben Dooks0e65fb22008-06-22 22:36:51 +0100351 size = (res->end - res->start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 wdt_mem = request_mem_region(res->start, size, pdev->name);
353 if (wdt_mem == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100354 dev_err(dev, "failed to get memory region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000355 ret = -ENOENT;
356 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 wdt_base = ioremap(res->start, size);
Ben Dooksb4253f82008-06-22 22:36:49 +0100360 if (wdt_base == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100361 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000362 ret = -EINVAL;
363 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 DBG("probe: mapped wdt_base=%p\n", wdt_base);
367
Arnaud Patard62be07412007-05-05 15:53:15 +0200368 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
369 if (wdt_irq == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100370 dev_err(dev, "no irq resource specified\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000371 ret = -ENOENT;
372 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Arnaud Patard62be07412007-05-05 15:53:15 +0200375 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (ret != 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100377 dev_err(dev, "failed to install irq (%d)\n", ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000378 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Russell King3ae5eae2005-11-09 22:32:44 +0000381 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900382 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100383 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900384 ret = PTR_ERR(wdt_clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000385 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 clk_enable(wdt_clock);
389
390 /* see if we can actually set the requested timer margin, and if
391 * not, try the default value */
392
393 if (s3c2410wdt_set_heartbeat(tmr_margin)) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100394 started = s3c2410wdt_set_heartbeat(
395 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Alan Cox41dc8b72008-08-04 17:54:46 +0100397 if (started == 0)
398 dev_info(dev,
399 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100401 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000402 dev_info(dev, "default timer value is out of range, "
403 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ret = misc_register(&s3c2410wdt_miscdev);
407 if (ret) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100408 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 WATCHDOG_MINOR, ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000410 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100414 dev_info(dev, "starting watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 s3c2410wdt_start();
Ben Dooks655516c2006-04-19 23:02:56 +0100416 } else if (!tmr_atboot) {
417 /* if we're not enabling the watchdog, then ensure it is
418 * disabled if it has been left running from the bootloader
419 * or other source */
420
421 s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Ben Dooks46b814d2007-06-14 12:08:54 +0100424 /* print out a statement of readiness */
425
426 wtcon = readl(wdt_base + S3C2410_WTCON);
427
Ben Dookse8ef92b2007-06-14 12:08:55 +0100428 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100429 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
430 (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
431 (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
Alan Cox41dc8b72008-08-04 17:54:46 +0100432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000434
435 err_clk:
436 clk_disable(wdt_clock);
437 clk_put(wdt_clock);
438
439 err_irq:
440 free_irq(wdt_irq->start, pdev);
441
442 err_map:
443 iounmap(wdt_base);
444
445 err_req:
446 release_resource(wdt_mem);
447 kfree(wdt_mem);
448
449 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000452static int __devexit s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000454 release_resource(wdt_mem);
455 kfree(wdt_mem);
456 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000458 free_irq(wdt_irq->start, dev);
459 wdt_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000461 clk_disable(wdt_clock);
462 clk_put(wdt_clock);
463 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Amol Lade34477e2006-10-06 13:41:12 -0700465 iounmap(wdt_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 misc_deregister(&s3c2410wdt_miscdev);
467 return 0;
468}
469
Russell King3ae5eae2005-11-09 22:32:44 +0000470static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200471{
Alan Cox41dc8b72008-08-04 17:54:46 +0100472 s3c2410wdt_stop();
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200473}
474
Ben Dooksaf4bb822005-08-17 09:03:23 +0200475#ifdef CONFIG_PM
476
477static unsigned long wtcon_save;
478static unsigned long wtdat_save;
479
Russell King3ae5eae2005-11-09 22:32:44 +0000480static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200481{
Russell King9480e302005-10-28 09:52:56 -0700482 /* Save watchdog state, and turn it off. */
483 wtcon_save = readl(wdt_base + S3C2410_WTCON);
484 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200485
Russell King9480e302005-10-28 09:52:56 -0700486 /* Note that WTCNT doesn't need to be saved. */
487 s3c2410wdt_stop();
Ben Dooksaf4bb822005-08-17 09:03:23 +0200488
489 return 0;
490}
491
Russell King3ae5eae2005-11-09 22:32:44 +0000492static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200493{
Russell King9480e302005-10-28 09:52:56 -0700494 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200495
Russell King9480e302005-10-28 09:52:56 -0700496 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
497 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
498 writel(wtcon_save, wdt_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200499
Russell King9480e302005-10-28 09:52:56 -0700500 printk(KERN_INFO PFX "watchdog %sabled\n",
501 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200502
503 return 0;
504}
505
506#else
507#define s3c2410wdt_suspend NULL
508#define s3c2410wdt_resume NULL
509#endif /* CONFIG_PM */
510
511
Russell King3ae5eae2005-11-09 22:32:44 +0000512static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 .probe = s3c2410wdt_probe,
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000514 .remove = __devexit_p(s3c2410wdt_remove),
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200515 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200516 .suspend = s3c2410wdt_suspend,
517 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000518 .driver = {
519 .owner = THIS_MODULE,
520 .name = "s3c2410-wdt",
521 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
524
Alan Cox41dc8b72008-08-04 17:54:46 +0100525static char banner[] __initdata =
526 KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528static int __init watchdog_init(void)
529{
530 printk(banner);
Russell King3ae5eae2005-11-09 22:32:44 +0000531 return platform_driver_register(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534static void __exit watchdog_exit(void)
535{
Russell King3ae5eae2005-11-09 22:32:44 +0000536 platform_driver_unregister(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539module_init(watchdog_init);
540module_exit(watchdog_exit);
541
Ben Dooksaf4bb822005-08-17 09:03:23 +0200542MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
543 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
545MODULE_LICENSE("GPL");
546MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700547MODULE_ALIAS("platform:s3c2410-wdt");