Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 2 | /* |
Martyn Welch | cda61c9 | 2010-03-01 17:06:20 +0000 | [diff] [blame] | 3 | * GE watchdog userspace interface |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 4 | * |
Martyn Welch | cda61c9 | 2010-03-01 17:06:20 +0000 | [diff] [blame] | 5 | * Author: Martyn Welch <martyn.welch@ge.com> |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 6 | * |
Martyn Welch | cda61c9 | 2010-03-01 17:06:20 +0000 | [diff] [blame] | 7 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 8 | * |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 9 | * Based on: mv64x60_wdt.c (MV64X60 watchdog userspace interface) |
| 10 | * Author: James Chapman <jchapman@katalix.com> |
| 11 | */ |
| 12 | |
| 13 | /* TODO: |
| 14 | * This driver does not provide support for the hardwares capability of sending |
| 15 | * an interrupt at a programmable threshold. |
| 16 | * |
| 17 | * This driver currently can only support 1 watchdog - there are 2 in the |
| 18 | * hardware that this driver supports. Thus one could be configured as a |
| 19 | * process-based watchdog (via /dev/watchdog), the second (using the interrupt |
| 20 | * capabilities) a kernel-based watchdog. |
| 21 | */ |
| 22 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/compiler.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/watchdog.h> |
Wolfram Sang & Martyn Welch | f6e0722 | 2010-12-02 00:11:16 +0100 | [diff] [blame] | 31 | #include <linux/fs.h> |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 32 | #include <linux/of.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 33 | #include <linux/of_address.h> |
Rob Herring | cc85f87 | 2023-07-26 17:33:00 -0600 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 35 | #include <linux/io.h> |
| 36 | #include <linux/uaccess.h> |
| 37 | |
| 38 | #include <sysdev/fsl_soc.h> |
| 39 | |
| 40 | /* |
| 41 | * The watchdog configuration register contains a pair of 2-bit fields, |
| 42 | * 1. a reload field, bits 27-26, which triggers a reload of |
| 43 | * the countdown register, and |
| 44 | * 2. an enable field, bits 25-24, which toggles between |
| 45 | * enabling and disabling the watchdog timer. |
| 46 | * Bit 31 is a read-only field which indicates whether the |
| 47 | * watchdog timer is currently enabled. |
| 48 | * |
| 49 | * The low 24 bits contain the timer reload value. |
| 50 | */ |
| 51 | #define GEF_WDC_ENABLE_SHIFT 24 |
| 52 | #define GEF_WDC_SERVICE_SHIFT 26 |
| 53 | #define GEF_WDC_ENABLED_SHIFT 31 |
| 54 | |
| 55 | #define GEF_WDC_ENABLED_TRUE 1 |
| 56 | #define GEF_WDC_ENABLED_FALSE 0 |
| 57 | |
| 58 | /* Flags bits */ |
| 59 | #define GEF_WDOG_FLAG_OPENED 0 |
| 60 | |
| 61 | static unsigned long wdt_flags; |
| 62 | static int wdt_status; |
| 63 | static void __iomem *gef_wdt_regs; |
| 64 | static int gef_wdt_timeout; |
| 65 | static int gef_wdt_count; |
| 66 | static unsigned int bus_clk; |
| 67 | static char expect_close; |
| 68 | static DEFINE_SPINLOCK(gef_wdt_spinlock); |
| 69 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 70 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 71 | module_param(nowayout, bool, 0); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 72 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 73 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 74 | |
| 75 | |
| 76 | static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift) |
| 77 | { |
| 78 | u32 data; |
| 79 | u32 enabled; |
| 80 | int ret = 0; |
| 81 | |
| 82 | spin_lock(&gef_wdt_spinlock); |
| 83 | data = ioread32be(gef_wdt_regs); |
| 84 | enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1; |
| 85 | |
| 86 | /* only toggle the requested field if enabled state matches predicate */ |
| 87 | if ((enabled ^ enabled_predicate) == 0) { |
| 88 | /* We write a 1, then a 2 -- to the appropriate field */ |
| 89 | data = (1 << field_shift) | gef_wdt_count; |
| 90 | iowrite32be(data, gef_wdt_regs); |
| 91 | |
| 92 | data = (2 << field_shift) | gef_wdt_count; |
| 93 | iowrite32be(data, gef_wdt_regs); |
| 94 | ret = 1; |
| 95 | } |
| 96 | spin_unlock(&gef_wdt_spinlock); |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static void gef_wdt_service(void) |
| 102 | { |
| 103 | gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE, |
| 104 | GEF_WDC_SERVICE_SHIFT); |
| 105 | } |
| 106 | |
| 107 | static void gef_wdt_handler_enable(void) |
| 108 | { |
| 109 | if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE, |
| 110 | GEF_WDC_ENABLE_SHIFT)) { |
| 111 | gef_wdt_service(); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 112 | pr_notice("watchdog activated\n"); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | static void gef_wdt_handler_disable(void) |
| 117 | { |
| 118 | if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE, |
| 119 | GEF_WDC_ENABLE_SHIFT)) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 120 | pr_notice("watchdog deactivated\n"); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void gef_wdt_set_timeout(unsigned int timeout) |
| 124 | { |
| 125 | /* maximum bus cycle count is 0xFFFFFFFF */ |
| 126 | if (timeout > 0xFFFFFFFF / bus_clk) |
| 127 | timeout = 0xFFFFFFFF / bus_clk; |
| 128 | |
| 129 | /* Register only holds upper 24 bits, bit shifted into lower 24 */ |
| 130 | gef_wdt_count = (timeout * bus_clk) >> 8; |
| 131 | gef_wdt_timeout = timeout; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static ssize_t gef_wdt_write(struct file *file, const char __user *data, |
| 136 | size_t len, loff_t *ppos) |
| 137 | { |
| 138 | if (len) { |
| 139 | if (!nowayout) { |
| 140 | size_t i; |
| 141 | |
| 142 | expect_close = 0; |
| 143 | |
| 144 | for (i = 0; i != len; i++) { |
| 145 | char c; |
| 146 | if (get_user(c, data + i)) |
| 147 | return -EFAULT; |
| 148 | if (c == 'V') |
| 149 | expect_close = 42; |
| 150 | } |
| 151 | } |
| 152 | gef_wdt_service(); |
| 153 | } |
| 154 | |
| 155 | return len; |
| 156 | } |
| 157 | |
| 158 | static long gef_wdt_ioctl(struct file *file, unsigned int cmd, |
| 159 | unsigned long arg) |
| 160 | { |
| 161 | int timeout; |
| 162 | int options; |
| 163 | void __user *argp = (void __user *)arg; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 164 | static const struct watchdog_info info = { |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 165 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | |
| 166 | WDIOF_KEEPALIVEPING, |
| 167 | .firmware_version = 0, |
Martyn Welch | cda61c9 | 2010-03-01 17:06:20 +0000 | [diff] [blame] | 168 | .identity = "GE watchdog", |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | switch (cmd) { |
| 172 | case WDIOC_GETSUPPORT: |
| 173 | if (copy_to_user(argp, &info, sizeof(info))) |
| 174 | return -EFAULT; |
| 175 | break; |
| 176 | |
| 177 | case WDIOC_GETSTATUS: |
| 178 | case WDIOC_GETBOOTSTATUS: |
| 179 | if (put_user(wdt_status, (int __user *)argp)) |
| 180 | return -EFAULT; |
| 181 | wdt_status &= ~WDIOF_KEEPALIVEPING; |
| 182 | break; |
| 183 | |
| 184 | case WDIOC_SETOPTIONS: |
| 185 | if (get_user(options, (int __user *)argp)) |
| 186 | return -EFAULT; |
| 187 | |
| 188 | if (options & WDIOS_DISABLECARD) |
| 189 | gef_wdt_handler_disable(); |
| 190 | |
| 191 | if (options & WDIOS_ENABLECARD) |
| 192 | gef_wdt_handler_enable(); |
| 193 | break; |
| 194 | |
| 195 | case WDIOC_KEEPALIVE: |
| 196 | gef_wdt_service(); |
| 197 | wdt_status |= WDIOF_KEEPALIVEPING; |
| 198 | break; |
| 199 | |
| 200 | case WDIOC_SETTIMEOUT: |
| 201 | if (get_user(timeout, (int __user *)argp)) |
| 202 | return -EFAULT; |
| 203 | gef_wdt_set_timeout(timeout); |
Gustavo A. R. Silva | bd490f8 | 2020-07-07 12:11:21 -0500 | [diff] [blame] | 204 | fallthrough; |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 205 | |
| 206 | case WDIOC_GETTIMEOUT: |
| 207 | if (put_user(gef_wdt_timeout, (int __user *)argp)) |
| 208 | return -EFAULT; |
| 209 | break; |
| 210 | |
| 211 | default: |
| 212 | return -ENOTTY; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int gef_wdt_open(struct inode *inode, struct file *file) |
| 219 | { |
| 220 | if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags)) |
| 221 | return -EBUSY; |
| 222 | |
| 223 | if (nowayout) |
| 224 | __module_get(THIS_MODULE); |
| 225 | |
| 226 | gef_wdt_handler_enable(); |
| 227 | |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 228 | return stream_open(inode, file); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int gef_wdt_release(struct inode *inode, struct file *file) |
| 232 | { |
| 233 | if (expect_close == 42) |
| 234 | gef_wdt_handler_disable(); |
| 235 | else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 236 | pr_crit("unexpected close, not stopping timer!\n"); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 237 | gef_wdt_service(); |
| 238 | } |
| 239 | expect_close = 0; |
| 240 | |
| 241 | clear_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static const struct file_operations gef_wdt_fops = { |
| 247 | .owner = THIS_MODULE, |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 248 | .write = gef_wdt_write, |
| 249 | .unlocked_ioctl = gef_wdt_ioctl, |
Arnd Bergmann | b6dfb24 | 2019-06-03 14:23:09 +0200 | [diff] [blame] | 250 | .compat_ioctl = compat_ptr_ioctl, |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 251 | .open = gef_wdt_open, |
| 252 | .release = gef_wdt_release, |
| 253 | }; |
| 254 | |
| 255 | static struct miscdevice gef_wdt_miscdev = { |
| 256 | .minor = WATCHDOG_MINOR, |
| 257 | .name = "watchdog", |
| 258 | .fops = &gef_wdt_fops, |
| 259 | }; |
| 260 | |
| 261 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 262 | static int gef_wdt_probe(struct platform_device *dev) |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 263 | { |
| 264 | int timeout = 10; |
| 265 | u32 freq; |
| 266 | |
| 267 | bus_clk = 133; /* in MHz */ |
| 268 | |
| 269 | freq = fsl_get_sys_freq(); |
Roel Kluin | 2695266 | 2009-03-03 15:10:05 +0100 | [diff] [blame] | 270 | if (freq != -1) |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 271 | bus_clk = freq; |
| 272 | |
| 273 | /* Map devices registers into memory */ |
Anatolij Gustschin | b74dbf2 | 2010-06-03 03:30:31 +0200 | [diff] [blame] | 274 | gef_wdt_regs = of_iomap(dev->dev.of_node, 0); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 275 | if (gef_wdt_regs == NULL) |
| 276 | return -ENOMEM; |
| 277 | |
| 278 | gef_wdt_set_timeout(timeout); |
| 279 | |
| 280 | gef_wdt_handler_disable(); /* in case timer was already running */ |
| 281 | |
| 282 | return misc_register(&gef_wdt_miscdev); |
| 283 | } |
| 284 | |
Uwe Kleine-König | b5f2e36 | 2023-03-03 22:36:54 +0100 | [diff] [blame] | 285 | static void gef_wdt_remove(struct platform_device *dev) |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 286 | { |
| 287 | misc_deregister(&gef_wdt_miscdev); |
| 288 | |
| 289 | gef_wdt_handler_disable(); |
| 290 | |
| 291 | iounmap(gef_wdt_regs); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static const struct of_device_id gef_wdt_ids[] = { |
| 295 | { |
| 296 | .compatible = "gef,fpga-wdt", |
| 297 | }, |
| 298 | {}, |
| 299 | }; |
Luis de Bethencourt | c73318f | 2015-09-03 13:06:09 +0200 | [diff] [blame] | 300 | MODULE_DEVICE_TABLE(of, gef_wdt_ids); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 301 | |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 302 | static struct platform_driver gef_wdt_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 303 | .driver = { |
| 304 | .name = "gef_wdt", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 305 | .of_match_table = gef_wdt_ids, |
| 306 | }, |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 307 | .probe = gef_wdt_probe, |
Uwe Kleine-König | b5f2e36 | 2023-03-03 22:36:54 +0100 | [diff] [blame] | 308 | .remove_new = gef_wdt_remove, |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | static int __init gef_wdt_init(void) |
| 312 | { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 313 | pr_info("GE watchdog driver\n"); |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 314 | return platform_driver_register(&gef_wdt_driver); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void __exit gef_wdt_exit(void) |
| 318 | { |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 319 | platform_driver_unregister(&gef_wdt_driver); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | module_init(gef_wdt_init); |
| 323 | module_exit(gef_wdt_exit); |
| 324 | |
Martyn Welch | cda61c9 | 2010-03-01 17:06:20 +0000 | [diff] [blame] | 325 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>"); |
| 326 | MODULE_DESCRIPTION("GE watchdog driver"); |
Martyn Welch | 3268b56 | 2008-11-10 12:31:26 +0000 | [diff] [blame] | 327 | MODULE_LICENSE("GPL"); |
Axel Lin | ae2a006 | 2011-06-27 22:37:16 +0800 | [diff] [blame] | 328 | MODULE_ALIAS("platform:gef_wdt"); |