2 * Watchdog driver for SiByte SB1 SoCs
4 * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@lsi.com>
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.
13 * I have not tried this driver on a 1480 processor; it might work
14 * just well enough to really screw things up.
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.
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.
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.
35 * Based on various other watchdog drivers, which are probably all
36 * loosely based on something Alan Cox wrote years ago.
38 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
39 * All Rights Reserved.
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.
47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49 #include <linux/module.h>
51 #include <linux/uaccess.h>
53 #include <linux/reboot.h>
54 #include <linux/miscdevice.h>
55 #include <linux/watchdog.h>
56 #include <linux/interrupt.h>
58 #include <asm/sibyte/sb1250.h>
59 #include <asm/sibyte/sb1250_regs.h>
60 #include <asm/sibyte/sb1250_int.h>
61 #include <asm/sibyte/sb1250_scd.h>
63 static DEFINE_SPINLOCK(sbwd_lock);
66 * set the initial count value of a timer
68 * wdog is the iomem address of the cfg register
70 static void sbwdog_set(char __iomem *wdog, unsigned long t)
72 spin_lock(&sbwd_lock);
73 __raw_writeb(0, wdog);
74 __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
75 spin_unlock(&sbwd_lock);
79 * cause the timer to [re]load it's initial count and start counting
82 * wdog is the iomem address of the cfg register
84 static void sbwdog_pet(char __iomem *wdog)
86 spin_lock(&sbwd_lock);
87 __raw_writeb(__raw_readb(wdog) | 1, wdog);
88 spin_unlock(&sbwd_lock);
91 static unsigned long sbwdog_gate; /* keeps it to one thread only */
92 static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
93 static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
94 static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
95 static int expect_close;
97 static const struct watchdog_info ident = {
98 .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
99 WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
100 .identity = "SiByte Watchdog",
104 * Allow only a single thread to walk the dog
106 static int sbwdog_open(struct inode *inode, struct file *file)
108 stream_open(inode, file);
109 if (test_and_set_bit(0, &sbwdog_gate))
111 __module_get(THIS_MODULE);
116 sbwdog_set(user_dog, timeout);
117 __raw_writeb(1, user_dog);
123 * Put the dog back in the kennel.
125 static int sbwdog_release(struct inode *inode, struct file *file)
127 if (expect_close == 42) {
128 __raw_writeb(0, user_dog);
129 module_put(THIS_MODULE);
131 pr_crit("%s: Unexpected close, not stopping watchdog!\n",
133 sbwdog_pet(user_dog);
135 clear_bit(0, &sbwdog_gate);
144 static ssize_t sbwdog_write(struct file *file, const char __user *data,
145 size_t len, loff_t *ppos)
155 for (i = 0; i != len; i++) {
158 if (get_user(c, data + i))
163 sbwdog_pet(user_dog);
169 static long sbwdog_ioctl(struct file *file, unsigned int cmd,
174 void __user *argp = (void __user *)arg;
175 int __user *p = argp;
178 case WDIOC_GETSUPPORT:
179 ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
182 case WDIOC_GETSTATUS:
183 case WDIOC_GETBOOTSTATUS:
184 ret = put_user(0, p);
187 case WDIOC_KEEPALIVE:
188 sbwdog_pet(user_dog);
192 case WDIOC_SETTIMEOUT:
193 ret = get_user(time, p);
198 if (time > 0x7fffffUL) {
203 sbwdog_set(user_dog, timeout);
204 sbwdog_pet(user_dog);
207 case WDIOC_GETTIMEOUT:
209 * get the remaining count from the ... count register
210 * which is 1*8 before the config register
212 ret = put_user((u32)__raw_readq(user_dog - 8) / 1000000, p);
219 * Notifier for system down
221 static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
224 if (code == SYS_DOWN || code == SYS_HALT) {
228 __raw_writeb(0, user_dog);
229 __raw_writeb(0, kern_dog);
235 static const struct file_operations sbwdog_fops = {
236 .owner = THIS_MODULE,
238 .write = sbwdog_write,
239 .unlocked_ioctl = sbwdog_ioctl,
240 .compat_ioctl = compat_ptr_ioctl,
242 .release = sbwdog_release,
245 static struct miscdevice sbwdog_miscdev = {
246 .minor = WATCHDOG_MINOR,
248 .fops = &sbwdog_fops,
251 static struct notifier_block sbwdog_notifier = {
252 .notifier_call = sbwdog_notify_sys,
258 * doesn't do a whole lot for user, but oh so cleverly written so kernel
259 * code can use it to re-up the watchdog, thereby saving the kernel from
260 * having to create and maintain a timer, just to tickle another timer,
261 * which is just so wrong.
263 irqreturn_t sbwdog_interrupt(int irq, void *addr)
265 unsigned long wd_init;
266 char *wd_cfg_reg = (char *)addr;
269 cfg = __raw_readb(wd_cfg_reg);
270 wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
273 * if it's the second watchdog timer, it's for those users
275 if (wd_cfg_reg == user_dog)
276 pr_crit("%s in danger of initiating system reset "
277 "in %ld.%01ld seconds\n",
279 wd_init / 1000000, (wd_init / 100000) % 10);
283 __raw_writeb(cfg, wd_cfg_reg);
288 static int __init sbwdog_init(void)
293 * register a reboot notifier
295 ret = register_reboot_notifier(&sbwdog_notifier);
297 pr_err("%s: cannot register reboot notifier (err=%d)\n",
298 ident.identity, ret);
306 ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
307 ident.identity, (void *)user_dog);
309 pr_err("%s: failed to request irq 1 - %d\n",
310 ident.identity, ret);
314 ret = misc_register(&sbwdog_miscdev);
316 pr_info("%s: timeout is %ld.%ld secs\n",
318 timeout / 1000000, (timeout / 100000) % 10);
321 free_irq(1, (void *)user_dog);
323 unregister_reboot_notifier(&sbwdog_notifier);
328 static void __exit sbwdog_exit(void)
330 misc_deregister(&sbwdog_miscdev);
331 free_irq(1, (void *)user_dog);
332 unregister_reboot_notifier(&sbwdog_notifier);
335 module_init(sbwdog_init);
336 module_exit(sbwdog_exit);
338 MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
339 MODULE_DESCRIPTION("SiByte Watchdog");
341 module_param(timeout, ulong, 0);
342 MODULE_PARM_DESC(timeout,
343 "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
345 MODULE_LICENSE("GPL");
348 * example code that can be put in a platform code area to utilize the
349 * first watchdog timer for the kernels own purpose.
351 void platform_wd_setup(void)
355 ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
356 "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
358 pr_crit("Watchdog IRQ zero(0) failed to be requested - %d\n", ret);