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