blob: 6db5aa75b54173e73994df5b9b31dfaf7291fed6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +02004 */
5
6#include <common.h>
7#include <dm.h>
Samuel Holland3a8713a2021-11-03 22:55:14 -05008#include <dm/device-internal.h>
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +02009#include <errno.h>
Samuel Holland3a8713a2021-11-03 22:55:14 -050010#include <malloc.h>
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020011#include <sysreset.h>
12#include <wdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060013#include <linux/printk.h>
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020014
Samuel Holland90722ba2021-11-03 22:55:13 -050015struct wdt_reboot_plat {
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020016 struct udevice *wdt;
17};
18
19static int wdt_reboot_request(struct udevice *dev, enum sysreset_t type)
20{
Samuel Holland90722ba2021-11-03 22:55:13 -050021 struct wdt_reboot_plat *plat = dev_get_plat(dev);
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020022 int ret;
23
Heinrich Schuchardt9a41ed32021-11-04 10:31:17 +010024 switch (type) {
25 case SYSRESET_COLD:
26 case SYSRESET_WARM:
27 ret = wdt_expire_now(plat->wdt, 0);
28 if (ret)
29 return ret;
30 break;
31 default:
Paul Barkerbdb3a3f2023-11-08 08:51:10 +000032 return -EPROTONOSUPPORT;
Heinrich Schuchardt9a41ed32021-11-04 10:31:17 +010033 }
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020034
35 return -EINPROGRESS;
36}
37
38static struct sysreset_ops wdt_reboot_ops = {
39 .request = wdt_reboot_request,
40};
41
Samuel Holland90722ba2021-11-03 22:55:13 -050042static int wdt_reboot_of_to_plat(struct udevice *dev)
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020043{
Samuel Holland90722ba2021-11-03 22:55:13 -050044 struct wdt_reboot_plat *plat = dev_get_plat(dev);
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020045 int err;
46
47 err = uclass_get_device_by_phandle(UCLASS_WDT, dev,
Samuel Holland90722ba2021-11-03 22:55:13 -050048 "wdt", &plat->wdt);
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020049 if (err) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090050 pr_err("unable to find wdt device\n");
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020051 return err;
52 }
53
54 return 0;
55}
56
57static const struct udevice_id wdt_reboot_ids[] = {
58 { .compatible = "wdt-reboot" },
59 { /* sentinel */ }
60};
61
62U_BOOT_DRIVER(wdt_reboot) = {
63 .name = "wdt_reboot",
64 .id = UCLASS_SYSRESET,
65 .of_match = wdt_reboot_ids,
Samuel Holland90722ba2021-11-03 22:55:13 -050066 .of_to_plat = wdt_reboot_of_to_plat,
67 .plat_auto = sizeof(struct wdt_reboot_plat),
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020068 .ops = &wdt_reboot_ops,
Álvaro Fernández Rojas0da081e2017-05-16 18:29:13 +020069};
Samuel Holland3a8713a2021-11-03 22:55:14 -050070
71#if IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)
72int sysreset_register_wdt(struct udevice *dev)
73{
74 struct wdt_reboot_plat *plat = malloc(sizeof(*plat));
75 int ret;
76
77 if (!plat)
78 return -ENOMEM;
79
80 plat->wdt = dev;
81
82 ret = device_bind(dev, DM_DRIVER_GET(wdt_reboot),
83 dev->name, plat, ofnode_null(), NULL);
84 if (ret) {
85 free(plat);
86 return ret;
87 }
88
89 return 0;
90}
91#endif