Commit | Line | Data |
---|---|---|
3268b561 | 1 | /* |
cda61c94 | 2 | * GE watchdog userspace interface |
3268b561 | 3 | * |
cda61c94 | 4 | * Author: Martyn Welch <martyn.welch@ge.com> |
3268b561 | 5 | * |
cda61c94 | 6 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
3268b561 MW |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. | |
12 | * | |
13 | * Based on: mv64x60_wdt.c (MV64X60 watchdog userspace interface) | |
14 | * Author: James Chapman <jchapman@katalix.com> | |
15 | */ | |
16 | ||
17 | /* TODO: | |
18 | * This driver does not provide support for the hardwares capability of sending | |
19 | * an interrupt at a programmable threshold. | |
20 | * | |
21 | * This driver currently can only support 1 watchdog - there are 2 in the | |
22 | * hardware that this driver supports. Thus one could be configured as a | |
23 | * process-based watchdog (via /dev/watchdog), the second (using the interrupt | |
24 | * capabilities) a kernel-based watchdog. | |
25 | */ | |
26 | ||
27 | #include <linux/kernel.h> | |
28 | #include <linux/compiler.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/miscdevice.h> | |
32 | #include <linux/watchdog.h> | |
f6e0722f | 33 | #include <linux/fs.h> |
3268b561 MW |
34 | #include <linux/of.h> |
35 | #include <linux/of_platform.h> | |
36 | #include <linux/io.h> | |
37 | #include <linux/uaccess.h> | |
38 | ||
39 | #include <sysdev/fsl_soc.h> | |
40 | ||
41 | /* | |
42 | * The watchdog configuration register contains a pair of 2-bit fields, | |
43 | * 1. a reload field, bits 27-26, which triggers a reload of | |
44 | * the countdown register, and | |
45 | * 2. an enable field, bits 25-24, which toggles between | |
46 | * enabling and disabling the watchdog timer. | |
47 | * Bit 31 is a read-only field which indicates whether the | |
48 | * watchdog timer is currently enabled. | |
49 | * | |
50 | * The low 24 bits contain the timer reload value. | |
51 | */ | |
52 | #define GEF_WDC_ENABLE_SHIFT 24 | |
53 | #define GEF_WDC_SERVICE_SHIFT 26 | |
54 | #define GEF_WDC_ENABLED_SHIFT 31 | |
55 | ||
56 | #define GEF_WDC_ENABLED_TRUE 1 | |
57 | #define GEF_WDC_ENABLED_FALSE 0 | |
58 | ||
59 | /* Flags bits */ | |
60 | #define GEF_WDOG_FLAG_OPENED 0 | |
61 | ||
62 | static unsigned long wdt_flags; | |
63 | static int wdt_status; | |
64 | static void __iomem *gef_wdt_regs; | |
65 | static int gef_wdt_timeout; | |
66 | static int gef_wdt_count; | |
67 | static unsigned int bus_clk; | |
68 | static char expect_close; | |
69 | static DEFINE_SPINLOCK(gef_wdt_spinlock); | |
70 | ||
71 | static int nowayout = WATCHDOG_NOWAYOUT; | |
72 | module_param(nowayout, int, 0); | |
73 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" | |
74 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
75 | ||
76 | ||
77 | static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift) | |
78 | { | |
79 | u32 data; | |
80 | u32 enabled; | |
81 | int ret = 0; | |
82 | ||
83 | spin_lock(&gef_wdt_spinlock); | |
84 | data = ioread32be(gef_wdt_regs); | |
85 | enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1; | |
86 | ||
87 | /* only toggle the requested field if enabled state matches predicate */ | |
88 | if ((enabled ^ enabled_predicate) == 0) { | |
89 | /* We write a 1, then a 2 -- to the appropriate field */ | |
90 | data = (1 << field_shift) | gef_wdt_count; | |
91 | iowrite32be(data, gef_wdt_regs); | |
92 | ||
93 | data = (2 << field_shift) | gef_wdt_count; | |
94 | iowrite32be(data, gef_wdt_regs); | |
95 | ret = 1; | |
96 | } | |
97 | spin_unlock(&gef_wdt_spinlock); | |
98 | ||
99 | return ret; | |
100 | } | |
101 | ||
102 | static void gef_wdt_service(void) | |
103 | { | |
104 | gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE, | |
105 | GEF_WDC_SERVICE_SHIFT); | |
106 | } | |
107 | ||
108 | static void gef_wdt_handler_enable(void) | |
109 | { | |
110 | if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE, | |
111 | GEF_WDC_ENABLE_SHIFT)) { | |
112 | gef_wdt_service(); | |
113 | printk(KERN_NOTICE "gef_wdt: watchdog activated\n"); | |
114 | } | |
115 | } | |
116 | ||
117 | static void gef_wdt_handler_disable(void) | |
118 | { | |
119 | if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE, | |
120 | GEF_WDC_ENABLE_SHIFT)) | |
121 | printk(KERN_NOTICE "gef_wdt: watchdog deactivated\n"); | |
122 | } | |
123 | ||
124 | static void gef_wdt_set_timeout(unsigned int timeout) | |
125 | { | |
126 | /* maximum bus cycle count is 0xFFFFFFFF */ | |
127 | if (timeout > 0xFFFFFFFF / bus_clk) | |
128 | timeout = 0xFFFFFFFF / bus_clk; | |
129 | ||
130 | /* Register only holds upper 24 bits, bit shifted into lower 24 */ | |
131 | gef_wdt_count = (timeout * bus_clk) >> 8; | |
132 | gef_wdt_timeout = timeout; | |
133 | } | |
134 | ||
135 | ||
136 | static ssize_t gef_wdt_write(struct file *file, const char __user *data, | |
137 | size_t len, loff_t *ppos) | |
138 | { | |
139 | if (len) { | |
140 | if (!nowayout) { | |
141 | size_t i; | |
142 | ||
143 | expect_close = 0; | |
144 | ||
145 | for (i = 0; i != len; i++) { | |
146 | char c; | |
147 | if (get_user(c, data + i)) | |
148 | return -EFAULT; | |
149 | if (c == 'V') | |
150 | expect_close = 42; | |
151 | } | |
152 | } | |
153 | gef_wdt_service(); | |
154 | } | |
155 | ||
156 | return len; | |
157 | } | |
158 | ||
159 | static long gef_wdt_ioctl(struct file *file, unsigned int cmd, | |
160 | unsigned long arg) | |
161 | { | |
162 | int timeout; | |
163 | int options; | |
164 | void __user *argp = (void __user *)arg; | |
42747d71 | 165 | static const struct watchdog_info info = { |
3268b561 MW |
166 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | |
167 | WDIOF_KEEPALIVEPING, | |
168 | .firmware_version = 0, | |
cda61c94 | 169 | .identity = "GE watchdog", |
3268b561 MW |
170 | }; |
171 | ||
172 | switch (cmd) { | |
173 | case WDIOC_GETSUPPORT: | |
174 | if (copy_to_user(argp, &info, sizeof(info))) | |
175 | return -EFAULT; | |
176 | break; | |
177 | ||
178 | case WDIOC_GETSTATUS: | |
179 | case WDIOC_GETBOOTSTATUS: | |
180 | if (put_user(wdt_status, (int __user *)argp)) | |
181 | return -EFAULT; | |
182 | wdt_status &= ~WDIOF_KEEPALIVEPING; | |
183 | break; | |
184 | ||
185 | case WDIOC_SETOPTIONS: | |
186 | if (get_user(options, (int __user *)argp)) | |
187 | return -EFAULT; | |
188 | ||
189 | if (options & WDIOS_DISABLECARD) | |
190 | gef_wdt_handler_disable(); | |
191 | ||
192 | if (options & WDIOS_ENABLECARD) | |
193 | gef_wdt_handler_enable(); | |
194 | break; | |
195 | ||
196 | case WDIOC_KEEPALIVE: | |
197 | gef_wdt_service(); | |
198 | wdt_status |= WDIOF_KEEPALIVEPING; | |
199 | break; | |
200 | ||
201 | case WDIOC_SETTIMEOUT: | |
202 | if (get_user(timeout, (int __user *)argp)) | |
203 | return -EFAULT; | |
204 | gef_wdt_set_timeout(timeout); | |
205 | /* Fall through */ | |
206 | ||
207 | case WDIOC_GETTIMEOUT: | |
208 | if (put_user(gef_wdt_timeout, (int __user *)argp)) | |
209 | return -EFAULT; | |
210 | break; | |
211 | ||
212 | default: | |
213 | return -ENOTTY; | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | static int gef_wdt_open(struct inode *inode, struct file *file) | |
220 | { | |
221 | if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags)) | |
222 | return -EBUSY; | |
223 | ||
224 | if (nowayout) | |
225 | __module_get(THIS_MODULE); | |
226 | ||
227 | gef_wdt_handler_enable(); | |
228 | ||
229 | return nonseekable_open(inode, file); | |
230 | } | |
231 | ||
232 | static int gef_wdt_release(struct inode *inode, struct file *file) | |
233 | { | |
234 | if (expect_close == 42) | |
235 | gef_wdt_handler_disable(); | |
236 | else { | |
237 | printk(KERN_CRIT | |
238 | "gef_wdt: unexpected close, not stopping timer!\n"); | |
239 | gef_wdt_service(); | |
240 | } | |
241 | expect_close = 0; | |
242 | ||
243 | clear_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags); | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
248 | static const struct file_operations gef_wdt_fops = { | |
249 | .owner = THIS_MODULE, | |
250 | .llseek = no_llseek, | |
251 | .write = gef_wdt_write, | |
252 | .unlocked_ioctl = gef_wdt_ioctl, | |
253 | .open = gef_wdt_open, | |
254 | .release = gef_wdt_release, | |
255 | }; | |
256 | ||
257 | static struct miscdevice gef_wdt_miscdev = { | |
258 | .minor = WATCHDOG_MINOR, | |
259 | .name = "watchdog", | |
260 | .fops = &gef_wdt_fops, | |
261 | }; | |
262 | ||
263 | ||
1c48a5c9 | 264 | static int __devinit gef_wdt_probe(struct platform_device *dev) |
3268b561 MW |
265 | { |
266 | int timeout = 10; | |
267 | u32 freq; | |
268 | ||
269 | bus_clk = 133; /* in MHz */ | |
270 | ||
271 | freq = fsl_get_sys_freq(); | |
26952669 | 272 | if (freq != -1) |
3268b561 MW |
273 | bus_clk = freq; |
274 | ||
275 | /* Map devices registers into memory */ | |
b74dbf2a | 276 | gef_wdt_regs = of_iomap(dev->dev.of_node, 0); |
3268b561 MW |
277 | if (gef_wdt_regs == NULL) |
278 | return -ENOMEM; | |
279 | ||
280 | gef_wdt_set_timeout(timeout); | |
281 | ||
282 | gef_wdt_handler_disable(); /* in case timer was already running */ | |
283 | ||
284 | return misc_register(&gef_wdt_miscdev); | |
285 | } | |
286 | ||
287 | static int __devexit gef_wdt_remove(struct platform_device *dev) | |
288 | { | |
289 | misc_deregister(&gef_wdt_miscdev); | |
290 | ||
291 | gef_wdt_handler_disable(); | |
292 | ||
293 | iounmap(gef_wdt_regs); | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | static const struct of_device_id gef_wdt_ids[] = { | |
299 | { | |
300 | .compatible = "gef,fpga-wdt", | |
301 | }, | |
302 | {}, | |
303 | }; | |
304 | ||
1c48a5c9 | 305 | static struct platform_driver gef_wdt_driver = { |
4018294b GL |
306 | .driver = { |
307 | .name = "gef_wdt", | |
308 | .owner = THIS_MODULE, | |
309 | .of_match_table = gef_wdt_ids, | |
310 | }, | |
3268b561 MW |
311 | .probe = gef_wdt_probe, |
312 | }; | |
313 | ||
314 | static int __init gef_wdt_init(void) | |
315 | { | |
cda61c94 | 316 | printk(KERN_INFO "GE watchdog driver\n"); |
1c48a5c9 | 317 | return platform_driver_register(&gef_wdt_driver); |
3268b561 MW |
318 | } |
319 | ||
320 | static void __exit gef_wdt_exit(void) | |
321 | { | |
1c48a5c9 | 322 | platform_driver_unregister(&gef_wdt_driver); |
3268b561 MW |
323 | } |
324 | ||
325 | module_init(gef_wdt_init); | |
326 | module_exit(gef_wdt_exit); | |
327 | ||
cda61c94 MW |
328 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>"); |
329 | MODULE_DESCRIPTION("GE watchdog driver"); | |
3268b561 MW |
330 | MODULE_LICENSE("GPL"); |
331 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | |
332 | MODULE_ALIAS("platform: gef_wdt"); |