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 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/moduleparam.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/miscdevice.h> |
Linus Walleij | 9540724 | 2019-01-27 14:08:36 +0100 | [diff] [blame] | 24 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/watchdog.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/bitops.h> |
Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 29 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 31 | static bool nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static int heartbeat = 60; /* (secs) Default is 1 minute */ |
| 33 | static unsigned long wdt_status; |
| 34 | static unsigned long boot_status; |
Adrian Bunk | 9229376 | 2008-08-10 14:03:41 +0300 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL) |
| 38 | |
| 39 | #define WDT_IN_USE 0 |
| 40 | #define WDT_OK_TO_CLOSE 1 |
| 41 | |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 42 | static void wdt_enable(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 44 | spin_lock(&wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | *IXP4XX_OSWK = IXP4XX_WDT_KEY; |
| 46 | *IXP4XX_OSWE = 0; |
| 47 | *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat; |
| 48 | *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE; |
| 49 | *IXP4XX_OSWK = 0; |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 50 | spin_unlock(&wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 53 | static void wdt_disable(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 55 | spin_lock(&wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | *IXP4XX_OSWK = IXP4XX_WDT_KEY; |
| 57 | *IXP4XX_OSWE = 0; |
| 58 | *IXP4XX_OSWK = 0; |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 59 | spin_unlock(&wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 62 | static int ixp4xx_wdt_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | if (test_and_set_bit(WDT_IN_USE, &wdt_status)) |
| 65 | return -EBUSY; |
| 66 | |
| 67 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | wdt_enable(); |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 69 | return stream_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static ssize_t |
| 73 | ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos) |
| 74 | { |
| 75 | if (len) { |
| 76 | if (!nowayout) { |
| 77 | size_t i; |
| 78 | |
| 79 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); |
| 80 | |
| 81 | for (i = 0; i != len; i++) { |
| 82 | char c; |
| 83 | |
| 84 | if (get_user(c, data + i)) |
| 85 | return -EFAULT; |
| 86 | if (c == 'V') |
| 87 | set_bit(WDT_OK_TO_CLOSE, &wdt_status); |
| 88 | } |
| 89 | } |
| 90 | wdt_enable(); |
| 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return len; |
| 93 | } |
| 94 | |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 95 | static const struct watchdog_info ident = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE | |
| 97 | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 98 | .identity = "IXP4xx Watchdog", |
| 99 | }; |
| 100 | |
| 101 | |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 102 | static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd, |
| 103 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 105 | int ret = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | int time; |
| 107 | |
| 108 | switch (cmd) { |
| 109 | case WDIOC_GETSUPPORT: |
| 110 | ret = copy_to_user((struct watchdog_info *)arg, &ident, |
| 111 | sizeof(ident)) ? -EFAULT : 0; |
| 112 | break; |
| 113 | |
| 114 | case WDIOC_GETSTATUS: |
| 115 | ret = put_user(0, (int *)arg); |
| 116 | break; |
| 117 | |
| 118 | case WDIOC_GETBOOTSTATUS: |
| 119 | ret = put_user(boot_status, (int *)arg); |
| 120 | break; |
| 121 | |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 122 | case WDIOC_KEEPALIVE: |
| 123 | wdt_enable(); |
| 124 | ret = 0; |
| 125 | break; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | case WDIOC_SETTIMEOUT: |
| 128 | ret = get_user(time, (int *)arg); |
| 129 | if (ret) |
| 130 | break; |
| 131 | |
| 132 | if (time <= 0 || time > 60) { |
| 133 | ret = -EINVAL; |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | heartbeat = time; |
| 138 | wdt_enable(); |
| 139 | /* Fall through */ |
| 140 | |
| 141 | case WDIOC_GETTIMEOUT: |
| 142 | ret = put_user(heartbeat, (int *)arg); |
| 143 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | return ret; |
| 146 | } |
| 147 | |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 148 | static int ixp4xx_wdt_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 150 | if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | wdt_disable(); |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 152 | else |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 153 | pr_crit("Device closed unexpectedly - timer will not stop\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | clear_bit(WDT_IN_USE, &wdt_status); |
| 155 | clear_bit(WDT_OK_TO_CLOSE, &wdt_status); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 161 | static const struct file_operations ixp4xx_wdt_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | .owner = THIS_MODULE, |
| 163 | .llseek = no_llseek, |
| 164 | .write = ixp4xx_wdt_write, |
Alan Cox | 20d35f3e | 2008-05-19 14:06:48 +0100 | [diff] [blame] | 165 | .unlocked_ioctl = ixp4xx_wdt_ioctl, |
Arnd Bergmann | b6dfb24 | 2019-06-03 14:23:09 +0200 | [diff] [blame] | 166 | .compat_ioctl = compat_ptr_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | .open = ixp4xx_wdt_open, |
| 168 | .release = ixp4xx_wdt_release, |
| 169 | }; |
| 170 | |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 171 | static struct miscdevice ixp4xx_wdt_miscdev = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | .minor = WATCHDOG_MINOR, |
Olaf Hering | 2dab3ca | 2005-08-17 08:58:34 +0200 | [diff] [blame] | 173 | .name = "watchdog", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .fops = &ixp4xx_wdt_fops, |
| 175 | }; |
| 176 | |
| 177 | static int __init ixp4xx_wdt_init(void) |
| 178 | { |
| 179 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Linus Walleij | 9540724 | 2019-01-27 14:08:36 +0100 | [diff] [blame] | 181 | /* |
| 182 | * FIXME: we bail out on device tree boot but this really needs |
| 183 | * to be fixed in a nicer way: this registers the MDIO bus before |
| 184 | * even matching the driver infrastructure, we should only probe |
| 185 | * detected hardware. |
| 186 | */ |
| 187 | if (of_have_populated_dt()) |
| 188 | return -ENODEV; |
Russell King | 0ba8b9b | 2008-08-10 18:08:10 +0100 | [diff] [blame] | 189 | if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 190 | pr_err("Rev. A0 IXP42x CPU detected - watchdog disabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | return -ENODEV; |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ? |
| 195 | WDIOF_CARDRESET : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | ret = misc_register(&ixp4xx_wdt_miscdev); |
| 197 | if (ret == 0) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 198 | pr_info("timer heartbeat %d sec\n", heartbeat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static void __exit ixp4xx_wdt_exit(void) |
| 203 | { |
| 204 | misc_deregister(&ixp4xx_wdt_miscdev); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | module_init(ixp4xx_wdt_init); |
| 209 | module_exit(ixp4xx_wdt_exit); |
| 210 | |
| 211 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); |
| 212 | MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog"); |
| 213 | |
| 214 | module_param(heartbeat, int, 0); |
| 215 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)"); |
| 216 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 217 | module_param(nowayout, bool, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"); |
| 219 | |
| 220 | MODULE_LICENSE("GPL"); |