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; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 47 | |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame^] | 48 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 49 | module_param(nowayout, int, 0); |
| 50 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 51 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 52 | static void mv64x60_wdt_reg_write(u32 val) |
| 53 | { |
| 54 | /* Allow write only to CTL1 / CTL2 fields, retaining values in |
| 55 | * other fields. |
| 56 | */ |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 57 | u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 58 | data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK); |
| 59 | data |= val; |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 60 | writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void mv64x60_wdt_service(void) |
| 64 | { |
| 65 | /* Write 01 followed by 10 to CTL2 */ |
| 66 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01)); |
| 67 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02)); |
| 68 | } |
| 69 | |
| 70 | static void mv64x60_wdt_handler_disable(void) |
| 71 | { |
| 72 | if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) { |
| 73 | /* Write 01 followed by 10 to CTL1 */ |
| 74 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01)); |
| 75 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02)); |
| 76 | printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void mv64x60_wdt_handler_enable(void) |
| 81 | { |
| 82 | if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) { |
| 83 | /* Write 01 followed by 10 to CTL1 */ |
| 84 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01)); |
| 85 | mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02)); |
| 86 | printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n"); |
| 87 | } |
| 88 | } |
| 89 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 90 | static void mv64x60_wdt_set_timeout(int timeout) |
| 91 | { |
| 92 | /* maximum bus cycle count is 0xFFFFFFFF */ |
| 93 | if (timeout > 0xFFFFFFFF / bus_clk) |
| 94 | timeout = 0xFFFFFFFF / bus_clk; |
| 95 | |
| 96 | mv64x60_wdt_timeout = timeout; |
| 97 | writel((timeout * bus_clk) >> 8, |
| 98 | mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET); |
| 99 | mv64x60_wdt_service(); |
| 100 | } |
| 101 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 102 | static int mv64x60_wdt_open(struct inode *inode, struct file *file) |
| 103 | { |
| 104 | if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags)) |
| 105 | return -EBUSY; |
| 106 | |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame^] | 107 | if (nowayout) |
| 108 | __module_get(THIS_MODULE); |
| 109 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 110 | mv64x60_wdt_service(); |
| 111 | mv64x60_wdt_handler_enable(); |
| 112 | |
Dale Farnsworth | 861e51377 | 2007-07-24 11:13:26 -0700 | [diff] [blame] | 113 | return nonseekable_open(inode, file); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int mv64x60_wdt_release(struct inode *inode, struct file *file) |
| 117 | { |
| 118 | mv64x60_wdt_service(); |
| 119 | |
Dale Farnsworth | d37a5c3 | 2007-07-24 11:17:23 -0700 | [diff] [blame^] | 120 | if (!nowayout) |
| 121 | mv64x60_wdt_handler_disable(); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 122 | |
| 123 | clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 128 | 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] | 129 | size_t len, loff_t * ppos) |
| 130 | { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 131 | if (len) |
| 132 | mv64x60_wdt_service(); |
| 133 | |
| 134 | return len; |
| 135 | } |
| 136 | |
| 137 | static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file, |
| 138 | unsigned int cmd, unsigned long arg) |
| 139 | { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 140 | int timeout; |
Dale Farnsworth | 85d5723 | 2007-07-24 11:16:29 -0700 | [diff] [blame] | 141 | int options; |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 142 | void __user *argp = (void __user *)arg; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 143 | static struct watchdog_info info = { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 144 | .options = WDIOF_SETTIMEOUT | |
| 145 | WDIOF_KEEPALIVEPING, |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 146 | .firmware_version = 0, |
| 147 | .identity = "MV64x60 watchdog", |
| 148 | }; |
| 149 | |
| 150 | switch (cmd) { |
| 151 | case WDIOC_GETSUPPORT: |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 152 | if (copy_to_user(argp, &info, sizeof(info))) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 153 | return -EFAULT; |
| 154 | break; |
| 155 | |
| 156 | case WDIOC_GETSTATUS: |
| 157 | case WDIOC_GETBOOTSTATUS: |
Al Viro | b2846df | 2005-09-29 00:42:27 +0100 | [diff] [blame] | 158 | if (put_user(wdt_status, (int __user *)argp)) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 159 | return -EFAULT; |
| 160 | wdt_status &= ~WDIOF_KEEPALIVEPING; |
| 161 | break; |
| 162 | |
| 163 | case WDIOC_GETTEMP: |
| 164 | return -EOPNOTSUPP; |
| 165 | |
| 166 | case WDIOC_SETOPTIONS: |
Dale Farnsworth | 85d5723 | 2007-07-24 11:16:29 -0700 | [diff] [blame] | 167 | if (get_user(options, (int __user *)argp)) |
| 168 | return -EFAULT; |
| 169 | |
| 170 | if (options & WDIOS_DISABLECARD) |
| 171 | mv64x60_wdt_handler_disable(); |
| 172 | |
| 173 | if (options & WDIOS_ENABLECARD) |
| 174 | mv64x60_wdt_handler_enable(); |
| 175 | break; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 176 | |
| 177 | case WDIOC_KEEPALIVE: |
| 178 | mv64x60_wdt_service(); |
| 179 | wdt_status |= WDIOF_KEEPALIVEPING; |
| 180 | break; |
| 181 | |
| 182 | case WDIOC_SETTIMEOUT: |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 183 | if (get_user(timeout, (int __user *)argp)) |
| 184 | return -EFAULT; |
| 185 | mv64x60_wdt_set_timeout(timeout); |
| 186 | /* Fall through */ |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 187 | |
| 188 | case WDIOC_GETTIMEOUT: |
Dale Farnsworth | 264f099 | 2007-07-24 11:14:21 -0700 | [diff] [blame] | 189 | if (put_user(mv64x60_wdt_timeout, (int __user *)argp)) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 190 | return -EFAULT; |
| 191 | break; |
| 192 | |
| 193 | default: |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 194 | return -ENOTTY; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 200 | static const struct file_operations mv64x60_wdt_fops = { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 201 | .owner = THIS_MODULE, |
| 202 | .llseek = no_llseek, |
| 203 | .write = mv64x60_wdt_write, |
| 204 | .ioctl = mv64x60_wdt_ioctl, |
| 205 | .open = mv64x60_wdt_open, |
| 206 | .release = mv64x60_wdt_release, |
| 207 | }; |
| 208 | |
| 209 | static struct miscdevice mv64x60_wdt_miscdev = { |
| 210 | .minor = WATCHDOG_MINOR, |
| 211 | .name = "watchdog", |
| 212 | .fops = &mv64x60_wdt_fops, |
| 213 | }; |
| 214 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 215 | static int __devinit mv64x60_wdt_probe(struct platform_device *dev) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 216 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 217 | struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data; |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 218 | struct resource *r; |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 219 | int timeout = 10; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 220 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 221 | bus_clk = 133; /* in MHz */ |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 222 | if (pdata) { |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 223 | timeout = pdata->timeout; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 224 | bus_clk = pdata->bus_clk; |
| 225 | } |
| 226 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 227 | /* Since bus_clk is truncated MHz, actual frequency could be |
| 228 | * up to 1MHz higher. Round up, since it's better to time out |
| 229 | * too late than too soon. |
| 230 | */ |
| 231 | bus_clk++; |
| 232 | bus_clk *= 1000000; /* convert to Hz */ |
| 233 | |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 234 | r = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 235 | if (!r) |
| 236 | return -ENODEV; |
| 237 | |
| 238 | mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1); |
| 239 | if (mv64x60_wdt_regs == NULL) |
| 240 | return -ENOMEM; |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 241 | |
Dale Farnsworth | 94796f9 | 2007-07-24 11:15:26 -0700 | [diff] [blame] | 242 | mv64x60_wdt_set_timeout(timeout); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 243 | |
| 244 | return misc_register(&mv64x60_wdt_miscdev); |
| 245 | } |
| 246 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 247 | static int __devexit mv64x60_wdt_remove(struct platform_device *dev) |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 248 | { |
| 249 | misc_deregister(&mv64x60_wdt_miscdev); |
| 250 | |
| 251 | mv64x60_wdt_service(); |
| 252 | mv64x60_wdt_handler_disable(); |
| 253 | |
Dale Farnsworth | 8a5cfa6 | 2007-07-24 11:09:18 -0700 | [diff] [blame] | 254 | iounmap(mv64x60_wdt_regs); |
| 255 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 259 | static struct platform_driver mv64x60_wdt_driver = { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 260 | .probe = mv64x60_wdt_probe, |
| 261 | .remove = __devexit_p(mv64x60_wdt_remove), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 262 | .driver = { |
| 263 | .owner = THIS_MODULE, |
| 264 | .name = MV64x60_WDT_NAME, |
| 265 | }, |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 266 | }; |
| 267 | |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 268 | static int __init mv64x60_wdt_init(void) |
| 269 | { |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 270 | printk(KERN_INFO "MV64x60 watchdog driver\n"); |
| 271 | |
Dale Farnsworth | 422db8d | 2007-07-24 11:07:38 -0700 | [diff] [blame] | 272 | return platform_driver_register(&mv64x60_wdt_driver); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void __exit mv64x60_wdt_exit(void) |
| 276 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 277 | platform_driver_unregister(&mv64x60_wdt_driver); |
James Chapman | 3be1021 | 2005-08-17 09:01:33 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | module_init(mv64x60_wdt_init); |
| 281 | module_exit(mv64x60_wdt_exit); |
| 282 | |
| 283 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 284 | MODULE_DESCRIPTION("MV64x60 watchdog driver"); |
| 285 | MODULE_LICENSE("GPL"); |
| 286 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |