]>
Commit | Line | Data |
---|---|---|
22ac9232 SB |
1 | /* |
2 | * drivers/watchdog/orion5x_wdt.c | |
3 | * | |
4 | * Watchdog driver for Orion5x processors | |
5 | * | |
6 | * Author: Sylver Bruneau <sylver.bruneau@googlemail.com> | |
7 | * | |
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. | |
11 | */ | |
12 | ||
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> | |
6d0f0dfd | 23 | #include <linux/spinlock.h> |
22ac9232 SB |
24 | |
25 | /* | |
26 | * Watchdog timer block registers. | |
27 | */ | |
28 | #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000) | |
29 | #define WDT_EN 0x0010 | |
30 | #define WDT_VAL (TIMER_VIRT_BASE + 0x0024) | |
31 | ||
c9a0c8a6 | 32 | #define ORION5X_TCLK 166666667 |
22ac9232 SB |
33 | #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK) |
34 | #define WDT_IN_USE 0 | |
35 | #define WDT_OK_TO_CLOSE 1 | |
36 | ||
37 | static int nowayout = WATCHDOG_NOWAYOUT; | |
38 | static int heartbeat = WDT_MAX_DURATION; /* (seconds) */ | |
39 | static unsigned long wdt_status; | |
6d0f0dfd | 40 | static spinlock_t wdt_lock; |
22ac9232 SB |
41 | |
42 | static void wdt_enable(void) | |
43 | { | |
44 | u32 reg; | |
45 | ||
6d0f0dfd WVS |
46 | spin_lock(&wdt_lock); |
47 | ||
22ac9232 SB |
48 | /* Set watchdog duration */ |
49 | writel(ORION5X_TCLK * heartbeat, WDT_VAL); | |
50 | ||
51 | /* Clear watchdog timer interrupt */ | |
52 | reg = readl(BRIDGE_CAUSE); | |
53 | reg &= ~WDT_INT_REQ; | |
54 | writel(reg, BRIDGE_CAUSE); | |
55 | ||
56 | /* Enable watchdog timer */ | |
57 | reg = readl(TIMER_CTRL); | |
58 | reg |= WDT_EN; | |
59 | writel(reg, TIMER_CTRL); | |
60 | ||
61 | /* Enable reset on watchdog */ | |
62 | reg = readl(CPU_RESET_MASK); | |
63 | reg |= WDT_RESET; | |
64 | writel(reg, CPU_RESET_MASK); | |
6d0f0dfd WVS |
65 | |
66 | spin_unlock(&wdt_lock); | |
22ac9232 SB |
67 | } |
68 | ||
69 | static void wdt_disable(void) | |
70 | { | |
71 | u32 reg; | |
72 | ||
6d0f0dfd WVS |
73 | spin_lock(&wdt_lock); |
74 | ||
22ac9232 SB |
75 | /* Disable reset on watchdog */ |
76 | reg = readl(CPU_RESET_MASK); | |
77 | reg &= ~WDT_RESET; | |
78 | writel(reg, CPU_RESET_MASK); | |
79 | ||
80 | /* Disable watchdog timer */ | |
81 | reg = readl(TIMER_CTRL); | |
82 | reg &= ~WDT_EN; | |
83 | writel(reg, TIMER_CTRL); | |
6d0f0dfd WVS |
84 | |
85 | spin_unlock(&wdt_lock); | |
86 | } | |
87 | ||
88 | static int orion5x_wdt_get_timeleft(int *time_left) | |
89 | { | |
90 | spin_lock(&wdt_lock); | |
91 | *time_left = readl(WDT_VAL) / ORION5X_TCLK; | |
92 | spin_unlock(&wdt_lock); | |
93 | return 0; | |
22ac9232 SB |
94 | } |
95 | ||
96 | static int orion5x_wdt_open(struct inode *inode, struct file *file) | |
97 | { | |
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); | |
103 | } | |
104 | ||
22ac9232 SB |
105 | static ssize_t orion5x_wdt_write(struct file *file, const char *data, |
106 | size_t len, loff_t *ppos) | |
107 | { | |
108 | if (len) { | |
109 | if (!nowayout) { | |
110 | size_t i; | |
111 | ||
112 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); | |
113 | for (i = 0; i != len; i++) { | |
114 | char c; | |
115 | ||
116 | if (get_user(c, data + i)) | |
117 | return -EFAULT; | |
118 | if (c == 'V') | |
119 | set_bit(WDT_OK_TO_CLOSE, &wdt_status); | |
120 | } | |
121 | } | |
122 | wdt_enable(); | |
123 | } | |
124 | return len; | |
125 | } | |
126 | ||
127 | static struct watchdog_info ident = { | |
128 | .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | | |
129 | WDIOF_KEEPALIVEPING, | |
130 | .identity = "Orion5x Watchdog", | |
131 | }; | |
132 | ||
133 | ||
134 | static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd, | |
135 | unsigned long arg) | |
136 | { | |
137 | int ret = -ENOTTY; | |
138 | int time; | |
139 | ||
140 | switch (cmd) { | |
141 | case WDIOC_GETSUPPORT: | |
142 | ret = copy_to_user((struct watchdog_info *)arg, &ident, | |
143 | sizeof(ident)) ? -EFAULT : 0; | |
144 | break; | |
145 | ||
146 | case WDIOC_GETSTATUS: | |
147 | case WDIOC_GETBOOTSTATUS: | |
148 | ret = put_user(0, (int *)arg); | |
149 | break; | |
150 | ||
151 | case WDIOC_KEEPALIVE: | |
152 | wdt_enable(); | |
153 | ret = 0; | |
154 | break; | |
155 | ||
156 | case WDIOC_SETTIMEOUT: | |
157 | ret = get_user(time, (int *)arg); | |
158 | if (ret) | |
159 | break; | |
160 | ||
161 | if (time <= 0 || time > WDT_MAX_DURATION) { | |
162 | ret = -EINVAL; | |
163 | break; | |
164 | } | |
165 | heartbeat = time; | |
166 | wdt_enable(); | |
167 | /* Fall through */ | |
168 | ||
169 | case WDIOC_GETTIMEOUT: | |
170 | ret = put_user(heartbeat, (int *)arg); | |
171 | break; | |
172 | ||
173 | case WDIOC_GETTIMELEFT: | |
174 | if (orion5x_wdt_get_timeleft(&time)) { | |
175 | ret = -EINVAL; | |
176 | break; | |
177 | } | |
178 | ret = put_user(time, (int *)arg); | |
179 | break; | |
180 | } | |
181 | return ret; | |
182 | } | |
183 | ||
184 | static int orion5x_wdt_release(struct inode *inode, struct file *file) | |
185 | { | |
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); | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
197 | ||
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, | |
205 | }; | |
206 | ||
207 | static struct miscdevice orion5x_wdt_miscdev = { | |
208 | .minor = WATCHDOG_MINOR, | |
209 | .name = "watchdog", | |
210 | .fops = &orion5x_wdt_fops, | |
211 | }; | |
212 | ||
213 | static int __init orion5x_wdt_init(void) | |
214 | { | |
215 | int ret; | |
216 | ||
6d0f0dfd WVS |
217 | spin_lock_init(&wdt_lock); |
218 | ||
22ac9232 SB |
219 | ret = misc_register(&orion5x_wdt_miscdev); |
220 | if (ret == 0) | |
221 | printk("Orion5x Watchdog Timer: heartbeat %d sec\n", | |
222 | heartbeat); | |
223 | ||
224 | return ret; | |
225 | } | |
226 | ||
227 | static void __exit orion5x_wdt_exit(void) | |
228 | { | |
229 | misc_deregister(&orion5x_wdt_miscdev); | |
230 | } | |
231 | ||
232 | module_init(orion5x_wdt_init); | |
233 | module_exit(orion5x_wdt_exit); | |
234 | ||
235 | MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>"); | |
236 | MODULE_DESCRIPTION("Orion5x Processor Watchdog"); | |
237 | ||
238 | module_param(heartbeat, int, 0); | |
239 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default is " | |
240 | __MODULE_STRING(WDT_MAX_DURATION) ")"); | |
241 | ||
242 | module_param(nowayout, int, 0); | |
243 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"); | |
244 | ||
245 | MODULE_LICENSE("GPL"); | |
246 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |