Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for SiByte SB1 SoCs |
| 3 | * |
Andrew Sharp | 5b73a41 | 2009-09-23 16:03:20 -0700 | [diff] [blame] | 4 | * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@lsi.com> |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 5 | * |
| 6 | * This driver is intended to make the second of two hardware watchdogs |
| 7 | * on the Sibyte 12XX and 11XX SoCs available to the user. There are two |
| 8 | * such devices available on the SoC, but it seems that there isn't an |
| 9 | * enumeration class for watchdogs in Linux like there is for RTCs. |
| 10 | * The second is used rather than the first because it uses IRQ 1, |
| 11 | * thereby avoiding all that IRQ 0 problematic nonsense. |
| 12 | * |
| 13 | * I have not tried this driver on a 1480 processor; it might work |
| 14 | * just well enough to really screw things up. |
| 15 | * |
| 16 | * It is a simple timer, and there is an interrupt that is raised the |
| 17 | * first time the timer expires. The second time it expires, the chip |
| 18 | * is reset and there is no way to redirect that NMI. Which could |
| 19 | * be problematic in some cases where this chip is sitting on the HT |
| 20 | * bus and has just taken responsibility for providing a cache block. |
| 21 | * Since the reset can't be redirected to the external reset pin, it is |
| 22 | * possible that other HT connected processors might hang and not reset. |
| 23 | * For Linux, a soft reset would probably be even worse than a hard reset. |
| 24 | * There you have it. |
| 25 | * |
| 26 | * The timer takes 23 bits of a 64 bit register (?) as a count value, |
| 27 | * and decrements the count every microsecond, for a max value of |
| 28 | * 0x7fffff usec or about 8.3ish seconds. |
| 29 | * |
| 30 | * This watchdog borrows some user semantics from the softdog driver, |
| 31 | * in that if you close the fd, it leaves the watchdog running, unless |
| 32 | * you previously wrote a 'V' to the fd, in which case it disables |
| 33 | * the watchdog when you close the fd like some other drivers. |
| 34 | * |
| 35 | * Based on various other watchdog drivers, which are probably all |
| 36 | * loosely based on something Alan Cox wrote years ago. |
| 37 | * |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 38 | * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
| 39 | * All Rights Reserved. |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 40 | * |
| 41 | * This program is free software; you can redistribute it and/or |
| 42 | * modify it under the terms of the GNU General Public License |
| 43 | * version 1 or 2 as published by the Free Software Foundation. |
| 44 | * |
| 45 | */ |
| 46 | #include <linux/module.h> |
| 47 | #include <linux/io.h> |
| 48 | #include <linux/uaccess.h> |
| 49 | #include <linux/fs.h> |
| 50 | #include <linux/reboot.h> |
| 51 | #include <linux/miscdevice.h> |
| 52 | #include <linux/watchdog.h> |
| 53 | #include <linux/interrupt.h> |
| 54 | |
| 55 | #include <asm/sibyte/sb1250.h> |
| 56 | #include <asm/sibyte/sb1250_regs.h> |
| 57 | #include <asm/sibyte/sb1250_int.h> |
| 58 | #include <asm/sibyte/sb1250_scd.h> |
| 59 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 60 | static DEFINE_SPINLOCK(sbwd_lock); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * set the initial count value of a timer |
| 64 | * |
| 65 | * wdog is the iomem address of the cfg register |
| 66 | */ |
| 67 | void sbwdog_set(char __iomem *wdog, unsigned long t) |
| 68 | { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 69 | spin_lock(&sbwd_lock); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 70 | __raw_writeb(0, wdog - 0x10); |
| 71 | __raw_writeq(t & 0x7fffffUL, wdog); |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 72 | spin_unlock(&sbwd_lock); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * cause the timer to [re]load it's initial count and start counting |
| 77 | * all over again |
| 78 | * |
| 79 | * wdog is the iomem address of the cfg register |
| 80 | */ |
| 81 | void sbwdog_pet(char __iomem *wdog) |
| 82 | { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 83 | spin_lock(&sbwd_lock); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 84 | __raw_writeb(__raw_readb(wdog) | 1, wdog); |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 85 | spin_unlock(&sbwd_lock); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static unsigned long sbwdog_gate; /* keeps it to one thread only */ |
| 89 | static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0)); |
| 90 | static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1)); |
| 91 | static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */ |
| 92 | static int expect_close; |
| 93 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 94 | static const struct watchdog_info ident = { |
| 95 | .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT | |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 96 | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 97 | .identity = "SiByte Watchdog", |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Allow only a single thread to walk the dog |
| 102 | */ |
| 103 | static int sbwdog_open(struct inode *inode, struct file *file) |
| 104 | { |
| 105 | nonseekable_open(inode, file); |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 106 | if (test_and_set_bit(0, &sbwdog_gate)) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 107 | return -EBUSY; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 108 | __module_get(THIS_MODULE); |
| 109 | |
| 110 | /* |
| 111 | * Activate the timer |
| 112 | */ |
| 113 | sbwdog_set(user_dog, timeout); |
| 114 | __raw_writeb(1, user_dog); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Put the dog back in the kennel. |
| 121 | */ |
| 122 | static int sbwdog_release(struct inode *inode, struct file *file) |
| 123 | { |
| 124 | if (expect_close == 42) { |
| 125 | __raw_writeb(0, user_dog); |
| 126 | module_put(THIS_MODULE); |
| 127 | } else { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 128 | printk(KERN_CRIT |
| 129 | "%s: Unexpected close, not stopping watchdog!\n", |
| 130 | ident.identity); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 131 | sbwdog_pet(user_dog); |
| 132 | } |
| 133 | clear_bit(0, &sbwdog_gate); |
| 134 | expect_close = 0; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * 42 - the answer |
| 141 | */ |
| 142 | static ssize_t sbwdog_write(struct file *file, const char __user *data, |
| 143 | size_t len, loff_t *ppos) |
| 144 | { |
| 145 | int i; |
| 146 | |
| 147 | if (len) { |
| 148 | /* |
| 149 | * restart the timer |
| 150 | */ |
| 151 | expect_close = 0; |
| 152 | |
| 153 | for (i = 0; i != len; i++) { |
| 154 | char c; |
| 155 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 156 | if (get_user(c, data + i)) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 157 | return -EFAULT; |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 158 | if (c == 'V') |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 159 | expect_close = 42; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 160 | } |
| 161 | sbwdog_pet(user_dog); |
| 162 | } |
| 163 | |
| 164 | return len; |
| 165 | } |
| 166 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 167 | static long sbwdog_ioctl(struct file *file, unsigned int cmd, |
| 168 | unsigned long arg) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 169 | { |
| 170 | int ret = -ENOTTY; |
| 171 | unsigned long time; |
| 172 | void __user *argp = (void __user *)arg; |
| 173 | int __user *p = argp; |
| 174 | |
| 175 | switch (cmd) { |
| 176 | case WDIOC_GETSUPPORT: |
| 177 | ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; |
| 178 | break; |
| 179 | |
| 180 | case WDIOC_GETSTATUS: |
| 181 | case WDIOC_GETBOOTSTATUS: |
| 182 | ret = put_user(0, p); |
| 183 | break; |
| 184 | |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 185 | case WDIOC_KEEPALIVE: |
| 186 | sbwdog_pet(user_dog); |
| 187 | ret = 0; |
| 188 | break; |
| 189 | |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 190 | case WDIOC_SETTIMEOUT: |
| 191 | ret = get_user(time, p); |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 192 | if (ret) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 193 | break; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 194 | |
| 195 | time *= 1000000; |
| 196 | if (time > 0x7fffffUL) { |
| 197 | ret = -EINVAL; |
| 198 | break; |
| 199 | } |
| 200 | timeout = time; |
| 201 | sbwdog_set(user_dog, timeout); |
| 202 | sbwdog_pet(user_dog); |
| 203 | |
| 204 | case WDIOC_GETTIMEOUT: |
| 205 | /* |
| 206 | * get the remaining count from the ... count register |
| 207 | * which is 1*8 before the config register |
| 208 | */ |
| 209 | ret = put_user(__raw_readq(user_dog - 8) / 1000000, p); |
| 210 | break; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 211 | } |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Notifier for system down |
| 217 | */ |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 218 | static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code, |
| 219 | void *erf) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 220 | { |
| 221 | if (code == SYS_DOWN || code == SYS_HALT) { |
| 222 | /* |
| 223 | * sit and sit |
| 224 | */ |
| 225 | __raw_writeb(0, user_dog); |
| 226 | __raw_writeb(0, kern_dog); |
| 227 | } |
| 228 | |
| 229 | return NOTIFY_DONE; |
| 230 | } |
| 231 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 232 | static const struct file_operations sbwdog_fops = { |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 233 | .owner = THIS_MODULE, |
| 234 | .llseek = no_llseek, |
| 235 | .write = sbwdog_write, |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 236 | .unlocked_ioctl = sbwdog_ioctl, |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 237 | .open = sbwdog_open, |
| 238 | .release = sbwdog_release, |
| 239 | }; |
| 240 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 241 | static struct miscdevice sbwdog_miscdev = { |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 242 | .minor = WATCHDOG_MINOR, |
| 243 | .name = "watchdog", |
| 244 | .fops = &sbwdog_fops, |
| 245 | }; |
| 246 | |
| 247 | static struct notifier_block sbwdog_notifier = { |
| 248 | .notifier_call = sbwdog_notify_sys, |
| 249 | }; |
| 250 | |
| 251 | /* |
| 252 | * interrupt handler |
| 253 | * |
| 254 | * doesn't do a whole lot for user, but oh so cleverly written so kernel |
| 255 | * code can use it to re-up the watchdog, thereby saving the kernel from |
| 256 | * having to create and maintain a timer, just to tickle another timer, |
| 257 | * which is just so wrong. |
| 258 | */ |
| 259 | irqreturn_t sbwdog_interrupt(int irq, void *addr) |
| 260 | { |
| 261 | unsigned long wd_init; |
| 262 | char *wd_cfg_reg = (char *)addr; |
| 263 | u8 cfg; |
| 264 | |
| 265 | cfg = __raw_readb(wd_cfg_reg); |
| 266 | wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff; |
| 267 | |
| 268 | /* |
| 269 | * if it's the second watchdog timer, it's for those users |
| 270 | */ |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 271 | if (wd_cfg_reg == user_dog) |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 272 | printk(KERN_CRIT "%s in danger of initiating system reset " |
| 273 | "in %ld.%01ld seconds\n", |
| 274 | ident.identity, |
| 275 | wd_init / 1000000, (wd_init / 100000) % 10); |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 276 | else |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 277 | cfg |= 1; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 278 | |
| 279 | __raw_writeb(cfg, wd_cfg_reg); |
| 280 | |
| 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
| 284 | static int __init sbwdog_init(void) |
| 285 | { |
| 286 | int ret; |
| 287 | |
| 288 | /* |
| 289 | * register a reboot notifier |
| 290 | */ |
| 291 | ret = register_reboot_notifier(&sbwdog_notifier); |
| 292 | if (ret) { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 293 | printk(KERN_ERR |
| 294 | "%s: cannot register reboot notifier (err=%d)\n", |
| 295 | ident.identity, ret); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * get the resources |
| 301 | */ |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 302 | |
| 303 | ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED, |
| 304 | ident.identity, (void *)user_dog); |
| 305 | if (ret) { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 306 | printk(KERN_ERR "%s: failed to request irq 1 - %d\n", |
| 307 | ident.identity, ret); |
| 308 | return ret; |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 311 | ret = misc_register(&sbwdog_miscdev); |
| 312 | if (ret == 0) { |
| 313 | printk(KERN_INFO "%s: timeout is %ld.%ld secs\n", |
| 314 | ident.identity, |
| 315 | timeout / 1000000, (timeout / 100000) % 10); |
| 316 | } else |
| 317 | free_irq(1, (void *)user_dog); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static void __exit sbwdog_exit(void) |
| 322 | { |
| 323 | misc_deregister(&sbwdog_miscdev); |
| 324 | } |
| 325 | |
| 326 | module_init(sbwdog_init); |
| 327 | module_exit(sbwdog_exit); |
| 328 | |
Andrew Sharp | 5b73a41 | 2009-09-23 16:03:20 -0700 | [diff] [blame] | 329 | MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>"); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 330 | MODULE_DESCRIPTION("SiByte Watchdog"); |
| 331 | |
| 332 | module_param(timeout, ulong, 0); |
| 333 | MODULE_PARM_DESC(timeout, |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 334 | "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)"); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 335 | |
| 336 | MODULE_LICENSE("GPL"); |
| 337 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 338 | |
| 339 | /* |
| 340 | * example code that can be put in a platform code area to utilize the |
| 341 | * first watchdog timer for the kernels own purpose. |
| 342 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 343 | void platform_wd_setup(void) |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 344 | { |
| 345 | int ret; |
| 346 | |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 347 | ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED, |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 348 | "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0)); |
| 349 | if (ret) { |
Alan Cox | df3c9de | 2008-05-19 14:08:33 +0100 | [diff] [blame] | 350 | printk(KERN_CRIT |
| 351 | "Watchdog IRQ zero(0) failed to be requested - %d\n", ret); |
Andrew Sharp | 75c752e | 2007-12-13 16:16:42 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | */ |