blob: 05425c1dfd4c7c639f07d19d3a3d490d6f404c1c [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
matthieu castet90074dc2009-06-05 18:57:18 +02002/*
3 * Watchdog driver for Broadcom BCM47XX
4 *
5 * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
6 * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +01007 * Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
matthieu castet90074dc2009-06-05 18:57:18 +02008 *
matthieu castet90074dc2009-06-05 18:57:18 +02009 */
10
Joe Perches27c766a2012-02-15 15:06:19 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +010013#include <linux/bcm47xx_wdt.h>
matthieu castet90074dc2009-06-05 18:57:18 +020014#include <linux/bitops.h>
15#include <linux/errno.h>
matthieu castet90074dc2009-06-05 18:57:18 +020016#include <linux/kernel.h>
matthieu castet90074dc2009-06-05 18:57:18 +020017#include <linux/module.h>
18#include <linux/moduleparam.h>
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +010019#include <linux/platform_device.h>
matthieu castet90074dc2009-06-05 18:57:18 +020020#include <linux/types.h>
matthieu castet90074dc2009-06-05 18:57:18 +020021#include <linux/watchdog.h>
22#include <linux/timer.h>
23#include <linux/jiffies.h>
matthieu castet90074dc2009-06-05 18:57:18 +020024
25#define DRV_NAME "bcm47xx_wdt"
26
27#define WDT_DEFAULT_TIME 30 /* seconds */
Hauke Mehrtensa3906892013-01-12 18:14:09 +010028#define WDT_SOFTTIMER_MAX 255 /* seconds */
Hauke Mehrtense3e83d02013-01-12 18:14:11 +010029#define WDT_SOFTTIMER_THRESHOLD 60 /* seconds */
matthieu castet90074dc2009-06-05 18:57:18 +020030
Hauke Mehrtens93aed1f2013-01-12 18:14:10 +010031static int timeout = WDT_DEFAULT_TIME;
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010032static bool nowayout = WATCHDOG_NOWAYOUT;
matthieu castet90074dc2009-06-05 18:57:18 +020033
Hauke Mehrtens93aed1f2013-01-12 18:14:10 +010034module_param(timeout, int, 0);
35MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
matthieu castet90074dc2009-06-05 18:57:18 +020036 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
37
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010038module_param(nowayout, bool, 0);
matthieu castet90074dc2009-06-05 18:57:18 +020039MODULE_PARM_DESC(nowayout,
40 "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
matthieu castet90074dc2009-06-05 18:57:18 +020042
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +010043static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
matthieu castet90074dc2009-06-05 18:57:18 +020044{
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +010045 return container_of(wdd, struct bcm47xx_wdt, wdd);
matthieu castet90074dc2009-06-05 18:57:18 +020046}
47
Hauke Mehrtense3e83d02013-01-12 18:14:11 +010048static int bcm47xx_wdt_hard_keepalive(struct watchdog_device *wdd)
49{
50 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
51
52 wdt->timer_set_ms(wdt, wdd->timeout * 1000);
53
54 return 0;
55}
56
57static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
58{
59 return 0;
60}
61
62static int bcm47xx_wdt_hard_stop(struct watchdog_device *wdd)
63{
64 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
65
66 wdt->timer_set(wdt, 0);
67
68 return 0;
69}
70
71static int bcm47xx_wdt_hard_set_timeout(struct watchdog_device *wdd,
72 unsigned int new_time)
73{
74 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
75 u32 max_timer = wdt->max_timer_ms;
76
77 if (new_time < 1 || new_time > max_timer / 1000) {
78 pr_warn("timeout value must be 1<=x<=%d, using %d\n",
79 max_timer / 1000, new_time);
80 return -EINVAL;
81 }
82
83 wdd->timeout = new_time;
84 return 0;
85}
86
Guenter Roeck4d8b2292016-02-26 17:32:49 -080087static int bcm47xx_wdt_restart(struct watchdog_device *wdd,
88 unsigned long action, void *data)
Damien Riegel65a4a1d2015-11-16 12:28:00 -050089{
90 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
91
92 wdt->timer_set(wdt, 1);
93
94 return 0;
95}
96
Arvind Yadavd34f1f42017-06-12 11:50:26 +053097static const struct watchdog_ops bcm47xx_wdt_hard_ops = {
Hauke Mehrtense3e83d02013-01-12 18:14:11 +010098 .owner = THIS_MODULE,
99 .start = bcm47xx_wdt_hard_start,
100 .stop = bcm47xx_wdt_hard_stop,
101 .ping = bcm47xx_wdt_hard_keepalive,
102 .set_timeout = bcm47xx_wdt_hard_set_timeout,
Damien Riegel65a4a1d2015-11-16 12:28:00 -0500103 .restart = bcm47xx_wdt_restart,
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100104};
105
Kees Cooke99e88a2017-10-16 14:43:17 -0700106static void bcm47xx_wdt_soft_timer_tick(struct timer_list *t)
matthieu castet90074dc2009-06-05 18:57:18 +0200107{
Kees Cooke99e88a2017-10-16 14:43:17 -0700108 struct bcm47xx_wdt *wdt = from_timer(wdt, t, soft_timer);
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100109 u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
matthieu castet90074dc2009-06-05 18:57:18 +0200110
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100111 if (!atomic_dec_and_test(&wdt->soft_ticks)) {
112 wdt->timer_set_ms(wdt, next_tick);
113 mod_timer(&wdt->soft_timer, jiffies + HZ);
matthieu castet90074dc2009-06-05 18:57:18 +0200114 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800115 pr_crit("Watchdog will fire soon!!!\n");
matthieu castet90074dc2009-06-05 18:57:18 +0200116 }
117}
118
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100119static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
matthieu castet90074dc2009-06-05 18:57:18 +0200120{
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100121 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
122
123 atomic_set(&wdt->soft_ticks, wdd->timeout);
Hauke Mehrtens5434a042013-01-12 18:14:07 +0100124
125 return 0;
matthieu castet90074dc2009-06-05 18:57:18 +0200126}
127
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100128static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
matthieu castet90074dc2009-06-05 18:57:18 +0200129{
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100130 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
131
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100132 bcm47xx_wdt_soft_keepalive(wdd);
Kees Cooke99e88a2017-10-16 14:43:17 -0700133 bcm47xx_wdt_soft_timer_tick(&wdt->soft_timer);
Hauke Mehrtens5434a042013-01-12 18:14:07 +0100134
135 return 0;
matthieu castet90074dc2009-06-05 18:57:18 +0200136}
137
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100138static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
matthieu castet90074dc2009-06-05 18:57:18 +0200139{
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100140 struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
141
142 del_timer_sync(&wdt->soft_timer);
143 wdt->timer_set(wdt, 0);
Hauke Mehrtens5434a042013-01-12 18:14:07 +0100144
145 return 0;
matthieu castet90074dc2009-06-05 18:57:18 +0200146}
147
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100148static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
149 unsigned int new_time)
matthieu castet90074dc2009-06-05 18:57:18 +0200150{
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100151 if (new_time < 1 || new_time > WDT_SOFTTIMER_MAX) {
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100152 pr_warn("timeout value must be 1<=x<=%d, using %d\n",
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100153 WDT_SOFTTIMER_MAX, new_time);
matthieu castet90074dc2009-06-05 18:57:18 +0200154 return -EINVAL;
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100155 }
matthieu castet90074dc2009-06-05 18:57:18 +0200156
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100157 wdd->timeout = new_time;
matthieu castet90074dc2009-06-05 18:57:18 +0200158 return 0;
159}
160
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000161static const struct watchdog_info bcm47xx_wdt_info = {
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000162 .identity = DRV_NAME,
163 .options = WDIOF_SETTIMEOUT |
matthieu castet90074dc2009-06-05 18:57:18 +0200164 WDIOF_KEEPALIVEPING |
165 WDIOF_MAGICCLOSE,
166};
167
Arvind Yadavd34f1f42017-06-12 11:50:26 +0530168static const struct watchdog_ops bcm47xx_wdt_soft_ops = {
matthieu castet90074dc2009-06-05 18:57:18 +0200169 .owner = THIS_MODULE,
Hauke Mehrtensa3906892013-01-12 18:14:09 +0100170 .start = bcm47xx_wdt_soft_start,
171 .stop = bcm47xx_wdt_soft_stop,
172 .ping = bcm47xx_wdt_soft_keepalive,
173 .set_timeout = bcm47xx_wdt_soft_set_timeout,
Damien Riegel65a4a1d2015-11-16 12:28:00 -0500174 .restart = bcm47xx_wdt_restart,
matthieu castet90074dc2009-06-05 18:57:18 +0200175};
176
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100177static int bcm47xx_wdt_probe(struct platform_device *pdev)
matthieu castet90074dc2009-06-05 18:57:18 +0200178{
179 int ret;
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100180 bool soft;
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100181 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
matthieu castet90074dc2009-06-05 18:57:18 +0200182
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100183 if (!wdt)
184 return -ENXIO;
matthieu castet90074dc2009-06-05 18:57:18 +0200185
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100186 soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
matthieu castet90074dc2009-06-05 18:57:18 +0200187
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100188 if (soft) {
189 wdt->wdd.ops = &bcm47xx_wdt_soft_ops;
Kees Cooke99e88a2017-10-16 14:43:17 -0700190 timer_setup(&wdt->soft_timer, bcm47xx_wdt_soft_timer_tick, 0);
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100191 } else {
192 wdt->wdd.ops = &bcm47xx_wdt_hard_ops;
193 }
194
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100195 wdt->wdd.info = &bcm47xx_wdt_info;
196 wdt->wdd.timeout = WDT_DEFAULT_TIME;
Pratyush Anand65518812015-08-20 14:05:01 +0530197 wdt->wdd.parent = &pdev->dev;
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100198 ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
matthieu castet90074dc2009-06-05 18:57:18 +0200199 if (ret)
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100200 goto err_timer;
201 watchdog_set_nowayout(&wdt->wdd, nowayout);
Damien Riegel65a4a1d2015-11-16 12:28:00 -0500202 watchdog_set_restart_priority(&wdt->wdd, 64);
Damien Riegel2786aad2015-11-20 16:54:52 -0500203 watchdog_stop_on_reboot(&wdt->wdd);
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100204
Rafał Miłecki1cc74952015-01-25 11:40:57 +0100205 ret = watchdog_register_device(&wdt->wdd);
206 if (ret)
Damien Riegel2786aad2015-11-20 16:54:52 -0500207 goto err_timer;
Rafał Miłecki1cc74952015-01-25 11:40:57 +0100208
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100209 dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
210 timeout, nowayout ? ", nowayout" : "",
211 soft ? ", Software Timer" : "");
matthieu castet90074dc2009-06-05 18:57:18 +0200212 return 0;
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100213
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100214err_timer:
Hauke Mehrtense3e83d02013-01-12 18:14:11 +0100215 if (soft)
216 del_timer_sync(&wdt->soft_timer);
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100217
218 return ret;
matthieu castet90074dc2009-06-05 18:57:18 +0200219}
220
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100221static int bcm47xx_wdt_remove(struct platform_device *pdev)
matthieu castet90074dc2009-06-05 18:57:18 +0200222{
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100223 struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
matthieu castet90074dc2009-06-05 18:57:18 +0200224
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100225 watchdog_unregister_device(&wdt->wdd);
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100226
227 return 0;
matthieu castet90074dc2009-06-05 18:57:18 +0200228}
229
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100230static struct platform_driver bcm47xx_wdt_driver = {
231 .driver = {
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100232 .name = "bcm47xx-wdt",
233 },
234 .probe = bcm47xx_wdt_probe,
235 .remove = bcm47xx_wdt_remove,
236};
237
238module_platform_driver(bcm47xx_wdt_driver);
matthieu castet90074dc2009-06-05 18:57:18 +0200239
240MODULE_AUTHOR("Aleksandar Radovanovic");
Hauke Mehrtensf82dedf2013-01-24 18:13:34 +0100241MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
matthieu castet90074dc2009-06-05 18:57:18 +0200242MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
243MODULE_LICENSE("GPL");