1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STM32 Independent Watchdog
5 * Copyright (C) STMicroelectronics 2017
6 * Author: Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
8 * This driver is based on tegra_wdt.c
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/watchdog.h>
25 #define IWDG_KR 0x00 /* Key register */
26 #define IWDG_PR 0x04 /* Prescaler Register */
27 #define IWDG_RLR 0x08 /* ReLoad Register */
28 #define IWDG_SR 0x0C /* Status Register */
29 #define IWDG_WINR 0x10 /* Windows Register */
31 /* IWDG_KR register bit mask */
32 #define KR_KEY_RELOAD 0xAAAA /* reload counter enable */
33 #define KR_KEY_ENABLE 0xCCCC /* peripheral enable */
34 #define KR_KEY_EWA 0x5555 /* write access enable */
35 #define KR_KEY_DWA 0x0000 /* write access disable */
37 /* IWDG_PR register bit values */
38 #define PR_4 0x00 /* prescaler set to 4 */
39 #define PR_8 0x01 /* prescaler set to 8 */
40 #define PR_16 0x02 /* prescaler set to 16 */
41 #define PR_32 0x03 /* prescaler set to 32 */
42 #define PR_64 0x04 /* prescaler set to 64 */
43 #define PR_128 0x05 /* prescaler set to 128 */
44 #define PR_256 0x06 /* prescaler set to 256 */
46 /* IWDG_RLR register values */
47 #define RLR_MIN 0x07C /* min value supported by reload register */
48 #define RLR_MAX 0xFFF /* max value supported by reload register */
50 /* IWDG_SR register bit mask */
51 #define FLAG_PVU BIT(0) /* Watchdog prescaler value update */
52 #define FLAG_RVU BIT(1) /* Watchdog counter reload value update */
54 /* set timeout to 100000 us */
55 #define TIMEOUT_US 100000
61 struct watchdog_device wdd
;
69 static inline u32
reg_read(void __iomem
*base
, u32 reg
)
71 return readl_relaxed(base
+ reg
);
74 static inline void reg_write(void __iomem
*base
, u32 reg
, u32 val
)
76 writel_relaxed(val
, base
+ reg
);
79 static int stm32_iwdg_start(struct watchdog_device
*wdd
)
81 struct stm32_iwdg
*wdt
= watchdog_get_drvdata(wdd
);
82 u32 val
= FLAG_PVU
| FLAG_RVU
;
86 dev_dbg(wdd
->parent
, "%s\n", __func__
);
88 /* prescaler fixed to 256 */
89 reload
= clamp_t(unsigned int, ((wdd
->timeout
* wdt
->rate
) / 256) - 1,
92 /* enable write access */
93 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_EWA
);
95 /* set prescaler & reload registers */
96 reg_write(wdt
->regs
, IWDG_PR
, PR_256
); /* prescaler fix to 256 */
97 reg_write(wdt
->regs
, IWDG_RLR
, reload
);
98 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_ENABLE
);
100 /* wait for the registers to be updated (max 100ms) */
101 ret
= readl_relaxed_poll_timeout(wdt
->regs
+ IWDG_SR
, val
,
102 !(val
& (FLAG_PVU
| FLAG_RVU
)),
103 SLEEP_US
, TIMEOUT_US
);
106 "Fail to set prescaler or reload registers\n");
110 /* reload watchdog */
111 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_RELOAD
);
116 static int stm32_iwdg_ping(struct watchdog_device
*wdd
)
118 struct stm32_iwdg
*wdt
= watchdog_get_drvdata(wdd
);
120 dev_dbg(wdd
->parent
, "%s\n", __func__
);
122 /* reload watchdog */
123 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_RELOAD
);
128 static int stm32_iwdg_set_timeout(struct watchdog_device
*wdd
,
129 unsigned int timeout
)
131 dev_dbg(wdd
->parent
, "%s timeout: %d sec\n", __func__
, timeout
);
133 wdd
->timeout
= timeout
;
135 if (watchdog_active(wdd
))
136 return stm32_iwdg_start(wdd
);
141 static int stm32_iwdg_clk_init(struct platform_device
*pdev
,
142 struct stm32_iwdg
*wdt
)
146 wdt
->clk_lsi
= devm_clk_get(&pdev
->dev
, "lsi");
147 if (IS_ERR(wdt
->clk_lsi
)) {
148 dev_err(&pdev
->dev
, "Unable to get lsi clock\n");
149 return PTR_ERR(wdt
->clk_lsi
);
152 /* optional peripheral clock */
154 wdt
->clk_pclk
= devm_clk_get(&pdev
->dev
, "pclk");
155 if (IS_ERR(wdt
->clk_pclk
)) {
156 dev_err(&pdev
->dev
, "Unable to get pclk clock\n");
157 return PTR_ERR(wdt
->clk_pclk
);
160 ret
= clk_prepare_enable(wdt
->clk_pclk
);
162 dev_err(&pdev
->dev
, "Unable to prepare pclk clock\n");
167 ret
= clk_prepare_enable(wdt
->clk_lsi
);
169 dev_err(&pdev
->dev
, "Unable to prepare lsi clock\n");
170 clk_disable_unprepare(wdt
->clk_pclk
);
174 wdt
->rate
= clk_get_rate(wdt
->clk_lsi
);
179 static const struct watchdog_info stm32_iwdg_info
= {
180 .options
= WDIOF_SETTIMEOUT
|
183 .identity
= "STM32 Independent Watchdog",
186 static const struct watchdog_ops stm32_iwdg_ops
= {
187 .owner
= THIS_MODULE
,
188 .start
= stm32_iwdg_start
,
189 .ping
= stm32_iwdg_ping
,
190 .set_timeout
= stm32_iwdg_set_timeout
,
193 static const struct of_device_id stm32_iwdg_of_match
[] = {
194 { .compatible
= "st,stm32-iwdg", .data
= (void *)!HAS_PCLK
},
195 { .compatible
= "st,stm32mp1-iwdg", .data
= (void *)HAS_PCLK
},
198 MODULE_DEVICE_TABLE(of
, stm32_iwdg_of_match
);
200 static int stm32_iwdg_probe(struct platform_device
*pdev
)
202 struct watchdog_device
*wdd
;
203 const struct of_device_id
*match
;
204 struct stm32_iwdg
*wdt
;
205 struct resource
*res
;
208 match
= of_match_device(stm32_iwdg_of_match
, &pdev
->dev
);
212 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
216 wdt
->has_pclk
= match
->data
;
218 /* This is the timer base. */
219 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
220 wdt
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
221 if (IS_ERR(wdt
->regs
)) {
222 dev_err(&pdev
->dev
, "Could not get resource\n");
223 return PTR_ERR(wdt
->regs
);
226 ret
= stm32_iwdg_clk_init(pdev
, wdt
);
230 /* Initialize struct watchdog_device. */
232 wdd
->info
= &stm32_iwdg_info
;
233 wdd
->ops
= &stm32_iwdg_ops
;
234 wdd
->min_timeout
= ((RLR_MIN
+ 1) * 256) / wdt
->rate
;
235 wdd
->max_hw_heartbeat_ms
= ((RLR_MAX
+ 1) * 256 * 1000) / wdt
->rate
;
236 wdd
->parent
= &pdev
->dev
;
238 watchdog_set_drvdata(wdd
, wdt
);
239 watchdog_set_nowayout(wdd
, WATCHDOG_NOWAYOUT
);
241 ret
= watchdog_init_timeout(wdd
, 0, &pdev
->dev
);
244 "unable to set timeout value, using default\n");
246 ret
= watchdog_register_device(wdd
);
248 dev_err(&pdev
->dev
, "failed to register watchdog device\n");
252 platform_set_drvdata(pdev
, wdt
);
256 clk_disable_unprepare(wdt
->clk_lsi
);
257 clk_disable_unprepare(wdt
->clk_pclk
);
262 static int stm32_iwdg_remove(struct platform_device
*pdev
)
264 struct stm32_iwdg
*wdt
= platform_get_drvdata(pdev
);
266 watchdog_unregister_device(&wdt
->wdd
);
267 clk_disable_unprepare(wdt
->clk_lsi
);
268 clk_disable_unprepare(wdt
->clk_pclk
);
273 static struct platform_driver stm32_iwdg_driver
= {
274 .probe
= stm32_iwdg_probe
,
275 .remove
= stm32_iwdg_remove
,
278 .of_match_table
= of_match_ptr(stm32_iwdg_of_match
),
281 module_platform_driver(stm32_iwdg_driver
);
283 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
284 MODULE_DESCRIPTION("STMicroelectronics STM32 Independent Watchdog Driver");
285 MODULE_LICENSE("GPL v2");