Blame view
drivers/watchdog/shwdt.c
9.1 KB
1da177e4c Linux-2.6.12-rc2 |
1 |
/* |
b1fa888e0 watchdog: shwdt: ... |
2 |
* drivers/watchdog/shwdt.c |
1da177e4c Linux-2.6.12-rc2 |
3 4 5 |
* * Watchdog driver for integrated watchdog in the SuperH processors. * |
409681263 watchdog: shwdt: ... |
6 |
* Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org> |
1da177e4c Linux-2.6.12-rc2 |
7 8 9 10 11 12 13 14 15 16 17 18 19 |
* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT * * 19-Apr-2002 Rob Radez <rob@osinvestor.com> * Added expect close support, made emulated timeout runtime changeable * general cleanups, add some ioctls */ |
27c766aaa watchdog: Use pr_... |
20 21 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4c Linux-2.6.12-rc2 |
22 23 |
#include <linux/module.h> #include <linux/moduleparam.h> |
8f5585ec3 watchdog: shwdt: ... |
24 |
#include <linux/platform_device.h> |
1da177e4c Linux-2.6.12-rc2 |
25 26 |
#include <linux/init.h> #include <linux/types.h> |
f9fb360cb watchdog: shwdt: ... |
27 |
#include <linux/spinlock.h> |
1da177e4c Linux-2.6.12-rc2 |
28 |
#include <linux/watchdog.h> |
8c013d964 watchdog: shwdt: ... |
29 |
#include <linux/pm_runtime.h> |
1da177e4c Linux-2.6.12-rc2 |
30 |
#include <linux/fs.h> |
f118420be watchdog: Add a s... |
31 |
#include <linux/mm.h> |
8f5585ec3 watchdog: shwdt: ... |
32 |
#include <linux/slab.h> |
70b814ec1 [WATCHDOG 45/57] ... |
33 |
#include <linux/io.h> |
9ea640469 watchdog: shwdt: ... |
34 |
#include <linux/clk.h> |
6330c7070 watchdog: Convert... |
35 |
#include <linux/err.h> |
58cf41984 [WATCHDOG] fix wa... |
36 |
#include <asm/watchdog.h> |
1da177e4c Linux-2.6.12-rc2 |
37 |
|
8f5585ec3 watchdog: shwdt: ... |
38 |
#define DRV_NAME "sh-wdt" |
1da177e4c Linux-2.6.12-rc2 |
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* * Default clock division ratio is 5.25 msecs. For an additional table of * values, consult the asm-sh/watchdog.h. Overload this at module load * time. * * In order for this to work reliably we need to have HZ set to 1000 or * something quite higher than 100 (or we need a proper high-res timer * implementation that will deal with this properly), otherwise the 10ms * resolution of a jiffy is enough to trigger the overflow. For things like * the SH-4 and SH-5, this isn't necessarily that big of a problem, though * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely * necssary. * * As a result of this timing problem, the only modes that are particularly |
25985edce Fix common misspe... |
54 |
* feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms |
1da177e4c Linux-2.6.12-rc2 |
55 56 57 |
* overflow periods respectively. * * Also, since we can't really expect userspace to be responsive enough |
ee0fc097e drivers/watchdog/... |
58 |
* before the overflow happens, we maintain two separate timers .. One in |
1da177e4c Linux-2.6.12-rc2 |
59 60 61 62 63 64 65 66 67 |
* the kernel for clearing out WOVF every 2ms or so (again, this depends on * HZ == 1000), and another for monitoring userspace writes to the WDT device. * * As such, we currently use a configurable heartbeat interval which defaults * to 30s. In this case, the userspace daemon is only responsible for periodic * writes to the device before the next heartbeat is scheduled. If the daemon * misses its deadline, the kernel timer will allow the WDT to overflow. */ static int clock_division_ratio = WTCSR_CKS_4096; |
bea190662 watchdog: shwdt: ... |
68 |
#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4)) |
1da177e4c Linux-2.6.12-rc2 |
69 |
|
1da177e4c Linux-2.6.12-rc2 |
70 71 |
#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ |
86a1e1896 watchdog: nowayou... |
72 |
static bool nowayout = WATCHDOG_NOWAYOUT; |
8f5585ec3 watchdog: shwdt: ... |
73 74 75 76 77 |
static unsigned long next_heartbeat; struct sh_wdt { void __iomem *base; struct device *dev; |
9ea640469 watchdog: shwdt: ... |
78 |
struct clk *clk; |
f9fb360cb watchdog: shwdt: ... |
79 |
spinlock_t lock; |
1da177e4c Linux-2.6.12-rc2 |
80 |
|
8f5585ec3 watchdog: shwdt: ... |
81 |
struct timer_list timer; |
8f5585ec3 watchdog: shwdt: ... |
82 |
}; |
1950f499d watchdog: shwdt: ... |
83 |
static int sh_wdt_start(struct watchdog_device *wdt_dev) |
1da177e4c Linux-2.6.12-rc2 |
84 |
{ |
1950f499d watchdog: shwdt: ... |
85 |
struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
70b814ec1 [WATCHDOG 45/57] ... |
86 |
unsigned long flags; |
8f5585ec3 watchdog: shwdt: ... |
87 |
u8 csr; |
70b814ec1 [WATCHDOG 45/57] ... |
88 |
|
8c013d964 watchdog: shwdt: ... |
89 |
pm_runtime_get_sync(wdt->dev); |
d42c97443 watchdog: shwdt: ... |
90 |
clk_enable(wdt->clk); |
8c013d964 watchdog: shwdt: ... |
91 |
|
f9fb360cb watchdog: shwdt: ... |
92 |
spin_lock_irqsave(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
93 94 |
next_heartbeat = jiffies + (heartbeat * HZ); |
8f5585ec3 watchdog: shwdt: ... |
95 |
mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
1da177e4c Linux-2.6.12-rc2 |
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
csr = sh_wdt_read_csr(); csr |= WTCSR_WT | clock_division_ratio; sh_wdt_write_csr(csr); sh_wdt_write_cnt(0); /* * These processors have a bit of an inconsistent initialization * process.. starting with SH-3, RSTS was moved to WTCSR, and the * RSTCSR register was removed. * * On the SH-2 however, in addition with bits being in different * locations, we must deal with RSTCSR outright.. */ csr = sh_wdt_read_csr(); csr |= WTCSR_TME; csr &= ~WTCSR_RSTS; sh_wdt_write_csr(csr); #ifdef CONFIG_CPU_SH2 |
1da177e4c Linux-2.6.12-rc2 |
117 118 119 120 |
csr = sh_wdt_read_rstcsr(); csr &= ~RSTCSR_RSTS; sh_wdt_write_rstcsr(csr); #endif |
f9fb360cb watchdog: shwdt: ... |
121 |
spin_unlock_irqrestore(&wdt->lock, flags); |
1950f499d watchdog: shwdt: ... |
122 123 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
124 |
} |
1950f499d watchdog: shwdt: ... |
125 |
static int sh_wdt_stop(struct watchdog_device *wdt_dev) |
1da177e4c Linux-2.6.12-rc2 |
126 |
{ |
1950f499d watchdog: shwdt: ... |
127 |
struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
70b814ec1 [WATCHDOG 45/57] ... |
128 |
unsigned long flags; |
8f5585ec3 watchdog: shwdt: ... |
129 |
u8 csr; |
70b814ec1 [WATCHDOG 45/57] ... |
130 |
|
f9fb360cb watchdog: shwdt: ... |
131 |
spin_lock_irqsave(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
132 |
|
8f5585ec3 watchdog: shwdt: ... |
133 |
del_timer(&wdt->timer); |
1da177e4c Linux-2.6.12-rc2 |
134 135 136 137 |
csr = sh_wdt_read_csr(); csr &= ~WTCSR_TME; sh_wdt_write_csr(csr); |
8f5585ec3 watchdog: shwdt: ... |
138 |
|
f9fb360cb watchdog: shwdt: ... |
139 |
spin_unlock_irqrestore(&wdt->lock, flags); |
1950f499d watchdog: shwdt: ... |
140 |
|
d42c97443 watchdog: shwdt: ... |
141 |
clk_disable(wdt->clk); |
8c013d964 watchdog: shwdt: ... |
142 |
pm_runtime_put_sync(wdt->dev); |
1950f499d watchdog: shwdt: ... |
143 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
144 |
} |
1950f499d watchdog: shwdt: ... |
145 |
static int sh_wdt_keepalive(struct watchdog_device *wdt_dev) |
1da177e4c Linux-2.6.12-rc2 |
146 |
{ |
f9fb360cb watchdog: shwdt: ... |
147 |
struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
70b814ec1 [WATCHDOG 45/57] ... |
148 |
unsigned long flags; |
f9fb360cb watchdog: shwdt: ... |
149 |
spin_lock_irqsave(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
150 |
next_heartbeat = jiffies + (heartbeat * HZ); |
f9fb360cb watchdog: shwdt: ... |
151 |
spin_unlock_irqrestore(&wdt->lock, flags); |
1950f499d watchdog: shwdt: ... |
152 153 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
154 |
} |
1950f499d watchdog: shwdt: ... |
155 |
static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t) |
1da177e4c Linux-2.6.12-rc2 |
156 |
{ |
f9fb360cb watchdog: shwdt: ... |
157 |
struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); |
70b814ec1 [WATCHDOG 45/57] ... |
158 159 160 |
unsigned long flags; if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */ |
1da177e4c Linux-2.6.12-rc2 |
161 |
return -EINVAL; |
f9fb360cb watchdog: shwdt: ... |
162 |
spin_lock_irqsave(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
163 |
heartbeat = t; |
1950f499d watchdog: shwdt: ... |
164 |
wdt_dev->timeout = t; |
f9fb360cb watchdog: shwdt: ... |
165 |
spin_unlock_irqrestore(&wdt->lock, flags); |
1950f499d watchdog: shwdt: ... |
166 |
|
1da177e4c Linux-2.6.12-rc2 |
167 168 |
return 0; } |
1da177e4c Linux-2.6.12-rc2 |
169 170 |
static void sh_wdt_ping(unsigned long data) { |
8f5585ec3 watchdog: shwdt: ... |
171 |
struct sh_wdt *wdt = (struct sh_wdt *)data; |
70b814ec1 [WATCHDOG 45/57] ... |
172 |
unsigned long flags; |
f9fb360cb watchdog: shwdt: ... |
173 |
spin_lock_irqsave(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
174 |
if (time_before(jiffies, next_heartbeat)) { |
8f5585ec3 watchdog: shwdt: ... |
175 |
u8 csr; |
1da177e4c Linux-2.6.12-rc2 |
176 177 178 179 180 181 |
csr = sh_wdt_read_csr(); csr &= ~WTCSR_IOVF; sh_wdt_write_csr(csr); sh_wdt_write_cnt(0); |
8f5585ec3 watchdog: shwdt: ... |
182 |
mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
e4c2cfee5 sh: Various cosme... |
183 |
} else |
8f5585ec3 watchdog: shwdt: ... |
184 185 186 |
dev_warn(wdt->dev, "Heartbeat lost! Will not ping " "the watchdog "); |
f9fb360cb watchdog: shwdt: ... |
187 |
spin_unlock_irqrestore(&wdt->lock, flags); |
1da177e4c Linux-2.6.12-rc2 |
188 |
} |
70b814ec1 [WATCHDOG 45/57] ... |
189 |
static const struct watchdog_info sh_wdt_info = { |
e4c2cfee5 sh: Various cosme... |
190 191 |
.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, |
1da177e4c Linux-2.6.12-rc2 |
192 193 194 |
.firmware_version = 1, .identity = "SH WDT", }; |
1950f499d watchdog: shwdt: ... |
195 196 197 198 199 200 201 202 203 204 205 |
static const struct watchdog_ops sh_wdt_ops = { .owner = THIS_MODULE, .start = sh_wdt_start, .stop = sh_wdt_stop, .ping = sh_wdt_keepalive, .set_timeout = sh_wdt_set_heartbeat, }; static struct watchdog_device sh_wdt_dev = { .info = &sh_wdt_info, .ops = &sh_wdt_ops, |
1da177e4c Linux-2.6.12-rc2 |
206 |
}; |
2d991a164 watchdog: remove ... |
207 |
static int sh_wdt_probe(struct platform_device *pdev) |
1da177e4c Linux-2.6.12-rc2 |
208 |
{ |
8f5585ec3 watchdog: shwdt: ... |
209 210 |
struct sh_wdt *wdt; struct resource *res; |
1da177e4c Linux-2.6.12-rc2 |
211 |
int rc; |
8f5585ec3 watchdog: shwdt: ... |
212 213 214 215 216 217 |
/* * As this driver only covers the global watchdog case, reject * any attempts to register per-CPU watchdogs. */ if (pdev->id != -1) return -EINVAL; |
8f5585ec3 watchdog: shwdt: ... |
218 |
wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL); |
9ea640469 watchdog: shwdt: ... |
219 220 |
if (unlikely(!wdt)) return -ENOMEM; |
1da177e4c Linux-2.6.12-rc2 |
221 |
|
8f5585ec3 watchdog: shwdt: ... |
222 |
wdt->dev = &pdev->dev; |
2f7b9b488 watchdog: shwdt: ... |
223 |
wdt->clk = devm_clk_get(&pdev->dev, NULL); |
9ea640469 watchdog: shwdt: ... |
224 225 226 227 228 229 230 |
if (IS_ERR(wdt->clk)) { /* * Clock framework support is optional, continue on * anyways if we don't find a matching clock. */ wdt->clk = NULL; } |
f9fb360cb watchdog: shwdt: ... |
231 |
|
2cdf25bb5 watchdog: shwdt: ... |
232 |
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
6330c7070 watchdog: Convert... |
233 |
wdt->base = devm_ioremap_resource(wdt->dev, res); |
2f7b9b488 watchdog: shwdt: ... |
234 235 |
if (IS_ERR(wdt->base)) return PTR_ERR(wdt->base); |
1da177e4c Linux-2.6.12-rc2 |
236 |
|
9ea640469 watchdog: shwdt: ... |
237 238 |
watchdog_set_nowayout(&sh_wdt_dev, nowayout); watchdog_set_drvdata(&sh_wdt_dev, wdt); |
6551881c8 Watchdog: Fix par... |
239 |
sh_wdt_dev.parent = &pdev->dev; |
9ea640469 watchdog: shwdt: ... |
240 |
|
f9fb360cb watchdog: shwdt: ... |
241 |
spin_lock_init(&wdt->lock); |
1950f499d watchdog: shwdt: ... |
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat); if (unlikely(rc)) { /* Default timeout if invalid */ sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT); dev_warn(&pdev->dev, "heartbeat value must be 1<=x<=3600, using %d ", sh_wdt_dev.timeout); } dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d) ", sh_wdt_dev.timeout, nowayout); |
8f5585ec3 watchdog: shwdt: ... |
256 |
|
1950f499d watchdog: shwdt: ... |
257 |
rc = watchdog_register_device(&sh_wdt_dev); |
e4c2cfee5 sh: Various cosme... |
258 |
if (unlikely(rc)) { |
1950f499d watchdog: shwdt: ... |
259 260 |
dev_err(&pdev->dev, "Can't register watchdog (err=%d) ", rc); |
2f7b9b488 watchdog: shwdt: ... |
261 |
return rc; |
1da177e4c Linux-2.6.12-rc2 |
262 |
} |
12fe36e2c watchdog: shwdt: ... |
263 |
setup_timer(&wdt->timer, sh_wdt_ping, (unsigned long)wdt); |
8f5585ec3 watchdog: shwdt: ... |
264 |
wdt->timer.expires = next_ping_period(clock_division_ratio); |
8f5585ec3 watchdog: shwdt: ... |
265 266 |
dev_info(&pdev->dev, "initialized. "); |
1da177e4c Linux-2.6.12-rc2 |
267 |
|
8c013d964 watchdog: shwdt: ... |
268 |
pm_runtime_enable(&pdev->dev); |
1da177e4c Linux-2.6.12-rc2 |
269 270 |
return 0; } |
4b12b896c watchdog: remove ... |
271 |
static int sh_wdt_remove(struct platform_device *pdev) |
1da177e4c Linux-2.6.12-rc2 |
272 |
{ |
1950f499d watchdog: shwdt: ... |
273 |
watchdog_unregister_device(&sh_wdt_dev); |
8f5585ec3 watchdog: shwdt: ... |
274 |
|
8c013d964 watchdog: shwdt: ... |
275 |
pm_runtime_disable(&pdev->dev); |
8f5585ec3 watchdog: shwdt: ... |
276 277 278 |
return 0; } |
409681263 watchdog: shwdt: ... |
279 280 |
static void sh_wdt_shutdown(struct platform_device *pdev) { |
1950f499d watchdog: shwdt: ... |
281 |
sh_wdt_stop(&sh_wdt_dev); |
409681263 watchdog: shwdt: ... |
282 |
} |
8f5585ec3 watchdog: shwdt: ... |
283 284 285 |
static struct platform_driver sh_wdt_driver = { .driver = { .name = DRV_NAME, |
8f5585ec3 watchdog: shwdt: ... |
286 |
}, |
409681263 watchdog: shwdt: ... |
287 |
.probe = sh_wdt_probe, |
82268714b watchdog: remove ... |
288 |
.remove = sh_wdt_remove, |
409681263 watchdog: shwdt: ... |
289 |
.shutdown = sh_wdt_shutdown, |
8f5585ec3 watchdog: shwdt: ... |
290 291 292 293 |
}; static int __init sh_wdt_init(void) { |
8f5585ec3 watchdog: shwdt: ... |
294 295 296 |
if (unlikely(clock_division_ratio < 0x5 || clock_division_ratio > 0x7)) { clock_division_ratio = WTCSR_CKS_4096; |
27c766aaa watchdog: Use pr_... |
297 298 299 |
pr_info("divisor must be 0x5<=x<=0x7, using %d ", clock_division_ratio); |
8f5585ec3 watchdog: shwdt: ... |
300 |
} |
8f5585ec3 watchdog: shwdt: ... |
301 |
return platform_driver_register(&sh_wdt_driver); |
1da177e4c Linux-2.6.12-rc2 |
302 |
} |
8f5585ec3 watchdog: shwdt: ... |
303 304 305 306 307 308 |
static void __exit sh_wdt_exit(void) { platform_driver_unregister(&sh_wdt_driver); } module_init(sh_wdt_init); module_exit(sh_wdt_exit); |
1da177e4c Linux-2.6.12-rc2 |
309 310 311 |
MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); MODULE_DESCRIPTION("SuperH watchdog driver"); MODULE_LICENSE("GPL"); |
8f5585ec3 watchdog: shwdt: ... |
312 |
MODULE_ALIAS("platform:" DRV_NAME); |
1da177e4c Linux-2.6.12-rc2 |
313 314 |
module_param(clock_division_ratio, int, 0); |
a77dba7e4 [WATCHDOG] Some m... |
315 316 |
MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) " |
76550d329 watchdog: fix sev... |
317 |
"to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")"); |
1da177e4c Linux-2.6.12-rc2 |
318 319 |
module_param(heartbeat, int, 0); |
70b814ec1 [WATCHDOG 45/57] ... |
320 321 322 |
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
1da177e4c Linux-2.6.12-rc2 |
323 |
|
86a1e1896 watchdog: nowayou... |
324 |
module_param(nowayout, bool, 0); |
70b814ec1 [WATCHDOG 45/57] ... |
325 326 327 |
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |