Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Paul Mundt | b1fa888 | 2010-05-24 10:55:59 +0900 | [diff] [blame] | 3 | * drivers/watchdog/shwdt.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Watchdog driver for integrated watchdog in the SuperH processors. |
| 6 | * |
Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 7 | * Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * 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 Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 16 | |
| 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/types.h> |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/watchdog.h> |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/fs.h> |
Paul Mundt | f118420 | 2006-09-27 17:53:59 +0900 | [diff] [blame] | 28 | #include <linux/mm.h> |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 30 | #include <linux/io.h> |
Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 31 | #include <linux/clk.h> |
Sachin Kamat | 6330c70 | 2013-03-04 10:36:41 +0530 | [diff] [blame] | 32 | #include <linux/err.h> |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 33 | #include <asm/watchdog.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 35 | #define DRV_NAME "sh-wdt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 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 Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 51 | * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * overflow periods respectively. |
| 53 | * |
| 54 | * Also, since we can't really expect userspace to be responsive enough |
Joe Perches | ee0fc09 | 2008-02-03 17:32:52 +0200 | [diff] [blame] | 55 | * before the overflow happens, we maintain two separate timers .. One in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * 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 | */ |
| 64 | static int clock_division_ratio = WTCSR_CKS_4096; |
David Engraf | bea1906 | 2011-07-20 15:03:39 +0200 | [diff] [blame] | 65 | #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ |
| 68 | static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 69 | static bool nowayout = WATCHDOG_NOWAYOUT; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 70 | static unsigned long next_heartbeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 72 | struct sh_wdt { |
| 73 | void __iomem *base; |
| 74 | struct device *dev; |
Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 75 | struct clk *clk; |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 76 | spinlock_t lock; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 77 | |
| 78 | struct timer_list timer; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 79 | }; |
| 80 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 81 | static int sh_wdt_start(struct watchdog_device *wdt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 83 | struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 84 | unsigned long flags; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 85 | u8 csr; |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 86 | |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 87 | pm_runtime_get_sync(wdt->dev); |
Paul Mundt | d42c974 | 2012-05-10 16:14:40 +0900 | [diff] [blame] | 88 | clk_enable(wdt->clk); |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 89 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 90 | spin_lock_irqsave(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | next_heartbeat = jiffies + (heartbeat * HZ); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 93 | mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | csr = sh_wdt_read_rstcsr(); |
| 116 | csr &= ~RSTCSR_RSTS; |
| 117 | sh_wdt_write_rstcsr(csr); |
| 118 | #endif |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 119 | spin_unlock_irqrestore(&wdt->lock, flags); |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 120 | |
| 121 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 124 | static int sh_wdt_stop(struct watchdog_device *wdt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 126 | struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 127 | unsigned long flags; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 128 | u8 csr; |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 129 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 130 | spin_lock_irqsave(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 132 | del_timer(&wdt->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | csr = sh_wdt_read_csr(); |
| 135 | csr &= ~WTCSR_TME; |
| 136 | sh_wdt_write_csr(csr); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 137 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 138 | spin_unlock_irqrestore(&wdt->lock, flags); |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 139 | |
Paul Mundt | d42c974 | 2012-05-10 16:14:40 +0900 | [diff] [blame] | 140 | clk_disable(wdt->clk); |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 141 | pm_runtime_put_sync(wdt->dev); |
| 142 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 146 | static int sh_wdt_keepalive(struct watchdog_device *wdt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 148 | struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 149 | unsigned long flags; |
| 150 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 151 | spin_lock_irqsave(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | next_heartbeat = jiffies + (heartbeat * HZ); |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 153 | spin_unlock_irqrestore(&wdt->lock, flags); |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 154 | |
| 155 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 158 | static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 160 | struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 161 | unsigned long flags; |
| 162 | |
| 163 | if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 166 | spin_lock_irqsave(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | heartbeat = t; |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 168 | wdt_dev->timeout = t; |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 169 | spin_unlock_irqrestore(&wdt->lock, flags); |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 174 | static void sh_wdt_ping(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 176 | struct sh_wdt *wdt = from_timer(wdt, t, timer); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 177 | unsigned long flags; |
| 178 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 179 | spin_lock_irqsave(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (time_before(jiffies, next_heartbeat)) { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 181 | u8 csr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 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 Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 189 | mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 190 | } else |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 191 | dev_warn(wdt->dev, "Heartbeat lost! Will not ping " |
| 192 | "the watchdog\n"); |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 193 | spin_unlock_irqrestore(&wdt->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 196 | static const struct watchdog_info sh_wdt_info = { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 197 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | |
| 198 | WDIOF_MAGICCLOSE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | .firmware_version = 1, |
| 200 | .identity = "SH WDT", |
| 201 | }; |
| 202 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 203 | static 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 | |
| 211 | static struct watchdog_device sh_wdt_dev = { |
| 212 | .info = &sh_wdt_info, |
| 213 | .ops = &sh_wdt_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 216 | static int sh_wdt_probe(struct platform_device *pdev) |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 217 | { |
| 218 | struct sh_wdt *wdt; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 219 | 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 Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 228 | wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL); |
Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 229 | if (unlikely(!wdt)) |
| 230 | return -ENOMEM; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 231 | |
| 232 | wdt->dev = &pdev->dev; |
| 233 | |
Jingoo Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 234 | wdt->clk = devm_clk_get(&pdev->dev, NULL); |
Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 235 | 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 Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 243 | wdt->base = devm_platform_ioremap_resource(pdev, 0); |
Jingoo Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 244 | if (IS_ERR(wdt->base)) |
| 245 | return PTR_ERR(wdt->base); |
Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 246 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 247 | watchdog_set_nowayout(&sh_wdt_dev, nowayout); |
| 248 | watchdog_set_drvdata(&sh_wdt_dev, wdt); |
Pratyush Anand | 6551881 | 2015-08-20 14:05:01 +0530 | [diff] [blame] | 249 | sh_wdt_dev.parent = &pdev->dev; |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 250 | |
Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 251 | spin_lock_init(&wdt->lock); |
| 252 | |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 253 | rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 254 | if (unlikely(rc)) { |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 255 | /* 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 Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 266 | 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 Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 269 | return rc; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 272 | timer_setup(&wdt->timer, sh_wdt_ping, 0); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 273 | wdt->timer.expires = next_ping_period(clock_division_ratio); |
| 274 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 275 | dev_info(&pdev->dev, "initialized.\n"); |
| 276 | |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 277 | pm_runtime_enable(&pdev->dev); |
| 278 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 279 | return 0; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 280 | } |
| 281 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 282 | static int sh_wdt_remove(struct platform_device *pdev) |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 283 | { |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 284 | watchdog_unregister_device(&sh_wdt_dev); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 285 | |
Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 286 | pm_runtime_disable(&pdev->dev); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 291 | static void sh_wdt_shutdown(struct platform_device *pdev) |
| 292 | { |
Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 293 | sh_wdt_stop(&sh_wdt_dev); |
Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 296 | static struct platform_driver sh_wdt_driver = { |
| 297 | .driver = { |
| 298 | .name = DRV_NAME, |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 299 | }, |
| 300 | |
Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 301 | .probe = sh_wdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 302 | .remove = sh_wdt_remove, |
Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 303 | .shutdown = sh_wdt_shutdown, |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 304 | }; |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | static int __init sh_wdt_init(void) |
| 307 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 308 | if (unlikely(clock_division_ratio < 0x5 || |
| 309 | clock_division_ratio > 0x7)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | clock_division_ratio = WTCSR_CKS_4096; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 311 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 312 | pr_info("divisor must be 0x5<=x<=0x7, using %d\n", |
| 313 | clock_division_ratio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 316 | return platform_driver_register(&sh_wdt_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | static void __exit sh_wdt_exit(void) |
| 320 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 321 | platform_driver_unregister(&sh_wdt_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 323 | module_init(sh_wdt_init); |
| 324 | module_exit(sh_wdt_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); |
| 327 | MODULE_DESCRIPTION("SuperH watchdog driver"); |
| 328 | MODULE_LICENSE("GPL"); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 329 | MODULE_ALIAS("platform:" DRV_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | module_param(clock_division_ratio, int, 0); |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 332 | MODULE_PARM_DESC(clock_division_ratio, |
| 333 | "Clock division ratio. Valid ranges are from 0x5 (1.31ms) " |
Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 334 | "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | module_param(heartbeat, int, 0); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 337 | MODULE_PARM_DESC(heartbeat, |
| 338 | "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default=" |
| 339 | __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 341 | module_param(nowayout, bool, 0); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 342 | MODULE_PARM_DESC(nowayout, |
| 343 | "Watchdog cannot be stopped once started (default=" |
| 344 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |