sync hh.org
[hh.org.git] / drivers / char / watchdog / rm9k_wdt.c
blobec3909371c217463c15437d8973857e57c492900
1 /*
2 * Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
3 * chips.
5 * Copyright (C) 2004 by Basler Vision Technologies AG
6 * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
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.
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.
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
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/notifier.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
32 #include <asm/io.h>
33 #include <asm/atomic.h>
34 #include <asm/processor.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/rm9k-ocd.h>
39 #include <rm9k_wdt.h>
42 #define CLOCK 125000000
43 #define MAX_TIMEOUT_SECONDS 32
44 #define CPCCR 0x0080
45 #define CPGIG1SR 0x0044
46 #define CPGIG1ER 0x0054
49 /* Function prototypes */
50 static irqreturn_t wdt_gpi_irqhdl(int, void *, struct pt_regs *);
51 static void wdt_gpi_start(void);
52 static void wdt_gpi_stop(void);
53 static void wdt_gpi_set_timeout(unsigned int);
54 static int wdt_gpi_open(struct inode *, struct file *);
55 static int wdt_gpi_release(struct inode *, struct file *);
56 static ssize_t wdt_gpi_write(struct file *, const char __user *, size_t, loff_t *);
57 static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
58 static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
59 static const struct resource *wdt_gpi_get_resource(struct platform_device *, const char *, unsigned int);
60 static int __init wdt_gpi_probe(struct device *);
61 static int __exit wdt_gpi_remove(struct device *);
64 static const char wdt_gpi_name[] = "wdt_gpi";
65 static atomic_t opencnt;
66 static int expect_close;
67 static int locked;
70 /* These are set from device resources */
71 static void __iomem * wd_regs;
72 static unsigned int wd_irq, wd_ctr;
75 /* Module arguments */
76 static int timeout = MAX_TIMEOUT_SECONDS;
77 module_param(timeout, int, 0444);
78 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
80 static unsigned long resetaddr = 0xbffdc200;
81 module_param(resetaddr, ulong, 0444);
82 MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");
84 static unsigned long flagaddr = 0xbffdc104;
85 module_param(flagaddr, ulong, 0444);
86 MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");
88 static int powercycle;
89 module_param(powercycle, bool, 0444);
90 MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
92 static int nowayout = WATCHDOG_NOWAYOUT;
93 module_param(nowayout, bool, 0444);
94 MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
97 /* Interrupt handler */
98 static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt, struct pt_regs *regs)
100 if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
101 return IRQ_NONE;
102 __raw_writel(0x1, wd_regs + 0x0008);
105 printk(KERN_CRIT "%s: watchdog expired - resetting system\n",
106 wdt_gpi_name);
108 *(volatile char *) flagaddr |= 0x01;
109 *(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
110 iob();
111 while (1)
112 cpu_relax();
116 /* Watchdog functions */
117 static void wdt_gpi_start(void)
119 u32 reg;
121 lock_titan_regs();
122 reg = titan_readl(CPGIG1ER);
123 titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
124 iob();
125 unlock_titan_regs();
128 static void wdt_gpi_stop(void)
130 u32 reg;
132 lock_titan_regs();
133 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
134 titan_writel(reg, CPCCR);
135 reg = titan_readl(CPGIG1ER);
136 titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
137 iob();
138 unlock_titan_regs();
141 static void wdt_gpi_set_timeout(unsigned int to)
143 u32 reg;
144 const u32 wdval = (to * CLOCK) & ~0x0000000f;
146 lock_titan_regs();
147 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
148 titan_writel(reg, CPCCR);
149 wmb();
150 __raw_writel(wdval, wd_regs + 0x0000);
151 wmb();
152 titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
153 wmb();
154 titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
155 iob();
156 unlock_titan_regs();
160 /* /dev/watchdog operations */
161 static int wdt_gpi_open(struct inode *inode, struct file *file)
163 int res;
165 if (unlikely(atomic_dec_if_positive(&opencnt) < 0))
166 return -EBUSY;
168 expect_close = 0;
169 if (locked) {
170 module_put(THIS_MODULE);
171 free_irq(wd_irq, &miscdev);
172 locked = 0;
175 res = request_irq(wd_irq, wdt_gpi_irqhdl, SA_SHIRQ | SA_INTERRUPT,
176 wdt_gpi_name, &miscdev);
177 if (unlikely(res))
178 return res;
180 wdt_gpi_set_timeout(timeout);
181 wdt_gpi_start();
183 printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
184 wdt_gpi_name, timeout);
185 return nonseekable_open(inode, file);
188 static int wdt_gpi_release(struct inode *inode, struct file *file)
190 if (nowayout) {
191 printk(KERN_INFO "%s: no way out - watchdog left running\n",
192 wdt_gpi_name);
193 __module_get(THIS_MODULE);
194 locked = 1;
195 } else {
196 if (expect_close) {
197 wdt_gpi_stop();
198 free_irq(wd_irq, &miscdev);
199 printk(KERN_INFO "%s: watchdog stopped\n", wdt_gpi_name);
200 } else {
201 printk(KERN_CRIT "%s: unexpected close() -"
202 " watchdog left running\n",
203 wdt_gpi_name);
204 wdt_gpi_set_timeout(timeout);
205 __module_get(THIS_MODULE);
206 locked = 1;
210 atomic_inc(&opencnt);
211 return 0;
214 static ssize_t
215 wdt_gpi_write(struct file *f, const char __user *d, size_t s, loff_t *o)
217 char val;
219 wdt_gpi_set_timeout(timeout);
220 expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
221 return s ? 1 : 0;
224 static long
225 wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
227 long res = -ENOTTY;
228 const long size = _IOC_SIZE(cmd);
229 int stat;
230 void __user *argp = (void __user *)arg;
231 static struct watchdog_info wdinfo = {
232 .identity = "RM9xxx/GPI watchdog",
233 .firmware_version = 0,
234 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
237 if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
238 return -ENOTTY;
240 if ((_IOC_DIR(cmd) & _IOC_READ)
241 && !access_ok(VERIFY_WRITE, arg, size))
242 return -EFAULT;
244 if ((_IOC_DIR(cmd) & _IOC_WRITE)
245 && !access_ok(VERIFY_READ, arg, size))
246 return -EFAULT;
248 expect_close = 0;
250 switch (cmd) {
251 case WDIOC_GETSUPPORT:
252 wdinfo.options = nowayout ?
253 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
254 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE;
255 res = __copy_to_user(argp, &wdinfo, size) ? -EFAULT : size;
256 break;
258 case WDIOC_GETSTATUS:
259 break;
261 case WDIOC_GETBOOTSTATUS:
262 stat = (*(volatile char *) flagaddr & 0x01)
263 ? WDIOF_CARDRESET : 0;
264 res = __copy_to_user(argp, &stat, size) ?
265 -EFAULT : size;
266 break;
268 case WDIOC_SETOPTIONS:
269 break;
271 case WDIOC_KEEPALIVE:
272 wdt_gpi_set_timeout(timeout);
273 res = size;
274 break;
276 case WDIOC_SETTIMEOUT:
278 int val;
279 if (unlikely(__copy_from_user(&val, argp, size))) {
280 res = -EFAULT;
281 break;
284 if (val > MAX_TIMEOUT_SECONDS)
285 val = MAX_TIMEOUT_SECONDS;
286 timeout = val;
287 wdt_gpi_set_timeout(val);
288 res = size;
289 printk(KERN_INFO "%s: timeout set to %u seconds\n",
290 wdt_gpi_name, timeout);
292 break;
294 case WDIOC_GETTIMEOUT:
295 res = __copy_to_user(argp, &timeout, size) ?
296 -EFAULT : size;
297 break;
300 return res;
304 /* Shutdown notifier */
305 static int
306 wdt_gpi_notify(struct notifier_block *this, unsigned long code, void *unused)
308 if (code == SYS_DOWN || code == SYS_HALT)
309 wdt_gpi_stop();
311 return NOTIFY_DONE;
315 /* Kernel interfaces */
316 static struct file_operations fops = {
317 .owner = THIS_MODULE,
318 .open = wdt_gpi_open,
319 .release = wdt_gpi_release,
320 .write = wdt_gpi_write,
321 .unlocked_ioctl = wdt_gpi_ioctl,
324 static struct miscdevice miscdev = {
325 .minor = WATCHDOG_MINOR,
326 .name = wdt_gpi_name,
327 .fops = &fops,
330 static struct notifier_block wdt_gpi_shutdown = {
331 .notifier_call = wdt_gpi_notify,
335 /* Init & exit procedures */
336 static const struct resource *
337 wdt_gpi_get_resource(struct platform_device *pdv, const char *name,
338 unsigned int type)
340 char buf[80];
341 if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
342 return NULL;
343 return platform_get_resource_byname(pdv, type, buf);
346 /* No hotplugging on the platform bus - use __init */
347 static int __init wdt_gpi_probe(struct device *dev)
349 int res;
350 struct platform_device * const pdv = to_platform_device(dev);
351 const struct resource
352 * const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
353 IORESOURCE_MEM),
354 * const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
355 IORESOURCE_IRQ),
356 * const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
359 if (unlikely(!rr || !ri || !rc))
360 return -ENXIO;
362 wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
363 if (unlikely(!wd_regs))
364 return -ENOMEM;
365 wd_irq = ri->start;
366 wd_ctr = rc->start;
367 res = misc_register(&miscdev);
368 if (res)
369 iounmap(wd_regs);
370 else
371 register_reboot_notifier(&wdt_gpi_shutdown);
372 return res;
375 static int __exit wdt_gpi_remove(struct device *dev)
377 int res;
379 unregister_reboot_notifier(&wdt_gpi_shutdown);
380 res = misc_deregister(&miscdev);
381 iounmap(wd_regs);
382 wd_regs = NULL;
383 return res;
387 /* Device driver init & exit */
388 static struct device_driver wdt_gpi_driver = {
389 .name = (char *) wdt_gpi_name,
390 .bus = &platform_bus_type,
391 .owner = THIS_MODULE,
392 .probe = wdt_gpi_probe,
393 .remove = __exit_p(wdt_gpi_remove),
394 .shutdown = NULL,
395 .suspend = NULL,
396 .resume = NULL,
399 static int __init wdt_gpi_init_module(void)
401 atomic_set(&opencnt, 1);
402 if (timeout > MAX_TIMEOUT_SECONDS)
403 timeout = MAX_TIMEOUT_SECONDS;
404 return driver_register(&wdt_gpi_driver);
407 static void __exit wdt_gpi_cleanup_module(void)
409 driver_unregister(&wdt_gpi_driver);
412 module_init(wdt_gpi_init_module);
413 module_exit(wdt_gpi_cleanup_module);
415 MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
416 MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
417 MODULE_VERSION("0.1");
418 MODULE_LICENSE("GPL");
419 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);