1 /*
2 * Copyright (c) 2016 Open-RnD Sp. z o.o.
3 * Copyright (c) 2017 RnDity Sp. z o.o.
4 * Copyright (c) 2018 qianfan Zhao
5 * Copyright (c) 2020 Libre Solar Technologies GmbH
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #define DT_DRV_COMPAT st_stm32_watchdog
11
12 #include <zephyr/drivers/watchdog.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys_clock.h>
15 #include <soc.h>
16 #include <stm32_ll_bus.h>
17 #include <stm32_ll_iwdg.h>
18 #include <stm32_ll_system.h>
19 #include <errno.h>
20
21 #include "wdt_iwdg_stm32.h"
22
23 #define IWDG_PRESCALER_MIN (4U)
24
25 #if defined(LL_IWDG_PRESCALER_1024)
26 #define IWDG_PRESCALER_MAX (1024U)
27 #else
28 #define IWDG_PRESCALER_MAX (256U)
29 #endif
30
31 #define IWDG_RELOAD_MIN (0x0000U)
32 #define IWDG_RELOAD_MAX (0x0FFFU)
33
34 /* Minimum timeout in microseconds. */
35 #define IWDG_TIMEOUT_MIN (IWDG_PRESCALER_MIN * (IWDG_RELOAD_MIN + 1U) \
36 * USEC_PER_SEC / LSI_VALUE)
37
38 /* Maximum timeout in microseconds. */
39 #define IWDG_TIMEOUT_MAX ((uint64_t)IWDG_PRESCALER_MAX * \
40 (IWDG_RELOAD_MAX + 1U) * \
41 USEC_PER_SEC / LSI_VALUE)
42
43 #define IS_IWDG_TIMEOUT(__TIMEOUT__) \
44 (((__TIMEOUT__) >= IWDG_TIMEOUT_MIN) && \
45 ((__TIMEOUT__) <= IWDG_TIMEOUT_MAX))
46
47 /*
48 * Status register needs 5 LSI clock cycles divided by prescaler to be updated.
49 * With highest prescaler and considering clock variation, we will wait
50 * maximum 6 cycles (48 ms at 32 kHz) for register update.
51 */
52 #define IWDG_SR_UPDATE_TIMEOUT (6U * IWDG_PRESCALER_MAX * \
53 MSEC_PER_SEC / LSI_VALUE)
54
55 /**
56 * @brief Calculates prescaler & reload values.
57 *
58 * @param timeout Timeout value in microseconds.
59 * @param prescaler Pointer to prescaler value.
60 * @param reload Pointer to reload value.
61 */
iwdg_stm32_convert_timeout(uint32_t timeout,uint32_t * prescaler,uint32_t * reload)62 static void iwdg_stm32_convert_timeout(uint32_t timeout,
63 uint32_t *prescaler,
64 uint32_t *reload)
65 {
66 uint16_t divider = 4U;
67 uint8_t shift = 0U;
68
69 /* Convert timeout to LSI clock ticks. */
70 uint32_t ticks = (uint64_t)timeout * LSI_VALUE / USEC_PER_SEC;
71
72 while ((ticks / divider) > IWDG_RELOAD_MAX) {
73 shift++;
74 divider = 4U << shift;
75 }
76
77 /*
78 * Value of the 'shift' variable corresponds to the
79 * defines of LL_IWDG_PRESCALER_XX type.
80 */
81 *prescaler = shift;
82 *reload = (uint32_t)(ticks / divider) - 1U;
83 }
84
iwdg_stm32_setup(const struct device * dev,uint8_t options)85 static int iwdg_stm32_setup(const struct device *dev, uint8_t options)
86 {
87 struct iwdg_stm32_data *data = IWDG_STM32_DATA(dev);
88 IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev);
89 uint32_t tickstart;
90
91 /* Deactivate running when debugger is attached. */
92 if (options & WDT_OPT_PAUSE_HALTED_BY_DBG) {
93 #if defined(CONFIG_SOC_SERIES_STM32F0X)
94 LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_DBGMCU);
95 #elif defined(CONFIG_SOC_SERIES_STM32C0X) || defined(CONFIG_SOC_SERIES_STM32G0X)
96 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DBGMCU);
97 #elif defined(CONFIG_SOC_SERIES_STM32L0X)
98 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_DBGMCU);
99 #endif
100 #if defined(CONFIG_SOC_SERIES_STM32H7X)
101 LL_DBGMCU_APB4_GRP1_FreezePeriph(LL_DBGMCU_APB4_GRP1_IWDG1_STOP);
102 #elif defined(CONFIG_SOC_SERIES_STM32H7RSX)
103 LL_DBGMCU_APB4_GRP1_FreezePeriph(LL_DBGMCU_APB4_GRP1_IWDG_STOP);
104 #else
105 LL_DBGMCU_APB1_GRP1_FreezePeriph(LL_DBGMCU_APB1_GRP1_IWDG_STOP);
106 #endif
107 }
108
109 if (options & WDT_OPT_PAUSE_IN_SLEEP) {
110 return -ENOTSUP;
111 }
112
113 /* Enable the IWDG now and write IWDG registers at the same time */
114 LL_IWDG_Enable(iwdg);
115 LL_IWDG_EnableWriteAccess(iwdg);
116 /* Write the prescaler and reload counter to the IWDG registers*/
117 LL_IWDG_SetPrescaler(iwdg, data->prescaler);
118 LL_IWDG_SetReloadCounter(iwdg, data->reload);
119
120 tickstart = k_uptime_get_32();
121
122 /* Wait for the update operation completed */
123 while (LL_IWDG_IsReady(iwdg) == 0) {
124 if ((k_uptime_get_32() - tickstart) > IWDG_SR_UPDATE_TIMEOUT) {
125 return -ENODEV;
126 }
127 }
128
129 /* Reload counter just before leaving */
130 LL_IWDG_ReloadCounter(iwdg);
131
132 return 0;
133 }
134
iwdg_stm32_disable(const struct device * dev)135 static int iwdg_stm32_disable(const struct device *dev)
136 {
137 /* watchdog cannot be stopped once started */
138 ARG_UNUSED(dev);
139
140 return -EPERM;
141 }
142
iwdg_stm32_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * config)143 static int iwdg_stm32_install_timeout(const struct device *dev,
144 const struct wdt_timeout_cfg *config)
145 {
146 struct iwdg_stm32_data *data = IWDG_STM32_DATA(dev);
147 uint32_t timeout = config->window.max * USEC_PER_MSEC;
148 uint32_t prescaler = 0U;
149 uint32_t reload = 0U;
150
151 if (config->callback != NULL) {
152 return -ENOTSUP;
153 }
154
155 /* Calculating parameters to be applied later, on setup */
156 iwdg_stm32_convert_timeout(timeout, &prescaler, &reload);
157
158 if (!(IS_IWDG_TIMEOUT(timeout) && IS_IWDG_PRESCALER(prescaler) &&
159 IS_IWDG_RELOAD(reload))) {
160 /* One of the parameters provided is invalid */
161 return -EINVAL;
162 }
163
164 /* Store the calculated values to write in the iwdg registers */
165 data->prescaler = prescaler;
166 data->reload = reload;
167
168 /* Do not enable and update the iwdg here but during wdt_setup() */
169 return 0;
170 }
171
iwdg_stm32_feed(const struct device * dev,int channel_id)172 static int iwdg_stm32_feed(const struct device *dev, int channel_id)
173 {
174 IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev);
175
176 ARG_UNUSED(channel_id);
177 LL_IWDG_ReloadCounter(iwdg);
178
179 return 0;
180 }
181
182 static DEVICE_API(wdt, iwdg_stm32_api) = {
183 .setup = iwdg_stm32_setup,
184 .disable = iwdg_stm32_disable,
185 .install_timeout = iwdg_stm32_install_timeout,
186 .feed = iwdg_stm32_feed,
187 };
188
iwdg_stm32_init(const struct device * dev)189 static int iwdg_stm32_init(const struct device *dev)
190 {
191 #ifndef CONFIG_WDT_DISABLE_AT_BOOT
192 struct wdt_timeout_cfg config = {
193 .window.max = CONFIG_IWDG_STM32_INITIAL_TIMEOUT
194 };
195 /* Watchdog should be configured and started by `wdt_setup`*/
196 iwdg_stm32_install_timeout(dev, &config);
197 iwdg_stm32_setup(dev, 0); /* no option specified */
198 #endif
199
200 /*
201 * The ST production value for the option bytes where WDG_SW bit is
202 * present is 0x00FF55AA, namely the Software watchdog mode is
203 * enabled by default.
204 * If the IWDG is started by either hardware option or software access,
205 * the LSI oscillator is forced ON and cannot be disabled.
206 *
207 * t_IWDG(ms) = t_LSI(ms) x 4 x 2^(IWDG_PR[2:0]) x (IWDG_RLR[11:0] + 1)
208 */
209
210 return 0;
211 }
212
213 static struct iwdg_stm32_data iwdg_stm32_dev_data = {
214 .Instance = (IWDG_TypeDef *)DT_INST_REG_ADDR(0)
215 };
216
217 DEVICE_DT_INST_DEFINE(0, iwdg_stm32_init, NULL,
218 &iwdg_stm32_dev_data, NULL,
219 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
220 &iwdg_stm32_api);
221