ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / wdt285.c
1 /*
2  *      Intel 21285 watchdog driver
3  *      Copyright (c) Phil Blundell <pb@nexus.co.uk>, 1998
4  *
5  *      based on
6  *
7  *      SoftDog 0.05:   A Software Watchdog Device
8  *
9  *      (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
10  *
11  *      This program is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU General Public License
13  *      as published by the Free Software Foundation; either version
14  *      2 of the License, or (at your option) any later version.
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/fs.h>
23 #include <linux/mm.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/reboot.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29
30 #include <asm/irq.h>
31 #include <asm/uaccess.h>
32 #include <asm/hardware.h>
33 #include <asm/mach-types.h>
34 #include <asm/hardware/dec21285.h>
35
36 /*
37  * Define this to stop the watchdog actually rebooting the machine.
38  */
39 #undef ONLY_TESTING
40
41 static unsigned int soft_margin = 60;           /* in seconds */
42 static unsigned int reload;
43 static unsigned long timer_alive;
44
45 #ifdef ONLY_TESTING
46 /*
47  *      If the timer expires..
48  */
49 static void watchdog_fire(int irq, void *dev_id, struct pt_regs *regs)
50 {
51         printk(KERN_CRIT "Watchdog: Would Reboot.\n");
52         *CSR_TIMER4_CNTL = 0;
53         *CSR_TIMER4_CLR = 0;
54 }
55 #endif
56
57 /*
58  *      Refresh the timer.
59  */
60 static void watchdog_ping(void)
61 {
62         *CSR_TIMER4_LOAD = reload;
63 }
64
65 /*
66  *      Allow only one person to hold it open
67  */
68 static int watchdog_open(struct inode *inode, struct file *file)
69 {
70         int ret;
71
72         if (*CSR_SA110_CNTL & (1 << 13))
73                 return -EBUSY;
74
75         if (test_and_set_bit(1, &timer_alive))
76                 return -EBUSY;
77
78         reload = soft_margin * (mem_fclk_21285 / 256);
79
80         *CSR_TIMER4_CLR = 0;
81         watchdog_ping();
82         *CSR_TIMER4_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD
83                 | TIMER_CNTL_DIV256;
84
85 #ifdef ONLY_TESTING
86         ret = request_irq(IRQ_TIMER4, watchdog_fire, 0, "watchdog", NULL);
87         if (ret) {
88                 *CSR_TIMER4_CNTL = 0;
89                 clear_bit(1, &timer_alive);
90         }
91 #else
92         /*
93          * Setting this bit is irreversible; once enabled, there is
94          * no way to disable the watchdog.
95          */
96         *CSR_SA110_CNTL |= 1 << 13;
97
98         ret = 0;
99 #endif
100         return ret;
101 }
102
103 /*
104  *      Shut off the timer.
105  *      Note: if we really have enabled the watchdog, there
106  *      is no way to turn off.
107  */
108 static int watchdog_release(struct inode *inode, struct file *file)
109 {
110 #ifdef ONLY_TESTING
111         free_irq(IRQ_TIMER4, NULL);
112         clear_bit(1, &timer_alive);
113 #endif
114         return 0;
115 }
116
117 static ssize_t
118 watchdog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
119 {
120         /* Can't seek (pwrite) on this device  */
121         if (ppos != &file->f_pos)
122                 return -ESPIPE;
123
124         /*
125          *      Refresh the timer.
126          */
127         if (len)
128                 watchdog_ping();
129
130         return len;
131 }
132
133 static struct watchdog_info ident = {
134         .options        = WDIOF_SETTIMEOUT,
135         .identity       = "Footbridge Watchdog",
136 };
137
138 static int
139 watchdog_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
140                unsigned long arg)
141 {
142         unsigned int new_margin;
143         int ret = -ENOIOCTLCMD;
144
145         switch(cmd) {
146         case WDIOC_GETSUPPORT:
147                 ret = 0;
148                 if (copy_to_user((void *)arg, &ident, sizeof(ident)))
149                         ret = -EFAULT;
150                 break;
151
152         case WDIOC_GETSTATUS:
153         case WDIOC_GETBOOTSTATUS:
154                 ret = put_user(0,(int *)arg);
155                 break;
156
157         case WDIOC_KEEPALIVE:
158                 watchdog_ping();
159                 ret = 0;
160                 break;
161
162         case WDIOC_SETTIMEOUT:
163                 ret = get_user(new_margin, (int *)arg);
164                 if (ret)
165                         break;
166
167                 /* Arbitrary, can't find the card's limits */
168                 if (new_margin < 0 || new_margin > 60) {
169                         ret = -EINVAL;
170                         break;
171                 }
172
173                 soft_margin = new_margin;
174                 reload = soft_margin * (mem_fclk_21285 / 256);
175                 watchdog_ping();
176                 /* Fall */
177         case WDIOC_GETTIMEOUT:
178                 ret = put_user(soft_margin, (int *)arg);
179                 break;
180         }
181         return ret;
182 }
183
184 static struct file_operations watchdog_fops = {
185         .owner          = THIS_MODULE,
186         .write          = watchdog_write,
187         .ioctl          = watchdog_ioctl,
188         .open           = watchdog_open,
189         .release        = watchdog_release,
190 };
191
192 static struct miscdevice watchdog_miscdev = {
193         .minor          = WATCHDOG_MINOR,
194         .name           = "watchdog",
195         .fops           = &watchdog_fops,
196 };
197
198 static int __init footbridge_watchdog_init(void)
199 {
200         int retval;
201
202         if (machine_is_netwinder())
203                 return -ENODEV;
204
205         retval = misc_register(&watchdog_miscdev);
206         if (retval < 0)
207                 return retval;
208
209         printk("Footbridge Watchdog Timer: 0.01, timer margin: %d sec\n",
210                soft_margin);
211
212         if (machine_is_cats())
213                 printk("Warning: Watchdog reset may not work on this machine.\n");
214         return 0;
215 }
216
217 static void __exit footbridge_watchdog_exit(void)
218 {
219         misc_deregister(&watchdog_miscdev);
220 }
221
222 MODULE_AUTHOR("Phil Blundell <pb@nexus.co.uk>");
223 MODULE_DESCRIPTION("Footbridge watchdog driver");
224 MODULE_LICENSE("GPL");
225 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
226
227 module_param(soft_margin, int, 0);
228 MODULE_PARM_DESC(soft_margin,"Watchdog timeout in seconds");
229
230 module_init(footbridge_watchdog_init);
231 module_exit(footbridge_watchdog_exit);