summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/watchdog.c
blob: 0b96cbaa0e3434248cea237b74cdb4d40cbacf59 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "config.h"
#include "hooks.h"
#include "watchdog.h"

#include <zephyr/device.h>
#include <zephyr/drivers/watchdog.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(watchdog_shim, LOG_LEVEL_ERR);

struct watchdog_info {
	const struct device *wdt_dev;
	struct wdt_timeout_cfg config;
};

__maybe_unused static void wdt_warning_handler(const struct device *wdt_dev,
					       int channel_id);

const struct watchdog_info wdt_info[] = {
	{
		.wdt_dev = DEVICE_DT_GET(DT_CHOSEN(cros_ec_watchdog)),
		.config = {
#if DT_NODE_HAS_COMPAT(DT_CHOSEN(cros_ec_watchdog), st_stm32_watchdog)
			.flags = WDT_FLAG_RESET_SOC,
			.window.min = 0U,
			.window.max = CONFIG_WATCHDOG_PERIOD_MS,
			.callback = NULL,
#else
			.flags = WDT_FLAG_RESET_SOC,
			.window.min = 0U,
			.window.max = CONFIG_AUX_TIMER_PERIOD_MS,
			.callback = wdt_warning_handler,
#endif
		},
	},
#ifdef CONFIG_PLATFORM_EC_WATCHDOG_HELPER
	{
		.wdt_dev = DEVICE_DT_GET(DT_CHOSEN(cros_ec_watchdog_helper)),
		.config = {
			.flags = 0U,
			.window.min = 0U,
			.window.max = CONFIG_AUX_TIMER_PERIOD_MS,
			.callback = wdt_warning_handler,
		},
	},
#endif
};

/* Array to keep channel used to implement watchdog */
int wdt_chan[ARRAY_SIZE(wdt_info)];
bool watchdog_initialized;

#ifdef TEST_BUILD
bool wdt_warning_triggered;
#endif /* TEST_BUILD */

static int watchdog_config(const struct watchdog_info *info)
{
	const struct device *wdt_dev = info->wdt_dev;
	const struct wdt_timeout_cfg *config = &info->config;
	int chan;

	chan = wdt_install_timeout(wdt_dev, config);

	/* If watchdog is running, reinstall it. */
	if (chan == -EBUSY) {
		wdt_disable(wdt_dev);
		chan = wdt_install_timeout(wdt_dev, config);
	}

	if (chan < 0) {
		LOG_ERR("Watchdog install error: %d", chan);
	}

	return chan;
}

static int watchdog_enable(const struct device *wdt_dev)
{
	int err;

	err = wdt_setup(wdt_dev, 0);
	if (err < 0)
		LOG_ERR("Watchdog %s setup error: %d", wdt_dev->name, err);

	return err;
}

static int watchdog_init_device(const struct watchdog_info *info)
{
	const struct device *wdt_dev = info->wdt_dev;
	int chan, err;

	if (!device_is_ready(wdt_dev)) {
		LOG_ERR("Error: device %s is not ready", wdt_dev->name);
		return -ENODEV;
	}

	chan = watchdog_config(info);
	if (chan < 0)
		return chan;

	err = watchdog_enable(wdt_dev);
	if (err < 0)
		return err;

	return chan;
}

int watchdog_init(void)
{
	int err = EC_SUCCESS;

	if (watchdog_initialized)
		return -EBUSY;

	for (int i = 0; i < ARRAY_SIZE(wdt_info); i++) {
		wdt_chan[i] = watchdog_init_device(&wdt_info[i]);
		if (wdt_chan[i] < 0 && err == EC_SUCCESS)
			err = wdt_chan[i];
	}

	watchdog_initialized = true;
	watchdog_reload();

	return err;
}

void watchdog_reload(void)
{
	if (!watchdog_initialized)
		return;

	for (int i = 0; i < ARRAY_SIZE(wdt_info); i++) {
		if (wdt_chan[i] < 0)
			continue;

		wdt_feed(wdt_info[i].wdt_dev, wdt_chan[i]);
	}
}
DECLARE_HOOK(HOOK_TICK, watchdog_reload, HOOK_PRIO_DEFAULT);

__maybe_unused static void wdt_warning_handler(const struct device *wdt_dev,
					       int channel_id)
{
	const char *thread_name = k_thread_name_get(k_current_get());

#ifdef CONFIG_RISCV
	printk("WDT pre-warning MEPC:%p THREAD_NAME:%s\n",
	       (void *)csr_read(mepc), thread_name);
#else
	/* TODO(b/176523207): watchdog warning message */
	printk("Watchdog deadline is close! THREAD_NAME:%s\n", thread_name);
#endif
#ifdef TEST_BUILD
	wdt_warning_triggered = true;
#endif
#ifdef CONFIG_SOC_SERIES_MEC172X
	extern void cros_chip_wdt_handler(const struct device *wdt_dev,
					  int channel_id);
	cros_chip_wdt_handler(wdt_dev, channel_id);
#endif

	/* Watchdog is disabled after calling handler. Re-enable it now. */
	watchdog_enable(wdt_dev);
}