1 /* $NetBSD: pciide_common.c,v 1.38 2008/03/18 20:46:37 cube Exp $ */ 2 3 4 /* 5 * Copyright (c) 1999, 2000, 2001, 2003 Manuel Bouyer. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Manuel Bouyer. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 36 /* 37 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by Christopher G. Demetriou 50 * for the NetBSD Project. 51 * 4. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* 67 * PCI IDE controller driver. 68 * 69 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD 70 * sys/dev/pci/ppb.c, revision 1.16). 71 * 72 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and 73 * "Programming Interface for Bus Master IDE Controller, Revision 1.0 74 * 5/16/94" from the PCI SIG. 75 * 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: pciide_common.c,v 1.38 2008/03/18 20:46:37 cube Exp $"); 80 81 #include <sys/param.h> 82 #include <sys/malloc.h> 83 84 #include <uvm/uvm_extern.h> 85 86 #include <dev/pci/pcireg.h> 87 #include <dev/pci/pcivar.h> 88 #include <dev/pci/pcidevs.h> 89 #include <dev/pci/pciidereg.h> 90 #include <dev/pci/pciidevar.h> 91 92 #include <dev/ic/wdcreg.h> 93 94 #ifdef ATADEBUG 95 int atadebug_pciide_mask = 0; 96 #endif 97 98 #if NATA_DMA 99 static const char dmaerrfmt[] = 100 "%s:%d: unable to %s table DMA map for drive %d, error=%d\n"; 101 #endif 102 103 /* Default product description for devices not known from this controller */ 104 const struct pciide_product_desc default_product_desc = { 105 0, 106 0, 107 "Generic PCI IDE controller", 108 default_chip_map, 109 }; 110 111 const struct pciide_product_desc * 112 pciide_lookup_product(id, pp) 113 pcireg_t id; 114 const struct pciide_product_desc *pp; 115 { 116 for (; pp->chip_map != NULL; pp++) 117 if (PCI_PRODUCT(id) == pp->ide_product) 118 break; 119 120 if (pp->chip_map == NULL) 121 return NULL; 122 return pp; 123 } 124 125 void 126 pciide_common_attach(sc, pa, pp) 127 struct pciide_softc *sc; 128 struct pci_attach_args *pa; 129 const struct pciide_product_desc *pp; 130 { 131 pci_chipset_tag_t pc = pa->pa_pc; 132 pcitag_t tag = pa->pa_tag; 133 #if NATA_DMA 134 pcireg_t csr; 135 #endif 136 char devinfo[256]; 137 const char *displaydev; 138 139 aprint_naive(": disk controller\n"); 140 aprint_normal("\n"); 141 142 sc->sc_pci_id = pa->pa_id; 143 if (pp == NULL) { 144 /* should only happen for generic pciide devices */ 145 sc->sc_pp = &default_product_desc; 146 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 147 displaydev = devinfo; 148 } else { 149 sc->sc_pp = pp; 150 displaydev = sc->sc_pp->ide_name; 151 } 152 153 /* if displaydev == NULL, printf is done in chip-specific map */ 154 if (displaydev) 155 aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev, 156 "%s (rev. 0x%02x)\n", displaydev, 157 PCI_REVISION(pa->pa_class)); 158 159 sc->sc_pc = pa->pa_pc; 160 sc->sc_tag = pa->pa_tag; 161 162 #if NATA_DMA 163 /* Set up DMA defaults; these might be adjusted by chip_map. */ 164 sc->sc_dma_maxsegsz = IDEDMA_BYTE_COUNT_MAX; 165 sc->sc_dma_boundary = IDEDMA_BYTE_COUNT_ALIGN; 166 #endif 167 168 #ifdef ATADEBUG 169 if (atadebug_pciide_mask & DEBUG_PROBE) 170 pci_conf_print(sc->sc_pc, sc->sc_tag, NULL); 171 #endif 172 sc->sc_pp->chip_map(sc, pa); 173 174 #if NATA_DMA 175 if (sc->sc_dma_ok) { 176 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 177 csr |= PCI_COMMAND_MASTER_ENABLE; 178 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr); 179 } 180 #endif 181 ATADEBUG_PRINT(("pciide: command/status register=%x\n", 182 pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE); 183 } 184 185 /* tell whether the chip is enabled or not */ 186 int 187 pciide_chipen(sc, pa) 188 struct pciide_softc *sc; 189 struct pci_attach_args *pa; 190 { 191 pcireg_t csr; 192 193 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) { 194 csr = pci_conf_read(sc->sc_pc, sc->sc_tag, 195 PCI_COMMAND_STATUS_REG); 196 aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev, 197 "device disabled (at %s)\n", 198 (csr & PCI_COMMAND_IO_ENABLE) == 0 ? 199 "device" : "bridge"); 200 return 0; 201 } 202 return 1; 203 } 204 205 void 206 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep) 207 struct pci_attach_args *pa; 208 struct pciide_channel *cp; 209 int compatchan; 210 bus_size_t *cmdsizep, *ctlsizep; 211 { 212 struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel); 213 struct ata_channel *wdc_cp = &cp->ata_channel; 214 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp); 215 int i; 216 217 cp->compat = 1; 218 *cmdsizep = PCIIDE_COMPAT_CMD_SIZE; 219 *ctlsizep = PCIIDE_COMPAT_CTL_SIZE; 220 221 wdr->cmd_iot = pa->pa_iot; 222 if (bus_space_map(wdr->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan), 223 PCIIDE_COMPAT_CMD_SIZE, 0, &wdr->cmd_baseioh) != 0) { 224 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 225 "couldn't map %s channel cmd regs\n", cp->name); 226 goto bad; 227 } 228 229 wdr->ctl_iot = pa->pa_iot; 230 if (bus_space_map(wdr->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan), 231 PCIIDE_COMPAT_CTL_SIZE, 0, &wdr->ctl_ioh) != 0) { 232 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 233 "couldn't map %s channel ctl regs\n", cp->name); 234 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 235 PCIIDE_COMPAT_CMD_SIZE); 236 goto bad; 237 } 238 239 for (i = 0; i < WDC_NREG; i++) { 240 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i, 241 i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) { 242 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 243 "couldn't subregion %s channel cmd regs\n", 244 cp->name); 245 goto bad; 246 } 247 } 248 wdc_init_shadow_regs(wdc_cp); 249 wdr->data32iot = wdr->cmd_iot; 250 wdr->data32ioh = wdr->cmd_iohs[0]; 251 return; 252 253 bad: 254 cp->ata_channel.ch_flags |= ATACH_DISABLED; 255 return; 256 } 257 258 void 259 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr) 260 struct pci_attach_args * pa; 261 struct pciide_channel *cp; 262 bus_size_t *cmdsizep, *ctlsizep; 263 int (*pci_intr)(void *); 264 { 265 struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel); 266 struct ata_channel *wdc_cp = &cp->ata_channel; 267 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp); 268 const char *intrstr; 269 pci_intr_handle_t intrhandle; 270 int i; 271 272 cp->compat = 0; 273 274 if (sc->sc_pci_ih == NULL) { 275 if (pci_intr_map(pa, &intrhandle) != 0) { 276 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 277 "couldn't map native-PCI interrupt\n"); 278 goto bad; 279 } 280 intrstr = pci_intr_string(pa->pa_pc, intrhandle); 281 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, 282 intrhandle, IPL_BIO, pci_intr, sc); 283 if (sc->sc_pci_ih != NULL) { 284 aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev, 285 "using %s for native-PCI interrupt\n", 286 intrstr ? intrstr : "unknown interrupt"); 287 } else { 288 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 289 "couldn't establish native-PCI interrupt"); 290 if (intrstr != NULL) 291 aprint_error(" at %s", intrstr); 292 aprint_error("\n"); 293 goto bad; 294 } 295 } 296 cp->ih = sc->sc_pci_ih; 297 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->ch_channel), 298 PCI_MAPREG_TYPE_IO, 0, 299 &wdr->cmd_iot, &wdr->cmd_baseioh, NULL, cmdsizep) != 0) { 300 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 301 "couldn't map %s channel cmd regs\n", cp->name); 302 goto bad; 303 } 304 305 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->ch_channel), 306 PCI_MAPREG_TYPE_IO, 0, 307 &wdr->ctl_iot, &cp->ctl_baseioh, NULL, ctlsizep) != 0) { 308 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 309 "couldn't map %s channel ctl regs\n", cp->name); 310 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 311 *cmdsizep); 312 goto bad; 313 } 314 /* 315 * In native mode, 4 bytes of I/O space are mapped for the control 316 * register, the control register is at offset 2. Pass the generic 317 * code a handle for only one byte at the right offset. 318 */ 319 if (bus_space_subregion(wdr->ctl_iot, cp->ctl_baseioh, 2, 1, 320 &wdr->ctl_ioh) != 0) { 321 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 322 "unable to subregion %s channel ctl regs\n", cp->name); 323 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 324 *cmdsizep); 325 bus_space_unmap(wdr->cmd_iot, cp->ctl_baseioh, *ctlsizep); 326 goto bad; 327 } 328 329 for (i = 0; i < WDC_NREG; i++) { 330 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i, 331 i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) { 332 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 333 "couldn't subregion %s channel cmd regs\n", 334 cp->name); 335 goto bad; 336 } 337 } 338 wdc_init_shadow_regs(wdc_cp); 339 wdr->data32iot = wdr->cmd_iot; 340 wdr->data32ioh = wdr->cmd_iohs[0]; 341 return; 342 343 bad: 344 cp->ata_channel.ch_flags |= ATACH_DISABLED; 345 return; 346 } 347 348 #if NATA_DMA 349 void 350 pciide_mapreg_dma(sc, pa) 351 struct pciide_softc *sc; 352 struct pci_attach_args *pa; 353 { 354 pcireg_t maptype; 355 bus_addr_t addr; 356 struct pciide_channel *pc; 357 int reg, chan; 358 bus_size_t size; 359 360 /* 361 * Map DMA registers 362 * 363 * Note that sc_dma_ok is the right variable to test to see if 364 * DMA can be done. If the interface doesn't support DMA, 365 * sc_dma_ok will never be non-zero. If the DMA regs couldn't 366 * be mapped, it'll be zero. I.e., sc_dma_ok will only be 367 * non-zero if the interface supports DMA and the registers 368 * could be mapped. 369 * 370 * XXX Note that despite the fact that the Bus Master IDE specs 371 * XXX say that "The bus master IDE function uses 16 bytes of IO 372 * XXX space," some controllers (at least the United 373 * XXX Microelectronics UM8886BF) place it in memory space. 374 */ 375 maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 376 PCIIDE_REG_BUS_MASTER_DMA); 377 378 switch (maptype) { 379 case PCI_MAPREG_TYPE_IO: 380 sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 381 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 382 &addr, NULL, NULL) == 0); 383 if (sc->sc_dma_ok == 0) { 384 aprint_verbose( 385 ", but unused (couldn't query registers)"); 386 break; 387 } 388 if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE) 389 && addr >= 0x10000) { 390 sc->sc_dma_ok = 0; 391 aprint_verbose( 392 ", but unused (registers at unsafe address " 393 "%#lx)", (unsigned long)addr); 394 break; 395 } 396 /* FALLTHROUGH */ 397 398 case PCI_MAPREG_MEM_TYPE_32BIT: 399 sc->sc_dma_ok = (pci_mapreg_map(pa, 400 PCIIDE_REG_BUS_MASTER_DMA, maptype, 0, 401 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0); 402 sc->sc_dmat = pa->pa_dmat; 403 if (sc->sc_dma_ok == 0) { 404 aprint_verbose(", but unused (couldn't map registers)"); 405 } else { 406 sc->sc_wdcdev.dma_arg = sc; 407 sc->sc_wdcdev.dma_init = pciide_dma_init; 408 sc->sc_wdcdev.dma_start = pciide_dma_start; 409 sc->sc_wdcdev.dma_finish = pciide_dma_finish; 410 } 411 412 if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags & 413 PCIIDE_OPTIONS_NODMA) { 414 aprint_verbose( 415 ", but unused (forced off by config file)"); 416 sc->sc_dma_ok = 0; 417 } 418 break; 419 420 default: 421 sc->sc_dma_ok = 0; 422 aprint_verbose( 423 ", but unsupported register maptype (0x%x)", maptype); 424 } 425 426 if (sc->sc_dma_ok == 0) 427 return; 428 429 /* 430 * Set up the default handles for the DMA registers. 431 * Just reserve 32 bits for each handle, unless space 432 * doesn't permit it. 433 */ 434 for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) { 435 pc = &sc->pciide_channels[chan]; 436 for (reg = 0; reg < IDEDMA_NREGS; reg++) { 437 size = 4; 438 if (size > (IDEDMA_SCH_OFFSET - reg)) 439 size = IDEDMA_SCH_OFFSET - reg; 440 if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh, 441 IDEDMA_SCH_OFFSET * chan + reg, size, 442 &pc->dma_iohs[reg]) != 0) { 443 sc->sc_dma_ok = 0; 444 aprint_verbose(", but can't subregion offset %d " 445 "size %lu", reg, (u_long)size); 446 return; 447 } 448 } 449 } 450 } 451 #endif /* NATA_DMA */ 452 453 int 454 pciide_compat_intr(arg) 455 void *arg; 456 { 457 struct pciide_channel *cp = arg; 458 459 #ifdef DIAGNOSTIC 460 /* should only be called for a compat channel */ 461 if (cp->compat == 0) 462 panic("pciide compat intr called for non-compat chan %p", cp); 463 #endif 464 return (wdcintr(&cp->ata_channel)); 465 } 466 467 int 468 pciide_pci_intr(arg) 469 void *arg; 470 { 471 struct pciide_softc *sc = arg; 472 struct pciide_channel *cp; 473 struct ata_channel *wdc_cp; 474 int i, rv, crv; 475 476 rv = 0; 477 for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) { 478 cp = &sc->pciide_channels[i]; 479 wdc_cp = &cp->ata_channel; 480 481 /* If a compat channel skip. */ 482 if (cp->compat) 483 continue; 484 /* if this channel not waiting for intr, skip */ 485 if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0) 486 continue; 487 488 crv = wdcintr(wdc_cp); 489 if (crv == 0) 490 ; /* leave rv alone */ 491 else if (crv == 1) 492 rv = 1; /* claim the intr */ 493 else if (rv == 0) /* crv should be -1 in this case */ 494 rv = crv; /* if we've done no better, take it */ 495 } 496 return (rv); 497 } 498 499 #if NATA_DMA 500 void 501 pciide_channel_dma_setup(cp) 502 struct pciide_channel *cp; 503 { 504 int drive, s; 505 struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel); 506 struct ata_drive_datas *drvp; 507 508 KASSERT(cp->ata_channel.ch_ndrive != 0); 509 510 for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) { 511 drvp = &cp->ata_channel.ch_drive[drive]; 512 /* If no drive, skip */ 513 if ((drvp->drive_flags & DRIVE) == 0) 514 continue; 515 /* setup DMA if needed */ 516 if (((drvp->drive_flags & DRIVE_DMA) == 0 && 517 (drvp->drive_flags & DRIVE_UDMA) == 0) || 518 sc->sc_dma_ok == 0) { 519 s = splbio(); 520 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 521 splx(s); 522 continue; 523 } 524 if (pciide_dma_table_setup(sc, cp->ata_channel.ch_channel, 525 drive) != 0) { 526 /* Abort DMA setup */ 527 s = splbio(); 528 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 529 splx(s); 530 continue; 531 } 532 } 533 } 534 535 #define NIDEDMA_TABLES(sc) \ 536 (MAXPHYS/(min((sc)->sc_dma_maxsegsz, PAGE_SIZE)) + 1) 537 538 int 539 pciide_dma_table_setup(sc, channel, drive) 540 struct pciide_softc *sc; 541 int channel, drive; 542 { 543 bus_dma_segment_t seg; 544 int error, rseg; 545 const bus_size_t dma_table_size = 546 sizeof(struct idedma_table) * NIDEDMA_TABLES(sc); 547 struct pciide_dma_maps *dma_maps = 548 &sc->pciide_channels[channel].dma_maps[drive]; 549 550 /* If table was already allocated, just return */ 551 if (dma_maps->dma_table) 552 return 0; 553 554 /* Allocate memory for the DMA tables and map it */ 555 if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size, 556 IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg, 557 BUS_DMA_NOWAIT)) != 0) { 558 aprint_error(dmaerrfmt, 559 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 560 "allocate", drive, error); 561 return error; 562 } 563 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 564 dma_table_size, 565 (void **)&dma_maps->dma_table, 566 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 567 aprint_error(dmaerrfmt, 568 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 569 "map", drive, error); 570 return error; 571 } 572 ATADEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, " 573 "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size, 574 (unsigned long)seg.ds_addr), DEBUG_PROBE); 575 /* Create and load table DMA map for this disk */ 576 if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size, 577 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT, 578 &dma_maps->dmamap_table)) != 0) { 579 aprint_error(dmaerrfmt, 580 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 581 "create", drive, error); 582 return error; 583 } 584 if ((error = bus_dmamap_load(sc->sc_dmat, 585 dma_maps->dmamap_table, 586 dma_maps->dma_table, 587 dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) { 588 aprint_error(dmaerrfmt, 589 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 590 "load", drive, error); 591 return error; 592 } 593 ATADEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n", 594 (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr), 595 DEBUG_PROBE); 596 /* Create a xfer DMA map for this drive */ 597 if ((error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 598 NIDEDMA_TABLES(sc), sc->sc_dma_maxsegsz, sc->sc_dma_boundary, 599 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, 600 &dma_maps->dmamap_xfer)) != 0) { 601 aprint_error(dmaerrfmt, 602 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 603 "create xfer", drive, error); 604 return error; 605 } 606 return 0; 607 } 608 609 int 610 pciide_dma_dmamap_setup(sc, channel, drive, databuf, datalen, flags) 611 struct pciide_softc *sc; 612 int channel, drive; 613 void *databuf; 614 size_t datalen; 615 int flags; 616 { 617 int error, seg; 618 struct pciide_channel *cp = &sc->pciide_channels[channel]; 619 struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive]; 620 621 error = bus_dmamap_load(sc->sc_dmat, 622 dma_maps->dmamap_xfer, 623 databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING | 624 ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)); 625 if (error) { 626 aprint_error(dmaerrfmt, 627 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 628 "load xfer", drive, error); 629 return error; 630 } 631 632 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0, 633 dma_maps->dmamap_xfer->dm_mapsize, 634 (flags & WDC_DMA_READ) ? 635 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 636 637 for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) { 638 #ifdef DIAGNOSTIC 639 /* A segment must not cross a 64k boundary */ 640 { 641 u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr; 642 u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len; 643 if ((phys & ~IDEDMA_BYTE_COUNT_MASK) != 644 ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) { 645 printf("pciide_dma: segment %d physical addr 0x%lx" 646 " len 0x%lx not properly aligned\n", 647 seg, phys, len); 648 panic("pciide_dma: buf align"); 649 } 650 } 651 #endif 652 dma_maps->dma_table[seg].base_addr = 653 htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr); 654 dma_maps->dma_table[seg].byte_count = 655 htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len & 656 IDEDMA_BYTE_COUNT_MASK); 657 ATADEBUG_PRINT(("\t seg %d len %d addr 0x%x\n", 658 seg, le32toh(dma_maps->dma_table[seg].byte_count), 659 le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA); 660 661 } 662 dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |= 663 htole32(IDEDMA_BYTE_COUNT_EOT); 664 665 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0, 666 dma_maps->dmamap_table->dm_mapsize, 667 BUS_DMASYNC_PREWRITE); 668 669 #ifdef DIAGNOSTIC 670 if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) { 671 printf("pciide_dma_dmamap_setup: addr 0x%lx " 672 "not properly aligned\n", 673 (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr); 674 panic("pciide_dma_init: table align"); 675 } 676 #endif 677 /* remember flags */ 678 dma_maps->dma_flags = flags; 679 680 return 0; 681 } 682 683 int 684 pciide_dma_init(v, channel, drive, databuf, datalen, flags) 685 void *v; 686 int channel, drive; 687 void *databuf; 688 size_t datalen; 689 int flags; 690 { 691 struct pciide_softc *sc = v; 692 int error; 693 struct pciide_channel *cp = &sc->pciide_channels[channel]; 694 struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive]; 695 696 if ((error = pciide_dma_dmamap_setup(sc, channel, drive, 697 databuf, datalen, flags)) != 0) 698 return error; 699 /* Maps are ready. Start DMA function */ 700 /* Clear status bits */ 701 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 702 bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0)); 703 /* Write table addr */ 704 bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0, 705 dma_maps->dmamap_table->dm_segs[0].ds_addr); 706 /* set read/write */ 707 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0, 708 ((flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE : 0) | cp->idedma_cmd); 709 return 0; 710 } 711 712 void 713 pciide_dma_start(void *v, int channel, int drive) 714 { 715 struct pciide_softc *sc = v; 716 struct pciide_channel *cp = &sc->pciide_channels[channel]; 717 718 ATADEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS); 719 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0, 720 bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0) 721 | IDEDMA_CMD_START); 722 } 723 724 int 725 pciide_dma_finish(v, channel, drive, force) 726 void *v; 727 int channel, drive; 728 int force; 729 { 730 struct pciide_softc *sc = v; 731 u_int8_t status; 732 int error = 0; 733 struct pciide_channel *cp = &sc->pciide_channels[channel]; 734 struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive]; 735 736 status = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0); 737 ATADEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status), 738 DEBUG_XFERS); 739 740 if (force == WDC_DMAEND_END && (status & IDEDMA_CTL_INTR) == 0) 741 return WDC_DMAST_NOIRQ; 742 743 /* stop DMA channel */ 744 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0, 745 bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0) 746 & ~IDEDMA_CMD_START); 747 748 /* Unload the map of the data buffer */ 749 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0, 750 dma_maps->dmamap_xfer->dm_mapsize, 751 (dma_maps->dma_flags & WDC_DMA_READ) ? 752 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 753 bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer); 754 755 if ((status & IDEDMA_CTL_ERR) != 0 && force != WDC_DMAEND_ABRT_QUIET) { 756 aprint_error("%s:%d:%d: bus-master DMA error: status=0x%x\n", 757 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel, 758 drive, status); 759 error |= WDC_DMAST_ERR; 760 } 761 762 if ((status & IDEDMA_CTL_INTR) == 0 && force != WDC_DMAEND_ABRT_QUIET) { 763 aprint_error("%s:%d:%d: bus-master DMA error: missing " 764 "interrupt, status=0x%x\n", 765 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), 766 channel, drive, status); 767 error |= WDC_DMAST_NOIRQ; 768 } 769 770 if ((status & IDEDMA_CTL_ACT) != 0 && force != WDC_DMAEND_ABRT_QUIET) { 771 /* data underrun, may be a valid condition for ATAPI */ 772 error |= WDC_DMAST_UNDER; 773 } 774 return error; 775 } 776 777 void 778 pciide_irqack(chp) 779 struct ata_channel *chp; 780 { 781 struct pciide_channel *cp = CHAN_TO_PCHAN(chp); 782 struct pciide_softc *sc = CHAN_TO_PCIIDE(chp); 783 784 /* clear status bits in IDE DMA registers */ 785 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 786 bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0)); 787 } 788 #endif /* NATA_DMA */ 789 790 /* some common code used by several chip_map */ 791 int 792 pciide_chansetup(sc, channel, interface) 793 struct pciide_softc *sc; 794 int channel; 795 pcireg_t interface; 796 { 797 struct pciide_channel *cp = &sc->pciide_channels[channel]; 798 sc->wdc_chanarray[channel] = &cp->ata_channel; 799 cp->name = PCIIDE_CHANNEL_NAME(channel); 800 cp->ata_channel.ch_channel = channel; 801 cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac; 802 cp->ata_channel.ch_queue = 803 malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT); 804 if (cp->ata_channel.ch_queue == NULL) { 805 aprint_error("%s %s channel: " 806 "can't allocate memory for command queue", 807 device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cp->name); 808 return 0; 809 } 810 cp->ata_channel.ch_ndrive = 2; 811 aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 812 "%s channel %s to %s mode\n", cp->name, 813 (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ? 814 "configured" : "wired", 815 (interface & PCIIDE_INTERFACE_PCI(channel)) ? 816 "native-PCI" : "compatibility"); 817 return 1; 818 } 819 820 /* some common code used by several chip channel_map */ 821 void 822 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep, pci_intr) 823 struct pci_attach_args *pa; 824 struct pciide_channel *cp; 825 pcireg_t interface; 826 bus_size_t *cmdsizep, *ctlsizep; 827 int (*pci_intr)(void *); 828 { 829 struct ata_channel *wdc_cp = &cp->ata_channel; 830 831 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel)) 832 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr); 833 else { 834 pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel, cmdsizep, 835 ctlsizep); 836 if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0) 837 pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel); 838 } 839 wdcattach(wdc_cp); 840 } 841 842 /* 843 * generic code to map the compat intr. 844 */ 845 void 846 pciide_map_compat_intr(pa, cp, compatchan) 847 struct pci_attach_args *pa; 848 struct pciide_channel *cp; 849 int compatchan; 850 { 851 struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel); 852 853 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH 854 cp->ih = 855 pciide_machdep_compat_intr_establish(sc->sc_wdcdev.sc_atac.atac_dev, 856 pa, compatchan, pciide_compat_intr, cp); 857 if (cp->ih == NULL) { 858 #endif 859 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 860 "no compatibility interrupt for use by %s " 861 "channel\n", cp->name); 862 cp->ata_channel.ch_flags |= ATACH_DISABLED; 863 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH 864 } 865 #endif 866 } 867 868 void 869 default_chip_map(sc, pa) 870 struct pciide_softc *sc; 871 struct pci_attach_args *pa; 872 { 873 struct pciide_channel *cp; 874 pcireg_t interface = PCI_INTERFACE(pa->pa_class); 875 pcireg_t csr; 876 int channel; 877 #if NATA_DMA 878 int drive; 879 u_int8_t idedma_ctl; 880 #endif 881 bus_size_t cmdsize, ctlsize; 882 const char *failreason; 883 struct wdc_regs *wdr; 884 885 if (pciide_chipen(sc, pa) == 0) 886 return; 887 888 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) { 889 #if NATA_DMA 890 aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 891 "bus-master DMA support present"); 892 if (sc->sc_pp == &default_product_desc && 893 (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags & 894 PCIIDE_OPTIONS_DMA) == 0) { 895 aprint_verbose(", but unused (no driver support)"); 896 sc->sc_dma_ok = 0; 897 } else { 898 pciide_mapreg_dma(sc, pa); 899 if (sc->sc_dma_ok != 0) 900 aprint_verbose(", used without full driver " 901 "support"); 902 } 903 #else 904 aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 905 "bus-master DMA support present, but unused (no driver " 906 "support)"); 907 #endif /* NATA_DMA */ 908 } else { 909 aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 910 "hardware does not support DMA"); 911 #if NATA_DMA 912 sc->sc_dma_ok = 0; 913 #endif 914 } 915 aprint_verbose("\n"); 916 #if NATA_DMA 917 if (sc->sc_dma_ok) { 918 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA; 919 sc->sc_wdcdev.irqack = pciide_irqack; 920 } 921 #endif 922 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0; 923 #if NATA_DMA 924 sc->sc_wdcdev.sc_atac.atac_dma_cap = 0; 925 #endif 926 927 sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray; 928 sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS; 929 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16; 930 931 wdc_allocate_regs(&sc->sc_wdcdev); 932 933 for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; 934 channel++) { 935 cp = &sc->pciide_channels[channel]; 936 if (pciide_chansetup(sc, channel, interface) == 0) 937 continue; 938 wdr = CHAN_TO_WDC_REGS(&cp->ata_channel); 939 if (interface & PCIIDE_INTERFACE_PCI(channel)) 940 pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize, 941 pciide_pci_intr); 942 else 943 pciide_mapregs_compat(pa, cp, 944 cp->ata_channel.ch_channel, &cmdsize, &ctlsize); 945 if (cp->ata_channel.ch_flags & ATACH_DISABLED) 946 continue; 947 /* 948 * Check to see if something appears to be there. 949 */ 950 failreason = NULL; 951 /* 952 * In native mode, always enable the controller. It's 953 * not possible to have an ISA board using the same address 954 * anyway. 955 */ 956 if (interface & PCIIDE_INTERFACE_PCI(channel)) { 957 wdcattach(&cp->ata_channel); 958 continue; 959 } 960 if (!wdcprobe(&cp->ata_channel)) { 961 failreason = "not responding; disabled or no drives?"; 962 goto next; 963 } 964 /* 965 * Now, make sure it's actually attributable to this PCI IDE 966 * channel by trying to access the channel again while the 967 * PCI IDE controller's I/O space is disabled. (If the 968 * channel no longer appears to be there, it belongs to 969 * this controller.) YUCK! 970 */ 971 csr = pci_conf_read(sc->sc_pc, sc->sc_tag, 972 PCI_COMMAND_STATUS_REG); 973 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, 974 csr & ~PCI_COMMAND_IO_ENABLE); 975 if (wdcprobe(&cp->ata_channel)) 976 failreason = "other hardware responding at addresses"; 977 pci_conf_write(sc->sc_pc, sc->sc_tag, 978 PCI_COMMAND_STATUS_REG, csr); 979 next: 980 if (failreason) { 981 aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev, 982 "%s channel ignored (%s)\n", cp->name, failreason); 983 cp->ata_channel.ch_flags |= ATACH_DISABLED; 984 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 985 cmdsize); 986 bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh, ctlsize); 987 } else { 988 pciide_map_compat_intr(pa, cp, 989 cp->ata_channel.ch_channel); 990 wdcattach(&cp->ata_channel); 991 } 992 } 993 994 #if NATA_DMA 995 if (sc->sc_dma_ok == 0) 996 return; 997 998 /* Allocate DMA maps */ 999 for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; 1000 channel++) { 1001 idedma_ctl = 0; 1002 cp = &sc->pciide_channels[channel]; 1003 for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) { 1004 /* 1005 * we have not probed the drives yet, allocate 1006 * ressources for all of them. 1007 */ 1008 if (pciide_dma_table_setup(sc, channel, drive) != 0) { 1009 /* Abort DMA setup */ 1010 aprint_error( 1011 "%s:%d:%d: can't allocate DMA maps, " 1012 "using PIO transfers\n", 1013 device_xname( 1014 sc->sc_wdcdev.sc_atac.atac_dev), 1015 channel, drive); 1016 sc->sc_dma_ok = 0; 1017 sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA; 1018 sc->sc_wdcdev.irqack = NULL; 1019 break; 1020 } 1021 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 1022 } 1023 if (idedma_ctl != 0) { 1024 /* Add software bits in status register */ 1025 bus_space_write_1(sc->sc_dma_iot, 1026 cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl); 1027 } 1028 } 1029 #endif /* NATA_DMA */ 1030 } 1031 1032 void 1033 sata_setup_channel(chp) 1034 struct ata_channel *chp; 1035 { 1036 #if NATA_DMA 1037 struct ata_drive_datas *drvp; 1038 int drive; 1039 #if NATA_UDMA 1040 int s; 1041 #endif 1042 u_int32_t idedma_ctl; 1043 struct pciide_channel *cp = CHAN_TO_PCHAN(chp); 1044 struct pciide_softc *sc = CHAN_TO_PCIIDE(chp); 1045 1046 /* setup DMA if needed */ 1047 pciide_channel_dma_setup(cp); 1048 1049 idedma_ctl = 0; 1050 1051 for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) { 1052 drvp = &chp->ch_drive[drive]; 1053 /* If no drive, skip */ 1054 if ((drvp->drive_flags & DRIVE) == 0) 1055 continue; 1056 #if NATA_UDMA 1057 if (drvp->drive_flags & DRIVE_UDMA) { 1058 /* use Ultra/DMA */ 1059 s = splbio(); 1060 drvp->drive_flags &= ~DRIVE_DMA; 1061 splx(s); 1062 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 1063 } else 1064 #endif /* NATA_UDMA */ 1065 if (drvp->drive_flags & DRIVE_DMA) { 1066 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 1067 } 1068 } 1069 1070 /* 1071 * Nothing to do to setup modes; it is meaningless in S-ATA 1072 * (but many S-ATA drives still want to get the SET_FEATURE 1073 * command). 1074 */ 1075 if (idedma_ctl != 0) { 1076 /* Add software bits in status register */ 1077 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 1078 idedma_ctl); 1079 } 1080 #endif /* NATA_DMA */ 1081 } 1082