blob: 9acf0015a1e747131cbc20d42527a8ccc629b867 [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);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000037MODULE_PARM_DESC(timeout,
38 "Watchdog timeout in seconds. 1<= timeout <=131, default="
39 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
Jordan Crouse0b360862008-01-21 10:07:00 -070040
41static int nowayout = WATCHDOG_NOWAYOUT;
42module_param(nowayout, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000043MODULE_PARM_DESC(nowayout,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Jordan Crouse0b360862008-01-21 10:07:00 -070046
47static struct platform_device *geodewdt_platform_device;
48static unsigned long wdt_flags;
49static int wdt_timer;
50static int safe_close;
51
52static void geodewdt_ping(void)
53{
54 /* Stop the counter */
55 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
56
57 /* Reset the counter */
58 geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
59
60 /* Enable the counter */
61 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
62}
63
64static void geodewdt_disable(void)
65{
66 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
67 geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
68}
69
70static int geodewdt_set_heartbeat(int val)
71{
72 if (val < 1 || val > GEODEWDT_MAX_SECONDS)
73 return -EINVAL;
74
75 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
76 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
77 geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
78 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
79
80 timeout = val;
81 return 0;
82}
83
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000084static int geodewdt_open(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070085{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000086 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
87 return -EBUSY;
Jordan Crouse0b360862008-01-21 10:07:00 -070088
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000089 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
90 __module_get(THIS_MODULE);
Jordan Crouse0b360862008-01-21 10:07:00 -070091
92 geodewdt_ping();
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000093 return nonseekable_open(inode, file);
Jordan Crouse0b360862008-01-21 10:07:00 -070094}
95
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000096static int geodewdt_release(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070097{
98 if (safe_close) {
99 geodewdt_disable();
100 module_put(THIS_MODULE);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000101 } else {
Jordan Crouse0b360862008-01-21 10:07:00 -0700102 printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
103 geodewdt_ping();
104
105 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
106 }
107
108 clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
109 safe_close = 0;
110 return 0;
111}
112
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000113static ssize_t geodewdt_write(struct file *file, const char __user *data,
114 size_t len, loff_t *ppos)
Jordan Crouse0b360862008-01-21 10:07:00 -0700115{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000116 if (len) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700117 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
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000137static long geodewdt_ioctl(struct file *file, unsigned int cmd,
138 unsigned long arg)
Jordan Crouse0b360862008-01-21 10:07:00 -0700139{
140 void __user *argp = (void __user *)arg;
141 int __user *p = argp;
142 int interval;
143
144 static struct watchdog_info ident = {
145 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
146 | WDIOF_MAGICCLOSE,
147 .firmware_version = 1,
148 .identity = WATCHDOG_NAME,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000149 };
Jordan Crouse0b360862008-01-21 10:07:00 -0700150
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000151 switch (cmd) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700152 case WDIOC_GETSUPPORT:
153 return copy_to_user(argp, &ident,
154 sizeof(ident)) ? -EFAULT : 0;
155 break;
156
157 case WDIOC_GETSTATUS:
158 case WDIOC_GETBOOTSTATUS:
159 return put_user(0, p);
160
Jordan Crouse0b360862008-01-21 10:07:00 -0700161 case WDIOC_SETOPTIONS:
162 {
163 int options, ret = -EINVAL;
164
165 if (get_user(options, p))
166 return -EFAULT;
167
168 if (options & WDIOS_DISABLECARD) {
169 geodewdt_disable();
170 ret = 0;
171 }
172
173 if (options & WDIOS_ENABLECARD) {
174 geodewdt_ping();
175 ret = 0;
176 }
177
178 return ret;
179 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000180 case WDIOC_KEEPALIVE:
181 geodewdt_ping();
182 return 0;
183
184 case WDIOC_SETTIMEOUT:
185 if (get_user(interval, p))
186 return -EFAULT;
187
188 if (geodewdt_set_heartbeat(interval))
189 return -EINVAL;
190 /* Fall through */
191 case WDIOC_GETTIMEOUT:
192 return put_user(timeout, p);
193
Jordan Crouse0b360862008-01-21 10:07:00 -0700194 default:
195 return -ENOTTY;
196 }
197
198 return 0;
199}
200
201static const struct file_operations geodewdt_fops = {
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000202 .owner = THIS_MODULE,
203 .llseek = no_llseek,
204 .write = geodewdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000205 .unlocked_ioctl = geodewdt_ioctl,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000206 .open = geodewdt_open,
207 .release = geodewdt_release,
Jordan Crouse0b360862008-01-21 10:07:00 -0700208};
209
210static struct miscdevice geodewdt_miscdev = {
211 .minor = WATCHDOG_MINOR,
212 .name = "watchdog",
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000213 .fops = &geodewdt_fops,
Jordan Crouse0b360862008-01-21 10:07:00 -0700214};
215
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000216static int __devinit geodewdt_probe(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700217{
218 int ret, timer;
219
Ingo Molnar57f50ca2008-05-30 17:02:50 +0200220 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
Jordan Crouse0b360862008-01-21 10:07:00 -0700221
222 if (timer == -1) {
223 printk(KERN_ERR "geodewdt: No timers were available\n");
224 return -ENODEV;
225 }
226
227 wdt_timer = timer;
228
229 /* Set up the timer */
230
231 geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
232 GEODEWDT_SCALE | (3 << 8));
233
234 /* Set up comparator 2 to reset when the event fires */
235 geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
236
237 /* Set up the initial timeout */
238
239 geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
240 timeout * GEODEWDT_HZ);
241
242 ret = misc_register(&geodewdt_miscdev);
243
244 return ret;
245}
246
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000247static int __devexit geodewdt_remove(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700248{
249 misc_deregister(&geodewdt_miscdev);
250 return 0;
251}
252
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000253static void geodewdt_shutdown(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700254{
255 geodewdt_disable();
256}
257
258static struct platform_driver geodewdt_driver = {
259 .probe = geodewdt_probe,
260 .remove = __devexit_p(geodewdt_remove),
261 .shutdown = geodewdt_shutdown,
262 .driver = {
263 .owner = THIS_MODULE,
264 .name = DRV_NAME,
265 },
266};
267
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000268static int __init geodewdt_init(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700269{
270 int ret;
271
272 ret = platform_driver_register(&geodewdt_driver);
273 if (ret)
274 return ret;
275
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000276 geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
277 -1, NULL, 0);
Jordan Crouse0b360862008-01-21 10:07:00 -0700278 if (IS_ERR(geodewdt_platform_device)) {
279 ret = PTR_ERR(geodewdt_platform_device);
280 goto err;
281 }
282
283 return 0;
284err:
285 platform_driver_unregister(&geodewdt_driver);
286 return ret;
287}
288
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000289static void __exit geodewdt_exit(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700290{
291 platform_device_unregister(geodewdt_platform_device);
292 platform_driver_unregister(&geodewdt_driver);
293}
294
295module_init(geodewdt_init);
296module_exit(geodewdt_exit);
297
298MODULE_AUTHOR("Advanced Micro Devices, Inc");
299MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
300MODULE_LICENSE("GPL");
301MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);