blob: d2b5074bca65c71a4a24ded04e33535fadc5c2b0 [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
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000154 return 0;
155}
156
Wolfram Sangbd99b682016-04-01 13:56:23 +0200157static const struct watchdog_info rwdt_ident = {
Veeraiyan Chidambaramfdac6a92018-04-13 16:19:24 +0200158 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
159 WDIOF_CARDRESET,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200160 .identity = "Renesas WDT Watchdog",
161};
162
163static const struct watchdog_ops rwdt_ops = {
164 .owner = THIS_MODULE,
165 .start = rwdt_start,
166 .stop = rwdt_stop,
167 .ping = rwdt_init_timeout,
168 .get_timeleft = rwdt_get_timeleft,
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000169 .restart = rwdt_restart,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200170};
171
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000172#if defined(CONFIG_ARCH_RCAR_GEN2) && defined(CONFIG_SMP)
173/*
174 * Watchdog-reset integration is broken on early revisions of R-Car Gen2 SoCs
175 */
176static const struct soc_device_attribute rwdt_quirks_match[] = {
177 {
178 .soc_id = "r8a7790",
179 .revision = "ES1.*",
180 .data = (void *)1, /* needs single CPU */
181 }, {
182 .soc_id = "r8a7791",
Geert Uytterhoeven665f9442018-05-18 11:55:40 +0200183 .revision = "ES1.*",
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000184 .data = (void *)1, /* needs single CPU */
185 }, {
186 .soc_id = "r8a7792",
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000187 .data = (void *)0, /* needs SMP disabled */
188 },
189 { /* sentinel */ }
190};
191
192static bool rwdt_blacklisted(struct device *dev)
193{
194 const struct soc_device_attribute *attr;
195
196 attr = soc_device_match(rwdt_quirks_match);
197 if (attr && setup_max_cpus > (uintptr_t)attr->data) {
198 dev_info(dev, "Watchdog blacklisted on %s %s\n", attr->soc_id,
199 attr->revision);
200 return true;
201 }
202
203 return false;
204}
205#else /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
206static inline bool rwdt_blacklisted(struct device *dev) { return false; }
207#endif /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
208
Wolfram Sangbd99b682016-04-01 13:56:23 +0200209static int rwdt_probe(struct platform_device *pdev)
210{
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900211 struct device *dev = &pdev->dev;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200212 struct rwdt_priv *priv;
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200213 unsigned long clks_per_sec;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200214 int ret, i;
Wolfram Sang962085a2020-09-08 11:56:15 +0200215 u8 csra;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200216
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900217 if (rwdt_blacklisted(dev))
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000218 return -ENODEV;
219
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900220 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200221 if (!priv)
222 return -ENOMEM;
223
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700224 priv->base = devm_platform_ioremap_resource(pdev, 0);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200225 if (IS_ERR(priv->base))
226 return PTR_ERR(priv->base);
227
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100228 priv->clk = devm_clk_get(dev, NULL);
229 if (IS_ERR(priv->clk))
230 return PTR_ERR(priv->clk);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200231
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900232 pm_runtime_enable(dev);
233 pm_runtime_get_sync(dev);
Wolfram Sangfa01fa72020-12-19 18:34:15 +0100234 priv->clk_rate = clk_get_rate(priv->clk);
Wolfram Sang962085a2020-09-08 11:56:15 +0200235 csra = readb_relaxed(priv->base + RWTCSRA);
236 priv->wdev.bootstatus = csra & RWTCSRA_WOVF ? WDIOF_CARDRESET : 0;
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900237 pm_runtime_put(dev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200238
239 if (!priv->clk_rate) {
240 ret = -ENOENT;
241 goto out_pm_disable;
242 }
Wolfram Sangbd99b682016-04-01 13:56:23 +0200243
244 for (i = ARRAY_SIZE(clk_divs) - 1; i >= 0; i--) {
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200245 clks_per_sec = priv->clk_rate / clk_divs[i];
Wolfram Sangb51247c2017-07-19 10:27:52 +0200246 if (clks_per_sec && clks_per_sec < 65536) {
Wolfram Sangbd99b682016-04-01 13:56:23 +0200247 priv->cks = i;
248 break;
249 }
250 }
251
Wolfram Sangb51247c2017-07-19 10:27:52 +0200252 if (i < 0) {
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900253 dev_err(dev, "Can't find suitable clock divider\n");
Wolfram Sang3be42942017-07-26 23:54:37 +0200254 ret = -ERANGE;
255 goto out_pm_disable;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200256 }
257
Fabrizio Castrof8cde722018-11-05 10:53:47 +0000258 priv->wdev.info = &rwdt_ident;
259 priv->wdev.ops = &rwdt_ops;
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900260 priv->wdev.parent = dev;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200261 priv->wdev.min_timeout = 1;
Wolfram Sang82f64cd2017-07-19 10:27:54 +0200262 priv->wdev.max_timeout = DIV_BY_CLKS_PER_SEC(priv, 65536);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200263 priv->wdev.timeout = min(priv->wdev.max_timeout, RWDT_DEFAULT_TIMEOUT);
264
265 platform_set_drvdata(pdev, priv);
266 watchdog_set_drvdata(&priv->wdev, priv);
267 watchdog_set_nowayout(&priv->wdev, nowayout);
Fabrizio Castro089bcaa2018-03-05 15:30:26 +0000268 watchdog_set_restart_priority(&priv->wdev, 0);
Wolfram Sang14de99b2018-08-28 12:13:48 +0200269 watchdog_stop_on_unregister(&priv->wdev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200270
271 /* This overrides the default timeout only if DT configuration was found */
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900272 watchdog_init_timeout(&priv->wdev, 0, dev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200273
Wolfram Sang962085a2020-09-08 11:56:15 +0200274 /* Check if FW enabled the watchdog */
275 if (csra & RWTCSRA_TME) {
276 /* Ensure properly initialized dividers */
277 rwdt_start(&priv->wdev);
278 set_bit(WDOG_HW_RUNNING, &priv->wdev.status);
279 }
280
Wolfram Sangbd99b682016-04-01 13:56:23 +0200281 ret = watchdog_register_device(&priv->wdev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200282 if (ret < 0)
283 goto out_pm_disable;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200284
285 return 0;
Wolfram Sang3be42942017-07-26 23:54:37 +0200286
287 out_pm_disable:
Hoan Nguyen Anb7fbd3e2019-05-23 18:29:38 +0900288 pm_runtime_disable(dev);
Wolfram Sang3be42942017-07-26 23:54:37 +0200289 return ret;
Wolfram Sangbd99b682016-04-01 13:56:23 +0200290}
291
292static int rwdt_remove(struct platform_device *pdev)
293{
294 struct rwdt_priv *priv = platform_get_drvdata(pdev);
295
296 watchdog_unregister_device(&priv->wdev);
Wolfram Sangbd99b682016-04-01 13:56:23 +0200297 pm_runtime_disable(&pdev->dev);
298
299 return 0;
300}
301
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000302static int __maybe_unused rwdt_suspend(struct device *dev)
303{
304 struct rwdt_priv *priv = dev_get_drvdata(dev);
305
Wolfram Sang90771232018-12-04 13:01:46 +0100306 if (watchdog_active(&priv->wdev))
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000307 rwdt_stop(&priv->wdev);
Wolfram Sang90771232018-12-04 13:01:46 +0100308
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000309 return 0;
310}
311
312static int __maybe_unused rwdt_resume(struct device *dev)
313{
314 struct rwdt_priv *priv = dev_get_drvdata(dev);
315
Wolfram Sang90771232018-12-04 13:01:46 +0100316 if (watchdog_active(&priv->wdev))
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000317 rwdt_start(&priv->wdev);
Wolfram Sang90771232018-12-04 13:01:46 +0100318
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000319 return 0;
320}
321
322static SIMPLE_DEV_PM_OPS(rwdt_pm_ops, rwdt_suspend, rwdt_resume);
323
Wolfram Sangbd99b682016-04-01 13:56:23 +0200324static const struct of_device_id rwdt_ids[] = {
Fabrizio Castro3fe95e62018-03-05 15:30:25 +0000325 { .compatible = "renesas,rcar-gen2-wdt", },
Wolfram Sangbd99b682016-04-01 13:56:23 +0200326 { .compatible = "renesas,rcar-gen3-wdt", },
327 { /* sentinel */ }
328};
329MODULE_DEVICE_TABLE(of, rwdt_ids);
330
331static struct platform_driver rwdt_driver = {
332 .driver = {
333 .name = "renesas_wdt",
334 .of_match_table = rwdt_ids,
Fabrizio Castro07278ca2018-03-05 15:30:24 +0000335 .pm = &rwdt_pm_ops,
Wolfram Sangbd99b682016-04-01 13:56:23 +0200336 },
337 .probe = rwdt_probe,
338 .remove = rwdt_remove,
339};
340module_platform_driver(rwdt_driver);
341
342MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
343MODULE_LICENSE("GPL v2");
344MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");