Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.33.2.8
      1 /*	$NetBSD: pciide.c,v 1.33.2.8 2000/07/27 17:36:40 he Exp $	*/
      2 
      3 
      4 /*
      5  * Copyright (c) 1999 Manuel Bouyer.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  */
     35 
     36 
     37 /*
     38  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *      This product includes software developed by Christopher G. Demetriou
     51  *	for the NetBSD Project.
     52  * 4. The name of the author may not be used to endorse or promote products
     53  *    derived from this software without specific prior written permission
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     57  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     58  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     59  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     60  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     64  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 /*
     68  * PCI IDE controller driver.
     69  *
     70  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     71  * sys/dev/pci/ppb.c, revision 1.16).
     72  *
     73  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     74  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     75  * 5/16/94" from the PCI SIG.
     76  *
     77  */
     78 
     79 #ifndef WDCDEBUG
     80 #define WDCDEBUG
     81 #endif
     82 
     83 #define DEBUG_DMA   0x01
     84 #define DEBUG_XFERS  0x02
     85 #define DEBUG_FUNCS  0x08
     86 #define DEBUG_PROBE  0x10
     87 #ifdef WDCDEBUG
     88 int wdcdebug_pciide_mask = 0;
     89 #define WDCDEBUG_PRINT(args, level) \
     90 	if (wdcdebug_pciide_mask & (level)) printf args
     91 #else
     92 #define WDCDEBUG_PRINT(args, level)
     93 #endif
     94 #include <sys/param.h>
     95 #include <sys/systm.h>
     96 #include <sys/device.h>
     97 #include <sys/malloc.h>
     98 
     99 #include <machine/endian.h>
    100 
    101 #include <vm/vm.h>
    102 #include <vm/vm_param.h>
    103 #include <vm/vm_kern.h>
    104 
    105 #include <dev/pci/pcireg.h>
    106 #include <dev/pci/pcivar.h>
    107 #include <dev/pci/pcidevs.h>
    108 #include <dev/pci/pciidereg.h>
    109 #include <dev/pci/pciidevar.h>
    110 #include <dev/pci/pciide_piix_reg.h>
    111 #include <dev/pci/pciide_amd_reg.h>
    112 #include <dev/pci/pciide_apollo_reg.h>
    113 #include <dev/pci/pciide_cmd_reg.h>
    114 #include <dev/pci/pciide_cy693_reg.h>
    115 #include <dev/pci/pciide_sis_reg.h>
    116 #include <dev/pci/pciide_acer_reg.h>
    117 #include <dev/pci/pciide_pdc202xx_reg.h>
    118 #include <dev/pci/pciide_opti_reg.h>
    119 #include <dev/pci/pciide_hpt_reg.h>
    120 
    121 #if BYTE_ORDER == BIG_ENDIAN
    122 #define htole16(x)      bswap16((u_int16_t)(x))
    123 #define htole32(x)      bswap32((u_int32_t)(x))
    124 #define htole64(x)      bswap64((u_int64_t)(x))
    125 #else   /* LITTLE_ENDIAN */
    126 #define htole16(x)      (x)
    127 #define htole32(x)      (x)
    128 #define htole64(x)      (x)
    129 #endif
    130 #define le16toh(x)      htole16(x)
    131 #define le32toh(x)      htole32(x)
    132 #define le64toh(x)      htole64(x)
    133 
    134 /* inlines for reading/writing 8-bit PCI registers */
    135 static __inline u_int8_t pciide_pci_read __P((pci_chipset_tag_t, pcitag_t,
    136 					      int));
    137 static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
    138 					   int, u_int8_t));
    139 
    140 static __inline u_int8_t
    141 pciide_pci_read(pc, pa, reg)
    142 	pci_chipset_tag_t pc;
    143 	pcitag_t pa;
    144 	int reg;
    145 {
    146 
    147 	return (pci_conf_read(pc, pa, (reg & ~0x03)) >>
    148 	    ((reg & 0x03) * 8) & 0xff);
    149 }
    150 
    151 static __inline void
    152 pciide_pci_write(pc, pa, reg, val)
    153 	pci_chipset_tag_t pc;
    154 	pcitag_t pa;
    155 	int reg;
    156 	u_int8_t val;
    157 {
    158 	pcireg_t pcival;
    159 
    160 	pcival = pci_conf_read(pc, pa, (reg & ~0x03));
    161 	pcival &= ~(0xff << ((reg & 0x03) * 8));
    162 	pcival |= (val << ((reg & 0x03) * 8));
    163 	pci_conf_write(pc, pa, (reg & ~0x03), pcival);
    164 }
    165 
    166 void default_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    167 
    168 void piix_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    169 void piix_setup_channel __P((struct channel_softc*));
    170 void piix3_4_setup_channel __P((struct channel_softc*));
    171 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    172 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    173 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    174 
    175 void amd756_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    176 void amd756_setup_channel __P((struct channel_softc*));
    177 
    178 void apollo_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    179 void apollo_setup_channel __P((struct channel_softc*));
    180 
    181 void cmd_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    182 void cmd0643_9_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    183 void cmd0643_9_setup_channel __P((struct channel_softc*));
    184 void cmd_channel_map __P((struct pci_attach_args *,
    185 			struct pciide_softc *, int));
    186 int  cmd_pci_intr __P((void *));
    187 void cmd646_9_irqack __P((struct channel_softc *));
    188 
    189 void cy693_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    190 void cy693_setup_channel __P((struct channel_softc*));
    191 
    192 void sis_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    193 void sis_setup_channel __P((struct channel_softc*));
    194 
    195 void acer_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    196 void acer_setup_channel __P((struct channel_softc*));
    197 int  acer_pci_intr __P((void *));
    198 
    199 void pdc202xx_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    200 void pdc202xx_setup_channel __P((struct channel_softc*));
    201 int  pdc202xx_pci_intr __P((void *));
    202 
    203 void opti_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    204 void opti_setup_channel __P((struct channel_softc*));
    205 
    206 void hpt_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
    207 void hpt_setup_channel __P((struct channel_softc*));
    208 int  hpt_pci_intr __P((void *));
    209 
    210 void pciide_channel_dma_setup __P((struct pciide_channel *));
    211 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    212 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    213 void pciide_dma_start __P((void*, int, int));
    214 int  pciide_dma_finish __P((void*, int, int, int));
    215 void pciide_irqack __P((struct channel_softc *));
    216 void pciide_print_modes __P((struct pciide_channel *));
    217 
    218 struct pciide_product_desc {
    219 	u_int32_t ide_product;
    220 	int ide_flags;
    221 	const char *ide_name;
    222 	/* map and setup chip, probe drives */
    223 	void (*chip_map) __P((struct pciide_softc*, struct pci_attach_args*));
    224 };
    225 
    226 /* Flags for ide_flags */
    227 #define IDE_PCI_CLASS_OVERRIDE 0x0001 /* accept even if class != pciide */
    228 
    229 /* Default product description for devices not known from this controller */
    230 const struct pciide_product_desc default_product_desc = {
    231 	0,
    232 	0,
    233 	"Generic PCI IDE controller",
    234 	default_chip_map,
    235 };
    236 
    237 const struct pciide_product_desc pciide_intel_products[] =  {
    238 	{ PCI_PRODUCT_INTEL_82092AA,
    239 	  0,
    240 	  "Intel 82092AA IDE controller",
    241 	  default_chip_map,
    242 	},
    243 	{ PCI_PRODUCT_INTEL_82371FB_IDE,
    244 	  0,
    245 	  "Intel 82371FB IDE controller (PIIX)",
    246 	  piix_chip_map,
    247 	},
    248 	{ PCI_PRODUCT_INTEL_82371SB_IDE,
    249 	  0,
    250 	  "Intel 82371SB IDE Interface (PIIX3)",
    251 	  piix_chip_map,
    252 	},
    253 	{ PCI_PRODUCT_INTEL_82371AB_IDE,
    254 	  0,
    255 	  "Intel 82371AB IDE controller (PIIX4)",
    256 	  piix_chip_map,
    257 	},
    258 	{ PCI_PRODUCT_INTEL_82801AA_IDE,
    259 	  0,
    260 	  "Intel 82801AA IDE Controller (ICH)",
    261 	  piix_chip_map,
    262 	},
    263 	{ PCI_PRODUCT_INTEL_82801AB_IDE,
    264 	  0,
    265 	  "Intel 82801AB IDE Controller (ICH0)",
    266 	  piix_chip_map,
    267 	},
    268 	{ 0,
    269 	  0,
    270 	  NULL,
    271 	}
    272 };
    273 
    274 const struct pciide_product_desc pciide_amd_products[] =  {
    275 	{ PCI_PRODUCT_AMD_PBC756_IDE,
    276 	  0,
    277 	  "Advanced Micro Devices AMD756 IDE Controller",
    278 	  amd756_chip_map
    279 	},
    280 	{ 0,
    281 	  0,
    282 	  NULL,
    283 	}
    284 };
    285 
    286 const struct pciide_product_desc pciide_cmd_products[] =  {
    287 	{ PCI_PRODUCT_CMDTECH_640,
    288 	  0,
    289 	  "CMD Technology PCI0640",
    290 	  cmd_chip_map
    291 	},
    292 	{ PCI_PRODUCT_CMDTECH_643,
    293 	  0,
    294 	  "CMD Technology PCI0643",
    295 	  cmd0643_9_chip_map,
    296 	},
    297 	{ PCI_PRODUCT_CMDTECH_646,
    298 	  0,
    299 	  "CMD Technology PCI0646",
    300 	  cmd0643_9_chip_map,
    301 	},
    302 	{ PCI_PRODUCT_CMDTECH_648,
    303 	  IDE_PCI_CLASS_OVERRIDE,
    304 	  "CMD Technology PCI0648",
    305 	  cmd0643_9_chip_map,
    306 	},
    307 	{ PCI_PRODUCT_CMDTECH_649,
    308 	  IDE_PCI_CLASS_OVERRIDE,
    309 	  "CMD Technology PCI0649",
    310 	  cmd0643_9_chip_map,
    311 	},
    312 	{ 0,
    313 	  0,
    314 	  NULL,
    315 	}
    316 };
    317 
    318 const struct pciide_product_desc pciide_via_products[] =  {
    319 	{ PCI_PRODUCT_VIATECH_VT82C586_IDE,
    320 	  0,
    321 	  "VIA Tech VT82C586 IDE Controller",
    322 	  apollo_chip_map,
    323 	 },
    324 	{ PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    325 	  0,
    326 	  "VIA Tech VT82C586A IDE Controller",
    327 	  apollo_chip_map,
    328 	},
    329 	{ 0,
    330 	  0,
    331 	  NULL,
    332 	}
    333 };
    334 
    335 const struct pciide_product_desc pciide_cypress_products[] =  {
    336 	{ PCI_PRODUCT_CONTAQ_82C693,
    337 	  0,
    338 	  "Cypress 82C693 IDE Controller",
    339 	  cy693_chip_map,
    340 	},
    341 	{ 0,
    342 	  0,
    343 	  NULL,
    344 	}
    345 };
    346 
    347 const struct pciide_product_desc pciide_sis_products[] =  {
    348 	{ PCI_PRODUCT_SIS_5597_IDE,
    349 	  0,
    350 	  "Silicon Integrated System 5597/5598 IDE controller",
    351 	  sis_chip_map,
    352 	},
    353 	{ 0,
    354 	  0,
    355 	  NULL,
    356 	}
    357 };
    358 
    359 const struct pciide_product_desc pciide_acer_products[] =  {
    360 	{ PCI_PRODUCT_ALI_M5229,
    361 	  0,
    362 	  "Acer Labs M5229 UDMA IDE Controller",
    363 	  acer_chip_map,
    364 	},
    365 	{ 0,
    366 	  0,
    367 	  NULL,
    368 	}
    369 };
    370 
    371 const struct pciide_product_desc pciide_promise_products[] =  {
    372 	{ PCI_PRODUCT_PROMISE_ULTRA33,
    373 	  IDE_PCI_CLASS_OVERRIDE,
    374 	  "Promise Ultra33/ATA Bus Master IDE Accelerator",
    375 	  pdc202xx_chip_map,
    376 	},
    377 	{ PCI_PRODUCT_PROMISE_ULTRA66,
    378 	  IDE_PCI_CLASS_OVERRIDE,
    379 	  "Promise Ultra66/ATA Bus Master IDE Accelerator",
    380 	  pdc202xx_chip_map,
    381 	},
    382 	{ PCI_PRODUCT_PROMISE_ULTRA100,
    383 	  IDE_PCI_CLASS_OVERRIDE,
    384 	  "Promise Ultra100/ATA Bus Master IDE Accelerator",
    385 	  pdc202xx_chip_map,
    386 	},
    387 	{ 0,
    388 	  0,
    389 	  NULL,
    390 	}
    391 };
    392 
    393 const struct pciide_product_desc pciide_opti_products[] =  {
    394 	{ PCI_PRODUCT_OPTI_82C621,
    395 	  0,
    396 	  "OPTi 82c621 PCI IDE controller",
    397 	  opti_chip_map,
    398 	},
    399 	{ PCI_PRODUCT_OPTI_82C568,
    400 	  0,
    401 	  "OPTi 82c568 (82c621 compatible) PCI IDE controller",
    402 	  opti_chip_map,
    403 	},
    404 	{ PCI_PRODUCT_OPTI_82D568,
    405 	  0,
    406 	  "OPTi 82d568 (82c621 compatible) PCI IDE controller",
    407 	  opti_chip_map,
    408 	},
    409 	{ 0,
    410 	  0,
    411 	  NULL,
    412 	}
    413 };
    414 
    415 const struct pciide_product_desc pciide_triones_products[] =  {
    416 	{ PCI_PRODUCT_TRIONES_HPT366,
    417 	  IDE_PCI_CLASS_OVERRIDE,
    418 	  "Triones/Highpoint HPT366/370 IDE Controller",
    419 	  hpt_chip_map,
    420 	},
    421 	{ 0,
    422 	  0,
    423 	  NULL,
    424 	}
    425 };
    426 
    427 struct pciide_vendor_desc {
    428 	u_int32_t ide_vendor;
    429 	const struct pciide_product_desc *ide_products;
    430 };
    431 
    432 const struct pciide_vendor_desc pciide_vendors[] = {
    433 	{ PCI_VENDOR_INTEL, pciide_intel_products },
    434 	{ PCI_VENDOR_CMDTECH, pciide_cmd_products },
    435 	{ PCI_VENDOR_VIATECH, pciide_via_products },
    436 	{ PCI_VENDOR_CONTAQ, pciide_cypress_products },
    437 	{ PCI_VENDOR_SIS, pciide_sis_products },
    438 	{ PCI_VENDOR_ALI, pciide_acer_products },
    439 	{ PCI_VENDOR_PROMISE, pciide_promise_products },
    440 	{ PCI_VENDOR_AMD, pciide_amd_products },
    441 	{ PCI_VENDOR_OPTI, pciide_opti_products },
    442 	{ PCI_VENDOR_TRIONES, pciide_triones_products },
    443 	{ 0, NULL }
    444 };
    445 
    446 /* options passed via the 'flags' config keyword */
    447 #define PCIIDE_OPTIONS_DMA	0x01
    448 
    449 int	pciide_match __P((struct device *, struct cfdata *, void *));
    450 void	pciide_attach __P((struct device *, struct device *, void *));
    451 
    452 struct cfattach pciide_ca = {
    453 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    454 };
    455 int	pciide_chipen __P((struct pciide_softc *, struct pci_attach_args *));
    456 int	pciide_mapregs_compat __P(( struct pci_attach_args *,
    457 	    struct pciide_channel *, int, bus_size_t *, bus_size_t*));
    458 int	pciide_mapregs_native __P((struct pci_attach_args *,
    459 	    struct pciide_channel *, bus_size_t *, bus_size_t *,
    460 	    int (*pci_intr) __P((void *))));
    461 void	pciide_mapreg_dma __P((struct pciide_softc *,
    462 	    struct pci_attach_args *));
    463 int	pciide_chansetup __P((struct pciide_softc *, int, pcireg_t));
    464 void	pciide_mapchan __P((struct pci_attach_args *,
    465 	    struct pciide_channel *, pcireg_t, bus_size_t *, bus_size_t *,
    466 	    int (*pci_intr) __P((void *))));
    467 int	pciide_chan_candisable __P((struct pciide_channel *));
    468 void	pciide_map_compat_intr __P(( struct pci_attach_args *,
    469 	    struct pciide_channel *, int, int));
    470 int	pciide_print __P((void *, const char *pnp));
    471 int	pciide_compat_intr __P((void *));
    472 int	pciide_pci_intr __P((void *));
    473 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    474 
    475 const struct pciide_product_desc *
    476 pciide_lookup_product(id)
    477 	u_int32_t id;
    478 {
    479 	const struct pciide_product_desc *pp;
    480 	const struct pciide_vendor_desc *vp;
    481 
    482 	for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    483 		if (PCI_VENDOR(id) == vp->ide_vendor)
    484 			break;
    485 
    486 	if ((pp = vp->ide_products) == NULL)
    487 		return NULL;
    488 
    489 	for (; pp->ide_name != NULL; pp++)
    490 		if (PCI_PRODUCT(id) == pp->ide_product)
    491 			break;
    492 
    493 	if (pp->ide_name == NULL)
    494 		return NULL;
    495 	return pp;
    496 }
    497 
    498 int
    499 pciide_match(parent, match, aux)
    500 	struct device *parent;
    501 	struct cfdata *match;
    502 	void *aux;
    503 {
    504 	struct pci_attach_args *pa = aux;
    505 	const struct pciide_product_desc *pp;
    506 
    507 	/*
    508 	 * Check the ID register to see that it's a PCI IDE controller.
    509 	 * If it is, we assume that we can deal with it; it _should_
    510 	 * work in a standardized way...
    511 	 */
    512 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    513 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    514 		return (1);
    515 	}
    516 
    517 	/*
    518 	 * Some controllers (e.g. promise Utra-33) don't claim to be PCI IDE
    519 	 * controllers. Let see if we can deal with it anyway.
    520 	 */
    521 	pp = pciide_lookup_product(pa->pa_id);
    522 	if (pp  && (pp->ide_flags & IDE_PCI_CLASS_OVERRIDE)) {
    523 		return (1);
    524 	}
    525 
    526 	return (0);
    527 }
    528 
    529 void
    530 pciide_attach(parent, self, aux)
    531 	struct device *parent, *self;
    532 	void *aux;
    533 {
    534 	struct pci_attach_args *pa = aux;
    535 	pci_chipset_tag_t pc = pa->pa_pc;
    536 	pcitag_t tag = pa->pa_tag;
    537 	struct pciide_softc *sc = (struct pciide_softc *)self;
    538 	pcireg_t csr;
    539 	char devinfo[256];
    540 	const char *displaydev;
    541 
    542 	sc->sc_pp = pciide_lookup_product(pa->pa_id);
    543 	if (sc->sc_pp == NULL) {
    544 		sc->sc_pp = &default_product_desc;
    545 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    546 		displaydev = devinfo;
    547 	} else
    548 		displaydev = sc->sc_pp->ide_name;
    549 
    550 	printf(": %s (rev. 0x%02x)\n", displaydev, PCI_REVISION(pa->pa_class));
    551 
    552 	sc->sc_pc = pa->pa_pc;
    553 	sc->sc_tag = pa->pa_tag;
    554 #ifdef WDCDEBUG
    555 	if (wdcdebug_pciide_mask & DEBUG_PROBE)
    556 		pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
    557 #endif
    558 	sc->sc_pp->chip_map(sc, pa);
    559 
    560 	if (sc->sc_dma_ok) {
    561 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    562 		csr |= PCI_COMMAND_MASTER_ENABLE;
    563 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    564 	}
    565 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    566 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    567 }
    568 
    569 /* tell wether the chip is enabled or not */
    570 int
    571 pciide_chipen(sc, pa)
    572 	struct pciide_softc *sc;
    573 	struct pci_attach_args *pa;
    574 {
    575 	pcireg_t csr;
    576 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    577 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    578 		    PCI_COMMAND_STATUS_REG);
    579 		printf("%s: device disabled (at %s)\n",
    580 	 	   sc->sc_wdcdev.sc_dev.dv_xname,
    581 	  	  (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    582 		  "device" : "bridge");
    583 		return 0;
    584 	}
    585 	return 1;
    586 }
    587 
    588 int
    589 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
    590 	struct pci_attach_args *pa;
    591 	struct pciide_channel *cp;
    592 	int compatchan;
    593 	bus_size_t *cmdsizep, *ctlsizep;
    594 {
    595 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    596 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    597 
    598 	cp->compat = 1;
    599 	*cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
    600 	*ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
    601 
    602 	wdc_cp->cmd_iot = pa->pa_iot;
    603 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    604 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    605 		printf("%s: couldn't map %s channel cmd regs\n",
    606 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    607 		return (0);
    608 	}
    609 
    610 	wdc_cp->ctl_iot = pa->pa_iot;
    611 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    612 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    613 		printf("%s: couldn't map %s channel ctl regs\n",
    614 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    615 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    616 		    PCIIDE_COMPAT_CMD_SIZE);
    617 		return (0);
    618 	}
    619 
    620 	return (1);
    621 }
    622 
    623 int
    624 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr)
    625 	struct pci_attach_args * pa;
    626 	struct pciide_channel *cp;
    627 	bus_size_t *cmdsizep, *ctlsizep;
    628 	int (*pci_intr) __P((void *));
    629 {
    630 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    631 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    632 	const char *intrstr;
    633 	pci_intr_handle_t intrhandle;
    634 
    635 	cp->compat = 0;
    636 
    637 	if (sc->sc_pci_ih == NULL) {
    638 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    639 		    pa->pa_intrline, &intrhandle) != 0) {
    640 			printf("%s: couldn't map native-PCI interrupt\n",
    641 			    sc->sc_wdcdev.sc_dev.dv_xname);
    642 			return 0;
    643 		}
    644 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    645 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    646 		    intrhandle, IPL_BIO, pci_intr, sc);
    647 		if (sc->sc_pci_ih != NULL) {
    648 			printf("%s: using %s for native-PCI interrupt\n",
    649 			    sc->sc_wdcdev.sc_dev.dv_xname,
    650 			    intrstr ? intrstr : "unknown interrupt");
    651 		} else {
    652 			printf("%s: couldn't establish native-PCI interrupt",
    653 			    sc->sc_wdcdev.sc_dev.dv_xname);
    654 			if (intrstr != NULL)
    655 				printf(" at %s", intrstr);
    656 			printf("\n");
    657 			return 0;
    658 		}
    659 	}
    660 	cp->ih = sc->sc_pci_ih;
    661 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
    662 	    PCI_MAPREG_TYPE_IO, 0,
    663 	    &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
    664 		printf("%s: couldn't map %s channel cmd regs\n",
    665 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    666 		return 0;
    667 	}
    668 
    669 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
    670 	    PCI_MAPREG_TYPE_IO, 0,
    671 	    &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, ctlsizep) != 0) {
    672 		printf("%s: couldn't map %s channel ctl regs\n",
    673 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    674 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
    675 		return 0;
    676 	}
    677 	return (1);
    678 }
    679 
    680 void
    681 pciide_mapreg_dma(sc, pa)
    682 	struct pciide_softc *sc;
    683 	struct pci_attach_args *pa;
    684 {
    685 	/*
    686 	 * Map DMA registers
    687 	 *
    688 	 * Note that sc_dma_ok is the right variable to test to see if
    689 	 * DMA can be done.  If the interface doesn't support DMA,
    690 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    691 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    692 	 * non-zero if the interface supports DMA and the registers
    693 	 * could be mapped.
    694 	 *
    695 	 * XXX Note that despite the fact that the Bus Master IDE specs
    696 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    697 	 * XXX space," some controllers (at least the United
    698 	 * XXX Microelectronics UM8886BF) place it in memory space.
    699 	 */
    700 	sc->sc_dma_ok = (pci_mapreg_map(pa,
    701 	    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    702 	    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    703 	sc->sc_dmat = pa->pa_dmat;
    704 	if (sc->sc_dma_ok == 0) {
    705 		printf(", but unused (couldn't map registers)");
    706 	} else {
    707 		sc->sc_wdcdev.dma_arg = sc;
    708 		sc->sc_wdcdev.dma_init = pciide_dma_init;
    709 		sc->sc_wdcdev.dma_start = pciide_dma_start;
    710 		sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    711 	}
    712 }
    713 
    714 int
    715 pciide_compat_intr(arg)
    716 	void *arg;
    717 {
    718 	struct pciide_channel *cp = arg;
    719 
    720 #ifdef DIAGNOSTIC
    721 	/* should only be called for a compat channel */
    722 	if (cp->compat == 0)
    723 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    724 #endif
    725 	return (wdcintr(&cp->wdc_channel));
    726 }
    727 
    728 int
    729 pciide_pci_intr(arg)
    730 	void *arg;
    731 {
    732 	struct pciide_softc *sc = arg;
    733 	struct pciide_channel *cp;
    734 	struct channel_softc *wdc_cp;
    735 	int i, rv, crv;
    736 
    737 	rv = 0;
    738 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    739 		cp = &sc->pciide_channels[i];
    740 		wdc_cp = &cp->wdc_channel;
    741 
    742 		/* If a compat channel skip. */
    743 		if (cp->compat)
    744 			continue;
    745 		/* if this channel not waiting for intr, skip */
    746 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    747 			continue;
    748 
    749 		crv = wdcintr(wdc_cp);
    750 		if (crv == 0)
    751 			;		/* leave rv alone */
    752 		else if (crv == 1)
    753 			rv = 1;		/* claim the intr */
    754 		else if (rv == 0)	/* crv should be -1 in this case */
    755 			rv = crv;	/* if we've done no better, take it */
    756 	}
    757 	return (rv);
    758 }
    759 
    760 void
    761 pciide_channel_dma_setup(cp)
    762 	struct pciide_channel *cp;
    763 {
    764 	int drive;
    765 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    766 	struct ata_drive_datas *drvp;
    767 
    768 	for (drive = 0; drive < 2; drive++) {
    769 		drvp = &cp->wdc_channel.ch_drive[drive];
    770 		/* If no drive, skip */
    771 		if ((drvp->drive_flags & DRIVE) == 0)
    772 			continue;
    773 		/* setup DMA if needed */
    774 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    775 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    776 		    sc->sc_dma_ok == 0) {
    777 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    778 			continue;
    779 		}
    780 		if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
    781 		    != 0) {
    782 			/* Abort DMA setup */
    783 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    784 			continue;
    785 		}
    786 	}
    787 }
    788 
    789 int
    790 pciide_dma_table_setup(sc, channel, drive)
    791 	struct pciide_softc *sc;
    792 	int channel, drive;
    793 {
    794 	bus_dma_segment_t seg;
    795 	int error, rseg;
    796 	const bus_size_t dma_table_size =
    797 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
    798 	struct pciide_dma_maps *dma_maps =
    799 	    &sc->pciide_channels[channel].dma_maps[drive];
    800 
    801 	/* If table was already allocated, just return */
    802 	if (dma_maps->dma_table)
    803 		return 0;
    804 
    805 	/* Allocate memory for the DMA tables and map it */
    806 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    807 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
    808 	    BUS_DMA_NOWAIT)) != 0) {
    809 		printf("%s:%d: unable to allocate table DMA for "
    810 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    811 		    channel, drive, error);
    812 		return error;
    813 	}
    814 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    815 	    dma_table_size,
    816 	    (caddr_t *)&dma_maps->dma_table,
    817 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    818 		printf("%s:%d: unable to map table DMA for"
    819 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    820 		    channel, drive, error);
    821 		return error;
    822 	}
    823 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
    824 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
    825 	    seg.ds_addr), DEBUG_PROBE);
    826 
    827 	/* Create and load table DMA map for this disk */
    828 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    829 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    830 	    &dma_maps->dmamap_table)) != 0) {
    831 		printf("%s:%d: unable to create table DMA map for "
    832 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    833 		    channel, drive, error);
    834 		return error;
    835 	}
    836 	if ((error = bus_dmamap_load(sc->sc_dmat,
    837 	    dma_maps->dmamap_table,
    838 	    dma_maps->dma_table,
    839 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    840 		printf("%s:%d: unable to load table DMA map for "
    841 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    842 		    channel, drive, error);
    843 		return error;
    844 	}
    845 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    846 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
    847 	/* Create a xfer DMA map for this drive */
    848 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
    849 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
    850 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    851 	    &dma_maps->dmamap_xfer)) != 0) {
    852 		printf("%s:%d: unable to create xfer DMA map for "
    853 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    854 		    channel, drive, error);
    855 		return error;
    856 	}
    857 	return 0;
    858 }
    859 
    860 int
    861 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
    862 	void *v;
    863 	int channel, drive;
    864 	void *databuf;
    865 	size_t datalen;
    866 	int flags;
    867 {
    868 	struct pciide_softc *sc = v;
    869 	int error, seg;
    870 	struct pciide_dma_maps *dma_maps =
    871 	    &sc->pciide_channels[channel].dma_maps[drive];
    872 
    873 	error = bus_dmamap_load(sc->sc_dmat,
    874 	    dma_maps->dmamap_xfer,
    875 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
    876 	if (error) {
    877 		printf("%s:%d: unable to load xfer DMA map for"
    878 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    879 		    channel, drive, error);
    880 		return error;
    881 	}
    882 
    883 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    884 	    dma_maps->dmamap_xfer->dm_mapsize,
    885 	    (flags & WDC_DMA_READ) ?
    886 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    887 
    888 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    889 #ifdef DIAGNOSTIC
    890 		/* A segment must not cross a 64k boundary */
    891 		{
    892 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    893 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    894 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    895 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    896 			printf("pciide_dma: segment %d physical addr 0x%lx"
    897 			    " len 0x%lx not properly aligned\n",
    898 			    seg, phys, len);
    899 			panic("pciide_dma: buf align");
    900 		}
    901 		}
    902 #endif
    903 		dma_maps->dma_table[seg].base_addr =
    904 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
    905 		dma_maps->dma_table[seg].byte_count =
    906 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    907 		    IDEDMA_BYTE_COUNT_MASK);
    908 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    909 		   seg, le32toh(dma_maps->dma_table[seg].byte_count),
    910 		   le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    911 
    912 	}
    913 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    914 	    htole32(IDEDMA_BYTE_COUNT_EOT);
    915 
    916 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    917 	    dma_maps->dmamap_table->dm_mapsize,
    918 	    BUS_DMASYNC_PREWRITE);
    919 
    920 	/* Maps are ready. Start DMA function */
    921 #ifdef DIAGNOSTIC
    922 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    923 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
    924 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    925 		panic("pciide_dma_init: table align");
    926 	}
    927 #endif
    928 
    929 	/* Clear status bits */
    930 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    931 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    932 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    933 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
    934 	/* Write table addr */
    935 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    936 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
    937 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    938 	/* set read/write */
    939 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    940 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    941 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
    942 	/* remember flags */
    943 	dma_maps->dma_flags = flags;
    944 	return 0;
    945 }
    946 
    947 void
    948 pciide_dma_start(v, channel, drive)
    949 	void *v;
    950 	int channel, drive;
    951 {
    952 	struct pciide_softc *sc = v;
    953 
    954 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    955 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    956 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    957 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    958 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
    959 }
    960 
    961 int
    962 pciide_dma_finish(v, channel, drive, force)
    963 	void *v;
    964 	int channel, drive;
    965 	int force;
    966 {
    967 	struct pciide_softc *sc = v;
    968 	u_int8_t status;
    969 	int error = 0;
    970 	struct pciide_dma_maps *dma_maps =
    971 	    &sc->pciide_channels[channel].dma_maps[drive];
    972 
    973 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    974 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
    975 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    976 	    DEBUG_XFERS);
    977 
    978 	if (force == 0 && (status & IDEDMA_CTL_INTR) == 0)
    979 		return WDC_DMAST_NOIRQ;
    980 
    981 	/* stop DMA channel */
    982 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    983 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    984 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    985 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
    986 
    987 	/* Unload the map of the data buffer */
    988 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    989 	    dma_maps->dmamap_xfer->dm_mapsize,
    990 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    991 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    992 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    993 
    994 	if ((status & IDEDMA_CTL_ERR) != 0) {
    995 		printf("%s:%d:%d: bus-master DMA error: status=0x%x\n",
    996 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
    997 		error |= WDC_DMAST_ERR;
    998 	}
    999 
   1000 	if ((status & IDEDMA_CTL_INTR) == 0) {
   1001 		printf("%s:%d:%d: bus-master DMA error: missing interrupt, "
   1002 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
   1003 		    drive, status);
   1004 		error |= WDC_DMAST_NOIRQ;
   1005 	}
   1006 
   1007 	if ((status & IDEDMA_CTL_ACT) != 0) {
   1008 		/* data underrun, may be a valid condition for ATAPI */
   1009 		error |= WDC_DMAST_UNDER;
   1010 	}
   1011 	return error;
   1012 }
   1013 
   1014 void
   1015 pciide_irqack(chp)
   1016 	struct channel_softc *chp;
   1017 {
   1018 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1019 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1020 
   1021 	/* clear status bits in IDE DMA registers */
   1022 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1023 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel,
   1024 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1025 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel));
   1026 }
   1027 
   1028 /* some common code used by several chip_map */
   1029 int
   1030 pciide_chansetup(sc, channel, interface)
   1031 	struct pciide_softc *sc;
   1032 	int channel;
   1033 	pcireg_t interface;
   1034 {
   1035 	struct pciide_channel *cp = &sc->pciide_channels[channel];
   1036 	sc->wdc_chanarray[channel] = &cp->wdc_channel;
   1037 	cp->name = PCIIDE_CHANNEL_NAME(channel);
   1038 	cp->wdc_channel.channel = channel;
   1039 	cp->wdc_channel.wdc = &sc->sc_wdcdev;
   1040 	cp->wdc_channel.ch_queue =
   1041 	    malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
   1042 	if (cp->wdc_channel.ch_queue == NULL) {
   1043 		printf("%s %s channel: "
   1044 		    "can't allocate memory for command queue",
   1045 		sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1046 		return 0;
   1047 	}
   1048 	printf("%s: %s channel %s to %s mode\n",
   1049 	    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
   1050 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
   1051 	    "configured" : "wired",
   1052 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
   1053 	    "native-PCI" : "compatibility");
   1054 	return 1;
   1055 }
   1056 
   1057 /* some common code used by several chip channel_map */
   1058 void
   1059 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep, pci_intr)
   1060 	struct pci_attach_args *pa;
   1061 	struct pciide_channel *cp;
   1062 	pcireg_t interface;
   1063 	bus_size_t *cmdsizep, *ctlsizep;
   1064 	int (*pci_intr) __P((void *));
   1065 {
   1066 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1067 
   1068 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
   1069 		cp->hw_ok = pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep,
   1070 		    pci_intr);
   1071 	else
   1072 		cp->hw_ok = pciide_mapregs_compat(pa, cp,
   1073 		    wdc_cp->channel, cmdsizep, ctlsizep);
   1074 
   1075 	if (cp->hw_ok == 0)
   1076 		return;
   1077 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1078 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1079 	wdcattach(wdc_cp);
   1080 }
   1081 
   1082 /*
   1083  * Generic code to call to know if a channel can be disabled. Return 1
   1084  * if channel can be disabled, 0 if not
   1085  */
   1086 int
   1087 pciide_chan_candisable(cp)
   1088 	struct pciide_channel *cp;
   1089 {
   1090 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1091 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1092 
   1093 	if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
   1094 	    (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
   1095 		printf("%s: disabling %s channel (no drives)\n",
   1096 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1097 		cp->hw_ok = 0;
   1098 		return 1;
   1099 	}
   1100 	return 0;
   1101 }
   1102 
   1103 /*
   1104  * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
   1105  * Set hw_ok=0 on failure
   1106  */
   1107 void
   1108 pciide_map_compat_intr(pa, cp, compatchan, interface)
   1109 	struct pci_attach_args *pa;
   1110 	struct pciide_channel *cp;
   1111 	int compatchan, interface;
   1112 {
   1113 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1114 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1115 
   1116 	if (cp->hw_ok == 0)
   1117 		return;
   1118 	if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
   1119 		return;
   1120 
   1121 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
   1122 	    pa, compatchan, pciide_compat_intr, cp);
   1123 	if (cp->ih == NULL) {
   1124 		printf("%s: no compatibility interrupt for use by %s "
   1125 		    "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1126 		cp->hw_ok = 0;
   1127 	}
   1128 }
   1129 
   1130 void
   1131 pciide_print_modes(cp)
   1132 	struct pciide_channel *cp;
   1133 {
   1134 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1135 	int drive;
   1136 	struct channel_softc *chp;
   1137 	struct ata_drive_datas *drvp;
   1138 
   1139 	chp = &cp->wdc_channel;
   1140 	for (drive = 0; drive < 2; drive++) {
   1141 		drvp = &chp->ch_drive[drive];
   1142 		if ((drvp->drive_flags & DRIVE) == 0)
   1143 			continue;
   1144 		printf("%s(%s:%d:%d): using PIO mode %d",
   1145 		    drvp->drv_softc->dv_xname,
   1146 		    sc->sc_wdcdev.sc_dev.dv_xname,
   1147 		    chp->channel, drive, drvp->PIO_mode);
   1148 		if (drvp->drive_flags & DRIVE_DMA)
   1149 			printf(", DMA mode %d", drvp->DMA_mode);
   1150 		if (drvp->drive_flags & DRIVE_UDMA)
   1151 			printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
   1152 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
   1153 			printf(" (using DMA data transfers)");
   1154 		printf("\n");
   1155 	}
   1156 }
   1157 
   1158 void
   1159 default_chip_map(sc, pa)
   1160 	struct pciide_softc *sc;
   1161 	struct pci_attach_args *pa;
   1162 {
   1163 	struct pciide_channel *cp;
   1164 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
   1165 	pcireg_t csr;
   1166 	int channel, drive;
   1167 	struct ata_drive_datas *drvp;
   1168 	u_int8_t idedma_ctl;
   1169 	bus_size_t cmdsize, ctlsize;
   1170 	char *failreason;
   1171 
   1172 	if (pciide_chipen(sc, pa) == 0)
   1173 		return;
   1174 
   1175 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
   1176 		printf("%s: bus-master DMA support present",
   1177 		    sc->sc_wdcdev.sc_dev.dv_xname);
   1178 		if (sc->sc_pp == &default_product_desc &&
   1179 		    (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
   1180 		    PCIIDE_OPTIONS_DMA) == 0) {
   1181 			printf(", but unused (no driver support)");
   1182 			sc->sc_dma_ok = 0;
   1183 		} else {
   1184 			pciide_mapreg_dma(sc, pa);
   1185 		if (sc->sc_dma_ok != 0)
   1186 			printf(", used without full driver "
   1187 			    "support");
   1188 		}
   1189 	} else {
   1190 		printf("%s: hardware does not support DMA",
   1191 		    sc->sc_wdcdev.sc_dev.dv_xname);
   1192 		sc->sc_dma_ok = 0;
   1193 	}
   1194 	printf("\n");
   1195 	if (sc->sc_dma_ok) {
   1196 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   1197 		sc->sc_wdcdev.irqack = pciide_irqack;
   1198 	}
   1199 	sc->sc_wdcdev.PIO_cap = 0;
   1200 	sc->sc_wdcdev.DMA_cap = 0;
   1201 
   1202 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   1203 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   1204 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
   1205 
   1206 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1207 		cp = &sc->pciide_channels[channel];
   1208 		if (pciide_chansetup(sc, channel, interface) == 0)
   1209 			continue;
   1210 		if (interface & PCIIDE_INTERFACE_PCI(channel)) {
   1211 			cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize,
   1212 			    &ctlsize, pciide_pci_intr);
   1213 		} else {
   1214 			cp->hw_ok = pciide_mapregs_compat(pa, cp,
   1215 			    channel, &cmdsize, &ctlsize);
   1216 		}
   1217 		if (cp->hw_ok == 0)
   1218 			continue;
   1219 		/*
   1220 		 * Check to see if something appears to be there.
   1221 		 */
   1222 		failreason = NULL;
   1223 		if (!wdcprobe(&cp->wdc_channel)) {
   1224 			failreason = "not responding; disabled or no drives?";
   1225 			goto next;
   1226 		}
   1227 		/*
   1228 		 * Now, make sure it's actually attributable to this PCI IDE
   1229 		 * channel by trying to access the channel again while the
   1230 		 * PCI IDE controller's I/O space is disabled.  (If the
   1231 		 * channel no longer appears to be there, it belongs to
   1232 		 * this controller.)  YUCK!
   1233 		 */
   1234 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
   1235 		    PCI_COMMAND_STATUS_REG);
   1236 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
   1237 		    csr & ~PCI_COMMAND_IO_ENABLE);
   1238 		if (wdcprobe(&cp->wdc_channel))
   1239 			failreason = "other hardware responding at addresses";
   1240 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   1241 		    PCI_COMMAND_STATUS_REG, csr);
   1242 next:
   1243 		if (failreason) {
   1244 			printf("%s: %s channel ignored (%s)\n",
   1245 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
   1246 			    failreason);
   1247 			cp->hw_ok = 0;
   1248 			bus_space_unmap(cp->wdc_channel.cmd_iot,
   1249 			    cp->wdc_channel.cmd_ioh, cmdsize);
   1250 			bus_space_unmap(cp->wdc_channel.ctl_iot,
   1251 			    cp->wdc_channel.ctl_ioh, ctlsize);
   1252 		} else {
   1253 			pciide_map_compat_intr(pa, cp, channel, interface);
   1254 		}
   1255 		if (cp->hw_ok) {
   1256 			cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
   1257 			cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
   1258 			wdcattach(&cp->wdc_channel);
   1259 		}
   1260 	}
   1261 
   1262 	if (sc->sc_dma_ok == 0)
   1263 		return;
   1264 
   1265 	/* Allocate DMA maps */
   1266 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1267 		idedma_ctl = 0;
   1268 		cp = &sc->pciide_channels[channel];
   1269 		for (drive = 0; drive < 2; drive++) {
   1270 			drvp = &cp->wdc_channel.ch_drive[drive];
   1271 			/* If no drive, skip */
   1272 			if ((drvp->drive_flags & DRIVE) == 0)
   1273 				continue;
   1274 			if ((drvp->drive_flags & DRIVE_DMA) == 0)
   1275 				continue;
   1276 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1277 				/* Abort DMA setup */
   1278 				printf("%s:%d:%d: can't allocate DMA maps, "
   1279 				    "using PIO transfers\n",
   1280 				    sc->sc_wdcdev.sc_dev.dv_xname,
   1281 				    channel, drive);
   1282 				drvp->drive_flags &= ~DRIVE_DMA;
   1283 			}
   1284 			printf("%s:%d:%d: using DMA data transfers\n",
   1285 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1286 			    channel, drive);
   1287 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1288 		}
   1289 		if (idedma_ctl != 0) {
   1290 			/* Add software bits in status register */
   1291 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1292 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1293 			    idedma_ctl);
   1294 		}
   1295 	}
   1296 }
   1297 
   1298 void
   1299 piix_chip_map(sc, pa)
   1300 	struct pciide_softc *sc;
   1301 	struct pci_attach_args *pa;
   1302 {
   1303 	struct pciide_channel *cp;
   1304 	int channel;
   1305 	u_int32_t idetim;
   1306 	bus_size_t cmdsize, ctlsize;
   1307 
   1308 	if (pciide_chipen(sc, pa) == 0)
   1309 		return;
   1310 
   1311 	printf("%s: bus-master DMA support present",
   1312 	    sc->sc_wdcdev.sc_dev.dv_xname);
   1313 	pciide_mapreg_dma(sc, pa);
   1314 	printf("\n");
   1315 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   1316 	    WDC_CAPABILITY_MODE;
   1317 	if (sc->sc_dma_ok) {
   1318 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   1319 		sc->sc_wdcdev.irqack = pciide_irqack;
   1320 		switch(sc->sc_pp->ide_product) {
   1321 		case PCI_PRODUCT_INTEL_82371AB_IDE:
   1322 		case PCI_PRODUCT_INTEL_82801AA_IDE:
   1323 		case PCI_PRODUCT_INTEL_82801AB_IDE:
   1324 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1325 		}
   1326 	}
   1327 	sc->sc_wdcdev.PIO_cap = 4;
   1328 	sc->sc_wdcdev.DMA_cap = 2;
   1329 	sc->sc_wdcdev.UDMA_cap =
   1330 	    (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE) ? 4 : 2;
   1331 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371FB_IDE)
   1332 		sc->sc_wdcdev.set_modes = piix_setup_channel;
   1333 	else
   1334 		sc->sc_wdcdev.set_modes = piix3_4_setup_channel;
   1335 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   1336 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   1337 
   1338 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x",
   1339 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)),
   1340 	    DEBUG_PROBE);
   1341 	if (sc->sc_pp->ide_product != PCI_PRODUCT_INTEL_82371FB_IDE) {
   1342 		WDCDEBUG_PRINT((", sidetim=0x%x",
   1343 		    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)),
   1344 		    DEBUG_PROBE);
   1345 		if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1346 			WDCDEBUG_PRINT((", udamreg 0x%x",
   1347 			    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1348 			    DEBUG_PROBE);
   1349 		}
   1350 		if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
   1351 		    sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
   1352 			WDCDEBUG_PRINT((", IDE_CONTROL 0x%x",
   1353 			    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG)),
   1354 			    DEBUG_PROBE);
   1355 		}
   1356 
   1357 	}
   1358 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1359 
   1360 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1361 		cp = &sc->pciide_channels[channel];
   1362 		/* PIIX is compat-only */
   1363 		if (pciide_chansetup(sc, channel, 0) == 0)
   1364 			continue;
   1365 		idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1366 		if ((PIIX_IDETIM_READ(idetim, channel) &
   1367 		    PIIX_IDETIM_IDE) == 0) {
   1368 			printf("%s: %s channel ignored (disabled)\n",
   1369 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1370 			continue;
   1371 		}
   1372 		/* PIIX are compat-only pciide devices */
   1373 		pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize, pciide_pci_intr);
   1374 		if (cp->hw_ok == 0)
   1375 			continue;
   1376 		if (pciide_chan_candisable(cp)) {
   1377 			idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
   1378 			    channel);
   1379 			pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM,
   1380 			    idetim);
   1381 		}
   1382 		pciide_map_compat_intr(pa, cp, channel, 0);
   1383 		if (cp->hw_ok == 0)
   1384 			continue;
   1385 		sc->sc_wdcdev.set_modes(&cp->wdc_channel);
   1386 	}
   1387 
   1388 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x",
   1389 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)),
   1390 	    DEBUG_PROBE);
   1391 	if (sc->sc_pp->ide_product != PCI_PRODUCT_INTEL_82371FB_IDE) {
   1392 		WDCDEBUG_PRINT((", sidetim=0x%x",
   1393 		    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)),
   1394 		    DEBUG_PROBE);
   1395 		if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1396 			WDCDEBUG_PRINT((", udamreg 0x%x",
   1397 			    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1398 			    DEBUG_PROBE);
   1399 		}
   1400 		if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
   1401 		    sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
   1402 			WDCDEBUG_PRINT((", IDE_CONTROL 0x%x",
   1403 			    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG)),
   1404 			    DEBUG_PROBE);
   1405 		}
   1406 	}
   1407 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1408 }
   1409 
   1410 void
   1411 piix_setup_channel(chp)
   1412 	struct channel_softc *chp;
   1413 {
   1414 	u_int8_t mode[2], drive;
   1415 	u_int32_t oidetim, idetim, idedma_ctl;
   1416 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1417 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1418 	struct ata_drive_datas *drvp = cp->wdc_channel.ch_drive;
   1419 
   1420 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1421 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1422 	idedma_ctl = 0;
   1423 
   1424 	/* set up new idetim: Enable IDE registers decode */
   1425 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
   1426 	    chp->channel);
   1427 
   1428 	/* setup DMA */
   1429 	pciide_channel_dma_setup(cp);
   1430 
   1431 	/*
   1432 	 * Here we have to mess up with drives mode: PIIX can't have
   1433 	 * different timings for master and slave drives.
   1434 	 * We need to find the best combination.
   1435 	 */
   1436 
   1437 	/* If both drives supports DMA, take the lower mode */
   1438 	if ((drvp[0].drive_flags & DRIVE_DMA) &&
   1439 	    (drvp[1].drive_flags & DRIVE_DMA)) {
   1440 		mode[0] = mode[1] =
   1441 		    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
   1442 		    drvp[0].DMA_mode = mode[0];
   1443 		    drvp[1].DMA_mode = mode[1];
   1444 		goto ok;
   1445 	}
   1446 	/*
   1447 	 * If only one drive supports DMA, use its mode, and
   1448 	 * put the other one in PIO mode 0 if mode not compatible
   1449 	 */
   1450 	if (drvp[0].drive_flags & DRIVE_DMA) {
   1451 		mode[0] = drvp[0].DMA_mode;
   1452 		mode[1] = drvp[1].PIO_mode;
   1453 		if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
   1454 		    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
   1455 			mode[1] = drvp[1].PIO_mode = 0;
   1456 		goto ok;
   1457 	}
   1458 	if (drvp[1].drive_flags & DRIVE_DMA) {
   1459 		mode[1] = drvp[1].DMA_mode;
   1460 		mode[0] = drvp[0].PIO_mode;
   1461 		if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
   1462 		    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
   1463 			mode[0] = drvp[0].PIO_mode = 0;
   1464 		goto ok;
   1465 	}
   1466 	/*
   1467 	 * If both drives are not DMA, takes the lower mode, unless
   1468 	 * one of them is PIO mode < 2
   1469 	 */
   1470 	if (drvp[0].PIO_mode < 2) {
   1471 		mode[0] = drvp[0].PIO_mode = 0;
   1472 		mode[1] = drvp[1].PIO_mode;
   1473 	} else if (drvp[1].PIO_mode < 2) {
   1474 		mode[1] = drvp[1].PIO_mode = 0;
   1475 		mode[0] = drvp[0].PIO_mode;
   1476 	} else {
   1477 		mode[0] = mode[1] =
   1478 		    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
   1479 		drvp[0].PIO_mode = mode[0];
   1480 		drvp[1].PIO_mode = mode[1];
   1481 	}
   1482 ok:	/* The modes are setup */
   1483 	for (drive = 0; drive < 2; drive++) {
   1484 		if (drvp[drive].drive_flags & DRIVE_DMA) {
   1485 			idetim |= piix_setup_idetim_timings(
   1486 			    mode[drive], 1, chp->channel);
   1487 			goto end;
   1488 		}
   1489 	}
   1490 	/* If we are there, none of the drives are DMA */
   1491 	if (mode[0] >= 2)
   1492 		idetim |= piix_setup_idetim_timings(
   1493 		    mode[0], 0, chp->channel);
   1494 	else
   1495 		idetim |= piix_setup_idetim_timings(
   1496 		    mode[1], 0, chp->channel);
   1497 end:	/*
   1498 	 * timing mode is now set up in the controller. Enable
   1499 	 * it per-drive
   1500 	 */
   1501 	for (drive = 0; drive < 2; drive++) {
   1502 		/* If no drive, skip */
   1503 		if ((drvp[drive].drive_flags & DRIVE) == 0)
   1504 			continue;
   1505 		idetim |= piix_setup_idetim_drvs(&drvp[drive]);
   1506 		if (drvp[drive].drive_flags & DRIVE_DMA)
   1507 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1508 	}
   1509 	if (idedma_ctl != 0) {
   1510 		/* Add software bits in status register */
   1511 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1512 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1513 		    idedma_ctl);
   1514 	}
   1515 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1516 	pciide_print_modes(cp);
   1517 }
   1518 
   1519 void
   1520 piix3_4_setup_channel(chp)
   1521 	struct channel_softc *chp;
   1522 {
   1523 	struct ata_drive_datas *drvp;
   1524 	u_int32_t oidetim, idetim, sidetim, udmareg, ideconf, idedma_ctl;
   1525 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1526 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1527 	int drive;
   1528 	int channel = chp->channel;
   1529 
   1530 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1531 	sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
   1532 	udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
   1533 	ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG);
   1534 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, channel);
   1535 	sidetim &= ~(PIIX_SIDETIM_ISP_MASK(channel) |
   1536 	    PIIX_SIDETIM_RTC_MASK(channel));
   1537 
   1538 	idedma_ctl = 0;
   1539 	/* If channel disabled, no need to go further */
   1540 	if ((PIIX_IDETIM_READ(oidetim, channel) & PIIX_IDETIM_IDE) == 0)
   1541 		return;
   1542 	/* set up new idetim: Enable IDE registers decode */
   1543 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, channel);
   1544 
   1545 	/* setup DMA if needed */
   1546 	pciide_channel_dma_setup(cp);
   1547 
   1548 	for (drive = 0; drive < 2; drive++) {
   1549 		udmareg &= ~(PIIX_UDMACTL_DRV_EN(channel, drive) |
   1550 		    PIIX_UDMATIM_SET(0x3, channel, drive));
   1551 		drvp = &chp->ch_drive[drive];
   1552 		/* If no drive, skip */
   1553 		if ((drvp->drive_flags & DRIVE) == 0)
   1554 			continue;
   1555 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1556 		    (drvp->drive_flags & DRIVE_UDMA) == 0))
   1557 			goto pio;
   1558 
   1559 		if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
   1560 		    sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
   1561 			ideconf |= PIIX_CONFIG_PINGPONG;
   1562 		}
   1563 		if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE) {
   1564 			/* setup Ultra/66 */
   1565 			if (drvp->UDMA_mode > 2 &&
   1566 			    (ideconf & PIIX_CONFIG_CR(channel, drive)) == 0)
   1567 				drvp->UDMA_mode = 2;
   1568 			if (drvp->UDMA_mode > 2)
   1569 				ideconf |= PIIX_CONFIG_UDMA66(channel, drive);
   1570 			else
   1571 				ideconf &= ~PIIX_CONFIG_UDMA66(channel, drive);
   1572 		}
   1573 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1574 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1575 			/* use Ultra/DMA */
   1576 			drvp->drive_flags &= ~DRIVE_DMA;
   1577 			udmareg |= PIIX_UDMACTL_DRV_EN( channel, drive);
   1578 			udmareg |= PIIX_UDMATIM_SET(
   1579 			    piix4_sct_udma[drvp->UDMA_mode], channel, drive);
   1580 		} else {
   1581 			/* use Multiword DMA */
   1582 			drvp->drive_flags &= ~DRIVE_UDMA;
   1583 			if (drive == 0) {
   1584 				idetim |= piix_setup_idetim_timings(
   1585 				    drvp->DMA_mode, 1, channel);
   1586 			} else {
   1587 				sidetim |= piix_setup_sidetim_timings(
   1588 					drvp->DMA_mode, 1, channel);
   1589 				idetim =PIIX_IDETIM_SET(idetim,
   1590 				    PIIX_IDETIM_SITRE, channel);
   1591 			}
   1592 		}
   1593 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1594 
   1595 pio:		/* use PIO mode */
   1596 		idetim |= piix_setup_idetim_drvs(drvp);
   1597 		if (drive == 0) {
   1598 			idetim |= piix_setup_idetim_timings(
   1599 			    drvp->PIO_mode, 0, channel);
   1600 		} else {
   1601 			sidetim |= piix_setup_sidetim_timings(
   1602 				drvp->PIO_mode, 0, channel);
   1603 			idetim =PIIX_IDETIM_SET(idetim,
   1604 			    PIIX_IDETIM_SITRE, channel);
   1605 		}
   1606 	}
   1607 	if (idedma_ctl != 0) {
   1608 		/* Add software bits in status register */
   1609 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1610 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1611 		    idedma_ctl);
   1612 	}
   1613 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1614 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
   1615 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
   1616 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_CONFIG, ideconf);
   1617 	pciide_print_modes(cp);
   1618 }
   1619 
   1620 
   1621 /* setup ISP and RTC fields, based on mode */
   1622 static u_int32_t
   1623 piix_setup_idetim_timings(mode, dma, channel)
   1624 	u_int8_t mode;
   1625 	u_int8_t dma;
   1626 	u_int8_t channel;
   1627 {
   1628 
   1629 	if (dma)
   1630 		return PIIX_IDETIM_SET(0,
   1631 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1632 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1633 		    channel);
   1634 	else
   1635 		return PIIX_IDETIM_SET(0,
   1636 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1637 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1638 		    channel);
   1639 }
   1640 
   1641 /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1642 static u_int32_t
   1643 piix_setup_idetim_drvs(drvp)
   1644 	struct ata_drive_datas *drvp;
   1645 {
   1646 	u_int32_t ret = 0;
   1647 	struct channel_softc *chp = drvp->chnl_softc;
   1648 	u_int8_t channel = chp->channel;
   1649 	u_int8_t drive = drvp->drive;
   1650 
   1651 	/*
   1652 	 * If drive is using UDMA, timings setups are independant
   1653 	 * So just check DMA and PIO here.
   1654 	 */
   1655 	if (drvp->drive_flags & DRIVE_DMA) {
   1656 		/* if mode = DMA mode 0, use compatible timings */
   1657 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1658 		    drvp->DMA_mode == 0) {
   1659 			drvp->PIO_mode = 0;
   1660 			return ret;
   1661 		}
   1662 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1663 		/*
   1664 		 * PIO and DMA timings are the same, use fast timings for PIO
   1665 		 * too, else use compat timings.
   1666 		 */
   1667 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1668 		    piix_isp_dma[drvp->DMA_mode]) ||
   1669 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1670 		    piix_rtc_dma[drvp->DMA_mode]))
   1671 			drvp->PIO_mode = 0;
   1672 		/* if PIO mode <= 2, use compat timings for PIO */
   1673 		if (drvp->PIO_mode <= 2) {
   1674 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1675 			    channel);
   1676 			return ret;
   1677 		}
   1678 	}
   1679 
   1680 	/*
   1681 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1682 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1683 	 * if PIO mode >= 3.
   1684 	 */
   1685 
   1686 	if (drvp->PIO_mode < 2)
   1687 		return ret;
   1688 
   1689 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1690 	if (drvp->PIO_mode >= 3) {
   1691 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1692 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1693 	}
   1694 	return ret;
   1695 }
   1696 
   1697 /* setup values in SIDETIM registers, based on mode */
   1698 static u_int32_t
   1699 piix_setup_sidetim_timings(mode, dma, channel)
   1700 	u_int8_t mode;
   1701 	u_int8_t dma;
   1702 	u_int8_t channel;
   1703 {
   1704 	if (dma)
   1705 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1706 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1707 	else
   1708 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1709 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1710 }
   1711 
   1712 void
   1713 amd756_chip_map(sc, pa)
   1714 	struct pciide_softc *sc;
   1715 	struct pci_attach_args *pa;
   1716 {
   1717 	struct pciide_channel *cp;
   1718 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
   1719 	int channel;
   1720 	pcireg_t chanenable;
   1721 	bus_size_t cmdsize, ctlsize;
   1722 
   1723 	if (pciide_chipen(sc, pa) == 0)
   1724 		return;
   1725 	printf("%s: bus-master DMA support present",
   1726 	    sc->sc_wdcdev.sc_dev.dv_xname);
   1727 	pciide_mapreg_dma(sc, pa);
   1728 	printf("\n");
   1729 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   1730 	    WDC_CAPABILITY_MODE;
   1731 	if (sc->sc_dma_ok) {
   1732 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   1733 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
   1734 		sc->sc_wdcdev.irqack = pciide_irqack;
   1735 	}
   1736 	sc->sc_wdcdev.PIO_cap = 4;
   1737 	sc->sc_wdcdev.DMA_cap = 2;
   1738 	sc->sc_wdcdev.UDMA_cap = 4;
   1739 	sc->sc_wdcdev.set_modes = amd756_setup_channel;
   1740 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   1741 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   1742 	chanenable = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_CHANSTATUS_EN);
   1743 
   1744 	WDCDEBUG_PRINT(("amd756_chip_map: Channel enable=0x%x\n", chanenable),
   1745 	    DEBUG_PROBE);
   1746 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1747 		cp = &sc->pciide_channels[channel];
   1748 		if (pciide_chansetup(sc, channel, interface) == 0)
   1749 			continue;
   1750 
   1751 		if ((chanenable & AMD756_CHAN_EN(channel)) == 0) {
   1752 			printf("%s: %s channel ignored (disabled)\n",
   1753 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1754 			continue;
   1755 		}
   1756 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   1757 		    pciide_pci_intr);
   1758 
   1759 		if (pciide_chan_candisable(cp))
   1760 			chanenable &= ~AMD756_CHAN_EN(channel);
   1761 		pciide_map_compat_intr(pa, cp, channel, interface);
   1762 		if (cp->hw_ok == 0)
   1763 			continue;
   1764 
   1765 		amd756_setup_channel(&cp->wdc_channel);
   1766 	}
   1767 	pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_CHANSTATUS_EN,
   1768 	    chanenable);
   1769 	return;
   1770 }
   1771 
   1772 void
   1773 amd756_setup_channel(chp)
   1774 	struct channel_softc *chp;
   1775 {
   1776 	u_int32_t udmatim_reg, datatim_reg;
   1777 	u_int8_t idedma_ctl;
   1778 	int mode, drive;
   1779 	struct ata_drive_datas *drvp;
   1780 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1781 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1782 #ifndef PCIIDE_AMD756_ENABLEDMA
   1783 	int rev = PCI_REVISION(
   1784 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1785 #endif
   1786 
   1787 	idedma_ctl = 0;
   1788 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_DATATIM);
   1789 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_UDMA);
   1790 	datatim_reg &= ~AMD756_DATATIM_MASK(chp->channel);
   1791 	udmatim_reg &= ~AMD756_UDMA_MASK(chp->channel);
   1792 
   1793 	/* setup DMA if needed */
   1794 	pciide_channel_dma_setup(cp);
   1795 
   1796 	for (drive = 0; drive < 2; drive++) {
   1797 		drvp = &chp->ch_drive[drive];
   1798 		/* If no drive, skip */
   1799 		if ((drvp->drive_flags & DRIVE) == 0)
   1800 			continue;
   1801 		/* add timing values, setup DMA if needed */
   1802 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1803 		    (drvp->drive_flags & DRIVE_UDMA) == 0)) {
   1804 			mode = drvp->PIO_mode;
   1805 			goto pio;
   1806 		}
   1807 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1808 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1809 			/* use Ultra/DMA */
   1810 			drvp->drive_flags &= ~DRIVE_DMA;
   1811 			udmatim_reg |= AMD756_UDMA_EN(chp->channel, drive) |
   1812 			    AMD756_UDMA_EN_MTH(chp->channel, drive) |
   1813 			    AMD756_UDMA_TIME(chp->channel, drive,
   1814 				amd756_udma_tim[drvp->UDMA_mode]);
   1815 			/* can use PIO timings, MW DMA unused */
   1816 			mode = drvp->PIO_mode;
   1817 		} else {
   1818 			/* use Multiword DMA, but only if revision is OK */
   1819 			drvp->drive_flags &= ~DRIVE_UDMA;
   1820 #ifndef PCIIDE_AMD756_ENABLEDMA
   1821 			/*
   1822 			 * The workaround doesn't seem to be necessary
   1823 			 * with all drives, so it can be disabled by
   1824 			 * PCIIDE_AMD756_ENABLEDMA. It causes a hard hang if
   1825 			 * triggered.
   1826 			 */
   1827 			if (AMD756_CHIPREV_DISABLEDMA(rev)) {
   1828 				printf("%s:%d:%d: multi-word DMA disabled due "
   1829 				    "to chip revision\n",
   1830 				    sc->sc_wdcdev.sc_dev.dv_xname,
   1831 				    chp->channel, drive);
   1832 				mode = drvp->PIO_mode;
   1833 				drvp->drive_flags &= ~DRIVE_DMA;
   1834 				goto pio;
   1835 			}
   1836 #endif
   1837 			/* mode = min(pio, dma+2) */
   1838 			if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1839 				mode = drvp->PIO_mode;
   1840 			else
   1841 				mode = drvp->DMA_mode + 2;
   1842 		}
   1843 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1844 
   1845 pio:		/* setup PIO mode */
   1846 		if (mode <= 2) {
   1847 			drvp->DMA_mode = 0;
   1848 			drvp->PIO_mode = 0;
   1849 			mode = 0;
   1850 		} else {
   1851 			drvp->PIO_mode = mode;
   1852 			drvp->DMA_mode = mode - 2;
   1853 		}
   1854 		datatim_reg |=
   1855 		    AMD756_DATATIM_PULSE(chp->channel, drive,
   1856 			amd756_pio_set[mode]) |
   1857 		    AMD756_DATATIM_RECOV(chp->channel, drive,
   1858 			amd756_pio_rec[mode]);
   1859 	}
   1860 	if (idedma_ctl != 0) {
   1861 		/* Add software bits in status register */
   1862 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1863 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1864 		    idedma_ctl);
   1865 	}
   1866 	pciide_print_modes(cp);
   1867 	pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_DATATIM, datatim_reg);
   1868 	pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_UDMA, udmatim_reg);
   1869 }
   1870 
   1871 void
   1872 apollo_chip_map(sc, pa)
   1873 	struct pciide_softc *sc;
   1874 	struct pci_attach_args *pa;
   1875 {
   1876 	struct pciide_channel *cp;
   1877 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
   1878 	int channel;
   1879 	u_int32_t ideconf;
   1880 	bus_size_t cmdsize, ctlsize;
   1881 
   1882 	if (pciide_chipen(sc, pa) == 0)
   1883 		return;
   1884 	printf("%s: bus-master DMA support present",
   1885 	    sc->sc_wdcdev.sc_dev.dv_xname);
   1886 	pciide_mapreg_dma(sc, pa);
   1887 	printf("\n");
   1888 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   1889 	    WDC_CAPABILITY_MODE;
   1890 	if (sc->sc_dma_ok) {
   1891 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   1892 		sc->sc_wdcdev.irqack = pciide_irqack;
   1893 		if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
   1894 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1895 	}
   1896 	sc->sc_wdcdev.PIO_cap = 4;
   1897 	sc->sc_wdcdev.DMA_cap = 2;
   1898 	sc->sc_wdcdev.UDMA_cap = 2;
   1899 	sc->sc_wdcdev.set_modes = apollo_setup_channel;
   1900 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   1901 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   1902 
   1903 	WDCDEBUG_PRINT(("apollo_chip_map: old APO_IDECONF=0x%x, "
   1904 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1905 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
   1906 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
   1907 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1908 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
   1909 	    DEBUG_PROBE);
   1910 
   1911 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1912 		cp = &sc->pciide_channels[channel];
   1913 		if (pciide_chansetup(sc, channel, interface) == 0)
   1914 			continue;
   1915 
   1916 		ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
   1917 		if ((ideconf & APO_IDECONF_EN(channel)) == 0) {
   1918 			printf("%s: %s channel ignored (disabled)\n",
   1919 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1920 			continue;
   1921 		}
   1922 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   1923 		    pciide_pci_intr);
   1924 		if (cp->hw_ok == 0)
   1925 			continue;
   1926 		if (pciide_chan_candisable(cp)) {
   1927 			ideconf &= ~APO_IDECONF_EN(channel);
   1928 			pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF,
   1929 			    ideconf);
   1930 		}
   1931 		pciide_map_compat_intr(pa, cp, channel, interface);
   1932 
   1933 		if (cp->hw_ok == 0)
   1934 			continue;
   1935 		apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1936 	}
   1937 	WDCDEBUG_PRINT(("apollo_chip_map: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1938 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1939 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
   1940 }
   1941 
   1942 void
   1943 apollo_setup_channel(chp)
   1944 	struct channel_softc *chp;
   1945 {
   1946 	u_int32_t udmatim_reg, datatim_reg;
   1947 	u_int8_t idedma_ctl;
   1948 	int mode, drive;
   1949 	struct ata_drive_datas *drvp;
   1950 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1951 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1952 
   1953 	idedma_ctl = 0;
   1954 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
   1955 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
   1956 	datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
   1957 	udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
   1958 
   1959 	/* setup DMA if needed */
   1960 	pciide_channel_dma_setup(cp);
   1961 
   1962 	for (drive = 0; drive < 2; drive++) {
   1963 		drvp = &chp->ch_drive[drive];
   1964 		/* If no drive, skip */
   1965 		if ((drvp->drive_flags & DRIVE) == 0)
   1966 			continue;
   1967 		/* add timing values, setup DMA if needed */
   1968 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1969 		    (drvp->drive_flags & DRIVE_UDMA) == 0)) {
   1970 			mode = drvp->PIO_mode;
   1971 			goto pio;
   1972 		}
   1973 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1974 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1975 			/* use Ultra/DMA */
   1976 			drvp->drive_flags &= ~DRIVE_DMA;
   1977 			udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
   1978 			    APO_UDMA_EN_MTH(chp->channel, drive) |
   1979 			    APO_UDMA_TIME(chp->channel, drive,
   1980 				apollo_udma_tim[drvp->UDMA_mode]);
   1981 			/* can use PIO timings, MW DMA unused */
   1982 			mode = drvp->PIO_mode;
   1983 		} else {
   1984 			/* use Multiword DMA */
   1985 			drvp->drive_flags &= ~DRIVE_UDMA;
   1986 			/* mode = min(pio, dma+2) */
   1987 			if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1988 				mode = drvp->PIO_mode;
   1989 			else
   1990 				mode = drvp->DMA_mode + 2;
   1991 		}
   1992 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1993 
   1994 pio:		/* setup PIO mode */
   1995 		if (mode <= 2) {
   1996 			drvp->DMA_mode = 0;
   1997 			drvp->PIO_mode = 0;
   1998 			mode = 0;
   1999 		} else {
   2000 			drvp->PIO_mode = mode;
   2001 			drvp->DMA_mode = mode - 2;
   2002 		}
   2003 		datatim_reg |=
   2004 		    APO_DATATIM_PULSE(chp->channel, drive,
   2005 			apollo_pio_set[mode]) |
   2006 		    APO_DATATIM_RECOV(chp->channel, drive,
   2007 			apollo_pio_rec[mode]);
   2008 	}
   2009 	if (idedma_ctl != 0) {
   2010 		/* Add software bits in status register */
   2011 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2012 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   2013 		    idedma_ctl);
   2014 	}
   2015 	pciide_print_modes(cp);
   2016 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
   2017 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
   2018 }
   2019 
   2020 void
   2021 cmd_channel_map(pa, sc, channel)
   2022 	struct pci_attach_args *pa;
   2023 	struct pciide_softc *sc;
   2024 	int channel;
   2025 {
   2026 	struct pciide_channel *cp = &sc->pciide_channels[channel];
   2027 	bus_size_t cmdsize, ctlsize;
   2028 	u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
   2029 	int interface;
   2030 
   2031 	/*
   2032 	 * The 0648/0649 can be told to identify as a RAID controller.
   2033 	 * In this case, we have to fake interface
   2034 	 */
   2035 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_IDE) {
   2036 		interface = PCIIDE_INTERFACE_SETTABLE(0) |
   2037 		    PCIIDE_INTERFACE_SETTABLE(1);
   2038 		if (pciide_pci_read(pa->pa_pc, pa->pa_tag, CMD_CONF) &
   2039 		    CMD_CONF_DSA1)
   2040 			interface |= PCIIDE_INTERFACE_PCI(0) |
   2041 			    PCIIDE_INTERFACE_PCI(1);
   2042 	} else {
   2043 		interface = PCI_INTERFACE(pa->pa_class);
   2044 	}
   2045 
   2046 	sc->wdc_chanarray[channel] = &cp->wdc_channel;
   2047 	cp->name = PCIIDE_CHANNEL_NAME(channel);
   2048 	cp->wdc_channel.channel = channel;
   2049 	cp->wdc_channel.wdc = &sc->sc_wdcdev;
   2050 
   2051 	if (channel > 0) {
   2052 		cp->wdc_channel.ch_queue =
   2053 		    sc->pciide_channels[0].wdc_channel.ch_queue;
   2054 	} else {
   2055 		cp->wdc_channel.ch_queue =
   2056 		    malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
   2057 	}
   2058 	if (cp->wdc_channel.ch_queue == NULL) {
   2059 		printf("%s %s channel: "
   2060 		    "can't allocate memory for command queue",
   2061 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2062 		    return;
   2063 	}
   2064 
   2065 	printf("%s: %s channel %s to %s mode\n",
   2066 	    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
   2067 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
   2068 	    "configured" : "wired",
   2069 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
   2070 	    "native-PCI" : "compatibility");
   2071 
   2072 	/*
   2073 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   2074 	 * there's no way to disable the first channel without disabling
   2075 	 * the whole device
   2076 	 */
   2077 	if (channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
   2078 		printf("%s: %s channel ignored (disabled)\n",
   2079 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2080 		return;
   2081 	}
   2082 
   2083 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize, cmd_pci_intr);
   2084 	if (cp->hw_ok == 0)
   2085 		return;
   2086 	if (channel == 1) {
   2087 		if (pciide_chan_candisable(cp)) {
   2088 			ctrl &= ~CMD_CTRL_2PORT;
   2089 			pciide_pci_write(pa->pa_pc, pa->pa_tag,
   2090 			    CMD_CTRL, ctrl);
   2091 		}
   2092 	}
   2093 	pciide_map_compat_intr(pa, cp, channel, interface);
   2094 }
   2095 
   2096 int
   2097 cmd_pci_intr(arg)
   2098 	void *arg;
   2099 {
   2100 	struct pciide_softc *sc = arg;
   2101 	struct pciide_channel *cp;
   2102 	struct channel_softc *wdc_cp;
   2103 	int i, rv, crv;
   2104 	u_int32_t priirq, secirq;
   2105 
   2106 	rv = 0;
   2107 	priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
   2108 	secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
   2109 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
   2110 		cp = &sc->pciide_channels[i];
   2111 		wdc_cp = &cp->wdc_channel;
   2112 		/* If a compat channel skip. */
   2113 		if (cp->compat)
   2114 			continue;
   2115 		if ((i == 0 && (priirq & CMD_CONF_DRV0_INTR)) ||
   2116 		    (i == 1 && (secirq & CMD_ARTTIM23_IRQ))) {
   2117 			crv = wdcintr(wdc_cp);
   2118 			if (crv == 0)
   2119 				printf("%s:%d: bogus intr\n",
   2120 				    sc->sc_wdcdev.sc_dev.dv_xname, i);
   2121 			else
   2122 				rv = 1;
   2123 		}
   2124 	}
   2125 	return rv;
   2126 }
   2127 
   2128 void
   2129 cmd_chip_map(sc, pa)
   2130 	struct pciide_softc *sc;
   2131 	struct pci_attach_args *pa;
   2132 {
   2133 	int channel;
   2134 
   2135 	/*
   2136 	 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
   2137 	 * and base adresses registers can be disabled at
   2138 	 * hardware level. In this case, the device is wired
   2139 	 * in compat mode and its first channel is always enabled,
   2140 	 * but we can't rely on PCI_COMMAND_IO_ENABLE.
   2141 	 * In fact, it seems that the first channel of the CMD PCI0640
   2142 	 * can't be disabled.
   2143 	 */
   2144 
   2145 #ifdef PCIIDE_CMD064x_DISABLE
   2146 	if (pciide_chipen(sc, pa) == 0)
   2147 		return;
   2148 #endif
   2149 
   2150 	printf("%s: hardware does not support DMA\n",
   2151 	    sc->sc_wdcdev.sc_dev.dv_xname);
   2152 	sc->sc_dma_ok = 0;
   2153 
   2154 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2155 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   2156 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16;
   2157 
   2158 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2159 		cmd_channel_map(pa, sc, channel);
   2160 	}
   2161 }
   2162 
   2163 void
   2164 cmd0643_9_chip_map(sc, pa)
   2165 	struct pciide_softc *sc;
   2166 	struct pci_attach_args *pa;
   2167 {
   2168 	struct pciide_channel *cp;
   2169 	int channel;
   2170 
   2171 	/*
   2172 	 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
   2173 	 * and base adresses registers can be disabled at
   2174 	 * hardware level. In this case, the device is wired
   2175 	 * in compat mode and its first channel is always enabled,
   2176 	 * but we can't rely on PCI_COMMAND_IO_ENABLE.
   2177 	 * In fact, it seems that the first channel of the CMD PCI0640
   2178 	 * can't be disabled.
   2179 	 */
   2180 
   2181 #ifdef PCIIDE_CMD064x_DISABLE
   2182 	if (pciide_chipen(sc, pa) == 0)
   2183 		return;
   2184 #endif
   2185 	printf("%s: bus-master DMA support present",
   2186 	    sc->sc_wdcdev.sc_dev.dv_xname);
   2187 	pciide_mapreg_dma(sc, pa);
   2188 	printf("\n");
   2189 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   2190 	    WDC_CAPABILITY_MODE;
   2191 	if (sc->sc_dma_ok) {
   2192 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   2193 		switch (sc->sc_pp->ide_product) {
   2194 		case PCI_PRODUCT_CMDTECH_649:
   2195 		case PCI_PRODUCT_CMDTECH_648:
   2196 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   2197 			sc->sc_wdcdev.UDMA_cap = 4;
   2198 		case PCI_PRODUCT_CMDTECH_646:
   2199 			sc->sc_wdcdev.irqack = cmd646_9_irqack;
   2200 			break;
   2201 		default:
   2202 			sc->sc_wdcdev.irqack = pciide_irqack;
   2203 		}
   2204 	}
   2205 
   2206 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2207 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   2208 	sc->sc_wdcdev.PIO_cap = 4;
   2209 	sc->sc_wdcdev.DMA_cap = 2;
   2210 	sc->sc_wdcdev.set_modes = cmd0643_9_setup_channel;
   2211 
   2212 	WDCDEBUG_PRINT(("cmd0643_9_chip_map: old timings reg 0x%x 0x%x\n",
   2213 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   2214 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   2215 		DEBUG_PROBE);
   2216 
   2217 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2218 		cp = &sc->pciide_channels[channel];
   2219 		cmd_channel_map(pa, sc, channel);
   2220 		if (cp->hw_ok == 0)
   2221 			continue;
   2222 		cmd0643_9_setup_channel(&cp->wdc_channel);
   2223 	}
   2224 	pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
   2225 	WDCDEBUG_PRINT(("cmd0643_9_chip_map: timings reg now 0x%x 0x%x\n",
   2226 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   2227 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   2228 	    DEBUG_PROBE);
   2229 }
   2230 
   2231 void
   2232 cmd0643_9_setup_channel(chp)
   2233 	struct channel_softc *chp;
   2234 {
   2235 	struct ata_drive_datas *drvp;
   2236 	u_int8_t tim;
   2237 	u_int32_t idedma_ctl, udma_reg;
   2238 	int drive;
   2239 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2240 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2241 
   2242 	idedma_ctl = 0;
   2243 	/* setup DMA if needed */
   2244 	pciide_channel_dma_setup(cp);
   2245 
   2246 	for (drive = 0; drive < 2; drive++) {
   2247 		drvp = &chp->ch_drive[drive];
   2248 		/* If no drive, skip */
   2249 		if ((drvp->drive_flags & DRIVE) == 0)
   2250 			continue;
   2251 		/* add timing values, setup DMA if needed */
   2252 		tim = cmd0643_9_data_tim_pio[drvp->PIO_mode];
   2253 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
   2254 			if (drvp->drive_flags & DRIVE_UDMA) {
   2255 				/* UltraDMA on a 0648 or 0649 */
   2256 				udma_reg = pciide_pci_read(sc->sc_pc,
   2257 				    sc->sc_tag, CMD_UDMATIM(chp->channel));
   2258 				if (drvp->UDMA_mode > 2 &&
   2259 				    (pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2260 				    CMD_BICSR) &
   2261 				    CMD_BICSR_80(chp->channel)) == 0)
   2262 					drvp->UDMA_mode = 2;
   2263 				if (drvp->UDMA_mode > 2)
   2264 					udma_reg &= ~CMD_UDMATIM_UDMA33(drive);
   2265 				else
   2266 					udma_reg |= CMD_UDMATIM_UDMA33(drive);
   2267 				udma_reg |= CMD_UDMATIM_UDMA(drive);
   2268 				udma_reg &= ~(CMD_UDMATIM_TIM_MASK <<
   2269 				    CMD_UDMATIM_TIM_OFF(drive));
   2270 				udma_reg |=
   2271 				    (cmd0648_9_tim_udma[drvp->UDMA_mode] <<
   2272 				    CMD_UDMATIM_TIM_OFF(drive));
   2273 				pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2274 				    CMD_UDMATIM(chp->channel), udma_reg);
   2275 			} else {
   2276 				/*
   2277 				 * use Multiword DMA.
   2278 				 * Timings will be used for both PIO and DMA,
   2279 				 * so adjust DMA mode if needed
   2280 				 * if we have a 0648/9, turn off UDMA
   2281 				 */
   2282 				if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   2283 					udma_reg = pciide_pci_read(sc->sc_pc,
   2284 					    sc->sc_tag,
   2285 					    CMD_UDMATIM(chp->channel));
   2286 					udma_reg &= ~CMD_UDMATIM_UDMA(drive);
   2287 					pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2288 					    CMD_UDMATIM(chp->channel),
   2289 					    udma_reg);
   2290 				}
   2291 				if (drvp->PIO_mode >= 3 &&
   2292 				    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
   2293 					drvp->DMA_mode = drvp->PIO_mode - 2;
   2294 				}
   2295 				tim = cmd0643_9_data_tim_dma[drvp->DMA_mode];
   2296 			}
   2297 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2298 		}
   2299 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2300 		    CMD_DATA_TIM(chp->channel, drive), tim);
   2301 	}
   2302 	if (idedma_ctl != 0) {
   2303 		/* Add software bits in status register */
   2304 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2305 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   2306 		    idedma_ctl);
   2307 	}
   2308 	pciide_print_modes(cp);
   2309 }
   2310 
   2311 void
   2312 cmd646_9_irqack(chp)
   2313 	struct channel_softc *chp;
   2314 {
   2315 	u_int32_t priirq, secirq;
   2316 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2317 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2318 
   2319 	if (chp->channel == 0) {
   2320 		priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
   2321 		pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_CONF, priirq);
   2322 	} else {
   2323 		secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
   2324 		pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23, secirq);
   2325 	}
   2326 	pciide_irqack(chp);
   2327 }
   2328 
   2329 void
   2330 cy693_chip_map(sc, pa)
   2331 	struct pciide_softc *sc;
   2332 	struct pci_attach_args *pa;
   2333 {
   2334 	struct pciide_channel *cp;
   2335 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
   2336 	bus_size_t cmdsize, ctlsize;
   2337 
   2338 	if (pciide_chipen(sc, pa) == 0)
   2339 		return;
   2340 	/*
   2341 	 * this chip has 2 PCI IDE functions, one for primary and one for
   2342 	 * secondary. So we need to call pciide_mapregs_compat() with
   2343 	 * the real channel
   2344 	 */
   2345 	if (pa->pa_function == 1) {
   2346 		sc->sc_cy_compatchan = 0;
   2347 	} else if (pa->pa_function == 2) {
   2348 		sc->sc_cy_compatchan = 1;
   2349 	} else {
   2350 		printf("%s: unexpected PCI function %d\n",
   2351 		    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
   2352 		return;
   2353 	}
   2354 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
   2355 		printf("%s: bus-master DMA support present, "
   2356 		    "but unused (no driver support)",
   2357 		    sc->sc_wdcdev.sc_dev.dv_xname);
   2358 	} else {
   2359 		printf("%s: hardware does not support DMA",
   2360 		    sc->sc_wdcdev.sc_dev.dv_xname);
   2361 	}
   2362 	sc->sc_dma_ok = 0;
   2363 	printf("\n");
   2364 
   2365 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   2366 	    WDC_CAPABILITY_MODE;
   2367 	sc->sc_wdcdev.PIO_cap = 4;
   2368 	sc->sc_wdcdev.set_modes = cy693_setup_channel;
   2369 
   2370 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2371 	sc->sc_wdcdev.nchannels = 1;
   2372 
   2373 	/* Only one channel for this chip; if we are here it's enabled */
   2374 	cp = &sc->pciide_channels[0];
   2375 	sc->wdc_chanarray[0] = &cp->wdc_channel;
   2376 	cp->name = PCIIDE_CHANNEL_NAME(0);
   2377 	cp->wdc_channel.channel = 0;
   2378 	cp->wdc_channel.wdc = &sc->sc_wdcdev;
   2379 	cp->wdc_channel.ch_queue =
   2380 	    malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
   2381 	if (cp->wdc_channel.ch_queue == NULL) {
   2382 		printf("%s primary channel: "
   2383 		    "can't allocate memory for command queue",
   2384 		sc->sc_wdcdev.sc_dev.dv_xname);
   2385 		return;
   2386 	}
   2387 	printf("%s: primary channel %s to ",
   2388 	    sc->sc_wdcdev.sc_dev.dv_xname,
   2389 	    (interface & PCIIDE_INTERFACE_SETTABLE(0)) ?
   2390 	    "configured" : "wired");
   2391 	if (interface & PCIIDE_INTERFACE_PCI(0)) {
   2392 		printf("native-PCI");
   2393 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize,
   2394 		    pciide_pci_intr);
   2395 	} else {
   2396 		printf("compatibility");
   2397 		cp->hw_ok = pciide_mapregs_compat(pa, cp, sc->sc_cy_compatchan,
   2398 		    &cmdsize, &ctlsize);
   2399 	}
   2400 	printf(" mode\n");
   2401 	cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
   2402 	cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
   2403 	wdcattach(&cp->wdc_channel);
   2404 	if (pciide_chan_candisable(cp)) {
   2405 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   2406 		    PCI_COMMAND_STATUS_REG, 0);
   2407 	}
   2408 	pciide_map_compat_intr(pa, cp, sc->sc_cy_compatchan, interface);
   2409 	if (cp->hw_ok == 0)
   2410 		return;
   2411 	WDCDEBUG_PRINT(("cy693_chip_map: old timings reg 0x%x\n",
   2412 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),DEBUG_PROBE);
   2413 	cy693_setup_channel(&cp->wdc_channel);
   2414 	WDCDEBUG_PRINT(("cy693_chip_map: new timings reg 0x%x\n",
   2415 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
   2416 }
   2417 
   2418 void
   2419 cy693_setup_channel(chp)
   2420 	struct channel_softc *chp;
   2421 {
   2422 	struct ata_drive_datas *drvp;
   2423 	int drive;
   2424 	u_int32_t cy_cmd_ctrl;
   2425 	u_int32_t idedma_ctl;
   2426 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2427 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2428 	cy_cmd_ctrl = idedma_ctl = 0;
   2429 
   2430 	for (drive = 0; drive < 2; drive++) {
   2431 		drvp = &chp->ch_drive[drive];
   2432 		/* If no drive, skip */
   2433 		if ((drvp->drive_flags & DRIVE) == 0)
   2434 			continue;
   2435 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   2436 		    CY_CMD_CTRL_IOW_PULSE_OFF(drive));
   2437 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   2438 		    CY_CMD_CTRL_IOW_REC_OFF(drive));
   2439 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   2440 		    CY_CMD_CTRL_IOR_PULSE_OFF(drive));
   2441 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   2442 		    CY_CMD_CTRL_IOR_REC_OFF(drive));
   2443 	}
   2444 	pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
   2445 
   2446 	pciide_print_modes(cp);
   2447 
   2448 	if (idedma_ctl != 0) {
   2449 		/* Add software bits in status register */
   2450 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2451 		    IDEDMA_CTL, idedma_ctl);
   2452 	}
   2453 }
   2454 
   2455 void
   2456 sis_chip_map(sc, pa)
   2457 	struct pciide_softc *sc;
   2458 	struct pci_attach_args *pa;
   2459 {
   2460 	struct pciide_channel *cp;
   2461 	int channel;
   2462 	u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
   2463 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
   2464 	pcireg_t rev = PCI_REVISION(pa->pa_class);
   2465 	bus_size_t cmdsize, ctlsize;
   2466 
   2467 	if (pciide_chipen(sc, pa) == 0)
   2468 		return;
   2469 	printf("%s: bus-master DMA support present",
   2470 	    sc->sc_wdcdev.sc_dev.dv_xname);
   2471 	pciide_mapreg_dma(sc, pa);
   2472 	printf("\n");
   2473 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   2474 	    WDC_CAPABILITY_MODE;
   2475 	if (sc->sc_dma_ok) {
   2476 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   2477 		sc->sc_wdcdev.irqack = pciide_irqack;
   2478 		if (rev >= 0xd0)
   2479 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   2480 	}
   2481 
   2482 	sc->sc_wdcdev.PIO_cap = 4;
   2483 	sc->sc_wdcdev.DMA_cap = 2;
   2484 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA)
   2485 		sc->sc_wdcdev.UDMA_cap = 2;
   2486 	sc->sc_wdcdev.set_modes = sis_setup_channel;
   2487 
   2488 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2489 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   2490 
   2491 	pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
   2492 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
   2493 	    SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
   2494 
   2495 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2496 		cp = &sc->pciide_channels[channel];
   2497 		if (pciide_chansetup(sc, channel, interface) == 0)
   2498 			continue;
   2499 		if ((channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
   2500 		    (channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
   2501 			printf("%s: %s channel ignored (disabled)\n",
   2502 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2503 			continue;
   2504 		}
   2505 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   2506 		    pciide_pci_intr);
   2507 		if (cp->hw_ok == 0)
   2508 			continue;
   2509 		if (pciide_chan_candisable(cp)) {
   2510 			if (channel == 0)
   2511 				sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
   2512 			else
   2513 				sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
   2514 			pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0,
   2515 			    sis_ctr0);
   2516 		}
   2517 		pciide_map_compat_intr(pa, cp, channel, interface);
   2518 		if (cp->hw_ok == 0)
   2519 			continue;
   2520 		sis_setup_channel(&cp->wdc_channel);
   2521 	}
   2522 }
   2523 
   2524 void
   2525 sis_setup_channel(chp)
   2526 	struct channel_softc *chp;
   2527 {
   2528 	struct ata_drive_datas *drvp;
   2529 	int drive;
   2530 	u_int32_t sis_tim;
   2531 	u_int32_t idedma_ctl;
   2532 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2533 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2534 
   2535 	WDCDEBUG_PRINT(("sis_setup_channel: old timings reg for "
   2536 	    "channel %d 0x%x\n", chp->channel,
   2537 	    pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
   2538 	    DEBUG_PROBE);
   2539 	sis_tim = 0;
   2540 	idedma_ctl = 0;
   2541 	/* setup DMA if needed */
   2542 	pciide_channel_dma_setup(cp);
   2543 
   2544 	for (drive = 0; drive < 2; drive++) {
   2545 		drvp = &chp->ch_drive[drive];
   2546 		/* If no drive, skip */
   2547 		if ((drvp->drive_flags & DRIVE) == 0)
   2548 			continue;
   2549 		/* add timing values, setup DMA if needed */
   2550 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2551 		    (drvp->drive_flags & DRIVE_UDMA) == 0)
   2552 			goto pio;
   2553 
   2554 		if (drvp->drive_flags & DRIVE_UDMA) {
   2555 			/* use Ultra/DMA */
   2556 			drvp->drive_flags &= ~DRIVE_DMA;
   2557 			sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
   2558 			    SIS_TIM_UDMA_TIME_OFF(drive);
   2559 			sis_tim |= SIS_TIM_UDMA_EN(drive);
   2560 		} else {
   2561 			/*
   2562 			 * use Multiword DMA
   2563 			 * Timings will be used for both PIO and DMA,
   2564 			 * so adjust DMA mode if needed
   2565 			 */
   2566 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2567 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2568 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2569 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2570 				    drvp->PIO_mode - 2 : 0;
   2571 			if (drvp->DMA_mode == 0)
   2572 				drvp->PIO_mode = 0;
   2573 		}
   2574 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2575 pio:		sis_tim |= sis_pio_act[drvp->PIO_mode] <<
   2576 		    SIS_TIM_ACT_OFF(drive);
   2577 		sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
   2578 		    SIS_TIM_REC_OFF(drive);
   2579 	}
   2580 	WDCDEBUG_PRINT(("sis_setup_channel: new timings reg for "
   2581 	    "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
   2582 	pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
   2583 	if (idedma_ctl != 0) {
   2584 		/* Add software bits in status register */
   2585 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2586 		    IDEDMA_CTL, idedma_ctl);
   2587 	}
   2588 	pciide_print_modes(cp);
   2589 }
   2590 
   2591 void
   2592 acer_chip_map(sc, pa)
   2593 	struct pciide_softc *sc;
   2594 	struct pci_attach_args *pa;
   2595 {
   2596 	struct pciide_channel *cp;
   2597 	int channel;
   2598 	pcireg_t cr, interface;
   2599 	bus_size_t cmdsize, ctlsize;
   2600 
   2601 	if (pciide_chipen(sc, pa) == 0)
   2602 		return;
   2603 	printf("%s: bus-master DMA support present",
   2604 	    sc->sc_wdcdev.sc_dev.dv_xname);
   2605 	pciide_mapreg_dma(sc, pa);
   2606 	printf("\n");
   2607 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   2608 	    WDC_CAPABILITY_MODE;
   2609 	if (sc->sc_dma_ok) {
   2610 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   2611 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
   2612 		sc->sc_wdcdev.irqack = pciide_irqack;
   2613 	}
   2614 
   2615 	sc->sc_wdcdev.PIO_cap = 4;
   2616 	sc->sc_wdcdev.DMA_cap = 2;
   2617 	sc->sc_wdcdev.UDMA_cap = 2;
   2618 	sc->sc_wdcdev.set_modes = acer_setup_channel;
   2619 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2620 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   2621 
   2622 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
   2623 	    (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
   2624 		ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
   2625 
   2626 	/* Enable "microsoft register bits" R/W. */
   2627 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
   2628 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
   2629 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
   2630 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
   2631 	    ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
   2632 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
   2633 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
   2634 	    ~ACER_CHANSTATUSREGS_RO);
   2635 	cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
   2636 	cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
   2637 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2638 	/* Don't use cr, re-read the real register content instead */
   2639 	interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
   2640 	    PCI_CLASS_REG));
   2641 
   2642 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2643 		cp = &sc->pciide_channels[channel];
   2644 		if (pciide_chansetup(sc, channel, interface) == 0)
   2645 			continue;
   2646 		if ((interface & PCIIDE_CHAN_EN(channel)) == 0) {
   2647 			printf("%s: %s channel ignored (disabled)\n",
   2648 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2649 			continue;
   2650 		}
   2651 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   2652 		    acer_pci_intr);
   2653 		if (cp->hw_ok == 0)
   2654 			continue;
   2655 		if (pciide_chan_candisable(cp)) {
   2656 			cr &= ~(PCIIDE_CHAN_EN(channel) << PCI_INTERFACE_SHIFT);
   2657 			pci_conf_write(sc->sc_pc, sc->sc_tag,
   2658 			    PCI_CLASS_REG, cr);
   2659 		}
   2660 		pciide_map_compat_intr(pa, cp, channel, interface);
   2661 		acer_setup_channel(&cp->wdc_channel);
   2662 	}
   2663 }
   2664 
   2665 void
   2666 acer_setup_channel(chp)
   2667 	struct channel_softc *chp;
   2668 {
   2669 	struct ata_drive_datas *drvp;
   2670 	int drive;
   2671 	u_int32_t acer_fifo_udma;
   2672 	u_int32_t idedma_ctl;
   2673 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2674 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2675 
   2676 	idedma_ctl = 0;
   2677 	acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
   2678 	WDCDEBUG_PRINT(("acer_setup_channel: old fifo/udma reg 0x%x\n",
   2679 	    acer_fifo_udma), DEBUG_PROBE);
   2680 	/* setup DMA if needed */
   2681 	pciide_channel_dma_setup(cp);
   2682 
   2683 	for (drive = 0; drive < 2; drive++) {
   2684 		drvp = &chp->ch_drive[drive];
   2685 		/* If no drive, skip */
   2686 		if ((drvp->drive_flags & DRIVE) == 0)
   2687 			continue;
   2688 		WDCDEBUG_PRINT(("acer_setup_channel: old timings reg for "
   2689 		    "channel %d drive %d 0x%x\n", chp->channel, drive,
   2690 		    pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2691 		    ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
   2692 		/* clear FIFO/DMA mode */
   2693 		acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
   2694 		    ACER_UDMA_EN(chp->channel, drive) |
   2695 		    ACER_UDMA_TIM(chp->channel, drive, 0x7));
   2696 
   2697 		/* add timing values, setup DMA if needed */
   2698 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2699 		    (drvp->drive_flags & DRIVE_UDMA) == 0) {
   2700 			acer_fifo_udma |=
   2701 			    ACER_FTH_OPL(chp->channel, drive, 0x1);
   2702 			goto pio;
   2703 		}
   2704 
   2705 		acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
   2706 		if (drvp->drive_flags & DRIVE_UDMA) {
   2707 			/* use Ultra/DMA */
   2708 			drvp->drive_flags &= ~DRIVE_DMA;
   2709 			acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
   2710 			acer_fifo_udma |=
   2711 			    ACER_UDMA_TIM(chp->channel, drive,
   2712 				acer_udma[drvp->UDMA_mode]);
   2713 		} else {
   2714 			/*
   2715 			 * use Multiword DMA
   2716 			 * Timings will be used for both PIO and DMA,
   2717 			 * so adjust DMA mode if needed
   2718 			 */
   2719 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2720 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2721 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2722 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2723 				    drvp->PIO_mode - 2 : 0;
   2724 			if (drvp->DMA_mode == 0)
   2725 				drvp->PIO_mode = 0;
   2726 		}
   2727 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2728 pio:		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2729 		    ACER_IDETIM(chp->channel, drive),
   2730 		    acer_pio[drvp->PIO_mode]);
   2731 	}
   2732 	WDCDEBUG_PRINT(("acer_setup_channel: new fifo/udma reg 0x%x\n",
   2733 	    acer_fifo_udma), DEBUG_PROBE);
   2734 	pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
   2735 	if (idedma_ctl != 0) {
   2736 		/* Add software bits in status register */
   2737 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2738 		    IDEDMA_CTL, idedma_ctl);
   2739 	}
   2740 	pciide_print_modes(cp);
   2741 }
   2742 
   2743 int
   2744 acer_pci_intr(arg)
   2745 	void *arg;
   2746 {
   2747 	struct pciide_softc *sc = arg;
   2748 	struct pciide_channel *cp;
   2749 	struct channel_softc *wdc_cp;
   2750 	int i, rv, crv;
   2751 	u_int32_t chids;
   2752 
   2753 	rv = 0;
   2754 	chids = pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CHIDS);
   2755 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
   2756 		cp = &sc->pciide_channels[i];
   2757 		wdc_cp = &cp->wdc_channel;
   2758 		/* If a compat channel skip. */
   2759 		if (cp->compat)
   2760 			continue;
   2761 		if (chids & ACER_CHIDS_INT(i)) {
   2762 			crv = wdcintr(wdc_cp);
   2763 			if (crv == 0)
   2764 				printf("%s:%d: bogus intr\n",
   2765 				    sc->sc_wdcdev.sc_dev.dv_xname, i);
   2766 			else
   2767 				rv = 1;
   2768 		}
   2769 	}
   2770 	return rv;
   2771 }
   2772 
   2773 void
   2774 hpt_chip_map(sc, pa)
   2775         struct pciide_softc *sc;
   2776 	struct pci_attach_args *pa;
   2777 {
   2778 	struct pciide_channel *cp;
   2779 	int i, compatchan, revision;
   2780 	pcireg_t interface;
   2781 	bus_size_t cmdsize, ctlsize;
   2782 
   2783 	if (pciide_chipen(sc, pa) == 0)
   2784 		return;
   2785 	revision = PCI_REVISION(pa->pa_class);
   2786 
   2787 	/*
   2788 	 * when the chip is in native mode it identifies itself as a
   2789 	 * 'misc mass storage'. Fake interface in this case.
   2790 	 */
   2791 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
   2792 		interface = PCI_INTERFACE(pa->pa_class);
   2793 	} else {
   2794 		interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
   2795 		    PCIIDE_INTERFACE_PCI(0);
   2796 		if (revision == HPT370_REV)
   2797 			interface |= PCIIDE_INTERFACE_PCI(1);
   2798 	}
   2799 
   2800 	printf("%s: bus-master DMA support present",
   2801 		sc->sc_wdcdev.sc_dev.dv_xname);
   2802 	pciide_mapreg_dma(sc, pa);
   2803 	printf("\n");
   2804 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   2805 	    WDC_CAPABILITY_MODE;
   2806 	if (sc->sc_dma_ok) {
   2807 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   2808 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
   2809 		sc->sc_wdcdev.irqack = pciide_irqack;
   2810 	}
   2811 	sc->sc_wdcdev.PIO_cap = 4;
   2812 	sc->sc_wdcdev.DMA_cap = 2;
   2813 	sc->sc_wdcdev.UDMA_cap = 4;
   2814 
   2815 	sc->sc_wdcdev.set_modes = hpt_setup_channel;
   2816 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   2817 	if (revision == HPT366_REV) {
   2818 		/*
   2819 		 * The 366 has 2 PCI IDE functions, one for primary and one
   2820 		 * for secondary. So we need to call pciide_mapregs_compat()
   2821 		 * with the real channel
   2822 		 */
   2823 		if (pa->pa_function == 0) {
   2824 			compatchan = 0;
   2825 		} else if (pa->pa_function == 1) {
   2826 			compatchan = 1;
   2827 		} else {
   2828 			printf("%s: unexpected PCI function %d\n",
   2829 			    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
   2830 			return;
   2831 		}
   2832 		sc->sc_wdcdev.nchannels = 1;
   2833 	} else {
   2834 		sc->sc_wdcdev.nchannels = 2;
   2835 	}
   2836 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
   2837 		cp = &sc->pciide_channels[i];
   2838 		if (sc->sc_wdcdev.nchannels > 1) {
   2839 			compatchan = i;
   2840 			if((pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2841 			   HPT370_CTRL1(i)) & HPT370_CTRL1_EN) == 0) {
   2842 				printf("%s: %s channel ignored (disabled)\n",
   2843 				    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2844 				continue;
   2845 			}
   2846 		}
   2847 		if (pciide_chansetup(sc, i, interface) == 0)
   2848 			continue;
   2849 		if (interface & PCIIDE_INTERFACE_PCI(i)) {
   2850 			cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize,
   2851 			    &ctlsize, hpt_pci_intr);
   2852 		} else {
   2853 			cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
   2854 			    &cmdsize, &ctlsize);
   2855 		}
   2856 		if (cp->hw_ok == 0)
   2857 			return;
   2858 		cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
   2859 		cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
   2860 		wdcattach(&cp->wdc_channel);
   2861 		hpt_setup_channel(&cp->wdc_channel);
   2862 	}
   2863 
   2864 	return;
   2865 }
   2866 
   2867 
   2868 void
   2869 hpt_setup_channel(chp)
   2870 	struct channel_softc *chp;
   2871 {
   2872         struct ata_drive_datas *drvp;
   2873 	int drive;
   2874 	int cable;
   2875 	u_int32_t before, after;
   2876 	u_int32_t idedma_ctl;
   2877 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2878 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2879 
   2880 	cable = pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL);
   2881 
   2882 	/* setup DMA if needed */
   2883 	pciide_channel_dma_setup(cp);
   2884 
   2885 	idedma_ctl = 0;
   2886 
   2887 	/* Per drive settings */
   2888 	for (drive = 0; drive < 2; drive++) {
   2889 		drvp = &chp->ch_drive[drive];
   2890 		/* If no drive, skip */
   2891 		if ((drvp->drive_flags & DRIVE) == 0)
   2892 			continue;
   2893 		before = pci_conf_read(sc->sc_pc, sc->sc_tag,
   2894 					HPT_IDETIM(chp->channel, drive));
   2895 
   2896                 /* add timing values, setup DMA if needed */
   2897                 if (drvp->drive_flags & DRIVE_UDMA) {
   2898 			if ((cable & HPT_CSEL_CBLID(chp->channel)) != 0 &&
   2899 			    drvp->UDMA_mode > 2)
   2900 				drvp->UDMA_mode = 2;
   2901                         after = (sc->sc_wdcdev.nchannels == 2) ?
   2902 			    hpt370_udma[drvp->UDMA_mode] :
   2903 			    hpt366_udma[drvp->UDMA_mode];
   2904                         idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2905                 } else if (drvp->drive_flags & DRIVE_DMA) {
   2906                         /*
   2907                          * use Multiword DMA.
   2908                          * Timings will be used for both PIO and DMA, so adjust
   2909                          * DMA mode if needed
   2910                          */
   2911                         if (drvp->PIO_mode >= 3 &&
   2912                             (drvp->DMA_mode + 2) > drvp->PIO_mode) {
   2913                                 drvp->DMA_mode = drvp->PIO_mode - 2;
   2914                         }
   2915                         after = (sc->sc_wdcdev.nchannels == 2) ?
   2916 			    hpt370_dma[drvp->DMA_mode] :
   2917 			    hpt366_dma[drvp->DMA_mode];
   2918                         idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2919                 } else {
   2920 			/* PIO only */
   2921                 	after = (sc->sc_wdcdev.nchannels == 2) ?
   2922 			    hpt370_pio[drvp->PIO_mode] :
   2923 			    hpt366_pio[drvp->PIO_mode];
   2924 		}
   2925 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   2926                     HPT_IDETIM(chp->channel, drive), after);
   2927 		WDCDEBUG_PRINT(("%s: bus speed register set to 0x%08x "
   2928 		    "(BIOS 0x%08x)\n", drvp->drv_softc->dv_xname,
   2929 		    after, before), DEBUG_PROBE);
   2930 	}
   2931 	if (idedma_ctl != 0) {
   2932 		/* Add software bits in status register */
   2933 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2934 		    IDEDMA_CTL, idedma_ctl);
   2935 	}
   2936 	pciide_print_modes(cp);
   2937 }
   2938 
   2939 int
   2940 hpt_pci_intr(arg)
   2941 	void *arg;
   2942 {
   2943 	struct pciide_softc *sc = arg;
   2944 	struct pciide_channel *cp;
   2945 	struct channel_softc *wdc_cp;
   2946 	int rv = 0;
   2947 	int dmastat, i, crv;
   2948 
   2949 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
   2950 		dmastat = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2951 		    IDEDMA_CTL + IDEDMA_SCH_OFFSET * i);
   2952 		if((dmastat & IDEDMA_CTL_INTR) == 0)
   2953 			continue;
   2954 		cp = &sc->pciide_channels[i];
   2955 		wdc_cp = &cp->wdc_channel;
   2956 		crv = wdcintr(wdc_cp);
   2957 		if (crv == 0) {
   2958 			printf("%s:%d: bogus intr\n",
   2959 			    sc->sc_wdcdev.sc_dev.dv_xname, i);
   2960 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2961 			    IDEDMA_CTL + IDEDMA_SCH_OFFSET * i, dmastat);
   2962 		} else
   2963 			rv = 1;
   2964 	}
   2965 	return rv;
   2966 }
   2967 
   2968 
   2969 /* A macro to test product */
   2970 #define PDC_IS_262(sc) (sc->sc_pp->ide_product == PCI_PRODUCT_PROMISE_ULTRA66)
   2971 
   2972 void
   2973 pdc202xx_chip_map(sc, pa)
   2974         struct pciide_softc *sc;
   2975 	struct pci_attach_args *pa;
   2976 {
   2977 	struct pciide_channel *cp;
   2978 	int channel;
   2979 	pcireg_t interface, st, mode;
   2980 	bus_size_t cmdsize, ctlsize;
   2981 
   2982 	st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
   2983 	WDCDEBUG_PRINT(("pdc202xx_setup_chip: controller state 0x%x\n", st),
   2984 	    DEBUG_PROBE);
   2985 	if (pciide_chipen(sc, pa) == 0)
   2986 		return;
   2987 
   2988 	/* turn off  RAID mode */
   2989 	st &= ~PDC2xx_STATE_IDERAID;
   2990 
   2991 	/*
   2992 	 * can't rely on the PCI_CLASS_REG content if the chip was in raid
   2993 	 * mode. We have to fake interface
   2994 	 */
   2995 	interface = PCIIDE_INTERFACE_SETTABLE(0) | PCIIDE_INTERFACE_SETTABLE(1);
   2996 	if (st & PDC2xx_STATE_NATIVE)
   2997 		interface |= PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
   2998 
   2999 	printf("%s: bus-master DMA support present",
   3000 	    sc->sc_wdcdev.sc_dev.dv_xname);
   3001 	pciide_mapreg_dma(sc, pa);
   3002 	printf("\n");
   3003 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   3004 	    WDC_CAPABILITY_MODE;
   3005 	if (sc->sc_dma_ok) {
   3006 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   3007 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
   3008 		sc->sc_wdcdev.irqack = pciide_irqack;
   3009 	}
   3010 	sc->sc_wdcdev.PIO_cap = 4;
   3011 	sc->sc_wdcdev.DMA_cap = 2;
   3012 	if (PDC_IS_262(sc))
   3013 		sc->sc_wdcdev.UDMA_cap = 4;
   3014 	else
   3015 		sc->sc_wdcdev.UDMA_cap = 2;
   3016 	sc->sc_wdcdev.set_modes = pdc202xx_setup_channel;
   3017 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   3018 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   3019 
   3020 	/* setup failsafe defaults */
   3021 	mode = 0;
   3022 	mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[0]);
   3023 	mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[0]);
   3024 	mode = PDC2xx_TIM_SET_MB(mode, pdc2xx_dma_mb[0]);
   3025 	mode = PDC2xx_TIM_SET_MC(mode, pdc2xx_dma_mc[0]);
   3026 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   3027 		WDCDEBUG_PRINT(("pdc202xx_setup_chip: channel %d drive 0 "
   3028 		    "initial timings  0x%x, now 0x%x\n", channel,
   3029 		    pci_conf_read(sc->sc_pc, sc->sc_tag,
   3030 		    PDC2xx_TIM(channel, 0)), mode | PDC2xx_TIM_IORDYp),
   3031 		    DEBUG_PROBE);
   3032 		pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_TIM(channel, 0),
   3033 		    mode | PDC2xx_TIM_IORDYp);
   3034 		WDCDEBUG_PRINT(("pdc202xx_setup_chip: channel %d drive 1 "
   3035 		    "initial timings  0x%x, now 0x%x\n", channel,
   3036 		    pci_conf_read(sc->sc_pc, sc->sc_tag,
   3037 		    PDC2xx_TIM(channel, 1)), mode), DEBUG_PROBE);
   3038 		pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_TIM(channel, 1),
   3039 		    mode);
   3040 	}
   3041 
   3042 	mode = PDC2xx_SCR_DMA;
   3043 	if (PDC_IS_262(sc)) {
   3044 		mode = PDC2xx_SCR_SET_GEN(mode, PDC262_SCR_GEN_LAT);
   3045 	} else {
   3046 		/* the BIOS set it up this way */
   3047 		mode = PDC2xx_SCR_SET_GEN(mode, 0x1);
   3048 	}
   3049 	mode = PDC2xx_SCR_SET_I2C(mode, 0x3); /* ditto */
   3050 	mode = PDC2xx_SCR_SET_POLL(mode, 0x1); /* ditto */
   3051 	WDCDEBUG_PRINT(("pdc202xx_setup_chip: initial SCR  0x%x, now 0x%x\n",
   3052 	    bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR), mode),
   3053 	    DEBUG_PROBE);
   3054 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR, mode);
   3055 
   3056 	/* controller initial state register is OK even without BIOS */
   3057 	/* Set DMA mode to IDE DMA compatibility */
   3058 	mode = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM);
   3059 	WDCDEBUG_PRINT(("pdc202xx_setup_chip: primary mode 0x%x", mode ),
   3060 	    DEBUG_PROBE);
   3061 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM,
   3062 	    mode | 0x1);
   3063 	mode = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM);
   3064 	WDCDEBUG_PRINT((", secondary mode 0x%x\n", mode ), DEBUG_PROBE);
   3065 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM,
   3066 	    mode | 0x1);
   3067 
   3068 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   3069 		cp = &sc->pciide_channels[channel];
   3070 		if (pciide_chansetup(sc, channel, interface) == 0)
   3071 			continue;
   3072 		if ((st & (PDC_IS_262(sc) ?
   3073 		    PDC262_STATE_EN(channel):PDC246_STATE_EN(channel))) == 0) {
   3074 			printf("%s: %s channel ignored (disabled)\n",
   3075 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   3076 			continue;
   3077 		}
   3078 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   3079 		    pdc202xx_pci_intr);
   3080 		if (cp->hw_ok == 0)
   3081 			continue;
   3082 		if (pciide_chan_candisable(cp))
   3083 			st &= ~(PDC_IS_262(sc) ?
   3084 			    PDC262_STATE_EN(channel):PDC246_STATE_EN(channel));
   3085 		pciide_map_compat_intr(pa, cp, channel, interface);
   3086 		pdc202xx_setup_channel(&cp->wdc_channel);
   3087 	}
   3088 	WDCDEBUG_PRINT(("pdc202xx_setup_chip: new controller state 0x%x\n", st),
   3089 	    DEBUG_PROBE);
   3090 	pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_STATE, st);
   3091 	return;
   3092 }
   3093 
   3094 void
   3095 pdc202xx_setup_channel(chp)
   3096 	struct channel_softc *chp;
   3097 {
   3098         struct ata_drive_datas *drvp;
   3099 	int drive;
   3100 	pcireg_t mode, st;
   3101 	u_int32_t idedma_ctl, scr, atapi;
   3102 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   3103 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   3104 	int channel = chp->channel;
   3105 
   3106 	/* setup DMA if needed */
   3107 	pciide_channel_dma_setup(cp);
   3108 
   3109 	idedma_ctl = 0;
   3110 
   3111 	/* Per channel settings */
   3112 	if (PDC_IS_262(sc)) {
   3113 		scr = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   3114 		    PDC262_U66);
   3115 		st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
   3116 		/* Trimm UDMA mode */
   3117 		if ((st & PDC262_STATE_80P(channel)) != 0 ||
   3118 		    (chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
   3119 		    chp->ch_drive[0].UDMA_mode <= 2) ||
   3120 		    (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
   3121 		    chp->ch_drive[1].UDMA_mode <= 2)) {
   3122 			if (chp->ch_drive[0].UDMA_mode > 2)
   3123 				chp->ch_drive[0].UDMA_mode = 2;
   3124 			if (chp->ch_drive[1].UDMA_mode > 2)
   3125 				chp->ch_drive[1].UDMA_mode = 2;
   3126 		}
   3127 		/* Set U66 if needed */
   3128 		if ((chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
   3129 		    chp->ch_drive[0].UDMA_mode > 2) ||
   3130 		    (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
   3131 		    chp->ch_drive[1].UDMA_mode > 2))
   3132 			scr |= PDC262_U66_EN(channel);
   3133 		else
   3134 			scr &= ~PDC262_U66_EN(channel);
   3135 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   3136 		    PDC262_U66, scr);
   3137 		if (chp->ch_drive[0].drive_flags & DRIVE_ATAPI ||
   3138 			chp->ch_drive[1].drive_flags & DRIVE_ATAPI) {
   3139 			if (((chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
   3140 			    !(chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
   3141 			    (chp->ch_drive[1].drive_flags & DRIVE_DMA)) ||
   3142 			    ((chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
   3143 			    !(chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
   3144 			    (chp->ch_drive[0].drive_flags & DRIVE_DMA)))
   3145 				atapi = 0;
   3146 			else
   3147 				atapi = PDC262_ATAPI_UDMA;
   3148 			bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   3149 			    PDC262_ATAPI(channel), atapi);
   3150 		}
   3151 	}
   3152 	for (drive = 0; drive < 2; drive++) {
   3153 		drvp = &chp->ch_drive[drive];
   3154 		/* If no drive, skip */
   3155 		if ((drvp->drive_flags & DRIVE) == 0)
   3156 			continue;
   3157 		mode = 0;
   3158 		if (drvp->drive_flags & DRIVE_UDMA) {
   3159 			mode = PDC2xx_TIM_SET_MB(mode,
   3160 			    pdc2xx_udma_mb[drvp->UDMA_mode]);
   3161 			mode = PDC2xx_TIM_SET_MC(mode,
   3162 			    pdc2xx_udma_mc[drvp->UDMA_mode]);
   3163 			drvp->drive_flags &= ~DRIVE_DMA;
   3164 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   3165 		} else if (drvp->drive_flags & DRIVE_DMA) {
   3166 			mode = PDC2xx_TIM_SET_MB(mode,
   3167 			    pdc2xx_dma_mb[drvp->DMA_mode]);
   3168 			mode = PDC2xx_TIM_SET_MC(mode,
   3169 			    pdc2xx_dma_mc[drvp->DMA_mode]);
   3170 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   3171 		} else {
   3172 			mode = PDC2xx_TIM_SET_MB(mode,
   3173 			    pdc2xx_dma_mb[0]);
   3174 			mode = PDC2xx_TIM_SET_MC(mode,
   3175 			    pdc2xx_dma_mc[0]);
   3176 		}
   3177 		mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[drvp->PIO_mode]);
   3178 		mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[drvp->PIO_mode]);
   3179 		if (drvp->drive_flags & DRIVE_ATA)
   3180 			mode |= PDC2xx_TIM_PRE;
   3181 		mode |= PDC2xx_TIM_SYNC | PDC2xx_TIM_ERRDY;
   3182 		if (drvp->PIO_mode >= 3) {
   3183 			mode |= PDC2xx_TIM_IORDY;
   3184 			if (drive == 0)
   3185 				mode |= PDC2xx_TIM_IORDYp;
   3186 		}
   3187 		WDCDEBUG_PRINT(("pdc202xx_setup_channel: %s:%d:%d "
   3188 		    "timings 0x%x\n",
   3189 		    sc->sc_wdcdev.sc_dev.dv_xname,
   3190 		    chp->channel, drive, mode), DEBUG_PROBE);
   3191 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   3192 		    PDC2xx_TIM(chp->channel, drive), mode);
   3193 	}
   3194 	if (idedma_ctl != 0) {
   3195 		/* Add software bits in status register */
   3196 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   3197 		    IDEDMA_CTL, idedma_ctl);
   3198 	}
   3199 	pciide_print_modes(cp);
   3200 }
   3201 
   3202 int
   3203 pdc202xx_pci_intr(arg)
   3204 	void *arg;
   3205 {
   3206 	struct pciide_softc *sc = arg;
   3207 	struct pciide_channel *cp;
   3208 	struct channel_softc *wdc_cp;
   3209 	int i, rv, crv;
   3210 	u_int32_t scr;
   3211 
   3212 	rv = 0;
   3213 	scr = bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR);
   3214 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
   3215 		cp = &sc->pciide_channels[i];
   3216 		wdc_cp = &cp->wdc_channel;
   3217 		/* If a compat channel skip. */
   3218 		if (cp->compat)
   3219 			continue;
   3220 		if (scr & PDC2xx_SCR_INT(i)) {
   3221 			crv = wdcintr(wdc_cp);
   3222 			if (crv == 0)
   3223 				printf("%s:%d: bogus intr\n",
   3224 				    sc->sc_wdcdev.sc_dev.dv_xname, i);
   3225 			else
   3226 				rv = 1;
   3227 		}
   3228 	}
   3229 	return rv;
   3230 }
   3231 
   3232 void
   3233 opti_chip_map(sc, pa)
   3234 	struct pciide_softc *sc;
   3235 	struct pci_attach_args *pa;
   3236 {
   3237 	struct pciide_channel *cp;
   3238 	bus_size_t cmdsize, ctlsize;
   3239 	pcireg_t interface;
   3240 	u_int8_t init_ctrl;
   3241 	int channel;
   3242 
   3243 	if (pciide_chipen(sc, pa) == 0)
   3244 		return;
   3245 	printf("%s: bus-master DMA support present",
   3246 	    sc->sc_wdcdev.sc_dev.dv_xname);
   3247 	pciide_mapreg_dma(sc, pa);
   3248 	printf("\n");
   3249 
   3250 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
   3251 	    WDC_CAPABILITY_MODE;
   3252 	sc->sc_wdcdev.PIO_cap = 4;
   3253 	if (sc->sc_dma_ok) {
   3254 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
   3255 		sc->sc_wdcdev.irqack = pciide_irqack;
   3256 		sc->sc_wdcdev.DMA_cap = 2;
   3257 	}
   3258 	sc->sc_wdcdev.set_modes = opti_setup_channel;
   3259 
   3260 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
   3261 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
   3262 
   3263 	init_ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag,
   3264 	    OPTI_REG_INIT_CONTROL);
   3265 
   3266 	interface = PCI_INTERFACE(pa->pa_class);
   3267 
   3268 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   3269 		cp = &sc->pciide_channels[channel];
   3270 		if (pciide_chansetup(sc, channel, interface) == 0)
   3271 			continue;
   3272 		if (channel == 1 &&
   3273 		    (init_ctrl & OPTI_INIT_CONTROL_CH2_DISABLE) != 0) {
   3274 			printf("%s: %s channel ignored (disabled)\n",
   3275 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   3276 			continue;
   3277 		}
   3278 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
   3279 		    pciide_pci_intr);
   3280 		if (cp->hw_ok == 0)
   3281 			continue;
   3282 		pciide_map_compat_intr(pa, cp, channel, interface);
   3283 		if (cp->hw_ok == 0)
   3284 			continue;
   3285 		opti_setup_channel(&cp->wdc_channel);
   3286 	}
   3287 }
   3288 
   3289 void
   3290 opti_setup_channel(chp)
   3291 	struct channel_softc *chp;
   3292 {
   3293 	struct ata_drive_datas *drvp;
   3294 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   3295 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   3296 	int drive, spd;
   3297 	int mode[2];
   3298 	u_int8_t rv, mr;
   3299 
   3300 	/*
   3301 	 * The `Delay' and `Address Setup Time' fields of the
   3302 	 * Miscellaneous Register are always zero initially.
   3303 	 */
   3304 	mr = opti_read_config(chp, OPTI_REG_MISC) & ~OPTI_MISC_INDEX_MASK;
   3305 	mr &= ~(OPTI_MISC_DELAY_MASK |
   3306 		OPTI_MISC_ADDR_SETUP_MASK |
   3307 		OPTI_MISC_INDEX_MASK);
   3308 
   3309 	/* Prime the control register before setting timing values */
   3310 	opti_write_config(chp, OPTI_REG_CONTROL, OPTI_CONTROL_DISABLE);
   3311 
   3312 	/* Determine the clockrate of the PCIbus the chip is attached to */
   3313 	spd = (int) opti_read_config(chp, OPTI_REG_STRAP);
   3314 	spd &= OPTI_STRAP_PCI_SPEED_MASK;
   3315 
   3316 	/* setup DMA if needed */
   3317 	pciide_channel_dma_setup(cp);
   3318 
   3319 	for (drive = 0; drive < 2; drive++) {
   3320 		drvp = &chp->ch_drive[drive];
   3321 		/* If no drive, skip */
   3322 		if ((drvp->drive_flags & DRIVE) == 0) {
   3323 			mode[drive] = -1;
   3324 			continue;
   3325 		}
   3326 
   3327 		if ((drvp->drive_flags & DRIVE_DMA)) {
   3328 			/*
   3329 			 * Timings will be used for both PIO and DMA,
   3330 			 * so adjust DMA mode if needed
   3331 			 */
   3332 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   3333 				drvp->PIO_mode = drvp->DMA_mode + 2;
   3334 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   3335 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   3336 				    drvp->PIO_mode - 2 : 0;
   3337 			if (drvp->DMA_mode == 0)
   3338 				drvp->PIO_mode = 0;
   3339 
   3340 			mode[drive] = drvp->DMA_mode + 5;
   3341 		} else
   3342 			mode[drive] = drvp->PIO_mode;
   3343 
   3344 		if (drive && mode[0] >= 0 &&
   3345 		    (opti_tim_as[spd][mode[0]] != opti_tim_as[spd][mode[1]])) {
   3346 			/*
   3347 			 * Can't have two drives using different values
   3348 			 * for `Address Setup Time'.
   3349 			 * Slow down the faster drive to compensate.
   3350 			 */
   3351 			int d = (opti_tim_as[spd][mode[0]] >
   3352 				 opti_tim_as[spd][mode[1]]) ?  0 : 1;
   3353 
   3354 			mode[d] = mode[1-d];
   3355 			chp->ch_drive[d].PIO_mode = chp->ch_drive[1-d].PIO_mode;
   3356 			chp->ch_drive[d].DMA_mode = 0;
   3357 			chp->ch_drive[d].drive_flags &= DRIVE_DMA;
   3358 		}
   3359 	}
   3360 
   3361 	for (drive = 0; drive < 2; drive++) {
   3362 		int m;
   3363 		if ((m = mode[drive]) < 0)
   3364 			continue;
   3365 
   3366 		/* Set the Address Setup Time and select appropriate index */
   3367 		rv = opti_tim_as[spd][m] << OPTI_MISC_ADDR_SETUP_SHIFT;
   3368 		rv |= OPTI_MISC_INDEX(drive);
   3369 		opti_write_config(chp, OPTI_REG_MISC, mr | rv);
   3370 
   3371 		/* Set the pulse width and recovery timing parameters */
   3372 		rv  = opti_tim_cp[spd][m] << OPTI_PULSE_WIDTH_SHIFT;
   3373 		rv |= opti_tim_rt[spd][m] << OPTI_RECOVERY_TIME_SHIFT;
   3374 		opti_write_config(chp, OPTI_REG_READ_CYCLE_TIMING, rv);
   3375 		opti_write_config(chp, OPTI_REG_WRITE_CYCLE_TIMING, rv);
   3376 
   3377 		/* Set the Enhanced Mode register appropriately */
   3378 	    	rv = pciide_pci_read(sc->sc_pc, sc->sc_tag, OPTI_REG_ENH_MODE);
   3379 		rv &= ~OPTI_ENH_MODE_MASK(chp->channel, drive);
   3380 		rv |= OPTI_ENH_MODE(chp->channel, drive, opti_tim_em[m]);
   3381 		pciide_pci_write(sc->sc_pc, sc->sc_tag, OPTI_REG_ENH_MODE, rv);
   3382 	}
   3383 
   3384 	/* Finally, enable the timings */
   3385 	opti_write_config(chp, OPTI_REG_CONTROL, OPTI_CONTROL_ENABLE);
   3386 
   3387 	pciide_print_modes(cp);
   3388 }
   3389