avila-wdt: add support for disabling the watchdog
[openwrt/svn-archive/archive.git] / package / avila-wdt / src / avila-wdt.c
1 /*
2 * avila-wdt.c
3 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
4 *
5 * based on:
6 * drivers/char/watchdog/ixp4xx_wdt.c
7 *
8 * Watchdog driver for Intel IXP4xx network processors
9 *
10 * Author: Deepak Saxena <dsaxena@plexity.net>
11 *
12 * Copyright 2004 (c) MontaVista, Software, Inc.
13 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 *
15 * This file is licensed under the terms of the GNU General Public
16 * License version 2. This program is licensed "as is" without any
17 * warranty of any kind, whether express or implied.
18 */
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
27 #include <linux/init.h>
28 #include <linux/bitops.h>
29 #include <linux/uaccess.h>
30 #include <mach/hardware.h>
31
32 static int nowayout = WATCHDOG_NOWAYOUT;
33 static int heartbeat = 20; /* (secs) Default is 20 seconds */
34 static unsigned long wdt_status;
35 static atomic_t wdt_counter;
36 struct timer_list wdt_timer;
37
38 #define WDT_IN_USE 0
39 #define WDT_OK_TO_CLOSE 1
40 #define WDT_RUNNING 2
41
42 static void wdt_refresh(unsigned long data)
43 {
44 if (test_bit(WDT_RUNNING, &wdt_status)) {
45 if (atomic_dec_and_test(&wdt_counter)) {
46 printk(KERN_WARNING "Avila watchdog expired, expect a reboot soon!\n");
47 clear_bit(WDT_RUNNING, &wdt_status);
48 return;
49 }
50 }
51
52 /* strobe to the watchdog */
53 gpio_line_set(14, IXP4XX_GPIO_HIGH);
54 gpio_line_set(14, IXP4XX_GPIO_LOW);
55
56 mod_timer(&wdt_timer, jiffies + msecs_to_jiffies(500));
57 }
58
59 static void wdt_enable(void)
60 {
61 atomic_set(&wdt_counter, heartbeat * 2);
62
63 /* Disable clock generator output on GPIO 14/15 */
64 *IXP4XX_GPIO_GPCLKR &= ~(1 << 8);
65
66 /* activate GPIO 14 out */
67 gpio_line_config(14, IXP4XX_GPIO_OUT);
68 gpio_line_set(14, IXP4XX_GPIO_LOW);
69
70 if (!test_bit(WDT_RUNNING, &wdt_status))
71 wdt_refresh(0);
72 set_bit(WDT_RUNNING, &wdt_status);
73 }
74
75 static void wdt_disable(void)
76 {
77 /* Re-enable clock generator output on GPIO 14/15 */
78 *IXP4XX_GPIO_GPCLKR |= (1 << 8);
79 }
80
81 static int avila_wdt_open(struct inode *inode, struct file *file)
82 {
83 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
84 return -EBUSY;
85
86 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
87 wdt_enable();
88 return nonseekable_open(inode, file);
89 }
90
91 static ssize_t
92 avila_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
93 {
94 if (len) {
95 if (!nowayout) {
96 size_t i;
97
98 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
99
100 for (i = 0; i != len; i++) {
101 char c;
102
103 if (get_user(c, data + i))
104 return -EFAULT;
105 if (c == 'V')
106 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
107 }
108 }
109 wdt_enable();
110 }
111 return len;
112 }
113
114 static struct watchdog_info ident = {
115 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
116 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
117 .identity = "Avila Watchdog",
118 };
119
120
121 static long avila_wdt_ioctl(struct file *file, unsigned int cmd,
122 unsigned long arg)
123 {
124 int ret = -ENOTTY;
125 int time;
126
127 switch (cmd) {
128 case WDIOC_GETSUPPORT:
129 ret = copy_to_user((struct watchdog_info *)arg, &ident,
130 sizeof(ident)) ? -EFAULT : 0;
131 break;
132
133 case WDIOC_GETSTATUS:
134 ret = put_user(0, (int *)arg);
135 break;
136
137 case WDIOC_KEEPALIVE:
138 wdt_enable();
139 ret = 0;
140 break;
141
142 case WDIOC_SETTIMEOUT:
143 ret = get_user(time, (int *)arg);
144 if (ret)
145 break;
146
147 if (time <= 0 || time > 60) {
148 ret = -EINVAL;
149 break;
150 }
151
152 heartbeat = time;
153 wdt_enable();
154 /* Fall through */
155
156 case WDIOC_GETTIMEOUT:
157 ret = put_user(heartbeat, (int *)arg);
158 break;
159 }
160 return ret;
161 }
162
163 static int avila_wdt_release(struct inode *inode, struct file *file)
164 {
165 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
166 wdt_disable();
167 else
168 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
169 "timer will not stop\n");
170 clear_bit(WDT_IN_USE, &wdt_status);
171 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
172
173 return 0;
174 }
175
176
177 static const struct file_operations avila_wdt_fops = {
178 .owner = THIS_MODULE,
179 .llseek = no_llseek,
180 .write = avila_wdt_write,
181 .unlocked_ioctl = avila_wdt_ioctl,
182 .open = avila_wdt_open,
183 .release = avila_wdt_release,
184 };
185
186 static struct miscdevice avila_wdt_miscdev = {
187 .minor = WATCHDOG_MINOR + 1,
188 .name = "avila_watchdog",
189 .fops = &avila_wdt_fops,
190 };
191
192 static int __init avila_wdt_init(void)
193 {
194 int ret;
195
196 init_timer(&wdt_timer);
197 wdt_timer.expires = 0;
198 wdt_timer.data = 0;
199 wdt_timer.function = wdt_refresh;
200 ret = misc_register(&avila_wdt_miscdev);
201 if (ret == 0)
202 printk(KERN_INFO "Avila Watchdog Timer: heartbeat %d sec\n",
203 heartbeat);
204 return ret;
205 }
206
207 static void __exit avila_wdt_exit(void)
208 {
209 misc_deregister(&avila_wdt_miscdev);
210 del_timer(&wdt_timer);
211 wdt_disable();
212 }
213
214
215 module_init(avila_wdt_init);
216 module_exit(avila_wdt_exit);
217
218 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
219 MODULE_DESCRIPTION("Gateworks Avila Hardware Watchdog");
220
221 module_param(heartbeat, int, 0);
222 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 20s)");
223
224 module_param(nowayout, int, 0);
225 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
226
227 MODULE_LICENSE("GPL");
228 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
229