blob: f1fe800658f408f933d437c4302172c2341e36f4 [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
122 spin_lock(&wdt_lock);
123 wtcon = readl(wdt_base + S3C2410_WTCON);
124 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
125 writel(wtcon, wdt_base + S3C2410_WTCON);
126 spin_unlock(&wdt_lock);
127}
128
129static void __s3c2410wdt_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 unsigned long wtcon;
132
133 wtcon = readl(wdt_base + S3C2410_WTCON);
134 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
135 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Alan Cox41dc8b72008-08-04 17:54:46 +0100138static void s3c2410wdt_stop(void)
139{
140 spin_lock(&wdt_lock);
141 __s3c2410wdt_stop();
142 spin_unlock(&wdt_lock);
143}
144
145static void s3c2410wdt_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 unsigned long wtcon;
148
Alan Cox41dc8b72008-08-04 17:54:46 +0100149 spin_lock(&wdt_lock);
150
151 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 wtcon = readl(wdt_base + S3C2410_WTCON);
154 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
155
156 if (soft_noboot) {
157 wtcon |= S3C2410_WTCON_INTEN;
158 wtcon &= ~S3C2410_WTCON_RSTEN;
159 } else {
160 wtcon &= ~S3C2410_WTCON_INTEN;
161 wtcon |= S3C2410_WTCON_RSTEN;
162 }
163
164 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800165 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 writel(wdt_count, wdt_base + S3C2410_WTDAT);
168 writel(wdt_count, wdt_base + S3C2410_WTCNT);
169 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100170 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return 0;
173}
174
175static int s3c2410wdt_set_heartbeat(int timeout)
176{
177 unsigned int freq = clk_get_rate(wdt_clock);
178 unsigned int count;
179 unsigned int divisor = 1;
180 unsigned long wtcon;
181
182 if (timeout < 1)
183 return -EINVAL;
184
185 freq /= 128;
186 count = timeout * freq;
187
188 DBG("%s: count=%d, timeout=%d, freq=%d\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800189 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* if the count is bigger than the watchdog register,
192 then work out what we need to do (and if) we can
193 actually make this value
194 */
195
196 if (count >= 0x10000) {
197 for (divisor = 1; divisor <= 0x100; divisor++) {
198 if ((count / divisor) < 0x10000)
199 break;
200 }
201
202 if ((count / divisor) >= 0x10000) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100203 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205 }
206 }
207
208 tmr_margin = timeout;
209
210 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800211 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 count /= divisor;
214 wdt_count = count;
215
216 /* update the pre-scaler */
217 wtcon = readl(wdt_base + S3C2410_WTCON);
218 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
219 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
220
221 writel(count, wdt_base + S3C2410_WTDAT);
222 writel(wtcon, wdt_base + S3C2410_WTCON);
223
224 return 0;
225}
226
227/*
228 * /dev/watchdog handling
229 */
230
231static int s3c2410wdt_open(struct inode *inode, struct file *file)
232{
Alan Cox41dc8b72008-08-04 17:54:46 +0100233 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EBUSY;
235
Ben Dooks25ff3782006-09-06 12:24:35 +0100236 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 __module_get(THIS_MODULE);
Ben Dooks25ff3782006-09-06 12:24:35 +0100238
239 allow_close = CLOSE_STATE_NOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* start the timer */
242 s3c2410wdt_start();
243 return nonseekable_open(inode, file);
244}
245
246static int s3c2410wdt_release(struct inode *inode, struct file *file)
247{
248 /*
249 * Shut off the timer.
250 * Lock it in if it's a module and we set nowayout
251 */
Ben Dooks25ff3782006-09-06 12:24:35 +0100252
Alan Cox41dc8b72008-08-04 17:54:46 +0100253 if (allow_close == CLOSE_STATE_ALLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 s3c2410wdt_stop();
Alan Cox41dc8b72008-08-04 17:54:46 +0100255 else {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100256 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 s3c2410wdt_keepalive();
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 allow_close = CLOSE_STATE_NOT;
Alan Cox41dc8b72008-08-04 17:54:46 +0100260 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262}
263
264static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
265 size_t len, loff_t *ppos)
266{
267 /*
268 * Refresh the timer.
269 */
Alan Cox41dc8b72008-08-04 17:54:46 +0100270 if (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (!nowayout) {
272 size_t i;
273
274 /* In case it was set long ago */
275 allow_close = CLOSE_STATE_NOT;
276
277 for (i = 0; i != len; i++) {
278 char c;
279
280 if (get_user(c, data + i))
281 return -EFAULT;
282 if (c == 'V')
283 allow_close = CLOSE_STATE_ALLOW;
284 }
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 s3c2410wdt_keepalive();
287 }
288 return len;
289}
290
291#define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
292
Alan Cox41dc8b72008-08-04 17:54:46 +0100293static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .options = OPTIONS,
295 .firmware_version = 0,
296 .identity = "S3C2410 Watchdog",
297};
298
299
Alan Cox41dc8b72008-08-04 17:54:46 +0100300static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
301 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 void __user *argp = (void __user *)arg;
304 int __user *p = argp;
305 int new_margin;
306
307 switch (cmd) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100308 default:
309 return -ENOTTY;
310 case WDIOC_GETSUPPORT:
311 return copy_to_user(argp, &s3c2410_wdt_ident,
312 sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
313 case WDIOC_GETSTATUS:
314 case WDIOC_GETBOOTSTATUS:
315 return put_user(0, p);
316 case WDIOC_KEEPALIVE:
317 s3c2410wdt_keepalive();
318 return 0;
319 case WDIOC_SETTIMEOUT:
320 if (get_user(new_margin, p))
321 return -EFAULT;
322 if (s3c2410wdt_set_heartbeat(new_margin))
323 return -EINVAL;
324 s3c2410wdt_keepalive();
325 return put_user(tmr_margin, p);
326 case WDIOC_GETTIMEOUT:
327 return put_user(tmr_margin, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/* kernel interface */
332
Arjan van de Ven62322d22006-07-03 00:24:21 -0700333static const struct file_operations s3c2410wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 .owner = THIS_MODULE,
335 .llseek = no_llseek,
336 .write = s3c2410wdt_write,
Alan Cox41dc8b72008-08-04 17:54:46 +0100337 .unlocked_ioctl = s3c2410wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .open = s3c2410wdt_open,
339 .release = s3c2410wdt_release,
340};
341
342static struct miscdevice s3c2410wdt_miscdev = {
343 .minor = WATCHDOG_MINOR,
344 .name = "watchdog",
345 .fops = &s3c2410wdt_fops,
346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* interrupt handler code */
349
David Howells7d12e782006-10-05 14:55:46 +0100350static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100352 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 s3c2410wdt_keepalive();
355 return IRQ_HANDLED;
356}
357/* device interface */
358
Russell King3ae5eae2005-11-09 22:32:44 +0000359static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct resource *res;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100362 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100363 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int started = 0;
365 int ret;
366 int size;
367
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800368 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Ben Dookse8ef92b2007-06-14 12:08:55 +0100370 dev = &pdev->dev;
371 wdt_dev = &pdev->dev;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* get the memory region for the watchdog timer */
374
375 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
376 if (res == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100377 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -ENOENT;
379 }
380
381 size = (res->end-res->start)+1;
382 wdt_mem = request_mem_region(res->start, size, pdev->name);
383 if (wdt_mem == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100384 dev_err(dev, "failed to get memory region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000385 ret = -ENOENT;
386 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 wdt_base = ioremap(res->start, size);
390 if (wdt_base == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100391 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000392 ret = -EINVAL;
393 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 DBG("probe: mapped wdt_base=%p\n", wdt_base);
397
Arnaud Patard62be07412007-05-05 15:53:15 +0200398 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
399 if (wdt_irq == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100400 dev_err(dev, "no irq resource specified\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000401 ret = -ENOENT;
402 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
Arnaud Patard62be07412007-05-05 15:53:15 +0200405 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (ret != 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100407 dev_err(dev, "failed to install irq (%d)\n", ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000408 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Russell King3ae5eae2005-11-09 22:32:44 +0000411 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900412 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100413 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900414 ret = PTR_ERR(wdt_clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000415 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 clk_enable(wdt_clock);
419
420 /* see if we can actually set the requested timer margin, and if
421 * not, try the default value */
422
423 if (s3c2410wdt_set_heartbeat(tmr_margin)) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100424 started = s3c2410wdt_set_heartbeat(
425 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Alan Cox41dc8b72008-08-04 17:54:46 +0100427 if (started == 0)
428 dev_info(dev,
429 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100431 else
Ben Dookse8ef92b2007-06-14 12:08:55 +0100432 dev_info(dev, "default timer value is out of range, cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ret = misc_register(&s3c2410wdt_miscdev);
436 if (ret) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100437 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 WATCHDOG_MINOR, ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000439 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100443 dev_info(dev, "starting watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 s3c2410wdt_start();
Ben Dooks655516c2006-04-19 23:02:56 +0100445 } else if (!tmr_atboot) {
446 /* if we're not enabling the watchdog, then ensure it is
447 * disabled if it has been left running from the bootloader
448 * or other source */
449
450 s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Ben Dooks46b814d2007-06-14 12:08:54 +0100453 /* print out a statement of readiness */
454
455 wtcon = readl(wdt_base + S3C2410_WTCON);
456
Ben Dookse8ef92b2007-06-14 12:08:55 +0100457 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100458 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
459 (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
460 (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
Alan Cox41dc8b72008-08-04 17:54:46 +0100461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000463
464 err_clk:
465 clk_disable(wdt_clock);
466 clk_put(wdt_clock);
467
468 err_irq:
469 free_irq(wdt_irq->start, pdev);
470
471 err_map:
472 iounmap(wdt_base);
473
474 err_req:
475 release_resource(wdt_mem);
476 kfree(wdt_mem);
477
478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Russell King3ae5eae2005-11-09 22:32:44 +0000481static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000483 release_resource(wdt_mem);
484 kfree(wdt_mem);
485 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000487 free_irq(wdt_irq->start, dev);
488 wdt_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000490 clk_disable(wdt_clock);
491 clk_put(wdt_clock);
492 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Amol Lade34477e2006-10-06 13:41:12 -0700494 iounmap(wdt_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 misc_deregister(&s3c2410wdt_miscdev);
496 return 0;
497}
498
Russell King3ae5eae2005-11-09 22:32:44 +0000499static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200500{
Alan Cox41dc8b72008-08-04 17:54:46 +0100501 s3c2410wdt_stop();
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200502}
503
Ben Dooksaf4bb822005-08-17 09:03:23 +0200504#ifdef CONFIG_PM
505
506static unsigned long wtcon_save;
507static unsigned long wtdat_save;
508
Russell King3ae5eae2005-11-09 22:32:44 +0000509static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200510{
Russell King9480e302005-10-28 09:52:56 -0700511 /* Save watchdog state, and turn it off. */
512 wtcon_save = readl(wdt_base + S3C2410_WTCON);
513 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200514
Russell King9480e302005-10-28 09:52:56 -0700515 /* Note that WTCNT doesn't need to be saved. */
516 s3c2410wdt_stop();
Ben Dooksaf4bb822005-08-17 09:03:23 +0200517
518 return 0;
519}
520
Russell King3ae5eae2005-11-09 22:32:44 +0000521static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200522{
Russell King9480e302005-10-28 09:52:56 -0700523 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200524
Russell King9480e302005-10-28 09:52:56 -0700525 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
526 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
527 writel(wtcon_save, wdt_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200528
Russell King9480e302005-10-28 09:52:56 -0700529 printk(KERN_INFO PFX "watchdog %sabled\n",
530 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200531
532 return 0;
533}
534
535#else
536#define s3c2410wdt_suspend NULL
537#define s3c2410wdt_resume NULL
538#endif /* CONFIG_PM */
539
540
Russell King3ae5eae2005-11-09 22:32:44 +0000541static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 .probe = s3c2410wdt_probe,
543 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200544 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200545 .suspend = s3c2410wdt_suspend,
546 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000547 .driver = {
548 .owner = THIS_MODULE,
549 .name = "s3c2410-wdt",
550 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551};
552
553
Alan Cox41dc8b72008-08-04 17:54:46 +0100554static char banner[] __initdata =
555 KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557static int __init watchdog_init(void)
558{
559 printk(banner);
Russell King3ae5eae2005-11-09 22:32:44 +0000560 return platform_driver_register(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563static void __exit watchdog_exit(void)
564{
Russell King3ae5eae2005-11-09 22:32:44 +0000565 platform_driver_unregister(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568module_init(watchdog_init);
569module_exit(watchdog_exit);
570
Ben Dooksaf4bb822005-08-17 09:03:23 +0200571MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
572 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
574MODULE_LICENSE("GPL");
575MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700576MODULE_ALIAS("platform:s3c2410-wdt");