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