Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/ide/setup-pci.c Version 1.10 2002/08/19 |
| 3 | * |
| 4 | * Copyright (c) 1998-2000 Andre Hedrick <andre@linux-ide.org> |
| 5 | * |
| 6 | * Copyright (c) 1995-1998 Mark Lord |
| 7 | * May be copied or modified under the terms of the GNU General Public License |
| 8 | * |
| 9 | * Recent Changes |
| 10 | * Split the set up function into multiple functions |
| 11 | * Use pci_set_master |
| 12 | * Fix misreporting of I/O v MMIO problems |
| 13 | * Initial fixups for simplex devices |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * This module provides support for automatic detection and |
| 18 | * configuration of all PCI IDE interfaces present in a system. |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/timer.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/ide.h> |
| 30 | #include <linux/dma-mapping.h> |
| 31 | |
| 32 | #include <asm/io.h> |
| 33 | #include <asm/irq.h> |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * ide_match_hwif - match a PCI IDE against an ide_hwif |
| 38 | * @io_base: I/O base of device |
| 39 | * @bootable: set if its bootable |
| 40 | * @name: name of device |
| 41 | * |
| 42 | * Match a PCI IDE port against an entry in ide_hwifs[], |
| 43 | * based on io_base port if possible. Return the matching hwif, |
| 44 | * or a new hwif. If we find an error (clashing, out of devices, etc) |
| 45 | * return NULL |
| 46 | * |
| 47 | * FIXME: we need to handle mmio matches here too |
| 48 | */ |
| 49 | |
| 50 | static ide_hwif_t *ide_match_hwif(unsigned long io_base, u8 bootable, const char *name) |
| 51 | { |
| 52 | int h; |
| 53 | ide_hwif_t *hwif; |
| 54 | |
| 55 | /* |
| 56 | * Look for a hwif with matching io_base specified using |
| 57 | * parameters to ide_setup(). |
| 58 | */ |
| 59 | for (h = 0; h < MAX_HWIFS; ++h) { |
| 60 | hwif = &ide_hwifs[h]; |
| 61 | if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) { |
| 62 | if (hwif->chipset == ide_forced) |
| 63 | return hwif; /* a perfect match */ |
| 64 | } |
| 65 | } |
| 66 | /* |
| 67 | * Look for a hwif with matching io_base default value. |
| 68 | * If chipset is "ide_unknown", then claim that hwif slot. |
| 69 | * Otherwise, some other chipset has already claimed it.. :( |
| 70 | */ |
| 71 | for (h = 0; h < MAX_HWIFS; ++h) { |
| 72 | hwif = &ide_hwifs[h]; |
| 73 | if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) { |
| 74 | if (hwif->chipset == ide_unknown) |
| 75 | return hwif; /* match */ |
| 76 | printk(KERN_ERR "%s: port 0x%04lx already claimed by %s\n", |
| 77 | name, io_base, hwif->name); |
| 78 | return NULL; /* already claimed */ |
| 79 | } |
| 80 | } |
| 81 | /* |
| 82 | * Okay, there is no hwif matching our io_base, |
| 83 | * so we'll just claim an unassigned slot. |
| 84 | * Give preference to claiming other slots before claiming ide0/ide1, |
| 85 | * just in case there's another interface yet-to-be-scanned |
| 86 | * which uses ports 1f0/170 (the ide0/ide1 defaults). |
| 87 | * |
| 88 | * Unless there is a bootable card that does not use the standard |
| 89 | * ports 1f0/170 (the ide0/ide1 defaults). The (bootable) flag. |
| 90 | */ |
| 91 | if (bootable) { |
| 92 | for (h = 0; h < MAX_HWIFS; ++h) { |
| 93 | hwif = &ide_hwifs[h]; |
| 94 | if (hwif->chipset == ide_unknown) |
| 95 | return hwif; /* pick an unused entry */ |
| 96 | } |
| 97 | } else { |
| 98 | for (h = 2; h < MAX_HWIFS; ++h) { |
| 99 | hwif = ide_hwifs + h; |
| 100 | if (hwif->chipset == ide_unknown) |
| 101 | return hwif; /* pick an unused entry */ |
| 102 | } |
| 103 | } |
Matt Mackall | 83d7dbc | 2006-10-03 01:14:16 -0700 | [diff] [blame] | 104 | for (h = 0; h < 2 && h < MAX_HWIFS; ++h) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | hwif = ide_hwifs + h; |
| 106 | if (hwif->chipset == ide_unknown) |
| 107 | return hwif; /* pick an unused entry */ |
| 108 | } |
| 109 | printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n", name); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * ide_setup_pci_baseregs - place a PCI IDE controller native |
| 115 | * @dev: PCI device of interface to switch native |
| 116 | * @name: Name of interface |
| 117 | * |
| 118 | * We attempt to place the PCI interface into PCI native mode. If |
| 119 | * we succeed the BARs are ok and the controller is in PCI mode. |
| 120 | * Returns 0 on success or an errno code. |
| 121 | * |
| 122 | * FIXME: if we program the interface and then fail to set the BARS |
| 123 | * we don't switch it back to legacy mode. Do we actually care ?? |
| 124 | */ |
| 125 | |
| 126 | static int ide_setup_pci_baseregs (struct pci_dev *dev, const char *name) |
| 127 | { |
| 128 | u8 progif = 0; |
| 129 | |
| 130 | /* |
| 131 | * Place both IDE interfaces into PCI "native" mode: |
| 132 | */ |
| 133 | if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) || |
| 134 | (progif & 5) != 5) { |
| 135 | if ((progif & 0xa) != 0xa) { |
| 136 | printk(KERN_INFO "%s: device not capable of full " |
| 137 | "native PCI mode\n", name); |
| 138 | return -EOPNOTSUPP; |
| 139 | } |
| 140 | printk("%s: placing both ports into native PCI mode\n", name); |
| 141 | (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5); |
| 142 | if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) || |
| 143 | (progif & 5) != 5) { |
| 144 | printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted " |
| 145 | "0x%04x, got 0x%04x\n", |
| 146 | name, progif|5, progif); |
| 147 | return -EOPNOTSUPP; |
| 148 | } |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | #ifdef CONFIG_BLK_DEV_IDEDMA_PCI |
| 154 | |
| 155 | #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED |
| 156 | /* |
| 157 | * Long lost data from 2.0.34 that is now in 2.0.39 |
| 158 | * |
| 159 | * This was used in ./drivers/block/triton.c to do DMA Base address setup |
| 160 | * when PnP failed. Oh the things we forget. I believe this was part |
| 161 | * of SFF-8038i that has been withdrawn from public access... :-(( |
| 162 | */ |
| 163 | #define DEFAULT_BMIBA 0xe800 /* in case BIOS did not init it */ |
| 164 | #define DEFAULT_BMCRBA 0xcc00 /* VIA's default value */ |
| 165 | #define DEFAULT_BMALIBA 0xd400 /* ALI's default value */ |
| 166 | #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */ |
| 167 | |
| 168 | /** |
| 169 | * ide_get_or_set_dma_base - setup BMIBA |
| 170 | * @hwif: Interface |
| 171 | * |
| 172 | * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space: |
| 173 | * If need be we set up the DMA base. Where a device has a partner that |
| 174 | * is already in DMA mode we check and enforce IDE simplex rules. |
| 175 | */ |
| 176 | |
| 177 | static unsigned long ide_get_or_set_dma_base (ide_hwif_t *hwif) |
| 178 | { |
| 179 | unsigned long dma_base = 0; |
| 180 | struct pci_dev *dev = hwif->pci_dev; |
| 181 | |
| 182 | #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED |
| 183 | int second_chance = 0; |
| 184 | |
| 185 | second_chance_to_dma: |
| 186 | #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */ |
| 187 | |
| 188 | if (hwif->mmio) |
| 189 | return hwif->dma_base; |
| 190 | |
| 191 | if (hwif->mate && hwif->mate->dma_base) { |
| 192 | dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8); |
| 193 | } else { |
| 194 | dma_base = pci_resource_start(dev, 4); |
| 195 | if (!dma_base) { |
| 196 | printk(KERN_ERR "%s: dma_base is invalid\n", |
| 197 | hwif->cds->name); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED |
| 202 | /* FIXME - should use pci_assign_resource surely */ |
| 203 | if ((!dma_base) && (!second_chance)) { |
| 204 | unsigned long set_bmiba = 0; |
| 205 | second_chance++; |
| 206 | switch(dev->vendor) { |
| 207 | case PCI_VENDOR_ID_AL: |
| 208 | set_bmiba = DEFAULT_BMALIBA; break; |
| 209 | case PCI_VENDOR_ID_VIA: |
| 210 | set_bmiba = DEFAULT_BMCRBA; break; |
| 211 | case PCI_VENDOR_ID_INTEL: |
| 212 | set_bmiba = DEFAULT_BMIBA; break; |
| 213 | default: |
| 214 | return dma_base; |
| 215 | } |
| 216 | pci_write_config_dword(dev, 0x20, set_bmiba|1); |
| 217 | goto second_chance_to_dma; |
| 218 | } |
| 219 | #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */ |
| 220 | |
| 221 | if (dma_base) { |
| 222 | u8 simplex_stat = 0; |
| 223 | dma_base += hwif->channel ? 8 : 0; |
| 224 | |
| 225 | switch(dev->device) { |
| 226 | case PCI_DEVICE_ID_AL_M5219: |
| 227 | case PCI_DEVICE_ID_AL_M5229: |
| 228 | case PCI_DEVICE_ID_AMD_VIPER_7409: |
| 229 | case PCI_DEVICE_ID_CMD_643: |
| 230 | case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE: |
Matt Gillette | 2f09a7f | 2005-08-18 22:27:07 +0200 | [diff] [blame] | 231 | case PCI_DEVICE_ID_REVOLUTION: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | simplex_stat = hwif->INB(dma_base + 2); |
| 233 | hwif->OUTB((simplex_stat&0x60),(dma_base + 2)); |
| 234 | simplex_stat = hwif->INB(dma_base + 2); |
| 235 | if (simplex_stat & 0x80) { |
| 236 | printk(KERN_INFO "%s: simplex device: " |
| 237 | "DMA forced\n", |
| 238 | hwif->cds->name); |
| 239 | } |
| 240 | break; |
| 241 | default: |
| 242 | /* |
| 243 | * If the device claims "simplex" DMA, |
| 244 | * this means only one of the two interfaces |
| 245 | * can be trusted with DMA at any point in time. |
| 246 | * So we should enable DMA only on one of the |
| 247 | * two interfaces. |
| 248 | */ |
| 249 | simplex_stat = hwif->INB(dma_base + 2); |
| 250 | if (simplex_stat & 0x80) { |
| 251 | /* simplex device? */ |
| 252 | /* |
| 253 | * At this point we haven't probed the drives so we can't make the |
| 254 | * appropriate decision. Really we should defer this problem |
| 255 | * until we tune the drive then try to grab DMA ownership if we want |
| 256 | * to be the DMA end. This has to be become dynamic to handle hot |
| 257 | * plug. |
| 258 | */ |
| 259 | if (hwif->mate && hwif->mate->dma_base) { |
| 260 | printk(KERN_INFO "%s: simplex device: " |
| 261 | "DMA disabled\n", |
| 262 | hwif->cds->name); |
| 263 | dma_base = 0; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return dma_base; |
| 269 | } |
| 270 | #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */ |
| 271 | |
| 272 | void ide_setup_pci_noise (struct pci_dev *dev, ide_pci_device_t *d) |
| 273 | { |
| 274 | printk(KERN_INFO "%s: IDE controller at PCI slot %s\n", |
| 275 | d->name, pci_name(dev)); |
| 276 | } |
| 277 | |
| 278 | EXPORT_SYMBOL_GPL(ide_setup_pci_noise); |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * ide_pci_enable - do PCI enables |
| 283 | * @dev: PCI device |
| 284 | * @d: IDE pci device data |
| 285 | * |
| 286 | * Enable the IDE PCI device. We attempt to enable the device in full |
| 287 | * but if that fails then we only need BAR4 so we will enable that. |
| 288 | * |
| 289 | * Returns zero on success or an error code |
| 290 | */ |
| 291 | |
| 292 | static int ide_pci_enable(struct pci_dev *dev, ide_pci_device_t *d) |
| 293 | { |
| 294 | int ret; |
| 295 | |
| 296 | if (pci_enable_device(dev)) { |
| 297 | ret = pci_enable_device_bars(dev, 1 << 4); |
| 298 | if (ret < 0) { |
| 299 | printk(KERN_WARNING "%s: (ide_setup_pci_device:) " |
| 300 | "Could not enable device.\n", d->name); |
| 301 | goto out; |
| 302 | } |
| 303 | printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * assume all devices can do 32-bit dma for now. we can add a |
| 308 | * dma mask field to the ide_pci_device_t if we need it (or let |
| 309 | * lower level driver set the dma mask) |
| 310 | */ |
| 311 | ret = pci_set_dma_mask(dev, DMA_32BIT_MASK); |
| 312 | if (ret < 0) { |
| 313 | printk(KERN_ERR "%s: can't set dma mask\n", d->name); |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | /* FIXME: Temporary - until we put in the hotplug interface logic |
| 318 | Check that the bits we want are not in use by someone else. */ |
| 319 | ret = pci_request_region(dev, 4, "ide_tmp"); |
| 320 | if (ret < 0) |
| 321 | goto out; |
| 322 | |
| 323 | pci_release_region(dev, 4); |
| 324 | out: |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * ide_pci_configure - configure an unconfigured device |
| 330 | * @dev: PCI device |
| 331 | * @d: IDE pci device data |
| 332 | * |
| 333 | * Enable and configure the PCI device we have been passed. |
| 334 | * Returns zero on success or an error code. |
| 335 | */ |
| 336 | |
| 337 | static int ide_pci_configure(struct pci_dev *dev, ide_pci_device_t *d) |
| 338 | { |
| 339 | u16 pcicmd = 0; |
| 340 | /* |
| 341 | * PnP BIOS was *supposed* to have setup this device, but we |
| 342 | * can do it ourselves, so long as the BIOS has assigned an IRQ |
| 343 | * (or possibly the device is using a "legacy header" for IRQs). |
| 344 | * Maybe the user deliberately *disabled* the device, |
| 345 | * but we'll eventually ignore it again if no drives respond. |
| 346 | */ |
| 347 | if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO)) |
| 348 | { |
| 349 | printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name); |
| 350 | return -ENODEV; |
| 351 | } |
| 352 | if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) { |
| 353 | printk(KERN_ERR "%s: error accessing PCI regs\n", d->name); |
| 354 | return -EIO; |
| 355 | } |
| 356 | if (!(pcicmd & PCI_COMMAND_IO)) { |
| 357 | printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name); |
| 358 | return -ENXIO; |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * ide_pci_check_iomem - check a register is I/O |
| 365 | * @dev: pci device |
| 366 | * @d: ide_pci_device |
| 367 | * @bar: bar number |
| 368 | * |
| 369 | * Checks if a BAR is configured and points to MMIO space. If so |
| 370 | * print an error and return an error code. Otherwise return 0 |
| 371 | */ |
| 372 | |
| 373 | static int ide_pci_check_iomem(struct pci_dev *dev, ide_pci_device_t *d, int bar) |
| 374 | { |
| 375 | ulong flags = pci_resource_flags(dev, bar); |
| 376 | |
| 377 | /* Unconfigured ? */ |
| 378 | if (!flags || pci_resource_len(dev, bar) == 0) |
| 379 | return 0; |
| 380 | |
| 381 | /* I/O space */ |
| 382 | if(flags & PCI_BASE_ADDRESS_IO_MASK) |
| 383 | return 0; |
| 384 | |
| 385 | /* Bad */ |
| 386 | printk(KERN_ERR "%s: IO baseregs (BIOS) are reported " |
| 387 | "as MEM, report to " |
| 388 | "<andre@linux-ide.org>.\n", d->name); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * ide_hwif_configure - configure an IDE interface |
| 394 | * @dev: PCI device holding interface |
| 395 | * @d: IDE pci data |
| 396 | * @mate: Paired interface if any |
| 397 | * |
| 398 | * Perform the initial set up for the hardware interface structure. This |
| 399 | * is done per interface port rather than per PCI device. There may be |
| 400 | * more than one port per device. |
| 401 | * |
| 402 | * Returns the new hardware interface structure, or NULL on a failure |
| 403 | */ |
| 404 | |
| 405 | static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *mate, int port, int irq) |
| 406 | { |
| 407 | unsigned long ctl = 0, base = 0; |
| 408 | ide_hwif_t *hwif; |
| 409 | |
Bartlomiej Zolnierkiewicz | a5d8c5c | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 410 | if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | /* Possibly we should fail if these checks report true */ |
| 412 | ide_pci_check_iomem(dev, d, 2*port); |
| 413 | ide_pci_check_iomem(dev, d, 2*port+1); |
| 414 | |
| 415 | ctl = pci_resource_start(dev, 2*port+1); |
| 416 | base = pci_resource_start(dev, 2*port); |
| 417 | if ((ctl && !base) || (base && !ctl)) { |
| 418 | printk(KERN_ERR "%s: inconsistent baseregs (BIOS) " |
| 419 | "for port %d, skipping\n", d->name, port); |
| 420 | return NULL; |
| 421 | } |
| 422 | } |
| 423 | if (!ctl) |
| 424 | { |
| 425 | /* Use default values */ |
| 426 | ctl = port ? 0x374 : 0x3f4; |
| 427 | base = port ? 0x170 : 0x1f0; |
| 428 | } |
| 429 | if ((hwif = ide_match_hwif(base, d->bootable, d->name)) == NULL) |
| 430 | return NULL; /* no room in ide_hwifs[] */ |
| 431 | if (hwif->io_ports[IDE_DATA_OFFSET] != base || |
| 432 | hwif->io_ports[IDE_CONTROL_OFFSET] != (ctl | 2)) { |
| 433 | memset(&hwif->hw, 0, sizeof(hwif->hw)); |
| 434 | #ifndef IDE_ARCH_OBSOLETE_INIT |
| 435 | ide_std_init_ports(&hwif->hw, base, (ctl | 2)); |
| 436 | hwif->hw.io_ports[IDE_IRQ_OFFSET] = 0; |
| 437 | #else |
| 438 | ide_init_hwif_ports(&hwif->hw, base, (ctl | 2), NULL); |
| 439 | #endif |
| 440 | memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports)); |
| 441 | hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET]; |
| 442 | } |
| 443 | hwif->chipset = ide_pci; |
| 444 | hwif->pci_dev = dev; |
| 445 | hwif->cds = (struct ide_pci_device_s *) d; |
| 446 | hwif->channel = port; |
| 447 | |
| 448 | if (!hwif->irq) |
| 449 | hwif->irq = irq; |
| 450 | if (mate) { |
| 451 | hwif->mate = mate; |
| 452 | mate->mate = hwif; |
| 453 | } |
| 454 | return hwif; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * ide_hwif_setup_dma - configure DMA interface |
| 459 | * @dev: PCI device |
| 460 | * @d: IDE pci data |
| 461 | * @hwif: Hardware interface we are configuring |
| 462 | * |
| 463 | * Set up the DMA base for the interface. Enable the master bits as |
| 464 | * necessary and attempt to bring the device DMA into a ready to use |
| 465 | * state |
| 466 | */ |
| 467 | |
| 468 | #ifndef CONFIG_BLK_DEV_IDEDMA_PCI |
| 469 | static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif) |
| 470 | { |
| 471 | } |
| 472 | #else |
| 473 | static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif) |
| 474 | { |
| 475 | u16 pcicmd; |
| 476 | pci_read_config_word(dev, PCI_COMMAND, &pcicmd); |
| 477 | |
| 478 | if ((d->autodma == AUTODMA) || |
| 479 | ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && |
| 480 | (dev->class & 0x80))) { |
| 481 | unsigned long dma_base = ide_get_or_set_dma_base(hwif); |
| 482 | if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) { |
| 483 | /* |
| 484 | * Set up BM-DMA capability |
| 485 | * (PnP BIOS should have done this) |
| 486 | */ |
| 487 | /* default DMA off if we had to configure it here */ |
| 488 | hwif->autodma = 0; |
| 489 | pci_set_master(dev); |
| 490 | if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) { |
| 491 | printk(KERN_ERR "%s: %s error updating PCICMD\n", |
| 492 | hwif->name, d->name); |
| 493 | dma_base = 0; |
| 494 | } |
| 495 | } |
| 496 | if (dma_base) { |
| 497 | if (d->init_dma) { |
| 498 | d->init_dma(hwif, dma_base); |
| 499 | } else { |
| 500 | ide_setup_dma(hwif, dma_base, 8); |
| 501 | } |
| 502 | } else { |
| 503 | printk(KERN_INFO "%s: %s Bus-Master DMA disabled " |
| 504 | "(BIOS)\n", hwif->name, d->name); |
| 505 | } |
| 506 | } |
| 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | #endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/ |
| 509 | |
| 510 | /** |
| 511 | * ide_setup_pci_controller - set up IDE PCI |
| 512 | * @dev: PCI device |
| 513 | * @d: IDE PCI data |
| 514 | * @noisy: verbose flag |
| 515 | * @config: returned as 1 if we configured the hardware |
| 516 | * |
| 517 | * Set up the PCI and controller side of the IDE interface. This brings |
| 518 | * up the PCI side of the device, checks that the device is enabled |
| 519 | * and enables it if need be |
| 520 | */ |
| 521 | |
| 522 | static int ide_setup_pci_controller(struct pci_dev *dev, ide_pci_device_t *d, int noisy, int *config) |
| 523 | { |
| 524 | int ret; |
| 525 | u32 class_rev; |
| 526 | u16 pcicmd; |
| 527 | |
| 528 | if (noisy) |
| 529 | ide_setup_pci_noise(dev, d); |
| 530 | |
| 531 | ret = ide_pci_enable(dev, d); |
| 532 | if (ret < 0) |
| 533 | goto out; |
| 534 | |
| 535 | ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd); |
| 536 | if (ret < 0) { |
| 537 | printk(KERN_ERR "%s: error accessing PCI regs\n", d->name); |
| 538 | goto out; |
| 539 | } |
| 540 | if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */ |
| 541 | ret = ide_pci_configure(dev, d); |
| 542 | if (ret < 0) |
| 543 | goto out; |
| 544 | *config = 1; |
| 545 | printk(KERN_INFO "%s: device enabled (Linux)\n", d->name); |
| 546 | } |
| 547 | |
| 548 | pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev); |
| 549 | class_rev &= 0xff; |
| 550 | if (noisy) |
| 551 | printk(KERN_INFO "%s: chipset revision %d\n", d->name, class_rev); |
| 552 | out: |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * ide_pci_setup_ports - configure ports/devices on PCI IDE |
| 558 | * @dev: PCI device |
| 559 | * @d: IDE pci device info |
| 560 | * @pciirq: IRQ line |
| 561 | * @index: ata index to update |
| 562 | * |
| 563 | * Scan the interfaces attached to this device and do any |
| 564 | * necessary per port setup. Attach the devices and ask the |
| 565 | * generic DMA layer to do its work for us. |
| 566 | * |
| 567 | * Normally called automaticall from do_ide_pci_setup_device, |
| 568 | * but is also used directly as a helper function by some controllers |
| 569 | * where the chipset setup is not the default PCI IDE one. |
| 570 | */ |
| 571 | |
| 572 | void ide_pci_setup_ports(struct pci_dev *dev, ide_pci_device_t *d, int pciirq, ata_index_t *index) |
| 573 | { |
Bartlomiej Zolnierkiewicz | a5d8c5c | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 574 | int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | int at_least_one_hwif_enabled = 0; |
| 576 | ide_hwif_t *hwif, *mate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | u8 tmp; |
| 578 | |
| 579 | index->all = 0xf0f0; |
| 580 | |
| 581 | /* |
| 582 | * Set up the IDE ports |
| 583 | */ |
| 584 | |
Bartlomiej Zolnierkiewicz | a5d8c5c | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 585 | for (port = 0; port < channels; ++port) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | ide_pci_enablebit_t *e = &(d->enablebits[port]); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) || |
| 589 | (tmp & e->mask) != e->val)) |
| 590 | continue; /* port not enabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL) |
| 593 | continue; |
| 594 | |
| 595 | /* setup proper ancestral information */ |
| 596 | hwif->gendev.parent = &dev->dev; |
| 597 | |
| 598 | if (hwif->channel) { |
| 599 | index->b.high = hwif->index; |
| 600 | } else { |
| 601 | index->b.low = hwif->index; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | if (d->init_iops) |
| 606 | d->init_iops(hwif); |
| 607 | |
| 608 | if (d->autodma == NODMA) |
| 609 | goto bypass_legacy_dma; |
| 610 | |
| 611 | if(d->init_setup_dma) |
| 612 | d->init_setup_dma(dev, d, hwif); |
| 613 | else |
| 614 | ide_hwif_setup_dma(dev, d, hwif); |
| 615 | bypass_legacy_dma: |
Bartlomiej Zolnierkiewicz | 6a824c9 | 2007-07-20 01:11:58 +0200 | [diff] [blame] | 616 | hwif->host_flags = d->host_flags; |
Bartlomiej Zolnierkiewicz | 4099d14 | 2007-07-20 01:11:59 +0200 | [diff] [blame^] | 617 | hwif->pio_mask = d->pio_mask; |
Bartlomiej Zolnierkiewicz | 6a824c9 | 2007-07-20 01:11:58 +0200 | [diff] [blame] | 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (d->init_hwif) |
| 620 | /* Call chipset-specific routine |
| 621 | * for each enabled hwif |
| 622 | */ |
| 623 | d->init_hwif(hwif); |
| 624 | |
| 625 | mate = hwif; |
| 626 | at_least_one_hwif_enabled = 1; |
| 627 | } |
| 628 | if (!at_least_one_hwif_enabled) |
| 629 | printk(KERN_INFO "%s: neither IDE port enabled (BIOS)\n", d->name); |
| 630 | } |
| 631 | |
| 632 | EXPORT_SYMBOL_GPL(ide_pci_setup_ports); |
| 633 | |
| 634 | /* |
| 635 | * ide_setup_pci_device() looks at the primary/secondary interfaces |
| 636 | * on a PCI IDE device and, if they are enabled, prepares the IDE driver |
| 637 | * for use with them. This generic code works for most PCI chipsets. |
| 638 | * |
| 639 | * One thing that is not standardized is the location of the |
| 640 | * primary/secondary interface "enable/disable" bits. For chipsets that |
| 641 | * we "know" about, this information is in the ide_pci_device_t struct; |
| 642 | * for all other chipsets, we just assume both interfaces are enabled. |
| 643 | */ |
| 644 | static int do_ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d, |
| 645 | ata_index_t *index, u8 noisy) |
| 646 | { |
| 647 | static ata_index_t ata_index = { .b = { .low = 0xff, .high = 0xff } }; |
| 648 | int tried_config = 0; |
| 649 | int pciirq, ret; |
| 650 | |
| 651 | ret = ide_setup_pci_controller(dev, d, noisy, &tried_config); |
| 652 | if (ret < 0) |
| 653 | goto out; |
| 654 | |
| 655 | /* |
| 656 | * Can we trust the reported IRQ? |
| 657 | */ |
| 658 | pciirq = dev->irq; |
| 659 | |
| 660 | /* Is it an "IDE storage" device in non-PCI mode? */ |
| 661 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) { |
| 662 | if (noisy) |
| 663 | printk(KERN_INFO "%s: not 100%% native mode: " |
| 664 | "will probe irqs later\n", d->name); |
| 665 | /* |
| 666 | * This allows offboard ide-pci cards the enable a BIOS, |
| 667 | * verify interrupt settings of split-mirror pci-config |
| 668 | * space, place chipset into init-mode, and/or preserve |
| 669 | * an interrupt if the card is not native ide support. |
| 670 | */ |
| 671 | ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0; |
| 672 | if (ret < 0) |
| 673 | goto out; |
| 674 | pciirq = ret; |
| 675 | } else if (tried_config) { |
| 676 | if (noisy) |
| 677 | printk(KERN_INFO "%s: will probe irqs later\n", d->name); |
| 678 | pciirq = 0; |
| 679 | } else if (!pciirq) { |
| 680 | if (noisy) |
| 681 | printk(KERN_WARNING "%s: bad irq (%d): will probe later\n", |
| 682 | d->name, pciirq); |
| 683 | pciirq = 0; |
| 684 | } else { |
| 685 | if (d->init_chipset) { |
| 686 | ret = d->init_chipset(dev, d->name); |
| 687 | if (ret < 0) |
| 688 | goto out; |
| 689 | } |
| 690 | if (noisy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | printk(KERN_INFO "%s: 100%% native mode on irq %d\n", |
| 692 | d->name, pciirq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* FIXME: silent failure can happen */ |
| 696 | |
| 697 | *index = ata_index; |
| 698 | ide_pci_setup_ports(dev, d, pciirq, index); |
| 699 | out: |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | int ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d) |
| 704 | { |
Bartlomiej Zolnierkiewicz | 5cbf79c | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 705 | ide_hwif_t *hwif = NULL, *mate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | ata_index_t index_list; |
| 707 | int ret; |
| 708 | |
| 709 | ret = do_ide_setup_pci_device(dev, d, &index_list, 1); |
| 710 | if (ret < 0) |
| 711 | goto out; |
| 712 | |
| 713 | if ((index_list.b.low & 0xf0) != 0xf0) |
Bartlomiej Zolnierkiewicz | 5cbf79c | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 714 | hwif = &ide_hwifs[index_list.b.low]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if ((index_list.b.high & 0xf0) != 0xf0) |
Bartlomiej Zolnierkiewicz | 5cbf79c | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 716 | mate = &ide_hwifs[index_list.b.high]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Bartlomiej Zolnierkiewicz | 5cbf79c | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 718 | if (hwif) |
| 719 | probe_hwif_init_with_fixup(hwif, d->fixup); |
| 720 | if (mate) |
| 721 | probe_hwif_init_with_fixup(mate, d->fixup); |
| 722 | |
| 723 | if (hwif) |
| 724 | ide_proc_register_port(hwif); |
| 725 | if (mate) |
| 726 | ide_proc_register_port(mate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | out: |
| 728 | return ret; |
| 729 | } |
| 730 | |
| 731 | EXPORT_SYMBOL_GPL(ide_setup_pci_device); |
| 732 | |
| 733 | int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2, |
| 734 | ide_pci_device_t *d) |
| 735 | { |
| 736 | struct pci_dev *pdev[] = { dev1, dev2 }; |
| 737 | ata_index_t index_list[2]; |
| 738 | int ret, i; |
| 739 | |
| 740 | for (i = 0; i < 2; i++) { |
| 741 | ret = do_ide_setup_pci_device(pdev[i], d, index_list + i, !i); |
| 742 | /* |
| 743 | * FIXME: Mom, mom, they stole me the helper function to undo |
| 744 | * do_ide_setup_pci_device() on the first device! |
| 745 | */ |
| 746 | if (ret < 0) |
| 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | for (i = 0; i < 2; i++) { |
| 751 | u8 idx[2] = { index_list[i].b.low, index_list[i].b.high }; |
| 752 | int j; |
| 753 | |
| 754 | for (j = 0; j < 2; j++) { |
| 755 | if ((idx[j] & 0xf0) != 0xf0) |
| 756 | probe_hwif_init(ide_hwifs + idx[j]); |
| 757 | } |
| 758 | } |
| 759 | |
Bartlomiej Zolnierkiewicz | 5cbf79c | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 760 | for (i = 0; i < 2; i++) { |
| 761 | u8 idx[2] = { index_list[i].b.low, index_list[i].b.high }; |
| 762 | int j; |
| 763 | |
| 764 | for (j = 0; j < 2; j++) { |
| 765 | if ((idx[j] & 0xf0) != 0xf0) |
| 766 | ide_proc_register_port(ide_hwifs + idx[j]); |
| 767 | } |
| 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | out: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | EXPORT_SYMBOL_GPL(ide_setup_pci_devices); |
| 774 | |
Bartlomiej Zolnierkiewicz | 6d208b3 | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 775 | #ifdef CONFIG_IDEPCI_PCIBUS_ORDER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /* |
| 777 | * Module interfaces |
| 778 | */ |
| 779 | |
| 780 | static int pre_init = 1; /* Before first ordered IDE scan */ |
| 781 | static LIST_HEAD(ide_pci_drivers); |
| 782 | |
| 783 | /* |
Ralf Baechle | c37ea21 | 2005-11-18 23:11:24 +0100 | [diff] [blame] | 784 | * __ide_pci_register_driver - attach IDE driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | * @driver: pci driver |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 786 | * @module: owner module of the driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | * |
| 788 | * Registers a driver with the IDE layer. The IDE layer arranges that |
| 789 | * boot time setup is done in the expected device order and then |
| 790 | * hands the controllers off to the core PCI code to do the rest of |
| 791 | * the work. |
| 792 | * |
| 793 | * The driver_data of the driver table must point to an ide_pci_device_t |
| 794 | * describing the interface. |
| 795 | * |
| 796 | * Returns are the same as for pci_register_driver |
| 797 | */ |
| 798 | |
Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 799 | int __ide_pci_register_driver(struct pci_driver *driver, struct module *module, |
| 800 | const char *mod_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
| 802 | if(!pre_init) |
Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 803 | return __pci_register_driver(driver, module, mod_name); |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 804 | driver->driver.owner = module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | list_add_tail(&driver->node, &ide_pci_drivers); |
| 806 | return 0; |
| 807 | } |
| 808 | |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 809 | EXPORT_SYMBOL_GPL(__ide_pci_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | * ide_scan_pcidev - find an IDE driver for a device |
| 813 | * @dev: PCI device to check |
| 814 | * |
| 815 | * Look for an IDE driver to handle the device we are considering. |
| 816 | * This is only used during boot up to get the ordering correct. After |
| 817 | * boot up the pci layer takes over the job. |
| 818 | */ |
| 819 | |
| 820 | static int __init ide_scan_pcidev(struct pci_dev *dev) |
| 821 | { |
| 822 | struct list_head *l; |
| 823 | struct pci_driver *d; |
| 824 | |
| 825 | list_for_each(l, &ide_pci_drivers) |
| 826 | { |
| 827 | d = list_entry(l, struct pci_driver, node); |
| 828 | if(d->id_table) |
| 829 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 830 | const struct pci_device_id *id = pci_match_id(d->id_table, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if(id != NULL) |
| 832 | { |
| 833 | if(d->probe(dev, id) >= 0) |
| 834 | { |
| 835 | dev->driver = d; |
| 836 | return 1; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * ide_scan_pcibus - perform the initial IDE driver scan |
| 846 | * @scan_direction: set for reverse order scanning |
| 847 | * |
| 848 | * Perform the initial bus rather than driver ordered scan of the |
| 849 | * PCI drivers. After this all IDE pci handling becomes standard |
| 850 | * module ordering not traditionally ordered. |
| 851 | */ |
| 852 | |
| 853 | void __init ide_scan_pcibus (int scan_direction) |
| 854 | { |
| 855 | struct pci_dev *dev = NULL; |
| 856 | struct pci_driver *d; |
| 857 | struct list_head *l, *n; |
| 858 | |
| 859 | pre_init = 0; |
| 860 | if (!scan_direction) { |
Alan Cox | 6451956 | 2006-12-10 02:18:53 -0800 | [diff] [blame] | 861 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | ide_scan_pcidev(dev); |
| 863 | } |
| 864 | } else { |
Alan Cox | 6451956 | 2006-12-10 02:18:53 -0800 | [diff] [blame] | 865 | while ((dev = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | ide_scan_pcidev(dev); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Hand the drivers over to the PCI layer now we |
| 872 | * are post init. |
| 873 | */ |
| 874 | |
Andrew Morton | d61bcce | 2007-07-03 22:28:36 +0200 | [diff] [blame] | 875 | list_for_each_safe(l, n, &ide_pci_drivers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | list_del(l); |
| 877 | d = list_entry(l, struct pci_driver, node); |
Andrew Morton | d61bcce | 2007-07-03 22:28:36 +0200 | [diff] [blame] | 878 | if (__pci_register_driver(d, d->driver.owner, |
| 879 | d->driver.mod_name)) { |
| 880 | printk(KERN_ERR "%s: failed to register driver " |
| 881 | "for %s\n", __FUNCTION__, |
| 882 | d->driver.mod_name); |
| 883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
Bartlomiej Zolnierkiewicz | 6d208b3 | 2007-05-10 00:01:11 +0200 | [diff] [blame] | 886 | #endif |