blob: c5a567a73f590a0b27c890638e329ea474b9b71c [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>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000035#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010036#include <linux/uaccess.h>
37#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000038#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020040#include <linux/err.h>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000041#include <linux/of.h>
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053042#include <linux/mfd/syscon.h>
43#include <linux/regmap.h>
Heiko Stuebnerf286e132014-08-19 17:45:36 -070044#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Tomasz Figaa8f54012013-06-17 23:45:24 +090046#define S3C2410_WTCON 0x00
47#define S3C2410_WTDAT 0x04
48#define S3C2410_WTCNT 0x08
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +000049#define S3C2410_WTCLRINT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030051#define S3C2410_WTCNT_MAXCNT 0xffff
52
Tomasz Figaa8f54012013-06-17 23:45:24 +090053#define S3C2410_WTCON_RSTEN (1 << 0)
54#define S3C2410_WTCON_INTEN (1 << 2)
55#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tomasz Figaa8f54012013-06-17 23:45:24 +090057#define S3C2410_WTCON_DIV16 (0 << 3)
58#define S3C2410_WTCON_DIV32 (1 << 3)
59#define S3C2410_WTCON_DIV64 (2 << 3)
60#define S3C2410_WTCON_DIV128 (3 << 3)
61
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030062#define S3C2410_WTCON_MAXDIV 0x80
63
Tomasz Figaa8f54012013-06-17 23:45:24 +090064#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
65#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030066#define S3C2410_WTCON_PRESCALE_MAX 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
69#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
70
Doug Andersoncffc9a62013-12-06 13:08:07 -080071#define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053072#define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
73#define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
74#define QUIRK_HAS_PMU_CONFIG (1 << 0)
Doug Andersoncffc9a62013-12-06 13:08:07 -080075#define QUIRK_HAS_RST_STAT (1 << 1)
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +000076#define QUIRK_HAS_WTCLRINT_REG (1 << 2)
Doug Andersoncffc9a62013-12-06 13:08:07 -080077
78/* These quirks require that we have a PMU register map */
79#define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
80 QUIRK_HAS_RST_STAT)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053081
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010082static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010083static int tmr_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010085static int soft_noboot;
86static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88module_param(tmr_margin, int, 0);
89module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010090module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091module_param(soft_noboot, int, 0);
92module_param(debug, int, 0);
93
Randy Dunlap76550d32010-05-01 09:46:15 -070094MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010095 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
96MODULE_PARM_DESC(tmr_atboot,
97 "Watchdog is started at boot time if set to 1, default="
98 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
99MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000101MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -0700102 "0 to reboot (default 0)");
103MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530105/**
106 * struct s3c2410_wdt_variant - Per-variant config data
107 *
108 * @disable_reg: Offset in pmureg for the register that disables the watchdog
109 * timer reset functionality.
110 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
111 * timer reset functionality.
112 * @mask_bit: Bit number for the watchdog timer in the disable register and the
113 * mask reset register.
Doug Andersoncffc9a62013-12-06 13:08:07 -0800114 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
115 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
116 * reset.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530117 * @quirks: A bitfield of quirks.
118 */
119
120struct s3c2410_wdt_variant {
121 int disable_reg;
122 int mask_reset_reg;
123 int mask_bit;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800124 int rst_stat_reg;
125 int rst_stat_bit;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530126 u32 quirks;
127};
128
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530129struct s3c2410_wdt {
130 struct device *dev;
131 struct clk *clock;
132 void __iomem *reg_base;
133 unsigned int count;
134 spinlock_t lock;
135 unsigned long wtcon_save;
136 unsigned long wtdat_save;
137 struct watchdog_device wdt_device;
138 struct notifier_block freq_transition;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530139 struct s3c2410_wdt_variant *drv_data;
140 struct regmap *pmureg;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530143static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
144 .quirks = 0
145};
146
147#ifdef CONFIG_OF
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000148static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
149 .quirks = QUIRK_HAS_WTCLRINT_REG,
150};
151
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530152static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
153 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
154 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
155 .mask_bit = 20,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800156 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
157 .rst_stat_bit = 20,
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000158 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
159 | QUIRK_HAS_WTCLRINT_REG,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530160};
161
162static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
163 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
164 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
165 .mask_bit = 0,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800166 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
167 .rst_stat_bit = 9,
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000168 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
169 | QUIRK_HAS_WTCLRINT_REG,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530170};
171
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530172static const struct s3c2410_wdt_variant drv_data_exynos7 = {
173 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
174 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
Abhilash Kesavan5476b2b2014-10-17 21:42:53 +0530175 .mask_bit = 23,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530176 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
177 .rst_stat_bit = 23, /* A57 WDTRESET */
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000178 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
179 | QUIRK_HAS_WTCLRINT_REG,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530180};
181
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530182static const struct of_device_id s3c2410_wdt_match[] = {
183 { .compatible = "samsung,s3c2410-wdt",
184 .data = &drv_data_s3c2410 },
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000185 { .compatible = "samsung,s3c6410-wdt",
186 .data = &drv_data_s3c6410 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530187 { .compatible = "samsung,exynos5250-wdt",
188 .data = &drv_data_exynos5250 },
189 { .compatible = "samsung,exynos5420-wdt",
190 .data = &drv_data_exynos5420 },
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530191 { .compatible = "samsung,exynos7-wdt",
192 .data = &drv_data_exynos7 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530193 {},
194};
195MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
196#endif
197
198static const struct platform_device_id s3c2410_wdt_ids[] = {
199 {
200 .name = "s3c2410-wdt",
201 .driver_data = (unsigned long)&drv_data_s3c2410,
202 },
203 {}
204};
205MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* watchdog control routines */
208
Joe Perches27c766a2012-02-15 15:06:19 -0800209#define DBG(fmt, ...) \
210do { \
211 if (debug) \
212 pr_info(fmt, ##__VA_ARGS__); \
213} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215/* functions */
216
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300217static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
218{
219 unsigned long freq = clk_get_rate(clock);
220
221 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
222 / S3C2410_WTCON_MAXDIV);
223}
224
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530225static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
226{
227 return container_of(nb, struct s3c2410_wdt, freq_transition);
228}
229
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530230static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
231{
232 int ret;
233 u32 mask_val = 1 << wdt->drv_data->mask_bit;
234 u32 val = 0;
235
236 /* No need to do anything if no PMU CONFIG needed */
237 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
238 return 0;
239
240 if (mask)
241 val = mask_val;
242
243 ret = regmap_update_bits(wdt->pmureg,
244 wdt->drv_data->disable_reg,
245 mask_val, val);
246 if (ret < 0)
247 goto error;
248
249 ret = regmap_update_bits(wdt->pmureg,
250 wdt->drv_data->mask_reset_reg,
251 mask_val, val);
252 error:
253 if (ret < 0)
254 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
255
256 return ret;
257}
258
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200259static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530261 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
262
263 spin_lock(&wdt->lock);
264 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
265 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200266
267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530270static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100271{
272 unsigned long wtcon;
273
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530274 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530276 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200279static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100280{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530281 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
282
283 spin_lock(&wdt->lock);
284 __s3c2410wdt_stop(wdt);
285 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200286
287 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100288}
289
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200290static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530293 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530295 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100296
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530297 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530299 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
301
302 if (soft_noboot) {
303 wtcon |= S3C2410_WTCON_INTEN;
304 wtcon &= ~S3C2410_WTCON_RSTEN;
305 } else {
306 wtcon &= ~S3C2410_WTCON_INTEN;
307 wtcon |= S3C2410_WTCON_RSTEN;
308 }
309
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530310 DBG("%s: count=0x%08x, wtcon=%08lx\n",
311 __func__, wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530313 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
314 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
315 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
316 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200317
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530321static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000322{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530323 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000324}
325
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200326static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530328 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
329 unsigned long freq = clk_get_rate(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned int count;
331 unsigned int divisor = 1;
332 unsigned long wtcon;
333
334 if (timeout < 1)
335 return -EINVAL;
336
Doug Anderson17862442013-11-26 16:57:19 -0800337 freq = DIV_ROUND_UP(freq, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 count = timeout * freq;
339
Ben Dookse02f8382009-10-30 00:30:25 +0000340 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800341 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* if the count is bigger than the watchdog register,
344 then work out what we need to do (and if) we can
345 actually make this value
346 */
347
348 if (count >= 0x10000) {
Doug Anderson17862442013-11-26 16:57:19 -0800349 divisor = DIV_ROUND_UP(count, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Doug Anderson17862442013-11-26 16:57:19 -0800351 if (divisor > 0x100) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530352 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EINVAL;
354 }
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Doug Anderson17862442013-11-26 16:57:19 -0800358 __func__, timeout, divisor, count, DIV_ROUND_UP(count, divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Doug Anderson17862442013-11-26 16:57:19 -0800360 count = DIV_ROUND_UP(count, divisor);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530361 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530364 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
366 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
367
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530368 writel(count, wdt->reg_base + S3C2410_WTDAT);
369 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Hans de Goede5f2430f2012-05-11 12:00:27 +0200371 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Guenter Roeck4d8b2292016-02-26 17:32:49 -0800376static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
377 void *data)
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500378{
379 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
380 void __iomem *wdt_base = wdt->reg_base;
381
382 /* disable watchdog, to be safe */
383 writel(0, wdt_base + S3C2410_WTCON);
384
385 /* put initial values into count and data */
386 writel(0x80, wdt_base + S3C2410_WTCNT);
387 writel(0x80, wdt_base + S3C2410_WTDAT);
388
389 /* set the watchdog to go and reset... */
390 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
391 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
392 wdt_base + S3C2410_WTCON);
393
394 /* wait for reset to assert... */
395 mdelay(500);
396
397 return 0;
398}
399
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000400#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Alan Cox41dc8b72008-08-04 17:54:46 +0100402static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .options = OPTIONS,
404 .firmware_version = 0,
405 .identity = "S3C2410 Watchdog",
406};
407
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200408static struct watchdog_ops s3c2410wdt_ops = {
409 .owner = THIS_MODULE,
410 .start = s3c2410wdt_start,
411 .stop = s3c2410wdt_stop,
412 .ping = s3c2410wdt_keepalive,
413 .set_timeout = s3c2410wdt_set_heartbeat,
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500414 .restart = s3c2410wdt_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200417static struct watchdog_device s3c2410_wdd = {
418 .info = &s3c2410_wdt_ident,
419 .ops = &s3c2410wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100420 .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421};
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/* interrupt handler code */
424
David Howells7d12e782006-10-05 14:55:46 +0100425static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530427 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530429 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
430
431 s3c2410wdt_keepalive(&wdt->wdt_device);
Krzysztof Kozlowski4dc1eb42017-04-04 19:32:35 +0000432
433 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
434 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return IRQ_HANDLED;
437}
Ben Dookse02f8382009-10-30 00:30:25 +0000438
Doug Anderson0f1dd982013-11-25 15:36:43 -0800439#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dookse02f8382009-10-30 00:30:25 +0000440
441static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
442 unsigned long val, void *data)
443{
444 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530445 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000446
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530447 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000448 goto done;
449
450 if (val == CPUFREQ_PRECHANGE) {
451 /* To ensure that over the change we don't cause the
452 * watchdog to trigger, we perform an keep-alive if
453 * the watchdog is running.
454 */
455
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530456 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000457 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530458 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000459
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530460 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
461 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000462
463 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530464 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000465 else
466 goto err;
467 }
468
469done:
470 return 0;
471
472 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530473 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
474 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000475 return ret;
476}
477
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530478static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000479{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530480 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
481
482 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000483 CPUFREQ_TRANSITION_NOTIFIER);
484}
485
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530486static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000487{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530488 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
489
490 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000491 CPUFREQ_TRANSITION_NOTIFIER);
492}
493
494#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530495
496static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000497{
498 return 0;
499}
500
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530501static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000502{
503}
504#endif
505
Doug Andersoncffc9a62013-12-06 13:08:07 -0800506static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
507{
508 unsigned int rst_stat;
509 int ret;
510
511 if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
512 return 0;
513
514 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
515 if (ret)
516 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
517 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
518 return WDIOF_CARDRESET;
519
520 return 0;
521}
522
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530523/* s3c2410_get_wdt_driver_data */
524static inline struct s3c2410_wdt_variant *
525get_wdt_drv_data(struct platform_device *pdev)
526{
527 if (pdev->dev.of_node) {
528 const struct of_device_id *match;
529 match = of_match_node(s3c2410_wdt_match, pdev->dev.of_node);
530 return (struct s3c2410_wdt_variant *)match->data;
531 } else {
532 return (struct s3c2410_wdt_variant *)
533 platform_get_device_id(pdev)->driver_data;
534 }
535}
536
Bill Pemberton2d991a12012-11-19 13:21:41 -0500537static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100539 struct device *dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530540 struct s3c2410_wdt *wdt;
541 struct resource *wdt_mem;
542 struct resource *wdt_irq;
Ben Dooks46b814d2007-06-14 12:08:54 +0100543 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 int started = 0;
545 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800547 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Ben Dookse8ef92b2007-06-14 12:08:55 +0100549 dev = &pdev->dev;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100550
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530551 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
552 if (!wdt)
553 return -ENOMEM;
554
555 wdt->dev = &pdev->dev;
556 spin_lock_init(&wdt->lock);
557 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530559 wdt->drv_data = get_wdt_drv_data(pdev);
Doug Andersoncffc9a62013-12-06 13:08:07 -0800560 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530561 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
562 "samsung,syscon-phandle");
563 if (IS_ERR(wdt->pmureg)) {
564 dev_err(dev, "syscon regmap lookup failed.\n");
565 return PTR_ERR(wdt->pmureg);
566 }
567 }
568
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900569 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
570 if (wdt_irq == NULL) {
571 dev_err(dev, "no irq resource specified\n");
572 ret = -ENOENT;
573 goto err;
574 }
575
576 /* get the memory region for the watchdog timer */
Julia Lawallbd5cc112013-08-14 11:11:24 +0200577 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530578 wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
579 if (IS_ERR(wdt->reg_base)) {
580 ret = PTR_ERR(wdt->reg_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900581 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530584 DBG("probe: mapped reg_base=%p\n", wdt->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530586 wdt->clock = devm_clk_get(dev, "watchdog");
587 if (IS_ERR(wdt->clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100588 dev_err(dev, "failed to find watchdog clock source\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530589 ret = PTR_ERR(wdt->clock);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900590 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Sachin Kamat01b6af92014-03-04 15:04:35 +0530593 ret = clk_prepare_enable(wdt->clock);
594 if (ret < 0) {
595 dev_err(dev, "failed to enable clock\n");
596 return ret;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300599 wdt->wdt_device.min_timeout = 1;
600 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
601
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530602 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900603 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900604 dev_err(dev, "failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000605 goto err_clk;
606 }
607
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530608 watchdog_set_drvdata(&wdt->wdt_device, wdt);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* see if we can actually set the requested timer margin, and if
611 * not, try the default value */
612
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530613 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, &pdev->dev);
614 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
615 wdt->wdt_device.timeout);
616 if (ret) {
617 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
Alan Cox41dc8b72008-08-04 17:54:46 +0100618 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Alan Cox41dc8b72008-08-04 17:54:46 +0100620 if (started == 0)
621 dev_info(dev,
622 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100624 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000625 dev_info(dev, "default timer value is out of range, "
626 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900629 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
630 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900631 if (ret != 0) {
632 dev_err(dev, "failed to install irq (%d)\n", ret);
633 goto err_cpufreq;
634 }
635
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530636 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500637 watchdog_set_restart_priority(&wdt->wdt_device, 128);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100638
Doug Andersoncffc9a62013-12-06 13:08:07 -0800639 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
Pratyush Anand65518812015-08-20 14:05:01 +0530640 wdt->wdt_device.parent = &pdev->dev;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800641
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530642 ret = watchdog_register_device(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200644 dev_err(dev, "cannot register watchdog (%d)\n", ret);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900645 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530648 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
649 if (ret < 0)
650 goto err_unregister;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100653 dev_info(dev, "starting watchdog timer\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530654 s3c2410wdt_start(&wdt->wdt_device);
Ben Dooks655516c2006-04-19 23:02:56 +0100655 } else if (!tmr_atboot) {
656 /* if we're not enabling the watchdog, then ensure it is
657 * disabled if it has been left running from the bootloader
658 * or other source */
659
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530660 s3c2410wdt_stop(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530663 platform_set_drvdata(pdev, wdt);
664
Ben Dooks46b814d2007-06-14 12:08:54 +0100665 /* print out a statement of readiness */
666
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530667 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100668
Ben Dookse8ef92b2007-06-14 12:08:55 +0100669 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100670 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400671 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
672 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000675
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530676 err_unregister:
677 watchdog_unregister_device(&wdt->wdt_device);
678
Ben Dookse02f8382009-10-30 00:30:25 +0000679 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530680 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000681
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000682 err_clk:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530683 clk_disable_unprepare(wdt->clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000684
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900685 err:
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Bill Pemberton4b12b892012-11-19 13:26:24 -0500689static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530691 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530692 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000693
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530694 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
695 if (ret < 0)
696 return ret;
697
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530698 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000699
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530700 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530702 clk_disable_unprepare(wdt->clock);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
Russell King3ae5eae2005-11-09 22:32:44 +0000707static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200708{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530709 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
710
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530711 s3c2410wdt_mask_and_disable_reset(wdt, true);
712
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530713 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200714}
715
Jingoo Han0183984c2013-03-14 10:31:21 +0900716#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200717
Jingoo Han0183984c2013-03-14 10:31:21 +0900718static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200719{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530720 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530721 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
722
Russell King9480e302005-10-28 09:52:56 -0700723 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530724 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
725 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200726
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530727 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
728 if (ret < 0)
729 return ret;
730
Russell King9480e302005-10-28 09:52:56 -0700731 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530732 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200733
734 return 0;
735}
736
Jingoo Han0183984c2013-03-14 10:31:21 +0900737static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200738{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530739 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530740 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200741
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530742 /* Restore watchdog state. */
743 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
744 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
745 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200746
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530747 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
748 if (ret < 0)
749 return ret;
750
Jingoo Han0183984c2013-03-14 10:31:21 +0900751 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530752 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200753
754 return 0;
755}
Jingoo Han0183984c2013-03-14 10:31:21 +0900756#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200757
Jingoo Han0183984c2013-03-14 10:31:21 +0900758static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
759 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200760
Russell King3ae5eae2005-11-09 22:32:44 +0000761static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500763 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200764 .shutdown = s3c2410wdt_shutdown,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530765 .id_table = s3c2410_wdt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +0000766 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000767 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900768 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000769 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000770 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771};
772
Sachin Kamat6b761b22012-07-12 17:17:40 +0530773module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Ben Dooksaf4bb822005-08-17 09:03:23 +0200775MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
776 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
778MODULE_LICENSE("GPL");