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 device *dev = &pdev->dev;
105 struct watchdog_device *imx_sc_wdd;
108 imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
112 platform_set_drvdata(pdev, imx_sc_wdd);
114 imx_sc_wdd->info = &imx_sc_wdt_info;
115 imx_sc_wdd->ops = &imx_sc_wdt_ops;
116 imx_sc_wdd->min_timeout = 1;
117 imx_sc_wdd->max_timeout = MAX_TIMEOUT;
118 imx_sc_wdd->parent = dev;
119 imx_sc_wdd->timeout = DEFAULT_TIMEOUT;
121 watchdog_init_timeout(imx_sc_wdd, 0, dev);
122 watchdog_stop_on_reboot(imx_sc_wdd);
123 watchdog_stop_on_unregister(imx_sc_wdd);
125 ret = devm_watchdog_register_device(dev, imx_sc_wdd);
127 dev_err(dev, "Failed to register watchdog device\n");
134 static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
136 struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
138 if (watchdog_active(imx_sc_wdd))
139 imx_sc_wdt_stop(imx_sc_wdd);
144 static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
146 struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
148 if (watchdog_active(imx_sc_wdd))
149 imx_sc_wdt_start(imx_sc_wdd);
154 static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
155 imx_sc_wdt_suspend, imx_sc_wdt_resume);
157 static const struct of_device_id imx_sc_wdt_dt_ids[] = {
158 { .compatible = "fsl,imx-sc-wdt", },
161 MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
163 static struct platform_driver imx_sc_wdt_driver = {
164 .probe = imx_sc_wdt_probe,
166 .name = "imx-sc-wdt",
167 .of_match_table = imx_sc_wdt_dt_ids,
168 .pm = &imx_sc_wdt_pm_ops,
171 module_platform_driver(imx_sc_wdt_driver);
173 MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
174 MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
175 MODULE_LICENSE("GPL v2");