22aab8703eda5afc4dd3733b17ac99eb8aebbfbb
[deliverable/linux.git] / drivers / char / watchdog / rm9k_wdt.c
1 /*
2 * Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
3 * chips.
4 *
5 * Copyright (C) 2004 by Basler Vision Technologies AG
6 * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include <linux/platform_device.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/interrupt.h>
27 #include <linux/fs.h>
28 #include <linux/reboot.h>
29 #include <linux/miscdevice.h>
30 #include <linux/watchdog.h>
31 #include <asm/io.h>
32 #include <asm/atomic.h>
33 #include <asm/processor.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/rm9k-ocd.h>
37
38 #include <rm9k_wdt.h>
39
40
41 #define CLOCK 125000000
42 #define MAX_TIMEOUT_SECONDS 32
43 #define CPCCR 0x0080
44 #define CPGIG1SR 0x0044
45 #define CPGIG1ER 0x0054
46
47
48 /* Function prototypes */
49 static irqreturn_t wdt_gpi_irqhdl(int, void *, struct pt_regs *);
50 static void wdt_gpi_set_timeout(unsigned int);
51 static int wdt_gpi_open(struct inode *, struct file *);
52 static int wdt_gpi_release(struct inode *, struct file *);
53 static ssize_t wdt_gpi_write(struct file *, const char __user *, size_t, loff_t *);
54 static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
55 static const struct resource *wdt_gpi_get_resource(struct platform_device *, const char *, unsigned int);
56 static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
57 static int __init wdt_gpi_probe(struct device *);
58 static int __exit wdt_gpi_remove(struct device *);
59
60
61 static const char wdt_gpi_name[] = "wdt_gpi";
62 static atomic_t opencnt;
63 static int expect_close;
64 static int locked = 0;
65
66
67 /* These are set from device resources */
68 static void __iomem * wd_regs;
69 static unsigned int wd_irq, wd_ctr;
70
71
72 /* Module arguments */
73 static int timeout = MAX_TIMEOUT_SECONDS;
74 module_param(timeout, int, 0444);
75 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
76
77 static unsigned long resetaddr = 0xbffdc200;
78 module_param(resetaddr, ulong, 0444);
79 MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");
80
81 static unsigned long flagaddr = 0xbffdc104;
82 module_param(flagaddr, ulong, 0444);
83 MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");
84
85 static int powercycle = 0;
86 module_param(powercycle, bool, 0444);
87 MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
88
89 static int nowayout = WATCHDOG_NOWAYOUT;
90 module_param(nowayout, bool, 0444);
91 MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
92
93
94 /* Interrupt handler */
95 static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt, struct pt_regs *regs)
96 {
97 if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
98 return IRQ_NONE;
99 __raw_writel(0x1, wd_regs + 0x0008);
100
101
102 printk(KERN_WARNING "%s: watchdog expired - resetting system\n",
103 wdt_gpi_name);
104
105 *(volatile char *) flagaddr |= 0x01;
106 *(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
107 iob();
108 while (1)
109 cpu_relax();
110 }
111
112
113 /* Watchdog functions */
114 static void wdt_gpi_start(void)
115 {
116 u32 reg;
117
118 lock_titan_regs();
119 reg = titan_readl(CPGIG1ER);
120 titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
121 iob();
122 unlock_titan_regs();
123 }
124
125 static void wdt_gpi_stop(void)
126 {
127 u32 reg;
128
129 lock_titan_regs();
130 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
131 titan_writel(reg, CPCCR);
132 reg = titan_readl(CPGIG1ER);
133 titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
134 iob();
135 unlock_titan_regs();
136 }
137
138 static void wdt_gpi_set_timeout(unsigned int to)
139 {
140 u32 reg;
141 const u32 wdval = (to * CLOCK) & ~0x0000000f;
142
143 lock_titan_regs();
144 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
145 titan_writel(reg, CPCCR);
146 wmb();
147 __raw_writel(wdval, wd_regs + 0x0000);
148 wmb();
149 titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
150 wmb();
151 titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
152 iob();
153 unlock_titan_regs();
154 }
155
156
157 /* /dev/watchdog operations */
158 static int wdt_gpi_open(struct inode *i, struct file *f)
159 {
160 int res;
161
162 if (unlikely(0 > atomic_dec_if_positive(&opencnt)))
163 return -EBUSY;
164
165 expect_close = 0;
166 if (locked) {
167 module_put(THIS_MODULE);
168 free_irq(wd_irq, &miscdev);
169 locked = 0;
170 }
171
172 res = request_irq(wd_irq, wdt_gpi_irqhdl, SA_SHIRQ | SA_INTERRUPT,
173 wdt_gpi_name, &miscdev);
174 if (unlikely(res))
175 return res;
176
177 wdt_gpi_set_timeout(timeout);
178 wdt_gpi_start();
179
180 printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
181 wdt_gpi_name, timeout);
182 return 0;
183 }
184
185 static int wdt_gpi_release(struct inode *i, struct file *f)
186 {
187 if (nowayout) {
188 printk(KERN_NOTICE "%s: no way out - watchdog left running\n",
189 wdt_gpi_name);
190 __module_get(THIS_MODULE);
191 locked = 1;
192 } else {
193 if (expect_close) {
194 wdt_gpi_stop();
195 free_irq(wd_irq, &miscdev);
196 printk(KERN_INFO "%s: watchdog stopped\n", wdt_gpi_name);
197 } else {
198 printk(KERN_NOTICE "%s: unexpected close() -"
199 " watchdog left running\n",
200 wdt_gpi_name);
201 wdt_gpi_set_timeout(timeout);
202 __module_get(THIS_MODULE);
203 locked = 1;
204 }
205 }
206
207 atomic_inc(&opencnt);
208 return 0;
209 }
210
211 static ssize_t
212 wdt_gpi_write(struct file *f, const char __user *d, size_t s, loff_t *o)
213 {
214 char val;
215
216 wdt_gpi_set_timeout(timeout);
217 expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
218 return s ? 1 : 0;
219 }
220
221 static long
222 wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
223 {
224 long res = -ENOTTY;
225 const long size = _IOC_SIZE(cmd);
226 int stat;
227 static struct watchdog_info wdinfo = {
228 .identity = "RM9xxx/GPI watchdog",
229 .firmware_version = 0,
230 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
231 };
232
233 if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
234 return -ENOTTY;
235
236 if ((_IOC_DIR(cmd) & _IOC_READ)
237 && !access_ok(VERIFY_WRITE, arg, size))
238 return -EFAULT;
239
240 if ((_IOC_DIR(cmd) & _IOC_WRITE)
241 && !access_ok(VERIFY_READ, arg, size))
242 return -EFAULT;
243
244 expect_close = 0;
245
246 switch (cmd) {
247 case WDIOC_GETSUPPORT:
248 wdinfo.options = nowayout ?
249 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
250 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE;
251 res = __copy_to_user((void __user *)arg, &wdinfo, size) ?
252 -EFAULT : size;
253 break;
254
255 case WDIOC_GETSTATUS:
256 break;
257
258 case WDIOC_GETBOOTSTATUS:
259 stat = (*(volatile char *) flagaddr & 0x01)
260 ? WDIOF_CARDRESET : 0;
261 res = __copy_to_user((void __user *)arg, &stat, size) ?
262 -EFAULT : size;
263 break;
264
265 case WDIOC_SETOPTIONS:
266 break;
267
268 case WDIOC_KEEPALIVE:
269 wdt_gpi_set_timeout(timeout);
270 res = size;
271 break;
272
273 case WDIOC_SETTIMEOUT:
274 {
275 int val;
276 if (unlikely(__copy_from_user(&val, (const void __user *) arg,
277 size))) {
278 res = -EFAULT;
279 break;
280 }
281
282 if (val > 32)
283 val = 32;
284 timeout = val;
285 wdt_gpi_set_timeout(val);
286 res = size;
287 printk("%s: timeout set to %u seconds\n",
288 wdt_gpi_name, timeout);
289 }
290 break;
291
292 case WDIOC_GETTIMEOUT:
293 res = __copy_to_user((void __user *) arg, &timeout, size) ?
294 -EFAULT : size;
295 break;
296 }
297
298 return res;
299 }
300
301
302 /* Shutdown notifier*/
303 static int
304 wdt_gpi_notify(struct notifier_block *this, unsigned long code, void *unused)
305 {
306 if (code == SYS_DOWN || code == SYS_HALT)
307 wdt_gpi_stop();
308
309 return NOTIFY_DONE;
310 }
311
312
313 /* Kernel interfaces */
314 static struct file_operations fops = {
315 .owner = THIS_MODULE,
316 .open = wdt_gpi_open,
317 .release = wdt_gpi_release,
318 .write = wdt_gpi_write,
319 .unlocked_ioctl = wdt_gpi_ioctl,
320 };
321
322 static struct miscdevice miscdev = {
323 .minor = WATCHDOG_MINOR,
324 .name = wdt_gpi_name,
325 .fops = &fops,
326 };
327
328 static struct notifier_block wdt_gpi_shutdown = {
329 .notifier_call = wdt_gpi_notify,
330 };
331
332
333 /* Init & exit procedures */
334 static const struct resource *
335 wdt_gpi_get_resource(struct platform_device *pdv, const char *name,
336 unsigned int type)
337 {
338 char buf[80];
339 if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
340 return NULL;
341 return platform_get_resource_byname(pdv, type, buf);
342 }
343
344 /* No hotplugging on the platform bus - use __init */
345 static int __init wdt_gpi_probe(struct device *dev)
346 {
347 int res;
348 struct platform_device * const pdv = to_platform_device(dev);
349 const struct resource
350 * const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
351 IORESOURCE_MEM),
352 * const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
353 IORESOURCE_IRQ),
354 * const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
355 0);
356
357 if (unlikely(!rr || !ri || !rc))
358 return -ENXIO;
359
360 wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
361 if (unlikely(!wd_regs))
362 return -ENOMEM;
363 wd_irq = ri->start;
364 wd_ctr = rc->start;
365 res = misc_register(&miscdev);
366 if (res)
367 iounmap(wd_regs);
368 else
369 register_reboot_notifier(&wdt_gpi_shutdown);
370 return res;
371 }
372
373 static int __exit wdt_gpi_remove(struct device *dev)
374 {
375 int res;
376
377 unregister_reboot_notifier(&wdt_gpi_shutdown);
378 res = misc_deregister(&miscdev);
379 iounmap(wd_regs);
380 wd_regs = NULL;
381 return res;
382 }
383
384
385 /* Device driver init & exit */
386 static struct device_driver wdt_gpi_driver = {
387 .name = (char *) wdt_gpi_name,
388 .bus = &platform_bus_type,
389 .owner = THIS_MODULE,
390 .probe = wdt_gpi_probe,
391 .remove = __exit_p(wdt_gpi_remove),
392 .shutdown = NULL,
393 .suspend = NULL,
394 .resume = NULL,
395 };
396
397 static int __init wdt_gpi_init_module(void)
398 {
399 atomic_set(&opencnt, 1);
400 if (timeout > MAX_TIMEOUT_SECONDS)
401 timeout = MAX_TIMEOUT_SECONDS;
402 return driver_register(&wdt_gpi_driver);
403 }
404
405 static void __exit wdt_gpi_cleanup_module(void)
406 {
407 driver_unregister(&wdt_gpi_driver);
408 }
409
410 module_init(wdt_gpi_init_module);
411 module_exit(wdt_gpi_cleanup_module);
412
413 MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
414 MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
415 MODULE_VERSION("0.1");
416 MODULE_LICENSE("GPL");
417 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
418
This page took 0.051827 seconds and 4 git commands to generate.