Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom BCM63xx SoC watchdog driver |
| 3 | * |
| 4 | * Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com> |
| 5 | * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/miscdevice.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 21 | #include <linux/types.h> |
| 22 | #include <linux/uaccess.h> |
| 23 | #include <linux/watchdog.h> |
| 24 | #include <linux/timer.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/ptrace.h> |
| 28 | #include <linux/resource.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | |
| 31 | #include <bcm63xx_cpu.h> |
| 32 | #include <bcm63xx_io.h> |
| 33 | #include <bcm63xx_regs.h> |
| 34 | #include <bcm63xx_timer.h> |
| 35 | |
| 36 | #define PFX KBUILD_MODNAME |
| 37 | |
| 38 | #define WDT_HZ 50000000 /* Fclk */ |
| 39 | #define WDT_DEFAULT_TIME 30 /* seconds */ |
| 40 | #define WDT_MAX_TIME 256 /* seconds */ |
| 41 | |
| 42 | static struct { |
| 43 | void __iomem *regs; |
| 44 | struct timer_list timer; |
| 45 | int default_ticks; |
| 46 | unsigned long inuse; |
| 47 | atomic_t ticks; |
| 48 | } bcm63xx_wdt_device; |
| 49 | |
| 50 | static int expect_close; |
| 51 | |
| 52 | static int wdt_time = WDT_DEFAULT_TIME; |
| 53 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 54 | module_param(nowayout, int, 0); |
| 55 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 56 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 57 | |
| 58 | /* HW functions */ |
| 59 | static void bcm63xx_wdt_hw_start(void) |
| 60 | { |
| 61 | bcm_writel(0xfffffffe, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG); |
| 62 | bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG); |
| 63 | bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG); |
| 64 | } |
| 65 | |
| 66 | static void bcm63xx_wdt_hw_stop(void) |
| 67 | { |
| 68 | bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG); |
| 69 | bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG); |
| 70 | } |
| 71 | |
| 72 | static void bcm63xx_wdt_isr(void *data) |
| 73 | { |
| 74 | struct pt_regs *regs = get_irq_regs(); |
| 75 | |
| 76 | die(PFX " fire", regs); |
| 77 | } |
| 78 | |
| 79 | static void bcm63xx_timer_tick(unsigned long unused) |
| 80 | { |
| 81 | if (!atomic_dec_and_test(&bcm63xx_wdt_device.ticks)) { |
| 82 | bcm63xx_wdt_hw_start(); |
| 83 | mod_timer(&bcm63xx_wdt_device.timer, jiffies + HZ); |
| 84 | } else |
| 85 | printk(KERN_CRIT PFX ": watchdog will restart system\n"); |
| 86 | } |
| 87 | |
| 88 | static void bcm63xx_wdt_pet(void) |
| 89 | { |
| 90 | atomic_set(&bcm63xx_wdt_device.ticks, wdt_time); |
| 91 | } |
| 92 | |
| 93 | static void bcm63xx_wdt_start(void) |
| 94 | { |
| 95 | bcm63xx_wdt_pet(); |
| 96 | bcm63xx_timer_tick(0); |
| 97 | } |
| 98 | |
| 99 | static void bcm63xx_wdt_pause(void) |
| 100 | { |
| 101 | del_timer_sync(&bcm63xx_wdt_device.timer); |
| 102 | bcm63xx_wdt_hw_stop(); |
| 103 | } |
| 104 | |
| 105 | static int bcm63xx_wdt_settimeout(int new_time) |
| 106 | { |
| 107 | if ((new_time <= 0) || (new_time > WDT_MAX_TIME)) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | wdt_time = new_time; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int bcm63xx_wdt_open(struct inode *inode, struct file *file) |
| 116 | { |
| 117 | if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse)) |
| 118 | return -EBUSY; |
| 119 | |
| 120 | bcm63xx_wdt_start(); |
| 121 | return nonseekable_open(inode, file); |
| 122 | } |
| 123 | |
| 124 | static int bcm63xx_wdt_release(struct inode *inode, struct file *file) |
| 125 | { |
| 126 | if (expect_close == 42) |
| 127 | bcm63xx_wdt_pause(); |
| 128 | else { |
| 129 | printk(KERN_CRIT PFX |
| 130 | ": Unexpected close, not stopping watchdog!\n"); |
| 131 | bcm63xx_wdt_start(); |
| 132 | } |
| 133 | clear_bit(0, &bcm63xx_wdt_device.inuse); |
| 134 | expect_close = 0; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static ssize_t bcm63xx_wdt_write(struct file *file, const char *data, |
| 139 | size_t len, loff_t *ppos) |
| 140 | { |
| 141 | if (len) { |
| 142 | if (!nowayout) { |
| 143 | size_t i; |
| 144 | |
| 145 | /* In case it was set long ago */ |
| 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 | bcm63xx_wdt_pet(); |
| 157 | } |
| 158 | return len; |
| 159 | } |
| 160 | |
| 161 | static struct watchdog_info bcm63xx_wdt_info = { |
| 162 | .identity = PFX, |
| 163 | .options = WDIOF_SETTIMEOUT | |
| 164 | WDIOF_KEEPALIVEPING | |
| 165 | WDIOF_MAGICCLOSE, |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd, |
| 170 | unsigned long arg) |
| 171 | { |
| 172 | void __user *argp = (void __user *)arg; |
| 173 | int __user *p = argp; |
| 174 | int new_value, retval = -EINVAL; |
| 175 | |
| 176 | switch (cmd) { |
| 177 | case WDIOC_GETSUPPORT: |
| 178 | return copy_to_user(argp, &bcm63xx_wdt_info, |
| 179 | sizeof(bcm63xx_wdt_info)) ? -EFAULT : 0; |
| 180 | |
| 181 | case WDIOC_GETSTATUS: |
| 182 | case WDIOC_GETBOOTSTATUS: |
| 183 | return put_user(0, p); |
| 184 | |
| 185 | case WDIOC_SETOPTIONS: |
| 186 | if (get_user(new_value, p)) |
| 187 | return -EFAULT; |
| 188 | |
| 189 | if (new_value & WDIOS_DISABLECARD) { |
| 190 | bcm63xx_wdt_pause(); |
| 191 | retval = 0; |
| 192 | } |
| 193 | if (new_value & WDIOS_ENABLECARD) { |
| 194 | bcm63xx_wdt_start(); |
| 195 | retval = 0; |
| 196 | } |
| 197 | |
| 198 | return retval; |
| 199 | |
| 200 | case WDIOC_KEEPALIVE: |
| 201 | bcm63xx_wdt_pet(); |
| 202 | return 0; |
| 203 | |
| 204 | case WDIOC_SETTIMEOUT: |
| 205 | if (get_user(new_value, p)) |
| 206 | return -EFAULT; |
| 207 | |
| 208 | if (bcm63xx_wdt_settimeout(new_value)) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | bcm63xx_wdt_pet(); |
| 212 | |
| 213 | case WDIOC_GETTIMEOUT: |
| 214 | return put_user(wdt_time, p); |
| 215 | |
| 216 | default: |
| 217 | return -ENOTTY; |
| 218 | |
| 219 | } |
| 220 | } |
| 221 | |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 222 | static const struct file_operations bcm63xx_wdt_fops = { |
| 223 | .owner = THIS_MODULE, |
| 224 | .llseek = no_llseek, |
| 225 | .write = bcm63xx_wdt_write, |
| 226 | .unlocked_ioctl = bcm63xx_wdt_ioctl, |
| 227 | .open = bcm63xx_wdt_open, |
| 228 | .release = bcm63xx_wdt_release, |
| 229 | }; |
| 230 | |
| 231 | static struct miscdevice bcm63xx_wdt_miscdev = { |
| 232 | .minor = WATCHDOG_MINOR, |
| 233 | .name = "watchdog", |
| 234 | .fops = &bcm63xx_wdt_fops, |
| 235 | }; |
| 236 | |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 237 | |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 238 | static int __devinit bcm63xx_wdt_probe(struct platform_device *pdev) |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 239 | { |
| 240 | int ret; |
| 241 | struct resource *r; |
| 242 | |
| 243 | setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_timer_tick, 0L); |
| 244 | |
| 245 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 246 | if (!r) { |
| 247 | dev_err(&pdev->dev, "failed to get resources\n"); |
| 248 | return -ENODEV; |
| 249 | } |
| 250 | |
Joe Perches | 06794ea | 2011-03-23 12:55:36 -0700 | [diff] [blame] | 251 | bcm63xx_wdt_device.regs = ioremap_nocache(r->start, resource_size(r)); |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 252 | if (!bcm63xx_wdt_device.regs) { |
| 253 | dev_err(&pdev->dev, "failed to remap I/O resources\n"); |
| 254 | return -ENXIO; |
| 255 | } |
| 256 | |
| 257 | ret = bcm63xx_timer_register(TIMER_WDT_ID, bcm63xx_wdt_isr, NULL); |
| 258 | if (ret < 0) { |
| 259 | dev_err(&pdev->dev, "failed to register wdt timer isr\n"); |
| 260 | goto unmap; |
| 261 | } |
| 262 | |
| 263 | if (bcm63xx_wdt_settimeout(wdt_time)) { |
| 264 | bcm63xx_wdt_settimeout(WDT_DEFAULT_TIME); |
| 265 | dev_info(&pdev->dev, |
| 266 | ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n", |
| 267 | wdt_time); |
| 268 | } |
| 269 | |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 270 | ret = misc_register(&bcm63xx_wdt_miscdev); |
| 271 | if (ret < 0) { |
| 272 | dev_err(&pdev->dev, "failed to register watchdog device\n"); |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 273 | goto unregister_timer; |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | dev_info(&pdev->dev, " started, timer margin: %d sec\n", |
| 277 | WDT_DEFAULT_TIME); |
| 278 | |
| 279 | return 0; |
| 280 | |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 281 | unregister_timer: |
| 282 | bcm63xx_timer_unregister(TIMER_WDT_ID); |
| 283 | unmap: |
| 284 | iounmap(bcm63xx_wdt_device.regs); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 288 | static int __devexit bcm63xx_wdt_remove(struct platform_device *pdev) |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 289 | { |
| 290 | if (!nowayout) |
| 291 | bcm63xx_wdt_pause(); |
| 292 | |
| 293 | misc_deregister(&bcm63xx_wdt_miscdev); |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 294 | bcm63xx_timer_unregister(TIMER_WDT_ID); |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 295 | iounmap(bcm63xx_wdt_device.regs); |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 299 | static void bcm63xx_wdt_shutdown(struct platform_device *pdev) |
| 300 | { |
| 301 | bcm63xx_wdt_pause(); |
| 302 | } |
| 303 | |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 304 | static struct platform_driver bcm63xx_wdt = { |
| 305 | .probe = bcm63xx_wdt_probe, |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 306 | .remove = __devexit_p(bcm63xx_wdt_remove), |
| 307 | .shutdown = bcm63xx_wdt_shutdown, |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 308 | .driver = { |
Wim Van Sebroeck | e6c3b69 | 2010-10-23 20:59:42 +0000 | [diff] [blame] | 309 | .owner = THIS_MODULE, |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 310 | .name = "bcm63xx-wdt", |
| 311 | } |
| 312 | }; |
| 313 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 314 | module_platform_driver(bcm63xx_wdt); |
Florian Fainelli | b63aa73 | 2010-08-28 22:03:45 +0200 | [diff] [blame] | 315 | |
| 316 | MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>"); |
| 317 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 318 | MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog"); |
| 319 | MODULE_LICENSE("GPL"); |
| 320 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 321 | MODULE_ALIAS("platform:bcm63xx-wdt"); |