xref: /OK3568_Linux_fs/kernel/drivers/watchdog/s3c2410_wdt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2004 Simtec Electronics
4*4882a593Smuzhiyun  *	Ben Dooks <ben@simtec.co.uk>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * S3C2410 Watchdog Timer Support
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Based on, softdog.c by Alan Cox,
9*4882a593Smuzhiyun  *     (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/moduleparam.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun #include <linux/timer.h>
16*4882a593Smuzhiyun #include <linux/watchdog.h>
17*4882a593Smuzhiyun #include <linux/platform_device.h>
18*4882a593Smuzhiyun #include <linux/interrupt.h>
19*4882a593Smuzhiyun #include <linux/clk.h>
20*4882a593Smuzhiyun #include <linux/uaccess.h>
21*4882a593Smuzhiyun #include <linux/io.h>
22*4882a593Smuzhiyun #include <linux/cpufreq.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/err.h>
25*4882a593Smuzhiyun #include <linux/of.h>
26*4882a593Smuzhiyun #include <linux/of_device.h>
27*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
28*4882a593Smuzhiyun #include <linux/regmap.h>
29*4882a593Smuzhiyun #include <linux/delay.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define S3C2410_WTCON		0x00
32*4882a593Smuzhiyun #define S3C2410_WTDAT		0x04
33*4882a593Smuzhiyun #define S3C2410_WTCNT		0x08
34*4882a593Smuzhiyun #define S3C2410_WTCLRINT	0x0c
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define S3C2410_WTCNT_MAXCNT	0xffff
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define S3C2410_WTCON_RSTEN	(1 << 0)
39*4882a593Smuzhiyun #define S3C2410_WTCON_INTEN	(1 << 2)
40*4882a593Smuzhiyun #define S3C2410_WTCON_ENABLE	(1 << 5)
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define S3C2410_WTCON_DIV16	(0 << 3)
43*4882a593Smuzhiyun #define S3C2410_WTCON_DIV32	(1 << 3)
44*4882a593Smuzhiyun #define S3C2410_WTCON_DIV64	(2 << 3)
45*4882a593Smuzhiyun #define S3C2410_WTCON_DIV128	(3 << 3)
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define S3C2410_WTCON_MAXDIV	0x80
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define S3C2410_WTCON_PRESCALE(x)	((x) << 8)
50*4882a593Smuzhiyun #define S3C2410_WTCON_PRESCALE_MASK	(0xff << 8)
51*4882a593Smuzhiyun #define S3C2410_WTCON_PRESCALE_MAX	0xff
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define S3C2410_WATCHDOG_ATBOOT		(0)
54*4882a593Smuzhiyun #define S3C2410_WATCHDOG_DEFAULT_TIME	(15)
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define EXYNOS5_RST_STAT_REG_OFFSET		0x0404
57*4882a593Smuzhiyun #define EXYNOS5_WDT_DISABLE_REG_OFFSET		0x0408
58*4882a593Smuzhiyun #define EXYNOS5_WDT_MASK_RESET_REG_OFFSET	0x040c
59*4882a593Smuzhiyun #define QUIRK_HAS_PMU_CONFIG			(1 << 0)
60*4882a593Smuzhiyun #define QUIRK_HAS_RST_STAT			(1 << 1)
61*4882a593Smuzhiyun #define QUIRK_HAS_WTCLRINT_REG			(1 << 2)
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* These quirks require that we have a PMU register map */
64*4882a593Smuzhiyun #define QUIRKS_HAVE_PMUREG			(QUIRK_HAS_PMU_CONFIG | \
65*4882a593Smuzhiyun 						 QUIRK_HAS_RST_STAT)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static bool nowayout	= WATCHDOG_NOWAYOUT;
68*4882a593Smuzhiyun static int tmr_margin;
69*4882a593Smuzhiyun static int tmr_atboot	= S3C2410_WATCHDOG_ATBOOT;
70*4882a593Smuzhiyun static int soft_noboot;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun module_param(tmr_margin,  int, 0);
73*4882a593Smuzhiyun module_param(tmr_atboot,  int, 0);
74*4882a593Smuzhiyun module_param(nowayout,   bool, 0);
75*4882a593Smuzhiyun module_param(soft_noboot, int, 0);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
78*4882a593Smuzhiyun 		__MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
79*4882a593Smuzhiyun MODULE_PARM_DESC(tmr_atboot,
80*4882a593Smuzhiyun 		"Watchdog is started at boot time if set to 1, default="
81*4882a593Smuzhiyun 			__MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
82*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
83*4882a593Smuzhiyun 			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
84*4882a593Smuzhiyun MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * struct s3c2410_wdt_variant - Per-variant config data
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * @disable_reg: Offset in pmureg for the register that disables the watchdog
90*4882a593Smuzhiyun  * timer reset functionality.
91*4882a593Smuzhiyun  * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
92*4882a593Smuzhiyun  * timer reset functionality.
93*4882a593Smuzhiyun  * @mask_bit: Bit number for the watchdog timer in the disable register and the
94*4882a593Smuzhiyun  * mask reset register.
95*4882a593Smuzhiyun  * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
96*4882a593Smuzhiyun  * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
97*4882a593Smuzhiyun  * reset.
98*4882a593Smuzhiyun  * @quirks: A bitfield of quirks.
99*4882a593Smuzhiyun  */
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun struct s3c2410_wdt_variant {
102*4882a593Smuzhiyun 	int disable_reg;
103*4882a593Smuzhiyun 	int mask_reset_reg;
104*4882a593Smuzhiyun 	int mask_bit;
105*4882a593Smuzhiyun 	int rst_stat_reg;
106*4882a593Smuzhiyun 	int rst_stat_bit;
107*4882a593Smuzhiyun 	u32 quirks;
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun struct s3c2410_wdt {
111*4882a593Smuzhiyun 	struct device		*dev;
112*4882a593Smuzhiyun 	struct clk		*clock;
113*4882a593Smuzhiyun 	void __iomem		*reg_base;
114*4882a593Smuzhiyun 	unsigned int		count;
115*4882a593Smuzhiyun 	spinlock_t		lock;
116*4882a593Smuzhiyun 	unsigned long		wtcon_save;
117*4882a593Smuzhiyun 	unsigned long		wtdat_save;
118*4882a593Smuzhiyun 	struct watchdog_device	wdt_device;
119*4882a593Smuzhiyun 	struct notifier_block	freq_transition;
120*4882a593Smuzhiyun 	const struct s3c2410_wdt_variant *drv_data;
121*4882a593Smuzhiyun 	struct regmap *pmureg;
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
125*4882a593Smuzhiyun 	.quirks = 0
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #ifdef CONFIG_OF
129*4882a593Smuzhiyun static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
130*4882a593Smuzhiyun 	.quirks = QUIRK_HAS_WTCLRINT_REG,
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static const struct s3c2410_wdt_variant drv_data_exynos5250  = {
134*4882a593Smuzhiyun 	.disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
135*4882a593Smuzhiyun 	.mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
136*4882a593Smuzhiyun 	.mask_bit = 20,
137*4882a593Smuzhiyun 	.rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
138*4882a593Smuzhiyun 	.rst_stat_bit = 20,
139*4882a593Smuzhiyun 	.quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
140*4882a593Smuzhiyun 		  | QUIRK_HAS_WTCLRINT_REG,
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
144*4882a593Smuzhiyun 	.disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
145*4882a593Smuzhiyun 	.mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
146*4882a593Smuzhiyun 	.mask_bit = 0,
147*4882a593Smuzhiyun 	.rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
148*4882a593Smuzhiyun 	.rst_stat_bit = 9,
149*4882a593Smuzhiyun 	.quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
150*4882a593Smuzhiyun 		  | QUIRK_HAS_WTCLRINT_REG,
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun static const struct s3c2410_wdt_variant drv_data_exynos7 = {
154*4882a593Smuzhiyun 	.disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
155*4882a593Smuzhiyun 	.mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
156*4882a593Smuzhiyun 	.mask_bit = 23,
157*4882a593Smuzhiyun 	.rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
158*4882a593Smuzhiyun 	.rst_stat_bit = 23,	/* A57 WDTRESET */
159*4882a593Smuzhiyun 	.quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
160*4882a593Smuzhiyun 		  | QUIRK_HAS_WTCLRINT_REG,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun static const struct of_device_id s3c2410_wdt_match[] = {
164*4882a593Smuzhiyun 	{ .compatible = "samsung,s3c2410-wdt",
165*4882a593Smuzhiyun 	  .data = &drv_data_s3c2410 },
166*4882a593Smuzhiyun 	{ .compatible = "samsung,s3c6410-wdt",
167*4882a593Smuzhiyun 	  .data = &drv_data_s3c6410 },
168*4882a593Smuzhiyun 	{ .compatible = "samsung,exynos5250-wdt",
169*4882a593Smuzhiyun 	  .data = &drv_data_exynos5250 },
170*4882a593Smuzhiyun 	{ .compatible = "samsung,exynos5420-wdt",
171*4882a593Smuzhiyun 	  .data = &drv_data_exynos5420 },
172*4882a593Smuzhiyun 	{ .compatible = "samsung,exynos7-wdt",
173*4882a593Smuzhiyun 	  .data = &drv_data_exynos7 },
174*4882a593Smuzhiyun 	{},
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun static const struct platform_device_id s3c2410_wdt_ids[] = {
180*4882a593Smuzhiyun 	{
181*4882a593Smuzhiyun 		.name = "s3c2410-wdt",
182*4882a593Smuzhiyun 		.driver_data = (unsigned long)&drv_data_s3c2410,
183*4882a593Smuzhiyun 	},
184*4882a593Smuzhiyun 	{}
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /* functions */
189*4882a593Smuzhiyun 
s3c2410wdt_max_timeout(struct clk * clock)190*4882a593Smuzhiyun static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	unsigned long freq = clk_get_rate(clock);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
195*4882a593Smuzhiyun 				       / S3C2410_WTCON_MAXDIV);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
freq_to_wdt(struct notifier_block * nb)198*4882a593Smuzhiyun static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	return container_of(nb, struct s3c2410_wdt, freq_transition);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt * wdt,bool mask)203*4882a593Smuzhiyun static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	int ret;
206*4882a593Smuzhiyun 	u32 mask_val = 1 << wdt->drv_data->mask_bit;
207*4882a593Smuzhiyun 	u32 val = 0;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* No need to do anything if no PMU CONFIG needed */
210*4882a593Smuzhiyun 	if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
211*4882a593Smuzhiyun 		return 0;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (mask)
214*4882a593Smuzhiyun 		val = mask_val;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	ret = regmap_update_bits(wdt->pmureg,
217*4882a593Smuzhiyun 			wdt->drv_data->disable_reg,
218*4882a593Smuzhiyun 			mask_val, val);
219*4882a593Smuzhiyun 	if (ret < 0)
220*4882a593Smuzhiyun 		goto error;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ret = regmap_update_bits(wdt->pmureg,
223*4882a593Smuzhiyun 			wdt->drv_data->mask_reset_reg,
224*4882a593Smuzhiyun 			mask_val, val);
225*4882a593Smuzhiyun  error:
226*4882a593Smuzhiyun 	if (ret < 0)
227*4882a593Smuzhiyun 		dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return ret;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
s3c2410wdt_keepalive(struct watchdog_device * wdd)232*4882a593Smuzhiyun static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	spin_lock(&wdt->lock);
237*4882a593Smuzhiyun 	writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
238*4882a593Smuzhiyun 	spin_unlock(&wdt->lock);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	return 0;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
__s3c2410wdt_stop(struct s3c2410_wdt * wdt)243*4882a593Smuzhiyun static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	unsigned long wtcon;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	wtcon = readl(wdt->reg_base + S3C2410_WTCON);
248*4882a593Smuzhiyun 	wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
249*4882a593Smuzhiyun 	writel(wtcon, wdt->reg_base + S3C2410_WTCON);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
s3c2410wdt_stop(struct watchdog_device * wdd)252*4882a593Smuzhiyun static int s3c2410wdt_stop(struct watchdog_device *wdd)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	spin_lock(&wdt->lock);
257*4882a593Smuzhiyun 	__s3c2410wdt_stop(wdt);
258*4882a593Smuzhiyun 	spin_unlock(&wdt->lock);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	return 0;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
s3c2410wdt_start(struct watchdog_device * wdd)263*4882a593Smuzhiyun static int s3c2410wdt_start(struct watchdog_device *wdd)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	unsigned long wtcon;
266*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	spin_lock(&wdt->lock);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	__s3c2410wdt_stop(wdt);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	wtcon = readl(wdt->reg_base + S3C2410_WTCON);
273*4882a593Smuzhiyun 	wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (soft_noboot) {
276*4882a593Smuzhiyun 		wtcon |= S3C2410_WTCON_INTEN;
277*4882a593Smuzhiyun 		wtcon &= ~S3C2410_WTCON_RSTEN;
278*4882a593Smuzhiyun 	} else {
279*4882a593Smuzhiyun 		wtcon &= ~S3C2410_WTCON_INTEN;
280*4882a593Smuzhiyun 		wtcon |= S3C2410_WTCON_RSTEN;
281*4882a593Smuzhiyun 	}
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
284*4882a593Smuzhiyun 		wdt->count, wtcon);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
287*4882a593Smuzhiyun 	writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
288*4882a593Smuzhiyun 	writel(wtcon, wdt->reg_base + S3C2410_WTCON);
289*4882a593Smuzhiyun 	spin_unlock(&wdt->lock);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	return 0;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
s3c2410wdt_is_running(struct s3c2410_wdt * wdt)294*4882a593Smuzhiyun static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
s3c2410wdt_set_heartbeat(struct watchdog_device * wdd,unsigned int timeout)299*4882a593Smuzhiyun static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
300*4882a593Smuzhiyun 				    unsigned int timeout)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
303*4882a593Smuzhiyun 	unsigned long freq = clk_get_rate(wdt->clock);
304*4882a593Smuzhiyun 	unsigned int count;
305*4882a593Smuzhiyun 	unsigned int divisor = 1;
306*4882a593Smuzhiyun 	unsigned long wtcon;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (timeout < 1)
309*4882a593Smuzhiyun 		return -EINVAL;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	freq = DIV_ROUND_UP(freq, 128);
312*4882a593Smuzhiyun 	count = timeout * freq;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
315*4882a593Smuzhiyun 		count, timeout, freq);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* if the count is bigger than the watchdog register,
318*4882a593Smuzhiyun 	   then work out what we need to do (and if) we can
319*4882a593Smuzhiyun 	   actually make this value
320*4882a593Smuzhiyun 	*/
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (count >= 0x10000) {
323*4882a593Smuzhiyun 		divisor = DIV_ROUND_UP(count, 0xffff);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		if (divisor > 0x100) {
326*4882a593Smuzhiyun 			dev_err(wdt->dev, "timeout %d too big\n", timeout);
327*4882a593Smuzhiyun 			return -EINVAL;
328*4882a593Smuzhiyun 		}
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
332*4882a593Smuzhiyun 		timeout, divisor, count, DIV_ROUND_UP(count, divisor));
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	count = DIV_ROUND_UP(count, divisor);
335*4882a593Smuzhiyun 	wdt->count = count;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* update the pre-scaler */
338*4882a593Smuzhiyun 	wtcon = readl(wdt->reg_base + S3C2410_WTCON);
339*4882a593Smuzhiyun 	wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
340*4882a593Smuzhiyun 	wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	writel(count, wdt->reg_base + S3C2410_WTDAT);
343*4882a593Smuzhiyun 	writel(wtcon, wdt->reg_base + S3C2410_WTCON);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	wdd->timeout = (count * divisor) / freq;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
s3c2410wdt_restart(struct watchdog_device * wdd,unsigned long action,void * data)350*4882a593Smuzhiyun static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
351*4882a593Smuzhiyun 			      void *data)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
354*4882a593Smuzhiyun 	void __iomem *wdt_base = wdt->reg_base;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* disable watchdog, to be safe  */
357*4882a593Smuzhiyun 	writel(0, wdt_base + S3C2410_WTCON);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	/* put initial values into count and data */
360*4882a593Smuzhiyun 	writel(0x80, wdt_base + S3C2410_WTCNT);
361*4882a593Smuzhiyun 	writel(0x80, wdt_base + S3C2410_WTDAT);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	/* set the watchdog to go and reset... */
364*4882a593Smuzhiyun 	writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
365*4882a593Smuzhiyun 		S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
366*4882a593Smuzhiyun 		wdt_base + S3C2410_WTCON);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	/* wait for reset to assert... */
369*4882a593Smuzhiyun 	mdelay(500);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun static const struct watchdog_info s3c2410_wdt_ident = {
377*4882a593Smuzhiyun 	.options          =     OPTIONS,
378*4882a593Smuzhiyun 	.firmware_version =	0,
379*4882a593Smuzhiyun 	.identity         =	"S3C2410 Watchdog",
380*4882a593Smuzhiyun };
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun static const struct watchdog_ops s3c2410wdt_ops = {
383*4882a593Smuzhiyun 	.owner = THIS_MODULE,
384*4882a593Smuzhiyun 	.start = s3c2410wdt_start,
385*4882a593Smuzhiyun 	.stop = s3c2410wdt_stop,
386*4882a593Smuzhiyun 	.ping = s3c2410wdt_keepalive,
387*4882a593Smuzhiyun 	.set_timeout = s3c2410wdt_set_heartbeat,
388*4882a593Smuzhiyun 	.restart = s3c2410wdt_restart,
389*4882a593Smuzhiyun };
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun static const struct watchdog_device s3c2410_wdd = {
392*4882a593Smuzhiyun 	.info = &s3c2410_wdt_ident,
393*4882a593Smuzhiyun 	.ops = &s3c2410wdt_ops,
394*4882a593Smuzhiyun 	.timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
395*4882a593Smuzhiyun };
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /* interrupt handler code */
398*4882a593Smuzhiyun 
s3c2410wdt_irq(int irqno,void * param)399*4882a593Smuzhiyun static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = platform_get_drvdata(param);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	dev_info(wdt->dev, "watchdog timer expired (irq)\n");
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	s3c2410wdt_keepalive(&wdt->wdt_device);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
408*4882a593Smuzhiyun 		writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	return IRQ_HANDLED;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
414*4882a593Smuzhiyun 
s3c2410wdt_cpufreq_transition(struct notifier_block * nb,unsigned long val,void * data)415*4882a593Smuzhiyun static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
416*4882a593Smuzhiyun 					  unsigned long val, void *data)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	int ret;
419*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = freq_to_wdt(nb);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	if (!s3c2410wdt_is_running(wdt))
422*4882a593Smuzhiyun 		goto done;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	if (val == CPUFREQ_PRECHANGE) {
425*4882a593Smuzhiyun 		/* To ensure that over the change we don't cause the
426*4882a593Smuzhiyun 		 * watchdog to trigger, we perform an keep-alive if
427*4882a593Smuzhiyun 		 * the watchdog is running.
428*4882a593Smuzhiyun 		 */
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		s3c2410wdt_keepalive(&wdt->wdt_device);
431*4882a593Smuzhiyun 	} else if (val == CPUFREQ_POSTCHANGE) {
432*4882a593Smuzhiyun 		s3c2410wdt_stop(&wdt->wdt_device);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
435*4882a593Smuzhiyun 						wdt->wdt_device.timeout);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 		if (ret >= 0)
438*4882a593Smuzhiyun 			s3c2410wdt_start(&wdt->wdt_device);
439*4882a593Smuzhiyun 		else
440*4882a593Smuzhiyun 			goto err;
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun done:
444*4882a593Smuzhiyun 	return 0;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun  err:
447*4882a593Smuzhiyun 	dev_err(wdt->dev, "cannot set new value for timeout %d\n",
448*4882a593Smuzhiyun 				wdt->wdt_device.timeout);
449*4882a593Smuzhiyun 	return ret;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun 
s3c2410wdt_cpufreq_register(struct s3c2410_wdt * wdt)452*4882a593Smuzhiyun static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	return cpufreq_register_notifier(&wdt->freq_transition,
457*4882a593Smuzhiyun 					 CPUFREQ_TRANSITION_NOTIFIER);
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt * wdt)460*4882a593Smuzhiyun static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	cpufreq_unregister_notifier(&wdt->freq_transition,
465*4882a593Smuzhiyun 				    CPUFREQ_TRANSITION_NOTIFIER);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun #else
469*4882a593Smuzhiyun 
s3c2410wdt_cpufreq_register(struct s3c2410_wdt * wdt)470*4882a593Smuzhiyun static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	return 0;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun 
s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt * wdt)475*4882a593Smuzhiyun static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun #endif
479*4882a593Smuzhiyun 
s3c2410wdt_get_bootstatus(struct s3c2410_wdt * wdt)480*4882a593Smuzhiyun static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	unsigned int rst_stat;
483*4882a593Smuzhiyun 	int ret;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
486*4882a593Smuzhiyun 		return 0;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
489*4882a593Smuzhiyun 	if (ret)
490*4882a593Smuzhiyun 		dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
491*4882a593Smuzhiyun 	else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
492*4882a593Smuzhiyun 		return WDIOF_CARDRESET;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	return 0;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun static inline const struct s3c2410_wdt_variant *
s3c2410_get_wdt_drv_data(struct platform_device * pdev)498*4882a593Smuzhiyun s3c2410_get_wdt_drv_data(struct platform_device *pdev)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	const struct s3c2410_wdt_variant *variant;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	variant = of_device_get_match_data(&pdev->dev);
503*4882a593Smuzhiyun 	if (!variant) {
504*4882a593Smuzhiyun 		/* Device matched by platform_device_id */
505*4882a593Smuzhiyun 		variant = (struct s3c2410_wdt_variant *)
506*4882a593Smuzhiyun 			   platform_get_device_id(pdev)->driver_data;
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	return variant;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
s3c2410wdt_probe(struct platform_device * pdev)512*4882a593Smuzhiyun static int s3c2410wdt_probe(struct platform_device *pdev)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
515*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt;
516*4882a593Smuzhiyun 	struct resource *wdt_irq;
517*4882a593Smuzhiyun 	unsigned int wtcon;
518*4882a593Smuzhiyun 	int started = 0;
519*4882a593Smuzhiyun 	int ret;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
522*4882a593Smuzhiyun 	if (!wdt)
523*4882a593Smuzhiyun 		return -ENOMEM;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	wdt->dev = dev;
526*4882a593Smuzhiyun 	spin_lock_init(&wdt->lock);
527*4882a593Smuzhiyun 	wdt->wdt_device = s3c2410_wdd;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
530*4882a593Smuzhiyun 	if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
531*4882a593Smuzhiyun 		wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
532*4882a593Smuzhiyun 						"samsung,syscon-phandle");
533*4882a593Smuzhiyun 		if (IS_ERR(wdt->pmureg)) {
534*4882a593Smuzhiyun 			dev_err(dev, "syscon regmap lookup failed.\n");
535*4882a593Smuzhiyun 			return PTR_ERR(wdt->pmureg);
536*4882a593Smuzhiyun 		}
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
540*4882a593Smuzhiyun 	if (wdt_irq == NULL) {
541*4882a593Smuzhiyun 		dev_err(dev, "no irq resource specified\n");
542*4882a593Smuzhiyun 		ret = -ENOENT;
543*4882a593Smuzhiyun 		goto err;
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	/* get the memory region for the watchdog timer */
547*4882a593Smuzhiyun 	wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
548*4882a593Smuzhiyun 	if (IS_ERR(wdt->reg_base)) {
549*4882a593Smuzhiyun 		ret = PTR_ERR(wdt->reg_base);
550*4882a593Smuzhiyun 		goto err;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	wdt->clock = devm_clk_get(dev, "watchdog");
554*4882a593Smuzhiyun 	if (IS_ERR(wdt->clock)) {
555*4882a593Smuzhiyun 		dev_err(dev, "failed to find watchdog clock source\n");
556*4882a593Smuzhiyun 		ret = PTR_ERR(wdt->clock);
557*4882a593Smuzhiyun 		goto err;
558*4882a593Smuzhiyun 	}
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	ret = clk_prepare_enable(wdt->clock);
561*4882a593Smuzhiyun 	if (ret < 0) {
562*4882a593Smuzhiyun 		dev_err(dev, "failed to enable clock\n");
563*4882a593Smuzhiyun 		return ret;
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	wdt->wdt_device.min_timeout = 1;
567*4882a593Smuzhiyun 	wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	ret = s3c2410wdt_cpufreq_register(wdt);
570*4882a593Smuzhiyun 	if (ret < 0) {
571*4882a593Smuzhiyun 		dev_err(dev, "failed to register cpufreq\n");
572*4882a593Smuzhiyun 		goto err_clk;
573*4882a593Smuzhiyun 	}
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	watchdog_set_drvdata(&wdt->wdt_device, wdt);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/* see if we can actually set the requested timer margin, and if
578*4882a593Smuzhiyun 	 * not, try the default value */
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
581*4882a593Smuzhiyun 	ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
582*4882a593Smuzhiyun 					wdt->wdt_device.timeout);
583*4882a593Smuzhiyun 	if (ret) {
584*4882a593Smuzhiyun 		started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
585*4882a593Smuzhiyun 					S3C2410_WATCHDOG_DEFAULT_TIME);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 		if (started == 0)
588*4882a593Smuzhiyun 			dev_info(dev,
589*4882a593Smuzhiyun 				 "tmr_margin value out of range, default %d used\n",
590*4882a593Smuzhiyun 				 S3C2410_WATCHDOG_DEFAULT_TIME);
591*4882a593Smuzhiyun 		else
592*4882a593Smuzhiyun 			dev_info(dev, "default timer value is out of range, cannot start\n");
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
596*4882a593Smuzhiyun 				pdev->name, pdev);
597*4882a593Smuzhiyun 	if (ret != 0) {
598*4882a593Smuzhiyun 		dev_err(dev, "failed to install irq (%d)\n", ret);
599*4882a593Smuzhiyun 		goto err_cpufreq;
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	watchdog_set_nowayout(&wdt->wdt_device, nowayout);
603*4882a593Smuzhiyun 	watchdog_set_restart_priority(&wdt->wdt_device, 128);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
606*4882a593Smuzhiyun 	wdt->wdt_device.parent = dev;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	ret = watchdog_register_device(&wdt->wdt_device);
609*4882a593Smuzhiyun 	if (ret)
610*4882a593Smuzhiyun 		goto err_cpufreq;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
613*4882a593Smuzhiyun 	if (ret < 0)
614*4882a593Smuzhiyun 		goto err_unregister;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	if (tmr_atboot && started == 0) {
617*4882a593Smuzhiyun 		dev_info(dev, "starting watchdog timer\n");
618*4882a593Smuzhiyun 		s3c2410wdt_start(&wdt->wdt_device);
619*4882a593Smuzhiyun 	} else if (!tmr_atboot) {
620*4882a593Smuzhiyun 		/* if we're not enabling the watchdog, then ensure it is
621*4882a593Smuzhiyun 		 * disabled if it has been left running from the bootloader
622*4882a593Smuzhiyun 		 * or other source */
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 		s3c2410wdt_stop(&wdt->wdt_device);
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	platform_set_drvdata(pdev, wdt);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/* print out a statement of readiness */
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	wtcon = readl(wdt->reg_base + S3C2410_WTCON);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
634*4882a593Smuzhiyun 		 (wtcon & S3C2410_WTCON_ENABLE) ?  "" : "in",
635*4882a593Smuzhiyun 		 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
636*4882a593Smuzhiyun 		 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	return 0;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun  err_unregister:
641*4882a593Smuzhiyun 	watchdog_unregister_device(&wdt->wdt_device);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun  err_cpufreq:
644*4882a593Smuzhiyun 	s3c2410wdt_cpufreq_deregister(wdt);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun  err_clk:
647*4882a593Smuzhiyun 	clk_disable_unprepare(wdt->clock);
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun  err:
650*4882a593Smuzhiyun 	return ret;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
s3c2410wdt_remove(struct platform_device * dev)653*4882a593Smuzhiyun static int s3c2410wdt_remove(struct platform_device *dev)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	int ret;
656*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
659*4882a593Smuzhiyun 	if (ret < 0)
660*4882a593Smuzhiyun 		return ret;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	watchdog_unregister_device(&wdt->wdt_device);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	s3c2410wdt_cpufreq_deregister(wdt);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	clk_disable_unprepare(wdt->clock);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	return 0;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
s3c2410wdt_shutdown(struct platform_device * dev)671*4882a593Smuzhiyun static void s3c2410wdt_shutdown(struct platform_device *dev)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	s3c2410wdt_mask_and_disable_reset(wdt, true);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	s3c2410wdt_stop(&wdt->wdt_device);
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
681*4882a593Smuzhiyun 
s3c2410wdt_suspend(struct device * dev)682*4882a593Smuzhiyun static int s3c2410wdt_suspend(struct device *dev)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	int ret;
685*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	/* Save watchdog state, and turn it off. */
688*4882a593Smuzhiyun 	wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
689*4882a593Smuzhiyun 	wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
692*4882a593Smuzhiyun 	if (ret < 0)
693*4882a593Smuzhiyun 		return ret;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	/* Note that WTCNT doesn't need to be saved. */
696*4882a593Smuzhiyun 	s3c2410wdt_stop(&wdt->wdt_device);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	return 0;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun 
s3c2410wdt_resume(struct device * dev)701*4882a593Smuzhiyun static int s3c2410wdt_resume(struct device *dev)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	int ret;
704*4882a593Smuzhiyun 	struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* Restore watchdog state. */
707*4882a593Smuzhiyun 	writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
708*4882a593Smuzhiyun 	writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
709*4882a593Smuzhiyun 	writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
712*4882a593Smuzhiyun 	if (ret < 0)
713*4882a593Smuzhiyun 		return ret;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	dev_info(dev, "watchdog %sabled\n",
716*4882a593Smuzhiyun 		(wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	return 0;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun #endif
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
723*4882a593Smuzhiyun 			s3c2410wdt_resume);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun static struct platform_driver s3c2410wdt_driver = {
726*4882a593Smuzhiyun 	.probe		= s3c2410wdt_probe,
727*4882a593Smuzhiyun 	.remove		= s3c2410wdt_remove,
728*4882a593Smuzhiyun 	.shutdown	= s3c2410wdt_shutdown,
729*4882a593Smuzhiyun 	.id_table	= s3c2410_wdt_ids,
730*4882a593Smuzhiyun 	.driver		= {
731*4882a593Smuzhiyun 		.name	= "s3c2410-wdt",
732*4882a593Smuzhiyun 		.pm	= &s3c2410wdt_pm_ops,
733*4882a593Smuzhiyun 		.of_match_table	= of_match_ptr(s3c2410_wdt_match),
734*4882a593Smuzhiyun 	},
735*4882a593Smuzhiyun };
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun module_platform_driver(s3c2410wdt_driver);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
740*4882a593Smuzhiyun MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
741*4882a593Smuzhiyun MODULE_LICENSE("GPL");
742