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