1 /* $Id: riowatchdog.c,v 1.3.2.2 2002/01/23 18:48:02 davem Exp $
2  * riowatchdog.c - driver for hw watchdog inside Super I/O of RIO
3  *
4  * Copyright (C) 2001 David S. Miller (davem@redhat.com)
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/fs.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/miscdevice.h>
14 
15 #include <asm/io.h>
16 #include <asm/ebus.h>
17 #include <asm/bbc.h>
18 #include <asm/oplib.h>
19 #include <asm/uaccess.h>
20 
21 #include <asm/watchdog.h>
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@redhat.com>");
48 MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
49 MODULE_SUPPORTED_DEVICE("watchdog");
50 MODULE_LICENSE("GPL");
51 
52 #define RIOWD_NAME	"pmc"
53 #define RIOWD_MINOR	215
54 
55 static spinlock_t riowd_lock = SPIN_LOCK_UNLOCKED;
56 
57 static void *bbc_regs;
58 static void *riowd_regs;
59 #define WDTO_INDEX	0x05
60 
61 static int riowd_timeout = 1;		/* in minutes */
62 MODULE_PARM(riowd_timeout,"i");
63 MODULE_PARM_DESC(riowd_timeout, "Watchdog timeout in minutes");
64 
65 #if 0 /* Currently unused. */
66 static u8 riowd_readreg(int index)
67 {
68 	unsigned long flags;
69 	u8 ret;
70 
71 	spin_lock_irqsave(&riowd_lock, flags);
72 	writeb(index, riowd_regs + 0);
73 	ret = readb(riowd_regs + 1);
74 	spin_unlock_irqrestore(&riowd_lock, flags);
75 
76 	return ret;
77 }
78 #endif
79 
riowd_writereg(u8 val,int index)80 static void riowd_writereg(u8 val, int index)
81 {
82 	unsigned long flags;
83 
84 	spin_lock_irqsave(&riowd_lock, flags);
85 	writeb(index, riowd_regs + 0);
86 	writeb(val, riowd_regs + 1);
87 	spin_unlock_irqrestore(&riowd_lock, flags);
88 }
89 
riowd_pingtimer(void)90 static void riowd_pingtimer(void)
91 {
92 	riowd_writereg(riowd_timeout, WDTO_INDEX);
93 }
94 
riowd_stoptimer(void)95 static void riowd_stoptimer(void)
96 {
97 	u8 val;
98 
99 	riowd_writereg(0, WDTO_INDEX);
100 
101 	val = readb(bbc_regs + BBC_WDACTION);
102 	val &= ~BBC_WDACTION_RST;
103 	writeb(val, bbc_regs + BBC_WDACTION);
104 }
105 
riowd_starttimer(void)106 static void riowd_starttimer(void)
107 {
108 	u8 val;
109 
110 	riowd_writereg(riowd_timeout, WDTO_INDEX);
111 
112 	val = readb(bbc_regs + BBC_WDACTION);
113 	val |= BBC_WDACTION_RST;
114 	writeb(val, bbc_regs + BBC_WDACTION);
115 }
116 
riowd_open(struct inode * inode,struct file * filp)117 static int riowd_open(struct inode *inode, struct file *filp)
118 {
119 	return 0;
120 }
121 
riowd_release(struct inode * inode,struct file * filp)122 static int riowd_release(struct inode *inode, struct file *filp)
123 {
124 	return 0;
125 }
126 
riowd_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)127 static int riowd_ioctl(struct inode *inode, struct file *filp,
128 		       unsigned int cmd, unsigned long arg)
129 {
130 	static struct watchdog_info info = {
131 	       	WDIOF_SETTIMEOUT, 0, "Natl. Semiconductor PC97317"
132 	};
133 	unsigned int options;
134 	int new_margin;
135 
136 	switch (cmd) {
137 	case WDIOC_GETSUPPORT:
138 		if (copy_to_user((struct watchdog_info *) arg, &info, sizeof(info)))
139 			return -EFAULT;
140 		break;
141 
142 	case WDIOC_GETSTATUS:
143 	case WDIOC_GETBOOTSTATUS:
144 		if (put_user(0, (int *) arg))
145 			return -EFAULT;
146 		break;
147 
148 	case WDIOC_KEEPALIVE:
149 		riowd_pingtimer();
150 		break;
151 
152 	case WDIOC_SETOPTIONS:
153 		if (copy_from_user(&options, (void *) arg, sizeof(options)))
154 			return -EFAULT;
155 
156 		if (options & WDIOS_DISABLECARD)
157 			riowd_stoptimer();
158 		else if (options & WDIOS_ENABLECARD)
159 			riowd_starttimer();
160 		else
161 			return -EINVAL;
162 
163 		break;
164 
165 	case WDIOC_SETTIMEOUT:
166 		if (get_user(new_margin, (int *)arg))
167 			return -EFAULT;
168 		if ((new_margin < 60) || (new_margin > (255 * 60)))
169 		    return -EINVAL;
170 		riowd_timeout = (new_margin + 59) / 60;
171 		riowd_pingtimer();
172 		/* Fall */
173 
174 	case WDIOC_GETTIMEOUT:
175 		return put_user(riowd_timeout * 60, (int *)arg);
176 
177 	default:
178 		return -EINVAL;
179 	};
180 
181 	return 0;
182 }
183 
riowd_write(struct file * file,const char * buf,size_t count,loff_t * ppos)184 static ssize_t riowd_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
185 {
186 	if (ppos != &file->f_pos)
187 		return -ESPIPE;
188 
189 	if (count) {
190 		riowd_pingtimer();
191 		return 1;
192 	}
193 
194 	return 0;
195 }
196 
riowd_read(struct file * file,char * buffer,size_t count,loff_t * ppos)197 static ssize_t riowd_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
198 {
199 	return -EINVAL;
200 }
201 
202 static struct file_operations riowd_fops = {
203 	owner:		THIS_MODULE,
204 	ioctl:		riowd_ioctl,
205 	open:		riowd_open,
206 	write:		riowd_write,
207 	read:		riowd_read,
208 	release:	riowd_release,
209 };
210 
211 static struct miscdevice riowd_miscdev = { RIOWD_MINOR, RIOWD_NAME, &riowd_fops };
212 
riowd_bbc_init(void)213 static int __init riowd_bbc_init(void)
214 {
215 	struct 	linux_ebus *ebus = NULL;
216 	struct 	linux_ebus_device *edev = NULL;
217 	u8 val;
218 
219 	for_each_ebus(ebus) {
220 		for_each_ebusdev(edev, ebus) {
221 			if (!strcmp(edev->prom_name, "bbc"))
222 				goto found_bbc;
223 		}
224 	}
225 
226 found_bbc:
227 	if (!edev)
228 		return -ENODEV;
229 	bbc_regs = ioremap(edev->resource[0].start, BBC_REGS_SIZE);
230 	if (!bbc_regs)
231 		return -ENODEV;
232 
233 	/* Turn it off. */
234 	val = readb(bbc_regs + BBC_WDACTION);
235 	val &= ~BBC_WDACTION_RST;
236 	writeb(val, bbc_regs + BBC_WDACTION);
237 
238 	return 0;
239 }
240 
riowd_init(void)241 static int __init riowd_init(void)
242 {
243 	struct 	linux_ebus *ebus = NULL;
244 	struct 	linux_ebus_device *edev = NULL;
245 
246 	for_each_ebus(ebus) {
247 		for_each_ebusdev(edev, ebus) {
248 			if (!strcmp(edev->prom_name, RIOWD_NAME))
249 				goto ebus_done;
250 		}
251 	}
252 
253 ebus_done:
254 	if (!edev)
255 		goto fail;
256 
257 	riowd_regs = ioremap(edev->resource[0].start, 2);
258 	if (riowd_regs == NULL) {
259 		printk(KERN_ERR "pmc: Cannot map registers.\n");
260 		return -ENODEV;
261 	}
262 
263 	if (riowd_bbc_init()) {
264 		printk(KERN_ERR "pmc: Failure initializing BBC config.\n");
265 		goto fail;
266 	}
267 
268 	if (misc_register(&riowd_miscdev)) {
269 		printk(KERN_ERR "pmc: Cannot register watchdog misc device.\n");
270 		goto fail;
271 	}
272 
273 	printk(KERN_INFO "pmc: Hardware watchdog [%i minutes], "
274 	       "regs at %p\n", riowd_timeout, riowd_regs);
275 
276 	return 0;
277 
278 fail:
279 	if (riowd_regs) {
280 		iounmap(riowd_regs);
281 		riowd_regs = NULL;
282 	}
283 	if (bbc_regs) {
284 		iounmap(bbc_regs);
285 		bbc_regs = NULL;
286 	}
287 	return -ENODEV;
288 }
289 
riowd_cleanup(void)290 static void __exit riowd_cleanup(void)
291 {
292 	misc_deregister(&riowd_miscdev);
293 	iounmap(riowd_regs);
294 	riowd_regs = NULL;
295 	iounmap(bbc_regs);
296 	bbc_regs = NULL;
297 }
298 
299 module_init(riowd_init);
300 module_exit(riowd_cleanup);
301