1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 29b0fd114SAndres Salomon /* Watchdog timer for machines with the CS5535/CS5536 companion chip 30b36086bSJordan Crouse * 40b36086bSJordan Crouse * Copyright (C) 2006-2007, Advanced Micro Devices, Inc. 59b0fd114SAndres Salomon * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk> 60b36086bSJordan Crouse */ 70b36086bSJordan Crouse 827c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 90b36086bSJordan Crouse 100b36086bSJordan Crouse #include <linux/module.h> 110b36086bSJordan Crouse #include <linux/moduleparam.h> 120b36086bSJordan Crouse #include <linux/types.h> 130b36086bSJordan Crouse #include <linux/miscdevice.h> 140b36086bSJordan Crouse #include <linux/watchdog.h> 150b36086bSJordan Crouse #include <linux/fs.h> 160b36086bSJordan Crouse #include <linux/platform_device.h> 170b36086bSJordan Crouse #include <linux/reboot.h> 18089ab079SWim Van Sebroeck #include <linux/uaccess.h> 190b36086bSJordan Crouse 209b0fd114SAndres Salomon #include <linux/cs5535.h> 210b36086bSJordan Crouse 220b36086bSJordan Crouse #define GEODEWDT_HZ 500 230b36086bSJordan Crouse #define GEODEWDT_SCALE 6 240b36086bSJordan Crouse #define GEODEWDT_MAX_SECONDS 131 250b36086bSJordan Crouse 260b36086bSJordan Crouse #define WDT_FLAGS_OPEN 1 270b36086bSJordan Crouse #define WDT_FLAGS_ORPHAN 2 280b36086bSJordan Crouse 290b36086bSJordan Crouse #define DRV_NAME "geodewdt" 300b36086bSJordan Crouse #define WATCHDOG_NAME "Geode GX/LX WDT" 310b36086bSJordan Crouse #define WATCHDOG_TIMEOUT 60 320b36086bSJordan Crouse 330b36086bSJordan Crouse static int timeout = WATCHDOG_TIMEOUT; 340b36086bSJordan Crouse module_param(timeout, int, 0); 35143a2e54SWim Van Sebroeck MODULE_PARM_DESC(timeout, 36143a2e54SWim Van Sebroeck "Watchdog timeout in seconds. 1<= timeout <=131, default=" 37143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_TIMEOUT) "."); 380b36086bSJordan Crouse 3986a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 4086a1e189SWim Van Sebroeck module_param(nowayout, bool, 0); 41143a2e54SWim Van Sebroeck MODULE_PARM_DESC(nowayout, 42143a2e54SWim Van Sebroeck "Watchdog cannot be stopped once started (default=" 43143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 440b36086bSJordan Crouse 450b36086bSJordan Crouse static struct platform_device *geodewdt_platform_device; 460b36086bSJordan Crouse static unsigned long wdt_flags; 479b0fd114SAndres Salomon static struct cs5535_mfgpt_timer *wdt_timer; 480b36086bSJordan Crouse static int safe_close; 490b36086bSJordan Crouse 500b36086bSJordan Crouse static void geodewdt_ping(void) 510b36086bSJordan Crouse { 520b36086bSJordan Crouse /* Stop the counter */ 539b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 540b36086bSJordan Crouse 550b36086bSJordan Crouse /* Reset the counter */ 569b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 570b36086bSJordan Crouse 580b36086bSJordan Crouse /* Enable the counter */ 599b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 600b36086bSJordan Crouse } 610b36086bSJordan Crouse 620b36086bSJordan Crouse static void geodewdt_disable(void) 630b36086bSJordan Crouse { 649b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 659b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 660b36086bSJordan Crouse } 670b36086bSJordan Crouse 680b36086bSJordan Crouse static int geodewdt_set_heartbeat(int val) 690b36086bSJordan Crouse { 700b36086bSJordan Crouse if (val < 1 || val > GEODEWDT_MAX_SECONDS) 710b36086bSJordan Crouse return -EINVAL; 720b36086bSJordan Crouse 739b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 749b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ); 759b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 769b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 770b36086bSJordan Crouse 780b36086bSJordan Crouse timeout = val; 790b36086bSJordan Crouse return 0; 800b36086bSJordan Crouse } 810b36086bSJordan Crouse 827944d3a5SWim Van Sebroeck static int geodewdt_open(struct inode *inode, struct file *file) 830b36086bSJordan Crouse { 840b36086bSJordan Crouse if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags)) 850b36086bSJordan Crouse return -EBUSY; 860b36086bSJordan Crouse 870b36086bSJordan Crouse if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags)) 880b36086bSJordan Crouse __module_get(THIS_MODULE); 890b36086bSJordan Crouse 900b36086bSJordan Crouse geodewdt_ping(); 91c5bf68feSKirill Smelkov return stream_open(inode, file); 920b36086bSJordan Crouse } 930b36086bSJordan Crouse 947944d3a5SWim Van Sebroeck static int geodewdt_release(struct inode *inode, struct file *file) 950b36086bSJordan Crouse { 960b36086bSJordan Crouse if (safe_close) { 970b36086bSJordan Crouse geodewdt_disable(); 980b36086bSJordan Crouse module_put(THIS_MODULE); 997944d3a5SWim Van Sebroeck } else { 10027c766aaSJoe Perches pr_crit("Unexpected close - watchdog is not stopping\n"); 1010b36086bSJordan Crouse geodewdt_ping(); 1020b36086bSJordan Crouse 1030b36086bSJordan Crouse set_bit(WDT_FLAGS_ORPHAN, &wdt_flags); 1040b36086bSJordan Crouse } 1050b36086bSJordan Crouse 1060b36086bSJordan Crouse clear_bit(WDT_FLAGS_OPEN, &wdt_flags); 1070b36086bSJordan Crouse safe_close = 0; 1080b36086bSJordan Crouse return 0; 1090b36086bSJordan Crouse } 1100b36086bSJordan Crouse 1117944d3a5SWim Van Sebroeck static ssize_t geodewdt_write(struct file *file, const char __user *data, 1127944d3a5SWim Van Sebroeck size_t len, loff_t *ppos) 1130b36086bSJordan Crouse { 1140b36086bSJordan Crouse if (len) { 1150b36086bSJordan Crouse if (!nowayout) { 1160b36086bSJordan Crouse size_t i; 1170b36086bSJordan Crouse safe_close = 0; 1180b36086bSJordan Crouse 1190b36086bSJordan Crouse for (i = 0; i != len; i++) { 1200b36086bSJordan Crouse char c; 1210b36086bSJordan Crouse 1220b36086bSJordan Crouse if (get_user(c, data + i)) 1230b36086bSJordan Crouse return -EFAULT; 1240b36086bSJordan Crouse 1250b36086bSJordan Crouse if (c == 'V') 1260b36086bSJordan Crouse safe_close = 1; 1270b36086bSJordan Crouse } 1280b36086bSJordan Crouse } 1290b36086bSJordan Crouse 1300b36086bSJordan Crouse geodewdt_ping(); 1310b36086bSJordan Crouse } 1320b36086bSJordan Crouse return len; 1330b36086bSJordan Crouse } 1340b36086bSJordan Crouse 1357275fc8cSWim Van Sebroeck static long geodewdt_ioctl(struct file *file, unsigned int cmd, 1367275fc8cSWim Van Sebroeck unsigned long arg) 1370b36086bSJordan Crouse { 1380b36086bSJordan Crouse void __user *argp = (void __user *)arg; 1390b36086bSJordan Crouse int __user *p = argp; 1400b36086bSJordan Crouse int interval; 1410b36086bSJordan Crouse 14242747d71SWim Van Sebroeck static const struct watchdog_info ident = { 1430b36086bSJordan Crouse .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING 1440b36086bSJordan Crouse | WDIOF_MAGICCLOSE, 1450b36086bSJordan Crouse .firmware_version = 1, 1460b36086bSJordan Crouse .identity = WATCHDOG_NAME, 1470b36086bSJordan Crouse }; 1480b36086bSJordan Crouse 1490b36086bSJordan Crouse switch (cmd) { 1500b36086bSJordan Crouse case WDIOC_GETSUPPORT: 1510b36086bSJordan Crouse return copy_to_user(argp, &ident, 1520b36086bSJordan Crouse sizeof(ident)) ? -EFAULT : 0; 1530b36086bSJordan Crouse break; 1540b36086bSJordan Crouse 1550b36086bSJordan Crouse case WDIOC_GETSTATUS: 1560b36086bSJordan Crouse case WDIOC_GETBOOTSTATUS: 1570b36086bSJordan Crouse return put_user(0, p); 1580b36086bSJordan Crouse 1590b36086bSJordan Crouse case WDIOC_SETOPTIONS: 1600b36086bSJordan Crouse { 1610b36086bSJordan Crouse int options, ret = -EINVAL; 1620b36086bSJordan Crouse 1630b36086bSJordan Crouse if (get_user(options, p)) 1640b36086bSJordan Crouse return -EFAULT; 1650b36086bSJordan Crouse 1660b36086bSJordan Crouse if (options & WDIOS_DISABLECARD) { 1670b36086bSJordan Crouse geodewdt_disable(); 1680b36086bSJordan Crouse ret = 0; 1690b36086bSJordan Crouse } 1700b36086bSJordan Crouse 1710b36086bSJordan Crouse if (options & WDIOS_ENABLECARD) { 1720b36086bSJordan Crouse geodewdt_ping(); 1730b36086bSJordan Crouse ret = 0; 1740b36086bSJordan Crouse } 1750b36086bSJordan Crouse 1760b36086bSJordan Crouse return ret; 1770b36086bSJordan Crouse } 1780c06090cSWim Van Sebroeck case WDIOC_KEEPALIVE: 1790c06090cSWim Van Sebroeck geodewdt_ping(); 1800c06090cSWim Van Sebroeck return 0; 1810c06090cSWim Van Sebroeck 1820c06090cSWim Van Sebroeck case WDIOC_SETTIMEOUT: 1830c06090cSWim Van Sebroeck if (get_user(interval, p)) 1840c06090cSWim Van Sebroeck return -EFAULT; 1850c06090cSWim Van Sebroeck 1860c06090cSWim Van Sebroeck if (geodewdt_set_heartbeat(interval)) 1870c06090cSWim Van Sebroeck return -EINVAL; 1880c06090cSWim Van Sebroeck /* Fall through */ 1890c06090cSWim Van Sebroeck case WDIOC_GETTIMEOUT: 1900c06090cSWim Van Sebroeck return put_user(timeout, p); 1910c06090cSWim Van Sebroeck 1920b36086bSJordan Crouse default: 1930b36086bSJordan Crouse return -ENOTTY; 1940b36086bSJordan Crouse } 1950b36086bSJordan Crouse 1960b36086bSJordan Crouse return 0; 1970b36086bSJordan Crouse } 1980b36086bSJordan Crouse 1990b36086bSJordan Crouse static const struct file_operations geodewdt_fops = { 2000b36086bSJordan Crouse .owner = THIS_MODULE, 2010b36086bSJordan Crouse .llseek = no_llseek, 2020b36086bSJordan Crouse .write = geodewdt_write, 2037275fc8cSWim Van Sebroeck .unlocked_ioctl = geodewdt_ioctl, 2040b36086bSJordan Crouse .open = geodewdt_open, 2050b36086bSJordan Crouse .release = geodewdt_release, 2060b36086bSJordan Crouse }; 2070b36086bSJordan Crouse 2080b36086bSJordan Crouse static struct miscdevice geodewdt_miscdev = { 2090b36086bSJordan Crouse .minor = WATCHDOG_MINOR, 2100b36086bSJordan Crouse .name = "watchdog", 2117944d3a5SWim Van Sebroeck .fops = &geodewdt_fops, 2120b36086bSJordan Crouse }; 2130b36086bSJordan Crouse 21478411be4SJean Delvare static int __init geodewdt_probe(struct platform_device *dev) 2150b36086bSJordan Crouse { 2169b0fd114SAndres Salomon int ret; 2170b36086bSJordan Crouse 2189b0fd114SAndres Salomon wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING); 2199b0fd114SAndres Salomon if (!wdt_timer) { 22027c766aaSJoe Perches pr_err("No timers were available\n"); 2210b36086bSJordan Crouse return -ENODEV; 2220b36086bSJordan Crouse } 2230b36086bSJordan Crouse 2240b36086bSJordan Crouse /* Set up the timer */ 2250b36086bSJordan Crouse 2269b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 2270b36086bSJordan Crouse GEODEWDT_SCALE | (3 << 8)); 2280b36086bSJordan Crouse 2290b36086bSJordan Crouse /* Set up comparator 2 to reset when the event fires */ 2309b0fd114SAndres Salomon cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1); 2310b36086bSJordan Crouse 2320b36086bSJordan Crouse /* Set up the initial timeout */ 2330b36086bSJordan Crouse 2349b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, 2350b36086bSJordan Crouse timeout * GEODEWDT_HZ); 2360b36086bSJordan Crouse 2370b36086bSJordan Crouse ret = misc_register(&geodewdt_miscdev); 2380b36086bSJordan Crouse 2390b36086bSJordan Crouse return ret; 2400b36086bSJordan Crouse } 2410b36086bSJordan Crouse 2424b12b896SBill Pemberton static int geodewdt_remove(struct platform_device *dev) 2430b36086bSJordan Crouse { 2440b36086bSJordan Crouse misc_deregister(&geodewdt_miscdev); 2450b36086bSJordan Crouse return 0; 2460b36086bSJordan Crouse } 2470b36086bSJordan Crouse 2487944d3a5SWim Van Sebroeck static void geodewdt_shutdown(struct platform_device *dev) 2490b36086bSJordan Crouse { 2500b36086bSJordan Crouse geodewdt_disable(); 2510b36086bSJordan Crouse } 2520b36086bSJordan Crouse 2530b36086bSJordan Crouse static struct platform_driver geodewdt_driver = { 25482268714SBill Pemberton .remove = geodewdt_remove, 2550b36086bSJordan Crouse .shutdown = geodewdt_shutdown, 2560b36086bSJordan Crouse .driver = { 2570b36086bSJordan Crouse .name = DRV_NAME, 2580b36086bSJordan Crouse }, 2590b36086bSJordan Crouse }; 2600b36086bSJordan Crouse 2617944d3a5SWim Van Sebroeck static int __init geodewdt_init(void) 2620b36086bSJordan Crouse { 2630b36086bSJordan Crouse int ret; 2640b36086bSJordan Crouse 265143a2e54SWim Van Sebroeck geodewdt_platform_device = platform_device_register_simple(DRV_NAME, 266143a2e54SWim Van Sebroeck -1, NULL, 0); 26778411be4SJean Delvare if (IS_ERR(geodewdt_platform_device)) 26878411be4SJean Delvare return PTR_ERR(geodewdt_platform_device); 26978411be4SJean Delvare 27078411be4SJean Delvare ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe); 27178411be4SJean Delvare if (ret) 2720b36086bSJordan Crouse goto err; 2730b36086bSJordan Crouse 2740b36086bSJordan Crouse return 0; 2750b36086bSJordan Crouse err: 27678411be4SJean Delvare platform_device_unregister(geodewdt_platform_device); 2770b36086bSJordan Crouse return ret; 2780b36086bSJordan Crouse } 2790b36086bSJordan Crouse 2807944d3a5SWim Van Sebroeck static void __exit geodewdt_exit(void) 2810b36086bSJordan Crouse { 2820b36086bSJordan Crouse platform_device_unregister(geodewdt_platform_device); 2830b36086bSJordan Crouse platform_driver_unregister(&geodewdt_driver); 2840b36086bSJordan Crouse } 2850b36086bSJordan Crouse 2860b36086bSJordan Crouse module_init(geodewdt_init); 2870b36086bSJordan Crouse module_exit(geodewdt_exit); 2880b36086bSJordan Crouse 2890b36086bSJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc"); 2900b36086bSJordan Crouse MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver"); 2910b36086bSJordan Crouse MODULE_LICENSE("GPL"); 292