Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IDT Interprise 79RC32434 watchdog driver |
| 3 | * |
| 4 | * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org> |
| 5 | * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org> |
| 6 | * |
| 7 | * based on |
| 8 | * SoftDog 0.05: A Software Watchdog Device |
| 9 | * |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 10 | * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
| 11 | * All Rights Reserved. |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version |
| 16 | * 2 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | */ |
| 19 | |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 20 | #include <linux/module.h> /* For module specific items */ |
| 21 | #include <linux/moduleparam.h> /* For new moduleparam's */ |
| 22 | #include <linux/types.h> /* For standard types (like size_t) */ |
| 23 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
| 24 | #include <linux/kernel.h> /* For printk/panic/... */ |
| 25 | #include <linux/fs.h> /* For file operations */ |
| 26 | #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV |
| 27 | (WATCHDOG_MINOR) */ |
| 28 | #include <linux/watchdog.h> /* For the watchdog specific items */ |
| 29 | #include <linux/init.h> /* For __init/__exit/... */ |
| 30 | #include <linux/platform_device.h> /* For platform_driver framework */ |
| 31 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 32 | |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 33 | #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */ |
| 34 | |
| 35 | #define PFX KBUILD_MODNAME ": " |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 36 | |
Phil Sutter | d9a8798 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 37 | #define VERSION "0.4" |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 38 | |
| 39 | static struct { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 40 | unsigned long inuse; |
| 41 | } rc32434_wdt_device; |
| 42 | |
| 43 | static struct integ __iomem *wdt_reg; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 44 | |
| 45 | static int expect_close; |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 46 | |
| 47 | /* Board internal clock speed in Hz, |
| 48 | * the watchdog timer ticks at. */ |
| 49 | extern unsigned int idt_cpu_freq; |
| 50 | |
| 51 | /* translate wtcompare value to seconds and vice versa */ |
| 52 | #define WTCOMP2SEC(x) (x / idt_cpu_freq) |
| 53 | #define SEC2WTCOMP(x) (x * idt_cpu_freq) |
| 54 | |
| 55 | /* Use a default timeout of 20s. This should be |
| 56 | * safe for CPU clock speeds up to 400MHz, as |
| 57 | * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */ |
| 58 | #define WATCHDOG_TIMEOUT 20 |
| 59 | |
| 60 | static int timeout = WATCHDOG_TIMEOUT; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 61 | |
| 62 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 63 | module_param(nowayout, int, 0); |
| 64 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 65 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 66 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 67 | /* apply or and nand masks to data read from addr and write back */ |
| 68 | #define SET_BITS(addr, or, nand) \ |
| 69 | writel((readl(&addr) | or) & ~nand, &addr) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 70 | |
| 71 | static void rc32434_wdt_start(void) |
| 72 | { |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 73 | u32 or, nand; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 74 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 75 | /* zero the counter before enabling */ |
| 76 | writel(0, &wdt_reg->wtcount); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 77 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 78 | /* don't generate a non-maskable interrupt, |
| 79 | * do a warm reset instead */ |
| 80 | nand = 1 << RC32434_ERR_WNE; |
| 81 | or = 1 << RC32434_ERR_WRE; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 82 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 83 | /* reset the ERRCS timeout bit in case it's set */ |
| 84 | nand |= 1 << RC32434_ERR_WTO; |
| 85 | |
| 86 | SET_BITS(wdt_reg->errcs, or, nand); |
| 87 | |
| 88 | /* reset WTC timeout bit and enable WDT */ |
| 89 | nand = 1 << RC32434_WTC_TO; |
| 90 | or = 1 << RC32434_WTC_EN; |
| 91 | |
| 92 | SET_BITS(wdt_reg->wtc, or, nand); |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 93 | |
| 94 | printk(KERN_INFO PFX "Started watchdog timer.\n"); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void rc32434_wdt_stop(void) |
| 98 | { |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 99 | /* Disable WDT */ |
| 100 | SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN); |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 101 | |
| 102 | printk(KERN_INFO PFX "Stopped watchdog timer.\n"); |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 103 | } |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 104 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 105 | static int rc32434_wdt_set(int new_timeout) |
| 106 | { |
| 107 | int max_to = WTCOMP2SEC((u32)-1); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 108 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 109 | if (new_timeout < 0 || new_timeout > max_to) { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 110 | printk(KERN_ERR PFX "timeout value must be between 0 and %d", |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 111 | max_to); |
| 112 | return -EINVAL; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 113 | } |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 114 | timeout = new_timeout; |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 115 | writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 116 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 117 | return 0; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 120 | static void rc32434_wdt_ping(void) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 121 | { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 122 | writel(0, &wdt_reg->wtcount); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int rc32434_wdt_open(struct inode *inode, struct file *file) |
| 126 | { |
| 127 | if (test_and_set_bit(0, &rc32434_wdt_device.inuse)) |
| 128 | return -EBUSY; |
| 129 | |
| 130 | if (nowayout) |
| 131 | __module_get(THIS_MODULE); |
| 132 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 133 | rc32434_wdt_start(); |
| 134 | rc32434_wdt_ping(); |
| 135 | |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 136 | return nonseekable_open(inode, file); |
| 137 | } |
| 138 | |
| 139 | static int rc32434_wdt_release(struct inode *inode, struct file *file) |
| 140 | { |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 141 | if (expect_close == 42) { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 142 | rc32434_wdt_stop(); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 143 | module_put(THIS_MODULE); |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 144 | } else { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 145 | printk(KERN_CRIT PFX |
| 146 | "device closed unexpectedly. WDT will not stop!\n"); |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 147 | rc32434_wdt_ping(); |
| 148 | } |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 149 | clear_bit(0, &rc32434_wdt_device.inuse); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static ssize_t rc32434_wdt_write(struct file *file, const char *data, |
| 154 | size_t len, loff_t *ppos) |
| 155 | { |
| 156 | if (len) { |
| 157 | if (!nowayout) { |
| 158 | size_t i; |
| 159 | |
| 160 | /* In case it was set long ago */ |
| 161 | expect_close = 0; |
| 162 | |
| 163 | for (i = 0; i != len; i++) { |
| 164 | char c; |
| 165 | if (get_user(c, data + i)) |
| 166 | return -EFAULT; |
| 167 | if (c == 'V') |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 168 | expect_close = 42; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 171 | rc32434_wdt_ping(); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 172 | return len; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Wim Van Sebroeck | 7275fc8 | 2008-09-18 12:26:15 +0000 | [diff] [blame] | 177 | static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd, |
| 178 | unsigned long arg) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 179 | { |
| 180 | void __user *argp = (void __user *)arg; |
| 181 | int new_timeout; |
| 182 | unsigned int value; |
| 183 | static struct watchdog_info ident = { |
| 184 | .options = WDIOF_SETTIMEOUT | |
| 185 | WDIOF_KEEPALIVEPING | |
| 186 | WDIOF_MAGICCLOSE, |
| 187 | .identity = "RC32434_WDT Watchdog", |
| 188 | }; |
| 189 | switch (cmd) { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 190 | case WDIOC_GETSUPPORT: |
| 191 | if (copy_to_user(argp, &ident, sizeof(ident))) |
| 192 | return -EFAULT; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 193 | break; |
| 194 | case WDIOC_GETSTATUS: |
| 195 | case WDIOC_GETBOOTSTATUS: |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 196 | value = 0; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 197 | if (copy_to_user(argp, &value, sizeof(int))) |
| 198 | return -EFAULT; |
| 199 | break; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 200 | case WDIOC_SETOPTIONS: |
| 201 | if (copy_from_user(&value, argp, sizeof(int))) |
| 202 | return -EFAULT; |
| 203 | switch (value) { |
| 204 | case WDIOS_ENABLECARD: |
| 205 | rc32434_wdt_start(); |
| 206 | break; |
| 207 | case WDIOS_DISABLECARD: |
| 208 | rc32434_wdt_stop(); |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 209 | break; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 210 | default: |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | break; |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 214 | case WDIOC_KEEPALIVE: |
| 215 | rc32434_wdt_ping(); |
| 216 | break; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 217 | case WDIOC_SETTIMEOUT: |
| 218 | if (copy_from_user(&new_timeout, argp, sizeof(int))) |
| 219 | return -EFAULT; |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 220 | if (rc32434_wdt_set(new_timeout)) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 221 | return -EINVAL; |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 222 | /* Fall through */ |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 223 | case WDIOC_GETTIMEOUT: |
| 224 | return copy_to_user(argp, &timeout, sizeof(int)); |
| 225 | default: |
| 226 | return -ENOTTY; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static struct file_operations rc32434_wdt_fops = { |
| 233 | .owner = THIS_MODULE, |
| 234 | .llseek = no_llseek, |
| 235 | .write = rc32434_wdt_write, |
Wim Van Sebroeck | 7275fc8 | 2008-09-18 12:26:15 +0000 | [diff] [blame] | 236 | .unlocked_ioctl = rc32434_wdt_ioctl, |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 237 | .open = rc32434_wdt_open, |
| 238 | .release = rc32434_wdt_release, |
| 239 | }; |
| 240 | |
| 241 | static struct miscdevice rc32434_wdt_miscdev = { |
| 242 | .minor = WATCHDOG_MINOR, |
| 243 | .name = "watchdog", |
| 244 | .fops = &rc32434_wdt_fops, |
| 245 | }; |
| 246 | |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 247 | static char banner[] __devinitdata = KERN_INFO PFX |
| 248 | "Watchdog Timer version " VERSION ", timer margin: %d sec\n"; |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 249 | |
Phil Sutter | d9a8798 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 250 | static int __devinit rc32434_wdt_probe(struct platform_device *pdev) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 251 | { |
| 252 | int ret; |
| 253 | struct resource *r; |
| 254 | |
Phil Sutter | 0af98d3 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 255 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res"); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 256 | if (!r) { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 257 | printk(KERN_ERR PFX "failed to retrieve resources\n"); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 258 | return -ENODEV; |
| 259 | } |
| 260 | |
| 261 | wdt_reg = ioremap_nocache(r->start, r->end - r->start); |
| 262 | if (!wdt_reg) { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 263 | printk(KERN_ERR PFX "failed to remap I/O resources\n"); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 264 | return -ENXIO; |
| 265 | } |
| 266 | |
| 267 | ret = misc_register(&rc32434_wdt_miscdev); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 268 | if (ret < 0) { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 269 | printk(KERN_ERR PFX "failed to register watchdog device\n"); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 270 | goto unmap; |
| 271 | } |
| 272 | |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 273 | printk(banner, timeout); |
| 274 | |
| 275 | return 0; |
| 276 | |
| 277 | unmap: |
| 278 | iounmap(wdt_reg); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Phil Sutter | d9a8798 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 282 | static int __devexit rc32434_wdt_remove(struct platform_device *pdev) |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 283 | { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 284 | misc_deregister(&rc32434_wdt_miscdev); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 285 | iounmap(wdt_reg); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 289 | static struct platform_driver rc32434_wdt_driver = { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 290 | .probe = rc32434_wdt_probe, |
Phil Sutter | d9a8798 | 2009-02-08 16:44:42 +0100 | [diff] [blame] | 291 | .remove = __devexit_p(rc32434_wdt_remove), |
| 292 | .driver = { |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 293 | .name = "rc32434_wdt", |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | static int __init rc32434_wdt_init(void) |
| 298 | { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 299 | return platform_driver_register(&rc32434_wdt_driver); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void __exit rc32434_wdt_exit(void) |
| 303 | { |
Phil Sutter | 9b655e0 | 2009-02-08 16:44:42 +0100 | [diff] [blame^] | 304 | platform_driver_unregister(&rc32434_wdt_driver); |
Florian Fainelli | 03ec585 | 2008-02-25 13:11:31 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | module_init(rc32434_wdt_init); |
| 308 | module_exit(rc32434_wdt_exit); |
| 309 | |
| 310 | MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>," |
| 311 | "Florian Fainelli <florian@openwrt.org>"); |
| 312 | MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog"); |
| 313 | MODULE_LICENSE("GPL"); |
| 314 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |