aboutsummaryrefslogtreecommitdiff
path: root/drivers/watchdog/sandbox_alarm-wdt.c
blob: 71bb5d924ea59b8f63b3a61bd696b6920bb00e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <common.h>
#include <dm.h>
#include <os.h>
#include <wdt.h>

struct alarm_wdt_priv {
	unsigned int timeout_sec;
};

static void alarm_handler(int sig)
{
	const char *msg = "!!! ALARM !!!\n";

	os_write(2, msg, strlen(msg));
	os_fd_restore();
	os_set_alarm_handler(NULL);
	os_raise_sigalrm();
}

static int alarm_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
{
	struct alarm_wdt_priv *priv = dev_get_priv(dev);
	unsigned int sec;

	timeout = DIV_ROUND_UP(timeout, 1000);
	sec = min_t(u64, UINT_MAX, timeout);
	priv->timeout_sec = sec;

	os_alarm(0);
	os_set_alarm_handler(alarm_handler);
	os_alarm(sec);

	return 0;
}

static int alarm_wdt_stop(struct udevice *dev)
{
	os_alarm(0);
	os_set_alarm_handler(NULL);

	return 0;
}

static int alarm_wdt_reset(struct udevice *dev)
{
	struct alarm_wdt_priv *priv = dev_get_priv(dev);

	os_alarm(priv->timeout_sec);

	return 0;
}

static int alarm_wdt_expire_now(struct udevice *dev, ulong flags)
{
	alarm_handler(0);

	return 0;
}


static const struct wdt_ops alarm_wdt_ops = {
	.start = alarm_wdt_start,
	.reset = alarm_wdt_reset,
	.stop = alarm_wdt_stop,
	.expire_now = alarm_wdt_expire_now,
};

static const struct udevice_id alarm_wdt_ids[] = {
	{ .compatible = "sandbox,alarm-wdt" },
	{}
};

U_BOOT_DRIVER(alarm_wdt_sandbox) = {
	.name = "alarm_wdt_sandbox",
	.id = UCLASS_WDT,
	.of_match = alarm_wdt_ids,
	.ops = &alarm_wdt_ops,
	.priv_auto = sizeof(struct alarm_wdt_priv),
};