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
;
93 /* Also reload default count */
94 WDT
->wdt_def_count
= ticks
;
95 WDT
->wdt_ctl
= 0xee00;
96 WDT
->wdt_ctl
= 0x00ee;
99 static void bcm963xx_wdt_start(void)
101 if (!bcm963xx_wdt_device
.queue
) {
102 bcm963xx_wdt_device
.queue
;
103 /* Enable the watchdog by writing 0xff00 ,then 0x00ff to the control register */
104 WDT
->wdt_ctl
= 0xff00;
105 WDT
->wdt_ctl
= 0x00ff;
106 bcm963xx_wdt_device
.timer
.expires
= jiffies
+ BCM963XX_INTERVAL
;
107 add_timer(&bcm963xx_wdt_device
.timer
);
109 bcm963xx_wdt_device
.running
++;
112 static int bcm963xx_wdt_stop(void)
114 if (bcm963xx_wdt_device
.running
)
115 bcm963xx_wdt_device
.running
= 0;
117 ticks
= bcm963xx_wdt_device
.default_ticks
;
119 /* Stop the watchdog by writing 0xee00 then 0x00ee to the control register */
120 WDT
->wdt_ctl
= 0xee00;
121 WDT
->wdt_ctl
= 0x00ee;
126 static int bcm963xx_wdt_open(struct inode
*inode
, struct file
*file
)
128 if (test_and_set_bit(0, &bcm963xx_wdt_device
.inuse
))
130 return nonseekable_open(inode
, file
);
133 static int bcm963xx_wdt_release(struct inode
*inode
, struct file
*file
)
135 clear_bit(0, &bcm963xx_wdt_device
.inuse
);
139 static int bcm963xx_wdt_ioctl(struct inode
*inode
, struct file
*file
,
140 unsigned int cmd
, unsigned long arg
)
142 void __user
*argp
= (void __user
*)arg
;
145 static struct watchdog_info ident
= {
146 .options
= WDIOF_CARDRESET
,
147 .identity
= "BCM963xx WDT",
151 case WDIOC_KEEPALIVE
:
152 bcm963xx_wdt_reset();
154 case WDIOC_GETSTATUS
:
155 /* Reading from the control register will return the current value */
156 value
= WDT
->wdt_ctl
;
157 if ( copy_to_user(argp
, &value
, sizeof(int)) )
160 case WDIOC_GETSUPPORT
:
161 if ( copy_to_user(argp
, &ident
, sizeof(ident
)) )
164 case WDIOC_SETOPTIONS
:
165 if ( copy_from_user(&value
, argp
, sizeof(int)) )
168 case WDIOS_ENABLECARD
:
169 bcm963xx_wdt_start();
171 case WDIOS_DISABLECARD
:
184 static int bcm963xx_wdt_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
188 bcm963xx_wdt_reset();
192 static const struct file_operations bcm963xx_wdt_fops
= {
193 .owner
= THIS_MODULE
,
195 .write
= bcm963xx_wdt_write
,
196 .ioctl
= bcm963xx_wdt_ioctl
,
197 .open
= bcm963xx_wdt_open
,
198 .release
= bcm963xx_wdt_release
,
201 static struct miscdevice bcm963xx_wdt_miscdev
= {
202 .minor
= WATCHDOG_MINOR
,
204 .fops
= &bcm963xx_wdt_fops
,
207 static void __exit
bcm963xx_wdt_exit(void)
209 if (bcm963xx_wdt_device
.queue
){
210 bcm963xx_wdt_device
.queue
= 0;
211 wait_for_completion(&bcm963xx_wdt_device
.stop
);
213 misc_deregister(&bcm963xx_wdt_miscdev
);
216 static int __init
bcm963xx_wdt_init(void)
220 printk("Broadcom BCM963xx Watchdog timer\n");
222 ret
= misc_register(&bcm963xx_wdt_miscdev
);
224 printk(KERN_CRIT
"Cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR
, ret
);
227 init_completion(&bcm963xx_wdt_device
.stop
);
228 bcm963xx_wdt_device
.queue
= 0;
230 clear_bit(0, &bcm963xx_wdt_device
.inuse
);
232 init_timer(&bcm963xx_wdt_device
.timer
);
233 bcm963xx_wdt_device
.timer
.function
= bcm963xx_wdt_trigger
;
234 bcm963xx_wdt_device
.timer
.data
= 0;
236 bcm963xx_wdt_device
.default_ticks
= ticks
;
241 module_init(bcm963xx_wdt_init
);
242 module_exit(bcm963xx_wdt_exit
);
244 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
245 MODULE_DESCRIPTION("Broadcom BCM963xx Watchdog driver");
246 MODULE_LICENSE("GPL");