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.
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>
18 #include <linux/platform_device.h>
19 #include <linux/reboot.h>
20 #include <linux/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
,
38 "Watchdog timeout in seconds. 1<= timeout <=131, default="
39 __MODULE_STRING(WATCHDOG_TIMEOUT
) ".");
41 static int nowayout
= WATCHDOG_NOWAYOUT
;
42 module_param(nowayout
, int, 0);
43 MODULE_PARM_DESC(nowayout
,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
47 static struct platform_device
*geodewdt_platform_device
;
48 static unsigned long wdt_flags
;
50 static int safe_close
;
52 static void geodewdt_ping(void)
54 /* Stop the counter */
55 geode_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
57 /* Reset the counter */
58 geode_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
60 /* Enable the counter */
61 geode_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, MFGPT_SETUP_CNTEN
);
64 static void geodewdt_disable(void)
66 geode_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
67 geode_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
70 static int geodewdt_set_heartbeat(int val
)
72 if (val
< 1 || val
> GEODEWDT_MAX_SECONDS
)
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
);
84 static int geodewdt_open(struct inode
*inode
, struct file
*file
)
86 if (test_and_set_bit(WDT_FLAGS_OPEN
, &wdt_flags
))
89 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
))
90 __module_get(THIS_MODULE
);
93 return nonseekable_open(inode
, file
);
96 static int geodewdt_release(struct inode
*inode
, struct file
*file
)
100 module_put(THIS_MODULE
);
102 printk(KERN_CRIT
"Unexpected close - watchdog is not stopping.\n");
105 set_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
);
108 clear_bit(WDT_FLAGS_OPEN
, &wdt_flags
);
113 static ssize_t
geodewdt_write(struct file
*file
, const char __user
*data
,
114 size_t len
, loff_t
*ppos
)
121 for (i
= 0; i
!= len
; i
++) {
124 if (get_user(c
, data
+ i
))
137 static long geodewdt_ioctl(struct file
*file
, unsigned int cmd
,
140 void __user
*argp
= (void __user
*)arg
;
141 int __user
*p
= argp
;
144 static struct watchdog_info ident
= {
145 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
147 .firmware_version
= 1,
148 .identity
= WATCHDOG_NAME
,
152 case WDIOC_GETSUPPORT
:
153 return copy_to_user(argp
, &ident
,
154 sizeof(ident
)) ? -EFAULT
: 0;
157 case WDIOC_GETSTATUS
:
158 case WDIOC_GETBOOTSTATUS
:
159 return put_user(0, p
);
161 case WDIOC_SETOPTIONS
:
163 int options
, ret
= -EINVAL
;
165 if (get_user(options
, p
))
168 if (options
& WDIOS_DISABLECARD
) {
173 if (options
& WDIOS_ENABLECARD
) {
180 case WDIOC_KEEPALIVE
:
184 case WDIOC_SETTIMEOUT
:
185 if (get_user(interval
, p
))
188 if (geodewdt_set_heartbeat(interval
))
191 case WDIOC_GETTIMEOUT
:
192 return put_user(timeout
, p
);
201 static const struct file_operations geodewdt_fops
= {
202 .owner
= THIS_MODULE
,
204 .write
= geodewdt_write
,
205 .unlocked_ioctl
= geodewdt_ioctl
,
206 .open
= geodewdt_open
,
207 .release
= geodewdt_release
,
210 static struct miscdevice geodewdt_miscdev
= {
211 .minor
= WATCHDOG_MINOR
,
213 .fops
= &geodewdt_fops
,
216 static int __devinit
geodewdt_probe(struct platform_device
*dev
)
220 timer
= geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY
, MFGPT_DOMAIN_WORKING
);
223 printk(KERN_ERR
"geodewdt: No timers were available\n");
229 /* Set up the timer */
231 geode_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
,
232 GEODEWDT_SCALE
| (3 << 8));
234 /* Set up comparator 2 to reset when the event fires */
235 geode_mfgpt_toggle_event(wdt_timer
, MFGPT_CMP2
, MFGPT_EVENT_RESET
, 1);
237 /* Set up the initial timeout */
239 geode_mfgpt_write(wdt_timer
, MFGPT_REG_CMP2
,
240 timeout
* GEODEWDT_HZ
);
242 ret
= misc_register(&geodewdt_miscdev
);
247 static int __devexit
geodewdt_remove(struct platform_device
*dev
)
249 misc_deregister(&geodewdt_miscdev
);
253 static void geodewdt_shutdown(struct platform_device
*dev
)
258 static struct platform_driver geodewdt_driver
= {
259 .probe
= geodewdt_probe
,
260 .remove
= __devexit_p(geodewdt_remove
),
261 .shutdown
= geodewdt_shutdown
,
263 .owner
= THIS_MODULE
,
268 static int __init
geodewdt_init(void)
272 ret
= platform_driver_register(&geodewdt_driver
);
276 geodewdt_platform_device
= platform_device_register_simple(DRV_NAME
,
278 if (IS_ERR(geodewdt_platform_device
)) {
279 ret
= PTR_ERR(geodewdt_platform_device
);
285 platform_driver_unregister(&geodewdt_driver
);
289 static void __exit
geodewdt_exit(void)
291 platform_device_unregister(geodewdt_platform_device
);
292 platform_driver_unregister(&geodewdt_driver
);
295 module_init(geodewdt_init
);
296 module_exit(geodewdt_exit
);
298 MODULE_AUTHOR("Advanced Micro Devices, Inc");
299 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
300 MODULE_LICENSE("GPL");
301 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);