Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sp5100_tco : TCO timer driver for sp5100 chipsets |
| 3 | * |
| 4 | * (c) Copyright 2009 Google Inc., All Rights Reserved. |
| 5 | * |
| 6 | * Based on i8xx_tco.c: |
| 7 | * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights |
| 8 | * Reserved. |
| 9 | * http://www.kernelconcepts.de |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | * |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 16 | * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide", |
| 17 | * AMD Publication 45482 "AMD SB800-Series Southbridges Register |
| 18 | * Reference Guide" |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Includes, defines, variables, module parameters, ... |
| 23 | */ |
| 24 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 25 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 26 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/miscdevice.h> |
| 31 | #include <linux/watchdog.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/pci.h> |
| 35 | #include <linux/ioport.h> |
| 36 | #include <linux/platform_device.h> |
| 37 | #include <linux/uaccess.h> |
| 38 | #include <linux/io.h> |
| 39 | |
| 40 | #include "sp5100_tco.h" |
| 41 | |
| 42 | /* Module and version information */ |
Takahisa Tanaka | 18e4321 | 2013-03-03 14:52:07 +0900 | [diff] [blame] | 43 | #define TCO_VERSION "0.05" |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 44 | #define TCO_MODULE_NAME "SP5100 TCO timer" |
| 45 | #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 46 | |
| 47 | /* internal variables */ |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 48 | static u32 tcobase_phys; |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 49 | static u32 tco_wdt_fired; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 50 | static void __iomem *tcobase; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 51 | static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */ |
| 52 | static unsigned long timer_alive; |
| 53 | static char tco_expect_close; |
| 54 | static struct pci_dev *sp5100_tco_pci; |
| 55 | |
| 56 | /* the watchdog platform device */ |
| 57 | static struct platform_device *sp5100_tco_platform_device; |
| 58 | |
| 59 | /* module parameters */ |
| 60 | |
| 61 | #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */ |
| 62 | static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ |
| 63 | module_param(heartbeat, int, 0); |
| 64 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default=" |
| 65 | __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
| 66 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 67 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 68 | module_param(nowayout, bool, 0); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 69 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started." |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 70 | " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 71 | |
| 72 | /* |
| 73 | * Some TCO specific functions |
| 74 | */ |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 75 | |
| 76 | static bool tco_has_sp5100_reg_layout(struct pci_dev *dev) |
| 77 | { |
| 78 | return dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS && |
| 79 | dev->revision < 0x40; |
| 80 | } |
| 81 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 82 | static void tco_timer_start(void) |
| 83 | { |
| 84 | u32 val; |
| 85 | unsigned long flags; |
| 86 | |
| 87 | spin_lock_irqsave(&tco_lock, flags); |
| 88 | val = readl(SP5100_WDT_CONTROL(tcobase)); |
| 89 | val |= SP5100_WDT_START_STOP_BIT; |
| 90 | writel(val, SP5100_WDT_CONTROL(tcobase)); |
| 91 | spin_unlock_irqrestore(&tco_lock, flags); |
| 92 | } |
| 93 | |
| 94 | static void tco_timer_stop(void) |
| 95 | { |
| 96 | u32 val; |
| 97 | unsigned long flags; |
| 98 | |
| 99 | spin_lock_irqsave(&tco_lock, flags); |
| 100 | val = readl(SP5100_WDT_CONTROL(tcobase)); |
| 101 | val &= ~SP5100_WDT_START_STOP_BIT; |
| 102 | writel(val, SP5100_WDT_CONTROL(tcobase)); |
| 103 | spin_unlock_irqrestore(&tco_lock, flags); |
| 104 | } |
| 105 | |
| 106 | static void tco_timer_keepalive(void) |
| 107 | { |
| 108 | u32 val; |
| 109 | unsigned long flags; |
| 110 | |
| 111 | spin_lock_irqsave(&tco_lock, flags); |
| 112 | val = readl(SP5100_WDT_CONTROL(tcobase)); |
| 113 | val |= SP5100_WDT_TRIGGER_BIT; |
| 114 | writel(val, SP5100_WDT_CONTROL(tcobase)); |
| 115 | spin_unlock_irqrestore(&tco_lock, flags); |
| 116 | } |
| 117 | |
| 118 | static int tco_timer_set_heartbeat(int t) |
| 119 | { |
| 120 | unsigned long flags; |
| 121 | |
| 122 | if (t < 0 || t > 0xffff) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | /* Write new heartbeat to watchdog */ |
| 126 | spin_lock_irqsave(&tco_lock, flags); |
| 127 | writel(t, SP5100_WDT_COUNT(tcobase)); |
| 128 | spin_unlock_irqrestore(&tco_lock, flags); |
| 129 | |
| 130 | heartbeat = t; |
| 131 | return 0; |
| 132 | } |
| 133 | |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 134 | static u8 sp5100_tco_read_pm_reg8(u8 index) |
| 135 | { |
| 136 | outb(index, SP5100_IO_PM_INDEX_REG); |
| 137 | return inb(SP5100_IO_PM_DATA_REG); |
| 138 | } |
| 139 | |
| 140 | static void sp5100_tco_update_pm_reg8(u8 index, u8 reset, u8 set) |
| 141 | { |
| 142 | u8 val; |
| 143 | |
| 144 | outb(index, SP5100_IO_PM_INDEX_REG); |
| 145 | val = inb(SP5100_IO_PM_DATA_REG); |
| 146 | val &= reset; |
| 147 | val |= set; |
| 148 | outb(val, SP5100_IO_PM_DATA_REG); |
| 149 | } |
| 150 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 151 | static void tco_timer_enable(void) |
| 152 | { |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 153 | if (!tco_has_sp5100_reg_layout(sp5100_tco_pci)) { |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 154 | /* For SB800 or later */ |
| 155 | /* Set the Watchdog timer resolution to 1 sec */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 156 | sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG, |
| 157 | 0xff, SB800_PM_WATCHDOG_SECOND_RES); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 158 | |
| 159 | /* Enable watchdog decode bit and watchdog timer */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 160 | sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL, |
| 161 | ~SB800_PM_WATCHDOG_DISABLE, |
| 162 | SB800_PCI_WATCHDOG_DECODE_EN); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 163 | } else { |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 164 | u32 val; |
| 165 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 166 | /* For SP5100 or SB7x0 */ |
| 167 | /* Enable watchdog decode bit */ |
| 168 | pci_read_config_dword(sp5100_tco_pci, |
| 169 | SP5100_PCI_WATCHDOG_MISC_REG, |
| 170 | &val); |
| 171 | |
| 172 | val |= SP5100_PCI_WATCHDOG_DECODE_EN; |
| 173 | |
| 174 | pci_write_config_dword(sp5100_tco_pci, |
| 175 | SP5100_PCI_WATCHDOG_MISC_REG, |
| 176 | val); |
| 177 | |
| 178 | /* Enable Watchdog timer and set the resolution to 1 sec */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 179 | sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL, |
| 180 | ~SP5100_PM_WATCHDOG_DISABLE, |
| 181 | SP5100_PM_WATCHDOG_SECOND_RES); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 185 | /* |
| 186 | * /dev/watchdog handling |
| 187 | */ |
| 188 | |
| 189 | static int sp5100_tco_open(struct inode *inode, struct file *file) |
| 190 | { |
| 191 | /* /dev/watchdog can only be opened once */ |
| 192 | if (test_and_set_bit(0, &timer_alive)) |
| 193 | return -EBUSY; |
| 194 | |
| 195 | /* Reload and activate timer */ |
| 196 | tco_timer_start(); |
| 197 | tco_timer_keepalive(); |
| 198 | return nonseekable_open(inode, file); |
| 199 | } |
| 200 | |
| 201 | static int sp5100_tco_release(struct inode *inode, struct file *file) |
| 202 | { |
| 203 | /* Shut off the timer. */ |
| 204 | if (tco_expect_close == 42) { |
| 205 | tco_timer_stop(); |
| 206 | } else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 207 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 208 | tco_timer_keepalive(); |
| 209 | } |
| 210 | clear_bit(0, &timer_alive); |
| 211 | tco_expect_close = 0; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static ssize_t sp5100_tco_write(struct file *file, const char __user *data, |
| 216 | size_t len, loff_t *ppos) |
| 217 | { |
| 218 | /* See if we got the magic character 'V' and reload the timer */ |
| 219 | if (len) { |
| 220 | if (!nowayout) { |
| 221 | size_t i; |
| 222 | |
| 223 | /* note: just in case someone wrote the magic character |
| 224 | * five months ago... */ |
| 225 | tco_expect_close = 0; |
| 226 | |
| 227 | /* scan to see whether or not we got the magic character |
| 228 | */ |
| 229 | for (i = 0; i != len; i++) { |
| 230 | char c; |
| 231 | if (get_user(c, data + i)) |
| 232 | return -EFAULT; |
| 233 | if (c == 'V') |
| 234 | tco_expect_close = 42; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* someone wrote to us, we should reload the timer */ |
| 239 | tco_timer_keepalive(); |
| 240 | } |
| 241 | return len; |
| 242 | } |
| 243 | |
| 244 | static long sp5100_tco_ioctl(struct file *file, unsigned int cmd, |
| 245 | unsigned long arg) |
| 246 | { |
| 247 | int new_options, retval = -EINVAL; |
| 248 | int new_heartbeat; |
| 249 | void __user *argp = (void __user *)arg; |
| 250 | int __user *p = argp; |
| 251 | static const struct watchdog_info ident = { |
| 252 | .options = WDIOF_SETTIMEOUT | |
| 253 | WDIOF_KEEPALIVEPING | |
| 254 | WDIOF_MAGICCLOSE, |
| 255 | .firmware_version = 0, |
| 256 | .identity = TCO_MODULE_NAME, |
| 257 | }; |
| 258 | |
| 259 | switch (cmd) { |
| 260 | case WDIOC_GETSUPPORT: |
| 261 | return copy_to_user(argp, &ident, |
| 262 | sizeof(ident)) ? -EFAULT : 0; |
| 263 | case WDIOC_GETSTATUS: |
| 264 | case WDIOC_GETBOOTSTATUS: |
| 265 | return put_user(0, p); |
| 266 | case WDIOC_SETOPTIONS: |
| 267 | if (get_user(new_options, p)) |
| 268 | return -EFAULT; |
| 269 | if (new_options & WDIOS_DISABLECARD) { |
| 270 | tco_timer_stop(); |
| 271 | retval = 0; |
| 272 | } |
| 273 | if (new_options & WDIOS_ENABLECARD) { |
| 274 | tco_timer_start(); |
| 275 | tco_timer_keepalive(); |
| 276 | retval = 0; |
| 277 | } |
| 278 | return retval; |
| 279 | case WDIOC_KEEPALIVE: |
| 280 | tco_timer_keepalive(); |
| 281 | return 0; |
| 282 | case WDIOC_SETTIMEOUT: |
| 283 | if (get_user(new_heartbeat, p)) |
| 284 | return -EFAULT; |
| 285 | if (tco_timer_set_heartbeat(new_heartbeat)) |
| 286 | return -EINVAL; |
| 287 | tco_timer_keepalive(); |
| 288 | /* Fall through */ |
| 289 | case WDIOC_GETTIMEOUT: |
| 290 | return put_user(heartbeat, p); |
| 291 | default: |
| 292 | return -ENOTTY; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Kernel Interfaces |
| 298 | */ |
| 299 | |
| 300 | static const struct file_operations sp5100_tco_fops = { |
| 301 | .owner = THIS_MODULE, |
| 302 | .llseek = no_llseek, |
| 303 | .write = sp5100_tco_write, |
| 304 | .unlocked_ioctl = sp5100_tco_ioctl, |
| 305 | .open = sp5100_tco_open, |
| 306 | .release = sp5100_tco_release, |
| 307 | }; |
| 308 | |
| 309 | static struct miscdevice sp5100_tco_miscdev = { |
| 310 | .minor = WATCHDOG_MINOR, |
| 311 | .name = "watchdog", |
| 312 | .fops = &sp5100_tco_fops, |
| 313 | }; |
| 314 | |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 315 | static u8 sp5100_tco_read_pm_reg32(u8 index) |
| 316 | { |
| 317 | u32 val = 0; |
| 318 | int i; |
| 319 | |
| 320 | for (i = 3; i >= 0; i--) |
| 321 | val = (val << 8) + sp5100_tco_read_pm_reg8(index + i); |
| 322 | |
| 323 | return val; |
| 324 | } |
| 325 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 326 | /* |
| 327 | * Init & exit routines |
| 328 | */ |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 329 | static int sp5100_tco_setupdevice(struct device *dev) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 330 | { |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 331 | const char *dev_name = NULL; |
Takahisa Tanaka | 18e4321 | 2013-03-03 14:52:07 +0900 | [diff] [blame] | 332 | u32 val; |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 333 | u8 base_addr; |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 334 | int ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 335 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 336 | /* |
| 337 | * Determine type of southbridge chipset. |
| 338 | */ |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 339 | if (tco_has_sp5100_reg_layout(sp5100_tco_pci)) { |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 340 | dev_name = SP5100_DEVNAME; |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 341 | base_addr = SP5100_PM_WATCHDOG_BASE; |
Huang Rui | bdecfcd | 2015-11-23 18:07:35 +0800 | [diff] [blame] | 342 | } else { |
| 343 | dev_name = SB800_DEVNAME; |
Huang Rui | bdecfcd | 2015-11-23 18:07:35 +0800 | [diff] [blame] | 344 | base_addr = SB800_PM_WATCHDOG_BASE; |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 347 | /* Request the IO ports used by this driver */ |
Guenter Roeck | 16e7730 | 2017-12-24 13:04:08 -0800 | [diff] [blame] | 348 | if (!request_muxed_region(SP5100_IO_PM_INDEX_REG, |
| 349 | SP5100_PM_IOPORTS_SIZE, dev_name)) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 350 | dev_err(dev, "I/O address 0x%04x already in use\n", |
| 351 | SP5100_IO_PM_INDEX_REG); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 352 | return -EBUSY; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 355 | /* |
| 356 | * First, Find the watchdog timer MMIO address from indirect I/O. |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 357 | * Low three bits of BASE are reserved. |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 358 | */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 359 | val = sp5100_tco_read_pm_reg32(base_addr) & 0xfffffff8; |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 360 | |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 361 | dev_dbg(dev, "Got 0x%04x from indirect I/O\n", val); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 362 | |
| 363 | /* Check MMIO address conflict */ |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 364 | if (!request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE, |
| 365 | dev_name)) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 366 | dev_dbg(dev, "MMIO address 0x%04x already in use\n", val); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 367 | /* |
| 368 | * Secondly, Find the watchdog timer MMIO address |
| 369 | * from SBResource_MMIO register. |
| 370 | */ |
| 371 | if (tco_has_sp5100_reg_layout(sp5100_tco_pci)) { |
| 372 | /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */ |
| 373 | pci_read_config_dword(sp5100_tco_pci, |
| 374 | SP5100_SB_RESOURCE_MMIO_BASE, |
| 375 | &val); |
| 376 | } else { |
| 377 | /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */ |
| 378 | val = sp5100_tco_read_pm_reg32(SB800_PM_ACPI_MMIO_EN); |
| 379 | } |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 380 | |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 381 | /* The SBResource_MMIO is enabled and mapped memory space? */ |
| 382 | if ((val & (SB800_ACPI_MMIO_DECODE_EN | SB800_ACPI_MMIO_SEL)) != |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 383 | SB800_ACPI_MMIO_DECODE_EN) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 384 | dev_notice(dev, |
| 385 | "failed to find MMIO address, giving up.\n"); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 386 | ret = -ENODEV; |
| 387 | goto unreg_region; |
| 388 | } |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 389 | /* Clear unnecessary the low twelve bits */ |
| 390 | val &= ~0xFFF; |
| 391 | /* Add the Watchdog Timer offset to base address. */ |
| 392 | val += SB800_PM_WDT_MMIO_OFFSET; |
| 393 | /* Check MMIO address conflict */ |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 394 | if (!request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE, |
| 395 | dev_name)) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 396 | dev_dbg(dev, "MMIO address 0x%04x already in use\n", |
| 397 | val); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 398 | ret = -EBUSY; |
| 399 | goto unreg_region; |
| 400 | } |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 401 | dev_dbg(dev, "Got 0x%04x from SBResource_MMIO register\n", val); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 402 | } |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 403 | |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 404 | tcobase_phys = val; |
| 405 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 406 | tcobase = ioremap(val, SP5100_WDT_MEM_MAP_SIZE); |
H Hartley Sweeten | 62a9aeb | 2012-05-02 16:54:43 -0700 | [diff] [blame] | 407 | if (!tcobase) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 408 | dev_err(dev, "failed to get tcobase address\n"); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 409 | ret = -ENOMEM; |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 410 | goto unreg_mem_region; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 413 | dev_info(dev, "Using 0x%04x for watchdog MMIO address\n", val); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 414 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 415 | /* Setup the watchdog timer */ |
| 416 | tco_timer_enable(); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 417 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 418 | /* Check that the watchdog action is set to reset the system */ |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 419 | val = readl(SP5100_WDT_CONTROL(tcobase)); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 420 | /* |
| 421 | * Save WatchDogFired status, because WatchDogFired flag is |
| 422 | * cleared here. |
| 423 | */ |
| 424 | tco_wdt_fired = val & SP5100_PM_WATCHDOG_FIRED; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 425 | val &= ~SP5100_PM_WATCHDOG_ACTION_RESET; |
| 426 | writel(val, SP5100_WDT_CONTROL(tcobase)); |
| 427 | |
| 428 | /* Set a reasonable heartbeat before we stop the timer */ |
| 429 | tco_timer_set_heartbeat(heartbeat); |
| 430 | |
| 431 | /* |
| 432 | * Stop the TCO before we change anything so we don't race with |
| 433 | * a zeroed timer. |
| 434 | */ |
| 435 | tco_timer_stop(); |
| 436 | |
Guenter Roeck | 16e7730 | 2017-12-24 13:04:08 -0800 | [diff] [blame] | 437 | release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 438 | |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 439 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 440 | |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 441 | unreg_mem_region: |
| 442 | release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 443 | unreg_region: |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 444 | release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 445 | return ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 448 | static int sp5100_tco_init(struct platform_device *pdev) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 449 | { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 450 | struct device *dev = &pdev->dev; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 451 | int ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 452 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 453 | /* |
| 454 | * Check whether or not the hardware watchdog is there. If found, then |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 455 | * set it up. |
| 456 | */ |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 457 | ret = sp5100_tco_setupdevice(dev); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 458 | if (ret) |
| 459 | return ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 460 | |
| 461 | /* Check to see if last reboot was due to watchdog timeout */ |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 462 | dev_info(dev, "Last reboot was %striggered by watchdog.\n", |
| 463 | tco_wdt_fired ? "" : "not "); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * Check that the heartbeat value is within it's range. |
| 467 | * If not, reset to the default. |
| 468 | */ |
| 469 | if (tco_timer_set_heartbeat(heartbeat)) { |
| 470 | heartbeat = WATCHDOG_HEARTBEAT; |
| 471 | tco_timer_set_heartbeat(heartbeat); |
| 472 | } |
| 473 | |
| 474 | ret = misc_register(&sp5100_tco_miscdev); |
| 475 | if (ret != 0) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 476 | dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n", |
| 477 | WATCHDOG_MINOR, ret); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 478 | goto exit; |
| 479 | } |
| 480 | |
| 481 | clear_bit(0, &timer_alive); |
| 482 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 483 | /* Show module parameters */ |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame^] | 484 | dev_info(dev, "initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n", |
| 485 | tcobase, heartbeat, nowayout); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 486 | |
| 487 | return 0; |
| 488 | |
| 489 | exit: |
| 490 | iounmap(tcobase); |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 491 | release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 495 | static void sp5100_tco_cleanup(void) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 496 | { |
| 497 | /* Stop the timer before we leave */ |
| 498 | if (!nowayout) |
| 499 | tco_timer_stop(); |
| 500 | |
| 501 | /* Deregister */ |
| 502 | misc_deregister(&sp5100_tco_miscdev); |
| 503 | iounmap(tcobase); |
Yinghai Lu | 90d241e | 2011-03-16 20:01:07 -0700 | [diff] [blame] | 504 | release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 507 | static int sp5100_tco_remove(struct platform_device *dev) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 508 | { |
| 509 | if (tcobase) |
| 510 | sp5100_tco_cleanup(); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static void sp5100_tco_shutdown(struct platform_device *dev) |
| 515 | { |
| 516 | tco_timer_stop(); |
| 517 | } |
| 518 | |
| 519 | static struct platform_driver sp5100_tco_driver = { |
| 520 | .probe = sp5100_tco_init, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 521 | .remove = sp5100_tco_remove, |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 522 | .shutdown = sp5100_tco_shutdown, |
| 523 | .driver = { |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 524 | .name = TCO_MODULE_NAME, |
| 525 | }, |
| 526 | }; |
| 527 | |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 528 | /* |
| 529 | * Data for PCI driver interface |
| 530 | * |
| 531 | * This data only exists for exporting the supported |
| 532 | * PCI ids via MODULE_DEVICE_TABLE. We do not actually |
| 533 | * register a pci_driver, because someone else might |
| 534 | * want to register another driver on the same PCI id. |
| 535 | */ |
| 536 | static const struct pci_device_id sp5100_tco_pci_tbl[] = { |
| 537 | { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID, |
| 538 | PCI_ANY_ID, }, |
| 539 | { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID, |
| 540 | PCI_ANY_ID, }, |
| 541 | { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID, |
| 542 | PCI_ANY_ID, }, |
| 543 | { 0, }, /* End of list */ |
| 544 | }; |
| 545 | MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl); |
| 546 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 547 | static int __init sp5100_tco_init_module(void) |
| 548 | { |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 549 | struct pci_dev *dev = NULL; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 550 | int err; |
| 551 | |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 552 | /* Match the PCI device */ |
| 553 | for_each_pci_dev(dev) { |
| 554 | if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) { |
| 555 | sp5100_tco_pci = dev; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (!sp5100_tco_pci) |
| 561 | return -ENODEV; |
| 562 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 563 | pr_info("SP5100/SB800 TCO WatchDog Timer Driver v%s\n", TCO_VERSION); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 564 | |
| 565 | err = platform_driver_register(&sp5100_tco_driver); |
| 566 | if (err) |
| 567 | return err; |
| 568 | |
| 569 | sp5100_tco_platform_device = platform_device_register_simple( |
| 570 | TCO_MODULE_NAME, -1, NULL, 0); |
| 571 | if (IS_ERR(sp5100_tco_platform_device)) { |
| 572 | err = PTR_ERR(sp5100_tco_platform_device); |
| 573 | goto unreg_platform_driver; |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | |
| 578 | unreg_platform_driver: |
| 579 | platform_driver_unregister(&sp5100_tco_driver); |
| 580 | return err; |
| 581 | } |
| 582 | |
| 583 | static void __exit sp5100_tco_cleanup_module(void) |
| 584 | { |
| 585 | platform_device_unregister(sp5100_tco_platform_device); |
| 586 | platform_driver_unregister(&sp5100_tco_driver); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | module_init(sp5100_tco_init_module); |
| 590 | module_exit(sp5100_tco_cleanup_module); |
| 591 | |
| 592 | MODULE_AUTHOR("Priyanka Gupta"); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 593 | MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset"); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 594 | MODULE_LICENSE("GPL"); |