103ec5856SFlorian Fainelli /* 203ec5856SFlorian Fainelli * IDT Interprise 79RC32434 watchdog driver 303ec5856SFlorian Fainelli * 403ec5856SFlorian Fainelli * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org> 503ec5856SFlorian Fainelli * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org> 603ec5856SFlorian Fainelli * 703ec5856SFlorian Fainelli * based on 803ec5856SFlorian Fainelli * SoftDog 0.05: A Software Watchdog Device 903ec5856SFlorian Fainelli * 1029fa0586SAlan Cox * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>, 1129fa0586SAlan Cox * All Rights Reserved. 1203ec5856SFlorian Fainelli * 1303ec5856SFlorian Fainelli * This program is free software; you can redistribute it and/or 1403ec5856SFlorian Fainelli * modify it under the terms of the GNU General Public License 1503ec5856SFlorian Fainelli * as published by the Free Software Foundation; either version 1603ec5856SFlorian Fainelli * 2 of the License, or (at your option) any later version. 1703ec5856SFlorian Fainelli * 1803ec5856SFlorian Fainelli */ 1903ec5856SFlorian Fainelli 2027c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2127c766aaSJoe Perches 229b655e07SPhil Sutter #include <linux/module.h> /* For module specific items */ 239b655e07SPhil Sutter #include <linux/moduleparam.h> /* For new moduleparam's */ 249b655e07SPhil Sutter #include <linux/types.h> /* For standard types (like size_t) */ 259b655e07SPhil Sutter #include <linux/errno.h> /* For the -ENODEV/... values */ 269b655e07SPhil Sutter #include <linux/kernel.h> /* For printk/panic/... */ 279b655e07SPhil Sutter #include <linux/fs.h> /* For file operations */ 289b655e07SPhil Sutter #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV 299b655e07SPhil Sutter (WATCHDOG_MINOR) */ 309b655e07SPhil Sutter #include <linux/watchdog.h> /* For the watchdog specific items */ 319b655e07SPhil Sutter #include <linux/init.h> /* For __init/__exit/... */ 329b655e07SPhil Sutter #include <linux/platform_device.h> /* For platform_driver framework */ 33e455b6b4SWim Van Sebroeck #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */ 349b655e07SPhil Sutter #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ 3503ec5856SFlorian Fainelli 369b655e07SPhil Sutter #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */ 379b655e07SPhil Sutter 38f296b143SWim Van Sebroeck #define VERSION "1.0" 3903ec5856SFlorian Fainelli 4003ec5856SFlorian Fainelli static struct { 4103ec5856SFlorian Fainelli unsigned long inuse; 42e455b6b4SWim Van Sebroeck spinlock_t io_lock; 4303ec5856SFlorian Fainelli } rc32434_wdt_device; 4403ec5856SFlorian Fainelli 4503ec5856SFlorian Fainelli static struct integ __iomem *wdt_reg; 4603ec5856SFlorian Fainelli 4703ec5856SFlorian Fainelli static int expect_close; 480af98d37SPhil Sutter 490af98d37SPhil Sutter /* Board internal clock speed in Hz, 500af98d37SPhil Sutter * the watchdog timer ticks at. */ 510af98d37SPhil Sutter extern unsigned int idt_cpu_freq; 520af98d37SPhil Sutter 530af98d37SPhil Sutter /* translate wtcompare value to seconds and vice versa */ 540af98d37SPhil Sutter #define WTCOMP2SEC(x) (x / idt_cpu_freq) 550af98d37SPhil Sutter #define SEC2WTCOMP(x) (x * idt_cpu_freq) 560af98d37SPhil Sutter 570af98d37SPhil Sutter /* Use a default timeout of 20s. This should be 580af98d37SPhil Sutter * safe for CPU clock speeds up to 400MHz, as 590af98d37SPhil Sutter * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */ 600af98d37SPhil Sutter #define WATCHDOG_TIMEOUT 20 610af98d37SPhil Sutter 620af98d37SPhil Sutter static int timeout = WATCHDOG_TIMEOUT; 6308eb2e0cSPhil Sutter module_param(timeout, int, 0); 6408eb2e0cSPhil Sutter MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default=" 65810a90aeSFlorian Fainelli __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); 6603ec5856SFlorian Fainelli 6786a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 6886a1e189SWim Van Sebroeck module_param(nowayout, bool, 0); 6903ec5856SFlorian Fainelli MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 7003ec5856SFlorian Fainelli __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 7103ec5856SFlorian Fainelli 720af98d37SPhil Sutter /* apply or and nand masks to data read from addr and write back */ 730af98d37SPhil Sutter #define SET_BITS(addr, or, nand) \ 740af98d37SPhil Sutter writel((readl(&addr) | or) & ~nand, &addr) 7503ec5856SFlorian Fainelli 7608eb2e0cSPhil Sutter static int rc32434_wdt_set(int new_timeout) 7708eb2e0cSPhil Sutter { 7808eb2e0cSPhil Sutter int max_to = WTCOMP2SEC((u32)-1); 7908eb2e0cSPhil Sutter 8008eb2e0cSPhil Sutter if (new_timeout < 0 || new_timeout > max_to) { 8127c766aaSJoe Perches pr_err("timeout value must be between 0 and %d\n", max_to); 8208eb2e0cSPhil Sutter return -EINVAL; 8308eb2e0cSPhil Sutter } 8408eb2e0cSPhil Sutter timeout = new_timeout; 85e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock); 8608eb2e0cSPhil Sutter writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare); 87e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock); 8808eb2e0cSPhil Sutter 8908eb2e0cSPhil Sutter return 0; 9008eb2e0cSPhil Sutter } 9108eb2e0cSPhil Sutter 9203ec5856SFlorian Fainelli static void rc32434_wdt_start(void) 9303ec5856SFlorian Fainelli { 940af98d37SPhil Sutter u32 or, nand; 9503ec5856SFlorian Fainelli 96e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock); 97e455b6b4SWim Van Sebroeck 980af98d37SPhil Sutter /* zero the counter before enabling */ 9903ec5856SFlorian Fainelli writel(0, &wdt_reg->wtcount); 10003ec5856SFlorian Fainelli 1010af98d37SPhil Sutter /* don't generate a non-maskable interrupt, 1020af98d37SPhil Sutter * do a warm reset instead */ 1030af98d37SPhil Sutter nand = 1 << RC32434_ERR_WNE; 1040af98d37SPhil Sutter or = 1 << RC32434_ERR_WRE; 10503ec5856SFlorian Fainelli 1060af98d37SPhil Sutter /* reset the ERRCS timeout bit in case it's set */ 1070af98d37SPhil Sutter nand |= 1 << RC32434_ERR_WTO; 1080af98d37SPhil Sutter 1090af98d37SPhil Sutter SET_BITS(wdt_reg->errcs, or, nand); 1100af98d37SPhil Sutter 11108eb2e0cSPhil Sutter /* set the timeout (either default or based on module param) */ 11208eb2e0cSPhil Sutter rc32434_wdt_set(timeout); 11308eb2e0cSPhil Sutter 1140af98d37SPhil Sutter /* reset WTC timeout bit and enable WDT */ 1150af98d37SPhil Sutter nand = 1 << RC32434_WTC_TO; 1160af98d37SPhil Sutter or = 1 << RC32434_WTC_EN; 1170af98d37SPhil Sutter 1180af98d37SPhil Sutter SET_BITS(wdt_reg->wtc, or, nand); 1199b655e07SPhil Sutter 120e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock); 12127c766aaSJoe Perches pr_info("Started watchdog timer\n"); 12203ec5856SFlorian Fainelli } 12303ec5856SFlorian Fainelli 12403ec5856SFlorian Fainelli static void rc32434_wdt_stop(void) 12503ec5856SFlorian Fainelli { 126e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock); 127e455b6b4SWim Van Sebroeck 1280af98d37SPhil Sutter /* Disable WDT */ 1290af98d37SPhil Sutter SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN); 1309b655e07SPhil Sutter 131e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock); 13227c766aaSJoe Perches pr_info("Stopped watchdog timer\n"); 13303ec5856SFlorian Fainelli } 13403ec5856SFlorian Fainelli 1350af98d37SPhil Sutter static void rc32434_wdt_ping(void) 13603ec5856SFlorian Fainelli { 137e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock); 13803ec5856SFlorian Fainelli writel(0, &wdt_reg->wtcount); 139e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock); 14003ec5856SFlorian Fainelli } 14103ec5856SFlorian Fainelli 14203ec5856SFlorian Fainelli static int rc32434_wdt_open(struct inode *inode, struct file *file) 14303ec5856SFlorian Fainelli { 14403ec5856SFlorian Fainelli if (test_and_set_bit(0, &rc32434_wdt_device.inuse)) 14503ec5856SFlorian Fainelli return -EBUSY; 14603ec5856SFlorian Fainelli 14703ec5856SFlorian Fainelli if (nowayout) 14803ec5856SFlorian Fainelli __module_get(THIS_MODULE); 14903ec5856SFlorian Fainelli 1500af98d37SPhil Sutter rc32434_wdt_start(); 1510af98d37SPhil Sutter rc32434_wdt_ping(); 1520af98d37SPhil Sutter 15303ec5856SFlorian Fainelli return nonseekable_open(inode, file); 15403ec5856SFlorian Fainelli } 15503ec5856SFlorian Fainelli 15603ec5856SFlorian Fainelli static int rc32434_wdt_release(struct inode *inode, struct file *file) 15703ec5856SFlorian Fainelli { 1580af98d37SPhil Sutter if (expect_close == 42) { 15903ec5856SFlorian Fainelli rc32434_wdt_stop(); 16003ec5856SFlorian Fainelli module_put(THIS_MODULE); 1610af98d37SPhil Sutter } else { 16227c766aaSJoe Perches pr_crit("device closed unexpectedly. WDT will not stop!\n"); 1630af98d37SPhil Sutter rc32434_wdt_ping(); 1640af98d37SPhil Sutter } 16503ec5856SFlorian Fainelli clear_bit(0, &rc32434_wdt_device.inuse); 16603ec5856SFlorian Fainelli return 0; 16703ec5856SFlorian Fainelli } 16803ec5856SFlorian Fainelli 16903ec5856SFlorian Fainelli static ssize_t rc32434_wdt_write(struct file *file, const char *data, 17003ec5856SFlorian Fainelli size_t len, loff_t *ppos) 17103ec5856SFlorian Fainelli { 17203ec5856SFlorian Fainelli if (len) { 17303ec5856SFlorian Fainelli if (!nowayout) { 17403ec5856SFlorian Fainelli size_t i; 17503ec5856SFlorian Fainelli 17603ec5856SFlorian Fainelli /* In case it was set long ago */ 17703ec5856SFlorian Fainelli expect_close = 0; 17803ec5856SFlorian Fainelli 17903ec5856SFlorian Fainelli for (i = 0; i != len; i++) { 18003ec5856SFlorian Fainelli char c; 18103ec5856SFlorian Fainelli if (get_user(c, data + i)) 18203ec5856SFlorian Fainelli return -EFAULT; 18303ec5856SFlorian Fainelli if (c == 'V') 1840af98d37SPhil Sutter expect_close = 42; 18503ec5856SFlorian Fainelli } 18603ec5856SFlorian Fainelli } 1870af98d37SPhil Sutter rc32434_wdt_ping(); 18803ec5856SFlorian Fainelli return len; 18903ec5856SFlorian Fainelli } 19003ec5856SFlorian Fainelli return 0; 19103ec5856SFlorian Fainelli } 19203ec5856SFlorian Fainelli 1937275fc8cSWim Van Sebroeck static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd, 1947275fc8cSWim Van Sebroeck unsigned long arg) 19503ec5856SFlorian Fainelli { 19603ec5856SFlorian Fainelli void __user *argp = (void __user *)arg; 19703ec5856SFlorian Fainelli int new_timeout; 19803ec5856SFlorian Fainelli unsigned int value; 19942747d71SWim Van Sebroeck static const struct watchdog_info ident = { 20003ec5856SFlorian Fainelli .options = WDIOF_SETTIMEOUT | 20103ec5856SFlorian Fainelli WDIOF_KEEPALIVEPING | 20203ec5856SFlorian Fainelli WDIOF_MAGICCLOSE, 20303ec5856SFlorian Fainelli .identity = "RC32434_WDT Watchdog", 20403ec5856SFlorian Fainelli }; 20503ec5856SFlorian Fainelli switch (cmd) { 2069b655e07SPhil Sutter case WDIOC_GETSUPPORT: 2079b655e07SPhil Sutter if (copy_to_user(argp, &ident, sizeof(ident))) 2089b655e07SPhil Sutter return -EFAULT; 20903ec5856SFlorian Fainelli break; 21003ec5856SFlorian Fainelli case WDIOC_GETSTATUS: 21103ec5856SFlorian Fainelli case WDIOC_GETBOOTSTATUS: 2120af98d37SPhil Sutter value = 0; 21303ec5856SFlorian Fainelli if (copy_to_user(argp, &value, sizeof(int))) 21403ec5856SFlorian Fainelli return -EFAULT; 21503ec5856SFlorian Fainelli break; 21603ec5856SFlorian Fainelli case WDIOC_SETOPTIONS: 21703ec5856SFlorian Fainelli if (copy_from_user(&value, argp, sizeof(int))) 21803ec5856SFlorian Fainelli return -EFAULT; 21903ec5856SFlorian Fainelli switch (value) { 22003ec5856SFlorian Fainelli case WDIOS_ENABLECARD: 22103ec5856SFlorian Fainelli rc32434_wdt_start(); 22203ec5856SFlorian Fainelli break; 22303ec5856SFlorian Fainelli case WDIOS_DISABLECARD: 22403ec5856SFlorian Fainelli rc32434_wdt_stop(); 2250af98d37SPhil Sutter break; 22603ec5856SFlorian Fainelli default: 22703ec5856SFlorian Fainelli return -EINVAL; 22803ec5856SFlorian Fainelli } 22903ec5856SFlorian Fainelli break; 2309b655e07SPhil Sutter case WDIOC_KEEPALIVE: 2319b655e07SPhil Sutter rc32434_wdt_ping(); 2329b655e07SPhil Sutter break; 23303ec5856SFlorian Fainelli case WDIOC_SETTIMEOUT: 23403ec5856SFlorian Fainelli if (copy_from_user(&new_timeout, argp, sizeof(int))) 23503ec5856SFlorian Fainelli return -EFAULT; 2360af98d37SPhil Sutter if (rc32434_wdt_set(new_timeout)) 23703ec5856SFlorian Fainelli return -EINVAL; 2380af98d37SPhil Sutter /* Fall through */ 23903ec5856SFlorian Fainelli case WDIOC_GETTIMEOUT: 24003ec5856SFlorian Fainelli return copy_to_user(argp, &timeout, sizeof(int)); 24103ec5856SFlorian Fainelli default: 24203ec5856SFlorian Fainelli return -ENOTTY; 24303ec5856SFlorian Fainelli } 24403ec5856SFlorian Fainelli 24503ec5856SFlorian Fainelli return 0; 24603ec5856SFlorian Fainelli } 24703ec5856SFlorian Fainelli 248d5c26a59SWim Van Sebroeck static const struct file_operations rc32434_wdt_fops = { 24903ec5856SFlorian Fainelli .owner = THIS_MODULE, 25003ec5856SFlorian Fainelli .llseek = no_llseek, 25103ec5856SFlorian Fainelli .write = rc32434_wdt_write, 2527275fc8cSWim Van Sebroeck .unlocked_ioctl = rc32434_wdt_ioctl, 25303ec5856SFlorian Fainelli .open = rc32434_wdt_open, 25403ec5856SFlorian Fainelli .release = rc32434_wdt_release, 25503ec5856SFlorian Fainelli }; 25603ec5856SFlorian Fainelli 25703ec5856SFlorian Fainelli static struct miscdevice rc32434_wdt_miscdev = { 25803ec5856SFlorian Fainelli .minor = WATCHDOG_MINOR, 25903ec5856SFlorian Fainelli .name = "watchdog", 26003ec5856SFlorian Fainelli .fops = &rc32434_wdt_fops, 26103ec5856SFlorian Fainelli }; 26203ec5856SFlorian Fainelli 263*2d991a16SBill Pemberton static int rc32434_wdt_probe(struct platform_device *pdev) 26403ec5856SFlorian Fainelli { 26503ec5856SFlorian Fainelli int ret; 26603ec5856SFlorian Fainelli struct resource *r; 26703ec5856SFlorian Fainelli 2680af98d37SPhil Sutter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res"); 26903ec5856SFlorian Fainelli if (!r) { 27027c766aaSJoe Perches pr_err("failed to retrieve resources\n"); 27103ec5856SFlorian Fainelli return -ENODEV; 27203ec5856SFlorian Fainelli } 27303ec5856SFlorian Fainelli 274be088b13SH Hartley Sweeten wdt_reg = ioremap_nocache(r->start, resource_size(r)); 27503ec5856SFlorian Fainelli if (!wdt_reg) { 27627c766aaSJoe Perches pr_err("failed to remap I/O resources\n"); 27703ec5856SFlorian Fainelli return -ENXIO; 27803ec5856SFlorian Fainelli } 27903ec5856SFlorian Fainelli 280e455b6b4SWim Van Sebroeck spin_lock_init(&rc32434_wdt_device.io_lock); 281e455b6b4SWim Van Sebroeck 282f296b143SWim Van Sebroeck /* Make sure the watchdog is not running */ 283f296b143SWim Van Sebroeck rc32434_wdt_stop(); 284f296b143SWim Van Sebroeck 28508eb2e0cSPhil Sutter /* Check that the heartbeat value is within it's range; 28608eb2e0cSPhil Sutter * if not reset to the default */ 28708eb2e0cSPhil Sutter if (rc32434_wdt_set(timeout)) { 28808eb2e0cSPhil Sutter rc32434_wdt_set(WATCHDOG_TIMEOUT); 28927c766aaSJoe Perches pr_info("timeout value must be between 0 and %d\n", 29008eb2e0cSPhil Sutter WTCOMP2SEC((u32)-1)); 29108eb2e0cSPhil Sutter } 29208eb2e0cSPhil Sutter 29303ec5856SFlorian Fainelli ret = misc_register(&rc32434_wdt_miscdev); 29403ec5856SFlorian Fainelli if (ret < 0) { 29527c766aaSJoe Perches pr_err("failed to register watchdog device\n"); 29603ec5856SFlorian Fainelli goto unmap; 29703ec5856SFlorian Fainelli } 29803ec5856SFlorian Fainelli 29927c766aaSJoe Perches pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n", 30027c766aaSJoe Perches timeout); 30103ec5856SFlorian Fainelli 30203ec5856SFlorian Fainelli return 0; 30303ec5856SFlorian Fainelli 30403ec5856SFlorian Fainelli unmap: 30503ec5856SFlorian Fainelli iounmap(wdt_reg); 30603ec5856SFlorian Fainelli return ret; 30703ec5856SFlorian Fainelli } 30803ec5856SFlorian Fainelli 309d9a8798cSPhil Sutter static int __devexit rc32434_wdt_remove(struct platform_device *pdev) 31003ec5856SFlorian Fainelli { 31103ec5856SFlorian Fainelli misc_deregister(&rc32434_wdt_miscdev); 31203ec5856SFlorian Fainelli iounmap(wdt_reg); 31303ec5856SFlorian Fainelli return 0; 31403ec5856SFlorian Fainelli } 31503ec5856SFlorian Fainelli 3160aaae661SWim Van Sebroeck static void rc32434_wdt_shutdown(struct platform_device *pdev) 3170aaae661SWim Van Sebroeck { 3180aaae661SWim Van Sebroeck rc32434_wdt_stop(); 3190aaae661SWim Van Sebroeck } 3200aaae661SWim Van Sebroeck 3219b655e07SPhil Sutter static struct platform_driver rc32434_wdt_driver = { 32203ec5856SFlorian Fainelli .probe = rc32434_wdt_probe, 32382268714SBill Pemberton .remove = rc32434_wdt_remove, 3240aaae661SWim Van Sebroeck .shutdown = rc32434_wdt_shutdown, 32503ec5856SFlorian Fainelli .driver = { 32603ec5856SFlorian Fainelli .name = "rc32434_wdt", 32703ec5856SFlorian Fainelli } 32803ec5856SFlorian Fainelli }; 32903ec5856SFlorian Fainelli 330b8ec6118SAxel Lin module_platform_driver(rc32434_wdt_driver); 33103ec5856SFlorian Fainelli 33203ec5856SFlorian Fainelli MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>," 33303ec5856SFlorian Fainelli "Florian Fainelli <florian@openwrt.org>"); 33403ec5856SFlorian Fainelli MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog"); 33503ec5856SFlorian Fainelli MODULE_LICENSE("GPL"); 33603ec5856SFlorian Fainelli MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 337