1b7e04f8cSWim Van Sebroeck /* 2b7e04f8cSWim Van Sebroeck * Driver for the MTX-1 Watchdog. 3b7e04f8cSWim Van Sebroeck * 4b7e04f8cSWim Van Sebroeck * (C) Copyright 2005 4G Systems <info@4g-systems.biz>, All Rights Reserved. 5b7e04f8cSWim Van Sebroeck * http://www.4g-systems.biz 6b7e04f8cSWim Van Sebroeck * 7b7e04f8cSWim Van Sebroeck * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org> 8b7e04f8cSWim Van Sebroeck * 9b7e04f8cSWim Van Sebroeck * This program is free software; you can redistribute it and/or 10b7e04f8cSWim Van Sebroeck * modify it under the terms of the GNU General Public License 11b7e04f8cSWim Van Sebroeck * as published by the Free Software Foundation; either version 12b7e04f8cSWim Van Sebroeck * 2 of the License, or (at your option) any later version. 13b7e04f8cSWim Van Sebroeck * 14b7e04f8cSWim Van Sebroeck * Neither Michael Stickel nor 4G Systems admit liability nor provide 15b7e04f8cSWim Van Sebroeck * warranty for any of this software. This material is provided 16b7e04f8cSWim Van Sebroeck * "AS-IS" and at no charge. 17b7e04f8cSWim Van Sebroeck * 18b7e04f8cSWim Van Sebroeck * (c) Copyright 2005 4G Systems <info@4g-systems.biz> 19b7e04f8cSWim Van Sebroeck * 20b7e04f8cSWim Van Sebroeck * Release 0.01. 21b7e04f8cSWim Van Sebroeck * Author: Michael Stickel michael.stickel@4g-systems.biz 22b7e04f8cSWim Van Sebroeck * 23b7e04f8cSWim Van Sebroeck * Release 0.02. 24b7e04f8cSWim Van Sebroeck * Author: Florian Fainelli florian@openwrt.org 25b7e04f8cSWim Van Sebroeck * use the Linux watchdog/timer APIs 26b7e04f8cSWim Van Sebroeck * 27b7e04f8cSWim Van Sebroeck * The Watchdog is configured to reset the MTX-1 28b7e04f8cSWim Van Sebroeck * if it is not triggered for 100 seconds. 29b7e04f8cSWim Van Sebroeck * It should not be triggered more often than 1.6 seconds. 30b7e04f8cSWim Van Sebroeck * 31b7e04f8cSWim Van Sebroeck * A timer triggers the watchdog every 5 seconds, until 32b7e04f8cSWim Van Sebroeck * it is opened for the first time. After the first open 33b7e04f8cSWim Van Sebroeck * it MUST be triggered every 2..95 seconds. 34b7e04f8cSWim Van Sebroeck */ 35b7e04f8cSWim Van Sebroeck 36b7e04f8cSWim Van Sebroeck #include <linux/module.h> 37b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h> 38b7e04f8cSWim Van Sebroeck #include <linux/types.h> 39b7e04f8cSWim Van Sebroeck #include <linux/errno.h> 40b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h> 41b7e04f8cSWim Van Sebroeck #include <linux/fs.h> 42b7e04f8cSWim Van Sebroeck #include <linux/init.h> 43b7e04f8cSWim Van Sebroeck #include <linux/ioport.h> 44b7e04f8cSWim Van Sebroeck #include <linux/timer.h> 45b7e04f8cSWim Van Sebroeck #include <linux/completion.h> 46b7e04f8cSWim Van Sebroeck #include <linux/jiffies.h> 47b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h> 486ea8115bSFlorian Fainelli #include <linux/platform_device.h> 496ea8115bSFlorian Fainelli 50b7e04f8cSWim Van Sebroeck #include <asm/io.h> 51b7e04f8cSWim Van Sebroeck #include <asm/uaccess.h> 52b7e04f8cSWim Van Sebroeck 53b7e04f8cSWim Van Sebroeck #include <asm/mach-au1x00/au1000.h> 546ea8115bSFlorian Fainelli #include <asm/gpio.h> 55b7e04f8cSWim Van Sebroeck 56b7e04f8cSWim Van Sebroeck #define MTX1_WDT_INTERVAL (5 * HZ) 57b7e04f8cSWim Van Sebroeck 58b7e04f8cSWim Van Sebroeck static int ticks = 100 * HZ; 59b7e04f8cSWim Van Sebroeck 60b7e04f8cSWim Van Sebroeck static struct { 61b7e04f8cSWim Van Sebroeck struct completion stop; 62*996d62d4SFlorian Fainelli int running; 63b7e04f8cSWim Van Sebroeck struct timer_list timer; 64*996d62d4SFlorian Fainelli int queue; 65b7e04f8cSWim Van Sebroeck int default_ticks; 66b7e04f8cSWim Van Sebroeck unsigned long inuse; 676ea8115bSFlorian Fainelli unsigned gpio; 68b7e04f8cSWim Van Sebroeck } mtx1_wdt_device; 69b7e04f8cSWim Van Sebroeck 70b7e04f8cSWim Van Sebroeck static void mtx1_wdt_trigger(unsigned long unused) 71b7e04f8cSWim Van Sebroeck { 72b7e04f8cSWim Van Sebroeck u32 tmp; 73b7e04f8cSWim Van Sebroeck 74b7e04f8cSWim Van Sebroeck if (mtx1_wdt_device.running) 75b7e04f8cSWim Van Sebroeck ticks--; 76b7e04f8cSWim Van Sebroeck /* 77b7e04f8cSWim Van Sebroeck * toggle GPIO2_15 78b7e04f8cSWim Van Sebroeck */ 79b7e04f8cSWim Van Sebroeck tmp = au_readl(GPIO2_DIR); 806ea8115bSFlorian Fainelli tmp = (tmp & ~(1 << mtx1_wdt_device.gpio)) | 816ea8115bSFlorian Fainelli ((~tmp) & (1 << mtx1_wdt_device.gpio)); 82b7e04f8cSWim Van Sebroeck au_writel (tmp, GPIO2_DIR); 83b7e04f8cSWim Van Sebroeck 84b7e04f8cSWim Van Sebroeck if (mtx1_wdt_device.queue && ticks) 85b7e04f8cSWim Van Sebroeck mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL); 86b7e04f8cSWim Van Sebroeck else { 87b7e04f8cSWim Van Sebroeck complete(&mtx1_wdt_device.stop); 88b7e04f8cSWim Van Sebroeck } 89b7e04f8cSWim Van Sebroeck } 90b7e04f8cSWim Van Sebroeck 91b7e04f8cSWim Van Sebroeck static void mtx1_wdt_reset(void) 92b7e04f8cSWim Van Sebroeck { 93b7e04f8cSWim Van Sebroeck ticks = mtx1_wdt_device.default_ticks; 94b7e04f8cSWim Van Sebroeck } 95b7e04f8cSWim Van Sebroeck 96b7e04f8cSWim Van Sebroeck 97b7e04f8cSWim Van Sebroeck static void mtx1_wdt_start(void) 98b7e04f8cSWim Van Sebroeck { 99b7e04f8cSWim Van Sebroeck if (!mtx1_wdt_device.queue) { 100b7e04f8cSWim Van Sebroeck mtx1_wdt_device.queue = 1; 1016ea8115bSFlorian Fainelli gpio_set_value(mtx1_wdt_device.gpio, 1); 102b7e04f8cSWim Van Sebroeck mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL); 103b7e04f8cSWim Van Sebroeck } 104b7e04f8cSWim Van Sebroeck mtx1_wdt_device.running++; 105b7e04f8cSWim Van Sebroeck } 106b7e04f8cSWim Van Sebroeck 107b7e04f8cSWim Van Sebroeck static int mtx1_wdt_stop(void) 108b7e04f8cSWim Van Sebroeck { 109b7e04f8cSWim Van Sebroeck if (mtx1_wdt_device.queue) { 110b7e04f8cSWim Van Sebroeck mtx1_wdt_device.queue = 0; 1116ea8115bSFlorian Fainelli gpio_set_value(mtx1_wdt_device.gpio, 0); 112b7e04f8cSWim Van Sebroeck } 113b7e04f8cSWim Van Sebroeck 114b7e04f8cSWim Van Sebroeck ticks = mtx1_wdt_device.default_ticks; 115b7e04f8cSWim Van Sebroeck 116b7e04f8cSWim Van Sebroeck return 0; 117b7e04f8cSWim Van Sebroeck } 118b7e04f8cSWim Van Sebroeck 119b7e04f8cSWim Van Sebroeck /* Filesystem functions */ 120b7e04f8cSWim Van Sebroeck 121b7e04f8cSWim Van Sebroeck static int mtx1_wdt_open(struct inode *inode, struct file *file) 122b7e04f8cSWim Van Sebroeck { 123b7e04f8cSWim Van Sebroeck if (test_and_set_bit(0, &mtx1_wdt_device.inuse)) 124b7e04f8cSWim Van Sebroeck return -EBUSY; 125b7e04f8cSWim Van Sebroeck 126b7e04f8cSWim Van Sebroeck return nonseekable_open(inode, file); 127b7e04f8cSWim Van Sebroeck } 128b7e04f8cSWim Van Sebroeck 129b7e04f8cSWim Van Sebroeck 130b7e04f8cSWim Van Sebroeck static int mtx1_wdt_release(struct inode *inode, struct file *file) 131b7e04f8cSWim Van Sebroeck { 132b7e04f8cSWim Van Sebroeck clear_bit(0, &mtx1_wdt_device.inuse); 133b7e04f8cSWim Van Sebroeck return 0; 134b7e04f8cSWim Van Sebroeck } 135b7e04f8cSWim Van Sebroeck 136b7e04f8cSWim Van Sebroeck static int mtx1_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 137b7e04f8cSWim Van Sebroeck { 138b7e04f8cSWim Van Sebroeck void __user *argp = (void __user *)arg; 139b7e04f8cSWim Van Sebroeck unsigned int value; 140b7e04f8cSWim Van Sebroeck static struct watchdog_info ident = 141b7e04f8cSWim Van Sebroeck { 142b7e04f8cSWim Van Sebroeck .options = WDIOF_CARDRESET, 143b7e04f8cSWim Van Sebroeck .identity = "MTX-1 WDT", 144b7e04f8cSWim Van Sebroeck }; 145b7e04f8cSWim Van Sebroeck 146b7e04f8cSWim Van Sebroeck switch(cmd) { 147b7e04f8cSWim Van Sebroeck case WDIOC_KEEPALIVE: 148b7e04f8cSWim Van Sebroeck mtx1_wdt_reset(); 149b7e04f8cSWim Van Sebroeck break; 150b7e04f8cSWim Van Sebroeck case WDIOC_GETSTATUS: 151b7e04f8cSWim Van Sebroeck case WDIOC_GETBOOTSTATUS: 152b7e04f8cSWim Van Sebroeck if ( copy_to_user(argp, &value, sizeof(int)) ) 153b7e04f8cSWim Van Sebroeck return -EFAULT; 154b7e04f8cSWim Van Sebroeck break; 155b7e04f8cSWim Van Sebroeck case WDIOC_GETSUPPORT: 156b7e04f8cSWim Van Sebroeck if ( copy_to_user(argp, &ident, sizeof(ident)) ) 157b7e04f8cSWim Van Sebroeck return -EFAULT; 158b7e04f8cSWim Van Sebroeck break; 159b7e04f8cSWim Van Sebroeck case WDIOC_SETOPTIONS: 160b7e04f8cSWim Van Sebroeck if ( copy_from_user(&value, argp, sizeof(int)) ) 161b7e04f8cSWim Van Sebroeck return -EFAULT; 162b7e04f8cSWim Van Sebroeck switch(value) { 163b7e04f8cSWim Van Sebroeck case WDIOS_ENABLECARD: 164b7e04f8cSWim Van Sebroeck mtx1_wdt_start(); 165b7e04f8cSWim Van Sebroeck break; 166b7e04f8cSWim Van Sebroeck case WDIOS_DISABLECARD: 167b7e04f8cSWim Van Sebroeck return mtx1_wdt_stop(); 168b7e04f8cSWim Van Sebroeck default: 169b7e04f8cSWim Van Sebroeck return -EINVAL; 170b7e04f8cSWim Van Sebroeck } 171b7e04f8cSWim Van Sebroeck break; 172b7e04f8cSWim Van Sebroeck default: 173b7e04f8cSWim Van Sebroeck return -ENOTTY; 174b7e04f8cSWim Van Sebroeck } 175b7e04f8cSWim Van Sebroeck return 0; 176b7e04f8cSWim Van Sebroeck } 177b7e04f8cSWim Van Sebroeck 178b7e04f8cSWim Van Sebroeck 179b7e04f8cSWim Van Sebroeck static ssize_t mtx1_wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos) 180b7e04f8cSWim Van Sebroeck { 181b7e04f8cSWim Van Sebroeck if (!count) 182b7e04f8cSWim Van Sebroeck return -EIO; 183b7e04f8cSWim Van Sebroeck 184b7e04f8cSWim Van Sebroeck mtx1_wdt_reset(); 185b7e04f8cSWim Van Sebroeck return count; 186b7e04f8cSWim Van Sebroeck } 187b7e04f8cSWim Van Sebroeck 188b47a166eSJan Engelhardt static const struct file_operations mtx1_wdt_fops = { 189b7e04f8cSWim Van Sebroeck .owner = THIS_MODULE, 190b7e04f8cSWim Van Sebroeck .llseek = no_llseek, 191b7e04f8cSWim Van Sebroeck .ioctl = mtx1_wdt_ioctl, 192b7e04f8cSWim Van Sebroeck .open = mtx1_wdt_open, 193b7e04f8cSWim Van Sebroeck .write = mtx1_wdt_write, 194b7e04f8cSWim Van Sebroeck .release = mtx1_wdt_release 195b7e04f8cSWim Van Sebroeck }; 196b7e04f8cSWim Van Sebroeck 197b7e04f8cSWim Van Sebroeck 198b7e04f8cSWim Van Sebroeck static struct miscdevice mtx1_wdt_misc = { 199b7e04f8cSWim Van Sebroeck .minor = WATCHDOG_MINOR, 200b7e04f8cSWim Van Sebroeck .name = "watchdog", 201b7e04f8cSWim Van Sebroeck .fops = &mtx1_wdt_fops 202b7e04f8cSWim Van Sebroeck }; 203b7e04f8cSWim Van Sebroeck 204b7e04f8cSWim Van Sebroeck 2056ea8115bSFlorian Fainelli static int mtx1_wdt_probe(struct platform_device *pdev) 206b7e04f8cSWim Van Sebroeck { 207b7e04f8cSWim Van Sebroeck int ret; 208b7e04f8cSWim Van Sebroeck 2096ea8115bSFlorian Fainelli mtx1_wdt_device.gpio = pdev->resource[0].start; 2106ea8115bSFlorian Fainelli 211b7e04f8cSWim Van Sebroeck if ((ret = misc_register(&mtx1_wdt_misc)) < 0) { 212b7e04f8cSWim Van Sebroeck printk(KERN_ERR " mtx-1_wdt : failed to register\n"); 213b7e04f8cSWim Van Sebroeck return ret; 214b7e04f8cSWim Van Sebroeck } 215b7e04f8cSWim Van Sebroeck 216b7e04f8cSWim Van Sebroeck init_completion(&mtx1_wdt_device.stop); 217b7e04f8cSWim Van Sebroeck mtx1_wdt_device.queue = 0; 218b7e04f8cSWim Van Sebroeck 219b7e04f8cSWim Van Sebroeck clear_bit(0, &mtx1_wdt_device.inuse); 220b7e04f8cSWim Van Sebroeck 221b7e04f8cSWim Van Sebroeck setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L); 222b7e04f8cSWim Van Sebroeck 223b7e04f8cSWim Van Sebroeck mtx1_wdt_device.default_ticks = ticks; 224b7e04f8cSWim Van Sebroeck 225b7e04f8cSWim Van Sebroeck mtx1_wdt_start(); 226b7e04f8cSWim Van Sebroeck 227b7e04f8cSWim Van Sebroeck printk(KERN_INFO "MTX-1 Watchdog driver\n"); 228b7e04f8cSWim Van Sebroeck 229b7e04f8cSWim Van Sebroeck return 0; 230b7e04f8cSWim Van Sebroeck } 231b7e04f8cSWim Van Sebroeck 2326ea8115bSFlorian Fainelli static int mtx1_wdt_remove(struct platform_device *pdev) 233b7e04f8cSWim Van Sebroeck { 234b7e04f8cSWim Van Sebroeck if (mtx1_wdt_device.queue) { 235b7e04f8cSWim Van Sebroeck mtx1_wdt_device.queue = 0; 236b7e04f8cSWim Van Sebroeck wait_for_completion(&mtx1_wdt_device.stop); 237b7e04f8cSWim Van Sebroeck } 238b7e04f8cSWim Van Sebroeck misc_deregister(&mtx1_wdt_misc); 2396ea8115bSFlorian Fainelli return 0; 2406ea8115bSFlorian Fainelli } 2416ea8115bSFlorian Fainelli 2426ea8115bSFlorian Fainelli static struct platform_driver mtx1_wdt = { 2436ea8115bSFlorian Fainelli .probe = mtx1_wdt_probe, 2446ea8115bSFlorian Fainelli .remove = mtx1_wdt_remove, 2456ea8115bSFlorian Fainelli .driver.name = "mtx1-wdt", 2466ea8115bSFlorian Fainelli }; 2476ea8115bSFlorian Fainelli 2486ea8115bSFlorian Fainelli static int __init mtx1_wdt_init(void) 2496ea8115bSFlorian Fainelli { 2506ea8115bSFlorian Fainelli return platform_driver_register(&mtx1_wdt); 2516ea8115bSFlorian Fainelli } 2526ea8115bSFlorian Fainelli 2536ea8115bSFlorian Fainelli static void __exit mtx1_wdt_exit(void) 2546ea8115bSFlorian Fainelli { 2556ea8115bSFlorian Fainelli platform_driver_unregister(&mtx1_wdt); 256b7e04f8cSWim Van Sebroeck } 257b7e04f8cSWim Van Sebroeck 258b7e04f8cSWim Van Sebroeck module_init(mtx1_wdt_init); 259b7e04f8cSWim Van Sebroeck module_exit(mtx1_wdt_exit); 260b7e04f8cSWim Van Sebroeck 261b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Michael Stickel, Florian Fainelli"); 262b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("Driver for the MTX-1 watchdog"); 263b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL"); 2646ea8115bSFlorian Fainelli MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 265