blob: bcd7e36ca0aa67b8f9e6e3c461d2b3f12912d0ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sma cpu5 watchdog driver
3 *
4 * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/types.h>
25#include <linux/errno.h>
26#include <linux/miscdevice.h>
27#include <linux/fs.h>
28#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/timer.h>
Steven Rostedt3fe0c272006-01-09 15:59:26 -080031#include <linux/completion.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080032#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/uaccess.h>
35
36#include <linux/watchdog.h>
37
38/* adjustable parameters */
39
40static int verbose = 0;
41static int port = 0x91;
42static int ticks = 10000;
43
44#define PFX "cpu5wdt: "
45
46#define CPU5WDT_EXTENT 0x0A
47
48#define CPU5WDT_STATUS_REG 0x00
49#define CPU5WDT_TIME_A_REG 0x02
50#define CPU5WDT_TIME_B_REG 0x03
51#define CPU5WDT_MODE_REG 0x04
52#define CPU5WDT_TRIGGER_REG 0x07
53#define CPU5WDT_ENABLE_REG 0x08
54#define CPU5WDT_RESET_REG 0x09
55
56#define CPU5WDT_INTERVAL (HZ/10+1)
57
58/* some device data */
59
60static struct {
Steven Rostedt3fe0c272006-01-09 15:59:26 -080061 struct completion stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 volatile int running;
63 struct timer_list timer;
64 volatile int queue;
65 int default_ticks;
66 unsigned long inuse;
67} cpu5wdt_device;
68
69/* generic helper functions */
70
71static void cpu5wdt_trigger(unsigned long unused)
72{
73 if ( verbose > 2 )
74 printk(KERN_DEBUG PFX "trigger at %i ticks\n", ticks);
75
76 if( cpu5wdt_device.running )
77 ticks--;
78
79 /* keep watchdog alive */
80 outb(1, port + CPU5WDT_TRIGGER_REG);
81
82 /* requeue?? */
Jiri Slaby82eb7c52007-02-08 18:39:36 +010083 if (cpu5wdt_device.queue && ticks)
84 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 else {
86 /* ticks doesn't matter anyway */
Steven Rostedt3fe0c272006-01-09 15:59:26 -080087 complete(&cpu5wdt_device.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
90}
91
92static void cpu5wdt_reset(void)
93{
94 ticks = cpu5wdt_device.default_ticks;
95
96 if ( verbose )
97 printk(KERN_DEBUG PFX "reset (%i ticks)\n", (int) ticks);
98
99}
100
101static void cpu5wdt_start(void)
102{
103 if ( !cpu5wdt_device.queue ) {
104 cpu5wdt_device.queue = 1;
105 outb(0, port + CPU5WDT_TIME_A_REG);
106 outb(0, port + CPU5WDT_TIME_B_REG);
107 outb(1, port + CPU5WDT_MODE_REG);
108 outb(0, port + CPU5WDT_RESET_REG);
109 outb(0, port + CPU5WDT_ENABLE_REG);
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100110 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 /* if process dies, counter is not decremented */
113 cpu5wdt_device.running++;
114}
115
116static int cpu5wdt_stop(void)
117{
118 if ( cpu5wdt_device.running )
119 cpu5wdt_device.running = 0;
120
121 ticks = cpu5wdt_device.default_ticks;
122
123 if ( verbose )
124 printk(KERN_CRIT PFX "stop not possible\n");
125
126 return -EIO;
127}
128
129/* filesystem operations */
130
131static int cpu5wdt_open(struct inode *inode, struct file *file)
132{
133 if ( test_and_set_bit(0, &cpu5wdt_device.inuse) )
134 return -EBUSY;
135
136 return nonseekable_open(inode, file);
137}
138
139static int cpu5wdt_release(struct inode *inode, struct file *file)
140{
141 clear_bit(0, &cpu5wdt_device.inuse);
142 return 0;
143}
144
145static int cpu5wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
146{
147 void __user *argp = (void __user *)arg;
148 unsigned int value;
149 static struct watchdog_info ident =
150 {
151 .options = WDIOF_CARDRESET,
152 .identity = "CPU5 WDT",
153 };
154
155 switch(cmd) {
156 case WDIOC_KEEPALIVE:
157 cpu5wdt_reset();
158 break;
159 case WDIOC_GETSTATUS:
160 value = inb(port + CPU5WDT_STATUS_REG);
161 value = (value >> 2) & 1;
162 if ( copy_to_user(argp, &value, sizeof(int)) )
163 return -EFAULT;
164 break;
165 case WDIOC_GETSUPPORT:
166 if ( copy_to_user(argp, &ident, sizeof(ident)) )
167 return -EFAULT;
168 break;
169 case WDIOC_SETOPTIONS:
170 if ( copy_from_user(&value, argp, sizeof(int)) )
171 return -EFAULT;
172 switch(value) {
173 case WDIOS_ENABLECARD:
174 cpu5wdt_start();
175 break;
176 case WDIOS_DISABLECARD:
177 return cpu5wdt_stop();
178 default:
179 return -EINVAL;
180 }
181 break;
182 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200183 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 return 0;
186}
187
188static ssize_t cpu5wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
189{
190 if ( !count )
191 return -EIO;
192
193 cpu5wdt_reset();
194
195 return count;
196}
197
Arjan van de Ven62322d22006-07-03 00:24:21 -0700198static const struct file_operations cpu5wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .owner = THIS_MODULE,
200 .llseek = no_llseek,
201 .ioctl = cpu5wdt_ioctl,
202 .open = cpu5wdt_open,
203 .write = cpu5wdt_write,
204 .release = cpu5wdt_release,
205};
206
207static struct miscdevice cpu5wdt_misc = {
208 .minor = WATCHDOG_MINOR,
209 .name = "watchdog",
210 .fops = &cpu5wdt_fops,
211};
212
213/* init/exit function */
214
215static int __devinit cpu5wdt_init(void)
216{
217 unsigned int val;
218 int err;
219
220 if ( verbose )
221 printk(KERN_DEBUG PFX "port=0x%x, verbose=%i\n", port, verbose);
222
223 if ( (err = misc_register(&cpu5wdt_misc)) < 0 ) {
224 printk(KERN_ERR PFX "misc_register failed\n");
225 goto no_misc;
226 }
227
228 if ( !request_region(port, CPU5WDT_EXTENT, PFX) ) {
229 printk(KERN_ERR PFX "request_region failed\n");
230 err = -EBUSY;
231 goto no_port;
232 }
233
234 /* watchdog reboot? */
235 val = inb(port + CPU5WDT_STATUS_REG);
236 val = (val >> 2) & 1;
237 if ( !val )
238 printk(KERN_INFO PFX "sorry, was my fault\n");
239
Steven Rostedt3fe0c272006-01-09 15:59:26 -0800240 init_completion(&cpu5wdt_device.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 cpu5wdt_device.queue = 0;
242
243 clear_bit(0, &cpu5wdt_device.inuse);
244
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100245 setup_timer(&cpu5wdt_device.timer, cpu5wdt_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 cpu5wdt_device.default_ticks = ticks;
248
249 printk(KERN_INFO PFX "init success\n");
250
251 return 0;
252
253no_port:
254 misc_deregister(&cpu5wdt_misc);
255no_misc:
256 return err;
257}
258
259static int __devinit cpu5wdt_init_module(void)
260{
261 return cpu5wdt_init();
262}
263
264static void __devexit cpu5wdt_exit(void)
265{
266 if ( cpu5wdt_device.queue ) {
267 cpu5wdt_device.queue = 0;
Steven Rostedt3fe0c272006-01-09 15:59:26 -0800268 wait_for_completion(&cpu5wdt_device.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 misc_deregister(&cpu5wdt_misc);
272
273 release_region(port, CPU5WDT_EXTENT);
274
275}
276
277static void __devexit cpu5wdt_exit_module(void)
278{
279 cpu5wdt_exit();
280}
281
282/* module entry points */
283
284module_init(cpu5wdt_init_module);
285module_exit(cpu5wdt_exit_module);
286
287MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
288MODULE_DESCRIPTION("sma cpu5 watchdog driver");
289MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
290MODULE_LICENSE("GPL");
291MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
292
293module_param(port, int, 0);
294MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
295
296module_param(verbose, int, 0);
297MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
298
299module_param(ticks, int, 0);
300MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");