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