2009-12-19 02:02:38 +08:00
|
|
|
/* Watchdog timer for machines with the CS5535/CS5536 companion chip
|
2008-01-22 01:07:00 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
|
2009-12-19 02:02:38 +08:00
|
|
|
* Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
|
2008-01-22 01:07:00 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/reboot.h>
|
2008-07-15 19:46:11 +08:00
|
|
|
#include <linux/uaccess.h>
|
2008-01-22 01:07:00 +08:00
|
|
|
|
2009-12-19 02:02:38 +08:00
|
|
|
#include <linux/cs5535.h>
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
#define GEODEWDT_HZ 500
|
|
|
|
#define GEODEWDT_SCALE 6
|
|
|
|
#define GEODEWDT_MAX_SECONDS 131
|
|
|
|
|
|
|
|
#define WDT_FLAGS_OPEN 1
|
|
|
|
#define WDT_FLAGS_ORPHAN 2
|
|
|
|
|
|
|
|
#define DRV_NAME "geodewdt"
|
|
|
|
#define WATCHDOG_NAME "Geode GX/LX WDT"
|
|
|
|
#define WATCHDOG_TIMEOUT 60
|
|
|
|
|
|
|
|
static int timeout = WATCHDOG_TIMEOUT;
|
|
|
|
module_param(timeout, int, 0);
|
2009-03-18 16:35:09 +08:00
|
|
|
MODULE_PARM_DESC(timeout,
|
|
|
|
"Watchdog timeout in seconds. 1<= timeout <=131, default="
|
|
|
|
__MODULE_STRING(WATCHDOG_TIMEOUT) ".");
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
static int nowayout = WATCHDOG_NOWAYOUT;
|
|
|
|
module_param(nowayout, int, 0);
|
2009-03-18 16:35:09 +08:00
|
|
|
MODULE_PARM_DESC(nowayout,
|
|
|
|
"Watchdog cannot be stopped once started (default="
|
|
|
|
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
static struct platform_device *geodewdt_platform_device;
|
|
|
|
static unsigned long wdt_flags;
|
2009-12-19 02:02:38 +08:00
|
|
|
static struct cs5535_mfgpt_timer *wdt_timer;
|
2008-01-22 01:07:00 +08:00
|
|
|
static int safe_close;
|
|
|
|
|
|
|
|
static void geodewdt_ping(void)
|
|
|
|
{
|
|
|
|
/* Stop the counter */
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
/* Reset the counter */
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
/* Enable the counter */
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
|
2008-01-22 01:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void geodewdt_disable(void)
|
|
|
|
{
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
|
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
|
2008-01-22 01:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int geodewdt_set_heartbeat(int val)
|
|
|
|
{
|
|
|
|
if (val < 1 || val > GEODEWDT_MAX_SECONDS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
|
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
|
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
|
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
timeout = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static int geodewdt_open(struct inode *inode, struct file *file)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
2008-08-07 04:19:41 +08:00
|
|
|
if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
|
|
|
|
return -EBUSY;
|
2008-01-22 01:07:00 +08:00
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
|
|
|
|
__module_get(THIS_MODULE);
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
geodewdt_ping();
|
2008-08-07 04:19:41 +08:00
|
|
|
return nonseekable_open(inode, file);
|
2008-01-22 01:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static int geodewdt_release(struct inode *inode, struct file *file)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
if (safe_close) {
|
|
|
|
geodewdt_disable();
|
|
|
|
module_put(THIS_MODULE);
|
2008-08-07 04:19:41 +08:00
|
|
|
} else {
|
2008-01-22 01:07:00 +08:00
|
|
|
printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
|
|
|
|
geodewdt_ping();
|
|
|
|
|
|
|
|
set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
|
|
|
|
safe_close = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static ssize_t geodewdt_write(struct file *file, const char __user *data,
|
|
|
|
size_t len, loff_t *ppos)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
2008-08-07 04:19:41 +08:00
|
|
|
if (len) {
|
2008-01-22 01:07:00 +08:00
|
|
|
if (!nowayout) {
|
|
|
|
size_t i;
|
|
|
|
safe_close = 0;
|
|
|
|
|
|
|
|
for (i = 0; i != len; i++) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (get_user(c, data + i))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (c == 'V')
|
|
|
|
safe_close = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
geodewdt_ping();
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-09-18 20:26:15 +08:00
|
|
|
static long geodewdt_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
int __user *p = argp;
|
|
|
|
int interval;
|
|
|
|
|
|
|
|
static struct watchdog_info ident = {
|
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
|
|
|
|
| WDIOF_MAGICCLOSE,
|
|
|
|
.firmware_version = 1,
|
|
|
|
.identity = WATCHDOG_NAME,
|
2008-08-07 04:19:41 +08:00
|
|
|
};
|
2008-01-22 01:07:00 +08:00
|
|
|
|
2008-07-18 02:08:47 +08:00
|
|
|
switch (cmd) {
|
2008-01-22 01:07:00 +08:00
|
|
|
case WDIOC_GETSUPPORT:
|
|
|
|
return copy_to_user(argp, &ident,
|
|
|
|
sizeof(ident)) ? -EFAULT : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WDIOC_GETSTATUS:
|
|
|
|
case WDIOC_GETBOOTSTATUS:
|
|
|
|
return put_user(0, p);
|
|
|
|
|
|
|
|
case WDIOC_SETOPTIONS:
|
|
|
|
{
|
|
|
|
int options, ret = -EINVAL;
|
|
|
|
|
|
|
|
if (get_user(options, p))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (options & WDIOS_DISABLECARD) {
|
|
|
|
geodewdt_disable();
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options & WDIOS_ENABLECARD) {
|
|
|
|
geodewdt_ping();
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-07-18 19:41:17 +08:00
|
|
|
case WDIOC_KEEPALIVE:
|
|
|
|
geodewdt_ping();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WDIOC_SETTIMEOUT:
|
|
|
|
if (get_user(interval, p))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (geodewdt_set_heartbeat(interval))
|
|
|
|
return -EINVAL;
|
|
|
|
/* Fall through */
|
|
|
|
case WDIOC_GETTIMEOUT:
|
|
|
|
return put_user(timeout, p);
|
|
|
|
|
2008-01-22 01:07:00 +08:00
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations geodewdt_fops = {
|
2008-08-07 04:19:41 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.write = geodewdt_write,
|
2008-09-18 20:26:15 +08:00
|
|
|
.unlocked_ioctl = geodewdt_ioctl,
|
2008-08-07 04:19:41 +08:00
|
|
|
.open = geodewdt_open,
|
|
|
|
.release = geodewdt_release,
|
2008-01-22 01:07:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice geodewdt_miscdev = {
|
|
|
|
.minor = WATCHDOG_MINOR,
|
|
|
|
.name = "watchdog",
|
2008-08-07 04:19:41 +08:00
|
|
|
.fops = &geodewdt_fops,
|
2008-01-22 01:07:00 +08:00
|
|
|
};
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static int __devinit geodewdt_probe(struct platform_device *dev)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
2009-12-19 02:02:38 +08:00
|
|
|
int ret;
|
2008-01-22 01:07:00 +08:00
|
|
|
|
2009-12-19 02:02:38 +08:00
|
|
|
wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
|
|
|
|
if (!wdt_timer) {
|
2008-01-22 01:07:00 +08:00
|
|
|
printk(KERN_ERR "geodewdt: No timers were available\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the timer */
|
|
|
|
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
|
2008-01-22 01:07:00 +08:00
|
|
|
GEODEWDT_SCALE | (3 << 8));
|
|
|
|
|
|
|
|
/* Set up comparator 2 to reset when the event fires */
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
|
2008-01-22 01:07:00 +08:00
|
|
|
|
|
|
|
/* Set up the initial timeout */
|
|
|
|
|
2009-12-19 02:02:38 +08:00
|
|
|
cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
|
2008-01-22 01:07:00 +08:00
|
|
|
timeout * GEODEWDT_HZ);
|
|
|
|
|
|
|
|
ret = misc_register(&geodewdt_miscdev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static int __devexit geodewdt_remove(struct platform_device *dev)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
misc_deregister(&geodewdt_miscdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static void geodewdt_shutdown(struct platform_device *dev)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
geodewdt_disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver geodewdt_driver = {
|
|
|
|
.probe = geodewdt_probe,
|
|
|
|
.remove = __devexit_p(geodewdt_remove),
|
|
|
|
.shutdown = geodewdt_shutdown,
|
|
|
|
.driver = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = DRV_NAME,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static int __init geodewdt_init(void)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = platform_driver_register(&geodewdt_driver);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2009-03-18 16:35:09 +08:00
|
|
|
geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
|
|
|
|
-1, NULL, 0);
|
2008-01-22 01:07:00 +08:00
|
|
|
if (IS_ERR(geodewdt_platform_device)) {
|
|
|
|
ret = PTR_ERR(geodewdt_platform_device);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
platform_driver_unregister(&geodewdt_driver);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-07 04:19:41 +08:00
|
|
|
static void __exit geodewdt_exit(void)
|
2008-01-22 01:07:00 +08:00
|
|
|
{
|
|
|
|
platform_device_unregister(geodewdt_platform_device);
|
|
|
|
platform_driver_unregister(&geodewdt_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(geodewdt_init);
|
|
|
|
module_exit(geodewdt_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Advanced Micro Devices, Inc");
|
|
|
|
MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
|