Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.11
      1 /*	$NetBSD: pciide.c,v 1.11 1998/10/19 12:24:33 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  */
     44 
     45 #define DEBUG_DMA   0x01
     46 #define DEBUG_XFERS  0x02
     47 #define DEBUG_FUNCS  0x08
     48 #define DEBUG_PROBE  0x10
     49 #ifdef WDCDEBUG
     50 int wdcdebug_pciide_mask = DEBUG_PROBE;
     51 #define WDCDEBUG_PRINT(args, level) \
     52 	if (wdcdebug_pciide_mask & (level)) printf args
     53 #else
     54 #define WDCDEBUG_PRINT(args, level)
     55 #endif
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/device.h>
     59 #include <sys/malloc.h>
     60 
     61 #include <vm/vm.h>
     62 #include <vm/vm_param.h>
     63 #include <vm/vm_kern.h>
     64 
     65 #include <dev/pci/pcireg.h>
     66 #include <dev/pci/pcivar.h>
     67 #include <dev/pci/pcidevs.h>
     68 #include <dev/pci/pciidereg.h>
     69 #include <dev/pci/pciidevar.h>
     70 #include <dev/pci/pciide_piix_reg.h>
     71 #include <dev/pci/pciide_apollo_reg.h>
     72 #include <dev/pci/pciide_cmd_reg.h>
     73 #include <dev/ata/atavar.h>
     74 #include <dev/ic/wdcreg.h>
     75 #include <dev/ic/wdcvar.h>
     76 
     77 struct pciide_softc {
     78 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     79 
     80 	void			*sc_pci_ih;	/* PCI interrupt handle */
     81 	int			sc_dma_ok;	/* bus-master DMA info */
     82 	bus_space_tag_t		sc_dma_iot;
     83 	bus_space_handle_t	sc_dma_ioh;
     84 	bus_dma_tag_t		sc_dmat;
     85 	/* Chip description */
     86 	const struct pciide_product_desc *sc_pp;
     87 	/* common definitions */
     88 	struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
     89 	/* internal bookkeeping */
     90 	struct pciide_channel {			/* per-channel data */
     91 		int		hw_ok;		/* hardware mapped & OK? */
     92 		int		compat;		/* is it compat? */
     93 		void		*ih;		/* compat or pci handle */
     94 		/* DMA tables and DMA map for xfer, for each drive */
     95 		struct pciide_dma_maps {
     96 			bus_dmamap_t    dmamap_table;
     97 			struct idedma_table *dma_table;
     98 			bus_dmamap_t    dmamap_xfer;
     99 		} dma_maps[2];
    100 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    101 };
    102 
    103 void default_setup_cap __P((struct pciide_softc*));
    104 void default_setup_chip __P((struct pciide_softc*,
    105 		pci_chipset_tag_t, pcitag_t));
    106 const char *default_channel_probe __P((struct pciide_softc *,
    107 		struct pci_attach_args *, int));
    108 int default_channel_disable __P((struct pciide_softc *,
    109 		struct pci_attach_args *, int));
    110 
    111 
    112 void piix_setup_cap __P((struct pciide_softc*));
    113 void piix_setup_chip __P((struct pciide_softc*,
    114 		pci_chipset_tag_t, pcitag_t));
    115 void piix3_4_setup_chip __P((struct pciide_softc*,
    116 		pci_chipset_tag_t, pcitag_t));
    117 const char *piix_channel_probe __P((struct pciide_softc *,
    118 		struct pci_attach_args *, int));
    119 int piix_channel_disable __P((struct pciide_softc *,
    120 		struct pci_attach_args *, int));
    121 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    122 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    123 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    124 
    125 void apollo_setup_cap __P((struct pciide_softc*));
    126 void apollo_setup_chip __P((struct pciide_softc*,
    127 		pci_chipset_tag_t, pcitag_t));
    128 const char *apollo_channel_probe __P((struct pciide_softc *,
    129 		struct pci_attach_args *, int));
    130 int apollo_channel_disable __P((struct pciide_softc *,
    131 		struct pci_attach_args *, int));
    132 
    133 const char *cmd_channel_probe __P((struct pciide_softc *,
    134 		struct pci_attach_args *, int));
    135 int cmd_channel_disable __P((struct pciide_softc *,
    136 		struct pci_attach_args *, int));
    137 
    138 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    139 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    140 void pciide_dma_start __P((void*, int, int, int));
    141 int  pciide_dma_finish __P((void*, int, int, int));
    142 
    143 struct pciide_product_desc {
    144     u_int32_t ide_product;
    145     int ide_flags;
    146     const char *ide_name;
    147     /* init controller's capabilities for drives probe */
    148     void (*setup_cap) __P((struct pciide_softc*));
    149     /* init controller after drives probe */
    150     void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
    151     /* Probe for compat channel enabled/disabled */
    152     const char * (*channel_probe) __P((struct pciide_softc *,
    153 		struct pci_attach_args *, int));
    154     int  (*channel_disable) __P((struct pciide_softc *,
    155 		struct pci_attach_args *, int));
    156 };
    157 
    158 /* Flags for ide_flags */
    159 #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
    160 #define ONE_QUEUE         0x02 /* device need serialised access */
    161 
    162 /* Default product description for devices not known from this controller */
    163 const struct pciide_product_desc default_product_desc = {
    164     0,
    165     0,
    166     "Generic PCI IDE controller",
    167     default_setup_cap,
    168     default_setup_chip,
    169     default_channel_probe,
    170     default_channel_disable
    171 };
    172 
    173 
    174 const struct pciide_product_desc pciide_intel_products[] =  {
    175     { PCI_PRODUCT_INTEL_82092AA,
    176       0,
    177       "Intel 82092AA IDE controller",
    178       default_setup_cap,
    179       default_setup_chip,
    180       default_channel_probe,
    181       default_channel_disable
    182     },
    183     { PCI_PRODUCT_INTEL_82371FB_IDE,
    184       0,
    185       "Intel 82371FB IDE controller (PIIX)",
    186       piix_setup_cap,
    187       piix_setup_chip,
    188       piix_channel_probe,
    189       piix_channel_disable
    190     },
    191     { PCI_PRODUCT_INTEL_82371SB_IDE,
    192       0,
    193       "Intel 82371SB IDE Interface (PIIX3)",
    194       piix_setup_cap,
    195       piix3_4_setup_chip,
    196       piix_channel_probe,
    197       piix_channel_disable
    198     },
    199     { PCI_PRODUCT_INTEL_82371AB_IDE,
    200       0,
    201       "Intel 82371AB IDE controller (PIIX4)",
    202       piix_setup_cap,
    203       piix3_4_setup_chip,
    204       piix_channel_probe,
    205       piix_channel_disable
    206     },
    207     { 0,
    208       0,
    209       NULL,
    210     }
    211 };
    212 const struct pciide_product_desc pciide_cmd_products[] =  {
    213     { PCI_PRODUCT_CMDTECH_640,
    214       ONE_QUEUE | CMD_PCI064x_IOEN,
    215       "CMD Technology PCI0640",
    216       default_setup_cap,
    217       default_setup_chip,
    218       cmd_channel_probe,
    219       cmd_channel_disable
    220     },
    221     { 0,
    222       0,
    223       NULL,
    224     }
    225 };
    226 
    227 const struct pciide_product_desc pciide_via_products[] =  {
    228     { PCI_PRODUCT_VIATECH_VT82C586_IDE,
    229       0,
    230       "VIA Technologies VT82C586 (Apollo VP) IDE Controller",
    231       apollo_setup_cap,
    232       apollo_setup_chip,
    233       apollo_channel_probe,
    234       apollo_channel_disable
    235      },
    236     { PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    237       0,
    238       "VIA Technologies VT82C586A IDE Controller",
    239       apollo_setup_cap,
    240       apollo_setup_chip,
    241       apollo_channel_probe,
    242       apollo_channel_disable
    243      },
    244      { 0,
    245        0,
    246        NULL,
    247      }
    248 };
    249 
    250 struct pciide_vendor_desc {
    251     u_int32_t ide_vendor;
    252     const struct pciide_product_desc *ide_products;
    253 };
    254 
    255 const struct pciide_vendor_desc pciide_vendors[] = {
    256     { PCI_VENDOR_INTEL, pciide_intel_products },
    257     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    258     { PCI_VENDOR_VIATECH, pciide_via_products },
    259     { 0, NULL }
    260 };
    261 
    262 
    263 #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    264 
    265 int	pciide_match __P((struct device *, struct cfdata *, void *));
    266 void	pciide_attach __P((struct device *, struct device *, void *));
    267 
    268 struct cfattach pciide_ca = {
    269 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    270 };
    271 
    272 int	pciide_map_channel_compat __P((struct pciide_softc *,
    273 	    struct pci_attach_args *, int));
    274 int	pciide_map_channel_native __P((struct pciide_softc *,
    275 	    struct pci_attach_args *, int));
    276 int	pciide_print __P((void *, const char *pnp));
    277 int	pciide_compat_intr __P((void *));
    278 int	pciide_pci_intr __P((void *));
    279 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    280 
    281 const struct pciide_product_desc*
    282 pciide_lookup_product(id)
    283     u_int32_t id;
    284 {
    285     const struct pciide_product_desc *pp;
    286     const struct pciide_vendor_desc *vp;
    287 
    288     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    289 	if (PCI_VENDOR(id) == vp->ide_vendor)
    290 	    break;
    291 
    292     if ((pp = vp->ide_products) == NULL)
    293 	return NULL;
    294 
    295     for (; pp->ide_name != NULL; pp++)
    296 	if (PCI_PRODUCT(id) == pp->ide_product)
    297 	    break;
    298 
    299     if (pp->ide_name == NULL)
    300 	return NULL;
    301     return pp;
    302 }
    303 
    304 int
    305 pciide_match(parent, match, aux)
    306 	struct device *parent;
    307 	struct cfdata *match;
    308 	void *aux;
    309 {
    310 	struct pci_attach_args *pa = aux;
    311 
    312 	/*
    313 	 * Check the ID register to see that it's a PCI IDE controller.
    314 	 * If it is, we assume that we can deal with it; it _should_
    315 	 * work in a standardized way...
    316 	 */
    317 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    318 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    319 		return (1);
    320 	}
    321 
    322 	return (0);
    323 }
    324 
    325 void
    326 pciide_attach(parent, self, aux)
    327 	struct device *parent, *self;
    328 	void *aux;
    329 {
    330 	struct pci_attach_args *pa = aux;
    331 	pci_chipset_tag_t pc = pa->pa_pc;
    332 	pcitag_t tag = pa->pa_tag;
    333 	struct pciide_softc *sc = (struct pciide_softc *)self;
    334 	struct pciide_channel *cp;
    335 	pcireg_t class, interface, csr;
    336 	pci_intr_handle_t intrhandle;
    337 	const char *intrstr;
    338 	char devinfo[256];
    339 	int i;
    340 
    341         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    342 	if (sc->sc_pp == NULL) {
    343 		sc->sc_pp = &default_product_desc;
    344 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    345 		printf(": %s (rev. 0x%02x)\n", devinfo,
    346 		    PCI_REVISION(pa->pa_class));
    347 	} else {
    348 		printf(": %s\n", sc->sc_pp->ide_name);
    349 	}
    350 
    351 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    352 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    353 		/*
    354 		 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    355 		 * and base adresses registers can be disabled at
    356 		 * hardware level. In this case, the device is wired
    357 		 * in compat mode and its first channel is always enabled,
    358 		 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    359 		 * In fact, it seems that the first channel of the CMD PCI0640
    360 		 * can't be disabled.
    361 		 */
    362 #ifndef PCIIDE_CMD064x_DISABLE
    363 		if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
    364 #else
    365 		if (1) {
    366 #endif
    367 			printf("%s: device disabled (at %s)\n",
    368 		 	   sc->sc_wdcdev.sc_dev.dv_xname,
    369 		  	  (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    370 			  "device" : "bridge");
    371 			return;
    372 		}
    373 	}
    374 
    375 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    376 	interface = PCI_INTERFACE(class);
    377 
    378 	/*
    379 	 * Set up PCI interrupt only if at last one channel is in native mode.
    380 	 * At last one device (CMD PCI0640) has a default value of 14, which
    381 	 * will be mapped even if both channels are in compat-only mode.
    382 	 */
    383 	if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1))) {
    384 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    385 		    pa->pa_intrline, &intrhandle) != 0) {
    386 			printf("%s: couldn't map native-PCI interrupt\n",
    387 			    sc->sc_wdcdev.sc_dev.dv_xname);
    388 		} else {
    389 			intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    390 			sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    391 			    intrhandle, IPL_BIO, pciide_pci_intr, sc);
    392 			if (sc->sc_pci_ih != NULL) {
    393 				printf("%s: using %s for native-PCI "
    394 				    "interrupt\n",
    395 				    sc->sc_wdcdev.sc_dev.dv_xname,
    396 				    intrstr ? intrstr : "unknown interrupt");
    397 			} else {
    398 				printf("%s: couldn't establish native-PCI "
    399 				    "interrupt",
    400 				    sc->sc_wdcdev.sc_dev.dv_xname);
    401 				if (intrstr != NULL)
    402 					printf(" at %s", intrstr);
    403 				printf("\n");
    404 			}
    405 		}
    406 	}
    407 
    408 	/*
    409 	 * Map DMA registers, if DMA is supported.
    410 	 *
    411 	 * Note that sc_dma_ok is the right variable to test to see if
    412 	 * DMA can be done.  If the interface doesn't support DMA,
    413 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    414 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    415 	 * non-zero if the interface supports DMA and the registers
    416 	 * could be mapped.
    417 	 *
    418 	 * XXX Note that despite the fact that the Bus Master IDE specs
    419 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    420 	 * XXX space," some controllers (at least the United
    421 	 * XXX Microelectronics UM8886BF) place it in memory space.
    422 	 * XXX eventually, we should probably read the register and check
    423 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    424 	 */
    425 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    426 		printf("%s: bus-master DMA support present",
    427 		    sc->sc_wdcdev.sc_dev.dv_xname);
    428 		if (sc->sc_pp == &default_product_desc) {
    429 			printf(", but unused (no driver support)");
    430 			sc->sc_dma_ok = 0;
    431 		} else {
    432 			sc->sc_dma_ok = (pci_mapreg_map(pa,
    433 			    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    434 			    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    435 			sc->sc_dmat = pa->pa_dmat;
    436 			if (sc->sc_dma_ok == 0) {
    437 				printf(", but unused (couldn't map registers)");
    438 			} else {
    439 				sc->sc_wdcdev.dma_arg = sc;
    440 				sc->sc_wdcdev.dma_init = pciide_dma_init;
    441 				sc->sc_wdcdev.dma_start = pciide_dma_start;
    442 				sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    443 			}
    444 		}
    445 		printf("\n");
    446 	}
    447 	sc->sc_pp->setup_cap(sc);
    448 	sc->sc_wdcdev.channels = sc->wdc_channels;
    449 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    450 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    451 
    452 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    453 		cp = &sc->pciide_channels[i];
    454 
    455 		sc->wdc_channels[i].channel = i;
    456 		sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
    457 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    458 		    sc->wdc_channels[i].ch_queue =
    459 			sc->wdc_channels[0].ch_queue;
    460 		} else {
    461 		    sc->wdc_channels[i].ch_queue =
    462 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    463 			M_NOWAIT);
    464 		}
    465 		if (sc->wdc_channels[i].ch_queue == NULL) {
    466 		    printf("%s %s channel: "
    467 			"can't allocate memory for command queue",
    468 			sc->sc_wdcdev.sc_dev.dv_xname,
    469 			PCIIDE_CHANNEL_NAME(i));
    470 			continue;
    471 		}
    472 		printf("%s: %s channel %s to %s mode\n",
    473 		    sc->sc_wdcdev.sc_dev.dv_xname,
    474 		    PCIIDE_CHANNEL_NAME(i),
    475 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    476 		      "configured" : "wired",
    477 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    478 		      "compatibility");
    479 
    480 		/*
    481 		 * pciide_map_channel_native() and pciide_map_channel_compat()
    482 		 * will also call wdcattach. Eventually the channel will be
    483 		 * disabled if there's no drive present
    484 		 */
    485 		if (interface & PCIIDE_INTERFACE_PCI(i))
    486 			cp->hw_ok = pciide_map_channel_native(sc, pa, i);
    487 		else
    488 			cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
    489 
    490 	}
    491 	sc->sc_pp->setup_chip(sc, pc, tag);
    492 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    493 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    494 }
    495 
    496 int
    497 pciide_map_channel_compat(sc, pa, chan)
    498 	struct pciide_softc *sc;
    499 	struct pci_attach_args *pa;
    500 	int chan;
    501 {
    502 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    503 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    504 	const char *probe_fail_reason;
    505 	int rv = 1;
    506 
    507 	cp->compat = 1;
    508 
    509 	wdc_cp->cmd_iot = pa->pa_iot;
    510 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
    511 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    512 		printf("%s: couldn't map %s channel cmd regs\n",
    513 		    sc->sc_wdcdev.sc_dev.dv_xname,
    514 		    PCIIDE_CHANNEL_NAME(chan));
    515 		rv = 0;
    516 	}
    517 
    518 	wdc_cp->ctl_iot = pa->pa_iot;
    519 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
    520 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    521 		printf("%s: couldn't map %s channel ctl regs\n",
    522 		    sc->sc_wdcdev.sc_dev.dv_xname,
    523 		    PCIIDE_CHANNEL_NAME(chan));
    524 		rv = 0;
    525 	}
    526 
    527 	/*
    528 	 * If we weren't able to map the device successfully,
    529 	 * we just give up now.  Something else has already
    530 	 * occupied those ports, indicating that the device has
    531 	 * (probably) been completely disabled (by some nonstandard
    532 	 * mechanism).
    533 	 *
    534 	 * XXX If we successfully map some ports, but not others,
    535 	 * XXX it might make sense to unmap the ones that we mapped.
    536 	 */
    537 	if (rv == 0)
    538 		goto out;
    539 
    540 	/*
    541 	 * If we were able to map the device successfully, check if
    542 	 * the channel is enabled. For "known" device, a chip-specific
    543 	 * routine will be used (which read the rigth PCI register).
    544 	 * For unknow device, a generic routine using "standart" wdc probe
    545 	 * will try to guess it.
    546 	 *
    547 	 * If the channel has been disabled, other devices are free to use
    548 	 * its ports.
    549 	 */
    550 	probe_fail_reason = sc->sc_pp->channel_probe(sc, pa, chan);
    551 	if (probe_fail_reason != NULL) {
    552 		printf("%s: %s channel ignored (%s)\n",
    553 		    sc->sc_wdcdev.sc_dev.dv_xname,
    554 		    PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
    555 		rv = 0;
    556 
    557 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    558 		    PCIIDE_COMPAT_CMD_SIZE);
    559 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    560 		    PCIIDE_COMPAT_CTL_SIZE);
    561 
    562 		goto out;
    563 	}
    564 	wdc_cp->data32iot = wdc_cp->cmd_iot;
    565 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
    566 	wdcattach(&sc->wdc_channels[chan]);
    567 	/*
    568 	 * If drive not present, try to disable the channel and
    569 	 * free the resources.
    570 	 */
    571 	if ((sc->wdc_channels[chan].ch_drive[0].drive_flags & DRIVE) == 0 &&
    572 	    (sc->wdc_channels[chan].ch_drive[1].drive_flags & DRIVE) == 0) {
    573 		if (sc->sc_pp->channel_disable(sc, pa, chan)) {
    574 			printf("%s: disabling %s channel (no drives)\n",
    575 			    sc->sc_wdcdev.sc_dev.dv_xname,
    576 			    PCIIDE_CHANNEL_NAME(chan));
    577 			bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    578 			    PCIIDE_COMPAT_CMD_SIZE);
    579 			bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    580 			    PCIIDE_COMPAT_CTL_SIZE);
    581 			rv = 0;
    582 			goto out;
    583 		}
    584 	}
    585 
    586 	/*
    587 	 * If we're here, we were able to map the device successfully
    588 	 * and it really looks like there's a controller there.
    589 	 *
    590 	 * Unless those conditions are true, we don't map the
    591 	 * compatibility interrupt.  The spec indicates that if a
    592 	 * channel is configured for compatibility mode and the PCI
    593 	 * device's I/O space is enabled, the channel will be enabled.
    594 	 * Hoewver, some devices seem to be able to disable invididual
    595 	 * compatibility channels (via non-standard mechanisms).  If
    596 	 * the channel is disabled, the interrupt line can (probably)
    597 	 * be used by other devices (and may be assigned to other
    598 	 * devices by the BIOS).  If we mapped the interrupt we might
    599 	 * conflict with another interrupt assignment.
    600 	 */
    601 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    602 	    pa, chan, pciide_compat_intr, wdc_cp);
    603 	if (cp->ih == NULL) {
    604 		printf("%s: no compatibility interrupt for use by %s channel\n",
    605 		    sc->sc_wdcdev.sc_dev.dv_xname,
    606 		    PCIIDE_CHANNEL_NAME(chan));
    607 		rv = 0;
    608 	}
    609 
    610 out:
    611 	return (rv);
    612 }
    613 
    614 int
    615 pciide_map_channel_native(sc, pa, chan)
    616 	struct pciide_softc *sc;
    617 	struct pci_attach_args *pa;
    618 	int chan;
    619 {
    620 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    621 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    622 	int rv = 1;
    623 
    624 	cp->compat = 0;
    625 
    626 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
    627 	    0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
    628 		printf("%s: couldn't map %s channel cmd regs\n",
    629 		    sc->sc_wdcdev.sc_dev.dv_xname,
    630 		    PCIIDE_CHANNEL_NAME(chan));
    631 		rv = 0;
    632 	}
    633 
    634 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
    635 	    0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
    636 		printf("%s: couldn't map %s channel ctl regs\n",
    637 		    sc->sc_wdcdev.sc_dev.dv_xname,
    638 		    PCIIDE_CHANNEL_NAME(chan));
    639 		rv = 0;
    640 	}
    641 
    642 	if ((cp->ih = sc->sc_pci_ih) == NULL) {
    643 		printf("%s: no native-PCI interrupt for use by %s channel\n",
    644 		    sc->sc_wdcdev.sc_dev.dv_xname,
    645 		    PCIIDE_CHANNEL_NAME(chan));
    646 		rv = 0;
    647 	}
    648 	wdc_cp->data32iot = wdc_cp->cmd_iot;
    649 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
    650 	if (rv) {
    651 		wdcattach(&sc->wdc_channels[chan]);
    652 		/*
    653 		 * If drive not present, try to disable the channel and
    654 		 * free the resources.
    655 		 */
    656 		/* XXX How do we unmap regs mapped with pci_mapreg_map ?*/
    657 #if 0
    658 		if ((sc->wdc_channels[chan].ch_drive[0].drive_flags & DRIVE)
    659 		    == 0 &&
    660 		    (sc->wdc_channels[chan].ch_drive[1].drive_flags & DRIVE)
    661 		    == 0) {
    662 			if (sc->sc_pp->channel_disable(sc, pa, chan)) {
    663 				printf("%s: disabling %s channel (no drives)\n",
    664 				    sc->sc_wdcdev.sc_dev.dv_xname,
    665 				    PCIIDE_CHANNEL_NAME(chan));
    666 				pci_mapreg_map(xxx);
    667 				rv = 0;
    668 			}
    669 		}
    670 #endif
    671 	}
    672 	return (rv);
    673 }
    674 
    675 int
    676 pciide_compat_intr(arg)
    677 	void *arg;
    678 {
    679 	struct channel_softc *wdc_cp = arg;
    680 
    681 #ifdef DIAGNOSTIC
    682 	struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
    683 	struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
    684 	/* should only be called for a compat channel */
    685 	if (cp->compat == 0)
    686 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    687 #endif
    688 	return (wdcintr(wdc_cp));
    689 }
    690 
    691 int
    692 pciide_pci_intr(arg)
    693 	void *arg;
    694 {
    695 	struct pciide_softc *sc = arg;
    696 	struct pciide_channel *cp;
    697 	struct channel_softc *wdc_cp;
    698 	int i, rv, crv;
    699 
    700 	rv = 0;
    701 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    702 		cp = &sc->pciide_channels[i];
    703 		wdc_cp = &sc->wdc_channels[i];
    704 
    705 		/* If a compat channel skip. */
    706 		if (cp->compat)
    707 			continue;
    708 		/* if this channel not waiting for intr, skip */
    709 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    710 			continue;
    711 
    712 		crv = wdcintr(wdc_cp);
    713 		if (crv == 0)
    714 			;		/* leave rv alone */
    715 		else if (crv == 1)
    716 			rv = 1;		/* claim the intr */
    717 		else if (rv == 0)	/* crv should be -1 in this case */
    718 			rv = crv;	/* if we've done no better, take it */
    719 	}
    720 	return (rv);
    721 }
    722 
    723 void
    724 default_setup_cap(sc)
    725 	struct pciide_softc *sc;
    726 {
    727 	sc->sc_wdcdev.pio_mode = 0;
    728 	sc->sc_wdcdev.dma_mode = 0;
    729 }
    730 
    731 void
    732 default_setup_chip(sc, pc, tag)
    733 	struct pciide_softc *sc;
    734 	pci_chipset_tag_t pc;
    735 	pcitag_t tag;
    736 {
    737 	int channel, drive, idedma_ctl;
    738 	struct channel_softc *chp;
    739 	struct ata_drive_datas *drvp;
    740 
    741 	if (sc->sc_dma_ok == 0)
    742 		return; /* nothing to do */
    743 
    744 	/* Allocate DMA maps */
    745 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    746 		idedma_ctl = 0;
    747 		chp = &sc->wdc_channels[channel];
    748 		for (drive = 0; drive < 2; drive++) {
    749 			drvp = &chp->ch_drive[drive];
    750 			/* If no drive, skip */
    751 			if ((drvp->drive_flags & DRIVE) == 0)
    752 				continue;
    753 			if ((drvp->drive_flags & DRIVE_DMA) == 0)
    754 				continue;
    755 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    756 				/* Abort DMA setup */
    757 				printf("%s:%d:%d: can't allocate DMA maps, "
    758 				    "using PIO transferts\n",
    759 				    sc->sc_wdcdev.sc_dev.dv_xname,
    760 				    channel, drive);
    761 				drvp->drive_flags &= ~DRIVE_DMA;
    762 			}
    763 			printf("%s:%d:%d: using DMA mode %d\n",
    764 			    sc->sc_wdcdev.sc_dev.dv_xname,
    765 			    channel, drive,
    766 			    drvp->DMA_mode);
    767 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    768 		}
    769 		if (idedma_ctl != 0) {
    770 			/* Add software bits in status register */
    771 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    772 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    773 			    idedma_ctl);
    774 		}
    775 	}
    776 
    777 }
    778 
    779 const char *
    780 default_channel_probe(sc, pa, chan)
    781 	struct pciide_softc *sc;
    782 	struct pci_attach_args *pa;
    783 {
    784 	pcireg_t csr;
    785 	const char *failreason = NULL;
    786 
    787 	/*
    788 	 * Check to see if something appears to be there.
    789 	 */
    790 	if (!wdcprobe(&sc->wdc_channels[chan])) {
    791 		failreason = "not responding; disabled or no drives?";
    792 		goto out;
    793 	}
    794 
    795 	/*
    796 	 * Now, make sure it's actually attributable to this PCI IDE
    797 	 * channel by trying to access the channel again while the
    798 	 * PCI IDE controller's I/O space is disabled.  (If the
    799 	 * channel no longer appears to be there, it belongs to
    800 	 * this controller.)  YUCK!
    801 	 */
    802 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    803 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    804 	    csr & ~PCI_COMMAND_IO_ENABLE);
    805 	if (wdcprobe(&sc->wdc_channels[chan]))
    806 		failreason = "other hardware responding at addresses";
    807 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    808 
    809 out:
    810 	return (failreason);
    811 }
    812 
    813 int
    814 default_channel_disable(sc, pa, chan)
    815 	struct pciide_softc *sc;
    816 	struct pci_attach_args *pa;
    817 {
    818 	/* don't know how to disable a channel */
    819 	return 0;
    820 }
    821 
    822 void
    823 piix_setup_cap(sc)
    824 	struct pciide_softc *sc;
    825 {
    826 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
    827 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    828 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
    829 	    WDC_CAPABILITY_DMA;
    830 	sc->sc_wdcdev.pio_mode = 4;
    831 	sc->sc_wdcdev.dma_mode = 2;
    832 }
    833 
    834 void
    835 piix_setup_chip(sc, pc, tag)
    836 	struct pciide_softc *sc;
    837 	pci_chipset_tag_t pc;
    838 	pcitag_t tag;
    839 {
    840 	struct channel_softc *chp;
    841 	u_int8_t mode[2];
    842 	u_int8_t channel, drive;
    843 	u_int32_t oidetim, idetim, sidetim, idedma_ctl;
    844 	struct ata_drive_datas *drvp;
    845 
    846 	oidetim = pci_conf_read(pc, tag, PIIX_IDETIM);
    847 	idetim = sidetim = 0;
    848 
    849 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    850 	    oidetim,
    851 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    852 
    853 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    854 		chp = &sc->wdc_channels[channel];
    855 		drvp = chp->ch_drive;
    856 		idedma_ctl = 0;
    857 		/* If channel disabled, no need to go further */
    858 		if ((PIIX_IDETIM_READ(oidetim, channel) & PIIX_IDETIM_IDE) == 0)
    859 			continue;
    860 		/* set up new idetim: Enable IDE registers decode */
    861 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    862 		    channel);
    863 
    864 		/* setup DMA if needed */
    865 		for (drive = 0; drive < 2; drive++) {
    866 			if (drvp[drive].drive_flags & DRIVE_DMA &&
    867 			    pciide_dma_table_setup(sc, channel, drive) != 0) {
    868 				drvp[drive].drive_flags &= ~DRIVE_DMA;
    869 			}
    870 		}
    871 
    872 		/*
    873 		 * Here we have to mess up with drives mode: PIIX can't have
    874 		 * different timings for master and slave drives.
    875 		 * We need to find the best combination.
    876 		 */
    877 
    878 		/* If both drives supports DMA, takes the lower mode */
    879 		if ((drvp[0].drive_flags & DRIVE_DMA) &&
    880 		    (drvp[1].drive_flags & DRIVE_DMA)) {
    881 			mode[0] = mode[1] =
    882 			    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
    883 			    drvp[0].DMA_mode = mode[0];
    884 			goto ok;
    885 		}
    886 		/*
    887 		 * If only one drive supports DMA, use its mode, and
    888 		 * put the other one in PIO mode 0 if mode not compatible
    889 		 */
    890 		if (drvp[0].drive_flags & DRIVE_DMA) {
    891 			mode[0] = drvp[0].DMA_mode;
    892 			mode[1] = drvp[1].PIO_mode;
    893 			if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
    894 			    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
    895 				mode[1] = 0;
    896 			goto ok;
    897 		}
    898 		if (drvp[1].drive_flags & DRIVE_DMA) {
    899 			mode[1] = drvp[1].DMA_mode;
    900 			mode[0] = drvp[0].PIO_mode;
    901 			if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
    902 			    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
    903 				mode[0] = 0;
    904 			goto ok;
    905 		}
    906 		/*
    907 		 * If both drives are not DMA, takes the lower mode, unless
    908 		 * one of them is PIO mode < 2
    909 		 */
    910 		if (drvp[0].PIO_mode < 2) {
    911 			mode[0] = 0;
    912 			mode[1] = drvp[1].PIO_mode;
    913 		} else if (drvp[1].PIO_mode < 2) {
    914 			mode[1] = 0;
    915 			mode[0] = drvp[0].PIO_mode;
    916 		} else {
    917 			mode[0] = mode[1] =
    918 			    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
    919 		}
    920 ok:		/* The modes are setup */
    921 		for (drive = 0; drive < 2; drive++) {
    922 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    923 				drvp[drive].DMA_mode = mode[drive];
    924 				idetim |= piix_setup_idetim_timings(
    925 				    mode[drive], 1, channel);
    926 				goto end;
    927 			} else
    928 				drvp[drive].PIO_mode = mode[drive];
    929 		}
    930 		/* If we are there, none of the drives are DMA */
    931 		if (mode[0] >= 2)
    932 			idetim |= piix_setup_idetim_timings(
    933 			    mode[0], 0, channel);
    934 		else
    935 			idetim |= piix_setup_idetim_timings(
    936 			    mode[1], 0, channel);
    937 end:		/*
    938 		 * timing mode is now set up in the controller. Enable
    939 		 * it per-drive
    940 		 */
    941 		for (drive = 0; drive < 2; drive++) {
    942 			/* If no drive, skip */
    943 			if ((drvp[drive].drive_flags & DRIVE) == 0)
    944 				continue;
    945 			idetim |= piix_setup_idetim_drvs(&drvp[drive]);
    946 			printf("%s(%s:%d:%d): using PIO mode %d",
    947 			    drvp[drive].drv_softc->dv_xname,
    948 			    sc->sc_wdcdev.sc_dev.dv_xname,
    949 			    channel, drive, drvp[drive].PIO_mode);
    950 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    951 				idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    952 				printf(", DMA mode %d", drvp[drive].DMA_mode);
    953 			}
    954 			printf("\n");
    955 		}
    956 		if (idedma_ctl != 0) {
    957 			/* Add software bits in status register */
    958 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    959 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    960 			    idedma_ctl);
    961 		}
    962 	}
    963 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
    964 	    idetim, sidetim), DEBUG_PROBE);
    965 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    966 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    967 }
    968 
    969 void
    970 piix3_4_setup_chip(sc, pc, tag)
    971 	struct pciide_softc *sc;
    972 	pci_chipset_tag_t pc;
    973 	pcitag_t tag;
    974 {
    975 	int channel, drive;
    976 	struct channel_softc *chp;
    977 	struct ata_drive_datas *drvp;
    978 	u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
    979 
    980 	idetim = sidetim = udmareg = 0;
    981 	oidetim = pci_conf_read(pc, tag, PIIX_IDETIM);
    982 
    983 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
    984 	    oidetim,
    985 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    986 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
    987 		WDCDEBUG_PRINT((", udamreg 0x%x",
    988 		    pci_conf_read(pc, tag, PIIX_UDMAREG)),
    989 		    DEBUG_PROBE);
    990 	}
    991 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    992 
    993 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    994 		chp = &sc->wdc_channels[channel];
    995 		idedma_ctl = 0;
    996 		/* If channel disabled, no need to go further */
    997 		if ((PIIX_IDETIM_READ(oidetim, channel) & PIIX_IDETIM_IDE) == 0)
    998 			continue;
    999 		/* set up new idetim: Enable IDE registers decode */
   1000 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
   1001 		    channel);
   1002 		for (drive = 0; drive < 2; drive++) {
   1003 			drvp = &chp->ch_drive[drive];
   1004 			/* If no drive, skip */
   1005 			if ((drvp->drive_flags & DRIVE) == 0)
   1006 				continue;
   1007 			/* add timing values, setup DMA if needed */
   1008 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1009 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
   1010 			    sc->sc_dma_ok == 0) {
   1011 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1012 				goto pio;
   1013 			}
   1014 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1015 				/* Abort DMA setup */
   1016 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1017 				goto pio;
   1018 			}
   1019 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1020 			    (drvp->drive_flags & DRIVE_UDMA)) {
   1021 				/* use Ultra/DMA */
   1022 				drvp->drive_flags &= ~DRIVE_DMA;
   1023 				udmareg |= PIIX_UDMACTL_DRV_EN(
   1024 				    channel, drive);
   1025 				udmareg |= PIIX_UDMATIM_SET(
   1026 				    piix4_sct_udma[drvp->UDMA_mode],
   1027 				    channel, drive);
   1028 			} else {
   1029 				/* use Multiword DMA */
   1030 				drvp->drive_flags &= ~DRIVE_UDMA;
   1031 				if (drive == 0) {
   1032 					idetim |= piix_setup_idetim_timings(
   1033 					    drvp->DMA_mode, 1, channel);
   1034 				} else {
   1035 					sidetim |= piix_setup_sidetim_timings(
   1036 						drvp->DMA_mode, 1, channel);
   1037 					idetim =PIIX_IDETIM_SET(idetim,
   1038 					    PIIX_IDETIM_SITRE, channel);
   1039 				}
   1040 			}
   1041 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1042 
   1043 pio:			/* use PIO mode */
   1044 			idetim |= piix_setup_idetim_drvs(drvp);
   1045 			if (drive == 0) {
   1046 				idetim |= piix_setup_idetim_timings(
   1047 				    drvp->PIO_mode, 0, channel);
   1048 			} else {
   1049 				sidetim |= piix_setup_sidetim_timings(
   1050 					drvp->PIO_mode, 0, channel);
   1051 				idetim =PIIX_IDETIM_SET(idetim,
   1052 				    PIIX_IDETIM_SITRE, channel);
   1053 			}
   1054 			printf("%s(%s:%d:%d): using PIO mode %d",
   1055 			    drvp->drv_softc->dv_xname,
   1056 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1057 			    channel, drive, drvp->PIO_mode);
   1058 			if (drvp->drive_flags & DRIVE_DMA)
   1059 			    printf(", DMA mode %d", drvp->DMA_mode);
   1060 			if (drvp->drive_flags & DRIVE_UDMA)
   1061 			    printf(", UDMA mode %d", drvp->UDMA_mode);
   1062 			printf("\n");
   1063 		}
   1064 		if (idedma_ctl != 0) {
   1065 			/* Add software bits in status register */
   1066 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1067 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1068 			    idedma_ctl);
   1069 		}
   1070 	}
   1071 
   1072 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
   1073 	    idetim, sidetim), DEBUG_PROBE);
   1074 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1075 		WDCDEBUG_PRINT((", udmareg=0x%x", udmareg), DEBUG_PROBE);
   1076 		pci_conf_write(pc, tag, PIIX_UDMAREG, udmareg);
   1077 	}
   1078 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1079 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
   1080 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
   1081 }
   1082 
   1083 /* setup ISP and RTC fields, based on mode */
   1084 static u_int32_t
   1085 piix_setup_idetim_timings(mode, dma, channel)
   1086 	u_int8_t mode;
   1087 	u_int8_t dma;
   1088 	u_int8_t channel;
   1089 {
   1090 
   1091 	if (dma)
   1092 		return PIIX_IDETIM_SET(0,
   1093 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1094 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1095 		    channel);
   1096 	else
   1097 		return PIIX_IDETIM_SET(0,
   1098 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1099 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1100 		    channel);
   1101 }
   1102 
   1103 /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1104 static u_int32_t
   1105 piix_setup_idetim_drvs(drvp)
   1106 	struct ata_drive_datas *drvp;
   1107 {
   1108 	u_int32_t ret = 0;
   1109 	struct channel_softc *chp = drvp->chnl_softc;
   1110 	u_int8_t channel = chp->channel;
   1111 	u_int8_t drive = drvp->drive;
   1112 
   1113 	/*
   1114 	 * If drive is using UDMA, timings setups are independant
   1115 	 * So just check DMA and PIO here.
   1116 	 */
   1117 	if (drvp->drive_flags & DRIVE_DMA) {
   1118 		/* if mode = DMA mode 0, use compatible timings */
   1119 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1120 		    drvp->DMA_mode == 0) {
   1121 			drvp->PIO_mode = 0;
   1122 			return ret;
   1123 		}
   1124 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1125 		/*
   1126 		 * PIO and DMA timings are the same, use fast timings for PIO
   1127 		 * too, else use compat timings.
   1128 		 */
   1129 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1130 		    piix_isp_dma[drvp->DMA_mode]) ||
   1131 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1132 		    piix_rtc_dma[drvp->DMA_mode]))
   1133 			drvp->PIO_mode = 0;
   1134 		/* if PIO mode <= 2, use compat timings for PIO */
   1135 		if (drvp->PIO_mode <= 2) {
   1136 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1137 			    channel);
   1138 			return ret;
   1139 		}
   1140 	}
   1141 
   1142 	/*
   1143 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1144 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1145 	 * if PIO mode >= 3.
   1146 	 */
   1147 
   1148 	if (drvp->PIO_mode < 2)
   1149 		return ret;
   1150 
   1151 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1152 	if (drvp->PIO_mode >= 3) {
   1153 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1154 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1155 	}
   1156 	return ret;
   1157 }
   1158 
   1159 /* setup values in SIDETIM registers, based on mode */
   1160 static u_int32_t
   1161 piix_setup_sidetim_timings(mode, dma, channel)
   1162 	u_int8_t mode;
   1163 	u_int8_t dma;
   1164 	u_int8_t channel;
   1165 {
   1166 	if (dma)
   1167 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1168 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1169 	else
   1170 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1171 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1172 }
   1173 
   1174 const char*
   1175 piix_channel_probe(sc, pa, chan)
   1176 	struct pciide_softc *sc;
   1177 	struct pci_attach_args *pa;
   1178 	int chan;
   1179 {
   1180 	u_int32_t idetim = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_IDETIM);
   1181 
   1182 	if (PIIX_IDETIM_READ(idetim, chan) & PIIX_IDETIM_IDE)
   1183 		return NULL;
   1184 	else
   1185 		return "disabled";
   1186 }
   1187 
   1188 int
   1189 piix_channel_disable(sc, pa, chan)
   1190 	struct pciide_softc *sc;
   1191 	struct pci_attach_args *pa;
   1192 {
   1193 	u_int32_t idetim = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_IDETIM);
   1194 	idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE, chan);
   1195 	pci_conf_write(pa->pa_pc, pa->pa_tag, PIIX_IDETIM, idetim);
   1196 	return 1;
   1197 }
   1198 
   1199 void
   1200 apollo_setup_cap(sc)
   1201 	struct pciide_softc *sc;
   1202 {
   1203 	if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
   1204 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1205 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1206 	    WDC_CAPABILITY_DMA;
   1207 	sc->sc_wdcdev.pio_mode = 4;
   1208 	sc->sc_wdcdev.dma_mode = 2;
   1209 
   1210 }
   1211 void
   1212 apollo_setup_chip(sc, pc, tag)
   1213 	struct pciide_softc *sc;
   1214 	pci_chipset_tag_t pc;
   1215 	pcitag_t tag;
   1216 {
   1217 	u_int32_t udmatim_reg, datatim_reg;
   1218 	u_int8_t idedma_ctl;
   1219 	int mode;
   1220 	int channel, drive;
   1221 	struct channel_softc *chp;
   1222 	struct ata_drive_datas *drvp;
   1223 
   1224 	WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
   1225 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1226 	    pci_conf_read(pc, tag, APO_IDECONF),
   1227 	    pci_conf_read(pc, tag, APO_CTLMISC),
   1228 	    pci_conf_read(pc, tag, APO_DATATIM),
   1229 	    pci_conf_read(pc, tag, APO_UDMA)),
   1230 	    DEBUG_PROBE);
   1231 
   1232 	datatim_reg = 0;
   1233 	udmatim_reg = 0;
   1234 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
   1235 		chp = &sc->wdc_channels[channel];
   1236 		idedma_ctl = 0;
   1237 		for (drive = 0; drive < 2; drive++) {
   1238 			drvp = &chp->ch_drive[drive];
   1239 			/* If no drive, skip */
   1240 			if ((drvp->drive_flags & DRIVE) == 0)
   1241 				continue;
   1242 			/* add timing values, setup DMA if needed */
   1243 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1244 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
   1245 			    sc->sc_dma_ok == 0) {
   1246 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1247 				mode = drvp->PIO_mode;
   1248 				goto pio;
   1249 			}
   1250 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1251 				/* Abort DMA setup */
   1252 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1253 				mode = drvp->PIO_mode;
   1254 				goto pio;
   1255 			}
   1256 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1257 			    (drvp->drive_flags & DRIVE_UDMA)) {
   1258 				/* use Ultra/DMA */
   1259 				drvp->drive_flags &= ~DRIVE_DMA;
   1260 				udmatim_reg |= APO_UDMA_EN(channel, drive) |
   1261 				    APO_UDMA_EN_MTH(channel, drive) |
   1262 				    APO_UDMA_TIME(channel, drive,
   1263 					apollo_udma_tim[drvp->UDMA_mode]);
   1264 				/* can use PIO timings, MW DMA unused */
   1265 				mode = drvp->PIO_mode;
   1266 			} else {
   1267 				/* use Multiword DMA */
   1268 				drvp->drive_flags &= ~DRIVE_UDMA;
   1269 				/* mode = min(pio, dma+2) */
   1270 				if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1271 					mode = drvp->PIO_mode;
   1272 				else
   1273 					mode = drvp->DMA_mode;
   1274 			}
   1275 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1276 
   1277 pio:			/* setup PIO mode */
   1278 			datatim_reg |=
   1279 			    APO_DATATIM_PULSE(channel, drive,
   1280 				apollo_pio_set[mode]) |
   1281 			    APO_DATATIM_RECOV(channel, drive,
   1282 				apollo_pio_rec[mode]);
   1283 			drvp->PIO_mode = mode;
   1284 			drvp->DMA_mode = mode + 2;
   1285 			printf("%s(%s:%d:%d): using PIO mode %d",
   1286 			    drvp->drv_softc->dv_xname,
   1287 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1288 			    channel, drive, drvp->PIO_mode);
   1289 			if (drvp[drive].drive_flags & DRIVE_DMA)
   1290 			    printf(", DMA mode %d", drvp->DMA_mode);
   1291 			if (drvp->drive_flags & DRIVE_UDMA)
   1292 			    printf(", UDMA mode %d", drvp->UDMA_mode);
   1293 			printf("\n");
   1294 		}
   1295 		if (idedma_ctl != 0) {
   1296 			/* Add software bits in status register */
   1297 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1298 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1299 			    idedma_ctl);
   1300 		}
   1301 	}
   1302 	WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1303 	    datatim_reg, udmatim_reg), DEBUG_PROBE);
   1304 	pci_conf_write(pc, tag, APO_DATATIM, datatim_reg);
   1305 	pci_conf_write(pc, tag, APO_UDMA, udmatim_reg);
   1306 }
   1307 
   1308 const char*
   1309 apollo_channel_probe(sc, pa, chan)
   1310 	struct pciide_softc *sc;
   1311 	struct pci_attach_args *pa;
   1312 	int chan;
   1313 {
   1314 
   1315 	u_int32_t ideconf = pci_conf_read(pa->pa_pc, pa->pa_tag, APO_IDECONF);
   1316 
   1317 	if (ideconf & APO_IDECONF_EN(chan))
   1318 		return NULL;
   1319 	else
   1320 		return "disabled";
   1321 
   1322 }
   1323 
   1324 int
   1325 apollo_channel_disable(sc, pa, chan)
   1326 	struct pciide_softc *sc;
   1327 	struct pci_attach_args *pa;
   1328 {
   1329 	u_int32_t ideconf = pci_conf_read(pa->pa_pc, pa->pa_tag, APO_IDECONF);
   1330 	ideconf &= ~APO_IDECONF_EN(chan);
   1331 	pci_conf_write(pa->pa_pc, pa->pa_tag, APO_IDECONF, ideconf);
   1332 	return 1;
   1333 }
   1334 
   1335 const char*
   1336 cmd_channel_probe(sc, pa, chan)
   1337 	struct pciide_softc *sc;
   1338 	struct pci_attach_args *pa;
   1339 	int chan;
   1340 {
   1341 
   1342 	/*
   1343 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   1344 	 * there's no way to disable the first channel without disabling
   1345 	 * the whole device
   1346 	 */
   1347 	if (chan == 0)
   1348 		return NULL;
   1349 
   1350 	/* Second channel is enabled if CMD_CONF_2PORT is set */
   1351 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, CMD_CONF_CTRL0) &
   1352 	    CMD_CONF_2PORT) == 0)
   1353 		return "disabled";
   1354 
   1355 	return NULL;
   1356 }
   1357 
   1358 int
   1359 cmd_channel_disable(sc, pa, chan)
   1360 	struct pciide_softc *sc;
   1361 	struct pci_attach_args *pa;
   1362 {
   1363 	u_int32_t ctrl0;
   1364 	/* with a CMD PCI64x, the first channel is always enabled */
   1365 	if (chan == 0)
   1366 		return 0;
   1367 	ctrl0 = pci_conf_read(pa->pa_pc, pa->pa_tag, CMD_CONF_CTRL0);
   1368 	ctrl0 &= ~CMD_CONF_2PORT;
   1369 	pci_conf_write(pa->pa_pc, pa->pa_tag, CMD_CONF_CTRL0, ctrl0);
   1370 	return 1;
   1371 }
   1372 
   1373 int
   1374 pciide_dma_table_setup(sc, channel, drive)
   1375 	struct pciide_softc *sc;
   1376 	int channel, drive;
   1377 {
   1378 	bus_dma_segment_t seg;
   1379 	int error, rseg;
   1380 	const bus_size_t dma_table_size =
   1381 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
   1382 	struct pciide_dma_maps *dma_maps =
   1383 	    &sc->pciide_channels[channel].dma_maps[drive];
   1384 
   1385 	/* Allocate memory for the DMA tables and map it */
   1386 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
   1387 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
   1388 	    BUS_DMA_NOWAIT)) != 0) {
   1389 		printf("%s:%d: unable to allocate table DMA for "
   1390 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1391 		    channel, drive, error);
   1392 		return error;
   1393 	}
   1394 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1395 	    dma_table_size,
   1396 	    (caddr_t *)&dma_maps->dma_table,
   1397 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1398 		printf("%s:%d: unable to map table DMA for"
   1399 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1400 		    channel, drive, error);
   1401 		return error;
   1402 	}
   1403 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
   1404 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
   1405 	    seg.ds_addr), DEBUG_PROBE);
   1406 
   1407 	/* Create and load table DMA map for this disk */
   1408 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
   1409 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
   1410 	    &dma_maps->dmamap_table)) != 0) {
   1411 		printf("%s:%d: unable to create table DMA map for "
   1412 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1413 		    channel, drive, error);
   1414 		return error;
   1415 	}
   1416 	if ((error = bus_dmamap_load(sc->sc_dmat,
   1417 	    dma_maps->dmamap_table,
   1418 	    dma_maps->dma_table,
   1419 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1420 		printf("%s:%d: unable to load table DMA map for "
   1421 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1422 		    channel, drive, error);
   1423 		return error;
   1424 	}
   1425 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
   1426 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
   1427 	/* Create a xfer DMA map for this drive */
   1428 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
   1429 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
   1430 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1431 	    &dma_maps->dmamap_xfer)) != 0) {
   1432 		printf("%s:%d: unable to create xfer DMA map for "
   1433 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1434 		    channel, drive, error);
   1435 		return error;
   1436 	}
   1437 	return 0;
   1438 }
   1439 
   1440 int
   1441 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
   1442 	void *v;
   1443 	int channel, drive;
   1444 	void *databuf;
   1445 	size_t datalen;
   1446 	int flags;
   1447 {
   1448 	struct pciide_softc *sc = v;
   1449 	int error, seg;
   1450 	struct pciide_dma_maps *dma_maps =
   1451 	    &sc->pciide_channels[channel].dma_maps[drive];
   1452 
   1453 	error = bus_dmamap_load(sc->sc_dmat,
   1454 	    dma_maps->dmamap_xfer,
   1455 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
   1456 	if (error) {
   1457 		printf("%s:%d: unable to load xfer DMA map for"
   1458 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1459 		    channel, drive, error);
   1460 		return error;
   1461 	}
   1462 
   1463 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1464 	    dma_maps->dmamap_xfer->dm_mapsize,
   1465 	    (flags & WDC_DMA_READ) ?
   1466 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1467 
   1468 	WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
   1469 	    dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
   1470 	    vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
   1471 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
   1472 #ifdef DIAGNOSTIC
   1473 		/* A segment must not cross a 64k boundary */
   1474 		{
   1475 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1476 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
   1477 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
   1478 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
   1479 			printf("pciide_dma: segment %d physical addr 0x%lx"
   1480 			    " len 0x%lx not properly aligned\n",
   1481 			    seg, phys, len);
   1482 			panic("pciide_dma: buf align");
   1483 		}
   1484 		}
   1485 #endif
   1486 		dma_maps->dma_table[seg].base_addr =
   1487 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1488 		dma_maps->dma_table[seg].byte_count =
   1489 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
   1490 		    IDEDMA_BYTE_COUNT_MASK;
   1491 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
   1492 		   seg, dma_maps->dma_table[seg].byte_count,
   1493 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
   1494 
   1495 	}
   1496 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
   1497 		IDEDMA_BYTE_COUNT_EOT;
   1498 
   1499 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
   1500 	    dma_maps->dmamap_table->dm_mapsize,
   1501 	    BUS_DMASYNC_PREWRITE);
   1502 
   1503 	/* Maps are ready. Start DMA function */
   1504 #ifdef DIAGNOSTIC
   1505 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
   1506 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
   1507 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1508 		panic("pciide_dma_init: table align");
   1509 	}
   1510 #endif
   1511 
   1512 	WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
   1513 	    "phys 0x%x)\n",
   1514 	    dma_maps->dma_table,
   1515 	    dma_maps->dmamap_table->dm_segs[0].ds_addr,
   1516 	    dma_maps->dmamap_table->dm_segs[0].ds_len,
   1517 	    dma_maps->dmamap_table->dm_nsegs,
   1518 	    vtophys(dma_maps->dma_table)), DEBUG_DMA);
   1519 	/* Clear status bits */
   1520 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1521 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1522 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1523 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
   1524 	/* Write table addr */
   1525 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   1526 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
   1527 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1528 	/* set read/write */
   1529 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1530 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1531 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
   1532 	return 0;
   1533 }
   1534 
   1535 void
   1536 pciide_dma_start(v, channel, drive, flags)
   1537 	void *v;
   1538 	int channel, drive, flags;
   1539 {
   1540 	struct pciide_softc *sc = v;
   1541 
   1542 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
   1543 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1544 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1545 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1546 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
   1547 }
   1548 
   1549 int
   1550 pciide_dma_finish(v, channel, drive, flags)
   1551 	void *v;
   1552 	int channel, drive;
   1553 	int flags;
   1554 {
   1555 	struct pciide_softc *sc = v;
   1556 	u_int8_t status;
   1557 	struct pciide_dma_maps *dma_maps =
   1558 	    &sc->pciide_channels[channel].dma_maps[drive];
   1559 
   1560 	/* Unload the map of the data buffer */
   1561 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1562 	    dma_maps->dmamap_xfer->dm_mapsize,
   1563 	    (flags & WDC_DMA_READ) ?
   1564 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1565 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
   1566 
   1567 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1568 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
   1569 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
   1570 	    DEBUG_XFERS);
   1571 
   1572 	/* stop DMA channel */
   1573 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1574 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1575 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1576 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
   1577 
   1578 	/* Clear status bits */
   1579 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1580 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1581 	    status);
   1582 
   1583 	if ((status & IDEDMA_CTL_ERR) != 0) {
   1584 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
   1585 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
   1586 		return -1;
   1587 	}
   1588 
   1589 	if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
   1590 		printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
   1591 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
   1592 		    drive, status);
   1593 		return -1;
   1594 	}
   1595 
   1596 	if ((status & IDEDMA_CTL_ACT) != 0) {
   1597 		/* data underrun, may be a valid condition for ATAPI */
   1598 		return 1;
   1599 	}
   1600 
   1601 	return 0;
   1602 }
   1603