1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018-2019 NXP.
6 #include <linux/arm-smccc.h>
7 #include <linux/firmware/imx/sci.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/watchdog.h>
16 #define DEFAULT_TIMEOUT 60
18 * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
19 * in theory, but for normal case, 1s~128s is enough, you can change this max
20 * value in case it's not enough.
22 #define MAX_TIMEOUT 128
24 #define IMX_SIP_TIMER 0xC2000002
25 #define IMX_SIP_TIMER_START_WDOG 0x01
26 #define IMX_SIP_TIMER_STOP_WDOG 0x02
27 #define IMX_SIP_TIMER_SET_WDOG_ACT 0x03
28 #define IMX_SIP_TIMER_PING_WDOG 0x04
29 #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG 0x05
30 #define IMX_SIP_TIMER_GET_WDOG_STAT 0x06
31 #define IMX_SIP_TIMER_SET_PRETIME_WDOG 0x07
33 #define SC_TIMER_WDOG_ACTION_PARTITION 0
36 #define SC_IRQ_GROUP_WDOG 1
37 #define SC_TIMER_ERR_BUSY 10
39 static bool nowayout = WATCHDOG_NOWAYOUT;
40 module_param(nowayout, bool, 0000);
41 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
42 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
44 struct imx_sc_wdt_device {
45 struct watchdog_device wdd;
46 struct notifier_block wdt_notifier;
49 static int imx_sc_wdt_ping(struct watchdog_device *wdog)
51 struct arm_smccc_res res;
53 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
54 0, 0, 0, 0, 0, 0, &res);
59 static int imx_sc_wdt_start(struct watchdog_device *wdog)
61 struct arm_smccc_res res;
63 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
64 0, 0, 0, 0, 0, 0, &res);
66 /* Ignore if already enabled(SC_TIMER_ERR_BUSY) */
67 if (res.a0 && res.a0 != SC_TIMER_ERR_BUSY)
70 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
71 SC_TIMER_WDOG_ACTION_PARTITION,
73 return res.a0 ? -EACCES : 0;
76 static int imx_sc_wdt_stop(struct watchdog_device *wdog)
78 struct arm_smccc_res res;
80 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
81 0, 0, 0, 0, 0, 0, &res);
83 return res.a0 ? -EACCES : 0;
86 static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
89 struct arm_smccc_res res;
91 wdog->timeout = timeout;
92 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
93 timeout * 1000, 0, 0, 0, 0, 0, &res);
95 return res.a0 ? -EACCES : 0;
98 static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog,
99 unsigned int pretimeout)
101 struct arm_smccc_res res;
104 * SCU firmware calculates pretimeout based on current time
105 * stamp instead of watchdog timeout stamp, need to convert
106 * the pretimeout to SCU firmware's timeout value.
108 arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG,
109 (wdog->timeout - pretimeout) * 1000, 0, 0, 0,
114 wdog->pretimeout = pretimeout;
119 static int imx_sc_wdt_notify(struct notifier_block *nb,
120 unsigned long event, void *group)
122 struct imx_sc_wdt_device *imx_sc_wdd =
124 struct imx_sc_wdt_device,
127 if (event & SC_IRQ_WDOG &&
128 *(u8 *)group == SC_IRQ_GROUP_WDOG)
129 watchdog_notify_pretimeout(&imx_sc_wdd->wdd);
134 static void imx_sc_wdt_action(void *data)
136 struct notifier_block *wdt_notifier = data;
138 imx_scu_irq_unregister_notifier(wdt_notifier);
139 imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
144 static const struct watchdog_ops imx_sc_wdt_ops = {
145 .owner = THIS_MODULE,
146 .start = imx_sc_wdt_start,
147 .stop = imx_sc_wdt_stop,
148 .ping = imx_sc_wdt_ping,
149 .set_timeout = imx_sc_wdt_set_timeout,
150 .set_pretimeout = imx_sc_wdt_set_pretimeout,
153 static struct watchdog_info imx_sc_wdt_info = {
154 .identity = "i.MX SC watchdog timer",
155 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
159 static int imx_sc_wdt_probe(struct platform_device *pdev)
161 struct imx_sc_wdt_device *imx_sc_wdd;
162 struct watchdog_device *wdog;
163 struct device *dev = &pdev->dev;
166 imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
170 platform_set_drvdata(pdev, imx_sc_wdd);
172 wdog = &imx_sc_wdd->wdd;
173 wdog->info = &imx_sc_wdt_info;
174 wdog->ops = &imx_sc_wdt_ops;
175 wdog->min_timeout = 1;
176 wdog->max_timeout = MAX_TIMEOUT;
178 wdog->timeout = DEFAULT_TIMEOUT;
180 watchdog_init_timeout(wdog, 0, dev);
182 ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout);
186 watchdog_stop_on_reboot(wdog);
187 watchdog_stop_on_unregister(wdog);
189 ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
193 dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n");
194 goto register_device;
197 imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify;
198 ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier);
200 imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
204 "Register irq notifier failed, pretimeout NOT supported\n");
205 goto register_device;
208 ret = devm_add_action_or_reset(dev, imx_sc_wdt_action,
209 &imx_sc_wdd->wdt_notifier);
211 imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT;
213 dev_warn(dev, "Add action failed, pretimeout NOT supported\n");
216 return devm_watchdog_register_device(dev, wdog);
219 static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
221 struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
223 if (watchdog_active(&imx_sc_wdd->wdd))
224 imx_sc_wdt_stop(&imx_sc_wdd->wdd);
229 static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
231 struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
233 if (watchdog_active(&imx_sc_wdd->wdd))
234 imx_sc_wdt_start(&imx_sc_wdd->wdd);
239 static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
240 imx_sc_wdt_suspend, imx_sc_wdt_resume);
242 static const struct of_device_id imx_sc_wdt_dt_ids[] = {
243 { .compatible = "fsl,imx-sc-wdt", },
246 MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
248 static struct platform_driver imx_sc_wdt_driver = {
249 .probe = imx_sc_wdt_probe,
251 .name = "imx-sc-wdt",
252 .of_match_table = imx_sc_wdt_dt_ids,
253 .pm = &imx_sc_wdt_pm_ops,
256 module_platform_driver(imx_sc_wdt_driver);
259 MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
260 MODULE_LICENSE("GPL v2");