Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  MachZ ZF-Logic Watchdog Timer driver for Linux
0004  *
0005  *  The author does NOT admit liability nor provide warranty for
0006  *  any of this software. This material is provided "AS-IS" in
0007  *  the hope that it may be useful for others.
0008  *
0009  *  Author: Fernando Fuganti <fuganti@conectiva.com.br>
0010  *
0011  *  Based on sbc60xxwdt.c by Jakob Oestergaard
0012  *
0013  *  We have two timers (wd#1, wd#2) driven by a 32 KHz clock with the
0014  *  following periods:
0015  *      wd#1 - 2 seconds;
0016  *      wd#2 - 7.2 ms;
0017  *  After the expiration of wd#1, it can generate a NMI, SCI, SMI, or
0018  *  a system RESET and it starts wd#2 that unconditionally will RESET
0019  *  the system when the counter reaches zero.
0020  *
0021  *  14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
0022  *      Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
0023  */
0024 
0025 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0026 
0027 #include <linux/module.h>
0028 #include <linux/moduleparam.h>
0029 #include <linux/types.h>
0030 #include <linux/timer.h>
0031 #include <linux/jiffies.h>
0032 #include <linux/miscdevice.h>
0033 #include <linux/watchdog.h>
0034 #include <linux/fs.h>
0035 #include <linux/ioport.h>
0036 #include <linux/notifier.h>
0037 #include <linux/reboot.h>
0038 #include <linux/init.h>
0039 #include <linux/io.h>
0040 #include <linux/uaccess.h>
0041 
0042 
0043 /* ports */
0044 #define ZF_IOBASE   0x218
0045 #define INDEX       0x218
0046 #define DATA_B      0x219
0047 #define DATA_W      0x21A
0048 #define DATA_D      0x21A
0049 
0050 /* indexes */           /* size */
0051 #define ZFL_VERSION 0x02    /* 16   */
0052 #define CONTROL     0x10    /* 16   */
0053 #define STATUS      0x12    /* 8    */
0054 #define COUNTER_1   0x0C    /* 16   */
0055 #define COUNTER_2   0x0E    /* 8    */
0056 #define PULSE_LEN   0x0F    /* 8    */
0057 
0058 /* controls */
0059 #define ENABLE_WD1  0x0001
0060 #define ENABLE_WD2  0x0002
0061 #define RESET_WD1   0x0010
0062 #define RESET_WD2   0x0020
0063 #define GEN_SCI     0x0100
0064 #define GEN_NMI     0x0200
0065 #define GEN_SMI     0x0400
0066 #define GEN_RESET   0x0800
0067 
0068 
0069 /* utilities */
0070 
0071 #define WD1 0
0072 #define WD2 1
0073 
0074 #define zf_writew(port, data)  { outb(port, INDEX); outw(data, DATA_W); }
0075 #define zf_writeb(port, data)  { outb(port, INDEX); outb(data, DATA_B); }
0076 #define zf_get_ZFL_version()   zf_readw(ZFL_VERSION)
0077 
0078 
0079 static unsigned short zf_readw(unsigned char port)
0080 {
0081     outb(port, INDEX);
0082     return inw(DATA_W);
0083 }
0084 
0085 
0086 MODULE_AUTHOR("Fernando Fuganti <fuganti@conectiva.com.br>");
0087 MODULE_DESCRIPTION("MachZ ZF-Logic Watchdog driver");
0088 MODULE_LICENSE("GPL");
0089 
0090 static bool nowayout = WATCHDOG_NOWAYOUT;
0091 module_param(nowayout, bool, 0);
0092 MODULE_PARM_DESC(nowayout,
0093         "Watchdog cannot be stopped once started (default="
0094                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0095 
0096 #define PFX "machzwd"
0097 
0098 static const struct watchdog_info zf_info = {
0099     .options        = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0100     .firmware_version   = 1,
0101     .identity       = "ZF-Logic watchdog",
0102 };
0103 
0104 
0105 /*
0106  * action refers to action taken when watchdog resets
0107  * 0 = GEN_RESET
0108  * 1 = GEN_SMI
0109  * 2 = GEN_NMI
0110  * 3 = GEN_SCI
0111  * defaults to GEN_RESET (0)
0112  */
0113 static int action;
0114 module_param(action, int, 0);
0115 MODULE_PARM_DESC(action, "after watchdog resets, generate: "
0116                 "0 = RESET(*)  1 = SMI  2 = NMI  3 = SCI");
0117 
0118 static void zf_ping(struct timer_list *unused);
0119 
0120 static int zf_action = GEN_RESET;
0121 static unsigned long zf_is_open;
0122 static char zf_expect_close;
0123 static DEFINE_SPINLOCK(zf_port_lock);
0124 static DEFINE_TIMER(zf_timer, zf_ping);
0125 static unsigned long next_heartbeat;
0126 
0127 
0128 /* timeout for user land heart beat (10 seconds) */
0129 #define ZF_USER_TIMEO (HZ*10)
0130 
0131 /* timeout for hardware watchdog (~500ms) */
0132 #define ZF_HW_TIMEO (HZ/2)
0133 
0134 /* number of ticks on WD#1 (driven by a 32KHz clock, 2s) */
0135 #define ZF_CTIMEOUT 0xffff
0136 
0137 #ifndef ZF_DEBUG
0138 #define dprintk(format, args...)
0139 #else
0140 #define dprintk(format, args...)                    \
0141     pr_debug(":%s:%d: " format, __func__, __LINE__ , ## args)
0142 #endif
0143 
0144 
0145 static inline void zf_set_status(unsigned char new)
0146 {
0147     zf_writeb(STATUS, new);
0148 }
0149 
0150 
0151 /* CONTROL register functions */
0152 
0153 static inline unsigned short zf_get_control(void)
0154 {
0155     return zf_readw(CONTROL);
0156 }
0157 
0158 static inline void zf_set_control(unsigned short new)
0159 {
0160     zf_writew(CONTROL, new);
0161 }
0162 
0163 
0164 /* WD#? counter functions */
0165 /*
0166  *  Just set counter value
0167  */
0168 
0169 static inline void zf_set_timer(unsigned short new, unsigned char n)
0170 {
0171     switch (n) {
0172     case WD1:
0173         zf_writew(COUNTER_1, new);
0174         fallthrough;
0175     case WD2:
0176         zf_writeb(COUNTER_2, new > 0xff ? 0xff : new);
0177         fallthrough;
0178     default:
0179         return;
0180     }
0181 }
0182 
0183 /*
0184  * stop hardware timer
0185  */
0186 static void zf_timer_off(void)
0187 {
0188     unsigned int ctrl_reg = 0;
0189     unsigned long flags;
0190 
0191     /* stop internal ping */
0192     del_timer_sync(&zf_timer);
0193 
0194     spin_lock_irqsave(&zf_port_lock, flags);
0195     /* stop watchdog timer */
0196     ctrl_reg = zf_get_control();
0197     ctrl_reg |= (ENABLE_WD1|ENABLE_WD2);    /* disable wd1 and wd2 */
0198     ctrl_reg &= ~(ENABLE_WD1|ENABLE_WD2);
0199     zf_set_control(ctrl_reg);
0200     spin_unlock_irqrestore(&zf_port_lock, flags);
0201 
0202     pr_info("Watchdog timer is now disabled\n");
0203 }
0204 
0205 
0206 /*
0207  * start hardware timer
0208  */
0209 static void zf_timer_on(void)
0210 {
0211     unsigned int ctrl_reg = 0;
0212     unsigned long flags;
0213 
0214     spin_lock_irqsave(&zf_port_lock, flags);
0215 
0216     zf_writeb(PULSE_LEN, 0xff);
0217 
0218     zf_set_timer(ZF_CTIMEOUT, WD1);
0219 
0220     /* user land ping */
0221     next_heartbeat = jiffies + ZF_USER_TIMEO;
0222 
0223     /* start the timer for internal ping */
0224     mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO);
0225 
0226     /* start watchdog timer */
0227     ctrl_reg = zf_get_control();
0228     ctrl_reg |= (ENABLE_WD1|zf_action);
0229     zf_set_control(ctrl_reg);
0230     spin_unlock_irqrestore(&zf_port_lock, flags);
0231 
0232     pr_info("Watchdog timer is now enabled\n");
0233 }
0234 
0235 
0236 static void zf_ping(struct timer_list *unused)
0237 {
0238     unsigned int ctrl_reg = 0;
0239     unsigned long flags;
0240 
0241     zf_writeb(COUNTER_2, 0xff);
0242 
0243     if (time_before(jiffies, next_heartbeat)) {
0244         dprintk("time_before: %ld\n", next_heartbeat - jiffies);
0245         /*
0246          * reset event is activated by transition from 0 to 1 on
0247          * RESET_WD1 bit and we assume that it is already zero...
0248          */
0249 
0250         spin_lock_irqsave(&zf_port_lock, flags);
0251         ctrl_reg = zf_get_control();
0252         ctrl_reg |= RESET_WD1;
0253         zf_set_control(ctrl_reg);
0254 
0255         /* ...and nothing changes until here */
0256         ctrl_reg &= ~(RESET_WD1);
0257         zf_set_control(ctrl_reg);
0258         spin_unlock_irqrestore(&zf_port_lock, flags);
0259 
0260         mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO);
0261     } else
0262         pr_crit("I will reset your machine\n");
0263 }
0264 
0265 static ssize_t zf_write(struct file *file, const char __user *buf, size_t count,
0266                                 loff_t *ppos)
0267 {
0268     /* See if we got the magic character */
0269     if (count) {
0270         /*
0271          * no need to check for close confirmation
0272          * no way to disable watchdog ;)
0273          */
0274         if (!nowayout) {
0275             size_t ofs;
0276             /*
0277              * note: just in case someone wrote the magic character
0278              * five months ago...
0279              */
0280             zf_expect_close = 0;
0281 
0282             /* now scan */
0283             for (ofs = 0; ofs != count; ofs++) {
0284                 char c;
0285                 if (get_user(c, buf + ofs))
0286                     return -EFAULT;
0287                 if (c == 'V') {
0288                     zf_expect_close = 42;
0289                     dprintk("zf_expect_close = 42\n");
0290                 }
0291             }
0292         }
0293 
0294         /*
0295          * Well, anyhow someone wrote to us,
0296          * we should return that favour
0297          */
0298         next_heartbeat = jiffies + ZF_USER_TIMEO;
0299         dprintk("user ping at %ld\n", jiffies);
0300     }
0301     return count;
0302 }
0303 
0304 static long zf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0305 {
0306     void __user *argp = (void __user *)arg;
0307     int __user *p = argp;
0308     switch (cmd) {
0309     case WDIOC_GETSUPPORT:
0310         if (copy_to_user(argp, &zf_info, sizeof(zf_info)))
0311             return -EFAULT;
0312         break;
0313     case WDIOC_GETSTATUS:
0314     case WDIOC_GETBOOTSTATUS:
0315         return put_user(0, p);
0316     case WDIOC_KEEPALIVE:
0317         zf_ping(NULL);
0318         break;
0319     default:
0320         return -ENOTTY;
0321     }
0322     return 0;
0323 }
0324 
0325 static int zf_open(struct inode *inode, struct file *file)
0326 {
0327     if (test_and_set_bit(0, &zf_is_open))
0328         return -EBUSY;
0329     if (nowayout)
0330         __module_get(THIS_MODULE);
0331     zf_timer_on();
0332     return stream_open(inode, file);
0333 }
0334 
0335 static int zf_close(struct inode *inode, struct file *file)
0336 {
0337     if (zf_expect_close == 42)
0338         zf_timer_off();
0339     else {
0340         del_timer(&zf_timer);
0341         pr_err("device file closed unexpectedly. Will not stop the WDT!\n");
0342     }
0343     clear_bit(0, &zf_is_open);
0344     zf_expect_close = 0;
0345     return 0;
0346 }
0347 
0348 /*
0349  * Notifier for system down
0350  */
0351 
0352 static int zf_notify_sys(struct notifier_block *this, unsigned long code,
0353                                 void *unused)
0354 {
0355     if (code == SYS_DOWN || code == SYS_HALT)
0356         zf_timer_off();
0357     return NOTIFY_DONE;
0358 }
0359 
0360 static const struct file_operations zf_fops = {
0361     .owner      = THIS_MODULE,
0362     .llseek     = no_llseek,
0363     .write      = zf_write,
0364     .unlocked_ioctl = zf_ioctl,
0365     .compat_ioctl   = compat_ptr_ioctl,
0366     .open       = zf_open,
0367     .release    = zf_close,
0368 };
0369 
0370 static struct miscdevice zf_miscdev = {
0371     .minor = WATCHDOG_MINOR,
0372     .name = "watchdog",
0373     .fops = &zf_fops,
0374 };
0375 
0376 
0377 /*
0378  * The device needs to learn about soft shutdowns in order to
0379  * turn the timebomb registers off.
0380  */
0381 static struct notifier_block zf_notifier = {
0382     .notifier_call = zf_notify_sys,
0383 };
0384 
0385 static void __init zf_show_action(int act)
0386 {
0387     static const char * const str[] = { "RESET", "SMI", "NMI", "SCI" };
0388 
0389     pr_info("Watchdog using action = %s\n", str[act]);
0390 }
0391 
0392 static int __init zf_init(void)
0393 {
0394     int ret;
0395 
0396     pr_info("MachZ ZF-Logic Watchdog driver initializing\n");
0397 
0398     ret = zf_get_ZFL_version();
0399     if (!ret || ret == 0xffff) {
0400         pr_warn("no ZF-Logic found\n");
0401         return -ENODEV;
0402     }
0403 
0404     if (action <= 3 && action >= 0)
0405         zf_action = zf_action >> action;
0406     else
0407         action = 0;
0408 
0409     zf_show_action(action);
0410 
0411     if (!request_region(ZF_IOBASE, 3, "MachZ ZFL WDT")) {
0412         pr_err("cannot reserve I/O ports at %d\n", ZF_IOBASE);
0413         ret = -EBUSY;
0414         goto no_region;
0415     }
0416 
0417     ret = register_reboot_notifier(&zf_notifier);
0418     if (ret) {
0419         pr_err("can't register reboot notifier (err=%d)\n", ret);
0420         goto no_reboot;
0421     }
0422 
0423     ret = misc_register(&zf_miscdev);
0424     if (ret) {
0425         pr_err("can't misc_register on minor=%d\n", WATCHDOG_MINOR);
0426         goto no_misc;
0427     }
0428 
0429     zf_set_status(0);
0430     zf_set_control(0);
0431 
0432     return 0;
0433 
0434 no_misc:
0435     unregister_reboot_notifier(&zf_notifier);
0436 no_reboot:
0437     release_region(ZF_IOBASE, 3);
0438 no_region:
0439     return ret;
0440 }
0441 
0442 
0443 static void __exit zf_exit(void)
0444 {
0445     zf_timer_off();
0446 
0447     misc_deregister(&zf_miscdev);
0448     unregister_reboot_notifier(&zf_notifier);
0449     release_region(ZF_IOBASE, 3);
0450 }
0451 
0452 module_init(zf_init);
0453 module_exit(zf_exit);