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 ORION5X_TCLK 166666667
33 #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK)
35 #define WDT_OK_TO_CLOSE 1
37 static int nowayout
= WATCHDOG_NOWAYOUT
;
38 static int heartbeat
= WDT_MAX_DURATION
; /* (seconds) */
39 static unsigned long wdt_status
;
40 static spinlock_t wdt_lock
;
42 static void wdt_enable(void)
48 /* Set watchdog duration */
49 writel(ORION5X_TCLK
* heartbeat
, WDT_VAL
);
51 /* Clear watchdog timer interrupt */
52 reg
= readl(BRIDGE_CAUSE
);
54 writel(reg
, BRIDGE_CAUSE
);
56 /* Enable watchdog timer */
57 reg
= readl(TIMER_CTRL
);
59 writel(reg
, TIMER_CTRL
);
61 /* Enable reset on watchdog */
62 reg
= readl(CPU_RESET_MASK
);
64 writel(reg
, CPU_RESET_MASK
);
66 spin_unlock(&wdt_lock
);
69 static void wdt_disable(void)
75 /* Disable reset on watchdog */
76 reg
= readl(CPU_RESET_MASK
);
78 writel(reg
, CPU_RESET_MASK
);
80 /* Disable watchdog timer */
81 reg
= readl(TIMER_CTRL
);
83 writel(reg
, TIMER_CTRL
);
85 spin_unlock(&wdt_lock
);
88 static int orion5x_wdt_get_timeleft(int *time_left
)
91 *time_left
= readl(WDT_VAL
) / ORION5X_TCLK
;
92 spin_unlock(&wdt_lock
);
96 static int orion5x_wdt_open(struct inode
*inode
, struct file
*file
)
98 if (test_and_set_bit(WDT_IN_USE
, &wdt_status
))
100 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
102 return nonseekable_open(inode
, file
);
105 static ssize_t
orion5x_wdt_write(struct file
*file
, const char *data
,
106 size_t len
, loff_t
*ppos
)
112 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
113 for (i
= 0; i
!= len
; i
++) {
116 if (get_user(c
, data
+ i
))
119 set_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
127 static struct watchdog_info ident
= {
128 .options
= WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT
|
130 .identity
= "Orion5x Watchdog",
134 static long orion5x_wdt_ioctl(struct file
*file
, unsigned int cmd
,
141 case WDIOC_GETSUPPORT
:
142 ret
= copy_to_user((struct watchdog_info
*)arg
, &ident
,
143 sizeof(ident
)) ? -EFAULT
: 0;
146 case WDIOC_GETSTATUS
:
147 case WDIOC_GETBOOTSTATUS
:
148 ret
= put_user(0, (int *)arg
);
151 case WDIOC_KEEPALIVE
:
156 case WDIOC_SETTIMEOUT
:
157 ret
= get_user(time
, (int *)arg
);
161 if (time
<= 0 || time
> WDT_MAX_DURATION
) {
169 case WDIOC_GETTIMEOUT
:
170 ret
= put_user(heartbeat
, (int *)arg
);
173 case WDIOC_GETTIMELEFT
:
174 if (orion5x_wdt_get_timeleft(&time
)) {
178 ret
= put_user(time
, (int *)arg
);
184 static int orion5x_wdt_release(struct inode
*inode
, struct file
*file
)
186 if (test_bit(WDT_OK_TO_CLOSE
, &wdt_status
))
189 printk(KERN_CRIT
"WATCHDOG: Device closed unexpectedly - "
190 "timer will not stop\n");
191 clear_bit(WDT_IN_USE
, &wdt_status
);
192 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
198 static const struct file_operations orion5x_wdt_fops
= {
199 .owner
= THIS_MODULE
,
201 .write
= orion5x_wdt_write
,
202 .unlocked_ioctl
= orion5x_wdt_ioctl
,
203 .open
= orion5x_wdt_open
,
204 .release
= orion5x_wdt_release
,
207 static struct miscdevice orion5x_wdt_miscdev
= {
208 .minor
= WATCHDOG_MINOR
,
210 .fops
= &orion5x_wdt_fops
,
213 static int __init
orion5x_wdt_init(void)
217 spin_lock_init(&wdt_lock
);
219 ret
= misc_register(&orion5x_wdt_miscdev
);
221 printk("Orion5x Watchdog Timer: heartbeat %d sec\n",
227 static void __exit
orion5x_wdt_exit(void)
229 misc_deregister(&orion5x_wdt_miscdev
);
232 module_init(orion5x_wdt_init
);
233 module_exit(orion5x_wdt_exit
);
235 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
236 MODULE_DESCRIPTION("Orion5x Processor Watchdog");
238 module_param(heartbeat
, int, 0);
239 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds (default is "
240 __MODULE_STRING(WDT_MAX_DURATION
) ")");
242 module_param(nowayout
, int, 0);
243 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started");
245 MODULE_LICENSE("GPL");
246 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);