[WATCHDOG] Add a watchdog driver based on the CS5535/CS5536 MFGPT timers
[deliverable/linux.git] / drivers / watchdog / geodewdt.c
CommitLineData
0b36086b
JC
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
21#include <asm/uaccess.h>
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
35static int timeout = WATCHDOG_TIMEOUT;
36module_param(timeout, int, 0);
37MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=131, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
38
39static int nowayout = WATCHDOG_NOWAYOUT;
40module_param(nowayout, int, 0);
41MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
43static struct platform_device *geodewdt_platform_device;
44static unsigned long wdt_flags;
45static int wdt_timer;
46static int safe_close;
47
48static 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
60static 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
66static 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
80static int
81geodewdt_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
93static int
94geodewdt_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
112static ssize_t
113geodewdt_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
137static int
138geodewdt_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
204static 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
213static struct miscdevice geodewdt_miscdev = {
214 .minor = WATCHDOG_MINOR,
215 .name = "watchdog",
216 .fops = &geodewdt_fops
217};
218
219static int __devinit
220geodewdt_probe(struct platform_device *dev)
221{
222 int ret, timer;
223
224 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY,
225 MFGPT_DOMAIN_WORKING, THIS_MODULE);
226
227 if (timer == -1) {
228 printk(KERN_ERR "geodewdt: No timers were available\n");
229 return -ENODEV;
230 }
231
232 wdt_timer = timer;
233
234 /* Set up the timer */
235
236 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
237 GEODEWDT_SCALE | (3 << 8));
238
239 /* Set up comparator 2 to reset when the event fires */
240 geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
241
242 /* Set up the initial timeout */
243
244 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
245 timeout * GEODEWDT_HZ);
246
247 ret = misc_register(&geodewdt_miscdev);
248
249 return ret;
250}
251
252static int __devexit
253geodewdt_remove(struct platform_device *dev)
254{
255 misc_deregister(&geodewdt_miscdev);
256 return 0;
257}
258
259static void
260geodewdt_shutdown(struct platform_device *dev)
261{
262 geodewdt_disable();
263}
264
265static struct platform_driver geodewdt_driver = {
266 .probe = geodewdt_probe,
267 .remove = __devexit_p(geodewdt_remove),
268 .shutdown = geodewdt_shutdown,
269 .driver = {
270 .owner = THIS_MODULE,
271 .name = DRV_NAME,
272 },
273};
274
275static int __init
276geodewdt_init(void)
277{
278 int ret;
279
280 ret = platform_driver_register(&geodewdt_driver);
281 if (ret)
282 return ret;
283
284 geodewdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
285 if (IS_ERR(geodewdt_platform_device)) {
286 ret = PTR_ERR(geodewdt_platform_device);
287 goto err;
288 }
289
290 return 0;
291err:
292 platform_driver_unregister(&geodewdt_driver);
293 return ret;
294}
295
296static void __exit
297geodewdt_exit(void)
298{
299 platform_device_unregister(geodewdt_platform_device);
300 platform_driver_unregister(&geodewdt_driver);
301}
302
303module_init(geodewdt_init);
304module_exit(geodewdt_exit);
305
306MODULE_AUTHOR("Advanced Micro Devices, Inc");
307MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
308MODULE_LICENSE("GPL");
309MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
This page took 0.036313 seconds and 5 git commands to generate.