| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * drivers/char/watchdog/ixp4xx_wdt.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * | 
|  | 4 | * Watchdog driver for Intel IXP4xx network processors | 
|  | 5 | * | 
|  | 6 | * Author: Deepak Saxena <dsaxena@plexity.net> | 
|  | 7 | * | 
|  | 8 | * Copyright 2004 (c) MontaVista, Software, Inc. | 
|  | 9 | * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu> | 
|  | 10 | * | 
|  | 11 | * This file is licensed under  the terms of the GNU General Public | 
|  | 12 | * License version 2. This program is licensed "as is" without any | 
|  | 13 | * warranty of any kind, whether express or implied. | 
|  | 14 | */ | 
|  | 15 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> | 
|  | 17 | #include <linux/moduleparam.h> | 
|  | 18 | #include <linux/types.h> | 
|  | 19 | #include <linux/kernel.h> | 
|  | 20 | #include <linux/fs.h> | 
|  | 21 | #include <linux/miscdevice.h> | 
|  | 22 | #include <linux/watchdog.h> | 
|  | 23 | #include <linux/init.h> | 
|  | 24 | #include <linux/bitops.h> | 
| Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 25 | #include <linux/uaccess.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/hardware.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
| Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 28 | static int nowayout = WATCHDOG_NOWAYOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static int heartbeat = 60;	/* (secs) Default is 1 minute */ | 
|  | 30 | static unsigned long wdt_status; | 
|  | 31 | static unsigned long boot_status; | 
| Adrian Bunk | 9229376 | 2008-08-10 14:03:41 +0300 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL) | 
|  | 35 |  | 
|  | 36 | #define	WDT_IN_USE		0 | 
|  | 37 | #define	WDT_OK_TO_CLOSE		1 | 
|  | 38 |  | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 39 | static void wdt_enable(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 41 | spin_lock(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | *IXP4XX_OSWK = IXP4XX_WDT_KEY; | 
|  | 43 | *IXP4XX_OSWE = 0; | 
|  | 44 | *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat; | 
|  | 45 | *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE; | 
|  | 46 | *IXP4XX_OSWK = 0; | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 47 | spin_unlock(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 50 | static void wdt_disable(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 52 | spin_lock(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | *IXP4XX_OSWK = IXP4XX_WDT_KEY; | 
|  | 54 | *IXP4XX_OSWE = 0; | 
|  | 55 | *IXP4XX_OSWK = 0; | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 56 | spin_unlock(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 59 | static int ixp4xx_wdt_open(struct inode *inode, struct file *file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { | 
|  | 61 | if (test_and_set_bit(WDT_IN_USE, &wdt_status)) | 
|  | 62 | return -EBUSY; | 
|  | 63 |  | 
|  | 64 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | wdt_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return nonseekable_open(inode, file); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static ssize_t | 
|  | 70 | ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos) | 
|  | 71 | { | 
|  | 72 | if (len) { | 
|  | 73 | if (!nowayout) { | 
|  | 74 | size_t i; | 
|  | 75 |  | 
|  | 76 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); | 
|  | 77 |  | 
|  | 78 | for (i = 0; i != len; i++) { | 
|  | 79 | char c; | 
|  | 80 |  | 
|  | 81 | if (get_user(c, data + i)) | 
|  | 82 | return -EFAULT; | 
|  | 83 | if (c == 'V') | 
|  | 84 | set_bit(WDT_OK_TO_CLOSE, &wdt_status); | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 | wdt_enable(); | 
|  | 88 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return len; | 
|  | 90 | } | 
|  | 91 |  | 
| Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 92 | static const struct watchdog_info ident = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .options	= WDIOF_CARDRESET | WDIOF_MAGICCLOSE | | 
|  | 94 | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | 
|  | 95 | .identity	= "IXP4xx Watchdog", | 
|  | 96 | }; | 
|  | 97 |  | 
|  | 98 |  | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 99 | static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd, | 
|  | 100 | unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { | 
| Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 102 | int ret = -ENOTTY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | int time; | 
|  | 104 |  | 
|  | 105 | switch (cmd) { | 
|  | 106 | case WDIOC_GETSUPPORT: | 
|  | 107 | ret = copy_to_user((struct watchdog_info *)arg, &ident, | 
|  | 108 | sizeof(ident)) ? -EFAULT : 0; | 
|  | 109 | break; | 
|  | 110 |  | 
|  | 111 | case WDIOC_GETSTATUS: | 
|  | 112 | ret = put_user(0, (int *)arg); | 
|  | 113 | break; | 
|  | 114 |  | 
|  | 115 | case WDIOC_GETBOOTSTATUS: | 
|  | 116 | ret = put_user(boot_status, (int *)arg); | 
|  | 117 | break; | 
|  | 118 |  | 
| Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 119 | case WDIOC_KEEPALIVE: | 
|  | 120 | wdt_enable(); | 
|  | 121 | ret = 0; | 
|  | 122 | break; | 
|  | 123 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | case WDIOC_SETTIMEOUT: | 
|  | 125 | ret = get_user(time, (int *)arg); | 
|  | 126 | if (ret) | 
|  | 127 | break; | 
|  | 128 |  | 
|  | 129 | if (time <= 0 || time > 60) { | 
|  | 130 | ret = -EINVAL; | 
|  | 131 | break; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | heartbeat = time; | 
|  | 135 | wdt_enable(); | 
|  | 136 | /* Fall through */ | 
|  | 137 |  | 
|  | 138 | case WDIOC_GETTIMEOUT: | 
|  | 139 | ret = put_user(heartbeat, (int *)arg); | 
|  | 140 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } | 
|  | 142 | return ret; | 
|  | 143 | } | 
|  | 144 |  | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 145 | static int ixp4xx_wdt_release(struct inode *inode, struct file *file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 147 | if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | wdt_disable(); | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 149 | else | 
| Lennert Buytenhek | b36bbb6 | 2005-06-28 20:44:46 -0700 | [diff] [blame] | 150 | printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - " | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | "timer will not stop\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | clear_bit(WDT_IN_USE, &wdt_status); | 
|  | 153 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); | 
|  | 154 |  | 
|  | 155 | return 0; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 |  | 
| Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 159 | static const struct file_operations ixp4xx_wdt_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | .owner		= THIS_MODULE, | 
|  | 161 | .llseek		= no_llseek, | 
|  | 162 | .write		= ixp4xx_wdt_write, | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 163 | .unlocked_ioctl	= ixp4xx_wdt_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | .open		= ixp4xx_wdt_open, | 
|  | 165 | .release	= ixp4xx_wdt_release, | 
|  | 166 | }; | 
|  | 167 |  | 
| Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 168 | static struct miscdevice ixp4xx_wdt_miscdev = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | .minor		= WATCHDOG_MINOR, | 
| Olaf Hering | 2dab3ca | 2005-08-17 08:58:34 +0200 | [diff] [blame] | 170 | .name		= "watchdog", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | .fops		= &ixp4xx_wdt_fops, | 
|  | 172 | }; | 
|  | 173 |  | 
|  | 174 | static int __init ixp4xx_wdt_init(void) | 
|  | 175 | { | 
|  | 176 | int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 |  | 
| Russell King | 0ba8b9b | 2008-08-10 18:08:10 +0100 | [diff] [blame] | 178 | if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) { | 
| Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 179 | printk(KERN_ERR "IXP4XXX Watchdog: Rev. A0 IXP42x CPU detected" | 
|  | 180 | " - watchdog disabled\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | return -ENODEV; | 
|  | 183 | } | 
| Alan Cox | 20d35f3 | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 184 | spin_lock_init(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ? | 
|  | 186 | WDIOF_CARDRESET : 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | ret = misc_register(&ixp4xx_wdt_miscdev); | 
|  | 188 | if (ret == 0) | 
| Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 189 | printk(KERN_INFO "IXP4xx Watchdog Timer: heartbeat %d sec\n", | 
|  | 190 | heartbeat); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return ret; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | static void __exit ixp4xx_wdt_exit(void) | 
|  | 195 | { | 
|  | 196 | misc_deregister(&ixp4xx_wdt_miscdev); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 |  | 
|  | 200 | module_init(ixp4xx_wdt_init); | 
|  | 201 | module_exit(ixp4xx_wdt_exit); | 
|  | 202 |  | 
|  | 203 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); | 
|  | 204 | MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog"); | 
|  | 205 |  | 
|  | 206 | module_param(heartbeat, int, 0); | 
|  | 207 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)"); | 
|  | 208 |  | 
|  | 209 | module_param(nowayout, int, 0); | 
|  | 210 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"); | 
|  | 211 |  | 
|  | 212 | MODULE_LICENSE("GPL"); | 
|  | 213 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | 
|  | 214 |  |