19b0fd114SAndres Salomon /* Watchdog timer for machines with the CS5535/CS5536 companion chip 20b36086bSJordan Crouse * 30b36086bSJordan Crouse * Copyright (C) 2006-2007, Advanced Micro Devices, Inc. 49b0fd114SAndres Salomon * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk> 50b36086bSJordan Crouse * 60b36086bSJordan Crouse * This program is free software; you can redistribute it and/or 70b36086bSJordan Crouse * modify it under the terms of the GNU General Public License 80b36086bSJordan Crouse * as published by the Free Software Foundation; either version 90b36086bSJordan Crouse * 2 of the License, or (at your option) any later version. 100b36086bSJordan Crouse */ 110b36086bSJordan Crouse 1227c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 130b36086bSJordan Crouse 140b36086bSJordan Crouse #include <linux/module.h> 150b36086bSJordan Crouse #include <linux/moduleparam.h> 160b36086bSJordan Crouse #include <linux/types.h> 170b36086bSJordan Crouse #include <linux/miscdevice.h> 180b36086bSJordan Crouse #include <linux/watchdog.h> 190b36086bSJordan Crouse #include <linux/fs.h> 200b36086bSJordan Crouse #include <linux/platform_device.h> 210b36086bSJordan Crouse #include <linux/reboot.h> 22089ab079SWim Van Sebroeck #include <linux/uaccess.h> 230b36086bSJordan Crouse 249b0fd114SAndres Salomon #include <linux/cs5535.h> 250b36086bSJordan Crouse 260b36086bSJordan Crouse #define GEODEWDT_HZ 500 270b36086bSJordan Crouse #define GEODEWDT_SCALE 6 280b36086bSJordan Crouse #define GEODEWDT_MAX_SECONDS 131 290b36086bSJordan Crouse 300b36086bSJordan Crouse #define WDT_FLAGS_OPEN 1 310b36086bSJordan Crouse #define WDT_FLAGS_ORPHAN 2 320b36086bSJordan Crouse 330b36086bSJordan Crouse #define DRV_NAME "geodewdt" 340b36086bSJordan Crouse #define WATCHDOG_NAME "Geode GX/LX WDT" 350b36086bSJordan Crouse #define WATCHDOG_TIMEOUT 60 360b36086bSJordan Crouse 370b36086bSJordan Crouse static int timeout = WATCHDOG_TIMEOUT; 380b36086bSJordan Crouse module_param(timeout, int, 0); 39143a2e54SWim Van Sebroeck MODULE_PARM_DESC(timeout, 40143a2e54SWim Van Sebroeck "Watchdog timeout in seconds. 1<= timeout <=131, default=" 41143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_TIMEOUT) "."); 420b36086bSJordan Crouse 4386a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 4486a1e189SWim Van Sebroeck module_param(nowayout, bool, 0); 45143a2e54SWim Van Sebroeck MODULE_PARM_DESC(nowayout, 46143a2e54SWim Van Sebroeck "Watchdog cannot be stopped once started (default=" 47143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 480b36086bSJordan Crouse 490b36086bSJordan Crouse static struct platform_device *geodewdt_platform_device; 500b36086bSJordan Crouse static unsigned long wdt_flags; 519b0fd114SAndres Salomon static struct cs5535_mfgpt_timer *wdt_timer; 520b36086bSJordan Crouse static int safe_close; 530b36086bSJordan Crouse 540b36086bSJordan Crouse static void geodewdt_ping(void) 550b36086bSJordan Crouse { 560b36086bSJordan Crouse /* Stop the counter */ 579b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 580b36086bSJordan Crouse 590b36086bSJordan Crouse /* Reset the counter */ 609b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 610b36086bSJordan Crouse 620b36086bSJordan Crouse /* Enable the counter */ 639b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 640b36086bSJordan Crouse } 650b36086bSJordan Crouse 660b36086bSJordan Crouse static void geodewdt_disable(void) 670b36086bSJordan Crouse { 689b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 699b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 700b36086bSJordan Crouse } 710b36086bSJordan Crouse 720b36086bSJordan Crouse static int geodewdt_set_heartbeat(int val) 730b36086bSJordan Crouse { 740b36086bSJordan Crouse if (val < 1 || val > GEODEWDT_MAX_SECONDS) 750b36086bSJordan Crouse return -EINVAL; 760b36086bSJordan Crouse 779b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 789b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ); 799b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 809b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 810b36086bSJordan Crouse 820b36086bSJordan Crouse timeout = val; 830b36086bSJordan Crouse return 0; 840b36086bSJordan Crouse } 850b36086bSJordan Crouse 867944d3a5SWim Van Sebroeck static int geodewdt_open(struct inode *inode, struct file *file) 870b36086bSJordan Crouse { 880b36086bSJordan Crouse if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags)) 890b36086bSJordan Crouse return -EBUSY; 900b36086bSJordan Crouse 910b36086bSJordan Crouse if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags)) 920b36086bSJordan Crouse __module_get(THIS_MODULE); 930b36086bSJordan Crouse 940b36086bSJordan Crouse geodewdt_ping(); 95*c5bf68feSKirill Smelkov return stream_open(inode, file); 960b36086bSJordan Crouse } 970b36086bSJordan Crouse 987944d3a5SWim Van Sebroeck static int geodewdt_release(struct inode *inode, struct file *file) 990b36086bSJordan Crouse { 1000b36086bSJordan Crouse if (safe_close) { 1010b36086bSJordan Crouse geodewdt_disable(); 1020b36086bSJordan Crouse module_put(THIS_MODULE); 1037944d3a5SWim Van Sebroeck } else { 10427c766aaSJoe Perches pr_crit("Unexpected close - watchdog is not stopping\n"); 1050b36086bSJordan Crouse geodewdt_ping(); 1060b36086bSJordan Crouse 1070b36086bSJordan Crouse set_bit(WDT_FLAGS_ORPHAN, &wdt_flags); 1080b36086bSJordan Crouse } 1090b36086bSJordan Crouse 1100b36086bSJordan Crouse clear_bit(WDT_FLAGS_OPEN, &wdt_flags); 1110b36086bSJordan Crouse safe_close = 0; 1120b36086bSJordan Crouse return 0; 1130b36086bSJordan Crouse } 1140b36086bSJordan Crouse 1157944d3a5SWim Van Sebroeck static ssize_t geodewdt_write(struct file *file, const char __user *data, 1167944d3a5SWim Van Sebroeck size_t len, loff_t *ppos) 1170b36086bSJordan Crouse { 1180b36086bSJordan Crouse if (len) { 1190b36086bSJordan Crouse if (!nowayout) { 1200b36086bSJordan Crouse size_t i; 1210b36086bSJordan Crouse safe_close = 0; 1220b36086bSJordan Crouse 1230b36086bSJordan Crouse for (i = 0; i != len; i++) { 1240b36086bSJordan Crouse char c; 1250b36086bSJordan Crouse 1260b36086bSJordan Crouse if (get_user(c, data + i)) 1270b36086bSJordan Crouse return -EFAULT; 1280b36086bSJordan Crouse 1290b36086bSJordan Crouse if (c == 'V') 1300b36086bSJordan Crouse safe_close = 1; 1310b36086bSJordan Crouse } 1320b36086bSJordan Crouse } 1330b36086bSJordan Crouse 1340b36086bSJordan Crouse geodewdt_ping(); 1350b36086bSJordan Crouse } 1360b36086bSJordan Crouse return len; 1370b36086bSJordan Crouse } 1380b36086bSJordan Crouse 1397275fc8cSWim Van Sebroeck static long geodewdt_ioctl(struct file *file, unsigned int cmd, 1407275fc8cSWim Van Sebroeck unsigned long arg) 1410b36086bSJordan Crouse { 1420b36086bSJordan Crouse void __user *argp = (void __user *)arg; 1430b36086bSJordan Crouse int __user *p = argp; 1440b36086bSJordan Crouse int interval; 1450b36086bSJordan Crouse 14642747d71SWim Van Sebroeck static const struct watchdog_info ident = { 1470b36086bSJordan Crouse .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING 1480b36086bSJordan Crouse | WDIOF_MAGICCLOSE, 1490b36086bSJordan Crouse .firmware_version = 1, 1500b36086bSJordan Crouse .identity = WATCHDOG_NAME, 1510b36086bSJordan Crouse }; 1520b36086bSJordan Crouse 1530b36086bSJordan Crouse switch (cmd) { 1540b36086bSJordan Crouse case WDIOC_GETSUPPORT: 1550b36086bSJordan Crouse return copy_to_user(argp, &ident, 1560b36086bSJordan Crouse sizeof(ident)) ? -EFAULT : 0; 1570b36086bSJordan Crouse break; 1580b36086bSJordan Crouse 1590b36086bSJordan Crouse case WDIOC_GETSTATUS: 1600b36086bSJordan Crouse case WDIOC_GETBOOTSTATUS: 1610b36086bSJordan Crouse return put_user(0, p); 1620b36086bSJordan Crouse 1630b36086bSJordan Crouse case WDIOC_SETOPTIONS: 1640b36086bSJordan Crouse { 1650b36086bSJordan Crouse int options, ret = -EINVAL; 1660b36086bSJordan Crouse 1670b36086bSJordan Crouse if (get_user(options, p)) 1680b36086bSJordan Crouse return -EFAULT; 1690b36086bSJordan Crouse 1700b36086bSJordan Crouse if (options & WDIOS_DISABLECARD) { 1710b36086bSJordan Crouse geodewdt_disable(); 1720b36086bSJordan Crouse ret = 0; 1730b36086bSJordan Crouse } 1740b36086bSJordan Crouse 1750b36086bSJordan Crouse if (options & WDIOS_ENABLECARD) { 1760b36086bSJordan Crouse geodewdt_ping(); 1770b36086bSJordan Crouse ret = 0; 1780b36086bSJordan Crouse } 1790b36086bSJordan Crouse 1800b36086bSJordan Crouse return ret; 1810b36086bSJordan Crouse } 1820c06090cSWim Van Sebroeck case WDIOC_KEEPALIVE: 1830c06090cSWim Van Sebroeck geodewdt_ping(); 1840c06090cSWim Van Sebroeck return 0; 1850c06090cSWim Van Sebroeck 1860c06090cSWim Van Sebroeck case WDIOC_SETTIMEOUT: 1870c06090cSWim Van Sebroeck if (get_user(interval, p)) 1880c06090cSWim Van Sebroeck return -EFAULT; 1890c06090cSWim Van Sebroeck 1900c06090cSWim Van Sebroeck if (geodewdt_set_heartbeat(interval)) 1910c06090cSWim Van Sebroeck return -EINVAL; 1920c06090cSWim Van Sebroeck /* Fall through */ 1930c06090cSWim Van Sebroeck case WDIOC_GETTIMEOUT: 1940c06090cSWim Van Sebroeck return put_user(timeout, p); 1950c06090cSWim Van Sebroeck 1960b36086bSJordan Crouse default: 1970b36086bSJordan Crouse return -ENOTTY; 1980b36086bSJordan Crouse } 1990b36086bSJordan Crouse 2000b36086bSJordan Crouse return 0; 2010b36086bSJordan Crouse } 2020b36086bSJordan Crouse 2030b36086bSJordan Crouse static const struct file_operations geodewdt_fops = { 2040b36086bSJordan Crouse .owner = THIS_MODULE, 2050b36086bSJordan Crouse .llseek = no_llseek, 2060b36086bSJordan Crouse .write = geodewdt_write, 2077275fc8cSWim Van Sebroeck .unlocked_ioctl = geodewdt_ioctl, 2080b36086bSJordan Crouse .open = geodewdt_open, 2090b36086bSJordan Crouse .release = geodewdt_release, 2100b36086bSJordan Crouse }; 2110b36086bSJordan Crouse 2120b36086bSJordan Crouse static struct miscdevice geodewdt_miscdev = { 2130b36086bSJordan Crouse .minor = WATCHDOG_MINOR, 2140b36086bSJordan Crouse .name = "watchdog", 2157944d3a5SWim Van Sebroeck .fops = &geodewdt_fops, 2160b36086bSJordan Crouse }; 2170b36086bSJordan Crouse 21878411be4SJean Delvare static int __init geodewdt_probe(struct platform_device *dev) 2190b36086bSJordan Crouse { 2209b0fd114SAndres Salomon int ret; 2210b36086bSJordan Crouse 2229b0fd114SAndres Salomon wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING); 2239b0fd114SAndres Salomon if (!wdt_timer) { 22427c766aaSJoe Perches pr_err("No timers were available\n"); 2250b36086bSJordan Crouse return -ENODEV; 2260b36086bSJordan Crouse } 2270b36086bSJordan Crouse 2280b36086bSJordan Crouse /* Set up the timer */ 2290b36086bSJordan Crouse 2309b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 2310b36086bSJordan Crouse GEODEWDT_SCALE | (3 << 8)); 2320b36086bSJordan Crouse 2330b36086bSJordan Crouse /* Set up comparator 2 to reset when the event fires */ 2349b0fd114SAndres Salomon cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1); 2350b36086bSJordan Crouse 2360b36086bSJordan Crouse /* Set up the initial timeout */ 2370b36086bSJordan Crouse 2389b0fd114SAndres Salomon cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, 2390b36086bSJordan Crouse timeout * GEODEWDT_HZ); 2400b36086bSJordan Crouse 2410b36086bSJordan Crouse ret = misc_register(&geodewdt_miscdev); 2420b36086bSJordan Crouse 2430b36086bSJordan Crouse return ret; 2440b36086bSJordan Crouse } 2450b36086bSJordan Crouse 2464b12b896SBill Pemberton static int geodewdt_remove(struct platform_device *dev) 2470b36086bSJordan Crouse { 2480b36086bSJordan Crouse misc_deregister(&geodewdt_miscdev); 2490b36086bSJordan Crouse return 0; 2500b36086bSJordan Crouse } 2510b36086bSJordan Crouse 2527944d3a5SWim Van Sebroeck static void geodewdt_shutdown(struct platform_device *dev) 2530b36086bSJordan Crouse { 2540b36086bSJordan Crouse geodewdt_disable(); 2550b36086bSJordan Crouse } 2560b36086bSJordan Crouse 2570b36086bSJordan Crouse static struct platform_driver geodewdt_driver = { 25882268714SBill Pemberton .remove = geodewdt_remove, 2590b36086bSJordan Crouse .shutdown = geodewdt_shutdown, 2600b36086bSJordan Crouse .driver = { 2610b36086bSJordan Crouse .name = DRV_NAME, 2620b36086bSJordan Crouse }, 2630b36086bSJordan Crouse }; 2640b36086bSJordan Crouse 2657944d3a5SWim Van Sebroeck static int __init geodewdt_init(void) 2660b36086bSJordan Crouse { 2670b36086bSJordan Crouse int ret; 2680b36086bSJordan Crouse 269143a2e54SWim Van Sebroeck geodewdt_platform_device = platform_device_register_simple(DRV_NAME, 270143a2e54SWim Van Sebroeck -1, NULL, 0); 27178411be4SJean Delvare if (IS_ERR(geodewdt_platform_device)) 27278411be4SJean Delvare return PTR_ERR(geodewdt_platform_device); 27378411be4SJean Delvare 27478411be4SJean Delvare ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe); 27578411be4SJean Delvare if (ret) 2760b36086bSJordan Crouse goto err; 2770b36086bSJordan Crouse 2780b36086bSJordan Crouse return 0; 2790b36086bSJordan Crouse err: 28078411be4SJean Delvare platform_device_unregister(geodewdt_platform_device); 2810b36086bSJordan Crouse return ret; 2820b36086bSJordan Crouse } 2830b36086bSJordan Crouse 2847944d3a5SWim Van Sebroeck static void __exit geodewdt_exit(void) 2850b36086bSJordan Crouse { 2860b36086bSJordan Crouse platform_device_unregister(geodewdt_platform_device); 2870b36086bSJordan Crouse platform_driver_unregister(&geodewdt_driver); 2880b36086bSJordan Crouse } 2890b36086bSJordan Crouse 2900b36086bSJordan Crouse module_init(geodewdt_init); 2910b36086bSJordan Crouse module_exit(geodewdt_exit); 2920b36086bSJordan Crouse 2930b36086bSJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc"); 2940b36086bSJordan Crouse MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver"); 2950b36086bSJordan Crouse MODULE_LICENSE("GPL"); 296