2 * Watchdog driver for Technologic Systems TS-72xx based SBCs
3 * (TS-7200, TS-7250 and TS-7260). These boards have external
4 * glue logic CPLD chip, which includes programmable watchdog
7 * Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi>
9 * This driver is based on ep93xx_wdt and wm831x_wdt drivers.
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/miscdevice.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/watchdog.h>
25 #include <linux/uaccess.h>
27 #define TS72XX_WDT_FEED_VAL 0x05
28 #define TS72XX_WDT_DEFAULT_TIMEOUT 8
30 static int timeout
= TS72XX_WDT_DEFAULT_TIMEOUT
;
31 module_param(timeout
, int, 0);
32 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds. "
33 "(1 <= timeout <= 8, default="
34 __MODULE_STRING(TS72XX_WDT_DEFAULT_TIMEOUT
)
37 static int nowayout
= WATCHDOG_NOWAYOUT
;
38 module_param(nowayout
, int, 0);
39 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
42 * struct ts72xx_wdt - watchdog control structure
43 * @lock: lock that protects this structure
44 * @regval: watchdog timeout value suitable for control register
45 * @flags: flags controlling watchdog device state
46 * @control_reg: watchdog control register
47 * @feed_reg: watchdog feed register
48 * @pdev: back pointer to platform dev
54 #define TS72XX_WDT_BUSY_FLAG 1
55 #define TS72XX_WDT_EXPECT_CLOSE_FLAG 2
58 void __iomem
*control_reg
;
59 void __iomem
*feed_reg
;
61 struct platform_device
*pdev
;
64 struct platform_device
*ts72xx_wdt_pdev
;
67 * TS-72xx Watchdog supports following timeouts (value written
68 * to control register):
70 * -------------------------
71 * 0x00 watchdog disabled
80 * Timeouts below 1s are not very usable so we don't
83 * We provide two functions that convert between these:
84 * timeout_to_regval() and regval_to_timeout().
89 } ts72xx_wdt_map
[] = {
97 * timeout_to_regval() - converts given timeout to control register value
98 * @new_timeout: timeout in seconds to be converted
100 * Function converts given @new_timeout into valid value that can
101 * be programmed into watchdog control register. When conversion is
102 * not possible, function returns %-EINVAL.
104 static int timeout_to_regval(int new_timeout
)
108 /* first limit it to 1 - 8 seconds */
109 new_timeout
= clamp_val(new_timeout
, 1, 8);
111 for (i
= 0; i
< ARRAY_SIZE(ts72xx_wdt_map
); i
++) {
112 if (ts72xx_wdt_map
[i
].timeout
>= new_timeout
)
113 return ts72xx_wdt_map
[i
].regval
;
120 * regval_to_timeout() - converts control register value to timeout
121 * @regval: control register value to be converted
123 * Function converts given @regval to timeout in seconds (1, 2, 4 or 8).
124 * If @regval cannot be converted, function returns %-EINVAL.
126 static int regval_to_timeout(int regval
)
130 for (i
= 0; i
< ARRAY_SIZE(ts72xx_wdt_map
); i
++) {
131 if (ts72xx_wdt_map
[i
].regval
== regval
)
132 return ts72xx_wdt_map
[i
].timeout
;
139 * ts72xx_wdt_kick() - kick the watchdog
140 * @wdt: watchdog to be kicked
142 * Called with @wdt->lock held.
144 static inline void ts72xx_wdt_kick(struct ts72xx_wdt
*wdt
)
146 __raw_writeb(TS72XX_WDT_FEED_VAL
, wdt
->feed_reg
);
150 * ts72xx_wdt_start() - starts the watchdog timer
151 * @wdt: watchdog to be started
153 * This function programs timeout to watchdog timer
156 * Called with @wdt->lock held.
158 static void ts72xx_wdt_start(struct ts72xx_wdt
*wdt
)
161 * To program the wdt, it first must be "fed" and
162 * only after that (within 30 usecs) the configuration
165 ts72xx_wdt_kick(wdt
);
166 __raw_writeb((u8
)wdt
->regval
, wdt
->control_reg
);
170 * ts72xx_wdt_stop() - stops the watchdog timer
171 * @wdt: watchdog to be stopped
173 * Called with @wdt->lock held.
175 static void ts72xx_wdt_stop(struct ts72xx_wdt
*wdt
)
177 ts72xx_wdt_kick(wdt
);
178 __raw_writeb(0, wdt
->control_reg
);
181 static int ts72xx_wdt_open(struct inode
*inode
, struct file
*file
)
183 struct ts72xx_wdt
*wdt
= platform_get_drvdata(ts72xx_wdt_pdev
);
187 * Try to convert default timeout to valid register
190 regval
= timeout_to_regval(timeout
);
192 dev_err(&wdt
->pdev
->dev
,
193 "failed to convert timeout (%d) to register value\n",
198 if (mutex_lock_interruptible(&wdt
->lock
))
201 if ((wdt
->flags
& TS72XX_WDT_BUSY_FLAG
) != 0) {
202 mutex_unlock(&wdt
->lock
);
206 wdt
->flags
= TS72XX_WDT_BUSY_FLAG
;
207 wdt
->regval
= regval
;
208 file
->private_data
= wdt
;
210 ts72xx_wdt_start(wdt
);
212 mutex_unlock(&wdt
->lock
);
213 return nonseekable_open(inode
, file
);
216 static int ts72xx_wdt_release(struct inode
*inode
, struct file
*file
)
218 struct ts72xx_wdt
*wdt
= file
->private_data
;
220 if (mutex_lock_interruptible(&wdt
->lock
))
223 if ((wdt
->flags
& TS72XX_WDT_EXPECT_CLOSE_FLAG
) != 0) {
224 ts72xx_wdt_stop(wdt
);
226 dev_warn(&wdt
->pdev
->dev
,
227 "TS-72XX WDT device closed unexpectly. "
228 "Watchdog timer will not stop!\n");
230 * Kick it one more time, to give userland some time
231 * to recover (for example, respawning the kicker
234 ts72xx_wdt_kick(wdt
);
239 mutex_unlock(&wdt
->lock
);
243 static ssize_t
ts72xx_wdt_write(struct file
*file
,
244 const char __user
*data
,
248 struct ts72xx_wdt
*wdt
= file
->private_data
;
253 if (mutex_lock_interruptible(&wdt
->lock
))
256 ts72xx_wdt_kick(wdt
);
259 * Support for magic character closing. User process
260 * writes 'V' into the device, just before it is closed.
261 * This means that we know that the wdt timer can be
262 * stopped after user closes the device.
267 for (i
= 0; i
< len
; i
++) {
270 /* In case it was set long ago */
271 wdt
->flags
&= ~TS72XX_WDT_EXPECT_CLOSE_FLAG
;
273 if (get_user(c
, data
+ i
)) {
274 mutex_unlock(&wdt
->lock
);
278 wdt
->flags
|= TS72XX_WDT_EXPECT_CLOSE_FLAG
;
284 mutex_unlock(&wdt
->lock
);
288 static const struct watchdog_info winfo
= {
289 .options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
|
291 .firmware_version
= 1,
292 .identity
= "TS-72XX WDT",
295 static long ts72xx_wdt_ioctl(struct file
*file
, unsigned int cmd
,
298 struct ts72xx_wdt
*wdt
= file
->private_data
;
299 void __user
*argp
= (void __user
*)arg
;
300 int __user
*p
= (int __user
*)argp
;
303 if (mutex_lock_interruptible(&wdt
->lock
))
307 case WDIOC_GETSUPPORT
:
308 error
= copy_to_user(argp
, &winfo
, sizeof(winfo
));
311 case WDIOC_GETSTATUS
:
312 case WDIOC_GETBOOTSTATUS
:
313 return put_user(0, p
);
315 case WDIOC_KEEPALIVE
:
316 ts72xx_wdt_kick(wdt
);
319 case WDIOC_SETOPTIONS
: {
322 if (get_user(options
, p
)) {
329 if ((options
& WDIOS_DISABLECARD
) != 0) {
330 ts72xx_wdt_stop(wdt
);
333 if ((options
& WDIOS_ENABLECARD
) != 0) {
334 ts72xx_wdt_start(wdt
);
341 case WDIOC_SETTIMEOUT
: {
344 if (get_user(new_timeout
, p
)) {
349 regval
= timeout_to_regval(new_timeout
);
353 ts72xx_wdt_stop(wdt
);
354 wdt
->regval
= regval
;
355 ts72xx_wdt_start(wdt
);
364 case WDIOC_GETTIMEOUT
:
365 if (put_user(regval_to_timeout(wdt
->regval
), p
))
374 mutex_unlock(&wdt
->lock
);
378 static const struct file_operations ts72xx_wdt_fops
= {
379 .owner
= THIS_MODULE
,
381 .open
= ts72xx_wdt_open
,
382 .release
= ts72xx_wdt_release
,
383 .write
= ts72xx_wdt_write
,
384 .unlocked_ioctl
= ts72xx_wdt_ioctl
,
387 static struct miscdevice ts72xx_wdt_miscdev
= {
388 .minor
= WATCHDOG_MINOR
,
390 .fops
= &ts72xx_wdt_fops
,
393 static __devinit
int ts72xx_wdt_probe(struct platform_device
*pdev
)
395 struct ts72xx_wdt
*wdt
;
396 struct resource
*r1
, *r2
;
399 wdt
= kzalloc(sizeof(struct ts72xx_wdt
), GFP_KERNEL
);
401 dev_err(&pdev
->dev
, "failed to allocate memory\n");
405 r1
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
407 dev_err(&pdev
->dev
, "failed to get memory resource\n");
412 r1
= request_mem_region(r1
->start
, resource_size(r1
), pdev
->name
);
414 dev_err(&pdev
->dev
, "cannot request memory region\n");
419 wdt
->control_reg
= ioremap(r1
->start
, resource_size(r1
));
420 if (!wdt
->control_reg
) {
421 dev_err(&pdev
->dev
, "failed to map memory\n");
423 goto fail_free_control
;
426 r2
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
428 dev_err(&pdev
->dev
, "failed to get memory resource\n");
430 goto fail_unmap_control
;
433 r2
= request_mem_region(r2
->start
, resource_size(r2
), pdev
->name
);
435 dev_err(&pdev
->dev
, "cannot request memory region\n");
437 goto fail_unmap_control
;
440 wdt
->feed_reg
= ioremap(r2
->start
, resource_size(r2
));
441 if (!wdt
->feed_reg
) {
442 dev_err(&pdev
->dev
, "failed to map memory\n");
447 platform_set_drvdata(pdev
, wdt
);
448 ts72xx_wdt_pdev
= pdev
;
450 mutex_init(&wdt
->lock
);
452 /* make sure that the watchdog is disabled */
453 ts72xx_wdt_stop(wdt
);
455 error
= misc_register(&ts72xx_wdt_miscdev
);
457 dev_err(&pdev
->dev
, "failed to register miscdev\n");
458 goto fail_unmap_feed
;
461 dev_info(&pdev
->dev
, "TS-72xx Watchdog driver\n");
466 platform_set_drvdata(pdev
, NULL
);
467 iounmap(wdt
->feed_reg
);
469 release_mem_region(r2
->start
, resource_size(r2
));
471 iounmap(wdt
->control_reg
);
473 release_mem_region(r1
->start
, resource_size(r1
));
479 static __devexit
int ts72xx_wdt_remove(struct platform_device
*pdev
)
481 struct ts72xx_wdt
*wdt
= platform_get_drvdata(pdev
);
482 struct resource
*res
;
485 error
= misc_deregister(&ts72xx_wdt_miscdev
);
486 platform_set_drvdata(pdev
, NULL
);
488 iounmap(wdt
->feed_reg
);
489 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
490 release_mem_region(res
->start
, resource_size(res
));
492 iounmap(wdt
->control_reg
);
493 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
494 release_mem_region(res
->start
, resource_size(res
));
500 static struct platform_driver ts72xx_wdt_driver
= {
501 .probe
= ts72xx_wdt_probe
,
502 .remove
= __devexit_p(ts72xx_wdt_remove
),
504 .name
= "ts72xx-wdt",
505 .owner
= THIS_MODULE
,
509 static __init
int ts72xx_wdt_init(void)
511 return platform_driver_register(&ts72xx_wdt_driver
);
513 module_init(ts72xx_wdt_init
);
515 static __exit
void ts72xx_wdt_exit(void)
517 platform_driver_unregister(&ts72xx_wdt_driver
);
519 module_exit(ts72xx_wdt_exit
);
521 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
522 MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
523 MODULE_LICENSE("GPL");
524 MODULE_ALIAS("platform:ts72xx-wdt");