1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018-2019 NXP.
6 #include <linux/arm-smccc.h>
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
13 #include <linux/platform_device.h>
14 #include <linux/reboot.h>
15 #include <linux/watchdog.h>
17 #define DEFAULT_TIMEOUT 60
19 * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
20 * in theory, but for normal case, 1s~128s is enough, you can change this max
21 * value in case it's not enough.
23 #define MAX_TIMEOUT 128
25 #define IMX_SIP_TIMER 0xC2000002
26 #define IMX_SIP_TIMER_START_WDOG 0x01
27 #define IMX_SIP_TIMER_STOP_WDOG 0x02
28 #define IMX_SIP_TIMER_SET_WDOG_ACT 0x03
29 #define IMX_SIP_TIMER_PING_WDOG 0x04
30 #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG 0x05
31 #define IMX_SIP_TIMER_GET_WDOG_STAT 0x06
32 #define IMX_SIP_TIMER_SET_PRETIME_WDOG 0x07
34 #define SC_TIMER_WDOG_ACTION_PARTITION 0
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, bool, 0000);
38 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
39 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
41 static int imx_sc_wdt_ping(struct watchdog_device *wdog)
43 struct arm_smccc_res res;
45 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
46 0, 0, 0, 0, 0, 0, &res);
51 static int imx_sc_wdt_start(struct watchdog_device *wdog)
53 struct arm_smccc_res res;
55 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
56 0, 0, 0, 0, 0, 0, &res);
60 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
61 SC_TIMER_WDOG_ACTION_PARTITION,
63 return res.a0 ? -EACCES : 0;
66 static int imx_sc_wdt_stop(struct watchdog_device *wdog)
68 struct arm_smccc_res res;
70 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
71 0, 0, 0, 0, 0, 0, &res);
73 return res.a0 ? -EACCES : 0;
76 static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
79 struct arm_smccc_res res;
81 wdog->timeout = timeout;
82 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
83 timeout * 1000, 0, 0, 0, 0, 0, &res);
85 return res.a0 ? -EACCES : 0;
88 static const struct watchdog_ops imx_sc_wdt_ops = {
90 .start = imx_sc_wdt_start,
91 .stop = imx_sc_wdt_stop,
92 .ping = imx_sc_wdt_ping,
93 .set_timeout = imx_sc_wdt_set_timeout,
96 static const struct watchdog_info imx_sc_wdt_info = {
97 .identity = "i.MX SC watchdog timer",
98 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
99 WDIOF_MAGICCLOSE | WDIOF_PRETIMEOUT,
102 static int imx_sc_wdt_probe(struct platform_device *pdev)
104 struct watchdog_device *imx_sc_wdd;
107 imx_sc_wdd = devm_kzalloc(&pdev->dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
111 platform_set_drvdata(pdev, imx_sc_wdd);
113 imx_sc_wdd->info = &imx_sc_wdt_info;
114 imx_sc_wdd->ops = &imx_sc_wdt_ops;
115 imx_sc_wdd->min_timeout = 1;
116 imx_sc_wdd->max_timeout = MAX_TIMEOUT;
117 imx_sc_wdd->parent = &pdev->dev;
118 imx_sc_wdd->timeout = DEFAULT_TIMEOUT;
120 ret = watchdog_init_timeout(imx_sc_wdd, 0, &pdev->dev);
122 dev_warn(&pdev->dev, "Failed to set timeout value, using default\n");
124 watchdog_stop_on_reboot(imx_sc_wdd);
125 watchdog_stop_on_unregister(imx_sc_wdd);
127 ret = devm_watchdog_register_device(&pdev->dev, imx_sc_wdd);
129 dev_err(&pdev->dev, "Failed to register watchdog device\n");
136 static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
138 struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
140 if (watchdog_active(imx_sc_wdd))
141 imx_sc_wdt_stop(imx_sc_wdd);
146 static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
148 struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
150 if (watchdog_active(imx_sc_wdd))
151 imx_sc_wdt_start(imx_sc_wdd);
156 static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
157 imx_sc_wdt_suspend, imx_sc_wdt_resume);
159 static const struct of_device_id imx_sc_wdt_dt_ids[] = {
160 { .compatible = "fsl,imx-sc-wdt", },
163 MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
165 static struct platform_driver imx_sc_wdt_driver = {
166 .probe = imx_sc_wdt_probe,
168 .name = "imx-sc-wdt",
169 .of_match_table = imx_sc_wdt_dt_ids,
170 .pm = &imx_sc_wdt_pm_ops,
173 module_platform_driver(imx_sc_wdt_driver);
175 MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
176 MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
177 MODULE_LICENSE("GPL v2");