| 1 | /* |
| 2 | * ADM5120_WDT 0.01: Infineon ADM5120 SoC watchdog driver |
| 3 | * Copyright (c) Ondrej Zajicek <santiago@crfreenet.org>, 2007 |
| 4 | * |
| 5 | * based on |
| 6 | * |
| 7 | * RC32434_WDT 0.01: IDT Interprise 79RC32434 watchdog driver |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/miscdevice.h> |
| 19 | #include <linux/watchdog.h> |
| 20 | #include <linux/irq.h> |
| 21 | |
| 22 | #include <asm/bootinfo.h> |
| 23 | |
| 24 | #include <asm/mach-adm5120/adm5120_info.h> |
| 25 | #include <asm/mach-adm5120/adm5120_defs.h> |
| 26 | #include <asm/mach-adm5120/adm5120_switch.h> |
| 27 | |
| 28 | #define DEFAULT_TIMEOUT 15 /* (secs) Default is 15 seconds */ |
| 29 | #define MAX_TIMEOUT 327 |
| 30 | /* Max is 327 seconds, counter is 15-bit integer, step is 10 ms */ |
| 31 | |
| 32 | #define NAME "adm5120_wdt" |
| 33 | #define VERSION "0.1" |
| 34 | |
| 35 | static int expect_close; |
| 36 | static int access; |
| 37 | static unsigned int timeout = DEFAULT_TIMEOUT; |
| 38 | |
| 39 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 40 | module_param(nowayout, int, 0); |
| 41 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
| 44 | |
| 45 | static inline void wdt_set_timeout(void) |
| 46 | { |
| 47 | u32 val = (1 << 31) | (((timeout * 100) & 0x7FFF) << 16); |
| 48 | SW_WRITE_REG(SWITCH_REG_WDOG0, val); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | It looks like WDOG0-register-write don't modify counter, |
| 53 | but WDOG0-register-read resets counter. |
| 54 | */ |
| 55 | |
| 56 | static inline void wdt_reset_counter(void) |
| 57 | { |
| 58 | SW_READ_REG(SWITCH_REG_WDOG0); |
| 59 | } |
| 60 | |
| 61 | static inline void wdt_disable(void) |
| 62 | { |
| 63 | SW_WRITE_REG(SWITCH_REG_WDOG0, 0x7FFF0000); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | static int wdt_open(struct inode *inode, struct file *file) |
| 69 | { |
| 70 | /* Allow only one person to hold it open */ |
| 71 | if (access) |
| 72 | return -EBUSY; |
| 73 | |
| 74 | if (nowayout) |
| 75 | __module_get(THIS_MODULE); |
| 76 | |
| 77 | /* Activate timer */ |
| 78 | wdt_reset_counter(); |
| 79 | wdt_set_timeout(); |
| 80 | printk(KERN_INFO NAME ": enabling watchdog timer\n"); |
| 81 | access = 1; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int wdt_release(struct inode *inode, struct file *file) |
| 86 | { |
| 87 | /* |
| 88 | * Shut off the timer. |
| 89 | * Lock it in if it's a module and we set nowayout |
| 90 | */ |
| 91 | if (expect_close && (nowayout == 0)) { |
| 92 | wdt_disable(); |
| 93 | printk(KERN_INFO NAME ": disabling watchdog timer\n"); |
| 94 | module_put(THIS_MODULE); |
| 95 | } else |
| 96 | printk(KERN_CRIT NAME ": device closed unexpectedly. WDT will not stop!\n"); |
| 97 | |
| 98 | access = 0; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static ssize_t wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos) |
| 103 | { |
| 104 | /* Refresh the timer. */ |
| 105 | if (len) { |
| 106 | if (!nowayout) { |
| 107 | size_t i; |
| 108 | |
| 109 | /* In case it was set long ago */ |
| 110 | expect_close = 0; |
| 111 | |
| 112 | for (i = 0; i != len; i++) { |
| 113 | char c; |
| 114 | if (get_user(c, data + i)) |
| 115 | return -EFAULT; |
| 116 | if (c == 'V') |
| 117 | expect_close = 1; |
| 118 | } |
| 119 | } |
| 120 | wdt_reset_counter(); |
| 121 | return len; |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int wdt_ioctl(struct inode *inode, struct file *file, |
| 127 | unsigned int cmd, unsigned long arg) |
| 128 | { |
| 129 | int new_timeout; |
| 130 | static struct watchdog_info ident = { |
| 131 | .options = WDIOF_SETTIMEOUT | |
| 132 | WDIOF_KEEPALIVEPING | |
| 133 | WDIOF_MAGICCLOSE, |
| 134 | .firmware_version = 0, |
| 135 | .identity = "ADM5120_WDT Watchdog", |
| 136 | }; |
| 137 | switch (cmd) { |
| 138 | default: |
| 139 | return -ENOTTY; |
| 140 | case WDIOC_GETSUPPORT: |
| 141 | if (copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))) |
| 142 | return -EFAULT; |
| 143 | return 0; |
| 144 | case WDIOC_GETSTATUS: |
| 145 | case WDIOC_GETBOOTSTATUS: |
| 146 | return put_user(0, (int *)arg); |
| 147 | case WDIOC_KEEPALIVE: |
| 148 | wdt_reset_counter(); |
| 149 | return 0; |
| 150 | case WDIOC_SETTIMEOUT: |
| 151 | if (get_user(new_timeout, (int *)arg)) |
| 152 | return -EFAULT; |
| 153 | if (new_timeout < 1) |
| 154 | return -EINVAL; |
| 155 | if (new_timeout > MAX_TIMEOUT) |
| 156 | return -EINVAL; |
| 157 | timeout = new_timeout; |
| 158 | wdt_set_timeout(); |
| 159 | /* Fall */ |
| 160 | case WDIOC_GETTIMEOUT: |
| 161 | return put_user(timeout, (int *)arg); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static const struct file_operations wdt_fops = { |
| 166 | .owner = THIS_MODULE, |
| 167 | .llseek = no_llseek, |
| 168 | .write = wdt_write, |
| 169 | .ioctl = wdt_ioctl, |
| 170 | .open = wdt_open, |
| 171 | .release = wdt_release, |
| 172 | }; |
| 173 | |
| 174 | static struct miscdevice wdt_miscdev = { |
| 175 | .minor = WATCHDOG_MINOR, |
| 176 | .name = "watchdog", |
| 177 | .fops = &wdt_fops, |
| 178 | }; |
| 179 | |
| 180 | static char banner[] __initdata = KERN_INFO NAME ": Watchdog Timer version " VERSION "\n"; |
| 181 | |
| 182 | static int __init watchdog_init(void) |
| 183 | { |
| 184 | int ret; |
| 185 | |
| 186 | ret = misc_register(&wdt_miscdev); |
| 187 | |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | wdt_disable(); |
| 192 | printk(banner); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static void __exit watchdog_exit(void) |
| 198 | { |
| 199 | misc_deregister(&wdt_miscdev); |
| 200 | } |
| 201 | |
| 202 | module_init(watchdog_init); |
| 203 | module_exit(watchdog_exit); |
| 204 | |