xref: /linux/drivers/watchdog/imx_sc_wdt.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1986857acSAnson Huang // SPDX-License-Identifier: GPL-2.0
2986857acSAnson Huang /*
3986857acSAnson Huang  * Copyright 2018-2019 NXP.
4986857acSAnson Huang  */
5986857acSAnson Huang 
6986857acSAnson Huang #include <linux/arm-smccc.h>
715f7d7fcSAnson Huang #include <linux/firmware/imx/sci.h>
8986857acSAnson Huang #include <linux/io.h>
9986857acSAnson Huang #include <linux/kernel.h>
10986857acSAnson Huang #include <linux/module.h>
11986857acSAnson Huang #include <linux/moduleparam.h>
12986857acSAnson Huang #include <linux/of.h>
13986857acSAnson Huang #include <linux/platform_device.h>
14986857acSAnson Huang #include <linux/watchdog.h>
15986857acSAnson Huang 
16986857acSAnson Huang #define DEFAULT_TIMEOUT 60
17986857acSAnson Huang /*
18986857acSAnson Huang  * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
19986857acSAnson Huang  * in theory, but for normal case, 1s~128s is enough, you can change this max
20986857acSAnson Huang  * value in case it's not enough.
21986857acSAnson Huang  */
22986857acSAnson Huang #define MAX_TIMEOUT 128
23986857acSAnson Huang 
24986857acSAnson Huang #define IMX_SIP_TIMER			0xC2000002
25986857acSAnson Huang #define IMX_SIP_TIMER_START_WDOG		0x01
26986857acSAnson Huang #define IMX_SIP_TIMER_STOP_WDOG		0x02
27986857acSAnson Huang #define IMX_SIP_TIMER_SET_WDOG_ACT	0x03
28986857acSAnson Huang #define IMX_SIP_TIMER_PING_WDOG		0x04
29986857acSAnson Huang #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG	0x05
30986857acSAnson Huang #define IMX_SIP_TIMER_GET_WDOG_STAT	0x06
31986857acSAnson Huang #define IMX_SIP_TIMER_SET_PRETIME_WDOG	0x07
32986857acSAnson Huang 
33986857acSAnson Huang #define SC_TIMER_WDOG_ACTION_PARTITION	0
34986857acSAnson Huang 
3515f7d7fcSAnson Huang #define SC_IRQ_WDOG			1
3615f7d7fcSAnson Huang #define SC_IRQ_GROUP_WDOG		1
3706fdbf4dSJacky Bai #define SC_TIMER_ERR_BUSY		10
3815f7d7fcSAnson Huang 
39986857acSAnson Huang static bool nowayout = WATCHDOG_NOWAYOUT;
40986857acSAnson Huang module_param(nowayout, bool, 0000);
41986857acSAnson Huang MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
42986857acSAnson Huang 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
43986857acSAnson Huang 
4415f7d7fcSAnson Huang struct imx_sc_wdt_device {
4515f7d7fcSAnson Huang 	struct watchdog_device wdd;
4615f7d7fcSAnson Huang 	struct notifier_block wdt_notifier;
4715f7d7fcSAnson Huang };
4815f7d7fcSAnson Huang 
imx_sc_wdt_ping(struct watchdog_device * wdog)49986857acSAnson Huang static int imx_sc_wdt_ping(struct watchdog_device *wdog)
50986857acSAnson Huang {
51986857acSAnson Huang 	struct arm_smccc_res res;
52986857acSAnson Huang 
53986857acSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
54986857acSAnson Huang 		      0, 0, 0, 0, 0, 0, &res);
55986857acSAnson Huang 
56986857acSAnson Huang 	return 0;
57986857acSAnson Huang }
58986857acSAnson Huang 
imx_sc_wdt_is_running(void)59*db60d55eSAlexander Sverdlin static bool imx_sc_wdt_is_running(void)
60*db60d55eSAlexander Sverdlin {
61*db60d55eSAlexander Sverdlin 	struct arm_smccc_res res;
62*db60d55eSAlexander Sverdlin 
63*db60d55eSAlexander Sverdlin 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
64*db60d55eSAlexander Sverdlin 		      0, 0, 0, 0, 0, 0, &res);
65*db60d55eSAlexander Sverdlin 
66*db60d55eSAlexander Sverdlin 	/* Already enabled (SC_TIMER_ERR_BUSY)? */
67*db60d55eSAlexander Sverdlin 	if (res.a0 == SC_TIMER_ERR_BUSY)
68*db60d55eSAlexander Sverdlin 		return true;
69*db60d55eSAlexander Sverdlin 
70*db60d55eSAlexander Sverdlin 	/* Undo only if that was us who has (successfully) enabled the WDT */
71*db60d55eSAlexander Sverdlin 	if (!res.a0)
72*db60d55eSAlexander Sverdlin 		arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
73*db60d55eSAlexander Sverdlin 			      0, 0, 0, 0, 0, 0, &res);
74*db60d55eSAlexander Sverdlin 
75*db60d55eSAlexander Sverdlin 	return false;
76*db60d55eSAlexander Sverdlin }
77*db60d55eSAlexander Sverdlin 
imx_sc_wdt_start(struct watchdog_device * wdog)78986857acSAnson Huang static int imx_sc_wdt_start(struct watchdog_device *wdog)
79986857acSAnson Huang {
80986857acSAnson Huang 	struct arm_smccc_res res;
81986857acSAnson Huang 
82986857acSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
83986857acSAnson Huang 		      0, 0, 0, 0, 0, 0, &res);
8406fdbf4dSJacky Bai 
8506fdbf4dSJacky Bai 	/* Ignore if already enabled(SC_TIMER_ERR_BUSY) */
8606fdbf4dSJacky Bai 	if (res.a0 && res.a0 != SC_TIMER_ERR_BUSY)
87986857acSAnson Huang 		return -EACCES;
88986857acSAnson Huang 
89986857acSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
90986857acSAnson Huang 		      SC_TIMER_WDOG_ACTION_PARTITION,
91986857acSAnson Huang 		      0, 0, 0, 0, 0, &res);
92986857acSAnson Huang 	return res.a0 ? -EACCES : 0;
93986857acSAnson Huang }
94986857acSAnson Huang 
imx_sc_wdt_stop(struct watchdog_device * wdog)95986857acSAnson Huang static int imx_sc_wdt_stop(struct watchdog_device *wdog)
96986857acSAnson Huang {
97986857acSAnson Huang 	struct arm_smccc_res res;
98986857acSAnson Huang 
99986857acSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
100986857acSAnson Huang 		      0, 0, 0, 0, 0, 0, &res);
101986857acSAnson Huang 
102986857acSAnson Huang 	return res.a0 ? -EACCES : 0;
103986857acSAnson Huang }
104986857acSAnson Huang 
imx_sc_wdt_set_timeout(struct watchdog_device * wdog,unsigned int timeout)105986857acSAnson Huang static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
106986857acSAnson Huang 				unsigned int timeout)
107986857acSAnson Huang {
108986857acSAnson Huang 	struct arm_smccc_res res;
109986857acSAnson Huang 
110986857acSAnson Huang 	wdog->timeout = timeout;
111986857acSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
112986857acSAnson Huang 		      timeout * 1000, 0, 0, 0, 0, 0, &res);
113986857acSAnson Huang 
114986857acSAnson Huang 	return res.a0 ? -EACCES : 0;
115986857acSAnson Huang }
116986857acSAnson Huang 
imx_sc_wdt_set_pretimeout(struct watchdog_device * wdog,unsigned int pretimeout)11715f7d7fcSAnson Huang static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog,
11815f7d7fcSAnson Huang 				     unsigned int pretimeout)
11915f7d7fcSAnson Huang {
12015f7d7fcSAnson Huang 	struct arm_smccc_res res;
12115f7d7fcSAnson Huang 
1222c50a6b8SAnson Huang 	/*
1232c50a6b8SAnson Huang 	 * SCU firmware calculates pretimeout based on current time
1242c50a6b8SAnson Huang 	 * stamp instead of watchdog timeout stamp, need to convert
1252c50a6b8SAnson Huang 	 * the pretimeout to SCU firmware's timeout value.
1262c50a6b8SAnson Huang 	 */
12715f7d7fcSAnson Huang 	arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG,
1282c50a6b8SAnson Huang 		      (wdog->timeout - pretimeout) * 1000, 0, 0, 0,
1292c50a6b8SAnson Huang 		      0, 0, &res);
13015f7d7fcSAnson Huang 	if (res.a0)
13115f7d7fcSAnson Huang 		return -EACCES;
13215f7d7fcSAnson Huang 
13315f7d7fcSAnson Huang 	wdog->pretimeout = pretimeout;
13415f7d7fcSAnson Huang 
13515f7d7fcSAnson Huang 	return 0;
13615f7d7fcSAnson Huang }
13715f7d7fcSAnson Huang 
imx_sc_wdt_notify(struct notifier_block * nb,unsigned long event,void * group)13815f7d7fcSAnson Huang static int imx_sc_wdt_notify(struct notifier_block *nb,
13915f7d7fcSAnson Huang 			     unsigned long event, void *group)
14015f7d7fcSAnson Huang {
14115f7d7fcSAnson Huang 	struct imx_sc_wdt_device *imx_sc_wdd =
14215f7d7fcSAnson Huang 				 container_of(nb,
14315f7d7fcSAnson Huang 					      struct imx_sc_wdt_device,
14415f7d7fcSAnson Huang 					      wdt_notifier);
14515f7d7fcSAnson Huang 
14615f7d7fcSAnson Huang 	if (event & SC_IRQ_WDOG &&
14715f7d7fcSAnson Huang 	    *(u8 *)group == SC_IRQ_GROUP_WDOG)
14815f7d7fcSAnson Huang 		watchdog_notify_pretimeout(&imx_sc_wdd->wdd);
14915f7d7fcSAnson Huang 
15015f7d7fcSAnson Huang 	return 0;
15115f7d7fcSAnson Huang }
15215f7d7fcSAnson Huang 
imx_sc_wdt_action(void * data)15315f7d7fcSAnson Huang static void imx_sc_wdt_action(void *data)
15415f7d7fcSAnson Huang {
15515f7d7fcSAnson Huang 	struct notifier_block *wdt_notifier = data;
15615f7d7fcSAnson Huang 
15715f7d7fcSAnson Huang 	imx_scu_irq_unregister_notifier(wdt_notifier);
15815f7d7fcSAnson Huang 	imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
15915f7d7fcSAnson Huang 				 SC_IRQ_WDOG,
16015f7d7fcSAnson Huang 				 false);
16115f7d7fcSAnson Huang }
16215f7d7fcSAnson Huang 
163986857acSAnson Huang static const struct watchdog_ops imx_sc_wdt_ops = {
164986857acSAnson Huang 	.owner = THIS_MODULE,
165986857acSAnson Huang 	.start = imx_sc_wdt_start,
166986857acSAnson Huang 	.stop  = imx_sc_wdt_stop,
167986857acSAnson Huang 	.ping  = imx_sc_wdt_ping,
168986857acSAnson Huang 	.set_timeout = imx_sc_wdt_set_timeout,
16915f7d7fcSAnson Huang 	.set_pretimeout = imx_sc_wdt_set_pretimeout,
170986857acSAnson Huang };
171986857acSAnson Huang 
17215f7d7fcSAnson Huang static struct watchdog_info imx_sc_wdt_info = {
173986857acSAnson Huang 	.identity	= "i.MX SC watchdog timer",
174986857acSAnson Huang 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
17515f7d7fcSAnson Huang 			  WDIOF_MAGICCLOSE,
176986857acSAnson Huang };
177986857acSAnson Huang 
imx_sc_wdt_probe(struct platform_device * pdev)178986857acSAnson Huang static int imx_sc_wdt_probe(struct platform_device *pdev)
179986857acSAnson Huang {
18015f7d7fcSAnson Huang 	struct imx_sc_wdt_device *imx_sc_wdd;
18115f7d7fcSAnson Huang 	struct watchdog_device *wdog;
18201022e33SGuenter Roeck 	struct device *dev = &pdev->dev;
183986857acSAnson Huang 	int ret;
184986857acSAnson Huang 
18501022e33SGuenter Roeck 	imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
186986857acSAnson Huang 	if (!imx_sc_wdd)
187986857acSAnson Huang 		return -ENOMEM;
188986857acSAnson Huang 
189986857acSAnson Huang 	platform_set_drvdata(pdev, imx_sc_wdd);
190986857acSAnson Huang 
19115f7d7fcSAnson Huang 	wdog = &imx_sc_wdd->wdd;
19215f7d7fcSAnson Huang 	wdog->info = &imx_sc_wdt_info;
19315f7d7fcSAnson Huang 	wdog->ops = &imx_sc_wdt_ops;
19415f7d7fcSAnson Huang 	wdog->min_timeout = 1;
19515f7d7fcSAnson Huang 	wdog->max_timeout = MAX_TIMEOUT;
19615f7d7fcSAnson Huang 	wdog->parent = dev;
19715f7d7fcSAnson Huang 	wdog->timeout = DEFAULT_TIMEOUT;
198986857acSAnson Huang 
19915f7d7fcSAnson Huang 	watchdog_init_timeout(wdog, 0, dev);
200e56d48e9SFabio Estevam 
201e56d48e9SFabio Estevam 	ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout);
202e56d48e9SFabio Estevam 	if (ret)
203e56d48e9SFabio Estevam 		return ret;
204e56d48e9SFabio Estevam 
205*db60d55eSAlexander Sverdlin 	if (imx_sc_wdt_is_running())
206*db60d55eSAlexander Sverdlin 		set_bit(WDOG_HW_RUNNING, &wdog->status);
207*db60d55eSAlexander Sverdlin 
20815f7d7fcSAnson Huang 	watchdog_stop_on_reboot(wdog);
20915f7d7fcSAnson Huang 	watchdog_stop_on_unregister(wdog);
210986857acSAnson Huang 
21115f7d7fcSAnson Huang 	ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
21215f7d7fcSAnson Huang 				       SC_IRQ_WDOG,
21315f7d7fcSAnson Huang 				       true);
21415f7d7fcSAnson Huang 	if (ret) {
21515f7d7fcSAnson Huang 		dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n");
216854478a3SStefan Eichenberger 		goto register_device;
21715f7d7fcSAnson Huang 	}
21815f7d7fcSAnson Huang 
21915f7d7fcSAnson Huang 	imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify;
22015f7d7fcSAnson Huang 	ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier);
22115f7d7fcSAnson Huang 	if (ret) {
22215f7d7fcSAnson Huang 		imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
22315f7d7fcSAnson Huang 					 SC_IRQ_WDOG,
22415f7d7fcSAnson Huang 					 false);
22515f7d7fcSAnson Huang 		dev_warn(dev,
22615f7d7fcSAnson Huang 			 "Register irq notifier failed, pretimeout NOT supported\n");
227854478a3SStefan Eichenberger 		goto register_device;
22815f7d7fcSAnson Huang 	}
22915f7d7fcSAnson Huang 
23015f7d7fcSAnson Huang 	ret = devm_add_action_or_reset(dev, imx_sc_wdt_action,
23115f7d7fcSAnson Huang 				       &imx_sc_wdd->wdt_notifier);
23215f7d7fcSAnson Huang 	if (!ret)
23315f7d7fcSAnson Huang 		imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT;
23415f7d7fcSAnson Huang 	else
23515f7d7fcSAnson Huang 		dev_warn(dev, "Add action failed, pretimeout NOT supported\n");
23615f7d7fcSAnson Huang 
237854478a3SStefan Eichenberger register_device:
238854478a3SStefan Eichenberger 	return devm_watchdog_register_device(dev, wdog);
239986857acSAnson Huang }
240986857acSAnson Huang 
241986857acSAnson Huang static const struct of_device_id imx_sc_wdt_dt_ids[] = {
242986857acSAnson Huang 	{ .compatible = "fsl,imx-sc-wdt", },
243986857acSAnson Huang 	{ /* sentinel */ }
244986857acSAnson Huang };
245986857acSAnson Huang MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
246986857acSAnson Huang 
247986857acSAnson Huang static struct platform_driver imx_sc_wdt_driver = {
248986857acSAnson Huang 	.probe		= imx_sc_wdt_probe,
249986857acSAnson Huang 	.driver		= {
250986857acSAnson Huang 		.name	= "imx-sc-wdt",
251986857acSAnson Huang 		.of_match_table = imx_sc_wdt_dt_ids,
252986857acSAnson Huang 	},
253986857acSAnson Huang };
254986857acSAnson Huang module_platform_driver(imx_sc_wdt_driver);
255986857acSAnson Huang 
256986857acSAnson Huang MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
257986857acSAnson Huang MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
258986857acSAnson Huang MODULE_LICENSE("GPL v2");
259