xref: /OK3568_Linux_fs/kernel/drivers/watchdog/riowd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* riowd.c - driver for hw watchdog inside Super I/O of RIO
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/errno.h>
14*4882a593Smuzhiyun #include <linux/miscdevice.h>
15*4882a593Smuzhiyun #include <linux/watchdog.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_device.h>
18*4882a593Smuzhiyun #include <linux/io.h>
19*4882a593Smuzhiyun #include <linux/uaccess.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* RIO uses the NatSemi Super I/O power management logical device
24*4882a593Smuzhiyun  * as its' watchdog.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * When the watchdog triggers, it asserts a line to the BBC (Boot Bus
27*4882a593Smuzhiyun  * Controller) of the machine.  The BBC can only be configured to
28*4882a593Smuzhiyun  * trigger a power-on reset when the signal is asserted.  The BBC
29*4882a593Smuzhiyun  * can be configured to ignore the signal entirely as well.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * The only Super I/O device register we care about is at index
32*4882a593Smuzhiyun  * 0x05 (WDTO_INDEX) which is the watchdog time-out in minutes (1-255).
33*4882a593Smuzhiyun  * If set to zero, this disables the watchdog.  When set, the system
34*4882a593Smuzhiyun  * must periodically (before watchdog expires) clear (set to zero) and
35*4882a593Smuzhiyun  * re-set the watchdog else it will trigger.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * There are two other indexed watchdog registers inside this Super I/O
38*4882a593Smuzhiyun  * logical device, but they are unused.  The first, at index 0x06 is
39*4882a593Smuzhiyun  * the watchdog control and can be used to make the watchdog timer re-set
40*4882a593Smuzhiyun  * when the PS/2 mouse or serial lines show activity.  The second, at
41*4882a593Smuzhiyun  * index 0x07 is merely a sampling of the line from the watchdog to the
42*4882a593Smuzhiyun  * BBC.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * The watchdog device generates no interrupts.
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
48*4882a593Smuzhiyun MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
49*4882a593Smuzhiyun MODULE_SUPPORTED_DEVICE("watchdog");
50*4882a593Smuzhiyun MODULE_LICENSE("GPL");
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define DRIVER_NAME	"riowd"
53*4882a593Smuzhiyun #define PFX		DRIVER_NAME ": "
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun struct riowd {
56*4882a593Smuzhiyun 	void __iomem		*regs;
57*4882a593Smuzhiyun 	spinlock_t		lock;
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun static struct riowd *riowd_device;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define WDTO_INDEX	0x05
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static int riowd_timeout = 1;		/* in minutes */
65*4882a593Smuzhiyun module_param(riowd_timeout, int, 0);
66*4882a593Smuzhiyun MODULE_PARM_DESC(riowd_timeout, "Watchdog timeout in minutes");
67*4882a593Smuzhiyun 
riowd_writereg(struct riowd * p,u8 val,int index)68*4882a593Smuzhiyun static void riowd_writereg(struct riowd *p, u8 val, int index)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	unsigned long flags;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	spin_lock_irqsave(&p->lock, flags);
73*4882a593Smuzhiyun 	writeb(index, p->regs + 0);
74*4882a593Smuzhiyun 	writeb(val, p->regs + 1);
75*4882a593Smuzhiyun 	spin_unlock_irqrestore(&p->lock, flags);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
riowd_open(struct inode * inode,struct file * filp)78*4882a593Smuzhiyun static int riowd_open(struct inode *inode, struct file *filp)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	stream_open(inode, filp);
81*4882a593Smuzhiyun 	return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
riowd_release(struct inode * inode,struct file * filp)84*4882a593Smuzhiyun static int riowd_release(struct inode *inode, struct file *filp)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
riowd_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)89*4882a593Smuzhiyun static long riowd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	static const struct watchdog_info info = {
92*4882a593Smuzhiyun 		.options		= WDIOF_SETTIMEOUT,
93*4882a593Smuzhiyun 		.firmware_version	= 1,
94*4882a593Smuzhiyun 		.identity		= DRIVER_NAME,
95*4882a593Smuzhiyun 	};
96*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
97*4882a593Smuzhiyun 	struct riowd *p = riowd_device;
98*4882a593Smuzhiyun 	unsigned int options;
99*4882a593Smuzhiyun 	int new_margin;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	switch (cmd) {
102*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
103*4882a593Smuzhiyun 		if (copy_to_user(argp, &info, sizeof(info)))
104*4882a593Smuzhiyun 			return -EFAULT;
105*4882a593Smuzhiyun 		break;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
108*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
109*4882a593Smuzhiyun 		if (put_user(0, (int __user *)argp))
110*4882a593Smuzhiyun 			return -EFAULT;
111*4882a593Smuzhiyun 		break;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
114*4882a593Smuzhiyun 		riowd_writereg(p, riowd_timeout, WDTO_INDEX);
115*4882a593Smuzhiyun 		break;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
118*4882a593Smuzhiyun 		if (copy_from_user(&options, argp, sizeof(options)))
119*4882a593Smuzhiyun 			return -EFAULT;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		if (options & WDIOS_DISABLECARD)
122*4882a593Smuzhiyun 			riowd_writereg(p, 0, WDTO_INDEX);
123*4882a593Smuzhiyun 		else if (options & WDIOS_ENABLECARD)
124*4882a593Smuzhiyun 			riowd_writereg(p, riowd_timeout, WDTO_INDEX);
125*4882a593Smuzhiyun 		else
126*4882a593Smuzhiyun 			return -EINVAL;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		break;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
131*4882a593Smuzhiyun 		if (get_user(new_margin, (int __user *)argp))
132*4882a593Smuzhiyun 			return -EFAULT;
133*4882a593Smuzhiyun 		if ((new_margin < 60) || (new_margin > (255 * 60)))
134*4882a593Smuzhiyun 			return -EINVAL;
135*4882a593Smuzhiyun 		riowd_timeout = (new_margin + 59) / 60;
136*4882a593Smuzhiyun 		riowd_writereg(p, riowd_timeout, WDTO_INDEX);
137*4882a593Smuzhiyun 		fallthrough;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
140*4882a593Smuzhiyun 		return put_user(riowd_timeout * 60, (int __user *)argp);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	default:
143*4882a593Smuzhiyun 		return -EINVAL;
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
riowd_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)149*4882a593Smuzhiyun static ssize_t riowd_write(struct file *file, const char __user *buf,
150*4882a593Smuzhiyun 						size_t count, loff_t *ppos)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct riowd *p = riowd_device;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (count) {
155*4882a593Smuzhiyun 		riowd_writereg(p, riowd_timeout, WDTO_INDEX);
156*4882a593Smuzhiyun 		return 1;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return 0;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static const struct file_operations riowd_fops = {
163*4882a593Smuzhiyun 	.owner =		THIS_MODULE,
164*4882a593Smuzhiyun 	.llseek =		no_llseek,
165*4882a593Smuzhiyun 	.unlocked_ioctl =	riowd_ioctl,
166*4882a593Smuzhiyun 	.compat_ioctl	=	compat_ptr_ioctl,
167*4882a593Smuzhiyun 	.open =			riowd_open,
168*4882a593Smuzhiyun 	.write =		riowd_write,
169*4882a593Smuzhiyun 	.release =		riowd_release,
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static struct miscdevice riowd_miscdev = {
173*4882a593Smuzhiyun 	.minor	= WATCHDOG_MINOR,
174*4882a593Smuzhiyun 	.name	= "watchdog",
175*4882a593Smuzhiyun 	.fops	= &riowd_fops
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
riowd_probe(struct platform_device * op)178*4882a593Smuzhiyun static int riowd_probe(struct platform_device *op)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	struct riowd *p;
181*4882a593Smuzhiyun 	int err = -EINVAL;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (riowd_device)
184*4882a593Smuzhiyun 		goto out;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	err = -ENOMEM;
187*4882a593Smuzhiyun 	p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
188*4882a593Smuzhiyun 	if (!p)
189*4882a593Smuzhiyun 		goto out;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	spin_lock_init(&p->lock);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	p->regs = of_ioremap(&op->resource[0], 0, 2, DRIVER_NAME);
194*4882a593Smuzhiyun 	if (!p->regs) {
195*4882a593Smuzhiyun 		pr_err("Cannot map registers\n");
196*4882a593Smuzhiyun 		goto out;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 	/* Make miscdev useable right away */
199*4882a593Smuzhiyun 	riowd_device = p;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	err = misc_register(&riowd_miscdev);
202*4882a593Smuzhiyun 	if (err) {
203*4882a593Smuzhiyun 		pr_err("Cannot register watchdog misc device\n");
204*4882a593Smuzhiyun 		goto out_iounmap;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	pr_info("Hardware watchdog [%i minutes], regs at %p\n",
208*4882a593Smuzhiyun 		riowd_timeout, p->regs);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	platform_set_drvdata(op, p);
211*4882a593Smuzhiyun 	return 0;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun out_iounmap:
214*4882a593Smuzhiyun 	riowd_device = NULL;
215*4882a593Smuzhiyun 	of_iounmap(&op->resource[0], p->regs, 2);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun out:
218*4882a593Smuzhiyun 	return err;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
riowd_remove(struct platform_device * op)221*4882a593Smuzhiyun static int riowd_remove(struct platform_device *op)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct riowd *p = platform_get_drvdata(op);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	misc_deregister(&riowd_miscdev);
226*4882a593Smuzhiyun 	of_iounmap(&op->resource[0], p->regs, 2);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	return 0;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun static const struct of_device_id riowd_match[] = {
232*4882a593Smuzhiyun 	{
233*4882a593Smuzhiyun 		.name = "pmc",
234*4882a593Smuzhiyun 	},
235*4882a593Smuzhiyun 	{},
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, riowd_match);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun static struct platform_driver riowd_driver = {
240*4882a593Smuzhiyun 	.driver = {
241*4882a593Smuzhiyun 		.name = DRIVER_NAME,
242*4882a593Smuzhiyun 		.of_match_table = riowd_match,
243*4882a593Smuzhiyun 	},
244*4882a593Smuzhiyun 	.probe		= riowd_probe,
245*4882a593Smuzhiyun 	.remove		= riowd_remove,
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun module_platform_driver(riowd_driver);
249