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