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