blob: 27af55f193d2d456a9e1a0ea47836ca40279b586 [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
162static 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",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800176 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
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) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100190 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
192 }
193 }
194
195 tmr_margin = timeout;
196
197 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800198 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
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
218static int s3c2410wdt_open(struct inode *inode, struct file *file)
219{
Alan Cox41dc8b72008-08-04 17:54:46 +0100220 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EBUSY;
222
Ben Dooks25ff3782006-09-06 12:24:35 +0100223 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 __module_get(THIS_MODULE);
Ben Dooks25ff3782006-09-06 12:24:35 +0100225
226 allow_close = CLOSE_STATE_NOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* start the timer */
229 s3c2410wdt_start();
230 return nonseekable_open(inode, file);
231}
232
233static 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 */
Ben Dooks25ff3782006-09-06 12:24:35 +0100239
Alan Cox41dc8b72008-08-04 17:54:46 +0100240 if (allow_close == CLOSE_STATE_ALLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 s3c2410wdt_stop();
Alan Cox41dc8b72008-08-04 17:54:46 +0100242 else {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100243 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 s3c2410wdt_keepalive();
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 allow_close = CLOSE_STATE_NOT;
Alan Cox41dc8b72008-08-04 17:54:46 +0100247 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250
251static 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 */
Alan Cox41dc8b72008-08-04 17:54:46 +0100257 if (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 s3c2410wdt_keepalive();
274 }
275 return len;
276}
277
278#define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
279
Alan Cox41dc8b72008-08-04 17:54:46 +0100280static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .options = OPTIONS,
282 .firmware_version = 0,
283 .identity = "S3C2410 Watchdog",
284};
285
286
Alan Cox41dc8b72008-08-04 17:54:46 +0100287static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
288 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 void __user *argp = (void __user *)arg;
291 int __user *p = argp;
292 int new_margin;
293
294 switch (cmd) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100295 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);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000313 default:
314 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* kernel interface */
319
Arjan van de Ven62322d22006-07-03 00:24:21 -0700320static const struct file_operations s3c2410wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 .owner = THIS_MODULE,
322 .llseek = no_llseek,
323 .write = s3c2410wdt_write,
Alan Cox41dc8b72008-08-04 17:54:46 +0100324 .unlocked_ioctl = s3c2410wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .open = s3c2410wdt_open,
326 .release = s3c2410wdt_release,
327};
328
329static struct miscdevice s3c2410wdt_miscdev = {
330 .minor = WATCHDOG_MINOR,
331 .name = "watchdog",
332 .fops = &s3c2410wdt_fops,
333};
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* interrupt handler code */
336
David Howells7d12e782006-10-05 14:55:46 +0100337static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100339 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 s3c2410wdt_keepalive();
342 return IRQ_HANDLED;
343}
344/* device interface */
345
Russell King3ae5eae2005-11-09 22:32:44 +0000346static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct resource *res;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100349 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100350 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int started = 0;
352 int ret;
353 int size;
354
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800355 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Ben Dookse8ef92b2007-06-14 12:08:55 +0100357 dev = &pdev->dev;
358 wdt_dev = &pdev->dev;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* get the memory region for the watchdog timer */
361
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 if (res == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100364 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 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) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100371 dev_err(dev, "failed to get memory region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000372 ret = -ENOENT;
373 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 wdt_base = ioremap(res->start, size);
Ben Dooksb4253f82008-06-22 22:36:49 +0100377 if (wdt_base == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100378 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000379 ret = -EINVAL;
380 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 DBG("probe: mapped wdt_base=%p\n", wdt_base);
384
Arnaud Patard62be07412007-05-05 15:53:15 +0200385 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
386 if (wdt_irq == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100387 dev_err(dev, "no irq resource specified\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000388 ret = -ENOENT;
389 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Arnaud Patard62be07412007-05-05 15:53:15 +0200392 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (ret != 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100394 dev_err(dev, "failed to install irq (%d)\n", ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000395 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Russell King3ae5eae2005-11-09 22:32:44 +0000398 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900399 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100400 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900401 ret = PTR_ERR(wdt_clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000402 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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)) {
Alan Cox41dc8b72008-08-04 17:54:46 +0100411 started = s3c2410wdt_set_heartbeat(
412 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alan Cox41dc8b72008-08-04 17:54:46 +0100414 if (started == 0)
415 dev_info(dev,
416 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100418 else
Ben Dookse8ef92b2007-06-14 12:08:55 +0100419 dev_info(dev, "default timer value is out of range, cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ret = misc_register(&s3c2410wdt_miscdev);
423 if (ret) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100424 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 WATCHDOG_MINOR, ret);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000426 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100430 dev_info(dev, "starting watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 s3c2410wdt_start();
Ben Dooks655516c2006-04-19 23:02:56 +0100432 } 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Ben Dooks46b814d2007-06-14 12:08:54 +0100440 /* print out a statement of readiness */
441
442 wtcon = readl(wdt_base + S3C2410_WTCON);
443
Ben Dookse8ef92b2007-06-14 12:08:55 +0100444 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100445 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
446 (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
447 (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
Alan Cox41dc8b72008-08-04 17:54:46 +0100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000450
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Russell King3ae5eae2005-11-09 22:32:44 +0000468static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000470 release_resource(wdt_mem);
471 kfree(wdt_mem);
472 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000474 free_irq(wdt_irq->start, dev);
475 wdt_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000477 clk_disable(wdt_clock);
478 clk_put(wdt_clock);
479 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Amol Lade34477e2006-10-06 13:41:12 -0700481 iounmap(wdt_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 misc_deregister(&s3c2410wdt_miscdev);
483 return 0;
484}
485
Russell King3ae5eae2005-11-09 22:32:44 +0000486static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200487{
Alan Cox41dc8b72008-08-04 17:54:46 +0100488 s3c2410wdt_stop();
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200489}
490
Ben Dooksaf4bb822005-08-17 09:03:23 +0200491#ifdef CONFIG_PM
492
493static unsigned long wtcon_save;
494static unsigned long wtdat_save;
495
Russell King3ae5eae2005-11-09 22:32:44 +0000496static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200497{
Russell King9480e302005-10-28 09:52:56 -0700498 /* Save watchdog state, and turn it off. */
499 wtcon_save = readl(wdt_base + S3C2410_WTCON);
500 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200501
Russell King9480e302005-10-28 09:52:56 -0700502 /* Note that WTCNT doesn't need to be saved. */
503 s3c2410wdt_stop();
Ben Dooksaf4bb822005-08-17 09:03:23 +0200504
505 return 0;
506}
507
Russell King3ae5eae2005-11-09 22:32:44 +0000508static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200509{
Russell King9480e302005-10-28 09:52:56 -0700510 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200511
Russell King9480e302005-10-28 09:52:56 -0700512 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);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200515
Russell King9480e302005-10-28 09:52:56 -0700516 printk(KERN_INFO PFX "watchdog %sabled\n",
517 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200518
519 return 0;
520}
521
522#else
523#define s3c2410wdt_suspend NULL
524#define s3c2410wdt_resume NULL
525#endif /* CONFIG_PM */
526
527
Russell King3ae5eae2005-11-09 22:32:44 +0000528static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .probe = s3c2410wdt_probe,
530 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200531 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200532 .suspend = s3c2410wdt_suspend,
533 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000534 .driver = {
535 .owner = THIS_MODULE,
536 .name = "s3c2410-wdt",
537 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538};
539
540
Alan Cox41dc8b72008-08-04 17:54:46 +0100541static char banner[] __initdata =
542 KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544static int __init watchdog_init(void)
545{
546 printk(banner);
Russell King3ae5eae2005-11-09 22:32:44 +0000547 return platform_driver_register(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static void __exit watchdog_exit(void)
551{
Russell King3ae5eae2005-11-09 22:32:44 +0000552 platform_driver_unregister(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555module_init(watchdog_init);
556module_exit(watchdog_exit);
557
Ben Dooksaf4bb822005-08-17 09:03:23 +0200558MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
559 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
561MODULE_LICENSE("GPL");
562MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700563MODULE_ALIAS("platform:s3c2410-wdt");