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