blob: adaa43543f0a7c2591ea0bf472926aa367567fa0 [file] [log] [blame]
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2004 Simtec Electronics
3 * Ben Dooks <ben@simtec.co.uk>
4 *
5 * S3C2410 Watchdog Timer Support
6 *
7 * Based on, softdog.c by Alan Cox,
Alan Cox29fa0582008-10-27 15:17:56 +00008 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +020019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/module.h>
22#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/watchdog.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010026#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000028#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010029#include <linux/uaccess.h>
30#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000031#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020033#include <linux/err.h>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000034#include <linux/of.h>
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +020035#include <linux/of_device.h>
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053036#include <linux/mfd/syscon.h>
37#include <linux/regmap.h>
Heiko Stuebnerf286e132014-08-19 17:45:36 -070038#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Tomasz Figaa8f54012013-06-17 23:45:24 +090040#define S3C2410_WTCON 0x00
41#define S3C2410_WTDAT 0x04
42#define S3C2410_WTCNT 0x08
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +020043#define S3C2410_WTCLRINT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030045#define S3C2410_WTCNT_MAXCNT 0xffff
46
Tomasz Figaa8f54012013-06-17 23:45:24 +090047#define S3C2410_WTCON_RSTEN (1 << 0)
48#define S3C2410_WTCON_INTEN (1 << 2)
49#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Tomasz Figaa8f54012013-06-17 23:45:24 +090051#define S3C2410_WTCON_DIV16 (0 << 3)
52#define S3C2410_WTCON_DIV32 (1 << 3)
53#define S3C2410_WTCON_DIV64 (2 << 3)
54#define S3C2410_WTCON_DIV128 (3 << 3)
55
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030056#define S3C2410_WTCON_MAXDIV 0x80
57
Tomasz Figaa8f54012013-06-17 23:45:24 +090058#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
59#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030060#define S3C2410_WTCON_PRESCALE_MAX 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020062#define S3C2410_WATCHDOG_ATBOOT (0)
63#define S3C2410_WATCHDOG_DEFAULT_TIME (15)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Doug Andersoncffc9a62013-12-06 13:08:07 -080065#define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053066#define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
67#define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
68#define QUIRK_HAS_PMU_CONFIG (1 << 0)
Doug Andersoncffc9a62013-12-06 13:08:07 -080069#define QUIRK_HAS_RST_STAT (1 << 1)
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +020070#define QUIRK_HAS_WTCLRINT_REG (1 << 2)
Doug Andersoncffc9a62013-12-06 13:08:07 -080071
72/* These quirks require that we have a PMU register map */
73#define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
74 QUIRK_HAS_RST_STAT)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053075
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010076static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010077static int tmr_margin;
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020078static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010079static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81module_param(tmr_margin, int, 0);
82module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010083module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084module_param(soft_noboot, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Randy Dunlap76550d32010-05-01 09:46:15 -070086MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020087 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
Alan Cox41dc8b72008-08-04 17:54:46 +010088MODULE_PARM_DESC(tmr_atboot,
89 "Watchdog is started at boot time if set to 1, default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020090 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
Alan Cox41dc8b72008-08-04 17:54:46 +010091MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
92 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +020093MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053095/**
96 * struct s3c2410_wdt_variant - Per-variant config data
97 *
98 * @disable_reg: Offset in pmureg for the register that disables the watchdog
99 * timer reset functionality.
100 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
101 * timer reset functionality.
102 * @mask_bit: Bit number for the watchdog timer in the disable register and the
103 * mask reset register.
Doug Andersoncffc9a62013-12-06 13:08:07 -0800104 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
105 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
106 * reset.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530107 * @quirks: A bitfield of quirks.
108 */
109
110struct s3c2410_wdt_variant {
111 int disable_reg;
112 int mask_reset_reg;
113 int mask_bit;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800114 int rst_stat_reg;
115 int rst_stat_bit;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530116 u32 quirks;
117};
118
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530119struct s3c2410_wdt {
120 struct device *dev;
121 struct clk *clock;
122 void __iomem *reg_base;
123 unsigned int count;
124 spinlock_t lock;
125 unsigned long wtcon_save;
126 unsigned long wtdat_save;
127 struct watchdog_device wdt_device;
128 struct notifier_block freq_transition;
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200129 const struct s3c2410_wdt_variant *drv_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530130 struct regmap *pmureg;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530131};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530133static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
134 .quirks = 0
135};
136
137#ifdef CONFIG_OF
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200138static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
139 .quirks = QUIRK_HAS_WTCLRINT_REG,
140};
141
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530142static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
143 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
144 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
145 .mask_bit = 20,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800146 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
147 .rst_stat_bit = 20,
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200148 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
149 | QUIRK_HAS_WTCLRINT_REG,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530150};
151
152static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
153 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
154 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
155 .mask_bit = 0,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800156 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
157 .rst_stat_bit = 9,
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200158 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
159 | QUIRK_HAS_WTCLRINT_REG,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530160};
161
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530162static const struct s3c2410_wdt_variant drv_data_exynos7 = {
163 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
164 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
Abhilash Kesavan5476b2b2014-10-17 21:42:53 +0530165 .mask_bit = 23,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530166 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
167 .rst_stat_bit = 23, /* A57 WDTRESET */
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200168 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
169 | QUIRK_HAS_WTCLRINT_REG,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530170};
171
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530172static const struct of_device_id s3c2410_wdt_match[] = {
173 { .compatible = "samsung,s3c2410-wdt",
174 .data = &drv_data_s3c2410 },
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200175 { .compatible = "samsung,s3c6410-wdt",
176 .data = &drv_data_s3c6410 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530177 { .compatible = "samsung,exynos5250-wdt",
178 .data = &drv_data_exynos5250 },
179 { .compatible = "samsung,exynos5420-wdt",
180 .data = &drv_data_exynos5420 },
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530181 { .compatible = "samsung,exynos7-wdt",
182 .data = &drv_data_exynos7 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530183 {},
184};
185MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
186#endif
187
188static const struct platform_device_id s3c2410_wdt_ids[] = {
189 {
190 .name = "s3c2410-wdt",
191 .driver_data = (unsigned long)&drv_data_s3c2410,
192 },
193 {}
194};
195MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* functions */
198
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300199static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
200{
201 unsigned long freq = clk_get_rate(clock);
202
203 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
204 / S3C2410_WTCON_MAXDIV);
205}
206
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530207static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
208{
209 return container_of(nb, struct s3c2410_wdt, freq_transition);
210}
211
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530212static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
213{
214 int ret;
215 u32 mask_val = 1 << wdt->drv_data->mask_bit;
216 u32 val = 0;
217
218 /* No need to do anything if no PMU CONFIG needed */
219 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
220 return 0;
221
222 if (mask)
223 val = mask_val;
224
225 ret = regmap_update_bits(wdt->pmureg,
226 wdt->drv_data->disable_reg,
227 mask_val, val);
228 if (ret < 0)
229 goto error;
230
231 ret = regmap_update_bits(wdt->pmureg,
232 wdt->drv_data->mask_reset_reg,
233 mask_val, val);
234 error:
235 if (ret < 0)
236 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
237
238 return ret;
239}
240
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200241static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530243 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
244
245 spin_lock(&wdt->lock);
246 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
247 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200248
249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530252static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100253{
254 unsigned long wtcon;
255
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530256 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530258 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200261static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100262{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530263 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
264
265 spin_lock(&wdt->lock);
266 __s3c2410wdt_stop(wdt);
267 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200268
269 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100270}
271
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200272static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530275 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530277 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100278
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530279 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530281 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
283
284 if (soft_noboot) {
285 wtcon |= S3C2410_WTCON_INTEN;
286 wtcon &= ~S3C2410_WTCON_RSTEN;
287 } else {
288 wtcon &= ~S3C2410_WTCON_INTEN;
289 wtcon |= S3C2410_WTCON_RSTEN;
290 }
291
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200292 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
293 wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530295 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
296 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
297 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
298 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200299
300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530303static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000304{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530305 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000306}
307
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200308static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
309 unsigned int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530311 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
312 unsigned long freq = clk_get_rate(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned int count;
314 unsigned int divisor = 1;
315 unsigned long wtcon;
316
317 if (timeout < 1)
318 return -EINVAL;
319
Doug Anderson17862442013-11-26 16:57:19 -0800320 freq = DIV_ROUND_UP(freq, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 count = timeout * freq;
322
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200323 dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
324 count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* if the count is bigger than the watchdog register,
327 then work out what we need to do (and if) we can
328 actually make this value
329 */
330
331 if (count >= 0x10000) {
Doug Anderson17862442013-11-26 16:57:19 -0800332 divisor = DIV_ROUND_UP(count, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Doug Anderson17862442013-11-26 16:57:19 -0800334 if (divisor > 0x100) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530335 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -EINVAL;
337 }
338 }
339
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200340 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
341 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Doug Anderson17862442013-11-26 16:57:19 -0800343 count = DIV_ROUND_UP(count, divisor);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530344 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530347 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
349 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
350
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530351 writel(count, wdt->reg_base + S3C2410_WTDAT);
352 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Hans de Goede5f2430f2012-05-11 12:00:27 +0200354 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
Guenter Roeck4d8b2292016-02-26 17:32:49 -0800359static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
360 void *data)
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500361{
362 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
363 void __iomem *wdt_base = wdt->reg_base;
364
365 /* disable watchdog, to be safe */
366 writel(0, wdt_base + S3C2410_WTCON);
367
368 /* put initial values into count and data */
369 writel(0x80, wdt_base + S3C2410_WTCNT);
370 writel(0x80, wdt_base + S3C2410_WTDAT);
371
372 /* set the watchdog to go and reset... */
373 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
374 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
375 wdt_base + S3C2410_WTCON);
376
377 /* wait for reset to assert... */
378 mdelay(500);
379
380 return 0;
381}
382
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000383#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alan Cox41dc8b72008-08-04 17:54:46 +0100385static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .options = OPTIONS,
387 .firmware_version = 0,
388 .identity = "S3C2410 Watchdog",
389};
390
Bhumika Goyalb893e342017-01-28 13:11:17 +0530391static const struct watchdog_ops s3c2410wdt_ops = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200392 .owner = THIS_MODULE,
393 .start = s3c2410wdt_start,
394 .stop = s3c2410wdt_stop,
395 .ping = s3c2410wdt_keepalive,
396 .set_timeout = s3c2410wdt_set_heartbeat,
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500397 .restart = s3c2410wdt_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200400static const struct watchdog_device s3c2410_wdd = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200401 .info = &s3c2410_wdt_ident,
402 .ops = &s3c2410wdt_ops,
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200403 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* interrupt handler code */
407
David Howells7d12e782006-10-05 14:55:46 +0100408static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530410 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530412 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
413
414 s3c2410wdt_keepalive(&wdt->wdt_device);
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200415
416 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
417 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return IRQ_HANDLED;
420}
Ben Dookse02f8382009-10-30 00:30:25 +0000421
Doug Anderson0f1dd982013-11-25 15:36:43 -0800422#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dookse02f8382009-10-30 00:30:25 +0000423
424static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
425 unsigned long val, void *data)
426{
427 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530428 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000429
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530430 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000431 goto done;
432
433 if (val == CPUFREQ_PRECHANGE) {
434 /* To ensure that over the change we don't cause the
435 * watchdog to trigger, we perform an keep-alive if
436 * the watchdog is running.
437 */
438
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530439 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000440 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530441 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000442
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530443 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
444 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000445
446 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530447 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000448 else
449 goto err;
450 }
451
452done:
453 return 0;
454
455 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530456 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
457 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000458 return ret;
459}
460
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530461static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000462{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530463 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
464
465 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000466 CPUFREQ_TRANSITION_NOTIFIER);
467}
468
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530469static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000470{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530471 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
472
473 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000474 CPUFREQ_TRANSITION_NOTIFIER);
475}
476
477#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530478
479static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000480{
481 return 0;
482}
483
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530484static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000485{
486}
487#endif
488
Doug Andersoncffc9a62013-12-06 13:08:07 -0800489static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
490{
491 unsigned int rst_stat;
492 int ret;
493
494 if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
495 return 0;
496
497 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
498 if (ret)
499 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
500 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
501 return WDIOF_CARDRESET;
502
503 return 0;
504}
505
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200506static inline const struct s3c2410_wdt_variant *
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200507s3c2410_get_wdt_drv_data(struct platform_device *pdev)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530508{
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200509 const struct s3c2410_wdt_variant *variant;
510
511 variant = of_device_get_match_data(&pdev->dev);
512 if (!variant) {
513 /* Device matched by platform_device_id */
514 variant = (struct s3c2410_wdt_variant *)
515 platform_get_device_id(pdev)->driver_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530516 }
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200517
518 return variant;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530519}
520
Bill Pemberton2d991a12012-11-19 13:21:41 -0500521static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200523 struct device *dev = &pdev->dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530524 struct s3c2410_wdt *wdt;
525 struct resource *wdt_mem;
526 struct resource *wdt_irq;
Ben Dooks46b814d2007-06-14 12:08:54 +0100527 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int started = 0;
529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530531 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
532 if (!wdt)
533 return -ENOMEM;
534
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200535 wdt->dev = dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530536 spin_lock_init(&wdt->lock);
537 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200539 wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
Doug Andersoncffc9a62013-12-06 13:08:07 -0800540 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530541 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
542 "samsung,syscon-phandle");
543 if (IS_ERR(wdt->pmureg)) {
544 dev_err(dev, "syscon regmap lookup failed.\n");
545 return PTR_ERR(wdt->pmureg);
546 }
547 }
548
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900549 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
550 if (wdt_irq == NULL) {
551 dev_err(dev, "no irq resource specified\n");
552 ret = -ENOENT;
553 goto err;
554 }
555
556 /* get the memory region for the watchdog timer */
Julia Lawallbd5cc112013-08-14 11:11:24 +0200557 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530558 wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
559 if (IS_ERR(wdt->reg_base)) {
560 ret = PTR_ERR(wdt->reg_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900561 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530564 wdt->clock = devm_clk_get(dev, "watchdog");
565 if (IS_ERR(wdt->clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100566 dev_err(dev, "failed to find watchdog clock source\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530567 ret = PTR_ERR(wdt->clock);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900568 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Sachin Kamat01b6af92014-03-04 15:04:35 +0530571 ret = clk_prepare_enable(wdt->clock);
572 if (ret < 0) {
573 dev_err(dev, "failed to enable clock\n");
574 return ret;
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300577 wdt->wdt_device.min_timeout = 1;
578 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
579
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530580 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900581 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900582 dev_err(dev, "failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000583 goto err_clk;
584 }
585
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530586 watchdog_set_drvdata(&wdt->wdt_device, wdt);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* see if we can actually set the requested timer margin, and if
589 * not, try the default value */
590
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200591 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530592 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
593 wdt->wdt_device.timeout);
594 if (ret) {
595 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200596 S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Alan Cox41dc8b72008-08-04 17:54:46 +0100598 if (started == 0)
599 dev_info(dev,
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200600 "tmr_margin value out of range, default %d used\n",
601 S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100602 else
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200603 dev_info(dev, "default timer value is out of range, cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900606 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
607 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900608 if (ret != 0) {
609 dev_err(dev, "failed to install irq (%d)\n", ret);
610 goto err_cpufreq;
611 }
612
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530613 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500614 watchdog_set_restart_priority(&wdt->wdt_device, 128);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100615
Doug Andersoncffc9a62013-12-06 13:08:07 -0800616 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200617 wdt->wdt_device.parent = dev;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800618
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530619 ret = watchdog_register_device(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200621 dev_err(dev, "cannot register watchdog (%d)\n", ret);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900622 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530625 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
626 if (ret < 0)
627 goto err_unregister;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100630 dev_info(dev, "starting watchdog timer\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530631 s3c2410wdt_start(&wdt->wdt_device);
Ben Dooks655516c2006-04-19 23:02:56 +0100632 } else if (!tmr_atboot) {
633 /* if we're not enabling the watchdog, then ensure it is
634 * disabled if it has been left running from the bootloader
635 * or other source */
636
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530637 s3c2410wdt_stop(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530640 platform_set_drvdata(pdev, wdt);
641
Ben Dooks46b814d2007-06-14 12:08:54 +0100642 /* print out a statement of readiness */
643
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530644 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100645
Ben Dookse8ef92b2007-06-14 12:08:55 +0100646 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100647 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400648 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
649 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000652
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530653 err_unregister:
654 watchdog_unregister_device(&wdt->wdt_device);
655
Ben Dookse02f8382009-10-30 00:30:25 +0000656 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530657 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000658
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000659 err_clk:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530660 clk_disable_unprepare(wdt->clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000661
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900662 err:
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Bill Pemberton4b12b892012-11-19 13:26:24 -0500666static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530668 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530669 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000670
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530671 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
672 if (ret < 0)
673 return ret;
674
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530675 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000676
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530677 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530679 clk_disable_unprepare(wdt->clock);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682}
683
Russell King3ae5eae2005-11-09 22:32:44 +0000684static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200685{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530686 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
687
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530688 s3c2410wdt_mask_and_disable_reset(wdt, true);
689
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530690 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200691}
692
Jingoo Han0183984c2013-03-14 10:31:21 +0900693#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200694
Jingoo Han0183984c2013-03-14 10:31:21 +0900695static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200696{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530697 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530698 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
699
Russell King9480e302005-10-28 09:52:56 -0700700 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530701 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
702 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200703
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530704 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
705 if (ret < 0)
706 return ret;
707
Russell King9480e302005-10-28 09:52:56 -0700708 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530709 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200710
711 return 0;
712}
713
Jingoo Han0183984c2013-03-14 10:31:21 +0900714static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200715{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530716 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530717 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200718
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530719 /* Restore watchdog state. */
720 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
721 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
722 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200723
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530724 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
725 if (ret < 0)
726 return ret;
727
Jingoo Han0183984c2013-03-14 10:31:21 +0900728 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530729 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200730
731 return 0;
732}
Jingoo Han0183984c2013-03-14 10:31:21 +0900733#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200734
Jingoo Han0183984c2013-03-14 10:31:21 +0900735static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
736 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200737
Russell King3ae5eae2005-11-09 22:32:44 +0000738static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500740 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200741 .shutdown = s3c2410wdt_shutdown,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530742 .id_table = s3c2410_wdt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +0000743 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000744 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900745 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000746 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000747 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748};
749
Sachin Kamat6b761b22012-07-12 17:17:40 +0530750module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200752MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
754MODULE_LICENSE("GPL");