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