blob: 71bb5d924ea59b8f63b3a61bd696b6920bb00e29 [file] [log] [blame]
Rasmus Villemoesae400f32022-09-27 11:54:04 +02001#include <common.h>
2#include <dm.h>
3#include <os.h>
4#include <wdt.h>
5
6struct alarm_wdt_priv {
7 unsigned int timeout_sec;
8};
9
10static void alarm_handler(int sig)
11{
12 const char *msg = "!!! ALARM !!!\n";
13
14 os_write(2, msg, strlen(msg));
15 os_fd_restore();
16 os_set_alarm_handler(NULL);
17 os_raise_sigalrm();
18}
19
20static int alarm_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
21{
22 struct alarm_wdt_priv *priv = dev_get_priv(dev);
23 unsigned int sec;
24
25 timeout = DIV_ROUND_UP(timeout, 1000);
26 sec = min_t(u64, UINT_MAX, timeout);
27 priv->timeout_sec = sec;
28
29 os_alarm(0);
30 os_set_alarm_handler(alarm_handler);
31 os_alarm(sec);
32
33 return 0;
34}
35
36static int alarm_wdt_stop(struct udevice *dev)
37{
38 os_alarm(0);
39 os_set_alarm_handler(NULL);
40
41 return 0;
42}
43
44static int alarm_wdt_reset(struct udevice *dev)
45{
46 struct alarm_wdt_priv *priv = dev_get_priv(dev);
47
48 os_alarm(priv->timeout_sec);
49
50 return 0;
51}
52
53static int alarm_wdt_expire_now(struct udevice *dev, ulong flags)
54{
55 alarm_handler(0);
56
57 return 0;
58}
59
60
61static const struct wdt_ops alarm_wdt_ops = {
62 .start = alarm_wdt_start,
63 .reset = alarm_wdt_reset,
64 .stop = alarm_wdt_stop,
65 .expire_now = alarm_wdt_expire_now,
66};
67
68static const struct udevice_id alarm_wdt_ids[] = {
69 { .compatible = "sandbox,alarm-wdt" },
70 {}
71};
72
73U_BOOT_DRIVER(alarm_wdt_sandbox) = {
74 .name = "alarm_wdt_sandbox",
75 .id = UCLASS_WDT,
76 .of_match = alarm_wdt_ids,
77 .ops = &alarm_wdt_ops,
78 .priv_auto = sizeof(struct alarm_wdt_priv),
79};