Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2016 National Instruments Corp.
0004  */
0005 
0006 #include <linux/acpi.h>
0007 #include <linux/bitops.h>
0008 #include <linux/device.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/watchdog.h>
0013 
0014 #define LOCK            0xA5
0015 #define UNLOCK          0x5A
0016 
0017 #define WDT_CTRL_RESET_EN   BIT(7)
0018 #define WDT_RELOAD_PORT_EN  BIT(7)
0019 
0020 #define WDT_CTRL        1
0021 #define WDT_RELOAD_CTRL     2
0022 #define WDT_PRESET_PRESCALE 4
0023 #define WDT_REG_LOCK        5
0024 #define WDT_COUNT       6
0025 #define WDT_RELOAD_PORT     7
0026 
0027 #define WDT_MIN_TIMEOUT     1
0028 #define WDT_MAX_TIMEOUT     464
0029 #define WDT_DEFAULT_TIMEOUT 80
0030 
0031 #define WDT_MAX_COUNTER     15
0032 
0033 static unsigned int timeout;
0034 module_param(timeout, uint, 0);
0035 MODULE_PARM_DESC(timeout,
0036          "Watchdog timeout in seconds. (default="
0037          __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
0038 
0039 static bool nowayout = WATCHDOG_NOWAYOUT;
0040 module_param(nowayout, bool, 0);
0041 MODULE_PARM_DESC(nowayout,
0042          "Watchdog cannot be stopped once started. (default="
0043          __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0044 
0045 struct nic7018_wdt {
0046     u16 io_base;
0047     u32 period;
0048     struct watchdog_device wdd;
0049 };
0050 
0051 struct nic7018_config {
0052     u32 period;
0053     u8 divider;
0054 };
0055 
0056 static const struct nic7018_config nic7018_configs[] = {
0057     {  2, 4 },
0058     { 32, 5 },
0059 };
0060 
0061 static inline u32 nic7018_timeout(u32 period, u8 counter)
0062 {
0063     return period * counter - period / 2;
0064 }
0065 
0066 static const struct nic7018_config *nic7018_get_config(u32 timeout,
0067                                u8 *counter)
0068 {
0069     const struct nic7018_config *config;
0070     u8 count;
0071 
0072     if (timeout < 30 && timeout != 16) {
0073         config = &nic7018_configs[0];
0074         count = timeout / 2 + 1;
0075     } else {
0076         config = &nic7018_configs[1];
0077         count = DIV_ROUND_UP(timeout + 16, 32);
0078 
0079         if (count > WDT_MAX_COUNTER)
0080             count = WDT_MAX_COUNTER;
0081     }
0082     *counter = count;
0083     return config;
0084 }
0085 
0086 static int nic7018_set_timeout(struct watchdog_device *wdd,
0087                    unsigned int timeout)
0088 {
0089     struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
0090     const struct nic7018_config *config;
0091     u8 counter;
0092 
0093     config = nic7018_get_config(timeout, &counter);
0094 
0095     outb(counter << 4 | config->divider,
0096          wdt->io_base + WDT_PRESET_PRESCALE);
0097 
0098     wdd->timeout = nic7018_timeout(config->period, counter);
0099     wdt->period = config->period;
0100 
0101     return 0;
0102 }
0103 
0104 static int nic7018_start(struct watchdog_device *wdd)
0105 {
0106     struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
0107     u8 control;
0108 
0109     nic7018_set_timeout(wdd, wdd->timeout);
0110 
0111     control = inb(wdt->io_base + WDT_RELOAD_CTRL);
0112     outb(control | WDT_RELOAD_PORT_EN, wdt->io_base + WDT_RELOAD_CTRL);
0113 
0114     outb(1, wdt->io_base + WDT_RELOAD_PORT);
0115 
0116     control = inb(wdt->io_base + WDT_CTRL);
0117     outb(control | WDT_CTRL_RESET_EN, wdt->io_base + WDT_CTRL);
0118 
0119     return 0;
0120 }
0121 
0122 static int nic7018_stop(struct watchdog_device *wdd)
0123 {
0124     struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
0125 
0126     outb(0, wdt->io_base + WDT_CTRL);
0127     outb(0, wdt->io_base + WDT_RELOAD_CTRL);
0128     outb(0xF0, wdt->io_base + WDT_PRESET_PRESCALE);
0129 
0130     return 0;
0131 }
0132 
0133 static int nic7018_ping(struct watchdog_device *wdd)
0134 {
0135     struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
0136 
0137     outb(1, wdt->io_base + WDT_RELOAD_PORT);
0138 
0139     return 0;
0140 }
0141 
0142 static unsigned int nic7018_get_timeleft(struct watchdog_device *wdd)
0143 {
0144     struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
0145     u8 count;
0146 
0147     count = inb(wdt->io_base + WDT_COUNT) & 0xF;
0148     if (!count)
0149         return 0;
0150 
0151     return nic7018_timeout(wdt->period, count);
0152 }
0153 
0154 static const struct watchdog_info nic7018_wdd_info = {
0155     .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0156     .identity = "NIC7018 Watchdog",
0157 };
0158 
0159 static const struct watchdog_ops nic7018_wdd_ops = {
0160     .owner = THIS_MODULE,
0161     .start = nic7018_start,
0162     .stop = nic7018_stop,
0163     .ping = nic7018_ping,
0164     .set_timeout = nic7018_set_timeout,
0165     .get_timeleft = nic7018_get_timeleft,
0166 };
0167 
0168 static int nic7018_probe(struct platform_device *pdev)
0169 {
0170     struct device *dev = &pdev->dev;
0171     struct watchdog_device *wdd;
0172     struct nic7018_wdt *wdt;
0173     struct resource *io_rc;
0174     int ret;
0175 
0176     wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
0177     if (!wdt)
0178         return -ENOMEM;
0179 
0180     platform_set_drvdata(pdev, wdt);
0181 
0182     io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
0183     if (!io_rc) {
0184         dev_err(dev, "missing IO resources\n");
0185         return -EINVAL;
0186     }
0187 
0188     if (!devm_request_region(dev, io_rc->start, resource_size(io_rc),
0189                  KBUILD_MODNAME)) {
0190         dev_err(dev, "failed to get IO region\n");
0191         return -EBUSY;
0192     }
0193 
0194     wdt->io_base = io_rc->start;
0195     wdd = &wdt->wdd;
0196     wdd->info = &nic7018_wdd_info;
0197     wdd->ops = &nic7018_wdd_ops;
0198     wdd->min_timeout = WDT_MIN_TIMEOUT;
0199     wdd->max_timeout = WDT_MAX_TIMEOUT;
0200     wdd->timeout = WDT_DEFAULT_TIMEOUT;
0201     wdd->parent = dev;
0202 
0203     watchdog_set_drvdata(wdd, wdt);
0204     watchdog_set_nowayout(wdd, nowayout);
0205     watchdog_init_timeout(wdd, timeout, dev);
0206 
0207     /* Unlock WDT register */
0208     outb(UNLOCK, wdt->io_base + WDT_REG_LOCK);
0209 
0210     ret = watchdog_register_device(wdd);
0211     if (ret) {
0212         outb(LOCK, wdt->io_base + WDT_REG_LOCK);
0213         return ret;
0214     }
0215 
0216     dev_dbg(dev, "io_base=0x%04X, timeout=%d, nowayout=%d\n",
0217         wdt->io_base, timeout, nowayout);
0218     return 0;
0219 }
0220 
0221 static int nic7018_remove(struct platform_device *pdev)
0222 {
0223     struct nic7018_wdt *wdt = platform_get_drvdata(pdev);
0224 
0225     watchdog_unregister_device(&wdt->wdd);
0226 
0227     /* Lock WDT register */
0228     outb(LOCK, wdt->io_base + WDT_REG_LOCK);
0229 
0230     return 0;
0231 }
0232 
0233 static const struct acpi_device_id nic7018_device_ids[] = {
0234     {"NIC7018", 0},
0235     {"", 0},
0236 };
0237 MODULE_DEVICE_TABLE(acpi, nic7018_device_ids);
0238 
0239 static struct platform_driver watchdog_driver = {
0240     .probe = nic7018_probe,
0241     .remove = nic7018_remove,
0242     .driver = {
0243         .name = KBUILD_MODNAME,
0244         .acpi_match_table = ACPI_PTR(nic7018_device_ids),
0245     },
0246 };
0247 
0248 module_platform_driver(watchdog_driver);
0249 
0250 MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
0251 MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
0252 MODULE_LICENSE("GPL");