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