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