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 <linux/uaccess.h>
39 #include <asm/addrspace.h>
40 #include <asm/ar7/ar7.h>
42 #define DRVNAME "ar7_wdt"
43 #define LONGNAME "TI AR7 Watchdog Timer"
45 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
46 MODULE_DESCRIPTION(LONGNAME
);
47 MODULE_LICENSE("GPL");
48 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
50 static int margin
= 60;
51 module_param(margin
, int, 0);
52 MODULE_PARM_DESC(margin
, "Watchdog margin in seconds");
54 static int nowayout
= WATCHDOG_NOWAYOUT
;
55 module_param(nowayout
, int, 0);
56 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
69 static struct semaphore open_semaphore
;
70 static unsigned expect_close
;
72 /* XXX currently fixed, allows max margin ~68.72 secs */
73 #define prescale_value 0xffff
75 /* Offset of the WDT registers */
76 static unsigned long ar7_regs_wdt
;
77 /* Pointer to the remapped WDT IO space */
78 static struct ar7_wdt
*ar7_wdt
;
79 static void ar7_wdt_get_regs(void)
81 u16 chip_id
= ar7_chip_id();
85 ar7_regs_wdt
= AR7_REGS_WDT
;
88 ar7_regs_wdt
= UR8_REGS_WDT
;
94 static void ar7_wdt_kick(u32 value
)
96 ar7_wdt
->kick_lock
= 0x5555;
97 if ((ar7_wdt
->kick_lock
& 3) == 1) {
98 ar7_wdt
->kick_lock
= 0xaaaa;
99 if ((ar7_wdt
->kick_lock
& 3) == 3) {
100 ar7_wdt
->kick
= value
;
104 printk(KERN_ERR DRVNAME
": failed to unlock WDT kick reg\n");
107 static void ar7_wdt_prescale(u32 value
)
109 ar7_wdt
->prescale_lock
= 0x5a5a;
110 if ((ar7_wdt
->prescale_lock
& 3) == 1) {
111 ar7_wdt
->prescale_lock
= 0xa5a5;
112 if ((ar7_wdt
->prescale_lock
& 3) == 3) {
113 ar7_wdt
->prescale
= value
;
117 printk(KERN_ERR DRVNAME
": failed to unlock WDT prescale reg\n");
120 static void ar7_wdt_change(u32 value
)
122 ar7_wdt
->change_lock
= 0x6666;
123 if ((ar7_wdt
->change_lock
& 3) == 1) {
124 ar7_wdt
->change_lock
= 0xbbbb;
125 if ((ar7_wdt
->change_lock
& 3) == 3) {
126 ar7_wdt
->change
= value
;
130 printk(KERN_ERR DRVNAME
": failed to unlock WDT change reg\n");
133 static void ar7_wdt_disable(u32 value
)
135 ar7_wdt
->disable_lock
= 0x7777;
136 if ((ar7_wdt
->disable_lock
& 3) == 1) {
137 ar7_wdt
->disable_lock
= 0xcccc;
138 if ((ar7_wdt
->disable_lock
& 3) == 2) {
139 ar7_wdt
->disable_lock
= 0xdddd;
140 if ((ar7_wdt
->disable_lock
& 3) == 3) {
141 ar7_wdt
->disable
= value
;
146 printk(KERN_ERR DRVNAME
": failed to unlock WDT disable reg\n");
149 static void ar7_wdt_update_margin(int new_margin
)
153 change
= new_margin
* (ar7_vbus_freq() / prescale_value
);
154 if (change
< 1) change
= 1;
155 if (change
> 0xffff) change
= 0xffff;
156 ar7_wdt_change(change
);
157 margin
= change
* prescale_value
/ ar7_vbus_freq();
158 printk(KERN_INFO DRVNAME
159 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
160 margin
, prescale_value
, change
, ar7_vbus_freq());
163 static void ar7_wdt_enable_wdt(void)
165 printk(KERN_DEBUG DRVNAME
": enabling watchdog timer\n");
170 static void ar7_wdt_disable_wdt(void)
172 printk(KERN_DEBUG DRVNAME
": disabling watchdog timer\n");
176 static int ar7_wdt_open(struct inode
*inode
, struct file
*file
)
178 /* only allow one at a time */
179 if (down_trylock(&open_semaphore
))
181 ar7_wdt_enable_wdt();
187 static int ar7_wdt_release(struct inode
*inode
, struct file
*file
)
190 printk(KERN_WARNING DRVNAME
191 ": watchdog device closed unexpectedly,"
192 "will not disable the watchdog timer\n");
193 } else if (!nowayout
) {
194 ar7_wdt_disable_wdt();
201 static int ar7_wdt_notify_sys(struct notifier_block
*this,
202 unsigned long code
, void *unused
)
204 if (code
== SYS_HALT
|| code
== SYS_POWER_OFF
)
206 ar7_wdt_disable_wdt();
211 static struct notifier_block ar7_wdt_notifier
= {
212 .notifier_call
= ar7_wdt_notify_sys
215 static ssize_t
ar7_wdt_write(struct file
*file
, const char *data
,
216 size_t len
, loff_t
*ppos
)
218 if (ppos
!= &file
->f_pos
)
221 /* check for a magic close character */
228 for (i
= 0; i
< len
; ++i
) {
230 if (get_user(c
, data
+i
))
240 static int ar7_wdt_ioctl(struct inode
*inode
, struct file
*file
,
241 unsigned int cmd
, unsigned long arg
)
243 static struct watchdog_info ident
= {
244 .identity
= LONGNAME
,
245 .firmware_version
= 1,
246 .options
= (WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
),
253 case WDIOC_GETSUPPORT
:
254 if (copy_to_user((struct watchdog_info
*)arg
, &ident
,
258 case WDIOC_GETSTATUS
:
259 case WDIOC_GETBOOTSTATUS
:
260 if (put_user(0, (int *)arg
))
263 case WDIOC_KEEPALIVE
:
266 case WDIOC_SETTIMEOUT
:
267 if (get_user(new_margin
, (int *)arg
))
272 ar7_wdt_update_margin(new_margin
);
275 case WDIOC_GETTIMEOUT
:
276 if (put_user(margin
, (int *)arg
))
282 static struct file_operations ar7_wdt_fops
= {
283 .owner
= THIS_MODULE
,
284 .write
= ar7_wdt_write
,
285 .ioctl
= ar7_wdt_ioctl
,
286 .open
= ar7_wdt_open
,
287 .release
= ar7_wdt_release
,
290 static struct miscdevice ar7_wdt_miscdev
= {
291 .minor
= WATCHDOG_MINOR
,
293 .fops
= &ar7_wdt_fops
,
296 static int __init
ar7_wdt_init(void)
302 if (!request_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
),
304 printk(KERN_WARNING DRVNAME
": watchdog I/O region busy\n");
308 ar7_wdt
= (struct ar7_wdt
*)
309 ioremap(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
311 ar7_wdt_disable_wdt();
312 ar7_wdt_prescale(prescale_value
);
313 ar7_wdt_update_margin(margin
);
315 sema_init(&open_semaphore
, 1);
317 rc
= misc_register(&ar7_wdt_miscdev
);
319 printk(KERN_ERR DRVNAME
": unable to register misc device\n");
323 rc
= register_reboot_notifier(&ar7_wdt_notifier
);
325 printk(KERN_ERR DRVNAME
326 ": unable to register reboot notifier\n");
332 misc_deregister(&ar7_wdt_miscdev
);
334 release_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
339 static void __exit
ar7_wdt_cleanup(void)
341 unregister_reboot_notifier(&ar7_wdt_notifier
);
342 misc_deregister(&ar7_wdt_miscdev
);
344 release_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
347 module_init(ar7_wdt_init
);
348 module_exit(ar7_wdt_cleanup
);