0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010
0011 #include <linux/init.h>
0012 #include <linux/errno.h>
0013 #include <linux/module.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/types.h>
0016 #include <linux/kernel.h>
0017 #include <linux/fs.h>
0018 #include <linux/miscdevice.h>
0019 #include <linux/watchdog.h>
0020 #include <linux/reboot.h>
0021 #include <linux/jiffies.h>
0022 #include <linux/timer.h>
0023 #include <linux/bitops.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/io.h>
0026 #include <linux/of_address.h>
0027 #include <linux/of_platform.h>
0028
0029 #define DRV_NAME "PIKA-WDT"
0030
0031
0032 #define WDT_HW_TIMEOUT 2
0033
0034
0035 #define WDT_TIMEOUT (HZ/2)
0036
0037
0038 #define WDT_HEARTBEAT 15
0039 static int heartbeat = WDT_HEARTBEAT;
0040 module_param(heartbeat, int, 0);
0041 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
0042 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
0043
0044 static bool nowayout = WATCHDOG_NOWAYOUT;
0045 module_param(nowayout, bool, 0);
0046 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
0047 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0048
0049 static struct {
0050 void __iomem *fpga;
0051 unsigned long next_heartbeat;
0052 unsigned long open;
0053 char expect_close;
0054 int bootstatus;
0055 struct timer_list timer;
0056 } pikawdt_private;
0057
0058 static struct watchdog_info ident __ro_after_init = {
0059 .identity = DRV_NAME,
0060 .options = WDIOF_CARDRESET |
0061 WDIOF_SETTIMEOUT |
0062 WDIOF_KEEPALIVEPING |
0063 WDIOF_MAGICCLOSE,
0064 };
0065
0066
0067
0068
0069 static inline void pikawdt_reset(void)
0070 {
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 unsigned reset = in_be32(pikawdt_private.fpga + 0x14);
0081
0082 reset |= (1 << 7) + (WDT_HW_TIMEOUT << 8);
0083 out_be32(pikawdt_private.fpga + 0x14, reset);
0084 }
0085
0086
0087
0088
0089 static void pikawdt_ping(struct timer_list *unused)
0090 {
0091 if (time_before(jiffies, pikawdt_private.next_heartbeat) ||
0092 (!nowayout && !pikawdt_private.open)) {
0093 pikawdt_reset();
0094 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
0095 } else
0096 pr_crit("I will reset your machine !\n");
0097 }
0098
0099
0100 static void pikawdt_keepalive(void)
0101 {
0102 pikawdt_private.next_heartbeat = jiffies + heartbeat * HZ;
0103 }
0104
0105 static void pikawdt_start(void)
0106 {
0107 pikawdt_keepalive();
0108 mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
0109 }
0110
0111
0112
0113
0114 static int pikawdt_open(struct inode *inode, struct file *file)
0115 {
0116
0117 if (test_and_set_bit(0, &pikawdt_private.open))
0118 return -EBUSY;
0119
0120 pikawdt_start();
0121
0122 return stream_open(inode, file);
0123 }
0124
0125
0126
0127
0128 static int pikawdt_release(struct inode *inode, struct file *file)
0129 {
0130
0131 if (!pikawdt_private.expect_close)
0132 del_timer(&pikawdt_private.timer);
0133
0134 clear_bit(0, &pikawdt_private.open);
0135 pikawdt_private.expect_close = 0;
0136 return 0;
0137 }
0138
0139
0140
0141
0142 static ssize_t pikawdt_write(struct file *file, const char __user *data,
0143 size_t len, loff_t *ppos)
0144 {
0145 if (!len)
0146 return 0;
0147
0148
0149 if (!nowayout) {
0150 size_t i;
0151
0152 pikawdt_private.expect_close = 0;
0153
0154 for (i = 0; i < len; i++) {
0155 char c;
0156 if (get_user(c, data + i))
0157 return -EFAULT;
0158 if (c == 'V') {
0159 pikawdt_private.expect_close = 42;
0160 break;
0161 }
0162 }
0163 }
0164
0165 pikawdt_keepalive();
0166
0167 return len;
0168 }
0169
0170
0171
0172
0173 static long pikawdt_ioctl(struct file *file,
0174 unsigned int cmd, unsigned long arg)
0175 {
0176 void __user *argp = (void __user *)arg;
0177 int __user *p = argp;
0178 int new_value;
0179
0180 switch (cmd) {
0181 case WDIOC_GETSUPPORT:
0182 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
0183
0184 case WDIOC_GETSTATUS:
0185 return put_user(0, p);
0186
0187 case WDIOC_GETBOOTSTATUS:
0188 return put_user(pikawdt_private.bootstatus, p);
0189
0190 case WDIOC_KEEPALIVE:
0191 pikawdt_keepalive();
0192 return 0;
0193
0194 case WDIOC_SETTIMEOUT:
0195 if (get_user(new_value, p))
0196 return -EFAULT;
0197
0198 heartbeat = new_value;
0199 pikawdt_keepalive();
0200
0201 return put_user(new_value, p);
0202
0203 case WDIOC_GETTIMEOUT:
0204 return put_user(heartbeat, p);
0205 }
0206 return -ENOTTY;
0207 }
0208
0209
0210 static const struct file_operations pikawdt_fops = {
0211 .owner = THIS_MODULE,
0212 .llseek = no_llseek,
0213 .open = pikawdt_open,
0214 .release = pikawdt_release,
0215 .write = pikawdt_write,
0216 .unlocked_ioctl = pikawdt_ioctl,
0217 .compat_ioctl = compat_ptr_ioctl,
0218 };
0219
0220 static struct miscdevice pikawdt_miscdev = {
0221 .minor = WATCHDOG_MINOR,
0222 .name = "watchdog",
0223 .fops = &pikawdt_fops,
0224 };
0225
0226 static int __init pikawdt_init(void)
0227 {
0228 struct device_node *np;
0229 void __iomem *fpga;
0230 u32 post1;
0231 int ret;
0232
0233 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
0234 if (np == NULL) {
0235 pr_err("Unable to find fpga\n");
0236 return -ENOENT;
0237 }
0238
0239 pikawdt_private.fpga = of_iomap(np, 0);
0240 of_node_put(np);
0241 if (pikawdt_private.fpga == NULL) {
0242 pr_err("Unable to map fpga\n");
0243 return -ENOMEM;
0244 }
0245
0246 ident.firmware_version = in_be32(pikawdt_private.fpga + 0x1c) & 0xffff;
0247
0248
0249 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
0250 if (np == NULL) {
0251 pr_err("Unable to find fpga-sd\n");
0252 ret = -ENOENT;
0253 goto out;
0254 }
0255
0256 fpga = of_iomap(np, 0);
0257 of_node_put(np);
0258 if (fpga == NULL) {
0259 pr_err("Unable to map fpga-sd\n");
0260 ret = -ENOMEM;
0261 goto out;
0262 }
0263
0264
0265
0266
0267
0268 post1 = in_be32(fpga + 0x40);
0269 if (post1 & 0x80000000)
0270 pikawdt_private.bootstatus = WDIOF_CARDRESET;
0271
0272 iounmap(fpga);
0273
0274 timer_setup(&pikawdt_private.timer, pikawdt_ping, 0);
0275
0276 ret = misc_register(&pikawdt_miscdev);
0277 if (ret) {
0278 pr_err("Unable to register miscdev\n");
0279 goto out;
0280 }
0281
0282 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
0283 heartbeat, nowayout);
0284 return 0;
0285
0286 out:
0287 iounmap(pikawdt_private.fpga);
0288 return ret;
0289 }
0290
0291 static void __exit pikawdt_exit(void)
0292 {
0293 misc_deregister(&pikawdt_miscdev);
0294
0295 iounmap(pikawdt_private.fpga);
0296 }
0297
0298 module_init(pikawdt_init);
0299 module_exit(pikawdt_exit);
0300
0301 MODULE_AUTHOR("Sean MacLennan <smaclennan@pikatech.com>");
0302 MODULE_DESCRIPTION("PIKA FPGA based Watchdog Timer");
0303 MODULE_LICENSE("GPL");