blob: 72920f09f4a71f6d21c8f8585ee8c1920750b6c9 [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 Cercueilf865c352010-12-05 21:08:22 +01008#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010012#include <linux/watchdog.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010013#include <linux/platform_device.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010014#include <linux/io.h>
15#include <linux/device.h>
16#include <linux/clk.h>
17#include <linux/slab.h>
Axel Lin85f6df12012-01-26 18:10:45 +080018#include <linux/err.h>
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +000019#include <linux/of.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010020
Paul Cercueilf865c352010-12-05 21:08:22 +010021#define DEFAULT_HEARTBEAT 5
22#define MAX_HEARTBEAT 2048
23
Axel Lin85f6df12012-01-26 18:10:45 +080024static bool nowayout = WATCHDOG_NOWAYOUT;
25module_param(nowayout, bool, 0);
26MODULE_PARM_DESC(nowayout,
27 "Watchdog cannot be stopped once started (default="
28 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
29
30static unsigned int heartbeat = DEFAULT_HEARTBEAT;
31module_param(heartbeat, uint, 0);
32MODULE_PARM_DESC(heartbeat,
33 "Watchdog heartbeat period in seconds from 1 to "
34 __MODULE_STRING(MAX_HEARTBEAT) ", default "
35 __MODULE_STRING(DEFAULT_HEARTBEAT));
36
37struct jz4740_wdt_drvdata {
38 struct watchdog_device wdt;
Paul Cercueilf865c352010-12-05 21:08:22 +010039 void __iomem *base;
Paul Cercueil1d9c3072019-10-23 19:47:12 +020040 struct clk *clk;
41 unsigned long clk_rate;
Axel Lin85f6df12012-01-26 18:10:45 +080042};
Paul Cercueilf865c352010-12-05 21:08:22 +010043
Axel Lin85f6df12012-01-26 18:10:45 +080044static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
Paul Cercueilf865c352010-12-05 21:08:22 +010045{
Axel Lin85f6df12012-01-26 18:10:45 +080046 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
47
Paul Cercueildf04cce2019-06-07 18:24:26 +020048 writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
Axel Lin85f6df12012-01-26 18:10:45 +080049 return 0;
Paul Cercueilf865c352010-12-05 21:08:22 +010050}
51
Axel Lin85f6df12012-01-26 18:10:45 +080052static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
53 unsigned int new_timeout)
Paul Cercueilf865c352010-12-05 21:08:22 +010054{
Axel Lin85f6df12012-01-26 18:10:45 +080055 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueil1d9c3072019-10-23 19:47:12 +020056 u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout);
Paul Cercueil9b346112019-06-07 18:24:27 +020057 u8 tcer;
Paul Cercueilf865c352010-12-05 21:08:22 +010058
Paul Cercueil9b346112019-06-07 18:24:27 +020059 tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueildf04cce2019-06-07 18:24:26 +020060 writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueilf865c352010-12-05 21:08:22 +010061
Paul Cercueildf04cce2019-06-07 18:24:26 +020062 writew((u16)timeout_value, drvdata->base + TCU_REG_WDT_TDR);
63 writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
Paul Cercueilf865c352010-12-05 21:08:22 +010064
Paul Cercueil9b346112019-06-07 18:24:27 +020065 if (tcer & TCU_WDT_TCER_TCEN)
66 writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueilf865c352010-12-05 21:08:22 +010067
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +010068 wdt_dev->timeout = new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +010069 return 0;
70}
71
Axel Lin85f6df12012-01-26 18:10:45 +080072static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
73{
Paul Cercueil9b346112019-06-07 18:24:27 +020074 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueil1d9c3072019-10-23 19:47:12 +020075 int ret;
Paul Cercueil9b346112019-06-07 18:24:27 +020076 u8 tcer;
77
Paul Cercueil1d9c3072019-10-23 19:47:12 +020078 ret = clk_prepare_enable(drvdata->clk);
79 if (ret)
80 return ret;
81
Paul Cercueil9b346112019-06-07 18:24:27 +020082 tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
83
Axel Lin85f6df12012-01-26 18:10:45 +080084 jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
85
Paul Cercueil9b346112019-06-07 18:24:27 +020086 /* Start watchdog if it wasn't started already */
87 if (!(tcer & TCU_WDT_TCER_TCEN))
88 writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
89
Axel Lin85f6df12012-01-26 18:10:45 +080090 return 0;
91}
92
93static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
94{
95 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
96
Paul Cercueildf04cce2019-06-07 18:24:26 +020097 writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueil1d9c3072019-10-23 19:47:12 +020098 clk_disable_unprepare(drvdata->clk);
Axel Lin85f6df12012-01-26 18:10:45 +080099
100 return 0;
101}
102
Paul Cercueilb4918052018-05-10 20:47:46 +0200103static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
104 unsigned long action, void *data)
105{
106 wdt_dev->timeout = 0;
107 jz4740_wdt_start(wdt_dev);
108 return 0;
109}
110
Axel Lin85f6df12012-01-26 18:10:45 +0800111static const struct watchdog_info jz4740_wdt_info = {
112 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
113 .identity = "jz4740 Watchdog",
Paul Cercueilf865c352010-12-05 21:08:22 +0100114};
115
Axel Lin85f6df12012-01-26 18:10:45 +0800116static const struct watchdog_ops jz4740_wdt_ops = {
117 .owner = THIS_MODULE,
118 .start = jz4740_wdt_start,
119 .stop = jz4740_wdt_stop,
120 .ping = jz4740_wdt_ping,
121 .set_timeout = jz4740_wdt_set_timeout,
Paul Cercueilb4918052018-05-10 20:47:46 +0200122 .restart = jz4740_wdt_restart,
Paul Cercueilf865c352010-12-05 21:08:22 +0100123};
124
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000125#ifdef CONFIG_OF
126static const struct of_device_id jz4740_wdt_of_matches[] = {
127 { .compatible = "ingenic,jz4740-watchdog", },
Mathieu Malaterre71246c32017-09-15 21:20:19 +0200128 { .compatible = "ingenic,jz4780-watchdog", },
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000129 { /* sentinel */ }
130};
Stephen Boyd35ffa962016-11-10 16:02:20 -0800131MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000132#endif
133
Bill Pemberton2d991a12012-11-19 13:21:41 -0500134static int jz4740_wdt_probe(struct platform_device *pdev)
Paul Cercueilf865c352010-12-05 21:08:22 +0100135{
Guenter Roeck02189bb2019-04-10 09:28:01 -0700136 struct device *dev = &pdev->dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800137 struct jz4740_wdt_drvdata *drvdata;
138 struct watchdog_device *jz4740_wdt;
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200139 long rate;
140 int ret;
Axel Lin85f6df12012-01-26 18:10:45 +0800141
Guenter Roeck02189bb2019-04-10 09:28:01 -0700142 drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
Axel Lin85f6df12012-01-26 18:10:45 +0800143 GFP_KERNEL);
Colin Ian Kinge26e74b2016-04-26 18:18:49 +0100144 if (!drvdata)
Axel Lin85f6df12012-01-26 18:10:45 +0800145 return -ENOMEM;
Axel Lin85f6df12012-01-26 18:10:45 +0800146
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200147 drvdata->clk = devm_clk_get(&pdev->dev, "wdt");
148 if (IS_ERR(drvdata->clk)) {
149 dev_err(&pdev->dev, "cannot find WDT clock\n");
150 return PTR_ERR(drvdata->clk);
151 }
Axel Lin85f6df12012-01-26 18:10:45 +0800152
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200153 /* Set smallest clock possible */
154 rate = clk_round_rate(drvdata->clk, 1);
155 if (rate < 0)
156 return rate;
157
158 ret = clk_set_rate(drvdata->clk, rate);
159 if (ret)
160 return ret;
161
162 drvdata->clk_rate = rate;
Axel Lin85f6df12012-01-26 18:10:45 +0800163 jz4740_wdt = &drvdata->wdt;
164 jz4740_wdt->info = &jz4740_wdt_info;
165 jz4740_wdt->ops = &jz4740_wdt_ops;
Axel Lin85f6df12012-01-26 18:10:45 +0800166 jz4740_wdt->min_timeout = 1;
Paul Cercueil1d9c3072019-10-23 19:47:12 +0200167 jz4740_wdt->max_timeout = 0xffff / rate;
168 jz4740_wdt->timeout = clamp(heartbeat,
169 jz4740_wdt->min_timeout,
170 jz4740_wdt->max_timeout);
Guenter Roeck02189bb2019-04-10 09:28:01 -0700171 jz4740_wdt->parent = dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800172 watchdog_set_nowayout(jz4740_wdt, nowayout);
173 watchdog_set_drvdata(jz4740_wdt, drvdata);
Paul Cercueilf865c352010-12-05 21:08:22 +0100174
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700175 drvdata->base = devm_platform_ioremap_resource(pdev, 0);
Paul Cercueil6bdbc1f2018-05-10 20:47:45 +0200176 if (IS_ERR(drvdata->base))
177 return PTR_ERR(drvdata->base);
Paul Cercueilf865c352010-12-05 21:08:22 +0100178
Wolfram Sang9ee644c2019-05-18 23:27:34 +0200179 return devm_watchdog_register_device(dev, &drvdata->wdt);
Paul Cercueilf865c352010-12-05 21:08:22 +0100180}
181
Paul Cercueilf865c352010-12-05 21:08:22 +0100182static struct platform_driver jz4740_wdt_driver = {
183 .probe = jz4740_wdt_probe,
Paul Cercueilf865c352010-12-05 21:08:22 +0100184 .driver = {
185 .name = "jz4740-wdt",
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000186 .of_match_table = of_match_ptr(jz4740_wdt_of_matches),
Paul Cercueilf865c352010-12-05 21:08:22 +0100187 },
188};
189
Axel Linb8ec6112011-11-29 13:56:27 +0800190module_platform_driver(jz4740_wdt_driver);
Paul Cercueilf865c352010-12-05 21:08:22 +0100191
192MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
193MODULE_DESCRIPTION("jz4740 Watchdog Driver");
Paul Cercueilf865c352010-12-05 21:08:22 +0100194MODULE_LICENSE("GPL");
Paul Cercueilf865c352010-12-05 21:08:22 +0100195MODULE_ALIAS("platform:jz4740-wdt");