Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 2 | /* |
| 3 | * SBC EPX C3 0.1 A Hardware Watchdog Device for the Winsystems EPX-C3 |
| 4 | * single board computer |
| 5 | * |
| 6 | * (c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights |
| 7 | * Reserved. |
| 8 | * |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 9 | * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk> |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/miscdevice.h> |
| 21 | #include <linux/watchdog.h> |
| 22 | #include <linux/notifier.h> |
| 23 | #include <linux/reboot.h> |
| 24 | #include <linux/init.h> |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 25 | #include <linux/ioport.h> |
Alan Cox | f4f6f65 | 2008-05-19 14:08:27 +0100 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/io.h> |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 28 | |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 29 | static int epx_c3_alive; |
| 30 | |
| 31 | #define WATCHDOG_TIMEOUT 1 /* 1 sec default timeout */ |
| 32 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 33 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 34 | module_param(nowayout, bool, 0); |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 35 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 36 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 37 | |
| 38 | #define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */ |
| 39 | #define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */ |
| 40 | |
| 41 | static void epx_c3_start(void) |
| 42 | { |
| 43 | outb(1, EPXC3_WATCHDOG_CTL_REG); |
| 44 | } |
| 45 | |
| 46 | static void epx_c3_stop(void) |
| 47 | { |
| 48 | |
| 49 | outb(0, EPXC3_WATCHDOG_CTL_REG); |
| 50 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 51 | pr_info("Stopped watchdog timer\n"); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void epx_c3_pet(void) |
| 55 | { |
| 56 | outb(1, EPXC3_WATCHDOG_PET_REG); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Allow only one person to hold it open |
| 61 | */ |
| 62 | static int epx_c3_open(struct inode *inode, struct file *file) |
| 63 | { |
| 64 | if (epx_c3_alive) |
| 65 | return -EBUSY; |
| 66 | |
| 67 | if (nowayout) |
| 68 | __module_get(THIS_MODULE); |
| 69 | |
| 70 | /* Activate timer */ |
| 71 | epx_c3_start(); |
| 72 | epx_c3_pet(); |
| 73 | |
| 74 | epx_c3_alive = 1; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 75 | pr_info("Started watchdog timer\n"); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 76 | |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 77 | return stream_open(inode, file); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int epx_c3_release(struct inode *inode, struct file *file) |
| 81 | { |
| 82 | /* Shut off the timer. |
| 83 | * Lock it in if it's a module and we defined ...NOWAYOUT */ |
| 84 | if (!nowayout) |
| 85 | epx_c3_stop(); /* Turn the WDT off */ |
| 86 | |
| 87 | epx_c3_alive = 0; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Al Viro | 73a09e6 | 2006-02-01 06:04:15 -0500 | [diff] [blame] | 92 | static ssize_t epx_c3_write(struct file *file, const char __user *data, |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 93 | size_t len, loff_t *ppos) |
| 94 | { |
| 95 | /* Refresh the timer. */ |
| 96 | if (len) |
| 97 | epx_c3_pet(); |
| 98 | return len; |
| 99 | } |
| 100 | |
Alan Cox | f4f6f65 | 2008-05-19 14:08:27 +0100 | [diff] [blame] | 101 | static long epx_c3_ioctl(struct file *file, unsigned int cmd, |
| 102 | unsigned long arg) |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 103 | { |
| 104 | int options, retval = -EINVAL; |
Al Viro | 73a09e6 | 2006-02-01 06:04:15 -0500 | [diff] [blame] | 105 | int __user *argp = (void __user *)arg; |
Alan Cox | f4f6f65 | 2008-05-19 14:08:27 +0100 | [diff] [blame] | 106 | static const struct watchdog_info ident = { |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 107 | .options = WDIOF_KEEPALIVEPING, |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 108 | .firmware_version = 0, |
| 109 | .identity = "Winsystems EPX-C3 H/W Watchdog", |
| 110 | }; |
| 111 | |
| 112 | switch (cmd) { |
| 113 | case WDIOC_GETSUPPORT: |
Al Viro | 73a09e6 | 2006-02-01 06:04:15 -0500 | [diff] [blame] | 114 | if (copy_to_user(argp, &ident, sizeof(ident))) |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 115 | return -EFAULT; |
| 116 | return 0; |
| 117 | case WDIOC_GETSTATUS: |
| 118 | case WDIOC_GETBOOTSTATUS: |
Al Viro | 73a09e6 | 2006-02-01 06:04:15 -0500 | [diff] [blame] | 119 | return put_user(0, argp); |
Al Viro | 73a09e6 | 2006-02-01 06:04:15 -0500 | [diff] [blame] | 120 | case WDIOC_SETOPTIONS: |
| 121 | if (get_user(options, argp)) |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 122 | return -EFAULT; |
| 123 | |
| 124 | if (options & WDIOS_DISABLECARD) { |
| 125 | epx_c3_stop(); |
| 126 | retval = 0; |
| 127 | } |
| 128 | |
| 129 | if (options & WDIOS_ENABLECARD) { |
| 130 | epx_c3_start(); |
| 131 | retval = 0; |
| 132 | } |
| 133 | |
| 134 | return retval; |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 135 | case WDIOC_KEEPALIVE: |
| 136 | epx_c3_pet(); |
| 137 | return 0; |
| 138 | case WDIOC_GETTIMEOUT: |
| 139 | return put_user(WATCHDOG_TIMEOUT, argp); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 140 | default: |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 141 | return -ENOTTY; |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | static int epx_c3_notify_sys(struct notifier_block *this, unsigned long code, |
| 146 | void *unused) |
| 147 | { |
| 148 | if (code == SYS_DOWN || code == SYS_HALT) |
| 149 | epx_c3_stop(); /* Turn the WDT off */ |
| 150 | |
| 151 | return NOTIFY_DONE; |
| 152 | } |
| 153 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 154 | static const struct file_operations epx_c3_fops = { |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 155 | .owner = THIS_MODULE, |
| 156 | .llseek = no_llseek, |
| 157 | .write = epx_c3_write, |
Alan Cox | f4f6f65 | 2008-05-19 14:08:27 +0100 | [diff] [blame] | 158 | .unlocked_ioctl = epx_c3_ioctl, |
Arnd Bergmann | b6dfb24 | 2019-06-03 14:23:09 +0200 | [diff] [blame] | 159 | .compat_ioctl = compat_ptr_ioctl, |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 160 | .open = epx_c3_open, |
| 161 | .release = epx_c3_release, |
| 162 | }; |
| 163 | |
| 164 | static struct miscdevice epx_c3_miscdev = { |
| 165 | .minor = WATCHDOG_MINOR, |
| 166 | .name = "watchdog", |
| 167 | .fops = &epx_c3_fops, |
| 168 | }; |
| 169 | |
| 170 | static struct notifier_block epx_c3_notifier = { |
| 171 | .notifier_call = epx_c3_notify_sys, |
| 172 | }; |
| 173 | |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 174 | static int __init watchdog_init(void) |
| 175 | { |
| 176 | int ret; |
| 177 | |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 178 | if (!request_region(EPXC3_WATCHDOG_CTL_REG, 2, "epxc3_watchdog")) |
| 179 | return -EBUSY; |
| 180 | |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 181 | ret = register_reboot_notifier(&epx_c3_notifier); |
| 182 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 183 | pr_err("cannot register reboot notifier (err=%d)\n", ret); |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 184 | goto out; |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | ret = misc_register(&epx_c3_miscdev); |
| 188 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 189 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
| 190 | WATCHDOG_MINOR, ret); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 191 | unregister_reboot_notifier(&epx_c3_notifier); |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 192 | goto out; |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 195 | pr_info("Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n"); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 196 | |
| 197 | return 0; |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 198 | |
| 199 | out: |
| 200 | release_region(EPXC3_WATCHDOG_CTL_REG, 2); |
| 201 | return ret; |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void __exit watchdog_exit(void) |
| 205 | { |
| 206 | misc_deregister(&epx_c3_miscdev); |
| 207 | unregister_reboot_notifier(&epx_c3_notifier); |
Alan Cox | de6c642 | 2006-02-03 03:03:58 -0800 | [diff] [blame] | 208 | release_region(EPXC3_WATCHDOG_CTL_REG, 2); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | module_init(watchdog_init); |
| 212 | module_exit(watchdog_exit); |
| 213 | |
| 214 | MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>"); |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 215 | MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC. " |
| 216 | "Note that there is no way to probe for this device -- " |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 217 | "so only use it if you are *sure* you are running on this specific " |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 218 | "SBC system from Winsystems! It writes to IO ports 0x1ee and 0x1ef!"); |
Calin A. Culianu | eed6565 | 2006-01-14 13:20:46 -0800 | [diff] [blame] | 219 | MODULE_LICENSE("GPL"); |