summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/watchdog.c
blob: eb82f228d532b0c32ac345e7eb594c4490a1956e (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
/* 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 <zephyr/device.h>
#include <zephyr/drivers/watchdog.h>
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>

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

LOG_MODULE_REGISTER(watchdog_shim, LOG_LEVEL_ERR);

#define wdt DEVICE_DT_GET(DT_CHOSEN(cros_ec_watchdog))

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

static void wdt_warning_handler(const struct device *wdt_dev, int channel_id)
{
	/* TODO(b/176523207): watchdog warning message */
	printk("Watchdog deadline is close!\n");
#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
}

int watchdog_init(void)
{
	int err;
	struct wdt_timeout_cfg wdt_config;

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

	/* Reset SoC when watchdog timer expires. */
	wdt_config.flags = WDT_FLAG_RESET_SOC;

	/*
	 * Set the Warning timer as CONFIG_AUX_TIMER_PERIOD_MS.
	 * Then the watchdog reset time = CONFIG_WATCHDOG_PERIOD_MS.
	 */
	wdt_config.window.min = 0U;
	wdt_config.window.max = CONFIG_AUX_TIMER_PERIOD_MS;
	wdt_config.callback = wdt_warning_handler;

	err = wdt_install_timeout(wdt, &wdt_config);

	/* If watchdog is running, reinstall it. */
	if (err == -EBUSY) {
		wdt_disable(wdt);
		err = wdt_install_timeout(wdt, &wdt_config);
	}

	if (err < 0) {
		LOG_ERR("Watchdog install error");
		return err;
	}

	err = wdt_setup(wdt, 0);
	if (err < 0) {
		LOG_ERR("Watchdog setup error");
		return err;
	}

	return EC_SUCCESS;
}

void watchdog_reload(void)
{
	if (!device_is_ready(wdt))
		LOG_ERR("Error: device %s is not ready", wdt->name);

	wdt_feed(wdt, 0);
}
DECLARE_HOOK(HOOK_TICK, watchdog_reload, HOOK_PRIO_DEFAULT);