Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Watchdog timer for machines with the CS5535/CS5536 companion chip
0003  *
0004  * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
0005  * Copyright (C) 2009  Andres Salomon <dilinger@collabora.co.uk>
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/types.h>
0013 #include <linux/miscdevice.h>
0014 #include <linux/watchdog.h>
0015 #include <linux/fs.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/reboot.h>
0018 #include <linux/uaccess.h>
0019 
0020 #include <linux/cs5535.h>
0021 
0022 #define GEODEWDT_HZ 500
0023 #define GEODEWDT_SCALE 6
0024 #define GEODEWDT_MAX_SECONDS 131
0025 
0026 #define WDT_FLAGS_OPEN 1
0027 #define WDT_FLAGS_ORPHAN 2
0028 
0029 #define DRV_NAME "geodewdt"
0030 #define WATCHDOG_NAME "Geode GX/LX WDT"
0031 #define WATCHDOG_TIMEOUT 60
0032 
0033 static int timeout = WATCHDOG_TIMEOUT;
0034 module_param(timeout, int, 0);
0035 MODULE_PARM_DESC(timeout,
0036     "Watchdog timeout in seconds. 1<= timeout <=131, default="
0037                 __MODULE_STRING(WATCHDOG_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 static struct platform_device *geodewdt_platform_device;
0046 static unsigned long wdt_flags;
0047 static struct cs5535_mfgpt_timer *wdt_timer;
0048 static int safe_close;
0049 
0050 static void geodewdt_ping(void)
0051 {
0052     /* Stop the counter */
0053     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
0054 
0055     /* Reset the counter */
0056     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
0057 
0058     /* Enable the counter */
0059     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
0060 }
0061 
0062 static void geodewdt_disable(void)
0063 {
0064     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
0065     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
0066 }
0067 
0068 static int geodewdt_set_heartbeat(int val)
0069 {
0070     if (val < 1 || val > GEODEWDT_MAX_SECONDS)
0071         return -EINVAL;
0072 
0073     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
0074     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
0075     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
0076     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
0077 
0078     timeout = val;
0079     return 0;
0080 }
0081 
0082 static int geodewdt_open(struct inode *inode, struct file *file)
0083 {
0084     if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
0085         return -EBUSY;
0086 
0087     if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
0088         __module_get(THIS_MODULE);
0089 
0090     geodewdt_ping();
0091     return stream_open(inode, file);
0092 }
0093 
0094 static int geodewdt_release(struct inode *inode, struct file *file)
0095 {
0096     if (safe_close) {
0097         geodewdt_disable();
0098         module_put(THIS_MODULE);
0099     } else {
0100         pr_crit("Unexpected close - watchdog is not stopping\n");
0101         geodewdt_ping();
0102 
0103         set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
0104     }
0105 
0106     clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
0107     safe_close = 0;
0108     return 0;
0109 }
0110 
0111 static ssize_t geodewdt_write(struct file *file, const char __user *data,
0112                 size_t len, loff_t *ppos)
0113 {
0114     if (len) {
0115         if (!nowayout) {
0116             size_t i;
0117             safe_close = 0;
0118 
0119             for (i = 0; i != len; i++) {
0120                 char c;
0121 
0122                 if (get_user(c, data + i))
0123                     return -EFAULT;
0124 
0125                 if (c == 'V')
0126                     safe_close = 1;
0127             }
0128         }
0129 
0130         geodewdt_ping();
0131     }
0132     return len;
0133 }
0134 
0135 static long geodewdt_ioctl(struct file *file, unsigned int cmd,
0136                 unsigned long arg)
0137 {
0138     void __user *argp = (void __user *)arg;
0139     int __user *p = argp;
0140     int interval;
0141 
0142     static const struct watchdog_info ident = {
0143         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
0144         | WDIOF_MAGICCLOSE,
0145         .firmware_version =     1,
0146         .identity =             WATCHDOG_NAME,
0147     };
0148 
0149     switch (cmd) {
0150     case WDIOC_GETSUPPORT:
0151         return copy_to_user(argp, &ident,
0152                     sizeof(ident)) ? -EFAULT : 0;
0153     case WDIOC_GETSTATUS:
0154     case WDIOC_GETBOOTSTATUS:
0155         return put_user(0, p);
0156 
0157     case WDIOC_SETOPTIONS:
0158     {
0159         int options, ret = -EINVAL;
0160 
0161         if (get_user(options, p))
0162             return -EFAULT;
0163 
0164         if (options & WDIOS_DISABLECARD) {
0165             geodewdt_disable();
0166             ret = 0;
0167         }
0168 
0169         if (options & WDIOS_ENABLECARD) {
0170             geodewdt_ping();
0171             ret = 0;
0172         }
0173 
0174         return ret;
0175     }
0176     case WDIOC_KEEPALIVE:
0177         geodewdt_ping();
0178         return 0;
0179 
0180     case WDIOC_SETTIMEOUT:
0181         if (get_user(interval, p))
0182             return -EFAULT;
0183 
0184         if (geodewdt_set_heartbeat(interval))
0185             return -EINVAL;
0186         fallthrough;
0187     case WDIOC_GETTIMEOUT:
0188         return put_user(timeout, p);
0189 
0190     default:
0191         return -ENOTTY;
0192     }
0193 
0194     return 0;
0195 }
0196 
0197 static const struct file_operations geodewdt_fops = {
0198     .owner          = THIS_MODULE,
0199     .llseek         = no_llseek,
0200     .write          = geodewdt_write,
0201     .unlocked_ioctl = geodewdt_ioctl,
0202     .compat_ioctl   = compat_ptr_ioctl,
0203     .open           = geodewdt_open,
0204     .release        = geodewdt_release,
0205 };
0206 
0207 static struct miscdevice geodewdt_miscdev = {
0208     .minor = WATCHDOG_MINOR,
0209     .name = "watchdog",
0210     .fops = &geodewdt_fops,
0211 };
0212 
0213 static int __init geodewdt_probe(struct platform_device *dev)
0214 {
0215     int ret;
0216 
0217     wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
0218     if (!wdt_timer) {
0219         pr_err("No timers were available\n");
0220         return -ENODEV;
0221     }
0222 
0223     /* Set up the timer */
0224 
0225     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
0226               GEODEWDT_SCALE | (3 << 8));
0227 
0228     /* Set up comparator 2 to reset when the event fires */
0229     cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
0230 
0231     /* Set up the initial timeout */
0232 
0233     cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
0234         timeout * GEODEWDT_HZ);
0235 
0236     ret = misc_register(&geodewdt_miscdev);
0237 
0238     return ret;
0239 }
0240 
0241 static int geodewdt_remove(struct platform_device *dev)
0242 {
0243     misc_deregister(&geodewdt_miscdev);
0244     return 0;
0245 }
0246 
0247 static void geodewdt_shutdown(struct platform_device *dev)
0248 {
0249     geodewdt_disable();
0250 }
0251 
0252 static struct platform_driver geodewdt_driver = {
0253     .remove     = geodewdt_remove,
0254     .shutdown   = geodewdt_shutdown,
0255     .driver     = {
0256         .name   = DRV_NAME,
0257     },
0258 };
0259 
0260 static int __init geodewdt_init(void)
0261 {
0262     int ret;
0263 
0264     geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
0265                                 -1, NULL, 0);
0266     if (IS_ERR(geodewdt_platform_device))
0267         return PTR_ERR(geodewdt_platform_device);
0268 
0269     ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe);
0270     if (ret)
0271         goto err;
0272 
0273     return 0;
0274 err:
0275     platform_device_unregister(geodewdt_platform_device);
0276     return ret;
0277 }
0278 
0279 static void __exit geodewdt_exit(void)
0280 {
0281     platform_device_unregister(geodewdt_platform_device);
0282     platform_driver_unregister(&geodewdt_driver);
0283 }
0284 
0285 module_init(geodewdt_init);
0286 module_exit(geodewdt_exit);
0287 
0288 MODULE_AUTHOR("Advanced Micro Devices, Inc");
0289 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
0290 MODULE_LICENSE("GPL");