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