1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RZ/G2L WDT Watchdog Driver
5 * Copyright (C) 2021 Renesas Electronics Corporation
7 #include <linux/bitops.h>
9 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
18 #include <linux/units.h>
19 #include <linux/watchdog.h>
27 #define WDTCNT_WDTEN BIT(0)
28 #define WDTINT_INTDISP BIT(0)
29 #define PEEN_FORCE BIT(0)
31 #define WDT_DEFAULT_TIMEOUT 60U
33 /* Setting period time register only 12 bit set in WDTSET[31:20] */
34 #define WDTSET_COUNTER_MASK (0xFFF00000)
35 #define WDTSET_COUNTER_VAL(f) ((f) << 20)
37 #define F2CYCLE_NSEC(f) (1000000000 / (f))
39 #define RZV2M_A_NSEC 730
41 static bool nowayout
= WATCHDOG_NOWAYOUT
;
42 module_param(nowayout
, bool, 0);
43 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
44 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
51 struct rzg2l_wdt_priv
{
53 struct watchdog_device wdev
;
54 struct reset_control
*rstc
;
55 unsigned long osc_clk_rate
;
59 enum rz_wdt_type devtype
;
62 static void rzg2l_wdt_wait_delay(struct rzg2l_wdt_priv
*priv
)
64 /* delay timer when change the setting register */
68 static u32
rzg2l_wdt_get_cycle_usec(unsigned long cycle
, u32 wdttime
)
70 u64 timer_cycle_us
= 1024 * 1024ULL * (wdttime
+ 1) * MICRO
;
72 return div64_ul(timer_cycle_us
, cycle
);
75 static void rzg2l_wdt_write(struct rzg2l_wdt_priv
*priv
, u32 val
, unsigned int reg
)
78 val
&= WDTSET_COUNTER_MASK
;
80 writel_relaxed(val
, priv
->base
+ reg
);
81 /* Registers other than the WDTINT is always synchronized with WDT_CLK */
83 rzg2l_wdt_wait_delay(priv
);
86 static void rzg2l_wdt_init_timeout(struct watchdog_device
*wdev
)
88 struct rzg2l_wdt_priv
*priv
= watchdog_get_drvdata(wdev
);
91 /* Clear Lapsed Time Register and clear Interrupt */
92 rzg2l_wdt_write(priv
, WDTINT_INTDISP
, WDTINT
);
93 /* 2 consecutive overflow cycle needed to trigger reset */
94 time_out
= (wdev
->timeout
* (MICRO
/ 2)) /
95 rzg2l_wdt_get_cycle_usec(priv
->osc_clk_rate
, 0);
96 rzg2l_wdt_write(priv
, WDTSET_COUNTER_VAL(time_out
), WDTSET
);
99 static int rzg2l_wdt_start(struct watchdog_device
*wdev
)
101 struct rzg2l_wdt_priv
*priv
= watchdog_get_drvdata(wdev
);
104 ret
= pm_runtime_resume_and_get(wdev
->parent
);
108 ret
= reset_control_deassert(priv
->rstc
);
110 pm_runtime_put(wdev
->parent
);
114 /* Initialize time out */
115 rzg2l_wdt_init_timeout(wdev
);
117 /* Initialize watchdog counter register */
118 rzg2l_wdt_write(priv
, 0, WDTTIM
);
120 /* Enable watchdog timer*/
121 rzg2l_wdt_write(priv
, WDTCNT_WDTEN
, WDTCNT
);
126 static int rzg2l_wdt_stop(struct watchdog_device
*wdev
)
128 struct rzg2l_wdt_priv
*priv
= watchdog_get_drvdata(wdev
);
131 ret
= reset_control_assert(priv
->rstc
);
135 ret
= pm_runtime_put(wdev
->parent
);
142 static int rzg2l_wdt_set_timeout(struct watchdog_device
*wdev
, unsigned int timeout
)
146 wdev
->timeout
= timeout
;
149 * If the watchdog is active, reset the module for updating the WDTSET
150 * register by calling rzg2l_wdt_stop() (which internally calls reset_control_reset()
151 * to reset the module) so that it is updated with new timeout values.
153 if (watchdog_active(wdev
)) {
154 ret
= rzg2l_wdt_stop(wdev
);
158 ret
= rzg2l_wdt_start(wdev
);
164 static int rzg2l_wdt_restart(struct watchdog_device
*wdev
,
165 unsigned long action
, void *data
)
167 struct rzg2l_wdt_priv
*priv
= watchdog_get_drvdata(wdev
);
171 * In case of RZ/G3S the watchdog device may be part of an IRQ safe power
172 * domain that is currently powered off. In this case we need to power
173 * it on before accessing registers. Along with this the clocks will be
174 * enabled. We don't undo the pm_runtime_resume_and_get() as the device
175 * need to be on for the reboot to happen.
177 * For the rest of SoCs not registering a watchdog IRQ safe power
178 * domain it is safe to call pm_runtime_resume_and_get() as the
179 * irq_safe_dev_in_sleep_domain() call in genpd_runtime_resume()
180 * returns non zero value and the genpd_lock() is avoided, thus, there
181 * will be no invalid wait context reported by lockdep.
183 ret
= pm_runtime_resume_and_get(wdev
->parent
);
187 if (priv
->devtype
== WDT_RZG2L
) {
188 ret
= reset_control_deassert(priv
->rstc
);
192 /* Generate Reset (WDTRSTB) Signal on parity error */
193 rzg2l_wdt_write(priv
, 0, PECR
);
195 /* Force parity error */
196 rzg2l_wdt_write(priv
, PEEN_FORCE
, PEEN
);
198 /* RZ/V2M doesn't have parity error registers */
199 ret
= reset_control_reset(priv
->rstc
);
205 /* Initialize time out */
206 rzg2l_wdt_init_timeout(wdev
);
208 /* Initialize watchdog counter register */
209 rzg2l_wdt_write(priv
, 0, WDTTIM
);
211 /* Enable watchdog timer*/
212 rzg2l_wdt_write(priv
, WDTCNT_WDTEN
, WDTCNT
);
214 /* Wait 2 consecutive overflow cycles for reset */
215 mdelay(DIV_ROUND_UP(2 * 0xFFFFF * 1000, priv
->osc_clk_rate
));
221 static const struct watchdog_info rzg2l_wdt_ident
= {
222 .options
= WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
,
223 .identity
= "Renesas RZ/G2L WDT Watchdog",
226 static int rzg2l_wdt_ping(struct watchdog_device
*wdev
)
228 struct rzg2l_wdt_priv
*priv
= watchdog_get_drvdata(wdev
);
230 rzg2l_wdt_write(priv
, WDTINT_INTDISP
, WDTINT
);
235 static const struct watchdog_ops rzg2l_wdt_ops
= {
236 .owner
= THIS_MODULE
,
237 .start
= rzg2l_wdt_start
,
238 .stop
= rzg2l_wdt_stop
,
239 .ping
= rzg2l_wdt_ping
,
240 .set_timeout
= rzg2l_wdt_set_timeout
,
241 .restart
= rzg2l_wdt_restart
,
244 static void rzg2l_wdt_pm_disable(void *data
)
246 struct watchdog_device
*wdev
= data
;
248 pm_runtime_disable(wdev
->parent
);
251 static int rzg2l_wdt_probe(struct platform_device
*pdev
)
253 struct device
*dev
= &pdev
->dev
;
254 struct rzg2l_wdt_priv
*priv
;
255 unsigned long pclk_rate
;
258 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
262 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
263 if (IS_ERR(priv
->base
))
264 return PTR_ERR(priv
->base
);
266 /* Get watchdog main clock */
267 priv
->osc_clk
= devm_clk_get(&pdev
->dev
, "oscclk");
268 if (IS_ERR(priv
->osc_clk
))
269 return dev_err_probe(&pdev
->dev
, PTR_ERR(priv
->osc_clk
), "no oscclk");
271 priv
->osc_clk_rate
= clk_get_rate(priv
->osc_clk
);
272 if (!priv
->osc_clk_rate
)
273 return dev_err_probe(&pdev
->dev
, -EINVAL
, "oscclk rate is 0");
275 /* Get Peripheral clock */
276 priv
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
277 if (IS_ERR(priv
->pclk
))
278 return dev_err_probe(&pdev
->dev
, PTR_ERR(priv
->pclk
), "no pclk");
280 pclk_rate
= clk_get_rate(priv
->pclk
);
282 return dev_err_probe(&pdev
->dev
, -EINVAL
, "pclk rate is 0");
284 priv
->delay
= F2CYCLE_NSEC(priv
->osc_clk_rate
) * 6 + F2CYCLE_NSEC(pclk_rate
) * 9;
286 priv
->rstc
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
287 if (IS_ERR(priv
->rstc
))
288 return dev_err_probe(&pdev
->dev
, PTR_ERR(priv
->rstc
),
289 "failed to get cpg reset");
291 priv
->devtype
= (uintptr_t)of_device_get_match_data(dev
);
293 pm_runtime_irq_safe(&pdev
->dev
);
294 pm_runtime_enable(&pdev
->dev
);
296 priv
->wdev
.info
= &rzg2l_wdt_ident
;
297 priv
->wdev
.ops
= &rzg2l_wdt_ops
;
298 priv
->wdev
.parent
= dev
;
299 priv
->wdev
.min_timeout
= 1;
300 priv
->wdev
.max_timeout
= rzg2l_wdt_get_cycle_usec(priv
->osc_clk_rate
, 0xfff) /
302 priv
->wdev
.timeout
= WDT_DEFAULT_TIMEOUT
;
304 watchdog_set_drvdata(&priv
->wdev
, priv
);
305 dev_set_drvdata(dev
, priv
);
306 ret
= devm_add_action_or_reset(&pdev
->dev
, rzg2l_wdt_pm_disable
, &priv
->wdev
);
310 watchdog_set_nowayout(&priv
->wdev
, nowayout
);
311 watchdog_stop_on_unregister(&priv
->wdev
);
313 ret
= watchdog_init_timeout(&priv
->wdev
, 0, dev
);
315 dev_warn(dev
, "Specified timeout invalid, using default");
317 return devm_watchdog_register_device(&pdev
->dev
, &priv
->wdev
);
320 static const struct of_device_id rzg2l_wdt_ids
[] = {
321 { .compatible
= "renesas,rzg2l-wdt", .data
= (void *)WDT_RZG2L
},
322 { .compatible
= "renesas,rzv2m-wdt", .data
= (void *)WDT_RZV2M
},
325 MODULE_DEVICE_TABLE(of
, rzg2l_wdt_ids
);
327 static int rzg2l_wdt_suspend_late(struct device
*dev
)
329 struct rzg2l_wdt_priv
*priv
= dev_get_drvdata(dev
);
331 if (!watchdog_active(&priv
->wdev
))
334 return rzg2l_wdt_stop(&priv
->wdev
);
337 static int rzg2l_wdt_resume_early(struct device
*dev
)
339 struct rzg2l_wdt_priv
*priv
= dev_get_drvdata(dev
);
341 if (!watchdog_active(&priv
->wdev
))
344 return rzg2l_wdt_start(&priv
->wdev
);
347 static const struct dev_pm_ops rzg2l_wdt_pm_ops
= {
348 LATE_SYSTEM_SLEEP_PM_OPS(rzg2l_wdt_suspend_late
, rzg2l_wdt_resume_early
)
351 static struct platform_driver rzg2l_wdt_driver
= {
354 .of_match_table
= rzg2l_wdt_ids
,
355 .pm
= &rzg2l_wdt_pm_ops
,
357 .probe
= rzg2l_wdt_probe
,
359 module_platform_driver(rzg2l_wdt_driver
);
361 MODULE_DESCRIPTION("Renesas RZ/G2L WDT Watchdog Driver");
362 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
363 MODULE_LICENSE("GPL v2");