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