blob: 4c2cc09c0c5780ec859c8643c65e887bfd3b0b54 [file] [log] [blame]
Paul Cercueilf865c352010-12-05 21:08:22 +01001/*
2 * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
3 * JZ4740 Watchdog driver
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010020#include <linux/watchdog.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010021#include <linux/platform_device.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010022#include <linux/io.h>
23#include <linux/device.h>
24#include <linux/clk.h>
25#include <linux/slab.h>
Axel Lin85f6df12012-01-26 18:10:45 +080026#include <linux/err.h>
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +000027#include <linux/of.h>
Paul Cercueilf865c352010-12-05 21:08:22 +010028
29#include <asm/mach-jz4740/timer.h>
30
31#define JZ_REG_WDT_TIMER_DATA 0x0
32#define JZ_REG_WDT_COUNTER_ENABLE 0x4
33#define JZ_REG_WDT_TIMER_COUNTER 0x8
34#define JZ_REG_WDT_TIMER_CONTROL 0xC
35
36#define JZ_WDT_CLOCK_PCLK 0x1
37#define JZ_WDT_CLOCK_RTC 0x2
38#define JZ_WDT_CLOCK_EXT 0x4
39
Paul Cercueilf865c352010-12-05 21:08:22 +010040#define JZ_WDT_CLOCK_DIV_SHIFT 3
41
42#define JZ_WDT_CLOCK_DIV_1 (0 << JZ_WDT_CLOCK_DIV_SHIFT)
43#define JZ_WDT_CLOCK_DIV_4 (1 << JZ_WDT_CLOCK_DIV_SHIFT)
44#define JZ_WDT_CLOCK_DIV_16 (2 << JZ_WDT_CLOCK_DIV_SHIFT)
45#define JZ_WDT_CLOCK_DIV_64 (3 << JZ_WDT_CLOCK_DIV_SHIFT)
46#define JZ_WDT_CLOCK_DIV_256 (4 << JZ_WDT_CLOCK_DIV_SHIFT)
47#define JZ_WDT_CLOCK_DIV_1024 (5 << JZ_WDT_CLOCK_DIV_SHIFT)
48
49#define DEFAULT_HEARTBEAT 5
50#define MAX_HEARTBEAT 2048
51
Axel Lin85f6df12012-01-26 18:10:45 +080052static bool nowayout = WATCHDOG_NOWAYOUT;
53module_param(nowayout, bool, 0);
54MODULE_PARM_DESC(nowayout,
55 "Watchdog cannot be stopped once started (default="
56 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
57
58static unsigned int heartbeat = DEFAULT_HEARTBEAT;
59module_param(heartbeat, uint, 0);
60MODULE_PARM_DESC(heartbeat,
61 "Watchdog heartbeat period in seconds from 1 to "
62 __MODULE_STRING(MAX_HEARTBEAT) ", default "
63 __MODULE_STRING(DEFAULT_HEARTBEAT));
64
65struct jz4740_wdt_drvdata {
66 struct watchdog_device wdt;
Paul Cercueilf865c352010-12-05 21:08:22 +010067 void __iomem *base;
Paul Cercueilf865c352010-12-05 21:08:22 +010068 struct clk *rtc_clk;
Axel Lin85f6df12012-01-26 18:10:45 +080069};
Paul Cercueilf865c352010-12-05 21:08:22 +010070
Axel Lin85f6df12012-01-26 18:10:45 +080071static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
Paul Cercueilf865c352010-12-05 21:08:22 +010072{
Axel Lin85f6df12012-01-26 18:10:45 +080073 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
74
75 writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
76 return 0;
Paul Cercueilf865c352010-12-05 21:08:22 +010077}
78
Axel Lin85f6df12012-01-26 18:10:45 +080079static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
80 unsigned int new_timeout)
Paul Cercueilf865c352010-12-05 21:08:22 +010081{
Axel Lin85f6df12012-01-26 18:10:45 +080082 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
Paul Cercueilf865c352010-12-05 21:08:22 +010083 unsigned int rtc_clk_rate;
84 unsigned int timeout_value;
85 unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
86
Axel Lin85f6df12012-01-26 18:10:45 +080087 rtc_clk_rate = clk_get_rate(drvdata->rtc_clk);
Paul Cercueilf865c352010-12-05 21:08:22 +010088
Axel Lin85f6df12012-01-26 18:10:45 +080089 timeout_value = rtc_clk_rate * new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +010090 while (timeout_value > 0xffff) {
91 if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
92 /* Requested timeout too high;
93 * use highest possible value. */
94 timeout_value = 0xffff;
95 break;
96 }
97 timeout_value >>= 2;
98 clock_div += (1 << JZ_WDT_CLOCK_DIV_SHIFT);
99 }
100
Axel Lin85f6df12012-01-26 18:10:45 +0800101 writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
102 writew(clock_div, drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
Paul Cercueilf865c352010-12-05 21:08:22 +0100103
Axel Lin85f6df12012-01-26 18:10:45 +0800104 writew((u16)timeout_value, drvdata->base + JZ_REG_WDT_TIMER_DATA);
105 writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
Paul Cercueilf865c352010-12-05 21:08:22 +0100106 writew(clock_div | JZ_WDT_CLOCK_RTC,
Axel Lin85f6df12012-01-26 18:10:45 +0800107 drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
Paul Cercueilf865c352010-12-05 21:08:22 +0100108
Axel Lin85f6df12012-01-26 18:10:45 +0800109 writeb(0x1, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
Paul Cercueilf865c352010-12-05 21:08:22 +0100110
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100111 wdt_dev->timeout = new_timeout;
Paul Cercueilf865c352010-12-05 21:08:22 +0100112 return 0;
113}
114
Axel Lin85f6df12012-01-26 18:10:45 +0800115static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
116{
117 jz4740_timer_enable_watchdog();
118 jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
119
120 return 0;
121}
122
123static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
124{
125 struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
126
127 jz4740_timer_disable_watchdog();
128 writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
129
130 return 0;
131}
132
133static const struct watchdog_info jz4740_wdt_info = {
134 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
135 .identity = "jz4740 Watchdog",
Paul Cercueilf865c352010-12-05 21:08:22 +0100136};
137
Axel Lin85f6df12012-01-26 18:10:45 +0800138static const struct watchdog_ops jz4740_wdt_ops = {
139 .owner = THIS_MODULE,
140 .start = jz4740_wdt_start,
141 .stop = jz4740_wdt_stop,
142 .ping = jz4740_wdt_ping,
143 .set_timeout = jz4740_wdt_set_timeout,
Paul Cercueilf865c352010-12-05 21:08:22 +0100144};
145
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000146#ifdef CONFIG_OF
147static const struct of_device_id jz4740_wdt_of_matches[] = {
148 { .compatible = "ingenic,jz4740-watchdog", },
149 { /* sentinel */ }
150};
151MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches)
152#endif
153
Bill Pemberton2d991a12012-11-19 13:21:41 -0500154static int jz4740_wdt_probe(struct platform_device *pdev)
Paul Cercueilf865c352010-12-05 21:08:22 +0100155{
Axel Lin85f6df12012-01-26 18:10:45 +0800156 struct jz4740_wdt_drvdata *drvdata;
157 struct watchdog_device *jz4740_wdt;
158 struct resource *res;
159 int ret;
160
161 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct jz4740_wdt_drvdata),
162 GFP_KERNEL);
163 if (!drvdata) {
164 dev_err(&pdev->dev, "Unable to alloacate watchdog device\n");
165 return -ENOMEM;
166 }
167
168 if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
169 heartbeat = DEFAULT_HEARTBEAT;
170
171 jz4740_wdt = &drvdata->wdt;
172 jz4740_wdt->info = &jz4740_wdt_info;
173 jz4740_wdt->ops = &jz4740_wdt_ops;
174 jz4740_wdt->timeout = heartbeat;
175 jz4740_wdt->min_timeout = 1;
176 jz4740_wdt->max_timeout = MAX_HEARTBEAT;
177 watchdog_set_nowayout(jz4740_wdt, nowayout);
178 watchdog_set_drvdata(jz4740_wdt, drvdata);
Paul Cercueilf865c352010-12-05 21:08:22 +0100179
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding4c271bb2013-01-21 11:09:25 +0100181 drvdata->base = devm_ioremap_resource(&pdev->dev, res);
182 if (IS_ERR(drvdata->base)) {
183 ret = PTR_ERR(drvdata->base);
Axel Lin85f6df12012-01-26 18:10:45 +0800184 goto err_out;
Paul Cercueilf865c352010-12-05 21:08:22 +0100185 }
186
Lars-Peter Clausen5f314972013-05-12 20:04:03 +0200187 drvdata->rtc_clk = clk_get(&pdev->dev, "rtc");
Axel Lin85f6df12012-01-26 18:10:45 +0800188 if (IS_ERR(drvdata->rtc_clk)) {
189 dev_err(&pdev->dev, "cannot find RTC clock\n");
190 ret = PTR_ERR(drvdata->rtc_clk);
191 goto err_out;
Paul Cercueilf865c352010-12-05 21:08:22 +0100192 }
193
Axel Lin85f6df12012-01-26 18:10:45 +0800194 ret = watchdog_register_device(&drvdata->wdt);
195 if (ret < 0)
Paul Cercueilf865c352010-12-05 21:08:22 +0100196 goto err_disable_clk;
Paul Cercueilf865c352010-12-05 21:08:22 +0100197
Axel Lin85f6df12012-01-26 18:10:45 +0800198 platform_set_drvdata(pdev, drvdata);
Paul Cercueilf865c352010-12-05 21:08:22 +0100199 return 0;
200
201err_disable_clk:
Axel Lin85f6df12012-01-26 18:10:45 +0800202 clk_put(drvdata->rtc_clk);
203err_out:
Paul Cercueilf865c352010-12-05 21:08:22 +0100204 return ret;
205}
206
Bill Pemberton4b12b892012-11-19 13:26:24 -0500207static int jz4740_wdt_remove(struct platform_device *pdev)
Paul Cercueilf865c352010-12-05 21:08:22 +0100208{
Axel Lin85f6df12012-01-26 18:10:45 +0800209 struct jz4740_wdt_drvdata *drvdata = platform_get_drvdata(pdev);
Paul Cercueilf865c352010-12-05 21:08:22 +0100210
Axel Lin85f6df12012-01-26 18:10:45 +0800211 jz4740_wdt_stop(&drvdata->wdt);
212 watchdog_unregister_device(&drvdata->wdt);
213 clk_put(drvdata->rtc_clk);
Paul Cercueilf865c352010-12-05 21:08:22 +0100214
215 return 0;
216}
217
Paul Cercueilf865c352010-12-05 21:08:22 +0100218static struct platform_driver jz4740_wdt_driver = {
219 .probe = jz4740_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500220 .remove = jz4740_wdt_remove,
Paul Cercueilf865c352010-12-05 21:08:22 +0100221 .driver = {
222 .name = "jz4740-wdt",
Zubair Lutfullah Kakakhel6b96c722015-02-03 10:25:48 +0000223 .of_match_table = of_match_ptr(jz4740_wdt_of_matches),
Paul Cercueilf865c352010-12-05 21:08:22 +0100224 },
225};
226
Axel Linb8ec6112011-11-29 13:56:27 +0800227module_platform_driver(jz4740_wdt_driver);
Paul Cercueilf865c352010-12-05 21:08:22 +0100228
229MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
230MODULE_DESCRIPTION("jz4740 Watchdog Driver");
Paul Cercueilf865c352010-12-05 21:08:22 +0100231MODULE_LICENSE("GPL");
Paul Cercueilf865c352010-12-05 21:08:22 +0100232MODULE_ALIAS("platform:jz4740-wdt");