treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / watchdog / kempld_wdt.c
blob40bd518ed87387a2f28b2d3e2ab3fd8002565df7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kontron PLD watchdog driver
5 * Copyright (c) 2010-2013 Kontron Europe GmbH
6 * Author: Michael Brunner <michael.brunner@kontron.com>
8 * Note: From the PLD watchdog point of view timeout and pretimeout are
9 * defined differently than in the kernel.
10 * First the pretimeout stage runs out before the timeout stage gets
11 * active.
13 * Kernel/API: P-----| pretimeout
14 * |-----------------------T timeout
15 * Watchdog: |-----------------P pretimeout_stage
16 * |-----T timeout_stage
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/uaccess.h>
22 #include <linux/watchdog.h>
23 #include <linux/platform_device.h>
24 #include <linux/mfd/kempld.h>
26 #define KEMPLD_WDT_STAGE_TIMEOUT(x) (0x1b + (x) * 4)
27 #define KEMPLD_WDT_STAGE_CFG(x) (0x18 + (x))
28 #define STAGE_CFG_GET_PRESCALER(x) (((x) & 0x30) >> 4)
29 #define STAGE_CFG_SET_PRESCALER(x) (((x) & 0x3) << 4)
30 #define STAGE_CFG_PRESCALER_MASK 0x30
31 #define STAGE_CFG_ACTION_MASK 0x7
32 #define STAGE_CFG_ASSERT (1 << 3)
34 #define KEMPLD_WDT_MAX_STAGES 2
35 #define KEMPLD_WDT_KICK 0x16
36 #define KEMPLD_WDT_CFG 0x17
37 #define KEMPLD_WDT_CFG_ENABLE 0x10
38 #define KEMPLD_WDT_CFG_ENABLE_LOCK 0x8
39 #define KEMPLD_WDT_CFG_GLOBAL_LOCK 0x80
41 enum {
42 ACTION_NONE = 0,
43 ACTION_RESET,
44 ACTION_NMI,
45 ACTION_SMI,
46 ACTION_SCI,
47 ACTION_DELAY,
50 enum {
51 STAGE_TIMEOUT = 0,
52 STAGE_PRETIMEOUT,
55 enum {
56 PRESCALER_21 = 0,
57 PRESCALER_17,
58 PRESCALER_12,
61 static const u32 kempld_prescaler[] = {
62 [PRESCALER_21] = (1 << 21) - 1,
63 [PRESCALER_17] = (1 << 17) - 1,
64 [PRESCALER_12] = (1 << 12) - 1,
68 struct kempld_wdt_stage {
69 unsigned int id;
70 u32 mask;
73 struct kempld_wdt_data {
74 struct kempld_device_data *pld;
75 struct watchdog_device wdd;
76 unsigned int pretimeout;
77 struct kempld_wdt_stage stage[KEMPLD_WDT_MAX_STAGES];
78 #ifdef CONFIG_PM
79 u8 pm_status_store;
80 #endif
83 #define DEFAULT_TIMEOUT 30 /* seconds */
84 #define DEFAULT_PRETIMEOUT 0
86 static unsigned int timeout = DEFAULT_TIMEOUT;
87 module_param(timeout, uint, 0);
88 MODULE_PARM_DESC(timeout,
89 "Watchdog timeout in seconds. (>=0, default="
90 __MODULE_STRING(DEFAULT_TIMEOUT) ")");
92 static unsigned int pretimeout = DEFAULT_PRETIMEOUT;
93 module_param(pretimeout, uint, 0);
94 MODULE_PARM_DESC(pretimeout,
95 "Watchdog pretimeout in seconds. (>=0, default="
96 __MODULE_STRING(DEFAULT_PRETIMEOUT) ")");
98 static bool nowayout = WATCHDOG_NOWAYOUT;
99 module_param(nowayout, bool, 0);
100 MODULE_PARM_DESC(nowayout,
101 "Watchdog cannot be stopped once started (default="
102 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
104 static int kempld_wdt_set_stage_action(struct kempld_wdt_data *wdt_data,
105 struct kempld_wdt_stage *stage,
106 u8 action)
108 struct kempld_device_data *pld = wdt_data->pld;
109 u8 stage_cfg;
111 if (!stage || !stage->mask)
112 return -EINVAL;
114 kempld_get_mutex(pld);
115 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
116 stage_cfg &= ~STAGE_CFG_ACTION_MASK;
117 stage_cfg |= (action & STAGE_CFG_ACTION_MASK);
119 if (action == ACTION_RESET)
120 stage_cfg |= STAGE_CFG_ASSERT;
121 else
122 stage_cfg &= ~STAGE_CFG_ASSERT;
124 kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
125 kempld_release_mutex(pld);
127 return 0;
130 static int kempld_wdt_set_stage_timeout(struct kempld_wdt_data *wdt_data,
131 struct kempld_wdt_stage *stage,
132 unsigned int timeout)
134 struct kempld_device_data *pld = wdt_data->pld;
135 u32 prescaler;
136 u64 stage_timeout64;
137 u32 stage_timeout;
138 u32 remainder;
139 u8 stage_cfg;
141 prescaler = kempld_prescaler[PRESCALER_21];
143 if (!stage)
144 return -EINVAL;
146 stage_timeout64 = (u64)timeout * pld->pld_clock;
147 remainder = do_div(stage_timeout64, prescaler);
148 if (remainder)
149 stage_timeout64++;
151 if (stage_timeout64 > stage->mask)
152 return -EINVAL;
154 stage_timeout = stage_timeout64 & stage->mask;
156 kempld_get_mutex(pld);
157 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
158 stage_cfg &= ~STAGE_CFG_PRESCALER_MASK;
159 stage_cfg |= STAGE_CFG_SET_PRESCALER(PRESCALER_21);
160 kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
161 kempld_write32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id),
162 stage_timeout);
163 kempld_release_mutex(pld);
165 return 0;
169 * kempld_get_mutex must be called prior to calling this function.
171 static unsigned int kempld_wdt_get_timeout(struct kempld_wdt_data *wdt_data,
172 struct kempld_wdt_stage *stage)
174 struct kempld_device_data *pld = wdt_data->pld;
175 unsigned int timeout;
176 u64 stage_timeout;
177 u32 prescaler;
178 u32 remainder;
179 u8 stage_cfg;
181 if (!stage->mask)
182 return 0;
184 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
185 stage_timeout = kempld_read32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id));
186 prescaler = kempld_prescaler[STAGE_CFG_GET_PRESCALER(stage_cfg)];
188 stage_timeout = (stage_timeout & stage->mask) * prescaler;
189 remainder = do_div(stage_timeout, pld->pld_clock);
190 if (remainder)
191 stage_timeout++;
193 timeout = stage_timeout;
194 WARN_ON_ONCE(timeout != stage_timeout);
196 return timeout;
199 static int kempld_wdt_set_timeout(struct watchdog_device *wdd,
200 unsigned int timeout)
202 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
203 struct kempld_wdt_stage *pretimeout_stage;
204 struct kempld_wdt_stage *timeout_stage;
205 int ret;
207 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
208 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
210 if (pretimeout_stage->mask && wdt_data->pretimeout > 0)
211 timeout = wdt_data->pretimeout;
213 ret = kempld_wdt_set_stage_action(wdt_data, timeout_stage,
214 ACTION_RESET);
215 if (ret)
216 return ret;
217 ret = kempld_wdt_set_stage_timeout(wdt_data, timeout_stage,
218 timeout);
219 if (ret)
220 return ret;
222 wdd->timeout = timeout;
223 return 0;
226 static int kempld_wdt_set_pretimeout(struct watchdog_device *wdd,
227 unsigned int pretimeout)
229 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
230 struct kempld_wdt_stage *pretimeout_stage;
231 u8 action = ACTION_NONE;
232 int ret;
234 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
236 if (!pretimeout_stage->mask)
237 return -ENXIO;
239 if (pretimeout > wdd->timeout)
240 return -EINVAL;
242 if (pretimeout > 0)
243 action = ACTION_NMI;
245 ret = kempld_wdt_set_stage_action(wdt_data, pretimeout_stage,
246 action);
247 if (ret)
248 return ret;
249 ret = kempld_wdt_set_stage_timeout(wdt_data, pretimeout_stage,
250 wdd->timeout - pretimeout);
251 if (ret)
252 return ret;
254 wdt_data->pretimeout = pretimeout;
255 return 0;
258 static void kempld_wdt_update_timeouts(struct kempld_wdt_data *wdt_data)
260 struct kempld_device_data *pld = wdt_data->pld;
261 struct kempld_wdt_stage *pretimeout_stage;
262 struct kempld_wdt_stage *timeout_stage;
263 unsigned int pretimeout, timeout;
265 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
266 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
268 kempld_get_mutex(pld);
269 pretimeout = kempld_wdt_get_timeout(wdt_data, pretimeout_stage);
270 timeout = kempld_wdt_get_timeout(wdt_data, timeout_stage);
271 kempld_release_mutex(pld);
273 if (pretimeout)
274 wdt_data->pretimeout = timeout;
275 else
276 wdt_data->pretimeout = 0;
278 wdt_data->wdd.timeout = pretimeout + timeout;
281 static int kempld_wdt_start(struct watchdog_device *wdd)
283 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
284 struct kempld_device_data *pld = wdt_data->pld;
285 u8 status;
286 int ret;
288 ret = kempld_wdt_set_timeout(wdd, wdd->timeout);
289 if (ret)
290 return ret;
292 kempld_get_mutex(pld);
293 status = kempld_read8(pld, KEMPLD_WDT_CFG);
294 status |= KEMPLD_WDT_CFG_ENABLE;
295 kempld_write8(pld, KEMPLD_WDT_CFG, status);
296 status = kempld_read8(pld, KEMPLD_WDT_CFG);
297 kempld_release_mutex(pld);
299 /* Check if the watchdog was enabled */
300 if (!(status & KEMPLD_WDT_CFG_ENABLE))
301 return -EACCES;
303 return 0;
306 static int kempld_wdt_stop(struct watchdog_device *wdd)
308 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
309 struct kempld_device_data *pld = wdt_data->pld;
310 u8 status;
312 kempld_get_mutex(pld);
313 status = kempld_read8(pld, KEMPLD_WDT_CFG);
314 status &= ~KEMPLD_WDT_CFG_ENABLE;
315 kempld_write8(pld, KEMPLD_WDT_CFG, status);
316 status = kempld_read8(pld, KEMPLD_WDT_CFG);
317 kempld_release_mutex(pld);
319 /* Check if the watchdog was disabled */
320 if (status & KEMPLD_WDT_CFG_ENABLE)
321 return -EACCES;
323 return 0;
326 static int kempld_wdt_keepalive(struct watchdog_device *wdd)
328 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
329 struct kempld_device_data *pld = wdt_data->pld;
331 kempld_get_mutex(pld);
332 kempld_write8(pld, KEMPLD_WDT_KICK, 'K');
333 kempld_release_mutex(pld);
335 return 0;
338 static long kempld_wdt_ioctl(struct watchdog_device *wdd, unsigned int cmd,
339 unsigned long arg)
341 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
342 void __user *argp = (void __user *)arg;
343 int ret = -ENOIOCTLCMD;
344 int __user *p = argp;
345 int new_value;
347 switch (cmd) {
348 case WDIOC_SETPRETIMEOUT:
349 if (get_user(new_value, p))
350 return -EFAULT;
351 ret = kempld_wdt_set_pretimeout(wdd, new_value);
352 if (ret)
353 return ret;
354 ret = kempld_wdt_keepalive(wdd);
355 break;
356 case WDIOC_GETPRETIMEOUT:
357 ret = put_user(wdt_data->pretimeout, (int __user *)arg);
358 break;
361 return ret;
364 static int kempld_wdt_probe_stages(struct watchdog_device *wdd)
366 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
367 struct kempld_device_data *pld = wdt_data->pld;
368 struct kempld_wdt_stage *pretimeout_stage;
369 struct kempld_wdt_stage *timeout_stage;
370 u8 index, data, data_orig;
371 u32 mask;
372 int i, j;
374 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
375 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
377 pretimeout_stage->mask = 0;
378 timeout_stage->mask = 0;
380 for (i = 0; i < 3; i++) {
381 index = KEMPLD_WDT_STAGE_TIMEOUT(i);
382 mask = 0;
384 kempld_get_mutex(pld);
385 /* Probe each byte individually. */
386 for (j = 0; j < 4; j++) {
387 data_orig = kempld_read8(pld, index + j);
388 kempld_write8(pld, index + j, 0x00);
389 data = kempld_read8(pld, index + j);
390 /* A failed write means this byte is reserved */
391 if (data != 0x00)
392 break;
393 kempld_write8(pld, index + j, data_orig);
394 mask |= 0xff << (j * 8);
396 kempld_release_mutex(pld);
398 /* Assign available stages to timeout and pretimeout */
399 if (!timeout_stage->mask) {
400 timeout_stage->mask = mask;
401 timeout_stage->id = i;
402 } else {
403 if (pld->feature_mask & KEMPLD_FEATURE_BIT_NMI) {
404 pretimeout_stage->mask = timeout_stage->mask;
405 timeout_stage->mask = mask;
406 pretimeout_stage->id = timeout_stage->id;
407 timeout_stage->id = i;
409 break;
413 if (!timeout_stage->mask)
414 return -ENODEV;
416 return 0;
419 static const struct watchdog_info kempld_wdt_info = {
420 .identity = "KEMPLD Watchdog",
421 .options = WDIOF_SETTIMEOUT |
422 WDIOF_KEEPALIVEPING |
423 WDIOF_MAGICCLOSE |
424 WDIOF_PRETIMEOUT
427 static const struct watchdog_ops kempld_wdt_ops = {
428 .owner = THIS_MODULE,
429 .start = kempld_wdt_start,
430 .stop = kempld_wdt_stop,
431 .ping = kempld_wdt_keepalive,
432 .set_timeout = kempld_wdt_set_timeout,
433 .ioctl = kempld_wdt_ioctl,
436 static int kempld_wdt_probe(struct platform_device *pdev)
438 struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
439 struct kempld_wdt_data *wdt_data;
440 struct device *dev = &pdev->dev;
441 struct watchdog_device *wdd;
442 u8 status;
443 int ret = 0;
445 wdt_data = devm_kzalloc(dev, sizeof(*wdt_data), GFP_KERNEL);
446 if (!wdt_data)
447 return -ENOMEM;
449 wdt_data->pld = pld;
450 wdd = &wdt_data->wdd;
451 wdd->parent = dev;
453 kempld_get_mutex(pld);
454 status = kempld_read8(pld, KEMPLD_WDT_CFG);
455 kempld_release_mutex(pld);
457 /* Enable nowayout if watchdog is already locked */
458 if (status & (KEMPLD_WDT_CFG_ENABLE_LOCK |
459 KEMPLD_WDT_CFG_GLOBAL_LOCK)) {
460 if (!nowayout)
461 dev_warn(dev,
462 "Forcing nowayout - watchdog lock enabled!\n");
463 nowayout = true;
466 wdd->info = &kempld_wdt_info;
467 wdd->ops = &kempld_wdt_ops;
469 watchdog_set_drvdata(wdd, wdt_data);
470 watchdog_set_nowayout(wdd, nowayout);
472 ret = kempld_wdt_probe_stages(wdd);
473 if (ret)
474 return ret;
476 kempld_wdt_set_timeout(wdd, timeout);
477 kempld_wdt_set_pretimeout(wdd, pretimeout);
479 /* Check if watchdog is already enabled */
480 if (status & KEMPLD_WDT_CFG_ENABLE) {
481 /* Get current watchdog settings */
482 kempld_wdt_update_timeouts(wdt_data);
483 dev_info(dev, "Watchdog was already enabled\n");
486 platform_set_drvdata(pdev, wdt_data);
487 watchdog_stop_on_reboot(wdd);
488 watchdog_stop_on_unregister(wdd);
489 ret = devm_watchdog_register_device(dev, wdd);
490 if (ret)
491 return ret;
493 dev_info(dev, "Watchdog registered with %ds timeout\n", wdd->timeout);
495 return 0;
498 #ifdef CONFIG_PM
499 /* Disable watchdog if it is active during suspend */
500 static int kempld_wdt_suspend(struct platform_device *pdev,
501 pm_message_t message)
503 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
504 struct kempld_device_data *pld = wdt_data->pld;
505 struct watchdog_device *wdd = &wdt_data->wdd;
507 kempld_get_mutex(pld);
508 wdt_data->pm_status_store = kempld_read8(pld, KEMPLD_WDT_CFG);
509 kempld_release_mutex(pld);
511 kempld_wdt_update_timeouts(wdt_data);
513 if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
514 return kempld_wdt_stop(wdd);
516 return 0;
519 /* Enable watchdog and configure it if necessary */
520 static int kempld_wdt_resume(struct platform_device *pdev)
522 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
523 struct watchdog_device *wdd = &wdt_data->wdd;
526 * If watchdog was stopped before suspend be sure it gets disabled
527 * again, for the case BIOS has enabled it during resume
529 if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
530 return kempld_wdt_start(wdd);
531 else
532 return kempld_wdt_stop(wdd);
534 #else
535 #define kempld_wdt_suspend NULL
536 #define kempld_wdt_resume NULL
537 #endif
539 static struct platform_driver kempld_wdt_driver = {
540 .driver = {
541 .name = "kempld-wdt",
543 .probe = kempld_wdt_probe,
544 .suspend = kempld_wdt_suspend,
545 .resume = kempld_wdt_resume,
548 module_platform_driver(kempld_wdt_driver);
550 MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
551 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
552 MODULE_LICENSE("GPL");