1 // SPDX-License-Identifier: GPL-2.0-only
3 * PIKA FPGA based Watchdog Timer
5 * Copyright (c) 2008 PIKA Technologies
6 * Sean MacLennan <smaclennan@pikatech.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/reboot.h>
21 #include <linux/jiffies.h>
22 #include <linux/timer.h>
23 #include <linux/bitops.h>
24 #include <linux/uaccess.h>
26 #include <linux/of_address.h>
27 #include <linux/of_platform.h>
29 #define DRV_NAME "PIKA-WDT"
31 /* Hardware timeout in seconds */
32 #define WDT_HW_TIMEOUT 2
34 /* Timer heartbeat (500ms) */
35 #define WDT_TIMEOUT (HZ/2)
37 /* User land timeout */
38 #define WDT_HEARTBEAT 15
39 static int heartbeat
= WDT_HEARTBEAT
;
40 module_param(heartbeat
, int, 0);
41 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeats in seconds. "
42 "(default = " __MODULE_STRING(WDT_HEARTBEAT
) ")");
44 static bool nowayout
= WATCHDOG_NOWAYOUT
;
45 module_param(nowayout
, bool, 0);
46 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
47 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
51 unsigned long next_heartbeat
; /* the next_heartbeat for the timer */
55 struct timer_list timer
; /* The timer that pings the watchdog */
58 static struct watchdog_info ident __ro_after_init
= {
60 .options
= WDIOF_CARDRESET
|
67 * Reload the watchdog timer. (ie, pat the watchdog)
69 static inline void pikawdt_reset(void)
71 /* -- FPGA: Reset Control Register (32bit R/W) (Offset: 0x14) --
72 * Bit 7, WTCHDG_EN: When set to 1, the watchdog timer is enabled.
73 * Once enabled, it cannot be disabled. The watchdog can be
74 * kicked by performing any write access to the reset
75 * control register (this register).
76 * Bit 8-11, WTCHDG_TIMEOUT_SEC: Sets the watchdog timeout value in
77 * seconds. Valid ranges are 1 to 15 seconds. The value can
78 * be modified dynamically.
80 unsigned reset
= in_be32(pikawdt_private
.fpga
+ 0x14);
81 /* enable with max timeout - 15 seconds */
82 reset
|= (1 << 7) + (WDT_HW_TIMEOUT
<< 8);
83 out_be32(pikawdt_private
.fpga
+ 0x14, reset
);
89 static void pikawdt_ping(struct timer_list
*unused
)
91 if (time_before(jiffies
, pikawdt_private
.next_heartbeat
) ||
92 (!nowayout
&& !pikawdt_private
.open
)) {
94 mod_timer(&pikawdt_private
.timer
, jiffies
+ WDT_TIMEOUT
);
96 pr_crit("I will reset your machine !\n");
100 static void pikawdt_keepalive(void)
102 pikawdt_private
.next_heartbeat
= jiffies
+ heartbeat
* HZ
;
105 static void pikawdt_start(void)
108 mod_timer(&pikawdt_private
.timer
, jiffies
+ WDT_TIMEOUT
);
112 * Watchdog device is opened, and watchdog starts running.
114 static int pikawdt_open(struct inode
*inode
, struct file
*file
)
116 /* /dev/watchdog can only be opened once */
117 if (test_and_set_bit(0, &pikawdt_private
.open
))
122 return stream_open(inode
, file
);
126 * Close the watchdog device.
128 static int pikawdt_release(struct inode
*inode
, struct file
*file
)
130 /* stop internal ping */
131 if (!pikawdt_private
.expect_close
)
132 del_timer(&pikawdt_private
.timer
);
134 clear_bit(0, &pikawdt_private
.open
);
135 pikawdt_private
.expect_close
= 0;
140 * Pat the watchdog whenever device is written to.
142 static ssize_t
pikawdt_write(struct file
*file
, const char __user
*data
,
143 size_t len
, loff_t
*ppos
)
148 /* Scan for magic character */
152 pikawdt_private
.expect_close
= 0;
154 for (i
= 0; i
< len
; i
++) {
156 if (get_user(c
, data
+ i
))
159 pikawdt_private
.expect_close
= 42;
171 * Handle commands from user-space.
173 static long pikawdt_ioctl(struct file
*file
,
174 unsigned int cmd
, unsigned long arg
)
176 void __user
*argp
= (void __user
*)arg
;
177 int __user
*p
= argp
;
181 case WDIOC_GETSUPPORT
:
182 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
184 case WDIOC_GETSTATUS
:
185 return put_user(0, p
);
187 case WDIOC_GETBOOTSTATUS
:
188 return put_user(pikawdt_private
.bootstatus
, p
);
190 case WDIOC_KEEPALIVE
:
194 case WDIOC_SETTIMEOUT
:
195 if (get_user(new_value
, p
))
198 heartbeat
= new_value
;
201 return put_user(new_value
, p
); /* return current value */
203 case WDIOC_GETTIMEOUT
:
204 return put_user(heartbeat
, p
);
210 static const struct file_operations pikawdt_fops
= {
211 .owner
= THIS_MODULE
,
213 .open
= pikawdt_open
,
214 .release
= pikawdt_release
,
215 .write
= pikawdt_write
,
216 .unlocked_ioctl
= pikawdt_ioctl
,
217 .compat_ioctl
= compat_ptr_ioctl
,
220 static struct miscdevice pikawdt_miscdev
= {
221 .minor
= WATCHDOG_MINOR
,
223 .fops
= &pikawdt_fops
,
226 static int __init
pikawdt_init(void)
228 struct device_node
*np
;
233 np
= of_find_compatible_node(NULL
, NULL
, "pika,fpga");
235 pr_err("Unable to find fpga\n");
239 pikawdt_private
.fpga
= of_iomap(np
, 0);
241 if (pikawdt_private
.fpga
== NULL
) {
242 pr_err("Unable to map fpga\n");
246 ident
.firmware_version
= in_be32(pikawdt_private
.fpga
+ 0x1c) & 0xffff;
248 /* POST information is in the sd area. */
249 np
= of_find_compatible_node(NULL
, NULL
, "pika,fpga-sd");
251 pr_err("Unable to find fpga-sd\n");
256 fpga
= of_iomap(np
, 0);
259 pr_err("Unable to map fpga-sd\n");
264 /* -- FPGA: POST Test Results Register 1 (32bit R/W) (Offset: 0x4040) --
265 * Bit 31, WDOG: Set to 1 when the last reset was caused by a watchdog
268 post1
= in_be32(fpga
+ 0x40);
269 if (post1
& 0x80000000)
270 pikawdt_private
.bootstatus
= WDIOF_CARDRESET
;
274 timer_setup(&pikawdt_private
.timer
, pikawdt_ping
, 0);
276 ret
= misc_register(&pikawdt_miscdev
);
278 pr_err("Unable to register miscdev\n");
282 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
283 heartbeat
, nowayout
);
287 iounmap(pikawdt_private
.fpga
);
291 static void __exit
pikawdt_exit(void)
293 misc_deregister(&pikawdt_miscdev
);
295 iounmap(pikawdt_private
.fpga
);
298 module_init(pikawdt_init
);
299 module_exit(pikawdt_exit
);
301 MODULE_AUTHOR("Sean MacLennan <smaclennan@pikatech.com>");
302 MODULE_DESCRIPTION("PIKA FPGA based Watchdog Timer");
303 MODULE_LICENSE("GPL");