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