blob: 9245b4d23bfe79ead0116c440fb6f4803fa23221 [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>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000043#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Russell Kinga09e64f2008-08-05 16:14:15 +010045#include <mach/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ben Dooksb4307082007-07-24 13:28:01 +010047#undef S3C_VA_WATCHDOG
48#define S3C_VA_WATCHDOG (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ben Dooks180ee702008-10-30 10:14:32 +000050#include <plat/regs-watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
53#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
54
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010055static bool nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
57static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010058static int soft_noboot;
59static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61module_param(tmr_margin, int, 0);
62module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010063module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param(soft_noboot, int, 0);
65module_param(debug, int, 0);
66
Randy Dunlap76550d32010-05-01 09:46:15 -070067MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010068 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
69MODULE_PARM_DESC(tmr_atboot,
70 "Watchdog is started at boot time if set to 1, default="
71 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
72MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
73 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000074MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -070075 "0 to reboot (default 0)");
76MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Ben Dookse8ef92b2007-06-14 12:08:55 +010078static struct device *wdt_dev; /* platform device attached to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static struct resource *wdt_mem;
80static struct resource *wdt_irq;
81static struct clk *wdt_clock;
82static void __iomem *wdt_base;
83static unsigned int wdt_count;
Alan Cox41dc8b72008-08-04 17:54:46 +010084static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* watchdog control routines */
87
Joe Perches27c766a2012-02-15 15:06:19 -080088#define DBG(fmt, ...) \
89do { \
90 if (debug) \
91 pr_info(fmt, ##__VA_ARGS__); \
92} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* functions */
95
Wolfram Sang25dc46e2011-09-26 15:40:14 +020096static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Alan Cox41dc8b72008-08-04 17:54:46 +010098 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 writel(wdt_count, wdt_base + S3C2410_WTCNT);
Alan Cox41dc8b72008-08-04 17:54:46 +0100100 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200101
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Alan Cox41dc8b72008-08-04 17:54:46 +0100105static void __s3c2410wdt_stop(void)
106{
107 unsigned long wtcon;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 wtcon = readl(wdt_base + S3C2410_WTCON);
110 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
111 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200114static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100115{
116 spin_lock(&wdt_lock);
117 __s3c2410wdt_stop();
118 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200119
120 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100121}
122
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200123static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned long wtcon;
126
Alan Cox41dc8b72008-08-04 17:54:46 +0100127 spin_lock(&wdt_lock);
128
129 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 wtcon = readl(wdt_base + S3C2410_WTCON);
132 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
133
134 if (soft_noboot) {
135 wtcon |= S3C2410_WTCON_INTEN;
136 wtcon &= ~S3C2410_WTCON_RSTEN;
137 } else {
138 wtcon &= ~S3C2410_WTCON_INTEN;
139 wtcon |= S3C2410_WTCON_RSTEN;
140 }
141
142 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800143 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 writel(wdt_count, wdt_base + S3C2410_WTDAT);
146 writel(wdt_count, wdt_base + S3C2410_WTCNT);
147 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100148 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200149
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Ben Dookse02f8382009-10-30 00:30:25 +0000153static inline int s3c2410wdt_is_running(void)
154{
155 return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
156}
157
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200158static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Ben Dookse02f8382009-10-30 00:30:25 +0000160 unsigned long freq = clk_get_rate(wdt_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int count;
162 unsigned int divisor = 1;
163 unsigned long wtcon;
164
165 if (timeout < 1)
166 return -EINVAL;
167
168 freq /= 128;
169 count = timeout * freq;
170
Ben Dookse02f8382009-10-30 00:30:25 +0000171 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800172 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* if the count is bigger than the watchdog register,
175 then work out what we need to do (and if) we can
176 actually make this value
177 */
178
179 if (count >= 0x10000) {
180 for (divisor = 1; divisor <= 0x100; divisor++) {
181 if ((count / divisor) < 0x10000)
182 break;
183 }
184
185 if ((count / divisor) >= 0x10000) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100186 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188 }
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800192 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 count /= divisor;
195 wdt_count = count;
196
197 /* update the pre-scaler */
198 wtcon = readl(wdt_base + S3C2410_WTCON);
199 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
200 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
201
202 writel(count, wdt_base + S3C2410_WTDAT);
203 writel(wtcon, wdt_base + S3C2410_WTCON);
204
Hans de Goede5f2430f2012-05-11 12:00:27 +0200205 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
209
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000210#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Alan Cox41dc8b72008-08-04 17:54:46 +0100212static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .options = OPTIONS,
214 .firmware_version = 0,
215 .identity = "S3C2410 Watchdog",
216};
217
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200218static struct watchdog_ops s3c2410wdt_ops = {
219 .owner = THIS_MODULE,
220 .start = s3c2410wdt_start,
221 .stop = s3c2410wdt_stop,
222 .ping = s3c2410wdt_keepalive,
223 .set_timeout = s3c2410wdt_set_heartbeat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200226static struct watchdog_device s3c2410_wdd = {
227 .info = &s3c2410_wdt_ident,
228 .ops = &s3c2410wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* interrupt handler code */
232
David Howells7d12e782006-10-05 14:55:46 +0100233static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100235 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200237 s3c2410wdt_keepalive(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return IRQ_HANDLED;
239}
Ben Dookse02f8382009-10-30 00:30:25 +0000240
241
242#ifdef CONFIG_CPU_FREQ
243
244static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
245 unsigned long val, void *data)
246{
247 int ret;
248
249 if (!s3c2410wdt_is_running())
250 goto done;
251
252 if (val == CPUFREQ_PRECHANGE) {
253 /* To ensure that over the change we don't cause the
254 * watchdog to trigger, we perform an keep-alive if
255 * the watchdog is running.
256 */
257
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200258 s3c2410wdt_keepalive(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000259 } else if (val == CPUFREQ_POSTCHANGE) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200260 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000261
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200262 ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000263
264 if (ret >= 0)
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200265 s3c2410wdt_start(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000266 else
267 goto err;
268 }
269
270done:
271 return 0;
272
273 err:
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200274 dev_err(wdt_dev, "cannot set new value for timeout %d\n",
275 s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000276 return ret;
277}
278
279static struct notifier_block s3c2410wdt_cpufreq_transition_nb = {
280 .notifier_call = s3c2410wdt_cpufreq_transition,
281};
282
283static inline int s3c2410wdt_cpufreq_register(void)
284{
285 return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb,
286 CPUFREQ_TRANSITION_NOTIFIER);
287}
288
289static inline void s3c2410wdt_cpufreq_deregister(void)
290{
291 cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb,
292 CPUFREQ_TRANSITION_NOTIFIER);
293}
294
295#else
296static inline int s3c2410wdt_cpufreq_register(void)
297{
298 return 0;
299}
300
301static inline void s3c2410wdt_cpufreq_deregister(void)
302{
303}
304#endif
305
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000306static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100308 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100309 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int started = 0;
311 int ret;
312 int size;
313
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800314 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Ben Dookse8ef92b2007-06-14 12:08:55 +0100316 dev = &pdev->dev;
317 wdt_dev = &pdev->dev;
318
Julia Lawallf72401e2011-02-26 17:34:38 +0100319 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
320 if (wdt_mem == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100321 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -ENOENT;
323 }
324
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900325 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
326 if (wdt_irq == NULL) {
327 dev_err(dev, "no irq resource specified\n");
328 ret = -ENOENT;
329 goto err;
330 }
331
332 /* get the memory region for the watchdog timer */
333
Julia Lawallf72401e2011-02-26 17:34:38 +0100334 size = resource_size(wdt_mem);
335 if (!request_mem_region(wdt_mem->start, size, pdev->name)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100336 dev_err(dev, "failed to get memory region\n");
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900337 ret = -EBUSY;
338 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Julia Lawallf72401e2011-02-26 17:34:38 +0100341 wdt_base = ioremap(wdt_mem->start, size);
Ben Dooksb4253f82008-06-22 22:36:49 +0100342 if (wdt_base == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100343 dev_err(dev, "failed to ioremap() region\n");
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000344 ret = -EINVAL;
345 goto err_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 DBG("probe: mapped wdt_base=%p\n", wdt_base);
349
Russell King3ae5eae2005-11-09 22:32:44 +0000350 wdt_clock = clk_get(&pdev->dev, "watchdog");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900351 if (IS_ERR(wdt_clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100352 dev_err(dev, "failed to find watchdog clock source\n");
Akinobu Mita9cd44612006-12-19 17:51:44 +0900353 ret = PTR_ERR(wdt_clock);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900354 goto err_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 clk_enable(wdt_clock);
358
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900359 ret = s3c2410wdt_cpufreq_register();
360 if (ret < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800361 pr_err("failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000362 goto err_clk;
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* see if we can actually set the requested timer margin, and if
366 * not, try the default value */
367
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200368 if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, tmr_margin)) {
369 started = s3c2410wdt_set_heartbeat(&s3c2410_wdd,
Alan Cox41dc8b72008-08-04 17:54:46 +0100370 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alan Cox41dc8b72008-08-04 17:54:46 +0100372 if (started == 0)
373 dev_info(dev,
374 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100376 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000377 dev_info(dev, "default timer value is out of range, "
378 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900381 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
382 if (ret != 0) {
383 dev_err(dev, "failed to install irq (%d)\n", ret);
384 goto err_cpufreq;
385 }
386
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100387 watchdog_set_nowayout(&s3c2410_wdd, nowayout);
388
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200389 ret = watchdog_register_device(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200391 dev_err(dev, "cannot register watchdog (%d)\n", ret);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900392 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100396 dev_info(dev, "starting watchdog timer\n");
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200397 s3c2410wdt_start(&s3c2410_wdd);
Ben Dooks655516c2006-04-19 23:02:56 +0100398 } else if (!tmr_atboot) {
399 /* if we're not enabling the watchdog, then ensure it is
400 * disabled if it has been left running from the bootloader
401 * or other source */
402
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200403 s3c2410wdt_stop(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Ben Dooks46b814d2007-06-14 12:08:54 +0100406 /* print out a statement of readiness */
407
408 wtcon = readl(wdt_base + S3C2410_WTCON);
409
Ben Dookse8ef92b2007-06-14 12:08:55 +0100410 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100411 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400412 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
413 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000416
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900417 err_irq:
418 free_irq(wdt_irq->start, pdev);
419
Ben Dookse02f8382009-10-30 00:30:25 +0000420 err_cpufreq:
421 s3c2410wdt_cpufreq_deregister();
422
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000423 err_clk:
424 clk_disable(wdt_clock);
425 clk_put(wdt_clock);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900426 wdt_clock = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000427
428 err_map:
429 iounmap(wdt_base);
430
431 err_req:
Julia Lawallf72401e2011-02-26 17:34:38 +0100432 release_mem_region(wdt_mem->start, size);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000433
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900434 err:
435 wdt_irq = NULL;
436 wdt_mem = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000440static int __devexit s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200442 watchdog_unregister_device(&s3c2410_wdd);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000443
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900444 free_irq(wdt_irq->start, dev);
445
Ben Dookse02f8382009-10-30 00:30:25 +0000446 s3c2410wdt_cpufreq_deregister();
447
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000448 clk_disable(wdt_clock);
449 clk_put(wdt_clock);
450 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Amol Lade34477e2006-10-06 13:41:12 -0700452 iounmap(wdt_base);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000453
Julia Lawallf72401e2011-02-26 17:34:38 +0100454 release_mem_region(wdt_mem->start, resource_size(wdt_mem));
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900455 wdt_irq = NULL;
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000456 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Russell King3ae5eae2005-11-09 22:32:44 +0000460static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200461{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200462 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200463}
464
Ben Dooksaf4bb822005-08-17 09:03:23 +0200465#ifdef CONFIG_PM
466
467static unsigned long wtcon_save;
468static unsigned long wtdat_save;
469
Russell King3ae5eae2005-11-09 22:32:44 +0000470static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200471{
Russell King9480e302005-10-28 09:52:56 -0700472 /* Save watchdog state, and turn it off. */
473 wtcon_save = readl(wdt_base + S3C2410_WTCON);
474 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200475
Russell King9480e302005-10-28 09:52:56 -0700476 /* Note that WTCNT doesn't need to be saved. */
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200477 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200478
479 return 0;
480}
481
Russell King3ae5eae2005-11-09 22:32:44 +0000482static int s3c2410wdt_resume(struct platform_device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200483{
Russell King9480e302005-10-28 09:52:56 -0700484 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200485
Russell King9480e302005-10-28 09:52:56 -0700486 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
487 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
488 writel(wtcon_save, wdt_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200489
Joe Perches27c766a2012-02-15 15:06:19 -0800490 pr_info("watchdog %sabled\n",
491 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200492
493 return 0;
494}
495
496#else
497#define s3c2410wdt_suspend NULL
498#define s3c2410wdt_resume NULL
499#endif /* CONFIG_PM */
500
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530501#ifdef CONFIG_OF
502static const struct of_device_id s3c2410_wdt_match[] = {
503 { .compatible = "samsung,s3c2410-wdt" },
504 {},
505};
506MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530507#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200508
Russell King3ae5eae2005-11-09 22:32:44 +0000509static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .probe = s3c2410wdt_probe,
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000511 .remove = __devexit_p(s3c2410wdt_remove),
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200512 .shutdown = s3c2410wdt_shutdown,
Ben Dooksaf4bb822005-08-17 09:03:23 +0200513 .suspend = s3c2410wdt_suspend,
514 .resume = s3c2410wdt_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000515 .driver = {
516 .owner = THIS_MODULE,
517 .name = "s3c2410-wdt",
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000518 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000519 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520};
521
Sachin Kamat6b761b22012-07-12 17:17:40 +0530522module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Ben Dooksaf4bb822005-08-17 09:03:23 +0200524MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
525 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
527MODULE_LICENSE("GPL");
528MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700529MODULE_ALIAS("platform:s3c2410-wdt");