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