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