Commit | Line | Data |
---|---|---|
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> | |
089ab079 | 20 | #include <linux/uaccess.h> |
0b36086b | 21 | |
0b36086b JC |
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 | ||
5eb82498 | 152 | switch (cmd) { |
0b36086b JC |
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 | ||
0b36086b JC |
162 | case WDIOC_SETOPTIONS: |
163 | { | |
164 | int options, ret = -EINVAL; | |
165 | ||
166 | if (get_user(options, p)) | |
167 | return -EFAULT; | |
168 | ||
169 | if (options & WDIOS_DISABLECARD) { | |
170 | geodewdt_disable(); | |
171 | ret = 0; | |
172 | } | |
173 | ||
174 | if (options & WDIOS_ENABLECARD) { | |
175 | geodewdt_ping(); | |
176 | ret = 0; | |
177 | } | |
178 | ||
179 | return ret; | |
180 | } | |
0c06090c WVS |
181 | case WDIOC_KEEPALIVE: |
182 | geodewdt_ping(); | |
183 | return 0; | |
184 | ||
185 | case WDIOC_SETTIMEOUT: | |
186 | if (get_user(interval, p)) | |
187 | return -EFAULT; | |
188 | ||
189 | if (geodewdt_set_heartbeat(interval)) | |
190 | return -EINVAL; | |
191 | /* Fall through */ | |
192 | case WDIOC_GETTIMEOUT: | |
193 | return put_user(timeout, p); | |
194 | ||
0b36086b JC |
195 | default: |
196 | return -ENOTTY; | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
202 | static const struct file_operations geodewdt_fops = { | |
203 | .owner = THIS_MODULE, | |
204 | .llseek = no_llseek, | |
205 | .write = geodewdt_write, | |
206 | .ioctl = geodewdt_ioctl, | |
207 | .open = geodewdt_open, | |
208 | .release = geodewdt_release, | |
209 | }; | |
210 | ||
211 | static struct miscdevice geodewdt_miscdev = { | |
212 | .minor = WATCHDOG_MINOR, | |
213 | .name = "watchdog", | |
214 | .fops = &geodewdt_fops | |
215 | }; | |
216 | ||
217 | static int __devinit | |
218 | geodewdt_probe(struct platform_device *dev) | |
219 | { | |
220 | int ret, timer; | |
221 | ||
57f50ca1 | 222 | timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING); |
0b36086b JC |
223 | |
224 | if (timer == -1) { | |
225 | printk(KERN_ERR "geodewdt: No timers were available\n"); | |
226 | return -ENODEV; | |
227 | } | |
228 | ||
229 | wdt_timer = timer; | |
230 | ||
231 | /* Set up the timer */ | |
232 | ||
233 | geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, | |
234 | GEODEWDT_SCALE | (3 << 8)); | |
235 | ||
236 | /* Set up comparator 2 to reset when the event fires */ | |
237 | geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1); | |
238 | ||
239 | /* Set up the initial timeout */ | |
240 | ||
241 | geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, | |
242 | timeout * GEODEWDT_HZ); | |
243 | ||
244 | ret = misc_register(&geodewdt_miscdev); | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
249 | static int __devexit | |
250 | geodewdt_remove(struct platform_device *dev) | |
251 | { | |
252 | misc_deregister(&geodewdt_miscdev); | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static void | |
257 | geodewdt_shutdown(struct platform_device *dev) | |
258 | { | |
259 | geodewdt_disable(); | |
260 | } | |
261 | ||
262 | static struct platform_driver geodewdt_driver = { | |
263 | .probe = geodewdt_probe, | |
264 | .remove = __devexit_p(geodewdt_remove), | |
265 | .shutdown = geodewdt_shutdown, | |
266 | .driver = { | |
267 | .owner = THIS_MODULE, | |
268 | .name = DRV_NAME, | |
269 | }, | |
270 | }; | |
271 | ||
272 | static int __init | |
273 | geodewdt_init(void) | |
274 | { | |
275 | int ret; | |
276 | ||
277 | ret = platform_driver_register(&geodewdt_driver); | |
278 | if (ret) | |
279 | return ret; | |
280 | ||
281 | geodewdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0); | |
282 | if (IS_ERR(geodewdt_platform_device)) { | |
283 | ret = PTR_ERR(geodewdt_platform_device); | |
284 | goto err; | |
285 | } | |
286 | ||
287 | return 0; | |
288 | err: | |
289 | platform_driver_unregister(&geodewdt_driver); | |
290 | return ret; | |
291 | } | |
292 | ||
293 | static void __exit | |
294 | geodewdt_exit(void) | |
295 | { | |
296 | platform_device_unregister(geodewdt_platform_device); | |
297 | platform_driver_unregister(&geodewdt_driver); | |
298 | } | |
299 | ||
300 | module_init(geodewdt_init); | |
301 | module_exit(geodewdt_exit); | |
302 | ||
303 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); | |
304 | MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver"); | |
305 | MODULE_LICENSE("GPL"); | |
306 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |