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