Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.39
      1 /*	$NetBSD: pciide.c,v 1.39 1999/06/08 10:38:15 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * PCI IDE controller driver.
     35  *
     36  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     37  * sys/dev/pci/ppb.c, revision 1.16).
     38  *
     39  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     40  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     41  * 5/16/94" from the PCI SIG.
     42  *
     43  */
     44 
     45 #ifndef WDCDEBUG
     46 #define WDCDEBUG
     47 #endif
     48 
     49 #define DEBUG_DMA   0x01
     50 #define DEBUG_XFERS  0x02
     51 #define DEBUG_FUNCS  0x08
     52 #define DEBUG_PROBE  0x10
     53 #ifdef WDCDEBUG
     54 int wdcdebug_pciide_mask = 0;
     55 #define WDCDEBUG_PRINT(args, level) \
     56 	if (wdcdebug_pciide_mask & (level)) printf args
     57 #else
     58 #define WDCDEBUG_PRINT(args, level)
     59 #endif
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/device.h>
     63 #include <sys/malloc.h>
     64 
     65 #include <vm/vm.h>
     66 #include <vm/vm_param.h>
     67 #include <vm/vm_kern.h>
     68 
     69 #include <dev/pci/pcireg.h>
     70 #include <dev/pci/pcivar.h>
     71 #include <dev/pci/pcidevs.h>
     72 #include <dev/pci/pciidereg.h>
     73 #include <dev/pci/pciidevar.h>
     74 #include <dev/pci/pciide_piix_reg.h>
     75 #include <dev/pci/pciide_apollo_reg.h>
     76 #include <dev/pci/pciide_cmd_reg.h>
     77 #include <dev/pci/pciide_cy693_reg.h>
     78 #include <dev/pci/pciide_sis_reg.h>
     79 #include <dev/pci/pciide_acer_reg.h>
     80 #include <dev/ata/atavar.h>
     81 #include <dev/ic/wdcreg.h>
     82 #include <dev/ic/wdcvar.h>
     83 
     84 #if BYTE_ORDER == BIG_ENDIAN
     85 #include <machine/bswap.h>
     86 #define	htopci(x)	bswap32(x)
     87 #define	pcitoh(x)	bswap32(x)
     88 #else
     89 #define	htopci(x)	(x)
     90 #define	pcitoh(x)	(x)
     91 #endif
     92 
     93 /* inlines for reading/writing 8-bit PCI registers */
     94 static __inline u_int8_t pciide_pci_read __P((pci_chipset_tag_t, pcitag_t,
     95 					      int));
     96 static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
     97 					   int, u_int8_t));
     98 
     99 static __inline u_int8_t
    100 pciide_pci_read(pc, pa, reg)
    101 	pci_chipset_tag_t pc;
    102 	pcitag_t pa;
    103 	int reg;
    104 {
    105 
    106 	return (pci_conf_read(pc, pa, (reg & ~0x03)) >>
    107 	    ((reg & 0x03) * 8) & 0xff);
    108 }
    109 
    110 static __inline void
    111 pciide_pci_write(pc, pa, reg, val)
    112 	pci_chipset_tag_t pc;
    113 	pcitag_t pa;
    114 	int reg;
    115 	u_int8_t val;
    116 {
    117 	pcireg_t pcival;
    118 
    119 	pcival = pci_conf_read(pc, pa, (reg & ~0x03));
    120 	pcival &= ~(0xff << ((reg & 0x03) * 8));
    121 	pcival |= (val << ((reg & 0x03) * 8));
    122 	pci_conf_write(pc, pa, (reg & ~0x03), pcival);
    123 }
    124 
    125 struct pciide_softc {
    126 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
    127 	pci_chipset_tag_t	sc_pc;		/* PCI registers info */
    128 	pcitag_t		sc_tag;
    129 	void			*sc_pci_ih;	/* PCI interrupt handle */
    130 	int			sc_dma_ok;	/* bus-master DMA info */
    131 	bus_space_tag_t		sc_dma_iot;
    132 	bus_space_handle_t	sc_dma_ioh;
    133 	bus_dma_tag_t		sc_dmat;
    134 	/* Chip description */
    135 	const struct pciide_product_desc *sc_pp;
    136 	/* common definitions */
    137 	struct channel_softc *wdc_chanarray[PCIIDE_NUM_CHANNELS];
    138 	/* internal bookkeeping */
    139 	struct pciide_channel {			/* per-channel data */
    140 		struct channel_softc wdc_channel; /* generic part */
    141 		char		*name;
    142 		int		hw_ok;		/* hardware mapped & OK? */
    143 		int		compat;		/* is it compat? */
    144 		void		*ih;		/* compat or pci handle */
    145 		/* DMA tables and DMA map for xfer, for each drive */
    146 		struct pciide_dma_maps {
    147 			bus_dmamap_t    dmamap_table;
    148 			struct idedma_table *dma_table;
    149 			bus_dmamap_t    dmamap_xfer;
    150 		} dma_maps[2];
    151 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    152 };
    153 
    154 void default_setup_cap __P((struct pciide_softc*));
    155 void default_setup_chip __P((struct pciide_softc*));
    156 void default_channel_map __P((struct pci_attach_args *,
    157 		struct pciide_channel *));
    158 
    159 void piix_setup_cap __P((struct pciide_softc*));
    160 void piix_setup_chip __P((struct pciide_softc*));
    161 void piix_setup_channel __P((struct channel_softc*));
    162 void piix3_4_setup_chip __P((struct pciide_softc*));
    163 void piix3_4_setup_channel __P((struct channel_softc*));
    164 void piix_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    165 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    166 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    167 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    168 
    169 void apollo_setup_cap __P((struct pciide_softc*));
    170 void apollo_setup_chip __P((struct pciide_softc*));
    171 void apollo_setup_channel __P((struct channel_softc*));
    172 void apollo_channel_map __P((struct pci_attach_args *,
    173 		struct pciide_channel *));
    174 
    175 void cmd0643_6_setup_cap __P((struct pciide_softc*));
    176 void cmd0643_6_setup_chip __P((struct pciide_softc*));
    177 void cmd0643_6_setup_channel __P((struct channel_softc*));
    178 void cmd_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    179 
    180 void cy693_setup_cap __P((struct pciide_softc*));
    181 void cy693_setup_chip __P((struct pciide_softc*));
    182 void cy693_setup_channel __P((struct channel_softc*));
    183 void cy693_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    184 
    185 void sis_setup_cap __P((struct pciide_softc*));
    186 void sis_setup_chip __P((struct pciide_softc*));
    187 void sis_setup_channel __P((struct channel_softc*));
    188 void sis_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    189 
    190 void acer_setup_cap __P((struct pciide_softc*));
    191 void acer_setup_chip __P((struct pciide_softc*));
    192 void acer_setup_channel __P((struct channel_softc*));
    193 void acer_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    194 
    195 void pciide_channel_dma_setup __P((struct pciide_channel *));
    196 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    197 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    198 void pciide_dma_start __P((void*, int, int, int));
    199 int  pciide_dma_finish __P((void*, int, int, int));
    200 void pciide_print_modes __P((struct pciide_channel *));
    201 
    202 struct pciide_product_desc {
    203 	u_int32_t ide_product;
    204 	int ide_flags;
    205 	int ide_num_channels;
    206 	const char *ide_name;
    207 	/* init controller's capabilities for drives probe */
    208 	void (*setup_cap) __P((struct pciide_softc*));
    209 	/* init controller after drives probe */
    210 	void (*setup_chip) __P((struct pciide_softc*));
    211 	/* map channel if possible/necessary */
    212 	void (*channel_map) __P((struct pci_attach_args *,
    213 		struct pciide_channel *));
    214 };
    215 
    216 /* Flags for ide_flags */
    217 #define CMD_PCI064x_IOEN	0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
    218 #define ONE_QUEUE		0x02 /* device need serialised access */
    219 
    220 /* Default product description for devices not known from this controller */
    221 const struct pciide_product_desc default_product_desc = {
    222 	0,
    223 	0,
    224 	PCIIDE_NUM_CHANNELS,
    225 	"Generic PCI IDE controller",
    226 	default_setup_cap,
    227 	default_setup_chip,
    228 	default_channel_map
    229 };
    230 
    231 const struct pciide_product_desc pciide_intel_products[] =  {
    232 	{ PCI_PRODUCT_INTEL_82092AA,
    233 	  0,
    234 	  PCIIDE_NUM_CHANNELS,
    235 	  "Intel 82092AA IDE controller",
    236 	  default_setup_cap,
    237 	  default_setup_chip,
    238 	  default_channel_map
    239 	},
    240 	{ PCI_PRODUCT_INTEL_82371FB_IDE,
    241 	  0,
    242 	  PCIIDE_NUM_CHANNELS,
    243 	  "Intel 82371FB IDE controller (PIIX)",
    244 	  piix_setup_cap,
    245 	  piix_setup_chip,
    246 	  piix_channel_map
    247 	},
    248 	{ PCI_PRODUCT_INTEL_82371SB_IDE,
    249 	  0,
    250 	  PCIIDE_NUM_CHANNELS,
    251 	  "Intel 82371SB IDE Interface (PIIX3)",
    252 	  piix_setup_cap,
    253 	  piix3_4_setup_chip,
    254 	  piix_channel_map
    255 	},
    256 	{ PCI_PRODUCT_INTEL_82371AB_IDE,
    257 	  0,
    258 	  PCIIDE_NUM_CHANNELS,
    259 	  "Intel 82371AB IDE controller (PIIX4)",
    260 	  piix_setup_cap,
    261 	  piix3_4_setup_chip,
    262 	  piix_channel_map
    263 	},
    264 	{ 0,
    265 	  0,
    266 	  0,
    267 	  NULL,
    268 	}
    269 };
    270 
    271 const struct pciide_product_desc pciide_cmd_products[] =  {
    272 	{ PCI_PRODUCT_CMDTECH_640,
    273 	  ONE_QUEUE | CMD_PCI064x_IOEN,
    274 	  PCIIDE_NUM_CHANNELS,
    275 	  "CMD Technology PCI0640",
    276 	  default_setup_cap,
    277 	  default_setup_chip,
    278 	  cmd_channel_map
    279 	},
    280 	{ PCI_PRODUCT_CMDTECH_643,
    281 	  ONE_QUEUE | CMD_PCI064x_IOEN,
    282 	  PCIIDE_NUM_CHANNELS,
    283 	  "CMD Technology PCI0643",
    284 	  cmd0643_6_setup_cap,
    285 	  cmd0643_6_setup_chip,
    286 	  cmd_channel_map
    287 	},
    288 	{ PCI_PRODUCT_CMDTECH_646,
    289 	  ONE_QUEUE | CMD_PCI064x_IOEN,
    290 	  PCIIDE_NUM_CHANNELS,
    291 	  "CMD Technology PCI0646",
    292 	  cmd0643_6_setup_cap,
    293 	  cmd0643_6_setup_chip,
    294 	  cmd_channel_map
    295 	},
    296 	{ 0,
    297 	  0,
    298 	  0,
    299 	  NULL,
    300 	}
    301 };
    302 
    303 const struct pciide_product_desc pciide_via_products[] =  {
    304 	{ PCI_PRODUCT_VIATECH_VT82C586_IDE,
    305 	  0,
    306 	  PCIIDE_NUM_CHANNELS,
    307 	  "VIA Technologies VT82C586 (Apollo VP) IDE Controller",
    308 	  apollo_setup_cap,
    309 	  apollo_setup_chip,
    310 	  apollo_channel_map
    311 	 },
    312 	{ PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    313 	  0,
    314 	  PCIIDE_NUM_CHANNELS,
    315 	  "VIA Technologies VT82C586A IDE Controller",
    316 	  apollo_setup_cap,
    317 	  apollo_setup_chip,
    318 	  apollo_channel_map
    319 	},
    320 	{ 0,
    321 	  0,
    322 	  0,
    323 	  NULL,
    324 	}
    325 };
    326 
    327 const struct pciide_product_desc pciide_cypress_products[] =  {
    328 	{ PCI_PRODUCT_CONTAQ_82C693,
    329 	  0,
    330 	  1,
    331 	  "Contaq Microsystems CY82C693 IDE Controller",
    332 	  cy693_setup_cap,
    333 	  cy693_setup_chip,
    334 	  cy693_channel_map
    335 	},
    336 	{ 0,
    337 	  0,
    338 	  0,
    339 	  NULL,
    340 	}
    341 };
    342 
    343 const struct pciide_product_desc pciide_sis_products[] =  {
    344 	{ PCI_PRODUCT_SIS_5597_IDE,
    345 	  0,
    346 	  PCIIDE_NUM_CHANNELS,
    347 	  "Silicon Integrated System 5597/5598 IDE controller",
    348 	  sis_setup_cap,
    349 	  sis_setup_chip,
    350 	  sis_channel_map
    351 	},
    352 	{ 0,
    353 	  0,
    354 	  0,
    355 	  NULL,
    356 	}
    357 };
    358 
    359 const struct pciide_product_desc pciide_acer_products[] =  {
    360 	{ PCI_PRODUCT_ALI_M5229,
    361 	  0,
    362 	  PCIIDE_NUM_CHANNELS,
    363 	  "Acer Labs M5229 UDMA IDE Controller",
    364 	  acer_setup_cap,
    365 	  acer_setup_chip,
    366 	  acer_channel_map
    367 	},
    368 	{ 0,
    369 	  0,
    370 	  0,
    371 	  NULL,
    372 	}
    373 };
    374 
    375 struct pciide_vendor_desc {
    376 	u_int32_t ide_vendor;
    377 	const struct pciide_product_desc *ide_products;
    378 };
    379 
    380 const struct pciide_vendor_desc pciide_vendors[] = {
    381 	{ PCI_VENDOR_INTEL, pciide_intel_products },
    382 	{ PCI_VENDOR_CMDTECH, pciide_cmd_products },
    383 	{ PCI_VENDOR_VIATECH, pciide_via_products },
    384 	{ PCI_VENDOR_CONTAQ, pciide_cypress_products },
    385 	{ PCI_VENDOR_SIS, pciide_sis_products },
    386 	{ PCI_VENDOR_ALI, pciide_acer_products },
    387 	{ 0, NULL }
    388 };
    389 
    390 #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    391 
    392 /* options passed via the 'flags' config keyword */
    393 #define PCIIDE_OPTIONS_DMA	0x01
    394 
    395 int	pciide_match __P((struct device *, struct cfdata *, void *));
    396 void	pciide_attach __P((struct device *, struct device *, void *));
    397 
    398 struct cfattach pciide_ca = {
    399 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    400 };
    401 
    402 int	pciide_mapregs_compat __P(( struct pci_attach_args *,
    403 	    struct pciide_channel *, int, bus_size_t *, bus_size_t*));
    404 int	pciide_mapregs_native __P((struct pci_attach_args *,
    405 	    struct pciide_channel *, bus_size_t *, bus_size_t *));
    406 void	pciide_mapchan __P((struct pci_attach_args *,
    407 	    struct pciide_channel *, int, bus_size_t *, bus_size_t *));
    408 int	pciiide_chan_candisable __P((struct pciide_channel *));
    409 void	pciide_map_compat_intr __P(( struct pci_attach_args *,
    410 	    struct pciide_channel *, int, int));
    411 int	pciide_print __P((void *, const char *pnp));
    412 int	pciide_compat_intr __P((void *));
    413 int	pciide_pci_intr __P((void *));
    414 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    415 
    416 const struct pciide_product_desc *
    417 pciide_lookup_product(id)
    418 	u_int32_t id;
    419 {
    420 	const struct pciide_product_desc *pp;
    421 	const struct pciide_vendor_desc *vp;
    422 
    423 	for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    424 		if (PCI_VENDOR(id) == vp->ide_vendor)
    425 			break;
    426 
    427 	if ((pp = vp->ide_products) == NULL)
    428 		return NULL;
    429 
    430 	for (; pp->ide_name != NULL; pp++)
    431 		if (PCI_PRODUCT(id) == pp->ide_product)
    432 			break;
    433 
    434 	if (pp->ide_name == NULL)
    435 		return NULL;
    436 	return pp;
    437 }
    438 
    439 int
    440 pciide_match(parent, match, aux)
    441 	struct device *parent;
    442 	struct cfdata *match;
    443 	void *aux;
    444 {
    445 	struct pci_attach_args *pa = aux;
    446 
    447 	/*
    448 	 * Check the ID register to see that it's a PCI IDE controller.
    449 	 * If it is, we assume that we can deal with it; it _should_
    450 	 * work in a standardized way...
    451 	 */
    452 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    453 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    454 		return (1);
    455 	}
    456 
    457 	return (0);
    458 }
    459 
    460 void
    461 pciide_attach(parent, self, aux)
    462 	struct device *parent, *self;
    463 	void *aux;
    464 {
    465 	struct pci_attach_args *pa = aux;
    466 	pci_chipset_tag_t pc = pa->pa_pc;
    467 	pcitag_t tag = pa->pa_tag;
    468 	struct pciide_softc *sc = (struct pciide_softc *)self;
    469 	struct pciide_channel *cp;
    470 	pcireg_t class, interface, csr;
    471 	char devinfo[256];
    472 	int i;
    473 
    474         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    475 	if (sc->sc_pp == NULL) {
    476 		sc->sc_pp = &default_product_desc;
    477 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    478 		printf(": %s (rev. 0x%02x)\n", devinfo,
    479 		    PCI_REVISION(pa->pa_class));
    480 	} else {
    481 		printf(": %s\n", sc->sc_pp->ide_name);
    482 	}
    483 
    484 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    485 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    486 		/*
    487 		 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    488 		 * and base adresses registers can be disabled at
    489 		 * hardware level. In this case, the device is wired
    490 		 * in compat mode and its first channel is always enabled,
    491 		 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    492 		 * In fact, it seems that the first channel of the CMD PCI0640
    493 		 * can't be disabled.
    494 		 */
    495 #ifndef PCIIDE_CMD064x_DISABLE
    496 		if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
    497 #else
    498 		if (1) {
    499 #endif
    500 			printf("%s: device disabled (at %s)\n",
    501 		 	   sc->sc_wdcdev.sc_dev.dv_xname,
    502 		  	  (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    503 			  "device" : "bridge");
    504 			return;
    505 		}
    506 	}
    507 
    508 	sc->sc_pc = pa->pa_pc;
    509 	sc->sc_tag = pa->pa_tag;
    510 
    511 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    512 	interface = PCI_INTERFACE(class);
    513 
    514 	/*
    515 	 * Map DMA registers, if DMA is supported.
    516 	 *
    517 	 * Note that sc_dma_ok is the right variable to test to see if
    518 	 * DMA can be done.  If the interface doesn't support DMA,
    519 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    520 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    521 	 * non-zero if the interface supports DMA and the registers
    522 	 * could be mapped.
    523 	 *
    524 	 * XXX Note that despite the fact that the Bus Master IDE specs
    525 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    526 	 * XXX space," some controllers (at least the United
    527 	 * XXX Microelectronics UM8886BF) place it in memory space.
    528 	 * XXX eventually, we should probably read the register and check
    529 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    530 	 */
    531 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    532 		printf("%s: bus-master DMA support present",
    533 		    sc->sc_wdcdev.sc_dev.dv_xname);
    534 		if (sc->sc_pp == &default_product_desc &&
    535 		    (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
    536 		    PCIIDE_OPTIONS_DMA) == 0) {
    537 			printf(", but unused (no driver support)");
    538 			sc->sc_dma_ok = 0;
    539 		} else {
    540 			sc->sc_dma_ok = (pci_mapreg_map(pa,
    541 			    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    542 			    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    543 			sc->sc_dmat = pa->pa_dmat;
    544 			if (sc->sc_dma_ok == 0) {
    545 				printf(", but unused (couldn't map registers)");
    546 			} else {
    547 				if (sc->sc_pp == &default_product_desc)
    548 					printf(", used without full driver "
    549 					    "support");
    550 				sc->sc_wdcdev.dma_arg = sc;
    551 				sc->sc_wdcdev.dma_init = pciide_dma_init;
    552 				sc->sc_wdcdev.dma_start = pciide_dma_start;
    553 				sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    554 			}
    555 		}
    556 	} else {
    557 		printf("%s: hardware does not support DMA",
    558 		    sc->sc_wdcdev.sc_dev.dv_xname);
    559 	}
    560 	printf("\n");
    561 	sc->sc_pp->setup_cap(sc);
    562 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    563 	sc->sc_wdcdev.nchannels = sc->sc_pp->ide_num_channels;;
    564 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    565 
    566 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    567 		cp = &sc->pciide_channels[i];
    568 		sc->wdc_chanarray[i] = &cp->wdc_channel;
    569 
    570 		cp->name = PCIIDE_CHANNEL_NAME(i);
    571 
    572 		cp->wdc_channel.channel = i;
    573 		cp->wdc_channel.wdc = &sc->sc_wdcdev;
    574 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    575 		    cp->wdc_channel.ch_queue =
    576 			sc->pciide_channels[0].wdc_channel.ch_queue;
    577 		} else {
    578 		    cp->wdc_channel.ch_queue =
    579 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    580 			M_NOWAIT);
    581 		}
    582 		if (cp->wdc_channel.ch_queue == NULL) {
    583 		    printf("%s %s channel: "
    584 			"can't allocate memory for command queue",
    585 			sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    586 			continue;
    587 		}
    588 		printf("%s: %s channel %s to %s mode\n",
    589 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
    590 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    591 		      "configured" : "wired",
    592 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    593 		      "compatibility");
    594 
    595 		/*
    596 		 * sc->sc_pp->channel_map() will also call wdcattach.
    597 		 * Eventually the channel will be  disabled if there's no
    598 		 * drive present. sc->hw_ok will be updated accordingly.
    599 		 */
    600 		sc->sc_pp->channel_map(pa, cp);
    601 
    602 	}
    603 	/* Now that all drives are know, setup DMA, etc ...*/
    604 	sc->sc_pp->setup_chip(sc);
    605 	if (sc->sc_dma_ok) {
    606 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    607 		csr |= PCI_COMMAND_MASTER_ENABLE;
    608 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    609 	}
    610 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    611 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    612 }
    613 
    614 int
    615 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
    616 	struct pci_attach_args *pa;
    617 	struct pciide_channel *cp;
    618 	int compatchan;
    619 	bus_size_t *cmdsizep, *ctlsizep;
    620 {
    621 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    622 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    623 	int rv = 1;
    624 
    625 	cp->compat = 1;
    626 	*cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
    627 	*ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
    628 
    629 	wdc_cp->cmd_iot = pa->pa_iot;
    630 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    631 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    632 		printf("%s: couldn't map %s channel cmd regs\n",
    633 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    634 		rv = 0;
    635 	}
    636 
    637 	wdc_cp->ctl_iot = pa->pa_iot;
    638 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    639 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    640 		printf("%s: couldn't map %s channel ctl regs\n",
    641 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    642 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    643 		    PCIIDE_COMPAT_CMD_SIZE);
    644 		rv = 0;
    645 	}
    646 
    647 	return (rv);
    648 }
    649 
    650 int
    651 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep)
    652 	struct pci_attach_args * pa;
    653 	struct pciide_channel *cp;
    654 	bus_size_t *cmdsizep, *ctlsizep;
    655 {
    656 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    657 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    658 	const char *intrstr;
    659 	pci_intr_handle_t intrhandle;
    660 
    661 	cp->compat = 0;
    662 
    663 	if (sc->sc_pci_ih == NULL) {
    664 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    665 		    pa->pa_intrline, &intrhandle) != 0) {
    666 			printf("%s: couldn't map native-PCI interrupt\n",
    667 			    sc->sc_wdcdev.sc_dev.dv_xname);
    668 			return 0;
    669 		}
    670 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    671 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    672 		    intrhandle, IPL_BIO, pciide_pci_intr, sc);
    673 		if (sc->sc_pci_ih != NULL) {
    674 			printf("%s: using %s for native-PCI interrupt\n",
    675 			    sc->sc_wdcdev.sc_dev.dv_xname,
    676 			    intrstr ? intrstr : "unknown interrupt");
    677 		} else {
    678 			printf("%s: couldn't establish native-PCI interrupt",
    679 			    sc->sc_wdcdev.sc_dev.dv_xname);
    680 			if (intrstr != NULL)
    681 				printf(" at %s", intrstr);
    682 			printf("\n");
    683 			return 0;
    684 		}
    685 	}
    686 	cp->ih = sc->sc_pci_ih;
    687 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
    688 	    PCI_MAPREG_TYPE_IO, 0,
    689 	    &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
    690 		printf("%s: couldn't map %s channel cmd regs\n",
    691 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    692 		return 0;
    693 	}
    694 
    695 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
    696 	    PCI_MAPREG_TYPE_IO, 0,
    697 	    &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, ctlsizep) != 0) {
    698 		printf("%s: couldn't map %s channel ctl regs\n",
    699 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    700 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
    701 		return 0;
    702 	}
    703 	return (1);
    704 }
    705 
    706 int
    707 pciide_compat_intr(arg)
    708 	void *arg;
    709 {
    710 	struct pciide_channel *cp = arg;
    711 
    712 #ifdef DIAGNOSTIC
    713 	/* should only be called for a compat channel */
    714 	if (cp->compat == 0)
    715 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    716 #endif
    717 	return (wdcintr(&cp->wdc_channel));
    718 }
    719 
    720 int
    721 pciide_pci_intr(arg)
    722 	void *arg;
    723 {
    724 	struct pciide_softc *sc = arg;
    725 	struct pciide_channel *cp;
    726 	struct channel_softc *wdc_cp;
    727 	int i, rv, crv;
    728 
    729 	rv = 0;
    730 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    731 		cp = &sc->pciide_channels[i];
    732 		wdc_cp = &cp->wdc_channel;
    733 
    734 		/* If a compat channel skip. */
    735 		if (cp->compat)
    736 			continue;
    737 		/* if this channel not waiting for intr, skip */
    738 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    739 			continue;
    740 
    741 		crv = wdcintr(wdc_cp);
    742 		if (crv == 0)
    743 			;		/* leave rv alone */
    744 		else if (crv == 1)
    745 			rv = 1;		/* claim the intr */
    746 		else if (rv == 0)	/* crv should be -1 in this case */
    747 			rv = crv;	/* if we've done no better, take it */
    748 	}
    749 	return (rv);
    750 }
    751 
    752 void
    753 pciide_channel_dma_setup(cp)
    754 	struct pciide_channel *cp;
    755 {
    756 	int drive;
    757 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    758 	struct ata_drive_datas *drvp;
    759 
    760 	for (drive = 0; drive < 2; drive++) {
    761 		drvp = &cp->wdc_channel.ch_drive[drive];
    762 		/* If no drive, skip */
    763 		if ((drvp->drive_flags & DRIVE) == 0)
    764 			continue;
    765 		/* setup DMA if needed */
    766 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    767 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    768 		    sc->sc_dma_ok == 0) {
    769 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    770 			continue;
    771 		}
    772 		if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
    773 		    != 0) {
    774 			/* Abort DMA setup */
    775 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    776 			continue;
    777 		}
    778 	}
    779 }
    780 
    781 int
    782 pciide_dma_table_setup(sc, channel, drive)
    783 	struct pciide_softc *sc;
    784 	int channel, drive;
    785 {
    786 	bus_dma_segment_t seg;
    787 	int error, rseg;
    788 	const bus_size_t dma_table_size =
    789 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
    790 	struct pciide_dma_maps *dma_maps =
    791 	    &sc->pciide_channels[channel].dma_maps[drive];
    792 
    793 	/* If table was already allocated, just return */
    794 	if (dma_maps->dma_table)
    795 		return 0;
    796 
    797 	/* Allocate memory for the DMA tables and map it */
    798 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    799 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
    800 	    BUS_DMA_NOWAIT)) != 0) {
    801 		printf("%s:%d: unable to allocate table DMA for "
    802 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    803 		    channel, drive, error);
    804 		return error;
    805 	}
    806 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    807 	    dma_table_size,
    808 	    (caddr_t *)&dma_maps->dma_table,
    809 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    810 		printf("%s:%d: unable to map table DMA for"
    811 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    812 		    channel, drive, error);
    813 		return error;
    814 	}
    815 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
    816 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
    817 	    seg.ds_addr), DEBUG_PROBE);
    818 
    819 	/* Create and load table DMA map for this disk */
    820 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    821 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    822 	    &dma_maps->dmamap_table)) != 0) {
    823 		printf("%s:%d: unable to create table DMA map for "
    824 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    825 		    channel, drive, error);
    826 		return error;
    827 	}
    828 	if ((error = bus_dmamap_load(sc->sc_dmat,
    829 	    dma_maps->dmamap_table,
    830 	    dma_maps->dma_table,
    831 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    832 		printf("%s:%d: unable to load table DMA map for "
    833 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    834 		    channel, drive, error);
    835 		return error;
    836 	}
    837 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    838 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
    839 	/* Create a xfer DMA map for this drive */
    840 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
    841 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
    842 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    843 	    &dma_maps->dmamap_xfer)) != 0) {
    844 		printf("%s:%d: unable to create xfer DMA map for "
    845 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    846 		    channel, drive, error);
    847 		return error;
    848 	}
    849 	return 0;
    850 }
    851 
    852 int
    853 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
    854 	void *v;
    855 	int channel, drive;
    856 	void *databuf;
    857 	size_t datalen;
    858 	int flags;
    859 {
    860 	struct pciide_softc *sc = v;
    861 	int error, seg;
    862 	struct pciide_dma_maps *dma_maps =
    863 	    &sc->pciide_channels[channel].dma_maps[drive];
    864 
    865 	error = bus_dmamap_load(sc->sc_dmat,
    866 	    dma_maps->dmamap_xfer,
    867 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
    868 	if (error) {
    869 		printf("%s:%d: unable to load xfer DMA map for"
    870 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    871 		    channel, drive, error);
    872 		return error;
    873 	}
    874 
    875 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    876 	    dma_maps->dmamap_xfer->dm_mapsize,
    877 	    (flags & WDC_DMA_READ) ?
    878 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    879 
    880 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    881 #ifdef DIAGNOSTIC
    882 		/* A segment must not cross a 64k boundary */
    883 		{
    884 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    885 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    886 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    887 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    888 			printf("pciide_dma: segment %d physical addr 0x%lx"
    889 			    " len 0x%lx not properly aligned\n",
    890 			    seg, phys, len);
    891 			panic("pciide_dma: buf align");
    892 		}
    893 		}
    894 #endif
    895 		dma_maps->dma_table[seg].base_addr =
    896 		    htopci(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
    897 		dma_maps->dma_table[seg].byte_count =
    898 		    htopci(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    899 		    IDEDMA_BYTE_COUNT_MASK);
    900 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    901 		   seg, pcitoh(dma_maps->dma_table[seg].byte_count),
    902 		   pcitoh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    903 
    904 	}
    905 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    906 	    htopci(IDEDMA_BYTE_COUNT_EOT);
    907 
    908 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    909 	    dma_maps->dmamap_table->dm_mapsize,
    910 	    BUS_DMASYNC_PREWRITE);
    911 
    912 	/* Maps are ready. Start DMA function */
    913 #ifdef DIAGNOSTIC
    914 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    915 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
    916 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    917 		panic("pciide_dma_init: table align");
    918 	}
    919 #endif
    920 
    921 	/* Clear status bits */
    922 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    923 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    924 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    925 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
    926 	/* Write table addr */
    927 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    928 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
    929 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    930 	/* set read/write */
    931 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    932 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    933 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
    934 	return 0;
    935 }
    936 
    937 void
    938 pciide_dma_start(v, channel, drive, flags)
    939 	void *v;
    940 	int channel, drive, flags;
    941 {
    942 	struct pciide_softc *sc = v;
    943 
    944 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    945 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    946 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    947 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    948 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
    949 }
    950 
    951 int
    952 pciide_dma_finish(v, channel, drive, flags)
    953 	void *v;
    954 	int channel, drive;
    955 	int flags;
    956 {
    957 	struct pciide_softc *sc = v;
    958 	u_int8_t status;
    959 	struct pciide_dma_maps *dma_maps =
    960 	    &sc->pciide_channels[channel].dma_maps[drive];
    961 
    962 	/* Unload the map of the data buffer */
    963 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    964 	    dma_maps->dmamap_xfer->dm_mapsize,
    965 	    (flags & WDC_DMA_READ) ?
    966 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    967 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    968 
    969 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    970 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
    971 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    972 	    DEBUG_XFERS);
    973 
    974 	/* stop DMA channel */
    975 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    976 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    977 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    978 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
    979 
    980 	/* Clear status bits */
    981 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    982 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    983 	    status);
    984 
    985 	if ((status & IDEDMA_CTL_ERR) != 0) {
    986 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
    987 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
    988 		return -1;
    989 	}
    990 
    991 	if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
    992 		printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
    993 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
    994 		    drive, status);
    995 		return -1;
    996 	}
    997 
    998 	if ((status & IDEDMA_CTL_ACT) != 0) {
    999 		/* data underrun, may be a valid condition for ATAPI */
   1000 		return 1;
   1001 	}
   1002 	return 0;
   1003 }
   1004 
   1005 /* some common code used by several chip channel_map */
   1006 void
   1007 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep)
   1008 	struct pci_attach_args *pa;
   1009 	int interface;
   1010 	struct pciide_channel *cp;
   1011 	bus_size_t *cmdsizep, *ctlsizep;
   1012 {
   1013 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1014 
   1015 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
   1016 		cp->hw_ok = pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep);
   1017 	else
   1018 		cp->hw_ok = pciide_mapregs_compat(pa, cp,
   1019 		    wdc_cp->channel, cmdsizep, ctlsizep);
   1020 	if (cp->hw_ok == 0)
   1021 		return;
   1022 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1023 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1024 	wdcattach(wdc_cp);
   1025 }
   1026 
   1027 /*
   1028  * Generic code to call to know if a channel can be disabled. Return 1
   1029  * if channel can be disabled, 0 if not
   1030  */
   1031 int
   1032 pciiide_chan_candisable(cp)
   1033 	struct pciide_channel *cp;
   1034 {
   1035 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1036 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1037 
   1038 	if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
   1039 	    (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
   1040 		printf("%s: disabling %s channel (no drives)\n",
   1041 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1042 		cp->hw_ok = 0;
   1043 		return 1;
   1044 	}
   1045 	return 0;
   1046 }
   1047 
   1048 /*
   1049  * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
   1050  * Set hw_ok=0 on failure
   1051  */
   1052 void
   1053 pciide_map_compat_intr(pa, cp, compatchan, interface)
   1054 	struct pci_attach_args *pa;
   1055 	struct pciide_channel *cp;
   1056 	int compatchan, interface;
   1057 {
   1058 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1059 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1060 
   1061 	if (cp->hw_ok == 0)
   1062 		return;
   1063 	if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
   1064 		return;
   1065 
   1066 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
   1067 	    pa, compatchan, pciide_compat_intr, cp);
   1068 	if (cp->ih == NULL) {
   1069 		printf("%s: no compatibility interrupt for use by %s "
   1070 		    "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1071 		cp->hw_ok = 0;
   1072 	}
   1073 }
   1074 
   1075 void
   1076 pciide_print_modes(cp)
   1077 	struct pciide_channel *cp;
   1078 {
   1079 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1080 	int drive;
   1081 	struct channel_softc *chp;
   1082 	struct ata_drive_datas *drvp;
   1083 
   1084 	chp = &cp->wdc_channel;
   1085 	for (drive = 0; drive < 2; drive++) {
   1086 		drvp = &chp->ch_drive[drive];
   1087 		if ((drvp->drive_flags & DRIVE) == 0)
   1088 			continue;
   1089 		printf("%s(%s:%d:%d): using PIO mode %d",
   1090 		    drvp->drv_softc->dv_xname,
   1091 		    sc->sc_wdcdev.sc_dev.dv_xname,
   1092 		    chp->channel, drive, drvp->PIO_mode);
   1093 		if (drvp->drive_flags & DRIVE_DMA)
   1094 			printf(", DMA mode %d", drvp->DMA_mode);
   1095 		if (drvp->drive_flags & DRIVE_UDMA)
   1096 			printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
   1097 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
   1098 			printf(" (using DMA data transfers)");
   1099 		printf("\n");
   1100 	}
   1101 }
   1102 
   1103 void
   1104 default_setup_cap(sc)
   1105 	struct pciide_softc *sc;
   1106 {
   1107 	if (sc->sc_dma_ok)
   1108 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
   1109 	sc->sc_wdcdev.PIO_cap = 0;
   1110 	sc->sc_wdcdev.DMA_cap = 0;
   1111 }
   1112 
   1113 void
   1114 default_setup_chip(sc)
   1115 	struct pciide_softc *sc;
   1116 {
   1117 	int channel, drive, idedma_ctl;
   1118 	struct channel_softc *chp;
   1119 	struct ata_drive_datas *drvp;
   1120 
   1121 	if (sc->sc_dma_ok == 0)
   1122 		return; /* nothing to do */
   1123 
   1124 	/* Allocate DMA maps */
   1125 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1126 		idedma_ctl = 0;
   1127 		chp = &sc->pciide_channels[channel].wdc_channel;
   1128 		for (drive = 0; drive < 2; drive++) {
   1129 			drvp = &chp->ch_drive[drive];
   1130 			/* If no drive, skip */
   1131 			if ((drvp->drive_flags & DRIVE) == 0)
   1132 				continue;
   1133 			if ((drvp->drive_flags & DRIVE_DMA) == 0)
   1134 				continue;
   1135 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1136 				/* Abort DMA setup */
   1137 				printf("%s:%d:%d: can't allocate DMA maps, "
   1138 				    "using PIO transfers\n",
   1139 				    sc->sc_wdcdev.sc_dev.dv_xname,
   1140 				    channel, drive);
   1141 				drvp->drive_flags &= ~DRIVE_DMA;
   1142 			}
   1143 			printf("%s:%d:%d: using DMA data tranferts\n",
   1144 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1145 			    channel, drive);
   1146 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1147 		}
   1148 		if (idedma_ctl != 0) {
   1149 			/* Add software bits in status register */
   1150 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1151 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1152 			    idedma_ctl);
   1153 		}
   1154 	}
   1155 
   1156 }
   1157 
   1158 void
   1159 default_channel_map(pa, cp)
   1160 	struct pci_attach_args *pa;
   1161 	struct pciide_channel *cp;
   1162 {
   1163 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1164 	bus_size_t cmdsize, ctlsize;
   1165 	pcireg_t csr;
   1166 	const char *failreason = NULL;
   1167 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1168 	int interface =
   1169 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1170 
   1171 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
   1172 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
   1173 	else
   1174 		cp->hw_ok = pciide_mapregs_compat(pa, cp, wdc_cp->channel,
   1175 		    &cmdsize, &ctlsize);
   1176 	if (cp->hw_ok == 0)
   1177 		return;
   1178 
   1179 	/*
   1180 	 * Check to see if something appears to be there.
   1181 	 */
   1182 	if (!wdcprobe(wdc_cp)) {
   1183 		failreason = "not responding; disabled or no drives?";
   1184 		goto out;
   1185 	}
   1186 
   1187 	/*
   1188 	 * Now, make sure it's actually attributable to this PCI IDE
   1189 	 * channel by trying to access the channel again while the
   1190 	 * PCI IDE controller's I/O space is disabled.  (If the
   1191 	 * channel no longer appears to be there, it belongs to
   1192 	 * this controller.)  YUCK!
   1193 	 */
   1194 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
   1195 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
   1196 	    csr & ~PCI_COMMAND_IO_ENABLE);
   1197 	if (wdcprobe(wdc_cp))
   1198 		failreason = "other hardware responding at addresses";
   1199 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
   1200 
   1201 out:
   1202 	if (failreason) {
   1203 		printf("%s: %s channel ignored (%s)\n",
   1204 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
   1205 		    failreason);
   1206 		cp->hw_ok = 0;
   1207 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, cmdsize);
   1208 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh, ctlsize);
   1209 	}
   1210 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1211 	if (cp->hw_ok) {
   1212 		wdc_cp->data32iot = wdc_cp->cmd_iot;
   1213 		wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1214 		wdcattach(wdc_cp);
   1215 	}
   1216 }
   1217 
   1218 void
   1219 piix_setup_cap(sc)
   1220 	struct pciide_softc *sc;
   1221 {
   1222 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
   1223 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1224 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1225 	    WDC_CAPABILITY_DMA;
   1226 	sc->sc_wdcdev.PIO_cap = 4;
   1227 	sc->sc_wdcdev.DMA_cap = 2;
   1228 	sc->sc_wdcdev.UDMA_cap = 2;
   1229 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371SB_IDE ||
   1230 	    sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
   1231 		sc->sc_wdcdev.set_modes = piix3_4_setup_channel;
   1232 	else
   1233 		sc->sc_wdcdev.set_modes = piix_setup_channel;
   1234 }
   1235 
   1236 void
   1237 piix_setup_chip(sc)
   1238 	struct pciide_softc *sc;
   1239 {
   1240 	u_int8_t channel;
   1241 
   1242 
   1243 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x\n",
   1244 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
   1245 
   1246 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1247 		piix_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1248 	}
   1249 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x\n",
   1250 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
   1251 }
   1252 
   1253 void
   1254 piix_setup_channel(chp)
   1255 	struct channel_softc *chp;
   1256 {
   1257 	u_int8_t mode[2], drive;
   1258 	u_int32_t oidetim, idetim, idedma_ctl;
   1259 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1260 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1261 	struct ata_drive_datas *drvp = cp->wdc_channel.ch_drive;
   1262 
   1263 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1264 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1265 	idedma_ctl = 0;
   1266 
   1267 	/* set up new idetim: Enable IDE registers decode */
   1268 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
   1269 	    chp->channel);
   1270 
   1271 	/* setup DMA */
   1272 	pciide_channel_dma_setup(cp);
   1273 
   1274 	/*
   1275 	 * Here we have to mess up with drives mode: PIIX can't have
   1276 	 * different timings for master and slave drives.
   1277 	 * We need to find the best combination.
   1278 	 */
   1279 
   1280 	/* If both drives supports DMA, take the lower mode */
   1281 	if ((drvp[0].drive_flags & DRIVE_DMA) &&
   1282 	    (drvp[1].drive_flags & DRIVE_DMA)) {
   1283 		mode[0] = mode[1] =
   1284 		    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
   1285 		    drvp[0].DMA_mode = mode[0];
   1286 		    drvp[1].DMA_mode = mode[1];
   1287 		goto ok;
   1288 	}
   1289 	/*
   1290 	 * If only one drive supports DMA, use its mode, and
   1291 	 * put the other one in PIO mode 0 if mode not compatible
   1292 	 */
   1293 	if (drvp[0].drive_flags & DRIVE_DMA) {
   1294 		mode[0] = drvp[0].DMA_mode;
   1295 		mode[1] = drvp[1].PIO_mode;
   1296 		if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
   1297 		    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
   1298 			mode[1] = drvp[1].PIO_mode = 0;
   1299 		goto ok;
   1300 	}
   1301 	if (drvp[1].drive_flags & DRIVE_DMA) {
   1302 		mode[1] = drvp[1].DMA_mode;
   1303 		mode[0] = drvp[0].PIO_mode;
   1304 		if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
   1305 		    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
   1306 			mode[0] = drvp[0].PIO_mode = 0;
   1307 		goto ok;
   1308 	}
   1309 	/*
   1310 	 * If both drives are not DMA, takes the lower mode, unless
   1311 	 * one of them is PIO mode < 2
   1312 	 */
   1313 	if (drvp[0].PIO_mode < 2) {
   1314 		mode[0] = drvp[0].PIO_mode = 0;
   1315 		mode[1] = drvp[1].PIO_mode;
   1316 	} else if (drvp[1].PIO_mode < 2) {
   1317 		mode[1] = drvp[1].PIO_mode = 0;
   1318 		mode[0] = drvp[0].PIO_mode;
   1319 	} else {
   1320 		mode[0] = mode[1] =
   1321 		    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
   1322 		drvp[0].PIO_mode = mode[0];
   1323 		drvp[1].PIO_mode = mode[1];
   1324 	}
   1325 ok:	/* The modes are setup */
   1326 	for (drive = 0; drive < 2; drive++) {
   1327 		if (drvp[drive].drive_flags & DRIVE_DMA) {
   1328 			idetim |= piix_setup_idetim_timings(
   1329 			    mode[drive], 1, chp->channel);
   1330 			goto end;
   1331 		}
   1332 	}
   1333 	/* If we are there, none of the drives are DMA */
   1334 	if (mode[0] >= 2)
   1335 		idetim |= piix_setup_idetim_timings(
   1336 		    mode[0], 0, chp->channel);
   1337 	else
   1338 		idetim |= piix_setup_idetim_timings(
   1339 		    mode[1], 0, chp->channel);
   1340 end:	/*
   1341 	 * timing mode is now set up in the controller. Enable
   1342 	 * it per-drive
   1343 	 */
   1344 	for (drive = 0; drive < 2; drive++) {
   1345 		/* If no drive, skip */
   1346 		if ((drvp[drive].drive_flags & DRIVE) == 0)
   1347 			continue;
   1348 		idetim |= piix_setup_idetim_drvs(&drvp[drive]);
   1349 		if (drvp[drive].drive_flags & DRIVE_DMA)
   1350 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1351 	}
   1352 	if (idedma_ctl != 0) {
   1353 		/* Add software bits in status register */
   1354 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1355 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1356 		    idedma_ctl);
   1357 	}
   1358 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1359 	pciide_print_modes(cp);
   1360 }
   1361 
   1362 void
   1363 piix3_4_setup_chip(sc)
   1364 	struct pciide_softc *sc;
   1365 {
   1366 	int channel;
   1367 
   1368 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
   1369 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1370 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1371 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1372 		WDCDEBUG_PRINT((", udamreg 0x%x",
   1373 		    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1374 		    DEBUG_PROBE);
   1375 	}
   1376 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1377 
   1378 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1379 		piix3_4_setup_channel(
   1380 		    &sc->pciide_channels[channel].wdc_channel);
   1381 	}
   1382 
   1383 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
   1384 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1385 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1386 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1387 		WDCDEBUG_PRINT((", udmareg=0x%x",
   1388 		pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1389 		DEBUG_PROBE);
   1390 	}
   1391 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1392 }
   1393 
   1394 void
   1395 piix3_4_setup_channel(chp)
   1396 	struct channel_softc *chp;
   1397 {
   1398 	struct ata_drive_datas *drvp;
   1399 	u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
   1400 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1401 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1402 	int drive;
   1403 
   1404 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1405 	sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
   1406 	udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
   1407 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1408 	sidetim &= ~(PIIX_SIDETIM_ISP_MASK(chp->channel) |
   1409 	    PIIX_SIDETIM_RTC_MASK(chp->channel));
   1410 
   1411 	idedma_ctl = 0;
   1412 	/* If channel disabled, no need to go further */
   1413 	if ((PIIX_IDETIM_READ(oidetim, chp->channel) & PIIX_IDETIM_IDE) == 0)
   1414 		return;
   1415 	/* set up new idetim: Enable IDE registers decode */
   1416 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, chp->channel);
   1417 
   1418 	/* setup DMA if needed */
   1419 	pciide_channel_dma_setup(cp);
   1420 
   1421 	for (drive = 0; drive < 2; drive++) {
   1422 		udmareg &= ~(PIIX_UDMACTL_DRV_EN(chp->channel, drive) |
   1423 		    PIIX_UDMATIM_SET(0x3, chp->channel, drive));
   1424 		drvp = &chp->ch_drive[drive];
   1425 		/* If no drive, skip */
   1426 		if ((drvp->drive_flags & DRIVE) == 0)
   1427 			continue;
   1428 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1429 		    (drvp->drive_flags & DRIVE_UDMA) == 0))
   1430 			goto pio;
   1431 
   1432 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1433 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1434 			/* use Ultra/DMA */
   1435 			drvp->drive_flags &= ~DRIVE_DMA;
   1436 			udmareg |= PIIX_UDMACTL_DRV_EN(
   1437 			    chp->channel, drive);
   1438 			udmareg |= PIIX_UDMATIM_SET(
   1439 			    piix4_sct_udma[drvp->UDMA_mode],
   1440 			    chp->channel, drive);
   1441 		} else {
   1442 			/* use Multiword DMA */
   1443 			drvp->drive_flags &= ~DRIVE_UDMA;
   1444 			if (drive == 0) {
   1445 				idetim |= piix_setup_idetim_timings(
   1446 				    drvp->DMA_mode, 1, chp->channel);
   1447 			} else {
   1448 				sidetim |= piix_setup_sidetim_timings(
   1449 					drvp->DMA_mode, 1, chp->channel);
   1450 				idetim =PIIX_IDETIM_SET(idetim,
   1451 				    PIIX_IDETIM_SITRE, chp->channel);
   1452 			}
   1453 		}
   1454 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1455 
   1456 pio:		/* use PIO mode */
   1457 		idetim |= piix_setup_idetim_drvs(drvp);
   1458 		if (drive == 0) {
   1459 			idetim |= piix_setup_idetim_timings(
   1460 			    drvp->PIO_mode, 0, chp->channel);
   1461 		} else {
   1462 			sidetim |= piix_setup_sidetim_timings(
   1463 				drvp->PIO_mode, 0, chp->channel);
   1464 			idetim =PIIX_IDETIM_SET(idetim,
   1465 			    PIIX_IDETIM_SITRE, chp->channel);
   1466 		}
   1467 	}
   1468 	if (idedma_ctl != 0) {
   1469 		/* Add software bits in status register */
   1470 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1471 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1472 		    idedma_ctl);
   1473 	}
   1474 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1475 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
   1476 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
   1477 	pciide_print_modes(cp);
   1478 }
   1479 
   1480 
   1481 /* setup ISP and RTC fields, based on mode */
   1482 static u_int32_t
   1483 piix_setup_idetim_timings(mode, dma, channel)
   1484 	u_int8_t mode;
   1485 	u_int8_t dma;
   1486 	u_int8_t channel;
   1487 {
   1488 
   1489 	if (dma)
   1490 		return PIIX_IDETIM_SET(0,
   1491 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1492 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1493 		    channel);
   1494 	else
   1495 		return PIIX_IDETIM_SET(0,
   1496 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1497 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1498 		    channel);
   1499 }
   1500 
   1501 /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1502 static u_int32_t
   1503 piix_setup_idetim_drvs(drvp)
   1504 	struct ata_drive_datas *drvp;
   1505 {
   1506 	u_int32_t ret = 0;
   1507 	struct channel_softc *chp = drvp->chnl_softc;
   1508 	u_int8_t channel = chp->channel;
   1509 	u_int8_t drive = drvp->drive;
   1510 
   1511 	/*
   1512 	 * If drive is using UDMA, timings setups are independant
   1513 	 * So just check DMA and PIO here.
   1514 	 */
   1515 	if (drvp->drive_flags & DRIVE_DMA) {
   1516 		/* if mode = DMA mode 0, use compatible timings */
   1517 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1518 		    drvp->DMA_mode == 0) {
   1519 			drvp->PIO_mode = 0;
   1520 			return ret;
   1521 		}
   1522 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1523 		/*
   1524 		 * PIO and DMA timings are the same, use fast timings for PIO
   1525 		 * too, else use compat timings.
   1526 		 */
   1527 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1528 		    piix_isp_dma[drvp->DMA_mode]) ||
   1529 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1530 		    piix_rtc_dma[drvp->DMA_mode]))
   1531 			drvp->PIO_mode = 0;
   1532 		/* if PIO mode <= 2, use compat timings for PIO */
   1533 		if (drvp->PIO_mode <= 2) {
   1534 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1535 			    channel);
   1536 			return ret;
   1537 		}
   1538 	}
   1539 
   1540 	/*
   1541 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1542 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1543 	 * if PIO mode >= 3.
   1544 	 */
   1545 
   1546 	if (drvp->PIO_mode < 2)
   1547 		return ret;
   1548 
   1549 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1550 	if (drvp->PIO_mode >= 3) {
   1551 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1552 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1553 	}
   1554 	return ret;
   1555 }
   1556 
   1557 /* setup values in SIDETIM registers, based on mode */
   1558 static u_int32_t
   1559 piix_setup_sidetim_timings(mode, dma, channel)
   1560 	u_int8_t mode;
   1561 	u_int8_t dma;
   1562 	u_int8_t channel;
   1563 {
   1564 	if (dma)
   1565 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1566 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1567 	else
   1568 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1569 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1570 }
   1571 
   1572 void
   1573 piix_channel_map(pa, cp)
   1574 	struct pci_attach_args *pa;
   1575 	struct pciide_channel *cp;
   1576 {
   1577 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1578 	bus_size_t cmdsize, ctlsize;
   1579 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1580 	u_int32_t idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1581 
   1582 	if ((PIIX_IDETIM_READ(idetim, wdc_cp->channel) &
   1583 	    PIIX_IDETIM_IDE) == 0) {
   1584 		printf("%s: %s channel ignored (disabled)\n",
   1585 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1586 		return;
   1587 	}
   1588 
   1589 	/* PIIX are compat-only pciide devices */
   1590 	pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize);
   1591 	if (cp->hw_ok == 0)
   1592 		return;
   1593 	if (pciiide_chan_candisable(cp)) {
   1594 		idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
   1595 					   wdc_cp->channel);
   1596 		pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1597 	}
   1598 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, 0);
   1599 }
   1600 
   1601 void
   1602 apollo_setup_cap(sc)
   1603 	struct pciide_softc *sc;
   1604 {
   1605 	if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
   1606 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1607 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1608 	    WDC_CAPABILITY_DMA;
   1609 	sc->sc_wdcdev.PIO_cap = 4;
   1610 	sc->sc_wdcdev.DMA_cap = 2;
   1611 	sc->sc_wdcdev.UDMA_cap = 2;
   1612 	sc->sc_wdcdev.set_modes = apollo_setup_channel;
   1613 
   1614 }
   1615 
   1616 void
   1617 apollo_setup_chip(sc)
   1618 	struct pciide_softc *sc;
   1619 {
   1620 	int channel;
   1621 
   1622 	WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
   1623 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1624 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
   1625 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
   1626 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1627 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
   1628 	    DEBUG_PROBE);
   1629 
   1630 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1631 		apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1632 	}
   1633 	WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1634 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1635 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
   1636 }
   1637 
   1638 void
   1639 apollo_setup_channel(chp)
   1640 	struct channel_softc *chp;
   1641 {
   1642 	u_int32_t udmatim_reg, datatim_reg;
   1643 	u_int8_t idedma_ctl;
   1644 	int mode, drive;
   1645 	struct ata_drive_datas *drvp;
   1646 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1647 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1648 
   1649 	idedma_ctl = 0;
   1650 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
   1651 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
   1652 	datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
   1653 	udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
   1654 
   1655 	/* setup DMA if needed */
   1656 	pciide_channel_dma_setup(cp);
   1657 
   1658 	for (drive = 0; drive < 2; drive++) {
   1659 		drvp = &chp->ch_drive[drive];
   1660 		/* If no drive, skip */
   1661 		if ((drvp->drive_flags & DRIVE) == 0)
   1662 			continue;
   1663 		/* add timing values, setup DMA if needed */
   1664 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1665 		    (drvp->drive_flags & DRIVE_UDMA) == 0)) {
   1666 			mode = drvp->PIO_mode;
   1667 			goto pio;
   1668 		}
   1669 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1670 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1671 			/* use Ultra/DMA */
   1672 			drvp->drive_flags &= ~DRIVE_DMA;
   1673 			udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
   1674 			    APO_UDMA_EN_MTH(chp->channel, drive) |
   1675 			    APO_UDMA_TIME(chp->channel, drive,
   1676 				apollo_udma_tim[drvp->UDMA_mode]);
   1677 			/* can use PIO timings, MW DMA unused */
   1678 			mode = drvp->PIO_mode;
   1679 		} else {
   1680 			/* use Multiword DMA */
   1681 			drvp->drive_flags &= ~DRIVE_UDMA;
   1682 			/* mode = min(pio, dma+2) */
   1683 			if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1684 				mode = drvp->PIO_mode;
   1685 			else
   1686 				mode = drvp->DMA_mode + 2;
   1687 		}
   1688 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1689 
   1690 pio:		/* setup PIO mode */
   1691 		if (mode <= 2) {
   1692 			drvp->DMA_mode = 0;
   1693 			drvp->PIO_mode = 0;
   1694 			mode = 0;
   1695 		} else {
   1696 			drvp->PIO_mode = mode;
   1697 			drvp->DMA_mode = mode - 2;
   1698 		}
   1699 		datatim_reg |=
   1700 		    APO_DATATIM_PULSE(chp->channel, drive,
   1701 			apollo_pio_set[mode]) |
   1702 		    APO_DATATIM_RECOV(chp->channel, drive,
   1703 			apollo_pio_rec[mode]);
   1704 	}
   1705 	if (idedma_ctl != 0) {
   1706 		/* Add software bits in status register */
   1707 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1708 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1709 		    idedma_ctl);
   1710 	}
   1711 	pciide_print_modes(cp);
   1712 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
   1713 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
   1714 }
   1715 
   1716 void
   1717 apollo_channel_map(pa, cp)
   1718 	struct pci_attach_args *pa;
   1719 	struct pciide_channel *cp;
   1720 {
   1721 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1722 	bus_size_t cmdsize, ctlsize;
   1723 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1724 	u_int32_t ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
   1725 	int interface =
   1726 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1727 
   1728 	if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
   1729 		printf("%s: %s channel ignored (disabled)\n",
   1730 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1731 		return;
   1732 	}
   1733 
   1734 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1735 	if (cp->hw_ok == 0)
   1736 		return;
   1737 	if (pciiide_chan_candisable(cp)) {
   1738 		ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
   1739 		pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF, ideconf);
   1740 	}
   1741 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1742 }
   1743 
   1744 void
   1745 cmd_channel_map(pa, cp)
   1746 	struct pci_attach_args *pa;
   1747 	struct pciide_channel *cp;
   1748 {
   1749 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1750 	bus_size_t cmdsize, ctlsize;
   1751 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1752 	u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
   1753 	int interface =
   1754 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1755 
   1756 	/*
   1757 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   1758 	 * there's no way to disable the first channel without disabling
   1759 	 * the whole device
   1760 	 */
   1761 	if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
   1762 		printf("%s: %s channel ignored (disabled)\n",
   1763 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1764 		return;
   1765 	}
   1766 
   1767 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1768 	if (cp->hw_ok == 0)
   1769 		return;
   1770 	if (wdc_cp->channel == 1) {
   1771 		if (pciiide_chan_candisable(cp)) {
   1772 			ctrl &= ~CMD_CTRL_2PORT;
   1773 			pciide_pci_write(pa->pa_pc, pa->pa_tag,
   1774 			    CMD_CTRL, ctrl);
   1775 		}
   1776 	}
   1777 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1778 }
   1779 
   1780 void
   1781 cmd0643_6_setup_cap(sc)
   1782 	struct pciide_softc *sc;
   1783 {
   1784 
   1785 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1786 	    WDC_CAPABILITY_DMA;
   1787 	sc->sc_wdcdev.PIO_cap = 4;
   1788 	sc->sc_wdcdev.DMA_cap = 2;
   1789 	sc->sc_wdcdev.set_modes = cmd0643_6_setup_channel;
   1790 }
   1791 
   1792 void
   1793 cmd0643_6_setup_chip(sc)
   1794 	struct pciide_softc *sc;
   1795 {
   1796 	int channel;
   1797 
   1798 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
   1799 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1800 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1801 		DEBUG_PROBE);
   1802 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1803 		cmd0643_6_setup_channel(
   1804 		    &sc->pciide_channels[channel].wdc_channel);
   1805 	}
   1806 	/* configure for DMA read multiple */
   1807 	pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
   1808 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
   1809 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1810 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1811 	    DEBUG_PROBE);
   1812 }
   1813 
   1814 void
   1815 cmd0643_6_setup_channel(chp)
   1816 	struct channel_softc *chp;
   1817 {
   1818 	struct ata_drive_datas *drvp;
   1819 	u_int8_t tim;
   1820 	u_int32_t idedma_ctl;
   1821 	int drive;
   1822 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1823 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1824 
   1825 	idedma_ctl = 0;
   1826 	/* setup DMA if needed */
   1827 	pciide_channel_dma_setup(cp);
   1828 
   1829 	for (drive = 0; drive < 2; drive++) {
   1830 		drvp = &chp->ch_drive[drive];
   1831 		/* If no drive, skip */
   1832 		if ((drvp->drive_flags & DRIVE) == 0)
   1833 			continue;
   1834 		/* add timing values, setup DMA if needed */
   1835 		tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
   1836 		if (drvp->drive_flags & DRIVE_DMA) {
   1837 			/*
   1838 			 * use Multiword DMA.
   1839 			 * Timings will be used for both PIO and DMA, so adjust
   1840 			 * DMA mode if needed
   1841 			 */
   1842 			if (drvp->PIO_mode >= 3 &&
   1843 			    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
   1844 				drvp->DMA_mode = drvp->PIO_mode - 2;
   1845 			}
   1846 			tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
   1847 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1848 		}
   1849 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   1850 		    CMD_DATA_TIM(chp->channel, drive), tim);
   1851 	}
   1852 	if (idedma_ctl != 0) {
   1853 		/* Add software bits in status register */
   1854 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1855 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1856 		    idedma_ctl);
   1857 	}
   1858 	pciide_print_modes(cp);
   1859 }
   1860 
   1861 void
   1862 cy693_setup_cap(sc)
   1863 	struct pciide_softc *sc;
   1864 {
   1865 
   1866 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1867 	    WDC_CAPABILITY_DMA;
   1868 	sc->sc_wdcdev.PIO_cap = 4;
   1869 	sc->sc_wdcdev.DMA_cap = 2;
   1870 	sc->sc_wdcdev.set_modes = cy693_setup_channel;
   1871 }
   1872 
   1873 void
   1874 cy693_setup_chip(sc)
   1875 	struct pciide_softc *sc;
   1876 {
   1877 
   1878 	WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
   1879 		pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),
   1880 		DEBUG_PROBE);
   1881 	cy693_setup_channel(&sc->pciide_channels[0].wdc_channel);
   1882 	WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
   1883 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
   1884 }
   1885 
   1886 void
   1887 cy693_setup_channel(chp)
   1888 	struct channel_softc *chp;
   1889 {
   1890 	struct ata_drive_datas *drvp;
   1891 	int drive;
   1892 	u_int32_t cy_cmd_ctrl;
   1893 	u_int32_t idedma_ctl;
   1894 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1895 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1896 
   1897 	cy_cmd_ctrl = idedma_ctl = 0;
   1898 
   1899 	/* setup DMA if needed */
   1900 	pciide_channel_dma_setup(cp);
   1901 
   1902 	for (drive = 0; drive < 2; drive++) {
   1903 		drvp = &chp->ch_drive[drive];
   1904 		/* If no drive, skip */
   1905 		if ((drvp->drive_flags & DRIVE) == 0)
   1906 			continue;
   1907 		/* add timing values, setup DMA if needed */
   1908 		if (drvp->drive_flags & DRIVE_DMA) {
   1909 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1910 			/*
   1911 			 * use Multiword DMA
   1912 			 * Timings will be used for both PIO and DMA, so adjust
   1913 			 * DMA mode if needed
   1914 			 */
   1915 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   1916 				drvp->PIO_mode = drvp->DMA_mode + 2;
   1917 			if (drvp->DMA_mode == 0)
   1918 				drvp->PIO_mode = 0;
   1919 		}
   1920 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1921 		    CY_CMD_CTRL_IOW_PULSE_OFF(drive));
   1922 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1923 		    CY_CMD_CTRL_IOW_REC_OFF(drive));
   1924 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1925 		    CY_CMD_CTRL_IOR_PULSE_OFF(drive));
   1926 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1927 		    CY_CMD_CTRL_IOR_REC_OFF(drive));
   1928 	}
   1929 	pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
   1930 	pciide_print_modes(cp);
   1931 	if (idedma_ctl != 0) {
   1932 		/* Add software bits in status register */
   1933 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1934 		    IDEDMA_CTL, idedma_ctl);
   1935 	}
   1936 }
   1937 
   1938 void
   1939 cy693_channel_map(pa, cp)
   1940 	struct pci_attach_args *pa;
   1941 	struct pciide_channel *cp;
   1942 {
   1943 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1944 	bus_size_t cmdsize, ctlsize;
   1945 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1946 	int interface =
   1947 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1948 	int compatchan;
   1949 
   1950 #ifdef DIAGNOSTIC
   1951 	if (wdc_cp->channel != 0)
   1952 		panic("cy693_channel_map: channel %d", wdc_cp->channel);
   1953 #endif
   1954 
   1955 	/*
   1956 	 * this chip has 2 PCI IDE functions, one for primary and one for
   1957 	 * secondary. So we need to call pciide_mapregs_compat() with
   1958 	 * the real channel
   1959 	 */
   1960 	if (pa->pa_function == 1) {
   1961 		compatchan = 0;
   1962 	} else if (pa->pa_function == 2) {
   1963 		compatchan = 1;
   1964 	} else {
   1965 		printf("%s: unexpected PCI function %d\n",
   1966 		    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
   1967 		cp->hw_ok = 0;
   1968 		return;
   1969 	}
   1970 
   1971 	/* Only one channel for this chip; if we are here it's enabled */
   1972 	if (interface & PCIIDE_INTERFACE_PCI(0))
   1973 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
   1974 	else
   1975 		cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
   1976 		    &cmdsize, &ctlsize);
   1977 	if (cp->hw_ok == 0)
   1978 		return;
   1979 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1980 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1981 	wdcattach(wdc_cp);
   1982 	if (pciiide_chan_candisable(cp)) {
   1983 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   1984 		    PCI_COMMAND_STATUS_REG, 0);
   1985 	}
   1986 	pciide_map_compat_intr(pa, cp, compatchan, interface);
   1987 }
   1988 
   1989 void
   1990 sis_setup_cap(sc)
   1991 	struct pciide_softc *sc;
   1992 {
   1993 
   1994 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1995 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   1996 	sc->sc_wdcdev.PIO_cap = 4;
   1997 	sc->sc_wdcdev.DMA_cap = 2;
   1998 	sc->sc_wdcdev.UDMA_cap = 2;
   1999 	sc->sc_wdcdev.set_modes = sis_setup_channel;
   2000 }
   2001 
   2002 void
   2003 sis_setup_chip(sc)
   2004 	struct pciide_softc *sc;
   2005 {
   2006 	int channel;
   2007 
   2008 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2009 		sis_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   2010 	}
   2011 	pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
   2012 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
   2013 	    SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
   2014 }
   2015 
   2016 void
   2017 sis_setup_channel(chp)
   2018 	struct channel_softc *chp;
   2019 {
   2020 	struct ata_drive_datas *drvp;
   2021 	int drive;
   2022 	u_int32_t sis_tim;
   2023 	u_int32_t idedma_ctl;
   2024 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2025 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2026 
   2027 	WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
   2028 	    "channel %d 0x%x\n", chp->channel,
   2029 	    pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
   2030 	    DEBUG_PROBE);
   2031 	sis_tim = 0;
   2032 	idedma_ctl = 0;
   2033 	/* setup DMA if needed */
   2034 	pciide_channel_dma_setup(cp);
   2035 
   2036 	for (drive = 0; drive < 2; drive++) {
   2037 		drvp = &chp->ch_drive[drive];
   2038 		/* If no drive, skip */
   2039 		if ((drvp->drive_flags & DRIVE) == 0)
   2040 			continue;
   2041 		/* add timing values, setup DMA if needed */
   2042 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2043 		    (drvp->drive_flags & DRIVE_UDMA) == 0)
   2044 			goto pio;
   2045 
   2046 		if (drvp->drive_flags & DRIVE_UDMA) {
   2047 			/* use Ultra/DMA */
   2048 			drvp->drive_flags &= ~DRIVE_DMA;
   2049 			sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
   2050 			    SIS_TIM_UDMA_TIME_OFF(drive);
   2051 			sis_tim |= SIS_TIM_UDMA_EN(drive);
   2052 		} else {
   2053 			/*
   2054 			 * use Multiword DMA
   2055 			 * Timings will be used for both PIO and DMA,
   2056 			 * so adjust DMA mode if needed
   2057 			 */
   2058 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2059 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2060 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2061 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2062 				    drvp->PIO_mode - 2 : 0;
   2063 			if (drvp->DMA_mode == 0)
   2064 				drvp->PIO_mode = 0;
   2065 		}
   2066 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2067 pio:		sis_tim |= sis_pio_act[drvp->PIO_mode] <<
   2068 		    SIS_TIM_ACT_OFF(drive);
   2069 		sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
   2070 		    SIS_TIM_REC_OFF(drive);
   2071 	}
   2072 	WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
   2073 	    "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
   2074 	pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
   2075 	if (idedma_ctl != 0) {
   2076 		/* Add software bits in status register */
   2077 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2078 		    IDEDMA_CTL, idedma_ctl);
   2079 	}
   2080 	pciide_print_modes(cp);
   2081 }
   2082 
   2083 void
   2084 sis_channel_map(pa, cp)
   2085 	struct pci_attach_args *pa;
   2086 	struct pciide_channel *cp;
   2087 {
   2088 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2089 	bus_size_t cmdsize, ctlsize;
   2090 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2091 	u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
   2092 	int interface =
   2093 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   2094 
   2095 	if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
   2096 	    (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
   2097 		printf("%s: %s channel ignored (disabled)\n",
   2098 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2099 		return;
   2100 	}
   2101 
   2102 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2103 	if (cp->hw_ok == 0)
   2104 		return;
   2105 	if (pciiide_chan_candisable(cp)) {
   2106 		if (wdc_cp->channel == 0)
   2107 			sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
   2108 		else
   2109 			sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
   2110 		pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0, sis_ctr0);
   2111 	}
   2112 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2113 }
   2114 
   2115 void
   2116 acer_setup_cap(sc)
   2117 	struct pciide_softc *sc;
   2118 {
   2119 
   2120 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   2121 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   2122 	sc->sc_wdcdev.PIO_cap = 4;
   2123 	sc->sc_wdcdev.DMA_cap = 2;
   2124 	sc->sc_wdcdev.UDMA_cap = 2;
   2125 	sc->sc_wdcdev.set_modes = acer_setup_channel;
   2126 }
   2127 
   2128 void
   2129 acer_setup_chip(sc)
   2130 	struct pciide_softc *sc;
   2131 {
   2132 	int channel;
   2133 
   2134 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
   2135 	    (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
   2136 		ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
   2137 
   2138 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2139 		acer_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   2140 	}
   2141 }
   2142 
   2143 void
   2144 acer_setup_channel(chp)
   2145 	struct channel_softc *chp;
   2146 {
   2147 	struct ata_drive_datas *drvp;
   2148 	int drive;
   2149 	u_int32_t acer_fifo_udma;
   2150 	u_int32_t idedma_ctl;
   2151 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2152 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2153 
   2154 	idedma_ctl = 0;
   2155 	acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
   2156 	WDCDEBUG_PRINT(("acer_setup_chip: old fifo/udma reg 0x%x\n",
   2157 	    acer_fifo_udma), DEBUG_PROBE);
   2158 	/* setup DMA if needed */
   2159 	pciide_channel_dma_setup(cp);
   2160 
   2161 	for (drive = 0; drive < 2; drive++) {
   2162 		drvp = &chp->ch_drive[drive];
   2163 		/* If no drive, skip */
   2164 		if ((drvp->drive_flags & DRIVE) == 0)
   2165 			continue;
   2166 		WDCDEBUG_PRINT(("acer_setup_chip: old timings reg for "
   2167 		    "channel %d drive %d 0x%x\n", chp->channel, drive,
   2168 		    pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2169 		    ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
   2170 		/* clear FIFO/DMA mode */
   2171 		acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
   2172 		    ACER_UDMA_EN(chp->channel, drive) |
   2173 		    ACER_UDMA_TIM(chp->channel, drive, 0x7));
   2174 
   2175 		/* add timing values, setup DMA if needed */
   2176 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2177 		    (drvp->drive_flags & DRIVE_UDMA) == 0) {
   2178 			acer_fifo_udma |=
   2179 			    ACER_FTH_OPL(chp->channel, drive, 0x1);
   2180 			goto pio;
   2181 		}
   2182 
   2183 		acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
   2184 		if (drvp->drive_flags & DRIVE_UDMA) {
   2185 			/* use Ultra/DMA */
   2186 			drvp->drive_flags &= ~DRIVE_DMA;
   2187 			acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
   2188 			acer_fifo_udma |=
   2189 			    ACER_UDMA_TIM(chp->channel, drive,
   2190 				acer_udma[drvp->UDMA_mode]);
   2191 		} else {
   2192 			/*
   2193 			 * use Multiword DMA
   2194 			 * Timings will be used for both PIO and DMA,
   2195 			 * so adjust DMA mode if needed
   2196 			 */
   2197 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2198 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2199 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2200 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2201 				    drvp->PIO_mode - 2 : 0;
   2202 			if (drvp->DMA_mode == 0)
   2203 				drvp->PIO_mode = 0;
   2204 		}
   2205 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2206 pio:		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2207 		    ACER_IDETIM(chp->channel, drive),
   2208 		    acer_pio[drvp->PIO_mode]);
   2209 	}
   2210 	WDCDEBUG_PRINT(("acer_setup_chip: new fifo/udma reg 0x%x\n",
   2211 	    acer_fifo_udma), DEBUG_PROBE);
   2212 	pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
   2213 	if (idedma_ctl != 0) {
   2214 		/* Add software bits in status register */
   2215 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2216 		    IDEDMA_CTL, idedma_ctl);
   2217 	}
   2218 	pciide_print_modes(cp);
   2219 }
   2220 
   2221 void
   2222 acer_channel_map(pa, cp)
   2223 	struct pci_attach_args *pa;
   2224 	struct pciide_channel *cp;
   2225 {
   2226 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2227 	bus_size_t cmdsize, ctlsize;
   2228 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2229 	u_int32_t cr;
   2230 	int interface;
   2231 
   2232 	/*
   2233 	 * Enable "microsoft register bits" R/W. Will be done 2 times
   2234 	 * (one for each channel) but should'nt be a problem. There's no
   2235 	 * better place where to put this.
   2236 	 */
   2237 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
   2238 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
   2239 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
   2240 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
   2241 	    ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
   2242 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
   2243 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
   2244 	    ~ACER_CHANSTATUSREGS_RO);
   2245 	cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
   2246 	cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
   2247 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2248 	/* Don't use cr, re-read the real register content instead */
   2249 	interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
   2250 	    PCI_CLASS_REG));
   2251 
   2252 	if ((interface & PCIIDE_CHAN_EN(wdc_cp->channel)) == 0) {
   2253 		printf("%s: %s channel ignored (disabled)\n",
   2254 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2255 		return;
   2256 	}
   2257 
   2258 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2259 	if (cp->hw_ok == 0)
   2260 		return;
   2261 	if (pciiide_chan_candisable(cp)) {
   2262 		cr &= ~(PCIIDE_CHAN_EN(wdc_cp->channel) << PCI_INTERFACE_SHIFT);
   2263 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2264 	}
   2265 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2266 }
   2267