mm owner: fix race between swapoff and exit
[linux-2.6/next.git] / drivers / watchdog / geodewdt.c
blob6799a6de66fe87d1ec01499278bb9b92f5683385
1 /* Watchdog timer for the Geode GX/LX with the CS5535/CS5536 companion chip
3 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
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 */
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>
22 #include <asm/geode.h>
24 #define GEODEWDT_HZ 500
25 #define GEODEWDT_SCALE 6
26 #define GEODEWDT_MAX_SECONDS 131
28 #define WDT_FLAGS_OPEN 1
29 #define WDT_FLAGS_ORPHAN 2
31 #define DRV_NAME "geodewdt"
32 #define WATCHDOG_NAME "Geode GX/LX WDT"
33 #define WATCHDOG_TIMEOUT 60
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) ".");
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) ")");
43 static struct platform_device *geodewdt_platform_device;
44 static unsigned long wdt_flags;
45 static int wdt_timer;
46 static int safe_close;
48 static void geodewdt_ping(void)
50 /* Stop the counter */
51 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
53 /* Reset the counter */
54 geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
56 /* Enable the counter */
57 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
60 static void geodewdt_disable(void)
62 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
63 geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
66 static int geodewdt_set_heartbeat(int val)
68 if (val < 1 || val > GEODEWDT_MAX_SECONDS)
69 return -EINVAL;
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);
76 timeout = val;
77 return 0;
80 static int geodewdt_open(struct inode *inode, struct file *file)
82 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
83 return -EBUSY;
85 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
86 __module_get(THIS_MODULE);
88 geodewdt_ping();
89 return nonseekable_open(inode, file);
92 static int geodewdt_release(struct inode *inode, struct file *file)
94 if (safe_close) {
95 geodewdt_disable();
96 module_put(THIS_MODULE);
97 } else {
98 printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
99 geodewdt_ping();
101 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
104 clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
105 safe_close = 0;
106 return 0;
109 static ssize_t geodewdt_write(struct file *file, const char __user *data,
110 size_t len, loff_t *ppos)
112 if (len) {
113 if (!nowayout) {
114 size_t i;
115 safe_close = 0;
117 for (i = 0; i != len; i++) {
118 char c;
120 if (get_user(c, data + i))
121 return -EFAULT;
123 if (c == 'V')
124 safe_close = 1;
128 geodewdt_ping();
130 return len;
133 static long geodewdt_ioctl(struct file *file, unsigned int cmd,
134 unsigned long arg)
136 void __user *argp = (void __user *)arg;
137 int __user *p = argp;
138 int interval;
140 static struct watchdog_info ident = {
141 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
142 | WDIOF_MAGICCLOSE,
143 .firmware_version = 1,
144 .identity = WATCHDOG_NAME,
147 switch (cmd) {
148 case WDIOC_GETSUPPORT:
149 return copy_to_user(argp, &ident,
150 sizeof(ident)) ? -EFAULT : 0;
151 break;
153 case WDIOC_GETSTATUS:
154 case WDIOC_GETBOOTSTATUS:
155 return put_user(0, p);
157 case WDIOC_SETOPTIONS:
159 int options, ret = -EINVAL;
161 if (get_user(options, p))
162 return -EFAULT;
164 if (options & WDIOS_DISABLECARD) {
165 geodewdt_disable();
166 ret = 0;
169 if (options & WDIOS_ENABLECARD) {
170 geodewdt_ping();
171 ret = 0;
174 return ret;
176 case WDIOC_KEEPALIVE:
177 geodewdt_ping();
178 return 0;
180 case WDIOC_SETTIMEOUT:
181 if (get_user(interval, p))
182 return -EFAULT;
184 if (geodewdt_set_heartbeat(interval))
185 return -EINVAL;
186 /* Fall through */
187 case WDIOC_GETTIMEOUT:
188 return put_user(timeout, p);
190 default:
191 return -ENOTTY;
194 return 0;
197 static const struct file_operations geodewdt_fops = {
198 .owner = THIS_MODULE,
199 .llseek = no_llseek,
200 .write = geodewdt_write,
201 .unlocked_ioctl = geodewdt_ioctl,
202 .open = geodewdt_open,
203 .release = geodewdt_release,
206 static struct miscdevice geodewdt_miscdev = {
207 .minor = WATCHDOG_MINOR,
208 .name = "watchdog",
209 .fops = &geodewdt_fops,
212 static int __devinit geodewdt_probe(struct platform_device *dev)
214 int ret, timer;
216 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
218 if (timer == -1) {
219 printk(KERN_ERR "geodewdt: No timers were available\n");
220 return -ENODEV;
223 wdt_timer = timer;
225 /* Set up the timer */
227 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
228 GEODEWDT_SCALE | (3 << 8));
230 /* Set up comparator 2 to reset when the event fires */
231 geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
233 /* Set up the initial timeout */
235 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
236 timeout * GEODEWDT_HZ);
238 ret = misc_register(&geodewdt_miscdev);
240 return ret;
243 static int __devexit geodewdt_remove(struct platform_device *dev)
245 misc_deregister(&geodewdt_miscdev);
246 return 0;
249 static void geodewdt_shutdown(struct platform_device *dev)
251 geodewdt_disable();
254 static struct platform_driver geodewdt_driver = {
255 .probe = geodewdt_probe,
256 .remove = __devexit_p(geodewdt_remove),
257 .shutdown = geodewdt_shutdown,
258 .driver = {
259 .owner = THIS_MODULE,
260 .name = DRV_NAME,
264 static int __init geodewdt_init(void)
266 int ret;
268 ret = platform_driver_register(&geodewdt_driver);
269 if (ret)
270 return ret;
272 geodewdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
273 if (IS_ERR(geodewdt_platform_device)) {
274 ret = PTR_ERR(geodewdt_platform_device);
275 goto err;
278 return 0;
279 err:
280 platform_driver_unregister(&geodewdt_driver);
281 return ret;
284 static void __exit geodewdt_exit(void)
286 platform_device_unregister(geodewdt_platform_device);
287 platform_driver_unregister(&geodewdt_driver);
290 module_init(geodewdt_init);
291 module_exit(geodewdt_exit);
293 MODULE_AUTHOR("Advanced Micro Devices, Inc");
294 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);