blob: 3da2b90d2fe6add80192c275fc8c8f7d59a1db55 [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,
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 *
Ben Dooksaf4bb822005-08-17 09:03:23 +020030 * 11-Jan-2005 BJD Fixed divide-by-2 in timeout code
31 *
32 * 25-Jan-2005 DA Added suspend/resume support
Ben Dooks94f1e9f2005-08-17 09:04:52 +020033 * Replaced reboot notifier with .shutdown method
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * 10-Mar-2005 LCVR Changed S3C2410_VA to S3C24XX_VA
36*/
37
38#include <linux/module.h>
39#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/timer.h>
42#include <linux/miscdevice.h>
43#include <linux/watchdog.h>
44#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000048#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010049#include <linux/uaccess.h>
50#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Russell Kinga09e64f2008-08-05 16:14:15 +010052#include <mach/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ben Dooksb4307082007-07-24 13:28:01 +010054#undef S3C_VA_WATCHDOG
55#define S3C_VA_WATCHDOG (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ben Dooksb4307082007-07-24 13:28:01 +010057#include <asm/plat-s3c/regs-watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define PFX "s3c2410-wdt: "
60
61#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
62#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
63
Ben Dooks25ff3782006-09-06 12:24:35 +010064static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
66static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010067static int soft_noboot;
68static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70module_param(tmr_margin, int, 0);
71module_param(tmr_atboot, int, 0);
72module_param(nowayout, int, 0);
73module_param(soft_noboot, int, 0);
74module_param(debug, int, 0);
75
Alan Cox41dc8b72008-08-04 17:54:46 +010076MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
77 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
78MODULE_PARM_DESC(tmr_atboot,
79 "Watchdog is started at boot time if set to 1, default="
80 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
81MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
82 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
85
86
87typedef enum close_state {
88 CLOSE_STATE_NOT,
Alan Cox41dc8b72008-08-04 17:54:46 +010089 CLOSE_STATE_ALLOW = 0x4021
Linus Torvalds1da177e2005-04-16 15:20:36 -070090} close_state_t;
91
Alan Cox41dc8b72008-08-04 17:54:46 +010092static unsigned long open_lock;
Ben Dookse8ef92b2007-06-14 12:08:55 +010093static struct device *wdt_dev; /* platform device attached to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct resource *wdt_mem;
95static struct resource *wdt_irq;
96static struct clk *wdt_clock;
97static void __iomem *wdt_base;
98static unsigned int wdt_count;
99static close_state_t allow_close;
Alan Cox41dc8b72008-08-04 17:54:46 +0100100static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* watchdog control routines */
103
104#define DBG(msg...) do { \
105 if (debug) \
106 printk(KERN_INFO msg); \
Alan Cox41dc8b72008-08-04 17:54:46 +0100107 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* functions */
110
Alan Cox41dc8b72008-08-04 17:54:46 +0100111static void s3c2410wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Alan Cox41dc8b72008-08-04 17:54:46 +0100113 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 writel(wdt_count, wdt_base + S3C2410_WTCNT);
Alan Cox41dc8b72008-08-04 17:54:46 +0100115 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Alan Cox41dc8b72008-08-04 17:54:46 +0100118static void __s3c2410wdt_stop(void)
119{
120 unsigned long wtcon;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 wtcon = readl(wdt_base + S3C2410_WTCON);
123 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
124 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Alan Cox41dc8b72008-08-04 17:54:46 +0100127static void s3c2410wdt_stop(void)
128{
129 spin_lock(&wdt_lock);
130 __s3c2410wdt_stop();
131 spin_unlock(&wdt_lock);
132}
133
134static void s3c2410wdt_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long wtcon;
137
Alan Cox41dc8b72008-08-04 17:54:46 +0100138 spin_lock(&wdt_lock);
139
140 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
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",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800154 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 writel(wdt_count, wdt_base + S3C2410_WTDAT);
157 writel(wdt_count, wdt_base + S3C2410_WTCNT);
158 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100159 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return 0;
162}
163
164static 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",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800178 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
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) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100192 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EINVAL;
194 }
195 }
196
197 tmr_margin = timeout;
198
199 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800200 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
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
220static int s3c2410wdt_open(struct inode *inode, struct file *file)
221{
Alan Cox41dc8b72008-08-04 17:54:46 +0100222 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EBUSY;
224
Ben Dooks25ff3782006-09-06 12:24:35 +0100225 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 __module_get(THIS_MODULE);
Ben Dooks25ff3782006-09-06 12:24:35 +0100227
228 allow_close = CLOSE_STATE_NOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* start the timer */
231 s3c2410wdt_start();
232 return nonseekable_open(inode, file);
233}
234
235static 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 */
Ben Dooks25ff3782006-09-06 12:24:35 +0100241
Alan Cox41dc8b72008-08-04 17:54:46 +0100242 if (allow_close == CLOSE_STATE_ALLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 s3c2410wdt_stop();
Alan Cox41dc8b72008-08-04 17:54:46 +0100244 else {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100245 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 s3c2410wdt_keepalive();
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 allow_close = CLOSE_STATE_NOT;
Alan Cox41dc8b72008-08-04 17:54:46 +0100249 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251}
252
253static 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 */
Alan Cox41dc8b72008-08-04 17:54:46 +0100259 if (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 s3c2410wdt_keepalive();
276 }
277 return len;
278}
279
280#define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
281
Alan Cox41dc8b72008-08-04 17:54:46 +0100282static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .options = OPTIONS,
284 .firmware_version = 0,
285 .identity = "S3C2410 Watchdog",
286};
287
288
Alan Cox41dc8b72008-08-04 17:54:46 +0100289static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
290 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 void __user *argp = (void __user *)arg;
293 int __user *p = argp;
294 int new_margin;
295
296 switch (cmd) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100297 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);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000315 default:
316 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* kernel interface */
321
Arjan van de Ven62322d22006-07-03 00:24:21 -0700322static const struct file_operations s3c2410wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .owner = THIS_MODULE,
324 .llseek = no_llseek,
325 .write = s3c2410wdt_write,
Alan Cox41dc8b72008-08-04 17:54:46 +0100326 .unlocked_ioctl = s3c2410wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .open = s3c2410wdt_open,
328 .release = s3c2410wdt_release,
329};
330
331static struct miscdevice s3c2410wdt_miscdev = {
332 .minor = WATCHDOG_MINOR,
333 .name = "watchdog",
334 .fops = &s3c2410wdt_fops,
335};
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/* interrupt handler code */
338
David Howells7d12e782006-10-05 14:55:46 +0100339static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100341 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 s3c2410wdt_keepalive();
344 return IRQ_HANDLED;
345}
346/* device interface */
347
Russell King3ae5eae2005-11-09 22:32:44 +0000348static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct resource *res;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100351 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100352 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int started = 0;
354 int ret;
355 int size;
356
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800357 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ben Dookse8ef92b2007-06-14 12:08:55 +0100359 dev = &pdev->dev;
360 wdt_dev = &pdev->dev;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* get the memory region for the watchdog timer */
363
364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365 if (res == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100366 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 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) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100373 dev_err(dev, "failed to get memory region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000374 ret = -ENOENT;
375 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 wdt_base = ioremap(res->start, size);
379 if (wdt_base == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100380 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000381 ret = -EINVAL;
382 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 DBG("probe: mapped wdt_base=%p\n", wdt_base);
386
Arnaud Patard62be07412007-05-05 15:53:15 +0200387 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
388 if (wdt_irq == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100389 dev_err(dev, "no irq resource specified\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000390 ret = -ENOENT;
391 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Arnaud Patard62be07412007-05-05 15:53:15 +0200394 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ret != 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100396 dev_err(dev, "failed to install irq (%d)\n", ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000397 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Russell King3ae5eae2005-11-09 22:32:44 +0000400 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900401 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100402 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900403 ret = PTR_ERR(wdt_clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000404 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 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)) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100413 started = s3c2410wdt_set_heartbeat(
414 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alan Cox41dc8b72008-08-04 17:54:46 +0100416 if (started == 0)
417 dev_info(dev,
418 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100420 else
Ben Dookse8ef92b2007-06-14 12:08:55 +0100421 dev_info(dev, "default timer value is out of range, cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ret = misc_register(&s3c2410wdt_miscdev);
425 if (ret) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100426 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 WATCHDOG_MINOR, ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000428 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100432 dev_info(dev, "starting watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 s3c2410wdt_start();
Ben Dooks655516c2006-04-19 23:02:56 +0100434 } 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Ben Dooks46b814d2007-06-14 12:08:54 +0100442 /* print out a statement of readiness */
443
444 wtcon = readl(wdt_base + S3C2410_WTCON);
445
Ben Dookse8ef92b2007-06-14 12:08:55 +0100446 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100447 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
448 (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
449 (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
Alan Cox41dc8b72008-08-04 17:54:46 +0100450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000452
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Russell King3ae5eae2005-11-09 22:32:44 +0000470static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000472 release_resource(wdt_mem);
473 kfree(wdt_mem);
474 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000476 free_irq(wdt_irq->start, dev);
477 wdt_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000479 clk_disable(wdt_clock);
480 clk_put(wdt_clock);
481 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Amol Lade34477e2006-10-06 13:41:12 -0700483 iounmap(wdt_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 misc_deregister(&s3c2410wdt_miscdev);
485 return 0;
486}
487
Russell King3ae5eae2005-11-09 22:32:44 +0000488static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200489{
Alan Cox41dc8b72008-08-04 17:54:46 +0100490 s3c2410wdt_stop();
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200491}
492
Ben Dooksaf4bb822005-08-17 09:03:23 +0200493#ifdef CONFIG_PM
494
495static unsigned long wtcon_save;
496static unsigned long wtdat_save;
497
Russell King3ae5eae2005-11-09 22:32:44 +0000498static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200499{
Russell King9480e302005-10-28 09:52:56 -0700500 /* Save watchdog state, and turn it off. */
501 wtcon_save = readl(wdt_base + S3C2410_WTCON);
502 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200503
Russell King9480e302005-10-28 09:52:56 -0700504 /* Note that WTCNT doesn't need to be saved. */
505 s3c2410wdt_stop();
Ben Dooksaf4bb822005-08-17 09:03:23 +0200506
507 return 0;
508}
509
Russell King3ae5eae2005-11-09 22:32:44 +0000510static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200511{
Russell King9480e302005-10-28 09:52:56 -0700512 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200513
Russell King9480e302005-10-28 09:52:56 -0700514 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);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200517
Russell King9480e302005-10-28 09:52:56 -0700518 printk(KERN_INFO PFX "watchdog %sabled\n",
519 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200520
521 return 0;
522}
523
524#else
525#define s3c2410wdt_suspend NULL
526#define s3c2410wdt_resume NULL
527#endif /* CONFIG_PM */
528
529
Russell King3ae5eae2005-11-09 22:32:44 +0000530static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 .probe = s3c2410wdt_probe,
532 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200533 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200534 .suspend = s3c2410wdt_suspend,
535 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000536 .driver = {
537 .owner = THIS_MODULE,
538 .name = "s3c2410-wdt",
539 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540};
541
542
Alan Cox41dc8b72008-08-04 17:54:46 +0100543static char banner[] __initdata =
544 KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546static int __init watchdog_init(void)
547{
548 printk(banner);
Russell King3ae5eae2005-11-09 22:32:44 +0000549 return platform_driver_register(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static void __exit watchdog_exit(void)
553{
Russell King3ae5eae2005-11-09 22:32:44 +0000554 platform_driver_unregister(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557module_init(watchdog_init);
558module_exit(watchdog_exit);
559
Ben Dooksaf4bb822005-08-17 09:03:23 +0200560MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
561 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
563MODULE_LICENSE("GPL");
564MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700565MODULE_ALIAS("platform:s3c2410-wdt");