2 * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
4 * Copyright (C) 2008-2011 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>
31 #include <linux/clk.h>
32 #include <linux/err.h>
34 #include <asm/mach-ath79/ath79.h>
35 #include <asm/mach-ath79/ar71xx_regs.h>
37 #define DRIVER_NAME "ath79-wdt"
39 #define WDT_TIMEOUT 15 /* seconds */
41 #define WDOG_CTRL_LAST_RESET BIT(31)
42 #define WDOG_CTRL_ACTION_MASK 3
43 #define WDOG_CTRL_ACTION_NONE 0 /* no action */
44 #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
45 #define WDOG_CTRL_ACTION_NMI 2 /* NMI */
46 #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
48 static int nowayout
= WATCHDOG_NOWAYOUT
;
49 module_param(nowayout
, int, 0);
50 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
51 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
53 static int timeout
= WDT_TIMEOUT
;
54 module_param(timeout
, int, 0);
55 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds "
56 "(default=" __MODULE_STRING(WDT_TIMEOUT
) "s)");
58 static unsigned long wdt_flags
;
60 #define WDT_FLAGS_BUSY 0
61 #define WDT_FLAGS_EXPECT_CLOSE 1
63 static struct clk
*wdt_clk
;
64 static unsigned long wdt_freq
;
65 static int boot_status
;
66 static int max_timeout
;
68 static inline void ath79_wdt_keepalive(void)
70 ath79_reset_wr(AR71XX_RESET_REG_WDOG
, wdt_freq
* timeout
);
72 ath79_reset_rr(AR71XX_RESET_REG_WDOG
);
75 static inline void ath79_wdt_enable(void)
77 ath79_wdt_keepalive();
78 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL
, WDOG_CTRL_ACTION_FCR
);
80 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL
);
83 static inline void ath79_wdt_disable(void)
85 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL
, WDOG_CTRL_ACTION_NONE
);
87 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL
);
90 static int ath79_wdt_set_timeout(int val
)
92 if (val
< 1 || val
> max_timeout
)
96 ath79_wdt_keepalive();
101 static int ath79_wdt_open(struct inode
*inode
, struct file
*file
)
103 if (test_and_set_bit(WDT_FLAGS_BUSY
, &wdt_flags
))
106 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
109 return nonseekable_open(inode
, file
);
112 static int ath79_wdt_release(struct inode
*inode
, struct file
*file
)
114 if (test_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
))
117 pr_crit(DRIVER_NAME
": device closed unexpectedly, "
118 "watchdog timer will not stop!\n");
119 ath79_wdt_keepalive();
122 clear_bit(WDT_FLAGS_BUSY
, &wdt_flags
);
123 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
128 static ssize_t
ath79_wdt_write(struct file
*file
, const char *data
,
129 size_t len
, loff_t
*ppos
)
135 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
137 for (i
= 0; i
!= len
; i
++) {
140 if (get_user(c
, data
+ i
))
144 set_bit(WDT_FLAGS_EXPECT_CLOSE
,
149 ath79_wdt_keepalive();
155 static const struct watchdog_info ath79_wdt_info
= {
156 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
|
157 WDIOF_MAGICCLOSE
| WDIOF_CARDRESET
,
158 .firmware_version
= 0,
159 .identity
= "ATH79 watchdog",
162 static long ath79_wdt_ioctl(struct file
*file
, unsigned int cmd
,
165 void __user
*argp
= (void __user
*)arg
;
166 int __user
*p
= argp
;
171 case WDIOC_GETSUPPORT
:
172 err
= copy_to_user(argp
, &ath79_wdt_info
,
173 sizeof(ath79_wdt_info
)) ? -EFAULT
: 0;
176 case WDIOC_GETSTATUS
:
177 err
= put_user(0, p
);
180 case WDIOC_GETBOOTSTATUS
:
181 err
= put_user(boot_status
, p
);
184 case WDIOC_KEEPALIVE
:
185 ath79_wdt_keepalive();
189 case WDIOC_SETTIMEOUT
:
190 err
= get_user(t
, p
);
194 err
= ath79_wdt_set_timeout(t
);
199 case WDIOC_GETTIMEOUT
:
200 err
= put_user(timeout
, p
);
211 static const struct file_operations ath79_wdt_fops
= {
212 .owner
= THIS_MODULE
,
214 .write
= ath79_wdt_write
,
215 .unlocked_ioctl
= ath79_wdt_ioctl
,
216 .open
= ath79_wdt_open
,
217 .release
= ath79_wdt_release
,
220 static struct miscdevice ath79_wdt_miscdev
= {
221 .minor
= WATCHDOG_MINOR
,
223 .fops
= &ath79_wdt_fops
,
226 static int __devinit
ath79_wdt_probe(struct platform_device
*pdev
)
231 wdt_clk
= clk_get(&pdev
->dev
, "wdt");
233 return PTR_ERR(wdt_clk
);
235 err
= clk_enable(wdt_clk
);
239 wdt_freq
= clk_get_rate(wdt_clk
);
242 goto err_clk_disable
;
245 max_timeout
= (0xfffffffful
/ wdt_freq
);
246 if (timeout
< 1 || timeout
> max_timeout
) {
247 timeout
= max_timeout
;
249 "timeout value must be 0 < timeout < %d, using %d\n",
250 max_timeout
, timeout
);
253 ctrl
= ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL
);
254 boot_status
= (ctrl
& WDOG_CTRL_LAST_RESET
) ? WDIOF_CARDRESET
: 0;
256 err
= misc_register(&ath79_wdt_miscdev
);
259 "unable to register misc device, err=%d\n", err
);
260 goto err_clk_disable
;
266 clk_disable(wdt_clk
);
272 static int __devexit
ath79_wdt_remove(struct platform_device
*pdev
)
274 misc_deregister(&ath79_wdt_miscdev
);
275 clk_disable(wdt_clk
);
280 static void ath97_wdt_shutdown(struct platform_device
*pdev
)
285 static struct platform_driver ath79_wdt_driver
= {
286 .remove
= __devexit_p(ath79_wdt_remove
),
287 .shutdown
= ath97_wdt_shutdown
,
290 .owner
= THIS_MODULE
,
294 static int __init
ath79_wdt_init(void)
296 return platform_driver_probe(&ath79_wdt_driver
, ath79_wdt_probe
);
298 module_init(ath79_wdt_init
);
300 static void __exit
ath79_wdt_exit(void)
302 platform_driver_unregister(&ath79_wdt_driver
);
304 module_exit(ath79_wdt_exit
);
306 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
307 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
308 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
309 MODULE_LICENSE("GPL v2");
310 MODULE_ALIAS("platform:" DRIVER_NAME
);
311 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);