James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface |
| 3 | * |
| 4 | * Author: James Chapman <jchapman@katalix.com> |
| 5 | * |
| 6 | * Platform-specific setup code should configure the dog to generate |
| 7 | * interrupt or reset as required. This code only enables/disables |
| 8 | * and services the watchdog. |
| 9 | * |
| 10 | * Derived from mpc8xx_wdt.c, with the following copyright. |
| 11 | * |
| 12 | * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under |
| 13 | * the terms of the GNU General Public License version 2. This program |
| 14 | * is licensed "as is" without any warranty of any kind, whether express |
| 15 | * or implied. |
| 16 | */ |
| 17 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 18 | #include <linux/fs.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/miscdevice.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/watchdog.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | |
Dale Farnsworth | 7e07a15 | 2007-07-24 11:12:24 -0700 | [diff] [blame] | 26 | #include <linux/mv643xx.h> |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 27 | #include <asm/uaccess.h> |
| 28 | #include <asm/io.h> |
| 29 | |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 30 | #define MV64x60_WDT_WDC_OFFSET 0 |
| 31 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 32 | /* MV64x60 WDC (config) register access definitions */ |
| 33 | #define MV64x60_WDC_CTL1_MASK (3 << 24) |
| 34 | #define MV64x60_WDC_CTL1(val) ((val & 3) << 24) |
| 35 | #define MV64x60_WDC_CTL2_MASK (3 << 26) |
| 36 | #define MV64x60_WDC_CTL2(val) ((val & 3) << 26) |
| 37 | |
| 38 | /* Flags bits */ |
| 39 | #define MV64x60_WDOG_FLAG_OPENED 0 |
| 40 | #define MV64x60_WDOG_FLAG_ENABLED 1 |
| 41 | |
| 42 | static unsigned long wdt_flags; |
| 43 | static int wdt_status; |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 44 | static void __iomem *mv64x60_wdt_regs; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 45 | static int mv64x60_wdt_timeout; |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 46 | static unsigned int bus_clk; |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 47 | static char expect_close; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 48 | |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame] | 49 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 50 | module_param(nowayout, int, 0); |
| 51 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 52 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 53 | static void mv64x60_wdt_reg_write(u32 val) |
| 54 | { |
| 55 | /* Allow write only to CTL1 / CTL2 fields, retaining values in |
| 56 | * other fields. |
| 57 | */ |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 58 | u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 59 | data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK); |
| 60 | data |= val; |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 61 | writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void mv64x60_wdt_service(void) |
| 65 | { |
| 66 | /* Write 01 followed by 10 to CTL2 */ |
| 67 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01)); |
| 68 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02)); |
| 69 | } |
| 70 | |
| 71 | static void mv64x60_wdt_handler_disable(void) |
| 72 | { |
| 73 | if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) { |
| 74 | /* Write 01 followed by 10 to CTL1 */ |
| 75 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01)); |
| 76 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02)); |
| 77 | printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void mv64x60_wdt_handler_enable(void) |
| 82 | { |
| 83 | if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) { |
| 84 | /* Write 01 followed by 10 to CTL1 */ |
| 85 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01)); |
| 86 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02)); |
| 87 | printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n"); |
| 88 | } |
| 89 | } |
| 90 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 91 | static void mv64x60_wdt_set_timeout(int timeout) |
| 92 | { |
| 93 | /* maximum bus cycle count is 0xFFFFFFFF */ |
| 94 | if (timeout > 0xFFFFFFFF / bus_clk) |
| 95 | timeout = 0xFFFFFFFF / bus_clk; |
| 96 | |
| 97 | mv64x60_wdt_timeout = timeout; |
| 98 | writel((timeout * bus_clk) >> 8, |
| 99 | mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
| 100 | mv64x60_wdt_service(); |
| 101 | } |
| 102 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 103 | static int mv64x60_wdt_open(struct inode *inode, struct file *file) |
| 104 | { |
| 105 | if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags)) |
| 106 | return -EBUSY; |
| 107 | |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame] | 108 | if (nowayout) |
| 109 | __module_get(THIS_MODULE); |
| 110 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 111 | mv64x60_wdt_service(); |
| 112 | mv64x60_wdt_handler_enable(); |
| 113 | |
Dale Farnsworth | 861e51377 | 2007-07-24 11:13:26 -0700 | [diff] [blame] | 114 | return nonseekable_open(inode, file); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int mv64x60_wdt_release(struct inode *inode, struct file *file) |
| 118 | { |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 119 | if (expect_close == 42) |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame] | 120 | mv64x60_wdt_handler_disable(); |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 121 | else { |
| 122 | printk(KERN_CRIT |
| 123 | "mv64x60_wdt: unexpected close, not stopping timer!\n"); |
| 124 | mv64x60_wdt_service(); |
| 125 | } |
| 126 | expect_close = 0; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 127 | |
| 128 | clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 133 | static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data, |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 134 | size_t len, loff_t * ppos) |
| 135 | { |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 136 | if (len) { |
| 137 | if (!nowayout) { |
| 138 | size_t i; |
| 139 | |
| 140 | expect_close = 0; |
| 141 | |
| 142 | for (i = 0; i != len; i++) { |
| 143 | char c; |
| 144 | if(get_user(c, data + i)) |
| 145 | return -EFAULT; |
| 146 | if (c == 'V') |
| 147 | expect_close = 42; |
| 148 | } |
| 149 | } |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 150 | mv64x60_wdt_service(); |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 151 | } |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 152 | |
| 153 | return len; |
| 154 | } |
| 155 | |
| 156 | static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file, |
| 157 | unsigned int cmd, unsigned long arg) |
| 158 | { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 159 | int timeout; |
Dale Farnsworth | 85d5723 | 2007-07-24 11:16:29 -0700 | [diff] [blame] | 160 | int options; |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 161 | void __user *argp = (void __user *)arg; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 162 | static struct watchdog_info info = { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 163 | .options = WDIOF_SETTIMEOUT | |
Dale Farnsworth | bf2fc92 | 2007-07-24 11:18:14 -0700 | [diff] [blame] | 164 | WDIOF_MAGICCLOSE | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 165 | WDIOF_KEEPALIVEPING, |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 166 | .firmware_version = 0, |
| 167 | .identity = "MV64x60 watchdog", |
| 168 | }; |
| 169 | |
| 170 | switch (cmd) { |
| 171 | case WDIOC_GETSUPPORT: |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 172 | if (copy_to_user(argp, &info, sizeof(info))) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 173 | return -EFAULT; |
| 174 | break; |
| 175 | |
| 176 | case WDIOC_GETSTATUS: |
| 177 | case WDIOC_GETBOOTSTATUS: |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 178 | if (put_user(wdt_status, (int __user *)argp)) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 179 | return -EFAULT; |
| 180 | wdt_status &= ~WDIOF_KEEPALIVEPING; |
| 181 | break; |
| 182 | |
| 183 | case WDIOC_GETTEMP: |
| 184 | return -EOPNOTSUPP; |
| 185 | |
| 186 | case WDIOC_SETOPTIONS: |
Dale Farnsworth | 85d5723 | 2007-07-24 11:16:29 -0700 | [diff] [blame] | 187 | if (get_user(options, (int __user *)argp)) |
| 188 | return -EFAULT; |
| 189 | |
| 190 | if (options & WDIOS_DISABLECARD) |
| 191 | mv64x60_wdt_handler_disable(); |
| 192 | |
| 193 | if (options & WDIOS_ENABLECARD) |
| 194 | mv64x60_wdt_handler_enable(); |
| 195 | break; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 196 | |
| 197 | case WDIOC_KEEPALIVE: |
| 198 | mv64x60_wdt_service(); |
| 199 | wdt_status |= WDIOF_KEEPALIVEPING; |
| 200 | break; |
| 201 | |
| 202 | case WDIOC_SETTIMEOUT: |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 203 | if (get_user(timeout, (int __user *)argp)) |
| 204 | return -EFAULT; |
| 205 | mv64x60_wdt_set_timeout(timeout); |
| 206 | /* Fall through */ |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 207 | |
| 208 | case WDIOC_GETTIMEOUT: |
Dale Farnsworth | 264f099 | 2007-07-24 11:14:21 -0700 | [diff] [blame] | 209 | if (put_user(mv64x60_wdt_timeout, (int __user *)argp)) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 210 | return -EFAULT; |
| 211 | break; |
| 212 | |
| 213 | default: |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 214 | return -ENOTTY; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 220 | static const struct file_operations mv64x60_wdt_fops = { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 221 | .owner = THIS_MODULE, |
| 222 | .llseek = no_llseek, |
| 223 | .write = mv64x60_wdt_write, |
| 224 | .ioctl = mv64x60_wdt_ioctl, |
| 225 | .open = mv64x60_wdt_open, |
| 226 | .release = mv64x60_wdt_release, |
| 227 | }; |
| 228 | |
| 229 | static struct miscdevice mv64x60_wdt_miscdev = { |
| 230 | .minor = WATCHDOG_MINOR, |
| 231 | .name = "watchdog", |
| 232 | .fops = &mv64x60_wdt_fops, |
| 233 | }; |
| 234 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 235 | static int __devinit mv64x60_wdt_probe(struct platform_device *dev) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 236 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 237 | struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data; |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 238 | struct resource *r; |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 239 | int timeout = 10; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 240 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 241 | bus_clk = 133; /* in MHz */ |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 242 | if (pdata) { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 243 | timeout = pdata->timeout; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 244 | bus_clk = pdata->bus_clk; |
| 245 | } |
| 246 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 247 | /* Since bus_clk is truncated MHz, actual frequency could be |
| 248 | * up to 1MHz higher. Round up, since it's better to time out |
| 249 | * too late than too soon. |
| 250 | */ |
| 251 | bus_clk++; |
| 252 | bus_clk *= 1000000; /* convert to Hz */ |
| 253 | |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 254 | r = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 255 | if (!r) |
| 256 | return -ENODEV; |
| 257 | |
| 258 | mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1); |
| 259 | if (mv64x60_wdt_regs == NULL) |
| 260 | return -ENOMEM; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 261 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 262 | mv64x60_wdt_set_timeout(timeout); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 263 | |
Dale Farnsworth | 2422df5 | 2007-07-24 11:19:47 -0700 | [diff] [blame^] | 264 | mv64x60_wdt_handler_disable(); /* in case timer was already running */ |
| 265 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 266 | return misc_register(&mv64x60_wdt_miscdev); |
| 267 | } |
| 268 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 269 | static int __devexit mv64x60_wdt_remove(struct platform_device *dev) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 270 | { |
| 271 | misc_deregister(&mv64x60_wdt_miscdev); |
| 272 | |
| 273 | mv64x60_wdt_service(); |
| 274 | mv64x60_wdt_handler_disable(); |
| 275 | |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 276 | iounmap(mv64x60_wdt_regs); |
| 277 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 281 | static struct platform_driver mv64x60_wdt_driver = { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 282 | .probe = mv64x60_wdt_probe, |
| 283 | .remove = __devexit_p(mv64x60_wdt_remove), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 284 | .driver = { |
| 285 | .owner = THIS_MODULE, |
| 286 | .name = MV64x60_WDT_NAME, |
| 287 | }, |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 288 | }; |
| 289 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 290 | static int __init mv64x60_wdt_init(void) |
| 291 | { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 292 | printk(KERN_INFO "MV64x60 watchdog driver\n"); |
| 293 | |
Dale Farnsworth | 422db8d | 2007-07-24 11:07:38 -0700 | [diff] [blame] | 294 | return platform_driver_register(&mv64x60_wdt_driver); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void __exit mv64x60_wdt_exit(void) |
| 298 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 299 | platform_driver_unregister(&mv64x60_wdt_driver); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | module_init(mv64x60_wdt_init); |
| 303 | module_exit(mv64x60_wdt_exit); |
| 304 | |
| 305 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 306 | MODULE_DESCRIPTION("MV64x60 watchdog driver"); |
| 307 | MODULE_LICENSE("GPL"); |
| 308 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |