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