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