blob: dbeb2146c9681a45a32724453c90a4dc8a1f27b1 [file] [log] [blame]
Viresh KUMAR4a370272010-08-04 11:44:14 +05301/*
2 * drivers/char/watchdog/sp805-wdt.c
3 *
4 * Watchdog driver for ARM SP805 watchdog module
5 *
6 * Copyright (C) 2010 ST Microelectronics
Viresh Kumarda899472015-07-17 16:23:50 -07007 * Viresh Kumar <vireshk@kernel.org>
Viresh KUMAR4a370272010-08-04 11:44:14 +05308 *
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 KUMAR4a370272010-08-04 11:44:14 +053019#include <linux/io.h>
20#include <linux/ioport.h>
21#include <linux/kernel.h>
22#include <linux/math64.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053023#include <linux/module.h>
24#include <linux/moduleparam.h>
Viresh Kumar16ac4ab2012-02-24 15:12:37 +053025#include <linux/pm.h>
Andy Shevchenko05f0a992021-05-17 20:44:56 +030026#include <linux/property.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053027#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/types.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053030#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 Juifa5072e2018-05-28 11:01:35 -070046 #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE)
Viresh KUMAR4a370272010-08-04 11:44:14 +053047#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 Kumar4a516532012-03-12 09:52:16 +053057 * @wdd: instance of struct watchdog_device
Viresh Kumarbfae14b2012-03-12 09:52:13 +053058 * @lock: spin lock protecting dev structure and io access
59 * @base: base address of wdt
Andy Shevchenko3452239e2021-05-17 20:49:12 +030060 * @clk: (optional) clock structure of wdt
61 * @rate: (optional) clock rate when provided via properties
Viresh Kumarbfae14b2012-03-12 09:52:13 +053062 * @adev: amba device structure of wdt
63 * @status: current status of wdt
64 * @load_val: load value to be set for current timeout
Viresh KUMAR4a370272010-08-04 11:44:14 +053065 */
66struct sp805_wdt {
Viresh Kumar4a516532012-03-12 09:52:16 +053067 struct watchdog_device wdd;
Viresh KUMAR4a370272010-08-04 11:44:14 +053068 spinlock_t lock;
69 void __iomem *base;
70 struct clk *clk;
Srinath Mannamdc0e4a32018-07-26 10:28:42 +053071 u64 rate;
Viresh KUMAR4a370272010-08-04 11:44:14 +053072 struct amba_device *adev;
Viresh KUMAR4a370272010-08-04 11:44:14 +053073 unsigned int load_val;
Viresh KUMAR4a370272010-08-04 11:44:14 +053074};
75
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010076static bool nowayout = WATCHDOG_NOWAYOUT;
Viresh Kumar4a516532012-03-12 09:52:16 +053077module_param(nowayout, bool, 0);
78MODULE_PARM_DESC(nowayout,
79 "Set to 1 to keep watchdog running after device release");
Viresh KUMAR4a370272010-08-04 11:44:14 +053080
Ray Juifa5072e2018-05-28 11:01:35 -070081/* returns true if wdt is running; otherwise returns false */
82static 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 KUMAR4a370272010-08-04 11:44:14 +053090/* This routine finds load value that will reset system in required timout */
Viresh Kumar4a516532012-03-12 09:52:16 +053091static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout)
Viresh KUMAR4a370272010-08-04 11:44:14 +053092{
Viresh Kumar4a516532012-03-12 09:52:16 +053093 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
Viresh KUMAR4a370272010-08-04 11:44:14 +053094 u64 load, rate;
95
Srinath Mannamdc0e4a32018-07-26 10:28:42 +053096 rate = wdt->rate;
Viresh KUMAR4a370272010-08-04 11:44:14 +053097
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 Marchi25985ed2011-03-30 22:57:33 -0300101 * interrupt already occurred then it resets the system. This is why
Viresh KUMAR4a370272010-08-04 11:44:14 +0530102 * 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 Kumar938626d2014-05-15 10:01:59 +0530112 wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530113 spin_unlock(&wdt->lock);
Viresh Kumar4a516532012-03-12 09:52:16 +0530114
115 return 0;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530116}
117
118/* returns number of seconds left for reset to occur */
Viresh Kumar4a516532012-03-12 09:52:16 +0530119static unsigned int wdt_timeleft(struct watchdog_device *wdd)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530120{
Viresh Kumar4a516532012-03-12 09:52:16 +0530121 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
Srinath Mannamdc0e4a32018-07-26 10:28:42 +0530122 u64 load;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530123
124 spin_lock(&wdt->lock);
Viresh Kumard2e89192012-03-12 09:52:14 +0530125 load = readl_relaxed(wdt->base + WDTVALUE);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530126
127 /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530128 if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK))
Viresh KUMAR4a370272010-08-04 11:44:14 +0530129 load += wdt->load_val + 1;
130 spin_unlock(&wdt->lock);
131
Srinath Mannamdc0e4a32018-07-26 10:28:42 +0530132 return div_u64(load, wdt->rate);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530133}
134
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900135static int
136wdt_restart(struct watchdog_device *wdd, unsigned long mode, void *cmd)
137{
138 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
139
Michael Walleea104a92020-03-27 17:24:50 +0100140 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900141 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 Walleea104a92020-03-27 17:24:50 +0100145 /* Flush posted writes. */
146 readl_relaxed(wdt->base + WDTLOCK);
147
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900148 return 0;
149}
150
Viresh Kumar4a516532012-03-12 09:52:16 +0530151static int wdt_config(struct watchdog_device *wdd, bool ping)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530152{
Viresh Kumar4a516532012-03-12 09:52:16 +0530153 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
154 int ret;
155
156 if (!ping) {
Viresh Kumard9df0ef2012-03-12 09:52:17 +0530157
Julia Lawall63fbbc12012-08-26 18:01:02 +0200158 ret = clk_prepare_enable(wdt->clk);
Viresh Kumar4a516532012-03-12 09:52:16 +0530159 if (ret) {
160 dev_err(&wdt->adev->dev, "clock enable fail");
161 return ret;
162 }
163 }
164
Viresh KUMAR4a370272010-08-04 11:44:14 +0530165 spin_lock(&wdt->lock);
166
Viresh Kumard2e89192012-03-12 09:52:14 +0530167 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
168 writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
Sandeep Tripathy55e07172016-01-19 14:44:49 +0530169 writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
Viresh Kumar4a516532012-03-12 09:52:16 +0530170
Sandeep Tripathy55e07172016-01-19 14:44:49 +0530171 if (!ping)
Viresh Kumar4a516532012-03-12 09:52:16 +0530172 writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
173 WDTCONTROL);
Viresh Kumar4a516532012-03-12 09:52:16 +0530174
Viresh Kumard2e89192012-03-12 09:52:14 +0530175 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530176
Nick Bowler081d83a2011-07-15 11:04:02 -0400177 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530178 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530179 spin_unlock(&wdt->lock);
Viresh Kumar4a516532012-03-12 09:52:16 +0530180
181 return 0;
182}
183
184static int wdt_ping(struct watchdog_device *wdd)
185{
186 return wdt_config(wdd, true);
187}
188
189/* enables watchdog timers reset */
190static int wdt_enable(struct watchdog_device *wdd)
191{
192 return wdt_config(wdd, false);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530193}
194
195/* disables watchdog timers reset */
Viresh Kumar4a516532012-03-12 09:52:16 +0530196static int wdt_disable(struct watchdog_device *wdd)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530197{
Viresh Kumar4a516532012-03-12 09:52:16 +0530198 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
199
Viresh KUMAR4a370272010-08-04 11:44:14 +0530200 spin_lock(&wdt->lock);
201
Viresh Kumard2e89192012-03-12 09:52:14 +0530202 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
203 writel_relaxed(0, wdt->base + WDTCONTROL);
204 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530205
Nick Bowler081d83a2011-07-15 11:04:02 -0400206 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530207 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530208 spin_unlock(&wdt->lock);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530209
Julia Lawall63fbbc12012-08-26 18:01:02 +0200210 clk_disable_unprepare(wdt->clk);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530211
212 return 0;
213}
214
Viresh Kumar4a516532012-03-12 09:52:16 +0530215static const struct watchdog_info wdt_info = {
216 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
217 .identity = MODULE_NAME,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530218};
219
Viresh Kumar4a516532012-03-12 09:52:16 +0530220static 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 Kim6c5c0d42018-05-04 15:05:03 +0900227 .restart = wdt_restart,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530228};
229
Bill Pemberton2d991a12012-11-19 13:21:41 -0500230static int
Russell Kingaa25afa2011-02-19 15:55:00 +0000231sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530232{
Viresh Kumar4a516532012-03-12 09:52:16 +0530233 struct sp805_wdt *wdt;
Andy Shevchenko05f0a992021-05-17 20:44:56 +0300234 u64 rate = 0;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530235 int ret = 0;
236
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530237 wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530238 if (!wdt) {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530239 ret = -ENOMEM;
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530240 goto err;
241 }
242
Jingoo Han9d11e4f82014-02-11 21:45:56 +0900243 wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
244 if (IS_ERR(wdt->base))
245 return PTR_ERR(wdt->base);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530246
Andy Shevchenko05f0a992021-05-17 20:44:56 +0300247 /*
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 KUMAR4a370272010-08-04 11:44:14 +0530264 }
265
266 wdt->adev = adev;
Viresh Kumar4a516532012-03-12 09:52:16 +0530267 wdt->wdd.info = &wdt_info;
268 wdt->wdd.ops = &wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530269 wdt->wdd.parent = &adev->dev;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530270
Viresh Kumar4a516532012-03-12 09:52:16 +0530271 spin_lock_init(&wdt->lock);
272 watchdog_set_nowayout(&wdt->wdd, nowayout);
273 watchdog_set_drvdata(&wdt->wdd, wdt);
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900274 watchdog_set_restart_priority(&wdt->wdd, 128);
Ray Juib8008852018-05-28 11:01:34 -0700275
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 Kumar4a516532012-03-12 09:52:16 +0530283
Ray Juifa5072e2018-05-28 11:01:35 -0700284 /*
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 Qiang42e967f2020-11-27 15:52:17 +0800293 watchdog_stop_on_reboot(&wdt->wdd);
Viresh Kumar4a516532012-03-12 09:52:16 +0530294 ret = watchdog_register_device(&wdt->wdd);
Wolfram Sang199801c2019-05-18 23:27:53 +0200295 if (ret)
Jingoo Han07bf9712013-04-29 18:16:49 +0900296 goto err;
Viresh Kumar4a516532012-03-12 09:52:16 +0530297 amba_set_drvdata(adev, wdt);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530298
299 dev_info(&adev->dev, "registration successful\n");
300 return 0;
301
Viresh KUMAR4a370272010-08-04 11:44:14 +0530302err:
303 dev_err(&adev->dev, "Probe Failed!!!\n");
304 return ret;
305}
306
Uwe Kleine-König3fd269e2021-01-26 17:58:34 +0100307static void sp805_wdt_remove(struct amba_device *adev)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530308{
Viresh Kumar4a516532012-03-12 09:52:16 +0530309 struct sp805_wdt *wdt = amba_get_drvdata(adev);
310
311 watchdog_unregister_device(&wdt->wdd);
Viresh Kumar4a516532012-03-12 09:52:16 +0530312 watchdog_set_drvdata(&wdt->wdd, NULL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530313}
314
Russell King60d6dd52012-11-08 10:51:03 +0000315static int __maybe_unused sp805_wdt_suspend(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530316{
Viresh Kumar4a516532012-03-12 09:52:16 +0530317 struct sp805_wdt *wdt = dev_get_drvdata(dev);
318
319 if (watchdog_active(&wdt->wdd))
320 return wdt_disable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530321
322 return 0;
323}
324
Russell King60d6dd52012-11-08 10:51:03 +0000325static int __maybe_unused sp805_wdt_resume(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530326{
Viresh Kumar4a516532012-03-12 09:52:16 +0530327 struct sp805_wdt *wdt = dev_get_drvdata(dev);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530328
Viresh Kumar4a516532012-03-12 09:52:16 +0530329 if (watchdog_active(&wdt->wdd))
330 return wdt_enable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530331
Viresh Kumar4a516532012-03-12 09:52:16 +0530332 return 0;
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530333}
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530334
335static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
336 sp805_wdt_resume);
337
Arvind Yadav05ce42f2017-08-23 22:04:08 +0530338static const struct amba_id sp805_wdt_ids[] = {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530339 {
340 .id = 0x00141805,
341 .mask = 0x00ffffff,
342 },
343 { 0, 0 },
344};
345
Dave Martin17885b02011-10-05 15:15:23 +0100346MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
347
Viresh KUMAR4a370272010-08-04 11:44:14 +0530348static struct amba_driver sp805_wdt_driver = {
349 .drv = {
350 .name = MODULE_NAME,
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530351 .pm = &sp805_wdt_dev_pm_ops,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530352 },
353 .id_table = sp805_wdt_ids,
354 .probe = sp805_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500355 .remove = sp805_wdt_remove,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530356};
357
viresh kumar9e5ed092012-03-15 10:40:38 +0100358module_amba_driver(sp805_wdt_driver);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530359
Viresh Kumarda899472015-07-17 16:23:50 -0700360MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
Viresh KUMAR4a370272010-08-04 11:44:14 +0530361MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
362MODULE_LICENSE("GPL");