blob: 7d8fd041ee250d3759fc2b86519012a09ded76e2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000036#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010037#include <linux/uaccess.h>
38#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000039#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020041#include <linux/err.h>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000042#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Tomasz Figaa8f54012013-06-17 23:45:24 +090044#define S3C2410_WTCON 0x00
45#define S3C2410_WTDAT 0x04
46#define S3C2410_WTCNT 0x08
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tomasz Figaa8f54012013-06-17 23:45:24 +090048#define S3C2410_WTCON_RSTEN (1 << 0)
49#define S3C2410_WTCON_INTEN (1 << 2)
50#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Tomasz Figaa8f54012013-06-17 23:45:24 +090052#define S3C2410_WTCON_DIV16 (0 << 3)
53#define S3C2410_WTCON_DIV32 (1 << 3)
54#define S3C2410_WTCON_DIV64 (2 << 3)
55#define S3C2410_WTCON_DIV128 (3 << 3)
56
57#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
58#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
61#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
62
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010063static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010064static int tmr_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010066static int soft_noboot;
67static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69module_param(tmr_margin, int, 0);
70module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010071module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072module_param(soft_noboot, int, 0);
73module_param(debug, int, 0);
74
Randy Dunlap76550d32010-05-01 09:46:15 -070075MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010076 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
77MODULE_PARM_DESC(tmr_atboot,
78 "Watchdog is started at boot time if set to 1, default="
79 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
80MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
81 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000082MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -070083 "0 to reboot (default 0)");
84MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +053086struct s3c2410_wdt {
87 struct device *dev;
88 struct clk *clock;
89 void __iomem *reg_base;
90 unsigned int count;
91 spinlock_t lock;
92 unsigned long wtcon_save;
93 unsigned long wtdat_save;
94 struct watchdog_device wdt_device;
95 struct notifier_block freq_transition;
96};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* watchdog control routines */
99
Joe Perches27c766a2012-02-15 15:06:19 -0800100#define DBG(fmt, ...) \
101do { \
102 if (debug) \
103 pr_info(fmt, ##__VA_ARGS__); \
104} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* functions */
107
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530108static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
109{
110 return container_of(nb, struct s3c2410_wdt, freq_transition);
111}
112
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200113static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530115 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
116
117 spin_lock(&wdt->lock);
118 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
119 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200120
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530124static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100125{
126 unsigned long wtcon;
127
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530128 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530130 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200133static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100134{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530135 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
136
137 spin_lock(&wdt->lock);
138 __s3c2410wdt_stop(wdt);
139 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200140
141 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100142}
143
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200144static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530147 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530149 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100150
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530151 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530153 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
155
156 if (soft_noboot) {
157 wtcon |= S3C2410_WTCON_INTEN;
158 wtcon &= ~S3C2410_WTCON_RSTEN;
159 } else {
160 wtcon &= ~S3C2410_WTCON_INTEN;
161 wtcon |= S3C2410_WTCON_RSTEN;
162 }
163
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530164 DBG("%s: count=0x%08x, wtcon=%08lx\n",
165 __func__, wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530167 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
168 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
169 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
170 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200171
172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530175static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000176{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530177 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000178}
179
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200180static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530182 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
183 unsigned long freq = clk_get_rate(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned int count;
185 unsigned int divisor = 1;
186 unsigned long wtcon;
187
188 if (timeout < 1)
189 return -EINVAL;
190
191 freq /= 128;
192 count = timeout * freq;
193
Ben Dookse02f8382009-10-30 00:30:25 +0000194 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800195 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* if the count is bigger than the watchdog register,
198 then work out what we need to do (and if) we can
199 actually make this value
200 */
201
202 if (count >= 0x10000) {
203 for (divisor = 1; divisor <= 0x100; divisor++) {
204 if ((count / divisor) < 0x10000)
205 break;
206 }
207
208 if ((count / divisor) >= 0x10000) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530209 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EINVAL;
211 }
212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800215 __func__, timeout, divisor, count, count/divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 count /= divisor;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530218 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530221 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
223 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
224
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530225 writel(count, wdt->reg_base + S3C2410_WTDAT);
226 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Hans de Goede5f2430f2012-05-11 12:00:27 +0200228 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
232
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000233#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Cox41dc8b72008-08-04 17:54:46 +0100235static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .options = OPTIONS,
237 .firmware_version = 0,
238 .identity = "S3C2410 Watchdog",
239};
240
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200241static struct watchdog_ops s3c2410wdt_ops = {
242 .owner = THIS_MODULE,
243 .start = s3c2410wdt_start,
244 .stop = s3c2410wdt_stop,
245 .ping = s3c2410wdt_keepalive,
246 .set_timeout = s3c2410wdt_set_heartbeat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200249static struct watchdog_device s3c2410_wdd = {
250 .info = &s3c2410_wdt_ident,
251 .ops = &s3c2410wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100252 .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* interrupt handler code */
256
David Howells7d12e782006-10-05 14:55:46 +0100257static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530259 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530261 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
262
263 s3c2410wdt_keepalive(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return IRQ_HANDLED;
265}
Ben Dookse02f8382009-10-30 00:30:25 +0000266
Ben Dookse02f8382009-10-30 00:30:25 +0000267#ifdef CONFIG_CPU_FREQ
268
269static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
270 unsigned long val, void *data)
271{
272 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530273 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000274
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530275 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000276 goto done;
277
278 if (val == CPUFREQ_PRECHANGE) {
279 /* To ensure that over the change we don't cause the
280 * watchdog to trigger, we perform an keep-alive if
281 * the watchdog is running.
282 */
283
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530284 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000285 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530286 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000287
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530288 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
289 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000290
291 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530292 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000293 else
294 goto err;
295 }
296
297done:
298 return 0;
299
300 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530301 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
302 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000303 return ret;
304}
305
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530306static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000307{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530308 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
309
310 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000311 CPUFREQ_TRANSITION_NOTIFIER);
312}
313
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530314static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000315{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530316 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
317
318 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000319 CPUFREQ_TRANSITION_NOTIFIER);
320}
321
322#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530323
324static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000325{
326 return 0;
327}
328
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530329static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000330{
331}
332#endif
333
Bill Pemberton2d991a12012-11-19 13:21:41 -0500334static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100336 struct device *dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530337 struct s3c2410_wdt *wdt;
338 struct resource *wdt_mem;
339 struct resource *wdt_irq;
Ben Dooks46b814d2007-06-14 12:08:54 +0100340 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int started = 0;
342 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800344 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Ben Dookse8ef92b2007-06-14 12:08:55 +0100346 dev = &pdev->dev;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100347
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530348 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
349 if (!wdt)
350 return -ENOMEM;
351
352 wdt->dev = &pdev->dev;
353 spin_lock_init(&wdt->lock);
354 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900356 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
357 if (wdt_irq == NULL) {
358 dev_err(dev, "no irq resource specified\n");
359 ret = -ENOENT;
360 goto err;
361 }
362
363 /* get the memory region for the watchdog timer */
Julia Lawallbd5cc112013-08-14 11:11:24 +0200364 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530365 wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
366 if (IS_ERR(wdt->reg_base)) {
367 ret = PTR_ERR(wdt->reg_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900368 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530371 DBG("probe: mapped reg_base=%p\n", wdt->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530373 wdt->clock = devm_clk_get(dev, "watchdog");
374 if (IS_ERR(wdt->clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100375 dev_err(dev, "failed to find watchdog clock source\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530376 ret = PTR_ERR(wdt->clock);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900377 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530380 clk_prepare_enable(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530382 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900383 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900384 dev_err(dev, "failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000385 goto err_clk;
386 }
387
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530388 watchdog_set_drvdata(&wdt->wdt_device, wdt);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* see if we can actually set the requested timer margin, and if
391 * not, try the default value */
392
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530393 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, &pdev->dev);
394 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
395 wdt->wdt_device.timeout);
396 if (ret) {
397 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
Alan Cox41dc8b72008-08-04 17:54:46 +0100398 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Alan Cox41dc8b72008-08-04 17:54:46 +0100400 if (started == 0)
401 dev_info(dev,
402 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100404 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000405 dev_info(dev, "default timer value is out of range, "
406 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900409 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
410 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900411 if (ret != 0) {
412 dev_err(dev, "failed to install irq (%d)\n", ret);
413 goto err_cpufreq;
414 }
415
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530416 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100417
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530418 ret = watchdog_register_device(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200420 dev_err(dev, "cannot register watchdog (%d)\n", ret);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900421 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100425 dev_info(dev, "starting watchdog timer\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530426 s3c2410wdt_start(&wdt->wdt_device);
Ben Dooks655516c2006-04-19 23:02:56 +0100427 } else if (!tmr_atboot) {
428 /* if we're not enabling the watchdog, then ensure it is
429 * disabled if it has been left running from the bootloader
430 * or other source */
431
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530432 s3c2410wdt_stop(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530435 platform_set_drvdata(pdev, wdt);
436
Ben Dooks46b814d2007-06-14 12:08:54 +0100437 /* print out a statement of readiness */
438
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530439 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100440
Ben Dookse8ef92b2007-06-14 12:08:55 +0100441 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100442 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400443 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
444 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000447
Ben Dookse02f8382009-10-30 00:30:25 +0000448 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530449 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000450
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000451 err_clk:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530452 clk_disable_unprepare(wdt->clock);
453 wdt->clock = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000454
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900455 err:
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Bill Pemberton4b12b892012-11-19 13:26:24 -0500459static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530461 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000462
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530463 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000464
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530465 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530467 clk_disable_unprepare(wdt->clock);
468 wdt->clock = NULL;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471}
472
Russell King3ae5eae2005-11-09 22:32:44 +0000473static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200474{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530475 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
476
477 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200478}
479
Jingoo Han0183984c2013-03-14 10:31:21 +0900480#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200481
Jingoo Han0183984c2013-03-14 10:31:21 +0900482static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200483{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530484 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
485
Russell King9480e302005-10-28 09:52:56 -0700486 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530487 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
488 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200489
Russell King9480e302005-10-28 09:52:56 -0700490 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530491 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200492
493 return 0;
494}
495
Jingoo Han0183984c2013-03-14 10:31:21 +0900496static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200497{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530498 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200499
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530500 /* Restore watchdog state. */
501 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
502 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
503 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200504
Jingoo Han0183984c2013-03-14 10:31:21 +0900505 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530506 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200507
508 return 0;
509}
Jingoo Han0183984c2013-03-14 10:31:21 +0900510#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200511
Jingoo Han0183984c2013-03-14 10:31:21 +0900512static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
513 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200514
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530515#ifdef CONFIG_OF
516static const struct of_device_id s3c2410_wdt_match[] = {
517 { .compatible = "samsung,s3c2410-wdt" },
518 {},
519};
520MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
Thomas Abraham9487a9c2011-06-22 15:24:32 +0530521#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200522
Russell King3ae5eae2005-11-09 22:32:44 +0000523static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500525 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200526 .shutdown = s3c2410wdt_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000527 .driver = {
528 .owner = THIS_MODULE,
529 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900530 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000531 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000532 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533};
534
Sachin Kamat6b761b22012-07-12 17:17:40 +0530535module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
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");
Kay Sieversf37d1932008-04-10 21:29:23 -0700541MODULE_ALIAS("platform:s3c2410-wdt");