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 #else
103 LL_DBGMCU_APB1_GRP1_FreezePeriph(LL_DBGMCU_APB1_GRP1_IWDG_STOP);
104 #endif
105 }
106
107 if (options & WDT_OPT_PAUSE_IN_SLEEP) {
108 return -ENOTSUP;
109 }
110
111 /* Enable the IWDG now and write IWDG registers at the same time */
112 LL_IWDG_Enable(iwdg);
113 LL_IWDG_EnableWriteAccess(iwdg);
114 /* Write the prescaler and reload counter to the IWDG registers*/
115 LL_IWDG_SetPrescaler(iwdg, data->prescaler);
116 LL_IWDG_SetReloadCounter(iwdg, data->reload);
117
118 tickstart = k_uptime_get_32();
119
120 /* Wait for the update operation completed */
121 while (LL_IWDG_IsReady(iwdg) == 0) {
122 if ((k_uptime_get_32() - tickstart) > IWDG_SR_UPDATE_TIMEOUT) {
123 return -ENODEV;
124 }
125 }
126
127 /* Reload counter just before leaving */
128 LL_IWDG_ReloadCounter(iwdg);
129
130 return 0;
131 }
132
iwdg_stm32_disable(const struct device * dev)133 static int iwdg_stm32_disable(const struct device *dev)
134 {
135 /* watchdog cannot be stopped once started */
136 ARG_UNUSED(dev);
137
138 return -EPERM;
139 }
140
iwdg_stm32_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * config)141 static int iwdg_stm32_install_timeout(const struct device *dev,
142 const struct wdt_timeout_cfg *config)
143 {
144 struct iwdg_stm32_data *data = IWDG_STM32_DATA(dev);
145 uint32_t timeout = config->window.max * USEC_PER_MSEC;
146 uint32_t prescaler = 0U;
147 uint32_t reload = 0U;
148
149 if (config->callback != NULL) {
150 return -ENOTSUP;
151 }
152
153 /* Calculating parameters to be applied later, on setup */
154 iwdg_stm32_convert_timeout(timeout, &prescaler, &reload);
155
156 if (!(IS_IWDG_TIMEOUT(timeout) && IS_IWDG_PRESCALER(prescaler) &&
157 IS_IWDG_RELOAD(reload))) {
158 /* One of the parameters provided is invalid */
159 return -EINVAL;
160 }
161
162 /* Store the calculated values to write in the iwdg registers */
163 data->prescaler = prescaler;
164 data->reload = reload;
165
166 /* Do not enable and update the iwdg here but during wdt_setup() */
167 return 0;
168 }
169
iwdg_stm32_feed(const struct device * dev,int channel_id)170 static int iwdg_stm32_feed(const struct device *dev, int channel_id)
171 {
172 IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev);
173
174 ARG_UNUSED(channel_id);
175 LL_IWDG_ReloadCounter(iwdg);
176
177 return 0;
178 }
179
180 static const struct wdt_driver_api iwdg_stm32_api = {
181 .setup = iwdg_stm32_setup,
182 .disable = iwdg_stm32_disable,
183 .install_timeout = iwdg_stm32_install_timeout,
184 .feed = iwdg_stm32_feed,
185 };
186
iwdg_stm32_init(const struct device * dev)187 static int iwdg_stm32_init(const struct device *dev)
188 {
189 #ifndef CONFIG_WDT_DISABLE_AT_BOOT
190 struct wdt_timeout_cfg config = {
191 .window.max = CONFIG_IWDG_STM32_INITIAL_TIMEOUT
192 };
193
194 iwdg_stm32_install_timeout(dev, &config);
195 #endif
196
197 /*
198 * The ST production value for the option bytes where WDG_SW bit is
199 * present is 0x00FF55AA, namely the Software watchdog mode is
200 * enabled by default.
201 * If the IWDG is started by either hardware option or software access,
202 * the LSI oscillator is forced ON and cannot be disabled.
203 *
204 * t_IWDG(ms) = t_LSI(ms) x 4 x 2^(IWDG_PR[2:0]) x (IWDG_RLR[11:0] + 1)
205 */
206
207 return 0;
208 }
209
210 static struct iwdg_stm32_data iwdg_stm32_dev_data = {
211 .Instance = (IWDG_TypeDef *)DT_INST_REG_ADDR(0)
212 };
213
214 DEVICE_DT_INST_DEFINE(0, iwdg_stm32_init, NULL,
215 &iwdg_stm32_dev_data, NULL,
216 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
217 &iwdg_stm32_api);
218