linux_dsm_epyc7002/drivers/watchdog/ts72xx_wdt.c

449 lines
9.8 KiB
C
Raw Normal View History

/*
* Watchdog driver for Technologic Systems TS-72xx based SBCs
* (TS-7200, TS-7250 and TS-7260). These boards have external
* glue logic CPLD chip, which includes programmable watchdog
* timer.
*
* Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi>
*
* This driver is based on ep93xx_wdt and wm831x_wdt drivers.
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/miscdevice.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
#include <linux/slab.h>
#include <linux/watchdog.h>
#include <linux/uaccess.h>
#define TS72XX_WDT_FEED_VAL 0x05
#define TS72XX_WDT_DEFAULT_TIMEOUT 8
static int timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
module_param(timeout, int, 0);
MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. "
"(1 <= timeout <= 8, default="
__MODULE_STRING(TS72XX_WDT_DEFAULT_TIMEOUT)
")");
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
/**
* struct ts72xx_wdt - watchdog control structure
* @lock: lock that protects this structure
* @regval: watchdog timeout value suitable for control register
* @flags: flags controlling watchdog device state
* @control_reg: watchdog control register
* @feed_reg: watchdog feed register
* @pdev: back pointer to platform dev
*/
struct ts72xx_wdt {
struct mutex lock;
int regval;
#define TS72XX_WDT_BUSY_FLAG 1
#define TS72XX_WDT_EXPECT_CLOSE_FLAG 2
int flags;
void __iomem *control_reg;
void __iomem *feed_reg;
struct platform_device *pdev;
};
static struct platform_device *ts72xx_wdt_pdev;
/*
* TS-72xx Watchdog supports following timeouts (value written
* to control register):
* value description
* -------------------------
* 0x00 watchdog disabled
* 0x01 250ms
* 0x02 500ms
* 0x03 1s
* 0x04 reserved
* 0x05 2s
* 0x06 4s
* 0x07 8s
*
* Timeouts below 1s are not very usable so we don't
* allow them at all.
*
* We provide two functions that convert between these:
* timeout_to_regval() and regval_to_timeout().
*/
static const struct {
int timeout;
int regval;
} ts72xx_wdt_map[] = {
{ 1, 3 },
{ 2, 5 },
{ 4, 6 },
{ 8, 7 },
};
/**
* timeout_to_regval() - converts given timeout to control register value
* @new_timeout: timeout in seconds to be converted
*
* Function converts given @new_timeout into valid value that can
* be programmed into watchdog control register. When conversion is
* not possible, function returns %-EINVAL.
*/
static int timeout_to_regval(int new_timeout)
{
int i;
/* first limit it to 1 - 8 seconds */
new_timeout = clamp_val(new_timeout, 1, 8);
for (i = 0; i < ARRAY_SIZE(ts72xx_wdt_map); i++) {
if (ts72xx_wdt_map[i].timeout >= new_timeout)
return ts72xx_wdt_map[i].regval;
}
return -EINVAL;
}
/**
* regval_to_timeout() - converts control register value to timeout
* @regval: control register value to be converted
*
* Function converts given @regval to timeout in seconds (1, 2, 4 or 8).
* If @regval cannot be converted, function returns %-EINVAL.
*/
static int regval_to_timeout(int regval)
{
int i;
for (i = 0; i < ARRAY_SIZE(ts72xx_wdt_map); i++) {
if (ts72xx_wdt_map[i].regval == regval)
return ts72xx_wdt_map[i].timeout;
}
return -EINVAL;
}
/**
* ts72xx_wdt_kick() - kick the watchdog
* @wdt: watchdog to be kicked
*
* Called with @wdt->lock held.
*/
static inline void ts72xx_wdt_kick(struct ts72xx_wdt *wdt)
{
__raw_writeb(TS72XX_WDT_FEED_VAL, wdt->feed_reg);
}
/**
* ts72xx_wdt_start() - starts the watchdog timer
* @wdt: watchdog to be started
*
* This function programs timeout to watchdog timer
* and starts it.
*
* Called with @wdt->lock held.
*/
static void ts72xx_wdt_start(struct ts72xx_wdt *wdt)
{
/*
* To program the wdt, it first must be "fed" and
* only after that (within 30 usecs) the configuration
* can be changed.
*/
ts72xx_wdt_kick(wdt);
__raw_writeb((u8)wdt->regval, wdt->control_reg);
}
/**
* ts72xx_wdt_stop() - stops the watchdog timer
* @wdt: watchdog to be stopped
*
* Called with @wdt->lock held.
*/
static void ts72xx_wdt_stop(struct ts72xx_wdt *wdt)
{
ts72xx_wdt_kick(wdt);
__raw_writeb(0, wdt->control_reg);
}
static int ts72xx_wdt_open(struct inode *inode, struct file *file)
{
struct ts72xx_wdt *wdt = platform_get_drvdata(ts72xx_wdt_pdev);
int regval;
/*
* Try to convert default timeout to valid register
* value first.
*/
regval = timeout_to_regval(timeout);
if (regval < 0) {
dev_err(&wdt->pdev->dev,
"failed to convert timeout (%d) to register value\n",
timeout);
return regval;
}
if (mutex_lock_interruptible(&wdt->lock))
return -ERESTARTSYS;
if ((wdt->flags & TS72XX_WDT_BUSY_FLAG) != 0) {
mutex_unlock(&wdt->lock);
return -EBUSY;
}
wdt->flags = TS72XX_WDT_BUSY_FLAG;
wdt->regval = regval;
file->private_data = wdt;
ts72xx_wdt_start(wdt);
mutex_unlock(&wdt->lock);
return nonseekable_open(inode, file);
}
static int ts72xx_wdt_release(struct inode *inode, struct file *file)
{
struct ts72xx_wdt *wdt = file->private_data;
if (mutex_lock_interruptible(&wdt->lock))
return -ERESTARTSYS;
if ((wdt->flags & TS72XX_WDT_EXPECT_CLOSE_FLAG) != 0) {
ts72xx_wdt_stop(wdt);
} else {
dev_warn(&wdt->pdev->dev,
"TS-72XX WDT device closed unexpectly. "
"Watchdog timer will not stop!\n");
/*
* Kick it one more time, to give userland some time
* to recover (for example, respawning the kicker
* daemon).
*/
ts72xx_wdt_kick(wdt);
}
wdt->flags = 0;
mutex_unlock(&wdt->lock);
return 0;
}
static ssize_t ts72xx_wdt_write(struct file *file,
const char __user *data,
size_t len,
loff_t *ppos)
{
struct ts72xx_wdt *wdt = file->private_data;
if (!len)
return 0;
if (mutex_lock_interruptible(&wdt->lock))
return -ERESTARTSYS;
ts72xx_wdt_kick(wdt);
/*
* Support for magic character closing. User process
* writes 'V' into the device, just before it is closed.
* This means that we know that the wdt timer can be
* stopped after user closes the device.
*/
if (!nowayout) {
int i;
for (i = 0; i < len; i++) {
char c;
/* In case it was set long ago */
wdt->flags &= ~TS72XX_WDT_EXPECT_CLOSE_FLAG;
if (get_user(c, data + i)) {
mutex_unlock(&wdt->lock);
return -EFAULT;
}
if (c == 'V') {
wdt->flags |= TS72XX_WDT_EXPECT_CLOSE_FLAG;
break;
}
}
}
mutex_unlock(&wdt->lock);
return len;
}
static const struct watchdog_info winfo = {
.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
WDIOF_MAGICCLOSE,
.firmware_version = 1,
.identity = "TS-72XX WDT",
};
static long ts72xx_wdt_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
struct ts72xx_wdt *wdt = file->private_data;
void __user *argp = (void __user *)arg;
int __user *p = (int __user *)argp;
int error = 0;
if (mutex_lock_interruptible(&wdt->lock))
return -ERESTARTSYS;
switch (cmd) {
case WDIOC_GETSUPPORT:
if (copy_to_user(argp, &winfo, sizeof(winfo)))
error = -EFAULT;
break;
case WDIOC_GETSTATUS:
case WDIOC_GETBOOTSTATUS:
error = put_user(0, p);
break;
case WDIOC_KEEPALIVE:
ts72xx_wdt_kick(wdt);
break;
case WDIOC_SETOPTIONS: {
int options;
error = get_user(options, p);
if (error)
break;
error = -EINVAL;
if ((options & WDIOS_DISABLECARD) != 0) {
ts72xx_wdt_stop(wdt);
error = 0;
}
if ((options & WDIOS_ENABLECARD) != 0) {
ts72xx_wdt_start(wdt);
error = 0;
}
break;
}
case WDIOC_SETTIMEOUT: {
int new_timeout;
int regval;
error = get_user(new_timeout, p);
if (error)
break;
regval = timeout_to_regval(new_timeout);
if (regval < 0) {
error = regval;
break;
}
ts72xx_wdt_stop(wdt);
wdt->regval = regval;
ts72xx_wdt_start(wdt);
/*FALLTHROUGH*/
}
case WDIOC_GETTIMEOUT:
error = put_user(regval_to_timeout(wdt->regval), p);
break;
default:
error = -ENOTTY;
break;
}
mutex_unlock(&wdt->lock);
return error;
}
static const struct file_operations ts72xx_wdt_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.open = ts72xx_wdt_open,
.release = ts72xx_wdt_release,
.write = ts72xx_wdt_write,
.unlocked_ioctl = ts72xx_wdt_ioctl,
};
static struct miscdevice ts72xx_wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &ts72xx_wdt_fops,
};
static int ts72xx_wdt_probe(struct platform_device *pdev)
{
struct ts72xx_wdt *wdt;
struct resource *r1, *r2;
int error = 0;
wdt = devm_kzalloc(&pdev->dev, sizeof(struct ts72xx_wdt), GFP_KERNEL);
if (!wdt)
return -ENOMEM;
r1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
wdt->control_reg = devm_ioremap_resource(&pdev->dev, r1);
if (IS_ERR(wdt->control_reg))
return PTR_ERR(wdt->control_reg);
r2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
wdt->feed_reg = devm_ioremap_resource(&pdev->dev, r2);
if (IS_ERR(wdt->feed_reg))
return PTR_ERR(wdt->feed_reg);
platform_set_drvdata(pdev, wdt);
ts72xx_wdt_pdev = pdev;
wdt->pdev = pdev;
mutex_init(&wdt->lock);
/* make sure that the watchdog is disabled */
ts72xx_wdt_stop(wdt);
error = misc_register(&ts72xx_wdt_miscdev);
if (error) {
dev_err(&pdev->dev, "failed to register miscdev\n");
return error;
}
dev_info(&pdev->dev, "TS-72xx Watchdog driver\n");
return 0;
}
static int ts72xx_wdt_remove(struct platform_device *pdev)
{
return misc_deregister(&ts72xx_wdt_miscdev);
}
static struct platform_driver ts72xx_wdt_driver = {
.probe = ts72xx_wdt_probe,
.remove = ts72xx_wdt_remove,
.driver = {
.name = "ts72xx-wdt",
.owner = THIS_MODULE,
},
};
module_platform_driver(ts72xx_wdt_driver);
MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ts72xx-wdt");