blob: 8a659ee9b972f4bb6084d7429e113647a447b7db [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +02004 */
5
6#include <common.h>
7#include <dm.h>
Samuel Hollanda8f63d12021-11-03 22:55:14 -05008#include <dm/device-internal.h>
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +02009#include <errno.h>
Samuel Hollanda8f63d12021-11-03 22:55:14 -050010#include <malloc.h>
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020011#include <sysreset.h>
12#include <wdt.h>
13
Samuel Holland5544a012021-11-03 22:55:13 -050014struct wdt_reboot_plat {
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020015 struct udevice *wdt;
16};
17
18static int wdt_reboot_request(struct udevice *dev, enum sysreset_t type)
19{
Samuel Holland5544a012021-11-03 22:55:13 -050020 struct wdt_reboot_plat *plat = dev_get_plat(dev);
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020021 int ret;
22
Heinrich Schuchardt88c4cbe2021-11-04 10:31:17 +010023 switch (type) {
24 case SYSRESET_COLD:
25 case SYSRESET_WARM:
26 ret = wdt_expire_now(plat->wdt, 0);
27 if (ret)
28 return ret;
29 break;
30 default:
31 return -ENOSYS;
32 }
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020033
34 return -EINPROGRESS;
35}
36
37static struct sysreset_ops wdt_reboot_ops = {
38 .request = wdt_reboot_request,
39};
40
Samuel Holland5544a012021-11-03 22:55:13 -050041static int wdt_reboot_of_to_plat(struct udevice *dev)
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020042{
Samuel Holland5544a012021-11-03 22:55:13 -050043 struct wdt_reboot_plat *plat = dev_get_plat(dev);
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020044 int err;
45
46 err = uclass_get_device_by_phandle(UCLASS_WDT, dev,
Samuel Holland5544a012021-11-03 22:55:13 -050047 "wdt", &plat->wdt);
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020048 if (err) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090049 pr_err("unable to find wdt device\n");
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020050 return err;
51 }
52
53 return 0;
54}
55
56static const struct udevice_id wdt_reboot_ids[] = {
57 { .compatible = "wdt-reboot" },
58 { /* sentinel */ }
59};
60
61U_BOOT_DRIVER(wdt_reboot) = {
62 .name = "wdt_reboot",
63 .id = UCLASS_SYSRESET,
64 .of_match = wdt_reboot_ids,
Samuel Holland5544a012021-11-03 22:55:13 -050065 .of_to_plat = wdt_reboot_of_to_plat,
66 .plat_auto = sizeof(struct wdt_reboot_plat),
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020067 .ops = &wdt_reboot_ops,
Álvaro Fernández Rojas17a0c142017-05-16 18:29:13 +020068};
Samuel Hollanda8f63d12021-11-03 22:55:14 -050069
70#if IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)
71int sysreset_register_wdt(struct udevice *dev)
72{
73 struct wdt_reboot_plat *plat = malloc(sizeof(*plat));
74 int ret;
75
76 if (!plat)
77 return -ENOMEM;
78
79 plat->wdt = dev;
80
81 ret = device_bind(dev, DM_DRIVER_GET(wdt_reboot),
82 dev->name, plat, ofnode_null(), NULL);
83 if (ret) {
84 free(plat);
85 return ret;
86 }
87
88 return 0;
89}
90#endif