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