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