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