2014-09-26 05:51:02 +07:00
|
|
|
/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 and
|
|
|
|
* only version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/clk.h>
|
2014-09-26 05:51:04 +07:00
|
|
|
#include <linux/delay.h>
|
2014-09-26 05:51:02 +07:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/watchdog.h>
|
2016-06-30 00:50:01 +07:00
|
|
|
#include <linux/of_device.h>
|
2014-09-26 05:51:02 +07:00
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
enum wdt_reg {
|
|
|
|
WDT_RST,
|
|
|
|
WDT_EN,
|
|
|
|
WDT_STS,
|
2016-06-29 01:35:21 +07:00
|
|
|
WDT_BARK_TIME,
|
2016-06-30 00:50:01 +07:00
|
|
|
WDT_BITE_TIME,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const u32 reg_offset_data_apcs_tmr[] = {
|
|
|
|
[WDT_RST] = 0x38,
|
|
|
|
[WDT_EN] = 0x40,
|
|
|
|
[WDT_STS] = 0x44,
|
2016-06-29 01:35:21 +07:00
|
|
|
[WDT_BARK_TIME] = 0x4C,
|
2016-06-30 00:50:01 +07:00
|
|
|
[WDT_BITE_TIME] = 0x5C,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const u32 reg_offset_data_kpss[] = {
|
|
|
|
[WDT_RST] = 0x4,
|
|
|
|
[WDT_EN] = 0x8,
|
|
|
|
[WDT_STS] = 0xC,
|
2016-06-29 01:35:21 +07:00
|
|
|
[WDT_BARK_TIME] = 0x10,
|
2016-06-30 00:50:01 +07:00
|
|
|
[WDT_BITE_TIME] = 0x14,
|
|
|
|
};
|
2014-09-26 05:51:02 +07:00
|
|
|
|
|
|
|
struct qcom_wdt {
|
|
|
|
struct watchdog_device wdd;
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned long rate;
|
|
|
|
void __iomem *base;
|
2016-06-30 00:50:01 +07:00
|
|
|
const u32 *layout;
|
2014-09-26 05:51:02 +07:00
|
|
|
};
|
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg)
|
|
|
|
{
|
|
|
|
return wdt->base + wdt->layout[reg];
|
|
|
|
}
|
|
|
|
|
2014-09-26 05:51:02 +07:00
|
|
|
static inline
|
|
|
|
struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
return container_of(wdd, struct qcom_wdt, wdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcom_wdt_start(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct qcom_wdt *wdt = to_qcom_wdt(wdd);
|
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(0, wdt_addr(wdt, WDT_EN));
|
|
|
|
writel(1, wdt_addr(wdt, WDT_RST));
|
2016-06-29 01:35:21 +07:00
|
|
|
writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME));
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME));
|
|
|
|
writel(1, wdt_addr(wdt, WDT_EN));
|
2014-09-26 05:51:02 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcom_wdt_stop(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct qcom_wdt *wdt = to_qcom_wdt(wdd);
|
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(0, wdt_addr(wdt, WDT_EN));
|
2014-09-26 05:51:02 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcom_wdt_ping(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct qcom_wdt *wdt = to_qcom_wdt(wdd);
|
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(1, wdt_addr(wdt, WDT_RST));
|
2014-09-26 05:51:02 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcom_wdt_set_timeout(struct watchdog_device *wdd,
|
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
wdd->timeout = timeout;
|
|
|
|
return qcom_wdt_start(wdd);
|
|
|
|
}
|
|
|
|
|
2016-02-27 08:32:49 +07:00
|
|
|
static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action,
|
|
|
|
void *data)
|
2014-09-26 05:51:04 +07:00
|
|
|
{
|
2015-11-17 00:28:09 +07:00
|
|
|
struct qcom_wdt *wdt = to_qcom_wdt(wdd);
|
2014-09-26 05:51:04 +07:00
|
|
|
u32 timeout;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger watchdog bite:
|
|
|
|
* Setup BITE_TIME to be 128ms, and enable WDT.
|
|
|
|
*/
|
|
|
|
timeout = 128 * wdt->rate / 1000;
|
|
|
|
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(0, wdt_addr(wdt, WDT_EN));
|
|
|
|
writel(1, wdt_addr(wdt, WDT_RST));
|
2016-06-29 01:35:21 +07:00
|
|
|
writel(timeout, wdt_addr(wdt, WDT_BARK_TIME));
|
2016-06-30 00:50:01 +07:00
|
|
|
writel(timeout, wdt_addr(wdt, WDT_BITE_TIME));
|
|
|
|
writel(1, wdt_addr(wdt, WDT_EN));
|
2014-09-26 05:51:04 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Actually make sure the above sequence hits hardware before sleeping.
|
|
|
|
*/
|
|
|
|
wmb();
|
|
|
|
|
|
|
|
msleep(150);
|
2015-11-17 00:28:09 +07:00
|
|
|
return 0;
|
2014-09-26 05:51:04 +07:00
|
|
|
}
|
|
|
|
|
2015-11-17 00:28:09 +07:00
|
|
|
static const struct watchdog_ops qcom_wdt_ops = {
|
|
|
|
.start = qcom_wdt_start,
|
|
|
|
.stop = qcom_wdt_stop,
|
|
|
|
.ping = qcom_wdt_ping,
|
|
|
|
.set_timeout = qcom_wdt_set_timeout,
|
|
|
|
.restart = qcom_wdt_restart,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct watchdog_info qcom_wdt_info = {
|
|
|
|
.options = WDIOF_KEEPALIVEPING
|
|
|
|
| WDIOF_MAGICCLOSE
|
2016-04-05 07:37:46 +07:00
|
|
|
| WDIOF_SETTIMEOUT
|
|
|
|
| WDIOF_CARDRESET,
|
2015-11-17 00:28:09 +07:00
|
|
|
.identity = KBUILD_MODNAME,
|
|
|
|
};
|
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
static void qcom_clk_disable_unprepare(void *data)
|
|
|
|
{
|
|
|
|
clk_disable_unprepare(data);
|
|
|
|
}
|
|
|
|
|
2014-09-26 05:51:02 +07:00
|
|
|
static int qcom_wdt_probe(struct platform_device *pdev)
|
|
|
|
{
|
2019-04-10 00:23:50 +07:00
|
|
|
struct device *dev = &pdev->dev;
|
2014-09-26 05:51:02 +07:00
|
|
|
struct qcom_wdt *wdt;
|
|
|
|
struct resource *res;
|
2019-04-10 00:23:50 +07:00
|
|
|
struct device_node *np = dev->of_node;
|
2016-06-30 00:50:01 +07:00
|
|
|
const u32 *regs;
|
2015-02-21 09:19:34 +07:00
|
|
|
u32 percpu_offset;
|
2014-09-26 05:51:02 +07:00
|
|
|
int ret;
|
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
regs = of_device_get_match_data(dev);
|
2016-06-30 00:50:01 +07:00
|
|
|
if (!regs) {
|
2019-04-10 00:23:50 +07:00
|
|
|
dev_err(dev, "Unsupported QCOM WDT module\n");
|
2016-06-30 00:50:01 +07:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
|
2014-09-26 05:51:02 +07:00
|
|
|
if (!wdt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2017-07-22 23:04:33 +07:00
|
|
|
if (!res)
|
|
|
|
return -ENOMEM;
|
2015-02-21 09:19:34 +07:00
|
|
|
|
|
|
|
/* We use CPU0's DGT for the watchdog */
|
|
|
|
if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
|
|
|
|
percpu_offset = 0;
|
|
|
|
|
|
|
|
res->start += percpu_offset;
|
|
|
|
res->end += percpu_offset;
|
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
wdt->base = devm_ioremap_resource(dev, res);
|
2014-09-26 05:51:02 +07:00
|
|
|
if (IS_ERR(wdt->base))
|
|
|
|
return PTR_ERR(wdt->base);
|
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
wdt->clk = devm_clk_get(dev, NULL);
|
2014-09-26 05:51:02 +07:00
|
|
|
if (IS_ERR(wdt->clk)) {
|
2019-04-10 00:23:50 +07:00
|
|
|
dev_err(dev, "failed to get input clock\n");
|
2014-09-26 05:51:02 +07:00
|
|
|
return PTR_ERR(wdt->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(wdt->clk);
|
|
|
|
if (ret) {
|
2019-04-10 00:23:50 +07:00
|
|
|
dev_err(dev, "failed to setup clock\n");
|
2014-09-26 05:51:02 +07:00
|
|
|
return ret;
|
|
|
|
}
|
2019-04-10 00:23:50 +07:00
|
|
|
ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare,
|
|
|
|
wdt->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-09-26 05:51:02 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the clock rate to calculate the max timeout, so ensure it's
|
|
|
|
* not zero to avoid a divide-by-zero exception.
|
|
|
|
*
|
|
|
|
* WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
|
|
|
|
* that it would bite before a second elapses it's usefulness is
|
|
|
|
* limited. Bail if this is the case.
|
|
|
|
*/
|
|
|
|
wdt->rate = clk_get_rate(wdt->clk);
|
|
|
|
if (wdt->rate == 0 ||
|
|
|
|
wdt->rate > 0x10000000U) {
|
2019-04-10 00:23:50 +07:00
|
|
|
dev_err(dev, "invalid clock rate\n");
|
|
|
|
return -EINVAL;
|
2014-09-26 05:51:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
wdt->wdd.info = &qcom_wdt_info;
|
|
|
|
wdt->wdd.ops = &qcom_wdt_ops;
|
|
|
|
wdt->wdd.min_timeout = 1;
|
|
|
|
wdt->wdd.max_timeout = 0x10000000U / wdt->rate;
|
2019-04-10 00:23:50 +07:00
|
|
|
wdt->wdd.parent = dev;
|
2016-06-30 00:50:01 +07:00
|
|
|
wdt->layout = regs;
|
2014-09-26 05:51:02 +07:00
|
|
|
|
2016-11-14 08:11:16 +07:00
|
|
|
if (readl(wdt_addr(wdt, WDT_STS)) & 1)
|
2016-04-05 07:37:46 +07:00
|
|
|
wdt->wdd.bootstatus = WDIOF_CARDRESET;
|
|
|
|
|
2014-09-26 05:51:02 +07:00
|
|
|
/*
|
|
|
|
* If 'timeout-sec' unspecified in devicetree, assume a 30 second
|
|
|
|
* default, unless the max timeout is less than 30 seconds, then use
|
|
|
|
* the max instead.
|
|
|
|
*/
|
|
|
|
wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U);
|
2019-04-10 00:23:50 +07:00
|
|
|
watchdog_init_timeout(&wdt->wdd, 0, dev);
|
2014-09-26 05:51:02 +07:00
|
|
|
|
2019-04-10 00:23:50 +07:00
|
|
|
ret = devm_watchdog_register_device(dev, &wdt->wdd);
|
2014-09-26 05:51:02 +07:00
|
|
|
if (ret) {
|
2019-04-10 00:23:50 +07:00
|
|
|
dev_err(dev, "failed to register watchdog\n");
|
|
|
|
return ret;
|
2014-09-26 05:51:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, wdt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-17 22:19:42 +07:00
|
|
|
static int __maybe_unused qcom_wdt_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct qcom_wdt *wdt = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (watchdog_active(&wdt->wdd))
|
|
|
|
qcom_wdt_stop(&wdt->wdd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __maybe_unused qcom_wdt_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct qcom_wdt *wdt = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (watchdog_active(&wdt->wdd))
|
|
|
|
qcom_wdt_start(&wdt->wdd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume);
|
|
|
|
|
2014-09-26 05:51:02 +07:00
|
|
|
static const struct of_device_id qcom_wdt_of_table[] = {
|
2016-06-30 00:50:01 +07:00
|
|
|
{ .compatible = "qcom,kpss-timer", .data = reg_offset_data_apcs_tmr },
|
|
|
|
{ .compatible = "qcom,scss-timer", .data = reg_offset_data_apcs_tmr },
|
|
|
|
{ .compatible = "qcom,kpss-wdt", .data = reg_offset_data_kpss },
|
2014-09-26 05:51:02 +07:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, qcom_wdt_of_table);
|
|
|
|
|
|
|
|
static struct platform_driver qcom_watchdog_driver = {
|
|
|
|
.probe = qcom_wdt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = KBUILD_MODNAME,
|
|
|
|
.of_match_table = qcom_wdt_of_table,
|
2019-01-17 22:19:42 +07:00
|
|
|
.pm = &qcom_wdt_pm_ops,
|
2014-09-26 05:51:02 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(qcom_watchdog_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|