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