2 * Kontron PLD watchdog driver
4 * Copyright (c) 2010-2013 Kontron Europe GmbH
5 * Author: Michael Brunner <michael.brunner@kontron.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License 2 as published
9 * by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Note: From the PLD watchdog point of view timeout and pretimeout are
17 * defined differently than in the kernel.
18 * First the pretimeout stage runs out before the timeout stage gets
21 * Kernel/API: P-----| pretimeout
22 * |-----------------------T timeout
23 * Watchdog: |-----------------P pretimeout_stage
24 * |-----T timeout_stage
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/miscdevice.h>
30 #include <linux/uaccess.h>
31 #include <linux/watchdog.h>
32 #include <linux/platform_device.h>
33 #include <linux/mfd/kempld.h>
35 #define KEMPLD_WDT_STAGE_TIMEOUT(x) (0x1b + (x) * 4)
36 #define KEMPLD_WDT_STAGE_CFG(x) (0x18 + (x))
37 #define STAGE_CFG_GET_PRESCALER(x) (((x) & 0x30) >> 4)
38 #define STAGE_CFG_SET_PRESCALER(x) (((x) & 0x3) << 4)
39 #define STAGE_CFG_PRESCALER_MASK 0x30
40 #define STAGE_CFG_ACTION_MASK 0x7
41 #define STAGE_CFG_ASSERT (1 << 3)
43 #define KEMPLD_WDT_MAX_STAGES 2
44 #define KEMPLD_WDT_KICK 0x16
45 #define KEMPLD_WDT_CFG 0x17
46 #define KEMPLD_WDT_CFG_ENABLE 0x10
47 #define KEMPLD_WDT_CFG_ENABLE_LOCK 0x8
48 #define KEMPLD_WDT_CFG_GLOBAL_LOCK 0x80
70 const u32 kempld_prescaler[] = {
71 [PRESCALER_21] = (1 << 21) - 1,
72 [PRESCALER_17] = (1 << 17) - 1,
73 [PRESCALER_12] = (1 << 12) - 1,
77 struct kempld_wdt_stage {
82 struct kempld_wdt_data {
83 struct kempld_device_data *pld;
84 struct watchdog_device wdd;
85 unsigned int pretimeout;
86 struct kempld_wdt_stage stage[KEMPLD_WDT_MAX_STAGES];
92 #define DEFAULT_TIMEOUT 30 /* seconds */
93 #define DEFAULT_PRETIMEOUT 0
95 static unsigned int timeout = DEFAULT_TIMEOUT;
96 module_param(timeout, uint, 0);
97 MODULE_PARM_DESC(timeout,
98 "Watchdog timeout in seconds. (>=0, default="
99 __MODULE_STRING(DEFAULT_TIMEOUT) ")");
101 static unsigned int pretimeout = DEFAULT_PRETIMEOUT;
102 module_param(pretimeout, uint, 0);
103 MODULE_PARM_DESC(pretimeout,
104 "Watchdog pretimeout in seconds. (>=0, default="
105 __MODULE_STRING(DEFAULT_PRETIMEOUT) ")");
107 static bool nowayout = WATCHDOG_NOWAYOUT;
108 module_param(nowayout, bool, 0);
109 MODULE_PARM_DESC(nowayout,
110 "Watchdog cannot be stopped once started (default="
111 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
113 static int kempld_wdt_set_stage_action(struct kempld_wdt_data *wdt_data,
114 struct kempld_wdt_stage *stage,
117 struct kempld_device_data *pld = wdt_data->pld;
120 if (!stage || !stage->mask)
123 kempld_get_mutex(pld);
124 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
125 stage_cfg &= ~STAGE_CFG_ACTION_MASK;
126 stage_cfg |= (action & STAGE_CFG_ACTION_MASK);
128 if (action == ACTION_RESET)
129 stage_cfg |= STAGE_CFG_ASSERT;
131 stage_cfg &= ~STAGE_CFG_ASSERT;
133 kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
134 kempld_release_mutex(pld);
139 static int kempld_wdt_set_stage_timeout(struct kempld_wdt_data *wdt_data,
140 struct kempld_wdt_stage *stage,
141 unsigned int timeout)
143 struct kempld_device_data *pld = wdt_data->pld;
144 u32 prescaler = kempld_prescaler[PRESCALER_21];
153 stage_timeout64 = (u64)timeout * pld->pld_clock;
154 remainder = do_div(stage_timeout64, prescaler);
158 if (stage_timeout64 > stage->mask)
161 stage_timeout = stage_timeout64 & stage->mask;
163 kempld_get_mutex(pld);
164 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
165 stage_cfg &= ~STAGE_CFG_PRESCALER_MASK;
166 stage_cfg |= STAGE_CFG_SET_PRESCALER(prescaler);
167 kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
168 kempld_write32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id),
170 kempld_release_mutex(pld);
176 * kempld_get_mutex must be called prior to calling this function.
178 static unsigned int kempld_wdt_get_timeout(struct kempld_wdt_data *wdt_data,
179 struct kempld_wdt_stage *stage)
181 struct kempld_device_data *pld = wdt_data->pld;
182 unsigned int timeout;
191 stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
192 stage_timeout = kempld_read32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id));
193 prescaler = kempld_prescaler[STAGE_CFG_GET_PRESCALER(stage_cfg)];
195 stage_timeout = (stage_timeout & stage->mask) * prescaler;
196 remainder = do_div(stage_timeout, pld->pld_clock);
200 timeout = stage_timeout;
201 WARN_ON_ONCE(timeout != stage_timeout);
206 static int kempld_wdt_set_timeout(struct watchdog_device *wdd,
207 unsigned int timeout)
209 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
210 struct kempld_wdt_stage *pretimeout_stage;
211 struct kempld_wdt_stage *timeout_stage;
214 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
215 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
217 if (pretimeout_stage->mask && wdt_data->pretimeout > 0)
218 timeout = wdt_data->pretimeout;
220 ret = kempld_wdt_set_stage_action(wdt_data, timeout_stage,
224 ret = kempld_wdt_set_stage_timeout(wdt_data, timeout_stage,
229 wdd->timeout = timeout;
233 static int kempld_wdt_set_pretimeout(struct watchdog_device *wdd,
234 unsigned int pretimeout)
236 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
237 struct kempld_wdt_stage *pretimeout_stage;
238 u8 action = ACTION_NONE;
241 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
243 if (!pretimeout_stage->mask)
246 if (pretimeout > wdd->timeout)
252 ret = kempld_wdt_set_stage_action(wdt_data, pretimeout_stage,
256 ret = kempld_wdt_set_stage_timeout(wdt_data, pretimeout_stage,
257 wdd->timeout - pretimeout);
261 wdt_data->pretimeout = pretimeout;
265 static void kempld_wdt_update_timeouts(struct kempld_wdt_data *wdt_data)
267 struct kempld_device_data *pld = wdt_data->pld;
268 struct kempld_wdt_stage *pretimeout_stage;
269 struct kempld_wdt_stage *timeout_stage;
270 unsigned int pretimeout, timeout;
272 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
273 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
275 kempld_get_mutex(pld);
276 pretimeout = kempld_wdt_get_timeout(wdt_data, pretimeout_stage);
277 timeout = kempld_wdt_get_timeout(wdt_data, timeout_stage);
278 kempld_release_mutex(pld);
281 wdt_data->pretimeout = timeout;
283 wdt_data->pretimeout = 0;
285 wdt_data->wdd.timeout = pretimeout + timeout;
288 static int kempld_wdt_start(struct watchdog_device *wdd)
290 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
291 struct kempld_device_data *pld = wdt_data->pld;
295 ret = kempld_wdt_set_timeout(wdd, wdd->timeout);
299 kempld_get_mutex(pld);
300 status = kempld_read8(pld, KEMPLD_WDT_CFG);
301 status |= KEMPLD_WDT_CFG_ENABLE;
302 kempld_write8(pld, KEMPLD_WDT_CFG, status);
303 status = kempld_read8(pld, KEMPLD_WDT_CFG);
304 kempld_release_mutex(pld);
306 /* Check if the watchdog was enabled */
307 if (!(status & KEMPLD_WDT_CFG_ENABLE))
313 static int kempld_wdt_stop(struct watchdog_device *wdd)
315 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
316 struct kempld_device_data *pld = wdt_data->pld;
319 kempld_get_mutex(pld);
320 status = kempld_read8(pld, KEMPLD_WDT_CFG);
321 status &= ~KEMPLD_WDT_CFG_ENABLE;
322 kempld_write8(pld, KEMPLD_WDT_CFG, status);
323 status = kempld_read8(pld, KEMPLD_WDT_CFG);
324 kempld_release_mutex(pld);
326 /* Check if the watchdog was disabled */
327 if (status & KEMPLD_WDT_CFG_ENABLE)
333 static int kempld_wdt_keepalive(struct watchdog_device *wdd)
335 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
336 struct kempld_device_data *pld = wdt_data->pld;
338 kempld_get_mutex(pld);
339 kempld_write8(pld, KEMPLD_WDT_KICK, 'K');
340 kempld_release_mutex(pld);
345 static long kempld_wdt_ioctl(struct watchdog_device *wdd, unsigned int cmd,
348 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
349 void __user *argp = (void __user *)arg;
350 int ret = -ENOIOCTLCMD;
351 int __user *p = argp;
355 case WDIOC_SETPRETIMEOUT:
356 if (get_user(new_value, p))
358 ret = kempld_wdt_set_pretimeout(wdd, new_value);
361 ret = kempld_wdt_keepalive(wdd);
363 case WDIOC_GETPRETIMEOUT:
364 ret = put_user(wdt_data->pretimeout, (int *)arg);
371 static int kempld_wdt_probe_stages(struct watchdog_device *wdd)
373 struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
374 struct kempld_device_data *pld = wdt_data->pld;
375 struct kempld_wdt_stage *pretimeout_stage;
376 struct kempld_wdt_stage *timeout_stage;
377 u8 index, data, data_orig;
381 pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
382 timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
384 pretimeout_stage->mask = 0;
385 timeout_stage->mask = 0;
387 for (i = 0; i < 3; i++) {
388 index = KEMPLD_WDT_STAGE_TIMEOUT(i);
391 kempld_get_mutex(pld);
392 /* Probe each byte individually. */
393 for (j = 0; j < 4; j++) {
394 data_orig = kempld_read8(pld, index + j);
395 kempld_write8(pld, index + j, 0x00);
396 data = kempld_read8(pld, index + j);
397 /* A failed write means this byte is reserved */
400 kempld_write8(pld, index + j, data_orig);
401 mask |= 0xff << (j * 8);
403 kempld_release_mutex(pld);
405 /* Assign available stages to timeout and pretimeout */
406 if (!timeout_stage->mask) {
407 timeout_stage->mask = mask;
408 timeout_stage->id = i;
410 if (pld->feature_mask & KEMPLD_FEATURE_BIT_NMI) {
411 pretimeout_stage->mask = timeout_stage->mask;
412 timeout_stage->mask = mask;
413 pretimeout_stage->id = timeout_stage->id;
414 timeout_stage->id = i;
420 if (!timeout_stage->mask)
426 static struct watchdog_info kempld_wdt_info = {
427 .identity = "KEMPLD Watchdog",
428 .options = WDIOF_SETTIMEOUT |
429 WDIOF_KEEPALIVEPING |
434 static struct watchdog_ops kempld_wdt_ops = {
435 .owner = THIS_MODULE,
436 .start = kempld_wdt_start,
437 .stop = kempld_wdt_stop,
438 .ping = kempld_wdt_keepalive,
439 .set_timeout = kempld_wdt_set_timeout,
440 .ioctl = kempld_wdt_ioctl,
443 static int kempld_wdt_probe(struct platform_device *pdev)
445 struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
446 struct kempld_wdt_data *wdt_data;
447 struct device *dev = &pdev->dev;
448 struct watchdog_device *wdd;
452 wdt_data = devm_kzalloc(dev, sizeof(*wdt_data), GFP_KERNEL);
457 wdd = &wdt_data->wdd;
460 kempld_get_mutex(pld);
461 status = kempld_read8(pld, KEMPLD_WDT_CFG);
462 kempld_release_mutex(pld);
464 /* Enable nowayout if watchdog is already locked */
465 if (status & (KEMPLD_WDT_CFG_ENABLE_LOCK |
466 KEMPLD_WDT_CFG_GLOBAL_LOCK)) {
469 "Forcing nowayout - watchdog lock enabled!\n");
473 wdd->info = &kempld_wdt_info;
474 wdd->ops = &kempld_wdt_ops;
476 watchdog_set_drvdata(wdd, wdt_data);
477 watchdog_set_nowayout(wdd, nowayout);
479 ret = kempld_wdt_probe_stages(wdd);
483 kempld_wdt_set_timeout(wdd, timeout);
484 kempld_wdt_set_pretimeout(wdd, pretimeout);
486 /* Check if watchdog is already enabled */
487 if (status & KEMPLD_WDT_CFG_ENABLE) {
488 /* Get current watchdog settings */
489 kempld_wdt_update_timeouts(wdt_data);
490 dev_info(dev, "Watchdog was already enabled\n");
493 platform_set_drvdata(pdev, wdt_data);
494 ret = watchdog_register_device(wdd);
498 dev_info(dev, "Watchdog registered with %ds timeout\n", wdd->timeout);
503 static void kempld_wdt_shutdown(struct platform_device *pdev)
505 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
507 kempld_wdt_stop(&wdt_data->wdd);
510 static int kempld_wdt_remove(struct platform_device *pdev)
512 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
513 struct watchdog_device *wdd = &wdt_data->wdd;
517 ret = kempld_wdt_stop(wdd);
518 watchdog_unregister_device(wdd);
524 /* Disable watchdog if it is active during suspend */
525 static int kempld_wdt_suspend(struct platform_device *pdev,
526 pm_message_t message)
528 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
529 struct kempld_device_data *pld = wdt_data->pld;
530 struct watchdog_device *wdd = &wdt_data->wdd;
532 kempld_get_mutex(pld);
533 wdt_data->pm_status_store = kempld_read8(pld, KEMPLD_WDT_CFG);
534 kempld_release_mutex(pld);
536 kempld_wdt_update_timeouts(wdt_data);
538 if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
539 return kempld_wdt_stop(wdd);
544 /* Enable watchdog and configure it if necessary */
545 static int kempld_wdt_resume(struct platform_device *pdev)
547 struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
548 struct watchdog_device *wdd = &wdt_data->wdd;
551 * If watchdog was stopped before suspend be sure it gets disabled
552 * again, for the case BIOS has enabled it during resume
554 if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
555 return kempld_wdt_start(wdd);
557 return kempld_wdt_stop(wdd);
560 #define kempld_wdt_suspend NULL
561 #define kempld_wdt_resume NULL
564 static struct platform_driver kempld_wdt_driver = {
566 .name = "kempld-wdt",
567 .owner = THIS_MODULE,
569 .probe = kempld_wdt_probe,
570 .remove = kempld_wdt_remove,
571 .shutdown = kempld_wdt_shutdown,
572 .suspend = kempld_wdt_suspend,
573 .resume = kempld_wdt_resume,
576 module_platform_driver(kempld_wdt_driver);
578 MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
579 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
580 MODULE_LICENSE("GPL");
581 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);