1 /*
2  * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <sysreset.h>
11 #include <wdt.h>
12 
13 struct wdt_reboot_priv {
14 	struct udevice *wdt;
15 };
16 
17 static int wdt_reboot_request(struct udevice *dev, enum sysreset_t type)
18 {
19 	struct wdt_reboot_priv *priv = dev_get_priv(dev);
20 	int ret;
21 
22 	ret = wdt_expire_now(priv->wdt, 0);
23 	if (ret)
24 		return ret;
25 
26 	return -EINPROGRESS;
27 }
28 
29 static struct sysreset_ops wdt_reboot_ops = {
30 	.request = wdt_reboot_request,
31 };
32 
33 int wdt_reboot_probe(struct udevice *dev)
34 {
35 	struct wdt_reboot_priv *priv = dev_get_priv(dev);
36 	int err;
37 
38 	err = uclass_get_device_by_phandle(UCLASS_WDT, dev,
39 					   "wdt", &priv->wdt);
40 	if (err) {
41 		pr_err("unable to find wdt device\n");
42 		return err;
43 	}
44 
45 	return 0;
46 }
47 
48 static const struct udevice_id wdt_reboot_ids[] = {
49 	{ .compatible = "wdt-reboot" },
50 	{ /* sentinel */ }
51 };
52 
53 U_BOOT_DRIVER(wdt_reboot) = {
54 	.name = "wdt_reboot",
55 	.id = UCLASS_SYSRESET,
56 	.of_match = wdt_reboot_ids,
57 	.ops = &wdt_reboot_ops,
58 	.priv_auto_alloc_size = sizeof(struct wdt_reboot_priv),
59 	.probe = wdt_reboot_probe,
60 };
61