[WATCHDOG] Clean-up includes
[safe/jmp/linux-2.6] / drivers / watchdog / geodewdt.c
1 /* Watchdog timer for the Geode GX/LX with the CS5535/CS5536 companion chip
2  *
3  * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/types.h>
15 #include <linux/miscdevice.h>
16 #include <linux/watchdog.h>
17 #include <linux/fs.h>
18 #include <linux/platform_device.h>
19 #include <linux/reboot.h>
20 #include <linux/uaccess.h>
21
22 #include <asm/geode.h>
23
24 #define GEODEWDT_HZ 500
25 #define GEODEWDT_SCALE 6
26 #define GEODEWDT_MAX_SECONDS 131
27
28 #define WDT_FLAGS_OPEN 1
29 #define WDT_FLAGS_ORPHAN 2
30
31 #define DRV_NAME "geodewdt"
32 #define WATCHDOG_NAME "Geode GX/LX WDT"
33 #define WATCHDOG_TIMEOUT 60
34
35 static int timeout = WATCHDOG_TIMEOUT;
36 module_param(timeout, int, 0);
37 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=131, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
38
39 static int nowayout = WATCHDOG_NOWAYOUT;
40 module_param(nowayout, int, 0);
41 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
43 static struct platform_device *geodewdt_platform_device;
44 static unsigned long wdt_flags;
45 static int wdt_timer;
46 static int safe_close;
47
48 static void geodewdt_ping(void)
49 {
50         /* Stop the counter */
51         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
52
53         /* Reset the counter */
54         geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
55
56         /* Enable the counter */
57         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
58 }
59
60 static void geodewdt_disable(void)
61 {
62         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
63         geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
64 }
65
66 static int geodewdt_set_heartbeat(int val)
67 {
68         if (val < 1 || val > GEODEWDT_MAX_SECONDS)
69                 return -EINVAL;
70
71         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
72         geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
73         geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
74         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
75
76         timeout = val;
77         return 0;
78 }
79
80 static int
81 geodewdt_open(struct inode *inode, struct file *file)
82 {
83         if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
84                 return -EBUSY;
85
86         if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
87                 __module_get(THIS_MODULE);
88
89         geodewdt_ping();
90         return nonseekable_open(inode, file);
91 }
92
93 static int
94 geodewdt_release(struct inode *inode, struct file *file)
95 {
96         if (safe_close) {
97                 geodewdt_disable();
98                 module_put(THIS_MODULE);
99         }
100         else {
101                 printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
102                 geodewdt_ping();
103
104                 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
105         }
106
107         clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
108         safe_close = 0;
109         return 0;
110 }
111
112 static ssize_t
113 geodewdt_write(struct file *file, const char __user *data, size_t len,
114                loff_t *ppos)
115 {
116         if(len) {
117                 if (!nowayout) {
118                         size_t i;
119                         safe_close = 0;
120
121                         for (i = 0; i != len; i++) {
122                                 char c;
123
124                                 if (get_user(c, data + i))
125                                         return -EFAULT;
126
127                                 if (c == 'V')
128                                         safe_close = 1;
129                         }
130                 }
131
132                 geodewdt_ping();
133         }
134         return len;
135 }
136
137 static int
138 geodewdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
139                unsigned long arg)
140 {
141         void __user *argp = (void __user *)arg;
142         int __user *p = argp;
143         int interval;
144
145         static struct watchdog_info ident = {
146                 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
147                 | WDIOF_MAGICCLOSE,
148                 .firmware_version =     1,
149                 .identity =             WATCHDOG_NAME,
150         };
151
152         switch(cmd) {
153         case WDIOC_GETSUPPORT:
154                 return copy_to_user(argp, &ident,
155                                     sizeof(ident)) ? -EFAULT : 0;
156                 break;
157
158         case WDIOC_GETSTATUS:
159         case WDIOC_GETBOOTSTATUS:
160                 return put_user(0, p);
161
162         case WDIOC_KEEPALIVE:
163                 geodewdt_ping();
164                 return 0;
165
166         case WDIOC_SETTIMEOUT:
167                 if (get_user(interval, p))
168                         return -EFAULT;
169
170                 if (geodewdt_set_heartbeat(interval))
171                         return -EINVAL;
172
173 /* Fall through */
174
175         case WDIOC_GETTIMEOUT:
176                 return put_user(timeout, p);
177
178         case WDIOC_SETOPTIONS:
179         {
180                 int options, ret = -EINVAL;
181
182                 if (get_user(options, p))
183                         return -EFAULT;
184
185                 if (options & WDIOS_DISABLECARD) {
186                         geodewdt_disable();
187                         ret = 0;
188                 }
189
190                 if (options & WDIOS_ENABLECARD) {
191                         geodewdt_ping();
192                         ret = 0;
193                 }
194
195                 return ret;
196         }
197         default:
198                 return -ENOTTY;
199         }
200
201         return 0;
202 }
203
204 static const struct file_operations geodewdt_fops = {
205         .owner          = THIS_MODULE,
206         .llseek         = no_llseek,
207         .write          = geodewdt_write,
208         .ioctl          = geodewdt_ioctl,
209         .open           = geodewdt_open,
210         .release        = geodewdt_release,
211 };
212
213 static struct miscdevice geodewdt_miscdev = {
214         .minor = WATCHDOG_MINOR,
215         .name = "watchdog",
216         .fops = &geodewdt_fops
217 };
218
219 static int __devinit
220 geodewdt_probe(struct platform_device *dev)
221 {
222         int ret, timer;
223
224         timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
225
226         if (timer == -1) {
227                 printk(KERN_ERR "geodewdt:  No timers were available\n");
228                 return -ENODEV;
229         }
230
231         wdt_timer = timer;
232
233         /* Set up the timer */
234
235         geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
236                           GEODEWDT_SCALE | (3 << 8));
237
238         /* Set up comparator 2 to reset when the event fires */
239         geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
240
241         /* Set up the initial timeout */
242
243         geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
244                 timeout * GEODEWDT_HZ);
245
246         ret = misc_register(&geodewdt_miscdev);
247
248         return ret;
249 }
250
251 static int __devexit
252 geodewdt_remove(struct platform_device *dev)
253 {
254         misc_deregister(&geodewdt_miscdev);
255         return 0;
256 }
257
258 static void
259 geodewdt_shutdown(struct platform_device *dev)
260 {
261         geodewdt_disable();
262 }
263
264 static struct platform_driver geodewdt_driver = {
265         .probe          = geodewdt_probe,
266         .remove         = __devexit_p(geodewdt_remove),
267         .shutdown       = geodewdt_shutdown,
268         .driver         = {
269                 .owner  = THIS_MODULE,
270                 .name   = DRV_NAME,
271         },
272 };
273
274 static int __init
275 geodewdt_init(void)
276 {
277         int ret;
278
279         ret = platform_driver_register(&geodewdt_driver);
280         if (ret)
281                 return ret;
282
283         geodewdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
284         if (IS_ERR(geodewdt_platform_device)) {
285                 ret = PTR_ERR(geodewdt_platform_device);
286                 goto err;
287         }
288
289         return 0;
290 err:
291         platform_driver_unregister(&geodewdt_driver);
292         return ret;
293 }
294
295 static void __exit
296 geodewdt_exit(void)
297 {
298         platform_device_unregister(geodewdt_platform_device);
299         platform_driver_unregister(&geodewdt_driver);
300 }
301
302 module_init(geodewdt_init);
303 module_exit(geodewdt_exit);
304
305 MODULE_AUTHOR("Advanced Micro Devices, Inc");
306 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);