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