blob: 072986d461b765df24c06a8ab09d3f6e2857c48d [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
Srinath Mannamdc0e4a32018-07-26 10:28:42 +053014#include <linux/acpi.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053015#include <linux/device.h>
16#include <linux/resource.h>
17#include <linux/amba/bus.h>
18#include <linux/bitops.h>
19#include <linux/clk.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053020#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/kernel.h>
23#include <linux/math64.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053024#include <linux/module.h>
25#include <linux/moduleparam.h>
Srinath Mannamdc0e4a32018-07-26 10:28:42 +053026#include <linux/of.h>
Viresh Kumar16ac4ab2012-02-24 15:12:37 +053027#include <linux/pm.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053028#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/types.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053031#include <linux/watchdog.h>
32
33/* default timeout in seconds */
34#define DEFAULT_TIMEOUT 60
35
36#define MODULE_NAME "sp805-wdt"
37
38/* watchdog register offsets and masks */
39#define WDTLOAD 0x000
40 #define LOAD_MIN 0x00000001
41 #define LOAD_MAX 0xFFFFFFFF
42#define WDTVALUE 0x004
43#define WDTCONTROL 0x008
44 /* control register masks */
45 #define INT_ENABLE (1 << 0)
46 #define RESET_ENABLE (1 << 1)
Ray Juifa5072e2018-05-28 11:01:35 -070047 #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE)
Viresh KUMAR4a370272010-08-04 11:44:14 +053048#define WDTINTCLR 0x00C
49#define WDTRIS 0x010
50#define WDTMIS 0x014
51 #define INT_MASK (1 << 0)
52#define WDTLOCK 0xC00
53 #define UNLOCK 0x1ACCE551
54 #define LOCK 0x00000001
55
56/**
57 * struct sp805_wdt: sp805 wdt device structure
Viresh Kumar4a516532012-03-12 09:52:16 +053058 * @wdd: instance of struct watchdog_device
Viresh Kumarbfae14b2012-03-12 09:52:13 +053059 * @lock: spin lock protecting dev structure and io access
60 * @base: base address of wdt
61 * @clk: clock structure of wdt
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 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
140 writel_relaxed(0, wdt->base + WDTCONTROL);
141 writel_relaxed(0, wdt->base + WDTLOAD);
142 writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base + WDTCONTROL);
143
144 return 0;
145}
146
Viresh Kumar4a516532012-03-12 09:52:16 +0530147static int wdt_config(struct watchdog_device *wdd, bool ping)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530148{
Viresh Kumar4a516532012-03-12 09:52:16 +0530149 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
150 int ret;
151
152 if (!ping) {
Viresh Kumard9df0ef2012-03-12 09:52:17 +0530153
Julia Lawall63fbbc12012-08-26 18:01:02 +0200154 ret = clk_prepare_enable(wdt->clk);
Viresh Kumar4a516532012-03-12 09:52:16 +0530155 if (ret) {
156 dev_err(&wdt->adev->dev, "clock enable fail");
157 return ret;
158 }
159 }
160
Viresh KUMAR4a370272010-08-04 11:44:14 +0530161 spin_lock(&wdt->lock);
162
Viresh Kumard2e89192012-03-12 09:52:14 +0530163 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
164 writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
Sandeep Tripathy55e07172016-01-19 14:44:49 +0530165 writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
Viresh Kumar4a516532012-03-12 09:52:16 +0530166
Sandeep Tripathy55e07172016-01-19 14:44:49 +0530167 if (!ping)
Viresh Kumar4a516532012-03-12 09:52:16 +0530168 writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
169 WDTCONTROL);
Viresh Kumar4a516532012-03-12 09:52:16 +0530170
Viresh Kumard2e89192012-03-12 09:52:14 +0530171 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530172
Nick Bowler081d83a2011-07-15 11:04:02 -0400173 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530174 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530175 spin_unlock(&wdt->lock);
Viresh Kumar4a516532012-03-12 09:52:16 +0530176
177 return 0;
178}
179
180static int wdt_ping(struct watchdog_device *wdd)
181{
182 return wdt_config(wdd, true);
183}
184
185/* enables watchdog timers reset */
186static int wdt_enable(struct watchdog_device *wdd)
187{
188 return wdt_config(wdd, false);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530189}
190
191/* disables watchdog timers reset */
Viresh Kumar4a516532012-03-12 09:52:16 +0530192static int wdt_disable(struct watchdog_device *wdd)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530193{
Viresh Kumar4a516532012-03-12 09:52:16 +0530194 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
195
Viresh KUMAR4a370272010-08-04 11:44:14 +0530196 spin_lock(&wdt->lock);
197
Viresh Kumard2e89192012-03-12 09:52:14 +0530198 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
199 writel_relaxed(0, wdt->base + WDTCONTROL);
200 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530201
Nick Bowler081d83a2011-07-15 11:04:02 -0400202 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530203 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530204 spin_unlock(&wdt->lock);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530205
Julia Lawall63fbbc12012-08-26 18:01:02 +0200206 clk_disable_unprepare(wdt->clk);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530207
208 return 0;
209}
210
Viresh Kumar4a516532012-03-12 09:52:16 +0530211static const struct watchdog_info wdt_info = {
212 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
213 .identity = MODULE_NAME,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530214};
215
Viresh Kumar4a516532012-03-12 09:52:16 +0530216static const struct watchdog_ops wdt_ops = {
217 .owner = THIS_MODULE,
218 .start = wdt_enable,
219 .stop = wdt_disable,
220 .ping = wdt_ping,
221 .set_timeout = wdt_setload,
222 .get_timeleft = wdt_timeleft,
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900223 .restart = wdt_restart,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530224};
225
Bill Pemberton2d991a12012-11-19 13:21:41 -0500226static int
Russell Kingaa25afa2011-02-19 15:55:00 +0000227sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530228{
Viresh Kumar4a516532012-03-12 09:52:16 +0530229 struct sp805_wdt *wdt;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530230 int ret = 0;
231
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530232 wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530233 if (!wdt) {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530234 ret = -ENOMEM;
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530235 goto err;
236 }
237
Jingoo Han9d11e4f82014-02-11 21:45:56 +0900238 wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
239 if (IS_ERR(wdt->base))
240 return PTR_ERR(wdt->base);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530241
Srinath Mannamdc0e4a32018-07-26 10:28:42 +0530242 if (adev->dev.of_node) {
243 wdt->clk = devm_clk_get(&adev->dev, NULL);
244 if (IS_ERR(wdt->clk)) {
245 dev_err(&adev->dev, "Clock not found\n");
246 return PTR_ERR(wdt->clk);
247 }
248 wdt->rate = clk_get_rate(wdt->clk);
249 } else if (has_acpi_companion(&adev->dev)) {
250 /*
251 * When Driver probe with ACPI device, clock devices
252 * are not available, so watchdog rate get from
253 * clock-frequency property given in _DSD object.
254 */
255 device_property_read_u64(&adev->dev, "clock-frequency",
256 &wdt->rate);
257 if (!wdt->rate) {
258 dev_err(&adev->dev, "no clock-frequency property\n");
259 return -ENODEV;
260 }
Viresh KUMAR4a370272010-08-04 11:44:14 +0530261 }
262
263 wdt->adev = adev;
Viresh Kumar4a516532012-03-12 09:52:16 +0530264 wdt->wdd.info = &wdt_info;
265 wdt->wdd.ops = &wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530266 wdt->wdd.parent = &adev->dev;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530267
Viresh Kumar4a516532012-03-12 09:52:16 +0530268 spin_lock_init(&wdt->lock);
269 watchdog_set_nowayout(&wdt->wdd, nowayout);
270 watchdog_set_drvdata(&wdt->wdd, wdt);
Jongsung Kim6c5c0d42018-05-04 15:05:03 +0900271 watchdog_set_restart_priority(&wdt->wdd, 128);
Ray Juib8008852018-05-28 11:01:34 -0700272
273 /*
274 * If 'timeout-sec' devicetree property is specified, use that.
275 * Otherwise, use DEFAULT_TIMEOUT
276 */
277 wdt->wdd.timeout = DEFAULT_TIMEOUT;
278 watchdog_init_timeout(&wdt->wdd, 0, &adev->dev);
279 wdt_setload(&wdt->wdd, wdt->wdd.timeout);
Viresh Kumar4a516532012-03-12 09:52:16 +0530280
Ray Juifa5072e2018-05-28 11:01:35 -0700281 /*
282 * If HW is already running, enable/reset the wdt and set the running
283 * bit to tell the wdt subsystem
284 */
285 if (wdt_is_running(&wdt->wdd)) {
286 wdt_enable(&wdt->wdd);
287 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
288 }
289
Viresh Kumar4a516532012-03-12 09:52:16 +0530290 ret = watchdog_register_device(&wdt->wdd);
291 if (ret) {
292 dev_err(&adev->dev, "watchdog_register_device() failed: %d\n",
293 ret);
Jingoo Han07bf9712013-04-29 18:16:49 +0900294 goto err;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530295 }
Viresh Kumar4a516532012-03-12 09:52:16 +0530296 amba_set_drvdata(adev, wdt);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530297
298 dev_info(&adev->dev, "registration successful\n");
299 return 0;
300
Viresh KUMAR4a370272010-08-04 11:44:14 +0530301err:
302 dev_err(&adev->dev, "Probe Failed!!!\n");
303 return ret;
304}
305
Bill Pemberton4b12b892012-11-19 13:26:24 -0500306static int sp805_wdt_remove(struct amba_device *adev)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530307{
Viresh Kumar4a516532012-03-12 09:52:16 +0530308 struct sp805_wdt *wdt = amba_get_drvdata(adev);
309
310 watchdog_unregister_device(&wdt->wdd);
Viresh Kumar4a516532012-03-12 09:52:16 +0530311 watchdog_set_drvdata(&wdt->wdd, NULL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530312
313 return 0;
314}
315
Russell King60d6dd52012-11-08 10:51:03 +0000316static int __maybe_unused sp805_wdt_suspend(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530317{
Viresh Kumar4a516532012-03-12 09:52:16 +0530318 struct sp805_wdt *wdt = dev_get_drvdata(dev);
319
320 if (watchdog_active(&wdt->wdd))
321 return wdt_disable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530322
323 return 0;
324}
325
Russell King60d6dd52012-11-08 10:51:03 +0000326static int __maybe_unused sp805_wdt_resume(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530327{
Viresh Kumar4a516532012-03-12 09:52:16 +0530328 struct sp805_wdt *wdt = dev_get_drvdata(dev);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530329
Viresh Kumar4a516532012-03-12 09:52:16 +0530330 if (watchdog_active(&wdt->wdd))
331 return wdt_enable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530332
Viresh Kumar4a516532012-03-12 09:52:16 +0530333 return 0;
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530334}
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530335
336static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
337 sp805_wdt_resume);
338
Arvind Yadav05ce42f2017-08-23 22:04:08 +0530339static const struct amba_id sp805_wdt_ids[] = {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530340 {
341 .id = 0x00141805,
342 .mask = 0x00ffffff,
343 },
344 { 0, 0 },
345};
346
Dave Martin17885b02011-10-05 15:15:23 +0100347MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
348
Viresh KUMAR4a370272010-08-04 11:44:14 +0530349static struct amba_driver sp805_wdt_driver = {
350 .drv = {
351 .name = MODULE_NAME,
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530352 .pm = &sp805_wdt_dev_pm_ops,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530353 },
354 .id_table = sp805_wdt_ids,
355 .probe = sp805_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500356 .remove = sp805_wdt_remove,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530357};
358
viresh kumar9e5ed092012-03-15 10:40:38 +0100359module_amba_driver(sp805_wdt_driver);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530360
Viresh Kumarda899472015-07-17 16:23:50 -0700361MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
Viresh KUMAR4a370272010-08-04 11:44:14 +0530362MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
363MODULE_LICENSE("GPL");