2 * drivers/watchdog/ar7_wdt.c
4 * Copyright (C) 2007 Nicolas Thill <nico@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/mach-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");
58 #define READ_REG(x) readl((void __iomem *)&(x))
59 #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
72 static unsigned long wdt_is_open
;
73 static spinlock_t wdt_lock
;
74 static unsigned expect_close
;
76 /* XXX currently fixed, allows max margin ~68.72 secs */
77 #define prescale_value 0xffff
79 /* Offset of the WDT registers */
80 static unsigned long ar7_regs_wdt
;
81 /* Pointer to the remapped WDT IO space */
82 static struct ar7_wdt
*ar7_wdt
;
83 static void ar7_wdt_get_regs(void)
85 u16 chip_id
= ar7_chip_id();
89 ar7_regs_wdt
= AR7_REGS_WDT
;
92 ar7_regs_wdt
= UR8_REGS_WDT
;
98 static void ar7_wdt_kick(u32 value
)
100 WRITE_REG(ar7_wdt
->kick_lock
, 0x5555);
101 if ((READ_REG(ar7_wdt
->kick_lock
) & 3) == 1) {
102 WRITE_REG(ar7_wdt
->kick_lock
, 0xaaaa);
103 if ((READ_REG(ar7_wdt
->kick_lock
) & 3) == 3) {
104 WRITE_REG(ar7_wdt
->kick
, value
);
108 printk(KERN_ERR DRVNAME
": failed to unlock WDT kick reg\n");
111 static void ar7_wdt_prescale(u32 value
)
113 WRITE_REG(ar7_wdt
->prescale_lock
, 0x5a5a);
114 if ((READ_REG(ar7_wdt
->prescale_lock
) & 3) == 1) {
115 WRITE_REG(ar7_wdt
->prescale_lock
, 0xa5a5);
116 if ((READ_REG(ar7_wdt
->prescale_lock
) & 3) == 3) {
117 WRITE_REG(ar7_wdt
->prescale
, value
);
121 printk(KERN_ERR DRVNAME
": failed to unlock WDT prescale reg\n");
124 static void ar7_wdt_change(u32 value
)
126 WRITE_REG(ar7_wdt
->change_lock
, 0x6666);
127 if ((READ_REG(ar7_wdt
->change_lock
) & 3) == 1) {
128 WRITE_REG(ar7_wdt
->change_lock
, 0xbbbb);
129 if ((READ_REG(ar7_wdt
->change_lock
) & 3) == 3) {
130 WRITE_REG(ar7_wdt
->change
, value
);
134 printk(KERN_ERR DRVNAME
": failed to unlock WDT change reg\n");
137 static void ar7_wdt_disable(u32 value
)
139 WRITE_REG(ar7_wdt
->disable_lock
, 0x7777);
140 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 1) {
141 WRITE_REG(ar7_wdt
->disable_lock
, 0xcccc);
142 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 2) {
143 WRITE_REG(ar7_wdt
->disable_lock
, 0xdddd);
144 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 3) {
145 WRITE_REG(ar7_wdt
->disable
, value
);
150 printk(KERN_ERR DRVNAME
": failed to unlock WDT disable reg\n");
153 static void ar7_wdt_update_margin(int new_margin
)
157 change
= new_margin
* (ar7_vbus_freq() / prescale_value
);
162 ar7_wdt_change(change
);
163 margin
= change
* prescale_value
/ ar7_vbus_freq();
164 printk(KERN_INFO DRVNAME
165 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
166 margin
, prescale_value
, change
, ar7_vbus_freq());
169 static void ar7_wdt_enable_wdt(void)
171 printk(KERN_DEBUG DRVNAME
": enabling watchdog timer\n");
176 static void ar7_wdt_disable_wdt(void)
178 printk(KERN_DEBUG DRVNAME
": disabling watchdog timer\n");
182 static int ar7_wdt_open(struct inode
*inode
, struct file
*file
)
184 /* only allow one at a time */
185 if (test_and_set_bit(0, &wdt_is_open
))
187 ar7_wdt_enable_wdt();
190 return nonseekable_open(inode
, file
);
193 static int ar7_wdt_release(struct inode
*inode
, struct file
*file
)
196 printk(KERN_WARNING DRVNAME
197 ": watchdog device closed unexpectedly,"
198 "will not disable the watchdog timer\n");
200 ar7_wdt_disable_wdt();
201 clear_bit(0, &wdt_is_open
);
205 static int ar7_wdt_notify_sys(struct notifier_block
*this,
206 unsigned long code
, void *unused
)
208 if (code
== SYS_HALT
|| code
== SYS_POWER_OFF
)
210 ar7_wdt_disable_wdt();
215 static struct notifier_block ar7_wdt_notifier
= {
216 .notifier_call
= ar7_wdt_notify_sys
,
219 static ssize_t
ar7_wdt_write(struct file
*file
, const char *data
,
220 size_t len
, loff_t
*ppos
)
222 /* check for a magic close character */
226 spin_lock(&wdt_lock
);
228 spin_unlock(&wdt_lock
);
231 for (i
= 0; i
< len
; ++i
) {
233 if (get_user(c
, data
+ i
))
243 static long ar7_wdt_ioctl(struct file
*file
,
244 unsigned int cmd
, unsigned long arg
)
246 static struct watchdog_info ident
= {
247 .identity
= LONGNAME
,
248 .firmware_version
= 1,
249 .options
= (WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
|
255 case WDIOC_GETSUPPORT
:
256 if (copy_to_user((struct watchdog_info
*)arg
, &ident
,
260 case WDIOC_GETSTATUS
:
261 case WDIOC_GETBOOTSTATUS
:
262 if (put_user(0, (int *)arg
))
265 case WDIOC_KEEPALIVE
:
268 case WDIOC_SETTIMEOUT
:
269 if (get_user(new_margin
, (int *)arg
))
274 spin_lock(&wdt_lock
);
275 ar7_wdt_update_margin(new_margin
);
277 spin_unlock(&wdt_lock
);
279 case WDIOC_GETTIMEOUT
:
280 if (put_user(margin
, (int *)arg
))
288 static const struct file_operations ar7_wdt_fops
= {
289 .owner
= THIS_MODULE
,
290 .write
= ar7_wdt_write
,
291 .unlocked_ioctl
= ar7_wdt_ioctl
,
292 .open
= ar7_wdt_open
,
293 .release
= ar7_wdt_release
,
296 static struct miscdevice ar7_wdt_miscdev
= {
297 .minor
= WATCHDOG_MINOR
,
299 .fops
= &ar7_wdt_fops
,
302 static int __init
ar7_wdt_init(void)
306 spin_lock_init(&wdt_lock
);
310 if (!request_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
),
312 printk(KERN_WARNING DRVNAME
": watchdog I/O region busy\n");
316 ar7_wdt
= (struct ar7_wdt
*)
317 ioremap(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
319 ar7_wdt_disable_wdt();
320 ar7_wdt_prescale(prescale_value
);
321 ar7_wdt_update_margin(margin
);
323 rc
= register_reboot_notifier(&ar7_wdt_notifier
);
325 printk(KERN_ERR DRVNAME
326 ": unable to register reboot notifier\n");
330 rc
= misc_register(&ar7_wdt_miscdev
);
332 printk(KERN_ERR DRVNAME
": unable to register misc device\n");
338 unregister_reboot_notifier(&ar7_wdt_notifier
);
341 release_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
346 static void __exit
ar7_wdt_cleanup(void)
348 misc_deregister(&ar7_wdt_miscdev
);
349 unregister_reboot_notifier(&ar7_wdt_notifier
);
351 release_mem_region(ar7_regs_wdt
, sizeof(struct ar7_wdt
));
354 module_init(ar7_wdt_init
);
355 module_exit(ar7_wdt_cleanup
);