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