Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Watchdog timer driver for the WinSystems EBC-C384
0004  * Copyright (C) 2016 William Breathitt Gray
0005  */
0006 #include <linux/device.h>
0007 #include <linux/dmi.h>
0008 #include <linux/errno.h>
0009 #include <linux/io.h>
0010 #include <linux/ioport.h>
0011 #include <linux/isa.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/types.h>
0016 #include <linux/watchdog.h>
0017 
0018 #define MODULE_NAME     "ebc-c384_wdt"
0019 #define WATCHDOG_TIMEOUT    60
0020 /*
0021  * The timeout value in minutes must fit in a single byte when sent to the
0022  * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds.
0023  */
0024 #define WATCHDOG_MAX_TIMEOUT    15300
0025 #define BASE_ADDR       0x564
0026 #define ADDR_EXTENT     5
0027 #define CFG_ADDR        (BASE_ADDR + 1)
0028 #define PET_ADDR        (BASE_ADDR + 2)
0029 
0030 static bool nowayout = WATCHDOG_NOWAYOUT;
0031 module_param(nowayout, bool, 0);
0032 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
0033     __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0034 
0035 static unsigned timeout;
0036 module_param(timeout, uint, 0);
0037 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
0038     __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
0039 
0040 static int ebc_c384_wdt_start(struct watchdog_device *wdev)
0041 {
0042     unsigned t = wdev->timeout;
0043 
0044     /* resolution is in minutes for timeouts greater than 255 seconds */
0045     if (t > 255)
0046         t = DIV_ROUND_UP(t, 60);
0047 
0048     outb(t, PET_ADDR);
0049 
0050     return 0;
0051 }
0052 
0053 static int ebc_c384_wdt_stop(struct watchdog_device *wdev)
0054 {
0055     outb(0x00, PET_ADDR);
0056 
0057     return 0;
0058 }
0059 
0060 static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
0061 {
0062     /* resolution is in minutes for timeouts greater than 255 seconds */
0063     if (t > 255) {
0064         /* round second resolution up to minute granularity */
0065         wdev->timeout = roundup(t, 60);
0066 
0067         /* set watchdog timer for minutes */
0068         outb(0x00, CFG_ADDR);
0069     } else {
0070         wdev->timeout = t;
0071 
0072         /* set watchdog timer for seconds */
0073         outb(0x80, CFG_ADDR);
0074     }
0075 
0076     return 0;
0077 }
0078 
0079 static const struct watchdog_ops ebc_c384_wdt_ops = {
0080     .start = ebc_c384_wdt_start,
0081     .stop = ebc_c384_wdt_stop,
0082     .set_timeout = ebc_c384_wdt_set_timeout
0083 };
0084 
0085 static const struct watchdog_info ebc_c384_wdt_info = {
0086     .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
0087     .identity = MODULE_NAME
0088 };
0089 
0090 static int ebc_c384_wdt_probe(struct device *dev, unsigned int id)
0091 {
0092     struct watchdog_device *wdd;
0093 
0094     if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) {
0095         dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
0096             BASE_ADDR, BASE_ADDR + ADDR_EXTENT);
0097         return -EBUSY;
0098     }
0099 
0100     wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
0101     if (!wdd)
0102         return -ENOMEM;
0103 
0104     wdd->info = &ebc_c384_wdt_info;
0105     wdd->ops = &ebc_c384_wdt_ops;
0106     wdd->timeout = WATCHDOG_TIMEOUT;
0107     wdd->min_timeout = 1;
0108     wdd->max_timeout = WATCHDOG_MAX_TIMEOUT;
0109 
0110     watchdog_set_nowayout(wdd, nowayout);
0111     watchdog_init_timeout(wdd, timeout, dev);
0112 
0113     return devm_watchdog_register_device(dev, wdd);
0114 }
0115 
0116 static struct isa_driver ebc_c384_wdt_driver = {
0117     .probe = ebc_c384_wdt_probe,
0118     .driver = {
0119         .name = MODULE_NAME
0120     },
0121 };
0122 
0123 static int __init ebc_c384_wdt_init(void)
0124 {
0125     if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC"))
0126         return -ENODEV;
0127 
0128     return isa_register_driver(&ebc_c384_wdt_driver, 1);
0129 }
0130 
0131 static void __exit ebc_c384_wdt_exit(void)
0132 {
0133     isa_unregister_driver(&ebc_c384_wdt_driver);
0134 }
0135 
0136 module_init(ebc_c384_wdt_init);
0137 module_exit(ebc_c384_wdt_exit);
0138 
0139 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
0140 MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
0141 MODULE_LICENSE("GPL v2");
0142 MODULE_ALIAS("isa:" MODULE_NAME);