blob: d4a90916dd382d7f876915b5ec3eb968ea978e87 [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
21#include <asm/mach-jz4740/timer.h>
22
Paul Cercueilf865c352010-12-05 21:08:22 +010023#define JZ_WDT_CLOCK_PCLK 0x1
24#define JZ_WDT_CLOCK_RTC 0x2
25#define JZ_WDT_CLOCK_EXT 0x4
26
Paul Cercueildf04cce2019-06-07 18:24:26 +020027#define JZ_WDT_CLOCK_DIV_1 (0 << TCU_TCSR_PRESCALE_LSB)
28#define JZ_WDT_CLOCK_DIV_4 (1 << TCU_TCSR_PRESCALE_LSB)
29#define JZ_WDT_CLOCK_DIV_16 (2 << TCU_TCSR_PRESCALE_LSB)
30#define JZ_WDT_CLOCK_DIV_64 (3 << TCU_TCSR_PRESCALE_LSB)
31#define JZ_WDT_CLOCK_DIV_256 (4 << TCU_TCSR_PRESCALE_LSB)
32#define JZ_WDT_CLOCK_DIV_1024 (5 << TCU_TCSR_PRESCALE_LSB)
Paul Cercueilf865c352010-12-05 21:08:22 +010033
34#define DEFAULT_HEARTBEAT 5
35#define MAX_HEARTBEAT 2048
36
Axel Lin85f6df12012-01-26 18:10:45 +080037static bool nowayout = WATCHDOG_NOWAYOUT;
38module_param(nowayout, bool, 0);
39MODULE_PARM_DESC(nowayout,
40 "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
43static unsigned int heartbeat = DEFAULT_HEARTBEAT;
44module_param(heartbeat, uint, 0);
45MODULE_PARM_DESC(heartbeat,
46 "Watchdog heartbeat period in seconds from 1 to "
47 __MODULE_STRING(MAX_HEARTBEAT) ", default "
48 __MODULE_STRING(DEFAULT_HEARTBEAT));
49
50struct jz4740_wdt_drvdata {
51 struct watchdog_device wdt;
Paul Cercueilf865c352010-12-05 21:08:22 +010052 void __iomem *base;
Paul Cercueilf865c352010-12-05 21:08:22 +010053 struct clk *rtc_clk;
Axel Lin85f6df12012-01-26 18:10:45 +080054};
Paul Cercueilf865c352010-12-05 21:08:22 +010055
Axel Lin85f6df12012-01-26 18:10:45 +080056static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
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);
59
Paul Cercueildf04cce2019-06-07 18:24:26 +020060 writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
Axel Lin85f6df12012-01-26 18:10:45 +080061 return 0;
Paul Cercueilf865c352010-12-05 21:08:22 +010062}
63
Axel Lin85f6df12012-01-26 18:10:45 +080064static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
65 unsigned int new_timeout)
Paul Cercueilf865c352010-12-05 21:08:22 +010066{
Axel Lin85f6df12012-01-26 18:10:45 +080067 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueilf865c352010-12-05 21:08:22 +010068 unsigned int rtc_clk_rate;
69 unsigned int timeout_value;
70 unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
Paul Cercueil9b346112019-06-07 18:24:27 +020071 u8 tcer;
Paul Cercueilf865c352010-12-05 21:08:22 +010072
Axel Lin85f6df12012-01-26 18:10:45 +080073 rtc_clk_rate = clk_get_rate(drvdata->rtc_clk);
Paul Cercueilf865c352010-12-05 21:08:22 +010074
Axel Lin85f6df12012-01-26 18:10:45 +080075 timeout_value = rtc_clk_rate * new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +010076 while (timeout_value > 0xffff) {
77 if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
78 /* Requested timeout too high;
79 * use highest possible value. */
80 timeout_value = 0xffff;
81 break;
82 }
83 timeout_value >>= 2;
Paul Cercueildf04cce2019-06-07 18:24:26 +020084 clock_div += (1 << TCU_TCSR_PRESCALE_LSB);
Paul Cercueilf865c352010-12-05 21:08:22 +010085 }
86
Paul Cercueil9b346112019-06-07 18:24:27 +020087 tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueildf04cce2019-06-07 18:24:26 +020088 writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
89 writew(clock_div, drvdata->base + TCU_REG_WDT_TCSR);
Paul Cercueilf865c352010-12-05 21:08:22 +010090
Paul Cercueildf04cce2019-06-07 18:24:26 +020091 writew((u16)timeout_value, drvdata->base + TCU_REG_WDT_TDR);
92 writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
93 writew(clock_div | JZ_WDT_CLOCK_RTC, drvdata->base + TCU_REG_WDT_TCSR);
Paul Cercueilf865c352010-12-05 21:08:22 +010094
Paul Cercueil9b346112019-06-07 18:24:27 +020095 if (tcer & TCU_WDT_TCER_TCEN)
96 writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueilf865c352010-12-05 21:08:22 +010097
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +010098 wdt_dev->timeout = new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +010099 return 0;
100}
101
Axel Lin85f6df12012-01-26 18:10:45 +0800102static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
103{
Paul Cercueil9b346112019-06-07 18:24:27 +0200104 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
105 u8 tcer;
106
107 tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
108
Axel Lin85f6df12012-01-26 18:10:45 +0800109 jz4740_timer_enable_watchdog();
110 jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
111
Paul Cercueil9b346112019-06-07 18:24:27 +0200112 /* Start watchdog if it wasn't started already */
113 if (!(tcer & TCU_WDT_TCER_TCEN))
114 writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
115
Axel Lin85f6df12012-01-26 18:10:45 +0800116 return 0;
117}
118
119static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
120{
121 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
122
Paul Cercueildf04cce2019-06-07 18:24:26 +0200123 writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
Paul Cercueil212c1052018-05-10 20:47:44 +0200124 jz4740_timer_disable_watchdog();
Axel Lin85f6df12012-01-26 18:10:45 +0800125
126 return 0;
127}
128
Paul Cercueilb4918052018-05-10 20:47:46 +0200129static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
130 unsigned long action, void *data)
131{
132 wdt_dev->timeout = 0;
133 jz4740_wdt_start(wdt_dev);
134 return 0;
135}
136
Axel Lin85f6df12012-01-26 18:10:45 +0800137static const struct watchdog_info jz4740_wdt_info = {
138 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
139 .identity = "jz4740 Watchdog",
Paul Cercueilf865c352010-12-05 21:08:22 +0100140};
141
Axel Lin85f6df12012-01-26 18:10:45 +0800142static const struct watchdog_ops jz4740_wdt_ops = {
143 .owner = THIS_MODULE,
144 .start = jz4740_wdt_start,
145 .stop = jz4740_wdt_stop,
146 .ping = jz4740_wdt_ping,
147 .set_timeout = jz4740_wdt_set_timeout,
Paul Cercueilb4918052018-05-10 20:47:46 +0200148 .restart = jz4740_wdt_restart,
Paul Cercueilf865c352010-12-05 21:08:22 +0100149};
150
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000151#ifdef CONFIG_OF
152static const struct of_device_id jz4740_wdt_of_matches[] = {
153 { .compatible = "ingenic,jz4740-watchdog", },
Mathieu Malaterre71246c32017-09-15 21:20:19 +0200154 { .compatible = "ingenic,jz4780-watchdog", },
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000155 { /* sentinel */ }
156};
Stephen Boyd35ffa962016-11-10 16:02:20 -0800157MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000158#endif
159
Bill Pemberton2d991a12012-11-19 13:21:41 -0500160static int jz4740_wdt_probe(struct platform_device *pdev)
Paul Cercueilf865c352010-12-05 21:08:22 +0100161{
Guenter Roeck02189bb2019-04-10 09:28:01 -0700162 struct device *dev = &pdev->dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800163 struct jz4740_wdt_drvdata *drvdata;
164 struct watchdog_device *jz4740_wdt;
Axel Lin85f6df12012-01-26 18:10:45 +0800165 int ret;
166
Guenter Roeck02189bb2019-04-10 09:28:01 -0700167 drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
Axel Lin85f6df12012-01-26 18:10:45 +0800168 GFP_KERNEL);
Colin Ian Kinge26e74b2016-04-26 18:18:49 +0100169 if (!drvdata)
Axel Lin85f6df12012-01-26 18:10:45 +0800170 return -ENOMEM;
Axel Lin85f6df12012-01-26 18:10:45 +0800171
172 if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
173 heartbeat = DEFAULT_HEARTBEAT;
174
175 jz4740_wdt = &drvdata->wdt;
176 jz4740_wdt->info = &jz4740_wdt_info;
177 jz4740_wdt->ops = &jz4740_wdt_ops;
178 jz4740_wdt->timeout = heartbeat;
179 jz4740_wdt->min_timeout = 1;
180 jz4740_wdt->max_timeout = MAX_HEARTBEAT;
Guenter Roeck02189bb2019-04-10 09:28:01 -0700181 jz4740_wdt->parent = dev;
Axel Lin85f6df12012-01-26 18:10:45 +0800182 watchdog_set_nowayout(jz4740_wdt, nowayout);
183 watchdog_set_drvdata(jz4740_wdt, drvdata);
Paul Cercueilf865c352010-12-05 21:08:22 +0100184
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700185 drvdata->base = devm_platform_ioremap_resource(pdev, 0);
Paul Cercueil6bdbc1f2018-05-10 20:47:45 +0200186 if (IS_ERR(drvdata->base))
187 return PTR_ERR(drvdata->base);
Paul Cercueilf865c352010-12-05 21:08:22 +0100188
Guenter Roeck02189bb2019-04-10 09:28:01 -0700189 drvdata->rtc_clk = devm_clk_get(dev, "rtc");
Axel Lin85f6df12012-01-26 18:10:45 +0800190 if (IS_ERR(drvdata->rtc_clk)) {
Guenter Roeck02189bb2019-04-10 09:28:01 -0700191 dev_err(dev, "cannot find RTC clock\n");
Paul Cercueil6bdbc1f2018-05-10 20:47:45 +0200192 return PTR_ERR(drvdata->rtc_clk);
Paul Cercueilf865c352010-12-05 21:08:22 +0100193 }
194
Wolfram Sang9ee644c2019-05-18 23:27:34 +0200195 return devm_watchdog_register_device(dev, &drvdata->wdt);
Paul Cercueilf865c352010-12-05 21:08:22 +0100196}
197
Paul Cercueilf865c352010-12-05 21:08:22 +0100198static struct platform_driver jz4740_wdt_driver = {
199 .probe = jz4740_wdt_probe,
Paul Cercueilf865c352010-12-05 21:08:22 +0100200 .driver = {
201 .name = "jz4740-wdt",
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000202 .of_match_table = of_match_ptr(jz4740_wdt_of_matches),
Paul Cercueilf865c352010-12-05 21:08:22 +0100203 },
204};
205
Axel Linb8ec6112011-11-29 13:56:27 +0800206module_platform_driver(jz4740_wdt_driver);
Paul Cercueilf865c352010-12-05 21:08:22 +0100207
208MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
209MODULE_DESCRIPTION("jz4740 Watchdog Driver");
Paul Cercueilf865c352010-12-05 21:08:22 +0100210MODULE_LICENSE("GPL");
Paul Cercueilf865c352010-12-05 21:08:22 +0100211MODULE_ALIAS("platform:jz4740-wdt");