/*
 *  File Name       : linux/drivers/char/mp200_wdt.c
 *  Function        : MP200 Watchdog driver 
 *  Release Version : Ver 1.02
 *  Release Date    : 2006/02/17
 *
 *  Copyright (C) NEC Electronics Corporation 2005-2006
 *
 *
 *  This program is free software;you can redistribute it and/or modify itunder the terms of
 *  the GNU General Public License as published by Free Softwere Foundation;either version 2
 *  of License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warrnty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program;
 *  If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/arch/smu.h>
#include <asm/arch/gpiocore.h>
#include <asm/arch/timer.h>

#include "mp200_wdt.h"

#ifndef CONFIG_MP201
static const unsigned int reg_tm_op[] = {
	IO_ADDRESS(MP200_WDT0_BASE) + 0x0000,
	IO_ADDRESS(MP200_WDT1_BASE) + 0x0000,
	IO_ADDRESS(MP200_WDT2_BASE) + 0x0000,
};
#endif				/* CONFIG_MP201 */

static int timeout;		/* in seconds */
static int opened = 0;

static int mp200_wdt_open(struct inode *inode, struct file *file)
{
	int ret;

	/*
	 *  Allow only one person to hold it open
	 */
	ret = test_and_set_bit(1, (volatile unsigned long *)&opened);
	if (ret != 0) {
		return -EBUSY;
	}

	ret = mp200_wdt_set_timeout(0, (DEFAULT_TIMEOUT * SEC_1));
	if (ret != 0) {
		opened = 0;
		return ret;
	}

	timeout = DEFAULT_TIMEOUT;

	outl((TM_EN | TSTART | TO_EN), IO_ADDRESS(MP200_WDT0_BASE) + 0x0000);

	return 0;
}

static int mp200_wdt_release(struct inode *inode, struct file *file)
{
	/*
	 *  Shut off the timer.
	 *  Lock it in if it's a module and we defined ...NOWAYOUT
	 */
#ifndef CONFIG_WATCHDOG_NOWAYOUT
	outl(0, IO_ADDRESS(MP200_WDT0_BASE) + 0x0000);
#endif

	opened = 0;

	return 0;
}

static ssize_t
mp200_wdt_write(struct file *file, const char *data, size_t len, loff_t * ppos)
{
	int ret;

	/* Can't seek (pwrite) on this device */
#ifdef CONFIG_MP201
	if (*ppos != 0) {
		return -ESPIPE;
	}
#else
	if (ppos != &file->f_pos) {
		return -ESPIPE;
	}
#endif				/* CONFIG_MP201 */

	if (len == 0) {
		return 0;
	}

	ret = mp200_wdt_ping(0);
	if (ret != 0) {
		return ret;
	}

	return 1;
}

static int
mp200_wdt_ioctl(struct inode *inode, struct file *file,
		unsigned int cmd, unsigned long arg)
{
	static struct watchdog_info ident = {
		.identity = "mp200 Watchdog",
		.options = WDIOF_SETTIMEOUT,
		.firmware_version = 0,
	};
	int new_margin;
	int ret;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		return copy_to_user((struct watchdog_info *)arg, &ident,
				    sizeof(ident));
	case WDIOC_GETSTATUS:
		return put_user(0, (int *)arg);
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, (int *)arg);
	case WDIOC_KEEPALIVE:
		return mp200_wdt_ping(0);
	case WDIOC_SETTIMEOUT:
		if (get_user(new_margin, (int *)arg) != 0) {
			return -EFAULT;
		}
		timeout = new_margin;
		if ((new_margin < WDT_MIN_TIMEOUT)
		    || (new_margin > WDT_MAX_TIMEOUT)) {
			timeout = DEFAULT_TIMEOUT;
		}
		ret = mp200_wdt_set_timeout(0, (timeout * SEC_1));
		if (ret != 0) {
			return ret;
		}
		/* Fall */
	case WDIOC_GETTIMEOUT:
		return put_user(timeout, (int *)arg);
	default:
		return -ENOIOCTLCMD;
	}
}

static struct file_operations mp200_wdt_fops = {
	.owner = THIS_MODULE,
	.open = mp200_wdt_open,
	.release = mp200_wdt_release,
	.write = mp200_wdt_write,
	.ioctl = mp200_wdt_ioctl,
};

static struct miscdevice mp200_wdt_miscdev = {
	.minor = WATCHDOG_MINOR,
	.name = "watchdog",
	.fops = &mp200_wdt_fops,
};

static int __init mp200_wdt_init(void)
{
	int ret;

	printk(KERN_INFO "Starting wdt.\n");

	ret = misc_register(&mp200_wdt_miscdev);
	if (ret != 0) {
		return ret;
	}

	opened = 0;

	outl(0, IO_ADDRESS(MP200_WDT0_BASE) + 0x0000);
	gpiocore_pwc_write(GPIOCORE_PWC_RDET_EN, RDET_EN, 0xff);
#ifndef CONFIG_MP201
	gpiocore_pwc_write(GPIOCORE_PWC_RSTMASK, RSTMASK, 0xff);
#endif				/* CONFIG_MP201 */
	outl(SMU_TW_TIMEOUT_ACTION, SMU_WDT_INT_RESET);

	return 0;
}

static void __exit mp200_wdt_exit(void)
{
	misc_deregister(&mp200_wdt_miscdev);
}

module_init(mp200_wdt_init);
module_exit(mp200_wdt_exit);
