Linux 4.2.1
[linux/fpc-iii.git] / drivers / watchdog / bcm47xx_wdt.c
blobb28a072abf78fbe030f647ef491e8fc24bc157d4
1 /*
2 * Watchdog driver for Broadcom BCM47XX
4 * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
5 * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
6 * Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/bcm47xx_wdt.h>
17 #include <linux/bitops.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/platform_device.h>
23 #include <linux/reboot.h>
24 #include <linux/types.h>
25 #include <linux/watchdog.h>
26 #include <linux/timer.h>
27 #include <linux/jiffies.h>
29 #define DRV_NAME "bcm47xx_wdt"
31 #define WDT_DEFAULT_TIME 30 /* seconds */
32 #define WDT_SOFTTIMER_MAX 255 /* seconds */
33 #define WDT_SOFTTIMER_THRESHOLD 60 /* seconds */
35 static int timeout = WDT_DEFAULT_TIME;
36 static bool nowayout = WATCHDOG_NOWAYOUT;
38 module_param(timeout, int, 0);
39 MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
40 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
42 module_param(nowayout, bool, 0);
43 MODULE_PARM_DESC(nowayout,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
47 static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
49 return container_of(wdd, struct bcm47xx_wdt, wdd);
52 static int bcm47xx_wdt_hard_keepalive(struct watchdog_device *wdd)
54 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
56 wdt->timer_set_ms(wdt, wdd->timeout * 1000);
58 return 0;
61 static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
63 return 0;
66 static int bcm47xx_wdt_hard_stop(struct watchdog_device *wdd)
68 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
70 wdt->timer_set(wdt, 0);
72 return 0;
75 static int bcm47xx_wdt_hard_set_timeout(struct watchdog_device *wdd,
76 unsigned int new_time)
78 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
79 u32 max_timer = wdt->max_timer_ms;
81 if (new_time < 1 || new_time > max_timer / 1000) {
82 pr_warn("timeout value must be 1<=x<=%d, using %d\n",
83 max_timer / 1000, new_time);
84 return -EINVAL;
87 wdd->timeout = new_time;
88 return 0;
91 static struct watchdog_ops bcm47xx_wdt_hard_ops = {
92 .owner = THIS_MODULE,
93 .start = bcm47xx_wdt_hard_start,
94 .stop = bcm47xx_wdt_hard_stop,
95 .ping = bcm47xx_wdt_hard_keepalive,
96 .set_timeout = bcm47xx_wdt_hard_set_timeout,
99 static void bcm47xx_wdt_soft_timer_tick(unsigned long data)
101 struct bcm47xx_wdt *wdt = (struct bcm47xx_wdt *)data;
102 u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
104 if (!atomic_dec_and_test(&wdt->soft_ticks)) {
105 wdt->timer_set_ms(wdt, next_tick);
106 mod_timer(&wdt->soft_timer, jiffies + HZ);
107 } else {
108 pr_crit("Watchdog will fire soon!!!\n");
112 static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
114 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
116 atomic_set(&wdt->soft_ticks, wdd->timeout);
118 return 0;
121 static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
123 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
125 bcm47xx_wdt_soft_keepalive(wdd);
126 bcm47xx_wdt_soft_timer_tick((unsigned long)wdt);
128 return 0;
131 static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
133 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
135 del_timer_sync(&wdt->soft_timer);
136 wdt->timer_set(wdt, 0);
138 return 0;
141 static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
142 unsigned int new_time)
144 if (new_time < 1 || new_time > WDT_SOFTTIMER_MAX) {
145 pr_warn("timeout value must be 1<=x<=%d, using %d\n",
146 WDT_SOFTTIMER_MAX, new_time);
147 return -EINVAL;
150 wdd->timeout = new_time;
151 return 0;
154 static const struct watchdog_info bcm47xx_wdt_info = {
155 .identity = DRV_NAME,
156 .options = WDIOF_SETTIMEOUT |
157 WDIOF_KEEPALIVEPING |
158 WDIOF_MAGICCLOSE,
161 static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
162 unsigned long code, void *unused)
164 struct bcm47xx_wdt *wdt;
166 wdt = container_of(this, struct bcm47xx_wdt, notifier);
167 if (code == SYS_DOWN || code == SYS_HALT)
168 wdt->wdd.ops->stop(&wdt->wdd);
169 return NOTIFY_DONE;
172 static int bcm47xx_wdt_restart(struct notifier_block *this, unsigned long mode,
173 void *cmd)
175 struct bcm47xx_wdt *wdt;
177 wdt = container_of(this, struct bcm47xx_wdt, restart_handler);
178 wdt->timer_set(wdt, 1);
180 return NOTIFY_DONE;
183 static struct watchdog_ops bcm47xx_wdt_soft_ops = {
184 .owner = THIS_MODULE,
185 .start = bcm47xx_wdt_soft_start,
186 .stop = bcm47xx_wdt_soft_stop,
187 .ping = bcm47xx_wdt_soft_keepalive,
188 .set_timeout = bcm47xx_wdt_soft_set_timeout,
191 static int bcm47xx_wdt_probe(struct platform_device *pdev)
193 int ret;
194 bool soft;
195 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
197 if (!wdt)
198 return -ENXIO;
200 soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
202 if (soft) {
203 wdt->wdd.ops = &bcm47xx_wdt_soft_ops;
204 setup_timer(&wdt->soft_timer, bcm47xx_wdt_soft_timer_tick,
205 (long unsigned int)wdt);
206 } else {
207 wdt->wdd.ops = &bcm47xx_wdt_hard_ops;
210 wdt->wdd.info = &bcm47xx_wdt_info;
211 wdt->wdd.timeout = WDT_DEFAULT_TIME;
212 ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
213 if (ret)
214 goto err_timer;
215 watchdog_set_nowayout(&wdt->wdd, nowayout);
217 wdt->notifier.notifier_call = &bcm47xx_wdt_notify_sys;
219 ret = register_reboot_notifier(&wdt->notifier);
220 if (ret)
221 goto err_timer;
223 wdt->restart_handler.notifier_call = &bcm47xx_wdt_restart;
224 wdt->restart_handler.priority = 64;
225 ret = register_restart_handler(&wdt->restart_handler);
226 if (ret)
227 goto err_notifier;
229 ret = watchdog_register_device(&wdt->wdd);
230 if (ret)
231 goto err_handler;
233 dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
234 timeout, nowayout ? ", nowayout" : "",
235 soft ? ", Software Timer" : "");
236 return 0;
238 err_handler:
239 unregister_restart_handler(&wdt->restart_handler);
240 err_notifier:
241 unregister_reboot_notifier(&wdt->notifier);
242 err_timer:
243 if (soft)
244 del_timer_sync(&wdt->soft_timer);
246 return ret;
249 static int bcm47xx_wdt_remove(struct platform_device *pdev)
251 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
253 if (!wdt)
254 return -ENXIO;
256 watchdog_unregister_device(&wdt->wdd);
257 unregister_reboot_notifier(&wdt->notifier);
259 return 0;
262 static struct platform_driver bcm47xx_wdt_driver = {
263 .driver = {
264 .name = "bcm47xx-wdt",
266 .probe = bcm47xx_wdt_probe,
267 .remove = bcm47xx_wdt_remove,
270 module_platform_driver(bcm47xx_wdt_driver);
272 MODULE_AUTHOR("Aleksandar Radovanovic");
273 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
274 MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
275 MODULE_LICENSE("GPL");