Blame view

drivers/watchdog/jz4740_wdt.c 6.21 KB
f865c3522   Paul Cercueil   watchdog: add JZ4...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   *  Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
   *  JZ4740 Watchdog driver
   *
   *  This program is free software; you can redistribute it and/or modify it
   *  under  the terms of the GNU General  Public License as published by the
   *  Free Software Foundation;  either version 2 of the License, or (at your
   *  option) any later version.
   *
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  675 Mass Ave, Cambridge, MA 02139, USA.
   *
   */
  
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
20
  #include <linux/watchdog.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
21
  #include <linux/platform_device.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
22
23
24
25
  #include <linux/io.h>
  #include <linux/device.h>
  #include <linux/clk.h>
  #include <linux/slab.h>
85f6df149   Axel Lin   watchdog: Convert...
26
  #include <linux/err.h>
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
27
  #include <linux/of.h>
f865c3522   Paul Cercueil   watchdog: add JZ4...
28
29
30
31
32
33
34
35
36
37
38
  
  #include <asm/mach-jz4740/timer.h>
  
  #define JZ_REG_WDT_TIMER_DATA     0x0
  #define JZ_REG_WDT_COUNTER_ENABLE 0x4
  #define JZ_REG_WDT_TIMER_COUNTER  0x8
  #define JZ_REG_WDT_TIMER_CONTROL  0xC
  
  #define JZ_WDT_CLOCK_PCLK 0x1
  #define JZ_WDT_CLOCK_RTC  0x2
  #define JZ_WDT_CLOCK_EXT  0x4
f865c3522   Paul Cercueil   watchdog: add JZ4...
39
40
41
42
43
44
45
46
47
48
49
  #define JZ_WDT_CLOCK_DIV_SHIFT   3
  
  #define JZ_WDT_CLOCK_DIV_1    (0 << JZ_WDT_CLOCK_DIV_SHIFT)
  #define JZ_WDT_CLOCK_DIV_4    (1 << JZ_WDT_CLOCK_DIV_SHIFT)
  #define JZ_WDT_CLOCK_DIV_16   (2 << JZ_WDT_CLOCK_DIV_SHIFT)
  #define JZ_WDT_CLOCK_DIV_64   (3 << JZ_WDT_CLOCK_DIV_SHIFT)
  #define JZ_WDT_CLOCK_DIV_256  (4 << JZ_WDT_CLOCK_DIV_SHIFT)
  #define JZ_WDT_CLOCK_DIV_1024 (5 << JZ_WDT_CLOCK_DIV_SHIFT)
  
  #define DEFAULT_HEARTBEAT 5
  #define MAX_HEARTBEAT     2048
85f6df149   Axel Lin   watchdog: Convert...
50
51
52
53
54
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
  MODULE_PARM_DESC(nowayout,
  		 "Watchdog cannot be stopped once started (default="
  		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
f865c3522   Paul Cercueil   watchdog: add JZ4...
55

85f6df149   Axel Lin   watchdog: Convert...
56
57
58
59
60
61
  static unsigned int heartbeat = DEFAULT_HEARTBEAT;
  module_param(heartbeat, uint, 0);
  MODULE_PARM_DESC(heartbeat,
  		"Watchdog heartbeat period in seconds from 1 to "
  		__MODULE_STRING(MAX_HEARTBEAT) ", default "
  		__MODULE_STRING(DEFAULT_HEARTBEAT));
f865c3522   Paul Cercueil   watchdog: add JZ4...
62

85f6df149   Axel Lin   watchdog: Convert...
63
64
65
66
67
  struct jz4740_wdt_drvdata {
  	struct watchdog_device wdt;
  	void __iomem *base;
  	struct clk *rtc_clk;
  };
f865c3522   Paul Cercueil   watchdog: add JZ4...
68

85f6df149   Axel Lin   watchdog: Convert...
69
  static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
70
  {
85f6df149   Axel Lin   watchdog: Convert...
71
72
73
74
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  
  	writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
75
  }
85f6df149   Axel Lin   watchdog: Convert...
76
77
  static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
  				    unsigned int new_timeout)
f865c3522   Paul Cercueil   watchdog: add JZ4...
78
  {
85f6df149   Axel Lin   watchdog: Convert...
79
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
f865c3522   Paul Cercueil   watchdog: add JZ4...
80
81
82
  	unsigned int rtc_clk_rate;
  	unsigned int timeout_value;
  	unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
85f6df149   Axel Lin   watchdog: Convert...
83
  	rtc_clk_rate = clk_get_rate(drvdata->rtc_clk);
f865c3522   Paul Cercueil   watchdog: add JZ4...
84

85f6df149   Axel Lin   watchdog: Convert...
85
  	timeout_value = rtc_clk_rate * new_timeout;
f865c3522   Paul Cercueil   watchdog: add JZ4...
86
87
88
89
90
91
92
93
94
95
  	while (timeout_value > 0xffff) {
  		if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
  			/* Requested timeout too high;
  			* use highest possible value. */
  			timeout_value = 0xffff;
  			break;
  		}
  		timeout_value >>= 2;
  		clock_div += (1 << JZ_WDT_CLOCK_DIV_SHIFT);
  	}
85f6df149   Axel Lin   watchdog: Convert...
96
97
  	writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
  	writew(clock_div, drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
f865c3522   Paul Cercueil   watchdog: add JZ4...
98

85f6df149   Axel Lin   watchdog: Convert...
99
100
  	writew((u16)timeout_value, drvdata->base + JZ_REG_WDT_TIMER_DATA);
  	writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
f865c3522   Paul Cercueil   watchdog: add JZ4...
101
  	writew(clock_div | JZ_WDT_CLOCK_RTC,
85f6df149   Axel Lin   watchdog: Convert...
102
  		drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
f865c3522   Paul Cercueil   watchdog: add JZ4...
103

85f6df149   Axel Lin   watchdog: Convert...
104
  	writeb(0x1, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
f865c3522   Paul Cercueil   watchdog: add JZ4...
105

0197c1c49   Wim Van Sebroeck   watchdog: fix set...
106
  	wdt_dev->timeout = new_timeout;
85f6df149   Axel Lin   watchdog: Convert...
107
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
108
  }
85f6df149   Axel Lin   watchdog: Convert...
109
  static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
110
  {
85f6df149   Axel Lin   watchdog: Convert...
111
112
  	jz4740_timer_enable_watchdog();
  	jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
f865c3522   Paul Cercueil   watchdog: add JZ4...
113

85f6df149   Axel Lin   watchdog: Convert...
114
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
115
  }
85f6df149   Axel Lin   watchdog: Convert...
116
  static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
117
  {
85f6df149   Axel Lin   watchdog: Convert...
118
  	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
742e4b630   Wim Van Sebroeck   watchdog: jz4740_...
119

85f6df149   Axel Lin   watchdog: Convert...
120
121
  	jz4740_timer_disable_watchdog();
  	writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
f865c3522   Paul Cercueil   watchdog: add JZ4...
122

85f6df149   Axel Lin   watchdog: Convert...
123
  	return 0;
f865c3522   Paul Cercueil   watchdog: add JZ4...
124
  }
85f6df149   Axel Lin   watchdog: Convert...
125
126
  static const struct watchdog_info jz4740_wdt_info = {
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
f865c3522   Paul Cercueil   watchdog: add JZ4...
127
128
  	.identity = "jz4740 Watchdog",
  };
85f6df149   Axel Lin   watchdog: Convert...
129
  static const struct watchdog_ops jz4740_wdt_ops = {
f865c3522   Paul Cercueil   watchdog: add JZ4...
130
  	.owner = THIS_MODULE,
85f6df149   Axel Lin   watchdog: Convert...
131
132
133
134
  	.start = jz4740_wdt_start,
  	.stop = jz4740_wdt_stop,
  	.ping = jz4740_wdt_ping,
  	.set_timeout = jz4740_wdt_set_timeout,
f865c3522   Paul Cercueil   watchdog: add JZ4...
135
  };
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
136
137
138
139
140
141
142
  #ifdef CONFIG_OF
  static const struct of_device_id jz4740_wdt_of_matches[] = {
  	{ .compatible = "ingenic,jz4740-watchdog", },
  	{ /* sentinel */ }
  };
  MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches)
  #endif
2d991a164   Bill Pemberton   watchdog: remove ...
143
  static int jz4740_wdt_probe(struct platform_device *pdev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
144
  {
85f6df149   Axel Lin   watchdog: Convert...
145
146
147
148
149
150
151
  	struct jz4740_wdt_drvdata *drvdata;
  	struct watchdog_device *jz4740_wdt;
  	struct resource	*res;
  	int ret;
  
  	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct jz4740_wdt_drvdata),
  			       GFP_KERNEL);
e26e74b15   Colin Ian King   watchdog: remove ...
152
  	if (!drvdata)
85f6df149   Axel Lin   watchdog: Convert...
153
  		return -ENOMEM;
f865c3522   Paul Cercueil   watchdog: add JZ4...
154

85f6df149   Axel Lin   watchdog: Convert...
155
156
  	if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  		heartbeat = DEFAULT_HEARTBEAT;
f865c3522   Paul Cercueil   watchdog: add JZ4...
157

85f6df149   Axel Lin   watchdog: Convert...
158
159
160
161
162
163
  	jz4740_wdt = &drvdata->wdt;
  	jz4740_wdt->info = &jz4740_wdt_info;
  	jz4740_wdt->ops = &jz4740_wdt_ops;
  	jz4740_wdt->timeout = heartbeat;
  	jz4740_wdt->min_timeout = 1;
  	jz4740_wdt->max_timeout = MAX_HEARTBEAT;
6551881c8   Pratyush Anand   Watchdog: Fix par...
164
  	jz4740_wdt->parent = &pdev->dev;
85f6df149   Axel Lin   watchdog: Convert...
165
166
167
168
  	watchdog_set_nowayout(jz4740_wdt, nowayout);
  	watchdog_set_drvdata(jz4740_wdt, drvdata);
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4c271bb67   Thierry Reding   watchdog: Convert...
169
170
171
  	drvdata->base = devm_ioremap_resource(&pdev->dev, res);
  	if (IS_ERR(drvdata->base)) {
  		ret = PTR_ERR(drvdata->base);
85f6df149   Axel Lin   watchdog: Convert...
172
  		goto err_out;
f865c3522   Paul Cercueil   watchdog: add JZ4...
173
  	}
5f314970b   Lars-Peter Clausen   watchdog: jz4740:...
174
  	drvdata->rtc_clk = clk_get(&pdev->dev, "rtc");
85f6df149   Axel Lin   watchdog: Convert...
175
176
177
178
179
  	if (IS_ERR(drvdata->rtc_clk)) {
  		dev_err(&pdev->dev, "cannot find RTC clock
  ");
  		ret = PTR_ERR(drvdata->rtc_clk);
  		goto err_out;
f865c3522   Paul Cercueil   watchdog: add JZ4...
180
  	}
85f6df149   Axel Lin   watchdog: Convert...
181
182
  	ret = watchdog_register_device(&drvdata->wdt);
  	if (ret < 0)
f865c3522   Paul Cercueil   watchdog: add JZ4...
183
  		goto err_disable_clk;
f865c3522   Paul Cercueil   watchdog: add JZ4...
184

85f6df149   Axel Lin   watchdog: Convert...
185
  	platform_set_drvdata(pdev, drvdata);
f865c3522   Paul Cercueil   watchdog: add JZ4...
186
187
188
  	return 0;
  
  err_disable_clk:
85f6df149   Axel Lin   watchdog: Convert...
189
190
  	clk_put(drvdata->rtc_clk);
  err_out:
f865c3522   Paul Cercueil   watchdog: add JZ4...
191
192
  	return ret;
  }
4b12b896c   Bill Pemberton   watchdog: remove ...
193
  static int jz4740_wdt_remove(struct platform_device *pdev)
f865c3522   Paul Cercueil   watchdog: add JZ4...
194
  {
85f6df149   Axel Lin   watchdog: Convert...
195
  	struct jz4740_wdt_drvdata *drvdata = platform_get_drvdata(pdev);
f865c3522   Paul Cercueil   watchdog: add JZ4...
196

85f6df149   Axel Lin   watchdog: Convert...
197
198
199
  	jz4740_wdt_stop(&drvdata->wdt);
  	watchdog_unregister_device(&drvdata->wdt);
  	clk_put(drvdata->rtc_clk);
f865c3522   Paul Cercueil   watchdog: add JZ4...
200
201
202
  
  	return 0;
  }
f865c3522   Paul Cercueil   watchdog: add JZ4...
203
204
  static struct platform_driver jz4740_wdt_driver = {
  	.probe = jz4740_wdt_probe,
82268714b   Bill Pemberton   watchdog: remove ...
205
  	.remove = jz4740_wdt_remove,
f865c3522   Paul Cercueil   watchdog: add JZ4...
206
207
  	.driver = {
  		.name = "jz4740-wdt",
6b96c7227   Zubair Lutfullah Kakakhel   watchdog: jz4740:...
208
  		.of_match_table = of_match_ptr(jz4740_wdt_of_matches),
f865c3522   Paul Cercueil   watchdog: add JZ4...
209
210
  	},
  };
b8ec61189   Axel Lin   watchdog: convert...
211
  module_platform_driver(jz4740_wdt_driver);
f865c3522   Paul Cercueil   watchdog: add JZ4...
212
213
214
  
  MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
  MODULE_DESCRIPTION("jz4740 Watchdog Driver");
f865c3522   Paul Cercueil   watchdog: add JZ4...
215
  MODULE_LICENSE("GPL");
f865c3522   Paul Cercueil   watchdog: add JZ4...
216
  MODULE_ALIAS("platform:jz4740-wdt");