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