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