crypto: api - Fix algorithm module auto-loading
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / watchdog / orion5x_wdt.c
blobb64ae1a17832c0e6dd53b045af77d805db005b86
1 /*
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>
17 #include <linux/fs.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/init.h>
21 #include <linux/uaccess.h>
22 #include <linux/io.h>
23 #include <linux/spinlock.h>
26 * Watchdog timer block registers.
28 #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
29 #define WDT_EN 0x0010
30 #define WDT_VAL (TIMER_VIRT_BASE + 0x0024)
32 #define ORION5X_TCLK 166666667
33 #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK)
34 #define WDT_IN_USE 0
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)
44 u32 reg;
46 spin_lock(&wdt_lock);
48 /* Set watchdog duration */
49 writel(ORION5X_TCLK * heartbeat, WDT_VAL);
51 /* Clear watchdog timer interrupt */
52 reg = readl(BRIDGE_CAUSE);
53 reg &= ~WDT_INT_REQ;
54 writel(reg, BRIDGE_CAUSE);
56 /* Enable watchdog timer */
57 reg = readl(TIMER_CTRL);
58 reg |= WDT_EN;
59 writel(reg, TIMER_CTRL);
61 /* Enable reset on watchdog */
62 reg = readl(CPU_RESET_MASK);
63 reg |= WDT_RESET;
64 writel(reg, CPU_RESET_MASK);
66 spin_unlock(&wdt_lock);
69 static void wdt_disable(void)
71 u32 reg;
73 spin_lock(&wdt_lock);
75 /* Disable reset on watchdog */
76 reg = readl(CPU_RESET_MASK);
77 reg &= ~WDT_RESET;
78 writel(reg, CPU_RESET_MASK);
80 /* Disable watchdog timer */
81 reg = readl(TIMER_CTRL);
82 reg &= ~WDT_EN;
83 writel(reg, TIMER_CTRL);
85 spin_unlock(&wdt_lock);
88 static int orion5x_wdt_get_timeleft(int *time_left)
90 spin_lock(&wdt_lock);
91 *time_left = readl(WDT_VAL) / ORION5X_TCLK;
92 spin_unlock(&wdt_lock);
93 return 0;
96 static int orion5x_wdt_open(struct inode *inode, struct file *file)
98 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
99 return -EBUSY;
100 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
101 wdt_enable();
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)
108 if (len) {
109 if (!nowayout) {
110 size_t i;
112 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
113 for (i = 0; i != len; i++) {
114 char c;
116 if (get_user(c, data + i))
117 return -EFAULT;
118 if (c == 'V')
119 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
122 wdt_enable();
124 return len;
127 static struct watchdog_info ident = {
128 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
129 WDIOF_KEEPALIVEPING,
130 .identity = "Orion5x Watchdog",
134 static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd,
135 unsigned long arg)
137 int ret = -ENOTTY;
138 int time;
140 switch (cmd) {
141 case WDIOC_GETSUPPORT:
142 ret = copy_to_user((struct watchdog_info *)arg, &ident,
143 sizeof(ident)) ? -EFAULT : 0;
144 break;
146 case WDIOC_GETSTATUS:
147 case WDIOC_GETBOOTSTATUS:
148 ret = put_user(0, (int *)arg);
149 break;
151 case WDIOC_KEEPALIVE:
152 wdt_enable();
153 ret = 0;
154 break;
156 case WDIOC_SETTIMEOUT:
157 ret = get_user(time, (int *)arg);
158 if (ret)
159 break;
161 if (time <= 0 || time > WDT_MAX_DURATION) {
162 ret = -EINVAL;
163 break;
165 heartbeat = time;
166 wdt_enable();
167 /* Fall through */
169 case WDIOC_GETTIMEOUT:
170 ret = put_user(heartbeat, (int *)arg);
171 break;
173 case WDIOC_GETTIMELEFT:
174 if (orion5x_wdt_get_timeleft(&time)) {
175 ret = -EINVAL;
176 break;
178 ret = put_user(time, (int *)arg);
179 break;
181 return ret;
184 static int orion5x_wdt_release(struct inode *inode, struct file *file)
186 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
187 wdt_disable();
188 else
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);
194 return 0;
198 static const struct file_operations orion5x_wdt_fops = {
199 .owner = THIS_MODULE,
200 .llseek = no_llseek,
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,
209 .name = "watchdog",
210 .fops = &orion5x_wdt_fops,
213 static int __init orion5x_wdt_init(void)
215 int ret;
217 spin_lock_init(&wdt_lock);
219 ret = misc_register(&orion5x_wdt_miscdev);
220 if (ret == 0)
221 printk("Orion5x Watchdog Timer: heartbeat %d sec\n",
222 heartbeat);
224 return ret;
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);