blob: bdf9564efa29e284b7810f203136ed46595fed38 [file] [log] [blame]
Thomas Gleixnera912e802019-05-27 08:55:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Cercueilf865c352010-12-05 21:08:22 +01002/*
3 * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
4 * JZ4740 Watchdog driver
Paul Cercueilf865c352010-12-05 21:08:22 +01005 */
6
Paul Cercueildf04cce2019-06-07 18:24:26 +02007#include <linux/mfd/ingenic-tcu.h>
Paul Cercueil6d532142019-10-23 19:47:13 +02008#include <linux/mfd/syscon.h>
Paul Cercueilf865c352010-12-05 21:08:22 +01009#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010013#include <linux/watchdog.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010014#include <linux/platform_device.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010015#include <linux/io.h>
16#include <linux/device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
Axel Lin85f6df12012-01-26 18:10:45 +080019#include <linux/err.h>
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +000020#include <linux/of.h>
Paul Cercueil6d532142019-10-23 19:47:13 +020021#include <linux/regmap.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010022
Paul Cercueilf865c352010-12-05 21:08:22 +010023#define DEFAULT_HEARTBEAT 5
24#define MAX_HEARTBEAT 2048
25
Axel Lin85f6df12012-01-26 18:10:45 +080026static bool nowayout = WATCHDOG_NOWAYOUT;
27module_param(nowayout, bool, 0);
28MODULE_PARM_DESC(nowayout,
29 "Watchdog cannot be stopped once started (default="
30 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
31
32static unsigned int heartbeat = DEFAULT_HEARTBEAT;
33module_param(heartbeat, uint, 0);
34MODULE_PARM_DESC(heartbeat,
35 "Watchdog heartbeat period in seconds from 1 to "
36 __MODULE_STRING(MAX_HEARTBEAT) ", default "
37 __MODULE_STRING(DEFAULT_HEARTBEAT));
38
39struct jz4740_wdt_drvdata {
40 struct watchdog_device wdt;
Paul Cercueil6d532142019-10-23 19:47:13 +020041 struct regmap *map;
Paul Cercueil1d9c3072019-10-23 19:47:12 +020042 struct clk *clk;
43 unsigned long clk_rate;
Axel Lin85f6df12012-01-26 18:10:45 +080044};
Paul Cercueilf865c352010-12-05 21:08:22 +010045
Axel Lin85f6df12012-01-26 18:10:45 +080046static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
Paul Cercueilf865c352010-12-05 21:08:22 +010047{
Axel Lin85f6df12012-01-26 18:10:45 +080048 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
49
Paul Cercueil6d532142019-10-23 19:47:13 +020050 regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
51
Axel Lin85f6df12012-01-26 18:10:45 +080052 return 0;
Paul Cercueilf865c352010-12-05 21:08:22 +010053}
54
Axel Lin85f6df12012-01-26 18:10:45 +080055static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
56 unsigned int new_timeout)
Paul Cercueilf865c352010-12-05 21:08:22 +010057{
Axel Lin85f6df12012-01-26 18:10:45 +080058 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueil1d9c3072019-10-23 19:47:12 +020059 u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout);
Paul Cercueil6d532142019-10-23 19:47:13 +020060 unsigned int tcer;
Paul Cercueilf865c352010-12-05 21:08:22 +010061
Paul Cercueil6d532142019-10-23 19:47:13 +020062 regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
63 regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
Paul Cercueilf865c352010-12-05 21:08:22 +010064
Paul Cercueil6d532142019-10-23 19:47:13 +020065 regmap_write(drvdata->map, TCU_REG_WDT_TDR, timeout_value);
66 regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
Paul Cercueilf865c352010-12-05 21:08:22 +010067
Paul Cercueil9b346112019-06-07 18:24:27 +020068 if (tcer & TCU_WDT_TCER_TCEN)
Paul Cercueil6d532142019-10-23 19:47:13 +020069 regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
Paul Cercueilf865c352010-12-05 21:08:22 +010070
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +010071 wdt_dev->timeout = new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +010072 return 0;
73}
74
Axel Lin85f6df12012-01-26 18:10:45 +080075static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
76{
Paul Cercueil9b346112019-06-07 18:24:27 +020077 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueil6d532142019-10-23 19:47:13 +020078 unsigned int tcer;
Paul Cercueil1d9c3072019-10-23 19:47:12 +020079 int ret;
Paul Cercueil9b346112019-06-07 18:24:27 +020080
Paul Cercueil1d9c3072019-10-23 19:47:12 +020081 ret = clk_prepare_enable(drvdata->clk);
82 if (ret)
83 return ret;
84
Paul Cercueil6d532142019-10-23 19:47:13 +020085 regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
Paul Cercueil9b346112019-06-07 18:24:27 +020086
Axel Lin85f6df12012-01-26 18:10:45 +080087 jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
88
Paul Cercueil9b346112019-06-07 18:24:27 +020089 /* Start watchdog if it wasn't started already */
90 if (!(tcer & TCU_WDT_TCER_TCEN))
Paul Cercueil6d532142019-10-23 19:47:13 +020091 regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
Paul Cercueil9b346112019-06-07 18:24:27 +020092
Axel Lin85f6df12012-01-26 18:10:45 +080093 return 0;
94}
95
96static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
97{
98 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
99
Paul Cercueil6d532142019-10-23 19:47:13 +0200100 regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200101 clk_disable_unprepare(drvdata->clk);
Axel Lin85f6df12012-01-26 18:10:45 +0800102
103 return 0;
104}
105
Paul Cercueilb4918052018-05-10 20:47:46 +0200106static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
107 unsigned long action, void *data)
108{
109 wdt_dev->timeout = 0;
110 jz4740_wdt_start(wdt_dev);
111 return 0;
112}
113
Axel Lin85f6df12012-01-26 18:10:45 +0800114static const struct watchdog_info jz4740_wdt_info = {
115 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
116 .identity = "jz4740 Watchdog",
Paul Cercueilf865c352010-12-05 21:08:22 +0100117};
118
Axel Lin85f6df12012-01-26 18:10:45 +0800119static const struct watchdog_ops jz4740_wdt_ops = {
120 .owner = THIS_MODULE,
121 .start = jz4740_wdt_start,
122 .stop = jz4740_wdt_stop,
123 .ping = jz4740_wdt_ping,
124 .set_timeout = jz4740_wdt_set_timeout,
Paul Cercueilb4918052018-05-10 20:47:46 +0200125 .restart = jz4740_wdt_restart,
Paul Cercueilf865c352010-12-05 21:08:22 +0100126};
127
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000128#ifdef CONFIG_OF
129static const struct of_device_id jz4740_wdt_of_matches[] = {
130 { .compatible = "ingenic,jz4740-watchdog", },
Mathieu Malaterre71246c32017-09-15 21:20:19 +0200131 { .compatible = "ingenic,jz4780-watchdog", },
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000132 { /* sentinel */ }
133};
Stephen Boyd35ffa962016-11-10 16:02:20 -0800134MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000135#endif
136
Bill Pemberton2d991a12012-11-19 13:21:41 -0500137static int jz4740_wdt_probe(struct platform_device *pdev)
Paul Cercueilf865c352010-12-05 21:08:22 +0100138{
Guenter Roeck02189bb2019-04-10 09:28:01 -0700139 struct device *dev = &pdev->dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800140 struct jz4740_wdt_drvdata *drvdata;
141 struct watchdog_device *jz4740_wdt;
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200142 long rate;
143 int ret;
Axel Lin85f6df12012-01-26 18:10:45 +0800144
Guenter Roeck02189bb2019-04-10 09:28:01 -0700145 drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
Axel Lin85f6df12012-01-26 18:10:45 +0800146 GFP_KERNEL);
Colin Ian Kinge26e74b2016-04-26 18:18:49 +0100147 if (!drvdata)
Axel Lin85f6df12012-01-26 18:10:45 +0800148 return -ENOMEM;
Axel Lin85f6df12012-01-26 18:10:45 +0800149
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200150 drvdata->clk = devm_clk_get(&pdev->dev, "wdt");
151 if (IS_ERR(drvdata->clk)) {
152 dev_err(&pdev->dev, "cannot find WDT clock\n");
153 return PTR_ERR(drvdata->clk);
154 }
Axel Lin85f6df12012-01-26 18:10:45 +0800155
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200156 /* Set smallest clock possible */
157 rate = clk_round_rate(drvdata->clk, 1);
158 if (rate < 0)
159 return rate;
160
161 ret = clk_set_rate(drvdata->clk, rate);
162 if (ret)
163 return ret;
164
165 drvdata->clk_rate = rate;
Axel Lin85f6df12012-01-26 18:10:45 +0800166 jz4740_wdt = &drvdata->wdt;
167 jz4740_wdt->info = &jz4740_wdt_info;
168 jz4740_wdt->ops = &jz4740_wdt_ops;
Axel Lin85f6df12012-01-26 18:10:45 +0800169 jz4740_wdt->min_timeout = 1;
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200170 jz4740_wdt->max_timeout = 0xffff / rate;
171 jz4740_wdt->timeout = clamp(heartbeat,
172 jz4740_wdt->min_timeout,
173 jz4740_wdt->max_timeout);
Guenter Roeck02189bb2019-04-10 09:28:01 -0700174 jz4740_wdt->parent = dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800175 watchdog_set_nowayout(jz4740_wdt, nowayout);
176 watchdog_set_drvdata(jz4740_wdt, drvdata);
Paul Cercueilf865c352010-12-05 21:08:22 +0100177
Paul Cercueil6d532142019-10-23 19:47:13 +0200178 drvdata->map = device_node_to_regmap(dev->parent->of_node);
179 if (!drvdata->map) {
180 dev_err(dev, "regmap not found\n");
181 return -EINVAL;
182 }
Paul Cercueilf865c352010-12-05 21:08:22 +0100183
Wolfram Sang9ee644c2019-05-18 23:27:34 +0200184 return devm_watchdog_register_device(dev, &drvdata->wdt);
Paul Cercueilf865c352010-12-05 21:08:22 +0100185}
186
Paul Cercueilf865c352010-12-05 21:08:22 +0100187static struct platform_driver jz4740_wdt_driver = {
188 .probe = jz4740_wdt_probe,
Paul Cercueilf865c352010-12-05 21:08:22 +0100189 .driver = {
190 .name = "jz4740-wdt",
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000191 .of_match_table = of_match_ptr(jz4740_wdt_of_matches),
Paul Cercueilf865c352010-12-05 21:08:22 +0100192 },
193};
194
Axel Linb8ec6112011-11-29 13:56:27 +0800195module_platform_driver(jz4740_wdt_driver);
Paul Cercueilf865c352010-12-05 21:08:22 +0100196
197MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
198MODULE_DESCRIPTION("jz4740 Watchdog Driver");
Paul Cercueilf865c352010-12-05 21:08:22 +0100199MODULE_LICENSE("GPL");
Paul Cercueilf865c352010-12-05 21:08:22 +0100200MODULE_ALIAS("platform:jz4740-wdt");