Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sma cpu5 watchdog driver |
| 3 | * |
| 4 | * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/ioport.h> |
| 31 | #include <linux/timer.h> |
Steven Rostedt | 3fe0c27 | 2006-01-09 15:59:26 -0800 | [diff] [blame] | 32 | #include <linux/completion.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 33 | #include <linux/jiffies.h> |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 34 | #include <linux/io.h> |
| 35 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/watchdog.h> |
| 37 | |
| 38 | /* adjustable parameters */ |
| 39 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 40 | static int verbose; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static int port = 0x91; |
| 42 | static int ticks = 10000; |
Axel Lin | 1334f32 | 2011-11-29 13:54:01 +0800 | [diff] [blame] | 43 | static DEFINE_SPINLOCK(cpu5wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #define PFX "cpu5wdt: " |
| 46 | |
| 47 | #define CPU5WDT_EXTENT 0x0A |
| 48 | |
| 49 | #define CPU5WDT_STATUS_REG 0x00 |
| 50 | #define CPU5WDT_TIME_A_REG 0x02 |
| 51 | #define CPU5WDT_TIME_B_REG 0x03 |
| 52 | #define CPU5WDT_MODE_REG 0x04 |
| 53 | #define CPU5WDT_TRIGGER_REG 0x07 |
| 54 | #define CPU5WDT_ENABLE_REG 0x08 |
| 55 | #define CPU5WDT_RESET_REG 0x09 |
| 56 | |
| 57 | #define CPU5WDT_INTERVAL (HZ/10+1) |
| 58 | |
| 59 | /* some device data */ |
| 60 | |
| 61 | static struct { |
Steven Rostedt | 3fe0c27 | 2006-01-09 15:59:26 -0800 | [diff] [blame] | 62 | struct completion stop; |
Florian Fainelli | 996d62d | 2008-02-25 13:39:57 +0100 | [diff] [blame] | 63 | int running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | struct timer_list timer; |
Florian Fainelli | 996d62d | 2008-02-25 13:39:57 +0100 | [diff] [blame] | 65 | int queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int default_ticks; |
| 67 | unsigned long inuse; |
| 68 | } cpu5wdt_device; |
| 69 | |
| 70 | /* generic helper functions */ |
| 71 | |
| 72 | static void cpu5wdt_trigger(unsigned long unused) |
| 73 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 74 | if (verbose > 2) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 75 | pr_debug("trigger at %i ticks\n", ticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 77 | if (cpu5wdt_device.running) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | ticks--; |
| 79 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 80 | spin_lock(&cpu5wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* keep watchdog alive */ |
| 82 | outb(1, port + CPU5WDT_TRIGGER_REG); |
| 83 | |
| 84 | /* requeue?? */ |
Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 85 | if (cpu5wdt_device.queue && ticks) |
| 86 | mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | else { |
| 88 | /* ticks doesn't matter anyway */ |
Steven Rostedt | 3fe0c27 | 2006-01-09 15:59:26 -0800 | [diff] [blame] | 89 | complete(&cpu5wdt_device.stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 91 | spin_unlock(&cpu5wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | |
| 95 | static void cpu5wdt_reset(void) |
| 96 | { |
| 97 | ticks = cpu5wdt_device.default_ticks; |
| 98 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 99 | if (verbose) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 100 | pr_debug("reset (%i ticks)\n", (int) ticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | } |
| 103 | |
| 104 | static void cpu5wdt_start(void) |
| 105 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 106 | unsigned long flags; |
| 107 | |
| 108 | spin_lock_irqsave(&cpu5wdt_lock, flags); |
| 109 | if (!cpu5wdt_device.queue) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | cpu5wdt_device.queue = 1; |
| 111 | outb(0, port + CPU5WDT_TIME_A_REG); |
| 112 | outb(0, port + CPU5WDT_TIME_B_REG); |
| 113 | outb(1, port + CPU5WDT_MODE_REG); |
| 114 | outb(0, port + CPU5WDT_RESET_REG); |
| 115 | outb(0, port + CPU5WDT_ENABLE_REG); |
Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 116 | mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | /* if process dies, counter is not decremented */ |
| 119 | cpu5wdt_device.running++; |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 120 | spin_unlock_irqrestore(&cpu5wdt_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int cpu5wdt_stop(void) |
| 124 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 125 | unsigned long flags; |
| 126 | |
| 127 | spin_lock_irqsave(&cpu5wdt_lock, flags); |
| 128 | if (cpu5wdt_device.running) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | cpu5wdt_device.running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | ticks = cpu5wdt_device.default_ticks; |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 131 | spin_unlock_irqrestore(&cpu5wdt_lock, flags); |
| 132 | if (verbose) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 133 | pr_crit("stop not possible\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -EIO; |
| 135 | } |
| 136 | |
| 137 | /* filesystem operations */ |
| 138 | |
| 139 | static int cpu5wdt_open(struct inode *inode, struct file *file) |
| 140 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 141 | if (test_and_set_bit(0, &cpu5wdt_device.inuse)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return nonseekable_open(inode, file); |
| 144 | } |
| 145 | |
| 146 | static int cpu5wdt_release(struct inode *inode, struct file *file) |
| 147 | { |
| 148 | clear_bit(0, &cpu5wdt_device.inuse); |
| 149 | return 0; |
| 150 | } |
| 151 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 152 | static long cpu5wdt_ioctl(struct file *file, unsigned int cmd, |
| 153 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | void __user *argp = (void __user *)arg; |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 156 | int __user *p = argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | unsigned int value; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 158 | static const struct watchdog_info ident = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | .options = WDIOF_CARDRESET, |
| 160 | .identity = "CPU5 WDT", |
| 161 | }; |
| 162 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 163 | switch (cmd) { |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 164 | case WDIOC_GETSUPPORT: |
| 165 | if (copy_to_user(argp, &ident, sizeof(ident))) |
| 166 | return -EFAULT; |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 167 | break; |
| 168 | case WDIOC_GETSTATUS: |
| 169 | value = inb(port + CPU5WDT_STATUS_REG); |
| 170 | value = (value >> 2) & 1; |
| 171 | return put_user(value, p); |
| 172 | case WDIOC_GETBOOTSTATUS: |
| 173 | return put_user(0, p); |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 174 | case WDIOC_SETOPTIONS: |
| 175 | if (get_user(value, p)) |
| 176 | return -EFAULT; |
| 177 | if (value & WDIOS_ENABLECARD) |
| 178 | cpu5wdt_start(); |
| 179 | if (value & WDIOS_DISABLECARD) |
| 180 | cpu5wdt_stop(); |
| 181 | break; |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 182 | case WDIOC_KEEPALIVE: |
| 183 | cpu5wdt_reset(); |
| 184 | break; |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 185 | default: |
| 186 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 191 | static ssize_t cpu5wdt_write(struct file *file, const char __user *buf, |
| 192 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 194 | if (!count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | cpu5wdt_reset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return count; |
| 198 | } |
| 199 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 200 | static const struct file_operations cpu5wdt_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .owner = THIS_MODULE, |
| 202 | .llseek = no_llseek, |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 203 | .unlocked_ioctl = cpu5wdt_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | .open = cpu5wdt_open, |
| 205 | .write = cpu5wdt_write, |
| 206 | .release = cpu5wdt_release, |
| 207 | }; |
| 208 | |
| 209 | static struct miscdevice cpu5wdt_misc = { |
| 210 | .minor = WATCHDOG_MINOR, |
| 211 | .name = "watchdog", |
| 212 | .fops = &cpu5wdt_fops, |
| 213 | }; |
| 214 | |
| 215 | /* init/exit function */ |
| 216 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 217 | static int cpu5wdt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | unsigned int val; |
| 220 | int err; |
| 221 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 222 | if (verbose) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 223 | pr_debug("port=0x%x, verbose=%i\n", port, verbose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 225 | init_completion(&cpu5wdt_device.stop); |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 226 | cpu5wdt_device.queue = 0; |
| 227 | setup_timer(&cpu5wdt_device.timer, cpu5wdt_trigger, 0); |
| 228 | cpu5wdt_device.default_ticks = ticks; |
| 229 | |
| 230 | if (!request_region(port, CPU5WDT_EXTENT, PFX)) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 231 | pr_err("request_region failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | err = -EBUSY; |
| 233 | goto no_port; |
| 234 | } |
| 235 | |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 236 | /* watchdog reboot? */ |
| 237 | val = inb(port + CPU5WDT_STATUS_REG); |
| 238 | val = (val >> 2) & 1; |
| 239 | if (!val) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 240 | pr_info("sorry, was my fault\n"); |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 241 | |
| 242 | err = misc_register(&cpu5wdt_misc); |
| 243 | if (err < 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 244 | pr_err("misc_register failed\n"); |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 245 | goto no_misc; |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 249 | pr_info("init success\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | no_misc: |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 253 | release_region(port, CPU5WDT_EXTENT); |
| 254 | no_port: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return err; |
| 256 | } |
| 257 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 258 | static int cpu5wdt_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | return cpu5wdt_init(); |
| 261 | } |
| 262 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 263 | static void cpu5wdt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Alan Cox | 6f932f1 | 2008-05-19 14:05:24 +0100 | [diff] [blame] | 265 | if (cpu5wdt_device.queue) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | cpu5wdt_device.queue = 0; |
Steven Rostedt | 3fe0c27 | 2006-01-09 15:59:26 -0800 | [diff] [blame] | 267 | wait_for_completion(&cpu5wdt_device.stop); |
devendra.aaru | e09d9c3 | 2012-06-26 14:48:26 +0530 | [diff] [blame] | 268 | del_timer(&cpu5wdt_device.timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | misc_deregister(&cpu5wdt_misc); |
| 272 | |
| 273 | release_region(port, CPU5WDT_EXTENT); |
| 274 | |
| 275 | } |
| 276 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 277 | static void cpu5wdt_exit_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | cpu5wdt_exit(); |
| 280 | } |
| 281 | |
| 282 | /* module entry points */ |
| 283 | |
| 284 | module_init(cpu5wdt_init_module); |
| 285 | module_exit(cpu5wdt_exit_module); |
| 286 | |
| 287 | MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>"); |
| 288 | MODULE_DESCRIPTION("sma cpu5 watchdog driver"); |
| 289 | MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog"); |
| 290 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | module_param(port, int, 0); |
| 293 | MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91"); |
| 294 | |
| 295 | module_param(verbose, int, 0); |
| 296 | MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)"); |
| 297 | |
| 298 | module_param(ticks, int, 0); |
| 299 | MODULE_PARM_DESC(ticks, "count down ticks, default is 10000"); |