Skip to content

Commit 72a9e7f

Browse files
jwerner-chromiumWim Van Sebroeck
authored andcommitted
watchdog: Add new arm_smc_wdt watchdog driver
This patch adds a watchdog driver that can be used on ARM systems with the appropriate watchdog implemented in Secure Monitor firmware. The driver communicates with firmware via a Secure Monitor Call. This may be useful for platforms using TrustZone that want the Secure Monitor firmware to have the final control over the watchdog. This is implemented on mt8173 chromebook devices oak, elm and hana in arm trusted firmware file plat/mediatek/mt8173/drivers/wdt/wdt.c. Signed-off-by: Julius Werner <[email protected]> Signed-off-by: Evan Benn <[email protected]> Reviewed-by: Julius Werner <[email protected]> Tested-by: Xingyu Chen<[email protected]> Reviewed-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/20200505131242.v6.2.Ia92bb4d4ce84bcefeba1d00aaa1c1e919b6164ef@changeid Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Wim Van Sebroeck <[email protected]>
1 parent 5c24a28 commit 72a9e7f

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,7 @@ M: Julius Werner <[email protected]>
14611461
R: Evan Benn <[email protected]>
14621462
S: Maintained
14631463
F: devicetree/bindings/watchdog/arm-smc-wdt.yaml
1464+
F: drivers/watchdog/arm_smc_wdt.c
14641465

14651466
ARM SMMU DRIVERS
14661467
M: Will Deacon <[email protected]>

arch/arm64/configs/defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ CONFIG_UNIPHIER_THERMAL=y
513513
CONFIG_WATCHDOG=y
514514
CONFIG_ARM_SP805_WATCHDOG=y
515515
CONFIG_ARM_SBSA_WATCHDOG=y
516+
CONFIG_ARM_SMC_WATCHDOG=y
516517
CONFIG_S3C2410_WATCHDOG=y
517518
CONFIG_DW_WATCHDOG=y
518519
CONFIG_SUNXI_WATCHDOG=m

drivers/watchdog/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,19 @@ config DIGICOLOR_WATCHDOG
868868
To compile this driver as a module, choose M here: the
869869
module will be called digicolor_wdt.
870870

871+
config ARM_SMC_WATCHDOG
872+
tristate "ARM Secure Monitor Call based watchdog support"
873+
depends on ARM || ARM64
874+
depends on OF
875+
depends on HAVE_ARM_SMCCC
876+
select WATCHDOG_CORE
877+
help
878+
Say Y here to include support for a watchdog timer
879+
implemented by the EL3 Secure Monitor on ARM platforms.
880+
Requires firmware support.
881+
To compile this driver as a module, choose M here: the
882+
module will be called arm_smc_wdt.
883+
871884
config LPC18XX_WATCHDOG
872885
tristate "LPC18xx/43xx Watchdog"
873886
depends on ARCH_LPC18XX || COMPILE_TEST

drivers/watchdog/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ obj-$(CONFIG_UNIPHIER_WATCHDOG) += uniphier_wdt.o
9494
obj-$(CONFIG_RTD119X_WATCHDOG) += rtd119x_wdt.o
9595
obj-$(CONFIG_SPRD_WATCHDOG) += sprd_wdt.o
9696
obj-$(CONFIG_PM8916_WATCHDOG) += pm8916_wdt.o
97+
obj-$(CONFIG_ARM_SMC_WATCHDOG) += arm_smc_wdt.o
9798

9899
# X86 (i386 + ia64 + x86_64) Architecture
99100
obj-$(CONFIG_ACQUIRE_WDT) += acquirewdt.o

drivers/watchdog/arm_smc_wdt.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* ARM Secure Monitor Call watchdog driver
4+
*
5+
* Copyright 2020 Google LLC.
6+
* Julius Werner <[email protected]>
7+
* Based on mtk_wdt.c
8+
*/
9+
10+
#include <linux/arm-smccc.h>
11+
#include <linux/err.h>
12+
#include <linux/module.h>
13+
#include <linux/moduleparam.h>
14+
#include <linux/of.h>
15+
#include <linux/platform_device.h>
16+
#include <linux/types.h>
17+
#include <linux/watchdog.h>
18+
#include <uapi/linux/psci.h>
19+
20+
#define DRV_NAME "arm_smc_wdt"
21+
#define DRV_VERSION "1.0"
22+
23+
enum smcwd_call {
24+
SMCWD_INIT = 0,
25+
SMCWD_SET_TIMEOUT = 1,
26+
SMCWD_ENABLE = 2,
27+
SMCWD_PET = 3,
28+
SMCWD_GET_TIMELEFT = 4,
29+
};
30+
31+
static bool nowayout = WATCHDOG_NOWAYOUT;
32+
static unsigned int timeout;
33+
34+
static int smcwd_call(struct watchdog_device *wdd, enum smcwd_call call,
35+
unsigned long arg, struct arm_smccc_res *res)
36+
{
37+
struct arm_smccc_res local_res;
38+
39+
if (!res)
40+
res = &local_res;
41+
42+
arm_smccc_smc((u32)(uintptr_t)watchdog_get_drvdata(wdd), call, arg, 0,
43+
0, 0, 0, 0, res);
44+
45+
if (res->a0 == PSCI_RET_NOT_SUPPORTED)
46+
return -ENODEV;
47+
if (res->a0 == PSCI_RET_INVALID_PARAMS)
48+
return -EINVAL;
49+
if (res->a0 != PSCI_RET_SUCCESS)
50+
return -EIO;
51+
return 0;
52+
}
53+
54+
static int smcwd_ping(struct watchdog_device *wdd)
55+
{
56+
return smcwd_call(wdd, SMCWD_PET, 0, NULL);
57+
}
58+
59+
static unsigned int smcwd_get_timeleft(struct watchdog_device *wdd)
60+
{
61+
struct arm_smccc_res res;
62+
63+
smcwd_call(wdd, SMCWD_GET_TIMELEFT, 0, &res);
64+
if (res.a0)
65+
return 0;
66+
return res.a1;
67+
}
68+
69+
static int smcwd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
70+
{
71+
int res;
72+
73+
res = smcwd_call(wdd, SMCWD_SET_TIMEOUT, timeout, NULL);
74+
if (!res)
75+
wdd->timeout = timeout;
76+
return res;
77+
}
78+
79+
static int smcwd_stop(struct watchdog_device *wdd)
80+
{
81+
return smcwd_call(wdd, SMCWD_ENABLE, 0, NULL);
82+
}
83+
84+
static int smcwd_start(struct watchdog_device *wdd)
85+
{
86+
return smcwd_call(wdd, SMCWD_ENABLE, 1, NULL);
87+
}
88+
89+
static const struct watchdog_info smcwd_info = {
90+
.identity = DRV_NAME,
91+
.options = WDIOF_SETTIMEOUT |
92+
WDIOF_KEEPALIVEPING |
93+
WDIOF_MAGICCLOSE,
94+
};
95+
96+
static const struct watchdog_ops smcwd_ops = {
97+
.start = smcwd_start,
98+
.stop = smcwd_stop,
99+
.ping = smcwd_ping,
100+
.set_timeout = smcwd_set_timeout,
101+
};
102+
103+
static const struct watchdog_ops smcwd_timeleft_ops = {
104+
.start = smcwd_start,
105+
.stop = smcwd_stop,
106+
.ping = smcwd_ping,
107+
.set_timeout = smcwd_set_timeout,
108+
.get_timeleft = smcwd_get_timeleft,
109+
};
110+
111+
static int smcwd_probe(struct platform_device *pdev)
112+
{
113+
struct watchdog_device *wdd;
114+
int err;
115+
struct arm_smccc_res res;
116+
u32 smc_func_id;
117+
118+
wdd = devm_kzalloc(&pdev->dev, sizeof(*wdd), GFP_KERNEL);
119+
if (!wdd)
120+
return -ENOMEM;
121+
platform_set_drvdata(pdev, wdd);
122+
123+
if (of_property_read_u32(pdev->dev.of_node, "arm,smc-id",
124+
&smc_func_id))
125+
smc_func_id = 0x82003D06;
126+
watchdog_set_drvdata(wdd, (void *)(uintptr_t)smc_func_id);
127+
128+
err = smcwd_call(wdd, SMCWD_INIT, 0, &res);
129+
if (err < 0)
130+
return err;
131+
132+
wdd->info = &smcwd_info;
133+
/* get_timeleft is optional */
134+
if (smcwd_call(wdd, SMCWD_GET_TIMELEFT, 0, NULL))
135+
wdd->ops = &smcwd_ops;
136+
else
137+
wdd->ops = &smcwd_timeleft_ops;
138+
wdd->timeout = res.a2;
139+
wdd->max_timeout = res.a2;
140+
wdd->min_timeout = res.a1;
141+
wdd->parent = &pdev->dev;
142+
143+
watchdog_stop_on_reboot(wdd);
144+
watchdog_stop_on_unregister(wdd);
145+
watchdog_set_nowayout(wdd, nowayout);
146+
watchdog_init_timeout(wdd, timeout, &pdev->dev);
147+
err = smcwd_set_timeout(wdd, wdd->timeout);
148+
if (err)
149+
return err;
150+
151+
err = devm_watchdog_register_device(&pdev->dev, wdd);
152+
if (err)
153+
return err;
154+
155+
dev_info(&pdev->dev,
156+
"Watchdog registered (timeout=%d sec, nowayout=%d)\n",
157+
wdd->timeout, nowayout);
158+
159+
return 0;
160+
}
161+
162+
static const struct of_device_id smcwd_dt_ids[] = {
163+
{ .compatible = "arm,smc-wdt" },
164+
{}
165+
};
166+
MODULE_DEVICE_TABLE(of, smcwd_dt_ids);
167+
168+
static struct platform_driver smcwd_driver = {
169+
.probe = smcwd_probe,
170+
.driver = {
171+
.name = DRV_NAME,
172+
.of_match_table = smcwd_dt_ids,
173+
},
174+
};
175+
176+
module_platform_driver(smcwd_driver);
177+
178+
module_param(timeout, uint, 0);
179+
MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
180+
181+
module_param(nowayout, bool, 0);
182+
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
183+
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
184+
185+
MODULE_LICENSE("GPL");
186+
MODULE_AUTHOR("Julius Werner <[email protected]>");
187+
MODULE_DESCRIPTION("ARM Secure Monitor Call Watchdog Driver");
188+
MODULE_VERSION(DRV_VERSION);

0 commit comments

Comments
 (0)