blob: ce18029011f7605f247868398a78332f8f1e1026 [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
Joe Perches27c766a2012-02-15 15:06:19 -080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/timer.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020032#include <linux/miscdevice.h> /* for MODULE_ALIAS_MISCDEV */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010035#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000037#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010038#include <linux/uaccess.h>
39#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000040#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020042#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Russell Kinga09e64f2008-08-05 16:14:15 +010044#include <mach/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ben Dooksb4307082007-07-24 13:28:01 +010046#undef S3C_VA_WATCHDOG
47#define S3C_VA_WATCHDOG (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Ben Dooks180ee702008-10-30 10:14:32 +000049#include <plat/regs-watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
52#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
53
Ben Dooks25ff3782006-09-06 12:24:35 +010054static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
56static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010057static int soft_noboot;
58static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60module_param(tmr_margin, int, 0);
61module_param(tmr_atboot, int, 0);
62module_param(nowayout, int, 0);
63module_param(soft_noboot, int, 0);
64module_param(debug, int, 0);
65
Randy Dunlap76550d32010-05-01 09:46:15 -070066MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010067 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
68MODULE_PARM_DESC(tmr_atboot,
69 "Watchdog is started at boot time if set to 1, default="
70 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
71MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
72 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000073MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -070074 "0 to reboot (default 0)");
75MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ben Dookse8ef92b2007-06-14 12:08:55 +010077static struct device *wdt_dev; /* platform device attached to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct resource *wdt_mem;
79static struct resource *wdt_irq;
80static struct clk *wdt_clock;
81static void __iomem *wdt_base;
82static unsigned int wdt_count;
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
Joe Perches27c766a2012-02-15 15:06:19 -080087#define DBG(fmt, ...) \
88do { \
89 if (debug) \
90 pr_info(fmt, ##__VA_ARGS__); \
91} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* functions */
94
Wolfram Sang25dc46e2011-09-26 15:40:14 +020095static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Alan Cox41dc8b72008-08-04 17:54:46 +010097 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 writel(wdt_count, wdt_base + S3C2410_WTCNT);
Alan Cox41dc8b72008-08-04 17:54:46 +010099 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Alan Cox41dc8b72008-08-04 17:54:46 +0100104static void __s3c2410wdt_stop(void)
105{
106 unsigned long wtcon;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 wtcon = readl(wdt_base + S3C2410_WTCON);
109 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
110 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200113static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100114{
115 spin_lock(&wdt_lock);
116 __s3c2410wdt_stop();
117 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200118
119 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100120}
121
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200122static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 unsigned long wtcon;
125
Alan Cox41dc8b72008-08-04 17:54:46 +0100126 spin_lock(&wdt_lock);
127
128 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 wtcon = readl(wdt_base + S3C2410_WTCON);
131 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
132
133 if (soft_noboot) {
134 wtcon |= S3C2410_WTCON_INTEN;
135 wtcon &= ~S3C2410_WTCON_RSTEN;
136 } else {
137 wtcon &= ~S3C2410_WTCON_INTEN;
138 wtcon |= S3C2410_WTCON_RSTEN;
139 }
140
141 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800142 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 writel(wdt_count, wdt_base + S3C2410_WTDAT);
145 writel(wdt_count, wdt_base + S3C2410_WTCNT);
146 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100147 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200148
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Ben Dookse02f8382009-10-30 00:30:25 +0000152static inline int s3c2410wdt_is_running(void)
153{
154 return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
155}
156
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200157static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Ben Dookse02f8382009-10-30 00:30:25 +0000159 unsigned long freq = clk_get_rate(wdt_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int count;
161 unsigned int divisor = 1;
162 unsigned long wtcon;
163
164 if (timeout < 1)
165 return -EINVAL;
166
167 freq /= 128;
168 count = timeout * freq;
169
Ben Dookse02f8382009-10-30 00:30:25 +0000170 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800171 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* if the count is bigger than the watchdog register,
174 then work out what we need to do (and if) we can
175 actually make this value
176 */
177
178 if (count >= 0x10000) {
179 for (divisor = 1; divisor <= 0x100; divisor++) {
180 if ((count / divisor) < 0x10000)
181 break;
182 }
183
184 if ((count / divisor) >= 0x10000) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100185 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
187 }
188 }
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800191 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 count /= divisor;
194 wdt_count = count;
195
196 /* update the pre-scaler */
197 wtcon = readl(wdt_base + S3C2410_WTCON);
198 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
199 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
200
201 writel(count, wdt_base + S3C2410_WTDAT);
202 writel(wtcon, wdt_base + S3C2410_WTCON);
203
204 return 0;
205}
206
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000207#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Alan Cox41dc8b72008-08-04 17:54:46 +0100209static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .options = OPTIONS,
211 .firmware_version = 0,
212 .identity = "S3C2410 Watchdog",
213};
214
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200215static struct watchdog_ops s3c2410wdt_ops = {
216 .owner = THIS_MODULE,
217 .start = s3c2410wdt_start,
218 .stop = s3c2410wdt_stop,
219 .ping = s3c2410wdt_keepalive,
220 .set_timeout = s3c2410wdt_set_heartbeat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200223static struct watchdog_device s3c2410_wdd = {
224 .info = &s3c2410_wdt_ident,
225 .ops = &s3c2410wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* interrupt handler code */
229
David Howells7d12e782006-10-05 14:55:46 +0100230static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100232 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200234 s3c2410wdt_keepalive(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return IRQ_HANDLED;
236}
Ben Dookse02f8382009-10-30 00:30:25 +0000237
238
239#ifdef CONFIG_CPU_FREQ
240
241static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
242 unsigned long val, void *data)
243{
244 int ret;
245
246 if (!s3c2410wdt_is_running())
247 goto done;
248
249 if (val == CPUFREQ_PRECHANGE) {
250 /* To ensure that over the change we don't cause the
251 * watchdog to trigger, we perform an keep-alive if
252 * the watchdog is running.
253 */
254
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200255 s3c2410wdt_keepalive(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000256 } else if (val == CPUFREQ_POSTCHANGE) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200257 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000258
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200259 ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000260
261 if (ret >= 0)
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200262 s3c2410wdt_start(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000263 else
264 goto err;
265 }
266
267done:
268 return 0;
269
270 err:
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200271 dev_err(wdt_dev, "cannot set new value for timeout %d\n",
272 s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000273 return ret;
274}
275
276static struct notifier_block s3c2410wdt_cpufreq_transition_nb = {
277 .notifier_call = s3c2410wdt_cpufreq_transition,
278};
279
280static inline int s3c2410wdt_cpufreq_register(void)
281{
282 return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb,
283 CPUFREQ_TRANSITION_NOTIFIER);
284}
285
286static inline void s3c2410wdt_cpufreq_deregister(void)
287{
288 cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb,
289 CPUFREQ_TRANSITION_NOTIFIER);
290}
291
292#else
293static inline int s3c2410wdt_cpufreq_register(void)
294{
295 return 0;
296}
297
298static inline void s3c2410wdt_cpufreq_deregister(void)
299{
300}
301#endif
302
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000303static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100305 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100306 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int started = 0;
308 int ret;
309 int size;
310
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800311 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ben Dookse8ef92b2007-06-14 12:08:55 +0100313 dev = &pdev->dev;
314 wdt_dev = &pdev->dev;
315
Julia Lawallf72401e2011-02-26 17:34:38 +0100316 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
317 if (wdt_mem == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100318 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -ENOENT;
320 }
321
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900322 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
323 if (wdt_irq == NULL) {
324 dev_err(dev, "no irq resource specified\n");
325 ret = -ENOENT;
326 goto err;
327 }
328
329 /* get the memory region for the watchdog timer */
330
Julia Lawallf72401e2011-02-26 17:34:38 +0100331 size = resource_size(wdt_mem);
332 if (!request_mem_region(wdt_mem->start, size, pdev->name)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100333 dev_err(dev, "failed to get memory region\n");
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900334 ret = -EBUSY;
335 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Julia Lawallf72401e2011-02-26 17:34:38 +0100338 wdt_base = ioremap(wdt_mem->start, size);
Ben Dooksb4253f82008-06-22 22:36:49 +0100339 if (wdt_base == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100340 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000341 ret = -EINVAL;
342 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 DBG("probe: mapped wdt_base=%p\n", wdt_base);
346
Russell King3ae5eae2005-11-09 22:32:44 +0000347 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900348 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100349 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900350 ret = PTR_ERR(wdt_clock);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900351 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 clk_enable(wdt_clock);
355
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900356 ret = s3c2410wdt_cpufreq_register();
357 if (ret < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800358 pr_err("failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000359 goto err_clk;
360 }
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* see if we can actually set the requested timer margin, and if
363 * not, try the default value */
364
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200365 if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, tmr_margin)) {
366 started = s3c2410wdt_set_heartbeat(&s3c2410_wdd,
Alan Cox41dc8b72008-08-04 17:54:46 +0100367 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Alan Cox41dc8b72008-08-04 17:54:46 +0100369 if (started == 0)
370 dev_info(dev,
371 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100373 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000374 dev_info(dev, "default timer value is out of range, "
375 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900378 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
379 if (ret != 0) {
380 dev_err(dev, "failed to install irq (%d)\n", ret);
381 goto err_cpufreq;
382 }
383
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100384 watchdog_set_nowayout(&s3c2410_wdd, nowayout);
385
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200386 ret = watchdog_register_device(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200388 dev_err(dev, "cannot register watchdog (%d)\n", ret);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900389 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100393 dev_info(dev, "starting watchdog timer\n");
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200394 s3c2410wdt_start(&s3c2410_wdd);
Ben Dooks655516c2006-04-19 23:02:56 +0100395 } else if (!tmr_atboot) {
396 /* if we're not enabling the watchdog, then ensure it is
397 * disabled if it has been left running from the bootloader
398 * or other source */
399
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200400 s3c2410wdt_stop(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Ben Dooks46b814d2007-06-14 12:08:54 +0100403 /* print out a statement of readiness */
404
405 wtcon = readl(wdt_base + S3C2410_WTCON);
406
Ben Dookse8ef92b2007-06-14 12:08:55 +0100407 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100408 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400409 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
410 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000413
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900414 err_irq:
415 free_irq(wdt_irq->start, pdev);
416
Ben Dookse02f8382009-10-30 00:30:25 +0000417 err_cpufreq:
418 s3c2410wdt_cpufreq_deregister();
419
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000420 err_clk:
421 clk_disable(wdt_clock);
422 clk_put(wdt_clock);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900423 wdt_clock = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000424
425 err_map:
426 iounmap(wdt_base);
427
428 err_req:
Julia Lawallf72401e2011-02-26 17:34:38 +0100429 release_mem_region(wdt_mem->start, size);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000430
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900431 err:
432 wdt_irq = NULL;
433 wdt_mem = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000437static int __devexit s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200439 watchdog_unregister_device(&s3c2410_wdd);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000440
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900441 free_irq(wdt_irq->start, dev);
442
Ben Dookse02f8382009-10-30 00:30:25 +0000443 s3c2410wdt_cpufreq_deregister();
444
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000445 clk_disable(wdt_clock);
446 clk_put(wdt_clock);
447 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Amol Lade34477e2006-10-06 13:41:12 -0700449 iounmap(wdt_base);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000450
Julia Lawallf72401e2011-02-26 17:34:38 +0100451 release_mem_region(wdt_mem->start, resource_size(wdt_mem));
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900452 wdt_irq = NULL;
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000453 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
Russell King3ae5eae2005-11-09 22:32:44 +0000457static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200458{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200459 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200460}
461
Ben Dooksaf4bb822005-08-17 09:03:23 +0200462#ifdef CONFIG_PM
463
464static unsigned long wtcon_save;
465static unsigned long wtdat_save;
466
Russell King3ae5eae2005-11-09 22:32:44 +0000467static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200468{
Russell King9480e302005-10-28 09:52:56 -0700469 /* Save watchdog state, and turn it off. */
470 wtcon_save = readl(wdt_base + S3C2410_WTCON);
471 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200472
Russell King9480e302005-10-28 09:52:56 -0700473 /* Note that WTCNT doesn't need to be saved. */
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200474 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200475
476 return 0;
477}
478
Russell King3ae5eae2005-11-09 22:32:44 +0000479static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200480{
Russell King9480e302005-10-28 09:52:56 -0700481 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200482
Russell King9480e302005-10-28 09:52:56 -0700483 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
484 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
485 writel(wtcon_save, wdt_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200486
Joe Perches27c766a2012-02-15 15:06:19 -0800487 pr_info("watchdog %sabled\n",
488 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200489
490 return 0;
491}
492
493#else
494#define s3c2410wdt_suspend NULL
495#define s3c2410wdt_resume NULL
496#endif /* CONFIG_PM */
497
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530498#ifdef CONFIG_OF
499static const struct of_device_id s3c2410_wdt_match[] = {
500 { .compatible = "samsung,s3c2410-wdt" },
501 {},
502};
503MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
504#else
505#define s3c2410_wdt_match NULL
506#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200507
Russell King3ae5eae2005-11-09 22:32:44 +0000508static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .probe = s3c2410wdt_probe,
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000510 .remove = __devexit_p(s3c2410wdt_remove),
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200511 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200512 .suspend = s3c2410wdt_suspend,
513 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000514 .driver = {
515 .owner = THIS_MODULE,
516 .name = "s3c2410-wdt",
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530517 .of_match_table = s3c2410_wdt_match,
Russell King3ae5eae2005-11-09 22:32:44 +0000518 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519};
520
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static int __init watchdog_init(void)
523{
Joe Perches27c766a2012-02-15 15:06:19 -0800524 pr_info("S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
525
Russell King3ae5eae2005-11-09 22:32:44 +0000526 return platform_driver_register(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void __exit watchdog_exit(void)
530{
Russell King3ae5eae2005-11-09 22:32:44 +0000531 platform_driver_unregister(&s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534module_init(watchdog_init);
535module_exit(watchdog_exit);
536
Ben Dooksaf4bb822005-08-17 09:03:23 +0200537MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
538 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
540MODULE_LICENSE("GPL");
541MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700542MODULE_ALIAS("platform:s3c2410-wdt");