Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/char/watchdog/shwdt.c |
| 3 | * |
| 4 | * Watchdog driver for integrated watchdog in the SuperH processors. |
| 5 | * |
| 6 | * Copyright (C) 2001, 2002, 2003 Paul Mundt <lethal@linux-sh.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> |
| 14 | * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT |
| 15 | * |
| 16 | * 19-Apr-2002 Rob Radez <rob@osinvestor.com> |
| 17 | * Added expect close support, made emulated timeout runtime changeable |
| 18 | * general cleanups, add some ioctls |
| 19 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/moduleparam.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/miscdevice.h> |
| 25 | #include <linux/watchdog.h> |
| 26 | #include <linux/reboot.h> |
| 27 | #include <linux/notifier.h> |
| 28 | #include <linux/ioport.h> |
| 29 | #include <linux/fs.h> |
| 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | #include <asm/watchdog.h> |
| 34 | |
| 35 | #define PFX "shwdt: " |
| 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 |
| 51 | * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms |
| 52 | * overflow periods respectively. |
| 53 | * |
| 54 | * Also, since we can't really expect userspace to be responsive enough |
| 55 | * before the overflow happens, we maintain two seperate timers .. One in |
| 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; |
| 65 | |
| 66 | #define next_ping_period(cks) msecs_to_jiffies(cks - 4) |
| 67 | |
| 68 | static unsigned long shwdt_is_open; |
| 69 | static struct watchdog_info sh_wdt_info; |
| 70 | static char shwdt_expect_close; |
| 71 | static struct timer_list timer; |
| 72 | static unsigned long next_heartbeat; |
| 73 | |
| 74 | #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ |
| 75 | static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ |
| 76 | |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 77 | static int nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * sh_wdt_start - Start the Watchdog |
| 81 | * |
| 82 | * Starts the watchdog. |
| 83 | */ |
| 84 | static void sh_wdt_start(void) |
| 85 | { |
| 86 | __u8 csr; |
| 87 | |
| 88 | next_heartbeat = jiffies + (heartbeat * HZ); |
| 89 | mod_timer(&timer, next_ping_period(clock_division_ratio)); |
| 90 | |
| 91 | csr = sh_wdt_read_csr(); |
| 92 | csr |= WTCSR_WT | clock_division_ratio; |
| 93 | sh_wdt_write_csr(csr); |
| 94 | |
| 95 | sh_wdt_write_cnt(0); |
| 96 | |
| 97 | /* |
| 98 | * These processors have a bit of an inconsistent initialization |
| 99 | * process.. starting with SH-3, RSTS was moved to WTCSR, and the |
| 100 | * RSTCSR register was removed. |
| 101 | * |
| 102 | * On the SH-2 however, in addition with bits being in different |
| 103 | * locations, we must deal with RSTCSR outright.. |
| 104 | */ |
| 105 | csr = sh_wdt_read_csr(); |
| 106 | csr |= WTCSR_TME; |
| 107 | csr &= ~WTCSR_RSTS; |
| 108 | sh_wdt_write_csr(csr); |
| 109 | |
| 110 | #ifdef CONFIG_CPU_SH2 |
| 111 | /* |
| 112 | * Whoever came up with the RSTCSR semantics must've been smoking |
| 113 | * some of the good stuff, since in addition to the WTCSR/WTCNT write |
| 114 | * brain-damage, it's managed to fuck things up one step further.. |
| 115 | * |
| 116 | * If we need to clear the WOVF bit, the upper byte has to be 0xa5.. |
| 117 | * but if we want to touch RSTE or RSTS, the upper byte has to be |
| 118 | * 0x5a.. |
| 119 | */ |
| 120 | csr = sh_wdt_read_rstcsr(); |
| 121 | csr &= ~RSTCSR_RSTS; |
| 122 | sh_wdt_write_rstcsr(csr); |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * sh_wdt_stop - Stop the Watchdog |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * Stops the watchdog. |
| 129 | */ |
| 130 | static void sh_wdt_stop(void) |
| 131 | { |
| 132 | __u8 csr; |
| 133 | |
| 134 | del_timer(&timer); |
| 135 | |
| 136 | csr = sh_wdt_read_csr(); |
| 137 | csr &= ~WTCSR_TME; |
| 138 | sh_wdt_write_csr(csr); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * sh_wdt_keepalive - Keep the Userspace Watchdog Alive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | * The Userspace watchdog got a KeepAlive: schedule the next heartbeat. |
| 144 | */ |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 145 | static inline void sh_wdt_keepalive(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | next_heartbeat = jiffies + (heartbeat * HZ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | * Set the Userspace Watchdog heartbeat |
| 153 | */ |
| 154 | static int sh_wdt_set_heartbeat(int t) |
| 155 | { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 156 | if (unlikely((t < 1) || (t > 3600))) /* arbitrary upper limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | |
| 159 | heartbeat = t; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * sh_wdt_ping - Ping the Watchdog |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * @data: Unused |
| 166 | * |
| 167 | * Clears overflow bit, resets timer counter. |
| 168 | */ |
| 169 | static void sh_wdt_ping(unsigned long data) |
| 170 | { |
| 171 | if (time_before(jiffies, next_heartbeat)) { |
| 172 | __u8 csr; |
| 173 | |
| 174 | csr = sh_wdt_read_csr(); |
| 175 | csr &= ~WTCSR_IOVF; |
| 176 | sh_wdt_write_csr(csr); |
| 177 | |
| 178 | sh_wdt_write_cnt(0); |
| 179 | |
| 180 | mod_timer(&timer, next_ping_period(clock_division_ratio)); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 181 | } else |
| 182 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping " |
| 183 | "the watchdog\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * sh_wdt_open - Open the Device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * @inode: inode of device |
| 189 | * @file: file handle of device |
| 190 | * |
| 191 | * Watchdog device is opened and started. |
| 192 | */ |
| 193 | static int sh_wdt_open(struct inode *inode, struct file *file) |
| 194 | { |
| 195 | if (test_and_set_bit(0, &shwdt_is_open)) |
| 196 | return -EBUSY; |
| 197 | if (nowayout) |
| 198 | __module_get(THIS_MODULE); |
| 199 | |
| 200 | sh_wdt_start(); |
| 201 | |
| 202 | return nonseekable_open(inode, file); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * sh_wdt_close - Close the Device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * @inode: inode of device |
| 208 | * @file: file handle of device |
| 209 | * |
| 210 | * Watchdog device is closed and stopped. |
| 211 | */ |
| 212 | static int sh_wdt_close(struct inode *inode, struct file *file) |
| 213 | { |
| 214 | if (shwdt_expect_close == 42) { |
| 215 | sh_wdt_stop(); |
| 216 | } else { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 217 | printk(KERN_CRIT PFX "Unexpected close, not " |
| 218 | "stopping watchdog!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | sh_wdt_keepalive(); |
| 220 | } |
| 221 | |
| 222 | clear_bit(0, &shwdt_is_open); |
| 223 | shwdt_expect_close = 0; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * sh_wdt_write - Write to Device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * @file: file handle of device |
| 231 | * @buf: buffer to write |
| 232 | * @count: length of buffer |
| 233 | * @ppos: offset |
| 234 | * |
| 235 | * Pings the watchdog on write. |
| 236 | */ |
| 237 | static ssize_t sh_wdt_write(struct file *file, const char *buf, |
| 238 | size_t count, loff_t *ppos) |
| 239 | { |
| 240 | if (count) { |
| 241 | if (!nowayout) { |
| 242 | size_t i; |
| 243 | |
| 244 | shwdt_expect_close = 0; |
| 245 | |
| 246 | for (i = 0; i != count; i++) { |
| 247 | char c; |
| 248 | if (get_user(c, buf + i)) |
| 249 | return -EFAULT; |
| 250 | if (c == 'V') |
| 251 | shwdt_expect_close = 42; |
| 252 | } |
| 253 | } |
| 254 | sh_wdt_keepalive(); |
| 255 | } |
| 256 | |
| 257 | return count; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * sh_wdt_ioctl - Query Device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * @inode: inode of device |
| 263 | * @file: file handle of device |
| 264 | * @cmd: watchdog command |
| 265 | * @arg: argument |
| 266 | * |
| 267 | * Query basic information from the device or ping it, as outlined by the |
| 268 | * watchdog API. |
| 269 | */ |
| 270 | static int sh_wdt_ioctl(struct inode *inode, struct file *file, |
| 271 | unsigned int cmd, unsigned long arg) |
| 272 | { |
| 273 | int new_heartbeat; |
| 274 | int options, retval = -EINVAL; |
| 275 | |
| 276 | switch (cmd) { |
| 277 | case WDIOC_GETSUPPORT: |
| 278 | return copy_to_user((struct watchdog_info *)arg, |
| 279 | &sh_wdt_info, |
| 280 | sizeof(sh_wdt_info)) ? -EFAULT : 0; |
| 281 | case WDIOC_GETSTATUS: |
| 282 | case WDIOC_GETBOOTSTATUS: |
| 283 | return put_user(0, (int *)arg); |
| 284 | case WDIOC_KEEPALIVE: |
| 285 | sh_wdt_keepalive(); |
| 286 | return 0; |
| 287 | case WDIOC_SETTIMEOUT: |
| 288 | if (get_user(new_heartbeat, (int *)arg)) |
| 289 | return -EFAULT; |
| 290 | |
| 291 | if (sh_wdt_set_heartbeat(new_heartbeat)) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | sh_wdt_keepalive(); |
| 295 | /* Fall */ |
| 296 | case WDIOC_GETTIMEOUT: |
| 297 | return put_user(heartbeat, (int *)arg); |
| 298 | case WDIOC_SETOPTIONS: |
| 299 | if (get_user(options, (int *)arg)) |
| 300 | return -EFAULT; |
| 301 | |
| 302 | if (options & WDIOS_DISABLECARD) { |
| 303 | sh_wdt_stop(); |
| 304 | retval = 0; |
| 305 | } |
| 306 | |
| 307 | if (options & WDIOS_ENABLECARD) { |
| 308 | sh_wdt_start(); |
| 309 | retval = 0; |
| 310 | } |
| 311 | |
| 312 | return retval; |
| 313 | default: |
| 314 | return -ENOIOCTLCMD; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * sh_wdt_notify_sys - Notifier Handler |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | * @this: notifier block |
| 323 | * @code: notifier event |
| 324 | * @unused: unused |
| 325 | * |
| 326 | * Handles specific events, such as turning off the watchdog during a |
| 327 | * shutdown event. |
| 328 | */ |
| 329 | static int sh_wdt_notify_sys(struct notifier_block *this, |
| 330 | unsigned long code, void *unused) |
| 331 | { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 332 | if (code == SYS_DOWN || code == SYS_HALT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | sh_wdt_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | return NOTIFY_DONE; |
| 336 | } |
| 337 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 338 | static const struct file_operations sh_wdt_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | .owner = THIS_MODULE, |
| 340 | .llseek = no_llseek, |
| 341 | .write = sh_wdt_write, |
| 342 | .ioctl = sh_wdt_ioctl, |
| 343 | .open = sh_wdt_open, |
| 344 | .release = sh_wdt_close, |
| 345 | }; |
| 346 | |
| 347 | static struct watchdog_info sh_wdt_info = { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 348 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | |
| 349 | WDIOF_MAGICCLOSE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | .firmware_version = 1, |
| 351 | .identity = "SH WDT", |
| 352 | }; |
| 353 | |
| 354 | static struct notifier_block sh_wdt_notifier = { |
| 355 | .notifier_call = sh_wdt_notify_sys, |
| 356 | }; |
| 357 | |
| 358 | static struct miscdevice sh_wdt_miscdev = { |
| 359 | .minor = WATCHDOG_MINOR, |
| 360 | .name = "watchdog", |
| 361 | .fops = &sh_wdt_fops, |
| 362 | }; |
| 363 | |
| 364 | /** |
| 365 | * sh_wdt_init - Initialize module |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | * Registers the device and notifier handler. Actual device |
| 367 | * initialization is handled by sh_wdt_open(). |
| 368 | */ |
| 369 | static int __init sh_wdt_init(void) |
| 370 | { |
| 371 | int rc; |
| 372 | |
| 373 | if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) { |
| 374 | clock_division_ratio = WTCSR_CKS_4096; |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 375 | printk(KERN_INFO PFX "clock_division_ratio value must " |
| 376 | "be 0x5<=x<=0x7, using %d\n", clock_division_ratio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 379 | rc = sh_wdt_set_heartbeat(heartbeat); |
| 380 | if (unlikely(rc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | heartbeat = WATCHDOG_HEARTBEAT; |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 382 | printk(KERN_INFO PFX "heartbeat value must " |
| 383 | "be 1<=x<=3600, using %d\n", heartbeat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | init_timer(&timer); |
| 387 | timer.function = sh_wdt_ping; |
| 388 | timer.data = 0; |
| 389 | |
| 390 | rc = register_reboot_notifier(&sh_wdt_notifier); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 391 | if (unlikely(rc)) { |
| 392 | printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n", |
| 393 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return rc; |
| 395 | } |
| 396 | |
| 397 | rc = misc_register(&sh_wdt_miscdev); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 398 | if (unlikely(rc)) { |
| 399 | printk(KERN_ERR PFX "Can't register miscdev on " |
| 400 | "minor=%d (err=%d)\n", sh_wdt_miscdev.minor, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | unregister_reboot_notifier(&sh_wdt_notifier); |
| 402 | return rc; |
| 403 | } |
| 404 | |
| 405 | printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", |
| 406 | heartbeat, nowayout); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * sh_wdt_exit - Deinitialize module |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | * Unregisters the device and notifier handler. Actual device |
| 414 | * deinitialization is handled by sh_wdt_close(). |
| 415 | */ |
| 416 | static void __exit sh_wdt_exit(void) |
| 417 | { |
| 418 | misc_deregister(&sh_wdt_miscdev); |
| 419 | unregister_reboot_notifier(&sh_wdt_notifier); |
| 420 | } |
| 421 | |
| 422 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); |
| 423 | MODULE_DESCRIPTION("SuperH watchdog driver"); |
| 424 | MODULE_LICENSE("GPL"); |
| 425 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 426 | |
| 427 | module_param(clock_division_ratio, int, 0); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 428 | MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | module_param(heartbeat, int, 0); |
| 431 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
| 432 | |
| 433 | module_param(nowayout, int, 0); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame^] | 434 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | module_init(sh_wdt_init); |
| 437 | module_exit(sh_wdt_exit); |