blob: 6a22cf5d35bdde323a94f2aebc20fc3dc4d4264a [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
Tomasz Figaa8f54012013-06-17 23:45:24 +090045#define S3C2410_WTCON 0x00
46#define S3C2410_WTDAT 0x04
47#define S3C2410_WTCNT 0x08
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Tomasz Figaa8f54012013-06-17 23:45:24 +090049#define S3C2410_WTCON_RSTEN (1 << 0)
50#define S3C2410_WTCON_INTEN (1 << 2)
51#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Tomasz Figaa8f54012013-06-17 23:45:24 +090053#define S3C2410_WTCON_DIV16 (0 << 3)
54#define S3C2410_WTCON_DIV32 (1 << 3)
55#define S3C2410_WTCON_DIV64 (2 << 3)
56#define S3C2410_WTCON_DIV128 (3 << 3)
57
58#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
59#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
62#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
63
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010064static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010065static int tmr_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static 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);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010072module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073module_param(soft_noboot, int, 0);
74module_param(debug, int, 0);
75
Randy Dunlap76550d32010-05-01 09:46:15 -070076MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010077 __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) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000083MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -070084 "0 to reboot (default 0)");
85MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Ben Dookse8ef92b2007-06-14 12:08:55 +010087static struct device *wdt_dev; /* platform device attached to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct resource *wdt_mem;
89static struct resource *wdt_irq;
90static struct clk *wdt_clock;
91static void __iomem *wdt_base;
92static unsigned int wdt_count;
Alan Cox41dc8b72008-08-04 17:54:46 +010093static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* watchdog control routines */
96
Joe Perches27c766a2012-02-15 15:06:19 -080097#define DBG(fmt, ...) \
98do { \
99 if (debug) \
100 pr_info(fmt, ##__VA_ARGS__); \
101} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* functions */
104
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200105static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Alan Cox41dc8b72008-08-04 17:54:46 +0100107 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 writel(wdt_count, wdt_base + S3C2410_WTCNT);
Alan Cox41dc8b72008-08-04 17:54:46 +0100109 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200110
111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Alan Cox41dc8b72008-08-04 17:54:46 +0100114static void __s3c2410wdt_stop(void)
115{
116 unsigned long wtcon;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 wtcon = readl(wdt_base + S3C2410_WTCON);
119 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
120 writel(wtcon, wdt_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200123static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100124{
125 spin_lock(&wdt_lock);
126 __s3c2410wdt_stop();
127 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200128
129 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100130}
131
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200132static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 unsigned long wtcon;
135
Alan Cox41dc8b72008-08-04 17:54:46 +0100136 spin_lock(&wdt_lock);
137
138 __s3c2410wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 wtcon = readl(wdt_base + S3C2410_WTCON);
141 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
142
143 if (soft_noboot) {
144 wtcon |= S3C2410_WTCON_INTEN;
145 wtcon &= ~S3C2410_WTCON_RSTEN;
146 } else {
147 wtcon &= ~S3C2410_WTCON_INTEN;
148 wtcon |= S3C2410_WTCON_RSTEN;
149 }
150
151 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800152 __func__, wdt_count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 writel(wdt_count, wdt_base + S3C2410_WTDAT);
155 writel(wdt_count, wdt_base + S3C2410_WTCNT);
156 writel(wtcon, wdt_base + S3C2410_WTCON);
Alan Cox41dc8b72008-08-04 17:54:46 +0100157 spin_unlock(&wdt_lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200158
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Ben Dookse02f8382009-10-30 00:30:25 +0000162static inline int s3c2410wdt_is_running(void)
163{
164 return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
165}
166
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200167static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Ben Dookse02f8382009-10-30 00:30:25 +0000169 unsigned long freq = clk_get_rate(wdt_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int count;
171 unsigned int divisor = 1;
172 unsigned long wtcon;
173
174 if (timeout < 1)
175 return -EINVAL;
176
177 freq /= 128;
178 count = timeout * freq;
179
Ben Dookse02f8382009-10-30 00:30:25 +0000180 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800181 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* if the count is bigger than the watchdog register,
184 then work out what we need to do (and if) we can
185 actually make this value
186 */
187
188 if (count >= 0x10000) {
189 for (divisor = 1; divisor <= 0x100; divisor++) {
190 if ((count / divisor) < 0x10000)
191 break;
192 }
193
194 if ((count / divisor) >= 0x10000) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100195 dev_err(wdt_dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197 }
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800201 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 count /= divisor;
204 wdt_count = count;
205
206 /* update the pre-scaler */
207 wtcon = readl(wdt_base + S3C2410_WTCON);
208 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
209 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
210
211 writel(count, wdt_base + S3C2410_WTDAT);
212 writel(wtcon, wdt_base + S3C2410_WTCON);
213
Hans de Goede5f2430f2012-05-11 12:00:27 +0200214 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000219#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Alan Cox41dc8b72008-08-04 17:54:46 +0100221static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .options = OPTIONS,
223 .firmware_version = 0,
224 .identity = "S3C2410 Watchdog",
225};
226
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200227static struct watchdog_ops s3c2410wdt_ops = {
228 .owner = THIS_MODULE,
229 .start = s3c2410wdt_start,
230 .stop = s3c2410wdt_stop,
231 .ping = s3c2410wdt_keepalive,
232 .set_timeout = s3c2410wdt_set_heartbeat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200235static struct watchdog_device s3c2410_wdd = {
236 .info = &s3c2410_wdt_ident,
237 .ops = &s3c2410wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100238 .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* interrupt handler code */
242
David Howells7d12e782006-10-05 14:55:46 +0100243static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100245 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200247 s3c2410wdt_keepalive(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return IRQ_HANDLED;
249}
Ben Dookse02f8382009-10-30 00:30:25 +0000250
251
252#ifdef CONFIG_CPU_FREQ
253
254static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
255 unsigned long val, void *data)
256{
257 int ret;
258
259 if (!s3c2410wdt_is_running())
260 goto done;
261
262 if (val == CPUFREQ_PRECHANGE) {
263 /* To ensure that over the change we don't cause the
264 * watchdog to trigger, we perform an keep-alive if
265 * the watchdog is running.
266 */
267
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200268 s3c2410wdt_keepalive(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000269 } else if (val == CPUFREQ_POSTCHANGE) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200270 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000271
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200272 ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000273
274 if (ret >= 0)
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200275 s3c2410wdt_start(&s3c2410_wdd);
Ben Dookse02f8382009-10-30 00:30:25 +0000276 else
277 goto err;
278 }
279
280done:
281 return 0;
282
283 err:
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200284 dev_err(wdt_dev, "cannot set new value for timeout %d\n",
285 s3c2410_wdd.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000286 return ret;
287}
288
289static struct notifier_block s3c2410wdt_cpufreq_transition_nb = {
290 .notifier_call = s3c2410wdt_cpufreq_transition,
291};
292
293static inline int s3c2410wdt_cpufreq_register(void)
294{
295 return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb,
296 CPUFREQ_TRANSITION_NOTIFIER);
297}
298
299static inline void s3c2410wdt_cpufreq_deregister(void)
300{
301 cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb,
302 CPUFREQ_TRANSITION_NOTIFIER);
303}
304
305#else
306static inline int s3c2410wdt_cpufreq_register(void)
307{
308 return 0;
309}
310
311static inline void s3c2410wdt_cpufreq_deregister(void)
312{
313}
314#endif
315
Bill Pemberton2d991a12012-11-19 13:21:41 -0500316static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100318 struct device *dev;
Ben Dooks46b814d2007-06-14 12:08:54 +0100319 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int started = 0;
321 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800323 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Ben Dookse8ef92b2007-06-14 12:08:55 +0100325 dev = &pdev->dev;
326 wdt_dev = &pdev->dev;
327
Julia Lawallf72401e2011-02-26 17:34:38 +0100328 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
329 if (wdt_mem == NULL) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100330 dev_err(dev, "no memory resource specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -ENOENT;
332 }
333
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900334 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
335 if (wdt_irq == NULL) {
336 dev_err(dev, "no irq resource specified\n");
337 ret = -ENOENT;
338 goto err;
339 }
340
341 /* get the memory region for the watchdog timer */
Sachin Kamat6330c702013-03-04 10:36:41 +0530342 wdt_base = devm_ioremap_resource(dev, wdt_mem);
343 if (IS_ERR(wdt_base)) {
344 ret = PTR_ERR(wdt_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900345 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 DBG("probe: mapped wdt_base=%p\n", wdt_base);
349
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900350 wdt_clock = devm_clk_get(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);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900354 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Thomas Abraham50d854c82012-10-03 07:32:40 +0900357 clk_prepare_enable(wdt_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900359 ret = s3c2410wdt_cpufreq_register();
360 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900361 dev_err(dev, "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
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100368 watchdog_init_timeout(&s3c2410_wdd, tmr_margin, &pdev->dev);
369 if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout)) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200370 started = s3c2410wdt_set_heartbeat(&s3c2410_wdd,
Alan Cox41dc8b72008-08-04 17:54:46 +0100371 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Alan Cox41dc8b72008-08-04 17:54:46 +0100373 if (started == 0)
374 dev_info(dev,
375 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100377 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000378 dev_info(dev, "default timer value is out of range, "
379 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900382 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
383 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900384 if (ret != 0) {
385 dev_err(dev, "failed to install irq (%d)\n", ret);
386 goto err_cpufreq;
387 }
388
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100389 watchdog_set_nowayout(&s3c2410_wdd, nowayout);
390
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200391 ret = watchdog_register_device(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200393 dev_err(dev, "cannot register watchdog (%d)\n", ret);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900394 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100398 dev_info(dev, "starting watchdog timer\n");
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200399 s3c2410wdt_start(&s3c2410_wdd);
Ben Dooks655516c2006-04-19 23:02:56 +0100400 } else if (!tmr_atboot) {
401 /* if we're not enabling the watchdog, then ensure it is
402 * disabled if it has been left running from the bootloader
403 * or other source */
404
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200405 s3c2410wdt_stop(&s3c2410_wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Ben Dooks46b814d2007-06-14 12:08:54 +0100408 /* print out a statement of readiness */
409
410 wtcon = readl(wdt_base + S3C2410_WTCON);
411
Ben Dookse8ef92b2007-06-14 12:08:55 +0100412 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100413 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400414 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
415 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000418
Ben Dookse02f8382009-10-30 00:30:25 +0000419 err_cpufreq:
420 s3c2410wdt_cpufreq_deregister();
421
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000422 err_clk:
Thomas Abraham50d854c82012-10-03 07:32:40 +0900423 clk_disable_unprepare(wdt_clock);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900424 wdt_clock = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000425
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900426 err:
427 wdt_irq = NULL;
428 wdt_mem = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Bill Pemberton4b12b892012-11-19 13:26:24 -0500432static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200434 watchdog_unregister_device(&s3c2410_wdd);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000435
Ben Dookse02f8382009-10-30 00:30:25 +0000436 s3c2410wdt_cpufreq_deregister();
437
Thomas Abraham50d854c82012-10-03 07:32:40 +0900438 clk_disable_unprepare(wdt_clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000439 wdt_clock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900441 wdt_irq = NULL;
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000442 wdt_mem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
Russell King3ae5eae2005-11-09 22:32:44 +0000446static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200447{
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200448 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200449}
450
Jingoo Han0183984c2013-03-14 10:31:21 +0900451#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200452
453static unsigned long wtcon_save;
454static unsigned long wtdat_save;
455
Jingoo Han0183984c2013-03-14 10:31:21 +0900456static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200457{
Russell King9480e302005-10-28 09:52:56 -0700458 /* Save watchdog state, and turn it off. */
459 wtcon_save = readl(wdt_base + S3C2410_WTCON);
460 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200461
Russell King9480e302005-10-28 09:52:56 -0700462 /* Note that WTCNT doesn't need to be saved. */
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200463 s3c2410wdt_stop(&s3c2410_wdd);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200464
465 return 0;
466}
467
Jingoo Han0183984c2013-03-14 10:31:21 +0900468static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200469{
Russell King9480e302005-10-28 09:52:56 -0700470 /* Restore watchdog state. */
Ben Dooksaf4bb822005-08-17 09:03:23 +0200471
Russell King9480e302005-10-28 09:52:56 -0700472 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
473 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
474 writel(wtcon_save, wdt_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200475
Jingoo Han0183984c2013-03-14 10:31:21 +0900476 dev_info(dev, "watchdog %sabled\n",
Joe Perches27c766a2012-02-15 15:06:19 -0800477 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200478
479 return 0;
480}
Jingoo Han0183984c2013-03-14 10:31:21 +0900481#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200482
Jingoo Han0183984c2013-03-14 10:31:21 +0900483static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
484 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200485
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530486#ifdef CONFIG_OF
487static const struct of_device_id s3c2410_wdt_match[] = {
488 { .compatible = "samsung,s3c2410-wdt" },
489 {},
490};
491MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530492#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200493
Russell King3ae5eae2005-11-09 22:32:44 +0000494static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500496 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200497 .shutdown = s3c2410wdt_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000498 .driver = {
499 .owner = THIS_MODULE,
500 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900501 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000502 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000503 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504};
505
Sachin Kamat6b761b22012-07-12 17:17:40 +0530506module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ben Dooksaf4bb822005-08-17 09:03:23 +0200508MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
509 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
511MODULE_LICENSE("GPL");
512MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700513MODULE_ALIAS("platform:s3c2410-wdt");