Merge master.kernel.org:/pub/scm/linux/kernel/git/dwmw2/audit-2.6
[linux-2.6] / 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 #include <linux/bitops.h>
26
27 #include <asm/hardware.h>
28 #include <asm/uaccess.h>
29
30 static int nowayout = WATCHDOG_NOWAYOUT;
31 static int heartbeat = 60;      /* (secs) Default is 1 minute */
32 static unsigned long wdt_status;
33 static unsigned long boot_status;
34
35 #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
36
37 #define WDT_IN_USE              0
38 #define WDT_OK_TO_CLOSE         1
39
40 static void
41 wdt_enable(void)
42 {
43         *IXP4XX_OSWK = IXP4XX_WDT_KEY;
44         *IXP4XX_OSWE = 0;
45         *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
46         *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
47         *IXP4XX_OSWK = 0;
48 }
49
50 static void
51 wdt_disable(void)
52 {
53         *IXP4XX_OSWK = IXP4XX_WDT_KEY;
54         *IXP4XX_OSWE = 0;
55         *IXP4XX_OSWK = 0;
56 }
57
58 static int
59 ixp4xx_wdt_open(struct inode *inode, struct file *file)
60 {
61         if (test_and_set_bit(WDT_IN_USE, &wdt_status))
62                 return -EBUSY;
63
64         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
65
66         wdt_enable();
67
68         return nonseekable_open(inode, file);
69 }
70
71 static ssize_t
72 ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
73 {
74         if (len) {
75                 if (!nowayout) {
76                         size_t i;
77
78                         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
79
80                         for (i = 0; i != len; i++) {
81                                 char c;
82
83                                 if (get_user(c, data + i))
84                                         return -EFAULT;
85                                 if (c == 'V')
86                                         set_bit(WDT_OK_TO_CLOSE, &wdt_status);
87                         }
88                 }
89                 wdt_enable();
90         }
91
92         return len;
93 }
94
95 static struct watchdog_info ident = {
96         .options        = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
97                           WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
98         .identity       = "IXP4xx Watchdog",
99 };
100
101
102 static int
103 ixp4xx_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
104                         unsigned long arg)
105 {
106         int ret = -ENOIOCTLCMD;
107         int time;
108
109         switch (cmd) {
110         case WDIOC_GETSUPPORT:
111                 ret = copy_to_user((struct watchdog_info *)arg, &ident,
112                                    sizeof(ident)) ? -EFAULT : 0;
113                 break;
114
115         case WDIOC_GETSTATUS:
116                 ret = put_user(0, (int *)arg);
117                 break;
118
119         case WDIOC_GETBOOTSTATUS:
120                 ret = put_user(boot_status, (int *)arg);
121                 break;
122
123         case WDIOC_SETTIMEOUT:
124                 ret = get_user(time, (int *)arg);
125                 if (ret)
126                         break;
127
128                 if (time <= 0 || time > 60) {
129                         ret = -EINVAL;
130                         break;
131                 }
132
133                 heartbeat = time;
134                 wdt_enable();
135                 /* Fall through */
136
137         case WDIOC_GETTIMEOUT:
138                 ret = put_user(heartbeat, (int *)arg);
139                 break;
140
141         case WDIOC_KEEPALIVE:
142                 wdt_enable();
143                 ret = 0;
144                 break;
145         }
146         return ret;
147 }
148
149 static int
150 ixp4xx_wdt_release(struct inode *inode, struct file *file)
151 {
152         if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
153                 wdt_disable();
154         } else {
155                 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
156                                         "timer will not stop\n");
157         }
158
159         clear_bit(WDT_IN_USE, &wdt_status);
160         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
161
162         return 0;
163 }
164
165
166 static struct file_operations ixp4xx_wdt_fops =
167 {
168         .owner          = THIS_MODULE,
169         .llseek         = no_llseek,
170         .write          = ixp4xx_wdt_write,
171         .ioctl          = ixp4xx_wdt_ioctl,
172         .open           = ixp4xx_wdt_open,
173         .release        = ixp4xx_wdt_release,
174 };
175
176 static struct miscdevice ixp4xx_wdt_miscdev =
177 {
178         .minor          = WATCHDOG_MINOR,
179         .name           = "watchdog",
180         .fops           = &ixp4xx_wdt_fops,
181 };
182
183 static int __init ixp4xx_wdt_init(void)
184 {
185         int ret;
186         unsigned long processor_id;
187
188         asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
189         if (!(processor_id & 0xf)) {
190                 printk("IXP4XXX Watchdog: Rev. A0 CPU detected - "
191                         "watchdog disabled\n");
192
193                 return -ENODEV;
194         }
195
196         ret = misc_register(&ixp4xx_wdt_miscdev);
197         if (ret == 0)
198                 printk("IXP4xx Watchdog Timer: heartbeat %d sec\n", heartbeat);
199
200         boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
201                         WDIOF_CARDRESET : 0;
202
203         return ret;
204 }
205
206 static void __exit ixp4xx_wdt_exit(void)
207 {
208         misc_deregister(&ixp4xx_wdt_miscdev);
209 }
210
211
212 module_init(ixp4xx_wdt_init);
213 module_exit(ixp4xx_wdt_exit);
214
215 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
216 MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
217
218 module_param(heartbeat, int, 0);
219 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
220
221 module_param(nowayout, int, 0);
222 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
223
224 MODULE_LICENSE("GPL");
225 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
226