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