watchdog: core: add reboot notifier support
[deliverable/linux.git] / drivers / watchdog / bcm47xx_wdt.c
CommitLineData
90074dce 1/*
2 * Watchdog driver for Broadcom BCM47XX
3 *
4 * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
5 * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
f82dedf8 6 * Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
90074dce 7 *
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.
12 */
13
27c766aa
JP
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
f82dedf8 16#include <linux/bcm47xx_wdt.h>
90074dce 17#include <linux/bitops.h>
18#include <linux/errno.h>
90074dce 19#include <linux/kernel.h>
90074dce 20#include <linux/module.h>
21#include <linux/moduleparam.h>
f82dedf8 22#include <linux/platform_device.h>
90074dce 23#include <linux/reboot.h>
24#include <linux/types.h>
90074dce 25#include <linux/watchdog.h>
26#include <linux/timer.h>
27#include <linux/jiffies.h>
90074dce 28
29#define DRV_NAME "bcm47xx_wdt"
30
31#define WDT_DEFAULT_TIME 30 /* seconds */
a3906892 32#define WDT_SOFTTIMER_MAX 255 /* seconds */
e3e83d00 33#define WDT_SOFTTIMER_THRESHOLD 60 /* seconds */
90074dce 34
93aed1f0 35static int timeout = WDT_DEFAULT_TIME;
86a1e189 36static bool nowayout = WATCHDOG_NOWAYOUT;
90074dce 37
93aed1f0
HM
38module_param(timeout, int, 0);
39MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
90074dce 40 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
41
86a1e189 42module_param(nowayout, bool, 0);
90074dce 43MODULE_PARM_DESC(nowayout,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
90074dce 46
f82dedf8 47static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
90074dce 48{
f82dedf8 49 return container_of(wdd, struct bcm47xx_wdt, wdd);
90074dce 50}
51
e3e83d00
HM
52static int bcm47xx_wdt_hard_keepalive(struct watchdog_device *wdd)
53{
54 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
55
56 wdt->timer_set_ms(wdt, wdd->timeout * 1000);
57
58 return 0;
59}
60
61static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
62{
63 return 0;
64}
65
66static int bcm47xx_wdt_hard_stop(struct watchdog_device *wdd)
67{
68 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
69
70 wdt->timer_set(wdt, 0);
71
72 return 0;
73}
74
75static int bcm47xx_wdt_hard_set_timeout(struct watchdog_device *wdd,
76 unsigned int new_time)
77{
78 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
79 u32 max_timer = wdt->max_timer_ms;
80
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;
85 }
86
87 wdd->timeout = new_time;
88 return 0;
89}
90
65a4a1dc
DR
91static int bcm47xx_wdt_restart(struct watchdog_device *wdd)
92{
93 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
94
95 wdt->timer_set(wdt, 1);
96
97 return 0;
98}
99
e3e83d00
HM
100static struct watchdog_ops bcm47xx_wdt_hard_ops = {
101 .owner = THIS_MODULE,
102 .start = bcm47xx_wdt_hard_start,
103 .stop = bcm47xx_wdt_hard_stop,
104 .ping = bcm47xx_wdt_hard_keepalive,
105 .set_timeout = bcm47xx_wdt_hard_set_timeout,
65a4a1dc 106 .restart = bcm47xx_wdt_restart,
e3e83d00
HM
107};
108
a3906892 109static void bcm47xx_wdt_soft_timer_tick(unsigned long data)
90074dce 110{
f82dedf8
HM
111 struct bcm47xx_wdt *wdt = (struct bcm47xx_wdt *)data;
112 u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
90074dce 113
f82dedf8
HM
114 if (!atomic_dec_and_test(&wdt->soft_ticks)) {
115 wdt->timer_set_ms(wdt, next_tick);
116 mod_timer(&wdt->soft_timer, jiffies + HZ);
90074dce 117 } else {
27c766aa 118 pr_crit("Watchdog will fire soon!!!\n");
90074dce 119 }
120}
121
a3906892 122static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
90074dce 123{
f82dedf8
HM
124 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
125
126 atomic_set(&wdt->soft_ticks, wdd->timeout);
5434a04d
HM
127
128 return 0;
90074dce 129}
130
a3906892 131static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
90074dce 132{
f82dedf8
HM
133 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
134
a3906892
HM
135 bcm47xx_wdt_soft_keepalive(wdd);
136 bcm47xx_wdt_soft_timer_tick((unsigned long)wdt);
5434a04d
HM
137
138 return 0;
90074dce 139}
140
a3906892 141static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
90074dce 142{
f82dedf8
HM
143 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
144
145 del_timer_sync(&wdt->soft_timer);
146 wdt->timer_set(wdt, 0);
90074dce 147
5434a04d 148 return 0;
90074dce 149}
150
a3906892
HM
151static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
152 unsigned int new_time)
90074dce 153{
a3906892 154 if (new_time < 1 || new_time > WDT_SOFTTIMER_MAX) {
f82dedf8 155 pr_warn("timeout value must be 1<=x<=%d, using %d\n",
a3906892 156 WDT_SOFTTIMER_MAX, new_time);
90074dce 157 return -EINVAL;
f82dedf8 158 }
90074dce 159
f82dedf8 160 wdd->timeout = new_time;
90074dce 161 return 0;
162}
163
42747d71 164static const struct watchdog_info bcm47xx_wdt_info = {
5f3b2756
WVS
165 .identity = DRV_NAME,
166 .options = WDIOF_SETTIMEOUT |
90074dce 167 WDIOF_KEEPALIVEPING |
168 WDIOF_MAGICCLOSE,
169};
170
90074dce 171static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
5434a04d 172 unsigned long code, void *unused)
90074dce 173{
f82dedf8
HM
174 struct bcm47xx_wdt *wdt;
175
176 wdt = container_of(this, struct bcm47xx_wdt, notifier);
90074dce 177 if (code == SYS_DOWN || code == SYS_HALT)
f82dedf8 178 wdt->wdd.ops->stop(&wdt->wdd);
90074dce 179 return NOTIFY_DONE;
180}
181
a3906892 182static struct watchdog_ops bcm47xx_wdt_soft_ops = {
90074dce 183 .owner = THIS_MODULE,
a3906892
HM
184 .start = bcm47xx_wdt_soft_start,
185 .stop = bcm47xx_wdt_soft_stop,
186 .ping = bcm47xx_wdt_soft_keepalive,
187 .set_timeout = bcm47xx_wdt_soft_set_timeout,
65a4a1dc 188 .restart = bcm47xx_wdt_restart,
90074dce 189};
190
f82dedf8 191static int bcm47xx_wdt_probe(struct platform_device *pdev)
90074dce 192{
193 int ret;
e3e83d00 194 bool soft;
f82dedf8 195 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
90074dce 196
f82dedf8
HM
197 if (!wdt)
198 return -ENXIO;
90074dce 199
e3e83d00
HM
200 soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
201
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;
208 }
90074dce 209
f82dedf8
HM
210 wdt->wdd.info = &bcm47xx_wdt_info;
211 wdt->wdd.timeout = WDT_DEFAULT_TIME;
6551881c 212 wdt->wdd.parent = &pdev->dev;
f82dedf8
HM
213 ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
214 if (ret)
215 goto err_timer;
216 watchdog_set_nowayout(&wdt->wdd, nowayout);
65a4a1dc 217 watchdog_set_restart_priority(&wdt->wdd, 64);
f82dedf8
HM
218
219 wdt->notifier.notifier_call = &bcm47xx_wdt_notify_sys;
90074dce 220
f82dedf8 221 ret = register_reboot_notifier(&wdt->notifier);
90074dce 222 if (ret)
f82dedf8 223 goto err_timer;
90074dce 224
1cc7495c
RM
225 ret = watchdog_register_device(&wdt->wdd);
226 if (ret)
65a4a1dc 227 goto err_notifier;
1cc7495c 228
e3e83d00
HM
229 dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
230 timeout, nowayout ? ", nowayout" : "",
231 soft ? ", Software Timer" : "");
90074dce 232 return 0;
f82dedf8
HM
233
234err_notifier:
235 unregister_reboot_notifier(&wdt->notifier);
236err_timer:
e3e83d00
HM
237 if (soft)
238 del_timer_sync(&wdt->soft_timer);
f82dedf8
HM
239
240 return ret;
90074dce 241}
242
f82dedf8 243static int bcm47xx_wdt_remove(struct platform_device *pdev)
90074dce 244{
f82dedf8
HM
245 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
246
247 if (!wdt)
248 return -ENXIO;
249
250 watchdog_unregister_device(&wdt->wdd);
251 unregister_reboot_notifier(&wdt->notifier);
90074dce 252
f82dedf8 253 return 0;
90074dce 254}
255
f82dedf8
HM
256static struct platform_driver bcm47xx_wdt_driver = {
257 .driver = {
f82dedf8
HM
258 .name = "bcm47xx-wdt",
259 },
260 .probe = bcm47xx_wdt_probe,
261 .remove = bcm47xx_wdt_remove,
262};
263
264module_platform_driver(bcm47xx_wdt_driver);
90074dce 265
266MODULE_AUTHOR("Aleksandar Radovanovic");
f82dedf8 267MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
90074dce 268MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
269MODULE_LICENSE("GPL");
This page took 0.461379 seconds and 5 git commands to generate.