Merge tag 'mvebu-dt-fixes-non-crit-3.15' of git://git.infradead.org/linux-mvebu into...
[firefly-linux-kernel-4.4.55.git] / drivers / watchdog / bcm47xx_wdt.c
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>
6  *  Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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>
28
29 #define DRV_NAME                "bcm47xx_wdt"
30
31 #define WDT_DEFAULT_TIME        30      /* seconds */
32 #define WDT_SOFTTIMER_MAX       255     /* seconds */
33 #define WDT_SOFTTIMER_THRESHOLD 60      /* seconds */
34
35 static int timeout = WDT_DEFAULT_TIME;
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37
38 module_param(timeout, int, 0);
39 MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
40                                 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
41
42 module_param(nowayout, bool, 0);
43 MODULE_PARM_DESC(nowayout,
44                 "Watchdog cannot be stopped once started (default="
45                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
46
47 static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
48 {
49         return container_of(wdd, struct bcm47xx_wdt, wdd);
50 }
51
52 static 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
61 static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
62 {
63         return 0;
64 }
65
66 static 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
75 static 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
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,
97 };
98
99 static void bcm47xx_wdt_soft_timer_tick(unsigned long data)
100 {
101         struct bcm47xx_wdt *wdt = (struct bcm47xx_wdt *)data;
102         u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
103
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");
109         }
110 }
111
112 static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
113 {
114         struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
115
116         atomic_set(&wdt->soft_ticks, wdd->timeout);
117
118         return 0;
119 }
120
121 static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
122 {
123         struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
124
125         bcm47xx_wdt_soft_keepalive(wdd);
126         bcm47xx_wdt_soft_timer_tick((unsigned long)wdt);
127
128         return 0;
129 }
130
131 static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
132 {
133         struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
134
135         del_timer_sync(&wdt->soft_timer);
136         wdt->timer_set(wdt, 0);
137
138         return 0;
139 }
140
141 static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
142                                         unsigned int new_time)
143 {
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;
148         }
149
150         wdd->timeout = new_time;
151         return 0;
152 }
153
154 static const struct watchdog_info bcm47xx_wdt_info = {
155         .identity       = DRV_NAME,
156         .options        = WDIOF_SETTIMEOUT |
157                                 WDIOF_KEEPALIVEPING |
158                                 WDIOF_MAGICCLOSE,
159 };
160
161 static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
162                                   unsigned long code, void *unused)
163 {
164         struct bcm47xx_wdt *wdt;
165
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;
170 }
171
172 static struct watchdog_ops bcm47xx_wdt_soft_ops = {
173         .owner          = THIS_MODULE,
174         .start          = bcm47xx_wdt_soft_start,
175         .stop           = bcm47xx_wdt_soft_stop,
176         .ping           = bcm47xx_wdt_soft_keepalive,
177         .set_timeout    = bcm47xx_wdt_soft_set_timeout,
178 };
179
180 static int bcm47xx_wdt_probe(struct platform_device *pdev)
181 {
182         int ret;
183         bool soft;
184         struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
185
186         if (!wdt)
187                 return -ENXIO;
188
189         soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
190
191         if (soft) {
192                 wdt->wdd.ops = &bcm47xx_wdt_soft_ops;
193                 setup_timer(&wdt->soft_timer, bcm47xx_wdt_soft_timer_tick,
194                             (long unsigned int)wdt);
195         } else {
196                 wdt->wdd.ops = &bcm47xx_wdt_hard_ops;
197         }
198
199         wdt->wdd.info = &bcm47xx_wdt_info;
200         wdt->wdd.timeout = WDT_DEFAULT_TIME;
201         ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
202         if (ret)
203                 goto err_timer;
204         watchdog_set_nowayout(&wdt->wdd, nowayout);
205
206         wdt->notifier.notifier_call = &bcm47xx_wdt_notify_sys;
207
208         ret = register_reboot_notifier(&wdt->notifier);
209         if (ret)
210                 goto err_timer;
211
212         ret = watchdog_register_device(&wdt->wdd);
213         if (ret)
214                 goto err_notifier;
215
216         dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
217                 timeout, nowayout ? ", nowayout" : "",
218                 soft ? ", Software Timer" : "");
219         return 0;
220
221 err_notifier:
222         unregister_reboot_notifier(&wdt->notifier);
223 err_timer:
224         if (soft)
225                 del_timer_sync(&wdt->soft_timer);
226
227         return ret;
228 }
229
230 static int bcm47xx_wdt_remove(struct platform_device *pdev)
231 {
232         struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
233
234         if (!wdt)
235                 return -ENXIO;
236
237         watchdog_unregister_device(&wdt->wdd);
238         unregister_reboot_notifier(&wdt->notifier);
239
240         return 0;
241 }
242
243 static struct platform_driver bcm47xx_wdt_driver = {
244         .driver         = {
245                 .owner  = THIS_MODULE,
246                 .name   = "bcm47xx-wdt",
247         },
248         .probe          = bcm47xx_wdt_probe,
249         .remove         = bcm47xx_wdt_remove,
250 };
251
252 module_platform_driver(bcm47xx_wdt_driver);
253
254 MODULE_AUTHOR("Aleksandar Radovanovic");
255 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
256 MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
257 MODULE_LICENSE("GPL");