1 /*
2  * Copyright (c) 2024 ENE Technology Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ene_kb1200_watchdog
8 
9 #include <zephyr/irq.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/watchdog.h>
12 #include <errno.h>
13 #include <reg/wdt.h>
14 
15 /* Device config */
16 struct wdt_kb1200_config {
17 	struct wdt_regs *wdt;
18 };
19 
20 /* Device data */
21 struct wdt_kb1200_data {
22 	wdt_callback_t cb;
23 	bool timeout_installed;
24 };
25 
26 /* WDT api functions */
wdt_kb1200_setup(const struct device * dev,uint8_t options)27 static int wdt_kb1200_setup(const struct device *dev, uint8_t options)
28 {
29 	struct wdt_kb1200_config const *cfg = dev->config;
30 	struct wdt_kb1200_data *data = dev->data;
31 
32 	if (!data->timeout_installed) {
33 		printk("No valid WDT timeout installed");
34 		return -EINVAL;
35 	}
36 	if (options & WDT_OPT_PAUSE_HALTED_BY_DBG) {
37 		printk("WDT_OPT_PAUSE_HALTED_BY_DBG is not supported");
38 		return -ENOTSUP;
39 	}
40 
41 	/* Setting Clock Source */
42 	if (options & WDT_OPT_PAUSE_IN_SLEEP) {
43 		cfg->wdt->WDTCFG = WDT_ADCO32K;
44 	} else {
45 		cfg->wdt->WDTCFG = WDT_PHER32K;
46 	}
47 	/* Clear Pending Flag */
48 	cfg->wdt->WDTPF = (WDT_HALF_WAY_EVENT | WDT_RESET_EVENT);
49 	/* WDT enable */
50 	cfg->wdt->WDTCFG |= WDT_FUNCTON_ENABLE;
51 
52 	return 0;
53 }
54 
wdt_kb1200_disable(const struct device * dev)55 static int wdt_kb1200_disable(const struct device *dev)
56 {
57 	struct wdt_kb1200_config const *cfg = dev->config;
58 	struct wdt_kb1200_data *data = dev->data;
59 
60 	if (!(cfg->wdt->WDTCFG & WDT_FUNCTON_ENABLE)) {
61 		return -EALREADY;
62 	}
63 	/* WDT disable, write bit 7~4 = 1001b */
64 	cfg->wdt->WDTCFG = (cfg->wdt->WDTCFG & ~WDT_FUNCTON_ENABLE) | WDT_DISABLE_PASSWORD;
65 	/* Clear Pending Flag */
66 	cfg->wdt->WDTPF = (WDT_HALF_WAY_EVENT | WDT_RESET_EVENT);
67 	/* Need disable IE,or the wdt-half-event interrupt will be entered */
68 	cfg->wdt->WDTIE &= ~WDT_HALF_WAY_EVENT;
69 	data->timeout_installed = false;
70 
71 	return 0;
72 }
73 
wdt_kb1200_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * config)74 static int wdt_kb1200_install_timeout(const struct device *dev,
75 				      const struct wdt_timeout_cfg *config)
76 {
77 	struct wdt_kb1200_config const *cfg = dev->config;
78 	struct wdt_kb1200_data *data = dev->data;
79 
80 	/* Watchdog Counter Match Value */
81 	if (config->window.min > 0U) {
82 		data->timeout_installed = false;
83 		return -EINVAL;
84 	}
85 	cfg->wdt->WDTM = (config->window.max * 1000) / WDT_TICK_TIME_US;
86 	/* (HW design) The counter match value must be >= 3 */
87 	if (cfg->wdt->WDTM < WDT_MIN_CNT) {
88 		data->timeout_installed = false;
89 		return -EINVAL;
90 	}
91 
92 	/* Watchdog behavior flags */
93 	if ((config->flags & WDT_FLAG_RESET_MASK) == WDT_FLAG_RESET_SOC) {
94 		/* Reset: SoC */
95 		cfg->wdt->WDTCFG_T = WDT_RESET_WHOLE_CHIP_WO_GPIO;
96 	} else if ((config->flags & WDT_FLAG_RESET_MASK) == WDT_FLAG_RESET_CPU_CORE) {
97 		/* Reset: CPU core */
98 		cfg->wdt->WDTCFG_T = WDT_RESET_WHOLE_CHIP;
99 	} else {
100 		/* Reset: none */
101 		cfg->wdt->WDTCFG_T = WDT_RESET_ONLY_MCU;
102 	}
103 
104 	/* Watchdog callback function */
105 	data->cb = config->callback;
106 	if (data->cb) {
107 		cfg->wdt->WDTIE |= WDT_HALF_WAY_EVENT;
108 	} else {
109 		/* If the callback function is NULL,the SoC will be reset directly.
110 		 * But still need enable interrupt.
111 		 */
112 		cfg->wdt->WDTIE |= WDT_HALF_WAY_EVENT;
113 	}
114 	data->timeout_installed = true;
115 
116 	return 0;
117 }
118 
wdt_kb1200_feed(const struct device * dev,int channel_id)119 static int wdt_kb1200_feed(const struct device *dev, int channel_id)
120 {
121 	struct wdt_kb1200_config const *cfg = dev->config;
122 
123 	ARG_UNUSED(dev);
124 	ARG_UNUSED(channel_id);
125 
126 	if (!(cfg->wdt->WDTCFG & WDT_FUNCTON_ENABLE)) {
127 		return -EINVAL;
128 	}
129 	/* Re-enable to reset counter */
130 	cfg->wdt->WDTCFG |= WDT_FUNCTON_ENABLE;
131 	/* Clear Pending Flag */
132 	cfg->wdt->WDTPF = WDT_HALF_WAY_EVENT;
133 
134 	return 0;
135 }
136 
wdt_kb1200_isr(const struct device * dev)137 static void wdt_kb1200_isr(const struct device *dev)
138 {
139 	struct wdt_kb1200_data *data = dev->data;
140 
141 	if (data->cb) {
142 		data->cb(dev, 0);
143 	}
144 }
145 
146 static DEVICE_API(wdt, wdt_kb1200_api) = {
147 	.setup = wdt_kb1200_setup,
148 	.disable = wdt_kb1200_disable,
149 	.install_timeout = wdt_kb1200_install_timeout,
150 	.feed = wdt_kb1200_feed,
151 };
152 
wdt_kb1200_init(const struct device * dev)153 static int wdt_kb1200_init(const struct device *dev)
154 {
155 	if (IS_ENABLED(CONFIG_WDT_DISABLE_AT_BOOT)) {
156 		wdt_kb1200_disable(dev);
157 	}
158 
159 	IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), wdt_kb1200_isr,
160 		    DEVICE_DT_INST_GET(0), 0);
161 	irq_enable(DT_INST_IRQN(0));
162 
163 	return 0;
164 }
165 
166 static const struct wdt_kb1200_config wdt_kb1200_config = {
167 	.wdt = (struct wdt_regs *)DT_INST_REG_ADDR(0),
168 };
169 
170 static struct wdt_kb1200_data wdt_kb1200_dev_data;
171 
172 DEVICE_DT_INST_DEFINE(0, wdt_kb1200_init, NULL, &wdt_kb1200_dev_data, &wdt_kb1200_config,
173 		      PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &wdt_kb1200_api);
174