2 * drivers/char/watchdog/sp805-wdt.c
4 * Watchdog driver for ARM SP805 watchdog module
6 * Copyright (C) 2010 ST Microelectronics
7 * Viresh Kumar <vireshk@kernel.org>
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.
14 #include <linux/acpi.h>
15 #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>
21 #include <linux/ioport.h>
22 #include <linux/kernel.h>
23 #include <linux/math64.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/watchdog.h>
33 /* default timeout in seconds */
34 #define DEFAULT_TIMEOUT 60
36 #define MODULE_NAME "sp805-wdt"
38 /* watchdog register offsets and masks */
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)
47 #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE)
48 #define WDTINTCLR 0x00C
51 #define INT_MASK (1 << 0)
53 #define UNLOCK 0x1ACCE551
54 #define LOCK 0x00000001
57 * struct sp805_wdt: sp805 wdt device structure
58 * @wdd: instance of struct watchdog_device
59 * @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
67 struct watchdog_device wdd
;
72 struct amba_device
*adev
;
73 unsigned int load_val
;
76 static bool nowayout
= WATCHDOG_NOWAYOUT
;
77 module_param(nowayout
, bool, 0);
78 MODULE_PARM_DESC(nowayout
,
79 "Set to 1 to keep watchdog running after device release");
81 /* returns true if wdt is running; otherwise returns false */
82 static bool wdt_is_running(struct watchdog_device
*wdd
)
84 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
85 u32 wdtcontrol
= readl_relaxed(wdt
->base
+ WDTCONTROL
);
87 return (wdtcontrol
& ENABLE_MASK
) == ENABLE_MASK
;
90 /* This routine finds load value that will reset system in required timout */
91 static int wdt_setload(struct watchdog_device
*wdd
, unsigned int timeout
)
93 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
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
101 * interrupt already occurred then it resets the system. This is why
102 * load is half of what should be required.
104 load
= div_u64(rate
, 2) * timeout
- 1;
106 load
= (load
> LOAD_MAX
) ? LOAD_MAX
: load
;
107 load
= (load
< LOAD_MIN
) ? LOAD_MIN
: load
;
109 spin_lock(&wdt
->lock
);
110 wdt
->load_val
= load
;
111 /* roundup timeout to closest positive integer value */
112 wdd
->timeout
= div_u64((load
+ 1) * 2 + (rate
/ 2), rate
);
113 spin_unlock(&wdt
->lock
);
118 /* returns number of seconds left for reset to occur */
119 static unsigned int wdt_timeleft(struct watchdog_device
*wdd
)
121 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
124 spin_lock(&wdt
->lock
);
125 load
= readl_relaxed(wdt
->base
+ WDTVALUE
);
127 /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
128 if (!(readl_relaxed(wdt
->base
+ WDTRIS
) & INT_MASK
))
129 load
+= wdt
->load_val
+ 1;
130 spin_unlock(&wdt
->lock
);
132 return div_u64(load
, wdt
->rate
);
136 wdt_restart(struct watchdog_device
*wdd
, unsigned long mode
, void *cmd
)
138 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
140 writel_relaxed(0, wdt
->base
+ WDTCONTROL
);
141 writel_relaxed(0, wdt
->base
+ WDTLOAD
);
142 writel_relaxed(INT_ENABLE
| RESET_ENABLE
, wdt
->base
+ WDTCONTROL
);
147 static int wdt_config(struct watchdog_device
*wdd
, bool ping
)
149 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
154 ret
= clk_prepare_enable(wdt
->clk
);
156 dev_err(&wdt
->adev
->dev
, "clock enable fail");
161 spin_lock(&wdt
->lock
);
163 writel_relaxed(UNLOCK
, wdt
->base
+ WDTLOCK
);
164 writel_relaxed(wdt
->load_val
, wdt
->base
+ WDTLOAD
);
165 writel_relaxed(INT_MASK
, wdt
->base
+ WDTINTCLR
);
168 writel_relaxed(INT_ENABLE
| RESET_ENABLE
, wdt
->base
+
171 writel_relaxed(LOCK
, wdt
->base
+ WDTLOCK
);
173 /* Flush posted writes. */
174 readl_relaxed(wdt
->base
+ WDTLOCK
);
175 spin_unlock(&wdt
->lock
);
180 static int wdt_ping(struct watchdog_device
*wdd
)
182 return wdt_config(wdd
, true);
185 /* enables watchdog timers reset */
186 static int wdt_enable(struct watchdog_device
*wdd
)
188 return wdt_config(wdd
, false);
191 /* disables watchdog timers reset */
192 static int wdt_disable(struct watchdog_device
*wdd
)
194 struct sp805_wdt
*wdt
= watchdog_get_drvdata(wdd
);
196 spin_lock(&wdt
->lock
);
198 writel_relaxed(UNLOCK
, wdt
->base
+ WDTLOCK
);
199 writel_relaxed(0, wdt
->base
+ WDTCONTROL
);
200 writel_relaxed(LOCK
, wdt
->base
+ WDTLOCK
);
202 /* Flush posted writes. */
203 readl_relaxed(wdt
->base
+ WDTLOCK
);
204 spin_unlock(&wdt
->lock
);
206 clk_disable_unprepare(wdt
->clk
);
211 static const struct watchdog_info wdt_info
= {
212 .options
= WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
213 .identity
= MODULE_NAME
,
216 static const struct watchdog_ops wdt_ops
= {
217 .owner
= THIS_MODULE
,
221 .set_timeout
= wdt_setload
,
222 .get_timeleft
= wdt_timeleft
,
223 .restart
= wdt_restart
,
227 sp805_wdt_probe(struct amba_device
*adev
, const struct amba_id
*id
)
229 struct sp805_wdt
*wdt
;
232 wdt
= devm_kzalloc(&adev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
238 wdt
->base
= devm_ioremap_resource(&adev
->dev
, &adev
->res
);
239 if (IS_ERR(wdt
->base
))
240 return PTR_ERR(wdt
->base
);
242 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
);
248 wdt
->rate
= clk_get_rate(wdt
->clk
);
249 } else if (has_acpi_companion(&adev
->dev
)) {
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.
255 device_property_read_u64(&adev
->dev
, "clock-frequency",
258 dev_err(&adev
->dev
, "no clock-frequency property\n");
264 wdt
->wdd
.info
= &wdt_info
;
265 wdt
->wdd
.ops
= &wdt_ops
;
266 wdt
->wdd
.parent
= &adev
->dev
;
268 spin_lock_init(&wdt
->lock
);
269 watchdog_set_nowayout(&wdt
->wdd
, nowayout
);
270 watchdog_set_drvdata(&wdt
->wdd
, wdt
);
271 watchdog_set_restart_priority(&wdt
->wdd
, 128);
274 * If 'timeout-sec' devicetree property is specified, use that.
275 * Otherwise, use DEFAULT_TIMEOUT
277 wdt
->wdd
.timeout
= DEFAULT_TIMEOUT
;
278 watchdog_init_timeout(&wdt
->wdd
, 0, &adev
->dev
);
279 wdt_setload(&wdt
->wdd
, wdt
->wdd
.timeout
);
282 * If HW is already running, enable/reset the wdt and set the running
283 * bit to tell the wdt subsystem
285 if (wdt_is_running(&wdt
->wdd
)) {
286 wdt_enable(&wdt
->wdd
);
287 set_bit(WDOG_HW_RUNNING
, &wdt
->wdd
.status
);
290 ret
= watchdog_register_device(&wdt
->wdd
);
292 dev_err(&adev
->dev
, "watchdog_register_device() failed: %d\n",
296 amba_set_drvdata(adev
, wdt
);
298 dev_info(&adev
->dev
, "registration successful\n");
302 dev_err(&adev
->dev
, "Probe Failed!!!\n");
306 static int sp805_wdt_remove(struct amba_device
*adev
)
308 struct sp805_wdt
*wdt
= amba_get_drvdata(adev
);
310 watchdog_unregister_device(&wdt
->wdd
);
311 watchdog_set_drvdata(&wdt
->wdd
, NULL
);
316 static int __maybe_unused
sp805_wdt_suspend(struct device
*dev
)
318 struct sp805_wdt
*wdt
= dev_get_drvdata(dev
);
320 if (watchdog_active(&wdt
->wdd
))
321 return wdt_disable(&wdt
->wdd
);
326 static int __maybe_unused
sp805_wdt_resume(struct device
*dev
)
328 struct sp805_wdt
*wdt
= dev_get_drvdata(dev
);
330 if (watchdog_active(&wdt
->wdd
))
331 return wdt_enable(&wdt
->wdd
);
336 static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops
, sp805_wdt_suspend
,
339 static const struct amba_id sp805_wdt_ids
[] = {
347 MODULE_DEVICE_TABLE(amba
, sp805_wdt_ids
);
349 static struct amba_driver sp805_wdt_driver
= {
352 .pm
= &sp805_wdt_dev_pm_ops
,
354 .id_table
= sp805_wdt_ids
,
355 .probe
= sp805_wdt_probe
,
356 .remove
= sp805_wdt_remove
,
359 module_amba_driver(sp805_wdt_driver
);
361 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
362 MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
363 MODULE_LICENSE("GPL");