Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * drivers/char/watchdog/sp805-wdt.c |
| 3 | * |
| 4 | * Watchdog driver for ARM SP805 watchdog module |
| 5 | * |
| 6 | * Copyright (C) 2010 ST Microelectronics |
Viresh Kumar | da89947 | 2015-07-17 16:23:50 -0700 | [diff] [blame] | 7 | * Viresh Kumar <vireshk@kernel.org> |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public |
| 10 | * License version 2 or later. This program is licensed "as is" without any |
| 11 | * warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/resource.h> |
| 16 | #include <linux/amba/bus.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/clk.h> |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 19 | #include <linux/io.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/math64.h> |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 25 | #include <linux/pm.h> |
Andy Shevchenko | 05f0a99 | 2021-05-17 20:44:56 +0300 | [diff] [blame] | 26 | #include <linux/property.h> |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/types.h> |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 30 | #include <linux/watchdog.h> |
| 31 | |
| 32 | /* default timeout in seconds */ |
| 33 | #define DEFAULT_TIMEOUT 60 |
| 34 | |
| 35 | #define MODULE_NAME "sp805-wdt" |
| 36 | |
| 37 | /* watchdog register offsets and masks */ |
| 38 | #define WDTLOAD 0x000 |
| 39 | #define LOAD_MIN 0x00000001 |
| 40 | #define LOAD_MAX 0xFFFFFFFF |
| 41 | #define WDTVALUE 0x004 |
| 42 | #define WDTCONTROL 0x008 |
| 43 | /* control register masks */ |
| 44 | #define INT_ENABLE (1 << 0) |
| 45 | #define RESET_ENABLE (1 << 1) |
Ray Jui | fa5072e | 2018-05-28 11:01:35 -0700 | [diff] [blame] | 46 | #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 47 | #define WDTINTCLR 0x00C |
| 48 | #define WDTRIS 0x010 |
| 49 | #define WDTMIS 0x014 |
| 50 | #define INT_MASK (1 << 0) |
| 51 | #define WDTLOCK 0xC00 |
| 52 | #define UNLOCK 0x1ACCE551 |
| 53 | #define LOCK 0x00000001 |
| 54 | |
| 55 | /** |
| 56 | * struct sp805_wdt: sp805 wdt device structure |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 57 | * @wdd: instance of struct watchdog_device |
Viresh Kumar | bfae14b | 2012-03-12 09:52:13 +0530 | [diff] [blame] | 58 | * @lock: spin lock protecting dev structure and io access |
| 59 | * @base: base address of wdt |
Andy Shevchenko | 3452239e | 2021-05-17 20:49:12 +0300 | [diff] [blame] | 60 | * @clk: (optional) clock structure of wdt |
| 61 | * @rate: (optional) clock rate when provided via properties |
Viresh Kumar | bfae14b | 2012-03-12 09:52:13 +0530 | [diff] [blame] | 62 | * @adev: amba device structure of wdt |
| 63 | * @status: current status of wdt |
| 64 | * @load_val: load value to be set for current timeout |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 65 | */ |
| 66 | struct sp805_wdt { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 67 | struct watchdog_device wdd; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 68 | spinlock_t lock; |
| 69 | void __iomem *base; |
| 70 | struct clk *clk; |
Srinath Mannam | dc0e4a3 | 2018-07-26 10:28:42 +0530 | [diff] [blame] | 71 | u64 rate; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 72 | struct amba_device *adev; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 73 | unsigned int load_val; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 74 | }; |
| 75 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 76 | static bool nowayout = WATCHDOG_NOWAYOUT; |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 77 | module_param(nowayout, bool, 0); |
| 78 | MODULE_PARM_DESC(nowayout, |
| 79 | "Set to 1 to keep watchdog running after device release"); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 80 | |
Ray Jui | fa5072e | 2018-05-28 11:01:35 -0700 | [diff] [blame] | 81 | /* returns true if wdt is running; otherwise returns false */ |
| 82 | static bool wdt_is_running(struct watchdog_device *wdd) |
| 83 | { |
| 84 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
| 85 | u32 wdtcontrol = readl_relaxed(wdt->base + WDTCONTROL); |
| 86 | |
| 87 | return (wdtcontrol & ENABLE_MASK) == ENABLE_MASK; |
| 88 | } |
| 89 | |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 90 | /* This routine finds load value that will reset system in required timout */ |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 91 | static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 92 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 93 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 94 | u64 load, rate; |
| 95 | |
Srinath Mannam | dc0e4a3 | 2018-07-26 10:28:42 +0530 | [diff] [blame] | 96 | rate = wdt->rate; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * sp805 runs counter with given value twice, after the end of first |
| 100 | * counter it gives an interrupt and then starts counter again. If |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 101 | * interrupt already occurred then it resets the system. This is why |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 102 | * load is half of what should be required. |
| 103 | */ |
| 104 | load = div_u64(rate, 2) * timeout - 1; |
| 105 | |
| 106 | load = (load > LOAD_MAX) ? LOAD_MAX : load; |
| 107 | load = (load < LOAD_MIN) ? LOAD_MIN : load; |
| 108 | |
| 109 | spin_lock(&wdt->lock); |
| 110 | wdt->load_val = load; |
| 111 | /* roundup timeout to closest positive integer value */ |
Viresh Kumar | 938626d | 2014-05-15 10:01:59 +0530 | [diff] [blame] | 112 | wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 113 | spin_unlock(&wdt->lock); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 114 | |
| 115 | return 0; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* returns number of seconds left for reset to occur */ |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 119 | static unsigned int wdt_timeleft(struct watchdog_device *wdd) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 120 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 121 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
Srinath Mannam | dc0e4a3 | 2018-07-26 10:28:42 +0530 | [diff] [blame] | 122 | u64 load; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 123 | |
| 124 | spin_lock(&wdt->lock); |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 125 | load = readl_relaxed(wdt->base + WDTVALUE); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 126 | |
| 127 | /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */ |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 128 | if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK)) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 129 | load += wdt->load_val + 1; |
| 130 | spin_unlock(&wdt->lock); |
| 131 | |
Srinath Mannam | dc0e4a3 | 2018-07-26 10:28:42 +0530 | [diff] [blame] | 132 | return div_u64(load, wdt->rate); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 133 | } |
| 134 | |
Jongsung Kim | 6c5c0d4 | 2018-05-04 15:05:03 +0900 | [diff] [blame] | 135 | static int |
| 136 | wdt_restart(struct watchdog_device *wdd, unsigned long mode, void *cmd) |
| 137 | { |
| 138 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
| 139 | |
Michael Walle | ea104a9 | 2020-03-27 17:24:50 +0100 | [diff] [blame] | 140 | writel_relaxed(UNLOCK, wdt->base + WDTLOCK); |
Jongsung Kim | 6c5c0d4 | 2018-05-04 15:05:03 +0900 | [diff] [blame] | 141 | writel_relaxed(0, wdt->base + WDTCONTROL); |
| 142 | writel_relaxed(0, wdt->base + WDTLOAD); |
| 143 | writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base + WDTCONTROL); |
| 144 | |
Michael Walle | ea104a9 | 2020-03-27 17:24:50 +0100 | [diff] [blame] | 145 | /* Flush posted writes. */ |
| 146 | readl_relaxed(wdt->base + WDTLOCK); |
| 147 | |
Jongsung Kim | 6c5c0d4 | 2018-05-04 15:05:03 +0900 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 151 | static int wdt_config(struct watchdog_device *wdd, bool ping) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 152 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 153 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
| 154 | int ret; |
| 155 | |
| 156 | if (!ping) { |
Viresh Kumar | d9df0ef | 2012-03-12 09:52:17 +0530 | [diff] [blame] | 157 | |
Julia Lawall | 63fbbc1 | 2012-08-26 18:01:02 +0200 | [diff] [blame] | 158 | ret = clk_prepare_enable(wdt->clk); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 159 | if (ret) { |
| 160 | dev_err(&wdt->adev->dev, "clock enable fail"); |
| 161 | return ret; |
| 162 | } |
| 163 | } |
| 164 | |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 165 | spin_lock(&wdt->lock); |
| 166 | |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 167 | writel_relaxed(UNLOCK, wdt->base + WDTLOCK); |
| 168 | writel_relaxed(wdt->load_val, wdt->base + WDTLOAD); |
Sandeep Tripathy | 55e0717 | 2016-01-19 14:44:49 +0530 | [diff] [blame] | 169 | writel_relaxed(INT_MASK, wdt->base + WDTINTCLR); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 170 | |
Sandeep Tripathy | 55e0717 | 2016-01-19 14:44:49 +0530 | [diff] [blame] | 171 | if (!ping) |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 172 | writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base + |
| 173 | WDTCONTROL); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 174 | |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 175 | writel_relaxed(LOCK, wdt->base + WDTLOCK); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 176 | |
Nick Bowler | 081d83a | 2011-07-15 11:04:02 -0400 | [diff] [blame] | 177 | /* Flush posted writes. */ |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 178 | readl_relaxed(wdt->base + WDTLOCK); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 179 | spin_unlock(&wdt->lock); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int wdt_ping(struct watchdog_device *wdd) |
| 185 | { |
| 186 | return wdt_config(wdd, true); |
| 187 | } |
| 188 | |
| 189 | /* enables watchdog timers reset */ |
| 190 | static int wdt_enable(struct watchdog_device *wdd) |
| 191 | { |
| 192 | return wdt_config(wdd, false); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* disables watchdog timers reset */ |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 196 | static int wdt_disable(struct watchdog_device *wdd) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 197 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 198 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
| 199 | |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 200 | spin_lock(&wdt->lock); |
| 201 | |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 202 | writel_relaxed(UNLOCK, wdt->base + WDTLOCK); |
| 203 | writel_relaxed(0, wdt->base + WDTCONTROL); |
| 204 | writel_relaxed(LOCK, wdt->base + WDTLOCK); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 205 | |
Nick Bowler | 081d83a | 2011-07-15 11:04:02 -0400 | [diff] [blame] | 206 | /* Flush posted writes. */ |
Viresh Kumar | d2e8919 | 2012-03-12 09:52:14 +0530 | [diff] [blame] | 207 | readl_relaxed(wdt->base + WDTLOCK); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 208 | spin_unlock(&wdt->lock); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 209 | |
Julia Lawall | 63fbbc1 | 2012-08-26 18:01:02 +0200 | [diff] [blame] | 210 | clk_disable_unprepare(wdt->clk); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 215 | static const struct watchdog_info wdt_info = { |
| 216 | .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 217 | .identity = MODULE_NAME, |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 218 | }; |
| 219 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 220 | static const struct watchdog_ops wdt_ops = { |
| 221 | .owner = THIS_MODULE, |
| 222 | .start = wdt_enable, |
| 223 | .stop = wdt_disable, |
| 224 | .ping = wdt_ping, |
| 225 | .set_timeout = wdt_setload, |
| 226 | .get_timeleft = wdt_timeleft, |
Jongsung Kim | 6c5c0d4 | 2018-05-04 15:05:03 +0900 | [diff] [blame] | 227 | .restart = wdt_restart, |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 228 | }; |
| 229 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 230 | static int |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 231 | sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 232 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 233 | struct sp805_wdt *wdt; |
Andy Shevchenko | 05f0a99 | 2021-05-17 20:44:56 +0300 | [diff] [blame] | 234 | u64 rate = 0; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 235 | int ret = 0; |
| 236 | |
Viresh Kumar | fb35a5a | 2012-03-12 09:52:15 +0530 | [diff] [blame] | 237 | wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 238 | if (!wdt) { |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 239 | ret = -ENOMEM; |
Viresh Kumar | fb35a5a | 2012-03-12 09:52:15 +0530 | [diff] [blame] | 240 | goto err; |
| 241 | } |
| 242 | |
Jingoo Han | 9d11e4f8 | 2014-02-11 21:45:56 +0900 | [diff] [blame] | 243 | wdt->base = devm_ioremap_resource(&adev->dev, &adev->res); |
| 244 | if (IS_ERR(wdt->base)) |
| 245 | return PTR_ERR(wdt->base); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 246 | |
Andy Shevchenko | 05f0a99 | 2021-05-17 20:44:56 +0300 | [diff] [blame] | 247 | /* |
| 248 | * When driver probe with ACPI device, clock devices |
| 249 | * are not available, so watchdog rate get from |
| 250 | * clock-frequency property given in _DSD object. |
| 251 | */ |
| 252 | device_property_read_u64(&adev->dev, "clock-frequency", &rate); |
| 253 | |
| 254 | wdt->clk = devm_clk_get_optional(&adev->dev, NULL); |
| 255 | if (IS_ERR(wdt->clk)) |
| 256 | return dev_err_probe(&adev->dev, PTR_ERR(wdt->clk), "Clock not found\n"); |
| 257 | |
| 258 | wdt->rate = clk_get_rate(wdt->clk); |
| 259 | if (!wdt->rate) |
| 260 | wdt->rate = rate; |
| 261 | if (!wdt->rate) { |
| 262 | dev_err(&adev->dev, "no clock-frequency property\n"); |
| 263 | return -ENODEV; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | wdt->adev = adev; |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 267 | wdt->wdd.info = &wdt_info; |
| 268 | wdt->wdd.ops = &wdt_ops; |
Pratyush Anand | 6551881 | 2015-08-20 14:05:01 +0530 | [diff] [blame] | 269 | wdt->wdd.parent = &adev->dev; |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 270 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 271 | spin_lock_init(&wdt->lock); |
| 272 | watchdog_set_nowayout(&wdt->wdd, nowayout); |
| 273 | watchdog_set_drvdata(&wdt->wdd, wdt); |
Jongsung Kim | 6c5c0d4 | 2018-05-04 15:05:03 +0900 | [diff] [blame] | 274 | watchdog_set_restart_priority(&wdt->wdd, 128); |
Ray Jui | b800885 | 2018-05-28 11:01:34 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * If 'timeout-sec' devicetree property is specified, use that. |
| 278 | * Otherwise, use DEFAULT_TIMEOUT |
| 279 | */ |
| 280 | wdt->wdd.timeout = DEFAULT_TIMEOUT; |
| 281 | watchdog_init_timeout(&wdt->wdd, 0, &adev->dev); |
| 282 | wdt_setload(&wdt->wdd, wdt->wdd.timeout); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 283 | |
Ray Jui | fa5072e | 2018-05-28 11:01:35 -0700 | [diff] [blame] | 284 | /* |
| 285 | * If HW is already running, enable/reset the wdt and set the running |
| 286 | * bit to tell the wdt subsystem |
| 287 | */ |
| 288 | if (wdt_is_running(&wdt->wdd)) { |
| 289 | wdt_enable(&wdt->wdd); |
| 290 | set_bit(WDOG_HW_RUNNING, &wdt->wdd.status); |
| 291 | } |
| 292 | |
Zhao Qiang | 42e967f | 2020-11-27 15:52:17 +0800 | [diff] [blame] | 293 | watchdog_stop_on_reboot(&wdt->wdd); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 294 | ret = watchdog_register_device(&wdt->wdd); |
Wolfram Sang | 199801c | 2019-05-18 23:27:53 +0200 | [diff] [blame] | 295 | if (ret) |
Jingoo Han | 07bf971 | 2013-04-29 18:16:49 +0900 | [diff] [blame] | 296 | goto err; |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 297 | amba_set_drvdata(adev, wdt); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 298 | |
| 299 | dev_info(&adev->dev, "registration successful\n"); |
| 300 | return 0; |
| 301 | |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 302 | err: |
| 303 | dev_err(&adev->dev, "Probe Failed!!!\n"); |
| 304 | return ret; |
| 305 | } |
| 306 | |
Uwe Kleine-König | 3fd269e | 2021-01-26 17:58:34 +0100 | [diff] [blame] | 307 | static void sp805_wdt_remove(struct amba_device *adev) |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 308 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 309 | struct sp805_wdt *wdt = amba_get_drvdata(adev); |
| 310 | |
| 311 | watchdog_unregister_device(&wdt->wdd); |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 312 | watchdog_set_drvdata(&wdt->wdd, NULL); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 313 | } |
| 314 | |
Russell King | 60d6dd5 | 2012-11-08 10:51:03 +0000 | [diff] [blame] | 315 | static int __maybe_unused sp805_wdt_suspend(struct device *dev) |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 316 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 317 | struct sp805_wdt *wdt = dev_get_drvdata(dev); |
| 318 | |
| 319 | if (watchdog_active(&wdt->wdd)) |
| 320 | return wdt_disable(&wdt->wdd); |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Russell King | 60d6dd5 | 2012-11-08 10:51:03 +0000 | [diff] [blame] | 325 | static int __maybe_unused sp805_wdt_resume(struct device *dev) |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 326 | { |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 327 | struct sp805_wdt *wdt = dev_get_drvdata(dev); |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 328 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 329 | if (watchdog_active(&wdt->wdd)) |
| 330 | return wdt_enable(&wdt->wdd); |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 331 | |
Viresh Kumar | 4a51653 | 2012-03-12 09:52:16 +0530 | [diff] [blame] | 332 | return 0; |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 333 | } |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 334 | |
| 335 | static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend, |
| 336 | sp805_wdt_resume); |
| 337 | |
Arvind Yadav | 05ce42f | 2017-08-23 22:04:08 +0530 | [diff] [blame] | 338 | static const struct amba_id sp805_wdt_ids[] = { |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 339 | { |
| 340 | .id = 0x00141805, |
| 341 | .mask = 0x00ffffff, |
| 342 | }, |
| 343 | { 0, 0 }, |
| 344 | }; |
| 345 | |
Dave Martin | 17885b0 | 2011-10-05 15:15:23 +0100 | [diff] [blame] | 346 | MODULE_DEVICE_TABLE(amba, sp805_wdt_ids); |
| 347 | |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 348 | static struct amba_driver sp805_wdt_driver = { |
| 349 | .drv = { |
| 350 | .name = MODULE_NAME, |
Viresh Kumar | 16ac4ab | 2012-02-24 15:12:37 +0530 | [diff] [blame] | 351 | .pm = &sp805_wdt_dev_pm_ops, |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 352 | }, |
| 353 | .id_table = sp805_wdt_ids, |
| 354 | .probe = sp805_wdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 355 | .remove = sp805_wdt_remove, |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 356 | }; |
| 357 | |
viresh kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 358 | module_amba_driver(sp805_wdt_driver); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 359 | |
Viresh Kumar | da89947 | 2015-07-17 16:23:50 -0700 | [diff] [blame] | 360 | MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>"); |
Viresh KUMAR | 4a37027 | 2010-08-04 11:44:14 +0530 | [diff] [blame] | 361 | MODULE_DESCRIPTION("ARM SP805 Watchdog Driver"); |
| 362 | MODULE_LICENSE("GPL"); |