Linux 2.6.26-rc6
[linux-2.6/kmemtrace.git] / drivers / watchdog / geodewdt.c
blob30d09cbbad949beeeee573bc0a31724dc38d8619
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>
21 #include <asm/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
81 geodewdt_open(struct inode *inode, struct file *file)
83 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
84 return -EBUSY;
86 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
87 __module_get(THIS_MODULE);
89 geodewdt_ping();
90 return nonseekable_open(inode, file);
93 static int
94 geodewdt_release(struct inode *inode, struct file *file)
96 if (safe_close) {
97 geodewdt_disable();
98 module_put(THIS_MODULE);
100 else {
101 printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
102 geodewdt_ping();
104 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
107 clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
108 safe_close = 0;
109 return 0;
112 static ssize_t
113 geodewdt_write(struct file *file, const char __user *data, size_t len,
114 loff_t *ppos)
116 if(len) {
117 if (!nowayout) {
118 size_t i;
119 safe_close = 0;
121 for (i = 0; i != len; i++) {
122 char c;
124 if (get_user(c, data + i))
125 return -EFAULT;
127 if (c == 'V')
128 safe_close = 1;
132 geodewdt_ping();
134 return len;
137 static int
138 geodewdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
139 unsigned long arg)
141 void __user *argp = (void __user *)arg;
142 int __user *p = argp;
143 int interval;
145 static struct watchdog_info ident = {
146 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
147 | WDIOF_MAGICCLOSE,
148 .firmware_version = 1,
149 .identity = WATCHDOG_NAME,
152 switch(cmd) {
153 case WDIOC_GETSUPPORT:
154 return copy_to_user(argp, &ident,
155 sizeof(ident)) ? -EFAULT : 0;
156 break;
158 case WDIOC_GETSTATUS:
159 case WDIOC_GETBOOTSTATUS:
160 return put_user(0, p);
162 case WDIOC_KEEPALIVE:
163 geodewdt_ping();
164 return 0;
166 case WDIOC_SETTIMEOUT:
167 if (get_user(interval, p))
168 return -EFAULT;
170 if (geodewdt_set_heartbeat(interval))
171 return -EINVAL;
173 /* Fall through */
175 case WDIOC_GETTIMEOUT:
176 return put_user(timeout, p);
178 case WDIOC_SETOPTIONS:
180 int options, ret = -EINVAL;
182 if (get_user(options, p))
183 return -EFAULT;
185 if (options & WDIOS_DISABLECARD) {
186 geodewdt_disable();
187 ret = 0;
190 if (options & WDIOS_ENABLECARD) {
191 geodewdt_ping();
192 ret = 0;
195 return ret;
197 default:
198 return -ENOTTY;
201 return 0;
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,
213 static struct miscdevice geodewdt_miscdev = {
214 .minor = WATCHDOG_MINOR,
215 .name = "watchdog",
216 .fops = &geodewdt_fops
219 static int __devinit
220 geodewdt_probe(struct platform_device *dev)
222 int ret, timer;
224 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
226 if (timer == -1) {
227 printk(KERN_ERR "geodewdt: No timers were available\n");
228 return -ENODEV;
231 wdt_timer = timer;
233 /* Set up the timer */
235 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
236 GEODEWDT_SCALE | (3 << 8));
238 /* Set up comparator 2 to reset when the event fires */
239 geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
241 /* Set up the initial timeout */
243 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
244 timeout * GEODEWDT_HZ);
246 ret = misc_register(&geodewdt_miscdev);
248 return ret;
251 static int __devexit
252 geodewdt_remove(struct platform_device *dev)
254 misc_deregister(&geodewdt_miscdev);
255 return 0;
258 static void
259 geodewdt_shutdown(struct platform_device *dev)
261 geodewdt_disable();
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,
274 static int __init
275 geodewdt_init(void)
277 int ret;
279 ret = platform_driver_register(&geodewdt_driver);
280 if (ret)
281 return ret;
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;
289 return 0;
290 err:
291 platform_driver_unregister(&geodewdt_driver);
292 return ret;
295 static void __exit
296 geodewdt_exit(void)
298 platform_device_unregister(geodewdt_platform_device);
299 platform_driver_unregister(&geodewdt_driver);
302 module_init(geodewdt_init);
303 module_exit(geodewdt_exit);
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);