| 1 | --- /dev/null |
| 2 | +++ b/drivers/watchdog/cns3xxx_wdt.c |
| 3 | @@ -0,0 +1,465 @@ |
| 4 | +/******************************************************************************* |
| 5 | + * |
| 6 | + * drivers/watchdog/cns3xxx_wdt.c |
| 7 | + * |
| 8 | + * Watchdog timer driver for the CNS3XXX SOCs |
| 9 | + * |
| 10 | + * Author: Scott Shu |
| 11 | + * |
| 12 | + * Copyright (c) 2008 Cavium Networks |
| 13 | + * |
| 14 | + * This file is free software; you can redistribute it and/or modify |
| 15 | + * it under the terms of the GNU General Public License, Version 2, as |
| 16 | + * published by the Free Software Foundation. |
| 17 | + * |
| 18 | + * This file is distributed in the hope that it will be useful, |
| 19 | + * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or |
| 21 | + * NONINFRINGEMENT. See the GNU General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public License |
| 24 | + * along with this file; if not, write to the Free Software |
| 25 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or |
| 26 | + * visit http://www.gnu.org/licenses/. |
| 27 | + * |
| 28 | + * This file may also be available under a different license from Cavium. |
| 29 | + * Contact Cavium Networks for more information |
| 30 | + * |
| 31 | + ******************************************************************************/ |
| 32 | + |
| 33 | +#include <linux/module.h> |
| 34 | +#include <linux/moduleparam.h> |
| 35 | +#include <linux/types.h> |
| 36 | +#include <linux/miscdevice.h> |
| 37 | +#include <linux/watchdog.h> |
| 38 | +#include <linux/fs.h> |
| 39 | +#include <linux/reboot.h> |
| 40 | +#include <linux/init.h> |
| 41 | +#include <linux/interrupt.h> |
| 42 | +#include <linux/platform_device.h> |
| 43 | +#include <linux/io.h> |
| 44 | +#include <linux/uaccess.h> |
| 45 | + |
| 46 | +#include <asm/hardware/arm_twd.h> |
| 47 | + |
| 48 | +struct cns3xxx_wdt { |
| 49 | + unsigned long timer_alive; |
| 50 | + struct device *dev; |
| 51 | + void __iomem *base; |
| 52 | + int irq; |
| 53 | + unsigned int perturb; |
| 54 | + char expect_close; |
| 55 | +}; |
| 56 | + |
| 57 | +static struct platform_device *cns3xxx_wdt_dev; |
| 58 | +extern unsigned int twd_timer_rate; |
| 59 | +static spinlock_t wdt_lock; |
| 60 | + |
| 61 | +#define TIMER_MARGIN 60 |
| 62 | +static int cns3xxx_margin = TIMER_MARGIN; |
| 63 | +module_param(cns3xxx_margin, int, 0); |
| 64 | +MODULE_PARM_DESC(cns3xxx_margin, |
| 65 | + "CNS3XXX timer margin in seconds. (0 < cns3xxx_margin < 65536, default=" |
| 66 | + __MODULE_STRING(TIMER_MARGIN) ")"); |
| 67 | + |
| 68 | +static int nowayout = WATCHDOG_NOWAYOUT; |
| 69 | +module_param(nowayout, int, 0); |
| 70 | +MODULE_PARM_DESC(nowayout, |
| 71 | + "Watchdog cannot be stopped once started (default=" |
| 72 | + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 73 | + |
| 74 | +#define ONLY_TESTING 0 |
| 75 | +static int cns3xxx_noboot = ONLY_TESTING; |
| 76 | +module_param(cns3xxx_noboot, int, 0); |
| 77 | +MODULE_PARM_DESC(cns3xxx_noboot, "CNS3XXX watchdog action, " |
| 78 | + "set to 1 to ignore reboots, 0 to reboot (default=" |
| 79 | + __MODULE_STRING(ONLY_TESTING) ")"); |
| 80 | + |
| 81 | +/* |
| 82 | + * This is the interrupt handler. Note that we only use this |
| 83 | + * in testing mode, so don't actually do a reboot here. |
| 84 | + */ |
| 85 | +static irqreturn_t cns3xxx_wdt_fire(int irq, void *arg) |
| 86 | +{ |
| 87 | + struct cns3xxx_wdt *wdt = arg; |
| 88 | + |
| 89 | + /* Check it really was our interrupt */ |
| 90 | + if (readl(wdt->base + TWD_WDOG_INTSTAT)) { |
| 91 | + dev_printk(KERN_CRIT, wdt->dev, |
| 92 | + "Triggered - Reboot ignored.\n"); |
| 93 | + /* Clear the interrupt on the watchdog */ |
| 94 | + writel(1, wdt->base + TWD_WDOG_INTSTAT); |
| 95 | + return IRQ_HANDLED; |
| 96 | + } |
| 97 | + return IRQ_NONE; |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + * cns3xxx_wdt_keepalive - reload the timer |
| 102 | + * |
| 103 | + * Note that the spec says a DIFFERENT value must be written to the reload |
| 104 | + * register each time. The "perturb" variable deals with this by adding 1 |
| 105 | + * to the count every other time the function is called. |
| 106 | + */ |
| 107 | +static void cns3xxx_wdt_keepalive(struct cns3xxx_wdt *wdt) |
| 108 | +{ |
| 109 | + unsigned int count; |
| 110 | + |
| 111 | + /* Assume prescale is set to 256 */ |
| 112 | + count = (twd_timer_rate / 256) * cns3xxx_margin; |
| 113 | + |
| 114 | + /* Reload the counter */ |
| 115 | + spin_lock(&wdt_lock); |
| 116 | + writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD); |
| 117 | + wdt->perturb = wdt->perturb ? 0 : 1; |
| 118 | + spin_unlock(&wdt_lock); |
| 119 | +} |
| 120 | + |
| 121 | +static void cns3xxx_wdt_stop(struct cns3xxx_wdt *wdt) |
| 122 | +{ |
| 123 | + spin_lock(&wdt_lock); |
| 124 | + writel(0x12345678, wdt->base + TWD_WDOG_DISABLE); |
| 125 | + writel(0x87654321, wdt->base + TWD_WDOG_DISABLE); |
| 126 | + writel(0x0, wdt->base + TWD_WDOG_CONTROL); |
| 127 | + spin_unlock(&wdt_lock); |
| 128 | +} |
| 129 | + |
| 130 | +static void cns3xxx_wdt_start(struct cns3xxx_wdt *wdt) |
| 131 | +{ |
| 132 | + dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n"); |
| 133 | + |
| 134 | + //spin_lock(&wdt_lock); |
| 135 | + /* This loads the count register but does NOT start the count yet */ |
| 136 | + cns3xxx_wdt_keepalive(wdt); |
| 137 | + spin_lock(&wdt_lock); |
| 138 | + |
| 139 | + if (cns3xxx_noboot) { |
| 140 | + /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */ |
| 141 | + writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL); |
| 142 | + } else { |
| 143 | + /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */ |
| 144 | + writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL); |
| 145 | + } |
| 146 | + spin_unlock(&wdt_lock); |
| 147 | +} |
| 148 | + |
| 149 | +static int cns3xxx_wdt_set_heartbeat(int t) |
| 150 | +{ |
| 151 | + if (t < 0x0001 || t > 0xFFFF) |
| 152 | + return -EINVAL; |
| 153 | + |
| 154 | + cns3xxx_margin = t; |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +/* |
| 159 | + * /dev/watchdog handling |
| 160 | + */ |
| 161 | +static int cns3xxx_wdt_open(struct inode *inode, struct file *file) |
| 162 | +{ |
| 163 | + struct cns3xxx_wdt *wdt = platform_get_drvdata(cns3xxx_wdt_dev); |
| 164 | + |
| 165 | + if (test_and_set_bit(0, &wdt->timer_alive)) |
| 166 | + return -EBUSY; |
| 167 | + |
| 168 | + if (nowayout) |
| 169 | + __module_get(THIS_MODULE); |
| 170 | + |
| 171 | + file->private_data = wdt; |
| 172 | + |
| 173 | + /* |
| 174 | + * Activate timer |
| 175 | + */ |
| 176 | + cns3xxx_wdt_start(wdt); |
| 177 | + |
| 178 | + return nonseekable_open(inode, file); |
| 179 | +} |
| 180 | + |
| 181 | +static int cns3xxx_wdt_release(struct inode *inode, struct file *file) |
| 182 | +{ |
| 183 | + struct cns3xxx_wdt *wdt = file->private_data; |
| 184 | + |
| 185 | + /* |
| 186 | + * Shut off the timer. |
| 187 | + * Lock it in if it's a module and we set nowayout |
| 188 | + */ |
| 189 | + if (wdt->expect_close == 42) |
| 190 | + cns3xxx_wdt_stop(wdt); |
| 191 | + else { |
| 192 | + dev_printk(KERN_CRIT, wdt->dev, |
| 193 | + "unexpected close, not stopping watchdog!\n"); |
| 194 | + cns3xxx_wdt_keepalive(wdt); |
| 195 | + } |
| 196 | + clear_bit(0, &wdt->timer_alive); |
| 197 | + wdt->expect_close = 0; |
| 198 | + return 0; |
| 199 | +} |
| 200 | + |
| 201 | +static ssize_t cns3xxx_wdt_write(struct file *file, const char *data, |
| 202 | + size_t len, loff_t *ppos) |
| 203 | +{ |
| 204 | + struct cns3xxx_wdt *wdt = file->private_data; |
| 205 | + |
| 206 | + /* |
| 207 | + * Refresh the timer. |
| 208 | + */ |
| 209 | + if (len) { |
| 210 | + if (!nowayout) { |
| 211 | + size_t i; |
| 212 | + |
| 213 | + /* In case it was set long ago */ |
| 214 | + wdt->expect_close = 0; |
| 215 | + |
| 216 | + for (i = 0; i != len; i++) { |
| 217 | + char c; |
| 218 | + |
| 219 | + if (get_user(c, data + i)) |
| 220 | + return -EFAULT; |
| 221 | + if (c == 'V') |
| 222 | + wdt->expect_close = 42; |
| 223 | + } |
| 224 | + } |
| 225 | + cns3xxx_wdt_keepalive(wdt); |
| 226 | + } |
| 227 | + return len; |
| 228 | +} |
| 229 | + |
| 230 | +static struct watchdog_info ident = { |
| 231 | + .options = WDIOF_SETTIMEOUT | |
| 232 | + WDIOF_KEEPALIVEPING | |
| 233 | + WDIOF_MAGICCLOSE, |
| 234 | + .identity = "CNS3XXX Watchdog", |
| 235 | +}; |
| 236 | + |
| 237 | +static long cns3xxx_wdt_ioctl(struct file *file, unsigned int cmd, |
| 238 | + unsigned long arg) |
| 239 | +{ |
| 240 | + struct cns3xxx_wdt *wdt = file->private_data; |
| 241 | + int ret; |
| 242 | + union { |
| 243 | + struct watchdog_info ident; |
| 244 | + int i; |
| 245 | + } uarg; |
| 246 | + |
| 247 | + if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg)) |
| 248 | + return -ENOTTY; |
| 249 | + |
| 250 | + if (_IOC_DIR(cmd) & _IOC_WRITE) { |
| 251 | + ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd)); |
| 252 | + if (ret) |
| 253 | + return -EFAULT; |
| 254 | + } |
| 255 | + |
| 256 | + switch (cmd) { |
| 257 | + case WDIOC_GETSUPPORT: |
| 258 | + uarg.ident = ident; |
| 259 | + ret = 0; |
| 260 | + break; |
| 261 | + |
| 262 | + case WDIOC_GETSTATUS: |
| 263 | + case WDIOC_GETBOOTSTATUS: |
| 264 | + uarg.i = 0; |
| 265 | + ret = 0; |
| 266 | + break; |
| 267 | + |
| 268 | + case WDIOC_SETOPTIONS: |
| 269 | + ret = -EINVAL; |
| 270 | + if (uarg.i & WDIOS_DISABLECARD) { |
| 271 | + cns3xxx_wdt_stop(wdt); |
| 272 | + ret = 0; |
| 273 | + } |
| 274 | + if (uarg.i & WDIOS_ENABLECARD) { |
| 275 | + cns3xxx_wdt_start(wdt); |
| 276 | + ret = 0; |
| 277 | + } |
| 278 | + break; |
| 279 | + |
| 280 | + case WDIOC_KEEPALIVE: |
| 281 | + cns3xxx_wdt_keepalive(wdt); |
| 282 | + ret = 0; |
| 283 | + break; |
| 284 | + |
| 285 | + case WDIOC_SETTIMEOUT: |
| 286 | + ret = cns3xxx_wdt_set_heartbeat(uarg.i); |
| 287 | + if (ret) |
| 288 | + break; |
| 289 | + |
| 290 | + cns3xxx_wdt_keepalive(wdt); |
| 291 | + /* Fall */ |
| 292 | + case WDIOC_GETTIMEOUT: |
| 293 | + uarg.i = cns3xxx_margin; |
| 294 | + ret = 0; |
| 295 | + break; |
| 296 | + |
| 297 | + default: |
| 298 | + return -ENOTTY; |
| 299 | + } |
| 300 | + |
| 301 | + if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) { |
| 302 | + ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd)); |
| 303 | + if (ret) |
| 304 | + ret = -EFAULT; |
| 305 | + } |
| 306 | + return ret; |
| 307 | +} |
| 308 | + |
| 309 | +/* |
| 310 | + * System shutdown handler. Turn off the watchdog if we're |
| 311 | + * restarting or halting the system. |
| 312 | + */ |
| 313 | +static void cns3xxx_wdt_shutdown(struct platform_device *dev) |
| 314 | +{ |
| 315 | + struct cns3xxx_wdt *wdt = platform_get_drvdata(dev); |
| 316 | + |
| 317 | + if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT) |
| 318 | + cns3xxx_wdt_stop(wdt); |
| 319 | +} |
| 320 | + |
| 321 | +/* |
| 322 | + * Kernel Interfaces |
| 323 | + */ |
| 324 | +static const struct file_operations cns3xxx_wdt_fops = { |
| 325 | + .owner = THIS_MODULE, |
| 326 | + .llseek = no_llseek, |
| 327 | + .write = cns3xxx_wdt_write, |
| 328 | + .unlocked_ioctl = cns3xxx_wdt_ioctl, |
| 329 | + .open = cns3xxx_wdt_open, |
| 330 | + .release = cns3xxx_wdt_release, |
| 331 | +}; |
| 332 | + |
| 333 | +static struct miscdevice cns3xxx_wdt_miscdev = { |
| 334 | + .minor = WATCHDOG_MINOR, |
| 335 | + .name = "watchdog", |
| 336 | + .fops = &cns3xxx_wdt_fops, |
| 337 | +}; |
| 338 | + |
| 339 | +static int __devinit cns3xxx_wdt_probe(struct platform_device *dev) |
| 340 | +{ |
| 341 | + struct cns3xxx_wdt *wdt; |
| 342 | + struct resource *res; |
| 343 | + int ret; |
| 344 | + |
| 345 | + /* We only accept one device, and it must have an id of -1 */ |
| 346 | + if (dev->id != -1) |
| 347 | + return -ENODEV; |
| 348 | + |
| 349 | + res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 350 | + if (!res) { |
| 351 | + ret = -ENODEV; |
| 352 | + goto err_out; |
| 353 | + } |
| 354 | + |
| 355 | + wdt = kzalloc(sizeof(struct cns3xxx_wdt), GFP_KERNEL); |
| 356 | + if (!wdt) { |
| 357 | + ret = -ENOMEM; |
| 358 | + goto err_out; |
| 359 | + } |
| 360 | + |
| 361 | + wdt->dev = &dev->dev; |
| 362 | + wdt->irq = platform_get_irq(dev, 0); |
| 363 | + if (wdt->irq < 0) { |
| 364 | + ret = -ENXIO; |
| 365 | + goto err_free; |
| 366 | + } |
| 367 | + wdt->base = ioremap(res->start, res->end - res->start + 1); |
| 368 | + if (!wdt->base) { |
| 369 | + ret = -ENOMEM; |
| 370 | + goto err_free; |
| 371 | + } |
| 372 | + |
| 373 | + cns3xxx_wdt_miscdev.parent = &dev->dev; |
| 374 | + ret = misc_register(&cns3xxx_wdt_miscdev); |
| 375 | + if (ret) { |
| 376 | + dev_printk(KERN_ERR, wdt->dev, |
| 377 | + "cannot register miscdev on minor=%d (err=%d)\n", |
| 378 | + WATCHDOG_MINOR, ret); |
| 379 | + goto err_misc; |
| 380 | + } |
| 381 | + |
| 382 | + ret = request_irq(wdt->irq, cns3xxx_wdt_fire, IRQF_DISABLED, |
| 383 | + dev->name, wdt); |
| 384 | + if (ret) { |
| 385 | + dev_printk(KERN_ERR, wdt->dev, |
| 386 | + "cannot register IRQ%d for watchdog\n", wdt->irq); |
| 387 | + goto err_irq; |
| 388 | + } |
| 389 | + |
| 390 | + cns3xxx_wdt_stop(wdt); |
| 391 | + platform_set_drvdata(dev, wdt); |
| 392 | + cns3xxx_wdt_dev = dev; |
| 393 | + |
| 394 | + return 0; |
| 395 | + |
| 396 | +err_irq: |
| 397 | + misc_deregister(&cns3xxx_wdt_miscdev); |
| 398 | +err_misc: |
| 399 | + platform_set_drvdata(dev, NULL); |
| 400 | + iounmap(wdt->base); |
| 401 | +err_free: |
| 402 | + kfree(wdt); |
| 403 | +err_out: |
| 404 | + return ret; |
| 405 | +} |
| 406 | + |
| 407 | +static int __devexit cns3xxx_wdt_remove(struct platform_device *dev) |
| 408 | +{ |
| 409 | + struct cns3xxx_wdt *wdt = platform_get_drvdata(dev); |
| 410 | + |
| 411 | + platform_set_drvdata(dev, NULL); |
| 412 | + |
| 413 | + misc_deregister(&cns3xxx_wdt_miscdev); |
| 414 | + |
| 415 | + cns3xxx_wdt_dev = NULL; |
| 416 | + |
| 417 | + free_irq(wdt->irq, wdt); |
| 418 | + iounmap(wdt->base); |
| 419 | + kfree(wdt); |
| 420 | + return 0; |
| 421 | +} |
| 422 | + |
| 423 | + |
| 424 | +static struct platform_driver cns3xxx_wdt_driver = { |
| 425 | + .probe = cns3xxx_wdt_probe, |
| 426 | + .remove = __devexit_p(cns3xxx_wdt_remove), |
| 427 | + .shutdown = cns3xxx_wdt_shutdown, |
| 428 | + .driver = { |
| 429 | + .owner = THIS_MODULE, |
| 430 | + .name = "cns3xxx-wdt", |
| 431 | + }, |
| 432 | +}; |
| 433 | + |
| 434 | +static char banner[] __initdata = KERN_INFO |
| 435 | + "CNS3XXX Watchdog Timer, noboot=%d margin=%d sec (nowayout= %d)\n"; |
| 436 | + |
| 437 | +static int __init cns3xxx_wdt_init(void) |
| 438 | +{ |
| 439 | + /* |
| 440 | + * Check that the margin value is within it's range; |
| 441 | + * if not reset to the default |
| 442 | + */ |
| 443 | + if (cns3xxx_wdt_set_heartbeat(cns3xxx_margin)) { |
| 444 | + cns3xxx_wdt_set_heartbeat(TIMER_MARGIN); |
| 445 | + printk(KERN_INFO "cns3xxx_margin value must be 0 < cns3xxx_margin < 65536, using %d\n", |
| 446 | + TIMER_MARGIN); |
| 447 | + } |
| 448 | + |
| 449 | + printk(banner, cns3xxx_noboot, cns3xxx_margin, nowayout); |
| 450 | + |
| 451 | + spin_lock_init(&wdt_lock); |
| 452 | + |
| 453 | + return platform_driver_register(&cns3xxx_wdt_driver); |
| 454 | +} |
| 455 | + |
| 456 | +static void __exit cns3xxx_wdt_exit(void) |
| 457 | +{ |
| 458 | + platform_driver_unregister(&cns3xxx_wdt_driver); |
| 459 | +} |
| 460 | + |
| 461 | +module_init(cns3xxx_wdt_init); |
| 462 | +module_exit(cns3xxx_wdt_exit); |
| 463 | + |
| 464 | +MODULE_AUTHOR("Scott Shu"); |
| 465 | +MODULE_DESCRIPTION("CNS3XXX Watchdog Device Driver"); |
| 466 | +MODULE_LICENSE("GPL"); |
| 467 | +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 468 | +MODULE_ALIAS("platform:cns3xxx-wdt"); |
| 469 | --- a/drivers/watchdog/Kconfig |
| 470 | +++ b/drivers/watchdog/Kconfig |
| 471 | @@ -231,6 +231,15 @@ config DAVINCI_WATCHDOG |
| 472 | NOTE: once enabled, this timer cannot be disabled. |
| 473 | Say N if you are unsure. |
| 474 | |
| 475 | +config CNS3XXX_WATCHDOG |
| 476 | + tristate "CNS3XXX watchdog" |
| 477 | + depends on ARCH_CNS3XXX && LOCAL_TIMERS |
| 478 | + help |
| 479 | + Watchdog timer embedded into the CNS3XXX SoCs system. |
| 480 | + |
| 481 | + To compile this driver as a module, choose M here: the |
| 482 | + module will be called cns3xxx_wdt. |
| 483 | + |
| 484 | config ORION_WATCHDOG |
| 485 | tristate "Orion watchdog" |
| 486 | depends on ARCH_ORION5X || ARCH_KIRKWOOD |
| 487 | --- a/drivers/watchdog/Makefile |
| 488 | +++ b/drivers/watchdog/Makefile |
| 489 | @@ -41,6 +41,7 @@ obj-$(CONFIG_EP93XX_WATCHDOG) += ep93xx_ |
| 490 | obj-$(CONFIG_PNX4008_WATCHDOG) += pnx4008_wdt.o |
| 491 | obj-$(CONFIG_IOP_WATCHDOG) += iop_wdt.o |
| 492 | obj-$(CONFIG_DAVINCI_WATCHDOG) += davinci_wdt.o |
| 493 | +obj-$(CONFIG_CNS3XXX_WATCHDOG) += cns3xxx_wdt.o |
| 494 | obj-$(CONFIG_ORION_WATCHDOG) += orion_wdt.o |
| 495 | obj-$(CONFIG_COH901327_WATCHDOG) += coh901327_wdt.o |
| 496 | obj-$(CONFIG_STMP3XXX_WATCHDOG) += stmp3xxx_wdt.o |
| 497 | |