Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * ALi M7101 PMU Computer Watchdog Timer driver |
| 4 | * |
| 5 | * Based on w83877f_wdt.c by Scott Jennings <linuxdrivers@oro.net> |
| 6 | * and the Cobalt kernel WDT timer driver by Tim Hockin |
| 7 | * <thockin@cobaltnet.com> |
| 8 | * |
| 9 | * (c)2002 Steve Hill <steve@navaho.co.uk> |
| 10 | * |
| 11 | * This WDT driver is different from most other Linux WDT |
| 12 | * drivers in that the driver will ping the watchdog by itself, |
| 13 | * because this particular WDT has a very short timeout (1.6 |
| 14 | * seconds) and it would be insane to count on any userspace |
| 15 | * daemon always getting scheduled within that time frame. |
| 16 | * |
| 17 | * Additions: |
| 18 | * Aug 23, 2004 - Added use_gpio module parameter for use on revision a1d PMUs |
| 19 | * found on very old cobalt hardware. |
| 20 | * -- Mike Waychison <michael.waychison@sun.com> |
| 21 | */ |
| 22 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/timer.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/watchdog.h> |
| 31 | #include <linux/ioport.h> |
| 32 | #include <linux/notifier.h> |
| 33 | #include <linux/reboot.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/fs.h> |
| 36 | #include <linux/pci.h> |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 37 | #include <linux/io.h> |
| 38 | #include <linux/uaccess.h> |
Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define WDT_ENABLE 0x9C |
| 42 | #define WDT_DISABLE 0x8C |
| 43 | |
| 44 | #define ALI_7101_WDT 0x92 |
| 45 | #define ALI_7101_GPIO 0x7D |
| 46 | #define ALI_7101_GPIO_O 0x7E |
| 47 | #define ALI_WDT_ARM 0x01 |
| 48 | |
| 49 | /* |
| 50 | * We're going to use a 1 second timeout. |
| 51 | * If we reset the watchdog every ~250ms we should be safe. */ |
| 52 | |
| 53 | #define WDT_INTERVAL (HZ/4+1) |
| 54 | |
| 55 | /* |
| 56 | * We must not require too good response from the userspace daemon. |
| 57 | * Here we require the userspace daemon to send us a heartbeat |
| 58 | * char to /dev/watchdog every 30 seconds. |
| 59 | */ |
| 60 | |
| 61 | #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 62 | /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */ |
| 63 | static int timeout = WATCHDOG_TIMEOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | module_param(timeout, int, 0); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 65 | MODULE_PARM_DESC(timeout, |
| 66 | "Watchdog timeout in seconds. (1<=timeout<=3600, default=" |
| 67 | __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 69 | static int use_gpio; /* Use the pic (for a1d revision alim7101) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | module_param(use_gpio, int, 0); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 71 | MODULE_PARM_DESC(use_gpio, |
| 72 | "Use the gpio watchdog (required by old cobalt boards)."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 74 | static void wdt_timer_ping(struct timer_list *); |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 75 | static DEFINE_TIMER(timer, wdt_timer_ping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | static unsigned long next_heartbeat; |
| 77 | static unsigned long wdt_is_open; |
| 78 | static char wdt_expect_close; |
| 79 | static struct pci_dev *alim7101_pmu; |
| 80 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 81 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 82 | module_param(nowayout, bool, 0); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 83 | MODULE_PARM_DESC(nowayout, |
| 84 | "Watchdog cannot be stopped once started (default=" |
| 85 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Whack the dog |
| 89 | */ |
| 90 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 91 | static void wdt_timer_ping(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | /* If we got a heartbeat pulse within the WDT_US_INTERVAL |
| 94 | * we agree to ping the WDT |
| 95 | */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 96 | char tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 98 | if (time_before(jiffies, next_heartbeat)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* Ping the WDT (this is actually a disarm/arm sequence) */ |
| 100 | pci_read_config_byte(alim7101_pmu, 0x92, &tmp); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 101 | pci_write_config_byte(alim7101_pmu, |
| 102 | ALI_7101_WDT, (tmp & ~ALI_WDT_ARM)); |
| 103 | pci_write_config_byte(alim7101_pmu, |
| 104 | ALI_7101_WDT, (tmp | ALI_WDT_ARM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (use_gpio) { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 106 | pci_read_config_byte(alim7101_pmu, |
| 107 | ALI_7101_GPIO_O, &tmp); |
| 108 | pci_write_config_byte(alim7101_pmu, |
| 109 | ALI_7101_GPIO_O, tmp | 0x20); |
| 110 | pci_write_config_byte(alim7101_pmu, |
| 111 | ALI_7101_GPIO_O, tmp & ~0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | } else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 114 | pr_warn("Heartbeat lost! Will not ping the watchdog\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | /* Re-set the timer interval */ |
Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 117 | mod_timer(&timer, jiffies + WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Utility routines |
| 122 | */ |
| 123 | |
| 124 | static void wdt_change(int writeval) |
| 125 | { |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 126 | char tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp); |
| 129 | if (writeval == WDT_ENABLE) { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 130 | pci_write_config_byte(alim7101_pmu, |
| 131 | ALI_7101_WDT, (tmp | ALI_WDT_ARM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (use_gpio) { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 133 | pci_read_config_byte(alim7101_pmu, |
| 134 | ALI_7101_GPIO_O, &tmp); |
| 135 | pci_write_config_byte(alim7101_pmu, |
| 136 | ALI_7101_GPIO_O, tmp & ~0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } else { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 140 | pci_write_config_byte(alim7101_pmu, |
| 141 | ALI_7101_WDT, (tmp & ~ALI_WDT_ARM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | if (use_gpio) { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 143 | pci_read_config_byte(alim7101_pmu, |
| 144 | ALI_7101_GPIO_O, &tmp); |
| 145 | pci_write_config_byte(alim7101_pmu, |
| 146 | ALI_7101_GPIO_O, tmp | 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static void wdt_startup(void) |
| 152 | { |
| 153 | next_heartbeat = jiffies + (timeout * HZ); |
| 154 | |
| 155 | /* We must enable before we kick off the timer in case the timer |
| 156 | occurs as we ping it */ |
| 157 | |
| 158 | wdt_change(WDT_ENABLE); |
| 159 | |
| 160 | /* Start the timer */ |
Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 161 | mod_timer(&timer, jiffies + WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 163 | pr_info("Watchdog timer is now enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void wdt_turnoff(void) |
| 167 | { |
| 168 | /* Stop the timer */ |
| 169 | del_timer_sync(&timer); |
| 170 | wdt_change(WDT_DISABLE); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 171 | pr_info("Watchdog timer is now disabled...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void wdt_keepalive(void) |
| 175 | { |
| 176 | /* user land ping */ |
| 177 | next_heartbeat = jiffies + (timeout * HZ); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * /dev/watchdog handling |
| 182 | */ |
| 183 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 184 | static ssize_t fop_write(struct file *file, const char __user *buf, |
| 185 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
| 187 | /* See if we got the magic character 'V' and reload the timer */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 188 | if (count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (!nowayout) { |
| 190 | size_t ofs; |
| 191 | |
| 192 | /* note: just in case someone wrote the magic character |
| 193 | * five months ago... */ |
| 194 | wdt_expect_close = 0; |
| 195 | |
| 196 | /* now scan */ |
| 197 | for (ofs = 0; ofs != count; ofs++) { |
| 198 | char c; |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 199 | if (get_user(c, buf + ofs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return -EFAULT; |
| 201 | if (c == 'V') |
| 202 | wdt_expect_close = 42; |
| 203 | } |
| 204 | } |
| 205 | /* someone wrote to us, we should restart timer */ |
| 206 | wdt_keepalive(); |
| 207 | } |
| 208 | return count; |
| 209 | } |
| 210 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 211 | static int fop_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | /* Just in case we're already talking to someone... */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 214 | if (test_and_set_bit(0, &wdt_is_open)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return -EBUSY; |
| 216 | /* Good, fire up the show */ |
| 217 | wdt_startup(); |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 218 | return stream_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 221 | static int fop_close(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 223 | if (wdt_expect_close == 42) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | wdt_turnoff(); |
| 225 | else { |
| 226 | /* wim: shouldn't there be a: del_timer(&timer); */ |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 227 | pr_crit("device file closed unexpectedly. Will not stop the WDT!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | clear_bit(0, &wdt_is_open); |
| 230 | wdt_expect_close = 0; |
| 231 | return 0; |
| 232 | } |
| 233 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 234 | static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
| 236 | void __user *argp = (void __user *)arg; |
| 237 | int __user *p = argp; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 238 | static const struct watchdog_info ident = { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 239 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
| 240 | | WDIOF_MAGICCLOSE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | .firmware_version = 1, |
| 242 | .identity = "ALiM7101", |
| 243 | }; |
| 244 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 245 | switch (cmd) { |
| 246 | case WDIOC_GETSUPPORT: |
| 247 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; |
| 248 | case WDIOC_GETSTATUS: |
| 249 | case WDIOC_GETBOOTSTATUS: |
| 250 | return put_user(0, p); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 251 | case WDIOC_SETOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 253 | int new_options, retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 255 | if (get_user(new_options, p)) |
| 256 | return -EFAULT; |
| 257 | if (new_options & WDIOS_DISABLECARD) { |
| 258 | wdt_turnoff(); |
| 259 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 261 | if (new_options & WDIOS_ENABLECARD) { |
| 262 | wdt_startup(); |
| 263 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 265 | return retval; |
| 266 | } |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 267 | case WDIOC_KEEPALIVE: |
| 268 | wdt_keepalive(); |
| 269 | return 0; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 270 | case WDIOC_SETTIMEOUT: |
| 271 | { |
| 272 | int new_timeout; |
| 273 | |
| 274 | if (get_user(new_timeout, p)) |
| 275 | return -EFAULT; |
| 276 | /* arbitrary upper limit */ |
| 277 | if (new_timeout < 1 || new_timeout > 3600) |
| 278 | return -EINVAL; |
| 279 | timeout = new_timeout; |
| 280 | wdt_keepalive(); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 281 | } |
Gustavo A. R. Silva | 265d11c | 2019-03-20 13:16:58 -0500 | [diff] [blame] | 282 | /* Fall through */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 283 | case WDIOC_GETTIMEOUT: |
| 284 | return put_user(timeout, p); |
| 285 | default: |
| 286 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 290 | static const struct file_operations wdt_fops = { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 291 | .owner = THIS_MODULE, |
| 292 | .llseek = no_llseek, |
| 293 | .write = fop_write, |
| 294 | .open = fop_open, |
| 295 | .release = fop_close, |
| 296 | .unlocked_ioctl = fop_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | static struct miscdevice wdt_miscdev = { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 300 | .minor = WATCHDOG_MINOR, |
| 301 | .name = "watchdog", |
| 302 | .fops = &wdt_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
Guenter Roeck | 87ffc69 | 2014-09-26 00:03:17 +0000 | [diff] [blame] | 305 | static int wdt_restart_handle(struct notifier_block *this, unsigned long mode, |
| 306 | void *cmd) |
| 307 | { |
| 308 | /* |
| 309 | * Cobalt devices have no way of rebooting themselves other |
| 310 | * than getting the watchdog to pull reset, so we restart the |
| 311 | * watchdog on reboot with no heartbeat. |
| 312 | */ |
| 313 | wdt_change(WDT_ENABLE); |
| 314 | |
| 315 | /* loop until the watchdog fires */ |
| 316 | while (true) |
| 317 | ; |
| 318 | |
| 319 | return NOTIFY_DONE; |
| 320 | } |
| 321 | |
| 322 | static struct notifier_block wdt_restart_handler = { |
| 323 | .notifier_call = wdt_restart_handle, |
| 324 | .priority = 128, |
| 325 | }; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | /* |
| 328 | * Notifier for system down |
| 329 | */ |
| 330 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 331 | static int wdt_notify_sys(struct notifier_block *this, |
| 332 | unsigned long code, void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 334 | if (code == SYS_DOWN || code == SYS_HALT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | wdt_turnoff(); |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return NOTIFY_DONE; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * The WDT needs to learn about soft shutdowns in order to |
| 342 | * turn the timebomb registers off. |
| 343 | */ |
| 344 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 345 | static struct notifier_block wdt_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .notifier_call = wdt_notify_sys, |
| 347 | }; |
| 348 | |
| 349 | static void __exit alim7101_wdt_unload(void) |
| 350 | { |
| 351 | wdt_turnoff(); |
| 352 | /* Deregister */ |
| 353 | misc_deregister(&wdt_miscdev); |
| 354 | unregister_reboot_notifier(&wdt_notifier); |
Guenter Roeck | 87ffc69 | 2014-09-26 00:03:17 +0000 | [diff] [blame] | 355 | unregister_restart_handler(&wdt_restart_handler); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 356 | pci_dev_put(alim7101_pmu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int __init alim7101_wdt_init(void) |
| 360 | { |
| 361 | int rc = -EBUSY; |
| 362 | struct pci_dev *ali1543_south; |
| 363 | char tmp; |
| 364 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 365 | pr_info("Steve Hill <steve@navaho.co.uk>\n"); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 366 | alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101, |
| 367 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (!alim7101_pmu) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 369 | pr_info("ALi M7101 PMU not present - WDT not set\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return -EBUSY; |
| 371 | } |
| 372 | |
| 373 | /* Set the WDT in the PMU to 1 second */ |
| 374 | pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02); |
| 375 | |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 376 | ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, |
| 377 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (!ali1543_south) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 379 | pr_info("ALi 1543 South-Bridge not present - WDT not set\n"); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 380 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | pci_read_config_byte(ali1543_south, 0x5e, &tmp); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 383 | pci_dev_put(ali1543_south); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if ((tmp & 0x1e) == 0x00) { |
| 385 | if (!use_gpio) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 386 | pr_info("Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n"); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 387 | goto err_out; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | nowayout = 1; |
| 390 | } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 391 | pr_info("ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n"); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 392 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 395 | if (timeout < 1 || timeout > 3600) { |
| 396 | /* arbitrary upper limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | timeout = WATCHDOG_TIMEOUT; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 398 | pr_info("timeout value must be 1 <= x <= 3600, using %d\n", |
| 399 | timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | rc = register_reboot_notifier(&wdt_notifier); |
| 403 | if (rc) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 404 | pr_err("cannot register reboot notifier (err=%d)\n", rc); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 405 | goto err_out; |
| 406 | } |
| 407 | |
Guenter Roeck | 87ffc69 | 2014-09-26 00:03:17 +0000 | [diff] [blame] | 408 | rc = register_restart_handler(&wdt_restart_handler); |
| 409 | if (rc) { |
| 410 | pr_err("cannot register restart handler (err=%d)\n", rc); |
| 411 | goto err_out_reboot; |
| 412 | } |
| 413 | |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 414 | rc = misc_register(&wdt_miscdev); |
| 415 | if (rc) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 416 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
| 417 | wdt_miscdev.minor, rc); |
Guenter Roeck | 87ffc69 | 2014-09-26 00:03:17 +0000 | [diff] [blame] | 418 | goto err_out_restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 421 | if (nowayout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | __module_get(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 424 | pr_info("WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | timeout, nowayout); |
| 426 | return 0; |
| 427 | |
Guenter Roeck | 87ffc69 | 2014-09-26 00:03:17 +0000 | [diff] [blame] | 428 | err_out_restart: |
| 429 | unregister_restart_handler(&wdt_restart_handler); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 430 | err_out_reboot: |
| 431 | unregister_reboot_notifier(&wdt_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | err_out: |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 433 | pci_dev_put(alim7101_pmu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return rc; |
| 435 | } |
| 436 | |
| 437 | module_init(alim7101_wdt_init); |
| 438 | module_exit(alim7101_wdt_unload); |
| 439 | |
Jingoo Han | bc17f9d | 2013-12-03 08:30:22 +0900 | [diff] [blame] | 440 | static const struct pci_device_id alim7101_pci_tbl[] __used = { |
Jiri Slaby | 29d73aa | 2007-02-12 00:51:48 -0800 | [diff] [blame] | 441 | { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) }, |
| 442 | { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) }, |
Ben Collins | 5cacb9f | 2006-10-18 08:24:30 -0400 | [diff] [blame] | 443 | { } |
| 444 | }; |
| 445 | |
| 446 | MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl); |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | MODULE_AUTHOR("Steve Hill"); |
| 449 | MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver"); |
| 450 | MODULE_LICENSE("GPL"); |