2 * PIKA FPGA based Watchdog Timer
4 * Copyright (c) 2008 PIKA Technologies
5 * Sean MacLennan <smaclennan@pikatech.com>
8 #include <linux/init.h>
9 #include <linux/errno.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
15 #include <linux/miscdevice.h>
16 #include <linux/watchdog.h>
17 #include <linux/reboot.h>
18 #include <linux/jiffies.h>
19 #include <linux/timer.h>
20 #include <linux/bitops.h>
21 #include <linux/uaccess.h>
23 #include <linux/of_platform.h>
25 #define DRV_NAME "PIKA-WDT"
26 #define PFX DRV_NAME ": "
28 /* Hardware timeout in seconds */
29 #define WDT_HW_TIMEOUT 2
31 /* Timer heartbeat (500ms) */
32 #define WDT_TIMEOUT (HZ/2)
34 /* User land timeout */
35 #define WDT_HEARTBEAT 15
36 static int heartbeat = WDT_HEARTBEAT;
37 module_param(heartbeat, int, 0);
38 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
39 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
41 static int nowayout = WATCHDOG_NOWAYOUT;
42 module_param(nowayout, int, 0);
43 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
44 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
48 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
52 struct timer_list timer; /* The timer that pings the watchdog */
55 static struct watchdog_info ident = {
57 .options = WDIOF_CARDRESET |
64 * Reload the watchdog timer. (ie, pat the watchdog)
66 static inline void pikawdt_reset(void)
68 /* -- FPGA: Reset Control Register (32bit R/W) (Offset: 0x14) --
69 * Bit 7, WTCHDG_EN: When set to 1, the watchdog timer is enabled.
70 * Once enabled, it cannot be disabled. The watchdog can be
71 * kicked by performing any write access to the reset
72 * control register (this register).
73 * Bit 8-11, WTCHDG_TIMEOUT_SEC: Sets the watchdog timeout value in
74 * seconds. Valid ranges are 1 to 15 seconds. The value can
75 * be modified dynamically.
77 unsigned reset = in_be32(pikawdt_private.fpga + 0x14);
78 /* enable with max timeout - 15 seconds */
79 reset |= (1 << 7) + (WDT_HW_TIMEOUT << 8);
80 out_be32(pikawdt_private.fpga + 0x14, reset);
86 static void pikawdt_ping(unsigned long data)
88 if (time_before(jiffies, pikawdt_private.next_heartbeat) ||
89 (!nowayout && !pikawdt_private.open)) {
91 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
93 printk(KERN_CRIT PFX "I will reset your machine !\n");
97 static void pikawdt_keepalive(void)
99 pikawdt_private.next_heartbeat = jiffies + heartbeat * HZ;
102 static void pikawdt_start(void)
105 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
109 * Watchdog device is opened, and watchdog starts running.
111 static int pikawdt_open(struct inode *inode, struct file *file)
113 /* /dev/watchdog can only be opened once */
114 if (test_and_set_bit(0, &pikawdt_private.open))
119 return nonseekable_open(inode, file);
123 * Close the watchdog device.
125 static int pikawdt_release(struct inode *inode, struct file *file)
127 /* stop internal ping */
128 if (!pikawdt_private.expect_close)
129 del_timer(&pikawdt_private.timer);
131 clear_bit(0, &pikawdt_private.open);
132 pikawdt_private.expect_close = 0;
137 * Pat the watchdog whenever device is written to.
139 static ssize_t pikawdt_write(struct file *file, const char __user *data,
140 size_t len, loff_t *ppos)
145 /* Scan for magic character */
149 pikawdt_private.expect_close = 0;
151 for (i = 0; i < len; i++) {
153 if (get_user(c, data + i))
156 pikawdt_private.expect_close = 42;
168 * Handle commands from user-space.
170 static long pikawdt_ioctl(struct file *file,
171 unsigned int cmd, unsigned long arg)
173 void __user *argp = (void __user *)arg;
174 int __user *p = argp;
178 case WDIOC_GETSUPPORT:
179 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
181 case WDIOC_GETSTATUS:
182 return put_user(0, p);
184 case WDIOC_GETBOOTSTATUS:
185 return put_user(pikawdt_private.bootstatus, p);
187 case WDIOC_KEEPALIVE:
191 case WDIOC_SETTIMEOUT:
192 if (get_user(new_value, p))
195 heartbeat = new_value;
198 return put_user(new_value, p); /* return current value */
200 case WDIOC_GETTIMEOUT:
201 return put_user(heartbeat, p);
207 static const struct file_operations pikawdt_fops = {
208 .owner = THIS_MODULE,
210 .open = pikawdt_open,
211 .release = pikawdt_release,
212 .write = pikawdt_write,
213 .unlocked_ioctl = pikawdt_ioctl,
216 static struct miscdevice pikawdt_miscdev = {
217 .minor = WATCHDOG_MINOR,
219 .fops = &pikawdt_fops,
222 static int __init pikawdt_init(void)
224 struct device_node *np;
229 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
231 printk(KERN_ERR PFX "Unable to find fpga.\n");
235 pikawdt_private.fpga = of_iomap(np, 0);
237 if (pikawdt_private.fpga == NULL) {
238 printk(KERN_ERR PFX "Unable to map fpga.\n");
242 ident.firmware_version = in_be32(pikawdt_private.fpga + 0x1c) & 0xffff;
244 /* POST information is in the sd area. */
245 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
247 printk(KERN_ERR PFX "Unable to find fpga-sd.\n");
252 fpga = of_iomap(np, 0);
255 printk(KERN_ERR PFX "Unable to map fpga-sd.\n");
260 /* -- FPGA: POST Test Results Register 1 (32bit R/W) (Offset: 0x4040) --
261 * Bit 31, WDOG: Set to 1 when the last reset was caused by a watchdog
264 post1 = in_be32(fpga + 0x40);
265 if (post1 & 0x80000000)
266 pikawdt_private.bootstatus = WDIOF_CARDRESET;
270 setup_timer(&pikawdt_private.timer, pikawdt_ping, 0);
272 ret = misc_register(&pikawdt_miscdev);
274 printk(KERN_ERR PFX "Unable to register miscdev.\n");
278 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
279 heartbeat, nowayout);
283 iounmap(pikawdt_private.fpga);
287 static void __exit pikawdt_exit(void)
289 misc_deregister(&pikawdt_miscdev);
291 iounmap(pikawdt_private.fpga);
294 module_init(pikawdt_init);
295 module_exit(pikawdt_exit);
297 MODULE_AUTHOR("Sean MacLennan <smaclennan@pikatech.com>");
298 MODULE_DESCRIPTION("PIKA FPGA based Watchdog Timer");
299 MODULE_LICENSE("GPL");
300 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);