2 * Watchdog driver for the BCM963xx devices
4 * Copyright (C) 2007 OpenWrt.org
5 * Florian Fainelli <florian@openwrt.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/miscdevice.h>
18 #include <linux/init.h>
19 #include <linux/notifier.h>
20 #include <linux/watchdog.h>
21 #include <linux/timer.h>
22 #include <linux/jiffies.h>
23 #include <linux/completion.h>
24 #include <linux/ioport.h>
26 typedef struct bcm963xx_timer
{
27 unsigned short unused0
;
28 unsigned char timer_mask
;
32 unsigned char timer_ints
;
37 unsigned long timer_ctl0
;
38 unsigned long timer_ctl1
;
39 unsigned long timer_ctl2
;
40 #define TIMERENABLE 0x80000000
41 #define RSTCNTCLR 0x40000000
42 unsigned long timer_cnt0
;
43 unsigned long timer_cnt1
;
44 unsigned long timer_cnt2
;
45 unsigned long wdt_def_count
;
47 /* Write 0xff00 0x00ff to Start timer
48 * Write 0xee00 0x00ee to Stop and re-load default count
49 * Read from this register returns current watch dog count
51 unsigned long wdt_ctl
;
53 /* Number of 40-MHz ticks for WD Reset pulse to last */
54 unsigned long wdt_rst_count
;
57 static struct bcm963xx_wdt_device
{
58 struct completion stop
;
60 struct timer_list timer
;
64 } bcm963xx_wdt_device
;
66 static int ticks
= 1000;
68 #define WDT_BASE 0xfffe0200
69 #define WDT ((volatile bcm963xx_timer * const) WDT_BASE)
71 #define BCM963XX_INTERVAL (HZ/10+1)
73 static void bcm963xx_wdt_trigger(unsigned long unused
)
75 if (bcm963xx_wdt_device
.running
)
78 /* Load the default ticking value into the reset counter register */
79 WDT
->wdt_rst_count
= bcm963xx_wdt_device
.default_ticks
;
81 if (bcm963xx_wdt_device
.queue
&& ticks
) {
82 bcm963xx_wdt_device
.timer
.expires
= jiffies
+ BCM963XX_INTERVAL
;
83 add_timer(&bcm963xx_wdt_device
.timer
);
86 complete(&bcm963xx_wdt_device
.stop
);
90 static void bcm963xx_wdt_reset(void)
92 ticks
= bcm963xx_wdt_device
.default_ticks
;
95 static void bcm963xx_wdt_start(void)
97 if (!bcm963xx_wdt_device
.queue
) {
98 bcm963xx_wdt_device
.queue
;
99 /* Enable the watchdog by writing 0xff00 ,then 0x00ff to the control register */
100 WDT
->wdt_ctl
= 0xff00;
101 WDT
->wdt_ctl
= 0x00ff;
102 bcm963xx_wdt_device
.timer
.expires
= jiffies
+ BCM963XX_INTERVAL
;
103 add_timer(&bcm963xx_wdt_device
.timer
);
105 bcm963xx_wdt_device
.running
++;
108 static int bcm963xx_wdt_stop(void)
110 if (bcm963xx_wdt_device
.running
)
111 bcm963xx_wdt_device
.running
= 0;
113 ticks
= bcm963xx_wdt_device
.default_ticks
;
115 /* Stop the watchdog by writing 0xee00 then 0x00ee to the control register */
116 WDT
->wdt_ctl
= 0xee00;
117 WDT
->wdt_ctl
= 0x00ee;
122 static int bcm963xx_wdt_open(struct inode
*inode
, struct file
*file
)
124 if (test_and_set_bit(0, &bcm963xx_wdt_device
.inuse
))
126 return nonseekable_open(inode
, file
);
129 static int bcm963xx_wdt_release(struct inode
*inode
, struct file
*file
)
131 clear_bit(0, &bcm963xx_wdt_device
.inuse
);
135 static int bcm963xx_wdt_ioctl(struct inode
*inode
, struct file
*file
,
136 unsigned int cmd
, unsigned long arg
)
138 void __user
*argp
= (void __user
*)arg
;
141 static struct watchdog_info ident
= {
142 .options
= WDIOF_CARDRESET
,
143 .identity
= "BCM963xx WDT",
147 case WDIOC_KEEPALIVE
:
148 bcm963xx_wdt_reset();
150 case WDIOC_GETSTATUS
:
151 /* Reading from the control register will return the current value */
152 value
= WDT
->wdt_ctl
;
153 if ( copy_to_user(argp
, &value
, sizeof(int)) )
156 case WDIOC_GETSUPPORT
:
157 if ( copy_to_user(argp
, &ident
, sizeof(ident
)) )
160 case WDIOC_SETOPTIONS
:
161 if ( copy_from_user(&value
, argp
, sizeof(int)) )
164 case WDIOS_ENABLECARD
:
165 bcm963xx_wdt_start();
167 case WDIOS_DISABLECARD
:
180 static int bcm963xx_wdt_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
184 bcm963xx_wdt_reset();
188 static const struct file_operations bcm963xx_wdt_fops
= {
189 .owner
= THIS_MODULE
,
191 .write
= bcm963xx_wdt_write
,
192 .ioctl
= bcm963xx_wdt_ioctl
,
193 .open
= bcm963xx_wdt_open
,
194 .release
= bcm963xx_wdt_release
,
197 static struct miscdevice bcm963xx_wdt_miscdev
= {
198 .minor
= WATCHDOG_MINOR
,
200 .fops
= &bcm963xx_wdt_fops
,
203 static void __exit
bcm963xx_wdt_exit(void)
205 if (bcm963xx_wdt_device
.queue
){
206 bcm963xx_wdt_device
.queue
= 0;
207 wait_for_completion(&bcm963xx_wdt_device
.stop
);
209 misc_deregister(&bcm963xx_wdt_miscdev
);
212 static int __init
bcm963xx_wdt_init(void)
216 printk("Broadcom BCM963xx Watchdog timer\n");
218 ret
= misc_register(&bcm963xx_wdt_miscdev
);
220 printk(KERN_CRIT
"Cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR
, ret
);
223 init_completion(&bcm963xx_wdt_device
.stop
);
224 bcm963xx_wdt_device
.queue
= 0;
226 clear_bit(0, &bcm963xx_wdt_device
.inuse
);
228 init_timer(&bcm963xx_wdt_device
.timer
);
229 bcm963xx_wdt_device
.timer
.function
= bcm963xx_wdt_trigger
;
230 bcm963xx_wdt_device
.timer
.data
= 0;
232 bcm963xx_wdt_device
.default_ticks
= ticks
;
237 module_init(bcm963xx_wdt_init
);
238 module_exit(bcm963xx_wdt_exit
);
240 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
241 MODULE_DESCRIPTION("Broadcom BCM963xx Watchdog driver");
242 MODULE_LICENSE("GPL");