Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 2 | /* Watchdog timer for machines with the CS5535/CS5536 companion chip |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006-2007, Advanced Micro Devices, Inc. |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 5 | * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk> |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -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 |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/miscdevice.h> |
| 14 | #include <linux/watchdog.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/reboot.h> |
Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 19 | |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 20 | #include <linux/cs5535.h> |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 21 | |
| 22 | #define GEODEWDT_HZ 500 |
| 23 | #define GEODEWDT_SCALE 6 |
| 24 | #define GEODEWDT_MAX_SECONDS 131 |
| 25 | |
| 26 | #define WDT_FLAGS_OPEN 1 |
| 27 | #define WDT_FLAGS_ORPHAN 2 |
| 28 | |
| 29 | #define DRV_NAME "geodewdt" |
| 30 | #define WATCHDOG_NAME "Geode GX/LX WDT" |
| 31 | #define WATCHDOG_TIMEOUT 60 |
| 32 | |
| 33 | static int timeout = WATCHDOG_TIMEOUT; |
| 34 | module_param(timeout, int, 0); |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 35 | MODULE_PARM_DESC(timeout, |
| 36 | "Watchdog timeout in seconds. 1<= timeout <=131, default=" |
| 37 | __MODULE_STRING(WATCHDOG_TIMEOUT) "."); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 38 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 39 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 40 | module_param(nowayout, bool, 0); |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 41 | MODULE_PARM_DESC(nowayout, |
| 42 | "Watchdog cannot be stopped once started (default=" |
| 43 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 44 | |
| 45 | static struct platform_device *geodewdt_platform_device; |
| 46 | static unsigned long wdt_flags; |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 47 | static struct cs5535_mfgpt_timer *wdt_timer; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 48 | static int safe_close; |
| 49 | |
| 50 | static void geodewdt_ping(void) |
| 51 | { |
| 52 | /* Stop the counter */ |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 53 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 54 | |
| 55 | /* Reset the counter */ |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 56 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 57 | |
| 58 | /* Enable the counter */ |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 59 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void geodewdt_disable(void) |
| 63 | { |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 64 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); |
| 65 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int geodewdt_set_heartbeat(int val) |
| 69 | { |
| 70 | if (val < 1 || val > GEODEWDT_MAX_SECONDS) |
| 71 | return -EINVAL; |
| 72 | |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 73 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); |
| 74 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ); |
| 75 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); |
| 76 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 77 | |
| 78 | timeout = val; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 82 | static int geodewdt_open(struct inode *inode, struct file *file) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 83 | { |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 84 | if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags)) |
| 85 | return -EBUSY; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 86 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 87 | if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags)) |
| 88 | __module_get(THIS_MODULE); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 89 | |
| 90 | geodewdt_ping(); |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 91 | return stream_open(inode, file); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 94 | static int geodewdt_release(struct inode *inode, struct file *file) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 95 | { |
| 96 | if (safe_close) { |
| 97 | geodewdt_disable(); |
| 98 | module_put(THIS_MODULE); |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 99 | } else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 100 | pr_crit("Unexpected close - watchdog is not stopping\n"); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 101 | geodewdt_ping(); |
| 102 | |
| 103 | set_bit(WDT_FLAGS_ORPHAN, &wdt_flags); |
| 104 | } |
| 105 | |
| 106 | clear_bit(WDT_FLAGS_OPEN, &wdt_flags); |
| 107 | safe_close = 0; |
| 108 | return 0; |
| 109 | } |
| 110 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 111 | static ssize_t geodewdt_write(struct file *file, const char __user *data, |
| 112 | size_t len, loff_t *ppos) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 113 | { |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 114 | if (len) { |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 115 | if (!nowayout) { |
| 116 | size_t i; |
| 117 | safe_close = 0; |
| 118 | |
| 119 | for (i = 0; i != len; i++) { |
| 120 | char c; |
| 121 | |
| 122 | if (get_user(c, data + i)) |
| 123 | return -EFAULT; |
| 124 | |
| 125 | if (c == 'V') |
| 126 | safe_close = 1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | geodewdt_ping(); |
| 131 | } |
| 132 | return len; |
| 133 | } |
| 134 | |
Wim Van Sebroeck | 7275fc8 | 2008-09-18 12:26:15 +0000 | [diff] [blame] | 135 | static long geodewdt_ioctl(struct file *file, unsigned int cmd, |
| 136 | unsigned long arg) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 137 | { |
| 138 | void __user *argp = (void __user *)arg; |
| 139 | int __user *p = argp; |
| 140 | int interval; |
| 141 | |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 142 | static const struct watchdog_info ident = { |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 143 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
| 144 | | WDIOF_MAGICCLOSE, |
| 145 | .firmware_version = 1, |
| 146 | .identity = WATCHDOG_NAME, |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 147 | }; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 148 | |
Wim Van Sebroeck | 5eb8249 | 2008-07-17 18:08:47 +0000 | [diff] [blame] | 149 | switch (cmd) { |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 150 | case WDIOC_GETSUPPORT: |
| 151 | return copy_to_user(argp, &ident, |
| 152 | sizeof(ident)) ? -EFAULT : 0; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 153 | case WDIOC_GETSTATUS: |
| 154 | case WDIOC_GETBOOTSTATUS: |
| 155 | return put_user(0, p); |
| 156 | |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 157 | case WDIOC_SETOPTIONS: |
| 158 | { |
| 159 | int options, ret = -EINVAL; |
| 160 | |
| 161 | if (get_user(options, p)) |
| 162 | return -EFAULT; |
| 163 | |
| 164 | if (options & WDIOS_DISABLECARD) { |
| 165 | geodewdt_disable(); |
| 166 | ret = 0; |
| 167 | } |
| 168 | |
| 169 | if (options & WDIOS_ENABLECARD) { |
| 170 | geodewdt_ping(); |
| 171 | ret = 0; |
| 172 | } |
| 173 | |
| 174 | return ret; |
| 175 | } |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 176 | case WDIOC_KEEPALIVE: |
| 177 | geodewdt_ping(); |
| 178 | return 0; |
| 179 | |
| 180 | case WDIOC_SETTIMEOUT: |
| 181 | if (get_user(interval, p)) |
| 182 | return -EFAULT; |
| 183 | |
| 184 | if (geodewdt_set_heartbeat(interval)) |
| 185 | return -EINVAL; |
Gustavo A. R. Silva | bd490f8 | 2020-07-07 12:11:21 -0500 | [diff] [blame] | 186 | fallthrough; |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 187 | case WDIOC_GETTIMEOUT: |
| 188 | return put_user(timeout, p); |
| 189 | |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 190 | default: |
| 191 | return -ENOTTY; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static const struct file_operations geodewdt_fops = { |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 198 | .owner = THIS_MODULE, |
| 199 | .llseek = no_llseek, |
| 200 | .write = geodewdt_write, |
Wim Van Sebroeck | 7275fc8 | 2008-09-18 12:26:15 +0000 | [diff] [blame] | 201 | .unlocked_ioctl = geodewdt_ioctl, |
Arnd Bergmann | b6dfb24 | 2019-06-03 14:23:09 +0200 | [diff] [blame] | 202 | .compat_ioctl = compat_ptr_ioctl, |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 203 | .open = geodewdt_open, |
| 204 | .release = geodewdt_release, |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static struct miscdevice geodewdt_miscdev = { |
| 208 | .minor = WATCHDOG_MINOR, |
| 209 | .name = "watchdog", |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 210 | .fops = &geodewdt_fops, |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Jean Delvare | 78411be | 2014-03-14 13:16:47 +0100 | [diff] [blame] | 213 | static int __init geodewdt_probe(struct platform_device *dev) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 214 | { |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 215 | int ret; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 216 | |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 217 | wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING); |
| 218 | if (!wdt_timer) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 219 | pr_err("No timers were available\n"); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 220 | return -ENODEV; |
| 221 | } |
| 222 | |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 223 | /* Set up the timer */ |
| 224 | |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 225 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 226 | GEODEWDT_SCALE | (3 << 8)); |
| 227 | |
| 228 | /* Set up comparator 2 to reset when the event fires */ |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 229 | cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 230 | |
| 231 | /* Set up the initial timeout */ |
| 232 | |
Andres Salomon | 9b0fd11 | 2009-12-18 13:02:38 -0500 | [diff] [blame] | 233 | cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 234 | timeout * GEODEWDT_HZ); |
| 235 | |
| 236 | ret = misc_register(&geodewdt_miscdev); |
| 237 | |
| 238 | return ret; |
| 239 | } |
| 240 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 241 | static int geodewdt_remove(struct platform_device *dev) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 242 | { |
| 243 | misc_deregister(&geodewdt_miscdev); |
| 244 | return 0; |
| 245 | } |
| 246 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 247 | static void geodewdt_shutdown(struct platform_device *dev) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 248 | { |
| 249 | geodewdt_disable(); |
| 250 | } |
| 251 | |
| 252 | static struct platform_driver geodewdt_driver = { |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 253 | .remove = geodewdt_remove, |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 254 | .shutdown = geodewdt_shutdown, |
| 255 | .driver = { |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 256 | .name = DRV_NAME, |
| 257 | }, |
| 258 | }; |
| 259 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 260 | static int __init geodewdt_init(void) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 261 | { |
| 262 | int ret; |
| 263 | |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 264 | geodewdt_platform_device = platform_device_register_simple(DRV_NAME, |
| 265 | -1, NULL, 0); |
Jean Delvare | 78411be | 2014-03-14 13:16:47 +0100 | [diff] [blame] | 266 | if (IS_ERR(geodewdt_platform_device)) |
| 267 | return PTR_ERR(geodewdt_platform_device); |
| 268 | |
| 269 | ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe); |
| 270 | if (ret) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 271 | goto err; |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | err: |
Jean Delvare | 78411be | 2014-03-14 13:16:47 +0100 | [diff] [blame] | 275 | platform_device_unregister(geodewdt_platform_device); |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 276 | return ret; |
| 277 | } |
| 278 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 279 | static void __exit geodewdt_exit(void) |
Jordan Crouse | 0b36086 | 2008-01-21 10:07:00 -0700 | [diff] [blame] | 280 | { |
| 281 | platform_device_unregister(geodewdt_platform_device); |
| 282 | platform_driver_unregister(&geodewdt_driver); |
| 283 | } |
| 284 | |
| 285 | module_init(geodewdt_init); |
| 286 | module_exit(geodewdt_exit); |
| 287 | |
| 288 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); |
| 289 | MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver"); |
| 290 | MODULE_LICENSE("GPL"); |