blob: 5791198960e6c5bf6d766c7eb2f5450513230897 [file] [log] [blame]
Wolfram Sang3bed02a2018-08-22 00:02:24 +02001// SPDX-License-Identifier: GPL-2.0
Wolfram Sangbd99b682016-04-01 13:56:23 +02002/*
3 * Watchdog driver for Renesas WDT watchdog
4 *
Wolfram Sang1f185592017-07-26 23:54:39 +02005 * Copyright (C) 2015-17 Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
6 * Copyright (C) 2015-17 Renesas Electronics Corporation
Wolfram Sangbd99b682016-04-01 13:56:23 +02007 */
8#include <linux/bitops.h>
9#include <linux/clk.h>
Yoshihiro Shimodab8360052019-06-05 14:04:00 +090010#include <linux/delay.h>
Wolfram Sangbd99b682016-04-01 13:56:23 +020011#include <linux/io.h>
Wolfram Sangfa01fa72020-12-19 18:34:15 +010012#include <linux/iopoll.h>
Wolfram Sangbd99b682016-04-01 13:56:23 +020013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/pm_runtime.h>
Fabrizio Castro3fe95e62018-03-05 15:30:25 +000018#include <linux/smp.h>
19#include <linux/sys_soc.h>
Wolfram Sangbd99b682016-04-01 13:56:23 +020020#include <linux/watchdog.h>
21
22#define RWTCNT 0
23#define RWTCSRA 4
24#define RWTCSRA_WOVF BIT(4)
25#define RWTCSRA_WRFLG BIT(5)
26#define RWTCSRA_TME BIT(7)
Wolfram Sang03a196f2017-07-19 10:27:55 +020027#define RWTCSRB 8
Wolfram Sangbd99b682016-04-01 13:56:23 +020028
29#define RWDT_DEFAULT_TIMEOUT 60U
30
Wolfram Sang82f64cd2017-07-19 10:27:54 +020031/*
32 * In probe, clk_rate is checked to be not more than 16 bit * biggest clock
Wolfram Sang03a196f2017-07-19 10:27:55 +020033 * divider (12 bits). d is only a factor to fully utilize the WDT counter and
Wolfram Sang82f64cd2017-07-19 10:27:54 +020034 * will not exceed its 16 bits. Thus, no overflow, we stay below 32 bits.
35 */
36#define MUL_BY_CLKS_PER_SEC(p, d) \
37 DIV_ROUND_UP((d) * (p)->clk_rate, clk_divs[(p)->cks])
38
Wolfram Sang03a196f2017-07-19 10:27:55 +020039/* d is 16 bit, clk_divs 12 bit -> no 32 bit overflow */
Wolfram Sang82f64cd2017-07-19 10:27:54 +020040#define DIV_BY_CLKS_PER_SEC(p, d) ((d) * clk_divs[(p)->cks] / (p)->clk_rate)
41
Wolfram Sang03a196f2017-07-19 10:27:55 +020042static const unsigned int clk_divs[] = { 1, 4, 16, 32, 64, 128, 1024, 4096 };
Wolfram Sangbd99b682016-04-01 13:56:23 +020043
44static bool nowayout = WATCHDOG_NOWAYOUT;
45module_param(nowayout, bool, 0);
46MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
47 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
48
49struct rwdt_priv {
50 void __iomem *base;
51 struct watchdog_device wdev;
Wolfram Sang82f64cd2017-07-19 10:27:54 +020052 unsigned long clk_rate;
Wolfram Sangbd99b682016-04-01 13:56:23 +020053 u8 cks;
Wolfram Sangfa01fa72020-12-19 18:34:15 +010054 struct clk *clk;
Wolfram Sangbd99b682016-04-01 13:56:23 +020055};
56
57static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
58{
59 if (reg == RWTCNT)
60 val |= 0x5a5a0000;
61 else
62 val |= 0xa5a5a500;
63
64 writel_relaxed(val, priv->base + reg);
65}
66
67static int rwdt_init_timeout(struct watchdog_device *wdev)
68{
69 struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
70
Wolfram Sang82f64cd2017-07-19 10:27:54 +020071 rwdt_write(priv, 65536 - MUL_BY_CLKS_PER_SEC(priv, wdev->timeout), RWTCNT);
Wolfram Sangbd99b682016-04-01 13:56:23 +020072
73 return 0;
74}
75
Yoshihiro Shimodab8360052019-06-05 14:04:00 +090076static void rwdt_wait_cycles(struct rwdt_priv *priv, unsigned int cycles)
77{
78 unsigned int delay;
79
80 delay = DIV_ROUND_UP(cycles * 1000000, priv->clk_rate);
81
82 usleep_range(delay, 2 * delay);
83}
84
Wolfram Sangbd99b682016-04-01 13:56:23 +020085static int rwdt_start(struct watchdog_device *wdev)
86{
87 struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
Wolfram Sange990e122018-11-07 20:46:02 +010088 u8 val;
Wolfram Sangbd99b682016-04-01 13:56:23 +020089
Wolfram Sang3be42942017-07-26 23:54:37 +020090 pm_runtime_get_sync(wdev->parent);
Wolfram Sangbd99b682016-04-01 13:56:23 +020091
Wolfram Sange990e122018-11-07 20:46:02 +010092 /* Stop the timer before we modify any register */
93 val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
94 rwdt_write(priv, val, RWTCSRA);
Yoshihiro Shimodab8360052019-06-05 14:04:00 +090095 /* Delay 2 cycles before setting watchdog counter */
96 rwdt_wait_cycles(priv, 2);
Wolfram Sange990e122018-11-07 20:46:02 +010097
Wolfram Sangbd99b682016-04-01 13:56:23 +020098 rwdt_init_timeout(wdev);
Wolfram Sange990e122018-11-07 20:46:02 +010099 rwdt_write(priv, priv->cks, RWTCSRA);
100 rwdt_write(priv, 0, RWTCSRB);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200101
102 while (readb_relaxed(priv->base + RWTCSRA) & RWTCSRA_WRFLG)
103 cpu_relax();
104
105 rwdt_write(priv, priv->cks | RWTCSRA_TME, RWTCSRA);
106
107 return 0;
108}
109
110static int rwdt_stop(struct watchdog_device *wdev)
111{
112 struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
113
114 rwdt_write(priv, priv->cks, RWTCSRA);
Yoshihiro Shimodab8360052019-06-05 14:04:00 +0900115 /* Delay 3 cycles before disabling module clock */
116 rwdt_wait_cycles(priv, 3);
Wolfram Sang3be42942017-07-26 23:54:37 +0200117 pm_runtime_put(wdev->parent);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200118
119 return 0;
120}
121
122static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
123{
124 struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
125 u16 val = readw_relaxed(priv->base + RWTCNT);
126
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200127 return DIV_BY_CLKS_PER_SEC(priv, 65536 - val);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200128}
129
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100130/* needs to be atomic - no RPM, no usleep_range, no scheduling! */
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000131static int rwdt_restart(struct watchdog_device *wdev, unsigned long action,
132 void *data)
133{
134 struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100135 u8 val;
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000136
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100137 clk_prepare_enable(priv->clk);
138
139 /* Stop the timer before we modify any register */
140 val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
141 rwdt_write(priv, val, RWTCSRA);
142 /* Delay 2 cycles before setting watchdog counter */
143 udelay(DIV_ROUND_UP(2 * 1000000, priv->clk_rate));
144
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000145 rwdt_write(priv, 0xffff, RWTCNT);
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100146 /* smallest divider to reboot soon */
147 rwdt_write(priv, 0, RWTCSRA);
148
149 readb_poll_timeout_atomic(priv->base + RWTCSRA, val,
150 !(val & RWTCSRA_WRFLG), 1, 100);
151
152 rwdt_write(priv, RWTCSRA_TME, RWTCSRA);
153
Wolfram Sange0073722021-01-18 10:45:58 +0100154 /* wait 2 cycles, so watchdog will trigger */
155 udelay(DIV_ROUND_UP(2 * 1000000, priv->clk_rate));
156
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000157 return 0;
158}
159
Wolfram Sangbd99b682016-04-01 13:56:23 +0200160static const struct watchdog_info rwdt_ident = {
Veeraiyan Chidambaramfdac6a92018-04-13 16:19:24 +0200161 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
162 WDIOF_CARDRESET,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200163 .identity = "Renesas WDT Watchdog",
164};
165
166static const struct watchdog_ops rwdt_ops = {
167 .owner = THIS_MODULE,
168 .start = rwdt_start,
169 .stop = rwdt_stop,
170 .ping = rwdt_init_timeout,
171 .get_timeleft = rwdt_get_timeleft,
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000172 .restart = rwdt_restart,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200173};
174
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000175#if defined(CONFIG_ARCH_RCAR_GEN2) && defined(CONFIG_SMP)
176/*
177 * Watchdog-reset integration is broken on early revisions of R-Car Gen2 SoCs
178 */
179static const struct soc_device_attribute rwdt_quirks_match[] = {
180 {
181 .soc_id = "r8a7790",
182 .revision = "ES1.*",
183 .data = (void *)1, /* needs single CPU */
184 }, {
185 .soc_id = "r8a7791",
Geert Uytterhoeven665f9442018-05-18 11:55:40 +0200186 .revision = "ES1.*",
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000187 .data = (void *)1, /* needs single CPU */
188 }, {
189 .soc_id = "r8a7792",
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000190 .data = (void *)0, /* needs SMP disabled */
191 },
192 { /* sentinel */ }
193};
194
195static bool rwdt_blacklisted(struct device *dev)
196{
197 const struct soc_device_attribute *attr;
198
199 attr = soc_device_match(rwdt_quirks_match);
200 if (attr && setup_max_cpus > (uintptr_t)attr->data) {
201 dev_info(dev, "Watchdog blacklisted on %s %s\n", attr->soc_id,
202 attr->revision);
203 return true;
204 }
205
206 return false;
207}
208#else /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
209static inline bool rwdt_blacklisted(struct device *dev) { return false; }
210#endif /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
211
Wolfram Sangbd99b682016-04-01 13:56:23 +0200212static int rwdt_probe(struct platform_device *pdev)
213{
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900214 struct device *dev = &pdev->dev;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200215 struct rwdt_priv *priv;
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200216 unsigned long clks_per_sec;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200217 int ret, i;
Wolfram Sang962085a2020-09-08 11:56:15 +0200218 u8 csra;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200219
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900220 if (rwdt_blacklisted(dev))
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000221 return -ENODEV;
222
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900223 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200224 if (!priv)
225 return -ENOMEM;
226
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700227 priv->base = devm_platform_ioremap_resource(pdev, 0);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200228 if (IS_ERR(priv->base))
229 return PTR_ERR(priv->base);
230
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100231 priv->clk = devm_clk_get(dev, NULL);
232 if (IS_ERR(priv->clk))
233 return PTR_ERR(priv->clk);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200234
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900235 pm_runtime_enable(dev);
236 pm_runtime_get_sync(dev);
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100237 priv->clk_rate = clk_get_rate(priv->clk);
Wolfram Sang962085a2020-09-08 11:56:15 +0200238 csra = readb_relaxed(priv->base + RWTCSRA);
239 priv->wdev.bootstatus = csra & RWTCSRA_WOVF ? WDIOF_CARDRESET : 0;
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900240 pm_runtime_put(dev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200241
242 if (!priv->clk_rate) {
243 ret = -ENOENT;
244 goto out_pm_disable;
245 }
Wolfram Sangbd99b682016-04-01 13:56:23 +0200246
247 for (i = ARRAY_SIZE(clk_divs) - 1; i >= 0; i--) {
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200248 clks_per_sec = priv->clk_rate / clk_divs[i];
Wolfram Sangb51247c2017-07-19 10:27:52 +0200249 if (clks_per_sec && clks_per_sec < 65536) {
Wolfram Sangbd99b682016-04-01 13:56:23 +0200250 priv->cks = i;
251 break;
252 }
253 }
254
Wolfram Sangb51247c2017-07-19 10:27:52 +0200255 if (i < 0) {
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900256 dev_err(dev, "Can't find suitable clock divider\n");
Wolfram Sang3be42942017-07-26 23:54:37 +0200257 ret = -ERANGE;
258 goto out_pm_disable;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200259 }
260
Fabrizio Castrof8cde722018-11-05 10:53:47 +0000261 priv->wdev.info = &rwdt_ident;
262 priv->wdev.ops = &rwdt_ops;
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900263 priv->wdev.parent = dev;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200264 priv->wdev.min_timeout = 1;
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200265 priv->wdev.max_timeout = DIV_BY_CLKS_PER_SEC(priv, 65536);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200266 priv->wdev.timeout = min(priv->wdev.max_timeout, RWDT_DEFAULT_TIMEOUT);
267
268 platform_set_drvdata(pdev, priv);
269 watchdog_set_drvdata(&priv->wdev, priv);
270 watchdog_set_nowayout(&priv->wdev, nowayout);
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000271 watchdog_set_restart_priority(&priv->wdev, 0);
Wolfram Sang14de99b2018-08-28 12:13:48 +0200272 watchdog_stop_on_unregister(&priv->wdev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200273
274 /* This overrides the default timeout only if DT configuration was found */
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900275 watchdog_init_timeout(&priv->wdev, 0, dev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200276
Wolfram Sang962085a2020-09-08 11:56:15 +0200277 /* Check if FW enabled the watchdog */
278 if (csra & RWTCSRA_TME) {
279 /* Ensure properly initialized dividers */
280 rwdt_start(&priv->wdev);
281 set_bit(WDOG_HW_RUNNING, &priv->wdev.status);
282 }
283
Wolfram Sangbd99b682016-04-01 13:56:23 +0200284 ret = watchdog_register_device(&priv->wdev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200285 if (ret < 0)
286 goto out_pm_disable;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200287
288 return 0;
Wolfram Sang3be42942017-07-26 23:54:37 +0200289
290 out_pm_disable:
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900291 pm_runtime_disable(dev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200292 return ret;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200293}
294
295static int rwdt_remove(struct platform_device *pdev)
296{
297 struct rwdt_priv *priv = platform_get_drvdata(pdev);
298
299 watchdog_unregister_device(&priv->wdev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200300 pm_runtime_disable(&pdev->dev);
301
302 return 0;
303}
304
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000305static int __maybe_unused rwdt_suspend(struct device *dev)
306{
307 struct rwdt_priv *priv = dev_get_drvdata(dev);
308
Wolfram Sang90771232018-12-04 13:01:46 +0100309 if (watchdog_active(&priv->wdev))
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000310 rwdt_stop(&priv->wdev);
Wolfram Sang90771232018-12-04 13:01:46 +0100311
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000312 return 0;
313}
314
315static int __maybe_unused rwdt_resume(struct device *dev)
316{
317 struct rwdt_priv *priv = dev_get_drvdata(dev);
318
Wolfram Sang90771232018-12-04 13:01:46 +0100319 if (watchdog_active(&priv->wdev))
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000320 rwdt_start(&priv->wdev);
Wolfram Sang90771232018-12-04 13:01:46 +0100321
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000322 return 0;
323}
324
325static SIMPLE_DEV_PM_OPS(rwdt_pm_ops, rwdt_suspend, rwdt_resume);
326
Wolfram Sangbd99b682016-04-01 13:56:23 +0200327static const struct of_device_id rwdt_ids[] = {
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000328 { .compatible = "renesas,rcar-gen2-wdt", },
Wolfram Sangbd99b682016-04-01 13:56:23 +0200329 { .compatible = "renesas,rcar-gen3-wdt", },
330 { /* sentinel */ }
331};
332MODULE_DEVICE_TABLE(of, rwdt_ids);
333
334static struct platform_driver rwdt_driver = {
335 .driver = {
336 .name = "renesas_wdt",
337 .of_match_table = rwdt_ids,
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000338 .pm = &rwdt_pm_ops,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200339 },
340 .probe = rwdt_probe,
341 .remove = rwdt_remove,
342};
343module_platform_driver(rwdt_driver);
344
345MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
346MODULE_LICENSE("GPL v2");
347MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");