Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.6.2.2
      1 /*	$NetBSD: pciide.c,v 1.6.2.2 1998/06/05 10:09:14 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * PCI IDE controller driver.
     35  *
     36  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     37  * sys/dev/pci/ppb.c, revision 1.16).
     38  *
     39  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     40  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     41  * 5/16/94" from the PCI SIG.
     42  *
     43  * XXX Does not yet support DMA (but does map the Bus Master DMA regs).
     44  *
     45  * XXX Does not support serializing the two channels for broken (at least
     46  * XXX according to linux and freebsd) controllers, e.g. CMD PCI0640.
     47  */
     48 
     49 #define WDCDEBUG
     50 
     51 #define DEBUG_DMA   0x01
     52 #define DEBUG_XFERS  0x02
     53 #define DEBUG_FUNCS  0x08
     54 #define DEBUG_PROBE  0x10
     55 #ifdef WDCDEBUG
     56 int wdcdebug_pciide_mask = DEBUG_PROBE;
     57 #define WDCDEBUG_PRINT(args, level) \
     58 	if (wdcdebug_pciide_mask & (level)) printf args
     59 #else
     60 #define WDCDEBUG_PRINT(args, level)
     61 #endif
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/device.h>
     65 #include <sys/malloc.h>
     66 
     67 #include <vm/vm.h>
     68 #include <vm/vm_param.h>
     69 #include <vm/vm_kern.h>
     70 
     71 #include <dev/pci/pcireg.h>
     72 #include <dev/pci/pcivar.h>
     73 #include <dev/pci/pcidevs.h>
     74 #include <dev/pci/pciidereg.h>
     75 #include <dev/pci/pciidevar.h>
     76 #include <dev/pci/pciide_pIIx_reg.h>
     77 #include <dev/ata/atavar.h>
     78 #include <dev/ic/wdcreg.h>
     79 #include <dev/ic/wdcvar.h>
     80 
     81 struct pciide_softc {
     82 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     83 
     84 	void			*sc_pci_ih;	/* PCI interrupt handle */
     85 	int			sc_dma_ok;	/* bus-master DMA info */
     86 	bus_space_tag_t		sc_dma_iot;
     87 	bus_space_handle_t	sc_dma_ioh;
     88 	bus_dma_tag_t		sc_dmat;
     89 	/* Chip description */
     90 	const struct pciide_product_desc *sc_pp;
     91 	/* common definitions */
     92 	struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
     93 	/* internal bookkeeping */
     94 	struct pciide_channel {			/* per-channel data */
     95 		int		hw_ok;		/* hardware mapped & OK? */
     96 		int		compat;		/* is it compat? */
     97 		void		*ih;		/* compat or pci handle */
     98 		/* DMA tables and DMA map for xfer, for each drive */
     99 		struct pciide_dma_maps {
    100 			bus_dmamap_t    dmamap_table;
    101 			struct idedma_table *dma_table;
    102 			bus_dmamap_t    dmamap_xfer;
    103 		} dma_maps[2];
    104 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    105 };
    106 
    107 void default_setup_cap __P((struct pciide_softc*));
    108 void default_setup_chip __P((struct pciide_softc*,
    109 				pci_chipset_tag_t, pcitag_t));
    110 void piix_setup_cap __P((struct pciide_softc*));
    111 void piix_setup_chip __P((struct pciide_softc*,
    112 				pci_chipset_tag_t, pcitag_t));
    113 void piix3_4_setup_chip __P((struct pciide_softc*,
    114 				pci_chipset_tag_t, pcitag_t));
    115 
    116 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    117 static u_int32_t piix_setup_idetim_drvs __P((u_int8_t, u_int8_t, u_int8_t));
    118 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    119 
    120 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    121 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    122 void pciide_dma_start __P((void*, int, int, int));
    123 int  pciide_dma_finish __P((void*, int, int, int));
    124 
    125 struct pciide_product_desc {
    126     u_int32_t ide_product;
    127     int ide_flags;
    128     const char *ide_name;
    129     /* init controller's capabilities for drives probe */
    130     void (*setup_cap) __P((struct pciide_softc*));
    131     /* init controller after drives probe */
    132     void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
    133 };
    134 
    135 /* Flags for ide_flags */
    136 #define NO_PCI_INTR       0x01 /* don't try to map the native PCI intr */
    137 #define ONE_QUEUE         0x02 /* device need serialised access */
    138 
    139 /* Default product description for devices not known from this controller */
    140 const struct pciide_product_desc default_product_desc = {
    141     0,
    142     0,
    143     "Generic PCI IDE controller",
    144     default_setup_cap,
    145     default_setup_chip
    146 };
    147 
    148 
    149 const struct pciide_product_desc pciide_intel_products[] =  {
    150     { PCI_PRODUCT_INTEL_82092AA,
    151       0,
    152       "Intel 82092AA IDE controller",
    153       default_setup_cap,
    154       default_setup_chip
    155     },
    156     { PCI_PRODUCT_INTEL_82371FB_IDE,
    157       0,
    158       "Intel 82371FB IDE controller (PIIX)",
    159       piix_setup_cap,
    160       piix_setup_chip
    161     },
    162     { PCI_PRODUCT_INTEL_82371SB_IDE,
    163       0,
    164       "Intel 82371SB IDE Interface (PIIX3)",
    165       piix_setup_cap,
    166       piix3_4_setup_chip
    167     },
    168     { PCI_PRODUCT_INTEL_82371AB_IDE,
    169       0,
    170       "Intel 82371AB IDE controller (PIIX4)",
    171       piix_setup_cap,
    172       piix3_4_setup_chip
    173     },
    174     { 0,
    175       0,
    176       NULL,
    177     }
    178 };
    179 const struct pciide_product_desc pciide_cmd_products[] =  {
    180     { PCI_PRODUCT_CMDTECH_640,
    181       NO_PCI_INTR | ONE_QUEUE,
    182       "CMD Technology PCI0640",
    183       default_setup_cap,
    184       default_setup_chip
    185     },
    186     { 0,
    187       0,
    188       NULL,
    189     }
    190 };
    191 
    192 struct pciide_vendor_desc {
    193     u_int32_t ide_vendor;
    194     const struct pciide_product_desc *ide_products;
    195 };
    196 
    197 const struct pciide_vendor_desc pciide_vendors[] = {
    198     { PCI_VENDOR_INTEL, pciide_intel_products },
    199     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    200     { 0, NULL }
    201 };
    202 
    203 
    204 #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    205 
    206 #ifdef __BROKEN_INDIRECT_CONFIG
    207 int	pciide_match __P((struct device *, void *, void *));
    208 #else
    209 int	pciide_match __P((struct device *, struct cfdata *, void *));
    210 #endif
    211 void	pciide_attach __P((struct device *, struct device *, void *));
    212 
    213 struct cfattach pciide_ca = {
    214 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    215 };
    216 
    217 int	pciide_map_channel_compat __P((struct pciide_softc *,
    218 	    struct pci_attach_args *, int));
    219 const char *pciide_compat_channel_probe __P((struct pciide_softc *,
    220 	    struct pci_attach_args *, int));
    221 int	pciide_map_channel_native __P((struct pciide_softc *,
    222 	    struct pci_attach_args *, int));
    223 int	pciide_print __P((void *, const char *pnp));
    224 int	pciide_compat_intr __P((void *));
    225 int	pciide_pci_intr __P((void *));
    226 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    227 
    228 const struct pciide_product_desc*
    229 pciide_lookup_product(id)
    230     u_int32_t id;
    231 {
    232     const struct pciide_product_desc *pp;
    233     const struct pciide_vendor_desc *vp;
    234 
    235     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    236 	if (PCI_VENDOR(id) == vp->ide_vendor)
    237 	    break;
    238 
    239     if ((pp = vp->ide_products) == NULL)
    240 	return NULL;
    241 
    242     for (; pp->ide_name != NULL; pp++)
    243 	if (PCI_PRODUCT(id) == pp->ide_product)
    244 	    break;
    245 
    246     if (pp->ide_name == NULL)
    247 	return NULL;
    248     return pp;
    249 }
    250 
    251 int
    252 pciide_match(parent, match, aux)
    253 	struct device *parent;
    254 #ifdef __BROKEN_INDIRECT_CONFIG
    255 	void *match;
    256 #else
    257 	struct cfdata *match;
    258 #endif
    259 	void *aux;
    260 {
    261 	struct pci_attach_args *pa = aux;
    262 
    263 	/*
    264 	 * Check the ID register to see that it's a PCI IDE controller.
    265 	 * If it is, we assume that we can deal with it; it _should_
    266 	 * work in a standardized way...
    267 	 */
    268 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    269 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    270 		return (1);
    271 	}
    272 
    273 	return (0);
    274 }
    275 
    276 void
    277 pciide_attach(parent, self, aux)
    278 	struct device *parent, *self;
    279 	void *aux;
    280 {
    281 	struct pci_attach_args *pa = aux;
    282 	pci_chipset_tag_t pc = pa->pa_pc;
    283 	pcitag_t tag = pa->pa_tag;
    284 	struct pciide_softc *sc = (struct pciide_softc *)self;
    285 	struct pciide_channel *cp;
    286 	pcireg_t class, interface, csr;
    287 	pci_intr_handle_t intrhandle;
    288 	const char *intrstr;
    289 	char devinfo[256];
    290 	int i;
    291 
    292         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    293 	if (sc->sc_pp == NULL) {
    294 		sc->sc_pp = &default_product_desc;
    295 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    296 		printf(": %s (rev. 0x%02x)\n", devinfo,
    297 		    PCI_REVISION(pa->pa_class));
    298 	} else {
    299 		printf(": %s\n", sc->sc_pp->ide_name);
    300 	}
    301 
    302 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    303 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    304 		printf("%s: device disabled (at %s)\n",
    305 		    sc->sc_wdcdev.sc_dev.dv_xname,
    306 		    (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
    307 		return;
    308 	}
    309 
    310 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    311 	interface = PCI_INTERFACE(class);
    312 
    313 	/*
    314 	 * Set up PCI interrupt.
    315 	 *
    316 	 * If mapping fails, that's (probably) because there's no pin
    317 	 * set to intr, which is (probably) because it's a compat-only
    318 	 * device (or hard-wired in compatibility-only mode).  Native-PCI
    319 	 * channels will complain later if the interrupt was needed.
    320 	 *
    321 	 * If establishment fails, that's (probably) some other problem.
    322 	 */
    323 	if ((sc->sc_pp->ide_flags & NO_PCI_INTR) == 0) {
    324 	    if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    325 	        pa->pa_intrline, &intrhandle) == 0) {
    326 		    intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    327 		    sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
    328 		        IPL_BIO, pciide_pci_intr, sc);
    329 
    330 		    if (sc->sc_pci_ih != NULL) {
    331 			printf("%s: using %s for native-PCI interrupt\n",
    332 			    sc->sc_wdcdev.sc_dev.dv_xname,
    333 			    intrstr ? intrstr : "unknown interrupt");
    334 		    } else {
    335 			printf("%s: couldn't establish native-PCI interrupt",
    336 			    sc->sc_wdcdev.sc_dev.dv_xname);
    337 			if (intrstr != NULL)
    338 			    printf(" at %s", intrstr);
    339 			printf("\n");
    340 		    }
    341 	    }
    342 	}
    343 
    344 	/*
    345 	 * Map DMA registers, if DMA is supported.
    346 	 *
    347 	 * Note that sc_dma_ok is the right variable to test to see if
    348 	 * DMA can * be done.  If the interface doesn't support DMA,
    349 	 * sc_dma_ok * will never be non-zero.  If the DMA regs couldn't
    350 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    351 	 * non-zero if the interface supports DMA and the registers
    352 	 * could be mapped.
    353 	 *
    354 	 * XXX Note that despite the fact that the Bus Master IDE specs
    355 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    356 	 * XXX space," some controllers (at least the United
    357 	 * XXX Microelectronics UM8886BF) place it in memory space.
    358 	 * XXX eventually, we should probably read the register and check
    359 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    360 	 */
    361 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    362 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    363 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    364 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    365 		sc->sc_dmat = pa->pa_dmat;
    366 		printf("%s: bus-master DMA support present",
    367 		    sc->sc_wdcdev.sc_dev.dv_xname);
    368 		if (sc->sc_dma_ok == 0) {
    369 			printf(", but unused (couldn't map registers)");
    370 		} else if (sc->sc_pp == 0) {
    371 			printf(", but unused (no driver support)");
    372 		} else {
    373 			sc->sc_wdcdev.dma_arg = sc;
    374 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    375 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    376 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    377 		}
    378 		printf("\n");
    379 	}
    380 	if (sc->sc_pp == NULL)
    381 		default_setup_cap(sc);
    382 	else
    383 		sc->sc_pp->setup_cap(sc);
    384 	sc->sc_wdcdev.channels = sc->wdc_channels;
    385 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    386 
    387 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    388 		cp = &sc->pciide_channels[i];
    389 
    390 		sc->wdc_channels[i].channel = i;
    391 		sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
    392 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    393 		    sc->wdc_channels[i].ch_queue =
    394 			sc->wdc_channels[0].ch_queue;
    395 		} else {
    396 		    sc->wdc_channels[i].ch_queue =
    397 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    398 			M_NOWAIT);
    399 		}
    400 		if (sc->wdc_channels[i].ch_queue == NULL) {
    401 		    printf("%s %s channel: "
    402 			"can't allocate memory for command queue",
    403 			sc->sc_wdcdev.sc_dev.dv_xname,
    404 			PCIIDE_CHANNEL_NAME(i));
    405 			continue;
    406 		}
    407 		printf("%s: %s channel %s to %s mode\n",
    408 		    sc->sc_wdcdev.sc_dev.dv_xname,
    409 		    PCIIDE_CHANNEL_NAME(i),
    410 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    411 		      "configured" : "wired",
    412 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    413 		      "compatibility");
    414 
    415 		if (interface & PCIIDE_INTERFACE_PCI(i))
    416 			cp->hw_ok = pciide_map_channel_native(sc, pa, i);
    417 		else
    418 			cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
    419 		if (!cp->hw_ok)
    420 			continue;
    421 		/* Now call common attach routine */
    422 		wdcattach(&sc->wdc_channels[i]);
    423 	}
    424 	if (sc->sc_pp == NULL)
    425 		default_setup_chip(sc, pc, tag);
    426 	else
    427 		sc->sc_pp->setup_chip(sc, pc, tag);
    428 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    429 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    430 }
    431 
    432 int
    433 pciide_map_channel_compat(sc, pa, chan)
    434 	struct pciide_softc *sc;
    435 	struct pci_attach_args *pa;
    436 	int chan;
    437 {
    438 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    439 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    440 	const char *probe_fail_reason;
    441 	int rv = 1;
    442 
    443 	cp->compat = 1;
    444 
    445 	wdc_cp->cmd_iot = pa->pa_iot;
    446 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
    447 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    448 		printf("%s: couldn't map %s channel cmd regs\n",
    449 		    sc->sc_wdcdev.sc_dev.dv_xname,
    450 		    PCIIDE_CHANNEL_NAME(chan));
    451 		rv = 0;
    452 	}
    453 
    454 	wdc_cp->ctl_iot = pa->pa_iot;
    455 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
    456 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    457 		printf("%s: couldn't map %s channel ctl regs\n",
    458 		    sc->sc_wdcdev.sc_dev.dv_xname,
    459 		    PCIIDE_CHANNEL_NAME(chan));
    460 		rv = 0;
    461 	}
    462 
    463 	/*
    464 	 * If we weren't able to map the device successfully,
    465 	 * we just give up now.  Something else has already
    466 	 * occupied those ports, indicating that the device has
    467 	 * (probably) been completely disabled (by some nonstandard
    468 	 * mechanism).
    469 	 *
    470 	 * XXX If we successfully map some ports, but not others,
    471 	 * XXX it might make sense to unmap the ones that we mapped.
    472 	 */
    473 	if (rv == 0)
    474 		goto out;
    475 
    476 	/*
    477 	 * If we were able to map the device successfully, try to
    478 	 * make sure that there's a wdc there and that it's
    479 	 * attributable to us.
    480 	 *
    481 	 * If there's not, then we assume that there's the device
    482 	 * has been disabled and that other devices are free to use
    483 	 * its ports.
    484 	 */
    485 	probe_fail_reason = pciide_compat_channel_probe(sc, pa, chan);
    486 	if (probe_fail_reason != NULL) {
    487 		printf("%s: %s channel ignored (%s)\n",
    488 		    sc->sc_wdcdev.sc_dev.dv_xname,
    489 		    PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
    490 		rv = 0;
    491 
    492 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    493 		    PCIIDE_COMPAT_CMD_SIZE);
    494 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    495 		    PCIIDE_COMPAT_CTL_SIZE);
    496 
    497 		goto out;
    498 	}
    499 
    500 	/*
    501 	 * If we're here, we were able to map the device successfully
    502 	 * and it really looks like there's a controller there.
    503 	 *
    504 	 * Unless those conditions are true, we don't map the
    505 	 * compatibility interrupt.  The spec indicates that if a
    506 	 * channel is configured for compatibility mode and the PCI
    507 	 * device's I/O space is enabled, the channel will be enabled.
    508 	 * Hoewver, some devices seem to be able to disable invididual
    509 	 * compatibility channels (via non-standard mechanisms).  If
    510 	 * the channel is disabled, the interrupt line can (probably)
    511 	 * be used by other devices (and may be assigned to other
    512 	 * devices by the BIOS).  If we mapped the interrupt we might
    513 	 * conflict with another interrupt assignment.
    514 	 */
    515 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    516 	    pa, chan, pciide_compat_intr, wdc_cp);
    517 	if (cp->ih == NULL) {
    518 		printf("%s: no compatibility interrupt for use by %s channel\n",
    519 		    sc->sc_wdcdev.sc_dev.dv_xname,
    520 		    PCIIDE_CHANNEL_NAME(chan));
    521 		rv = 0;
    522 	}
    523 
    524 out:
    525 	return (rv);
    526 }
    527 
    528 const char *
    529 pciide_compat_channel_probe(sc, pa, chan)
    530 	struct pciide_softc *sc;
    531 	struct pci_attach_args *pa;
    532 {
    533 	pcireg_t csr;
    534 	const char *failreason = NULL;
    535 
    536 	/*
    537 	 * Check to see if something appears to be there.
    538 	 */
    539 	if (!wdcprobe(&sc->wdc_channels[chan])) {
    540 		failreason = "not responding; disabled or no drives?";
    541 		goto out;
    542 	}
    543 
    544 	/*
    545 	 * Now, make sure it's actually attributable to this PCI IDE
    546 	 * channel by trying to access the channel again while the
    547 	 * PCI IDE controller's I/O space is disabled.  (If the
    548 	 * channel no longer appears to be there, it belongs to
    549 	 * this controller.)  YUCK!
    550 	 */
    551 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    552 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    553 	    csr & ~PCI_COMMAND_IO_ENABLE);
    554 	if (wdcprobe(&sc->wdc_channels[chan]))
    555 		failreason = "other hardware responding at addresses";
    556 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    557 
    558 out:
    559 	return (failreason);
    560 }
    561 
    562 int
    563 pciide_map_channel_native(sc, pa, chan)
    564 	struct pciide_softc *sc;
    565 	struct pci_attach_args *pa;
    566 	int chan;
    567 {
    568 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    569 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    570 	int rv = 1;
    571 
    572 	cp->compat = 0;
    573 
    574 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
    575 	    0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
    576 		printf("%s: couldn't map %s channel cmd regs\n",
    577 		    sc->sc_wdcdev.sc_dev.dv_xname,
    578 		    PCIIDE_CHANNEL_NAME(chan));
    579 		rv = 0;
    580 	}
    581 
    582 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
    583 	    0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
    584 		printf("%s: couldn't map %s channel ctl regs\n",
    585 		    sc->sc_wdcdev.sc_dev.dv_xname,
    586 		    PCIIDE_CHANNEL_NAME(chan));
    587 		rv = 0;
    588 	}
    589 
    590 	if ((cp->ih = sc->sc_pci_ih) == NULL) {
    591 		printf("%s: no native-PCI interrupt for use by %s channel\n",
    592 		    sc->sc_wdcdev.sc_dev.dv_xname,
    593 		    PCIIDE_CHANNEL_NAME(chan));
    594 		rv = 0;
    595 	}
    596 
    597 	return (rv);
    598 }
    599 
    600 int
    601 pciide_compat_intr(arg)
    602 	void *arg;
    603 {
    604 	struct channel_softc *wdc_cp = arg;
    605 
    606 #ifdef DIAGNOSTIC
    607 	struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
    608 	struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
    609 	/* should only be called for a compat channel */
    610 	if (cp->compat == 0)
    611 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    612 #endif
    613 	return (wdcintr(wdc_cp));
    614 }
    615 
    616 int
    617 pciide_pci_intr(arg)
    618 	void *arg;
    619 {
    620 	struct pciide_softc *sc = arg;
    621 	struct pciide_channel *cp;
    622 	struct channel_softc *wdc_cp;
    623 	int i, rv, crv;
    624 
    625 	rv = 0;
    626 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    627 		cp = &sc->pciide_channels[i];
    628 		wdc_cp = &sc->wdc_channels[i];
    629 
    630 		/* If a compat channel skip. */
    631 		if (cp->compat)
    632 			continue;
    633 		/* if this channel not waiting for intr, skip */
    634 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    635 			continue;
    636 
    637 		crv = wdcintr(wdc_cp);
    638 		if (crv == 0)
    639 			;		/* leave rv alone */
    640 		else if (crv == 1)
    641 			rv = 1;		/* claim the intr */
    642 		else if (rv == 0)	/* crv should be -1 in this case */
    643 			rv = crv;	/* if we've done no better, take it */
    644 	}
    645 	return (rv);
    646 }
    647 
    648 void
    649 default_setup_cap(sc)
    650 	struct pciide_softc *sc;
    651 {
    652 	if (sc->sc_dma_ok)
    653 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    654 	sc->sc_wdcdev.pio_mode = 0;
    655 	sc->sc_wdcdev.dma_mode = 0;
    656 }
    657 
    658 void
    659 default_setup_chip(sc, pc, tag)
    660 	struct pciide_softc *sc;
    661 	pci_chipset_tag_t pc;
    662 	pcitag_t tag;
    663 {
    664 	int channel, drive, idedma_ctl;
    665 	struct channel_softc *chp;
    666 	struct ata_drive_datas *drvp;
    667 
    668 	if (sc->sc_dma_ok == 0)
    669 		return; /* nothing to do */
    670 
    671 	/* Allocate DMA maps */
    672 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    673 		idedma_ctl = 0;
    674 		chp = &sc->wdc_channels[channel];
    675 		for (drive = 0; drive < 2; drive++) {
    676 			drvp = &chp->ch_drive[drive];
    677 			/* If no drive, skip */
    678 			if ((drvp->drive_flags & DRIVE) == 0)
    679 				continue;
    680 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    681 				/* Abort DMA setup */
    682 				printf("%s:%d:%d: can't allocate DMA maps, "
    683 				    "using PIO transferts\n",
    684 				    sc->sc_wdcdev.sc_dev.dv_xname,
    685 				    channel, drive);
    686 				drvp->drive_flags &= ~DRIVE_DMA;
    687 			}
    688 			printf("%s:%d:%d: using DMA mode %d\n",
    689 			    sc->sc_wdcdev.sc_dev.dv_xname,
    690 			    channel, drive,
    691 			    drvp->DMA_mode);
    692 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    693 		}
    694 		if (idedma_ctl != 0) {
    695 			/* Add software bits in status register */
    696 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    697 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    698 			    idedma_ctl);
    699 		}
    700 	}
    701 
    702 }
    703 
    704 void
    705 piix_setup_cap(sc)
    706 	struct pciide_softc *sc;
    707 {
    708 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
    709 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    710 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
    711 	    WDC_CAPABILITY_DMA;
    712 	sc->sc_wdcdev.pio_mode = 4;
    713 	sc->sc_wdcdev.dma_mode = 2;
    714 }
    715 
    716 void
    717 piix_setup_chip(sc, pc, tag)
    718 	struct pciide_softc *sc;
    719 	pci_chipset_tag_t pc;
    720 	pcitag_t tag;
    721 {
    722 	struct channel_softc *chp;
    723 	u_int8_t mode[2];
    724 	u_int8_t channel, drive;
    725 	u_int32_t idetim, sidetim, idedma_ctl;
    726 	struct ata_drive_datas *drvp;
    727 
    728 	idetim = sidetim = 0;
    729 
    730 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    731 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    732 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    733 
    734 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    735 		chp = &sc->wdc_channels[channel];
    736 		drvp = chp->ch_drive;
    737 		idedma_ctl = 0;
    738 		/* Enable IDE registers decode */
    739 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    740 		    channel);
    741 
    742 		/* setup DMA if needed */
    743 		for (drive = 0; drive < 2; drive++) {
    744 			if (drvp[drive].drive_flags & DRIVE_DMA &&
    745 			    pciide_dma_table_setup(sc, channel, drive) != 0) {
    746 				drvp[drive].drive_flags &= ~DRIVE_DMA;
    747 			}
    748 		}
    749 
    750 		/*
    751 		 * Here we have to mess up with drives mode: PIIX can't have
    752 		 * different timings for master and slave drives.
    753 		 * We need to find the best combination.
    754 		 */
    755 
    756 		/* If both drives supports DMA, takes the lower mode */
    757 		if ((drvp[0].drive_flags & DRIVE_DMA) &&
    758 		    (drvp[1].drive_flags & DRIVE_DMA)) {
    759 			mode[0] = mode[1] =
    760 			    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
    761 			goto ok;
    762 		}
    763 		/*
    764 		 * If only one drive supports DMA, use its mode, and
    765 		 * put the other one in PIO mode 0 if mode not compatible
    766 		 */
    767 		if (drvp[0].drive_flags & DRIVE_DMA) {
    768 			mode[0] = drvp[0].DMA_mode;
    769 			mode[1] = drvp[1].PIO_mode;
    770 			if (piix_isp_pio[mode[1]] < piix_isp_dma[mode[0]] ||
    771 			    piix_rtc_pio[mode[1]] < piix_rtc_dma[mode[0]])
    772 				mode[1] = 0;
    773 			goto ok;
    774 		}
    775 		if (drvp[1].drive_flags & DRIVE_DMA) {
    776 			mode[1] = drvp[1].DMA_mode;
    777 			mode[0] = drvp[0].PIO_mode;
    778 			if (piix_isp_pio[mode[0]] < piix_isp_dma[mode[1]] ||
    779 			    piix_rtc_pio[mode[0]] < piix_rtc_dma[mode[1]])
    780 				mode[0] = 0;
    781 			goto ok;
    782 		}
    783 		/*
    784 		 * If both drives are not DMA, takes the lower mode, unless
    785 		 * one of them is PIO mode 0
    786 		 */
    787 		if (drvp[0].PIO_mode == 0) {
    788 			mode[0] = 0;
    789 			mode[1] = drvp[1].PIO_mode;
    790 		} else if (drvp[1].PIO_mode == 0) {
    791 			mode[1] = 0;
    792 			mode[0] = drvp[0].PIO_mode;
    793 		} else {
    794 			mode[0] = mode[1] =
    795 			    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
    796 		}
    797 ok:		/* The modes are setup */
    798 		for (drive = 0; drive < 2; drive++) {
    799 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    800 				idetim |= piix_setup_idetim_timings(
    801 				    mode[drive], 1, channel);
    802 				goto end;
    803 			}
    804 		}
    805 		/* If we are there, none of the drives are DMA */
    806 		if (mode[0] > 0)
    807 			idetim |= piix_setup_idetim_timings(
    808 			    mode[0], 0, channel);
    809 		else
    810 			idetim |= piix_setup_idetim_timings(
    811 			    mode[1], 0, channel);
    812 end:		/*
    813 		 * timing mode is now set up in the controller. Enable
    814 		 * it per-drive
    815 		 */
    816 		for (drive = 0; drive < 2; drive++) {
    817 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    818 				idetim = PIIX_IDETIM_SET(idetim,
    819 				    PIIX_IDETIM_DTE(drive), channel);
    820 				idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    821 				drvp[drive].DMA_mode = mode[drive];
    822 				drvp[drive].PIO_mode = 0;
    823 				printf("%s:%d:%d: using DMA mode %d\n",
    824 				    sc->sc_wdcdev.sc_dev.dv_xname,
    825 				    channel, drive, mode[drive]);
    826 			} else {
    827 				if (mode[drive] > 0)
    828 					idetim |= piix_setup_idetim_drvs(
    829 					    mode[drive], channel, drive);
    830 				drvp[drive].PIO_mode = mode[drive];
    831 				printf("%s:%d:%d: using PIO mode %d\n",
    832 				    sc->sc_wdcdev.sc_dev.dv_xname,
    833 				    channel, drive, mode[drive]);
    834 			}
    835 		}
    836 		if (idedma_ctl != 0) {
    837 			/* Add software bits in status register */
    838 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    839 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    840 			    idedma_ctl);
    841 		}
    842 	}
    843 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
    844 	    idetim, sidetim), DEBUG_PROBE);
    845 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    846 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    847 }
    848 
    849 void
    850 piix3_4_setup_chip(sc, pc, tag)
    851 	struct pciide_softc *sc;
    852 	pci_chipset_tag_t pc;
    853 	pcitag_t tag;
    854 {
    855 	int channel, drive;
    856 	struct channel_softc *chp;
    857 	struct ata_drive_datas *drvp;
    858 	u_int32_t idetim, sidetim, udmactl, udmatim, idedma_ctl;
    859 
    860 	idetim = sidetim = udmactl = udmatim = 0;
    861 
    862 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    863 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    864 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    865 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    866 		chp = &sc->wdc_channels[channel];
    867 		idedma_ctl = 0;
    868 		/* Enable IDE registers decode */
    869 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    870 		    channel);
    871 		for (drive = 0; drive < 2; drive++) {
    872 			drvp = &chp->ch_drive[drive];
    873 			/* If no drive, skip */
    874 			if ((drvp->drive_flags & DRIVE) == 0)
    875 				continue;
    876 			/* add timing values, setup DMA if needed */
    877 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    878 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    879 			    sc->sc_dma_ok == 0)
    880 				goto pio;
    881 			if (pciide_dma_table_setup(sc, channel, drive) != 0)
    882 			    goto pio; /* Abort DMA setup */
    883 			drvp->PIO_mode = 0; /* use compatible timings for PIO */
    884 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
    885 			    (drvp->drive_flags & DRIVE_UDMA)) {
    886 				/* use Ultra/DMA */
    887 				drvp->drive_flags &= ~DRIVE_DMA;
    888 				udmactl |= PIIX_UDMACTL_DRV_EN(
    889 				    channel, drive);
    890 				udmatim |= PIIX_UDMATIM_SET(
    891 				    piix4_sct_udma[drvp->UDMA_mode],
    892 				    channel, drive);
    893 				printf("%s:%d:%d: using Ultra DMA/33 mode %d\n",
    894 				     sc->sc_wdcdev.sc_dev.dv_xname,
    895 				     channel, drive,
    896 				     drvp->UDMA_mode);
    897 			} else {
    898 				/* use Multiword DMA */
    899 				drvp->drive_flags &= ~DRIVE_UDMA;
    900 				if (drive == 0) {
    901 					idetim |= piix_setup_idetim_timings(
    902 					    drvp->DMA_mode, 1, channel);
    903 				} else {
    904 					sidetim |= piix_setup_sidetim_timings(
    905 						drvp->DMA_mode, 1, channel);
    906 					idetim =PIIX_IDETIM_SET(idetim,
    907 					    PIIX_IDETIM_SITRE, channel);
    908 				}
    909 				printf("%s:%d:%d: using DMA mode %d\n",
    910 				     sc->sc_wdcdev.sc_dev.dv_xname,
    911 				     channel, drive,
    912 				     drvp->DMA_mode);
    913 			}
    914 			/* Enable DMA only PIO modes may be wrong */
    915 			idetim = PIIX_IDETIM_SET(idetim,
    916 			    PIIX_IDETIM_DTE(drive), channel);
    917 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    918 			continue;
    919 
    920 pio:			/* use PIO mode */
    921 			drvp->drive_flags &= ~DRIVE_DMA | DRIVE_UDMA;
    922 			if (drive == 0) {
    923 				idetim |= piix_setup_idetim_timings(
    924 				    drvp->PIO_mode, 0, channel);
    925 			} else {
    926 				sidetim |= piix_setup_sidetim_timings(
    927 					drvp->PIO_mode, 0, channel);
    928 				idetim =PIIX_IDETIM_SET(idetim,
    929 				    PIIX_IDETIM_SITRE, channel);
    930 			}
    931 			idetim |= piix_setup_idetim_drvs(drvp->PIO_mode,
    932 			    channel, drive);
    933 			printf("%s:%d:%d: using PIO mode %d\n",
    934 			     sc->sc_wdcdev.sc_dev.dv_xname, channel, drive,
    935 			     drvp->PIO_mode);
    936 		}
    937 		if (idedma_ctl != 0) {
    938 			/* Add software bits in status register */
    939 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    940 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    941 			    idedma_ctl);
    942 		}
    943 	}
    944 
    945 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
    946 	    idetim, sidetim), DEBUG_PROBE);
    947 	if (chp->wdc->cap & WDC_CAPABILITY_UDMA) {
    948 		WDCDEBUG_PRINT((", udmactl=0x%x, udmatim=0x%x", udmactl,
    949 		    udmatim), DEBUG_PROBE);
    950 		pci_conf_write(pc, tag, PIIX_UDMACTL, udmactl);
    951 		pci_conf_write(pc, tag, PIIX_UDMATIM, udmatim);
    952 	}
    953 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    954 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    955 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    956 }
    957 
    958 /* setup ISP and RTC fields, based on mode */
    959 static u_int32_t
    960 piix_setup_idetim_timings(mode, dma, channel)
    961 	u_int8_t mode;
    962 	u_int8_t dma;
    963 	u_int8_t channel;
    964 {
    965 
    966 	if (dma)
    967 		return PIIX_IDETIM_SET(0,
    968 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
    969 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
    970 		    channel);
    971 	else
    972 		return PIIX_IDETIM_SET(0,
    973 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
    974 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
    975 		    channel);
    976 }
    977 
    978 /* setup PPE, IE and TIME1 field based on PIO mode */
    979 static u_int32_t
    980 piix_setup_idetim_drvs(mode, channel, drive)
    981 	u_int8_t mode;
    982 	u_int8_t channel;
    983 	u_int8_t drive;
    984 {
    985 	u_int32_t ret = 0;
    986 
    987 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
    988 	/* I didn't read anything about this, it's just a guess */
    989 	if (mode >= 2)
    990 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
    991 	if (mode >= 3)
    992 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
    993 	return ret;
    994 }
    995 
    996 /* setup values in SIDETIM registers, based on mode */
    997 static u_int32_t
    998 piix_setup_sidetim_timings(mode, dma, channel)
    999 	u_int8_t mode;
   1000 	u_int8_t dma;
   1001 	u_int8_t channel;
   1002 {
   1003 	if (dma)
   1004 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1005 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1006 	else
   1007 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1008 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1009 }
   1010 
   1011 
   1012 
   1013 int
   1014 pciide_dma_table_setup(sc, channel, drive)
   1015 	struct pciide_softc *sc;
   1016 	int channel, drive;
   1017 {
   1018 	bus_dma_segment_t seg;
   1019 	int error, rseg;
   1020 	const bus_size_t dma_table_size =
   1021 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
   1022 	struct pciide_dma_maps *dma_maps =
   1023 	    &sc->pciide_channels[channel].dma_maps[drive];
   1024 
   1025 	/* Allocate memory for the DMA tables and map it */
   1026 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
   1027 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
   1028 	    BUS_DMA_NOWAIT)) != 0) {
   1029 		printf("%s:%d: unable to allocate table DMA for"
   1030 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1031 		    channel, drive, error);
   1032 		return error;
   1033 	}
   1034 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1035 	    dma_table_size,
   1036 	    (caddr_t *)&dma_maps->dma_table,
   1037 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1038 		printf("%s:%d: unable to map table DMA for"
   1039 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1040 		    channel, drive, error);
   1041 		return error;
   1042 	}
   1043 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
   1044 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
   1045 	    seg.ds_addr), DEBUG_PROBE);
   1046 
   1047 	/* Create and load table DMA map for this disk */
   1048 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
   1049 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
   1050 	    &dma_maps->dmamap_table)) != 0) {
   1051 		printf("%s:%d: unable to create table DMA map for"
   1052 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1053 		    channel, drive, error);
   1054 		return error;
   1055 	}
   1056 	if ((error = bus_dmamap_load(sc->sc_dmat,
   1057 	    dma_maps->dmamap_table,
   1058 	    dma_maps->dma_table,
   1059 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1060 		printf("%s:%d: unable to load table DMA map for"
   1061 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1062 		    channel, drive, error);
   1063 		return error;
   1064 	}
   1065 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
   1066 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
   1067 	/* Create a xfer DMA map for this drive */
   1068 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
   1069 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
   1070 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1071 	    &dma_maps->dmamap_xfer)) != 0) {
   1072 		printf("%s:%d: unable to create xfer DMA map for"
   1073 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1074 		    channel, drive, error);
   1075 		return error;
   1076 	}
   1077 	return 0;
   1078 }
   1079 
   1080 int
   1081 pciide_dma_init(v, channel, drive, databuf, datalen, read)
   1082 	void *v;
   1083 	int channel, drive;
   1084 	void *databuf;
   1085 	size_t datalen;
   1086 	int read;
   1087 {
   1088 	struct pciide_softc *sc = v;
   1089 	int error, seg;
   1090 	struct pciide_dma_maps *dma_maps =
   1091 	    &sc->pciide_channels[channel].dma_maps[drive];
   1092 
   1093 	error = bus_dmamap_load(sc->sc_dmat,
   1094 	    dma_maps->dmamap_xfer,
   1095 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
   1096 	if (error) {
   1097 		printf("%s:%d: unable to load xfer DMA map for"
   1098 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1099 		    channel, drive, error);
   1100 		return error;
   1101 	}
   1102 
   1103 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1104 	    dma_maps->dmamap_xfer->dm_mapsize,
   1105 	    (read) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1106 
   1107 	WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
   1108 	    dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
   1109 	    vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
   1110 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
   1111 #ifdef DIAGNOSTIC
   1112 		/* A segment must not cross a 64k boundary */
   1113 		{
   1114 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1115 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
   1116 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
   1117 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
   1118 			printf("pciide_dma: segment %d physical addr 0x%lx"
   1119 			    " len 0x%lx not properly aligned\n",
   1120 			    seg, phys, len);
   1121 			panic("pciide_dma: buf align");
   1122 		}
   1123 		}
   1124 #endif
   1125 		dma_maps->dma_table[seg].base_addr =
   1126 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1127 		dma_maps->dma_table[seg].byte_count =
   1128 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
   1129 		    IDEDMA_BYTE_COUNT_MASK;
   1130 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
   1131 		   seg, dma_maps->dma_table[seg].byte_count,
   1132 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
   1133 
   1134 	}
   1135 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
   1136 		IDEDMA_BYTE_COUNT_EOT;
   1137 
   1138 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
   1139 	    dma_maps->dmamap_table->dm_mapsize,
   1140 	    BUS_DMASYNC_PREWRITE);
   1141 
   1142 	/* Maps are ready. Start DMA function */
   1143 #ifdef DIAGNOSTIC
   1144 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
   1145 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
   1146 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1147 		panic("pciide_dma_init: table align");
   1148 	}
   1149 #endif
   1150 
   1151 	WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
   1152 	    "phys 0x%x)\n",
   1153 	    dma_maps->dma_table,
   1154 	    dma_maps->dmamap_table->dm_segs[0].ds_addr,
   1155 	    dma_maps->dmamap_table->dm_segs[0].ds_len,
   1156 	    dma_maps->dmamap_table->dm_nsegs,
   1157 	    vtophys(dma_maps->dma_table)), DEBUG_DMA);
   1158 	/* Clear status bits */
   1159 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1160 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1161 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1162 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
   1163 	/* Write table addr */
   1164 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   1165 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
   1166 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1167 	/* set read/write */
   1168 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1169 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1170 	    (read) ? IDEDMA_CMD_WRITE: 0);
   1171 	return 0;
   1172 }
   1173 
   1174 void
   1175 pciide_dma_start(v, channel, drive, read)
   1176 	void *v;
   1177 	int channel, drive;
   1178 {
   1179 	struct pciide_softc *sc = v;
   1180 
   1181 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
   1182 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1183 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1184 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1185 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
   1186 }
   1187 
   1188 int
   1189 pciide_dma_finish(v, channel, drive, read)
   1190 	void *v;
   1191 	int channel, drive;
   1192 	int read;
   1193 {
   1194 	struct pciide_softc *sc = v;
   1195 	u_int8_t status;
   1196 	struct pciide_dma_maps *dma_maps =
   1197 	    &sc->pciide_channels[channel].dma_maps[drive];
   1198 
   1199 	/* Unload the map of the data buffer */
   1200 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1201 	    dma_maps->dmamap_xfer->dm_mapsize,
   1202 	    (read) ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1203 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
   1204 
   1205 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1206 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
   1207 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
   1208 	    DEBUG_XFERS);
   1209 
   1210 	/* stop DMA channel */
   1211 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1212 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1213 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1214 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
   1215 
   1216 	/* Clear status bits */
   1217 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1218 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1219 	    status);
   1220 
   1221 	if ((status & (IDEDMA_CTL_INTR | IDEDMA_CTL_ERR | IDEDMA_CTL_ACT)) !=
   1222 	    IDEDMA_CTL_INTR) {
   1223 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
   1224 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
   1225 		return 1;
   1226 	}
   1227 	return 0;
   1228 }
   1229