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>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/units.h>
18 #include <linux/watchdog.h>
26 #define WDTCNT_WDTEN BIT(0)
27 #define WDTINT_INTDISP BIT(0)
28 #define PEEN_FORCE BIT(0)
30 #define WDT_DEFAULT_TIMEOUT 60U
32 /* Setting period time register only 12 bit set in WDTSET[31:20] */
33 #define WDTSET_COUNTER_MASK (0xFFF00000)
34 #define WDTSET_COUNTER_VAL(f) ((f) << 20)
36 #define F2CYCLE_NSEC(f) (1000000000 / (f))
38 static bool nowayout = WATCHDOG_NOWAYOUT;
39 module_param(nowayout, bool, 0);
40 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
48 struct rzg2l_wdt_priv {
50 struct watchdog_device wdev;
51 struct reset_control *rstc;
52 unsigned long osc_clk_rate;
56 enum rz_wdt_type devtype;
59 static void rzg2l_wdt_wait_delay(struct rzg2l_wdt_priv *priv)
61 /* delay timer when change the setting register */
65 static u32 rzg2l_wdt_get_cycle_usec(unsigned long cycle, u32 wdttime)
67 u64 timer_cycle_us = 1024 * 1024ULL * (wdttime + 1) * MICRO;
69 return div64_ul(timer_cycle_us, cycle);
72 static void rzg2l_wdt_write(struct rzg2l_wdt_priv *priv, u32 val, unsigned int reg)
75 val &= WDTSET_COUNTER_MASK;
77 writel_relaxed(val, priv->base + reg);
78 /* Registers other than the WDTINT is always synchronized with WDT_CLK */
80 rzg2l_wdt_wait_delay(priv);
83 static void rzg2l_wdt_init_timeout(struct watchdog_device *wdev)
85 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
88 /* Clear Lapsed Time Register and clear Interrupt */
89 rzg2l_wdt_write(priv, WDTINT_INTDISP, WDTINT);
90 /* 2 consecutive overflow cycle needed to trigger reset */
91 time_out = (wdev->timeout * (MICRO / 2)) /
92 rzg2l_wdt_get_cycle_usec(priv->osc_clk_rate, 0);
93 rzg2l_wdt_write(priv, WDTSET_COUNTER_VAL(time_out), WDTSET);
96 static int rzg2l_wdt_start(struct watchdog_device *wdev)
98 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
100 pm_runtime_get_sync(wdev->parent);
102 /* Initialize time out */
103 rzg2l_wdt_init_timeout(wdev);
105 /* Initialize watchdog counter register */
106 rzg2l_wdt_write(priv, 0, WDTTIM);
108 /* Enable watchdog timer*/
109 rzg2l_wdt_write(priv, WDTCNT_WDTEN, WDTCNT);
114 static int rzg2l_wdt_stop(struct watchdog_device *wdev)
116 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
118 pm_runtime_put(wdev->parent);
119 reset_control_reset(priv->rstc);
124 static int rzg2l_wdt_set_timeout(struct watchdog_device *wdev, unsigned int timeout)
126 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
128 wdev->timeout = timeout;
131 * If the watchdog is active, reset the module for updating the WDTSET
132 * register so that it is updated with new timeout values.
134 if (watchdog_active(wdev)) {
135 pm_runtime_put(wdev->parent);
136 reset_control_reset(priv->rstc);
137 rzg2l_wdt_start(wdev);
143 static int rzg2l_wdt_restart(struct watchdog_device *wdev,
144 unsigned long action, void *data)
146 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
148 clk_prepare_enable(priv->pclk);
149 clk_prepare_enable(priv->osc_clk);
151 if (priv->devtype == WDT_RZG2L) {
152 /* Generate Reset (WDTRSTB) Signal on parity error */
153 rzg2l_wdt_write(priv, 0, PECR);
155 /* Force parity error */
156 rzg2l_wdt_write(priv, PEEN_FORCE, PEEN);
158 /* RZ/V2M doesn't have parity error registers */
162 /* Initialize time out */
163 rzg2l_wdt_init_timeout(wdev);
165 /* Initialize watchdog counter register */
166 rzg2l_wdt_write(priv, 0, WDTTIM);
168 /* Enable watchdog timer*/
169 rzg2l_wdt_write(priv, WDTCNT_WDTEN, WDTCNT);
171 /* Wait 2 consecutive overflow cycles for reset */
172 mdelay(DIV_ROUND_UP(2 * 0xFFFFF * 1000, priv->osc_clk_rate));
178 static const struct watchdog_info rzg2l_wdt_ident = {
179 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
180 .identity = "Renesas RZ/G2L WDT Watchdog",
183 static int rzg2l_wdt_ping(struct watchdog_device *wdev)
185 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
187 rzg2l_wdt_write(priv, WDTINT_INTDISP, WDTINT);
192 static const struct watchdog_ops rzg2l_wdt_ops = {
193 .owner = THIS_MODULE,
194 .start = rzg2l_wdt_start,
195 .stop = rzg2l_wdt_stop,
196 .ping = rzg2l_wdt_ping,
197 .set_timeout = rzg2l_wdt_set_timeout,
198 .restart = rzg2l_wdt_restart,
201 static void rzg2l_wdt_reset_assert_pm_disable(void *data)
203 struct watchdog_device *wdev = data;
204 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
206 pm_runtime_disable(wdev->parent);
207 reset_control_assert(priv->rstc);
210 static int rzg2l_wdt_probe(struct platform_device *pdev)
212 struct device *dev = &pdev->dev;
213 struct rzg2l_wdt_priv *priv;
214 unsigned long pclk_rate;
217 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
221 priv->base = devm_platform_ioremap_resource(pdev, 0);
222 if (IS_ERR(priv->base))
223 return PTR_ERR(priv->base);
225 /* Get watchdog main clock */
226 priv->osc_clk = devm_clk_get(&pdev->dev, "oscclk");
227 if (IS_ERR(priv->osc_clk))
228 return dev_err_probe(&pdev->dev, PTR_ERR(priv->osc_clk), "no oscclk");
230 priv->osc_clk_rate = clk_get_rate(priv->osc_clk);
231 if (!priv->osc_clk_rate)
232 return dev_err_probe(&pdev->dev, -EINVAL, "oscclk rate is 0");
234 /* Get Peripheral clock */
235 priv->pclk = devm_clk_get(&pdev->dev, "pclk");
236 if (IS_ERR(priv->pclk))
237 return dev_err_probe(&pdev->dev, PTR_ERR(priv->pclk), "no pclk");
239 pclk_rate = clk_get_rate(priv->pclk);
241 return dev_err_probe(&pdev->dev, -EINVAL, "pclk rate is 0");
243 priv->delay = F2CYCLE_NSEC(priv->osc_clk_rate) * 6 + F2CYCLE_NSEC(pclk_rate) * 9;
245 priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
246 if (IS_ERR(priv->rstc))
247 return dev_err_probe(&pdev->dev, PTR_ERR(priv->rstc),
248 "failed to get cpg reset");
250 ret = reset_control_deassert(priv->rstc);
252 return dev_err_probe(dev, ret, "failed to deassert");
254 priv->devtype = (uintptr_t)of_device_get_match_data(dev);
256 pm_runtime_enable(&pdev->dev);
258 priv->wdev.info = &rzg2l_wdt_ident;
259 priv->wdev.ops = &rzg2l_wdt_ops;
260 priv->wdev.parent = dev;
261 priv->wdev.min_timeout = 1;
262 priv->wdev.max_timeout = rzg2l_wdt_get_cycle_usec(priv->osc_clk_rate, 0xfff) /
264 priv->wdev.timeout = WDT_DEFAULT_TIMEOUT;
266 watchdog_set_drvdata(&priv->wdev, priv);
267 ret = devm_add_action_or_reset(&pdev->dev,
268 rzg2l_wdt_reset_assert_pm_disable,
273 watchdog_set_nowayout(&priv->wdev, nowayout);
274 watchdog_stop_on_unregister(&priv->wdev);
276 ret = watchdog_init_timeout(&priv->wdev, 0, dev);
278 dev_warn(dev, "Specified timeout invalid, using default");
280 return devm_watchdog_register_device(&pdev->dev, &priv->wdev);
283 static const struct of_device_id rzg2l_wdt_ids[] = {
284 { .compatible = "renesas,rzg2l-wdt", .data = (void *)WDT_RZG2L },
285 { .compatible = "renesas,rzv2m-wdt", .data = (void *)WDT_RZV2M },
288 MODULE_DEVICE_TABLE(of, rzg2l_wdt_ids);
290 static struct platform_driver rzg2l_wdt_driver = {
293 .of_match_table = rzg2l_wdt_ids,
295 .probe = rzg2l_wdt_probe,
297 module_platform_driver(rzg2l_wdt_driver);
299 MODULE_DESCRIPTION("Renesas RZ/G2L WDT Watchdog Driver");
300 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
301 MODULE_LICENSE("GPL v2");