2 * Driver for the Atheros AR71xx SoC's built-in hardware watchdog timer.
4 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
5 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
8 * Parts of this file are based on Atheros 2.6.31 BSP
10 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
11 * Author: Deepak Saxena <dsaxena@plexity.net>
12 * Copyright 2004 (c) MontaVista, Software, Inc.
14 * which again was based on sa1100 driver,
15 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License version 2 as published
19 * by the Free Software Foundation.
23 #include <linux/bitops.h>
24 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/miscdevice.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/platform_device.h>
32 #include <linux/types.h>
33 #include <linux/watchdog.h>
34 #include <linux/delay.h>
36 #include <asm/mach-ar71xx/ar71xx.h>
38 #define DRV_NAME "ar71xx-wdt"
39 #define DRV_DESC "Atheros AR71xx hardware watchdog driver"
40 #define DRV_VERSION "0.1.0"
42 #define WDT_TIMEOUT 15 /* seconds */
44 static int nowayout
= WATCHDOG_NOWAYOUT
;
46 #ifdef CONFIG_WATCHDOG_NOWAYOUT
47 module_param(nowayout
, int, 0);
48 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
49 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
52 static unsigned long wdt_flags
;
54 #define WDT_FLAGS_BUSY 0
55 #define WDT_FLAGS_EXPECT_CLOSE 1
57 static int wdt_timeout
= WDT_TIMEOUT
;
58 static int boot_status
;
59 static int max_timeout
;
60 static u32 wdt_clk_freq
;
62 static inline void ar71xx_wdt_keepalive(void)
64 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG
, wdt_clk_freq
* wdt_timeout
);
67 static inline void ar71xx_wdt_enable(void)
69 printk(KERN_DEBUG DRV_NAME
": enabling watchdog timer\n");
70 ar71xx_wdt_keepalive();
72 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL
, WDOG_CTRL_ACTION_FCR
);
75 static inline void ar71xx_wdt_disable(void)
77 printk(KERN_DEBUG DRV_NAME
": disabling watchdog timer\n");
78 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL
, WDOG_CTRL_ACTION_NONE
);
81 static int ar71xx_wdt_set_timeout(int val
)
83 if (val
< 1 || val
> max_timeout
)
87 ar71xx_wdt_keepalive();
89 printk(KERN_DEBUG DRV_NAME
": timeout=%d secs\n", wdt_timeout
);
94 static int ar71xx_wdt_open(struct inode
*inode
, struct file
*file
)
96 if (test_and_set_bit(WDT_FLAGS_BUSY
, &wdt_flags
))
99 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
103 return nonseekable_open(inode
, file
);
106 static int ar71xx_wdt_release(struct inode
*inode
, struct file
*file
)
108 if (test_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
)) {
109 ar71xx_wdt_disable();
111 printk(KERN_CRIT DRV_NAME
": device closed unexpectedly, "
112 "watchdog timer will not stop!\n");
115 clear_bit(WDT_FLAGS_BUSY
, &wdt_flags
);
116 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
121 static ssize_t
ar71xx_wdt_write(struct file
*file
, const char *data
,
122 size_t len
, loff_t
*ppos
)
128 clear_bit(WDT_FLAGS_EXPECT_CLOSE
, &wdt_flags
);
130 for (i
= 0; i
!= len
; i
++) {
133 if (get_user(c
, data
+ i
))
137 set_bit(WDT_FLAGS_EXPECT_CLOSE
,
142 ar71xx_wdt_keepalive();
148 static struct watchdog_info ar71xx_wdt_info
= {
149 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
|
150 WDIOF_MAGICCLOSE
| WDIOF_CARDRESET
,
151 .firmware_version
= 0,
152 .identity
= "AR71XX watchdog",
155 static long ar71xx_wdt_ioctl(struct file
*file
,
156 unsigned int cmd
, unsigned long arg
)
162 case WDIOC_GETSUPPORT
:
163 ret
= copy_to_user((struct watchdog_info
*)arg
,
165 sizeof(ar71xx_wdt_info
)) ? -EFAULT
: 0;
168 case WDIOC_GETSTATUS
:
169 ret
= put_user(0, (int *)arg
) ? -EFAULT
: 0;
172 case WDIOC_GETBOOTSTATUS
:
173 ret
= put_user(boot_status
, (int *)arg
) ? -EFAULT
: 0;
176 case WDIOC_KEEPALIVE
:
177 ar71xx_wdt_keepalive();
181 case WDIOC_SETTIMEOUT
:
182 ret
= get_user(t
, (int *)arg
) ? -EFAULT
: 0;
186 ret
= ar71xx_wdt_set_timeout(t
);
191 case WDIOC_GETTIMEOUT
:
192 ret
= put_user(wdt_timeout
, (int *)arg
) ? -EFAULT
: 0;
203 static const struct file_operations ar71xx_wdt_fops
= {
204 .owner
= THIS_MODULE
,
205 .write
= ar71xx_wdt_write
,
206 .unlocked_ioctl
= ar71xx_wdt_ioctl
,
207 .open
= ar71xx_wdt_open
,
208 .release
= ar71xx_wdt_release
,
211 static struct miscdevice ar71xx_wdt_miscdev
= {
212 .minor
= WATCHDOG_MINOR
,
214 .fops
= &ar71xx_wdt_fops
,
217 static int __devinit
ar71xx_wdt_probe(struct platform_device
*pdev
)
221 switch (ar71xx_soc
) {
222 case AR71XX_SOC_AR9341
:
223 case AR71XX_SOC_AR9342
:
224 case AR71XX_SOC_AR9344
:
225 wdt_clk_freq
= ar71xx_ref_freq
;
229 wdt_clk_freq
= ar71xx_ahb_freq
;
233 max_timeout
= (0xfffffffful
/ wdt_clk_freq
);
234 wdt_timeout
= (max_timeout
< WDT_TIMEOUT
) ? max_timeout
: WDT_TIMEOUT
;
236 if (ar71xx_reset_rr(AR71XX_RESET_REG_WDOG_CTRL
) & WDOG_CTRL_LAST_RESET
)
237 boot_status
= WDIOF_CARDRESET
;
239 ret
= misc_register(&ar71xx_wdt_miscdev
);
243 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
245 printk(KERN_DEBUG DRV_NAME
": timeout=%d secs (max=%d)\n",
246 wdt_timeout
, max_timeout
);
254 static int __devexit
ar71xx_wdt_remove(struct platform_device
*pdev
)
256 misc_deregister(&ar71xx_wdt_miscdev
);
260 static struct platform_driver ar71xx_wdt_driver
= {
261 .probe
= ar71xx_wdt_probe
,
262 .remove
= __devexit_p(ar71xx_wdt_remove
),
265 .owner
= THIS_MODULE
,
269 static int __init
ar71xx_wdt_init(void)
271 return platform_driver_register(&ar71xx_wdt_driver
);
273 module_init(ar71xx_wdt_init
);
275 static void __exit
ar71xx_wdt_exit(void)
277 platform_driver_unregister(&ar71xx_wdt_driver
);
279 module_exit(ar71xx_wdt_exit
);
281 MODULE_DESCRIPTION(DRV_DESC
);
282 MODULE_VERSION(DRV_VERSION
);
283 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
284 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
285 MODULE_LICENSE("GPL v2");
286 MODULE_ALIAS("platform:" DRV_NAME
);
287 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);