1 // SPDX-License-Identifier: GPL-2.0-only
3 * ARM Secure Monitor Call watchdog driver
5 * Copyright 2020 Google LLC.
6 * Julius Werner <jwerner@chromium.org>
10 #include <linux/arm-smccc.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
17 #include <linux/watchdog.h>
18 #include <uapi/linux/psci.h>
20 #define DRV_NAME "arm_smc_wdt"
21 #define DRV_VERSION "1.0"
25 SMCWD_SET_TIMEOUT
= 1,
28 SMCWD_GET_TIMELEFT
= 4,
31 static bool nowayout
= WATCHDOG_NOWAYOUT
;
32 static unsigned int timeout
;
34 static int smcwd_call(struct watchdog_device
*wdd
, enum smcwd_call call
,
35 unsigned long arg
, struct arm_smccc_res
*res
)
37 struct arm_smccc_res local_res
;
42 arm_smccc_smc((u32
)(uintptr_t)watchdog_get_drvdata(wdd
), call
, arg
, 0,
45 if (res
->a0
== PSCI_RET_NOT_SUPPORTED
)
47 if (res
->a0
== PSCI_RET_INVALID_PARAMS
)
49 if (res
->a0
!= PSCI_RET_SUCCESS
)
54 static int smcwd_ping(struct watchdog_device
*wdd
)
56 return smcwd_call(wdd
, SMCWD_PET
, 0, NULL
);
59 static unsigned int smcwd_get_timeleft(struct watchdog_device
*wdd
)
61 struct arm_smccc_res res
;
63 smcwd_call(wdd
, SMCWD_GET_TIMELEFT
, 0, &res
);
69 static int smcwd_set_timeout(struct watchdog_device
*wdd
, unsigned int timeout
)
73 res
= smcwd_call(wdd
, SMCWD_SET_TIMEOUT
, timeout
, NULL
);
75 wdd
->timeout
= timeout
;
79 static int smcwd_stop(struct watchdog_device
*wdd
)
81 return smcwd_call(wdd
, SMCWD_ENABLE
, 0, NULL
);
84 static int smcwd_start(struct watchdog_device
*wdd
)
86 return smcwd_call(wdd
, SMCWD_ENABLE
, 1, NULL
);
89 static const struct watchdog_info smcwd_info
= {
91 .options
= WDIOF_SETTIMEOUT
|
96 static const struct watchdog_ops smcwd_ops
= {
100 .set_timeout
= smcwd_set_timeout
,
103 static const struct watchdog_ops smcwd_timeleft_ops
= {
104 .start
= smcwd_start
,
107 .set_timeout
= smcwd_set_timeout
,
108 .get_timeleft
= smcwd_get_timeleft
,
111 static int smcwd_probe(struct platform_device
*pdev
)
113 struct watchdog_device
*wdd
;
115 struct arm_smccc_res res
;
118 wdd
= devm_kzalloc(&pdev
->dev
, sizeof(*wdd
), GFP_KERNEL
);
121 platform_set_drvdata(pdev
, wdd
);
123 if (of_property_read_u32(pdev
->dev
.of_node
, "arm,smc-id",
125 smc_func_id
= 0x82003D06;
126 watchdog_set_drvdata(wdd
, (void *)(uintptr_t)smc_func_id
);
128 err
= smcwd_call(wdd
, SMCWD_INIT
, 0, &res
);
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
;
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
;
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
);
151 err
= devm_watchdog_register_device(&pdev
->dev
, wdd
);
156 "Watchdog registered (timeout=%d sec, nowayout=%d)\n",
157 wdd
->timeout
, nowayout
);
162 static const struct of_device_id smcwd_dt_ids
[] = {
163 { .compatible
= "arm,smc-wdt" },
166 MODULE_DEVICE_TABLE(of
, smcwd_dt_ids
);
168 static struct platform_driver smcwd_driver
= {
169 .probe
= smcwd_probe
,
172 .of_match_table
= smcwd_dt_ids
,
176 module_platform_driver(smcwd_driver
);
178 module_param(timeout
, uint
, 0);
179 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
181 module_param(nowayout
, bool, 0);
182 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
183 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
185 MODULE_LICENSE("GPL");
186 MODULE_AUTHOR("Julius Werner <jwerner@chromium.org>");
187 MODULE_DESCRIPTION("ARM Secure Monitor Call Watchdog Driver");
188 MODULE_VERSION(DRV_VERSION
);