This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / char / watchdog / ixp4xx_wdt.c
1 /*
2  * drivers/watchdog/ixp4xx_wdt.c
3  *
4  * Watchdog driver for Intel IXP4xx network processors
5  *
6  * Author: Deepak Saxena <dsaxena@plexity.net>
7  *
8  * Copyright 2004 (c) MontaVista, Software, Inc.
9  * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
10  *
11  * This file is licensed under  the terms of the GNU General Public
12  * License version 2. This program is licensed "as is" without any
13  * warranty of any kind, whether express or implied.
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/fs.h>
22 #include <linux/miscdevice.h>
23 #include <linux/watchdog.h>
24 #include <linux/init.h>
25
26 #include <asm/hardware.h>
27 #include <asm/bitops.h>
28 #include <asm/uaccess.h>
29
30 #ifdef CONFIG_WATCHDOG_NOWAYOUT
31 static int nowayout = 1;
32 #else
33 static int nowayout = 0;
34 #endif
35 static int heartbeat = 60;      /* (secs) Default is 1 minute */
36 static unsigned long wdt_status;
37 static unsigned long boot_status;
38
39 #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
40
41 #define WDT_IN_USE              0
42 #define WDT_OK_TO_CLOSE         1
43
44 static void
45 wdt_enable(void)
46 {
47         *IXP4XX_OSWK = IXP4XX_WDT_KEY;
48         *IXP4XX_OSWE = 0;
49         *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
50         *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
51         *IXP4XX_OSWK = 0;
52 }
53
54 static void
55 wdt_disable(void)
56 {
57         *IXP4XX_OSWK = IXP4XX_WDT_KEY;
58         *IXP4XX_OSWE = 0;
59         *IXP4XX_OSWK = 0;
60 }
61
62 static int
63 ixp4xx_wdt_open(struct inode *inode, struct file *file)
64 {
65         if (test_and_set_bit(WDT_IN_USE, &wdt_status))
66                 return -EBUSY;
67
68         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
69
70         wdt_enable();
71
72         return 0;
73 }
74
75 static ssize_t
76 ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
77 {
78         /* Can't seek (pwrite) on this device  */
79         if (ppos != &file->f_pos)
80                 return -ESPIPE;
81
82         if (len) {
83                 if (!nowayout) {
84                         size_t i;
85
86                         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
87
88                         for (i = 0; i != len; i++) {
89                                 char c;
90
91                                 if (get_user(c, data + i))
92                                         return -EFAULT;
93                                 if (c == 'V')
94                                         set_bit(WDT_OK_TO_CLOSE, &wdt_status);
95                         }
96                 }
97                 wdt_enable();
98         }
99
100         return len;
101 }
102
103 static struct watchdog_info ident = {
104         .options        = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
105                           WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
106         .identity       = "IXP4xx Watchdog",
107 };
108
109
110 static int
111 ixp4xx_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
112                         unsigned long arg)
113 {
114         int ret = -ENOIOCTLCMD;
115         int time;
116
117         switch (cmd) {
118         case WDIOC_GETSUPPORT:
119                 ret = copy_to_user((struct watchdog_info *)arg, &ident,
120                                    sizeof(ident)) ? -EFAULT : 0;
121                 break;
122
123         case WDIOC_GETSTATUS:
124                 ret = put_user(0, (int *)arg);
125                 break;
126
127         case WDIOC_GETBOOTSTATUS:
128                 ret = put_user(boot_status, (int *)arg);
129                 break;
130
131         case WDIOC_SETTIMEOUT:
132                 ret = get_user(time, (int *)arg);
133                 if (ret)
134                         break;
135
136                 if (time <= 0 || time > 60) {
137                         ret = -EINVAL;
138                         break;
139                 }
140
141                 heartbeat = time;
142                 wdt_enable();
143                 /* Fall through */
144
145         case WDIOC_GETTIMEOUT:
146                 ret = put_user(heartbeat, (int *)arg);
147                 break;
148
149         case WDIOC_KEEPALIVE:
150                 wdt_enable();
151                 ret = 0;
152                 break;
153         }
154         return ret;
155 }
156
157 static int
158 ixp4xx_wdt_release(struct inode *inode, struct file *file)
159 {
160         if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
161                 wdt_disable();
162         } else {
163                 printk(KERN_CRIT "WATCHDOG: Device closed unexpectdly - "
164                                         "timer will not stop\n");
165         }
166
167         clear_bit(WDT_IN_USE, &wdt_status);
168         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
169
170         return 0;
171 }
172
173
174 static struct file_operations ixp4xx_wdt_fops =
175 {
176         .owner          = THIS_MODULE,
177         .write          = ixp4xx_wdt_write,
178         .ioctl          = ixp4xx_wdt_ioctl,
179         .open           = ixp4xx_wdt_open,
180         .release        = ixp4xx_wdt_release,
181 };
182
183 static struct miscdevice ixp4xx_wdt_miscdev =
184 {
185         .minor          = WATCHDOG_MINOR,
186         .name           = "IXP4xx Watchdog",
187         .fops           = &ixp4xx_wdt_fops,
188 };
189
190 static int __init ixp4xx_wdt_init(void)
191 {
192         int ret;
193         unsigned long processor_id;
194
195         asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
196         if (!(processor_id & 0xf)) {
197                 printk("IXP4XXX Watchdog: Rev. A0 CPU detected - "
198                         "watchdog disabled\n");
199
200                 return -ENODEV;
201         }
202
203         ret = misc_register(&ixp4xx_wdt_miscdev);
204         if (ret == 0)
205                 printk("IXP4xx Watchdog Timer: heartbeat %d sec\n", heartbeat);
206
207         boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
208                         WDIOF_CARDRESET : 0;
209
210         return ret;
211 }
212
213 static void __exit ixp4xx_wdt_exit(void)
214 {
215         misc_deregister(&ixp4xx_wdt_miscdev);
216 }
217
218
219 module_init(ixp4xx_wdt_init);
220 module_exit(ixp4xx_wdt_exit);
221
222 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net">);
223 MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
224
225 module_param(heartbeat, int, 0);
226 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
227
228 module_param(nowayout, int, 0);
229 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
230
231 MODULE_LICENSE("GPL");
232 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
233