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/platform_device.h>
21 #include <linux/pm_wakeirq.h>
22 #include <linux/watchdog.h>
24 #define DEFAULT_TIMEOUT 10
27 #define IWDG_KR 0x00 /* Key register */
28 #define IWDG_PR 0x04 /* Prescaler Register */
29 #define IWDG_RLR 0x08 /* ReLoad Register */
30 #define IWDG_SR 0x0C /* Status Register */
31 #define IWDG_WINR 0x10 /* Windows Register */
32 #define IWDG_EWCR 0x14 /* Early Wake-up Register */
34 /* IWDG_KR register bit mask */
35 #define KR_KEY_RELOAD 0xAAAA /* reload counter enable */
36 #define KR_KEY_ENABLE 0xCCCC /* peripheral enable */
37 #define KR_KEY_EWA 0x5555 /* write access enable */
38 #define KR_KEY_DWA 0x0000 /* write access disable */
40 /* IWDG_PR register */
42 #define PR_MIN BIT(PR_SHIFT)
44 /* IWDG_RLR register values */
45 #define RLR_MIN 0x2 /* min value recommended */
46 #define RLR_MAX GENMASK(11, 0) /* max value of reload register */
48 /* IWDG_SR register bit mask */
49 #define SR_PVU BIT(0) /* Watchdog prescaler value update */
50 #define SR_RVU BIT(1) /* Watchdog counter reload value update */
52 #define EWCR_EWIT GENMASK(11, 0) /* Watchdog counter window value */
53 #define EWCR_EWIC BIT(14) /* Watchdog early interrupt acknowledge */
54 #define EWCR_EWIE BIT(15) /* Watchdog early interrupt enable */
56 /* set timeout to 100000 us */
57 #define TIMEOUT_US 100000
60 struct stm32_iwdg_data
{
62 bool has_early_wakeup
;
66 static const struct stm32_iwdg_data stm32_iwdg_data
= {
68 .has_early_wakeup
= false,
72 static const struct stm32_iwdg_data stm32mp1_iwdg_data
= {
74 .has_early_wakeup
= true,
75 .max_prescaler
= 1024,
79 struct watchdog_device wdd
;
80 const struct stm32_iwdg_data
*data
;
87 static inline u32
reg_read(void __iomem
*base
, u32 reg
)
89 return readl_relaxed(base
+ reg
);
92 static inline void reg_write(void __iomem
*base
, u32 reg
, u32 val
)
94 writel_relaxed(val
, base
+ reg
);
97 static int stm32_iwdg_start(struct watchdog_device
*wdd
)
99 struct stm32_iwdg
*wdt
= watchdog_get_drvdata(wdd
);
100 u32 tout
, ptot
, presc
, iwdg_rlr
, iwdg_ewcr
, iwdg_pr
, iwdg_sr
;
103 dev_dbg(wdd
->parent
, "%s\n", __func__
);
105 if (!wdd
->pretimeout
)
106 wdd
->pretimeout
= 3 * wdd
->timeout
/ 4;
108 tout
= clamp_t(unsigned int, wdd
->timeout
,
109 wdd
->min_timeout
, wdd
->max_hw_heartbeat_ms
/ 1000);
110 ptot
= clamp_t(unsigned int, tout
- wdd
->pretimeout
,
111 wdd
->min_timeout
, tout
);
113 presc
= DIV_ROUND_UP(tout
* wdt
->rate
, RLR_MAX
+ 1);
115 /* The prescaler is align on power of 2 and start at 2 ^ PR_SHIFT. */
116 presc
= roundup_pow_of_two(presc
);
117 iwdg_pr
= presc
<= 1 << PR_SHIFT
? 0 : ilog2(presc
) - PR_SHIFT
;
118 iwdg_rlr
= ((tout
* wdt
->rate
) / presc
) - 1;
119 iwdg_ewcr
= ((ptot
* wdt
->rate
) / presc
) - 1;
121 /* enable write access */
122 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_EWA
);
124 /* set prescaler & reload registers */
125 reg_write(wdt
->regs
, IWDG_PR
, iwdg_pr
);
126 reg_write(wdt
->regs
, IWDG_RLR
, iwdg_rlr
);
127 if (wdt
->data
->has_early_wakeup
)
128 reg_write(wdt
->regs
, IWDG_EWCR
, iwdg_ewcr
| EWCR_EWIE
);
129 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_ENABLE
);
131 /* wait for the registers to be updated (max 100ms) */
132 ret
= readl_relaxed_poll_timeout(wdt
->regs
+ IWDG_SR
, iwdg_sr
,
133 !(iwdg_sr
& (SR_PVU
| SR_RVU
)),
134 SLEEP_US
, TIMEOUT_US
);
136 dev_err(wdd
->parent
, "Fail to set prescaler, reload regs\n");
140 /* reload watchdog */
141 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_RELOAD
);
146 static int stm32_iwdg_ping(struct watchdog_device
*wdd
)
148 struct stm32_iwdg
*wdt
= watchdog_get_drvdata(wdd
);
150 dev_dbg(wdd
->parent
, "%s\n", __func__
);
152 /* reload watchdog */
153 reg_write(wdt
->regs
, IWDG_KR
, KR_KEY_RELOAD
);
158 static int stm32_iwdg_set_timeout(struct watchdog_device
*wdd
,
159 unsigned int timeout
)
161 dev_dbg(wdd
->parent
, "%s timeout: %d sec\n", __func__
, timeout
);
163 wdd
->timeout
= timeout
;
165 if (watchdog_active(wdd
))
166 return stm32_iwdg_start(wdd
);
171 static int stm32_iwdg_set_pretimeout(struct watchdog_device
*wdd
,
172 unsigned int pretimeout
)
174 dev_dbg(wdd
->parent
, "%s pretimeout: %d sec\n", __func__
, pretimeout
);
176 wdd
->pretimeout
= pretimeout
;
178 if (watchdog_active(wdd
))
179 return stm32_iwdg_start(wdd
);
184 static irqreturn_t
stm32_iwdg_isr(int irq
, void *wdog_arg
)
186 struct watchdog_device
*wdd
= wdog_arg
;
187 struct stm32_iwdg
*wdt
= watchdog_get_drvdata(wdd
);
190 reg
= reg_read(wdt
->regs
, IWDG_EWCR
);
192 reg_write(wdt
->regs
, IWDG_EWCR
, reg
);
194 watchdog_notify_pretimeout(wdd
);
199 static void stm32_clk_disable_unprepare(void *data
)
201 clk_disable_unprepare(data
);
204 static int stm32_iwdg_clk_init(struct platform_device
*pdev
,
205 struct stm32_iwdg
*wdt
)
207 struct device
*dev
= &pdev
->dev
;
210 wdt
->clk_lsi
= devm_clk_get(dev
, "lsi");
211 if (IS_ERR(wdt
->clk_lsi
))
212 return dev_err_probe(dev
, PTR_ERR(wdt
->clk_lsi
), "Unable to get lsi clock\n");
214 /* optional peripheral clock */
215 if (wdt
->data
->has_pclk
) {
216 wdt
->clk_pclk
= devm_clk_get(dev
, "pclk");
217 if (IS_ERR(wdt
->clk_pclk
))
218 return dev_err_probe(dev
, PTR_ERR(wdt
->clk_pclk
),
219 "Unable to get pclk clock\n");
221 ret
= clk_prepare_enable(wdt
->clk_pclk
);
223 dev_err(dev
, "Unable to prepare pclk clock\n");
226 ret
= devm_add_action_or_reset(dev
,
227 stm32_clk_disable_unprepare
,
233 ret
= clk_prepare_enable(wdt
->clk_lsi
);
235 dev_err(dev
, "Unable to prepare lsi clock\n");
238 ret
= devm_add_action_or_reset(dev
, stm32_clk_disable_unprepare
,
243 wdt
->rate
= clk_get_rate(wdt
->clk_lsi
);
248 static const struct watchdog_info stm32_iwdg_info
= {
249 .options
= WDIOF_SETTIMEOUT
|
252 .identity
= "STM32 Independent Watchdog",
255 static const struct watchdog_info stm32_iwdg_preinfo
= {
256 .options
= WDIOF_SETTIMEOUT
|
258 WDIOF_KEEPALIVEPING
|
260 .identity
= "STM32 Independent Watchdog",
263 static const struct watchdog_ops stm32_iwdg_ops
= {
264 .owner
= THIS_MODULE
,
265 .start
= stm32_iwdg_start
,
266 .ping
= stm32_iwdg_ping
,
267 .set_timeout
= stm32_iwdg_set_timeout
,
268 .set_pretimeout
= stm32_iwdg_set_pretimeout
,
271 static const struct of_device_id stm32_iwdg_of_match
[] = {
272 { .compatible
= "st,stm32-iwdg", .data
= &stm32_iwdg_data
},
273 { .compatible
= "st,stm32mp1-iwdg", .data
= &stm32mp1_iwdg_data
},
276 MODULE_DEVICE_TABLE(of
, stm32_iwdg_of_match
);
278 static int stm32_iwdg_irq_init(struct platform_device
*pdev
,
279 struct stm32_iwdg
*wdt
)
281 struct device_node
*np
= pdev
->dev
.of_node
;
282 struct watchdog_device
*wdd
= &wdt
->wdd
;
283 struct device
*dev
= &pdev
->dev
;
286 if (!wdt
->data
->has_early_wakeup
)
289 irq
= platform_get_irq(pdev
, 0);
293 if (of_property_read_bool(np
, "wakeup-source")) {
294 ret
= device_init_wakeup(dev
, true);
298 ret
= dev_pm_set_wake_irq(dev
, irq
);
303 ret
= devm_request_irq(dev
, irq
, stm32_iwdg_isr
, 0,
308 wdd
->info
= &stm32_iwdg_preinfo
;
312 static int stm32_iwdg_probe(struct platform_device
*pdev
)
314 struct device
*dev
= &pdev
->dev
;
315 struct watchdog_device
*wdd
;
316 struct stm32_iwdg
*wdt
;
319 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
323 wdt
->data
= of_device_get_match_data(&pdev
->dev
);
327 /* This is the timer base. */
328 wdt
->regs
= devm_platform_ioremap_resource(pdev
, 0);
329 if (IS_ERR(wdt
->regs
))
330 return PTR_ERR(wdt
->regs
);
332 ret
= stm32_iwdg_clk_init(pdev
, wdt
);
336 /* Initialize struct watchdog_device. */
339 wdd
->info
= &stm32_iwdg_info
;
340 wdd
->ops
= &stm32_iwdg_ops
;
341 wdd
->timeout
= DEFAULT_TIMEOUT
;
342 wdd
->min_timeout
= DIV_ROUND_UP((RLR_MIN
+ 1) * PR_MIN
, wdt
->rate
);
343 wdd
->max_hw_heartbeat_ms
= ((RLR_MAX
+ 1) * wdt
->data
->max_prescaler
*
346 /* Initialize IRQ, this might override wdd->info, hence it is here. */
347 ret
= stm32_iwdg_irq_init(pdev
, wdt
);
351 watchdog_set_drvdata(wdd
, wdt
);
352 watchdog_set_nowayout(wdd
, WATCHDOG_NOWAYOUT
);
353 watchdog_init_timeout(wdd
, 0, dev
);
356 * In case of CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED is set
357 * (Means U-Boot/bootloaders leaves the watchdog running)
358 * When we get here we should make a decision to prevent
359 * any side effects before user space daemon will take care of it.
360 * The best option, taking into consideration that there is no
361 * way to read values back from hardware, is to enforce watchdog
362 * being run with deterministic values.
364 if (IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED
)) {
365 ret
= stm32_iwdg_start(wdd
);
369 /* Make sure the watchdog is serviced */
370 set_bit(WDOG_HW_RUNNING
, &wdd
->status
);
373 ret
= devm_watchdog_register_device(dev
, wdd
);
377 platform_set_drvdata(pdev
, wdt
);
382 static struct platform_driver stm32_iwdg_driver
= {
383 .probe
= stm32_iwdg_probe
,
386 .of_match_table
= stm32_iwdg_of_match
,
389 module_platform_driver(stm32_iwdg_driver
);
391 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
392 MODULE_DESCRIPTION("STMicroelectronics STM32 Independent Watchdog Driver");
393 MODULE_LICENSE("GPL v2");