1
2
3
4
5
6
7
8
9
10
11
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>
19#include <linux/io.h>
20#include <linux/ioport.h>
21#include <linux/kernel.h>
22#include <linux/math64.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/pm.h>
26#include <linux/property.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/types.h>
30#include <linux/watchdog.h>
31
32
33#define DEFAULT_TIMEOUT 60
34
35#define MODULE_NAME "sp805-wdt"
36
37
38#define WDTLOAD 0x000
39 #define LOAD_MIN 0x00000001
40 #define LOAD_MAX 0xFFFFFFFF
41#define WDTVALUE 0x004
42#define WDTCONTROL 0x008
43
44 #define INT_ENABLE (1 << 0)
45 #define RESET_ENABLE (1 << 1)
46 #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE)
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
57
58
59
60
61
62
63
64
65
66struct sp805_wdt {
67 struct watchdog_device wdd;
68 spinlock_t lock;
69 void __iomem *base;
70 struct clk *clk;
71 u64 rate;
72 struct amba_device *adev;
73 unsigned int load_val;
74};
75
76static bool nowayout = WATCHDOG_NOWAYOUT;
77module_param(nowayout, bool, 0);
78MODULE_PARM_DESC(nowayout,
79 "Set to 1 to keep watchdog running after device release");
80
81
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
90
91static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout)
92{
93 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
94 u64 load, rate;
95
96 rate = wdt->rate;
97
98
99
100
101
102
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
112 wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
113 spin_unlock(&wdt->lock);
114
115 return 0;
116}
117
118
119static unsigned int wdt_timeleft(struct watchdog_device *wdd)
120{
121 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
122 u64 load;
123
124 spin_lock(&wdt->lock);
125 load = readl_relaxed(wdt->base + WDTVALUE);
126
127
128 if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK))
129 load += wdt->load_val + 1;
130 spin_unlock(&wdt->lock);
131
132 return div_u64(load, wdt->rate);
133}
134
135static 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(UNLOCK, wdt->base + WDTLOCK);
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
145
146 readl_relaxed(wdt->base + WDTLOCK);
147
148 return 0;
149}
150
151static int wdt_config(struct watchdog_device *wdd, bool ping)
152{
153 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
154 int ret;
155
156 if (!ping) {
157
158 ret = clk_prepare_enable(wdt->clk);
159 if (ret) {
160 dev_err(&wdt->adev->dev, "clock enable fail");
161 return ret;
162 }
163 }
164
165 spin_lock(&wdt->lock);
166
167 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
168 writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
169 writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
170
171 if (!ping)
172 writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
173 WDTCONTROL);
174
175 writel_relaxed(LOCK, wdt->base + WDTLOCK);
176
177
178 readl_relaxed(wdt->base + WDTLOCK);
179 spin_unlock(&wdt->lock);
180
181 return 0;
182}
183
184static int wdt_ping(struct watchdog_device *wdd)
185{
186 return wdt_config(wdd, true);
187}
188
189
190static int wdt_enable(struct watchdog_device *wdd)
191{
192 return wdt_config(wdd, false);
193}
194
195
196static int wdt_disable(struct watchdog_device *wdd)
197{
198 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
199
200 spin_lock(&wdt->lock);
201
202 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
203 writel_relaxed(0, wdt->base + WDTCONTROL);
204 writel_relaxed(LOCK, wdt->base + WDTLOCK);
205
206
207 readl_relaxed(wdt->base + WDTLOCK);
208 spin_unlock(&wdt->lock);
209
210 clk_disable_unprepare(wdt->clk);
211
212 return 0;
213}
214
215static const struct watchdog_info wdt_info = {
216 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
217 .identity = MODULE_NAME,
218};
219
220static 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,
227 .restart = wdt_restart,
228};
229
230static int
231sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
232{
233 struct sp805_wdt *wdt;
234 u64 rate = 0;
235 int ret = 0;
236
237 wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
238 if (!wdt) {
239 ret = -ENOMEM;
240 goto err;
241 }
242
243 wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
244 if (IS_ERR(wdt->base))
245 return PTR_ERR(wdt->base);
246
247
248
249
250
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;
264 }
265
266 wdt->adev = adev;
267 wdt->wdd.info = &wdt_info;
268 wdt->wdd.ops = &wdt_ops;
269 wdt->wdd.parent = &adev->dev;
270
271 spin_lock_init(&wdt->lock);
272 watchdog_set_nowayout(&wdt->wdd, nowayout);
273 watchdog_set_drvdata(&wdt->wdd, wdt);
274 watchdog_set_restart_priority(&wdt->wdd, 128);
275 watchdog_stop_on_unregister(&wdt->wdd);
276
277
278
279
280
281 wdt->wdd.timeout = DEFAULT_TIMEOUT;
282 watchdog_init_timeout(&wdt->wdd, 0, &adev->dev);
283 wdt_setload(&wdt->wdd, wdt->wdd.timeout);
284
285
286
287
288
289 if (wdt_is_running(&wdt->wdd)) {
290 wdt_enable(&wdt->wdd);
291 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
292 }
293
294 watchdog_stop_on_reboot(&wdt->wdd);
295 ret = watchdog_register_device(&wdt->wdd);
296 if (ret)
297 goto err;
298 amba_set_drvdata(adev, wdt);
299
300 dev_info(&adev->dev, "registration successful\n");
301 return 0;
302
303err:
304 dev_err(&adev->dev, "Probe Failed!!!\n");
305 return ret;
306}
307
308static void sp805_wdt_remove(struct amba_device *adev)
309{
310 struct sp805_wdt *wdt = amba_get_drvdata(adev);
311
312 watchdog_unregister_device(&wdt->wdd);
313 watchdog_set_drvdata(&wdt->wdd, NULL);
314}
315
316static int __maybe_unused sp805_wdt_suspend(struct device *dev)
317{
318 struct sp805_wdt *wdt = dev_get_drvdata(dev);
319
320 if (watchdog_active(&wdt->wdd))
321 return wdt_disable(&wdt->wdd);
322
323 return 0;
324}
325
326static int __maybe_unused sp805_wdt_resume(struct device *dev)
327{
328 struct sp805_wdt *wdt = dev_get_drvdata(dev);
329
330 if (watchdog_active(&wdt->wdd))
331 return wdt_enable(&wdt->wdd);
332
333 return 0;
334}
335
336static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
337 sp805_wdt_resume);
338
339static const struct amba_id sp805_wdt_ids[] = {
340 {
341 .id = 0x00141805,
342 .mask = 0x00ffffff,
343 },
344 { 0, 0 },
345};
346
347MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
348
349static struct amba_driver sp805_wdt_driver = {
350 .drv = {
351 .name = MODULE_NAME,
352 .pm = &sp805_wdt_dev_pm_ops,
353 },
354 .id_table = sp805_wdt_ids,
355 .probe = sp805_wdt_probe,
356 .remove = sp805_wdt_remove,
357};
358
359module_amba_driver(sp805_wdt_driver);
360
361MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
362MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
363MODULE_LICENSE("GPL");
364