2 * drivers/watchdog/orion5x_wdt.c
4 * Watchdog driver for Orion5x processors
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/init.h>
21 #include <linux/uaccess.h>
23 #include <linux/spinlock.h>
26 * Watchdog timer block registers.
28 #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
30 #define WDT_VAL (TIMER_VIRT_BASE + 0x0024)
32 #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK)
34 #define WDT_OK_TO_CLOSE 1
36 static int nowayout
= WATCHDOG_NOWAYOUT
;
37 static int heartbeat
= WDT_MAX_DURATION
; /* (seconds) */
38 static unsigned long wdt_status
;
39 static spinlock_t wdt_lock
;
41 static void wdt_enable(void)
47 /* Set watchdog duration */
48 writel(ORION5X_TCLK
* heartbeat
, WDT_VAL
);
50 /* Clear watchdog timer interrupt */
51 reg
= readl(BRIDGE_CAUSE
);
53 writel(reg
, BRIDGE_CAUSE
);
55 /* Enable watchdog timer */
56 reg
= readl(TIMER_CTRL
);
58 writel(reg
, TIMER_CTRL
);
60 /* Enable reset on watchdog */
61 reg
= readl(CPU_RESET_MASK
);
63 writel(reg
, CPU_RESET_MASK
);
65 spin_unlock(&wdt_lock
);
68 static void wdt_disable(void)
74 /* Disable reset on watchdog */
75 reg
= readl(CPU_RESET_MASK
);
77 writel(reg
, CPU_RESET_MASK
);
79 /* Disable watchdog timer */
80 reg
= readl(TIMER_CTRL
);
82 writel(reg
, TIMER_CTRL
);
84 spin_unlock(&wdt_lock
);
87 static int orion5x_wdt_get_timeleft(int *time_left
)
90 *time_left
= readl(WDT_VAL
) / ORION5X_TCLK
;
91 spin_unlock(&wdt_lock
);
95 static int orion5x_wdt_open(struct inode
*inode
, struct file
*file
)
97 if (test_and_set_bit(WDT_IN_USE
, &wdt_status
))
99 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
101 return nonseekable_open(inode
, file
);
104 static ssize_t
orion5x_wdt_write(struct file
*file
, const char *data
,
105 size_t len
, loff_t
*ppos
)
111 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
112 for (i
= 0; i
!= len
; i
++) {
115 if (get_user(c
, data
+ i
))
118 set_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
126 static struct watchdog_info ident
= {
127 .options
= WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT
|
129 .identity
= "Orion5x Watchdog",
133 static long orion5x_wdt_ioctl(struct file
*file
, unsigned int cmd
,
140 case WDIOC_GETSUPPORT
:
141 ret
= copy_to_user((struct watchdog_info
*)arg
, &ident
,
142 sizeof(ident
)) ? -EFAULT
: 0;
145 case WDIOC_GETSTATUS
:
146 case WDIOC_GETBOOTSTATUS
:
147 ret
= put_user(0, (int *)arg
);
150 case WDIOC_KEEPALIVE
:
155 case WDIOC_SETTIMEOUT
:
156 ret
= get_user(time
, (int *)arg
);
160 if (time
<= 0 || time
> WDT_MAX_DURATION
) {
168 case WDIOC_GETTIMEOUT
:
169 ret
= put_user(heartbeat
, (int *)arg
);
172 case WDIOC_GETTIMELEFT
:
173 if (orion5x_wdt_get_timeleft(&time
)) {
177 ret
= put_user(time
, (int *)arg
);
183 static int orion5x_wdt_release(struct inode
*inode
, struct file
*file
)
185 if (test_bit(WDT_OK_TO_CLOSE
, &wdt_status
))
188 printk(KERN_CRIT
"WATCHDOG: Device closed unexpectedly - "
189 "timer will not stop\n");
190 clear_bit(WDT_IN_USE
, &wdt_status
);
191 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
197 static const struct file_operations orion5x_wdt_fops
= {
198 .owner
= THIS_MODULE
,
200 .write
= orion5x_wdt_write
,
201 .unlocked_ioctl
= orion5x_wdt_ioctl
,
202 .open
= orion5x_wdt_open
,
203 .release
= orion5x_wdt_release
,
206 static struct miscdevice orion5x_wdt_miscdev
= {
207 .minor
= WATCHDOG_MINOR
,
209 .fops
= &orion5x_wdt_fops
,
212 static int __init
orion5x_wdt_init(void)
216 spin_lock_init(&wdt_lock
);
218 ret
= misc_register(&orion5x_wdt_miscdev
);
220 printk("Orion5x Watchdog Timer: heartbeat %d sec\n",
226 static void __exit
orion5x_wdt_exit(void)
228 misc_deregister(&orion5x_wdt_miscdev
);
231 module_init(orion5x_wdt_init
);
232 module_exit(orion5x_wdt_exit
);
234 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
235 MODULE_DESCRIPTION("Orion5x Processor Watchdog");
237 module_param(heartbeat
, int, 0);
238 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds (default is "
239 __MODULE_STRING(WDT_MAX_DURATION
) ")");
241 module_param(nowayout
, int, 0);
242 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started");
244 MODULE_LICENSE("GPL");
245 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);