2024-09-09 08:52:07 +00:00
|
|
|
/*
|
2024-09-09 08:57:42 +00:00
|
|
|
* Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
|
|
|
|
*
|
|
|
|
* (C) Copyright 2013 - 2014 Xilinx, Inc.
|
|
|
|
* (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#include <linux/err.h>
|
2024-09-09 08:52:07 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/of_address.h>
|
|
|
|
|
|
|
|
/* Register offsets for the Wdt device */
|
|
|
|
#define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
|
|
|
|
#define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
|
|
|
|
#define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
|
|
|
|
|
|
|
|
/* Control/Status Register Masks */
|
|
|
|
#define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
|
|
|
|
#define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
|
|
|
|
#define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
|
|
|
|
|
|
|
|
/* Control/Status Register 0/1 bits */
|
|
|
|
#define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
|
|
|
|
|
|
|
|
/* SelfTest constants */
|
|
|
|
#define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
|
|
|
|
#define XWT_TIMER_FAILED 0xFFFFFFFF
|
|
|
|
|
|
|
|
#define WATCHDOG_NAME "Xilinx Watchdog"
|
|
|
|
|
|
|
|
struct xwdt_device {
|
|
|
|
void __iomem *base;
|
|
|
|
u32 wdt_interval;
|
2024-09-09 08:57:42 +00:00
|
|
|
spinlock_t spinlock;
|
|
|
|
struct watchdog_device xilinx_wdt_wdd;
|
2024-09-09 08:52:07 +00:00
|
|
|
};
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int xilinx_wdt_start(struct watchdog_device *wdd)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
u32 control_status_reg;
|
|
|
|
struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
|
|
|
|
|
|
|
|
spin_lock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
/* Clean previous status and enable the watchdog timer */
|
2024-09-09 08:57:42 +00:00
|
|
|
control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
|
|
|
|
|
|
|
|
iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
|
2024-09-09 08:57:42 +00:00
|
|
|
xdev->base + XWT_TWCSR0_OFFSET);
|
|
|
|
|
|
|
|
iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_unlock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int xilinx_wdt_stop(struct watchdog_device *wdd)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
u32 control_status_reg;
|
|
|
|
struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_lock(&xdev->spinlock);
|
|
|
|
|
|
|
|
control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
|
2024-09-09 08:57:42 +00:00
|
|
|
xdev->base + XWT_TWCSR0_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_unlock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
pr_info("Stopped!\n");
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
u32 control_status_reg;
|
|
|
|
struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_lock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
|
|
|
|
control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
|
|
|
|
iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_unlock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static const struct watchdog_info xilinx_wdt_ident = {
|
|
|
|
.options = WDIOF_MAGICCLOSE |
|
|
|
|
WDIOF_KEEPALIVEPING,
|
|
|
|
.firmware_version = 1,
|
|
|
|
.identity = WATCHDOG_NAME,
|
|
|
|
};
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static const struct watchdog_ops xilinx_wdt_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = xilinx_wdt_start,
|
|
|
|
.stop = xilinx_wdt_stop,
|
|
|
|
.ping = xilinx_wdt_keepalive,
|
|
|
|
};
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static u32 xwdt_selftest(struct xwdt_device *xdev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 timer_value1;
|
|
|
|
u32 timer_value2;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_lock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
|
|
|
|
timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
for (i = 0;
|
|
|
|
((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
|
|
|
|
(timer_value2 == timer_value1)); i++) {
|
2024-09-09 08:57:42 +00:00
|
|
|
timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_unlock(&xdev->spinlock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
if (timer_value2 != timer_value1)
|
|
|
|
return ~XWT_TIMER_FAILED;
|
|
|
|
else
|
|
|
|
return XWT_TIMER_FAILED;
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int xwdt_probe(struct platform_device *pdev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2024-09-09 08:57:42 +00:00
|
|
|
u32 pfreq = 0, enable_once = 0;
|
|
|
|
struct resource *res;
|
|
|
|
struct xwdt_device *xdev;
|
|
|
|
struct watchdog_device *xilinx_wdt_wdd;
|
|
|
|
|
|
|
|
xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
|
|
|
|
if (!xdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
|
|
|
|
xilinx_wdt_wdd->info = &xilinx_wdt_ident;
|
|
|
|
xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
|
|
|
|
xilinx_wdt_wdd->parent = &pdev->dev;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
xdev->base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(xdev->base))
|
|
|
|
return PTR_ERR(xdev->base);
|
|
|
|
|
|
|
|
rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &pfreq);
|
|
|
|
if (rc)
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"The watchdog clock frequency cannot be obtained\n");
|
|
|
|
|
|
|
|
rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
|
|
|
|
&xdev->wdt_interval);
|
|
|
|
if (rc)
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"Parameter \"xlnx,wdt-interval\" not found\n");
|
|
|
|
|
|
|
|
rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
|
|
|
|
&enable_once);
|
|
|
|
if (rc)
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"Parameter \"xlnx,wdt-enable-once\" not found\n");
|
|
|
|
|
|
|
|
watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Twice of the 2^wdt_interval / freq because the first wdt overflow is
|
|
|
|
* ignored (interrupt), reset is only generated at second wdt overflow
|
|
|
|
*/
|
|
|
|
if (pfreq && xdev->wdt_interval)
|
|
|
|
xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
|
|
|
|
pfreq);
|
|
|
|
|
|
|
|
spin_lock_init(&xdev->spinlock);
|
|
|
|
watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
|
|
|
|
|
|
|
|
rc = xwdt_selftest(xdev);
|
2024-09-09 08:52:07 +00:00
|
|
|
if (rc == XWT_TIMER_FAILED) {
|
2024-09-09 08:57:42 +00:00
|
|
|
dev_err(&pdev->dev, "SelfTest routine error\n");
|
|
|
|
return rc;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
rc = watchdog_register_device(xilinx_wdt_wdd);
|
2024-09-09 08:52:07 +00:00
|
|
|
if (rc) {
|
2024-09-09 08:57:42 +00:00
|
|
|
dev_err(&pdev->dev, "Cannot register watchdog (err=%d)\n", rc);
|
|
|
|
return rc;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
|
|
|
|
xdev->base, xilinx_wdt_wdd->timeout);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
platform_set_drvdata(pdev, xdev);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int xwdt_remove(struct platform_device *pdev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct xwdt_device *xdev = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Match table for of_platform binding */
|
2024-09-09 08:57:42 +00:00
|
|
|
static const struct of_device_id xwdt_of_match[] = {
|
|
|
|
{ .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
|
2024-09-09 08:52:07 +00:00
|
|
|
{ .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, xwdt_of_match);
|
|
|
|
|
|
|
|
static struct platform_driver xwdt_driver = {
|
|
|
|
.probe = xwdt_probe,
|
2024-09-09 08:57:42 +00:00
|
|
|
.remove = xwdt_remove,
|
2024-09-09 08:52:07 +00:00
|
|
|
.driver = {
|
|
|
|
.name = WATCHDOG_NAME,
|
|
|
|
.of_match_table = xwdt_of_match,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(xwdt_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
|
|
|
|
MODULE_DESCRIPTION("Xilinx Watchdog driver");
|
2024-09-09 08:57:42 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|