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