blob: 614a5c7017b64a68022ab936281aad01c8db832b [file] [log] [blame]
Jordan Crouse0b360862008-01-21 10:07:00 -07001/* 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>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000020#include <linux/uaccess.h>
Jordan Crouse0b360862008-01-21 10:07:00 -070021
Jordan Crouse0b360862008-01-21 10:07:00 -070022#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
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000080static int geodewdt_open(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070081{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000082 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
83 return -EBUSY;
Jordan Crouse0b360862008-01-21 10:07:00 -070084
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000085 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
86 __module_get(THIS_MODULE);
Jordan Crouse0b360862008-01-21 10:07:00 -070087
88 geodewdt_ping();
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000089 return nonseekable_open(inode, file);
Jordan Crouse0b360862008-01-21 10:07:00 -070090}
91
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000092static int geodewdt_release(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070093{
94 if (safe_close) {
95 geodewdt_disable();
96 module_put(THIS_MODULE);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000097 } else {
Jordan Crouse0b360862008-01-21 10:07:00 -070098 printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
99 geodewdt_ping();
100
101 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
102 }
103
104 clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
105 safe_close = 0;
106 return 0;
107}
108
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000109static ssize_t geodewdt_write(struct file *file, const char __user *data,
110 size_t len, loff_t *ppos)
Jordan Crouse0b360862008-01-21 10:07:00 -0700111{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000112 if (len) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700113 if (!nowayout) {
114 size_t i;
115 safe_close = 0;
116
117 for (i = 0; i != len; i++) {
118 char c;
119
120 if (get_user(c, data + i))
121 return -EFAULT;
122
123 if (c == 'V')
124 safe_close = 1;
125 }
126 }
127
128 geodewdt_ping();
129 }
130 return len;
131}
132
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000133static int geodewdt_ioctl(struct inode *inode, struct file *file,
134 unsigned int cmd, unsigned long arg)
Jordan Crouse0b360862008-01-21 10:07:00 -0700135{
136 void __user *argp = (void __user *)arg;
137 int __user *p = argp;
138 int interval;
139
140 static struct watchdog_info ident = {
141 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
142 | WDIOF_MAGICCLOSE,
143 .firmware_version = 1,
144 .identity = WATCHDOG_NAME,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000145 };
Jordan Crouse0b360862008-01-21 10:07:00 -0700146
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000147 switch (cmd) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700148 case WDIOC_GETSUPPORT:
149 return copy_to_user(argp, &ident,
150 sizeof(ident)) ? -EFAULT : 0;
151 break;
152
153 case WDIOC_GETSTATUS:
154 case WDIOC_GETBOOTSTATUS:
155 return put_user(0, p);
156
Jordan Crouse0b360862008-01-21 10:07:00 -0700157 case WDIOC_SETOPTIONS:
158 {
159 int options, ret = -EINVAL;
160
161 if (get_user(options, p))
162 return -EFAULT;
163
164 if (options & WDIOS_DISABLECARD) {
165 geodewdt_disable();
166 ret = 0;
167 }
168
169 if (options & WDIOS_ENABLECARD) {
170 geodewdt_ping();
171 ret = 0;
172 }
173
174 return ret;
175 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000176 case WDIOC_KEEPALIVE:
177 geodewdt_ping();
178 return 0;
179
180 case WDIOC_SETTIMEOUT:
181 if (get_user(interval, p))
182 return -EFAULT;
183
184 if (geodewdt_set_heartbeat(interval))
185 return -EINVAL;
186 /* Fall through */
187 case WDIOC_GETTIMEOUT:
188 return put_user(timeout, p);
189
Jordan Crouse0b360862008-01-21 10:07:00 -0700190 default:
191 return -ENOTTY;
192 }
193
194 return 0;
195}
196
197static const struct file_operations geodewdt_fops = {
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000198 .owner = THIS_MODULE,
199 .llseek = no_llseek,
200 .write = geodewdt_write,
201 .ioctl = geodewdt_ioctl,
202 .open = geodewdt_open,
203 .release = geodewdt_release,
Jordan Crouse0b360862008-01-21 10:07:00 -0700204};
205
206static struct miscdevice geodewdt_miscdev = {
207 .minor = WATCHDOG_MINOR,
208 .name = "watchdog",
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000209 .fops = &geodewdt_fops,
Jordan Crouse0b360862008-01-21 10:07:00 -0700210};
211
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000212static int __devinit geodewdt_probe(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700213{
214 int ret, timer;
215
Ingo Molnar57f50ca2008-05-30 17:02:50 +0200216 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
Jordan Crouse0b360862008-01-21 10:07:00 -0700217
218 if (timer == -1) {
219 printk(KERN_ERR "geodewdt: No timers were available\n");
220 return -ENODEV;
221 }
222
223 wdt_timer = timer;
224
225 /* Set up the timer */
226
227 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
228 GEODEWDT_SCALE | (3 << 8));
229
230 /* Set up comparator 2 to reset when the event fires */
231 geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
232
233 /* Set up the initial timeout */
234
235 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
236 timeout * GEODEWDT_HZ);
237
238 ret = misc_register(&geodewdt_miscdev);
239
240 return ret;
241}
242
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000243static int __devexit geodewdt_remove(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700244{
245 misc_deregister(&geodewdt_miscdev);
246 return 0;
247}
248
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000249static void geodewdt_shutdown(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700250{
251 geodewdt_disable();
252}
253
254static 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,
261 },
262};
263
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000264static int __init geodewdt_init(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700265{
266 int ret;
267
268 ret = platform_driver_register(&geodewdt_driver);
269 if (ret)
270 return ret;
271
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;
276 }
277
278 return 0;
279err:
280 platform_driver_unregister(&geodewdt_driver);
281 return ret;
282}
283
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000284static void __exit geodewdt_exit(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700285{
286 platform_device_unregister(geodewdt_platform_device);
287 platform_driver_unregister(&geodewdt_driver);
288}
289
290module_init(geodewdt_init);
291module_exit(geodewdt_exit);
292
293MODULE_AUTHOR("Advanced Micro Devices, Inc");
294MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
295MODULE_LICENSE("GPL");
296MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);