Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mundt | b1fa888 | 2010-05-24 10:55:59 +0900 | [diff] [blame] | 2 | * drivers/watchdog/shwdt.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Watchdog driver for integrated watchdog in the SuperH processors. |
| 5 | * |
Paul Mundt | b1fa888 | 2010-05-24 10:55:59 +0900 | [diff] [blame] | 6 | * Copyright (C) 2001 - 2010 Paul Mundt <lethal@linux-sh.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/watchdog.h> |
| 27 | #include <linux/reboot.h> |
| 28 | #include <linux/notifier.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/fs.h> |
Paul Mundt | f118420 | 2006-09-27 17:53:59 +0900 | [diff] [blame] | 31 | #include <linux/mm.h> |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 33 | #include <linux/io.h> |
| 34 | #include <linux/uaccess.h> |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 35 | #include <asm/watchdog.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 37 | #define DRV_NAME "sh-wdt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Default clock division ratio is 5.25 msecs. For an additional table of |
| 41 | * values, consult the asm-sh/watchdog.h. Overload this at module load |
| 42 | * time. |
| 43 | * |
| 44 | * In order for this to work reliably we need to have HZ set to 1000 or |
| 45 | * something quite higher than 100 (or we need a proper high-res timer |
| 46 | * implementation that will deal with this properly), otherwise the 10ms |
| 47 | * resolution of a jiffy is enough to trigger the overflow. For things like |
| 48 | * the SH-4 and SH-5, this isn't necessarily that big of a problem, though |
| 49 | * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely |
| 50 | * necssary. |
| 51 | * |
| 52 | * 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] | 53 | * 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] | 54 | * overflow periods respectively. |
| 55 | * |
| 56 | * Also, since we can't really expect userspace to be responsive enough |
Joe Perches | ee0fc09 | 2008-02-03 17:32:52 +0200 | [diff] [blame] | 57 | * before the overflow happens, we maintain two separate timers .. One in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * the kernel for clearing out WOVF every 2ms or so (again, this depends on |
| 59 | * HZ == 1000), and another for monitoring userspace writes to the WDT device. |
| 60 | * |
| 61 | * As such, we currently use a configurable heartbeat interval which defaults |
| 62 | * to 30s. In this case, the userspace daemon is only responsible for periodic |
| 63 | * writes to the device before the next heartbeat is scheduled. If the daemon |
| 64 | * misses its deadline, the kernel timer will allow the WDT to overflow. |
| 65 | */ |
| 66 | static int clock_division_ratio = WTCSR_CKS_4096; |
David Engraf | bea1906 | 2011-07-20 15:03:39 +0200 | [diff] [blame] | 67 | #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 69 | static const struct watchdog_info sh_wdt_info; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 70 | static struct platform_device *sh_wdt_dev; |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 71 | static DEFINE_SPINLOCK(shwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ |
| 74 | static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 75 | static int nowayout = WATCHDOG_NOWAYOUT; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 76 | static unsigned long next_heartbeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 78 | struct sh_wdt { |
| 79 | void __iomem *base; |
| 80 | struct device *dev; |
| 81 | |
| 82 | struct timer_list timer; |
| 83 | |
| 84 | unsigned long enabled; |
| 85 | char expect_close; |
| 86 | }; |
| 87 | |
| 88 | static void sh_wdt_start(struct sh_wdt *wdt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 90 | unsigned long flags; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 91 | u8 csr; |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 92 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 93 | spin_lock_irqsave(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | next_heartbeat = jiffies + (heartbeat * HZ); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 96 | mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | csr = sh_wdt_read_csr(); |
| 99 | csr |= WTCSR_WT | clock_division_ratio; |
| 100 | sh_wdt_write_csr(csr); |
| 101 | |
| 102 | sh_wdt_write_cnt(0); |
| 103 | |
| 104 | /* |
| 105 | * These processors have a bit of an inconsistent initialization |
| 106 | * process.. starting with SH-3, RSTS was moved to WTCSR, and the |
| 107 | * RSTCSR register was removed. |
| 108 | * |
| 109 | * On the SH-2 however, in addition with bits being in different |
| 110 | * locations, we must deal with RSTCSR outright.. |
| 111 | */ |
| 112 | csr = sh_wdt_read_csr(); |
| 113 | csr |= WTCSR_TME; |
| 114 | csr &= ~WTCSR_RSTS; |
| 115 | sh_wdt_write_csr(csr); |
| 116 | |
| 117 | #ifdef CONFIG_CPU_SH2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | csr = sh_wdt_read_rstcsr(); |
| 119 | csr &= ~RSTCSR_RSTS; |
| 120 | sh_wdt_write_rstcsr(csr); |
| 121 | #endif |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 122 | spin_unlock_irqrestore(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 125 | static void sh_wdt_stop(struct sh_wdt *wdt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
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 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 130 | spin_lock_irqsave(&shwdt_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 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 138 | spin_unlock_irqrestore(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 141 | static inline void sh_wdt_keepalive(struct sh_wdt *wdt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 143 | unsigned long flags; |
| 144 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 145 | spin_lock_irqsave(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | next_heartbeat = jiffies + (heartbeat * HZ); |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 147 | spin_unlock_irqrestore(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static int sh_wdt_set_heartbeat(int t) |
| 151 | { |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 152 | unsigned long flags; |
| 153 | |
| 154 | if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 157 | spin_lock_irqsave(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | heartbeat = t; |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 159 | spin_unlock_irqrestore(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | static void sh_wdt_ping(unsigned long data) |
| 164 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 165 | struct sh_wdt *wdt = (struct sh_wdt *)data; |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 166 | unsigned long flags; |
| 167 | |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 168 | spin_lock_irqsave(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (time_before(jiffies, next_heartbeat)) { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 170 | u8 csr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | csr = sh_wdt_read_csr(); |
| 173 | csr &= ~WTCSR_IOVF; |
| 174 | sh_wdt_write_csr(csr); |
| 175 | |
| 176 | sh_wdt_write_cnt(0); |
| 177 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 178 | mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 179 | } else |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 180 | dev_warn(wdt->dev, "Heartbeat lost! Will not ping " |
| 181 | "the watchdog\n"); |
Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 182 | spin_unlock_irqrestore(&shwdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | static int sh_wdt_open(struct inode *inode, struct file *file) |
| 186 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 187 | struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev); |
| 188 | |
| 189 | if (test_and_set_bit(0, &wdt->enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return -EBUSY; |
| 191 | if (nowayout) |
| 192 | __module_get(THIS_MODULE); |
| 193 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 194 | file->private_data = wdt; |
| 195 | |
| 196 | sh_wdt_start(wdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | return nonseekable_open(inode, file); |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | static int sh_wdt_close(struct inode *inode, struct file *file) |
| 202 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 203 | struct sh_wdt *wdt = file->private_data; |
| 204 | |
| 205 | if (wdt->expect_close == 42) { |
| 206 | sh_wdt_stop(wdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } else { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 208 | dev_crit(wdt->dev, "Unexpected close, not " |
| 209 | "stopping watchdog!\n"); |
| 210 | sh_wdt_keepalive(wdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 213 | clear_bit(0, &wdt->enabled); |
| 214 | wdt->expect_close = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | static ssize_t sh_wdt_write(struct file *file, const char *buf, |
| 220 | size_t count, loff_t *ppos) |
| 221 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 222 | struct sh_wdt *wdt = file->private_data; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (count) { |
| 225 | if (!nowayout) { |
| 226 | size_t i; |
| 227 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 228 | wdt->expect_close = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | for (i = 0; i != count; i++) { |
| 231 | char c; |
| 232 | if (get_user(c, buf + i)) |
| 233 | return -EFAULT; |
| 234 | if (c == 'V') |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 235 | wdt->expect_close = 42; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 238 | sh_wdt_keepalive(wdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return count; |
| 242 | } |
| 243 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 244 | static long sh_wdt_ioctl(struct file *file, unsigned int cmd, |
| 245 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 247 | struct sh_wdt *wdt = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | int new_heartbeat; |
| 249 | int options, retval = -EINVAL; |
| 250 | |
| 251 | switch (cmd) { |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 252 | case WDIOC_GETSUPPORT: |
| 253 | return copy_to_user((struct watchdog_info *)arg, |
| 254 | &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0; |
| 255 | case WDIOC_GETSTATUS: |
| 256 | case WDIOC_GETBOOTSTATUS: |
| 257 | return put_user(0, (int *)arg); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 258 | case WDIOC_SETOPTIONS: |
| 259 | if (get_user(options, (int *)arg)) |
| 260 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 262 | if (options & WDIOS_DISABLECARD) { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 263 | sh_wdt_stop(wdt); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 264 | retval = 0; |
| 265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 267 | if (options & WDIOS_ENABLECARD) { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 268 | sh_wdt_start(wdt); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 269 | retval = 0; |
| 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 272 | return retval; |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 273 | case WDIOC_KEEPALIVE: |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 274 | sh_wdt_keepalive(wdt); |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | case WDIOC_SETTIMEOUT: |
| 277 | if (get_user(new_heartbeat, (int *)arg)) |
| 278 | return -EFAULT; |
| 279 | |
| 280 | if (sh_wdt_set_heartbeat(new_heartbeat)) |
| 281 | return -EINVAL; |
| 282 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 283 | sh_wdt_keepalive(wdt); |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 284 | /* Fall */ |
| 285 | case WDIOC_GETTIMEOUT: |
| 286 | return put_user(heartbeat, (int *)arg); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 287 | default: |
| 288 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | static int sh_wdt_notify_sys(struct notifier_block *this, |
| 294 | unsigned long code, void *unused) |
| 295 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 296 | struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev); |
| 297 | |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 298 | if (code == SYS_DOWN || code == SYS_HALT) |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 299 | sh_wdt_stop(wdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | return NOTIFY_DONE; |
| 302 | } |
| 303 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 304 | static const struct file_operations sh_wdt_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | .owner = THIS_MODULE, |
| 306 | .llseek = no_llseek, |
| 307 | .write = sh_wdt_write, |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 308 | .unlocked_ioctl = sh_wdt_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | .open = sh_wdt_open, |
| 310 | .release = sh_wdt_close, |
| 311 | }; |
| 312 | |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 313 | static const struct watchdog_info sh_wdt_info = { |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 314 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | |
| 315 | WDIOF_MAGICCLOSE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | .firmware_version = 1, |
| 317 | .identity = "SH WDT", |
| 318 | }; |
| 319 | |
| 320 | static struct notifier_block sh_wdt_notifier = { |
| 321 | .notifier_call = sh_wdt_notify_sys, |
| 322 | }; |
| 323 | |
| 324 | static struct miscdevice sh_wdt_miscdev = { |
| 325 | .minor = WATCHDOG_MINOR, |
| 326 | .name = "watchdog", |
| 327 | .fops = &sh_wdt_fops, |
| 328 | }; |
| 329 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 330 | static int __devinit sh_wdt_probe(struct platform_device *pdev) |
| 331 | { |
| 332 | struct sh_wdt *wdt; |
| 333 | struct resource *res; |
| 334 | int rc; |
| 335 | |
| 336 | /* |
| 337 | * As this driver only covers the global watchdog case, reject |
| 338 | * any attempts to register per-CPU watchdogs. |
| 339 | */ |
| 340 | if (pdev->id != -1) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 344 | if (unlikely(!res)) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | if (!devm_request_mem_region(&pdev->dev, res->start, |
| 348 | resource_size(res), DRV_NAME)) |
| 349 | return -EBUSY; |
| 350 | |
| 351 | wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL); |
| 352 | if (unlikely(!wdt)) { |
| 353 | rc = -ENOMEM; |
| 354 | goto out_release; |
| 355 | } |
| 356 | |
| 357 | wdt->dev = &pdev->dev; |
| 358 | |
| 359 | wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 360 | if (unlikely(!wdt->base)) { |
| 361 | rc = -ENXIO; |
| 362 | goto out_err; |
| 363 | } |
| 364 | |
| 365 | rc = register_reboot_notifier(&sh_wdt_notifier); |
| 366 | if (unlikely(rc)) { |
| 367 | dev_err(&pdev->dev, |
| 368 | "Can't register reboot notifier (err=%d)\n", rc); |
| 369 | goto out_unmap; |
| 370 | } |
| 371 | |
| 372 | sh_wdt_miscdev.parent = wdt->dev; |
| 373 | |
| 374 | rc = misc_register(&sh_wdt_miscdev); |
| 375 | if (unlikely(rc)) { |
| 376 | dev_err(&pdev->dev, |
| 377 | "Can't register miscdev on minor=%d (err=%d)\n", |
| 378 | sh_wdt_miscdev.minor, rc); |
| 379 | goto out_unreg; |
| 380 | } |
| 381 | |
| 382 | init_timer(&wdt->timer); |
| 383 | wdt->timer.function = sh_wdt_ping; |
| 384 | wdt->timer.data = (unsigned long)wdt; |
| 385 | wdt->timer.expires = next_ping_period(clock_division_ratio); |
| 386 | |
| 387 | platform_set_drvdata(pdev, wdt); |
| 388 | sh_wdt_dev = pdev; |
| 389 | |
| 390 | dev_info(&pdev->dev, "initialized.\n"); |
| 391 | |
| 392 | return 0; |
| 393 | |
| 394 | out_unreg: |
| 395 | unregister_reboot_notifier(&sh_wdt_notifier); |
| 396 | out_unmap: |
| 397 | devm_iounmap(&pdev->dev, wdt->base); |
| 398 | out_err: |
| 399 | devm_kfree(&pdev->dev, wdt); |
| 400 | out_release: |
| 401 | devm_release_mem_region(&pdev->dev, res->start, resource_size(res)); |
| 402 | |
| 403 | return rc; |
| 404 | } |
| 405 | |
| 406 | static int __devexit sh_wdt_remove(struct platform_device *pdev) |
| 407 | { |
| 408 | struct sh_wdt *wdt = platform_get_drvdata(pdev); |
| 409 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 410 | |
| 411 | platform_set_drvdata(pdev, NULL); |
| 412 | |
| 413 | misc_deregister(&sh_wdt_miscdev); |
| 414 | |
| 415 | sh_wdt_dev = NULL; |
| 416 | |
| 417 | unregister_reboot_notifier(&sh_wdt_notifier); |
| 418 | devm_release_mem_region(&pdev->dev, res->start, resource_size(res)); |
| 419 | devm_iounmap(&pdev->dev, wdt->base); |
| 420 | devm_kfree(&pdev->dev, wdt); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static struct platform_driver sh_wdt_driver = { |
| 426 | .driver = { |
| 427 | .name = DRV_NAME, |
| 428 | .owner = THIS_MODULE, |
| 429 | }, |
| 430 | |
| 431 | .probe = sh_wdt_probe, |
| 432 | .remove = __devexit_p(sh_wdt_remove), |
| 433 | }; |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | static int __init sh_wdt_init(void) |
| 436 | { |
| 437 | int rc; |
| 438 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 439 | if (unlikely(clock_division_ratio < 0x5 || |
| 440 | clock_division_ratio > 0x7)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | clock_division_ratio = WTCSR_CKS_4096; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 442 | |
| 443 | pr_info("%s: divisor must be 0x5<=x<=0x7, using %d\n", |
| 444 | DRV_NAME, clock_division_ratio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 447 | rc = sh_wdt_set_heartbeat(heartbeat); |
| 448 | if (unlikely(rc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | heartbeat = WATCHDOG_HEARTBEAT; |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 450 | |
| 451 | pr_info("%s: heartbeat value must be 1<=x<=3600, using %d\n", |
| 452 | DRV_NAME, heartbeat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 455 | pr_info("%s: configured with heartbeat=%d sec (nowayout=%d)\n", |
| 456 | DRV_NAME, heartbeat, nowayout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 458 | return platform_driver_register(&sh_wdt_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | static void __exit sh_wdt_exit(void) |
| 462 | { |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 463 | platform_driver_unregister(&sh_wdt_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 465 | module_init(sh_wdt_init); |
| 466 | module_exit(sh_wdt_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); |
| 469 | MODULE_DESCRIPTION("SuperH watchdog driver"); |
| 470 | MODULE_LICENSE("GPL"); |
Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 471 | MODULE_ALIAS("platform:" DRV_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 473 | |
| 474 | module_param(clock_division_ratio, int, 0); |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 475 | MODULE_PARM_DESC(clock_division_ratio, |
| 476 | "Clock division ratio. Valid ranges are from 0x5 (1.31ms) " |
Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 477 | "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | module_param(heartbeat, int, 0); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 480 | MODULE_PARM_DESC(heartbeat, |
| 481 | "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default=" |
| 482 | __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | module_param(nowayout, int, 0); |
Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 485 | MODULE_PARM_DESC(nowayout, |
| 486 | "Watchdog cannot be stopped once started (default=" |
| 487 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |