Debian Codegraph @

drivers/watchdog/s3c2410_wdt.c

raw
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2004 Simtec Electronics
4 * Ben Dooks <[email protected]>
5 *
6 * S3C2410 Watchdog Timer Support
7 *
8 * Based on, softdog.c by Alan Cox,
9 * (c) Copyright 1996 Alan Cox <[email protected]>
10 */
11
12 #include <linux/bits.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/timer.h>
17 #include <linux/watchdog.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/clk.h>
21 #include <linux/uaccess.h>
22 #include <linux/io.h>
23 #include <linux/cpufreq.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/of.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/regmap.h>
29 #include <linux/delay.h>
30 #include <linux/math64.h>
31
32 #define S3C2410_WTCON 0x00
33 #define S3C2410_WTDAT 0x04
34 #define S3C2410_WTCNT 0x08
35 #define S3C2410_WTCLRINT 0x0c
36
37 #define S3C2410_WTCNT_MAXCNT_16 0xffff
38 #define S3C2410_WTCNT_MAXCNT_32 0xffffffff
39
40 #define S3C2410_WTCON_RSTEN BIT(0)
41 #define S3C2410_WTCON_INTEN BIT(2)
42 #define S3C2410_WTCON_ENABLE BIT(5)
43 #define S3C2410_WTCON_DBGACK_MASK BIT(16)
44
45 #define S3C2410_WTCON_DIV16 (0 << 3)
46 #define S3C2410_WTCON_DIV32 (1 << 3)
47 #define S3C2410_WTCON_DIV64 (2 << 3)
48 #define S3C2410_WTCON_DIV128 (3 << 3)
49
50 #define S3C2410_WTCON_MAXDIV 0x80
51
52 #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
53 #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
54 #define S3C2410_WTCON_PRESCALE_MAX 0xff
55
56 #define S3C2410_WATCHDOG_ATBOOT (0)
57 #define S3C2410_WATCHDOG_DEFAULT_TIME (15)
58
59 #define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
60 #define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
61 #define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
62 #define EXYNOS850_CLUSTER0_NONCPU_OUT 0x1220
63 #define EXYNOS850_CLUSTER0_NONCPU_INT_EN 0x1244
64 #define EXYNOS850_CLUSTER1_NONCPU_OUT 0x1620
65 #define EXYNOS850_CLUSTER1_NONCPU_INT_EN 0x1644
66 #define EXYNOSAUTOV9_CLUSTER1_NONCPU_OUT 0x1520
67 #define EXYNOSAUTOV9_CLUSTER1_NONCPU_INT_EN 0x1544
68 #define EXYNOSAUTOV920_CLUSTER0_NONCPU_OUT 0x1420
69 #define EXYNOSAUTOV920_CLUSTER0_NONCPU_INT_EN 0x1444
70 #define EXYNOSAUTOV920_CLUSTER1_NONCPU_OUT 0x1720
71 #define EXYNOSAUTOV920_CLUSTER1_NONCPU_INT_EN 0x1744
72
73 #define EXYNOS850_CLUSTER0_WDTRESET_BIT 24
74 #define EXYNOS850_CLUSTER1_WDTRESET_BIT 23
75 #define EXYNOSAUTOV9_CLUSTER0_WDTRESET_BIT 25
76 #define EXYNOSAUTOV9_CLUSTER1_WDTRESET_BIT 24
77 #define EXYNOSAUTOV920_CLUSTER0_WDTRESET_BIT 0
78 #define EXYNOSAUTOV920_CLUSTER1_WDTRESET_BIT 1
79
80 #define GS_CLUSTER0_NONCPU_OUT 0x1220
81 #define GS_CLUSTER1_NONCPU_OUT 0x1420
82 #define GS_CLUSTER0_NONCPU_INT_EN 0x1244
83 #define GS_CLUSTER1_NONCPU_INT_EN 0x1444
84 #define GS_CLUSTER2_NONCPU_INT_EN 0x1644
85 #define GS_RST_STAT_REG_OFFSET 0x3B44
86
87 #define EXYNOS990_CLUSTER2_NONCPU_OUT 0x1620
88 #define EXYNOS990_CLUSTER2_NONCPU_INT_EN 0x1644
89 #define EXYNOS990_CLUSTER2_WDTRESET_BIT 23
90
91 /**
92 * DOC: Quirk flags for different Samsung watchdog IP-cores
93 *
94 * This driver supports multiple Samsung SoCs, each of which might have
95 * different set of registers and features supported. As watchdog block
96 * sometimes requires modifying PMU registers for proper functioning, register
97 * differences in both watchdog and PMU IP-cores should be accounted for. Quirk
98 * flags described below serve the purpose of telling the driver about mentioned
99 * SoC traits, and can be specified in driver data for each particular supported
100 * device.
101 *
102 * %QUIRK_HAS_WTCLRINT_REG: Watchdog block has WTCLRINT register. It's used to
103 * clear the interrupt once the interrupt service routine is complete. It's
104 * write-only, writing any values to this register clears the interrupt, but
105 * reading is not permitted.
106 *
107 * %QUIRK_HAS_PMU_MASK_RESET: PMU block has the register for disabling/enabling
108 * WDT reset request. On old SoCs it's usually called MASK_WDT_RESET_REQUEST,
109 * new SoCs have CLUSTERx_NONCPU_INT_EN register, which 'mask_bit' value is
110 * inverted compared to the former one.
111 *
112 * %QUIRK_HAS_PMU_RST_STAT: PMU block has RST_STAT (reset status) register,
113 * which contains bits indicating the reason for most recent CPU reset. If
114 * present, driver will use this register to check if previous reboot was due to
115 * watchdog timer reset.
116 *
117 * %QUIRK_HAS_PMU_AUTO_DISABLE: PMU block has AUTOMATIC_WDT_RESET_DISABLE
118 * register. If 'mask_bit' bit is set, PMU will disable WDT reset when
119 * corresponding processor is in reset state.
120 *
121 * %QUIRK_HAS_PMU_CNT_EN: PMU block has some register (e.g. CLUSTERx_NONCPU_OUT)
122 * with "watchdog counter enable" bit. That bit should be set to make watchdog
123 * counter running.
124 *
125 * %QUIRK_HAS_DBGACK_BIT: WTCON register has DBGACK_MASK bit. Setting the
126 * DBGACK_MASK bit disables the watchdog outputs when the SoC is in debug mode.
127 * Debug mode is determined by the DBGACK CPU signal.
128 *
129 * %QUIRK_HAS_32BIT_CNT: WTDAT and WTCNT are 32-bit registers. With these
130 * 32-bit registers, larger values will be set, which means that larger timeouts
131 * value can be set.
132 */
133 #define QUIRK_HAS_WTCLRINT_REG BIT(0)
134 #define QUIRK_HAS_PMU_MASK_RESET BIT(1)
135 #define QUIRK_HAS_PMU_RST_STAT BIT(2)
136 #define QUIRK_HAS_PMU_AUTO_DISABLE BIT(3)
137 #define QUIRK_HAS_PMU_CNT_EN BIT(4)
138 #define QUIRK_HAS_DBGACK_BIT BIT(5)
139 #define QUIRK_HAS_32BIT_CNT BIT(6)
140
141 /* These quirks require that we have a PMU register map */
142 #define QUIRKS_HAVE_PMUREG \
143 (QUIRK_HAS_PMU_MASK_RESET | QUIRK_HAS_PMU_RST_STAT | \
144 QUIRK_HAS_PMU_AUTO_DISABLE | QUIRK_HAS_PMU_CNT_EN)
145
146 static bool nowayout = WATCHDOG_NOWAYOUT;
147 static int tmr_margin;
148 static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
149 static int soft_noboot;
150
151 module_param(tmr_margin, int, 0);
152 module_param(tmr_atboot, int, 0);
153 module_param(nowayout, bool, 0);
154 module_param(soft_noboot, int, 0);
155
156 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
157 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
158 MODULE_PARM_DESC(tmr_atboot,
159 "Watchdog is started at boot time if set to 1, default="
160 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
161 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
162 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
163 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
164
165 /**
166 * struct s3c2410_wdt_variant - Per-variant config data
167 *
168 * @disable_reg: Offset in pmureg for the register that disables the watchdog
169 * timer reset functionality.
170 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
171 * timer reset functionality.
172 * @mask_reset_inv: If set, mask_reset_reg value will have inverted meaning.
173 * @mask_bit: Bit number for the watchdog timer in the disable register and the
174 * mask reset register.
175 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
176 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
177 * reset.
178 * @cnt_en_reg: Offset in pmureg for the register that enables WDT counter.
179 * @cnt_en_bit: Bit number for "watchdog counter enable" in cnt_en register.
180 * @quirks: A bitfield of quirks.
181 */
182
183 struct s3c2410_wdt_variant {
184 int disable_reg;
185 int mask_reset_reg;
186 bool mask_reset_inv;
187 int mask_bit;
188 int rst_stat_reg;
189 int rst_stat_bit;
190 int cnt_en_reg;
191 int cnt_en_bit;
192 u32 quirks;
193 };
194
195 struct s3c2410_wdt {
196 struct device *dev;
197 struct clk *bus_clk; /* for register interface (PCLK) */
198 struct clk *src_clk; /* for WDT counter */
199 void __iomem *reg_base;
200 unsigned int count;
201 spinlock_t lock;
202 unsigned long wtcon_save;
203 unsigned long wtdat_save;
204 struct watchdog_device wdt_device;
205 struct notifier_block freq_transition;
206 const struct s3c2410_wdt_variant *drv_data;
207 struct regmap *pmureg;
208 u32 max_cnt;
209 };
210
211 static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
212 .quirks = QUIRK_HAS_WTCLRINT_REG,
213 };
214
215 static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
216 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
217 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
218 .mask_bit = 20,
219 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
220 .rst_stat_bit = 20,
221 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
222 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
223 };
224
225 static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
226 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
227 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
228 .mask_bit = 0,
229 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
230 .rst_stat_bit = 9,
231 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
232 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
233 };
234
235 static const struct s3c2410_wdt_variant drv_data_exynos7 = {
236 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
237 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
238 .mask_bit = 23,
239 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
240 .rst_stat_bit = 23, /* A57 WDTRESET */
241 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
242 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
243 };
244
245 static const struct s3c2410_wdt_variant drv_data_exynos850_cl0 = {
246 .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
247 .mask_bit = 2,
248 .mask_reset_inv = true,
249 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
250 .rst_stat_bit = EXYNOS850_CLUSTER0_WDTRESET_BIT,
251 .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
252 .cnt_en_bit = 7,
253 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
254 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
255 };
256
257 static const struct s3c2410_wdt_variant drv_data_exynos850_cl1 = {
258 .mask_reset_reg = EXYNOS850_CLUSTER1_NONCPU_INT_EN,
259 .mask_bit = 2,
260 .mask_reset_inv = true,
261 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
262 .rst_stat_bit = EXYNOS850_CLUSTER1_WDTRESET_BIT,
263 .cnt_en_reg = EXYNOS850_CLUSTER1_NONCPU_OUT,
264 .cnt_en_bit = 7,
265 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
266 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
267 };
268
269 static const struct s3c2410_wdt_variant drv_data_exynos990_cl0 = {
270 .mask_reset_reg = GS_CLUSTER0_NONCPU_INT_EN,
271 .mask_bit = 2,
272 .mask_reset_inv = true,
273 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
274 .rst_stat_bit = EXYNOS850_CLUSTER0_WDTRESET_BIT,
275 .cnt_en_reg = EXYNOSAUTOV920_CLUSTER0_NONCPU_OUT,
276 .cnt_en_bit = 7,
277 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
278 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
279 QUIRK_HAS_DBGACK_BIT,
280 };
281
282 static const struct s3c2410_wdt_variant drv_data_exynos990_cl2 = {
283 .mask_reset_reg = EXYNOS990_CLUSTER2_NONCPU_INT_EN,
284 .mask_bit = 2,
285 .mask_reset_inv = true,
286 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
287 .rst_stat_bit = EXYNOS990_CLUSTER2_WDTRESET_BIT,
288 .cnt_en_reg = EXYNOS990_CLUSTER2_NONCPU_OUT,
289 .cnt_en_bit = 7,
290 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
291 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
292 QUIRK_HAS_DBGACK_BIT,
293 };
294
295 static const struct s3c2410_wdt_variant drv_data_exynosautov9_cl0 = {
296 .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
297 .mask_bit = 2,
298 .mask_reset_inv = true,
299 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
300 .rst_stat_bit = EXYNOSAUTOV9_CLUSTER0_WDTRESET_BIT,
301 .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
302 .cnt_en_bit = 7,
303 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
304 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
305 QUIRK_HAS_DBGACK_BIT | QUIRK_HAS_32BIT_CNT,
306 };
307
308 static const struct s3c2410_wdt_variant drv_data_exynosautov9_cl1 = {
309 .mask_reset_reg = EXYNOSAUTOV9_CLUSTER1_NONCPU_INT_EN,
310 .mask_bit = 2,
311 .mask_reset_inv = true,
312 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
313 .rst_stat_bit = EXYNOSAUTOV9_CLUSTER1_WDTRESET_BIT,
314 .cnt_en_reg = EXYNOSAUTOV9_CLUSTER1_NONCPU_OUT,
315 .cnt_en_bit = 7,
316 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
317 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
318 QUIRK_HAS_DBGACK_BIT | QUIRK_HAS_32BIT_CNT,
319 };
320
321 static const struct s3c2410_wdt_variant drv_data_gs101_cl0 = {
322 .mask_reset_reg = GS_CLUSTER0_NONCPU_INT_EN,
323 .mask_bit = 2,
324 .mask_reset_inv = true,
325 .rst_stat_reg = GS_RST_STAT_REG_OFFSET,
326 .rst_stat_bit = 0,
327 .cnt_en_reg = GS_CLUSTER0_NONCPU_OUT,
328 .cnt_en_bit = 8,
329 .quirks = QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_MASK_RESET |
330 QUIRK_HAS_PMU_CNT_EN | QUIRK_HAS_WTCLRINT_REG |
331 QUIRK_HAS_DBGACK_BIT,
332 };
333
334 static const struct s3c2410_wdt_variant drv_data_gs101_cl1 = {
335 .mask_reset_reg = GS_CLUSTER1_NONCPU_INT_EN,
336 .mask_bit = 2,
337 .mask_reset_inv = true,
338 .rst_stat_reg = GS_RST_STAT_REG_OFFSET,
339 .rst_stat_bit = 1,
340 .cnt_en_reg = GS_CLUSTER1_NONCPU_OUT,
341 .cnt_en_bit = 7,
342 .quirks = QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_MASK_RESET |
343 QUIRK_HAS_PMU_CNT_EN | QUIRK_HAS_WTCLRINT_REG |
344 QUIRK_HAS_DBGACK_BIT,
345 };
346
347 static const struct s3c2410_wdt_variant drv_data_exynosautov920_cl0 = {
348 .mask_reset_reg = EXYNOSAUTOV920_CLUSTER0_NONCPU_INT_EN,
349 .mask_bit = 2,
350 .mask_reset_inv = true,
351 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
352 .rst_stat_bit = EXYNOSAUTOV920_CLUSTER0_WDTRESET_BIT,
353 .cnt_en_reg = EXYNOSAUTOV920_CLUSTER0_NONCPU_OUT,
354 .cnt_en_bit = 8,
355 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
356 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
357 QUIRK_HAS_DBGACK_BIT | QUIRK_HAS_32BIT_CNT,
358 };
359
360 static const struct s3c2410_wdt_variant drv_data_exynosautov920_cl1 = {
361 .mask_reset_reg = EXYNOSAUTOV920_CLUSTER1_NONCPU_INT_EN,
362 .mask_bit = 2,
363 .mask_reset_inv = true,
364 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
365 .rst_stat_bit = EXYNOSAUTOV920_CLUSTER1_WDTRESET_BIT,
366 .cnt_en_reg = EXYNOSAUTOV920_CLUSTER1_NONCPU_OUT,
367 .cnt_en_bit = 8,
368 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
369 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN |
370 QUIRK_HAS_DBGACK_BIT | QUIRK_HAS_32BIT_CNT,
371 };
372
373 static const struct of_device_id s3c2410_wdt_match[] = {
374 { .compatible = "google,gs101-wdt",
375 .data = &drv_data_gs101_cl0 },
376 { .compatible = "samsung,s3c6410-wdt",
377 .data = &drv_data_s3c6410 },
378 { .compatible = "samsung,exynos5250-wdt",
379 .data = &drv_data_exynos5250 },
380 { .compatible = "samsung,exynos5420-wdt",
381 .data = &drv_data_exynos5420 },
382 { .compatible = "samsung,exynos7-wdt",
383 .data = &drv_data_exynos7 },
384 { .compatible = "samsung,exynos850-wdt",
385 .data = &drv_data_exynos850_cl0 },
386 { .compatible = "samsung,exynos990-wdt",
387 .data = &drv_data_exynos990_cl0 },
388 { .compatible = "samsung,exynosautov9-wdt",
389 .data = &drv_data_exynosautov9_cl0 },
390 { .compatible = "samsung,exynosautov920-wdt",
391 .data = &drv_data_exynosautov920_cl0 },
392 {},
393 };
394 MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
395
396 /* functions */
397
398 static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt)
399 {
400 return clk_get_rate(wdt->src_clk ? wdt->src_clk : wdt->bus_clk);
401 }
402
403 static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt)
404 {
405 const unsigned long freq = s3c2410wdt_get_freq(wdt);
406 const u64 n_max = (u64)(S3C2410_WTCON_PRESCALE_MAX + 1) *
407 S3C2410_WTCON_MAXDIV * wdt->max_cnt;
408 u64 t_max = div64_ul(n_max, freq);
409
410 if (t_max > UINT_MAX)
411 t_max = UINT_MAX;
412
413 return t_max;
414 }
415
416 static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
417 {
418 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
419 const u32 val = mask ? mask_val : 0;
420 int ret;
421
422 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->disable_reg,
423 mask_val, val);
424 if (ret < 0)
425 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
426
427 return ret;
428 }
429
430 static int s3c2410wdt_mask_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
431 {
432 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
433 const bool val_inv = wdt->drv_data->mask_reset_inv;
434 const u32 val = (mask ^ val_inv) ? mask_val : 0;
435 int ret;
436
437 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->mask_reset_reg,
438 mask_val, val);
439 if (ret < 0)
440 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
441
442 return ret;
443 }
444
445 static int s3c2410wdt_enable_counter(struct s3c2410_wdt *wdt, bool en)
446 {
447 const u32 mask_val = BIT(wdt->drv_data->cnt_en_bit);
448 const u32 val = en ? mask_val : 0;
449 int ret;
450
451 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->cnt_en_reg,
452 mask_val, val);
453 if (ret < 0)
454 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
455
456 return ret;
457 }
458
459 static int s3c2410wdt_enable(struct s3c2410_wdt *wdt, bool en)
460 {
461 int ret;
462
463 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_AUTO_DISABLE) {
464 ret = s3c2410wdt_disable_wdt_reset(wdt, !en);
465 if (ret < 0)
466 return ret;
467 }
468
469 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_MASK_RESET) {
470 ret = s3c2410wdt_mask_wdt_reset(wdt, !en);
471 if (ret < 0)
472 return ret;
473 }
474
475 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_CNT_EN) {
476 ret = s3c2410wdt_enable_counter(wdt, en);
477 if (ret < 0)
478 return ret;
479 }
480
481 return 0;
482 }
483
484 /* Disable watchdog outputs if CPU is in debug mode */
485 static void s3c2410wdt_mask_dbgack(struct s3c2410_wdt *wdt)
486 {
487 unsigned long wtcon;
488
489 if (!(wdt->drv_data->quirks & QUIRK_HAS_DBGACK_BIT))
490 return;
491
492 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
493 wtcon |= S3C2410_WTCON_DBGACK_MASK;
494 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
495 }
496
497 static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
498 {
499 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
500 unsigned long flags;
501
502 spin_lock_irqsave(&wdt->lock, flags);
503 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
504 spin_unlock_irqrestore(&wdt->lock, flags);
505
506 return 0;
507 }
508
509 static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
510 {
511 unsigned long wtcon;
512
513 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
514 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
515 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
516 }
517
518 static int s3c2410wdt_stop(struct watchdog_device *wdd)
519 {
520 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
521 unsigned long flags;
522
523 spin_lock_irqsave(&wdt->lock, flags);
524 __s3c2410wdt_stop(wdt);
525 spin_unlock_irqrestore(&wdt->lock, flags);
526
527 return 0;
528 }
529
530 static int s3c2410wdt_start(struct watchdog_device *wdd)
531 {
532 unsigned long wtcon;
533 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
534 unsigned long flags;
535
536 spin_lock_irqsave(&wdt->lock, flags);
537
538 __s3c2410wdt_stop(wdt);
539
540 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
541 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
542
543 if (soft_noboot) {
544 wtcon |= S3C2410_WTCON_INTEN;
545 wtcon &= ~S3C2410_WTCON_RSTEN;
546 } else {
547 wtcon &= ~S3C2410_WTCON_INTEN;
548 wtcon |= S3C2410_WTCON_RSTEN;
549 }
550
551 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
552 wdt->count, wtcon);
553
554 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
555 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
556 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
557 spin_unlock_irqrestore(&wdt->lock, flags);
558
559 return 0;
560 }
561
562 static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
563 unsigned int timeout)
564 {
565 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
566 unsigned long freq = s3c2410wdt_get_freq(wdt);
567 unsigned long count;
568 unsigned int divisor = 1;
569 unsigned long wtcon;
570
571 if (timeout < 1)
572 return -EINVAL;
573
574 freq = DIV_ROUND_UP(freq, 128);
575 count = timeout * freq;
576
577 dev_dbg(wdt->dev, "Heartbeat: count=%lu, timeout=%d, freq=%lu\n",
578 count, timeout, freq);
579
580 /* if the count is bigger than the watchdog register,
581 then work out what we need to do (and if) we can
582 actually make this value
583 */
584
585 if (count > wdt->max_cnt) {
586 divisor = DIV_ROUND_UP(count, wdt->max_cnt);
587
588 if (divisor > S3C2410_WTCON_PRESCALE_MAX + 1) {
589 dev_err(wdt->dev, "timeout %d too big\n", timeout);
590 return -EINVAL;
591 }
592 }
593
594 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%lu (%08lx)\n",
595 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
596
597 count = DIV_ROUND_UP(count, divisor);
598 wdt->count = count;
599
600 /* update the pre-scaler */
601 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
602 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
603 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
604
605 writel(count, wdt->reg_base + S3C2410_WTDAT);
606 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
607
608 wdd->timeout = (count * divisor) / freq;
609
610 return 0;
611 }
612
613 static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
614 void *data)
615 {
616 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
617 void __iomem *wdt_base = wdt->reg_base;
618
619 /* disable watchdog, to be safe */
620 writel(0, wdt_base + S3C2410_WTCON);
621
622 /* put initial values into count and data */
623 writel(0x80, wdt_base + S3C2410_WTCNT);
624 writel(0x80, wdt_base + S3C2410_WTDAT);
625
626 /* set the watchdog to go and reset... */
627 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
628 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
629 wdt_base + S3C2410_WTCON);
630
631 /* wait for reset to assert... */
632 mdelay(500);
633
634 return 0;
635 }
636
637 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
638
639 static const struct watchdog_info s3c2410_wdt_ident = {
640 .options = OPTIONS,
641 .firmware_version = 0,
642 .identity = "S3C2410 Watchdog",
643 };
644
645 static const struct watchdog_ops s3c2410wdt_ops = {
646 .owner = THIS_MODULE,
647 .start = s3c2410wdt_start,
648 .stop = s3c2410wdt_stop,
649 .ping = s3c2410wdt_keepalive,
650 .set_timeout = s3c2410wdt_set_heartbeat,
651 .restart = s3c2410wdt_restart,
652 };
653
654 static const struct watchdog_device s3c2410_wdd = {
655 .info = &s3c2410_wdt_ident,
656 .ops = &s3c2410wdt_ops,
657 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
658 };
659
660 /* interrupt handler code */
661
662 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
663 {
664 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
665
666 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
667
668 s3c2410wdt_keepalive(&wdt->wdt_device);
669
670 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
671 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
672
673 return IRQ_HANDLED;
674 }
675
676 static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
677 {
678 unsigned int rst_stat;
679 int ret;
680
681 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_RST_STAT))
682 return 0;
683
684 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
685 if (ret)
686 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
687 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
688 return WDIOF_CARDRESET;
689
690 return 0;
691 }
692
693 static inline int
694 s3c2410_get_wdt_drv_data(struct platform_device *pdev, struct s3c2410_wdt *wdt)
695 {
696 const struct s3c2410_wdt_variant *variant;
697 struct device *dev = &pdev->dev;
698
699 variant = of_device_get_match_data(dev);
700 if (!variant) {
701 /* Device matched by platform_device_id */
702 variant = (struct s3c2410_wdt_variant *)
703 platform_get_device_id(pdev)->driver_data;
704 }
705
706 /* Choose Exynos850/ExynosAutov9 driver data w.r.t. cluster index */
707 if (variant == &drv_data_exynos850_cl0 ||
708 variant == &drv_data_exynosautov9_cl0 ||
709 variant == &drv_data_gs101_cl0 ||
710 variant == &drv_data_exynosautov920_cl0 ||
711 variant == &drv_data_exynos990_cl0) {
712 u32 index;
713 int err;
714
715 err = of_property_read_u32(dev->of_node,
716 "samsung,cluster-index", &index);
717 if (err)
718 return dev_err_probe(dev, -EINVAL, "failed to get cluster index\n");
719
720 switch (index) {
721 case 0:
722 break;
723 case 1:
724 if (variant == &drv_data_exynos850_cl0)
725 variant = &drv_data_exynos850_cl1;
726 else if (variant == &drv_data_exynosautov9_cl0)
727 variant = &drv_data_exynosautov9_cl1;
728 else if (variant == &drv_data_gs101_cl0)
729 variant = &drv_data_gs101_cl1;
730 else if (variant == &drv_data_exynosautov920_cl0)
731 variant = &drv_data_exynosautov920_cl1;
732 break;
733 case 2:
734 if (variant == &drv_data_exynos990_cl0)
735 variant = &drv_data_exynos990_cl2;
736 break;
737 default:
738 return dev_err_probe(dev, -EINVAL, "wrong cluster index: %u\n", index);
739 }
740 }
741
742 wdt->drv_data = variant;
743 return 0;
744 }
745
746 static void s3c2410wdt_wdt_disable_action(void *data)
747 {
748 s3c2410wdt_enable(data, false);
749 }
750
751 static int s3c2410wdt_probe(struct platform_device *pdev)
752 {
753 struct device *dev = &pdev->dev;
754 struct s3c2410_wdt *wdt;
755 unsigned int wtcon;
756 int wdt_irq;
757 int ret;
758
759 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
760 if (!wdt)
761 return -ENOMEM;
762
763 wdt->dev = dev;
764 spin_lock_init(&wdt->lock);
765 wdt->wdt_device = s3c2410_wdd;
766
767 ret = s3c2410_get_wdt_drv_data(pdev, wdt);
768 if (ret)
769 return ret;
770
771 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
772 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
773 "samsung,syscon-phandle");
774 if (IS_ERR(wdt->pmureg))
775 return dev_err_probe(dev, PTR_ERR(wdt->pmureg),
776 "syscon regmap lookup failed.\n");
777 }
778
779 wdt_irq = platform_get_irq(pdev, 0);
780 if (wdt_irq < 0)
781 return wdt_irq;
782
783 /* get the memory region for the watchdog timer */
784 wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
785 if (IS_ERR(wdt->reg_base))
786 return PTR_ERR(wdt->reg_base);
787
788 wdt->bus_clk = devm_clk_get_enabled(dev, "watchdog");
789 if (IS_ERR(wdt->bus_clk))
790 return dev_err_probe(dev, PTR_ERR(wdt->bus_clk), "failed to get bus clock\n");
791
792 /*
793 * "watchdog_src" clock is optional; if it's not present -- just skip it
794 * and use "watchdog" clock as both bus and source clock.
795 */
796 wdt->src_clk = devm_clk_get_optional_enabled(dev, "watchdog_src");
797 if (IS_ERR(wdt->src_clk))
798 return dev_err_probe(dev, PTR_ERR(wdt->src_clk), "failed to get source clock\n");
799
800 if (wdt->drv_data->quirks & QUIRK_HAS_32BIT_CNT)
801 wdt->max_cnt = S3C2410_WTCNT_MAXCNT_32;
802 else
803 wdt->max_cnt = S3C2410_WTCNT_MAXCNT_16;
804
805 wdt->wdt_device.min_timeout = 1;
806 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt);
807
808 watchdog_set_drvdata(&wdt->wdt_device, wdt);
809
810 /* see if we can actually set the requested timer margin, and if
811 * not, try the default value */
812
813 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
814 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
815 wdt->wdt_device.timeout);
816 if (ret) {
817 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
818 S3C2410_WATCHDOG_DEFAULT_TIME);
819 if (ret == 0)
820 dev_warn(dev, "tmr_margin value out of range, default %d used\n",
821 S3C2410_WATCHDOG_DEFAULT_TIME);
822 else
823 return dev_err_probe(dev, ret, "failed to use default timeout\n");
824 }
825
826 ret = devm_request_irq(dev, wdt_irq, s3c2410wdt_irq, 0,
827 pdev->name, pdev);
828 if (ret != 0)
829 return dev_err_probe(dev, ret, "failed to install irq (%d)\n", ret);
830
831 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
832 watchdog_set_restart_priority(&wdt->wdt_device, 128);
833
834 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
835 wdt->wdt_device.parent = dev;
836
837 s3c2410wdt_mask_dbgack(wdt);
838
839 /*
840 * If "tmr_atboot" param is non-zero, start the watchdog right now. Also
841 * set WDOG_HW_RUNNING bit, so that watchdog core can kick the watchdog.
842 *
843 * If we're not enabling the watchdog, then ensure it is disabled if it
844 * has been left running from the bootloader or other source.
845 */
846 if (tmr_atboot) {
847 dev_info(dev, "starting watchdog timer\n");
848 s3c2410wdt_start(&wdt->wdt_device);
849 set_bit(WDOG_HW_RUNNING, &wdt->wdt_device.status);
850 } else {
851 s3c2410wdt_stop(&wdt->wdt_device);
852 }
853
854 ret = devm_watchdog_register_device(dev, &wdt->wdt_device);
855 if (ret)
856 return ret;
857
858 ret = s3c2410wdt_enable(wdt, true);
859 if (ret < 0)
860 return ret;
861
862 ret = devm_add_action_or_reset(dev, s3c2410wdt_wdt_disable_action, wdt);
863 if (ret)
864 return ret;
865
866 platform_set_drvdata(pdev, wdt);
867
868 /* print out a statement of readiness */
869
870 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
871
872 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
873 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
874 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
875 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
876
877 return 0;
878 }
879
880 static void s3c2410wdt_shutdown(struct platform_device *dev)
881 {
882 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
883
884 s3c2410wdt_enable(wdt, false);
885 s3c2410wdt_stop(&wdt->wdt_device);
886 }
887
888 static int s3c2410wdt_suspend(struct device *dev)
889 {
890 int ret;
891 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
892
893 /* Save watchdog state, and turn it off. */
894 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
895 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
896
897 ret = s3c2410wdt_enable(wdt, false);
898 if (ret < 0)
899 return ret;
900
901 /* Note that WTCNT doesn't need to be saved. */
902 s3c2410wdt_stop(&wdt->wdt_device);
903
904 return 0;
905 }
906
907 static int s3c2410wdt_resume(struct device *dev)
908 {
909 int ret;
910 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
911
912 /* Restore watchdog state. */
913 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
914 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
915 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
916
917 ret = s3c2410wdt_enable(wdt, true);
918 if (ret < 0)
919 return ret;
920
921 dev_info(dev, "watchdog %sabled\n",
922 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
923
924 return 0;
925 }
926
927 static DEFINE_SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops,
928 s3c2410wdt_suspend, s3c2410wdt_resume);
929
930 static struct platform_driver s3c2410wdt_driver = {
931 .probe = s3c2410wdt_probe,
932 .shutdown = s3c2410wdt_shutdown,
933 .driver = {
934 .name = "s3c2410-wdt",
935 .pm = pm_sleep_ptr(&s3c2410wdt_pm_ops),
936 .of_match_table = s3c2410_wdt_match,
937 },
938 };
939
940 module_platform_driver(s3c2410wdt_driver);
941
942 MODULE_AUTHOR("Ben Dooks <[email protected]>, Dimitry Andric <[email protected]>");
943 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
944 MODULE_LICENSE("GPL");