2007-12-20 22:29:45 +00: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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty 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.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 infineon
|
|
|
|
* Copyright (C) 2007 John Crispin <blogic@openwrt.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/module.h>
|
2007-12-22 13:55:14 +00:00
|
|
|
#include <asm-mips/ifxmips/ifxmips_wdt.h>
|
|
|
|
#include <asm-mips/ifxmips/ifxmips.h>
|
2007-12-26 17:20:31 +00:00
|
|
|
#include <linux/platform_device.h>
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-26 17:20:31 +00:00
|
|
|
#define DRVNAME "ifxmips_wdt"
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
// TODO remove magic numbers and weirdo macros
|
2007-12-22 13:55:14 +00:00
|
|
|
extern unsigned int ifxmips_get_fpi_hz (void);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
static int ifxmips_wdt_inuse = 0;
|
|
|
|
static int ifxmips_wdt_major = 0;
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
int
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_enable (unsigned int timeout)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
unsigned int wdt_cr = 0;
|
|
|
|
unsigned int wdt_reload = 0;
|
|
|
|
unsigned int wdt_clkdiv, wdt_pwl, ffpi;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
/* clock divider & prewarning limit */
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_clkdiv = 1 << (7 * IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR)));
|
|
|
|
wdt_pwl = 0x8000 >> IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR));
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
//TODO
|
|
|
|
printk("WARNING FUNCTION CALL MISSING!!!");
|
|
|
|
//ffpi = cgu_get_io_region_clock();
|
|
|
|
printk("cpu clock = %d\n", ffpi);
|
|
|
|
|
|
|
|
/* caculate reload value */
|
|
|
|
wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
|
|
|
|
|
2007-12-26 17:20:31 +00:00
|
|
|
printk(KERN_WARNING DRVNAME ": wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
|
2007-12-20 22:29:45 +00:00
|
|
|
wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
|
|
|
|
|
|
|
|
if (wdt_reload > 0xFFFF)
|
|
|
|
{
|
2007-12-26 17:20:31 +00:00
|
|
|
printk(KERN_WARNING DRVNAME ": timeout too large %d\n", timeout);
|
2007-12-20 22:29:45 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write first part of password access */
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
|
|
|
|
wdt_cr &= (!IFXMIPS_BIU_WDT_CR_PW_SET(0xff) &
|
|
|
|
!IFXMIPS_BIU_WDT_CR_PWL_SET(0x3) &
|
|
|
|
!IFXMIPS_BIU_WDT_CR_CLKDIV_SET(0x3) &
|
|
|
|
!IFXMIPS_BIU_WDT_CR_RELOAD_SET(0xffff));
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) |
|
|
|
|
IFXMIPS_BIU_WDT_CR_PWL_SET(IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR))) |
|
|
|
|
IFXMIPS_BIU_WDT_CR_CLKDIV_SET(IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR))) |
|
|
|
|
IFXMIPS_BIU_WDT_CR_RELOAD_SET(wdt_reload) |
|
|
|
|
IFXMIPS_BIU_WDT_CR_GEN);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
printk("watchdog enabled\n");
|
|
|
|
|
|
|
|
out:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_disable (void)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
|
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2), IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
printk("watchdog disabled\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* passed LPEN or DSEN */
|
|
|
|
void
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_enable_feature (int en, int type)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
unsigned int wdt_cr = 0;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
if (en)
|
|
|
|
{
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff));
|
|
|
|
wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | type);
|
2007-12-20 22:29:45 +00:00
|
|
|
} else {
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff) & ~type);
|
|
|
|
wdt_cr |= IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2);
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_prewarning_limit (int pwl)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
unsigned int wdt_cr = 0;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
|
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
wdt_cr &= 0xf300ffff;
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_PWL_SET(pwl));
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
/* Set reload value in second password access */
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_set_clkdiv (int clkdiv)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
unsigned int wdt_cr = 0;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
|
|
|
|
writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
wdt_cr &= 0xfc00ffff;
|
2007-12-22 00:17:22 +00:00
|
|
|
wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_CLKDIV_SET(clkdiv));
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
/* Set reload value in second password access */
|
2007-12-22 00:17:22 +00:00
|
|
|
writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
|
2007-12-20 22:29:45 +00:00
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
static int timeout = -1;
|
|
|
|
unsigned int user_arg;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
if ((cmd != IFXMIPS_WDT_IOC_STOP) && (cmd != IFXMIPS_WDT_IOC_PING) && (cmd != IFXMIPS_WDT_IOC_GET_STATUS))
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
if (copy_from_user((void *) &user_arg, (void *) arg, sizeof (int))){
|
|
|
|
result = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_START:
|
2007-12-22 13:55:14 +00:00
|
|
|
if ((result = ifxmips_wdt_enable(user_arg)) < 0)
|
2007-12-20 22:29:45 +00:00
|
|
|
timeout = -1;
|
|
|
|
else
|
|
|
|
timeout = user_arg;
|
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_STOP:
|
2007-12-26 17:20:31 +00:00
|
|
|
printk(KERN_INFO DRVNAME ": disable watch dog timer\n");
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_disable();
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_PING:
|
2007-12-20 22:29:45 +00:00
|
|
|
if (timeout < 0)
|
|
|
|
result = -EIO;
|
|
|
|
else
|
2007-12-22 13:55:14 +00:00
|
|
|
result = ifxmips_wdt_enable(timeout);
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_GET_STATUS:
|
|
|
|
user_arg = readl(IFXMIPS_BIU_WDT_SR);
|
2007-12-20 22:29:45 +00:00
|
|
|
copy_to_user((int*)arg, (int*)&user_arg, sizeof(int));
|
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_SET_PWL:
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_prewarning_limit(user_arg);
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_SET_DSEN:
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_DSEN);
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_SET_LPEN:
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_LPEN);
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
case IFXMIPS_WDT_IOC_SET_CLKDIV:
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_set_clkdiv(user_arg);
|
2007-12-20 22:29:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-12-26 17:20:31 +00:00
|
|
|
printk(KERN_WARNING DRVNAME ": unknown watchdog iotcl\n");
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_open (struct inode *inode, struct file *file)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
2007-12-22 13:55:14 +00:00
|
|
|
if (ifxmips_wdt_inuse)
|
2007-12-20 22:29:45 +00:00
|
|
|
return -EBUSY;
|
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_inuse = 1;
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_release (struct inode *inode, struct file *file)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_inuse = 0;
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
|
2007-12-20 22:29:45 +00:00
|
|
|
int *eof, void *data)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
2007-12-22 00:17:22 +00:00
|
|
|
len += sprintf (buf + len, "IFXMIPS_BIU_WDT_PROC_READ\n");
|
|
|
|
len += sprintf (buf + len, "IFXMIPS_BIU_WDT_CR(0x%08x) : 0x%08x\n",
|
|
|
|
(unsigned int)IFXMIPS_BIU_WDT_CR, readl(IFXMIPS_BIU_WDT_CR));
|
|
|
|
len += sprintf (buf + len, "IFXMIPS_BIU_WDT_SR(0x%08x) : 0x%08x\n",
|
|
|
|
(unsigned int)IFXMIPS_BIU_WDT_SR, readl(IFXMIPS_BIU_WDT_SR));
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
*eof = 1;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_operations wdt_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
2007-12-22 13:55:14 +00:00
|
|
|
.ioctl = ifxmips_wdt_ioctl,
|
|
|
|
.open = ifxmips_wdt_open,
|
|
|
|
.release = ifxmips_wdt_release,
|
2007-12-20 22:29:45 +00:00
|
|
|
};
|
|
|
|
|
2007-12-26 17:20:31 +00:00
|
|
|
static int
|
|
|
|
ifxmips_wdt_probe (struct platform_device *pdev)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
if (ifxmips_wdt_major < 0)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
|
|
|
printk("cannot register watchdog device\n");
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
create_proc_read_entry("ifxmips_wdt", 0, NULL, ifxmips_wdt_register_proc_read, NULL);
|
2007-12-20 22:29:45 +00:00
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
printk("ifxmips watchdog loaded\n");
|
2007-12-20 22:29:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-26 17:20:31 +00:00
|
|
|
static int
|
|
|
|
ifxmips_wdt_remove (struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
unregister_chrdev(ifxmips_wdt_major, "wdt");
|
|
|
|
remove_proc_entry(DRVNAME, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct
|
|
|
|
platform_driver ifxmips_wdt_driver = {
|
|
|
|
.probe = ifxmips_wdt_probe,
|
|
|
|
.remove = ifxmips_wdt_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = DRVNAME,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
int __init
|
|
|
|
ifxmips_wdt_init_module (void)
|
|
|
|
{
|
|
|
|
int ret = platform_driver_register(&ifxmips_wdt_driver);
|
|
|
|
if (ret)
|
|
|
|
printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-20 22:29:45 +00:00
|
|
|
void
|
2007-12-22 13:55:14 +00:00
|
|
|
ifxmips_wdt_cleanup_module (void)
|
2007-12-20 22:29:45 +00:00
|
|
|
{
|
2007-12-26 17:20:31 +00:00
|
|
|
platform_driver_unregister(&ifxmips_wdt_driver);
|
2007-12-20 22:29:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 13:55:14 +00:00
|
|
|
module_init(ifxmips_wdt_init_module);
|
|
|
|
module_exit(ifxmips_wdt_cleanup_module);
|