aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/pnx4008_wdt.c
diff options
context:
space:
mode:
authorWolfram Sang <w.sang@pengutronix.de>2012-02-02 12:48:11 -0500
committerWim Van Sebroeck <wim@iguana.be>2012-03-27 14:06:37 -0400
commit6b1e83869d13a6b5ce9ceb4b8f79a7538d467dee (patch)
treeb4d9cc2997cad8c9b8a699132a6f41afb4f9e405 /drivers/watchdog/pnx4008_wdt.c
parent2e51d90f4db6c94bc75c6ff22e959237f3cc27ba (diff)
watchdog: pnx4008: convert driver to use the watchdog framework
Make this driver a user of the watchdog framework and remove parts now handled by the core. Tested on a custom lpc32xx-board. [wim@iguana.be: Added set_timeout operation] Signed-off-by: Wolfram Sang <w.sang@pengutronix.de> Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
Diffstat (limited to 'drivers/watchdog/pnx4008_wdt.c')
-rw-r--r--drivers/watchdog/pnx4008_wdt.c180
1 files changed, 40 insertions, 140 deletions
diff --git a/drivers/watchdog/pnx4008_wdt.c b/drivers/watchdog/pnx4008_wdt.c
index 38fe5488cc96..3d9bb89776a7 100644
--- a/drivers/watchdog/pnx4008_wdt.c
+++ b/drivers/watchdog/pnx4008_wdt.c
@@ -8,10 +8,13 @@
8 * Based on sa1100 driver, 8 * Based on sa1100 driver,
9 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu> 9 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
10 * 10 *
11 * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under 11 * 2005-2006 (c) MontaVista Software, Inc.
12 * the terms of the GNU General Public License version 2. This program 12 *
13 * is licensed "as is" without any warranty of any kind, whether express 13 * (C) 2012 Wolfram Sang, Pengutronix
14 * or implied. 14 *
15 * This file is licensed under the terms of the GNU General Public License
16 * version 2. This program is licensed "as is" without any warranty of any
17 * kind, whether express or implied.
15 */ 18 */
16 19
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -20,23 +23,17 @@
20#include <linux/moduleparam.h> 23#include <linux/moduleparam.h>
21#include <linux/types.h> 24#include <linux/types.h>
22#include <linux/kernel.h> 25#include <linux/kernel.h>
23#include <linux/fs.h>
24#include <linux/miscdevice.h> 26#include <linux/miscdevice.h>
25#include <linux/watchdog.h> 27#include <linux/watchdog.h>
26#include <linux/init.h> 28#include <linux/init.h>
27#include <linux/bitops.h>
28#include <linux/ioport.h>
29#include <linux/device.h>
30#include <linux/platform_device.h> 29#include <linux/platform_device.h>
31#include <linux/clk.h> 30#include <linux/clk.h>
32#include <linux/spinlock.h> 31#include <linux/spinlock.h>
33#include <linux/uaccess.h>
34#include <linux/io.h> 32#include <linux/io.h>
35#include <linux/slab.h> 33#include <linux/slab.h>
34#include <linux/err.h>
36#include <mach/hardware.h> 35#include <mach/hardware.h>
37 36
38#define MODULE_NAME "PNX4008-WDT: "
39
40/* WatchDog Timer - Chapter 23 Page 207 */ 37/* WatchDog Timer - Chapter 23 Page 207 */
41 38
42#define DEFAULT_HEARTBEAT 19 39#define DEFAULT_HEARTBEAT 19
@@ -80,19 +77,13 @@
80#define WDOG_COUNTER_RATE 13000000 /*the counter clock is 13 MHz fixed */ 77#define WDOG_COUNTER_RATE 13000000 /*the counter clock is 13 MHz fixed */
81 78
82static bool nowayout = WATCHDOG_NOWAYOUT; 79static bool nowayout = WATCHDOG_NOWAYOUT;
83static int heartbeat = DEFAULT_HEARTBEAT; 80static unsigned int heartbeat = DEFAULT_HEARTBEAT;
84 81
85static DEFINE_SPINLOCK(io_lock); 82static DEFINE_SPINLOCK(io_lock);
86static unsigned long wdt_status;
87#define WDT_IN_USE 0
88#define WDT_OK_TO_CLOSE 1
89
90static unsigned long boot_status;
91
92static void __iomem *wdt_base; 83static void __iomem *wdt_base;
93struct clk *wdt_clk; 84struct clk *wdt_clk;
94 85
95static void wdt_enable(void) 86static int pnx4008_wdt_start(struct watchdog_device *wdd)
96{ 87{
97 spin_lock(&io_lock); 88 spin_lock(&io_lock);
98 89
@@ -109,144 +100,48 @@ static void wdt_enable(void)
109 writel(MATCH_INT, WDTIM_INT(wdt_base)); 100 writel(MATCH_INT, WDTIM_INT(wdt_base));
110 /* the longest pulse period 65541/(13*10^6) seconds ~ 5 ms. */ 101 /* the longest pulse period 65541/(13*10^6) seconds ~ 5 ms. */
111 writel(0xFFFF, WDTIM_PULSE(wdt_base)); 102 writel(0xFFFF, WDTIM_PULSE(wdt_base));
112 writel(heartbeat * WDOG_COUNTER_RATE, WDTIM_MATCH0(wdt_base)); 103 writel(wdd->timeout * WDOG_COUNTER_RATE, WDTIM_MATCH0(wdt_base));
113 /*enable counter, stop when debugger active */ 104 /*enable counter, stop when debugger active */
114 writel(COUNT_ENAB | DEBUG_EN, WDTIM_CTRL(wdt_base)); 105 writel(COUNT_ENAB | DEBUG_EN, WDTIM_CTRL(wdt_base));
115 106
116 spin_unlock(&io_lock); 107 spin_unlock(&io_lock);
108 return 0;
117} 109}
118 110
119static void wdt_disable(void) 111static int pnx4008_wdt_stop(struct watchdog_device *wdd)
120{ 112{
121 spin_lock(&io_lock); 113 spin_lock(&io_lock);
122 114
123 writel(0, WDTIM_CTRL(wdt_base)); /*stop counter */ 115 writel(0, WDTIM_CTRL(wdt_base)); /*stop counter */
124 116
125 spin_unlock(&io_lock); 117 spin_unlock(&io_lock);
118 return 0;
126} 119}
127 120
128static int pnx4008_wdt_open(struct inode *inode, struct file *file) 121static int pnx4008_wdt_set_timeout(struct watchdog_device *wdd,
129{ 122 unsigned int new_timeout)
130 int ret;
131
132 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
133 return -EBUSY;
134
135 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
136
137 ret = clk_enable(wdt_clk);
138 if (ret) {
139 clear_bit(WDT_IN_USE, &wdt_status);
140 return ret;
141 }
142
143 wdt_enable();
144
145 return nonseekable_open(inode, file);
146}
147
148static ssize_t pnx4008_wdt_write(struct file *file, const char *data,
149 size_t len, loff_t *ppos)
150{ 123{
151 if (len) { 124 return 0;
152 if (!nowayout) {
153 size_t i;
154
155 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
156
157 for (i = 0; i != len; i++) {
158 char c;
159
160 if (get_user(c, data + i))
161 return -EFAULT;
162 if (c == 'V')
163 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
164 }
165 }
166 wdt_enable();
167 }
168
169 return len;
170} 125}
171 126
172static const struct watchdog_info ident = { 127static const struct watchdog_info pnx4008_wdt_ident = {
173 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE | 128 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
174 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, 129 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
175 .identity = "PNX4008 Watchdog", 130 .identity = "PNX4008 Watchdog",
176}; 131};
177 132
178static long pnx4008_wdt_ioctl(struct file *file, unsigned int cmd, 133static const struct watchdog_ops pnx4008_wdt_ops = {
179 unsigned long arg)
180{
181 int ret = -ENOTTY;
182 int time;
183
184 switch (cmd) {
185 case WDIOC_GETSUPPORT:
186 ret = copy_to_user((struct watchdog_info *)arg, &ident,
187 sizeof(ident)) ? -EFAULT : 0;
188 break;
189
190 case WDIOC_GETSTATUS:
191 ret = put_user(0, (int *)arg);
192 break;
193
194 case WDIOC_GETBOOTSTATUS:
195 ret = put_user(boot_status, (int *)arg);
196 break;
197
198 case WDIOC_KEEPALIVE:
199 wdt_enable();
200 ret = 0;
201 break;
202
203 case WDIOC_SETTIMEOUT:
204 ret = get_user(time, (int *)arg);
205 if (ret)
206 break;
207
208 if (time <= 0 || time > MAX_HEARTBEAT) {
209 ret = -EINVAL;
210 break;
211 }
212
213 heartbeat = time;
214 wdt_enable();
215 /* Fall through */
216
217 case WDIOC_GETTIMEOUT:
218 ret = put_user(heartbeat, (int *)arg);
219 break;
220 }
221 return ret;
222}
223
224static int pnx4008_wdt_release(struct inode *inode, struct file *file)
225{
226 if (!test_bit(WDT_OK_TO_CLOSE, &wdt_status))
227 pr_warn("Device closed unexpectedly\n");
228
229 wdt_disable();
230 clk_disable(wdt_clk);
231 clear_bit(WDT_IN_USE, &wdt_status);
232 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
233
234 return 0;
235}
236
237static const struct file_operations pnx4008_wdt_fops = {
238 .owner = THIS_MODULE, 134 .owner = THIS_MODULE,
239 .llseek = no_llseek, 135 .start = pnx4008_wdt_start,
240 .write = pnx4008_wdt_write, 136 .stop = pnx4008_wdt_stop,
241 .unlocked_ioctl = pnx4008_wdt_ioctl, 137 .set_timeout = pnx4008_wdt_set_timeout,
242 .open = pnx4008_wdt_open,
243 .release = pnx4008_wdt_release,
244}; 138};
245 139
246static struct miscdevice pnx4008_wdt_miscdev = { 140static struct watchdog_device pnx4008_wdd = {
247 .minor = WATCHDOG_MINOR, 141 .info = &pnx4008_wdt_ident,
248 .name = "watchdog", 142 .ops = &pnx4008_wdt_ops,
249 .fops = &pnx4008_wdt_fops, 143 .min_timeout = 1,
144 .max_timeout = MAX_HEARTBEAT,
250}; 145};
251 146
252static int __devinit pnx4008_wdt_probe(struct platform_device *pdev) 147static int __devinit pnx4008_wdt_probe(struct platform_device *pdev)
@@ -270,15 +165,17 @@ static int __devinit pnx4008_wdt_probe(struct platform_device *pdev)
270 if (ret) 165 if (ret)
271 goto out; 166 goto out;
272 167
273 boot_status = (readl(WDTIM_RES(wdt_base)) & WDOG_RESET) ? 168 pnx4008_wdd.timeout = heartbeat;
169 pnx4008_wdd.bootstatus = (readl(WDTIM_RES(wdt_base)) & WDOG_RESET) ?
274 WDIOF_CARDRESET : 0; 170 WDIOF_CARDRESET : 0;
275 wdt_disable(); /*disable for now */ 171 watchdog_set_nowayout(&pnx4008_wdd, nowayout);
276 clk_disable(wdt_clk); 172
173 pnx4008_wdt_stop(&pnx4008_wdd); /* disable for now */
277 174
278 ret = misc_register(&pnx4008_wdt_miscdev); 175 ret = watchdog_register_device(&pnx4008_wdd);
279 if (ret < 0) { 176 if (ret < 0) {
280 dev_err(&pdev->dev, "cannot register misc device\n"); 177 dev_err(&pdev->dev, "cannot register watchdog device\n");
281 goto out; 178 goto disable_clk;
282 } 179 }
283 180
284 dev_info(&pdev->dev, "PNX4008 Watchdog Timer: heartbeat %d sec\n", 181 dev_info(&pdev->dev, "PNX4008 Watchdog Timer: heartbeat %d sec\n",
@@ -286,6 +183,8 @@ static int __devinit pnx4008_wdt_probe(struct platform_device *pdev)
286 183
287 return 0; 184 return 0;
288 185
186disable_clk:
187 clk_disable(wdt_clk);
289out: 188out:
290 clk_put(wdt_clk); 189 clk_put(wdt_clk);
291 return ret; 190 return ret;
@@ -293,7 +192,7 @@ out:
293 192
294static int __devexit pnx4008_wdt_remove(struct platform_device *pdev) 193static int __devexit pnx4008_wdt_remove(struct platform_device *pdev)
295{ 194{
296 misc_deregister(&pnx4008_wdt_miscdev); 195 watchdog_unregister_device(&pnx4008_wdd);
297 196
298 clk_disable(wdt_clk); 197 clk_disable(wdt_clk);
299 clk_put(wdt_clk); 198 clk_put(wdt_clk);
@@ -313,9 +212,10 @@ static struct platform_driver platform_wdt_driver = {
313module_platform_driver(platform_wdt_driver); 212module_platform_driver(platform_wdt_driver);
314 213
315MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>"); 214MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
215MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
316MODULE_DESCRIPTION("PNX4008 Watchdog Driver"); 216MODULE_DESCRIPTION("PNX4008 Watchdog Driver");
317 217
318module_param(heartbeat, int, 0); 218module_param(heartbeat, uint, 0);
319MODULE_PARM_DESC(heartbeat, 219MODULE_PARM_DESC(heartbeat,
320 "Watchdog heartbeat period in seconds from 1 to " 220 "Watchdog heartbeat period in seconds from 1 to "
321 __MODULE_STRING(MAX_HEARTBEAT) ", default " 221 __MODULE_STRING(MAX_HEARTBEAT) ", default "