blob: 2a61b6ea56027d97cae27ddb8f4c1f5be1e4e87a [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +02002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +020010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/module.h>
13#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/watchdog.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010017#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000019#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010020#include <linux/uaccess.h>
21#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000022#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020024#include <linux/err.h>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000025#include <linux/of.h>
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +020026#include <linux/of_device.h>
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053027#include <linux/mfd/syscon.h>
28#include <linux/regmap.h>
Heiko Stuebnerf286e132014-08-19 17:45:36 -070029#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tomasz Figaa8f54012013-06-17 23:45:24 +090031#define S3C2410_WTCON 0x00
32#define S3C2410_WTDAT 0x04
33#define S3C2410_WTCNT 0x08
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +020034#define S3C2410_WTCLRINT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030036#define S3C2410_WTCNT_MAXCNT 0xffff
37
Tomasz Figaa8f54012013-06-17 23:45:24 +090038#define S3C2410_WTCON_RSTEN (1 << 0)
39#define S3C2410_WTCON_INTEN (1 << 2)
40#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Tomasz Figaa8f54012013-06-17 23:45:24 +090042#define S3C2410_WTCON_DIV16 (0 << 3)
43#define S3C2410_WTCON_DIV32 (1 << 3)
44#define S3C2410_WTCON_DIV64 (2 << 3)
45#define S3C2410_WTCON_DIV128 (3 << 3)
46
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030047#define S3C2410_WTCON_MAXDIV 0x80
48
Tomasz Figaa8f54012013-06-17 23:45:24 +090049#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
50#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Javier Martinez Canillas882dec12016-03-01 13:45:17 -030051#define S3C2410_WTCON_PRESCALE_MAX 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020053#define S3C2410_WATCHDOG_ATBOOT (0)
54#define S3C2410_WATCHDOG_DEFAULT_TIME (15)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Doug Andersoncffc9a62013-12-06 13:08:07 -080056#define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053057#define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
58#define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
59#define QUIRK_HAS_PMU_CONFIG (1 << 0)
Doug Andersoncffc9a62013-12-06 13:08:07 -080060#define QUIRK_HAS_RST_STAT (1 << 1)
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +020061#define QUIRK_HAS_WTCLRINT_REG (1 << 2)
Sam Protsenko8d9fdf62021-11-21 18:56:40 +020062#define QUIRK_HAS_PMU_AUTO_DISABLE (1 << 3)
Doug Andersoncffc9a62013-12-06 13:08:07 -080063
64/* These quirks require that we have a PMU register map */
65#define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
Sam Protsenko8d9fdf62021-11-21 18:56:40 +020066 QUIRK_HAS_RST_STAT | \
67 QUIRK_HAS_PMU_AUTO_DISABLE)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053068
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010069static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010070static int tmr_margin;
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020071static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010072static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74module_param(tmr_margin, int, 0);
75module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010076module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077module_param(soft_noboot, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Randy Dunlap76550d32010-05-01 09:46:15 -070079MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020080 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
Alan Cox41dc8b72008-08-04 17:54:46 +010081MODULE_PARM_DESC(tmr_atboot,
82 "Watchdog is started at boot time if set to 1, default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +020083 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
Alan Cox41dc8b72008-08-04 17:54:46 +010084MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
85 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +020086MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053088/**
89 * struct s3c2410_wdt_variant - Per-variant config data
90 *
91 * @disable_reg: Offset in pmureg for the register that disables the watchdog
92 * timer reset functionality.
93 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
94 * timer reset functionality.
Sam Protsenko370bc7f2021-11-21 18:56:42 +020095 * @mask_reset_inv: If set, mask_reset_reg value will have inverted meaning.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053096 * @mask_bit: Bit number for the watchdog timer in the disable register and the
97 * mask reset register.
Doug Andersoncffc9a62013-12-06 13:08:07 -080098 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
99 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
100 * reset.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530101 * @quirks: A bitfield of quirks.
102 */
103
104struct s3c2410_wdt_variant {
105 int disable_reg;
106 int mask_reset_reg;
Sam Protsenko370bc7f2021-11-21 18:56:42 +0200107 bool mask_reset_inv;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530108 int mask_bit;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800109 int rst_stat_reg;
110 int rst_stat_bit;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530111 u32 quirks;
112};
113
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530114struct s3c2410_wdt {
115 struct device *dev;
116 struct clk *clock;
117 void __iomem *reg_base;
118 unsigned int count;
119 spinlock_t lock;
120 unsigned long wtcon_save;
121 unsigned long wtdat_save;
122 struct watchdog_device wdt_device;
123 struct notifier_block freq_transition;
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200124 const struct s3c2410_wdt_variant *drv_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530125 struct regmap *pmureg;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530126};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530128static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
129 .quirks = 0
130};
131
132#ifdef CONFIG_OF
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200133static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
134 .quirks = QUIRK_HAS_WTCLRINT_REG,
135};
136
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530137static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
138 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
139 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
140 .mask_bit = 20,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800141 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
142 .rst_stat_bit = 20,
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200143 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
Sam Protsenko8d9fdf62021-11-21 18:56:40 +0200144 | QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_AUTO_DISABLE,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530145};
146
147static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
148 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
149 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
150 .mask_bit = 0,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800151 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
152 .rst_stat_bit = 9,
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200153 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
Sam Protsenko8d9fdf62021-11-21 18:56:40 +0200154 | QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_AUTO_DISABLE,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530155};
156
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530157static const struct s3c2410_wdt_variant drv_data_exynos7 = {
158 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
159 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
Abhilash Kesavan5476b2b2014-10-17 21:42:53 +0530160 .mask_bit = 23,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530161 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
162 .rst_stat_bit = 23, /* A57 WDTRESET */
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200163 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
Sam Protsenko8d9fdf62021-11-21 18:56:40 +0200164 | QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_AUTO_DISABLE,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530165};
166
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530167static const struct of_device_id s3c2410_wdt_match[] = {
168 { .compatible = "samsung,s3c2410-wdt",
169 .data = &drv_data_s3c2410 },
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200170 { .compatible = "samsung,s3c6410-wdt",
171 .data = &drv_data_s3c6410 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530172 { .compatible = "samsung,exynos5250-wdt",
173 .data = &drv_data_exynos5250 },
174 { .compatible = "samsung,exynos5420-wdt",
175 .data = &drv_data_exynos5420 },
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530176 { .compatible = "samsung,exynos7-wdt",
177 .data = &drv_data_exynos7 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530178 {},
179};
180MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
181#endif
182
183static const struct platform_device_id s3c2410_wdt_ids[] = {
184 {
185 .name = "s3c2410-wdt",
186 .driver_data = (unsigned long)&drv_data_s3c2410,
187 },
188 {}
189};
190MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* functions */
193
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300194static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
195{
196 unsigned long freq = clk_get_rate(clock);
197
198 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
199 / S3C2410_WTCON_MAXDIV);
200}
201
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530202static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
203{
204 return container_of(nb, struct s3c2410_wdt, freq_transition);
205}
206
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200207static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530208{
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200209 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
210 const u32 val = mask ? mask_val : 0;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530211 int ret;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530212
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200213 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->disable_reg,
214 mask_val, val);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530215 if (ret < 0)
216 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
217
218 return ret;
219}
220
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200221static int s3c2410wdt_mask_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
222{
223 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
Sam Protsenko370bc7f2021-11-21 18:56:42 +0200224 const bool val_inv = wdt->drv_data->mask_reset_inv;
225 const u32 val = (mask ^ val_inv) ? mask_val : 0;
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200226 int ret;
227
228 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->mask_reset_reg,
229 mask_val, val);
230 if (ret < 0)
231 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
232
233 return ret;
234}
235
236static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
237{
238 int ret;
239
240 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_AUTO_DISABLE) {
241 ret = s3c2410wdt_disable_wdt_reset(wdt, mask);
242 if (ret < 0)
243 return ret;
244 }
245
246 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG) {
247 ret = s3c2410wdt_mask_wdt_reset(wdt, mask);
248 if (ret < 0)
249 return ret;
250 }
251
252 return 0;
253}
254
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200255static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530257 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
258
259 spin_lock(&wdt->lock);
260 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
261 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200262
263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530266static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100267{
268 unsigned long wtcon;
269
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530270 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530272 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200275static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100276{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530277 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
278
279 spin_lock(&wdt->lock);
280 __s3c2410wdt_stop(wdt);
281 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200282
283 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100284}
285
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200286static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530289 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530291 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100292
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530293 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530295 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
297
298 if (soft_noboot) {
299 wtcon |= S3C2410_WTCON_INTEN;
300 wtcon &= ~S3C2410_WTCON_RSTEN;
301 } else {
302 wtcon &= ~S3C2410_WTCON_INTEN;
303 wtcon |= S3C2410_WTCON_RSTEN;
304 }
305
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200306 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
307 wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530309 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
310 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
311 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
312 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200313
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530317static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000318{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530319 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000320}
321
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200322static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
323 unsigned int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530325 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
326 unsigned long freq = clk_get_rate(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int count;
328 unsigned int divisor = 1;
329 unsigned long wtcon;
330
331 if (timeout < 1)
332 return -EINVAL;
333
Doug Anderson17862442013-11-26 16:57:19 -0800334 freq = DIV_ROUND_UP(freq, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 count = timeout * freq;
336
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200337 dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
338 count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* if the count is bigger than the watchdog register,
341 then work out what we need to do (and if) we can
342 actually make this value
343 */
344
345 if (count >= 0x10000) {
Doug Anderson17862442013-11-26 16:57:19 -0800346 divisor = DIV_ROUND_UP(count, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Doug Anderson17862442013-11-26 16:57:19 -0800348 if (divisor > 0x100) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530349 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -EINVAL;
351 }
352 }
353
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200354 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
355 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Doug Anderson17862442013-11-26 16:57:19 -0800357 count = DIV_ROUND_UP(count, divisor);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530358 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530361 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
363 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
364
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530365 writel(count, wdt->reg_base + S3C2410_WTDAT);
366 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Hans de Goede5f2430f2012-05-11 12:00:27 +0200368 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
Guenter Roeck4d8b2292016-02-26 17:32:49 -0800373static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
374 void *data)
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500375{
376 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
377 void __iomem *wdt_base = wdt->reg_base;
378
379 /* disable watchdog, to be safe */
380 writel(0, wdt_base + S3C2410_WTCON);
381
382 /* put initial values into count and data */
383 writel(0x80, wdt_base + S3C2410_WTCNT);
384 writel(0x80, wdt_base + S3C2410_WTDAT);
385
386 /* set the watchdog to go and reset... */
387 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
388 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
389 wdt_base + S3C2410_WTCON);
390
391 /* wait for reset to assert... */
392 mdelay(500);
393
394 return 0;
395}
396
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000397#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Alan Cox41dc8b72008-08-04 17:54:46 +0100399static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .options = OPTIONS,
401 .firmware_version = 0,
402 .identity = "S3C2410 Watchdog",
403};
404
Bhumika Goyalb893e342017-01-28 13:11:17 +0530405static const struct watchdog_ops s3c2410wdt_ops = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200406 .owner = THIS_MODULE,
407 .start = s3c2410wdt_start,
408 .stop = s3c2410wdt_stop,
409 .ping = s3c2410wdt_keepalive,
410 .set_timeout = s3c2410wdt_set_heartbeat,
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500411 .restart = s3c2410wdt_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200414static const struct watchdog_device s3c2410_wdd = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200415 .info = &s3c2410_wdt_ident,
416 .ops = &s3c2410wdt_ops,
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200417 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418};
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* interrupt handler code */
421
David Howells7d12e782006-10-05 14:55:46 +0100422static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530424 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530426 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
427
428 s3c2410wdt_keepalive(&wdt->wdt_device);
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200429
430 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
431 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return IRQ_HANDLED;
434}
Ben Dookse02f8382009-10-30 00:30:25 +0000435
Doug Anderson0f1dd982013-11-25 15:36:43 -0800436#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dookse02f8382009-10-30 00:30:25 +0000437
438static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
439 unsigned long val, void *data)
440{
441 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530442 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000443
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530444 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000445 goto done;
446
447 if (val == CPUFREQ_PRECHANGE) {
448 /* To ensure that over the change we don't cause the
449 * watchdog to trigger, we perform an keep-alive if
450 * the watchdog is running.
451 */
452
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530453 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000454 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530455 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000456
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530457 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
458 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000459
460 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530461 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000462 else
463 goto err;
464 }
465
466done:
467 return 0;
468
469 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530470 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
471 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000472 return ret;
473}
474
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530475static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000476{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530477 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
478
479 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000480 CPUFREQ_TRANSITION_NOTIFIER);
481}
482
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530483static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000484{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530485 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
486
487 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000488 CPUFREQ_TRANSITION_NOTIFIER);
489}
490
491#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530492
493static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000494{
495 return 0;
496}
497
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530498static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000499{
500}
501#endif
502
Doug Andersoncffc9a62013-12-06 13:08:07 -0800503static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
504{
505 unsigned int rst_stat;
506 int ret;
507
508 if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
509 return 0;
510
511 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
512 if (ret)
513 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
514 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
515 return WDIOF_CARDRESET;
516
517 return 0;
518}
519
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200520static inline const struct s3c2410_wdt_variant *
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200521s3c2410_get_wdt_drv_data(struct platform_device *pdev)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530522{
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200523 const struct s3c2410_wdt_variant *variant;
524
525 variant = of_device_get_match_data(&pdev->dev);
526 if (!variant) {
527 /* Device matched by platform_device_id */
528 variant = (struct s3c2410_wdt_variant *)
529 platform_get_device_id(pdev)->driver_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530530 }
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200531
532 return variant;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530533}
534
Bill Pemberton2d991a12012-11-19 13:21:41 -0500535static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200537 struct device *dev = &pdev->dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530538 struct s3c2410_wdt *wdt;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530539 struct resource *wdt_irq;
Ben Dooks46b814d2007-06-14 12:08:54 +0100540 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530543 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
544 if (!wdt)
545 return -ENOMEM;
546
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200547 wdt->dev = dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530548 spin_lock_init(&wdt->lock);
549 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200551 wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
Doug Andersoncffc9a62013-12-06 13:08:07 -0800552 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530553 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
554 "samsung,syscon-phandle");
555 if (IS_ERR(wdt->pmureg)) {
556 dev_err(dev, "syscon regmap lookup failed.\n");
557 return PTR_ERR(wdt->pmureg);
558 }
559 }
560
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900561 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
562 if (wdt_irq == NULL) {
563 dev_err(dev, "no irq resource specified\n");
564 ret = -ENOENT;
565 goto err;
566 }
567
568 /* get the memory region for the watchdog timer */
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700569 wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530570 if (IS_ERR(wdt->reg_base)) {
571 ret = PTR_ERR(wdt->reg_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900572 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530575 wdt->clock = devm_clk_get(dev, "watchdog");
576 if (IS_ERR(wdt->clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100577 dev_err(dev, "failed to find watchdog clock source\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530578 ret = PTR_ERR(wdt->clock);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900579 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Sachin Kamat01b6af92014-03-04 15:04:35 +0530582 ret = clk_prepare_enable(wdt->clock);
583 if (ret < 0) {
584 dev_err(dev, "failed to enable clock\n");
585 return ret;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300588 wdt->wdt_device.min_timeout = 1;
589 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
590
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530591 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900592 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900593 dev_err(dev, "failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000594 goto err_clk;
595 }
596
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530597 watchdog_set_drvdata(&wdt->wdt_device, wdt);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* see if we can actually set the requested timer margin, and if
600 * not, try the default value */
601
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200602 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530603 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
604 wdt->wdt_device.timeout);
605 if (ret) {
Sam Protsenkof197d472021-11-21 18:56:38 +0200606 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
607 S3C2410_WATCHDOG_DEFAULT_TIME);
608 if (ret == 0) {
609 dev_warn(dev, "tmr_margin value out of range, default %d used\n",
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200610 S3C2410_WATCHDOG_DEFAULT_TIME);
Sam Protsenkof197d472021-11-21 18:56:38 +0200611 } else {
612 dev_err(dev, "failed to use default timeout\n");
613 goto err_cpufreq;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900617 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
618 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900619 if (ret != 0) {
620 dev_err(dev, "failed to install irq (%d)\n", ret);
621 goto err_cpufreq;
622 }
623
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530624 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500625 watchdog_set_restart_priority(&wdt->wdt_device, 128);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100626
Doug Andersoncffc9a62013-12-06 13:08:07 -0800627 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200628 wdt->wdt_device.parent = dev;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800629
Sam Protsenkoa90102e2021-11-21 18:56:39 +0200630 /*
631 * If "tmr_atboot" param is non-zero, start the watchdog right now. Also
632 * set WDOG_HW_RUNNING bit, so that watchdog core can kick the watchdog.
633 *
634 * If we're not enabling the watchdog, then ensure it is disabled if it
635 * has been left running from the bootloader or other source.
636 */
637 if (tmr_atboot) {
638 dev_info(dev, "starting watchdog timer\n");
639 s3c2410wdt_start(&wdt->wdt_device);
640 set_bit(WDOG_HW_RUNNING, &wdt->wdt_device.status);
641 } else {
642 s3c2410wdt_stop(&wdt->wdt_device);
643 }
644
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530645 ret = watchdog_register_device(&wdt->wdt_device);
Wolfram Sang386f4652019-05-18 23:27:50 +0200646 if (ret)
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900647 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530649 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
650 if (ret < 0)
651 goto err_unregister;
652
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530653 platform_set_drvdata(pdev, wdt);
654
Ben Dooks46b814d2007-06-14 12:08:54 +0100655 /* print out a statement of readiness */
656
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530657 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100658
Ben Dookse8ef92b2007-06-14 12:08:55 +0100659 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100660 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400661 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
662 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000665
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530666 err_unregister:
667 watchdog_unregister_device(&wdt->wdt_device);
668
Ben Dookse02f8382009-10-30 00:30:25 +0000669 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530670 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000671
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000672 err_clk:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530673 clk_disable_unprepare(wdt->clock);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000674
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900675 err:
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Bill Pemberton4b12b892012-11-19 13:26:24 -0500679static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530681 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530682 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000683
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530684 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
685 if (ret < 0)
686 return ret;
687
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530688 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000689
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530690 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530692 clk_disable_unprepare(wdt->clock);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695}
696
Russell King3ae5eae2005-11-09 22:32:44 +0000697static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200698{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530699 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
700
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530701 s3c2410wdt_mask_and_disable_reset(wdt, true);
702
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530703 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200704}
705
Jingoo Han0183984c2013-03-14 10:31:21 +0900706#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200707
Jingoo Han0183984c2013-03-14 10:31:21 +0900708static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200709{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530710 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530711 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
712
Russell King9480e302005-10-28 09:52:56 -0700713 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530714 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
715 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200716
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530717 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
718 if (ret < 0)
719 return ret;
720
Russell King9480e302005-10-28 09:52:56 -0700721 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530722 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200723
724 return 0;
725}
726
Jingoo Han0183984c2013-03-14 10:31:21 +0900727static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200728{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530729 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530730 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200731
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530732 /* Restore watchdog state. */
733 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
734 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
735 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200736
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530737 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
738 if (ret < 0)
739 return ret;
740
Jingoo Han0183984c2013-03-14 10:31:21 +0900741 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530742 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200743
744 return 0;
745}
Jingoo Han0183984c2013-03-14 10:31:21 +0900746#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200747
Jingoo Han0183984c2013-03-14 10:31:21 +0900748static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
749 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200750
Russell King3ae5eae2005-11-09 22:32:44 +0000751static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500753 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200754 .shutdown = s3c2410wdt_shutdown,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530755 .id_table = s3c2410_wdt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +0000756 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000757 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900758 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000759 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000760 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761};
762
Sachin Kamat6b761b22012-07-12 17:17:40 +0530763module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200765MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
767MODULE_LICENSE("GPL");