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