Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.36
      1 /*	$NetBSD: pciide.c,v 1.36 1999/05/03 07:44:47 ross 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 u_int8_t
     97 pciide_pci_read(pc, pa, reg)
     98 	pci_chipset_tag_t pc;
     99 	pcitag_t pa;
    100 	int reg;
    101 {
    102 	return (
    103 	    pci_conf_read(pc, pa, (reg & ~0x03)) >> ((reg & 0x03) * 8) & 0xff);
    104 }
    105 
    106 
    107 static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
    108 		int, u_int8_t));
    109 static __inline void
    110 pciide_pci_write(pc, pa, reg, val)
    111 	pci_chipset_tag_t pc;
    112 	pcitag_t pa;
    113 	int reg;
    114 	u_int8_t val;
    115 {
    116 	pcireg_t pcival;
    117 
    118 	pcival = pci_conf_read(pc, pa, (reg & ~0x03));
    119 	pcival &= ~(0xff << ((reg & 0x03) * 8));
    120 	pcival |= (val << ((reg & 0x03) * 8));
    121 	pci_conf_write(pc, pa, (reg & ~0x03), pcival);
    122 }
    123 
    124 struct pciide_softc {
    125 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
    126 	pci_chipset_tag_t	sc_pc;		/* PCI registers info */
    127 	pcitag_t		sc_tag;
    128 	void			*sc_pci_ih;	/* PCI interrupt handle */
    129 	int			sc_dma_ok;	/* bus-master DMA info */
    130 	bus_space_tag_t		sc_dma_iot;
    131 	bus_space_handle_t	sc_dma_ioh;
    132 	bus_dma_tag_t		sc_dmat;
    133 	/* Chip description */
    134 	const struct pciide_product_desc *sc_pp;
    135 	/* common definitions */
    136 	struct channel_softc *wdc_chanarray[PCIIDE_NUM_CHANNELS];
    137 	/* internal bookkeeping */
    138 	struct pciide_channel {			/* per-channel data */
    139 		struct channel_softc wdc_channel; /* generic part */
    140 		char		*name;
    141 		int		hw_ok;		/* hardware mapped & OK? */
    142 		int		compat;		/* is it compat? */
    143 		void		*ih;		/* compat or pci handle */
    144 		/* DMA tables and DMA map for xfer, for each drive */
    145 		struct pciide_dma_maps {
    146 			bus_dmamap_t    dmamap_table;
    147 			struct idedma_table *dma_table;
    148 			bus_dmamap_t    dmamap_xfer;
    149 		} dma_maps[2];
    150 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    151 };
    152 
    153 void default_setup_cap __P((struct pciide_softc*));
    154 void default_setup_chip __P((struct pciide_softc*));
    155 void default_channel_map __P((struct pci_attach_args *,
    156 		struct pciide_channel *));
    157 
    158 void piix_setup_cap __P((struct pciide_softc*));
    159 void piix_setup_chip __P((struct pciide_softc*));
    160 void piix_setup_channel __P((struct channel_softc*));
    161 void piix3_4_setup_chip __P((struct pciide_softc*));
    162 void piix3_4_setup_channel __P((struct channel_softc*));
    163 void piix_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    164 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    165 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    166 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    167 
    168 void apollo_setup_cap __P((struct pciide_softc*));
    169 void apollo_setup_chip __P((struct pciide_softc*));
    170 void apollo_setup_channel __P((struct channel_softc*));
    171 void apollo_channel_map __P((struct pci_attach_args *,
    172 		struct pciide_channel *));
    173 
    174 void cmd0643_6_setup_cap __P((struct pciide_softc*));
    175 void cmd0643_6_setup_chip __P((struct pciide_softc*));
    176 void cmd0643_6_setup_channel __P((struct channel_softc*));
    177 void cmd_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    178 
    179 void cy693_setup_cap __P((struct pciide_softc*));
    180 void cy693_setup_chip __P((struct pciide_softc*));
    181 void cy693_setup_channel __P((struct channel_softc*));
    182 void cy693_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    183 
    184 void sis_setup_cap __P((struct pciide_softc*));
    185 void sis_setup_chip __P((struct pciide_softc*));
    186 void sis_setup_channel __P((struct channel_softc*));
    187 void sis_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    188 
    189 void acer_setup_cap __P((struct pciide_softc*));
    190 void acer_setup_chip __P((struct pciide_softc*));
    191 void acer_setup_channel __P((struct channel_softc*));
    192 void acer_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    193 
    194 void pciide_channel_dma_setup __P((struct pciide_channel *));
    195 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    196 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    197 void pciide_dma_start __P((void*, int, int, int));
    198 int  pciide_dma_finish __P((void*, int, int, int));
    199 void pciide_print_modes __P((struct pciide_channel *));
    200 
    201 struct pciide_product_desc {
    202     u_int32_t ide_product;
    203     int ide_flags;
    204     int ide_num_channels;
    205     const char *ide_name;
    206     /* init controller's capabilities for drives probe */
    207     void (*setup_cap) __P((struct pciide_softc*));
    208     /* init controller after drives probe */
    209     void (*setup_chip) __P((struct pciide_softc*));
    210     /* map channel if possible/necessary */
    211     void (*channel_map) __P((struct pci_attach_args *,
    212 		struct pciide_channel *));
    213 };
    214 
    215 /* Flags for ide_flags */
    216 #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
    217 #define ONE_QUEUE         0x02 /* device need serialised access */
    218 
    219 /* Default product description for devices not known from this controller */
    220 const struct pciide_product_desc default_product_desc = {
    221     0,
    222     0,
    223     PCIIDE_NUM_CHANNELS,
    224     "Generic PCI IDE controller",
    225     default_setup_cap,
    226     default_setup_chip,
    227     default_channel_map
    228 };
    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 const struct pciide_product_desc pciide_cmd_products[] =  {
    271     { PCI_PRODUCT_CMDTECH_640,
    272       ONE_QUEUE | CMD_PCI064x_IOEN,
    273       PCIIDE_NUM_CHANNELS,
    274       "CMD Technology PCI0640",
    275       default_setup_cap,
    276       default_setup_chip,
    277       cmd_channel_map
    278     },
    279     { PCI_PRODUCT_CMDTECH_643,
    280       ONE_QUEUE | CMD_PCI064x_IOEN,
    281       PCIIDE_NUM_CHANNELS,
    282       "CMD Technology PCI0643",
    283       cmd0643_6_setup_cap,
    284       cmd0643_6_setup_chip,
    285       cmd_channel_map
    286     },
    287     { PCI_PRODUCT_CMDTECH_646,
    288       ONE_QUEUE | CMD_PCI064x_IOEN,
    289       PCIIDE_NUM_CHANNELS,
    290       "CMD Technology PCI0646",
    291       cmd0643_6_setup_cap,
    292       cmd0643_6_setup_chip,
    293       cmd_channel_map
    294     },
    295     { 0,
    296       0,
    297       0,
    298       NULL,
    299     }
    300 };
    301 
    302 const struct pciide_product_desc pciide_via_products[] =  {
    303     { PCI_PRODUCT_VIATECH_VT82C586_IDE,
    304       0,
    305       PCIIDE_NUM_CHANNELS,
    306       "VIA Technologies VT82C586 (Apollo VP) IDE Controller",
    307       apollo_setup_cap,
    308       apollo_setup_chip,
    309       apollo_channel_map
    310      },
    311     { PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    312       0,
    313       PCIIDE_NUM_CHANNELS,
    314       "VIA Technologies VT82C586A IDE Controller",
    315       apollo_setup_cap,
    316       apollo_setup_chip,
    317       apollo_channel_map
    318     },
    319     { 0,
    320       0,
    321       0,
    322       NULL,
    323     }
    324 };
    325 
    326 const struct pciide_product_desc pciide_cypress_products[] =  {
    327     { PCI_PRODUCT_CONTAQ_82C693,
    328       0,
    329       1,
    330       "Contaq Microsystems CY82C693 IDE Controller",
    331       cy693_setup_cap,
    332       cy693_setup_chip,
    333       cy693_channel_map
    334     },
    335     { 0,
    336       0,
    337       0,
    338       NULL,
    339     }
    340 };
    341 
    342 const struct pciide_product_desc pciide_sis_products[] =  {
    343     { PCI_PRODUCT_SIS_5597_IDE,
    344       0,
    345       PCIIDE_NUM_CHANNELS,
    346       "Silicon Integrated System 5597/5598 IDE controller",
    347       sis_setup_cap,
    348       sis_setup_chip,
    349       sis_channel_map
    350     },
    351     { 0,
    352       0,
    353       0,
    354       NULL,
    355     }
    356 };
    357 
    358 const struct pciide_product_desc pciide_acer_products[] =  {
    359     { PCI_PRODUCT_ALI_M5229,
    360       0,
    361       PCIIDE_NUM_CHANNELS,
    362       "Acer Labs M5229 UDMA IDE Controller",
    363       acer_setup_cap,
    364       acer_setup_chip,
    365       acer_channel_map
    366     },
    367     { 0,
    368       0,
    369       0,
    370       NULL,
    371     }
    372 };
    373 
    374 struct pciide_vendor_desc {
    375     u_int32_t ide_vendor;
    376     const struct pciide_product_desc *ide_products;
    377 };
    378 
    379 const struct pciide_vendor_desc pciide_vendors[] = {
    380     { PCI_VENDOR_INTEL, pciide_intel_products },
    381     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    382     { PCI_VENDOR_VIATECH, pciide_via_products },
    383     { PCI_VENDOR_CONTAQ, pciide_cypress_products },
    384     { PCI_VENDOR_SIS, pciide_sis_products },
    385     { PCI_VENDOR_ALI, pciide_acer_products },
    386     { 0, NULL }
    387 };
    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 functoin 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 		goto ok;
   1287 	}
   1288 	/*
   1289 	 * If only one drive supports DMA, use its mode, and
   1290 	 * put the other one in PIO mode 0 if mode not compatible
   1291 	 */
   1292 	if (drvp[0].drive_flags & DRIVE_DMA) {
   1293 		mode[0] = drvp[0].DMA_mode;
   1294 		mode[1] = drvp[1].PIO_mode;
   1295 		if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
   1296 		    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
   1297 			mode[1] = 0;
   1298 		goto ok;
   1299 	}
   1300 	if (drvp[1].drive_flags & DRIVE_DMA) {
   1301 		mode[1] = drvp[1].DMA_mode;
   1302 		mode[0] = drvp[0].PIO_mode;
   1303 		if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
   1304 		    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
   1305 			mode[0] = 0;
   1306 		goto ok;
   1307 	}
   1308 	/*
   1309 	 * If both drives are not DMA, takes the lower mode, unless
   1310 	 * one of them is PIO mode < 2
   1311 	 */
   1312 	if (drvp[0].PIO_mode < 2) {
   1313 		mode[0] = 0;
   1314 		mode[1] = drvp[1].PIO_mode;
   1315 	} else if (drvp[1].PIO_mode < 2) {
   1316 		mode[1] = 0;
   1317 		mode[0] = drvp[0].PIO_mode;
   1318 	} else {
   1319 		mode[0] = mode[1] =
   1320 		    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
   1321 	}
   1322 ok:	/* The modes are setup */
   1323 	for (drive = 0; drive < 2; drive++) {
   1324 		if (drvp[drive].drive_flags & DRIVE_DMA) {
   1325 			drvp[drive].DMA_mode = mode[drive];
   1326 			idetim |= piix_setup_idetim_timings(
   1327 			    mode[drive], 1, chp->channel);
   1328 			goto end;
   1329 		} else
   1330 			drvp[drive].PIO_mode = mode[drive];
   1331 	}
   1332 	/* If we are there, none of the drives are DMA */
   1333 	if (mode[0] >= 2)
   1334 		idetim |= piix_setup_idetim_timings(
   1335 		    mode[0], 0, chp->channel);
   1336 	else
   1337 		idetim |= piix_setup_idetim_timings(
   1338 		    mode[1], 0, chp->channel);
   1339 end:	/*
   1340 	 * timing mode is now set up in the controller. Enable
   1341 	 * it per-drive
   1342 	 */
   1343 	for (drive = 0; drive < 2; drive++) {
   1344 		/* If no drive, skip */
   1345 		if ((drvp[drive].drive_flags & DRIVE) == 0)
   1346 			continue;
   1347 		idetim |= piix_setup_idetim_drvs(&drvp[drive]);
   1348 		if (drvp[drive].drive_flags & DRIVE_DMA)
   1349 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1350 	}
   1351 	if (idedma_ctl != 0) {
   1352 		/* Add software bits in status register */
   1353 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1354 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1355 		    idedma_ctl);
   1356 	}
   1357 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1358 	pciide_print_modes(cp);
   1359 }
   1360 
   1361 void
   1362 piix3_4_setup_chip(sc)
   1363 	struct pciide_softc *sc;
   1364 {
   1365 	int channel;
   1366 
   1367 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
   1368 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1369 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1370 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1371 		WDCDEBUG_PRINT((", udamreg 0x%x",
   1372 		    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1373 		    DEBUG_PROBE);
   1374 	}
   1375 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1376 
   1377 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1378 		piix3_4_setup_channel(
   1379 		    &sc->pciide_channels[channel].wdc_channel);
   1380 	}
   1381 
   1382 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
   1383 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1384 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1385 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1386 		WDCDEBUG_PRINT((", udmareg=0x%x",
   1387 		pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1388 		DEBUG_PROBE);
   1389 	}
   1390 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1391 }
   1392 
   1393 void
   1394 piix3_4_setup_channel(chp)
   1395 	struct channel_softc *chp;
   1396 {
   1397 	struct ata_drive_datas *drvp;
   1398 	u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
   1399 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1400 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1401 	int drive;
   1402 
   1403 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1404 	sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
   1405 	udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
   1406 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1407 	sidetim &= ~(PIIX_SIDETIM_ISP_MASK(chp->channel) |
   1408 	    PIIX_SIDETIM_RTC_MASK(chp->channel));
   1409 
   1410 	idedma_ctl = 0;
   1411 	/* If channel disabled, no need to go further */
   1412 	if ((PIIX_IDETIM_READ(oidetim, chp->channel) & PIIX_IDETIM_IDE) == 0)
   1413 		return;
   1414 	/* set up new idetim: Enable IDE registers decode */
   1415 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, chp->channel);
   1416 
   1417 	/* setup DMA if needed */
   1418 	pciide_channel_dma_setup(cp);
   1419 
   1420 	for (drive = 0; drive < 2; drive++) {
   1421 		udmareg &= ~(PIIX_UDMACTL_DRV_EN(chp->channel, drive) |
   1422 		    PIIX_UDMATIM_SET(0x3, chp->channel, drive));
   1423 		drvp = &chp->ch_drive[drive];
   1424 		/* If no drive, skip */
   1425 		if ((drvp->drive_flags & DRIVE) == 0)
   1426 			continue;
   1427 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1428 		    (drvp->drive_flags & DRIVE_UDMA) == 0))
   1429 			goto pio;
   1430 
   1431 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1432 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1433 			/* use Ultra/DMA */
   1434 			drvp->drive_flags &= ~DRIVE_DMA;
   1435 			udmareg |= PIIX_UDMACTL_DRV_EN(
   1436 			    chp->channel, drive);
   1437 			udmareg |= PIIX_UDMATIM_SET(
   1438 			    piix4_sct_udma[drvp->UDMA_mode],
   1439 			    chp->channel, drive);
   1440 		} else {
   1441 			/* use Multiword DMA */
   1442 			drvp->drive_flags &= ~DRIVE_UDMA;
   1443 			if (drive == 0) {
   1444 				idetim |= piix_setup_idetim_timings(
   1445 				    drvp->DMA_mode, 1, chp->channel);
   1446 			} else {
   1447 				sidetim |= piix_setup_sidetim_timings(
   1448 					drvp->DMA_mode, 1, chp->channel);
   1449 				idetim =PIIX_IDETIM_SET(idetim,
   1450 				    PIIX_IDETIM_SITRE, chp->channel);
   1451 			}
   1452 		}
   1453 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1454 
   1455 pio:		/* use PIO mode */
   1456 		idetim |= piix_setup_idetim_drvs(drvp);
   1457 		if (drive == 0) {
   1458 			idetim |= piix_setup_idetim_timings(
   1459 			    drvp->PIO_mode, 0, chp->channel);
   1460 		} else {
   1461 			sidetim |= piix_setup_sidetim_timings(
   1462 				drvp->PIO_mode, 0, chp->channel);
   1463 			idetim =PIIX_IDETIM_SET(idetim,
   1464 			    PIIX_IDETIM_SITRE, chp->channel);
   1465 		}
   1466 	}
   1467 	if (idedma_ctl != 0) {
   1468 		/* Add software bits in status register */
   1469 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1470 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1471 		    idedma_ctl);
   1472 	}
   1473 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1474 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
   1475 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
   1476 	pciide_print_modes(cp);
   1477 }
   1478 
   1479 
   1480 /* setup ISP and RTC fields, based on mode */
   1481 static u_int32_t
   1482 piix_setup_idetim_timings(mode, dma, channel)
   1483 	u_int8_t mode;
   1484 	u_int8_t dma;
   1485 	u_int8_t channel;
   1486 {
   1487 
   1488 	if (dma)
   1489 		return PIIX_IDETIM_SET(0,
   1490 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1491 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1492 		    channel);
   1493 	else
   1494 		return PIIX_IDETIM_SET(0,
   1495 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1496 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1497 		    channel);
   1498 }
   1499 
   1500 /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1501 static u_int32_t
   1502 piix_setup_idetim_drvs(drvp)
   1503 	struct ata_drive_datas *drvp;
   1504 {
   1505 	u_int32_t ret = 0;
   1506 	struct channel_softc *chp = drvp->chnl_softc;
   1507 	u_int8_t channel = chp->channel;
   1508 	u_int8_t drive = drvp->drive;
   1509 
   1510 	/*
   1511 	 * If drive is using UDMA, timings setups are independant
   1512 	 * So just check DMA and PIO here.
   1513 	 */
   1514 	if (drvp->drive_flags & DRIVE_DMA) {
   1515 		/* if mode = DMA mode 0, use compatible timings */
   1516 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1517 		    drvp->DMA_mode == 0) {
   1518 			drvp->PIO_mode = 0;
   1519 			return ret;
   1520 		}
   1521 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1522 		/*
   1523 		 * PIO and DMA timings are the same, use fast timings for PIO
   1524 		 * too, else use compat timings.
   1525 		 */
   1526 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1527 		    piix_isp_dma[drvp->DMA_mode]) ||
   1528 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1529 		    piix_rtc_dma[drvp->DMA_mode]))
   1530 			drvp->PIO_mode = 0;
   1531 		/* if PIO mode <= 2, use compat timings for PIO */
   1532 		if (drvp->PIO_mode <= 2) {
   1533 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1534 			    channel);
   1535 			return ret;
   1536 		}
   1537 	}
   1538 
   1539 	/*
   1540 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1541 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1542 	 * if PIO mode >= 3.
   1543 	 */
   1544 
   1545 	if (drvp->PIO_mode < 2)
   1546 		return ret;
   1547 
   1548 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1549 	if (drvp->PIO_mode >= 3) {
   1550 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1551 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1552 	}
   1553 	return ret;
   1554 }
   1555 
   1556 /* setup values in SIDETIM registers, based on mode */
   1557 static u_int32_t
   1558 piix_setup_sidetim_timings(mode, dma, channel)
   1559 	u_int8_t mode;
   1560 	u_int8_t dma;
   1561 	u_int8_t channel;
   1562 {
   1563 	if (dma)
   1564 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1565 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1566 	else
   1567 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1568 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1569 }
   1570 
   1571 void
   1572 piix_channel_map(pa, cp)
   1573 	struct pci_attach_args *pa;
   1574 	struct pciide_channel *cp;
   1575 {
   1576 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1577 	bus_size_t cmdsize, ctlsize;
   1578 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1579 	u_int32_t idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1580 
   1581 	if ((PIIX_IDETIM_READ(idetim, wdc_cp->channel) &
   1582 	    PIIX_IDETIM_IDE) == 0) {
   1583 		printf("%s: %s channel ignored (disabled)\n",
   1584 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1585 		return;
   1586 	}
   1587 
   1588 	/* PIIX are compat-only pciide devices */
   1589 	pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize);
   1590 	if (cp->hw_ok == 0)
   1591 		return;
   1592 	if (pciiide_chan_candisable(cp)) {
   1593 		idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
   1594 					   wdc_cp->channel);
   1595 		pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1596 	}
   1597 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, 0);
   1598 }
   1599 
   1600 void
   1601 apollo_setup_cap(sc)
   1602 	struct pciide_softc *sc;
   1603 {
   1604 	if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
   1605 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1606 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1607 	    WDC_CAPABILITY_DMA;
   1608 	sc->sc_wdcdev.PIO_cap = 4;
   1609 	sc->sc_wdcdev.DMA_cap = 2;
   1610 	sc->sc_wdcdev.UDMA_cap = 2;
   1611 	sc->sc_wdcdev.set_modes = apollo_setup_channel;
   1612 
   1613 }
   1614 
   1615 void
   1616 apollo_setup_chip(sc)
   1617 	struct pciide_softc *sc;
   1618 {
   1619 	int channel;
   1620 
   1621 	WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
   1622 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1623 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
   1624 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
   1625 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1626 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
   1627 	    DEBUG_PROBE);
   1628 
   1629 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1630 		apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1631 	}
   1632 	WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1633 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1634 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
   1635 }
   1636 
   1637 void
   1638 apollo_setup_channel(chp)
   1639 	struct channel_softc *chp;
   1640 {
   1641 	u_int32_t udmatim_reg, datatim_reg;
   1642 	u_int8_t idedma_ctl;
   1643 	int mode, drive;
   1644 	struct ata_drive_datas *drvp;
   1645 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1646 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1647 
   1648 	idedma_ctl = 0;
   1649 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
   1650 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
   1651 	datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
   1652 	udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
   1653 
   1654 	/* setup DMA if needed */
   1655 	pciide_channel_dma_setup(cp);
   1656 
   1657 	for (drive = 0; drive < 2; drive++) {
   1658 		drvp = &chp->ch_drive[drive];
   1659 		/* If no drive, skip */
   1660 		if ((drvp->drive_flags & DRIVE) == 0)
   1661 			continue;
   1662 		/* add timing values, setup DMA if needed */
   1663 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1664 		    (drvp->drive_flags & DRIVE_UDMA) == 0)) {
   1665 			mode = drvp->PIO_mode;
   1666 			goto pio;
   1667 		}
   1668 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1669 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1670 			/* use Ultra/DMA */
   1671 			drvp->drive_flags &= ~DRIVE_DMA;
   1672 			udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
   1673 			    APO_UDMA_EN_MTH(chp->channel, drive) |
   1674 			    APO_UDMA_TIME(chp->channel, drive,
   1675 				apollo_udma_tim[drvp->UDMA_mode]);
   1676 			/* can use PIO timings, MW DMA unused */
   1677 			mode = drvp->PIO_mode;
   1678 		} else {
   1679 			/* use Multiword DMA */
   1680 			drvp->drive_flags &= ~DRIVE_UDMA;
   1681 			/* mode = min(pio, dma+2) */
   1682 			if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1683 				mode = drvp->PIO_mode;
   1684 			else
   1685 				mode = drvp->DMA_mode;
   1686 		}
   1687 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1688 
   1689 pio:		/* setup PIO mode */
   1690 		datatim_reg |=
   1691 		    APO_DATATIM_PULSE(chp->channel, drive,
   1692 			apollo_pio_set[mode]) |
   1693 		    APO_DATATIM_RECOV(chp->channel, drive,
   1694 			apollo_pio_rec[mode]);
   1695 		drvp->PIO_mode = mode;
   1696 		drvp->DMA_mode = mode - 2;
   1697 	}
   1698 	if (idedma_ctl != 0) {
   1699 		/* Add software bits in status register */
   1700 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1701 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1702 		    idedma_ctl);
   1703 	}
   1704 	pciide_print_modes(cp);
   1705 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
   1706 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
   1707 }
   1708 
   1709 void
   1710 apollo_channel_map(pa, cp)
   1711 	struct pci_attach_args *pa;
   1712 	struct pciide_channel *cp;
   1713 {
   1714 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1715 	bus_size_t cmdsize, ctlsize;
   1716 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1717 	u_int32_t ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
   1718 	int interface =
   1719 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1720 
   1721 	if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
   1722 		printf("%s: %s channel ignored (disabled)\n",
   1723 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1724 		return;
   1725 	}
   1726 
   1727 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1728 	if (cp->hw_ok == 0)
   1729 		return;
   1730 	if (pciiide_chan_candisable(cp)) {
   1731 		ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
   1732 		pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF, ideconf);
   1733 	}
   1734 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1735 }
   1736 
   1737 void
   1738 cmd_channel_map(pa, cp)
   1739 	struct pci_attach_args *pa;
   1740 	struct pciide_channel *cp;
   1741 {
   1742 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1743 	bus_size_t cmdsize, ctlsize;
   1744 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1745 	u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
   1746 	int interface =
   1747 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1748 
   1749 	/*
   1750 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   1751 	 * there's no way to disable the first channel without disabling
   1752 	 * the whole device
   1753 	 */
   1754 	if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
   1755 		printf("%s: %s channel ignored (disabled)\n",
   1756 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1757 		return;
   1758 	}
   1759 
   1760 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1761 	if (cp->hw_ok == 0)
   1762 		return;
   1763 	if (wdc_cp->channel == 1) {
   1764 		if (pciiide_chan_candisable(cp)) {
   1765 			ctrl &= ~CMD_CTRL_2PORT;
   1766 			pciide_pci_write(pa->pa_pc, pa->pa_tag,
   1767 			    CMD_CTRL, ctrl);
   1768 		}
   1769 	}
   1770 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1771 }
   1772 
   1773 void
   1774 cmd0643_6_setup_cap(sc)
   1775 	struct pciide_softc *sc;
   1776 {
   1777 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1778 	    WDC_CAPABILITY_DMA;
   1779 	sc->sc_wdcdev.PIO_cap = 4;
   1780 	sc->sc_wdcdev.DMA_cap = 2;
   1781 	sc->sc_wdcdev.set_modes = cmd0643_6_setup_channel;
   1782 }
   1783 
   1784 void
   1785 cmd0643_6_setup_chip(sc)
   1786 	struct pciide_softc *sc;
   1787 {
   1788 	int channel;
   1789 
   1790 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
   1791 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1792 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1793 		DEBUG_PROBE);
   1794 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1795 		cmd0643_6_setup_channel(
   1796 		    &sc->pciide_channels[channel].wdc_channel);
   1797 	}
   1798 	/* configure for DMA read multiple */
   1799 	pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
   1800 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
   1801 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1802 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1803 	    DEBUG_PROBE);
   1804 }
   1805 
   1806 void
   1807 cmd0643_6_setup_channel(chp)
   1808 	struct channel_softc *chp;
   1809 {
   1810 	struct ata_drive_datas *drvp;
   1811 	u_int8_t tim;
   1812 	u_int32_t idedma_ctl;
   1813 	int drive;
   1814 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1815 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1816 
   1817 	idedma_ctl = 0;
   1818 	/* setup DMA if needed */
   1819 	pciide_channel_dma_setup(cp);
   1820 
   1821 	for (drive = 0; drive < 2; drive++) {
   1822 		drvp = &chp->ch_drive[drive];
   1823 		/* If no drive, skip */
   1824 		if ((drvp->drive_flags & DRIVE) == 0)
   1825 			continue;
   1826 		/* add timing values, setup DMA if needed */
   1827 		tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
   1828 		if (drvp->drive_flags & DRIVE_DMA) {
   1829 			/*
   1830 			 * use Multiword DMA.
   1831 			 * Timings will be used for both PIO and DMA, so adjust
   1832 			 * DMA mode if needed
   1833 			 */
   1834 			if (drvp->PIO_mode >= 3 &&
   1835 			    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
   1836 				drvp->DMA_mode = drvp->PIO_mode - 2;
   1837 			}
   1838 			tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
   1839 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1840 		}
   1841 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   1842 		    CMD_DATA_TIM(chp->channel, drive), tim);
   1843 	}
   1844 	if (idedma_ctl != 0) {
   1845 		/* Add software bits in status register */
   1846 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1847 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1848 		    idedma_ctl);
   1849 	}
   1850 	pciide_print_modes(cp);
   1851 }
   1852 
   1853 void
   1854 cy693_setup_cap(sc)
   1855 	struct pciide_softc *sc;
   1856 {
   1857 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1858 	    WDC_CAPABILITY_DMA;
   1859 	sc->sc_wdcdev.PIO_cap = 4;
   1860 	sc->sc_wdcdev.DMA_cap = 2;
   1861 	sc->sc_wdcdev.set_modes = cy693_setup_channel;
   1862 }
   1863 
   1864 void
   1865 cy693_setup_chip(sc)
   1866 	struct pciide_softc *sc;
   1867 {
   1868 	WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
   1869 		pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),
   1870 		DEBUG_PROBE);
   1871 	cy693_setup_channel(&sc->pciide_channels[0].wdc_channel);
   1872 	WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
   1873 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
   1874 }
   1875 
   1876 void
   1877 cy693_setup_channel(chp)
   1878 	struct channel_softc *chp;
   1879 {
   1880 	struct ata_drive_datas *drvp;
   1881 	int drive;
   1882 	u_int32_t cy_cmd_ctrl;
   1883 	u_int32_t idedma_ctl;
   1884 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1885 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1886 
   1887 	cy_cmd_ctrl = idedma_ctl = 0;
   1888 
   1889 	/* setup DMA if needed */
   1890 	pciide_channel_dma_setup(cp);
   1891 
   1892 	for (drive = 0; drive < 2; drive++) {
   1893 		drvp = &chp->ch_drive[drive];
   1894 		/* If no drive, skip */
   1895 		if ((drvp->drive_flags & DRIVE) == 0)
   1896 			continue;
   1897 		/* add timing values, setup DMA if needed */
   1898 		if (drvp->drive_flags & DRIVE_DMA) {
   1899 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1900 			/*
   1901 			 * use Multiword DMA
   1902 			 * Timings will be used for both PIO and DMA, so adjust
   1903 			 * DMA mode if needed
   1904 			 */
   1905 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   1906 				drvp->PIO_mode = drvp->DMA_mode + 2;
   1907 			if (drvp->DMA_mode == 0)
   1908 				drvp->PIO_mode = 0;
   1909 		}
   1910 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1911 		    CY_CMD_CTRL_IOW_PULSE_OFF(drive));
   1912 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1913 		    CY_CMD_CTRL_IOW_REC_OFF(drive));
   1914 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1915 		    CY_CMD_CTRL_IOR_PULSE_OFF(drive));
   1916 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1917 		    CY_CMD_CTRL_IOR_REC_OFF(drive));
   1918 	}
   1919 	pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
   1920 	pciide_print_modes(cp);
   1921 	if (idedma_ctl != 0) {
   1922 		/* Add software bits in status register */
   1923 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1924 		    IDEDMA_CTL, idedma_ctl);
   1925 	}
   1926 }
   1927 
   1928 void
   1929 cy693_channel_map(pa, cp)
   1930 	struct pci_attach_args *pa;
   1931 	struct pciide_channel *cp;
   1932 {
   1933 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1934 	bus_size_t cmdsize, ctlsize;
   1935 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1936 	int interface =
   1937 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1938 	int compatchan;
   1939 
   1940 #ifdef DIAGNOSTIC
   1941 	if (wdc_cp->channel != 0)
   1942 		panic("cy693_channel_map: channel %d", wdc_cp->channel);
   1943 #endif
   1944 
   1945 	/*
   1946 	 * this chip has 2 PCI IDE functions, one for primary and one for
   1947 	 * secondary. So we need to call pciide_mapregs_compat() with
   1948 	 * the real channel
   1949 	 */
   1950 	if (pa->pa_function == 1) {
   1951 		compatchan = 0;
   1952 	} else if (pa->pa_function == 2) {
   1953 		compatchan = 1;
   1954 	} else {
   1955 		printf("%s: unexpected PCI function %d\n",
   1956 		    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
   1957 		cp->hw_ok = 0;
   1958 		return;
   1959 	}
   1960 
   1961 	/* Only one channel for this chip; if we are here it's enabled */
   1962 	if (interface & PCIIDE_INTERFACE_PCI(0))
   1963 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
   1964 	else
   1965 		cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
   1966 		    &cmdsize, &ctlsize);
   1967 	if (cp->hw_ok == 0)
   1968 		return;
   1969 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1970 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1971 	wdcattach(wdc_cp);
   1972 	if (pciiide_chan_candisable(cp)) {
   1973 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   1974 		    PCI_COMMAND_STATUS_REG, 0);
   1975 	}
   1976 	pciide_map_compat_intr(pa, cp, compatchan, interface);
   1977 }
   1978 
   1979 void
   1980 sis_setup_cap(sc)
   1981 	struct pciide_softc *sc;
   1982 {
   1983 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1984 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   1985 	sc->sc_wdcdev.PIO_cap = 4;
   1986 	sc->sc_wdcdev.DMA_cap = 2;
   1987 	sc->sc_wdcdev.UDMA_cap = 2;
   1988 	sc->sc_wdcdev.set_modes = sis_setup_channel;
   1989 }
   1990 
   1991 void
   1992 sis_setup_chip(sc)
   1993 	struct pciide_softc *sc;
   1994 {
   1995 	int channel;
   1996 
   1997 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1998 		sis_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1999 	}
   2000 	pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
   2001 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
   2002 	    SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
   2003 }
   2004 
   2005 void
   2006 sis_setup_channel(chp)
   2007 	struct channel_softc *chp;
   2008 {
   2009 	struct ata_drive_datas *drvp;
   2010 	int drive;
   2011 	u_int32_t sis_tim;
   2012 	u_int32_t idedma_ctl;
   2013 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2014 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2015 
   2016 	WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
   2017 	    "channel %d 0x%x\n", chp->channel,
   2018 	    pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
   2019 	    DEBUG_PROBE);
   2020 	sis_tim = 0;
   2021 	idedma_ctl = 0;
   2022 	/* setup DMA if needed */
   2023 	pciide_channel_dma_setup(cp);
   2024 
   2025 	for (drive = 0; drive < 2; drive++) {
   2026 		drvp = &chp->ch_drive[drive];
   2027 		/* If no drive, skip */
   2028 		if ((drvp->drive_flags & DRIVE) == 0)
   2029 			continue;
   2030 		/* add timing values, setup DMA if needed */
   2031 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2032 		    (drvp->drive_flags & DRIVE_UDMA) == 0)
   2033 			goto pio;
   2034 
   2035 		if (drvp->drive_flags & DRIVE_UDMA) {
   2036 			/* use Ultra/DMA */
   2037 			drvp->drive_flags &= ~DRIVE_DMA;
   2038 			sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
   2039 			    SIS_TIM_UDMA_TIME_OFF(drive);
   2040 			sis_tim |= SIS_TIM_UDMA_EN(drive);
   2041 		} else {
   2042 			/*
   2043 			 * use Multiword DMA
   2044 			 * Timings will be used for both PIO and DMA,
   2045 			 * so adjust DMA mode if needed
   2046 			 */
   2047 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2048 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2049 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2050 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2051 				    drvp->PIO_mode - 2 : 0;
   2052 			if (drvp->DMA_mode == 0)
   2053 				drvp->PIO_mode = 0;
   2054 		}
   2055 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2056 pio:		sis_tim |= sis_pio_act[drvp->PIO_mode] <<
   2057 		    SIS_TIM_ACT_OFF(drive);
   2058 		sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
   2059 		    SIS_TIM_REC_OFF(drive);
   2060 	}
   2061 	WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
   2062 	    "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
   2063 	pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
   2064 	if (idedma_ctl != 0) {
   2065 		/* Add software bits in status register */
   2066 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2067 		    IDEDMA_CTL, idedma_ctl);
   2068 	}
   2069 	pciide_print_modes(cp);
   2070 }
   2071 
   2072 void
   2073 sis_channel_map(pa, cp)
   2074 	struct pci_attach_args *pa;
   2075 	struct pciide_channel *cp;
   2076 {
   2077 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2078 	bus_size_t cmdsize, ctlsize;
   2079 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2080 	u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
   2081 	int interface =
   2082 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   2083 
   2084 	if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
   2085 	    (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
   2086 		printf("%s: %s channel ignored (disabled)\n",
   2087 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2088 		return;
   2089 	}
   2090 
   2091 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2092 	if (cp->hw_ok == 0)
   2093 		return;
   2094 	if (pciiide_chan_candisable(cp)) {
   2095 		if (wdc_cp->channel == 0)
   2096 			sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
   2097 		else
   2098 			sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
   2099 		pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0, sis_ctr0);
   2100 	}
   2101 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2102 }
   2103 
   2104 void
   2105 acer_setup_cap(sc)
   2106 	struct pciide_softc *sc;
   2107 {
   2108 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   2109 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   2110 	sc->sc_wdcdev.PIO_cap = 4;
   2111 	sc->sc_wdcdev.DMA_cap = 2;
   2112 	sc->sc_wdcdev.UDMA_cap = 2;
   2113 	sc->sc_wdcdev.set_modes = acer_setup_channel;
   2114 }
   2115 
   2116 void
   2117 acer_setup_chip(sc)
   2118 	struct pciide_softc *sc;
   2119 {
   2120 	int channel;
   2121 
   2122 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
   2123 	    (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
   2124 		ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
   2125 
   2126 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2127 		acer_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   2128 	}
   2129 }
   2130 
   2131 void
   2132 acer_setup_channel(chp)
   2133 	struct channel_softc *chp;
   2134 {
   2135 	struct ata_drive_datas *drvp;
   2136 	int drive;
   2137 	u_int32_t acer_fifo_udma;
   2138 	u_int32_t idedma_ctl;
   2139 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2140 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2141 
   2142 	idedma_ctl = 0;
   2143 	acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
   2144 	WDCDEBUG_PRINT(("acer_setup_chip: old fifo/udma reg 0x%x\n",
   2145 	    acer_fifo_udma), DEBUG_PROBE);
   2146 	/* setup DMA if needed */
   2147 	pciide_channel_dma_setup(cp);
   2148 
   2149 	for (drive = 0; drive < 2; drive++) {
   2150 		drvp = &chp->ch_drive[drive];
   2151 		/* If no drive, skip */
   2152 		if ((drvp->drive_flags & DRIVE) == 0)
   2153 			continue;
   2154 		WDCDEBUG_PRINT(("acer_setup_chip: old timings reg for "
   2155 		    "channel %d drive %d 0x%x\n", chp->channel, drive,
   2156 		    pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2157 		    ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
   2158 		/* clear FIFO/DMA mode */
   2159 		acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
   2160 		    ACER_UDMA_EN(chp->channel, drive) |
   2161 		    ACER_UDMA_TIM(chp->channel, drive, 0x7));
   2162 
   2163 		/* add timing values, setup DMA if needed */
   2164 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2165 		    (drvp->drive_flags & DRIVE_UDMA) == 0) {
   2166 			acer_fifo_udma |=
   2167 			    ACER_FTH_OPL(chp->channel, drive, 0x1);
   2168 			goto pio;
   2169 		}
   2170 
   2171 		acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
   2172 		if (drvp->drive_flags & DRIVE_UDMA) {
   2173 			/* use Ultra/DMA */
   2174 			drvp->drive_flags &= ~DRIVE_DMA;
   2175 			acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
   2176 			acer_fifo_udma |=
   2177 			    ACER_UDMA_TIM(chp->channel, drive,
   2178 				acer_udma[drvp->UDMA_mode]);
   2179 		} else {
   2180 			/*
   2181 			 * use Multiword DMA
   2182 			 * Timings will be used for both PIO and DMA,
   2183 			 * so adjust DMA mode if needed
   2184 			 */
   2185 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2186 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2187 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2188 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2189 				    drvp->PIO_mode - 2 : 0;
   2190 			if (drvp->DMA_mode == 0)
   2191 				drvp->PIO_mode = 0;
   2192 		}
   2193 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2194 pio:		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2195 		    ACER_IDETIM(chp->channel, drive),
   2196 		    acer_pio[drvp->PIO_mode]);
   2197 	}
   2198 	WDCDEBUG_PRINT(("acer_setup_chip: new fifo/udma reg 0x%x\n",
   2199 	    acer_fifo_udma), DEBUG_PROBE);
   2200 	pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
   2201 	if (idedma_ctl != 0) {
   2202 		/* Add software bits in status register */
   2203 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2204 		    IDEDMA_CTL, idedma_ctl);
   2205 	}
   2206 	pciide_print_modes(cp);
   2207 }
   2208 
   2209 void
   2210 acer_channel_map(pa, cp)
   2211 	struct pci_attach_args *pa;
   2212 	struct pciide_channel *cp;
   2213 {
   2214 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2215 	bus_size_t cmdsize, ctlsize;
   2216 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2217 	u_int32_t cr;
   2218 	int interface;
   2219 
   2220 	/*
   2221 	 * Enable "microsoft register bits" R/W. Will be done 2 times
   2222 	 * (one for each channel) but should'nt be a problem. There's no
   2223 	 * better place where to put this.
   2224 	 */
   2225 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
   2226 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
   2227 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
   2228 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
   2229 	    ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
   2230 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
   2231 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
   2232 	    ~ACER_CHANSTATUSREGS_RO);
   2233 	cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
   2234 	cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
   2235 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2236 	/* Don't use cr, re-read the real register content instead */
   2237 	interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
   2238 	    PCI_CLASS_REG));
   2239 
   2240 	if ((interface & PCIIDE_CHAN_EN(wdc_cp->channel)) == 0) {
   2241 		printf("%s: %s channel ignored (disabled)\n",
   2242 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2243 		return;
   2244 	}
   2245 
   2246 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2247 	if (cp->hw_ok == 0)
   2248 		return;
   2249 	if (pciiide_chan_candisable(cp)) {
   2250 		cr &= ~(PCIIDE_CHAN_EN(wdc_cp->channel) << PCI_INTERFACE_SHIFT);
   2251 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2252 	}
   2253 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2254 }
   2255