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>
25 * Watchdog timer block registers.
27 #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
29 #define WDT_VAL (TIMER_VIRT_BASE + 0x0024)
31 #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK)
33 #define WDT_OK_TO_CLOSE 1
35 static int nowayout = WATCHDOG_NOWAYOUT;
36 static int heartbeat = WDT_MAX_DURATION; /* (seconds) */
37 static unsigned long wdt_status;
39 static void wdt_enable(void)
43 /* Set watchdog duration */
44 writel(ORION5X_TCLK * heartbeat, WDT_VAL);
46 /* Clear watchdog timer interrupt */
47 reg = readl(BRIDGE_CAUSE);
49 writel(reg, BRIDGE_CAUSE);
51 /* Enable watchdog timer */
52 reg = readl(TIMER_CTRL);
54 writel(reg, TIMER_CTRL);
56 /* Enable reset on watchdog */
57 reg = readl(CPU_RESET_MASK);
59 writel(reg, CPU_RESET_MASK);
62 static void wdt_disable(void)
66 /* Disable reset on watchdog */
67 reg = readl(CPU_RESET_MASK);
69 writel(reg, CPU_RESET_MASK);
71 /* Disable watchdog timer */
72 reg = readl(TIMER_CTRL);
74 writel(reg, TIMER_CTRL);
77 static int orion5x_wdt_open(struct inode *inode, struct file *file)
79 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
81 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
83 return nonseekable_open(inode, file);
86 static int orion5x_wdt_get_timeleft(int *time_left)
88 *time_left = readl(WDT_VAL) / ORION5X_TCLK;
92 static ssize_t orion5x_wdt_write(struct file *file, const char *data,
93 size_t len, loff_t *ppos)
99 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
100 for (i = 0; i != len; i++) {
103 if (get_user(c, data + i))
106 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
114 static struct watchdog_info ident = {
115 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
117 .identity = "Orion5x Watchdog",
121 static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd,
128 case WDIOC_GETSUPPORT:
129 ret = copy_to_user((struct watchdog_info *)arg, &ident,
130 sizeof(ident)) ? -EFAULT : 0;
133 case WDIOC_GETSTATUS:
134 case WDIOC_GETBOOTSTATUS:
135 ret = put_user(0, (int *)arg);
138 case WDIOC_KEEPALIVE:
143 case WDIOC_SETTIMEOUT:
144 ret = get_user(time, (int *)arg);
148 if (time <= 0 || time > WDT_MAX_DURATION) {
156 case WDIOC_GETTIMEOUT:
157 ret = put_user(heartbeat, (int *)arg);
160 case WDIOC_GETTIMELEFT:
161 if (orion5x_wdt_get_timeleft(&time)) {
165 ret = put_user(time, (int *)arg);
171 static int orion5x_wdt_release(struct inode *inode, struct file *file)
173 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
176 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
177 "timer will not stop\n");
178 clear_bit(WDT_IN_USE, &wdt_status);
179 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
185 static const struct file_operations orion5x_wdt_fops = {
186 .owner = THIS_MODULE,
188 .write = orion5x_wdt_write,
189 .unlocked_ioctl = orion5x_wdt_ioctl,
190 .open = orion5x_wdt_open,
191 .release = orion5x_wdt_release,
194 static struct miscdevice orion5x_wdt_miscdev = {
195 .minor = WATCHDOG_MINOR,
197 .fops = &orion5x_wdt_fops,
200 static int __init orion5x_wdt_init(void)
204 ret = misc_register(&orion5x_wdt_miscdev);
206 printk("Orion5x Watchdog Timer: heartbeat %d sec\n",
212 static void __exit orion5x_wdt_exit(void)
214 misc_deregister(&orion5x_wdt_miscdev);
217 module_init(orion5x_wdt_init);
218 module_exit(orion5x_wdt_exit);
220 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
221 MODULE_DESCRIPTION("Orion5x Processor Watchdog");
223 module_param(heartbeat, int, 0);
224 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default is "
225 __MODULE_STRING(WDT_MAX_DURATION) ")");
227 module_param(nowayout, int, 0);
228 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
230 MODULE_LICENSE("GPL");
231 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);