10b36086bSJordan Crouse /* Watchdog timer for the Geode GX/LX with the CS5535/CS5536 companion chip 20b36086bSJordan Crouse * 30b36086bSJordan Crouse * Copyright (C) 2006-2007, Advanced Micro Devices, Inc. 40b36086bSJordan Crouse * 50b36086bSJordan Crouse * This program is free software; you can redistribute it and/or 60b36086bSJordan Crouse * modify it under the terms of the GNU General Public License 70b36086bSJordan Crouse * as published by the Free Software Foundation; either version 80b36086bSJordan Crouse * 2 of the License, or (at your option) any later version. 90b36086bSJordan Crouse */ 100b36086bSJordan Crouse 110b36086bSJordan Crouse 120b36086bSJordan Crouse #include <linux/module.h> 130b36086bSJordan Crouse #include <linux/moduleparam.h> 140b36086bSJordan Crouse #include <linux/types.h> 150b36086bSJordan Crouse #include <linux/miscdevice.h> 160b36086bSJordan Crouse #include <linux/watchdog.h> 170b36086bSJordan Crouse #include <linux/fs.h> 180b36086bSJordan Crouse #include <linux/platform_device.h> 190b36086bSJordan Crouse #include <linux/reboot.h> 20089ab079SWim Van Sebroeck #include <linux/uaccess.h> 210b36086bSJordan Crouse 220b36086bSJordan Crouse #include <asm/geode.h> 230b36086bSJordan Crouse 240b36086bSJordan Crouse #define GEODEWDT_HZ 500 250b36086bSJordan Crouse #define GEODEWDT_SCALE 6 260b36086bSJordan Crouse #define GEODEWDT_MAX_SECONDS 131 270b36086bSJordan Crouse 280b36086bSJordan Crouse #define WDT_FLAGS_OPEN 1 290b36086bSJordan Crouse #define WDT_FLAGS_ORPHAN 2 300b36086bSJordan Crouse 310b36086bSJordan Crouse #define DRV_NAME "geodewdt" 320b36086bSJordan Crouse #define WATCHDOG_NAME "Geode GX/LX WDT" 330b36086bSJordan Crouse #define WATCHDOG_TIMEOUT 60 340b36086bSJordan Crouse 350b36086bSJordan Crouse static int timeout = WATCHDOG_TIMEOUT; 360b36086bSJordan Crouse module_param(timeout, int, 0); 37*143a2e54SWim Van Sebroeck MODULE_PARM_DESC(timeout, 38*143a2e54SWim Van Sebroeck "Watchdog timeout in seconds. 1<= timeout <=131, default=" 39*143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_TIMEOUT) "."); 400b36086bSJordan Crouse 410b36086bSJordan Crouse static int nowayout = WATCHDOG_NOWAYOUT; 420b36086bSJordan Crouse module_param(nowayout, int, 0); 43*143a2e54SWim Van Sebroeck MODULE_PARM_DESC(nowayout, 44*143a2e54SWim Van Sebroeck "Watchdog cannot be stopped once started (default=" 45*143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 460b36086bSJordan Crouse 470b36086bSJordan Crouse static struct platform_device *geodewdt_platform_device; 480b36086bSJordan Crouse static unsigned long wdt_flags; 490b36086bSJordan Crouse static int wdt_timer; 500b36086bSJordan Crouse static int safe_close; 510b36086bSJordan Crouse 520b36086bSJordan Crouse static void geodewdt_ping(void) 530b36086bSJordan Crouse { 540b36086bSJordan Crouse /* Stop the counter */ 550b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 560b36086bSJordan Crouse 570b36086bSJordan Crouse /* Reset the counter */ 580b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 590b36086bSJordan Crouse 600b36086bSJordan Crouse /* Enable the counter */ 610b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 620b36086bSJordan Crouse } 630b36086bSJordan Crouse 640b36086bSJordan Crouse static void geodewdt_disable(void) 650b36086bSJordan Crouse { 660b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 670b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 680b36086bSJordan Crouse } 690b36086bSJordan Crouse 700b36086bSJordan Crouse static int geodewdt_set_heartbeat(int val) 710b36086bSJordan Crouse { 720b36086bSJordan Crouse if (val < 1 || val > GEODEWDT_MAX_SECONDS) 730b36086bSJordan Crouse return -EINVAL; 740b36086bSJordan Crouse 750b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0); 760b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ); 770b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0); 780b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); 790b36086bSJordan Crouse 800b36086bSJordan Crouse timeout = val; 810b36086bSJordan Crouse return 0; 820b36086bSJordan Crouse } 830b36086bSJordan Crouse 847944d3a5SWim Van Sebroeck static int geodewdt_open(struct inode *inode, struct file *file) 850b36086bSJordan Crouse { 860b36086bSJordan Crouse if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags)) 870b36086bSJordan Crouse return -EBUSY; 880b36086bSJordan Crouse 890b36086bSJordan Crouse if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags)) 900b36086bSJordan Crouse __module_get(THIS_MODULE); 910b36086bSJordan Crouse 920b36086bSJordan Crouse geodewdt_ping(); 930b36086bSJordan Crouse return nonseekable_open(inode, file); 940b36086bSJordan Crouse } 950b36086bSJordan Crouse 967944d3a5SWim Van Sebroeck static int geodewdt_release(struct inode *inode, struct file *file) 970b36086bSJordan Crouse { 980b36086bSJordan Crouse if (safe_close) { 990b36086bSJordan Crouse geodewdt_disable(); 1000b36086bSJordan Crouse module_put(THIS_MODULE); 1017944d3a5SWim Van Sebroeck } else { 1020b36086bSJordan Crouse printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n"); 1030b36086bSJordan Crouse geodewdt_ping(); 1040b36086bSJordan Crouse 1050b36086bSJordan Crouse set_bit(WDT_FLAGS_ORPHAN, &wdt_flags); 1060b36086bSJordan Crouse } 1070b36086bSJordan Crouse 1080b36086bSJordan Crouse clear_bit(WDT_FLAGS_OPEN, &wdt_flags); 1090b36086bSJordan Crouse safe_close = 0; 1100b36086bSJordan Crouse return 0; 1110b36086bSJordan Crouse } 1120b36086bSJordan Crouse 1137944d3a5SWim Van Sebroeck static ssize_t geodewdt_write(struct file *file, const char __user *data, 1147944d3a5SWim Van Sebroeck size_t len, loff_t *ppos) 1150b36086bSJordan Crouse { 1160b36086bSJordan Crouse if (len) { 1170b36086bSJordan Crouse if (!nowayout) { 1180b36086bSJordan Crouse size_t i; 1190b36086bSJordan Crouse safe_close = 0; 1200b36086bSJordan Crouse 1210b36086bSJordan Crouse for (i = 0; i != len; i++) { 1220b36086bSJordan Crouse char c; 1230b36086bSJordan Crouse 1240b36086bSJordan Crouse if (get_user(c, data + i)) 1250b36086bSJordan Crouse return -EFAULT; 1260b36086bSJordan Crouse 1270b36086bSJordan Crouse if (c == 'V') 1280b36086bSJordan Crouse safe_close = 1; 1290b36086bSJordan Crouse } 1300b36086bSJordan Crouse } 1310b36086bSJordan Crouse 1320b36086bSJordan Crouse geodewdt_ping(); 1330b36086bSJordan Crouse } 1340b36086bSJordan Crouse return len; 1350b36086bSJordan Crouse } 1360b36086bSJordan Crouse 1377275fc8cSWim Van Sebroeck static long geodewdt_ioctl(struct file *file, unsigned int cmd, 1387275fc8cSWim Van Sebroeck unsigned long arg) 1390b36086bSJordan Crouse { 1400b36086bSJordan Crouse void __user *argp = (void __user *)arg; 1410b36086bSJordan Crouse int __user *p = argp; 1420b36086bSJordan Crouse int interval; 1430b36086bSJordan Crouse 1440b36086bSJordan Crouse static struct watchdog_info ident = { 1450b36086bSJordan Crouse .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING 1460b36086bSJordan Crouse | WDIOF_MAGICCLOSE, 1470b36086bSJordan Crouse .firmware_version = 1, 1480b36086bSJordan Crouse .identity = WATCHDOG_NAME, 1490b36086bSJordan Crouse }; 1500b36086bSJordan Crouse 1510b36086bSJordan Crouse switch (cmd) { 1520b36086bSJordan Crouse case WDIOC_GETSUPPORT: 1530b36086bSJordan Crouse return copy_to_user(argp, &ident, 1540b36086bSJordan Crouse sizeof(ident)) ? -EFAULT : 0; 1550b36086bSJordan Crouse break; 1560b36086bSJordan Crouse 1570b36086bSJordan Crouse case WDIOC_GETSTATUS: 1580b36086bSJordan Crouse case WDIOC_GETBOOTSTATUS: 1590b36086bSJordan Crouse return put_user(0, p); 1600b36086bSJordan Crouse 1610b36086bSJordan Crouse case WDIOC_SETOPTIONS: 1620b36086bSJordan Crouse { 1630b36086bSJordan Crouse int options, ret = -EINVAL; 1640b36086bSJordan Crouse 1650b36086bSJordan Crouse if (get_user(options, p)) 1660b36086bSJordan Crouse return -EFAULT; 1670b36086bSJordan Crouse 1680b36086bSJordan Crouse if (options & WDIOS_DISABLECARD) { 1690b36086bSJordan Crouse geodewdt_disable(); 1700b36086bSJordan Crouse ret = 0; 1710b36086bSJordan Crouse } 1720b36086bSJordan Crouse 1730b36086bSJordan Crouse if (options & WDIOS_ENABLECARD) { 1740b36086bSJordan Crouse geodewdt_ping(); 1750b36086bSJordan Crouse ret = 0; 1760b36086bSJordan Crouse } 1770b36086bSJordan Crouse 1780b36086bSJordan Crouse return ret; 1790b36086bSJordan Crouse } 1800c06090cSWim Van Sebroeck case WDIOC_KEEPALIVE: 1810c06090cSWim Van Sebroeck geodewdt_ping(); 1820c06090cSWim Van Sebroeck return 0; 1830c06090cSWim Van Sebroeck 1840c06090cSWim Van Sebroeck case WDIOC_SETTIMEOUT: 1850c06090cSWim Van Sebroeck if (get_user(interval, p)) 1860c06090cSWim Van Sebroeck return -EFAULT; 1870c06090cSWim Van Sebroeck 1880c06090cSWim Van Sebroeck if (geodewdt_set_heartbeat(interval)) 1890c06090cSWim Van Sebroeck return -EINVAL; 1900c06090cSWim Van Sebroeck /* Fall through */ 1910c06090cSWim Van Sebroeck case WDIOC_GETTIMEOUT: 1920c06090cSWim Van Sebroeck return put_user(timeout, p); 1930c06090cSWim Van Sebroeck 1940b36086bSJordan Crouse default: 1950b36086bSJordan Crouse return -ENOTTY; 1960b36086bSJordan Crouse } 1970b36086bSJordan Crouse 1980b36086bSJordan Crouse return 0; 1990b36086bSJordan Crouse } 2000b36086bSJordan Crouse 2010b36086bSJordan Crouse static const struct file_operations geodewdt_fops = { 2020b36086bSJordan Crouse .owner = THIS_MODULE, 2030b36086bSJordan Crouse .llseek = no_llseek, 2040b36086bSJordan Crouse .write = geodewdt_write, 2057275fc8cSWim Van Sebroeck .unlocked_ioctl = geodewdt_ioctl, 2060b36086bSJordan Crouse .open = geodewdt_open, 2070b36086bSJordan Crouse .release = geodewdt_release, 2080b36086bSJordan Crouse }; 2090b36086bSJordan Crouse 2100b36086bSJordan Crouse static struct miscdevice geodewdt_miscdev = { 2110b36086bSJordan Crouse .minor = WATCHDOG_MINOR, 2120b36086bSJordan Crouse .name = "watchdog", 2137944d3a5SWim Van Sebroeck .fops = &geodewdt_fops, 2140b36086bSJordan Crouse }; 2150b36086bSJordan Crouse 2167944d3a5SWim Van Sebroeck static int __devinit geodewdt_probe(struct platform_device *dev) 2170b36086bSJordan Crouse { 2180b36086bSJordan Crouse int ret, timer; 2190b36086bSJordan Crouse 22057f50ca1SIngo Molnar timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING); 2210b36086bSJordan Crouse 2220b36086bSJordan Crouse if (timer == -1) { 2230b36086bSJordan Crouse printk(KERN_ERR "geodewdt: No timers were available\n"); 2240b36086bSJordan Crouse return -ENODEV; 2250b36086bSJordan Crouse } 2260b36086bSJordan Crouse 2270b36086bSJordan Crouse wdt_timer = timer; 2280b36086bSJordan Crouse 2290b36086bSJordan Crouse /* Set up the timer */ 2300b36086bSJordan Crouse 2310b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 2320b36086bSJordan Crouse GEODEWDT_SCALE | (3 << 8)); 2330b36086bSJordan Crouse 2340b36086bSJordan Crouse /* Set up comparator 2 to reset when the event fires */ 2350b36086bSJordan Crouse geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1); 2360b36086bSJordan Crouse 2370b36086bSJordan Crouse /* Set up the initial timeout */ 2380b36086bSJordan Crouse 2390b36086bSJordan Crouse geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, 2400b36086bSJordan Crouse timeout * GEODEWDT_HZ); 2410b36086bSJordan Crouse 2420b36086bSJordan Crouse ret = misc_register(&geodewdt_miscdev); 2430b36086bSJordan Crouse 2440b36086bSJordan Crouse return ret; 2450b36086bSJordan Crouse } 2460b36086bSJordan Crouse 2477944d3a5SWim Van Sebroeck static int __devexit geodewdt_remove(struct platform_device *dev) 2480b36086bSJordan Crouse { 2490b36086bSJordan Crouse misc_deregister(&geodewdt_miscdev); 2500b36086bSJordan Crouse return 0; 2510b36086bSJordan Crouse } 2520b36086bSJordan Crouse 2537944d3a5SWim Van Sebroeck static void geodewdt_shutdown(struct platform_device *dev) 2540b36086bSJordan Crouse { 2550b36086bSJordan Crouse geodewdt_disable(); 2560b36086bSJordan Crouse } 2570b36086bSJordan Crouse 2580b36086bSJordan Crouse static struct platform_driver geodewdt_driver = { 2590b36086bSJordan Crouse .probe = geodewdt_probe, 2600b36086bSJordan Crouse .remove = __devexit_p(geodewdt_remove), 2610b36086bSJordan Crouse .shutdown = geodewdt_shutdown, 2620b36086bSJordan Crouse .driver = { 2630b36086bSJordan Crouse .owner = THIS_MODULE, 2640b36086bSJordan Crouse .name = DRV_NAME, 2650b36086bSJordan Crouse }, 2660b36086bSJordan Crouse }; 2670b36086bSJordan Crouse 2687944d3a5SWim Van Sebroeck static int __init geodewdt_init(void) 2690b36086bSJordan Crouse { 2700b36086bSJordan Crouse int ret; 2710b36086bSJordan Crouse 2720b36086bSJordan Crouse ret = platform_driver_register(&geodewdt_driver); 2730b36086bSJordan Crouse if (ret) 2740b36086bSJordan Crouse return ret; 2750b36086bSJordan Crouse 276*143a2e54SWim Van Sebroeck geodewdt_platform_device = platform_device_register_simple(DRV_NAME, 277*143a2e54SWim Van Sebroeck -1, NULL, 0); 2780b36086bSJordan Crouse if (IS_ERR(geodewdt_platform_device)) { 2790b36086bSJordan Crouse ret = PTR_ERR(geodewdt_platform_device); 2800b36086bSJordan Crouse goto err; 2810b36086bSJordan Crouse } 2820b36086bSJordan Crouse 2830b36086bSJordan Crouse return 0; 2840b36086bSJordan Crouse err: 2850b36086bSJordan Crouse platform_driver_unregister(&geodewdt_driver); 2860b36086bSJordan Crouse return ret; 2870b36086bSJordan Crouse } 2880b36086bSJordan Crouse 2897944d3a5SWim Van Sebroeck static void __exit geodewdt_exit(void) 2900b36086bSJordan Crouse { 2910b36086bSJordan Crouse platform_device_unregister(geodewdt_platform_device); 2920b36086bSJordan Crouse platform_driver_unregister(&geodewdt_driver); 2930b36086bSJordan Crouse } 2940b36086bSJordan Crouse 2950b36086bSJordan Crouse module_init(geodewdt_init); 2960b36086bSJordan Crouse module_exit(geodewdt_exit); 2970b36086bSJordan Crouse 2980b36086bSJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc"); 2990b36086bSJordan Crouse MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver"); 3000b36086bSJordan Crouse MODULE_LICENSE("GPL"); 3010b36086bSJordan Crouse MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 302