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