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