blob: f55533e0e0454efa94e9384688521ca3400d4a02 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Paul Mundtb1fa8882010-05-24 10:55:59 +09003 * drivers/watchdog/shwdt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Watchdog driver for integrated watchdog in the SuperH processors.
6 *
Paul Mundt40968122012-05-10 14:21:15 +09007 * Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
10 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
11 *
12 * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
13 * Added expect close support, made emulated timeout runtime changeable
14 * general cleanups, add some ioctls
15 */
Joe Perches27c766a2012-02-15 15:06:19 -080016
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleparam.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090021#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/types.h>
Paul Mundtf9fb3602012-05-10 15:15:08 +090024#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/watchdog.h>
Paul Mundt8c013d962012-05-10 16:08:35 +090026#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/fs.h>
Paul Mundtf1184202006-09-27 17:53:59 +090028#include <linux/mm.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090029#include <linux/slab.h>
Alan Cox70b814e2008-05-19 14:08:55 +010030#include <linux/io.h>
Paul Mundt9ea64042012-05-10 15:46:48 +090031#include <linux/clk.h>
Sachin Kamat6330c702013-03-04 10:36:41 +053032#include <linux/err.h>
Adrian Bunk58cf4192008-08-08 18:39:11 +030033#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mundt8f5585e2010-05-25 18:31:12 +090035#define DRV_NAME "sh-wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Default clock division ratio is 5.25 msecs. For an additional table of
39 * values, consult the asm-sh/watchdog.h. Overload this at module load
40 * time.
41 *
42 * In order for this to work reliably we need to have HZ set to 1000 or
43 * something quite higher than 100 (or we need a proper high-res timer
44 * implementation that will deal with this properly), otherwise the 10ms
45 * resolution of a jiffy is enough to trigger the overflow. For things like
46 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
47 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
48 * necssary.
49 *
50 * As a result of this timing problem, the only modes that are particularly
Lucas De Marchi25985ed2011-03-30 22:57:33 -030051 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * overflow periods respectively.
53 *
54 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020055 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
57 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
58 *
59 * As such, we currently use a configurable heartbeat interval which defaults
60 * to 30s. In this case, the userspace daemon is only responsible for periodic
61 * writes to the device before the next heartbeat is scheduled. If the daemon
62 * misses its deadline, the kernel timer will allow the WDT to overflow.
63 */
64static int clock_division_ratio = WTCSR_CKS_4096;
David Engrafbea19062011-07-20 15:03:39 +020065#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
68static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010069static bool nowayout = WATCHDOG_NOWAYOUT;
Paul Mundt8f5585e2010-05-25 18:31:12 +090070static unsigned long next_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Paul Mundt8f5585e2010-05-25 18:31:12 +090072struct sh_wdt {
73 void __iomem *base;
74 struct device *dev;
Paul Mundt9ea64042012-05-10 15:46:48 +090075 struct clk *clk;
Paul Mundtf9fb3602012-05-10 15:15:08 +090076 spinlock_t lock;
Paul Mundt8f5585e2010-05-25 18:31:12 +090077
78 struct timer_list timer;
Paul Mundt8f5585e2010-05-25 18:31:12 +090079};
80
Paul Mundt1950f492012-05-10 15:07:53 +090081static int sh_wdt_start(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Paul Mundt1950f492012-05-10 15:07:53 +090083 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +010084 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +090085 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +010086
Paul Mundt8c013d962012-05-10 16:08:35 +090087 pm_runtime_get_sync(wdt->dev);
Paul Mundtd42c9742012-05-10 16:14:40 +090088 clk_enable(wdt->clk);
Paul Mundt8c013d962012-05-10 16:08:35 +090089
Paul Mundtf9fb3602012-05-10 15:15:08 +090090 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundt8f5585e2010-05-25 18:31:12 +090093 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 csr = sh_wdt_read_csr();
96 csr |= WTCSR_WT | clock_division_ratio;
97 sh_wdt_write_csr(csr);
98
99 sh_wdt_write_cnt(0);
100
101 /*
102 * These processors have a bit of an inconsistent initialization
103 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
104 * RSTCSR register was removed.
105 *
106 * On the SH-2 however, in addition with bits being in different
107 * locations, we must deal with RSTCSR outright..
108 */
109 csr = sh_wdt_read_csr();
110 csr |= WTCSR_TME;
111 csr &= ~WTCSR_RSTS;
112 sh_wdt_write_csr(csr);
113
114#ifdef CONFIG_CPU_SH2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 csr = sh_wdt_read_rstcsr();
116 csr &= ~RSTCSR_RSTS;
117 sh_wdt_write_rstcsr(csr);
118#endif
Paul Mundtf9fb3602012-05-10 15:15:08 +0900119 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900120
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Paul Mundt1950f492012-05-10 15:07:53 +0900124static int sh_wdt_stop(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Paul Mundt1950f492012-05-10 15:07:53 +0900126 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100127 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900128 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +0100129
Paul Mundtf9fb3602012-05-10 15:15:08 +0900130 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Paul Mundt8f5585e2010-05-25 18:31:12 +0900132 del_timer(&wdt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 csr = sh_wdt_read_csr();
135 csr &= ~WTCSR_TME;
136 sh_wdt_write_csr(csr);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900137
Paul Mundtf9fb3602012-05-10 15:15:08 +0900138 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900139
Paul Mundtd42c9742012-05-10 16:14:40 +0900140 clk_disable(wdt->clk);
Paul Mundt8c013d962012-05-10 16:08:35 +0900141 pm_runtime_put_sync(wdt->dev);
142
Paul Mundt1950f492012-05-10 15:07:53 +0900143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Paul Mundt1950f492012-05-10 15:07:53 +0900146static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900148 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100149 unsigned long flags;
150
Paul Mundtf9fb3602012-05-10 15:15:08 +0900151 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundtf9fb3602012-05-10 15:15:08 +0900153 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900154
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Paul Mundt1950f492012-05-10 15:07:53 +0900158static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900160 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100161 unsigned long flags;
162
163 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -EINVAL;
165
Paul Mundtf9fb3602012-05-10 15:15:08 +0900166 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 heartbeat = t;
Paul Mundt1950f492012-05-10 15:07:53 +0900168 wdt_dev->timeout = t;
Paul Mundtf9fb3602012-05-10 15:15:08 +0900169 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
Kees Cooke99e88a2017-10-16 14:43:17 -0700174static void sh_wdt_ping(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Kees Cooke99e88a2017-10-16 14:43:17 -0700176 struct sh_wdt *wdt = from_timer(wdt, t, timer);
Alan Cox70b814e2008-05-19 14:08:55 +0100177 unsigned long flags;
178
Paul Mundtf9fb3602012-05-10 15:15:08 +0900179 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (time_before(jiffies, next_heartbeat)) {
Paul Mundt8f5585e2010-05-25 18:31:12 +0900181 u8 csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 csr = sh_wdt_read_csr();
184 csr &= ~WTCSR_IOVF;
185 sh_wdt_write_csr(csr);
186
187 sh_wdt_write_cnt(0);
188
Paul Mundt8f5585e2010-05-25 18:31:12 +0900189 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900190 } else
Paul Mundt8f5585e2010-05-25 18:31:12 +0900191 dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
192 "the watchdog\n");
Paul Mundtf9fb3602012-05-10 15:15:08 +0900193 spin_unlock_irqrestore(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Alan Cox70b814e2008-05-19 14:08:55 +0100196static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900197 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
198 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .firmware_version = 1,
200 .identity = "SH WDT",
201};
202
Paul Mundt1950f492012-05-10 15:07:53 +0900203static const struct watchdog_ops sh_wdt_ops = {
204 .owner = THIS_MODULE,
205 .start = sh_wdt_start,
206 .stop = sh_wdt_stop,
207 .ping = sh_wdt_keepalive,
208 .set_timeout = sh_wdt_set_heartbeat,
209};
210
211static struct watchdog_device sh_wdt_dev = {
212 .info = &sh_wdt_info,
213 .ops = &sh_wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Bill Pemberton2d991a12012-11-19 13:21:41 -0500216static int sh_wdt_probe(struct platform_device *pdev)
Paul Mundt8f5585e2010-05-25 18:31:12 +0900217{
218 struct sh_wdt *wdt;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900219 int rc;
220
221 /*
222 * As this driver only covers the global watchdog case, reject
223 * any attempts to register per-CPU watchdogs.
224 */
225 if (pdev->id != -1)
226 return -EINVAL;
227
Paul Mundt8f5585e2010-05-25 18:31:12 +0900228 wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
Paul Mundt9ea64042012-05-10 15:46:48 +0900229 if (unlikely(!wdt))
230 return -ENOMEM;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900231
232 wdt->dev = &pdev->dev;
233
Jingoo Han2f7b9b42013-04-29 18:16:33 +0900234 wdt->clk = devm_clk_get(&pdev->dev, NULL);
Paul Mundt9ea64042012-05-10 15:46:48 +0900235 if (IS_ERR(wdt->clk)) {
236 /*
237 * Clock framework support is optional, continue on
238 * anyways if we don't find a matching clock.
239 */
240 wdt->clk = NULL;
241 }
242
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700243 wdt->base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han2f7b9b42013-04-29 18:16:33 +0900244 if (IS_ERR(wdt->base))
245 return PTR_ERR(wdt->base);
Paul Mundt9ea64042012-05-10 15:46:48 +0900246
Paul Mundtf9fb3602012-05-10 15:15:08 +0900247 watchdog_set_nowayout(&sh_wdt_dev, nowayout);
248 watchdog_set_drvdata(&sh_wdt_dev, wdt);
Pratyush Anand65518812015-08-20 14:05:01 +0530249 sh_wdt_dev.parent = &pdev->dev;
Paul Mundtf9fb3602012-05-10 15:15:08 +0900250
Paul Mundtf9fb3602012-05-10 15:15:08 +0900251 spin_lock_init(&wdt->lock);
252
Paul Mundt1950f492012-05-10 15:07:53 +0900253 rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900254 if (unlikely(rc)) {
Paul Mundt1950f492012-05-10 15:07:53 +0900255 /* Default timeout if invalid */
256 sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
257
258 dev_warn(&pdev->dev,
259 "heartbeat value must be 1<=x<=3600, using %d\n",
260 sh_wdt_dev.timeout);
261 }
262
263 dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
264 sh_wdt_dev.timeout, nowayout);
265
Paul Mundt1950f492012-05-10 15:07:53 +0900266 rc = watchdog_register_device(&sh_wdt_dev);
267 if (unlikely(rc)) {
268 dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
Jingoo Han2f7b9b42013-04-29 18:16:33 +0900269 return rc;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900270 }
271
Kees Cooke99e88a2017-10-16 14:43:17 -0700272 timer_setup(&wdt->timer, sh_wdt_ping, 0);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900273 wdt->timer.expires = next_ping_period(clock_division_ratio);
274
Paul Mundt8f5585e2010-05-25 18:31:12 +0900275 dev_info(&pdev->dev, "initialized.\n");
276
Paul Mundt8c013d962012-05-10 16:08:35 +0900277 pm_runtime_enable(&pdev->dev);
278
Paul Mundt8f5585e2010-05-25 18:31:12 +0900279 return 0;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900280}
281
Bill Pemberton4b12b892012-11-19 13:26:24 -0500282static int sh_wdt_remove(struct platform_device *pdev)
Paul Mundt8f5585e2010-05-25 18:31:12 +0900283{
Paul Mundt1950f492012-05-10 15:07:53 +0900284 watchdog_unregister_device(&sh_wdt_dev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900285
Paul Mundt8c013d962012-05-10 16:08:35 +0900286 pm_runtime_disable(&pdev->dev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900287
288 return 0;
289}
290
Paul Mundt40968122012-05-10 14:21:15 +0900291static void sh_wdt_shutdown(struct platform_device *pdev)
292{
Paul Mundt1950f492012-05-10 15:07:53 +0900293 sh_wdt_stop(&sh_wdt_dev);
Paul Mundt40968122012-05-10 14:21:15 +0900294}
295
Paul Mundt8f5585e2010-05-25 18:31:12 +0900296static struct platform_driver sh_wdt_driver = {
297 .driver = {
298 .name = DRV_NAME,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900299 },
300
Paul Mundt40968122012-05-10 14:21:15 +0900301 .probe = sh_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500302 .remove = sh_wdt_remove,
Paul Mundt40968122012-05-10 14:21:15 +0900303 .shutdown = sh_wdt_shutdown,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int __init sh_wdt_init(void)
307{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900308 if (unlikely(clock_division_ratio < 0x5 ||
309 clock_division_ratio > 0x7)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 clock_division_ratio = WTCSR_CKS_4096;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900311
Joe Perches27c766a2012-02-15 15:06:19 -0800312 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
313 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
Paul Mundt8f5585e2010-05-25 18:31:12 +0900316 return platform_driver_register(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static void __exit sh_wdt_exit(void)
320{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900321 platform_driver_unregister(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Paul Mundt8f5585e2010-05-25 18:31:12 +0900323module_init(sh_wdt_init);
324module_exit(sh_wdt_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
327MODULE_DESCRIPTION("SuperH watchdog driver");
328MODULE_LICENSE("GPL");
Paul Mundt8f5585e2010-05-25 18:31:12 +0900329MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000332MODULE_PARM_DESC(clock_division_ratio,
333 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
Randy Dunlap76550d32010-05-01 09:46:15 -0700334 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100337MODULE_PARM_DESC(heartbeat,
338 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
339 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100341module_param(nowayout, bool, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100342MODULE_PARM_DESC(nowayout,
343 "Watchdog cannot be stopped once started (default="
344 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");