1 /* Watchdog timer for machines with the CS5535/CS5536 companion chip
3 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
4 * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/miscdevice.h>
17 #include <linux/watchdog.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/uaccess.h>
23 #include <linux/cs5535.h>
25 #define GEODEWDT_HZ 500
26 #define GEODEWDT_SCALE 6
27 #define GEODEWDT_MAX_SECONDS 131
29 #define WDT_FLAGS_OPEN 1
30 #define WDT_FLAGS_ORPHAN 2
32 #define DRV_NAME "geodewdt"
33 #define WATCHDOG_NAME "Geode GX/LX WDT"
34 #define WATCHDOG_TIMEOUT 60
36 static int timeout
= WATCHDOG_TIMEOUT
;
37 module_param(timeout
, int, 0);
38 MODULE_PARM_DESC(timeout
,
39 "Watchdog timeout in seconds. 1<= timeout <=131, default="
40 __MODULE_STRING(WATCHDOG_TIMEOUT
) ".");
42 static int nowayout
= WATCHDOG_NOWAYOUT
;
43 module_param(nowayout
, int, 0);
44 MODULE_PARM_DESC(nowayout
,
45 "Watchdog cannot be stopped once started (default="
46 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
48 static struct platform_device
*geodewdt_platform_device
;
49 static unsigned long wdt_flags
;
50 static struct cs5535_mfgpt_timer
*wdt_timer
;
51 static int safe_close
;
53 static void geodewdt_ping(void)
55 /* Stop the counter */
56 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
58 /* Reset the counter */
59 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
61 /* Enable the counter */
62 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, MFGPT_SETUP_CNTEN
);
65 static void geodewdt_disable(void)
67 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
68 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
71 static int geodewdt_set_heartbeat(int val
)
73 if (val
< 1 || val
> GEODEWDT_MAX_SECONDS
)
76 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
77 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_CMP2
, val
* GEODEWDT_HZ
);
78 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
79 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, MFGPT_SETUP_CNTEN
);
85 static int geodewdt_open(struct inode
*inode
, struct file
*file
)
87 if (test_and_set_bit(WDT_FLAGS_OPEN
, &wdt_flags
))
90 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
))
91 __module_get(THIS_MODULE
);
94 return nonseekable_open(inode
, file
);
97 static int geodewdt_release(struct inode
*inode
, struct file
*file
)
101 module_put(THIS_MODULE
);
103 printk(KERN_CRIT
"Unexpected close - watchdog is not stopping.\n");
106 set_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
);
109 clear_bit(WDT_FLAGS_OPEN
, &wdt_flags
);
114 static ssize_t
geodewdt_write(struct file
*file
, const char __user
*data
,
115 size_t len
, loff_t
*ppos
)
122 for (i
= 0; i
!= len
; i
++) {
125 if (get_user(c
, data
+ i
))
138 static long geodewdt_ioctl(struct file
*file
, unsigned int cmd
,
141 void __user
*argp
= (void __user
*)arg
;
142 int __user
*p
= argp
;
145 static const struct watchdog_info ident
= {
146 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
148 .firmware_version
= 1,
149 .identity
= WATCHDOG_NAME
,
153 case WDIOC_GETSUPPORT
:
154 return copy_to_user(argp
, &ident
,
155 sizeof(ident
)) ? -EFAULT
: 0;
158 case WDIOC_GETSTATUS
:
159 case WDIOC_GETBOOTSTATUS
:
160 return put_user(0, p
);
162 case WDIOC_SETOPTIONS
:
164 int options
, ret
= -EINVAL
;
166 if (get_user(options
, p
))
169 if (options
& WDIOS_DISABLECARD
) {
174 if (options
& WDIOS_ENABLECARD
) {
181 case WDIOC_KEEPALIVE
:
185 case WDIOC_SETTIMEOUT
:
186 if (get_user(interval
, p
))
189 if (geodewdt_set_heartbeat(interval
))
192 case WDIOC_GETTIMEOUT
:
193 return put_user(timeout
, p
);
202 static const struct file_operations geodewdt_fops
= {
203 .owner
= THIS_MODULE
,
205 .write
= geodewdt_write
,
206 .unlocked_ioctl
= geodewdt_ioctl
,
207 .open
= geodewdt_open
,
208 .release
= geodewdt_release
,
211 static struct miscdevice geodewdt_miscdev
= {
212 .minor
= WATCHDOG_MINOR
,
214 .fops
= &geodewdt_fops
,
217 static int __devinit
geodewdt_probe(struct platform_device
*dev
)
221 wdt_timer
= cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY
, MFGPT_DOMAIN_WORKING
);
223 printk(KERN_ERR
"geodewdt: No timers were available\n");
227 /* Set up the timer */
229 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
,
230 GEODEWDT_SCALE
| (3 << 8));
232 /* Set up comparator 2 to reset when the event fires */
233 cs5535_mfgpt_toggle_event(wdt_timer
, MFGPT_CMP2
, MFGPT_EVENT_RESET
, 1);
235 /* Set up the initial timeout */
237 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_CMP2
,
238 timeout
* GEODEWDT_HZ
);
240 ret
= misc_register(&geodewdt_miscdev
);
245 static int __devexit
geodewdt_remove(struct platform_device
*dev
)
247 misc_deregister(&geodewdt_miscdev
);
251 static void geodewdt_shutdown(struct platform_device
*dev
)
256 static struct platform_driver geodewdt_driver
= {
257 .probe
= geodewdt_probe
,
258 .remove
= __devexit_p(geodewdt_remove
),
259 .shutdown
= geodewdt_shutdown
,
261 .owner
= THIS_MODULE
,
266 static int __init
geodewdt_init(void)
270 ret
= platform_driver_register(&geodewdt_driver
);
274 geodewdt_platform_device
= platform_device_register_simple(DRV_NAME
,
276 if (IS_ERR(geodewdt_platform_device
)) {
277 ret
= PTR_ERR(geodewdt_platform_device
);
283 platform_driver_unregister(&geodewdt_driver
);
287 static void __exit
geodewdt_exit(void)
289 platform_device_unregister(geodewdt_platform_device
);
290 platform_driver_unregister(&geodewdt_driver
);
293 module_init(geodewdt_init
);
294 module_exit(geodewdt_exit
);
296 MODULE_AUTHOR("Advanced Micro Devices, Inc");
297 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
298 MODULE_LICENSE("GPL");
299 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);