Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.6.2.3
      1  1.6.2.3  bouyer /*	$NetBSD: pciide.c,v 1.6.2.3 1998/06/05 17:31:37 bouyer Exp $	*/
      2      1.1     cgd 
      3      1.1     cgd /*
      4      1.1     cgd  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5      1.1     cgd  *
      6      1.1     cgd  * Redistribution and use in source and binary forms, with or without
      7      1.1     cgd  * modification, are permitted provided that the following conditions
      8      1.1     cgd  * are met:
      9      1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     10      1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     11      1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     13      1.1     cgd  *    documentation and/or other materials provided with the distribution.
     14      1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     15      1.1     cgd  *    must display the following acknowledgement:
     16      1.1     cgd  *      This product includes software developed by Christopher G. Demetriou
     17      1.1     cgd  *	for the NetBSD Project.
     18      1.1     cgd  * 4. The name of the author may not be used to endorse or promote products
     19      1.1     cgd  *    derived from this software without specific prior written permission
     20      1.1     cgd  *
     21      1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1     cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1     cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1     cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1     cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1     cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1     cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1     cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1     cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1     cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1     cgd  */
     32      1.1     cgd 
     33      1.1     cgd /*
     34      1.1     cgd  * PCI IDE controller driver.
     35      1.1     cgd  *
     36      1.1     cgd  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     37      1.1     cgd  * sys/dev/pci/ppb.c, revision 1.16).
     38      1.1     cgd  *
     39      1.2     cgd  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     40      1.2     cgd  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     41      1.2     cgd  * 5/16/94" from the PCI SIG.
     42      1.1     cgd  *
     43      1.2     cgd  * XXX Does not yet support DMA (but does map the Bus Master DMA regs).
     44      1.1     cgd  *
     45      1.1     cgd  * XXX Does not support serializing the two channels for broken (at least
     46      1.1     cgd  * XXX according to linux and freebsd) controllers, e.g. CMD PCI0640.
     47      1.1     cgd  */
     48      1.1     cgd 
     49  1.6.2.1  bouyer #define WDCDEBUG
     50  1.6.2.1  bouyer 
     51  1.6.2.1  bouyer #define DEBUG_DMA   0x01
     52  1.6.2.1  bouyer #define DEBUG_XFERS  0x02
     53  1.6.2.1  bouyer #define DEBUG_FUNCS  0x08
     54  1.6.2.1  bouyer #define DEBUG_PROBE  0x10
     55  1.6.2.1  bouyer #ifdef WDCDEBUG
     56  1.6.2.1  bouyer int wdcdebug_pciide_mask = DEBUG_PROBE;
     57  1.6.2.1  bouyer #define WDCDEBUG_PRINT(args, level) \
     58  1.6.2.1  bouyer 	if (wdcdebug_pciide_mask & (level)) printf args
     59  1.6.2.1  bouyer #else
     60  1.6.2.1  bouyer #define WDCDEBUG_PRINT(args, level)
     61  1.6.2.1  bouyer #endif
     62      1.1     cgd #include <sys/param.h>
     63      1.1     cgd #include <sys/systm.h>
     64      1.1     cgd #include <sys/device.h>
     65  1.6.2.1  bouyer #include <sys/malloc.h>
     66  1.6.2.1  bouyer 
     67  1.6.2.1  bouyer #include <vm/vm.h>
     68  1.6.2.1  bouyer #include <vm/vm_param.h>
     69  1.6.2.1  bouyer #include <vm/vm_kern.h>
     70      1.1     cgd 
     71      1.1     cgd #include <dev/pci/pcireg.h>
     72      1.1     cgd #include <dev/pci/pcivar.h>
     73  1.6.2.1  bouyer #include <dev/pci/pcidevs.h>
     74      1.1     cgd #include <dev/pci/pciidereg.h>
     75      1.1     cgd #include <dev/pci/pciidevar.h>
     76  1.6.2.1  bouyer #include <dev/pci/pciide_pIIx_reg.h>
     77  1.6.2.1  bouyer #include <dev/ata/atavar.h>
     78      1.6     cgd #include <dev/ic/wdcreg.h>
     79  1.6.2.1  bouyer #include <dev/ic/wdcvar.h>
     80      1.1     cgd 
     81      1.1     cgd struct pciide_softc {
     82  1.6.2.1  bouyer 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     83      1.1     cgd 
     84      1.1     cgd 	void			*sc_pci_ih;	/* PCI interrupt handle */
     85      1.5     cgd 	int			sc_dma_ok;	/* bus-master DMA info */
     86      1.2     cgd 	bus_space_tag_t		sc_dma_iot;
     87      1.2     cgd 	bus_space_handle_t	sc_dma_ioh;
     88  1.6.2.1  bouyer 	bus_dma_tag_t		sc_dmat;
     89  1.6.2.1  bouyer 	/* Chip description */
     90  1.6.2.1  bouyer 	const struct pciide_product_desc *sc_pp;
     91  1.6.2.1  bouyer 	/* common definitions */
     92  1.6.2.1  bouyer 	struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
     93  1.6.2.1  bouyer 	/* internal bookkeeping */
     94      1.1     cgd 	struct pciide_channel {			/* per-channel data */
     95      1.5     cgd 		int		hw_ok;		/* hardware mapped & OK? */
     96      1.1     cgd 		int		compat;		/* is it compat? */
     97      1.1     cgd 		void		*ih;		/* compat or pci handle */
     98  1.6.2.1  bouyer 		/* DMA tables and DMA map for xfer, for each drive */
     99  1.6.2.1  bouyer 		struct pciide_dma_maps {
    100  1.6.2.1  bouyer 			bus_dmamap_t    dmamap_table;
    101  1.6.2.1  bouyer 			struct idedma_table *dma_table;
    102  1.6.2.1  bouyer 			bus_dmamap_t    dmamap_xfer;
    103  1.6.2.1  bouyer 		} dma_maps[2];
    104  1.6.2.1  bouyer 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    105  1.6.2.1  bouyer };
    106  1.6.2.1  bouyer 
    107  1.6.2.1  bouyer void default_setup_cap __P((struct pciide_softc*));
    108  1.6.2.1  bouyer void default_setup_chip __P((struct pciide_softc*,
    109  1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    110  1.6.2.1  bouyer void piix_setup_cap __P((struct pciide_softc*));
    111  1.6.2.1  bouyer void piix_setup_chip __P((struct pciide_softc*,
    112  1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    113  1.6.2.1  bouyer void piix3_4_setup_chip __P((struct pciide_softc*,
    114  1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    115  1.6.2.1  bouyer 
    116  1.6.2.1  bouyer static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    117  1.6.2.1  bouyer static u_int32_t piix_setup_idetim_drvs __P((u_int8_t, u_int8_t, u_int8_t));
    118  1.6.2.1  bouyer static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    119  1.6.2.1  bouyer 
    120  1.6.2.1  bouyer int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    121  1.6.2.1  bouyer int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    122  1.6.2.1  bouyer void pciide_dma_start __P((void*, int, int, int));
    123  1.6.2.1  bouyer int  pciide_dma_finish __P((void*, int, int, int));
    124  1.6.2.1  bouyer 
    125  1.6.2.1  bouyer struct pciide_product_desc {
    126  1.6.2.1  bouyer     u_int32_t ide_product;
    127  1.6.2.1  bouyer     int ide_flags;
    128  1.6.2.1  bouyer     const char *ide_name;
    129  1.6.2.1  bouyer     /* init controller's capabilities for drives probe */
    130  1.6.2.1  bouyer     void (*setup_cap) __P((struct pciide_softc*));
    131  1.6.2.1  bouyer     /* init controller after drives probe */
    132  1.6.2.1  bouyer     void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
    133  1.6.2.1  bouyer };
    134  1.6.2.1  bouyer 
    135  1.6.2.1  bouyer /* Flags for ide_flags */
    136  1.6.2.1  bouyer #define NO_PCI_INTR       0x01 /* don't try to map the native PCI intr */
    137  1.6.2.1  bouyer #define ONE_QUEUE         0x02 /* device need serialised access */
    138  1.6.2.1  bouyer 
    139  1.6.2.1  bouyer /* Default product description for devices not known from this controller */
    140  1.6.2.1  bouyer const struct pciide_product_desc default_product_desc = {
    141  1.6.2.1  bouyer     0,
    142  1.6.2.1  bouyer     0,
    143  1.6.2.1  bouyer     "Generic PCI IDE controller",
    144  1.6.2.1  bouyer     default_setup_cap,
    145  1.6.2.1  bouyer     default_setup_chip
    146  1.6.2.1  bouyer };
    147      1.1     cgd 
    148  1.6.2.1  bouyer 
    149  1.6.2.1  bouyer const struct pciide_product_desc pciide_intel_products[] =  {
    150  1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82092AA,
    151  1.6.2.1  bouyer       0,
    152  1.6.2.1  bouyer       "Intel 82092AA IDE controller",
    153  1.6.2.1  bouyer       default_setup_cap,
    154  1.6.2.1  bouyer       default_setup_chip
    155  1.6.2.1  bouyer     },
    156  1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371FB_IDE,
    157  1.6.2.1  bouyer       0,
    158  1.6.2.1  bouyer       "Intel 82371FB IDE controller (PIIX)",
    159  1.6.2.1  bouyer       piix_setup_cap,
    160  1.6.2.1  bouyer       piix_setup_chip
    161  1.6.2.1  bouyer     },
    162  1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371SB_IDE,
    163  1.6.2.1  bouyer       0,
    164  1.6.2.1  bouyer       "Intel 82371SB IDE Interface (PIIX3)",
    165  1.6.2.1  bouyer       piix_setup_cap,
    166  1.6.2.1  bouyer       piix3_4_setup_chip
    167  1.6.2.1  bouyer     },
    168  1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371AB_IDE,
    169  1.6.2.1  bouyer       0,
    170  1.6.2.1  bouyer       "Intel 82371AB IDE controller (PIIX4)",
    171  1.6.2.1  bouyer       piix_setup_cap,
    172  1.6.2.1  bouyer       piix3_4_setup_chip
    173  1.6.2.1  bouyer     },
    174  1.6.2.1  bouyer     { 0,
    175  1.6.2.1  bouyer       0,
    176  1.6.2.1  bouyer       NULL,
    177  1.6.2.1  bouyer     }
    178  1.6.2.1  bouyer };
    179  1.6.2.1  bouyer const struct pciide_product_desc pciide_cmd_products[] =  {
    180  1.6.2.1  bouyer     { PCI_PRODUCT_CMDTECH_640,
    181  1.6.2.1  bouyer       NO_PCI_INTR | ONE_QUEUE,
    182  1.6.2.1  bouyer       "CMD Technology PCI0640",
    183  1.6.2.1  bouyer       default_setup_cap,
    184  1.6.2.1  bouyer       default_setup_chip
    185  1.6.2.1  bouyer     },
    186  1.6.2.1  bouyer     { 0,
    187  1.6.2.1  bouyer       0,
    188  1.6.2.1  bouyer       NULL,
    189  1.6.2.1  bouyer     }
    190  1.6.2.1  bouyer };
    191  1.6.2.1  bouyer 
    192  1.6.2.1  bouyer struct pciide_vendor_desc {
    193  1.6.2.1  bouyer     u_int32_t ide_vendor;
    194  1.6.2.1  bouyer     const struct pciide_product_desc *ide_products;
    195  1.6.2.1  bouyer };
    196  1.6.2.1  bouyer 
    197  1.6.2.1  bouyer const struct pciide_vendor_desc pciide_vendors[] = {
    198  1.6.2.1  bouyer     { PCI_VENDOR_INTEL, pciide_intel_products },
    199  1.6.2.1  bouyer     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    200  1.6.2.1  bouyer     { 0, NULL }
    201      1.1     cgd };
    202      1.1     cgd 
    203  1.6.2.1  bouyer 
    204      1.1     cgd #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    205      1.1     cgd 
    206      1.1     cgd #ifdef __BROKEN_INDIRECT_CONFIG
    207      1.1     cgd int	pciide_match __P((struct device *, void *, void *));
    208      1.1     cgd #else
    209      1.1     cgd int	pciide_match __P((struct device *, struct cfdata *, void *));
    210      1.1     cgd #endif
    211      1.1     cgd void	pciide_attach __P((struct device *, struct device *, void *));
    212      1.1     cgd 
    213      1.1     cgd struct cfattach pciide_ca = {
    214      1.1     cgd 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    215      1.1     cgd };
    216      1.1     cgd 
    217      1.5     cgd int	pciide_map_channel_compat __P((struct pciide_softc *,
    218      1.5     cgd 	    struct pci_attach_args *, int));
    219      1.6     cgd const char *pciide_compat_channel_probe __P((struct pciide_softc *,
    220      1.5     cgd 	    struct pci_attach_args *, int));
    221      1.5     cgd int	pciide_map_channel_native __P((struct pciide_softc *,
    222      1.5     cgd 	    struct pci_attach_args *, int));
    223      1.5     cgd int	pciide_print __P((void *, const char *pnp));
    224      1.1     cgd int	pciide_compat_intr __P((void *));
    225      1.1     cgd int	pciide_pci_intr __P((void *));
    226  1.6.2.1  bouyer const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    227      1.1     cgd 
    228  1.6.2.1  bouyer const struct pciide_product_desc*
    229  1.6.2.1  bouyer pciide_lookup_product(id)
    230  1.6.2.1  bouyer     u_int32_t id;
    231  1.6.2.1  bouyer {
    232  1.6.2.1  bouyer     const struct pciide_product_desc *pp;
    233  1.6.2.1  bouyer     const struct pciide_vendor_desc *vp;
    234  1.6.2.1  bouyer 
    235  1.6.2.1  bouyer     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    236  1.6.2.1  bouyer 	if (PCI_VENDOR(id) == vp->ide_vendor)
    237  1.6.2.1  bouyer 	    break;
    238  1.6.2.1  bouyer 
    239  1.6.2.1  bouyer     if ((pp = vp->ide_products) == NULL)
    240  1.6.2.1  bouyer 	return NULL;
    241  1.6.2.1  bouyer 
    242  1.6.2.1  bouyer     for (; pp->ide_name != NULL; pp++)
    243  1.6.2.1  bouyer 	if (PCI_PRODUCT(id) == pp->ide_product)
    244  1.6.2.1  bouyer 	    break;
    245  1.6.2.1  bouyer 
    246  1.6.2.1  bouyer     if (pp->ide_name == NULL)
    247  1.6.2.1  bouyer 	return NULL;
    248  1.6.2.1  bouyer     return pp;
    249  1.6.2.1  bouyer }
    250      1.6     cgd 
    251      1.1     cgd int
    252      1.1     cgd pciide_match(parent, match, aux)
    253      1.1     cgd 	struct device *parent;
    254      1.1     cgd #ifdef __BROKEN_INDIRECT_CONFIG
    255      1.1     cgd 	void *match;
    256      1.1     cgd #else
    257      1.1     cgd 	struct cfdata *match;
    258      1.1     cgd #endif
    259      1.1     cgd 	void *aux;
    260      1.1     cgd {
    261      1.1     cgd 	struct pci_attach_args *pa = aux;
    262      1.1     cgd 
    263      1.1     cgd 	/*
    264      1.1     cgd 	 * Check the ID register to see that it's a PCI IDE controller.
    265      1.1     cgd 	 * If it is, we assume that we can deal with it; it _should_
    266      1.1     cgd 	 * work in a standardized way...
    267      1.1     cgd 	 */
    268      1.1     cgd 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    269      1.1     cgd 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    270      1.1     cgd 		return (1);
    271      1.1     cgd 	}
    272      1.1     cgd 
    273      1.1     cgd 	return (0);
    274      1.1     cgd }
    275      1.1     cgd 
    276      1.1     cgd void
    277      1.1     cgd pciide_attach(parent, self, aux)
    278      1.1     cgd 	struct device *parent, *self;
    279      1.1     cgd 	void *aux;
    280      1.1     cgd {
    281      1.1     cgd 	struct pci_attach_args *pa = aux;
    282      1.1     cgd 	pci_chipset_tag_t pc = pa->pa_pc;
    283  1.6.2.1  bouyer 	pcitag_t tag = pa->pa_tag;
    284      1.1     cgd 	struct pciide_softc *sc = (struct pciide_softc *)self;
    285      1.1     cgd 	struct pciide_channel *cp;
    286      1.1     cgd 	pcireg_t class, interface, csr;
    287      1.1     cgd 	pci_intr_handle_t intrhandle;
    288      1.1     cgd 	const char *intrstr;
    289      1.1     cgd 	char devinfo[256];
    290      1.1     cgd 	int i;
    291      1.1     cgd 
    292  1.6.2.1  bouyer         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    293  1.6.2.1  bouyer 	if (sc->sc_pp == NULL) {
    294  1.6.2.1  bouyer 		sc->sc_pp = &default_product_desc;
    295  1.6.2.1  bouyer 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    296  1.6.2.1  bouyer 		printf(": %s (rev. 0x%02x)\n", devinfo,
    297  1.6.2.1  bouyer 		    PCI_REVISION(pa->pa_class));
    298  1.6.2.1  bouyer 	} else {
    299  1.6.2.1  bouyer 		printf(": %s\n", sc->sc_pp->ide_name);
    300  1.6.2.1  bouyer 	}
    301      1.1     cgd 
    302      1.1     cgd 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    303  1.6.2.1  bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    304  1.6.2.1  bouyer 		printf("%s: device disabled (at %s)\n",
    305  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    306      1.1     cgd 		    (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
    307      1.1     cgd 		return;
    308      1.1     cgd 	}
    309      1.1     cgd 
    310  1.6.2.1  bouyer 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    311      1.1     cgd 	interface = PCI_INTERFACE(class);
    312      1.1     cgd 
    313      1.1     cgd 	/*
    314      1.1     cgd 	 * Set up PCI interrupt.
    315      1.1     cgd 	 *
    316      1.1     cgd 	 * If mapping fails, that's (probably) because there's no pin
    317      1.1     cgd 	 * set to intr, which is (probably) because it's a compat-only
    318      1.1     cgd 	 * device (or hard-wired in compatibility-only mode).  Native-PCI
    319      1.1     cgd 	 * channels will complain later if the interrupt was needed.
    320      1.1     cgd 	 *
    321      1.1     cgd 	 * If establishment fails, that's (probably) some other problem.
    322      1.1     cgd 	 */
    323  1.6.2.1  bouyer 	if ((sc->sc_pp->ide_flags & NO_PCI_INTR) == 0) {
    324  1.6.2.1  bouyer 	    if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    325  1.6.2.1  bouyer 	        pa->pa_intrline, &intrhandle) == 0) {
    326  1.6.2.1  bouyer 		    intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    327  1.6.2.1  bouyer 		    sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
    328  1.6.2.1  bouyer 		        IPL_BIO, pciide_pci_intr, sc);
    329      1.1     cgd 
    330  1.6.2.1  bouyer 		    if (sc->sc_pci_ih != NULL) {
    331      1.1     cgd 			printf("%s: using %s for native-PCI interrupt\n",
    332  1.6.2.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    333      1.1     cgd 			    intrstr ? intrstr : "unknown interrupt");
    334  1.6.2.1  bouyer 		    } else {
    335      1.1     cgd 			printf("%s: couldn't establish native-PCI interrupt",
    336  1.6.2.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    337      1.1     cgd 			if (intrstr != NULL)
    338  1.6.2.1  bouyer 			    printf(" at %s", intrstr);
    339      1.1     cgd 			printf("\n");
    340  1.6.2.1  bouyer 		    }
    341  1.6.2.1  bouyer 	    }
    342      1.1     cgd 	}
    343      1.1     cgd 
    344      1.2     cgd 	/*
    345      1.2     cgd 	 * Map DMA registers, if DMA is supported.
    346      1.2     cgd 	 *
    347      1.5     cgd 	 * Note that sc_dma_ok is the right variable to test to see if
    348      1.5     cgd 	 * DMA can * be done.  If the interface doesn't support DMA,
    349      1.5     cgd 	 * sc_dma_ok * will never be non-zero.  If the DMA regs couldn't
    350      1.5     cgd 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    351      1.5     cgd 	 * non-zero if the interface supports DMA and the registers
    352      1.5     cgd 	 * could be mapped.
    353      1.4     cgd 	 *
    354      1.4     cgd 	 * XXX Note that despite the fact that the Bus Master IDE specs
    355      1.4     cgd 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    356      1.4     cgd 	 * XXX space," some controllers (at least the United
    357      1.4     cgd 	 * XXX Microelectronics UM8886BF) place it in memory space.
    358      1.4     cgd 	 * XXX eventually, we should probably read the register and check
    359      1.4     cgd 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    360      1.2     cgd 	 */
    361      1.2     cgd 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    362      1.5     cgd 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    363      1.2     cgd 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    364      1.2     cgd 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    365  1.6.2.1  bouyer 		sc->sc_dmat = pa->pa_dmat;
    366  1.6.2.1  bouyer 		printf("%s: bus-master DMA support present",
    367  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    368  1.6.2.1  bouyer 		if (sc->sc_dma_ok == 0) {
    369  1.6.2.1  bouyer 			printf(", but unused (couldn't map registers)");
    370  1.6.2.1  bouyer 		} else if (sc->sc_pp == 0) {
    371  1.6.2.1  bouyer 			printf(", but unused (no driver support)");
    372  1.6.2.1  bouyer 		} else {
    373  1.6.2.1  bouyer 			sc->sc_wdcdev.dma_arg = sc;
    374  1.6.2.1  bouyer 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    375  1.6.2.1  bouyer 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    376  1.6.2.1  bouyer 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    377  1.6.2.1  bouyer 		}
    378  1.6.2.1  bouyer 		printf("\n");
    379      1.1     cgd 	}
    380  1.6.2.1  bouyer 	if (sc->sc_pp == NULL)
    381  1.6.2.1  bouyer 		default_setup_cap(sc);
    382  1.6.2.1  bouyer 	else
    383  1.6.2.1  bouyer 		sc->sc_pp->setup_cap(sc);
    384  1.6.2.1  bouyer 	sc->sc_wdcdev.channels = sc->wdc_channels;
    385  1.6.2.1  bouyer 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    386      1.1     cgd 
    387      1.1     cgd 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    388  1.6.2.1  bouyer 		cp = &sc->pciide_channels[i];
    389      1.2     cgd 
    390  1.6.2.1  bouyer 		sc->wdc_channels[i].channel = i;
    391  1.6.2.1  bouyer 		sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
    392  1.6.2.1  bouyer 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    393  1.6.2.1  bouyer 		    sc->wdc_channels[i].ch_queue =
    394  1.6.2.1  bouyer 			sc->wdc_channels[0].ch_queue;
    395  1.6.2.1  bouyer 		} else {
    396  1.6.2.1  bouyer 		    sc->wdc_channels[i].ch_queue =
    397  1.6.2.1  bouyer 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    398  1.6.2.1  bouyer 			M_NOWAIT);
    399  1.6.2.1  bouyer 		}
    400  1.6.2.1  bouyer 		if (sc->wdc_channels[i].ch_queue == NULL) {
    401  1.6.2.1  bouyer 		    printf("%s %s channel: "
    402  1.6.2.1  bouyer 			"can't allocate memory for command queue",
    403  1.6.2.1  bouyer 			sc->sc_wdcdev.sc_dev.dv_xname,
    404  1.6.2.1  bouyer 			PCIIDE_CHANNEL_NAME(i));
    405  1.6.2.1  bouyer 			continue;
    406  1.6.2.1  bouyer 		}
    407      1.2     cgd 		printf("%s: %s channel %s to %s mode\n",
    408  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    409  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(i),
    410      1.2     cgd 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    411      1.2     cgd 		      "configured" : "wired",
    412      1.2     cgd 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    413      1.2     cgd 		      "compatibility");
    414      1.1     cgd 
    415      1.5     cgd 		if (interface & PCIIDE_INTERFACE_PCI(i))
    416      1.5     cgd 			cp->hw_ok = pciide_map_channel_native(sc, pa, i);
    417      1.5     cgd 		else
    418      1.5     cgd 			cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
    419      1.5     cgd 		if (!cp->hw_ok)
    420      1.5     cgd 			continue;
    421  1.6.2.1  bouyer 		/* Now call common attach routine */
    422  1.6.2.1  bouyer 		wdcattach(&sc->wdc_channels[i]);
    423      1.5     cgd 	}
    424  1.6.2.1  bouyer 	if (sc->sc_pp == NULL)
    425  1.6.2.1  bouyer 		default_setup_chip(sc, pc, tag);
    426  1.6.2.1  bouyer 	else
    427  1.6.2.1  bouyer 		sc->sc_pp->setup_chip(sc, pc, tag);
    428  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    429  1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    430      1.5     cgd }
    431      1.5     cgd 
    432      1.5     cgd int
    433      1.5     cgd pciide_map_channel_compat(sc, pa, chan)
    434      1.5     cgd 	struct pciide_softc *sc;
    435      1.5     cgd 	struct pci_attach_args *pa;
    436      1.5     cgd 	int chan;
    437      1.5     cgd {
    438  1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    439  1.6.2.1  bouyer 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    440      1.6     cgd 	const char *probe_fail_reason;
    441      1.5     cgd 	int rv = 1;
    442      1.5     cgd 
    443      1.5     cgd 	cp->compat = 1;
    444      1.5     cgd 
    445  1.6.2.1  bouyer 	wdc_cp->cmd_iot = pa->pa_iot;
    446  1.6.2.1  bouyer 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
    447  1.6.2.1  bouyer 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    448      1.5     cgd 		printf("%s: couldn't map %s channel cmd regs\n",
    449  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    450  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    451      1.5     cgd 		rv = 0;
    452      1.5     cgd 	}
    453      1.5     cgd 
    454  1.6.2.1  bouyer 	wdc_cp->ctl_iot = pa->pa_iot;
    455  1.6.2.1  bouyer 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
    456  1.6.2.1  bouyer 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    457      1.5     cgd 		printf("%s: couldn't map %s channel ctl regs\n",
    458  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    459  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    460      1.5     cgd 		rv = 0;
    461      1.5     cgd 	}
    462      1.5     cgd 
    463      1.5     cgd 	/*
    464      1.5     cgd 	 * If we weren't able to map the device successfully,
    465      1.5     cgd 	 * we just give up now.  Something else has already
    466      1.5     cgd 	 * occupied those ports, indicating that the device has
    467      1.5     cgd 	 * (probably) been completely disabled (by some nonstandard
    468      1.5     cgd 	 * mechanism).
    469      1.5     cgd 	 *
    470      1.5     cgd 	 * XXX If we successfully map some ports, but not others,
    471      1.5     cgd 	 * XXX it might make sense to unmap the ones that we mapped.
    472      1.5     cgd 	 */
    473      1.5     cgd 	if (rv == 0)
    474      1.5     cgd 		goto out;
    475      1.5     cgd 
    476      1.5     cgd 	/*
    477      1.5     cgd 	 * If we were able to map the device successfully, try to
    478      1.5     cgd 	 * make sure that there's a wdc there and that it's
    479      1.5     cgd 	 * attributable to us.
    480      1.5     cgd 	 *
    481      1.5     cgd 	 * If there's not, then we assume that there's the device
    482      1.5     cgd 	 * has been disabled and that other devices are free to use
    483      1.5     cgd 	 * its ports.
    484      1.5     cgd 	 */
    485      1.6     cgd 	probe_fail_reason = pciide_compat_channel_probe(sc, pa, chan);
    486      1.6     cgd 	if (probe_fail_reason != NULL) {
    487  1.6.2.1  bouyer 		printf("%s: %s channel ignored (%s)\n",
    488  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    489      1.6     cgd 		    PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
    490      1.5     cgd 		rv = 0;
    491      1.5     cgd 
    492  1.6.2.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    493      1.5     cgd 		    PCIIDE_COMPAT_CMD_SIZE);
    494  1.6.2.1  bouyer 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    495      1.5     cgd 		    PCIIDE_COMPAT_CTL_SIZE);
    496      1.5     cgd 
    497      1.5     cgd 		goto out;
    498      1.5     cgd 	}
    499      1.5     cgd 
    500      1.5     cgd 	/*
    501      1.5     cgd 	 * If we're here, we were able to map the device successfully
    502      1.5     cgd 	 * and it really looks like there's a controller there.
    503      1.5     cgd 	 *
    504      1.5     cgd 	 * Unless those conditions are true, we don't map the
    505      1.5     cgd 	 * compatibility interrupt.  The spec indicates that if a
    506      1.5     cgd 	 * channel is configured for compatibility mode and the PCI
    507      1.5     cgd 	 * device's I/O space is enabled, the channel will be enabled.
    508      1.5     cgd 	 * Hoewver, some devices seem to be able to disable invididual
    509      1.5     cgd 	 * compatibility channels (via non-standard mechanisms).  If
    510      1.5     cgd 	 * the channel is disabled, the interrupt line can (probably)
    511      1.5     cgd 	 * be used by other devices (and may be assigned to other
    512      1.5     cgd 	 * devices by the BIOS).  If we mapped the interrupt we might
    513      1.5     cgd 	 * conflict with another interrupt assignment.
    514      1.5     cgd 	 */
    515  1.6.2.1  bouyer 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    516  1.6.2.1  bouyer 	    pa, chan, pciide_compat_intr, wdc_cp);
    517      1.5     cgd 	if (cp->ih == NULL) {
    518      1.5     cgd 		printf("%s: no compatibility interrupt for use by %s channel\n",
    519  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    520  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    521      1.5     cgd 		rv = 0;
    522      1.5     cgd 	}
    523      1.5     cgd 
    524      1.5     cgd out:
    525      1.5     cgd 	return (rv);
    526      1.5     cgd }
    527      1.5     cgd 
    528      1.6     cgd const char *
    529      1.5     cgd pciide_compat_channel_probe(sc, pa, chan)
    530      1.5     cgd 	struct pciide_softc *sc;
    531      1.5     cgd 	struct pci_attach_args *pa;
    532      1.5     cgd {
    533      1.6     cgd 	pcireg_t csr;
    534      1.6     cgd 	const char *failreason = NULL;
    535      1.6     cgd 
    536      1.6     cgd 	/*
    537      1.6     cgd 	 * Check to see if something appears to be there.
    538      1.6     cgd 	 */
    539  1.6.2.1  bouyer 	if (!wdcprobe(&sc->wdc_channels[chan])) {
    540      1.6     cgd 		failreason = "not responding; disabled or no drives?";
    541      1.6     cgd 		goto out;
    542      1.6     cgd 	}
    543      1.5     cgd 
    544      1.5     cgd 	/*
    545      1.6     cgd 	 * Now, make sure it's actually attributable to this PCI IDE
    546      1.6     cgd 	 * channel by trying to access the channel again while the
    547      1.6     cgd 	 * PCI IDE controller's I/O space is disabled.  (If the
    548      1.6     cgd 	 * channel no longer appears to be there, it belongs to
    549      1.6     cgd 	 * this controller.)  YUCK!
    550      1.5     cgd 	 */
    551      1.6     cgd 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    552      1.6     cgd 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    553      1.6     cgd 	    csr & ~PCI_COMMAND_IO_ENABLE);
    554  1.6.2.1  bouyer 	if (wdcprobe(&sc->wdc_channels[chan]))
    555      1.6     cgd 		failreason = "other hardware responding at addresses";
    556      1.6     cgd 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    557      1.6     cgd 
    558      1.6     cgd out:
    559      1.6     cgd 	return (failreason);
    560      1.6     cgd }
    561      1.6     cgd 
    562      1.6     cgd int
    563      1.5     cgd pciide_map_channel_native(sc, pa, chan)
    564      1.5     cgd 	struct pciide_softc *sc;
    565      1.5     cgd 	struct pci_attach_args *pa;
    566      1.5     cgd 	int chan;
    567      1.5     cgd {
    568  1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    569  1.6.2.1  bouyer 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    570      1.5     cgd 	int rv = 1;
    571      1.5     cgd 
    572      1.5     cgd 	cp->compat = 0;
    573      1.5     cgd 
    574      1.5     cgd 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
    575  1.6.2.1  bouyer 	    0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
    576      1.5     cgd 		printf("%s: couldn't map %s channel cmd regs\n",
    577  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    578  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    579      1.5     cgd 		rv = 0;
    580      1.5     cgd 	}
    581      1.5     cgd 
    582      1.5     cgd 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
    583  1.6.2.1  bouyer 	    0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
    584      1.5     cgd 		printf("%s: couldn't map %s channel ctl regs\n",
    585  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    586  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    587      1.5     cgd 		rv = 0;
    588      1.5     cgd 	}
    589      1.5     cgd 
    590      1.5     cgd 	if ((cp->ih = sc->sc_pci_ih) == NULL) {
    591      1.5     cgd 		printf("%s: no native-PCI interrupt for use by %s channel\n",
    592  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    593  1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    594      1.5     cgd 		rv = 0;
    595      1.1     cgd 	}
    596      1.5     cgd 
    597      1.5     cgd 	return (rv);
    598      1.1     cgd }
    599      1.1     cgd 
    600      1.1     cgd int
    601      1.1     cgd pciide_compat_intr(arg)
    602      1.1     cgd 	void *arg;
    603      1.1     cgd {
    604  1.6.2.1  bouyer 	struct channel_softc *wdc_cp = arg;
    605      1.1     cgd 
    606      1.1     cgd #ifdef DIAGNOSTIC
    607  1.6.2.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
    608  1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
    609      1.1     cgd 	/* should only be called for a compat channel */
    610      1.1     cgd 	if (cp->compat == 0)
    611      1.1     cgd 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    612      1.1     cgd #endif
    613  1.6.2.1  bouyer 	return (wdcintr(wdc_cp));
    614      1.1     cgd }
    615      1.1     cgd 
    616      1.1     cgd int
    617      1.1     cgd pciide_pci_intr(arg)
    618      1.1     cgd 	void *arg;
    619      1.1     cgd {
    620      1.1     cgd 	struct pciide_softc *sc = arg;
    621      1.1     cgd 	struct pciide_channel *cp;
    622  1.6.2.1  bouyer 	struct channel_softc *wdc_cp;
    623      1.1     cgd 	int i, rv, crv;
    624      1.1     cgd 
    625      1.1     cgd 	rv = 0;
    626      1.1     cgd 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    627  1.6.2.1  bouyer 		cp = &sc->pciide_channels[i];
    628  1.6.2.1  bouyer 		wdc_cp = &sc->wdc_channels[i];
    629      1.1     cgd 
    630  1.6.2.1  bouyer 		/* If a compat channel skip. */
    631  1.6.2.1  bouyer 		if (cp->compat)
    632  1.6.2.1  bouyer 			continue;
    633  1.6.2.1  bouyer 		/* if this channel not waiting for intr, skip */
    634  1.6.2.1  bouyer 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    635      1.1     cgd 			continue;
    636      1.1     cgd 
    637  1.6.2.1  bouyer 		crv = wdcintr(wdc_cp);
    638      1.1     cgd 		if (crv == 0)
    639      1.1     cgd 			;		/* leave rv alone */
    640      1.1     cgd 		else if (crv == 1)
    641      1.1     cgd 			rv = 1;		/* claim the intr */
    642      1.1     cgd 		else if (rv == 0)	/* crv should be -1 in this case */
    643      1.1     cgd 			rv = crv;	/* if we've done no better, take it */
    644      1.1     cgd 	}
    645      1.1     cgd 	return (rv);
    646  1.6.2.1  bouyer }
    647  1.6.2.1  bouyer 
    648  1.6.2.1  bouyer void
    649  1.6.2.1  bouyer default_setup_cap(sc)
    650  1.6.2.1  bouyer 	struct pciide_softc *sc;
    651  1.6.2.1  bouyer {
    652  1.6.2.1  bouyer 	if (sc->sc_dma_ok)
    653  1.6.2.1  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    654  1.6.2.2  bouyer 	sc->sc_wdcdev.pio_mode = 0;
    655  1.6.2.1  bouyer 	sc->sc_wdcdev.dma_mode = 0;
    656  1.6.2.1  bouyer }
    657  1.6.2.1  bouyer 
    658  1.6.2.1  bouyer void
    659  1.6.2.1  bouyer default_setup_chip(sc, pc, tag)
    660  1.6.2.1  bouyer 	struct pciide_softc *sc;
    661  1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    662  1.6.2.1  bouyer 	pcitag_t tag;
    663  1.6.2.1  bouyer {
    664  1.6.2.2  bouyer 	int channel, drive, idedma_ctl;
    665  1.6.2.2  bouyer 	struct channel_softc *chp;
    666  1.6.2.2  bouyer 	struct ata_drive_datas *drvp;
    667  1.6.2.2  bouyer 
    668  1.6.2.2  bouyer 	if (sc->sc_dma_ok == 0)
    669  1.6.2.2  bouyer 		return; /* nothing to do */
    670  1.6.2.2  bouyer 
    671  1.6.2.2  bouyer 	/* Allocate DMA maps */
    672  1.6.2.2  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    673  1.6.2.2  bouyer 		idedma_ctl = 0;
    674  1.6.2.2  bouyer 		chp = &sc->wdc_channels[channel];
    675  1.6.2.2  bouyer 		for (drive = 0; drive < 2; drive++) {
    676  1.6.2.2  bouyer 			drvp = &chp->ch_drive[drive];
    677  1.6.2.2  bouyer 			/* If no drive, skip */
    678  1.6.2.2  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
    679  1.6.2.2  bouyer 				continue;
    680  1.6.2.2  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    681  1.6.2.2  bouyer 				/* Abort DMA setup */
    682  1.6.2.2  bouyer 				printf("%s:%d:%d: can't allocate DMA maps, "
    683  1.6.2.2  bouyer 				    "using PIO transferts\n",
    684  1.6.2.2  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    685  1.6.2.2  bouyer 				    channel, drive);
    686  1.6.2.2  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    687  1.6.2.2  bouyer 			}
    688  1.6.2.2  bouyer 			printf("%s:%d:%d: using DMA mode %d\n",
    689  1.6.2.2  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    690  1.6.2.2  bouyer 			    channel, drive,
    691  1.6.2.2  bouyer 			    drvp->DMA_mode);
    692  1.6.2.2  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    693  1.6.2.2  bouyer 		}
    694  1.6.2.2  bouyer 		if (idedma_ctl != 0) {
    695  1.6.2.2  bouyer 			/* Add software bits in status register */
    696  1.6.2.2  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    697  1.6.2.2  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    698  1.6.2.2  bouyer 			    idedma_ctl);
    699  1.6.2.2  bouyer 		}
    700  1.6.2.2  bouyer 	}
    701  1.6.2.2  bouyer 
    702  1.6.2.1  bouyer }
    703  1.6.2.1  bouyer 
    704  1.6.2.1  bouyer void
    705  1.6.2.1  bouyer piix_setup_cap(sc)
    706  1.6.2.1  bouyer 	struct pciide_softc *sc;
    707  1.6.2.1  bouyer {
    708  1.6.2.3  bouyer #if 0
    709  1.6.2.1  bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
    710  1.6.2.1  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    711  1.6.2.3  bouyer #endif
    712  1.6.2.2  bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
    713  1.6.2.2  bouyer 	    WDC_CAPABILITY_DMA;
    714  1.6.2.1  bouyer 	sc->sc_wdcdev.pio_mode = 4;
    715  1.6.2.1  bouyer 	sc->sc_wdcdev.dma_mode = 2;
    716  1.6.2.1  bouyer }
    717  1.6.2.1  bouyer 
    718  1.6.2.1  bouyer void
    719  1.6.2.1  bouyer piix_setup_chip(sc, pc, tag)
    720  1.6.2.1  bouyer 	struct pciide_softc *sc;
    721  1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    722  1.6.2.1  bouyer 	pcitag_t tag;
    723  1.6.2.1  bouyer {
    724  1.6.2.1  bouyer 	struct channel_softc *chp;
    725  1.6.2.1  bouyer 	u_int8_t mode[2];
    726  1.6.2.1  bouyer 	u_int8_t channel, drive;
    727  1.6.2.1  bouyer 	u_int32_t idetim, sidetim, idedma_ctl;
    728  1.6.2.1  bouyer 	struct ata_drive_datas *drvp;
    729  1.6.2.1  bouyer 
    730  1.6.2.1  bouyer 	idetim = sidetim = 0;
    731  1.6.2.1  bouyer 
    732  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    733  1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    734  1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    735  1.6.2.1  bouyer 
    736  1.6.2.1  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    737  1.6.2.1  bouyer 		chp = &sc->wdc_channels[channel];
    738  1.6.2.1  bouyer 		drvp = chp->ch_drive;
    739  1.6.2.1  bouyer 		idedma_ctl = 0;
    740  1.6.2.1  bouyer 		/* Enable IDE registers decode */
    741  1.6.2.1  bouyer 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    742  1.6.2.1  bouyer 		    channel);
    743  1.6.2.1  bouyer 
    744  1.6.2.1  bouyer 		/* setup DMA if needed */
    745  1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    746  1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA &&
    747  1.6.2.1  bouyer 			    pciide_dma_table_setup(sc, channel, drive) != 0) {
    748  1.6.2.1  bouyer 				drvp[drive].drive_flags &= ~DRIVE_DMA;
    749  1.6.2.1  bouyer 			}
    750  1.6.2.1  bouyer 		}
    751  1.6.2.1  bouyer 
    752  1.6.2.1  bouyer 		/*
    753  1.6.2.1  bouyer 		 * Here we have to mess up with drives mode: PIIX can't have
    754  1.6.2.1  bouyer 		 * different timings for master and slave drives.
    755  1.6.2.1  bouyer 		 * We need to find the best combination.
    756  1.6.2.1  bouyer 		 */
    757  1.6.2.1  bouyer 
    758  1.6.2.1  bouyer 		/* If both drives supports DMA, takes the lower mode */
    759  1.6.2.1  bouyer 		if ((drvp[0].drive_flags & DRIVE_DMA) &&
    760  1.6.2.1  bouyer 		    (drvp[1].drive_flags & DRIVE_DMA)) {
    761  1.6.2.1  bouyer 			mode[0] = mode[1] =
    762  1.6.2.1  bouyer 			    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
    763  1.6.2.1  bouyer 			goto ok;
    764  1.6.2.1  bouyer 		}
    765  1.6.2.1  bouyer 		/*
    766  1.6.2.1  bouyer 		 * If only one drive supports DMA, use its mode, and
    767  1.6.2.1  bouyer 		 * put the other one in PIO mode 0 if mode not compatible
    768  1.6.2.1  bouyer 		 */
    769  1.6.2.1  bouyer 		if (drvp[0].drive_flags & DRIVE_DMA) {
    770  1.6.2.1  bouyer 			mode[0] = drvp[0].DMA_mode;
    771  1.6.2.1  bouyer 			mode[1] = drvp[1].PIO_mode;
    772  1.6.2.1  bouyer 			if (piix_isp_pio[mode[1]] < piix_isp_dma[mode[0]] ||
    773  1.6.2.1  bouyer 			    piix_rtc_pio[mode[1]] < piix_rtc_dma[mode[0]])
    774  1.6.2.1  bouyer 				mode[1] = 0;
    775  1.6.2.1  bouyer 			goto ok;
    776  1.6.2.1  bouyer 		}
    777  1.6.2.1  bouyer 		if (drvp[1].drive_flags & DRIVE_DMA) {
    778  1.6.2.1  bouyer 			mode[1] = drvp[1].DMA_mode;
    779  1.6.2.1  bouyer 			mode[0] = drvp[0].PIO_mode;
    780  1.6.2.1  bouyer 			if (piix_isp_pio[mode[0]] < piix_isp_dma[mode[1]] ||
    781  1.6.2.1  bouyer 			    piix_rtc_pio[mode[0]] < piix_rtc_dma[mode[1]])
    782  1.6.2.1  bouyer 				mode[0] = 0;
    783  1.6.2.1  bouyer 			goto ok;
    784  1.6.2.1  bouyer 		}
    785  1.6.2.1  bouyer 		/*
    786  1.6.2.1  bouyer 		 * If both drives are not DMA, takes the lower mode, unless
    787  1.6.2.1  bouyer 		 * one of them is PIO mode 0
    788  1.6.2.1  bouyer 		 */
    789  1.6.2.1  bouyer 		if (drvp[0].PIO_mode == 0) {
    790  1.6.2.1  bouyer 			mode[0] = 0;
    791  1.6.2.1  bouyer 			mode[1] = drvp[1].PIO_mode;
    792  1.6.2.1  bouyer 		} else if (drvp[1].PIO_mode == 0) {
    793  1.6.2.1  bouyer 			mode[1] = 0;
    794  1.6.2.1  bouyer 			mode[0] = drvp[0].PIO_mode;
    795  1.6.2.1  bouyer 		} else {
    796  1.6.2.1  bouyer 			mode[0] = mode[1] =
    797  1.6.2.1  bouyer 			    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
    798  1.6.2.1  bouyer 		}
    799  1.6.2.1  bouyer ok:		/* The modes are setup */
    800  1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    801  1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    802  1.6.2.1  bouyer 				idetim |= piix_setup_idetim_timings(
    803  1.6.2.1  bouyer 				    mode[drive], 1, channel);
    804  1.6.2.1  bouyer 				goto end;
    805  1.6.2.1  bouyer 			}
    806  1.6.2.1  bouyer 		}
    807  1.6.2.1  bouyer 		/* If we are there, none of the drives are DMA */
    808  1.6.2.1  bouyer 		if (mode[0] > 0)
    809  1.6.2.1  bouyer 			idetim |= piix_setup_idetim_timings(
    810  1.6.2.1  bouyer 			    mode[0], 0, channel);
    811  1.6.2.1  bouyer 		else
    812  1.6.2.1  bouyer 			idetim |= piix_setup_idetim_timings(
    813  1.6.2.1  bouyer 			    mode[1], 0, channel);
    814  1.6.2.1  bouyer end:		/*
    815  1.6.2.1  bouyer 		 * timing mode is now set up in the controller. Enable
    816  1.6.2.1  bouyer 		 * it per-drive
    817  1.6.2.1  bouyer 		 */
    818  1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    819  1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    820  1.6.2.1  bouyer 				idetim = PIIX_IDETIM_SET(idetim,
    821  1.6.2.1  bouyer 				    PIIX_IDETIM_DTE(drive), channel);
    822  1.6.2.1  bouyer 				idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    823  1.6.2.1  bouyer 				drvp[drive].DMA_mode = mode[drive];
    824  1.6.2.1  bouyer 				drvp[drive].PIO_mode = 0;
    825  1.6.2.1  bouyer 				printf("%s:%d:%d: using DMA mode %d\n",
    826  1.6.2.1  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    827  1.6.2.1  bouyer 				    channel, drive, mode[drive]);
    828  1.6.2.1  bouyer 			} else {
    829  1.6.2.1  bouyer 				if (mode[drive] > 0)
    830  1.6.2.1  bouyer 					idetim |= piix_setup_idetim_drvs(
    831  1.6.2.1  bouyer 					    mode[drive], channel, drive);
    832  1.6.2.1  bouyer 				drvp[drive].PIO_mode = mode[drive];
    833  1.6.2.1  bouyer 				printf("%s:%d:%d: using PIO mode %d\n",
    834  1.6.2.1  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    835  1.6.2.1  bouyer 				    channel, drive, mode[drive]);
    836  1.6.2.1  bouyer 			}
    837  1.6.2.1  bouyer 		}
    838  1.6.2.1  bouyer 		if (idedma_ctl != 0) {
    839  1.6.2.1  bouyer 			/* Add software bits in status register */
    840  1.6.2.1  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    841  1.6.2.1  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    842  1.6.2.1  bouyer 			    idedma_ctl);
    843  1.6.2.1  bouyer 		}
    844  1.6.2.1  bouyer 	}
    845  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
    846  1.6.2.1  bouyer 	    idetim, sidetim), DEBUG_PROBE);
    847  1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    848  1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    849  1.6.2.1  bouyer }
    850  1.6.2.1  bouyer 
    851  1.6.2.1  bouyer void
    852  1.6.2.1  bouyer piix3_4_setup_chip(sc, pc, tag)
    853  1.6.2.1  bouyer 	struct pciide_softc *sc;
    854  1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    855  1.6.2.1  bouyer 	pcitag_t tag;
    856  1.6.2.1  bouyer {
    857  1.6.2.1  bouyer 	int channel, drive;
    858  1.6.2.1  bouyer 	struct channel_softc *chp;
    859  1.6.2.1  bouyer 	struct ata_drive_datas *drvp;
    860  1.6.2.1  bouyer 	u_int32_t idetim, sidetim, udmactl, udmatim, idedma_ctl;
    861  1.6.2.1  bouyer 
    862  1.6.2.1  bouyer 	idetim = sidetim = udmactl = udmatim = 0;
    863  1.6.2.1  bouyer 
    864  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    865  1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    866  1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    867  1.6.2.1  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    868  1.6.2.1  bouyer 		chp = &sc->wdc_channels[channel];
    869  1.6.2.1  bouyer 		idedma_ctl = 0;
    870  1.6.2.1  bouyer 		/* Enable IDE registers decode */
    871  1.6.2.1  bouyer 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    872  1.6.2.1  bouyer 		    channel);
    873  1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    874  1.6.2.1  bouyer 			drvp = &chp->ch_drive[drive];
    875  1.6.2.1  bouyer 			/* If no drive, skip */
    876  1.6.2.1  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
    877  1.6.2.1  bouyer 				continue;
    878  1.6.2.1  bouyer 			/* add timing values, setup DMA if needed */
    879  1.6.2.1  bouyer 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    880  1.6.2.1  bouyer 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    881  1.6.2.1  bouyer 			    sc->sc_dma_ok == 0)
    882  1.6.2.1  bouyer 				goto pio;
    883  1.6.2.1  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0)
    884  1.6.2.1  bouyer 			    goto pio; /* Abort DMA setup */
    885  1.6.2.1  bouyer 			drvp->PIO_mode = 0; /* use compatible timings for PIO */
    886  1.6.2.1  bouyer 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
    887  1.6.2.1  bouyer 			    (drvp->drive_flags & DRIVE_UDMA)) {
    888  1.6.2.1  bouyer 				/* use Ultra/DMA */
    889  1.6.2.1  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    890  1.6.2.1  bouyer 				udmactl |= PIIX_UDMACTL_DRV_EN(
    891  1.6.2.1  bouyer 				    channel, drive);
    892  1.6.2.1  bouyer 				udmatim |= PIIX_UDMATIM_SET(
    893  1.6.2.1  bouyer 				    piix4_sct_udma[drvp->UDMA_mode],
    894  1.6.2.1  bouyer 				    channel, drive);
    895  1.6.2.1  bouyer 				printf("%s:%d:%d: using Ultra DMA/33 mode %d\n",
    896  1.6.2.1  bouyer 				     sc->sc_wdcdev.sc_dev.dv_xname,
    897  1.6.2.1  bouyer 				     channel, drive,
    898  1.6.2.1  bouyer 				     drvp->UDMA_mode);
    899  1.6.2.1  bouyer 			} else {
    900  1.6.2.1  bouyer 				/* use Multiword DMA */
    901  1.6.2.1  bouyer 				drvp->drive_flags &= ~DRIVE_UDMA;
    902  1.6.2.1  bouyer 				if (drive == 0) {
    903  1.6.2.1  bouyer 					idetim |= piix_setup_idetim_timings(
    904  1.6.2.1  bouyer 					    drvp->DMA_mode, 1, channel);
    905  1.6.2.1  bouyer 				} else {
    906  1.6.2.1  bouyer 					sidetim |= piix_setup_sidetim_timings(
    907  1.6.2.1  bouyer 						drvp->DMA_mode, 1, channel);
    908  1.6.2.1  bouyer 					idetim =PIIX_IDETIM_SET(idetim,
    909  1.6.2.1  bouyer 					    PIIX_IDETIM_SITRE, channel);
    910  1.6.2.1  bouyer 				}
    911  1.6.2.1  bouyer 				printf("%s:%d:%d: using DMA mode %d\n",
    912  1.6.2.1  bouyer 				     sc->sc_wdcdev.sc_dev.dv_xname,
    913  1.6.2.1  bouyer 				     channel, drive,
    914  1.6.2.1  bouyer 				     drvp->DMA_mode);
    915  1.6.2.1  bouyer 			}
    916  1.6.2.1  bouyer 			/* Enable DMA only PIO modes may be wrong */
    917  1.6.2.1  bouyer 			idetim = PIIX_IDETIM_SET(idetim,
    918  1.6.2.1  bouyer 			    PIIX_IDETIM_DTE(drive), channel);
    919  1.6.2.1  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    920  1.6.2.1  bouyer 			continue;
    921  1.6.2.1  bouyer 
    922  1.6.2.1  bouyer pio:			/* use PIO mode */
    923  1.6.2.1  bouyer 			drvp->drive_flags &= ~DRIVE_DMA | DRIVE_UDMA;
    924  1.6.2.1  bouyer 			if (drive == 0) {
    925  1.6.2.1  bouyer 				idetim |= piix_setup_idetim_timings(
    926  1.6.2.1  bouyer 				    drvp->PIO_mode, 0, channel);
    927  1.6.2.1  bouyer 			} else {
    928  1.6.2.1  bouyer 				sidetim |= piix_setup_sidetim_timings(
    929  1.6.2.1  bouyer 					drvp->PIO_mode, 0, channel);
    930  1.6.2.1  bouyer 				idetim =PIIX_IDETIM_SET(idetim,
    931  1.6.2.1  bouyer 				    PIIX_IDETIM_SITRE, channel);
    932  1.6.2.1  bouyer 			}
    933  1.6.2.1  bouyer 			idetim |= piix_setup_idetim_drvs(drvp->PIO_mode,
    934  1.6.2.1  bouyer 			    channel, drive);
    935  1.6.2.1  bouyer 			printf("%s:%d:%d: using PIO mode %d\n",
    936  1.6.2.1  bouyer 			     sc->sc_wdcdev.sc_dev.dv_xname, channel, drive,
    937  1.6.2.1  bouyer 			     drvp->PIO_mode);
    938  1.6.2.1  bouyer 		}
    939  1.6.2.1  bouyer 		if (idedma_ctl != 0) {
    940  1.6.2.1  bouyer 			/* Add software bits in status register */
    941  1.6.2.1  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    942  1.6.2.1  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    943  1.6.2.1  bouyer 			    idedma_ctl);
    944  1.6.2.1  bouyer 		}
    945  1.6.2.1  bouyer 	}
    946  1.6.2.1  bouyer 
    947  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
    948  1.6.2.1  bouyer 	    idetim, sidetim), DEBUG_PROBE);
    949  1.6.2.1  bouyer 	if (chp->wdc->cap & WDC_CAPABILITY_UDMA) {
    950  1.6.2.1  bouyer 		WDCDEBUG_PRINT((", udmactl=0x%x, udmatim=0x%x", udmactl,
    951  1.6.2.1  bouyer 		    udmatim), DEBUG_PROBE);
    952  1.6.2.1  bouyer 		pci_conf_write(pc, tag, PIIX_UDMACTL, udmactl);
    953  1.6.2.1  bouyer 		pci_conf_write(pc, tag, PIIX_UDMATIM, udmatim);
    954  1.6.2.1  bouyer 	}
    955  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    956  1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    957  1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    958  1.6.2.1  bouyer }
    959  1.6.2.1  bouyer 
    960  1.6.2.1  bouyer /* setup ISP and RTC fields, based on mode */
    961  1.6.2.1  bouyer static u_int32_t
    962  1.6.2.1  bouyer piix_setup_idetim_timings(mode, dma, channel)
    963  1.6.2.1  bouyer 	u_int8_t mode;
    964  1.6.2.1  bouyer 	u_int8_t dma;
    965  1.6.2.1  bouyer 	u_int8_t channel;
    966  1.6.2.1  bouyer {
    967  1.6.2.1  bouyer 
    968  1.6.2.1  bouyer 	if (dma)
    969  1.6.2.1  bouyer 		return PIIX_IDETIM_SET(0,
    970  1.6.2.1  bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
    971  1.6.2.1  bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
    972  1.6.2.1  bouyer 		    channel);
    973  1.6.2.1  bouyer 	else
    974  1.6.2.1  bouyer 		return PIIX_IDETIM_SET(0,
    975  1.6.2.1  bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
    976  1.6.2.1  bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
    977  1.6.2.1  bouyer 		    channel);
    978  1.6.2.1  bouyer }
    979  1.6.2.1  bouyer 
    980  1.6.2.1  bouyer /* setup PPE, IE and TIME1 field based on PIO mode */
    981  1.6.2.1  bouyer static u_int32_t
    982  1.6.2.1  bouyer piix_setup_idetim_drvs(mode, channel, drive)
    983  1.6.2.1  bouyer 	u_int8_t mode;
    984  1.6.2.1  bouyer 	u_int8_t channel;
    985  1.6.2.1  bouyer 	u_int8_t drive;
    986  1.6.2.1  bouyer {
    987  1.6.2.1  bouyer 	u_int32_t ret = 0;
    988  1.6.2.1  bouyer 
    989  1.6.2.1  bouyer 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
    990  1.6.2.1  bouyer 	/* I didn't read anything about this, it's just a guess */
    991  1.6.2.1  bouyer 	if (mode >= 2)
    992  1.6.2.1  bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
    993  1.6.2.1  bouyer 	if (mode >= 3)
    994  1.6.2.1  bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
    995  1.6.2.1  bouyer 	return ret;
    996  1.6.2.1  bouyer }
    997  1.6.2.1  bouyer 
    998  1.6.2.1  bouyer /* setup values in SIDETIM registers, based on mode */
    999  1.6.2.1  bouyer static u_int32_t
   1000  1.6.2.1  bouyer piix_setup_sidetim_timings(mode, dma, channel)
   1001  1.6.2.1  bouyer 	u_int8_t mode;
   1002  1.6.2.1  bouyer 	u_int8_t dma;
   1003  1.6.2.1  bouyer 	u_int8_t channel;
   1004  1.6.2.1  bouyer {
   1005  1.6.2.1  bouyer 	if (dma)
   1006  1.6.2.1  bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1007  1.6.2.1  bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1008  1.6.2.1  bouyer 	else
   1009  1.6.2.1  bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1010  1.6.2.1  bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1011  1.6.2.1  bouyer }
   1012  1.6.2.1  bouyer 
   1013  1.6.2.1  bouyer 
   1014  1.6.2.1  bouyer 
   1015  1.6.2.1  bouyer int
   1016  1.6.2.1  bouyer pciide_dma_table_setup(sc, channel, drive)
   1017  1.6.2.1  bouyer 	struct pciide_softc *sc;
   1018  1.6.2.1  bouyer 	int channel, drive;
   1019  1.6.2.1  bouyer {
   1020  1.6.2.1  bouyer 	bus_dma_segment_t seg;
   1021  1.6.2.1  bouyer 	int error, rseg;
   1022  1.6.2.1  bouyer 	const bus_size_t dma_table_size =
   1023  1.6.2.1  bouyer 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
   1024  1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1025  1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1026  1.6.2.1  bouyer 
   1027  1.6.2.1  bouyer 	/* Allocate memory for the DMA tables and map it */
   1028  1.6.2.1  bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
   1029  1.6.2.1  bouyer 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
   1030  1.6.2.1  bouyer 	    BUS_DMA_NOWAIT)) != 0) {
   1031  1.6.2.1  bouyer 		printf("%s:%d: unable to allocate table DMA for"
   1032  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1033  1.6.2.1  bouyer 		    channel, drive, error);
   1034  1.6.2.1  bouyer 		return error;
   1035  1.6.2.1  bouyer 	}
   1036  1.6.2.1  bouyer 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1037  1.6.2.1  bouyer 	    dma_table_size,
   1038  1.6.2.1  bouyer 	    (caddr_t *)&dma_maps->dma_table,
   1039  1.6.2.1  bouyer 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1040  1.6.2.1  bouyer 		printf("%s:%d: unable to map table DMA for"
   1041  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1042  1.6.2.1  bouyer 		    channel, drive, error);
   1043  1.6.2.1  bouyer 		return error;
   1044  1.6.2.1  bouyer 	}
   1045  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
   1046  1.6.2.1  bouyer 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
   1047  1.6.2.1  bouyer 	    seg.ds_addr), DEBUG_PROBE);
   1048  1.6.2.1  bouyer 
   1049  1.6.2.1  bouyer 	/* Create and load table DMA map for this disk */
   1050  1.6.2.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
   1051  1.6.2.1  bouyer 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
   1052  1.6.2.1  bouyer 	    &dma_maps->dmamap_table)) != 0) {
   1053  1.6.2.1  bouyer 		printf("%s:%d: unable to create table DMA map for"
   1054  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1055  1.6.2.1  bouyer 		    channel, drive, error);
   1056  1.6.2.1  bouyer 		return error;
   1057  1.6.2.1  bouyer 	}
   1058  1.6.2.1  bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat,
   1059  1.6.2.1  bouyer 	    dma_maps->dmamap_table,
   1060  1.6.2.1  bouyer 	    dma_maps->dma_table,
   1061  1.6.2.1  bouyer 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1062  1.6.2.1  bouyer 		printf("%s:%d: unable to load table DMA map for"
   1063  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1064  1.6.2.1  bouyer 		    channel, drive, error);
   1065  1.6.2.1  bouyer 		return error;
   1066  1.6.2.1  bouyer 	}
   1067  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
   1068  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
   1069  1.6.2.1  bouyer 	/* Create a xfer DMA map for this drive */
   1070  1.6.2.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
   1071  1.6.2.1  bouyer 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
   1072  1.6.2.1  bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1073  1.6.2.1  bouyer 	    &dma_maps->dmamap_xfer)) != 0) {
   1074  1.6.2.1  bouyer 		printf("%s:%d: unable to create xfer DMA map for"
   1075  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1076  1.6.2.1  bouyer 		    channel, drive, error);
   1077  1.6.2.1  bouyer 		return error;
   1078  1.6.2.1  bouyer 	}
   1079  1.6.2.1  bouyer 	return 0;
   1080  1.6.2.1  bouyer }
   1081  1.6.2.1  bouyer 
   1082  1.6.2.1  bouyer int
   1083  1.6.2.1  bouyer pciide_dma_init(v, channel, drive, databuf, datalen, read)
   1084  1.6.2.1  bouyer 	void *v;
   1085  1.6.2.1  bouyer 	int channel, drive;
   1086  1.6.2.1  bouyer 	void *databuf;
   1087  1.6.2.1  bouyer 	size_t datalen;
   1088  1.6.2.1  bouyer 	int read;
   1089  1.6.2.1  bouyer {
   1090  1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1091  1.6.2.1  bouyer 	int error, seg;
   1092  1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1093  1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1094  1.6.2.1  bouyer 
   1095  1.6.2.1  bouyer 	error = bus_dmamap_load(sc->sc_dmat,
   1096  1.6.2.1  bouyer 	    dma_maps->dmamap_xfer,
   1097  1.6.2.1  bouyer 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
   1098  1.6.2.1  bouyer 	if (error) {
   1099  1.6.2.1  bouyer 		printf("%s:%d: unable to load xfer DMA map for"
   1100  1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1101  1.6.2.1  bouyer 		    channel, drive, error);
   1102  1.6.2.1  bouyer 		return error;
   1103  1.6.2.1  bouyer 	}
   1104  1.6.2.1  bouyer 
   1105  1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1106  1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
   1107  1.6.2.1  bouyer 	    (read) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1108  1.6.2.1  bouyer 
   1109  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
   1110  1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
   1111  1.6.2.1  bouyer 	    vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
   1112  1.6.2.1  bouyer 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
   1113  1.6.2.1  bouyer #ifdef DIAGNOSTIC
   1114  1.6.2.1  bouyer 		/* A segment must not cross a 64k boundary */
   1115  1.6.2.1  bouyer 		{
   1116  1.6.2.1  bouyer 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1117  1.6.2.1  bouyer 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
   1118  1.6.2.1  bouyer 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
   1119  1.6.2.1  bouyer 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
   1120  1.6.2.1  bouyer 			printf("pciide_dma: segment %d physical addr 0x%lx"
   1121  1.6.2.1  bouyer 			    " len 0x%lx not properly aligned\n",
   1122  1.6.2.1  bouyer 			    seg, phys, len);
   1123  1.6.2.1  bouyer 			panic("pciide_dma: buf align");
   1124  1.6.2.1  bouyer 		}
   1125  1.6.2.1  bouyer 		}
   1126  1.6.2.1  bouyer #endif
   1127  1.6.2.1  bouyer 		dma_maps->dma_table[seg].base_addr =
   1128  1.6.2.1  bouyer 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1129  1.6.2.1  bouyer 		dma_maps->dma_table[seg].byte_count =
   1130  1.6.2.1  bouyer 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
   1131  1.6.2.1  bouyer 		    IDEDMA_BYTE_COUNT_MASK;
   1132  1.6.2.1  bouyer 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
   1133  1.6.2.1  bouyer 		   seg, dma_maps->dma_table[seg].byte_count,
   1134  1.6.2.1  bouyer 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
   1135  1.6.2.1  bouyer 
   1136  1.6.2.1  bouyer 	}
   1137  1.6.2.1  bouyer 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
   1138  1.6.2.1  bouyer 		IDEDMA_BYTE_COUNT_EOT;
   1139  1.6.2.1  bouyer 
   1140  1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
   1141  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_mapsize,
   1142  1.6.2.1  bouyer 	    BUS_DMASYNC_PREWRITE);
   1143  1.6.2.1  bouyer 
   1144  1.6.2.1  bouyer 	/* Maps are ready. Start DMA function */
   1145  1.6.2.1  bouyer #ifdef DIAGNOSTIC
   1146  1.6.2.1  bouyer 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
   1147  1.6.2.1  bouyer 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
   1148  1.6.2.1  bouyer 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1149  1.6.2.1  bouyer 		panic("pciide_dma_init: table align");
   1150  1.6.2.1  bouyer 	}
   1151  1.6.2.1  bouyer #endif
   1152  1.6.2.1  bouyer 
   1153  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
   1154  1.6.2.1  bouyer 	    "phys 0x%x)\n",
   1155  1.6.2.1  bouyer 	    dma_maps->dma_table,
   1156  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr,
   1157  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_len,
   1158  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_nsegs,
   1159  1.6.2.1  bouyer 	    vtophys(dma_maps->dma_table)), DEBUG_DMA);
   1160  1.6.2.1  bouyer 	/* Clear status bits */
   1161  1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1162  1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1163  1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1164  1.6.2.1  bouyer 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
   1165  1.6.2.1  bouyer 	/* Write table addr */
   1166  1.6.2.1  bouyer 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   1167  1.6.2.1  bouyer 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
   1168  1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1169  1.6.2.1  bouyer 	/* set read/write */
   1170  1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1171  1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1172  1.6.2.1  bouyer 	    (read) ? IDEDMA_CMD_WRITE: 0);
   1173  1.6.2.1  bouyer 	return 0;
   1174  1.6.2.1  bouyer }
   1175  1.6.2.1  bouyer 
   1176  1.6.2.1  bouyer void
   1177  1.6.2.1  bouyer pciide_dma_start(v, channel, drive, read)
   1178  1.6.2.1  bouyer 	void *v;
   1179  1.6.2.1  bouyer 	int channel, drive;
   1180  1.6.2.1  bouyer {
   1181  1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1182  1.6.2.1  bouyer 
   1183  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
   1184  1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1185  1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1186  1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1187  1.6.2.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
   1188  1.6.2.1  bouyer }
   1189  1.6.2.1  bouyer 
   1190  1.6.2.1  bouyer int
   1191  1.6.2.1  bouyer pciide_dma_finish(v, channel, drive, read)
   1192  1.6.2.1  bouyer 	void *v;
   1193  1.6.2.1  bouyer 	int channel, drive;
   1194  1.6.2.1  bouyer 	int read;
   1195  1.6.2.1  bouyer {
   1196  1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1197  1.6.2.1  bouyer 	u_int8_t status;
   1198  1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1199  1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1200  1.6.2.1  bouyer 
   1201  1.6.2.1  bouyer 	/* Unload the map of the data buffer */
   1202  1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1203  1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
   1204  1.6.2.1  bouyer 	    (read) ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1205  1.6.2.1  bouyer 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
   1206  1.6.2.1  bouyer 
   1207  1.6.2.1  bouyer 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1208  1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
   1209  1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
   1210  1.6.2.1  bouyer 	    DEBUG_XFERS);
   1211  1.6.2.1  bouyer 
   1212  1.6.2.1  bouyer 	/* stop DMA channel */
   1213  1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1214  1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1215  1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1216  1.6.2.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
   1217  1.6.2.1  bouyer 
   1218  1.6.2.1  bouyer 	/* Clear status bits */
   1219  1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1220  1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1221  1.6.2.1  bouyer 	    status);
   1222  1.6.2.1  bouyer 
   1223  1.6.2.1  bouyer 	if ((status & (IDEDMA_CTL_INTR | IDEDMA_CTL_ERR | IDEDMA_CTL_ACT)) !=
   1224  1.6.2.1  bouyer 	    IDEDMA_CTL_INTR) {
   1225  1.6.2.1  bouyer 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
   1226  1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
   1227  1.6.2.1  bouyer 		return 1;
   1228  1.6.2.1  bouyer 	}
   1229  1.6.2.1  bouyer 	return 0;
   1230      1.1     cgd }
   1231