blob: aec946df6ed976d2479762dc51d69060a5504382 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/drivers/char/watchdog/s3c2410_wdt.c
2 *
3 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070024*/
25
Joe Perches27c766a2012-02-15 15:06:19 -080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000036#include <linux/clk.h>
Alan Cox41dc8b72008-08-04 17:54:46 +010037#include <linux/uaccess.h>
38#include <linux/io.h>
Ben Dookse02f8382009-10-30 00:30:25 +000039#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Wolfram Sang25dc46e2011-09-26 15:40:14 +020041#include <linux/err.h>
Wim Van Sebroeck3016a552012-05-03 05:24:17 +000042#include <linux/of.h>
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053043#include <linux/mfd/syscon.h>
44#include <linux/regmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Tomasz Figaa8f54012013-06-17 23:45:24 +090046#define S3C2410_WTCON 0x00
47#define S3C2410_WTDAT 0x04
48#define S3C2410_WTCNT 0x08
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tomasz Figaa8f54012013-06-17 23:45:24 +090050#define S3C2410_WTCON_RSTEN (1 << 0)
51#define S3C2410_WTCON_INTEN (1 << 2)
52#define S3C2410_WTCON_ENABLE (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tomasz Figaa8f54012013-06-17 23:45:24 +090054#define S3C2410_WTCON_DIV16 (0 << 3)
55#define S3C2410_WTCON_DIV32 (1 << 3)
56#define S3C2410_WTCON_DIV64 (2 << 3)
57#define S3C2410_WTCON_DIV128 (3 << 3)
58
59#define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
60#define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
63#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
64
Doug Andersoncffc9a62013-12-06 13:08:07 -080065#define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053066#define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
67#define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
68#define QUIRK_HAS_PMU_CONFIG (1 << 0)
Doug Andersoncffc9a62013-12-06 13:08:07 -080069#define QUIRK_HAS_RST_STAT (1 << 1)
70
71/* These quirks require that we have a PMU register map */
72#define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
73 QUIRK_HAS_RST_STAT)
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053074
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010075static bool nowayout = WATCHDOG_NOWAYOUT;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +010076static int tmr_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
Alan Cox41dc8b72008-08-04 17:54:46 +010078static int soft_noboot;
79static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81module_param(tmr_margin, int, 0);
82module_param(tmr_atboot, int, 0);
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010083module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084module_param(soft_noboot, int, 0);
85module_param(debug, int, 0);
86
Randy Dunlap76550d32010-05-01 09:46:15 -070087MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
Alan Cox41dc8b72008-08-04 17:54:46 +010088 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
89MODULE_PARM_DESC(tmr_atboot,
90 "Watchdog is started at boot time if set to 1, default="
91 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
92MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
93 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000094MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
Randy Dunlap76550d32010-05-01 09:46:15 -070095 "0 to reboot (default 0)");
96MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +053098/**
99 * struct s3c2410_wdt_variant - Per-variant config data
100 *
101 * @disable_reg: Offset in pmureg for the register that disables the watchdog
102 * timer reset functionality.
103 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
104 * timer reset functionality.
105 * @mask_bit: Bit number for the watchdog timer in the disable register and the
106 * mask reset register.
Doug Andersoncffc9a62013-12-06 13:08:07 -0800107 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
108 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
109 * reset.
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530110 * @quirks: A bitfield of quirks.
111 */
112
113struct s3c2410_wdt_variant {
114 int disable_reg;
115 int mask_reset_reg;
116 int mask_bit;
Doug Andersoncffc9a62013-12-06 13:08:07 -0800117 int rst_stat_reg;
118 int rst_stat_bit;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530119 u32 quirks;
120};
121
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530122struct s3c2410_wdt {
123 struct device *dev;
124 struct clk *clock;
125 void __iomem *reg_base;
126 unsigned int count;
127 spinlock_t lock;
128 unsigned long wtcon_save;
129 unsigned long wtdat_save;
130 struct watchdog_device wdt_device;
131 struct notifier_block freq_transition;
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530132 struct s3c2410_wdt_variant *drv_data;
133 struct regmap *pmureg;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530134};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530136static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
137 .quirks = 0
138};
139
140#ifdef CONFIG_OF
141static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
142 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
143 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
144 .mask_bit = 20,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800145 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
146 .rst_stat_bit = 20,
147 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530148};
149
150static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
151 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
152 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
153 .mask_bit = 0,
Doug Andersoncffc9a62013-12-06 13:08:07 -0800154 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
155 .rst_stat_bit = 9,
156 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530157};
158
159static const struct of_device_id s3c2410_wdt_match[] = {
160 { .compatible = "samsung,s3c2410-wdt",
161 .data = &drv_data_s3c2410 },
162 { .compatible = "samsung,exynos5250-wdt",
163 .data = &drv_data_exynos5250 },
164 { .compatible = "samsung,exynos5420-wdt",
165 .data = &drv_data_exynos5420 },
166 {},
167};
168MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
169#endif
170
171static const struct platform_device_id s3c2410_wdt_ids[] = {
172 {
173 .name = "s3c2410-wdt",
174 .driver_data = (unsigned long)&drv_data_s3c2410,
175 },
176 {}
177};
178MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* watchdog control routines */
181
Joe Perches27c766a2012-02-15 15:06:19 -0800182#define DBG(fmt, ...) \
183do { \
184 if (debug) \
185 pr_info(fmt, ##__VA_ARGS__); \
186} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* functions */
189
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530190static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
191{
192 return container_of(nb, struct s3c2410_wdt, freq_transition);
193}
194
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530195static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
196{
197 int ret;
198 u32 mask_val = 1 << wdt->drv_data->mask_bit;
199 u32 val = 0;
200
201 /* No need to do anything if no PMU CONFIG needed */
202 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
203 return 0;
204
205 if (mask)
206 val = mask_val;
207
208 ret = regmap_update_bits(wdt->pmureg,
209 wdt->drv_data->disable_reg,
210 mask_val, val);
211 if (ret < 0)
212 goto error;
213
214 ret = regmap_update_bits(wdt->pmureg,
215 wdt->drv_data->mask_reset_reg,
216 mask_val, val);
217 error:
218 if (ret < 0)
219 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
220
221 return ret;
222}
223
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200224static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530226 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
227
228 spin_lock(&wdt->lock);
229 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
230 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200231
232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530235static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
Alan Cox41dc8b72008-08-04 17:54:46 +0100236{
237 unsigned long wtcon;
238
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530239 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530241 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200244static int s3c2410wdt_stop(struct watchdog_device *wdd)
Alan Cox41dc8b72008-08-04 17:54:46 +0100245{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530246 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
247
248 spin_lock(&wdt->lock);
249 __s3c2410wdt_stop(wdt);
250 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200251
252 return 0;
Alan Cox41dc8b72008-08-04 17:54:46 +0100253}
254
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200255static int s3c2410wdt_start(struct watchdog_device *wdd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 unsigned long wtcon;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530258 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530260 spin_lock(&wdt->lock);
Alan Cox41dc8b72008-08-04 17:54:46 +0100261
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530262 __s3c2410wdt_stop(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530264 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
266
267 if (soft_noboot) {
268 wtcon |= S3C2410_WTCON_INTEN;
269 wtcon &= ~S3C2410_WTCON_RSTEN;
270 } else {
271 wtcon &= ~S3C2410_WTCON_INTEN;
272 wtcon |= S3C2410_WTCON_RSTEN;
273 }
274
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530275 DBG("%s: count=0x%08x, wtcon=%08lx\n",
276 __func__, wdt->count, wtcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530278 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
279 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
280 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
281 spin_unlock(&wdt->lock);
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200282
283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530286static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000287{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530288 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
Ben Dookse02f8382009-10-30 00:30:25 +0000289}
290
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200291static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530293 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
294 unsigned long freq = clk_get_rate(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned int count;
296 unsigned int divisor = 1;
297 unsigned long wtcon;
298
299 if (timeout < 1)
300 return -EINVAL;
301
Doug Anderson17862442013-11-26 16:57:19 -0800302 freq = DIV_ROUND_UP(freq, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 count = timeout * freq;
304
Ben Dookse02f8382009-10-30 00:30:25 +0000305 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800306 __func__, count, timeout, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* if the count is bigger than the watchdog register,
309 then work out what we need to do (and if) we can
310 actually make this value
311 */
312
313 if (count >= 0x10000) {
Doug Anderson17862442013-11-26 16:57:19 -0800314 divisor = DIV_ROUND_UP(count, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Doug Anderson17862442013-11-26 16:57:19 -0800316 if (divisor > 0x100) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530317 dev_err(wdt->dev, "timeout %d too big\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
319 }
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
Doug Anderson17862442013-11-26 16:57:19 -0800323 __func__, timeout, divisor, count, DIV_ROUND_UP(count, divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Doug Anderson17862442013-11-26 16:57:19 -0800325 count = DIV_ROUND_UP(count, divisor);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530326 wdt->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* update the pre-scaler */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530329 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
331 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
332
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530333 writel(count, wdt->reg_base + S3C2410_WTDAT);
334 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Hans de Goede5f2430f2012-05-11 12:00:27 +0200336 wdd->timeout = (count * divisor) / freq;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339}
340
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000341#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Alan Cox41dc8b72008-08-04 17:54:46 +0100343static const struct watchdog_info s3c2410_wdt_ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .options = OPTIONS,
345 .firmware_version = 0,
346 .identity = "S3C2410 Watchdog",
347};
348
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200349static struct watchdog_ops s3c2410wdt_ops = {
350 .owner = THIS_MODULE,
351 .start = s3c2410wdt_start,
352 .stop = s3c2410wdt_stop,
353 .ping = s3c2410wdt_keepalive,
354 .set_timeout = s3c2410wdt_set_heartbeat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200357static struct watchdog_device s3c2410_wdd = {
358 .info = &s3c2410_wdt_ident,
359 .ops = &s3c2410wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100360 .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* interrupt handler code */
364
David Howells7d12e782006-10-05 14:55:46 +0100365static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530367 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530369 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
370
371 s3c2410wdt_keepalive(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return IRQ_HANDLED;
373}
Ben Dookse02f8382009-10-30 00:30:25 +0000374
Doug Anderson0f1dd982013-11-25 15:36:43 -0800375#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dookse02f8382009-10-30 00:30:25 +0000376
377static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
378 unsigned long val, void *data)
379{
380 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530381 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
Ben Dookse02f8382009-10-30 00:30:25 +0000382
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530383 if (!s3c2410wdt_is_running(wdt))
Ben Dookse02f8382009-10-30 00:30:25 +0000384 goto done;
385
386 if (val == CPUFREQ_PRECHANGE) {
387 /* To ensure that over the change we don't cause the
388 * watchdog to trigger, we perform an keep-alive if
389 * the watchdog is running.
390 */
391
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530392 s3c2410wdt_keepalive(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000393 } else if (val == CPUFREQ_POSTCHANGE) {
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530394 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000395
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530396 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
397 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000398
399 if (ret >= 0)
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530400 s3c2410wdt_start(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000401 else
402 goto err;
403 }
404
405done:
406 return 0;
407
408 err:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530409 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
410 wdt->wdt_device.timeout);
Ben Dookse02f8382009-10-30 00:30:25 +0000411 return ret;
412}
413
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530414static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000415{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530416 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
417
418 return cpufreq_register_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000419 CPUFREQ_TRANSITION_NOTIFIER);
420}
421
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530422static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000423{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530424 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
425
426 cpufreq_unregister_notifier(&wdt->freq_transition,
Ben Dookse02f8382009-10-30 00:30:25 +0000427 CPUFREQ_TRANSITION_NOTIFIER);
428}
429
430#else
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530431
432static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000433{
434 return 0;
435}
436
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530437static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
Ben Dookse02f8382009-10-30 00:30:25 +0000438{
439}
440#endif
441
Doug Andersoncffc9a62013-12-06 13:08:07 -0800442static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
443{
444 unsigned int rst_stat;
445 int ret;
446
447 if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
448 return 0;
449
450 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
451 if (ret)
452 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
453 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
454 return WDIOF_CARDRESET;
455
456 return 0;
457}
458
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530459/* s3c2410_get_wdt_driver_data */
460static inline struct s3c2410_wdt_variant *
461get_wdt_drv_data(struct platform_device *pdev)
462{
463 if (pdev->dev.of_node) {
464 const struct of_device_id *match;
465 match = of_match_node(s3c2410_wdt_match, pdev->dev.of_node);
466 return (struct s3c2410_wdt_variant *)match->data;
467 } else {
468 return (struct s3c2410_wdt_variant *)
469 platform_get_device_id(pdev)->driver_data;
470 }
471}
472
Bill Pemberton2d991a12012-11-19 13:21:41 -0500473static int s3c2410wdt_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Ben Dookse8ef92b2007-06-14 12:08:55 +0100475 struct device *dev;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530476 struct s3c2410_wdt *wdt;
477 struct resource *wdt_mem;
478 struct resource *wdt_irq;
Ben Dooks46b814d2007-06-14 12:08:54 +0100479 unsigned int wtcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int started = 0;
481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Harvey Harrisonfa9363c2008-03-05 18:24:58 -0800483 DBG("%s: probe=%p\n", __func__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ben Dookse8ef92b2007-06-14 12:08:55 +0100485 dev = &pdev->dev;
Ben Dookse8ef92b2007-06-14 12:08:55 +0100486
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530487 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
488 if (!wdt)
489 return -ENOMEM;
490
491 wdt->dev = &pdev->dev;
492 spin_lock_init(&wdt->lock);
493 wdt->wdt_device = s3c2410_wdd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530495 wdt->drv_data = get_wdt_drv_data(pdev);
Doug Andersoncffc9a62013-12-06 13:08:07 -0800496 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530497 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
498 "samsung,syscon-phandle");
499 if (IS_ERR(wdt->pmureg)) {
500 dev_err(dev, "syscon regmap lookup failed.\n");
501 return PTR_ERR(wdt->pmureg);
502 }
503 }
504
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900505 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
506 if (wdt_irq == NULL) {
507 dev_err(dev, "no irq resource specified\n");
508 ret = -ENOENT;
509 goto err;
510 }
511
512 /* get the memory region for the watchdog timer */
Julia Lawallbd5cc112013-08-14 11:11:24 +0200513 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530514 wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
515 if (IS_ERR(wdt->reg_base)) {
516 ret = PTR_ERR(wdt->reg_base);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900517 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530520 DBG("probe: mapped reg_base=%p\n", wdt->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530522 wdt->clock = devm_clk_get(dev, "watchdog");
523 if (IS_ERR(wdt->clock)) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100524 dev_err(dev, "failed to find watchdog clock source\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530525 ret = PTR_ERR(wdt->clock);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900526 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530529 clk_prepare_enable(wdt->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530531 ret = s3c2410wdt_cpufreq_register(wdt);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900532 if (ret < 0) {
Jingoo Han38289242013-03-14 10:30:21 +0900533 dev_err(dev, "failed to register cpufreq\n");
Ben Dookse02f8382009-10-30 00:30:25 +0000534 goto err_clk;
535 }
536
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530537 watchdog_set_drvdata(&wdt->wdt_device, wdt);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* see if we can actually set the requested timer margin, and if
540 * not, try the default value */
541
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530542 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, &pdev->dev);
543 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
544 wdt->wdt_device.timeout);
545 if (ret) {
546 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
Alan Cox41dc8b72008-08-04 17:54:46 +0100547 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alan Cox41dc8b72008-08-04 17:54:46 +0100549 if (started == 0)
550 dev_info(dev,
551 "tmr_margin value out of range, default %d used\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
Alan Cox41dc8b72008-08-04 17:54:46 +0100553 else
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000554 dev_info(dev, "default timer value is out of range, "
555 "cannot start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900558 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
559 pdev->name, pdev);
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900560 if (ret != 0) {
561 dev_err(dev, "failed to install irq (%d)\n", ret);
562 goto err_cpufreq;
563 }
564
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530565 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100566
Doug Andersoncffc9a62013-12-06 13:08:07 -0800567 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
568
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530569 ret = watchdog_register_device(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (ret) {
Wolfram Sang25dc46e2011-09-26 15:40:14 +0200571 dev_err(dev, "cannot register watchdog (%d)\n", ret);
Jingoo Han04ecc7d2013-01-10 11:06:33 +0900572 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530575 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
576 if (ret < 0)
577 goto err_unregister;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (tmr_atboot && started == 0) {
Ben Dookse8ef92b2007-06-14 12:08:55 +0100580 dev_info(dev, "starting watchdog timer\n");
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530581 s3c2410wdt_start(&wdt->wdt_device);
Ben Dooks655516c2006-04-19 23:02:56 +0100582 } else if (!tmr_atboot) {
583 /* if we're not enabling the watchdog, then ensure it is
584 * disabled if it has been left running from the bootloader
585 * or other source */
586
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530587 s3c2410wdt_stop(&wdt->wdt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530590 platform_set_drvdata(pdev, wdt);
591
Ben Dooks46b814d2007-06-14 12:08:54 +0100592 /* print out a statement of readiness */
593
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530594 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
Ben Dooks46b814d2007-06-14 12:08:54 +0100595
Ben Dookse8ef92b2007-06-14 12:08:55 +0100596 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
Ben Dooks46b814d2007-06-14 12:08:54 +0100597 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
Dmitry Artamonow20403e82011-11-16 12:46:13 +0400598 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
599 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
Alan Cox41dc8b72008-08-04 17:54:46 +0100600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000602
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530603 err_unregister:
604 watchdog_unregister_device(&wdt->wdt_device);
605
Ben Dookse02f8382009-10-30 00:30:25 +0000606 err_cpufreq:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530607 s3c2410wdt_cpufreq_deregister(wdt);
Ben Dookse02f8382009-10-30 00:30:25 +0000608
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000609 err_clk:
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530610 clk_disable_unprepare(wdt->clock);
611 wdt->clock = NULL;
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000612
MyungJoo Ham78d3e002012-01-13 14:14:23 +0900613 err:
Ben Dooks0b6dd8a2006-12-18 10:31:32 +0000614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Bill Pemberton4b12b892012-11-19 13:26:24 -0500617static int s3c2410wdt_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530619 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530620 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
Wim Van Sebroeck9a372562010-05-21 08:11:42 +0000621
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530622 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
623 if (ret < 0)
624 return ret;
625
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530626 watchdog_unregister_device(&wdt->wdt_device);
Ben Dookse02f8382009-10-30 00:30:25 +0000627
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530628 s3c2410wdt_cpufreq_deregister(wdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530630 clk_disable_unprepare(wdt->clock);
631 wdt->clock = NULL;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 0;
634}
635
Russell King3ae5eae2005-11-09 22:32:44 +0000636static void s3c2410wdt_shutdown(struct platform_device *dev)
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200637{
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530638 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
639
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530640 s3c2410wdt_mask_and_disable_reset(wdt, true);
641
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530642 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200643}
644
Jingoo Han0183984c2013-03-14 10:31:21 +0900645#ifdef CONFIG_PM_SLEEP
Ben Dooksaf4bb822005-08-17 09:03:23 +0200646
Jingoo Han0183984c2013-03-14 10:31:21 +0900647static int s3c2410wdt_suspend(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200648{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530649 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530650 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
651
Russell King9480e302005-10-28 09:52:56 -0700652 /* Save watchdog state, and turn it off. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530653 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
654 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200655
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530656 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
657 if (ret < 0)
658 return ret;
659
Russell King9480e302005-10-28 09:52:56 -0700660 /* Note that WTCNT doesn't need to be saved. */
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530661 s3c2410wdt_stop(&wdt->wdt_device);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200662
663 return 0;
664}
665
Jingoo Han0183984c2013-03-14 10:31:21 +0900666static int s3c2410wdt_resume(struct device *dev)
Ben Dooksaf4bb822005-08-17 09:03:23 +0200667{
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530668 int ret;
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530669 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200670
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530671 /* Restore watchdog state. */
672 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
673 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
674 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200675
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530676 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
677 if (ret < 0)
678 return ret;
679
Jingoo Han0183984c2013-03-14 10:31:21 +0900680 dev_info(dev, "watchdog %sabled\n",
Leela Krishna Amudalaaf4ea632013-08-27 15:36:03 +0530681 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
Ben Dooksaf4bb822005-08-17 09:03:23 +0200682
683 return 0;
684}
Jingoo Han0183984c2013-03-14 10:31:21 +0900685#endif
Ben Dooksaf4bb822005-08-17 09:03:23 +0200686
Jingoo Han0183984c2013-03-14 10:31:21 +0900687static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
688 s3c2410wdt_resume);
Ben Dooksaf4bb822005-08-17 09:03:23 +0200689
Russell King3ae5eae2005-11-09 22:32:44 +0000690static struct platform_driver s3c2410wdt_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 .probe = s3c2410wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500692 .remove = s3c2410wdt_remove,
Ben Dooks94f1e9f2005-08-17 09:04:52 +0200693 .shutdown = s3c2410wdt_shutdown,
Leela Krishna Amudala4f1f6532013-12-06 11:17:47 +0530694 .id_table = s3c2410_wdt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +0000695 .driver = {
696 .owner = THIS_MODULE,
697 .name = "s3c2410-wdt",
Jingoo Han0183984c2013-03-14 10:31:21 +0900698 .pm = &s3c2410wdt_pm_ops,
Wim Van Sebroeck3016a552012-05-03 05:24:17 +0000699 .of_match_table = of_match_ptr(s3c2410_wdt_match),
Russell King3ae5eae2005-11-09 22:32:44 +0000700 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
Sachin Kamat6b761b22012-07-12 17:17:40 +0530703module_platform_driver(s3c2410wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Ben Dooksaf4bb822005-08-17 09:03:23 +0200705MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
706 "Dimitry Andric <dimitry.andric@tomtom.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
708MODULE_LICENSE("GPL");