1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun * Driver for watchdog aspect of for Zodiac Inflight Innovations RAVE
5*4882a593Smuzhiyun * Supervisory Processor(SP) MCU
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2017 Zodiac Inflight Innovation
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/mfd/rave-sp.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/nvmem-consumer.h>
16*4882a593Smuzhiyun #include <linux/of_device.h>
17*4882a593Smuzhiyun #include <linux/platform_device.h>
18*4882a593Smuzhiyun #include <linux/reboot.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/watchdog.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun enum {
23*4882a593Smuzhiyun RAVE_SP_RESET_BYTE = 1,
24*4882a593Smuzhiyun RAVE_SP_RESET_REASON_NORMAL = 0,
25*4882a593Smuzhiyun RAVE_SP_RESET_DELAY_MS = 500,
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun * struct rave_sp_wdt_variant - RAVE SP watchdog variant
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * @max_timeout: Largest possible watchdog timeout setting
32*4882a593Smuzhiyun * @min_timeout: Smallest possible watchdog timeout setting
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * @configure: Function to send configuration command
35*4882a593Smuzhiyun * @restart: Function to send "restart" command
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun struct rave_sp_wdt_variant {
38*4882a593Smuzhiyun unsigned int max_timeout;
39*4882a593Smuzhiyun unsigned int min_timeout;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun int (*configure)(struct watchdog_device *, bool);
42*4882a593Smuzhiyun int (*restart)(struct watchdog_device *);
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun * struct rave_sp_wdt - RAVE SP watchdog
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * @wdd: Underlying watchdog device
49*4882a593Smuzhiyun * @sp: Pointer to parent RAVE SP device
50*4882a593Smuzhiyun * @variant: Device specific variant information
51*4882a593Smuzhiyun * @reboot_notifier: Reboot notifier implementing machine reset
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun struct rave_sp_wdt {
54*4882a593Smuzhiyun struct watchdog_device wdd;
55*4882a593Smuzhiyun struct rave_sp *sp;
56*4882a593Smuzhiyun const struct rave_sp_wdt_variant *variant;
57*4882a593Smuzhiyun struct notifier_block reboot_notifier;
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
to_rave_sp_wdt(struct watchdog_device * wdd)60*4882a593Smuzhiyun static struct rave_sp_wdt *to_rave_sp_wdt(struct watchdog_device *wdd)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return container_of(wdd, struct rave_sp_wdt, wdd);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
rave_sp_wdt_exec(struct watchdog_device * wdd,void * data,size_t data_size)65*4882a593Smuzhiyun static int rave_sp_wdt_exec(struct watchdog_device *wdd, void *data,
66*4882a593Smuzhiyun size_t data_size)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun return rave_sp_exec(to_rave_sp_wdt(wdd)->sp,
69*4882a593Smuzhiyun data, data_size, NULL, 0);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
rave_sp_wdt_legacy_configure(struct watchdog_device * wdd,bool on)72*4882a593Smuzhiyun static int rave_sp_wdt_legacy_configure(struct watchdog_device *wdd, bool on)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun u8 cmd[] = {
75*4882a593Smuzhiyun [0] = RAVE_SP_CMD_SW_WDT,
76*4882a593Smuzhiyun [1] = 0,
77*4882a593Smuzhiyun [2] = 0,
78*4882a593Smuzhiyun [3] = on,
79*4882a593Smuzhiyun [4] = on ? wdd->timeout : 0,
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
rave_sp_wdt_rdu_configure(struct watchdog_device * wdd,bool on)85*4882a593Smuzhiyun static int rave_sp_wdt_rdu_configure(struct watchdog_device *wdd, bool on)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun u8 cmd[] = {
88*4882a593Smuzhiyun [0] = RAVE_SP_CMD_SW_WDT,
89*4882a593Smuzhiyun [1] = 0,
90*4882a593Smuzhiyun [2] = on,
91*4882a593Smuzhiyun [3] = (u8)wdd->timeout,
92*4882a593Smuzhiyun [4] = (u8)(wdd->timeout >> 8),
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * rave_sp_wdt_configure - Configure watchdog device
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * @wdd: Device to configure
102*4882a593Smuzhiyun * @on: Desired state of the watchdog timer (ON/OFF)
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * This function configures two aspects of the watchdog timer:
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * - Wheither it is ON or OFF
107*4882a593Smuzhiyun * - Its timeout duration
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * with first aspect specified via function argument and second via
110*4882a593Smuzhiyun * the value of 'wdd->timeout'.
111*4882a593Smuzhiyun */
rave_sp_wdt_configure(struct watchdog_device * wdd,bool on)112*4882a593Smuzhiyun static int rave_sp_wdt_configure(struct watchdog_device *wdd, bool on)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun return to_rave_sp_wdt(wdd)->variant->configure(wdd, on);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
rave_sp_wdt_legacy_restart(struct watchdog_device * wdd)117*4882a593Smuzhiyun static int rave_sp_wdt_legacy_restart(struct watchdog_device *wdd)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun u8 cmd[] = {
120*4882a593Smuzhiyun [0] = RAVE_SP_CMD_RESET,
121*4882a593Smuzhiyun [1] = 0,
122*4882a593Smuzhiyun [2] = RAVE_SP_RESET_BYTE
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
rave_sp_wdt_rdu_restart(struct watchdog_device * wdd)128*4882a593Smuzhiyun static int rave_sp_wdt_rdu_restart(struct watchdog_device *wdd)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun u8 cmd[] = {
131*4882a593Smuzhiyun [0] = RAVE_SP_CMD_RESET,
132*4882a593Smuzhiyun [1] = 0,
133*4882a593Smuzhiyun [2] = RAVE_SP_RESET_BYTE,
134*4882a593Smuzhiyun [3] = RAVE_SP_RESET_REASON_NORMAL
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
rave_sp_wdt_reboot_notifier(struct notifier_block * nb,unsigned long action,void * data)140*4882a593Smuzhiyun static int rave_sp_wdt_reboot_notifier(struct notifier_block *nb,
141*4882a593Smuzhiyun unsigned long action, void *data)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * Restart handler is called in atomic context which means we
145*4882a593Smuzhiyun * can't communicate to SP via UART. Luckily for use SP will
146*4882a593Smuzhiyun * wait 500ms before actually resetting us, so we ask it to do
147*4882a593Smuzhiyun * so here and let the rest of the system go on wrapping
148*4882a593Smuzhiyun * things up.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun if (action == SYS_DOWN || action == SYS_HALT) {
151*4882a593Smuzhiyun struct rave_sp_wdt *sp_wd =
152*4882a593Smuzhiyun container_of(nb, struct rave_sp_wdt, reboot_notifier);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun const int ret = sp_wd->variant->restart(&sp_wd->wdd);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (ret < 0)
157*4882a593Smuzhiyun dev_err(sp_wd->wdd.parent,
158*4882a593Smuzhiyun "Failed to issue restart command (%d)", ret);
159*4882a593Smuzhiyun return NOTIFY_OK;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return NOTIFY_DONE;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
rave_sp_wdt_restart(struct watchdog_device * wdd,unsigned long action,void * data)165*4882a593Smuzhiyun static int rave_sp_wdt_restart(struct watchdog_device *wdd,
166*4882a593Smuzhiyun unsigned long action, void *data)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * The actual work was done by reboot notifier above. SP
170*4882a593Smuzhiyun * firmware waits 500 ms before issuing reset, so let's hang
171*4882a593Smuzhiyun * here for twice that delay and hopefuly we'd never reach
172*4882a593Smuzhiyun * the return statement.
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun mdelay(2 * RAVE_SP_RESET_DELAY_MS);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return -EIO;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
rave_sp_wdt_start(struct watchdog_device * wdd)179*4882a593Smuzhiyun static int rave_sp_wdt_start(struct watchdog_device *wdd)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun int ret;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun ret = rave_sp_wdt_configure(wdd, true);
184*4882a593Smuzhiyun if (!ret)
185*4882a593Smuzhiyun set_bit(WDOG_HW_RUNNING, &wdd->status);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun return ret;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
rave_sp_wdt_stop(struct watchdog_device * wdd)190*4882a593Smuzhiyun static int rave_sp_wdt_stop(struct watchdog_device *wdd)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun return rave_sp_wdt_configure(wdd, false);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
rave_sp_wdt_set_timeout(struct watchdog_device * wdd,unsigned int timeout)195*4882a593Smuzhiyun static int rave_sp_wdt_set_timeout(struct watchdog_device *wdd,
196*4882a593Smuzhiyun unsigned int timeout)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun wdd->timeout = timeout;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return rave_sp_wdt_configure(wdd, watchdog_active(wdd));
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
rave_sp_wdt_ping(struct watchdog_device * wdd)203*4882a593Smuzhiyun static int rave_sp_wdt_ping(struct watchdog_device *wdd)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun u8 cmd[] = {
206*4882a593Smuzhiyun [0] = RAVE_SP_CMD_PET_WDT,
207*4882a593Smuzhiyun [1] = 0,
208*4882a593Smuzhiyun };
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun static const struct watchdog_info rave_sp_wdt_info = {
214*4882a593Smuzhiyun .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
215*4882a593Smuzhiyun .identity = "RAVE SP Watchdog",
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun static const struct watchdog_ops rave_sp_wdt_ops = {
219*4882a593Smuzhiyun .owner = THIS_MODULE,
220*4882a593Smuzhiyun .start = rave_sp_wdt_start,
221*4882a593Smuzhiyun .stop = rave_sp_wdt_stop,
222*4882a593Smuzhiyun .ping = rave_sp_wdt_ping,
223*4882a593Smuzhiyun .set_timeout = rave_sp_wdt_set_timeout,
224*4882a593Smuzhiyun .restart = rave_sp_wdt_restart,
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun static const struct rave_sp_wdt_variant rave_sp_wdt_legacy = {
228*4882a593Smuzhiyun .max_timeout = 255,
229*4882a593Smuzhiyun .min_timeout = 1,
230*4882a593Smuzhiyun .configure = rave_sp_wdt_legacy_configure,
231*4882a593Smuzhiyun .restart = rave_sp_wdt_legacy_restart,
232*4882a593Smuzhiyun };
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun static const struct rave_sp_wdt_variant rave_sp_wdt_rdu = {
235*4882a593Smuzhiyun .max_timeout = 180,
236*4882a593Smuzhiyun .min_timeout = 60,
237*4882a593Smuzhiyun .configure = rave_sp_wdt_rdu_configure,
238*4882a593Smuzhiyun .restart = rave_sp_wdt_rdu_restart,
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun static const struct of_device_id rave_sp_wdt_of_match[] = {
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun .compatible = "zii,rave-sp-watchdog-legacy",
244*4882a593Smuzhiyun .data = &rave_sp_wdt_legacy,
245*4882a593Smuzhiyun },
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun .compatible = "zii,rave-sp-watchdog",
248*4882a593Smuzhiyun .data = &rave_sp_wdt_rdu,
249*4882a593Smuzhiyun },
250*4882a593Smuzhiyun { /* sentinel */ }
251*4882a593Smuzhiyun };
252*4882a593Smuzhiyun
rave_sp_wdt_probe(struct platform_device * pdev)253*4882a593Smuzhiyun static int rave_sp_wdt_probe(struct platform_device *pdev)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun struct device *dev = &pdev->dev;
256*4882a593Smuzhiyun struct watchdog_device *wdd;
257*4882a593Smuzhiyun struct rave_sp_wdt *sp_wd;
258*4882a593Smuzhiyun struct nvmem_cell *cell;
259*4882a593Smuzhiyun __le16 timeout = 0;
260*4882a593Smuzhiyun int ret;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun sp_wd = devm_kzalloc(dev, sizeof(*sp_wd), GFP_KERNEL);
263*4882a593Smuzhiyun if (!sp_wd)
264*4882a593Smuzhiyun return -ENOMEM;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun sp_wd->variant = of_device_get_match_data(dev);
267*4882a593Smuzhiyun sp_wd->sp = dev_get_drvdata(dev->parent);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun wdd = &sp_wd->wdd;
270*4882a593Smuzhiyun wdd->parent = dev;
271*4882a593Smuzhiyun wdd->info = &rave_sp_wdt_info;
272*4882a593Smuzhiyun wdd->ops = &rave_sp_wdt_ops;
273*4882a593Smuzhiyun wdd->min_timeout = sp_wd->variant->min_timeout;
274*4882a593Smuzhiyun wdd->max_timeout = sp_wd->variant->max_timeout;
275*4882a593Smuzhiyun wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
276*4882a593Smuzhiyun wdd->timeout = 60;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun cell = nvmem_cell_get(dev, "wdt-timeout");
279*4882a593Smuzhiyun if (!IS_ERR(cell)) {
280*4882a593Smuzhiyun size_t len;
281*4882a593Smuzhiyun void *value = nvmem_cell_read(cell, &len);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun if (!IS_ERR(value)) {
284*4882a593Smuzhiyun memcpy(&timeout, value, min(len, sizeof(timeout)));
285*4882a593Smuzhiyun kfree(value);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun nvmem_cell_put(cell);
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun watchdog_init_timeout(wdd, le16_to_cpu(timeout), dev);
290*4882a593Smuzhiyun watchdog_set_restart_priority(wdd, 255);
291*4882a593Smuzhiyun watchdog_stop_on_unregister(wdd);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun sp_wd->reboot_notifier.notifier_call = rave_sp_wdt_reboot_notifier;
294*4882a593Smuzhiyun ret = devm_register_reboot_notifier(dev, &sp_wd->reboot_notifier);
295*4882a593Smuzhiyun if (ret) {
296*4882a593Smuzhiyun dev_err(dev, "Failed to register reboot notifier\n");
297*4882a593Smuzhiyun return ret;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /*
301*4882a593Smuzhiyun * We don't know if watchdog is running now. To be sure, let's
302*4882a593Smuzhiyun * start it and depend on watchdog core to ping it
303*4882a593Smuzhiyun */
304*4882a593Smuzhiyun wdd->max_hw_heartbeat_ms = wdd->max_timeout * 1000;
305*4882a593Smuzhiyun ret = rave_sp_wdt_start(wdd);
306*4882a593Smuzhiyun if (ret) {
307*4882a593Smuzhiyun dev_err(dev, "Watchdog didn't start\n");
308*4882a593Smuzhiyun return ret;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun ret = devm_watchdog_register_device(dev, wdd);
312*4882a593Smuzhiyun if (ret) {
313*4882a593Smuzhiyun rave_sp_wdt_stop(wdd);
314*4882a593Smuzhiyun return ret;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun static struct platform_driver rave_sp_wdt_driver = {
321*4882a593Smuzhiyun .probe = rave_sp_wdt_probe,
322*4882a593Smuzhiyun .driver = {
323*4882a593Smuzhiyun .name = KBUILD_MODNAME,
324*4882a593Smuzhiyun .of_match_table = rave_sp_wdt_of_match,
325*4882a593Smuzhiyun },
326*4882a593Smuzhiyun };
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun module_platform_driver(rave_sp_wdt_driver);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, rave_sp_wdt_of_match);
331*4882a593Smuzhiyun MODULE_LICENSE("GPL");
332*4882a593Smuzhiyun MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
333*4882a593Smuzhiyun MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
334*4882a593Smuzhiyun MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
335*4882a593Smuzhiyun MODULE_DESCRIPTION("RAVE SP Watchdog driver");
336*4882a593Smuzhiyun MODULE_ALIAS("platform:rave-sp-watchdog");
337