blob: 6db22f2e3a4f0392a2a12cd7a95343b747605a33 [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
Sam Protsenkocd4eadf2021-11-21 18:56:47 +020059#define EXYNOS850_CLUSTER0_NONCPU_OUT 0x1220
60#define EXYNOS850_CLUSTER0_NONCPU_INT_EN 0x1244
61#define EXYNOS850_CLUSTER1_NONCPU_OUT 0x1620
62#define EXYNOS850_CLUSTER1_NONCPU_INT_EN 0x1644
63
64#define EXYNOS850_CLUSTER0_WDTRESET_BIT 24
65#define EXYNOS850_CLUSTER1_WDTRESET_BIT 23
Sam Protsenkocf3fad42021-11-24 01:26:13 +020066
67/**
68 * DOC: Quirk flags for different Samsung watchdog IP-cores
69 *
70 * This driver supports multiple Samsung SoCs, each of which might have
71 * different set of registers and features supported. As watchdog block
72 * sometimes requires modifying PMU registers for proper functioning, register
73 * differences in both watchdog and PMU IP-cores should be accounted for. Quirk
74 * flags described below serve the purpose of telling the driver about mentioned
75 * SoC traits, and can be specified in driver data for each particular supported
76 * device.
77 *
78 * %QUIRK_HAS_WTCLRINT_REG: Watchdog block has WTCLRINT register. It's used to
79 * clear the interrupt once the interrupt service routine is complete. It's
80 * write-only, writing any values to this register clears the interrupt, but
81 * reading is not permitted.
82 *
83 * %QUIRK_HAS_PMU_MASK_RESET: PMU block has the register for disabling/enabling
84 * WDT reset request. On old SoCs it's usually called MASK_WDT_RESET_REQUEST,
85 * new SoCs have CLUSTERx_NONCPU_INT_EN register, which 'mask_bit' value is
86 * inverted compared to the former one.
87 *
88 * %QUIRK_HAS_PMU_RST_STAT: PMU block has RST_STAT (reset status) register,
89 * which contains bits indicating the reason for most recent CPU reset. If
90 * present, driver will use this register to check if previous reboot was due to
91 * watchdog timer reset.
92 *
93 * %QUIRK_HAS_PMU_AUTO_DISABLE: PMU block has AUTOMATIC_WDT_RESET_DISABLE
94 * register. If 'mask_bit' bit is set, PMU will disable WDT reset when
95 * corresponding processor is in reset state.
96 *
97 * %QUIRK_HAS_PMU_CNT_EN: PMU block has some register (e.g. CLUSTERx_NONCPU_OUT)
98 * with "watchdog counter enable" bit. That bit should be set to make watchdog
99 * counter running.
100 */
101#define QUIRK_HAS_WTCLRINT_REG (1 << 0)
102#define QUIRK_HAS_PMU_MASK_RESET (1 << 1)
103#define QUIRK_HAS_PMU_RST_STAT (1 << 2)
Sam Protsenko8d9fdf62021-11-21 18:56:40 +0200104#define QUIRK_HAS_PMU_AUTO_DISABLE (1 << 3)
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200105#define QUIRK_HAS_PMU_CNT_EN (1 << 4)
Doug Andersoncffc9a62013-12-06 13:08:07 -0800106
107/* These quirks require that we have a PMU register map */
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200108#define QUIRKS_HAVE_PMUREG \
109 (QUIRK_HAS_PMU_MASK_RESET | QUIRK_HAS_PMU_RST_STAT | \
110 QUIRK_HAS_PMU_AUTO_DISABLE | QUIRK_HAS_PMU_CNT_EN)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530111
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100112static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100113static int tmr_margin;
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200114static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +0100115static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117module_param(tmr_margin, int, 0);
118module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100119module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(soft_noboot, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Randy Dunlap76550d32010-05-01 09:46:15 -0700122MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200123 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
Alan Cox41dc8b72008-08-04 17:54:46 +0100124MODULE_PARM_DESC(tmr_atboot,
125 "Watchdog is started at boot time if set to 1, default="
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200126 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
Alan Cox41dc8b72008-08-04 17:54:46 +0100127MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
128 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200129MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530131/**
132 * struct s3c2410_wdt_variant - Per-variant config data
133 *
134 * @disable_reg: Offset in pmureg for the register that disables the watchdog
135 * timer reset functionality.
136 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
137 * timer reset functionality.
Sam Protsenko370bc7f2021-11-21 18:56:42 +0200138 * @mask_reset_inv: If set, mask_reset_reg value will have inverted meaning.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530139 * @mask_bit: Bit number for the watchdog timer in the disable register and the
140 * mask reset register.
Doug Andersoncffc9a62013-12-06 13:08:07 -0800141 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
142 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
143 * reset.
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200144 * @cnt_en_reg: Offset in pmureg for the register that enables WDT counter.
145 * @cnt_en_bit: Bit number for "watchdog counter enable" in cnt_en register.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530146 * @quirks: A bitfield of quirks.
147 */
148
149struct s3c2410_wdt_variant {
150 int disable_reg;
151 int mask_reset_reg;
Sam Protsenko370bc7f2021-11-21 18:56:42 +0200152 bool mask_reset_inv;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530153 int mask_bit;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800154 int rst_stat_reg;
155 int rst_stat_bit;
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200156 int cnt_en_reg;
157 int cnt_en_bit;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530158 u32 quirks;
159};
160
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530161struct s3c2410_wdt {
162 struct device *dev;
Sam Protsenkoe249d012021-11-21 18:56:45 +0200163 struct clk *bus_clk; /* for register interface (PCLK) */
164 struct clk *src_clk; /* for WDT counter */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530165 void __iomem *reg_base;
166 unsigned int count;
167 spinlock_t lock;
168 unsigned long wtcon_save;
169 unsigned long wtdat_save;
170 struct watchdog_device wdt_device;
171 struct notifier_block freq_transition;
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200172 const struct s3c2410_wdt_variant *drv_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530173 struct regmap *pmureg;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530176static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
177 .quirks = 0
178};
179
180#ifdef CONFIG_OF
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200181static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
182 .quirks = QUIRK_HAS_WTCLRINT_REG,
183};
184
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530185static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
186 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
187 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
188 .mask_bit = 20,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800189 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
190 .rst_stat_bit = 20,
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200191 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
192 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530193};
194
195static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
196 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
197 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
198 .mask_bit = 0,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800199 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
200 .rst_stat_bit = 9,
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200201 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
202 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530203};
204
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530205static const struct s3c2410_wdt_variant drv_data_exynos7 = {
206 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
207 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
Abhilash Kesavan5476b2b2014-10-17 21:42:53 +0530208 .mask_bit = 23,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530209 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
210 .rst_stat_bit = 23, /* A57 WDTRESET */
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200211 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
212 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530213};
214
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200215static const struct s3c2410_wdt_variant drv_data_exynos850_cl0 = {
216 .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
217 .mask_bit = 2,
218 .mask_reset_inv = true,
219 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
220 .rst_stat_bit = EXYNOS850_CLUSTER0_WDTRESET_BIT,
221 .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
222 .cnt_en_bit = 7,
223 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
224 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
225};
226
227static const struct s3c2410_wdt_variant drv_data_exynos850_cl1 = {
228 .mask_reset_reg = EXYNOS850_CLUSTER1_NONCPU_INT_EN,
229 .mask_bit = 2,
230 .mask_reset_inv = true,
231 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
232 .rst_stat_bit = EXYNOS850_CLUSTER1_WDTRESET_BIT,
233 .cnt_en_reg = EXYNOS850_CLUSTER1_NONCPU_OUT,
234 .cnt_en_bit = 7,
235 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
236 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
237};
238
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530239static const struct of_device_id s3c2410_wdt_match[] = {
240 { .compatible = "samsung,s3c2410-wdt",
241 .data = &drv_data_s3c2410 },
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200242 { .compatible = "samsung,s3c6410-wdt",
243 .data = &drv_data_s3c6410 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530244 { .compatible = "samsung,exynos5250-wdt",
245 .data = &drv_data_exynos5250 },
246 { .compatible = "samsung,exynos5420-wdt",
247 .data = &drv_data_exynos5420 },
Naveen Krishna Chatradhi2b9366b2014-08-27 15:17:11 +0530248 { .compatible = "samsung,exynos7-wdt",
249 .data = &drv_data_exynos7 },
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200250 { .compatible = "samsung,exynos850-wdt",
251 .data = &drv_data_exynos850_cl0 },
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530252 {},
253};
254MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
255#endif
256
257static const struct platform_device_id s3c2410_wdt_ids[] = {
258 {
259 .name = "s3c2410-wdt",
260 .driver_data = (unsigned long)&drv_data_s3c2410,
261 },
262 {}
263};
264MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/* functions */
267
Sam Protsenkoe249d012021-11-21 18:56:45 +0200268static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt)
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300269{
Sam Protsenkoe249d012021-11-21 18:56:45 +0200270 return clk_get_rate(wdt->src_clk ? wdt->src_clk : wdt->bus_clk);
271}
272
273static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt)
274{
275 const unsigned long freq = s3c2410wdt_get_freq(wdt);
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300276
277 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
278 / S3C2410_WTCON_MAXDIV);
279}
280
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530281static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
282{
283 return container_of(nb, struct s3c2410_wdt, freq_transition);
284}
285
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200286static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530287{
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200288 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
289 const u32 val = mask ? mask_val : 0;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530290 int ret;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530291
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200292 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->disable_reg,
293 mask_val, val);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530294 if (ret < 0)
295 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
296
297 return ret;
298}
299
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200300static int s3c2410wdt_mask_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
301{
302 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
Sam Protsenko370bc7f2021-11-21 18:56:42 +0200303 const bool val_inv = wdt->drv_data->mask_reset_inv;
304 const u32 val = (mask ^ val_inv) ? mask_val : 0;
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200305 int ret;
306
307 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->mask_reset_reg,
308 mask_val, val);
309 if (ret < 0)
310 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
311
312 return ret;
313}
314
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200315static int s3c2410wdt_enable_counter(struct s3c2410_wdt *wdt, bool en)
316{
317 const u32 mask_val = BIT(wdt->drv_data->cnt_en_bit);
318 const u32 val = en ? mask_val : 0;
319 int ret;
320
321 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->cnt_en_reg,
322 mask_val, val);
323 if (ret < 0)
324 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
325
326 return ret;
327}
328
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200329static int s3c2410wdt_enable(struct s3c2410_wdt *wdt, bool en)
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200330{
331 int ret;
332
333 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_AUTO_DISABLE) {
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200334 ret = s3c2410wdt_disable_wdt_reset(wdt, !en);
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200335 if (ret < 0)
336 return ret;
337 }
338
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200339 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_MASK_RESET) {
340 ret = s3c2410wdt_mask_wdt_reset(wdt, !en);
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200341 if (ret < 0)
342 return ret;
343 }
344
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200345 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_CNT_EN) {
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200346 ret = s3c2410wdt_enable_counter(wdt, en);
Sam Protsenkoaa220bc2021-11-21 18:56:43 +0200347 if (ret < 0)
348 return ret;
349 }
350
Sam Protsenko2bd33bb2021-11-21 18:56:41 +0200351 return 0;
352}
353
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200354static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530356 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
357
358 spin_lock(&wdt->lock);
359 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
360 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200361
362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530365static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100366{
367 unsigned long wtcon;
368
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530369 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530371 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200374static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100375{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530376 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
377
378 spin_lock(&wdt->lock);
379 __s3c2410wdt_stop(wdt);
380 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200381
382 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100383}
384
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200385static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530388 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530390 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100391
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530392 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530394 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
396
397 if (soft_noboot) {
398 wtcon |= S3C2410_WTCON_INTEN;
399 wtcon &= ~S3C2410_WTCON_RSTEN;
400 } else {
401 wtcon &= ~S3C2410_WTCON_INTEN;
402 wtcon |= S3C2410_WTCON_RSTEN;
403 }
404
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200405 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
406 wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530408 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
409 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
410 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
411 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200412
413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530416static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000417{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530418 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000419}
420
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200421static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
422 unsigned int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530424 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Sam Protsenkoe249d012021-11-21 18:56:45 +0200425 unsigned long freq = s3c2410wdt_get_freq(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned int count;
427 unsigned int divisor = 1;
428 unsigned long wtcon;
429
430 if (timeout < 1)
431 return -EINVAL;
432
Doug Anderson17862442013-11-26 16:57:19 -0800433 freq = DIV_ROUND_UP(freq, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 count = timeout * freq;
435
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200436 dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
437 count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* if the count is bigger than the watchdog register,
440 then work out what we need to do (and if) we can
441 actually make this value
442 */
443
444 if (count >= 0x10000) {
Doug Anderson17862442013-11-26 16:57:19 -0800445 divisor = DIV_ROUND_UP(count, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Doug Anderson17862442013-11-26 16:57:19 -0800447 if (divisor > 0x100) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530448 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EINVAL;
450 }
451 }
452
Krzysztof Kozlowski456f53d2017-02-24 23:07:40 +0200453 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
454 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Doug Anderson17862442013-11-26 16:57:19 -0800456 count = DIV_ROUND_UP(count, divisor);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530457 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530460 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
462 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
463
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530464 writel(count, wdt->reg_base + S3C2410_WTDAT);
465 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Hans de Goede5f2430f2012-05-11 12:00:27 +0200467 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470}
471
Guenter Roeck4d8b2292016-02-26 17:32:49 -0800472static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
473 void *data)
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500474{
475 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
476 void __iomem *wdt_base = wdt->reg_base;
477
478 /* disable watchdog, to be safe */
479 writel(0, wdt_base + S3C2410_WTCON);
480
481 /* put initial values into count and data */
482 writel(0x80, wdt_base + S3C2410_WTCNT);
483 writel(0x80, wdt_base + S3C2410_WTDAT);
484
485 /* set the watchdog to go and reset... */
486 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
487 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
488 wdt_base + S3C2410_WTCON);
489
490 /* wait for reset to assert... */
491 mdelay(500);
492
493 return 0;
494}
495
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000496#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Alan Cox41dc8b72008-08-04 17:54:46 +0100498static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .options = OPTIONS,
500 .firmware_version = 0,
501 .identity = "S3C2410 Watchdog",
502};
503
Bhumika Goyalb893e342017-01-28 13:11:17 +0530504static const struct watchdog_ops s3c2410wdt_ops = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200505 .owner = THIS_MODULE,
506 .start = s3c2410wdt_start,
507 .stop = s3c2410wdt_stop,
508 .ping = s3c2410wdt_keepalive,
509 .set_timeout = s3c2410wdt_set_heartbeat,
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500510 .restart = s3c2410wdt_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200513static const struct watchdog_device s3c2410_wdd = {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200514 .info = &s3c2410_wdt_ident,
515 .ops = &s3c2410wdt_ops,
Krzysztof Kozlowski4f211952017-02-24 17:11:15 +0200516 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517};
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* interrupt handler code */
520
David Howells7d12e782006-10-05 14:55:46 +0100521static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530523 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530525 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
526
527 s3c2410wdt_keepalive(&wdt->wdt_device);
Krzysztof Kozlowski0b445542017-02-24 17:11:16 +0200528
529 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
530 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return IRQ_HANDLED;
533}
Ben Dookse02f8382009-10-30 00:30:25 +0000534
Doug Anderson0f1dd982013-11-25 15:36:43 -0800535#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dookse02f8382009-10-30 00:30:25 +0000536
537static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
538 unsigned long val, void *data)
539{
540 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530541 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000542
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530543 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000544 goto done;
545
546 if (val == CPUFREQ_PRECHANGE) {
547 /* To ensure that over the change we don't cause the
548 * watchdog to trigger, we perform an keep-alive if
549 * the watchdog is running.
550 */
551
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530552 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000553 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530554 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000555
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530556 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
557 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000558
559 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530560 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000561 else
562 goto err;
563 }
564
565done:
566 return 0;
567
568 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530569 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
570 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000571 return ret;
572}
573
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530574static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000575{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530576 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
577
578 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000579 CPUFREQ_TRANSITION_NOTIFIER);
580}
581
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530582static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000583{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530584 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
585
586 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000587 CPUFREQ_TRANSITION_NOTIFIER);
588}
589
590#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530591
592static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000593{
594 return 0;
595}
596
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530597static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000598{
599}
600#endif
601
Doug Andersoncffc9a62013-12-06 13:08:07 -0800602static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
603{
604 unsigned int rst_stat;
605 int ret;
606
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200607 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_RST_STAT))
Doug Andersoncffc9a62013-12-06 13:08:07 -0800608 return 0;
609
610 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
611 if (ret)
612 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
613 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
614 return WDIOF_CARDRESET;
615
616 return 0;
617}
618
Krzysztof Kozlowski58415ef2017-03-13 21:07:24 +0200619static inline const struct s3c2410_wdt_variant *
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200620s3c2410_get_wdt_drv_data(struct platform_device *pdev)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530621{
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200622 const struct s3c2410_wdt_variant *variant;
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200623 struct device *dev = &pdev->dev;
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200624
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200625 variant = of_device_get_match_data(dev);
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200626 if (!variant) {
627 /* Device matched by platform_device_id */
628 variant = (struct s3c2410_wdt_variant *)
629 platform_get_device_id(pdev)->driver_data;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530630 }
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200631
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200632#ifdef CONFIG_OF
633 /* Choose Exynos850 driver data w.r.t. cluster index */
634 if (variant == &drv_data_exynos850_cl0) {
635 u32 index;
636 int err;
637
638 err = of_property_read_u32(dev->of_node,
639 "samsung,cluster-index", &index);
640 if (err) {
641 dev_err(dev, "failed to get cluster index\n");
642 return NULL;
643 }
644
645 switch (index) {
646 case 0:
647 return &drv_data_exynos850_cl0;
648 case 1:
649 return &drv_data_exynos850_cl1;
650 default:
651 dev_err(dev, "wrong cluster index: %u\n", index);
652 return NULL;
653 }
654 }
655#endif
656
Krzysztof Kozlowskia9a02c42017-03-13 21:07:25 +0200657 return variant;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530658}
659
Bill Pemberton2d991a12012-11-19 13:21:41 -0500660static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200662 struct device *dev = &pdev->dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530663 struct s3c2410_wdt *wdt;
Ben Dooks46b814d2007-06-14 12:08:54 +0100664 unsigned int wtcon;
Lad Prabhakara51f5892021-12-16 21:47:47 +0000665 int wdt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530668 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
669 if (!wdt)
670 return -ENOMEM;
671
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200672 wdt->dev = dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530673 spin_lock_init(&wdt->lock);
674 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Krzysztof Kozlowskie3a60ea2017-02-24 23:07:43 +0200676 wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
Sam Protsenkocd4eadf2021-11-21 18:56:47 +0200677 if (!wdt->drv_data)
678 return -EINVAL;
679
Doug Andersoncffc9a62013-12-06 13:08:07 -0800680 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530681 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
682 "samsung,syscon-phandle");
683 if (IS_ERR(wdt->pmureg)) {
684 dev_err(dev, "syscon regmap lookup failed.\n");
685 return PTR_ERR(wdt->pmureg);
686 }
687 }
688
Lad Prabhakara51f5892021-12-16 21:47:47 +0000689 wdt_irq = platform_get_irq(pdev, 0);
690 if (wdt_irq < 0)
691 return wdt_irq;
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900692
693 /* get the memory region for the watchdog timer */
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700694 wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
Sam Protsenko1a47cda2021-11-21 18:56:46 +0200695 if (IS_ERR(wdt->reg_base))
696 return PTR_ERR(wdt->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Sam Protsenkoe249d012021-11-21 18:56:45 +0200698 wdt->bus_clk = devm_clk_get(dev, "watchdog");
699 if (IS_ERR(wdt->bus_clk)) {
700 dev_err(dev, "failed to find bus clock\n");
Sam Protsenko1a47cda2021-11-21 18:56:46 +0200701 return PTR_ERR(wdt->bus_clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Sam Protsenkoe249d012021-11-21 18:56:45 +0200704 ret = clk_prepare_enable(wdt->bus_clk);
Sachin Kamat01b6af92014-03-04 15:04:35 +0530705 if (ret < 0) {
Sam Protsenkoe249d012021-11-21 18:56:45 +0200706 dev_err(dev, "failed to enable bus clock\n");
Sachin Kamat01b6af92014-03-04 15:04:35 +0530707 return ret;
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Sam Protsenkoe249d012021-11-21 18:56:45 +0200710 /*
711 * "watchdog_src" clock is optional; if it's not present -- just skip it
712 * and use "watchdog" clock as both bus and source clock.
713 */
Sam Protsenkof7bcb022021-12-12 19:02:47 +0200714 wdt->src_clk = devm_clk_get_optional(dev, "watchdog_src");
715 if (IS_ERR(wdt->src_clk)) {
716 dev_err_probe(dev, PTR_ERR(wdt->src_clk),
717 "failed to get source clock\n");
718 ret = PTR_ERR(wdt->src_clk);
719 goto err_bus_clk;
720 }
721
722 ret = clk_prepare_enable(wdt->src_clk);
723 if (ret) {
724 dev_err(dev, "failed to enable source clock\n");
725 goto err_bus_clk;
Sam Protsenkoe249d012021-11-21 18:56:45 +0200726 }
727
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300728 wdt->wdt_device.min_timeout = 1;
Sam Protsenkoe249d012021-11-21 18:56:45 +0200729 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt);
Javier Martinez Canillas882dec12016-03-01 13:45:17 -0300730
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530731 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900732 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900733 dev_err(dev, "failed to register cpufreq\n");
Sam Protsenkoe249d012021-11-21 18:56:45 +0200734 goto err_src_clk;
Ben Dookse02f8382009-10-30 00:30:25 +0000735 }
736
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530737 watchdog_set_drvdata(&wdt->wdt_device, wdt);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* see if we can actually set the requested timer margin, and if
740 * not, try the default value */
741
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200742 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530743 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
744 wdt->wdt_device.timeout);
745 if (ret) {
Sam Protsenkof197d472021-11-21 18:56:38 +0200746 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
747 S3C2410_WATCHDOG_DEFAULT_TIME);
748 if (ret == 0) {
749 dev_warn(dev, "tmr_margin value out of range, default %d used\n",
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200750 S3C2410_WATCHDOG_DEFAULT_TIME);
Sam Protsenkof197d472021-11-21 18:56:38 +0200751 } else {
752 dev_err(dev, "failed to use default timeout\n");
753 goto err_cpufreq;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Lad Prabhakara51f5892021-12-16 21:47:47 +0000757 ret = devm_request_irq(dev, wdt_irq, s3c2410wdt_irq, 0,
758 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900759 if (ret != 0) {
760 dev_err(dev, "failed to install irq (%d)\n", ret);
761 goto err_cpufreq;
762 }
763
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530764 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Damien Riegelc71f5cd2015-11-16 12:28:10 -0500765 watchdog_set_restart_priority(&wdt->wdt_device, 128);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100766
Doug Andersoncffc9a62013-12-06 13:08:07 -0800767 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200768 wdt->wdt_device.parent = dev;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800769
Sam Protsenkoa90102e2021-11-21 18:56:39 +0200770 /*
771 * If "tmr_atboot" param is non-zero, start the watchdog right now. Also
772 * set WDOG_HW_RUNNING bit, so that watchdog core can kick the watchdog.
773 *
774 * If we're not enabling the watchdog, then ensure it is disabled if it
775 * has been left running from the bootloader or other source.
776 */
777 if (tmr_atboot) {
778 dev_info(dev, "starting watchdog timer\n");
779 s3c2410wdt_start(&wdt->wdt_device);
780 set_bit(WDOG_HW_RUNNING, &wdt->wdt_device.status);
781 } else {
782 s3c2410wdt_stop(&wdt->wdt_device);
783 }
784
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530785 ret = watchdog_register_device(&wdt->wdt_device);
Wolfram Sang386f4652019-05-18 23:27:50 +0200786 if (ret)
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900787 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200789 ret = s3c2410wdt_enable(wdt, true);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530790 if (ret < 0)
791 goto err_unregister;
792
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530793 platform_set_drvdata(pdev, wdt);
794
Ben Dooks46b814d2007-06-14 12:08:54 +0100795 /* print out a statement of readiness */
796
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530797 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100798
Ben Dookse8ef92b2007-06-14 12:08:55 +0100799 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100800 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400801 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
802 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000805
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530806 err_unregister:
807 watchdog_unregister_device(&wdt->wdt_device);
808
Ben Dookse02f8382009-10-30 00:30:25 +0000809 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530810 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000811
Sam Protsenkoe249d012021-11-21 18:56:45 +0200812 err_src_clk:
813 clk_disable_unprepare(wdt->src_clk);
814
815 err_bus_clk:
816 clk_disable_unprepare(wdt->bus_clk);
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000817
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Bill Pemberton4b12b892012-11-19 13:26:24 -0500821static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530823 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530824 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000825
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200826 ret = s3c2410wdt_enable(wdt, false);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530827 if (ret < 0)
828 return ret;
829
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530830 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000831
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530832 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Sam Protsenkoe249d012021-11-21 18:56:45 +0200834 clk_disable_unprepare(wdt->src_clk);
835 clk_disable_unprepare(wdt->bus_clk);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838}
839
Russell King3ae5eae2005-11-09 22:32:44 +0000840static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200841{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530842 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
843
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200844 s3c2410wdt_enable(wdt, false);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530845 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200846}
847
Jingoo Han0183984c2013-03-14 10:31:21 +0900848#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200849
Jingoo Han0183984c2013-03-14 10:31:21 +0900850static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200851{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530852 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530853 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
854
Russell King9480e302005-10-28 09:52:56 -0700855 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530856 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
857 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200858
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200859 ret = s3c2410wdt_enable(wdt, false);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530860 if (ret < 0)
861 return ret;
862
Russell King9480e302005-10-28 09:52:56 -0700863 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530864 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200865
866 return 0;
867}
868
Jingoo Han0183984c2013-03-14 10:31:21 +0900869static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200870{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530871 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530872 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200873
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530874 /* Restore watchdog state. */
875 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
876 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
877 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200878
Sam Protsenkocf3fad42021-11-24 01:26:13 +0200879 ret = s3c2410wdt_enable(wdt, true);
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530880 if (ret < 0)
881 return ret;
882
Jingoo Han0183984c2013-03-14 10:31:21 +0900883 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530884 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200885
886 return 0;
887}
Jingoo Han0183984c2013-03-14 10:31:21 +0900888#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200889
Jingoo Han0183984c2013-03-14 10:31:21 +0900890static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
891 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200892
Russell King3ae5eae2005-11-09 22:32:44 +0000893static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500895 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200896 .shutdown = s3c2410wdt_shutdown,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530897 .id_table = s3c2410_wdt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +0000898 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000899 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900900 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000901 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000902 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903};
904
Sachin Kamat6b761b22012-07-12 17:17:40 +0530905module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Krzysztof Kozlowski08497f22017-03-13 21:07:26 +0200907MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
909MODULE_LICENSE("GPL");