xref: /OK3568_Linux_fs/kernel/drivers/watchdog/iop_wdt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * drivers/char/watchdog/iop_wdt.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * WDT driver for Intel I/O Processors
6*4882a593Smuzhiyun  * Copyright (C) 2005, Intel Corporation.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Based on ixp4xx driver, Copyright 2004 (c) MontaVista, Software, Inc.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *	Curt E Bruns <curt.e.bruns@intel.com>
11*4882a593Smuzhiyun  *	Peter Milne <peter.milne@d-tacq.com>
12*4882a593Smuzhiyun  *	Dan Williams <dan.j.williams@intel.com>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/fs.h>
20*4882a593Smuzhiyun #include <linux/init.h>
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/miscdevice.h>
23*4882a593Smuzhiyun #include <linux/watchdog.h>
24*4882a593Smuzhiyun #include <linux/uaccess.h>
25*4882a593Smuzhiyun #include <mach/hardware.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
28*4882a593Smuzhiyun static unsigned long wdt_status;
29*4882a593Smuzhiyun static unsigned long boot_status;
30*4882a593Smuzhiyun static DEFINE_SPINLOCK(wdt_lock);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define WDT_IN_USE		0
33*4882a593Smuzhiyun #define WDT_OK_TO_CLOSE		1
34*4882a593Smuzhiyun #define WDT_ENABLED		2
35*4882a593Smuzhiyun 
iop_watchdog_timeout(void)36*4882a593Smuzhiyun static unsigned long iop_watchdog_timeout(void)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	return (0xffffffffUL / get_iop_tick_rate());
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun  * wdt_supports_disable - determine if we are accessing a iop13xx watchdog
43*4882a593Smuzhiyun  * or iop3xx by whether it has a disable command
44*4882a593Smuzhiyun  */
wdt_supports_disable(void)45*4882a593Smuzhiyun static int wdt_supports_disable(void)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	int can_disable;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	if (IOP_WDTCR_EN_ARM != IOP_WDTCR_DIS_ARM)
50*4882a593Smuzhiyun 		can_disable = 1;
51*4882a593Smuzhiyun 	else
52*4882a593Smuzhiyun 		can_disable = 0;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	return can_disable;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
wdt_enable(void)57*4882a593Smuzhiyun static void wdt_enable(void)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	/* Arm and enable the Timer to starting counting down from 0xFFFF.FFFF
60*4882a593Smuzhiyun 	 * Takes approx. 10.7s to timeout
61*4882a593Smuzhiyun 	 */
62*4882a593Smuzhiyun 	spin_lock(&wdt_lock);
63*4882a593Smuzhiyun 	write_wdtcr(IOP_WDTCR_EN_ARM);
64*4882a593Smuzhiyun 	write_wdtcr(IOP_WDTCR_EN);
65*4882a593Smuzhiyun 	spin_unlock(&wdt_lock);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /* returns 0 if the timer was successfully disabled */
wdt_disable(void)69*4882a593Smuzhiyun static int wdt_disable(void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	/* Stop Counting */
72*4882a593Smuzhiyun 	if (wdt_supports_disable()) {
73*4882a593Smuzhiyun 		spin_lock(&wdt_lock);
74*4882a593Smuzhiyun 		write_wdtcr(IOP_WDTCR_DIS_ARM);
75*4882a593Smuzhiyun 		write_wdtcr(IOP_WDTCR_DIS);
76*4882a593Smuzhiyun 		clear_bit(WDT_ENABLED, &wdt_status);
77*4882a593Smuzhiyun 		spin_unlock(&wdt_lock);
78*4882a593Smuzhiyun 		pr_info("Disabled\n");
79*4882a593Smuzhiyun 		return 0;
80*4882a593Smuzhiyun 	} else
81*4882a593Smuzhiyun 		return 1;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
iop_wdt_open(struct inode * inode,struct file * file)84*4882a593Smuzhiyun static int iop_wdt_open(struct inode *inode, struct file *file)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	if (test_and_set_bit(WDT_IN_USE, &wdt_status))
87*4882a593Smuzhiyun 		return -EBUSY;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
90*4882a593Smuzhiyun 	wdt_enable();
91*4882a593Smuzhiyun 	set_bit(WDT_ENABLED, &wdt_status);
92*4882a593Smuzhiyun 	return stream_open(inode, file);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
iop_wdt_write(struct file * file,const char * data,size_t len,loff_t * ppos)95*4882a593Smuzhiyun static ssize_t iop_wdt_write(struct file *file, const char *data, size_t len,
96*4882a593Smuzhiyun 		  loff_t *ppos)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	if (len) {
99*4882a593Smuzhiyun 		if (!nowayout) {
100*4882a593Smuzhiyun 			size_t i;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 			clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 			for (i = 0; i != len; i++) {
105*4882a593Smuzhiyun 				char c;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 				if (get_user(c, data + i))
108*4882a593Smuzhiyun 					return -EFAULT;
109*4882a593Smuzhiyun 				if (c == 'V')
110*4882a593Smuzhiyun 					set_bit(WDT_OK_TO_CLOSE, &wdt_status);
111*4882a593Smuzhiyun 			}
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 		wdt_enable();
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 	return len;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun static const struct watchdog_info ident = {
119*4882a593Smuzhiyun 	.options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
120*4882a593Smuzhiyun 	.identity = "iop watchdog",
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
iop_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)123*4882a593Smuzhiyun static long iop_wdt_ioctl(struct file *file,
124*4882a593Smuzhiyun 				unsigned int cmd, unsigned long arg)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	int options;
127*4882a593Smuzhiyun 	int ret = -ENOTTY;
128*4882a593Smuzhiyun 	int __user *argp = (int __user *)arg;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	switch (cmd) {
131*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
132*4882a593Smuzhiyun 		if (copy_to_user(argp, &ident, sizeof(ident)))
133*4882a593Smuzhiyun 			ret = -EFAULT;
134*4882a593Smuzhiyun 		else
135*4882a593Smuzhiyun 			ret = 0;
136*4882a593Smuzhiyun 		break;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
139*4882a593Smuzhiyun 		ret = put_user(0, argp);
140*4882a593Smuzhiyun 		break;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
143*4882a593Smuzhiyun 		ret = put_user(boot_status, argp);
144*4882a593Smuzhiyun 		break;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
147*4882a593Smuzhiyun 		if (get_user(options, (int *)arg))
148*4882a593Smuzhiyun 			return -EFAULT;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		if (options & WDIOS_DISABLECARD) {
151*4882a593Smuzhiyun 			if (!nowayout) {
152*4882a593Smuzhiyun 				if (wdt_disable() == 0) {
153*4882a593Smuzhiyun 					set_bit(WDT_OK_TO_CLOSE, &wdt_status);
154*4882a593Smuzhiyun 					ret = 0;
155*4882a593Smuzhiyun 				} else
156*4882a593Smuzhiyun 					ret = -ENXIO;
157*4882a593Smuzhiyun 			} else
158*4882a593Smuzhiyun 				ret = 0;
159*4882a593Smuzhiyun 		}
160*4882a593Smuzhiyun 		if (options & WDIOS_ENABLECARD) {
161*4882a593Smuzhiyun 			wdt_enable();
162*4882a593Smuzhiyun 			ret = 0;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 		break;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
167*4882a593Smuzhiyun 		wdt_enable();
168*4882a593Smuzhiyun 		ret = 0;
169*4882a593Smuzhiyun 		break;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
172*4882a593Smuzhiyun 		ret = put_user(iop_watchdog_timeout(), argp);
173*4882a593Smuzhiyun 		break;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 	return ret;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
iop_wdt_release(struct inode * inode,struct file * file)178*4882a593Smuzhiyun static int iop_wdt_release(struct inode *inode, struct file *file)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	int state = 1;
181*4882a593Smuzhiyun 	if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
182*4882a593Smuzhiyun 		if (test_bit(WDT_ENABLED, &wdt_status))
183*4882a593Smuzhiyun 			state = wdt_disable();
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	/* if the timer is not disabled reload and notify that we are still
186*4882a593Smuzhiyun 	 * going down
187*4882a593Smuzhiyun 	 */
188*4882a593Smuzhiyun 	if (state != 0) {
189*4882a593Smuzhiyun 		wdt_enable();
190*4882a593Smuzhiyun 		pr_crit("Device closed unexpectedly - reset in %lu seconds\n",
191*4882a593Smuzhiyun 			iop_watchdog_timeout());
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	clear_bit(WDT_IN_USE, &wdt_status);
195*4882a593Smuzhiyun 	clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun static const struct file_operations iop_wdt_fops = {
201*4882a593Smuzhiyun 	.owner = THIS_MODULE,
202*4882a593Smuzhiyun 	.llseek = no_llseek,
203*4882a593Smuzhiyun 	.write = iop_wdt_write,
204*4882a593Smuzhiyun 	.unlocked_ioctl = iop_wdt_ioctl,
205*4882a593Smuzhiyun 	.compat_ioctl = compat_ptr_ioctl,
206*4882a593Smuzhiyun 	.open = iop_wdt_open,
207*4882a593Smuzhiyun 	.release = iop_wdt_release,
208*4882a593Smuzhiyun };
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun static struct miscdevice iop_wdt_miscdev = {
211*4882a593Smuzhiyun 	.minor = WATCHDOG_MINOR,
212*4882a593Smuzhiyun 	.name = "watchdog",
213*4882a593Smuzhiyun 	.fops = &iop_wdt_fops,
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun 
iop_wdt_init(void)216*4882a593Smuzhiyun static int __init iop_wdt_init(void)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int ret;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	/* check if the reset was caused by the watchdog timer */
221*4882a593Smuzhiyun 	boot_status = (read_rcsr() & IOP_RCSR_WDT) ? WDIOF_CARDRESET : 0;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Configure Watchdog Timeout to cause an Internal Bus (IB) Reset
224*4882a593Smuzhiyun 	 * NOTE: An IB Reset will Reset both cores in the IOP342
225*4882a593Smuzhiyun 	 */
226*4882a593Smuzhiyun 	write_wdtsr(IOP13XX_WDTCR_IB_RESET);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	/* Register after we have the device set up so we cannot race
229*4882a593Smuzhiyun 	   with an open */
230*4882a593Smuzhiyun 	ret = misc_register(&iop_wdt_miscdev);
231*4882a593Smuzhiyun 	if (ret == 0)
232*4882a593Smuzhiyun 		pr_info("timeout %lu sec\n", iop_watchdog_timeout());
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return ret;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
iop_wdt_exit(void)237*4882a593Smuzhiyun static void __exit iop_wdt_exit(void)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	misc_deregister(&iop_wdt_miscdev);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun module_init(iop_wdt_init);
243*4882a593Smuzhiyun module_exit(iop_wdt_exit);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun module_param(nowayout, bool, 0);
246*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun MODULE_AUTHOR("Curt E Bruns <curt.e.bruns@intel.com>");
249*4882a593Smuzhiyun MODULE_DESCRIPTION("iop watchdog timer driver");
250*4882a593Smuzhiyun MODULE_LICENSE("GPL");
251