2 * Driver for the Atheros AR71xx SoC's built-in hardware watchdog timer.
4 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
8 * Author: Deepak Saxena <dsaxena@plexity.net>
9 * Copyright 2004 (c) MontaVista, Software, Inc.
11 * which again was based on sa1100 driver,
12 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
20 #include <linux/bitops.h>
21 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/types.h>
30 #include <linux/watchdog.h>
32 #include <asm/mach-ar71xx/ar71xx.h>
34 #define DRV_NAME "ar71xx-wdt"
35 #define DRV_DESC "Atheros AR71xx hardware watchdog driver"
36 #define DRV_VERSION "0.1.0"
38 #define WDT_TIMEOUT 15 /* seconds */
40 static int nowayout = WATCHDOG_NOWAYOUT;
42 #ifdef CONFIG_WATCHDOG_NOWAYOUT
43 module_param(nowayout, int, 0);
44 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
45 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
48 static unsigned long wdt_flags;
50 #define WDT_FLAGS_BUSY 0
51 #define WDT_FLAGS_EXPECT_CLOSE 1
53 static int wdt_timeout = WDT_TIMEOUT;
54 static int boot_status;
55 static int max_timeout;
57 static void inline ar71xx_wdt_keepalive(void)
59 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG, ar71xx_ahb_freq * wdt_timeout);
62 static void inline ar71xx_wdt_enable(void)
64 printk(KERN_DEBUG DRV_NAME ": enabling watchdog timer\n");
65 ar71xx_wdt_keepalive();
66 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
69 static void inline ar71xx_wdt_disable(void)
71 printk(KERN_DEBUG DRV_NAME ": disabling watchdog timer\n");
72 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
75 static int ar71xx_wdt_set_timeout(int val)
77 if (val < 1 || val > max_timeout)
81 ar71xx_wdt_keepalive();
83 printk(KERN_DEBUG DRV_NAME ": timeout=%d secs\n", wdt_timeout);
88 static int ar71xx_wdt_open(struct inode *inode, struct file *file)
90 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
93 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
97 return nonseekable_open(inode, file);
100 static int ar71xx_wdt_release(struct inode *inode, struct file *file)
102 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags)) {
103 ar71xx_wdt_disable();
105 printk(KERN_CRIT DRV_NAME ": device closed unexpectedly, "
106 "watchdog timer will not stop!\n");
109 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
110 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
115 static ssize_t ar71xx_wdt_write(struct file *file, const char *data,
116 size_t len, loff_t *ppos)
122 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
124 for (i = 0; i != len; i++) {
127 if (get_user(c, data + i))
131 set_bit(WDT_FLAGS_EXPECT_CLOSE,
136 ar71xx_wdt_keepalive();
142 static struct watchdog_info ar71xx_wdt_info = {
143 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
144 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
145 .firmware_version = 0,
146 .identity = "AR71XX watchdog",
149 static int ar71xx_wdt_ioctl(struct inode *inode, struct file *file,
150 unsigned int cmd, unsigned long arg)
156 case WDIOC_GETSUPPORT:
157 ret = copy_to_user((struct watchdog_info *)arg,
159 sizeof(&ar71xx_wdt_info)) ? -EFAULT : 0;
162 case WDIOC_GETSTATUS:
163 ret = put_user(0, (int *)arg) ? -EFAULT : 0;
166 case WDIOC_GETBOOTSTATUS:
167 ret = put_user(boot_status, (int *)arg) ? -EFAULT : 0;
170 case WDIOC_KEEPALIVE:
171 ar71xx_wdt_keepalive();
175 case WDIOC_SETTIMEOUT:
176 ret = get_user(t, (int *)arg) ? -EFAULT : 0;
180 ret = ar71xx_wdt_set_timeout(t);
185 case WDIOC_GETTIMEOUT:
186 ret = put_user(wdt_timeout, (int *)arg) ? -EFAULT : 0;
197 static const struct file_operations ar71xx_wdt_fops = {
198 .owner = THIS_MODULE,
199 .write = ar71xx_wdt_write,
200 .ioctl = ar71xx_wdt_ioctl,
201 .open = ar71xx_wdt_open,
202 .release = ar71xx_wdt_release,
205 static struct miscdevice ar71xx_wdt_miscdev = {
206 .minor = WATCHDOG_MINOR,
208 .fops = &ar71xx_wdt_fops,
211 static int __devinit ar71xx_wdt_probe(struct platform_device *pdev)
215 max_timeout = (0xfffffffful / ar71xx_ahb_freq);
216 wdt_timeout = (max_timeout < WDT_TIMEOUT) ? max_timeout : WDT_TIMEOUT;
219 (ar71xx_reset_rr(AR71XX_RESET_REG_WDOG_CTRL) & WDOG_CTRL_LAST_RESET) ?
222 ret = misc_register(&ar71xx_wdt_miscdev);
226 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
228 printk(KERN_DEBUG DRV_NAME ": timeout=%d secs (max=%d)\n",
229 wdt_timeout, max_timeout);
237 static int __devexit ar71xx_wdt_remove(struct platform_device *pdev)
239 misc_deregister(&ar71xx_wdt_miscdev);
243 static struct platform_driver ar71xx_wdt_driver = {
244 .probe = ar71xx_wdt_probe,
245 .remove = __devexit_p(ar71xx_wdt_remove),
248 .owner = THIS_MODULE,
252 static int __init ar71xx_wdt_init(void)
254 return platform_driver_register(&ar71xx_wdt_driver);
256 module_init(ar71xx_wdt_init);
258 static void __exit ar71xx_wdt_exit(void)
260 platform_driver_unregister(&ar71xx_wdt_driver);
262 module_exit(ar71xx_wdt_exit);
264 MODULE_DESCRIPTION(DRV_DESC);
265 MODULE_VERSION(DRV_VERSION);
266 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
267 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
268 MODULE_LICENSE("GPL v2");
269 MODULE_ALIAS("platform:" DRV_NAME);
270 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);