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/watchdog.h>
24 #include <linux/uaccess.h>
26 #define TS72XX_WDT_FEED_VAL 0x05
27 #define TS72XX_WDT_DEFAULT_TIMEOUT 8
29 static int timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
30 module_param(timeout, int, 0);
31 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. "
32 "(1 <= timeout <= 8, default="
33 __MODULE_STRING(TS72XX_WDT_DEFAULT_TIMEOUT)
36 static int nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, int, 0);
38 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
41 * struct ts72xx_wdt - watchdog control structure
42 * @lock: lock that protects this structure
43 * @regval: watchdog timeout value suitable for control register
44 * @flags: flags controlling watchdog device state
45 * @control_reg: watchdog control register
46 * @feed_reg: watchdog feed register
47 * @pdev: back pointer to platform dev
53 #define TS72XX_WDT_BUSY_FLAG 1
54 #define TS72XX_WDT_EXPECT_CLOSE_FLAG 2
57 void __iomem *control_reg;
58 void __iomem *feed_reg;
60 struct platform_device *pdev;
63 struct platform_device *ts72xx_wdt_pdev;
66 * TS-72xx Watchdog supports following timeouts (value written
67 * to control register):
69 * -------------------------
70 * 0x00 watchdog disabled
79 * Timeouts below 1s are not very usable so we don't
82 * We provide two functions that convert between these:
83 * timeout_to_regval() and regval_to_timeout().
88 } ts72xx_wdt_map[] = {
96 * timeout_to_regval() - converts given timeout to control register value
97 * @new_timeout: timeout in seconds to be converted
99 * Function converts given @new_timeout into valid value that can
100 * be programmed into watchdog control register. When conversion is
101 * not possible, function returns %-EINVAL.
103 static int timeout_to_regval(int new_timeout)
107 /* first limit it to 1 - 8 seconds */
108 new_timeout = clamp_val(new_timeout, 1, 8);
110 for (i = 0; i < ARRAY_SIZE(ts72xx_wdt_map); i++) {
111 if (ts72xx_wdt_map[i].timeout >= new_timeout)
112 return ts72xx_wdt_map[i].regval;
119 * regval_to_timeout() - converts control register value to timeout
120 * @regval: control register value to be converted
122 * Function converts given @regval to timeout in seconds (1, 2, 4 or 8).
123 * If @regval cannot be converted, function returns %-EINVAL.
125 static int regval_to_timeout(int regval)
129 for (i = 0; i < ARRAY_SIZE(ts72xx_wdt_map); i++) {
130 if (ts72xx_wdt_map[i].regval == regval)
131 return ts72xx_wdt_map[i].timeout;
138 * ts72xx_wdt_kick() - kick the watchdog
139 * @wdt: watchdog to be kicked
141 * Called with @wdt->lock held.
143 static inline void ts72xx_wdt_kick(struct ts72xx_wdt *wdt)
145 __raw_writeb(TS72XX_WDT_FEED_VAL, wdt->feed_reg);
149 * ts72xx_wdt_start() - starts the watchdog timer
150 * @wdt: watchdog to be started
152 * This function programs timeout to watchdog timer
155 * Called with @wdt->lock held.
157 static void ts72xx_wdt_start(struct ts72xx_wdt *wdt)
160 * To program the wdt, it first must be "fed" and
161 * only after that (within 30 usecs) the configuration
164 ts72xx_wdt_kick(wdt);
165 __raw_writeb((u8)wdt->regval, wdt->control_reg);
169 * ts72xx_wdt_stop() - stops the watchdog timer
170 * @wdt: watchdog to be stopped
172 * Called with @wdt->lock held.
174 static void ts72xx_wdt_stop(struct ts72xx_wdt *wdt)
176 ts72xx_wdt_kick(wdt);
177 __raw_writeb(0, wdt->control_reg);
180 static int ts72xx_wdt_open(struct inode *inode, struct file *file)
182 struct ts72xx_wdt *wdt = platform_get_drvdata(ts72xx_wdt_pdev);
186 * Try to convert default timeout to valid register
189 regval = timeout_to_regval(timeout);
191 dev_err(&wdt->pdev->dev,
192 "failed to convert timeout (%d) to register value\n",
197 if (mutex_lock_interruptible(&wdt->lock))
200 if ((wdt->flags & TS72XX_WDT_BUSY_FLAG) != 0) {
201 mutex_unlock(&wdt->lock);
205 wdt->flags = TS72XX_WDT_BUSY_FLAG;
206 wdt->regval = regval;
207 file->private_data = wdt;
209 ts72xx_wdt_start(wdt);
211 mutex_unlock(&wdt->lock);
212 return nonseekable_open(inode, file);
215 static int ts72xx_wdt_release(struct inode *inode, struct file *file)
217 struct ts72xx_wdt *wdt = file->private_data;
219 if (mutex_lock_interruptible(&wdt->lock))
222 if ((wdt->flags & TS72XX_WDT_EXPECT_CLOSE_FLAG) != 0) {
223 ts72xx_wdt_stop(wdt);
225 dev_warn(&wdt->pdev->dev,
226 "TS-72XX WDT device closed unexpectly. "
227 "Watchdog timer will not stop!\n");
229 * Kick it one more time, to give userland some time
230 * to recover (for example, respawning the kicker
233 ts72xx_wdt_kick(wdt);
238 mutex_unlock(&wdt->lock);
242 static ssize_t ts72xx_wdt_write(struct file *file,
243 const char __user *data,
247 struct ts72xx_wdt *wdt = file->private_data;
252 if (mutex_lock_interruptible(&wdt->lock))
255 ts72xx_wdt_kick(wdt);
258 * Support for magic character closing. User process
259 * writes 'V' into the device, just before it is closed.
260 * This means that we know that the wdt timer can be
261 * stopped after user closes the device.
266 for (i = 0; i < len; i++) {
269 /* In case it was set long ago */
270 wdt->flags &= ~TS72XX_WDT_EXPECT_CLOSE_FLAG;
272 if (get_user(c, data + i)) {
273 mutex_unlock(&wdt->lock);
277 wdt->flags |= TS72XX_WDT_EXPECT_CLOSE_FLAG;
283 mutex_unlock(&wdt->lock);
287 static const struct watchdog_info winfo = {
288 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
290 .firmware_version = 1,
291 .identity = "TS-72XX WDT",
294 static long ts72xx_wdt_ioctl(struct file *file, unsigned int cmd,
297 struct ts72xx_wdt *wdt = file->private_data;
298 void __user *argp = (void __user *)arg;
299 int __user *p = (int __user *)argp;
302 if (mutex_lock_interruptible(&wdt->lock))
306 case WDIOC_GETSUPPORT:
307 error = copy_to_user(argp, &winfo, sizeof(winfo));
310 case WDIOC_GETSTATUS:
311 case WDIOC_GETBOOTSTATUS:
312 return put_user(0, p);
314 case WDIOC_KEEPALIVE:
315 ts72xx_wdt_kick(wdt);
318 case WDIOC_SETOPTIONS: {
321 if (get_user(options, p)) {
328 if ((options & WDIOS_DISABLECARD) != 0) {
329 ts72xx_wdt_stop(wdt);
332 if ((options & WDIOS_ENABLECARD) != 0) {
333 ts72xx_wdt_start(wdt);
340 case WDIOC_SETTIMEOUT: {
343 if (get_user(new_timeout, p)) {
348 regval = timeout_to_regval(new_timeout);
352 ts72xx_wdt_stop(wdt);
353 wdt->regval = regval;
354 ts72xx_wdt_start(wdt);
363 case WDIOC_GETTIMEOUT:
364 if (put_user(regval_to_timeout(wdt->regval), p))
373 mutex_unlock(&wdt->lock);
377 static const struct file_operations ts72xx_wdt_fops = {
378 .owner = THIS_MODULE,
380 .open = ts72xx_wdt_open,
381 .release = ts72xx_wdt_release,
382 .write = ts72xx_wdt_write,
383 .unlocked_ioctl = ts72xx_wdt_ioctl,
386 static struct miscdevice ts72xx_wdt_miscdev = {
387 .minor = WATCHDOG_MINOR,
389 .fops = &ts72xx_wdt_fops,
392 static __devinit int ts72xx_wdt_probe(struct platform_device *pdev)
394 struct ts72xx_wdt *wdt;
395 struct resource *r1, *r2;
398 wdt = kzalloc(sizeof(struct ts72xx_wdt), GFP_KERNEL);
400 dev_err(&pdev->dev, "failed to allocate memory\n");
404 r1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
406 dev_err(&pdev->dev, "failed to get memory resource\n");
411 r1 = request_mem_region(r1->start, resource_size(r1), pdev->name);
413 dev_err(&pdev->dev, "cannot request memory region\n");
418 wdt->control_reg = ioremap(r1->start, resource_size(r1));
419 if (!wdt->control_reg) {
420 dev_err(&pdev->dev, "failed to map memory\n");
422 goto fail_free_control;
425 r2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
427 dev_err(&pdev->dev, "failed to get memory resource\n");
429 goto fail_unmap_control;
432 r2 = request_mem_region(r2->start, resource_size(r2), pdev->name);
434 dev_err(&pdev->dev, "cannot request memory region\n");
436 goto fail_unmap_control;
439 wdt->feed_reg = ioremap(r2->start, resource_size(r2));
440 if (!wdt->feed_reg) {
441 dev_err(&pdev->dev, "failed to map memory\n");
446 platform_set_drvdata(pdev, wdt);
447 ts72xx_wdt_pdev = pdev;
449 mutex_init(&wdt->lock);
451 error = misc_register(&ts72xx_wdt_miscdev);
453 dev_err(&pdev->dev, "failed to register miscdev\n");
454 goto fail_unmap_feed;
457 dev_info(&pdev->dev, "TS-72xx Watchdog driver\n");
462 platform_set_drvdata(pdev, NULL);
463 iounmap(wdt->feed_reg);
465 release_mem_region(r2->start, resource_size(r2));
467 iounmap(wdt->control_reg);
469 release_mem_region(r1->start, resource_size(r1));
475 static __devexit int ts72xx_wdt_remove(struct platform_device *pdev)
477 struct ts72xx_wdt *wdt = platform_get_drvdata(pdev);
478 struct resource *res;
481 error = misc_deregister(&ts72xx_wdt_miscdev);
482 platform_set_drvdata(pdev, NULL);
484 iounmap(wdt->feed_reg);
485 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
486 release_mem_region(res->start, resource_size(res));
488 iounmap(wdt->control_reg);
489 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
490 release_mem_region(res->start, resource_size(res));
496 static struct platform_driver ts72xx_wdt_driver = {
497 .probe = ts72xx_wdt_probe,
498 .remove = __devexit_p(ts72xx_wdt_remove),
500 .name = "ts72xx-wdt",
501 .owner = THIS_MODULE,
505 static __init int ts72xx_wdt_init(void)
507 return platform_driver_register(&ts72xx_wdt_driver);
509 module_init(ts72xx_wdt_init);
511 static __exit void ts72xx_wdt_exit(void)
513 platform_driver_unregister(&ts72xx_wdt_driver);
515 module_exit(ts72xx_wdt_exit);
517 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
518 MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
519 MODULE_LICENSE("GPL");
520 MODULE_ALIAS("platform:ts72xx-wdt");