Commit | Line | Data |
---|---|---|
4a370278 VK |
1 | /* |
2 | * drivers/char/watchdog/sp805-wdt.c | |
3 | * | |
4 | * Watchdog driver for ARM SP805 watchdog module | |
5 | * | |
6 | * Copyright (C) 2010 ST Microelectronics | |
da89947b | 7 | * Viresh Kumar <vireshk@kernel.org> |
4a370278 VK |
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 | ||
dc0e4a3b | 14 | #include <linux/acpi.h> |
4a370278 VK |
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> | |
4a370278 VK |
20 | #include <linux/io.h> |
21 | #include <linux/ioport.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/math64.h> | |
4a370278 VK |
24 | #include <linux/module.h> |
25 | #include <linux/moduleparam.h> | |
dc0e4a3b | 26 | #include <linux/of.h> |
16ac4abe | 27 | #include <linux/pm.h> |
4a370278 VK |
28 | #include <linux/slab.h> |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/types.h> | |
4a370278 VK |
31 | #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) | |
fa5072ed | 47 | #define ENABLE_MASK (INT_ENABLE | RESET_ENABLE) |
4a370278 VK |
48 | #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 | |
4a516539 | 58 | * @wdd: instance of struct watchdog_device |
bfae14b6 VK |
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 | |
4a370278 VK |
65 | */ |
66 | struct sp805_wdt { | |
4a516539 | 67 | struct watchdog_device wdd; |
4a370278 VK |
68 | spinlock_t lock; |
69 | void __iomem *base; | |
70 | struct clk *clk; | |
dc0e4a3b | 71 | u64 rate; |
4a370278 | 72 | struct amba_device *adev; |
4a370278 | 73 | unsigned int load_val; |
4a370278 VK |
74 | }; |
75 | ||
86a1e189 | 76 | static bool nowayout = WATCHDOG_NOWAYOUT; |
4a516539 VK |
77 | module_param(nowayout, bool, 0); |
78 | MODULE_PARM_DESC(nowayout, | |
79 | "Set to 1 to keep watchdog running after device release"); | |
4a370278 | 80 | |
fa5072ed RJ |
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 | ||
4a370278 | 90 | /* This routine finds load value that will reset system in required timout */ |
4a516539 | 91 | static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout) |
4a370278 | 92 | { |
4a516539 | 93 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
4a370278 VK |
94 | u64 load, rate; |
95 | ||
dc0e4a3b | 96 | rate = wdt->rate; |
4a370278 VK |
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 | |
25985edc | 101 | * interrupt already occurred then it resets the system. This is why |
4a370278 VK |
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 */ | |
938626d9 | 112 | wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate); |
4a370278 | 113 | spin_unlock(&wdt->lock); |
4a516539 VK |
114 | |
115 | return 0; | |
4a370278 VK |
116 | } |
117 | ||
118 | /* returns number of seconds left for reset to occur */ | |
4a516539 | 119 | static unsigned int wdt_timeleft(struct watchdog_device *wdd) |
4a370278 | 120 | { |
4a516539 | 121 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
dc0e4a3b | 122 | u64 load; |
4a370278 VK |
123 | |
124 | spin_lock(&wdt->lock); | |
d2e8919b | 125 | load = readl_relaxed(wdt->base + WDTVALUE); |
4a370278 VK |
126 | |
127 | /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */ | |
d2e8919b | 128 | if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK)) |
4a370278 VK |
129 | load += wdt->load_val + 1; |
130 | spin_unlock(&wdt->lock); | |
131 | ||
dc0e4a3b | 132 | return div_u64(load, wdt->rate); |
4a370278 VK |
133 | } |
134 | ||
6c5c0d48 JK |
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 | ||
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 | ||
4a516539 | 147 | static int wdt_config(struct watchdog_device *wdd, bool ping) |
4a370278 | 148 | { |
4a516539 VK |
149 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
150 | int ret; | |
151 | ||
152 | if (!ping) { | |
d9df0ef1 | 153 | |
63fbbc16 | 154 | ret = clk_prepare_enable(wdt->clk); |
4a516539 VK |
155 | if (ret) { |
156 | dev_err(&wdt->adev->dev, "clock enable fail"); | |
157 | return ret; | |
158 | } | |
159 | } | |
160 | ||
4a370278 VK |
161 | spin_lock(&wdt->lock); |
162 | ||
d2e8919b VK |
163 | writel_relaxed(UNLOCK, wdt->base + WDTLOCK); |
164 | writel_relaxed(wdt->load_val, wdt->base + WDTLOAD); | |
55e07177 | 165 | writel_relaxed(INT_MASK, wdt->base + WDTINTCLR); |
4a370278 | 166 | |
55e07177 | 167 | if (!ping) |
4a516539 VK |
168 | writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base + |
169 | WDTCONTROL); | |
4a370278 | 170 | |
d2e8919b | 171 | writel_relaxed(LOCK, wdt->base + WDTLOCK); |
4a370278 | 172 | |
081d83a3 | 173 | /* Flush posted writes. */ |
d2e8919b | 174 | readl_relaxed(wdt->base + WDTLOCK); |
4a370278 | 175 | spin_unlock(&wdt->lock); |
4a516539 VK |
176 | |
177 | return 0; | |
4a370278 VK |
178 | } |
179 | ||
4a516539 | 180 | static int wdt_ping(struct watchdog_device *wdd) |
4a370278 | 181 | { |
4a516539 | 182 | return wdt_config(wdd, true); |
4a370278 VK |
183 | } |
184 | ||
4a516539 VK |
185 | /* enables watchdog timers reset */ |
186 | static int wdt_enable(struct watchdog_device *wdd) | |
4a370278 | 187 | { |
4a516539 | 188 | return wdt_config(wdd, false); |
4a370278 VK |
189 | } |
190 | ||
4a516539 VK |
191 | /* disables watchdog timers reset */ |
192 | static int wdt_disable(struct watchdog_device *wdd) | |
4a370278 | 193 | { |
4a516539 | 194 | struct sp805_wdt *wdt = watchdog_get_drvdata(wdd); |
4a370278 | 195 | |
4a516539 | 196 | spin_lock(&wdt->lock); |
4a370278 | 197 | |
4a516539 VK |
198 | writel_relaxed(UNLOCK, wdt->base + WDTLOCK); |
199 | writel_relaxed(0, wdt->base + WDTCONTROL); | |
200 | writel_relaxed(LOCK, wdt->base + WDTLOCK); | |
4a370278 | 201 | |
4a516539 VK |
202 | /* Flush posted writes. */ |
203 | readl_relaxed(wdt->base + WDTLOCK); | |
204 | spin_unlock(&wdt->lock); | |
4a370278 | 205 | |
63fbbc16 | 206 | clk_disable_unprepare(wdt->clk); |
4a370278 VK |
207 | |
208 | return 0; | |
209 | } | |
210 | ||
4a516539 VK |
211 | static const struct watchdog_info wdt_info = { |
212 | .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | |
213 | .identity = MODULE_NAME, | |
4a370278 VK |
214 | }; |
215 | ||
4a516539 VK |
216 | static 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, | |
6c5c0d48 | 223 | .restart = wdt_restart, |
4a370278 VK |
224 | }; |
225 | ||
2d991a16 | 226 | static int |
aa25afad | 227 | sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id) |
4a370278 | 228 | { |
4a516539 | 229 | struct sp805_wdt *wdt; |
4a370278 VK |
230 | int ret = 0; |
231 | ||
fb35a5ad | 232 | wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL); |
4a370278 | 233 | if (!wdt) { |
4a370278 | 234 | ret = -ENOMEM; |
fb35a5ad VK |
235 | goto err; |
236 | } | |
237 | ||
9d11e4f8 JH |
238 | wdt->base = devm_ioremap_resource(&adev->dev, &adev->res); |
239 | if (IS_ERR(wdt->base)) | |
240 | return PTR_ERR(wdt->base); | |
4a370278 | 241 | |
dc0e4a3b SM |
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); | |
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 | } | |
4a370278 VK |
261 | } |
262 | ||
263 | wdt->adev = adev; | |
4a516539 VK |
264 | wdt->wdd.info = &wdt_info; |
265 | wdt->wdd.ops = &wdt_ops; | |
6551881c | 266 | wdt->wdd.parent = &adev->dev; |
4a516539 | 267 | |
4a370278 | 268 | spin_lock_init(&wdt->lock); |
4a516539 VK |
269 | watchdog_set_nowayout(&wdt->wdd, nowayout); |
270 | watchdog_set_drvdata(&wdt->wdd, wdt); | |
6c5c0d48 | 271 | watchdog_set_restart_priority(&wdt->wdd, 128); |
b8008858 RJ |
272 | |
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); | |
4a370278 | 280 | |
fa5072ed RJ |
281 | /* |
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 | ||
4a516539 VK |
290 | ret = watchdog_register_device(&wdt->wdd); |
291 | if (ret) { | |
292 | dev_err(&adev->dev, "watchdog_register_device() failed: %d\n", | |
293 | ret); | |
07bf971a | 294 | goto err; |
4a370278 | 295 | } |
4a516539 | 296 | amba_set_drvdata(adev, wdt); |
4a370278 VK |
297 | |
298 | dev_info(&adev->dev, "registration successful\n"); | |
299 | return 0; | |
300 | ||
4a370278 VK |
301 | err: |
302 | dev_err(&adev->dev, "Probe Failed!!!\n"); | |
303 | return ret; | |
304 | } | |
305 | ||
4b12b896 | 306 | static int sp805_wdt_remove(struct amba_device *adev) |
4a370278 | 307 | { |
4a516539 VK |
308 | struct sp805_wdt *wdt = amba_get_drvdata(adev); |
309 | ||
310 | watchdog_unregister_device(&wdt->wdd); | |
4a516539 | 311 | watchdog_set_drvdata(&wdt->wdd, NULL); |
4a370278 VK |
312 | |
313 | return 0; | |
314 | } | |
315 | ||
60d6dd53 | 316 | static int __maybe_unused sp805_wdt_suspend(struct device *dev) |
16ac4abe | 317 | { |
4a516539 VK |
318 | struct sp805_wdt *wdt = dev_get_drvdata(dev); |
319 | ||
320 | if (watchdog_active(&wdt->wdd)) | |
321 | return wdt_disable(&wdt->wdd); | |
16ac4abe VK |
322 | |
323 | return 0; | |
324 | } | |
325 | ||
60d6dd53 | 326 | static int __maybe_unused sp805_wdt_resume(struct device *dev) |
16ac4abe | 327 | { |
4a516539 | 328 | struct sp805_wdt *wdt = dev_get_drvdata(dev); |
16ac4abe | 329 | |
4a516539 VK |
330 | if (watchdog_active(&wdt->wdd)) |
331 | return wdt_enable(&wdt->wdd); | |
16ac4abe | 332 | |
4a516539 | 333 | return 0; |
16ac4abe | 334 | } |
16ac4abe VK |
335 | |
336 | static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend, | |
337 | sp805_wdt_resume); | |
338 | ||
05ce42ff | 339 | static const struct amba_id sp805_wdt_ids[] = { |
4a370278 VK |
340 | { |
341 | .id = 0x00141805, | |
342 | .mask = 0x00ffffff, | |
343 | }, | |
344 | { 0, 0 }, | |
345 | }; | |
346 | ||
17885b05 DM |
347 | MODULE_DEVICE_TABLE(amba, sp805_wdt_ids); |
348 | ||
4a370278 VK |
349 | static struct amba_driver sp805_wdt_driver = { |
350 | .drv = { | |
351 | .name = MODULE_NAME, | |
16ac4abe | 352 | .pm = &sp805_wdt_dev_pm_ops, |
4a370278 VK |
353 | }, |
354 | .id_table = sp805_wdt_ids, | |
355 | .probe = sp805_wdt_probe, | |
82268714 | 356 | .remove = sp805_wdt_remove, |
4a370278 VK |
357 | }; |
358 | ||
9e5ed094 | 359 | module_amba_driver(sp805_wdt_driver); |
4a370278 | 360 | |
da89947b | 361 | MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>"); |
4a370278 VK |
362 | MODULE_DESCRIPTION("ARM SP805 Watchdog Driver"); |
363 | MODULE_LICENSE("GPL"); |