2 * PIKA FPGA based Watchdog Timer
4 * Copyright (c) 2008 PIKA Technologies
5 * Sean MacLennan <smaclennan@pikatech.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/watchdog.h>
19 #include <linux/reboot.h>
20 #include <linux/jiffies.h>
21 #include <linux/timer.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
25 #include <linux/of_address.h>
26 #include <linux/of_platform.h>
28 #define DRV_NAME "PIKA-WDT"
30 /* Hardware timeout in seconds */
31 #define WDT_HW_TIMEOUT 2
33 /* Timer heartbeat (500ms) */
34 #define WDT_TIMEOUT (HZ/2)
36 /* User land timeout */
37 #define WDT_HEARTBEAT 15
38 static int heartbeat = WDT_HEARTBEAT;
39 module_param(heartbeat, int, 0);
40 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
41 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
43 static bool nowayout = WATCHDOG_NOWAYOUT;
44 module_param(nowayout, bool, 0);
45 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
46 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
50 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
54 struct timer_list timer; /* The timer that pings the watchdog */
57 static struct watchdog_info ident = {
59 .options = WDIOF_CARDRESET |
66 * Reload the watchdog timer. (ie, pat the watchdog)
68 static inline void pikawdt_reset(void)
70 /* -- FPGA: Reset Control Register (32bit R/W) (Offset: 0x14) --
71 * Bit 7, WTCHDG_EN: When set to 1, the watchdog timer is enabled.
72 * Once enabled, it cannot be disabled. The watchdog can be
73 * kicked by performing any write access to the reset
74 * control register (this register).
75 * Bit 8-11, WTCHDG_TIMEOUT_SEC: Sets the watchdog timeout value in
76 * seconds. Valid ranges are 1 to 15 seconds. The value can
77 * be modified dynamically.
79 unsigned reset = in_be32(pikawdt_private.fpga + 0x14);
80 /* enable with max timeout - 15 seconds */
81 reset |= (1 << 7) + (WDT_HW_TIMEOUT << 8);
82 out_be32(pikawdt_private.fpga + 0x14, reset);
88 static void pikawdt_ping(unsigned long data)
90 if (time_before(jiffies, pikawdt_private.next_heartbeat) ||
91 (!nowayout && !pikawdt_private.open)) {
93 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
95 pr_crit("I will reset your machine !\n");
99 static void pikawdt_keepalive(void)
101 pikawdt_private.next_heartbeat = jiffies + heartbeat * HZ;
104 static void pikawdt_start(void)
107 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
111 * Watchdog device is opened, and watchdog starts running.
113 static int pikawdt_open(struct inode *inode, struct file *file)
115 /* /dev/watchdog can only be opened once */
116 if (test_and_set_bit(0, &pikawdt_private.open))
121 return nonseekable_open(inode, file);
125 * Close the watchdog device.
127 static int pikawdt_release(struct inode *inode, struct file *file)
129 /* stop internal ping */
130 if (!pikawdt_private.expect_close)
131 del_timer(&pikawdt_private.timer);
133 clear_bit(0, &pikawdt_private.open);
134 pikawdt_private.expect_close = 0;
139 * Pat the watchdog whenever device is written to.
141 static ssize_t pikawdt_write(struct file *file, const char __user *data,
142 size_t len, loff_t *ppos)
147 /* Scan for magic character */
151 pikawdt_private.expect_close = 0;
153 for (i = 0; i < len; i++) {
155 if (get_user(c, data + i))
158 pikawdt_private.expect_close = 42;
170 * Handle commands from user-space.
172 static long pikawdt_ioctl(struct file *file,
173 unsigned int cmd, unsigned long arg)
175 void __user *argp = (void __user *)arg;
176 int __user *p = argp;
180 case WDIOC_GETSUPPORT:
181 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
183 case WDIOC_GETSTATUS:
184 return put_user(0, p);
186 case WDIOC_GETBOOTSTATUS:
187 return put_user(pikawdt_private.bootstatus, p);
189 case WDIOC_KEEPALIVE:
193 case WDIOC_SETTIMEOUT:
194 if (get_user(new_value, p))
197 heartbeat = new_value;
200 return put_user(new_value, p); /* return current value */
202 case WDIOC_GETTIMEOUT:
203 return put_user(heartbeat, p);
209 static const struct file_operations pikawdt_fops = {
210 .owner = THIS_MODULE,
212 .open = pikawdt_open,
213 .release = pikawdt_release,
214 .write = pikawdt_write,
215 .unlocked_ioctl = pikawdt_ioctl,
218 static struct miscdevice pikawdt_miscdev = {
219 .minor = WATCHDOG_MINOR,
221 .fops = &pikawdt_fops,
224 static int __init pikawdt_init(void)
226 struct device_node *np;
231 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
233 pr_err("Unable to find fpga\n");
237 pikawdt_private.fpga = of_iomap(np, 0);
239 if (pikawdt_private.fpga == NULL) {
240 pr_err("Unable to map fpga\n");
244 ident.firmware_version = in_be32(pikawdt_private.fpga + 0x1c) & 0xffff;
246 /* POST information is in the sd area. */
247 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
249 pr_err("Unable to find fpga-sd\n");
254 fpga = of_iomap(np, 0);
257 pr_err("Unable to map fpga-sd\n");
262 /* -- FPGA: POST Test Results Register 1 (32bit R/W) (Offset: 0x4040) --
263 * Bit 31, WDOG: Set to 1 when the last reset was caused by a watchdog
266 post1 = in_be32(fpga + 0x40);
267 if (post1 & 0x80000000)
268 pikawdt_private.bootstatus = WDIOF_CARDRESET;
272 setup_timer(&pikawdt_private.timer, pikawdt_ping, 0);
274 ret = misc_register(&pikawdt_miscdev);
276 pr_err("Unable to register miscdev\n");
280 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
281 heartbeat, nowayout);
285 iounmap(pikawdt_private.fpga);
289 static void __exit pikawdt_exit(void)
291 misc_deregister(&pikawdt_miscdev);
293 iounmap(pikawdt_private.fpga);
296 module_init(pikawdt_init);
297 module_exit(pikawdt_exit);
299 MODULE_AUTHOR("Sean MacLennan <smaclennan@pikatech.com>");
300 MODULE_DESCRIPTION("PIKA FPGA based Watchdog Timer");
301 MODULE_LICENSE("GPL");