2 * Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
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>
28 #include <linux/reboot.h>
29 #include <linux/notifier.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
33 #include <linux/uaccess.h>
34 #include <asm/atomic.h>
35 #include <asm/processor.h>
36 #include <asm/system.h>
37 #include <asm/rm9k-ocd.h>
42 #define CLOCK 125000000
43 #define MAX_TIMEOUT_SECONDS 32
45 #define CPGIG1SR 0x0044
46 #define CPGIG1ER 0x0054
49 /* Function prototypes */
50 static irqreturn_t wdt_gpi_irqhdl(int, void *);
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,
58 static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
59 static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
60 static const struct resource *wdt_gpi_get_resource(struct platform_device *,
61 const char *, unsigned int);
62 static int __init wdt_gpi_probe(struct device *);
63 static int __exit wdt_gpi_remove(struct device *);
66 static const char wdt_gpi_name[] = "wdt_gpi";
67 static atomic_t opencnt;
68 static int expect_close;
72 /* These are set from device resources */
73 static void __iomem *wd_regs;
74 static unsigned int wd_irq, wd_ctr;
77 /* Module arguments */
78 static int timeout = MAX_TIMEOUT_SECONDS;
79 module_param(timeout, int, 0444);
80 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
82 static unsigned long resetaddr = 0xbffdc200;
83 module_param(resetaddr, ulong, 0444);
84 MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");
86 static unsigned long flagaddr = 0xbffdc104;
87 module_param(flagaddr, ulong, 0444);
88 MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");
90 static int powercycle;
91 module_param(powercycle, bool, 0444);
92 MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
94 static int nowayout = WATCHDOG_NOWAYOUT;
95 module_param(nowayout, bool, 0444);
96 MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
99 /* Kernel interfaces */
100 static const struct file_operations fops = {
101 .owner = THIS_MODULE,
102 .open = wdt_gpi_open,
103 .release = wdt_gpi_release,
104 .write = wdt_gpi_write,
105 .unlocked_ioctl = wdt_gpi_ioctl,
108 static struct miscdevice miscdev = {
109 .minor = WATCHDOG_MINOR,
110 .name = wdt_gpi_name,
114 static struct notifier_block wdt_gpi_shutdown = {
115 .notifier_call = wdt_gpi_notify,
119 /* Interrupt handler */
120 static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt)
122 if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
124 __raw_writel(0x1, wd_regs + 0x0008);
127 printk(KERN_CRIT "%s: watchdog expired - resetting system\n",
130 *(volatile char *) flagaddr |= 0x01;
131 *(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
138 /* Watchdog functions */
139 static void wdt_gpi_start(void)
144 reg = titan_readl(CPGIG1ER);
145 titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
150 static void wdt_gpi_stop(void)
155 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
156 titan_writel(reg, CPCCR);
157 reg = titan_readl(CPGIG1ER);
158 titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
163 static void wdt_gpi_set_timeout(unsigned int to)
166 const u32 wdval = (to * CLOCK) & ~0x0000000f;
169 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
170 titan_writel(reg, CPCCR);
172 __raw_writel(wdval, wd_regs + 0x0000);
174 titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
176 titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
182 /* /dev/watchdog operations */
183 static int wdt_gpi_open(struct inode *inode, struct file *file)
187 if (unlikely(atomic_dec_if_positive(&opencnt) < 0))
192 module_put(THIS_MODULE);
193 free_irq(wd_irq, &miscdev);
197 res = request_irq(wd_irq, wdt_gpi_irqhdl, IRQF_SHARED | IRQF_DISABLED,
198 wdt_gpi_name, &miscdev);
202 wdt_gpi_set_timeout(timeout);
205 printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
206 wdt_gpi_name, timeout);
207 return nonseekable_open(inode, file);
210 static int wdt_gpi_release(struct inode *inode, struct file *file)
213 printk(KERN_INFO "%s: no way out - watchdog left running\n",
215 __module_get(THIS_MODULE);
220 free_irq(wd_irq, &miscdev);
221 printk(KERN_INFO "%s: watchdog stopped\n",
224 printk(KERN_CRIT "%s: unexpected close() -"
225 " watchdog left running\n",
227 wdt_gpi_set_timeout(timeout);
228 __module_get(THIS_MODULE);
233 atomic_inc(&opencnt);
237 static ssize_t wdt_gpi_write(struct file *f, const char __user *d, size_t s,
242 wdt_gpi_set_timeout(timeout);
243 expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
247 static long wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
250 const long size = _IOC_SIZE(cmd);
252 void __user *argp = (void __user *)arg;
253 static struct watchdog_info wdinfo = {
254 .identity = "RM9xxx/GPI watchdog",
255 .firmware_version = 0,
256 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
259 if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
262 if ((_IOC_DIR(cmd) & _IOC_READ)
263 && !access_ok(VERIFY_WRITE, arg, size))
266 if ((_IOC_DIR(cmd) & _IOC_WRITE)
267 && !access_ok(VERIFY_READ, arg, size))
273 case WDIOC_GETSUPPORT:
274 wdinfo.options = nowayout ?
275 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
276 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
278 res = __copy_to_user(argp, &wdinfo, size) ? -EFAULT : size;
281 case WDIOC_GETSTATUS:
284 case WDIOC_GETBOOTSTATUS:
285 stat = (*(volatile char *) flagaddr & 0x01)
286 ? WDIOF_CARDRESET : 0;
287 res = __copy_to_user(argp, &stat, size) ?
291 case WDIOC_SETOPTIONS:
294 case WDIOC_KEEPALIVE:
295 wdt_gpi_set_timeout(timeout);
299 case WDIOC_SETTIMEOUT:
302 if (unlikely(__copy_from_user(&val, argp, size))) {
307 if (val > MAX_TIMEOUT_SECONDS)
308 val = MAX_TIMEOUT_SECONDS;
310 wdt_gpi_set_timeout(val);
312 printk(KERN_INFO "%s: timeout set to %u seconds\n",
313 wdt_gpi_name, timeout);
317 case WDIOC_GETTIMEOUT:
318 res = __copy_to_user(argp, &timeout, size) ?
327 /* Shutdown notifier */
328 static int wdt_gpi_notify(struct notifier_block *this, unsigned long code,
331 if (code == SYS_DOWN || code == SYS_HALT)
338 /* Init & exit procedures */
339 static const struct resource *wdt_gpi_get_resource(struct platform_device *pdv,
340 const char *name, unsigned int type)
343 if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
345 return platform_get_resource_byname(pdv, type, buf);
348 /* No hotplugging on the platform bus - use __init */
349 static int __init wdt_gpi_probe(struct device *dev)
352 struct platform_device * const pdv = to_platform_device(dev);
353 const struct resource
354 * const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
356 * const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
358 * const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
361 if (unlikely(!rr || !ri || !rc))
364 wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
365 if (unlikely(!wd_regs))
369 res = misc_register(&miscdev);
373 register_reboot_notifier(&wdt_gpi_shutdown);
377 static int __exit wdt_gpi_remove(struct device *dev)
381 unregister_reboot_notifier(&wdt_gpi_shutdown);
382 res = misc_deregister(&miscdev);
389 /* Device driver init & exit */
390 static struct device_driver wdt_gpi_driver = {
391 .name = (char *) wdt_gpi_name,
392 .bus = &platform_bus_type,
393 .owner = THIS_MODULE,
394 .probe = wdt_gpi_probe,
395 .remove = __exit_p(wdt_gpi_remove),
401 static int __init wdt_gpi_init_module(void)
403 atomic_set(&opencnt, 1);
404 if (timeout > MAX_TIMEOUT_SECONDS)
405 timeout = MAX_TIMEOUT_SECONDS;
406 return driver_register(&wdt_gpi_driver);
409 static void __exit wdt_gpi_cleanup_module(void)
411 driver_unregister(&wdt_gpi_driver);
414 module_init(wdt_gpi_init_module);
415 module_exit(wdt_gpi_cleanup_module);
417 MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
418 MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
419 MODULE_VERSION("0.1");
420 MODULE_LICENSE("GPL");
421 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);