Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/watchdog/ar7_wdt.c |
| 3 | * |
| 4 | * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org> |
| 5 | * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org> |
| 6 | * |
| 7 | * Some code taken from: |
| 8 | * National Semiconductor SCx200 Watchdog support |
| 9 | * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/miscdevice.h> |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 32 | #include <linux/watchdog.h> |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 33 | #include <linux/fs.h> |
| 34 | #include <linux/ioport.h> |
| 35 | #include <linux/io.h> |
| 36 | #include <linux/uaccess.h> |
| 37 | |
| 38 | #include <asm/addrspace.h> |
Florian Fainelli | c5e7f5a | 2009-06-03 13:05:48 +0200 | [diff] [blame] | 39 | #include <asm/mach-ar7/ar7.h> |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 40 | |
| 41 | #define DRVNAME "ar7_wdt" |
| 42 | #define LONGNAME "TI AR7 Watchdog Timer" |
| 43 | |
| 44 | MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>"); |
| 45 | MODULE_DESCRIPTION(LONGNAME); |
| 46 | MODULE_LICENSE("GPL"); |
| 47 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 48 | |
| 49 | static int margin = 60; |
| 50 | module_param(margin, int, 0); |
| 51 | MODULE_PARM_DESC(margin, "Watchdog margin in seconds"); |
| 52 | |
| 53 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 54 | module_param(nowayout, int, 0); |
| 55 | MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close"); |
| 56 | |
| 57 | #define READ_REG(x) readl((void __iomem *)&(x)) |
| 58 | #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x)) |
| 59 | |
| 60 | struct ar7_wdt { |
| 61 | u32 kick_lock; |
| 62 | u32 kick; |
| 63 | u32 change_lock; |
| 64 | u32 change; |
| 65 | u32 disable_lock; |
| 66 | u32 disable; |
| 67 | u32 prescale_lock; |
| 68 | u32 prescale; |
| 69 | }; |
| 70 | |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 71 | static unsigned long wdt_is_open; |
| 72 | static spinlock_t wdt_lock; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 73 | static unsigned expect_close; |
| 74 | |
| 75 | /* XXX currently fixed, allows max margin ~68.72 secs */ |
| 76 | #define prescale_value 0xffff |
| 77 | |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 78 | /* Resource of the WDT registers */ |
| 79 | static struct resource *ar7_regs_wdt; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 80 | /* Pointer to the remapped WDT IO space */ |
| 81 | static struct ar7_wdt *ar7_wdt; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 82 | |
| 83 | static void ar7_wdt_kick(u32 value) |
| 84 | { |
| 85 | WRITE_REG(ar7_wdt->kick_lock, 0x5555); |
| 86 | if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) { |
| 87 | WRITE_REG(ar7_wdt->kick_lock, 0xaaaa); |
| 88 | if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) { |
| 89 | WRITE_REG(ar7_wdt->kick, value); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n"); |
| 94 | } |
| 95 | |
| 96 | static void ar7_wdt_prescale(u32 value) |
| 97 | { |
| 98 | WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a); |
| 99 | if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) { |
| 100 | WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5); |
| 101 | if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) { |
| 102 | WRITE_REG(ar7_wdt->prescale, value); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n"); |
| 107 | } |
| 108 | |
| 109 | static void ar7_wdt_change(u32 value) |
| 110 | { |
| 111 | WRITE_REG(ar7_wdt->change_lock, 0x6666); |
| 112 | if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) { |
| 113 | WRITE_REG(ar7_wdt->change_lock, 0xbbbb); |
| 114 | if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) { |
| 115 | WRITE_REG(ar7_wdt->change, value); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n"); |
| 120 | } |
| 121 | |
| 122 | static void ar7_wdt_disable(u32 value) |
| 123 | { |
| 124 | WRITE_REG(ar7_wdt->disable_lock, 0x7777); |
| 125 | if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) { |
| 126 | WRITE_REG(ar7_wdt->disable_lock, 0xcccc); |
| 127 | if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) { |
| 128 | WRITE_REG(ar7_wdt->disable_lock, 0xdddd); |
| 129 | if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) { |
| 130 | WRITE_REG(ar7_wdt->disable, value); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n"); |
| 136 | } |
| 137 | |
| 138 | static void ar7_wdt_update_margin(int new_margin) |
| 139 | { |
| 140 | u32 change; |
| 141 | |
| 142 | change = new_margin * (ar7_vbus_freq() / prescale_value); |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 143 | if (change < 1) |
| 144 | change = 1; |
| 145 | if (change > 0xffff) |
| 146 | change = 0xffff; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 147 | ar7_wdt_change(change); |
| 148 | margin = change * prescale_value / ar7_vbus_freq(); |
| 149 | printk(KERN_INFO DRVNAME |
| 150 | ": timer margin %d seconds (prescale %d, change %d, freq %d)\n", |
| 151 | margin, prescale_value, change, ar7_vbus_freq()); |
| 152 | } |
| 153 | |
| 154 | static void ar7_wdt_enable_wdt(void) |
| 155 | { |
| 156 | printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n"); |
| 157 | ar7_wdt_disable(1); |
| 158 | ar7_wdt_kick(1); |
| 159 | } |
| 160 | |
| 161 | static void ar7_wdt_disable_wdt(void) |
| 162 | { |
| 163 | printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n"); |
| 164 | ar7_wdt_disable(0); |
| 165 | } |
| 166 | |
| 167 | static int ar7_wdt_open(struct inode *inode, struct file *file) |
| 168 | { |
| 169 | /* only allow one at a time */ |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 170 | if (test_and_set_bit(0, &wdt_is_open)) |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 171 | return -EBUSY; |
| 172 | ar7_wdt_enable_wdt(); |
| 173 | expect_close = 0; |
| 174 | |
| 175 | return nonseekable_open(inode, file); |
| 176 | } |
| 177 | |
| 178 | static int ar7_wdt_release(struct inode *inode, struct file *file) |
| 179 | { |
| 180 | if (!expect_close) |
| 181 | printk(KERN_WARNING DRVNAME |
| 182 | ": watchdog device closed unexpectedly," |
| 183 | "will not disable the watchdog timer\n"); |
| 184 | else if (!nowayout) |
| 185 | ar7_wdt_disable_wdt(); |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 186 | clear_bit(0, &wdt_is_open); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 190 | static ssize_t ar7_wdt_write(struct file *file, const char *data, |
| 191 | size_t len, loff_t *ppos) |
| 192 | { |
| 193 | /* check for a magic close character */ |
| 194 | if (len) { |
| 195 | size_t i; |
| 196 | |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 197 | spin_lock(&wdt_lock); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 198 | ar7_wdt_kick(1); |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 199 | spin_unlock(&wdt_lock); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 200 | |
| 201 | expect_close = 0; |
| 202 | for (i = 0; i < len; ++i) { |
| 203 | char c; |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 204 | if (get_user(c, data + i)) |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 205 | return -EFAULT; |
| 206 | if (c == 'V') |
| 207 | expect_close = 1; |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | return len; |
| 212 | } |
| 213 | |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 214 | static long ar7_wdt_ioctl(struct file *file, |
| 215 | unsigned int cmd, unsigned long arg) |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 216 | { |
| 217 | static struct watchdog_info ident = { |
| 218 | .identity = LONGNAME, |
| 219 | .firmware_version = 1, |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 220 | .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 221 | WDIOF_MAGICCLOSE), |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 222 | }; |
| 223 | int new_margin; |
| 224 | |
| 225 | switch (cmd) { |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 226 | case WDIOC_GETSUPPORT: |
| 227 | if (copy_to_user((struct watchdog_info *)arg, &ident, |
| 228 | sizeof(ident))) |
| 229 | return -EFAULT; |
| 230 | return 0; |
| 231 | case WDIOC_GETSTATUS: |
| 232 | case WDIOC_GETBOOTSTATUS: |
| 233 | if (put_user(0, (int *)arg)) |
| 234 | return -EFAULT; |
| 235 | return 0; |
| 236 | case WDIOC_KEEPALIVE: |
| 237 | ar7_wdt_kick(1); |
| 238 | return 0; |
| 239 | case WDIOC_SETTIMEOUT: |
| 240 | if (get_user(new_margin, (int *)arg)) |
| 241 | return -EFAULT; |
| 242 | if (new_margin < 1) |
| 243 | return -EINVAL; |
| 244 | |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 245 | spin_lock(&wdt_lock); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 246 | ar7_wdt_update_margin(new_margin); |
| 247 | ar7_wdt_kick(1); |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 248 | spin_unlock(&wdt_lock); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 249 | |
| 250 | case WDIOC_GETTIMEOUT: |
| 251 | if (put_user(margin, (int *)arg)) |
| 252 | return -EFAULT; |
| 253 | return 0; |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 254 | default: |
| 255 | return -ENOTTY; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Jan Engelhardt | b47a166 | 2008-01-22 20:48:10 +0100 | [diff] [blame] | 259 | static const struct file_operations ar7_wdt_fops = { |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 260 | .owner = THIS_MODULE, |
| 261 | .write = ar7_wdt_write, |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 262 | .unlocked_ioctl = ar7_wdt_ioctl, |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 263 | .open = ar7_wdt_open, |
| 264 | .release = ar7_wdt_release, |
| 265 | }; |
| 266 | |
| 267 | static struct miscdevice ar7_wdt_miscdev = { |
| 268 | .minor = WATCHDOG_MINOR, |
| 269 | .name = "watchdog", |
| 270 | .fops = &ar7_wdt_fops, |
| 271 | }; |
| 272 | |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 273 | static int __devinit ar7_wdt_probe(struct platform_device *pdev) |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 274 | { |
| 275 | int rc; |
| 276 | |
Alan Cox | 670d59c | 2008-08-04 17:53:22 +0100 | [diff] [blame] | 277 | spin_lock_init(&wdt_lock); |
| 278 | |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 279 | ar7_regs_wdt = |
| 280 | platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs"); |
| 281 | if (!ar7_regs_wdt) { |
| 282 | printk(KERN_ERR DRVNAME ": could not get registers resource\n"); |
| 283 | rc = -ENODEV; |
| 284 | goto out; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 287 | if (!request_mem_region(ar7_regs_wdt->start, |
| 288 | resource_size(ar7_regs_wdt), LONGNAME)) { |
| 289 | printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n"); |
| 290 | rc = -EBUSY; |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt)); |
| 295 | if (!ar7_wdt) { |
| 296 | printk(KERN_ERR DRVNAME ": could not ioremap registers\n"); |
| 297 | rc = -ENXIO; |
Wim Van Sebroeck | d7e9791 | 2009-08-31 13:49:14 +0000 | [diff] [blame] | 298 | goto out_mem_region; |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 299 | } |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 300 | |
| 301 | ar7_wdt_disable_wdt(); |
| 302 | ar7_wdt_prescale(prescale_value); |
| 303 | ar7_wdt_update_margin(margin); |
| 304 | |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 305 | rc = misc_register(&ar7_wdt_miscdev); |
| 306 | if (rc) { |
| 307 | printk(KERN_ERR DRVNAME ": unable to register misc device\n"); |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 308 | goto out_alloc; |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 309 | } |
| 310 | goto out; |
| 311 | |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 312 | out_alloc: |
| 313 | iounmap(ar7_wdt); |
Wim Van Sebroeck | d7e9791 | 2009-08-31 13:49:14 +0000 | [diff] [blame] | 314 | out_mem_region: |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 315 | release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt)); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 316 | out: |
| 317 | return rc; |
| 318 | } |
| 319 | |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 320 | static int __devexit ar7_wdt_remove(struct platform_device *pdev) |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 321 | { |
| 322 | misc_deregister(&ar7_wdt_miscdev); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 323 | iounmap(ar7_wdt); |
Florian Fainelli | 64d4062 | 2009-07-21 12:11:32 +0200 | [diff] [blame] | 324 | release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt)); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void ar7_wdt_shutdown(struct platform_device *pdev) |
| 330 | { |
| 331 | if (!nowayout) |
| 332 | ar7_wdt_disable_wdt(); |
| 333 | } |
| 334 | |
| 335 | static struct platform_driver ar7_wdt_driver = { |
| 336 | .probe = ar7_wdt_probe, |
| 337 | .remove = __devexit_p(ar7_wdt_remove), |
| 338 | .shutdown = ar7_wdt_shutdown, |
| 339 | .driver = { |
| 340 | .owner = THIS_MODULE, |
| 341 | .name = "ar7_wdt", |
| 342 | }, |
| 343 | }; |
| 344 | |
| 345 | static int __init ar7_wdt_init(void) |
| 346 | { |
| 347 | return platform_driver_register(&ar7_wdt_driver); |
| 348 | } |
| 349 | |
| 350 | static void __exit ar7_wdt_cleanup(void) |
| 351 | { |
| 352 | platform_driver_unregister(&ar7_wdt_driver); |
Matteo Croce | c283cf2 | 2007-09-20 18:06:41 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | module_init(ar7_wdt_init); |
| 356 | module_exit(ar7_wdt_cleanup); |