1 /* SPDX-License-Identifier: Apache-2.0 */
2 /*
3  * Copyright (c) 2023 Intel Corporation
4  *
5  * Author: Adrian Warecki <adrian.warecki@intel.com>
6  */
7 
8 #include <zephyr/drivers/watchdog.h>
9 #include <zephyr/logging/log.h>
10 #include <zephyr/sys_clock.h>
11 #include <zephyr/math/ilog2.h>
12 
13 #include "wdt_dw_common.h"
14 #include "wdt_dw.h"
15 
16 LOG_MODULE_REGISTER(wdt_dw_common, CONFIG_WDT_LOG_LEVEL);
17 
18 #define WDT_DW_FLAG_CONFIGURED	0x80000000
19 
dw_wdt_check_options(const uint8_t options)20 int dw_wdt_check_options(const uint8_t options)
21 {
22 	if (options & WDT_OPT_PAUSE_HALTED_BY_DBG) {
23 		LOG_ERR("Pausing watchdog by debugger is not supported.");
24 		return -ENOTSUP;
25 	}
26 
27 	if (options & WDT_OPT_PAUSE_IN_SLEEP) {
28 		LOG_ERR("Pausing watchdog in sleep is not supported.");
29 		return -ENOTSUP;
30 	}
31 
32 	return 0;
33 }
34 
dw_wdt_configure(const uint32_t base,const uint32_t config)35 int dw_wdt_configure(const uint32_t base, const uint32_t config)
36 {
37 	uint32_t period;
38 
39 	if (!(config & WDT_DW_FLAG_CONFIGURED)) {
40 		LOG_ERR("Timeout not installed.");
41 		return -ENOTSUP;
42 	}
43 
44 	/* Configure timeout */
45 	period = config & ~WDT_DW_FLAG_CONFIGURED;
46 
47 	if (dw_wdt_dual_timeout_period_get(base)) {
48 		dw_wdt_timeout_period_init_set(base, period);
49 	}
50 
51 	dw_wdt_timeout_period_set(base, period);
52 
53 	/* Enable watchdog */
54 	dw_wdt_enable(base);
55 	dw_wdt_counter_restart(base);
56 
57 	return 0;
58 }
59 
dw_wdt_calc_period(const uint32_t base,const uint32_t clk_freq,const struct wdt_timeout_cfg * config,uint32_t * period_out)60 int dw_wdt_calc_period(const uint32_t base, const uint32_t clk_freq,
61 		       const struct wdt_timeout_cfg *config, uint32_t *period_out)
62 {
63 	uint64_t period64;
64 	uint32_t period;
65 
66 	/* Window timeout is not supported by this driver */
67 	if (config->window.min) {
68 		LOG_ERR("Window timeout is not supported.");
69 		return -ENOTSUP;
70 	}
71 
72 	period64 = (uint64_t)clk_freq * config->window.max;
73 	period64 /= 1000;
74 	if (!period64 || (period64 >> 31)) {
75 		LOG_ERR("Window max is out of range.");
76 		return -EINVAL;
77 	}
78 
79 	period = period64 - 1;
80 	period = ilog2(period);
81 
82 	if (period >= dw_wdt_cnt_width_get(base)) {
83 		LOG_ERR("Watchdog timeout too long.");
84 		return -EINVAL;
85 	}
86 
87 	/* The minimum counter value is 64k, maximum 2G */
88 	*period_out = WDT_DW_FLAG_CONFIGURED | (period >= 15 ? period - 15 : 0);
89 	return 0;
90 }
91 
dw_wdt_probe(const uint32_t base,const uint32_t reset_pulse_length)92 int dw_wdt_probe(const uint32_t base, const uint32_t reset_pulse_length)
93 {
94 	/* Check component type */
95 	const uint32_t type = dw_wdt_comp_type_get(base);
96 
97 	if (type != WDT_COMP_TYPE_VALUE) {
98 		LOG_ERR("Invalid component type %x", type);
99 		return -ENODEV;
100 	}
101 
102 	dw_wdt_reset_pulse_length_set(base, reset_pulse_length);
103 
104 	return 0;
105 }
106 
dw_wdt_disable(const struct device * dev)107 int dw_wdt_disable(const struct device *dev)
108 {
109 	return -ENOTSUP;
110 }
111