drm: add modifiers for MediaTek tiled formats
[drm/drm-misc.git] / drivers / watchdog / rzg2l_wdt.c
blob11bbe48160ec9c7a9b896daf2a1ec0fd42cad758
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas RZ/G2L WDT Watchdog Driver
5 * Copyright (C) 2021 Renesas Electronics Corporation
6 */
7 #include <linux/bitops.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.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>
21 #define WDTCNT 0x00
22 #define WDTSET 0x04
23 #define WDTTIM 0x08
24 #define WDTINT 0x0C
25 #define PECR 0x10
26 #define PEEN 0x14
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) ")");
46 enum rz_wdt_type {
47 WDT_RZG2L,
48 WDT_RZV2M,
51 struct rzg2l_wdt_priv {
52 void __iomem *base;
53 struct watchdog_device wdev;
54 struct reset_control *rstc;
55 unsigned long osc_clk_rate;
56 unsigned long delay;
57 struct clk *pclk;
58 struct clk *osc_clk;
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 */
65 ndelay(priv->delay);
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)
77 if (reg == WDTSET)
78 val &= WDTSET_COUNTER_MASK;
80 writel_relaxed(val, priv->base + reg);
81 /* Registers other than the WDTINT is always synchronized with WDT_CLK */
82 if (reg != WDTINT)
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);
89 u32 time_out;
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);
102 int ret;
104 ret = pm_runtime_resume_and_get(wdev->parent);
105 if (ret)
106 return ret;
108 ret = reset_control_deassert(priv->rstc);
109 if (ret) {
110 pm_runtime_put(wdev->parent);
111 return ret;
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);
123 return 0;
126 static int rzg2l_wdt_stop(struct watchdog_device *wdev)
128 struct rzg2l_wdt_priv *priv = watchdog_get_drvdata(wdev);
129 int ret;
131 ret = reset_control_assert(priv->rstc);
132 if (ret)
133 return ret;
135 ret = pm_runtime_put(wdev->parent);
136 if (ret < 0)
137 return ret;
139 return 0;
142 static int rzg2l_wdt_set_timeout(struct watchdog_device *wdev, unsigned int timeout)
144 int ret = 0;
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);
155 if (ret)
156 return ret;
158 ret = rzg2l_wdt_start(wdev);
161 return ret;
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);
168 int ret;
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);
184 if (ret)
185 return ret;
187 if (priv->devtype == WDT_RZG2L) {
188 ret = reset_control_deassert(priv->rstc);
189 if (ret)
190 return ret;
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);
197 } else {
198 /* RZ/V2M doesn't have parity error registers */
199 ret = reset_control_reset(priv->rstc);
200 if (ret)
201 return ret;
203 wdev->timeout = 0;
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));
218 return 0;
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);
232 return 0;
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;
256 int ret;
258 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
259 if (!priv)
260 return -ENOMEM;
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);
281 if (!pclk_rate)
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) /
301 USEC_PER_SEC;
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);
307 if (ret)
308 return ret;
310 watchdog_set_nowayout(&priv->wdev, nowayout);
311 watchdog_stop_on_unregister(&priv->wdev);
313 ret = watchdog_init_timeout(&priv->wdev, 0, dev);
314 if (ret)
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 },
323 { /* sentinel */ }
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))
332 return 0;
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))
342 return 0;
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 = {
352 .driver = {
353 .name = "rzg2l_wdt",
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");