2 * linux/drivers/char/ar7_wdt.c
4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
35 #include <linux/ioport.h>
37 #include <asm/addrspace.h>
39 #include <asm/uaccess.h>
41 #include <asm/ar7/ar7.h>
43 #define DRVNAME "ar7_wdt"
44 #define LONGNAME "TI AR7 Watchdog Timer"
46 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
47 MODULE_DESCRIPTION(LONGNAME
);
48 MODULE_LICENSE("GPL");
49 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
51 static int margin
= 60;
52 module_param(margin
, int, 0);
53 MODULE_PARM_DESC(margin
, "Watchdog margin in seconds");
55 static int nowayout
= WATCHDOG_NOWAYOUT
;
56 module_param(nowayout
, int, 0);
57 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
70 static struct semaphore open_semaphore
;
71 static unsigned expect_close
;
73 /* XXX currently fixed, allows max margin ~68.72 secs */
74 #define prescale_value 0xFFFF
76 static void ar7_wdt_kick(u32 value
)
78 volatile ar7_wdt_t
*ar7_wdt
= (ar7_wdt_t
*)ioremap(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
80 ar7_wdt
->kick_lock
= 0x5555;
81 if ((ar7_wdt
->kick_lock
& 3) == 1) {
82 ar7_wdt
->kick_lock
= 0xAAAA;
83 if ((ar7_wdt
->kick_lock
& 3) == 3) {
84 ar7_wdt
->kick
= value
;
88 printk(KERN_ERR DRVNAME
": failed to unlock WDT kick reg\n");
91 static void ar7_wdt_prescale(u32 value
)
93 volatile ar7_wdt_t
*ar7_wdt
= (ar7_wdt_t
*)ioremap(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
95 ar7_wdt
->prescale_lock
= 0x5A5A;
96 if ((ar7_wdt
->prescale_lock
& 3) == 1) {
97 ar7_wdt
->prescale_lock
= 0xA5A5;
98 if ((ar7_wdt
->prescale_lock
& 3) == 3) {
99 ar7_wdt
->prescale
= value
;
103 printk(KERN_ERR DRVNAME
": failed to unlock WDT prescale reg\n");
106 static void ar7_wdt_change(u32 value
)
108 volatile ar7_wdt_t
*ar7_wdt
= (ar7_wdt_t
*)ioremap(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
110 ar7_wdt
->change_lock
= 0x6666;
111 if ((ar7_wdt
->change_lock
& 3) == 1) {
112 ar7_wdt
->change_lock
= 0xBBBB;
113 if ((ar7_wdt
->change_lock
& 3) == 3) {
114 ar7_wdt
->change
= value
;
118 printk(KERN_ERR DRVNAME
": failed to unlock WDT change reg\n");
121 static void ar7_wdt_disable(u32 value
)
123 volatile ar7_wdt_t
*ar7_wdt
= (ar7_wdt_t
*)ioremap(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
125 ar7_wdt
->disable_lock
= 0x7777;
126 if ((ar7_wdt
->disable_lock
& 3) == 1) {
127 ar7_wdt
->disable_lock
= 0xCCCC;
128 if ((ar7_wdt
->disable_lock
& 3) == 2) {
129 ar7_wdt
->disable_lock
= 0xDDDD;
130 if ((ar7_wdt
->disable_lock
& 3) == 3) {
131 ar7_wdt
->disable
= value
;
136 printk(KERN_ERR DRVNAME
": failed to unlock WDT disable reg\n");
139 static void ar7_wdt_update_margin(int new_margin
)
143 change
= new_margin
* (ar7_vbus_freq() / prescale_value
);
144 if (change
< 1) change
= 1;
145 if (change
> 0xFFFF) change
= 0xFFFF;
146 ar7_wdt_change(change
);
147 margin
= change
* prescale_value
/ ar7_vbus_freq();
148 printk(KERN_INFO DRVNAME
149 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
150 margin
, prescale_value
, change
, ar7_vbus_freq());
153 static void ar7_wdt_enable_wdt(void)
155 printk(KERN_DEBUG DRVNAME
": enabling watchdog timer\n");
160 static void ar7_wdt_disable_wdt(void)
162 printk(KERN_DEBUG DRVNAME
": disabling watchdog timer\n");
166 static int ar7_wdt_open(struct inode
*inode
, struct file
*file
)
168 /* only allow one at a time */
169 if (down_trylock(&open_semaphore
))
171 ar7_wdt_enable_wdt();
177 static int ar7_wdt_release(struct inode
*inode
, struct file
*file
)
180 printk(KERN_WARNING DRVNAME
": watchdog device closed unexpectedly, will not disable the watchdog timer\n");
181 } else if (!nowayout
) {
182 ar7_wdt_disable_wdt();
189 static int ar7_wdt_notify_sys(struct notifier_block
*this,
190 unsigned long code
, void *unused
)
192 if (code
== SYS_HALT
|| code
== SYS_POWER_OFF
)
194 ar7_wdt_disable_wdt();
199 static struct notifier_block ar7_wdt_notifier
=
201 .notifier_call
= ar7_wdt_notify_sys
204 static ssize_t
ar7_wdt_write(struct file
*file
, const char *data
,
205 size_t len
, loff_t
*ppos
)
207 if (ppos
!= &file
->f_pos
)
210 /* check for a magic close character */
217 for (i
= 0; i
< len
; ++i
) {
219 if (get_user(c
, data
+i
))
229 static int ar7_wdt_ioctl(struct inode
*inode
, struct file
*file
,
230 unsigned int cmd
, unsigned long arg
)
232 static struct watchdog_info ident
= {
233 .identity
= LONGNAME
,
234 .firmware_version
= 1,
235 .options
= (WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
),
242 case WDIOC_GETSUPPORT
:
243 if(copy_to_user((struct watchdog_info
*)arg
, &ident
,
247 case WDIOC_GETSTATUS
:
248 case WDIOC_GETBOOTSTATUS
:
249 if (put_user(0, (int *)arg
))
252 case WDIOC_KEEPALIVE
:
255 case WDIOC_SETTIMEOUT
:
256 if (get_user(new_margin
, (int *)arg
))
261 ar7_wdt_update_margin(new_margin
);
264 case WDIOC_GETTIMEOUT
:
265 if (put_user(margin
, (int *)arg
))
271 static struct file_operations ar7_wdt_fops
= {
272 .owner
= THIS_MODULE
,
273 .write
= ar7_wdt_write
,
274 .ioctl
= ar7_wdt_ioctl
,
275 .open
= ar7_wdt_open
,
276 .release
= ar7_wdt_release
,
279 static struct miscdevice ar7_wdt_miscdev
= {
280 .minor
= WATCHDOG_MINOR
,
282 .fops
= &ar7_wdt_fops
,
285 static int __init
ar7_wdt_init(void)
289 if (!request_mem_region(AR7_REGS_WDT
, sizeof(ar7_wdt_t
), LONGNAME
)) {
290 printk(KERN_WARNING DRVNAME
": watchdog I/O region busy\n");
294 ar7_wdt_disable_wdt();
295 ar7_wdt_prescale(prescale_value
);
296 ar7_wdt_update_margin(margin
);
298 sema_init(&open_semaphore
, 1);
300 rc
= misc_register(&ar7_wdt_miscdev
);
302 printk(KERN_ERR DRVNAME
": unable to register misc device\n");
306 rc
= register_reboot_notifier(&ar7_wdt_notifier
);
308 printk(KERN_ERR DRVNAME
": unable to register reboot notifier\n");
314 misc_deregister(&ar7_wdt_miscdev
);
316 release_mem_region(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
321 static void __exit
ar7_wdt_cleanup(void)
323 unregister_reboot_notifier(&ar7_wdt_notifier
);
324 misc_deregister(&ar7_wdt_miscdev
);
325 release_mem_region(AR7_REGS_WDT
, sizeof(ar7_wdt_t
));
328 module_init(ar7_wdt_init
);
329 module_exit(ar7_wdt_cleanup
);